-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.php
More file actions
executable file
·589 lines (474 loc) · 13.2 KB
/
api.php
File metadata and controls
executable file
·589 lines (474 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<?php
/**
* Api functions
*
* @package SW Adaptive Content Management
* @subpackage Api functions
* @copyright Studio Wolf
* @license Studio Wolf
* @since 1.0
* @todo all functional that accept an ID should also accept a slug
*/
/**
* Get single field from meta data
*
* @param $field the field to get
* @param int $post_id post_id
* @param boolean $repeater_field, is the field an repeater field?
* @return return the field value or the repeater array
* @since 1.3.1
*/
function sw_get_field($field, $post_id = null, $repeater_field = false)
{
if(!$post_id) $post_id = get_the_id();
/*
* Override for preview
*
* If the $_GET['preview_id'] is set, then the user wants to see the preview data.
* There is also the case of previewing a page with post_id = 1, but using get_field
* to load data from another post_id.
* In this case, we need to make sure that the autosave revision is actually related
* to the $post_id variable. If they match, then the autosave data will be used, otherwise,
* the user wants to load data from a completely different post_id
*/
if(isset($_GET['preview_id'])) {
$autosave = wp_get_post_autosave($_GET['preview_id']);
if($autosave->post_parent == $post_id) {
$post_id = intval($autosave->ID);
}
}
// If not a repeater field, fetch the data directly from get_post_meta
if(!$repeater_field) {
return get_post_meta($post_id, $field, true);
} else {
if($amount = get_post_meta($post_id, $field, true)) {
$objects = array(); $i = 0;
// Loop all meta data to check the pattern
foreach(get_post_meta($post_id) as $key => $value) {
preg_match('/^' . $field . '_(0|[1-9][0-9]*)_(.*)/', $key, $matches);
// If there is a match build the array
// object[item order][element name] = [value]
if($matches) {
$objects[$matches[1]][$matches[2]] = $value[0];
}
}
ksort($objects);
return $objects;
}
return false;
}
}
/**
* Get the navigation title
*
* @param int $sw_post_id post_id
* @return string short title
* @since 1.0
*/
function sw_get_title($reference = null)
{
if($reference) {
// Check if a locked reference exists
$post_id = reference_lookup($reference);
// If no result is fount and the $reference is an ID, then look further
if(!$post_id && is_numeric($reference)) $post_id = $reference;
return get_the_title($post_id);
} else {
return get_the_title();
}
}
/**
* Get the headline
*
* @param int $sw_post_id post_id
* @return string headline, or short title if headline does not exist
* @since 1.0
*/
function sw_get_headline($sw_post_id = null)
{
if(!$sw_headline = sw_get_field('headline', $sw_post_id)) {
$sw_headline = sw_get_title($sw_post_id);
}
return $sw_headline;
}
/**
* Get the sub headline
*
* @param int $sw_post_id post_id
* @return string sub headline or false if not existant
* @since 1.0
*/
function sw_get_sub_headline($sw_post_id = null)
{
if($sw_sub_headline = sw_get_field('sub_headline', $sw_post_id)) {
return $sw_sub_headline;
}
return false;
}
/**
* Get the window title
*
* @param int $sw_post_id post_id
* @return string the headline combined with the blog name
* @since 1.0
*/
function sw_get_window_title($post_id = null)
{
if(is_search()) {
$title = ucfirst(get_search_query());
} elseif(is_404()) {
$title = '404';
} elseif(is_author()) {
global $wp_query;
$user = $wp_query->get_queried_object();
$title = sw_get_user_field('first_name', $user) . ' ' . sw_get_user_field('last_name', $user);
} else {
if(!$post_id) $post_id = get_the_id();
$title = sw_get_headline($post_id);
}
return strip_tags($title) . " - " . sw_get_option_field('organisation');
}
/**
* Get mini teaser
*
* @param int $sw_post_id post_id
* @return string mini teaser or false if not existant
* @since 1.0
*/
function sw_get_mini_teaser($reference = null)
{
if($reference){
$post_id = reference_lookup($reference);
if(!$post_id && is_numeric($reference)) $post_id = $reference;
if($sw_mini_teaser = sw_get_field('mini_teaser', $post_id)) {
return $sw_mini_teaser;
}
} else {
if($sw_mini_teaser = sw_get_field('mini_teaser', $reference)) {
return $sw_mini_teaser;
}
return false;
}
}
/**
* Get the teaser
*
* @param int $sw_post_id post_id
* @return string teaser or false if not existant
* @since 1.0
*/
function sw_get_teaser($sw_post_id = null)
{
if($sw_teaser = sw_get_field('teaser', $sw_post_id)) {
return $sw_teaser;
}
return false;
}
/**
* Get page or post link
*
* @param int $sw_post_id post_id
* @return string the page link
* @since 1.0
*/
function sw_get_link($reference = null)
{
// Check if a locked reference exists
$post_id = reference_lookup($reference);
// If no result is fount and the $reference is an ID, then look further
if(!$post_id && is_int($reference)) $post_id = $reference;
return get_page_link($post_id);
}
/**
* Get the meta descriptions
*
* @param int $sw_post_id post_id
* @return string meta description, which is actually the mini teaser
* @since 1.0
*/
function sw_get_metadescription()
{
if(!is_author() && !is_404()) {
return sw_get_mini_teaser();
} else {
return "";
}
}
/**
* Get images beloning to a post
*
* @param int $sw_post_id post_id
* @param int $amount the number of images to fetch
* @return array of images
* @since 1.0
*/
function sw_get_images($post_id = null, $amount = false)
{
if($image_ids = sw_get_field('images', $post_id)) {
return $image_ids;
}
return false;
}
/**
* Get the thumbnail
*
* @param int $post_id post_id
* @return array image object
* @since 1.6
*/
function sw_get_thumbnail($post_id = null)
{
if(!$thumbnail = sw_get_field('thumbnail', $post_id)) {
$thumbnail = sw_get_first_image($post_id, false);
}
return $thumbnail;
}
/**
* Get the first image
*
* @param int $post_id post_id
* @return array image object
* @since 1.0
*/
function sw_get_first_image($post_id = null, $raiseIndex = false)
{
if($image_ids = sw_get_images($post_id)) {
if($raiseIndex) {
SWShortTags::$image_index++;
}
return array_shift($image_ids);
}
return false;
}
/**
* Get the file objects of the files belonging to a post
*
* @param int $post_id the id of the post to get the files from
* @param int $amount the amount of files to fetch
* @return array of file objects
*/
function sw_get_files($post_id = null, $amount = false)
{
if($files = sw_get_field('files', $post_id, true)) {
$objects = array();
foreach($files as $file) {
$objects[] = get_post( $file['file'] );
}
return $objects;
}
}
function sw_get_videos($post_id = null, $amount = false)
{
if($video_objects = sw_get_field('videos', $post_id, true)) {
return $video_objects;
}
return false;
}
/**
* Get the reference of a given page id
*
* @param int $sw_post_id post_id
* @return string reference or false if not found
* @since 1.2
*/
function sw_get_reference($sw_post_id = null)
{
$locked_pages = get_option('locked_pages', array());
global $post;
if(!$sw_post_id) $sw_post_id = $post->ID;
// Find the reference by a page ID
if(isset($locked_pages[$sw_post_id])) {
return $locked_pages[$sw_post_id];
} else {
return false;
}
}
/**
* Get the reference of a given page id
*
* @param int $term_id the term id
* @return string reference or false if not found
* @since 1.2
*/
function sw_get_term_reference($term_id)
{
$locked_terms = get_option('locked_terms', array());
// Find the reference by a page ID
if(isset($locked_terms[$term_id])) {
return $locked_terms[$term_id];
} else {
return false;
}
}
/**
* Get page object by reference code
*
* @param string $reference the reference to match
* @return page object
* @since 1.2
*/
function sw_get_page_by_reference($reference)
{
// Check if a locked reference exists
$post_id = sw_reference_lookup($reference);
// If no result is fount and the $reference is an ID, then look further
if(!$post_id && is_int($reference)) $post_id = $reference;
return get_page($post_id);
}
/**
* Get term object by reference code
*
* @param string $reference the reference to match
* @return term id
* @since 1.8
*/
function sw_get_term_id_by_reference($reference)
{
// Check if a locked reference exists
$term_id = sw_term_reference_lookup($reference);
// If no result is fount and the $reference is an ID, then look further
if(!$term_id && is_int($reference)) $term_id = $reference;
return get_term($term_id);
}
/**
* Conditional tag to check if the current page matches the reference
*
* @param string $reference the reference to match or array with references
* @return boolean
* @since 1.3.2
*/
function sw_is_reference_page($references)
{
// Check if a locked reference exists
$locked_pages = get_option('locked_pages');
// Check if $references is an array, otherwise convert to array
if(!is_array($references)) {
$references = array($references);
}
// Loop all references to find the matching ids
$ids = array();
foreach($references as $reference) {
$ids[] = array_search($reference, $locked_pages);
}
if(in_array(get_the_ID(), $ids)) return true;
return false;
}
/**
* Get first video
*
* @param int $sw_post_id post_id
* @return array video object
* @since 1.0
*/
function sw_get_first_video($post_id = null)
{
if(!$post_id) $post_id = get_the_id();
if($video_objects = sw_get_field('videos', $post_id, true)) {
SWShortTags::$embed_index++;
return array_shift($video_objects);
}
return false;
}
/**
* Get option field value
*
* @param string $field the field to fetch
* @param $repeater_field is the field an repeater field?
* @return string the content of the option or array if repeater field
* @since 1.0
*/
function sw_get_option_field($field, $repeater_field = false)
{
if(!$repeater_field) {
return get_option('options_' . $field);
} else {
$objects = array(); $i = 0;
// Loop all meta data to check the pattern
foreach(wp_load_alloptions() as $key => $value) {
preg_match('/^options_' . $field . '_(0|[1-9][0-9]*)_(.*)/', $key, $matches);
// If there is a match build the array
// object[item order][element name] = [value]
if($matches) {
$objects[$matches[1]][$matches[2]] = $value;
}
}
ksort($objects);
return $objects;
}
}
/**
* Get a user field
*
* @param string $field the field to fetch
* @param User Object or ID of the user
* @return string the content of the field
* @since 1.0
*/
function sw_get_user_field($field, $user = false)
{
// If $user is false, then get current post user
if(!$user) {
global $post;
$user = $post->post_author;
// Return false if no user is found
if(!$user) return false;
}
// Check the $user is an ID or object
if(is_numeric($user)) {
//return get_field($field, 'user_' . $user);
return get_user_meta($user, $field, true);
} else {
//return get_field($field, 'user_' . $user->ID);
return get_user_meta($user->ID, $field, true);
}
}
/**
* Get the e-mailadres of an user
*
* @param User Object $user
* @return Email address
* @since 1.0
*/
function sw_get_user_email($user)
{
return $user->user_email;
}
/**
* Lookup post_id by reference
*
* @param string $reference the reference to lookup
* @return post_id if found or false, of not found
* @since 1.5
*/
function sw_reference_lookup($reference)
{
// Read the locked_pages option
$locked_pages = get_option('locked_pages', array());
// Check if $reference is inside the locked_pages array
$post_id = array_search($reference, $locked_pages);
// Apply filter to allow plugins to hook in
$post_id = apply_filters('sw_acm_reference_lookup', $post_id, $reference);
return $post_id;
}
function sw_term_reference_lookup($reference)
{
// Read the locked_pages option
$locked_terms = get_option('locked_terms', array());
// Check if $reference is inside the locked_pages array
$term_id = array_search($reference, $locked_terms);
// Apply filter to allow plugins to hook in
$term_id = apply_filters('sw_acm_term_reference_lookup', $term_id, $reference);
return $term_id;
}
function reference_lookup($reference)
{
return sw_reference_lookup($reference);
}
function sw_get_embed_index($post_id = null)
{
// Get the current embed index and raise by one
}
function sw_get_image_index($post_id = null)
{
// Get the current image index and raise by one
}