| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | namespace Automattic\Jetpack\Extensions\Featured_Image_Duplicate; |
| 9 | |
| 10 | use Jetpack_Gutenberg; |
| 11 | use WP_Block; |
| 12 | use WP_Block_Supports; |
| 13 | use WP_Post; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit( 0 ); |
| 17 | } |
| 18 | |
| 19 | const FEATURE_NAME = 'featured-image-duplicate'; |
| 20 | const HIDE_META_KEY = '_jetpack_hide_featured_image'; |
| 21 | const DISMISSED_META_KEY = '_jetpack_featured_image_duplicate_dismissed'; |
| 22 | const POST_TYPES = array( 'post', 'page' ); |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | function register_plugin() { |
| 30 | Jetpack_Gutenberg::set_extension_available( FEATURE_NAME ); |
| 31 | } |
| 32 | add_action( 'jetpack_register_gutenberg_extensions', __NAMESPACE__ . '\register_plugin' ); |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | function can_edit_post_meta( $allowed, $meta_key, $object_id ) { |
| 43 | return current_user_can( 'edit_post', (int) $object_id ); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | function register_meta_keys() { |
| 52 | foreach ( POST_TYPES as $post_type ) { |
| 53 | register_post_meta( |
| 54 | $post_type, |
| 55 | HIDE_META_KEY, |
| 56 | array( |
| 57 | 'type' => 'boolean', |
| 58 | 'description' => __( 'Whether to hide the featured image on this post’s own page.', 'jetpack' ), |
| 59 | 'single' => true, |
| 60 | 'default' => false, |
| 61 | 'show_in_rest' => true, |
| 62 | 'revisions_enabled' => true, |
| 63 | 'auth_callback' => __NAMESPACE__ . '\can_edit_post_meta', |
| 64 | ) |
| 65 | ); |
| 66 | register_post_meta( |
| 67 | $post_type, |
| 68 | DISMISSED_META_KEY, |
| 69 | array( |
| 70 | 'type' => 'integer', |
| 71 | 'description' => __( 'Featured image ID whose duplicate notice was dismissed.', 'jetpack' ), |
| 72 | 'single' => true, |
| 73 | 'default' => 0, |
| 74 | 'show_in_rest' => true, |
| 75 | 'sanitize_callback' => 'absint', |
| 76 | 'auth_callback' => __NAMESPACE__ . '\can_edit_post_meta', |
| 77 | ) |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | add_action( 'init', __NAMESPACE__ . '\register_meta_keys' ); |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | function is_hide_supported( $post ) { |
| 90 | $post = get_post( $post ); |
| 91 | if ( ! $post instanceof WP_Post || ! in_array( $post->post_type, POST_TYPES, true ) ) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | if ( ! wp_is_block_theme() ) { |
| 96 | return classic_theme_supports_hiding( $post->post_type ); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | $hierarchy = get_template_hierarchy_for_post( $post ); |
| 101 | $contents = wp_list_pluck( get_block_templates( array( 'slug__in' => $hierarchy ), 'wp_template' ), 'content', 'slug' ); |
| 102 | |
| 103 | |
| 104 | foreach ( $hierarchy as $slug ) { |
| 105 | if ( isset( $contents[ $slug ] ) ) { |
| 106 | return blocks_contain_featured_image( parse_blocks( $contents[ $slug ] ) ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | function classic_theme_supports_hiding( $post_type ) { |
| 120 | $options = get_theme_support( 'jetpack-content-options' ); |
| 121 | return ! empty( $options[0]['featured-images'][ $post_type ] ); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | function get_template_hierarchy_for_post( WP_Post $post ): array { |
| 131 | if ( 'page' === $post->post_type ) { |
| 132 | $hierarchy = array( "page-{$post->post_name}", "page-{$post->ID}", 'page', 'singular', 'index' ); |
| 133 | } else { |
| 134 | $hierarchy = array( "single-{$post->post_type}-{$post->post_name}", "single-{$post->post_type}", 'single', 'singular', 'index' ); |
| 135 | } |
| 136 | |
| 137 | $custom = get_page_template_slug( $post ); |
| 138 | if ( $custom ) { |
| 139 | array_unshift( $hierarchy, $custom ); |
| 140 | } |
| 141 | |
| 142 | return $hierarchy; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | function blocks_contain_featured_image( array $blocks, $depth = 0 ): bool { |
| 155 | if ( $depth > 5 ) { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | foreach ( resolve_pattern_blocks( $blocks ) as $block ) { |
| 160 | $name = $block['blockName'] ?? ''; |
| 161 | $inner = $block['innerBlocks'] ?? array(); |
| 162 | |
| 163 | if ( 'core/post-featured-image' === $name ) { |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | if ( 'core/query' === $name ) { |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | if ( 'core/template-part' === $name && ! empty( $block['attrs']['slug'] ) ) { |
| 172 | $part = get_block_template( ( $block['attrs']['theme'] ?? get_stylesheet() ) . '//' . $block['attrs']['slug'], 'wp_template_part' ); |
| 173 | $inner = $part ? parse_blocks( $part->content ) : array(); |
| 174 | } |
| 175 | |
| 176 | if ( $inner && blocks_contain_featured_image( $inner, $depth + 1 ) ) { |
| 177 | return true; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | function add_editor_feature_flag( $flags ): array { |
| 191 | $flags['featured-image-hide-supported'] = is_hide_supported( get_post() ); |
| 192 | return $flags; |
| 193 | } |
| 194 | add_filter( 'jetpack_block_editor_feature_flags', __NAMESPACE__ . '\add_editor_feature_flag' ); |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | function maybe_filter_thumbnail_id() { |
| 202 | |
| 203 | if ( ! wp_is_block_theme() && should_hide_for_post( get_queried_object_id() ) ) { |
| 204 | |
| 205 | add_filter( 'get_post_metadata', __NAMESPACE__ . '\filter_thumbnail_id', PHP_INT_MAX, 3 ); |
| 206 | } |
| 207 | } |
| 208 | add_action( 'template_redirect', __NAMESPACE__ . '\maybe_filter_thumbnail_id' ); |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | function should_hide_for_post( $post_id ): bool { |
| 217 | return is_singular( POST_TYPES ) |
| 218 | && get_queried_object_id() === $post_id |
| 219 | && (bool) get_post_meta( $post_id, HIDE_META_KEY, true ); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | function filter_thumbnail_id( $value, $object_id, $meta_key ) { |
| 233 | if ( |
| 234 | '_thumbnail_id' !== $meta_key |
| 235 | || get_queried_object_id() !== (int) $object_id |
| 236 | || ! in_the_loop() |
| 237 | || null !== WP_Block_Supports::$block_to_render |
| 238 | ) { |
| 239 | return $value; |
| 240 | } |
| 241 | |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | function hide_featured_image_block( $block_content, $block, $instance ): string { |
| 254 | $context = $instance->context; |
| 255 | |
| 256 | |
| 257 | if ( array_key_exists( 'queryId', $context ) || empty( $context['postId'] ) ) { |
| 258 | return $block_content; |
| 259 | } |
| 260 | |
| 261 | return should_hide_for_post( (int) $context['postId'] ) ? '' : $block_content; |
| 262 | } |
| 263 | add_filter( 'render_block_core/post-featured-image', __NAMESPACE__ . '\hide_featured_image_block', 10, 3 ); |