1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
if ( ! class_exists( 'Featured_Content' ) && isset( $GLOBALS['pagenow'] ) && 'plugins.php' !== $GLOBALS['pagenow'] ) { |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Featured Content. |
7
|
|
|
* |
8
|
|
|
* This module will allow users to define a subset of posts to be displayed in a |
9
|
|
|
* theme-designated featured content area. |
10
|
|
|
* |
11
|
|
|
* This feature will only be activated for themes that declare that they support |
12
|
|
|
* it. This can be done by adding code similar to the following during the |
13
|
|
|
* "after_setup_theme" action: |
14
|
|
|
* |
15
|
|
|
* add_theme_support( 'featured-content', array( |
16
|
|
|
* 'filter' => 'mytheme_get_featured_content', |
17
|
|
|
* 'max_posts' => 20, |
18
|
|
|
* 'post_types' => array( 'post', 'page' ), |
19
|
|
|
* ) ); |
20
|
|
|
* |
21
|
|
|
* For maximum compatibility with different methods of posting users will |
22
|
|
|
* designate a featured post tag to associate posts with. Since this tag now has |
23
|
|
|
* special meaning beyond that of a normal tags, users will have the ability to |
24
|
|
|
* hide it from the front-end of their site. |
25
|
|
|
*/ |
26
|
|
|
class Featured_Content { |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The maximum number of posts that a Featured Content area can contain. We |
30
|
|
|
* define a default value here but themes can override this by defining a |
31
|
|
|
* "max_posts" entry in the second parameter passed in the call to |
32
|
|
|
* add_theme_support( 'featured-content' ). |
33
|
|
|
* |
34
|
|
|
* @see Featured_Content::init() |
35
|
|
|
*/ |
36
|
|
|
public static $max_posts = 15; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The registered post types supported by Featured Content. Themes can add |
40
|
|
|
* Featured Content support for registered post types by defining a |
41
|
|
|
* 'post_types' argument (string|array) in the call to |
42
|
|
|
* add_theme_support( 'featured-content' ). |
43
|
|
|
* |
44
|
|
|
* @see Featured_Content::init() |
45
|
|
|
*/ |
46
|
|
|
public static $post_types = array( 'post' ); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The tag that is used to mark featured content. Users can define |
50
|
|
|
* a custom tag name that will be stored in this variable. |
51
|
|
|
* |
52
|
|
|
* @see Featured_Content::hide_featured_term |
53
|
|
|
*/ |
54
|
|
|
public static $tag; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Instantiate. |
58
|
|
|
* |
59
|
|
|
* All custom functionality will be hooked into the "init" action. |
60
|
|
|
*/ |
61
|
|
|
public static function setup() { |
62
|
|
|
add_action( 'init', array( __CLASS__, 'init' ), 30 ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Conditionally hook into WordPress. |
67
|
|
|
* |
68
|
|
|
* Themes must declare that they support this module by adding |
69
|
|
|
* add_theme_support( 'featured-content' ); during after_setup_theme. |
70
|
|
|
* |
71
|
|
|
* If no theme support is found there is no need to hook into WordPress. We'll |
72
|
|
|
* just return early instead. |
73
|
|
|
* |
74
|
|
|
* @uses Featured_Content::$max_posts |
75
|
|
|
*/ |
76
|
|
|
public static function init() { |
77
|
|
|
$theme_support = get_theme_support( 'featured-content' ); |
78
|
|
|
|
79
|
|
|
// Return early if theme does not support featured content. |
80
|
|
|
if ( ! $theme_support ) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/* |
85
|
|
|
* An array of named arguments must be passed as the second parameter |
86
|
|
|
* of add_theme_support(). |
87
|
|
|
*/ |
88
|
|
|
if ( ! isset( $theme_support[0] ) ) { |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ( isset( $theme_support[0]['featured_content_filter'] ) ) { |
93
|
|
|
$theme_support[0]['filter'] = $theme_support[0]['featured_content_filter']; |
94
|
|
|
unset( $theme_support[0]['featured_content_filter'] ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Return early if "filter" has not been defined. |
98
|
|
|
if ( ! isset( $theme_support[0]['filter'] ) ) { |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Theme can override the number of max posts. |
103
|
|
|
if ( isset( $theme_support[0]['max_posts'] ) ) { |
104
|
|
|
self::$max_posts = absint( $theme_support[0]['max_posts'] ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
add_filter( $theme_support[0]['filter'], array( __CLASS__, 'get_featured_posts' ) ); |
108
|
|
|
add_action( 'customize_register', array( __CLASS__, 'customize_register' ), 9 ); |
109
|
|
|
add_action( 'admin_init', array( __CLASS__, 'register_setting' ) ); |
110
|
|
|
add_action( 'save_post', array( __CLASS__, 'delete_transient' ) ); |
111
|
|
|
add_action( 'delete_post_tag', array( __CLASS__, 'delete_post_tag' ) ); |
112
|
|
|
add_action( 'customize_controls_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) ); |
113
|
|
|
add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ) ); |
114
|
|
|
add_action( 'switch_theme', array( __CLASS__, 'switch_theme' ) ); |
115
|
|
|
add_action( 'switch_theme', array( __CLASS__, 'delete_transient' ) ); |
116
|
|
|
add_action( 'wp_loaded', array( __CLASS__, 'wp_loaded' ) ); |
117
|
|
|
add_action( 'split_shared_term', array( __CLASS__, 'jetpack_update_featured_content_for_split_terms', 10, 4 ) ); |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
if ( isset( $theme_support[0]['additional_post_types'] ) ) { |
121
|
|
|
$theme_support[0]['post_types'] = array_merge( array( 'post' ), (array) $theme_support[0]['additional_post_types'] ); |
122
|
|
|
unset( $theme_support[0]['additional_post_types'] ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// Themes can allow Featured Content pages |
126
|
|
|
if ( isset( $theme_support[0]['post_types'] ) ) { |
127
|
|
|
self::$post_types = array_merge( self::$post_types, (array) $theme_support[0]['post_types'] ); |
128
|
|
|
|
129
|
|
|
// register post_tag support for each post type |
130
|
|
|
foreach ( self::$post_types as $post_type ) { |
131
|
|
|
register_taxonomy_for_object_type( 'post_tag', $post_type ); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Hide "featured" tag from the front-end. |
138
|
|
|
* |
139
|
|
|
* Has to run on wp_loaded so that the preview filters of the customizer |
140
|
|
|
* have a chance to alter the value. |
141
|
|
|
*/ |
142
|
|
|
public static function wp_loaded() { |
143
|
|
|
if ( self::get_setting( 'hide-tag' ) ) { |
144
|
|
|
$settings = self::get_setting(); |
145
|
|
|
|
146
|
|
|
// This is done before setting filters for get_terms in order to avoid an infinite filter loop |
147
|
|
|
self::$tag = get_term_by( 'name', $settings['tag-name'], 'post_tag' ); |
148
|
|
|
|
149
|
|
|
add_filter( 'get_terms', array( __CLASS__, 'hide_featured_term' ), 10, 3 ); |
150
|
|
|
add_filter( 'get_the_terms', array( __CLASS__, 'hide_the_featured_term' ), 10, 3 ); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get featured posts |
156
|
|
|
* |
157
|
|
|
* This method is not intended to be called directly. Theme developers should |
158
|
|
|
* place a filter directly in their theme and then pass its name as a value of |
159
|
|
|
* the "filter" key in the array passed as the $args parameter during the call |
160
|
|
|
* to: add_theme_support( 'featured-content', $args ). |
161
|
|
|
* |
162
|
|
|
* @uses Featured_Content::get_featured_post_ids() |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
public static function get_featured_posts() { |
167
|
|
|
$post_ids = self::get_featured_post_ids(); |
168
|
|
|
|
169
|
|
|
// No need to query if there is are no featured posts. |
170
|
|
|
if ( empty( $post_ids ) ) { |
171
|
|
|
return array(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$featured_posts = get_posts( array( |
175
|
|
|
'include' => $post_ids, |
176
|
|
|
'posts_per_page' => count( $post_ids ), |
177
|
|
|
'post_type' => self::$post_types, |
178
|
|
|
) ); |
179
|
|
|
|
180
|
|
|
return $featured_posts; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get featured post IDs |
185
|
|
|
* |
186
|
|
|
* This function will return the an array containing the post IDs of all |
187
|
|
|
* featured posts. |
188
|
|
|
* |
189
|
|
|
* Sets the "featured_content_ids" transient. |
190
|
|
|
* |
191
|
|
|
* @return array Array of post IDs. |
192
|
|
|
*/ |
193
|
|
|
public static function get_featured_post_ids() { |
194
|
|
|
// Return array of cached results if they exist. |
195
|
|
|
$featured_ids = get_transient( 'featured_content_ids' ); |
196
|
|
|
if ( ! empty( $featured_ids ) ) { |
197
|
|
|
return array_map( |
198
|
|
|
'absint', |
199
|
|
|
/** |
200
|
|
|
* Filter the list of Featured Posts IDs. |
201
|
|
|
* |
202
|
|
|
* @module theme-tools |
203
|
|
|
* |
204
|
|
|
* @since 2.7.0 |
205
|
|
|
* |
206
|
|
|
* @param array $featured_ids Array of post IDs. |
207
|
|
|
*/ |
208
|
|
|
apply_filters( 'featured_content_post_ids', (array) $featured_ids ) |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$settings = self::get_setting(); |
213
|
|
|
|
214
|
|
|
// Return empty array if no tag name is set. |
215
|
|
|
$term = get_term_by( 'name', $settings['tag-name'], 'post_tag' ); |
216
|
|
|
if ( ! $term ) { |
217
|
|
|
$term = get_term_by( 'id', $settings['tag-id'], 'post_tag' ); |
218
|
|
|
} |
219
|
|
|
if ( $term ) { |
220
|
|
|
$tag = $term->term_id; |
221
|
|
|
} else { |
222
|
|
|
/** This action is documented in modules/theme-tools/featured-content.php */ |
223
|
|
|
return apply_filters( 'featured_content_post_ids', array() ); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
// Back compat for installs that have the quantity option still set. |
227
|
|
|
$quantity = isset( $settings['quantity'] ) ? $settings['quantity'] : self::$max_posts; |
228
|
|
|
|
229
|
|
|
// Query for featured posts. |
230
|
|
|
$featured = get_posts( array( |
231
|
|
|
'numberposts' => $quantity, |
232
|
|
|
'post_type' => self::$post_types, |
233
|
|
|
'tax_query' => array( |
234
|
|
|
array( |
235
|
|
|
'field' => 'term_id', |
236
|
|
|
'taxonomy' => 'post_tag', |
237
|
|
|
'terms' => $tag, |
238
|
|
|
), |
239
|
|
|
), |
240
|
|
|
) ); |
241
|
|
|
|
242
|
|
|
// Return empty array if no featured content exists. |
243
|
|
|
if ( ! $featured ) { |
244
|
|
|
/** This action is documented in modules/theme-tools/featured-content.php */ |
245
|
|
|
return apply_filters( 'featured_content_post_ids', array() ); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
// Ensure correct format before save/return. |
249
|
|
|
$featured_ids = wp_list_pluck( (array) $featured, 'ID' ); |
250
|
|
|
$featured_ids = array_map( 'absint', $featured_ids ); |
251
|
|
|
|
252
|
|
|
set_transient( 'featured_content_ids', $featured_ids ); |
253
|
|
|
|
254
|
|
|
/** This action is documented in modules/theme-tools/featured-content.php */ |
255
|
|
|
return apply_filters( 'featured_content_post_ids', $featured_ids ); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Delete Transient. |
260
|
|
|
* |
261
|
|
|
* Hooks in the "save_post" action. |
262
|
|
|
* @see Featured_Content::validate_settings(). |
263
|
|
|
*/ |
264
|
|
|
public static function delete_transient() { |
265
|
|
|
delete_transient( 'featured_content_ids' ); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Exclude featured posts from the blog query when the blog is the front-page, |
270
|
|
|
* and user has not checked the "Also display tagged posts outside the Featured Content area" checkbox. |
271
|
|
|
* |
272
|
|
|
* Filter the home page posts, and remove any featured post ID's from it. |
273
|
|
|
* Hooked onto the 'pre_get_posts' action, this changes the parameters of the |
274
|
|
|
* query before it gets any posts. |
275
|
|
|
* |
276
|
|
|
* @uses Featured_Content::get_featured_post_ids(); |
277
|
|
|
* @uses Featured_Content::get_setting(); |
278
|
|
|
* @param WP_Query $query |
279
|
|
|
* @return WP_Query Possibly modified WP_Query |
280
|
|
|
*/ |
281
|
|
|
public static function pre_get_posts( $query ) { |
282
|
|
|
|
283
|
|
|
// Bail if not home or not main query. |
284
|
|
|
if ( ! $query->is_home() || ! $query->is_main_query() ) { |
285
|
|
|
return; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
// Bail if the blog page is not the front page. |
289
|
|
|
if ( 'posts' !== get_option( 'show_on_front' ) ) { |
290
|
|
|
return; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
$featured = self::get_featured_post_ids(); |
294
|
|
|
|
295
|
|
|
// Bail if no featured posts. |
296
|
|
|
if ( ! $featured ) { |
|
|
|
|
297
|
|
|
return; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
$settings = self::get_setting(); |
301
|
|
|
|
302
|
|
|
// Bail if the user wants featured posts always displayed. |
303
|
|
|
if ( true == $settings['show-all'] ) { |
304
|
|
|
return; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
// We need to respect post ids already in the blacklist. |
308
|
|
|
$post__not_in = $query->get( 'post__not_in' ); |
309
|
|
|
|
310
|
|
|
if ( ! empty( $post__not_in ) ) { |
311
|
|
|
$featured = array_merge( (array) $post__not_in, $featured ); |
312
|
|
|
$featured = array_unique( $featured ); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
$query->set( 'post__not_in', $featured ); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Reset tag option when the saved tag is deleted. |
320
|
|
|
* |
321
|
|
|
* It's important to mention that the transient needs to be deleted, too. |
322
|
|
|
* While it may not be obvious by looking at the function alone, the transient |
323
|
|
|
* is deleted by Featured_Content::validate_settings(). |
324
|
|
|
* |
325
|
|
|
* Hooks in the "delete_post_tag" action. |
326
|
|
|
* @see Featured_Content::validate_settings(). |
327
|
|
|
* |
328
|
|
|
* @param int $tag_id The term_id of the tag that has been deleted. |
329
|
|
|
* @return void |
330
|
|
|
*/ |
331
|
|
|
public static function delete_post_tag( $tag_id ) { |
332
|
|
|
$settings = self::get_setting(); |
333
|
|
|
|
334
|
|
|
if ( empty( $settings['tag-id'] ) || $tag_id != $settings['tag-id'] ) { |
335
|
|
|
return; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
$settings['tag-id'] = 0; |
339
|
|
|
$settings = self::validate_settings( $settings ); |
340
|
|
|
update_option( 'featured-content', $settings ); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Hide featured tag from displaying when global terms are queried from |
345
|
|
|
* the front-end. |
346
|
|
|
* |
347
|
|
|
* Hooks into the "get_terms" filter. |
348
|
|
|
* |
349
|
|
|
* @uses Featured_Content::get_setting() |
350
|
|
|
* |
351
|
|
|
* @param array $terms A list of term objects. This is the return value of get_terms(). |
352
|
|
|
* @param array $taxonomies An array of taxonomy slugs. |
353
|
|
|
* @return array $terms |
354
|
|
|
*/ |
355
|
|
|
public static function hide_featured_term( $terms, $taxonomies, $args ) { |
356
|
|
|
|
357
|
|
|
// This filter is only appropriate on the front-end. |
358
|
|
|
if ( is_admin() ) { |
359
|
|
|
return $terms; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
// We only want to hide the featured tag. |
363
|
|
|
if ( ! in_array( 'post_tag', $taxonomies ) ) { |
364
|
|
|
return $terms; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
// Bail if no terms were returned. |
368
|
|
|
if ( empty( $terms ) ) { |
369
|
|
|
return $terms; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
// Bail if term objects are unavailable. |
373
|
|
|
if ( 'all' != $args['fields'] ) { |
374
|
|
|
return $terms; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
$settings = self::get_setting(); |
378
|
|
|
|
379
|
|
View Code Duplication |
if ( false !== self::$tag ) { |
380
|
|
|
foreach ( $terms as $order => $term ) { |
381
|
|
|
if ( |
382
|
|
|
is_object( $term ) |
383
|
|
|
&& ( |
384
|
|
|
$settings['tag-id'] === $term->term_id |
385
|
|
|
|| $settings['tag-name'] === $term->name |
386
|
|
|
) |
387
|
|
|
) { |
388
|
|
|
unset( $terms[ $order ] ); |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
return $terms; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Hide featured tag from displaying when terms associated with a post object |
398
|
|
|
* are queried from the front-end. |
399
|
|
|
* |
400
|
|
|
* Hooks into the "get_the_terms" filter. |
401
|
|
|
* |
402
|
|
|
* @uses Featured_Content::get_setting() |
403
|
|
|
* |
404
|
|
|
* @param array $terms A list of term objects. This is the return value of get_the_terms(). |
405
|
|
|
* @param int $id The ID field for the post object that terms are associated with. |
406
|
|
|
* @param array $taxonomy An array of taxonomy slugs. |
407
|
|
|
* @return array $terms |
408
|
|
|
*/ |
409
|
|
|
public static function hide_the_featured_term( $terms, $id, $taxonomy ) { |
410
|
|
|
|
411
|
|
|
// This filter is only appropriate on the front-end. |
412
|
|
|
if ( is_admin() ) { |
413
|
|
|
return $terms; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
// Make sure we are in the correct taxonomy. |
417
|
|
|
if ( 'post_tag' != $taxonomy ) { |
418
|
|
|
return $terms; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
// No terms? Return early! |
422
|
|
|
if ( empty( $terms ) ) { |
423
|
|
|
return $terms; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
$settings = self::get_setting(); |
427
|
|
|
$tag = get_term_by( 'name', $settings['tag-name'], 'post_tag' ); |
428
|
|
|
|
429
|
|
View Code Duplication |
if ( false !== $tag ) { |
430
|
|
|
foreach ( $terms as $order => $term ) { |
431
|
|
|
if ( $settings['tag-id'] === $term->term_id || $settings['tag-name'] === $term->name ) { |
432
|
|
|
unset( $terms[ $order ] ); |
433
|
|
|
} |
434
|
|
|
} |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
return $terms; |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* Register custom setting on the Settings -> Reading screen. |
442
|
|
|
* |
443
|
|
|
* @uses Featured_Content::render_form() |
444
|
|
|
* @uses Featured_Content::validate_settings() |
445
|
|
|
* |
446
|
|
|
* @return void |
447
|
|
|
*/ |
448
|
|
|
public static function register_setting() { |
449
|
|
|
add_settings_field( 'featured-content', __( 'Featured Content', 'jetpack' ), array( __class__, 'render_form' ), 'reading' ); |
450
|
|
|
|
451
|
|
|
// Register sanitization callback for the Customizer. |
452
|
|
|
register_setting( 'featured-content', 'featured-content', array( __class__, 'validate_settings' ) ); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Add settings to the Customizer. |
457
|
|
|
* |
458
|
|
|
* @param WP_Customize_Manager $wp_customize Theme Customizer object. |
459
|
|
|
*/ |
460
|
|
|
public static function customize_register( $wp_customize ) { |
461
|
|
|
$wp_customize->add_section( 'featured_content', array( |
462
|
|
|
'title' => __( 'Featured Content', 'jetpack' ), |
463
|
|
|
'description' => sprintf( __( 'Easily feature all posts with the <a href="%1$s">"featured" tag</a> or a tag of your choice. Your theme supports up to %2$s posts in its featured content area.', 'jetpack' ), admin_url( '/edit.php?tag=featured' ), absint( self::$max_posts ) ), |
464
|
|
|
'priority' => 130, |
465
|
|
|
'theme_supports' => 'featured-content', |
466
|
|
|
) ); |
467
|
|
|
|
468
|
|
|
/* Add Featured Content settings. |
469
|
|
|
* |
470
|
|
|
* Sanitization callback registered in Featured_Content::validate_settings(). |
471
|
|
|
* See http://themeshaper.com/2013/04/29/validation-sanitization-in-customizer/comment-page-1/#comment-12374 |
472
|
|
|
*/ |
473
|
|
|
$wp_customize->add_setting( 'featured-content[tag-name]', array( |
474
|
|
|
'type' => 'option', |
475
|
|
|
'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ), |
476
|
|
|
) ); |
477
|
|
|
$wp_customize->add_setting( 'featured-content[hide-tag]', array( |
478
|
|
|
'default' => true, |
479
|
|
|
'type' => 'option', |
480
|
|
|
'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ), |
481
|
|
|
) ); |
482
|
|
|
$wp_customize->add_setting( 'featured-content[show-all]', array( |
483
|
|
|
'default' => false, |
484
|
|
|
'type' => 'option', |
485
|
|
|
'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ), |
486
|
|
|
) ); |
487
|
|
|
|
488
|
|
|
// Add Featured Content controls. |
489
|
|
|
$wp_customize->add_control( 'featured-content[tag-name]', array( |
490
|
|
|
'label' => __( 'Tag name', 'jetpack' ), |
491
|
|
|
'section' => 'featured_content', |
492
|
|
|
'theme_supports' => 'featured-content', |
493
|
|
|
'priority' => 20, |
494
|
|
|
) ); |
495
|
|
|
$wp_customize->add_control( 'featured-content[hide-tag]', array( |
496
|
|
|
'label' => __( 'Do not display tag in post details and tag clouds.', 'jetpack' ), |
497
|
|
|
'section' => 'featured_content', |
498
|
|
|
'theme_supports' => 'featured-content', |
499
|
|
|
'type' => 'checkbox', |
500
|
|
|
'priority' => 30, |
501
|
|
|
) ); |
502
|
|
|
$wp_customize->add_control( 'featured-content[show-all]', array( |
503
|
|
|
'label' => __( 'Also display tagged posts outside the Featured Content area.', 'jetpack' ), |
504
|
|
|
'section' => 'featured_content', |
505
|
|
|
'theme_supports' => 'featured-content', |
506
|
|
|
'type' => 'checkbox', |
507
|
|
|
'priority' => 40, |
508
|
|
|
) ); |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Enqueue the tag suggestion script. |
513
|
|
|
*/ |
514
|
|
|
public static function enqueue_scripts() { |
515
|
|
|
wp_enqueue_script( 'featured-content-suggest', plugins_url( 'js/suggest.js', __FILE__ ), array( 'suggest' ), '20131022', true ); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* Renders all form fields on the Settings -> Reading screen. |
520
|
|
|
*/ |
521
|
|
|
public static function render_form() { |
522
|
|
|
printf( __( 'The settings for Featured Content have <a href="%s">moved to Appearance → Customize</a>.', 'jetpack' ), admin_url( 'customize.php?#accordion-section-featured_content' ) ); |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
/** |
526
|
|
|
* Get settings |
527
|
|
|
* |
528
|
|
|
* Get all settings recognized by this module. This function will return all |
529
|
|
|
* settings whether or not they have been stored in the database yet. This |
530
|
|
|
* ensures that all keys are available at all times. |
531
|
|
|
* |
532
|
|
|
* In the event that you only require one setting, you may pass its name as the |
533
|
|
|
* first parameter to the function and only that value will be returned. |
534
|
|
|
* |
535
|
|
|
* @param string $key The key of a recognized setting. |
536
|
|
|
* @return mixed Array of all settings by default. A single value if passed as first parameter. |
537
|
|
|
*/ |
538
|
|
|
public static function get_setting( $key = 'all' ) { |
539
|
|
|
$saved = (array) get_option( 'featured-content' ); |
540
|
|
|
|
541
|
|
|
/** |
542
|
|
|
* Filter Featured Content's default settings. |
543
|
|
|
* |
544
|
|
|
* @module theme-tools |
545
|
|
|
* |
546
|
|
|
* @since 2.7.0 |
547
|
|
|
* |
548
|
|
|
* @param array $args { |
549
|
|
|
* Array of Featured Content Settings |
550
|
|
|
* |
551
|
|
|
* @type int hide-tag Default is 1. |
552
|
|
|
* @type int tag-id Default is 0. |
553
|
|
|
* @type string tag-name Default is empty. |
554
|
|
|
* @type int show-all Default is 0. |
555
|
|
|
* } |
556
|
|
|
*/ |
557
|
|
|
$defaults = apply_filters( 'featured_content_default_settings', array( |
558
|
|
|
'hide-tag' => 1, |
559
|
|
|
'tag-id' => 0, |
560
|
|
|
'tag-name' => '', |
561
|
|
|
'show-all' => 0, |
562
|
|
|
) ); |
563
|
|
|
|
564
|
|
|
$options = wp_parse_args( $saved, $defaults ); |
565
|
|
|
$options = array_intersect_key( $options, $defaults ); |
566
|
|
|
|
567
|
|
|
if ( 'all' != $key ) { |
568
|
|
|
return isset( $options[ $key ] ) ? $options[ $key ] : false; |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
return $options; |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
/** |
575
|
|
|
* Validate settings |
576
|
|
|
* |
577
|
|
|
* Make sure that all user supplied content is in an expected format before |
578
|
|
|
* saving to the database. This function will also delete the transient set in |
579
|
|
|
* Featured_Content::get_featured_content(). |
580
|
|
|
* |
581
|
|
|
* @uses Featured_Content::delete_transient() |
582
|
|
|
* |
583
|
|
|
* @param array $input |
584
|
|
|
* @return array $output |
585
|
|
|
*/ |
586
|
|
|
public static function validate_settings( $input ) { |
587
|
|
|
$output = array(); |
588
|
|
|
|
589
|
|
|
if ( empty( $input['tag-name'] ) ) { |
590
|
|
|
$output['tag-id'] = 0; |
591
|
|
|
} else { |
592
|
|
|
$term = get_term_by( 'name', $input['tag-name'], 'post_tag' ); |
593
|
|
|
|
594
|
|
|
if ( $term ) { |
595
|
|
|
$output['tag-id'] = $term->term_id; |
596
|
|
|
} else { |
597
|
|
|
$new_tag = wp_create_tag( $input['tag-name'] ); |
598
|
|
|
|
599
|
|
|
if ( ! is_wp_error( $new_tag ) && isset( $new_tag['term_id'] ) ) { |
600
|
|
|
$output['tag-id'] = $new_tag['term_id']; |
601
|
|
|
} |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
$output['tag-name'] = $input['tag-name']; |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
$output['hide-tag'] = isset( $input['hide-tag'] ) && $input['hide-tag'] ? 1 : 0; |
608
|
|
|
|
609
|
|
|
$output['show-all'] = isset( $input['show-all'] ) && $input['show-all'] ? 1 : 0; |
610
|
|
|
|
611
|
|
|
self::delete_transient(); |
612
|
|
|
|
613
|
|
|
return $output; |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
/** |
617
|
|
|
* Removes the quantity setting from the options array. |
618
|
|
|
* |
619
|
|
|
* @return void |
620
|
|
|
*/ |
621
|
|
|
public static function switch_theme() { |
622
|
|
|
$option = (array) get_option( 'featured-content' ); |
623
|
|
|
|
624
|
|
|
if ( isset( $option['quantity'] ) ) { |
625
|
|
|
unset( $option['quantity'] ); |
626
|
|
|
update_option( 'featured-content', $option ); |
627
|
|
|
} |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
public static function jetpack_update_featured_content_for_split_terms( $old_term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) { |
631
|
|
|
$featured_content_settings = get_option( 'featured-content', array() ); |
632
|
|
|
|
633
|
|
|
// Check to see whether the stored tag ID is the one that's just been split. |
634
|
|
|
if ( isset( $featured_content_settings['tag-id'] ) && $old_term_id == $featured_content_settings['tag-id'] && 'post_tag' == $taxonomy ) { |
635
|
|
|
// We have a match, so we swap out the old tag ID for the new one and resave the option. |
636
|
|
|
$featured_content_settings['tag-id'] = $new_term_id; |
637
|
|
|
update_option( 'featured-content', $featured_content_settings ); |
638
|
|
|
} |
639
|
|
|
} |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
Featured_Content::setup(); |
643
|
|
|
|
644
|
|
|
/** |
645
|
|
|
* Adds the featured content plugin to the set of files for which action |
646
|
|
|
* handlers should be copied when the theme context is loaded by the REST API. |
647
|
|
|
* |
648
|
|
|
* @param array $copy_dirs Copy paths with actions to be copied |
649
|
|
|
* @return array Copy paths with featured content plugin |
650
|
|
|
*/ |
651
|
|
|
function featured_content_copy_plugin_actions( $copy_dirs ) { |
652
|
|
|
$copy_dirs[] = __FILE__; |
653
|
|
|
return $copy_dirs; |
654
|
|
|
} |
655
|
|
|
add_action( 'restapi_theme_action_copy_dirs', 'featured_content_copy_plugin_actions' ); |
656
|
|
|
|
657
|
|
|
} // end if ( ! class_exists( 'Featured_Content' ) && isset( $GLOBALS['pagenow'] ) && 'plugins.php' !== $GLOBALS['pagenow'] ) { |
658
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.