forked from zeen101/issuem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
issuem-functions.php
538 lines (349 loc) · 11.8 KB
/
issuem-functions.php
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
<?php
/**
* Misc helper functions for IssueM
*
* @package IssueM
* @since 1.0.0
*/
if ( !function_exists( 'get_newest_issuem_issue_id' ) ) {
/**
* Get newest IssueM issue
*
* @since 1.0.0
*
* @param string $orderby
* @return int $id
*/
function get_newest_issuem_issue_id( $orderby = 'issue_order' ) {
$issues = array();
$issuem_issues = get_terms( 'issuem_issue' );
foreach ( $issuem_issues as $issue ) {
$issue_meta = get_option( 'issuem_issue_' . $issue->term_id . '_meta' );
// If issue is not a Draft, add it to the archive array;
if ( 'Draft' !== $issue_meta['issue_status'] ) {
switch( $orderby ) {
case "issue_order":
if ( !empty( $issue_meta['issue_order'] ) )
$issues[ $issue_meta['issue_order'] ] = $issue->term_id;
else
$issues_no_issue_order[] = $issue->term_id;
break;
case "name":
$issues[ $issue_meta['name'] ] = $issue->term_id;
break;
case "term_id":
$issues[ $issue->term_id ] = $issue->term_id;
break;
}
}
}
if ( 'issue_order' == $orderby && !empty( $issues_no_issue_order ) )
$issues = array_merge( $issues_no_issue_order, $issues );
krsort( $issues );
return array_shift( $issues );
}
}
if ( !function_exists( 'get_issuem_issue_meta' ) ) {
/**
* Get issue meta information, assumes latest issue if no id supplied
*
* @since 1.0.0
*
* @param int $id Issue ID
* @return mixed Value set for the issue meta option.
*/
function get_issuem_issue_meta( $id = false ) {
if ( !$id ) {
return get_option( 'issuem_issue_' . get_newest_issuem_issue_id() . '_meta' );
} else {
return get_option( 'issuem_issue_' . $id . '_meta' );
}
}
}
if ( !function_exists( 'get_issuem_issue_cover' ) ) {
/**
* Get issue cover image, assumes latest issue if no id supplied
*
* @since 1.0.0
*
* @param int $id Issue ID
* @return string URL of cover image
*/
function get_issuem_issue_cover( $id = false ) {
if ( !$id ) {
$issue_meta = get_option( 'issuem_issue_' . get_newest_issuem_issue_id() . '_meta' );
return $issue_meta['cover_image'];
} else {
$issue_meta = get_option( 'issuem_issue_' . $id . '_meta' );
return $issue_meta['cover_image'];
}
}
}
if ( !function_exists( 'get_issuem_issue_slug' ) ) {
/**
* Get issue slug, assumes latest issue if no id supplied
*
* @since 1.0.0
*
* @param int $id Issue ID
* @return string issue slug
*/
function get_issuem_issue_slug( $id = false ) {
if ( !$id ) {
$issue = get_term_by( 'id', get_newest_issuem_issue_id(), 'issuem_issue' );
return $issue->slug;
} else {
$issue = get_term_by( 'id', $id, 'issuem_issue' );
return $issue->slug;
}
}
}
if ( !function_exists( 'get_issuem_issue_title' ) ) {
/**
* Get issue title, assumes latest issue if no id supplied
*
* @since 1.0.0
*
* @param int $id Issue ID
* @return string issue name
*/
function get_issuem_issue_title( $id = false ) {
if ( !$id ) {
$issue = get_term_by( 'id', get_newest_issuem_issue_id(), 'issuem_issue' );
return $issue->name;
} else {
$issue = get_term_by( 'id', $id, 'issuem_issue' );
return $issue->name;
}
}
}
if ( !function_exists( 'get_active_issuem_issue' ) ) {
/**
* Gets active issue, set by latest issue or by cookie if user selects a specific issue
*
* @since 1.0.0
*
* @return string issue slug
*/
function get_active_issuem_issue() {
if ( isset( $_COOKIE['issuem_issue'] ) )
return $_COOKIE['issuem_issue'];
else if ( isset( $_GET['issue'] ) )
return $_GET['issue'];
else
return get_issuem_issue_slug();
}
}
if ( !function_exists( 'set_issuem_cookie' ) ) {
/**
* Sets IssueM issue cookie
*
* @since 1.0.0
*/
function set_issuem_cookie() {
if ( isset( $_GET['issue'] ) ) {
$_COOKIE['issuem_issue'] = $_GET['issue'];
setcookie( 'issuem_issue', $_GET['issue'], time() + 3600, '/' );
} else {
global $wp_query;
$issuem_settings = get_issuem_settings();
if ( is_page( $issuem_settings['page_for_articles'] ) ) {
$_COOKIE['issuem_issue'] = get_issuem_issue_slug();
setcookie( 'issuem_issue', $_COOKIE['issuem_issue'], time() + 3600, '/' );
} else if ( isset( $wp_query->post_type ) && 'article' != $wp_query->post_type ) {
unset( $_COOKIE['issuem_issue'] );
setcookie( 'issuem_issue', '', 1, '/' );
}
}
}
add_action( 'wp', 'set_issuem_cookie' );
}
if ( !function_exists( 'issuem_replacements_args' ) ) {
/**
* Replaces variables with WordPress content
*
* @since 1.0.0
*
* @param int $id User ID
*/
function issuem_replacements_args( $string, $post ) {
$issuem_settings = get_issuem_settings();
if ( isset( $issuem_settings['use_wp_taxonomies'] ) && !empty( $issuem_settings['use_wp_taxonomies'] ) ) {
$tags = 'post_tag';
$cats = 'category';
} else {
$tags = 'issuem_issue_tags';
$cats = 'issuem_issue_categories';
}
$string = str_ireplace( '%TITLE%', get_the_title(), $string );
$string = str_ireplace( '%URL%', apply_filters( 'issuem_article_url', get_permalink( $post->ID ), $post->ID ), $string );
if ( preg_match( '/%CATEGORY\[?(\d*)\]?%/i', $string, $matches ) ) {
$post_cats = get_the_terms( $post->ID, $cats );
$categories = '';
if ( $post_cats && !is_wp_error( $post_cats ) ) :
if ( !empty( $matches[1] ) )
$max_cats = $matches[1];
else
$max_cats = 0;
$cat_array = array();
$count = 1;
foreach ( $post_cats as $post_cat ) {
$cat_array[] = $post_cat->name;
if ( 0 != $max_cats && $max_cats <= $count )
break;
$count++;
}
$categories = join( ", ", $cat_array );
endif;
$string = preg_replace( '/%CATEGORY\[?(\d*)\]?%/i', $categories, $string );
}
if ( preg_match( '/%TAG\[?(\d*)\]?%/i', $string, $matches ) ) {
$post_tags = get_the_terms( $post->ID, $tags );
$tag_string = '';
if ( $post_tags && !is_wp_error( $post_tags ) ) :
if ( !empty( $matches[1] ) )
$max_tags = $matches[1];
else
$max_tags = 0;
$cat_array = array();
$count = 1;
foreach ( $post_tags as $post_tag ) {
$cat_array[] = $post_tag->name;
if ( 0 != $max_tags && $max_tags <= $count )
break;
$count++;
}
$tag_string = join( ", ", $cat_array );
endif;
$string = preg_replace( '/%TAG\[?(\d*)\]?%/i', $tag_string, $string );
}
if ( preg_match( '/%TEASER%/i', $string, $matches ) ) {
if ( $teaser = get_post_meta( $post->ID, '_teaser_text', true ) )
$string = preg_replace( '/%TEASER%/i', $teaser, $string );
else
$string = preg_replace( '/%TEASER%/i', '%EXCERPT%', $string ); // If no Teaser Text exists, try to get an excerpt
}
if ( preg_match( '/%EXCERPT\[?(\d*)\]?%/i', $string, $matches ) ) {
if ( empty( $post->post_excerpt ) )
$excerpt = get_the_content();
else
$excerpt = $post->post_excerpt;
$excerpt = strip_shortcodes( $excerpt );
$excerpt = apply_filters( 'the_content', $excerpt );
$excerpt = str_replace( ']]>', ']]>', $excerpt );
if ( !empty( $matches[1] ) )
$excerpt_length = $matches[1];
else
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
$string = preg_replace( '/%EXCERPT\[?(\d*)\]?%/i', $excerpt, $string );
}
if ( preg_match( '/%CONTENT%/i', $string, $matches ) ) {
$content = get_the_content();
$string = preg_replace( '/%CONTENT%/i', $content, $string );
}
if ( preg_match( '/%BYLINE%/i', $string, $matches ) ) {
if ( isset( $issuem_settings['issuem_author_name'] ) && false !== $issuem_settings['issuem_author_name'] ) {
$author_name = get_post_meta( $post->ID, '_issuem_author_name', true );
} else {
if ( 'user_firstlast' == $issuem_settings['display_byline_as'] )
$author_name = get_the_author_meta( 'user_firstname', $post->post_author ) . ' ' . get_the_author_meta( 'user_lastname', $post->post_author );
else
$author_name = get_the_author_meta( $issuem_settings['display_byline_as'], $post->post_author );
}
$byline = sprintf( __( 'By %s', 'issuem' ), apply_filters( 'issuem_author_name', $author_name, $post->ID ) );
$string = preg_replace( '/%BYLINE%/i', $byline, $string );
}
$string = apply_filters( 'issuem_custom_replacement_args', $string, $post );
return stripcslashes( $string );
}
}
if ( !function_exists( 'get_issuem_settings' ) ) {
/**
* Helper function to get IssueM settings for current site
*
* @since 1.0.0
*
* @return mixed Value set for the issuem options.
*/
function get_issuem_settings() {
global $dl_plugin_issuem;
return $dl_plugin_issuem->get_issuem_settings();
}
}
if ( !function_exists( 'default_issue_content_filter' ) ) {
/**
* Default content filter, sets IssueM Page for Articles to default shortcode content if no content exists for page
*
* @since 1.0.7
*
* @return string new content.
*/
function default_issue_content_filter( $content ) {
global $post;
$issuem_settings = get_issuem_settings();
if ( $post->ID == $issuem_settings['page_for_articles'] && empty( $content ) ) {
$content = '[issuem_featured_rotator] [issuem_featured_thumbnails max_images="3"] [issuem_articles]';
}
return $content;
}
add_filter( 'the_content', 'default_issue_content_filter', 5 );
}
if ( !function_exists( 'issuem_dot_com_rss_feed_check' ) ) {
/**
* Check issuem.com for new RSS items in the buy-news category feed, to update clients of latest IssueM news
*
* @since 1.1.1
*/
function issuem_dot_com_rss_feed_check() {
include_once( ABSPATH . WPINC . '/feed.php' );
$output = '';
$feedurl = 'http://issuem.com/category/buyer-news/feed';
$rss = fetch_feed( $feedurl );
if ( $rss && !is_wp_error( $rss ) ) {
$rss_items = $rss->get_items( 0, 1 );
foreach ( $rss_items as $item ) {
$last_rss_item = get_option( 'last_issuem_dot_com_rss_item' );
$latest_rss_item = '<a href="' . $item->get_permalink() . '" target="_blank">' . esc_html( $item->get_title() ) . '</a> - ' . $item->get_description() . '... <a href="' . $item->get_permalink() . '" target="_blank">read more</a>';
if ( $last_rss_item !== $latest_rss_item )
update_option( 'last_issuem_dot_com_rss_item', $latest_rss_item );
}
}
}
add_action( 'issuem_dot_com_rss_feed_check', 'issuem_dot_com_rss_feed_check' );
if ( !wp_next_scheduled( 'issuem_dot_com_rss_feed_check' ) )
wp_schedule_event( time(), 'daily', 'issuem_dot_com_rss_feed_check' );
}
if ( !function_exists( 'issuem_api_request' ) ) {
/**
* Helper function used to send API requests to IssueM.com
*
* HT: Glenn Ansley @ iThemes.com
*
* @since 1.2.0
*
* @param int $args Arguments to pass to API request
*/
function issuem_api_request( $args ) {
global $dl_plugin_issuem;
return $dl_plugin_issuem->issuem_api_request( $args );
}
}
if ( !function_exists( 'wp_print_r' ) ) {
/**
* Helper function used for printing out debug information
*
* HT: Glenn Ansley @ iThemes.com
*
* @since 1.1.6
*
* @param int $args Arguments to pass to print_r
* @param bool $die TRUE to die else FALSE (default FALSE)
*/
function wp_print_r( $args, $die = false ) {
$echo = '<pre>' . print_r( $args, true ) . '</pre>';
if ( $die ) die( $echo );
else echo $echo;
}
}