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