|
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( 'update_option_featured-content', array( __CLASS__, 'flush_post_tag_cache' ), 10, 2 ); |
|
118
|
|
|
add_action( 'delete_option_featured-content', array( __CLASS__, 'flush_post_tag_cache' ), 10, 2 ); |
|
119
|
|
|
add_action( 'split_shared_term', array( __CLASS__, 'jetpack_update_featured_content_for_split_terms', 10, 4 ) ); |
|
120
|
|
|
|
|
121
|
|
|
if ( isset( $theme_support[0]['additional_post_types'] ) ) { |
|
122
|
|
|
$theme_support[0]['post_types'] = array_merge( array( 'post' ), (array) $theme_support[0]['additional_post_types'] ); |
|
123
|
|
|
unset( $theme_support[0]['additional_post_types'] ); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// Themes can allow Featured Content pages |
|
127
|
|
|
if ( isset( $theme_support[0]['post_types'] ) ) { |
|
128
|
|
|
self::$post_types = array_merge( self::$post_types, (array) $theme_support[0]['post_types'] ); |
|
129
|
|
|
self::$post_types = array_unique( self::$post_types ); |
|
130
|
|
|
|
|
131
|
|
|
// register post_tag support for each post type |
|
132
|
|
|
foreach ( self::$post_types as $post_type ) { |
|
133
|
|
|
register_taxonomy_for_object_type( 'post_tag', $post_type ); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Hide "featured" tag from the front-end. |
|
140
|
|
|
* |
|
141
|
|
|
* Has to run on wp_loaded so that the preview filters of the customizer |
|
142
|
|
|
* have a chance to alter the value. |
|
143
|
|
|
*/ |
|
144
|
|
|
public static function wp_loaded() { |
|
145
|
|
|
if ( self::get_setting( 'hide-tag' ) ) { |
|
146
|
|
|
$settings = self::get_setting(); |
|
147
|
|
|
|
|
148
|
|
|
// This is done before setting filters for get_terms in order to avoid an infinite filter loop |
|
149
|
|
|
self::$tag = get_term_by( 'name', $settings['tag-name'], 'post_tag' ); |
|
150
|
|
|
|
|
151
|
|
|
add_filter( 'get_terms', array( __CLASS__, 'hide_featured_term' ), 10, 3 ); |
|
152
|
|
|
add_filter( 'get_the_terms', array( __CLASS__, 'hide_the_featured_term' ), 10, 3 ); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get featured posts |
|
158
|
|
|
* |
|
159
|
|
|
* This method is not intended to be called directly. Theme developers should |
|
160
|
|
|
* place a filter directly in their theme and then pass its name as a value of |
|
161
|
|
|
* the "filter" key in the array passed as the $args parameter during the call |
|
162
|
|
|
* to: add_theme_support( 'featured-content', $args ). |
|
163
|
|
|
* |
|
164
|
|
|
* @uses Featured_Content::get_featured_post_ids() |
|
165
|
|
|
* |
|
166
|
|
|
* @return array |
|
167
|
|
|
*/ |
|
168
|
|
|
public static function get_featured_posts() { |
|
169
|
|
|
$post_ids = self::get_featured_post_ids(); |
|
170
|
|
|
|
|
171
|
|
|
// No need to query if there is are no featured posts. |
|
172
|
|
|
if ( empty( $post_ids ) ) { |
|
173
|
|
|
return array(); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$featured_posts = get_posts( |
|
177
|
|
|
array( |
|
178
|
|
|
'include' => $post_ids, |
|
179
|
|
|
'posts_per_page' => count( $post_ids ), |
|
180
|
|
|
'post_type' => self::$post_types, |
|
181
|
|
|
'suppress_filters' => false, |
|
182
|
|
|
) |
|
183
|
|
|
); |
|
184
|
|
|
|
|
185
|
|
|
return $featured_posts; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Get featured post IDs |
|
190
|
|
|
* |
|
191
|
|
|
* This function will return the an array containing the post IDs of all |
|
192
|
|
|
* featured posts. |
|
193
|
|
|
* |
|
194
|
|
|
* Sets the "featured_content_ids" transient. |
|
195
|
|
|
* |
|
196
|
|
|
* @return array Array of post IDs. |
|
197
|
|
|
*/ |
|
198
|
|
|
public static function get_featured_post_ids() { |
|
199
|
|
|
// Return array of cached results if they exist. |
|
200
|
|
|
$featured_ids = get_transient( 'featured_content_ids' ); |
|
201
|
|
|
if ( ! empty( $featured_ids ) ) { |
|
202
|
|
|
return array_map( |
|
203
|
|
|
'absint', |
|
204
|
|
|
/** |
|
205
|
|
|
* Filter the list of Featured Posts IDs. |
|
206
|
|
|
* |
|
207
|
|
|
* @module theme-tools |
|
208
|
|
|
* |
|
209
|
|
|
* @since 2.7.0 |
|
210
|
|
|
* |
|
211
|
|
|
* @param array $featured_ids Array of post IDs. |
|
212
|
|
|
*/ |
|
213
|
|
|
apply_filters( 'featured_content_post_ids', (array) $featured_ids ) |
|
214
|
|
|
); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
$settings = self::get_setting(); |
|
218
|
|
|
|
|
219
|
|
|
// Return empty array if no tag name is set. |
|
220
|
|
|
$term = get_term_by( 'name', $settings['tag-name'], 'post_tag' ); |
|
221
|
|
|
if ( ! $term ) { |
|
222
|
|
|
$term = get_term_by( 'id', $settings['tag-id'], 'post_tag' ); |
|
223
|
|
|
} |
|
224
|
|
|
if ( $term ) { |
|
225
|
|
|
$tag = $term->term_id; |
|
226
|
|
|
} else { |
|
227
|
|
|
/** This action is documented in modules/theme-tools/featured-content.php */ |
|
228
|
|
|
return apply_filters( 'featured_content_post_ids', array() ); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
// Back compat for installs that have the quantity option still set. |
|
232
|
|
|
$quantity = isset( $settings['quantity'] ) ? $settings['quantity'] : self::$max_posts; |
|
233
|
|
|
|
|
234
|
|
|
// Query for featured posts. |
|
235
|
|
|
$featured = get_posts( |
|
236
|
|
|
array( |
|
237
|
|
|
'numberposts' => $quantity, |
|
238
|
|
|
'post_type' => self::$post_types, |
|
239
|
|
|
'suppress_filters' => false, |
|
240
|
|
|
'tax_query' => array( |
|
241
|
|
|
array( |
|
242
|
|
|
'field' => 'term_id', |
|
243
|
|
|
'taxonomy' => 'post_tag', |
|
244
|
|
|
'terms' => $tag, |
|
245
|
|
|
), |
|
246
|
|
|
), |
|
247
|
|
|
) |
|
248
|
|
|
); |
|
249
|
|
|
|
|
250
|
|
|
// Return empty array if no featured content exists. |
|
251
|
|
|
if ( ! $featured ) { |
|
252
|
|
|
/** This action is documented in modules/theme-tools/featured-content.php */ |
|
253
|
|
|
return apply_filters( 'featured_content_post_ids', array() ); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
// Ensure correct format before save/return. |
|
257
|
|
|
$featured_ids = wp_list_pluck( (array) $featured, 'ID' ); |
|
258
|
|
|
$featured_ids = array_map( 'absint', $featured_ids ); |
|
259
|
|
|
|
|
260
|
|
|
set_transient( 'featured_content_ids', $featured_ids ); |
|
261
|
|
|
|
|
262
|
|
|
/** This action is documented in modules/theme-tools/featured-content.php */ |
|
263
|
|
|
return apply_filters( 'featured_content_post_ids', $featured_ids ); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Delete Transient. |
|
268
|
|
|
* |
|
269
|
|
|
* Hooks in the "save_post" action. |
|
270
|
|
|
* |
|
271
|
|
|
* @see Featured_Content::validate_settings(). |
|
272
|
|
|
*/ |
|
273
|
|
|
public static function delete_transient() { |
|
274
|
|
|
delete_transient( 'featured_content_ids' ); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Flush the Post Tag relationships cache. |
|
279
|
|
|
* |
|
280
|
|
|
* Hooks in the "update_option_featured-content" action. |
|
281
|
|
|
*/ |
|
282
|
|
|
public static function flush_post_tag_cache( $prev, $opts ) { |
|
283
|
|
|
if ( ! empty( $opts ) && ! empty( $opts['tag-id'] ) ) { |
|
284
|
|
|
$query = new WP_Query( |
|
285
|
|
|
array( |
|
286
|
|
|
'tag_id' => (int) $opts['tag-id'], |
|
287
|
|
|
'posts_per_page' => -1, |
|
288
|
|
|
) |
|
289
|
|
|
); |
|
290
|
|
|
foreach ( $query->posts as $post ) { |
|
291
|
|
|
wp_cache_delete( $post->ID, 'post_tag_relationships' ); |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Exclude featured posts from the blog query when the blog is the front-page, |
|
298
|
|
|
* and user has not checked the "Also display tagged posts outside the Featured Content area" checkbox. |
|
299
|
|
|
* |
|
300
|
|
|
* Filter the home page posts, and remove any featured post ID's from it. |
|
301
|
|
|
* Hooked onto the 'pre_get_posts' action, this changes the parameters of the |
|
302
|
|
|
* query before it gets any posts. |
|
303
|
|
|
* |
|
304
|
|
|
* @uses Featured_Content::get_featured_post_ids(); |
|
305
|
|
|
* @uses Featured_Content::get_setting(); |
|
306
|
|
|
* @param WP_Query $query |
|
307
|
|
|
* @return WP_Query Possibly modified WP_Query |
|
308
|
|
|
*/ |
|
309
|
|
|
public static function pre_get_posts( $query ) { |
|
310
|
|
|
|
|
311
|
|
|
// Bail if not home or not main query. |
|
312
|
|
|
if ( ! $query->is_home() || ! $query->is_main_query() ) { |
|
313
|
|
|
return; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
// Bail if the blog page is not the front page. |
|
317
|
|
|
if ( 'posts' !== get_option( 'show_on_front' ) ) { |
|
318
|
|
|
return; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
$featured = self::get_featured_post_ids(); |
|
322
|
|
|
|
|
323
|
|
|
// Bail if no featured posts. |
|
324
|
|
|
if ( ! $featured ) { |
|
|
|
|
|
|
325
|
|
|
return; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
$settings = self::get_setting(); |
|
329
|
|
|
|
|
330
|
|
|
// Bail if the user wants featured posts always displayed. |
|
331
|
|
|
if ( true == $settings['show-all'] ) { |
|
332
|
|
|
return; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
// We need to respect post ids already in the blacklist. |
|
336
|
|
|
$post__not_in = $query->get( 'post__not_in' ); |
|
337
|
|
|
|
|
338
|
|
|
if ( ! empty( $post__not_in ) ) { |
|
339
|
|
|
$featured = array_merge( (array) $post__not_in, $featured ); |
|
340
|
|
|
$featured = array_unique( $featured ); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
$query->set( 'post__not_in', $featured ); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* Reset tag option when the saved tag is deleted. |
|
348
|
|
|
* |
|
349
|
|
|
* It's important to mention that the transient needs to be deleted, too. |
|
350
|
|
|
* While it may not be obvious by looking at the function alone, the transient |
|
351
|
|
|
* is deleted by Featured_Content::validate_settings(). |
|
352
|
|
|
* |
|
353
|
|
|
* Hooks in the "delete_post_tag" action. |
|
354
|
|
|
* |
|
355
|
|
|
* @see Featured_Content::validate_settings(). |
|
356
|
|
|
* |
|
357
|
|
|
* @param int $tag_id The term_id of the tag that has been deleted. |
|
358
|
|
|
* @return void |
|
359
|
|
|
*/ |
|
360
|
|
|
public static function delete_post_tag( $tag_id ) { |
|
361
|
|
|
$settings = self::get_setting(); |
|
362
|
|
|
|
|
363
|
|
|
if ( empty( $settings['tag-id'] ) || $tag_id != $settings['tag-id'] ) { |
|
364
|
|
|
return; |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
$settings['tag-id'] = 0; |
|
368
|
|
|
$settings = self::validate_settings( $settings ); |
|
369
|
|
|
update_option( 'featured-content', $settings ); |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* Hide featured tag from displaying when global terms are queried from |
|
374
|
|
|
* the front-end. |
|
375
|
|
|
* |
|
376
|
|
|
* Hooks into the "get_terms" filter. |
|
377
|
|
|
* |
|
378
|
|
|
* @uses Featured_Content::get_setting() |
|
379
|
|
|
* |
|
380
|
|
|
* @param array $terms A list of term objects. This is the return value of get_terms(). |
|
381
|
|
|
* @param array $taxonomies An array of taxonomy slugs. |
|
382
|
|
|
* @return array $terms |
|
383
|
|
|
*/ |
|
384
|
|
|
public static function hide_featured_term( $terms, $taxonomies, $args ) { |
|
385
|
|
|
|
|
386
|
|
|
// This filter is only appropriate on the front-end. |
|
387
|
|
|
if ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) ) { |
|
388
|
|
|
return $terms; |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
// We only want to hide the featured tag. |
|
392
|
|
|
if ( ! in_array( 'post_tag', $taxonomies ) ) { |
|
393
|
|
|
return $terms; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
// Bail if no terms were returned. |
|
397
|
|
|
if ( empty( $terms ) ) { |
|
398
|
|
|
return $terms; |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
// Bail if term objects are unavailable. |
|
402
|
|
|
if ( 'all' != $args['fields'] ) { |
|
403
|
|
|
return $terms; |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
$settings = self::get_setting(); |
|
407
|
|
|
|
|
408
|
|
View Code Duplication |
if ( false !== self::$tag ) { |
|
409
|
|
|
foreach ( $terms as $order => $term ) { |
|
410
|
|
|
if ( |
|
411
|
|
|
is_object( $term ) |
|
412
|
|
|
&& ( |
|
413
|
|
|
$settings['tag-id'] === $term->term_id |
|
414
|
|
|
|| $settings['tag-name'] === $term->name |
|
415
|
|
|
) |
|
416
|
|
|
) { |
|
417
|
|
|
unset( $terms[ $order ] ); |
|
418
|
|
|
} |
|
419
|
|
|
} |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
return $terms; |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
/** |
|
426
|
|
|
* Hide featured tag from displaying when terms associated with a post object |
|
427
|
|
|
* are queried from the front-end. |
|
428
|
|
|
* |
|
429
|
|
|
* Hooks into the "get_the_terms" filter. |
|
430
|
|
|
* |
|
431
|
|
|
* @uses Featured_Content::get_setting() |
|
432
|
|
|
* |
|
433
|
|
|
* @param array $terms A list of term objects. This is the return value of get_the_terms(). |
|
434
|
|
|
* @param int $id The ID field for the post object that terms are associated with. |
|
435
|
|
|
* @param array $taxonomy An array of taxonomy slugs. |
|
436
|
|
|
* @return array $terms |
|
437
|
|
|
*/ |
|
438
|
|
|
public static function hide_the_featured_term( $terms, $id, $taxonomy ) { |
|
439
|
|
|
|
|
440
|
|
|
// This filter is only appropriate on the front-end. |
|
441
|
|
|
if ( is_admin() ) { |
|
442
|
|
|
return $terms; |
|
443
|
|
|
} |
|
444
|
|
|
|
|
445
|
|
|
// Make sure we are in the correct taxonomy. |
|
446
|
|
|
if ( 'post_tag' != $taxonomy ) { |
|
447
|
|
|
return $terms; |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
// No terms? Return early! |
|
451
|
|
|
if ( empty( $terms ) ) { |
|
452
|
|
|
return $terms; |
|
453
|
|
|
} |
|
454
|
|
|
|
|
455
|
|
|
$settings = self::get_setting(); |
|
456
|
|
|
$tag = get_term_by( 'name', $settings['tag-name'], 'post_tag' ); |
|
457
|
|
|
|
|
458
|
|
View Code Duplication |
if ( false !== $tag ) { |
|
459
|
|
|
foreach ( $terms as $order => $term ) { |
|
460
|
|
|
if ( $settings['tag-id'] === $term->term_id || $settings['tag-name'] === $term->name ) { |
|
461
|
|
|
unset( $terms[ $order ] ); |
|
462
|
|
|
} |
|
463
|
|
|
} |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
|
|
return $terms; |
|
467
|
|
|
} |
|
468
|
|
|
|
|
469
|
|
|
/** |
|
470
|
|
|
* Register custom setting on the Settings -> Reading screen. |
|
471
|
|
|
* |
|
472
|
|
|
* @uses Featured_Content::render_form() |
|
473
|
|
|
* @uses Featured_Content::validate_settings() |
|
474
|
|
|
* |
|
475
|
|
|
* @return void |
|
476
|
|
|
*/ |
|
477
|
|
|
public static function register_setting() { |
|
478
|
|
|
add_settings_field( 'featured-content', __( 'Featured Content', 'jetpack' ), array( __class__, 'render_form' ), 'reading' ); |
|
479
|
|
|
|
|
480
|
|
|
// Register sanitization callback for the Customizer. |
|
481
|
|
|
register_setting( 'featured-content', 'featured-content', array( __class__, 'validate_settings' ) ); |
|
482
|
|
|
} |
|
483
|
|
|
|
|
484
|
|
|
/** |
|
485
|
|
|
* Add settings to the Customizer. |
|
486
|
|
|
* |
|
487
|
|
|
* @param WP_Customize_Manager $wp_customize Theme Customizer object. |
|
488
|
|
|
*/ |
|
489
|
|
|
public static function customize_register( $wp_customize ) { |
|
490
|
|
|
$wp_customize->add_section( |
|
491
|
|
|
'featured_content', |
|
492
|
|
|
array( |
|
493
|
|
|
'title' => esc_html__( 'Featured Content', 'jetpack' ), |
|
494
|
|
|
'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 ) ), |
|
495
|
|
|
'priority' => 130, |
|
496
|
|
|
'theme_supports' => 'featured-content', |
|
497
|
|
|
) |
|
498
|
|
|
); |
|
499
|
|
|
|
|
500
|
|
|
/* |
|
501
|
|
|
Add Featured Content settings. |
|
502
|
|
|
* |
|
503
|
|
|
* Sanitization callback registered in Featured_Content::validate_settings(). |
|
504
|
|
|
* See http://themeshaper.com/2013/04/29/validation-sanitization-in-customizer/comment-page-1/#comment-12374 |
|
505
|
|
|
*/ |
|
506
|
|
|
$wp_customize->add_setting( |
|
507
|
|
|
'featured-content[tag-name]', |
|
508
|
|
|
array( |
|
509
|
|
|
'type' => 'option', |
|
510
|
|
|
'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ), |
|
511
|
|
|
) |
|
512
|
|
|
); |
|
513
|
|
|
$wp_customize->add_setting( |
|
514
|
|
|
'featured-content[hide-tag]', |
|
515
|
|
|
array( |
|
516
|
|
|
'default' => true, |
|
517
|
|
|
'type' => 'option', |
|
518
|
|
|
'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ), |
|
519
|
|
|
) |
|
520
|
|
|
); |
|
521
|
|
|
$wp_customize->add_setting( |
|
522
|
|
|
'featured-content[show-all]', |
|
523
|
|
|
array( |
|
524
|
|
|
'default' => false, |
|
525
|
|
|
'type' => 'option', |
|
526
|
|
|
'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ), |
|
527
|
|
|
) |
|
528
|
|
|
); |
|
529
|
|
|
|
|
530
|
|
|
// Add Featured Content controls. |
|
531
|
|
|
$wp_customize->add_control( |
|
532
|
|
|
'featured-content[tag-name]', |
|
533
|
|
|
array( |
|
534
|
|
|
'label' => esc_html__( 'Tag name', 'jetpack' ), |
|
535
|
|
|
'section' => 'featured_content', |
|
536
|
|
|
'theme_supports' => 'featured-content', |
|
537
|
|
|
'priority' => 20, |
|
538
|
|
|
) |
|
539
|
|
|
); |
|
540
|
|
|
$wp_customize->add_control( |
|
541
|
|
|
'featured-content[hide-tag]', |
|
542
|
|
|
array( |
|
543
|
|
|
'label' => esc_html__( 'Do not display tag in post details and tag clouds.', 'jetpack' ), |
|
544
|
|
|
'section' => 'featured_content', |
|
545
|
|
|
'theme_supports' => 'featured-content', |
|
546
|
|
|
'type' => 'checkbox', |
|
547
|
|
|
'priority' => 30, |
|
548
|
|
|
) |
|
549
|
|
|
); |
|
550
|
|
|
$wp_customize->add_control( |
|
551
|
|
|
'featured-content[show-all]', |
|
552
|
|
|
array( |
|
553
|
|
|
'label' => esc_html__( 'Also display tagged posts outside the Featured Content area.', 'jetpack' ), |
|
554
|
|
|
'section' => 'featured_content', |
|
555
|
|
|
'theme_supports' => 'featured-content', |
|
556
|
|
|
'type' => 'checkbox', |
|
557
|
|
|
'priority' => 40, |
|
558
|
|
|
) |
|
559
|
|
|
); |
|
560
|
|
|
} |
|
561
|
|
|
|
|
562
|
|
|
/** |
|
563
|
|
|
* Enqueue the tag suggestion script. |
|
564
|
|
|
*/ |
|
565
|
|
|
public static function enqueue_scripts() { |
|
566
|
|
|
wp_enqueue_script( 'featured-content-suggest', plugins_url( 'js/suggest.js', __FILE__ ), array( 'suggest' ), '20131022', true ); |
|
567
|
|
|
} |
|
568
|
|
|
|
|
569
|
|
|
/** |
|
570
|
|
|
* Renders all form fields on the Settings -> Reading screen. |
|
571
|
|
|
*/ |
|
572
|
|
|
public static function render_form() { |
|
573
|
|
|
printf( __( 'The settings for Featured Content have <a href="%s">moved to Appearance → Customize</a>.', 'jetpack' ), admin_url( 'customize.php?#accordion-section-featured_content' ) ); |
|
574
|
|
|
} |
|
575
|
|
|
|
|
576
|
|
|
/** |
|
577
|
|
|
* Get settings |
|
578
|
|
|
* |
|
579
|
|
|
* Get all settings recognized by this module. This function will return all |
|
580
|
|
|
* settings whether or not they have been stored in the database yet. This |
|
581
|
|
|
* ensures that all keys are available at all times. |
|
582
|
|
|
* |
|
583
|
|
|
* In the event that you only require one setting, you may pass its name as the |
|
584
|
|
|
* first parameter to the function and only that value will be returned. |
|
585
|
|
|
* |
|
586
|
|
|
* @param string $key The key of a recognized setting. |
|
587
|
|
|
* @return mixed Array of all settings by default. A single value if passed as first parameter. |
|
588
|
|
|
*/ |
|
589
|
|
|
public static function get_setting( $key = 'all' ) { |
|
590
|
|
|
$saved = (array) get_option( 'featured-content' ); |
|
591
|
|
|
|
|
592
|
|
|
/** |
|
593
|
|
|
* Filter Featured Content's default settings. |
|
594
|
|
|
* |
|
595
|
|
|
* @module theme-tools |
|
596
|
|
|
* |
|
597
|
|
|
* @since 2.7.0 |
|
598
|
|
|
* |
|
599
|
|
|
* @param array $args { |
|
600
|
|
|
* Array of Featured Content Settings |
|
601
|
|
|
* |
|
602
|
|
|
* @type int hide-tag Default is 1. |
|
603
|
|
|
* @type int tag-id Default is 0. |
|
604
|
|
|
* @type string tag-name Default is empty. |
|
605
|
|
|
* @type int show-all Default is 0. |
|
606
|
|
|
* } |
|
607
|
|
|
*/ |
|
608
|
|
|
$defaults = apply_filters( |
|
609
|
|
|
'featured_content_default_settings', |
|
610
|
|
|
array( |
|
611
|
|
|
'hide-tag' => 1, |
|
612
|
|
|
'tag-id' => 0, |
|
613
|
|
|
'tag-name' => '', |
|
614
|
|
|
'show-all' => 0, |
|
615
|
|
|
) |
|
616
|
|
|
); |
|
617
|
|
|
|
|
618
|
|
|
$options = wp_parse_args( $saved, $defaults ); |
|
619
|
|
|
$options = array_intersect_key( $options, $defaults ); |
|
620
|
|
|
|
|
621
|
|
|
if ( 'all' != $key ) { |
|
622
|
|
|
return isset( $options[ $key ] ) ? $options[ $key ] : false; |
|
623
|
|
|
} |
|
624
|
|
|
|
|
625
|
|
|
return $options; |
|
626
|
|
|
} |
|
627
|
|
|
|
|
628
|
|
|
/** |
|
629
|
|
|
* Validate settings |
|
630
|
|
|
* |
|
631
|
|
|
* Make sure that all user supplied content is in an expected format before |
|
632
|
|
|
* saving to the database. This function will also delete the transient set in |
|
633
|
|
|
* Featured_Content::get_featured_content(). |
|
634
|
|
|
* |
|
635
|
|
|
* @uses Featured_Content::delete_transient() |
|
636
|
|
|
* |
|
637
|
|
|
* @param array $input |
|
638
|
|
|
* @return array $output |
|
639
|
|
|
*/ |
|
640
|
|
|
public static function validate_settings( $input ) { |
|
641
|
|
|
$output = array(); |
|
642
|
|
|
|
|
643
|
|
|
if ( empty( $input['tag-name'] ) ) { |
|
644
|
|
|
$output['tag-id'] = 0; |
|
645
|
|
|
} else { |
|
646
|
|
|
$term = get_term_by( 'name', $input['tag-name'], 'post_tag' ); |
|
647
|
|
|
|
|
648
|
|
|
if ( $term ) { |
|
649
|
|
|
$output['tag-id'] = $term->term_id; |
|
650
|
|
|
} else { |
|
651
|
|
|
$new_tag = wp_create_tag( $input['tag-name'] ); |
|
652
|
|
|
|
|
653
|
|
|
if ( ! is_wp_error( $new_tag ) && isset( $new_tag['term_id'] ) ) { |
|
654
|
|
|
$output['tag-id'] = $new_tag['term_id']; |
|
655
|
|
|
} |
|
656
|
|
|
} |
|
657
|
|
|
|
|
658
|
|
|
$output['tag-name'] = $input['tag-name']; |
|
659
|
|
|
} |
|
660
|
|
|
|
|
661
|
|
|
$output['hide-tag'] = isset( $input['hide-tag'] ) && $input['hide-tag'] ? 1 : 0; |
|
662
|
|
|
|
|
663
|
|
|
$output['show-all'] = isset( $input['show-all'] ) && $input['show-all'] ? 1 : 0; |
|
664
|
|
|
|
|
665
|
|
|
self::delete_transient(); |
|
666
|
|
|
|
|
667
|
|
|
return $output; |
|
668
|
|
|
} |
|
669
|
|
|
|
|
670
|
|
|
/** |
|
671
|
|
|
* Removes the quantity setting from the options array. |
|
672
|
|
|
* |
|
673
|
|
|
* @return void |
|
674
|
|
|
*/ |
|
675
|
|
|
public static function switch_theme() { |
|
676
|
|
|
$option = (array) get_option( 'featured-content' ); |
|
677
|
|
|
|
|
678
|
|
|
if ( isset( $option['quantity'] ) ) { |
|
679
|
|
|
unset( $option['quantity'] ); |
|
680
|
|
|
update_option( 'featured-content', $option ); |
|
681
|
|
|
} |
|
682
|
|
|
} |
|
683
|
|
|
|
|
684
|
|
|
public static function jetpack_update_featured_content_for_split_terms( $old_term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) { |
|
685
|
|
|
$featured_content_settings = get_option( 'featured-content', array() ); |
|
686
|
|
|
|
|
687
|
|
|
// Check to see whether the stored tag ID is the one that's just been split. |
|
688
|
|
|
if ( isset( $featured_content_settings['tag-id'] ) && $old_term_id == $featured_content_settings['tag-id'] && 'post_tag' == $taxonomy ) { |
|
689
|
|
|
// We have a match, so we swap out the old tag ID for the new one and resave the option. |
|
690
|
|
|
$featured_content_settings['tag-id'] = $new_term_id; |
|
691
|
|
|
update_option( 'featured-content', $featured_content_settings ); |
|
692
|
|
|
} |
|
693
|
|
|
} |
|
694
|
|
|
} |
|
695
|
|
|
|
|
696
|
|
|
Featured_Content::setup(); |
|
697
|
|
|
|
|
698
|
|
|
/** |
|
699
|
|
|
* Adds the featured content plugin to the set of files for which action |
|
700
|
|
|
* handlers should be copied when the theme context is loaded by the REST API. |
|
701
|
|
|
* |
|
702
|
|
|
* @param array $copy_dirs Copy paths with actions to be copied |
|
703
|
|
|
* @return array Copy paths with featured content plugin |
|
704
|
|
|
*/ |
|
705
|
|
|
function wpcom_rest_api_featured_content_copy_plugin_actions( $copy_dirs ) { |
|
706
|
|
|
$copy_dirs[] = __FILE__; |
|
707
|
|
|
return $copy_dirs; |
|
708
|
|
|
} |
|
709
|
|
|
add_action( 'restapi_theme_action_copy_dirs', 'wpcom_rest_api_featured_content_copy_plugin_actions' ); |
|
710
|
|
|
|
|
711
|
|
|
} // end if ( ! class_exists( 'Featured_Content' ) && isset( $GLOBALS['pagenow'] ) && 'plugins.php' !== $GLOBALS['pagenow'] ) { |
|
712
|
|
|
|
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.