Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 60 |
|
0.00% |
0 / 3 |
CRAP | n/a |
0 / 0 |
|
| jetpack_featured_images_remove_post_thumbnail | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
1332 | |||
| jetpack_is_product | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| jetpack_is_shop_page | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Theme Tools: functions for Featured Images. |
| 4 | * |
| 5 | * @package automattic/jetpack-classic-theme-helper |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit( 0 ); |
| 10 | } |
| 11 | |
| 12 | if ( ! function_exists( 'jetpack_featured_images_remove_post_thumbnail' ) ) { |
| 13 | |
| 14 | /** |
| 15 | * The function to prevent for Featured Images to be displayed in a theme. |
| 16 | * |
| 17 | * @param array $metadata Post metadata. |
| 18 | * @param int $object_id Post ID. |
| 19 | * @param string $meta_key Metadata key. |
| 20 | */ |
| 21 | function jetpack_featured_images_remove_post_thumbnail( $metadata, $object_id, $meta_key ) { |
| 22 | $opts = jetpack_featured_images_get_settings(); |
| 23 | |
| 24 | /** |
| 25 | * Allow featured images to be displayed at all times for specific CPTs. |
| 26 | * |
| 27 | * @module theme-tools |
| 28 | * |
| 29 | * @since 9.1.0 |
| 30 | * |
| 31 | * @param array $excluded_post_types Array of excluded post types. |
| 32 | */ |
| 33 | $excluded_post_types = apply_filters( |
| 34 | 'jetpack_content_options_featured_image_exclude_cpt', |
| 35 | array( 'jp_pay_product' ) |
| 36 | ); |
| 37 | |
| 38 | // Automatically return metadata for specific post types, when we don't want to hide the Featured Image. |
| 39 | if ( in_array( get_post_type( $object_id ), $excluded_post_types, true ) ) { |
| 40 | return $metadata; |
| 41 | } |
| 42 | |
| 43 | // Return false if the archive option or singular option is unticked. |
| 44 | if ( |
| 45 | ( true === $opts['archive'] |
| 46 | && ( is_home() || is_archive() || is_search() ) |
| 47 | && ! jetpack_is_shop_page() |
| 48 | && ! $opts['archive-option'] |
| 49 | && ( isset( $meta_key ) |
| 50 | && '_thumbnail_id' === $meta_key ) |
| 51 | && in_the_loop() |
| 52 | ) |
| 53 | || ( true === $opts['post'] |
| 54 | && is_single() |
| 55 | && ! jetpack_is_product() |
| 56 | && ! $opts['post-option'] |
| 57 | && ( isset( $meta_key ) |
| 58 | && '_thumbnail_id' === $meta_key ) |
| 59 | && in_the_loop() |
| 60 | ) |
| 61 | || ( true === $opts['page'] |
| 62 | && is_singular() |
| 63 | && is_page() |
| 64 | && ! $opts['page-option'] |
| 65 | && ( isset( $meta_key ) |
| 66 | && '_thumbnail_id' === $meta_key ) |
| 67 | && in_the_loop() |
| 68 | ) |
| 69 | || ( true === $opts['portfolio'] |
| 70 | && post_type_exists( 'jetpack-portfolio' ) |
| 71 | && is_singular( 'jetpack-portfolio' ) |
| 72 | && ! $opts['portfolio-option'] |
| 73 | && ( isset( $meta_key ) |
| 74 | && '_thumbnail_id' === $meta_key ) |
| 75 | && in_the_loop() |
| 76 | ) |
| 77 | ) { |
| 78 | |
| 79 | // Do not override thumbnail settings within blocks (eg. Latest Posts block). |
| 80 | $trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
| 81 | foreach ( $trace as $frame ) { |
| 82 | if ( ! empty( $frame['class'] ) && class_exists( $frame['class'], false ) && is_a( $frame['class'], WP_Block::class, true ) ) { |
| 83 | return $metadata; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return false; |
| 88 | } else { |
| 89 | return $metadata; |
| 90 | } |
| 91 | } |
| 92 | // We need this hook to execute at the end since core overrides the thumbnail id in _wp_preview_post_thumbnail_filter. |
| 93 | add_filter( 'get_post_metadata', 'jetpack_featured_images_remove_post_thumbnail', PHP_INT_MAX, 3 ); |
| 94 | |
| 95 | } |
| 96 | |
| 97 | if ( ! function_exists( 'jetpack_is_product' ) ) { |
| 98 | |
| 99 | /** |
| 100 | * Check if we are in a WooCommerce Product in order to exclude it from the is_single check. |
| 101 | */ |
| 102 | function jetpack_is_product() { |
| 103 | return ( function_exists( 'is_product' ) ) ? is_product() : false; // @phan-suppress-current-line PhanUndeclaredFunction -- We only call the function if it exists. |
| 104 | } |
| 105 | |
| 106 | } |
| 107 | |
| 108 | if ( ! function_exists( 'jetpack_is_shop_page' ) ) { |
| 109 | |
| 110 | /** |
| 111 | * Check if we are in a WooCommerce Shop in order to exclude it from the is_archive check. |
| 112 | */ |
| 113 | function jetpack_is_shop_page() { |
| 114 | // Check if WooCommerce is active first. |
| 115 | if ( ! class_exists( 'WooCommerce' ) ) { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | global $wp_query; |
| 120 | |
| 121 | $front_page_id = get_option( 'page_on_front' ); |
| 122 | $current_page_id = $wp_query->get( 'page_id' ); |
| 123 | $is_static_front_page = 'page' === get_option( 'show_on_front' ); |
| 124 | |
| 125 | $is_shop_page = false; |
| 126 | if ( $is_static_front_page && $front_page_id === $current_page_id ) { |
| 127 | $is_shop_page = function_exists( 'wc_get_page_id' ) && wc_get_page_id( 'shop' ) === $current_page_id; // @phan-suppress-current-line PhanUndeclaredFunction -- We're checking the function exists first. |
| 128 | } elseif ( function_exists( 'is_shop' ) ) { |
| 129 | $is_shop_page = is_shop(); // @phan-suppress-current-line PhanUndeclaredFunction -- We're checking the function exists first. |
| 130 | } |
| 131 | |
| 132 | return $is_shop_page; |
| 133 | } |
| 134 | |
| 135 | } |