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