attachments.php
6 years ago
blocks.php
1 month ago
common.php
4 months ago
maxmegamenu.php
1 month ago
meow_gallery.php
3 months ago
metaslider.php
1 month ago
my-calendar.php
1 month ago
woocommerce.php
1 month ago
wpseo.php
3 years ago
blocks.php
91 lines
| 1 | <?php |
| 2 | |
| 3 | // WordPress Block Editor (core blocks). |
| 4 | // |
| 5 | // Since WordPress 6.5, blocks can carry a background image through the block |
| 6 | // supports API. The image is kept in the block attributes only, and WordPress |
| 7 | // renders the background-image rule at display time, so the URL never appears |
| 8 | // in the saved HTML. Scanning the content alone therefore misses it, and the |
| 9 | // media looks unused. The attributes are read here instead. |
| 10 | |
| 11 | add_action( 'wpmc_scan_post', 'wpmc_scan_post_blocks', 10, 2 ); |
| 12 | |
| 13 | function wpmc_scan_post_blocks( $html, $id ) { |
| 14 | global $wpmc; |
| 15 | |
| 16 | if ( empty( $html ) || !is_string( $html ) ) { |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | $ids = array(); |
| 21 | $urls = array(); |
| 22 | |
| 23 | if ( strpos( $html, '<!-- wp:' ) !== false ) { |
| 24 | $blocks = parse_blocks( $html ); |
| 25 | if ( is_array( $blocks ) ) { |
| 26 | wpmc_scan_blocks_recursively( $blocks, $ids, $urls ); |
| 27 | } |
| 28 | } |
| 29 | else if ( get_post_type( $id ) === 'wp_global_styles' ) { |
| 30 | // The site-wide background set in the Site Editor is kept here as plain JSON, |
| 31 | // with no block markup at all, so nothing above ever sees it. The attributes |
| 32 | // are shaped the same, so the same walk finds it. |
| 33 | $styles = json_decode( $html, true ); |
| 34 | if ( is_array( $styles ) ) { |
| 35 | wpmc_scan_block_attributes( $styles, $ids, $urls ); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | if ( !empty( $ids ) ) { |
| 40 | $wpmc->add_reference_id( array_values( array_unique( $ids ) ), 'Block Background (ID)', $id ); |
| 41 | } |
| 42 | if ( !empty( $urls ) ) { |
| 43 | $wpmc->add_reference_url( array_values( array_unique( $urls ) ), 'Block Background (URL)', $id ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | function wpmc_scan_blocks_recursively( $blocks, &$ids, &$urls, $depth = 0 ) { |
| 48 | if ( $depth > 32 || !is_array( $blocks ) ) { |
| 49 | return; |
| 50 | } |
| 51 | foreach ( $blocks as $block ) { |
| 52 | if ( !is_array( $block ) ) { |
| 53 | continue; |
| 54 | } |
| 55 | if ( !empty( $block['attrs'] ) && is_array( $block['attrs'] ) ) { |
| 56 | wpmc_scan_block_attributes( $block['attrs'], $ids, $urls ); |
| 57 | } |
| 58 | // Background images are commonly set on nested blocks too. |
| 59 | if ( !empty( $block['innerBlocks'] ) ) { |
| 60 | wpmc_scan_blocks_recursively( $block['innerBlocks'], $ids, $urls, $depth + 1 ); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | function wpmc_scan_block_attributes( $attributes, &$ids, &$urls, $depth = 0 ) { |
| 66 | global $wpmc; |
| 67 | |
| 68 | if ( $depth > 32 || !is_array( $attributes ) ) { |
| 69 | return; |
| 70 | } |
| 71 | foreach ( $attributes as $key => $value ) { |
| 72 | if ( $key === 'backgroundImage' && is_array( $value ) ) { |
| 73 | if ( !empty( $value['id'] ) && is_numeric( $value['id'] ) ) { |
| 74 | $ids[] = (int) $value['id']; |
| 75 | } |
| 76 | if ( !empty( $value['url'] ) && is_string( $value['url'] ) ) { |
| 77 | $url = $wpmc->clean_url( $value['url'] ); |
| 78 | if ( !empty( $url ) ) { |
| 79 | $urls[] = $url; |
| 80 | } |
| 81 | } |
| 82 | continue; |
| 83 | } |
| 84 | if ( is_array( $value ) ) { |
| 85 | wpmc_scan_block_attributes( $value, $ids, $urls, $depth + 1 ); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | ?> |
| 91 |