| 1 |
<?php |
| 2 |
/* |
| 3 |
* License: GPLv3 |
| 4 |
* License URI: https://www.gnu.org/licenses/gpl.txt |
| 5 |
* Copyright 2012-2026 Jean-Sebastien Morisset (https://wpsso.com/) |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
|
| 10 |
die( 'These aren\'t the droids you\'re looking for.' ); |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! defined( 'WPSSO_PLUGINDIR' ) ) { |
| 14 |
|
| 15 |
die( 'Do. Or do not. There is no try.' ); |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'WpssoUtilBlocks' ) ) { |
| 19 |
|
| 20 |
class WpssoUtilBlocks { |
| 21 |
|
| 22 |
private $p; // Wpsso class object. |
| 23 |
private $u; // WpssoUtil class object. |
| 24 |
|
| 25 |
/* |
| 26 |
* Instantiated by WpssoUtil->__construct(). |
| 27 |
*/ |
| 28 |
public function __construct( &$plugin, &$util ) { |
| 29 |
|
| 30 |
$this->p =& $plugin; |
| 31 |
$this->u =& $util; |
| 32 |
|
| 33 |
if ( $this->p->debug->enabled ) { |
| 34 |
|
| 35 |
$this->p->debug->mark(); |
| 36 |
} |
| 37 |
|
| 38 |
$this->u->add_plugin_filters( $this, array( |
| 39 |
'import_content_blocks' => 2, |
| 40 |
) ); |
| 41 |
} |
| 42 |
|
| 43 |
public function filter_import_content_blocks( array $md_opts, $content = '' ) { |
| 44 |
|
| 45 |
if ( $this->p->debug->enabled ) { |
| 46 |
|
| 47 |
$this->p->debug->mark(); |
| 48 |
} |
| 49 |
|
| 50 |
if ( function_exists( 'is_sitemap' ) && is_sitemap() ) { |
| 51 |
|
| 52 |
if ( $this->p->debug->enabled ) { |
| 53 |
|
| 54 |
$this->p->debug->log( 'skipping importing content blocks for sitemap' ); |
| 55 |
} |
| 56 |
|
| 57 |
return $md_opts; |
| 58 |
} |
| 59 |
|
| 60 |
if ( empty( $content ) ) { |
| 61 |
|
| 62 |
if ( $this->p->debug->enabled ) { |
| 63 |
|
| 64 |
$this->p->debug->log( 'exiting early: content is empty' ); |
| 65 |
} |
| 66 |
|
| 67 |
return $md_opts; |
| 68 |
} |
| 69 |
|
| 70 |
if ( ! function_exists( 'parse_blocks' ) ) { |
| 71 |
|
| 72 |
if ( $this->p->debug->enabled ) { |
| 73 |
|
| 74 |
$this->p->debug->log( 'exiting early: parse_blocks function not found' ); |
| 75 |
} |
| 76 |
|
| 77 |
return $md_opts; |
| 78 |
} |
| 79 |
|
| 80 |
if ( $this->p->debug->enabled ) { |
| 81 |
|
| 82 |
$this->p->debug->mark( 'importing content blocks' ); // Begin timer. |
| 83 |
} |
| 84 |
|
| 85 |
$blocks = parse_blocks( $content ); |
| 86 |
|
| 87 |
if ( $this->p->debug->enabled ) { |
| 88 |
|
| 89 |
$this->p->debug->log_arr( 'blocks', $blocks ); |
| 90 |
} |
| 91 |
|
| 92 |
foreach ( $blocks as $block ) { |
| 93 |
|
| 94 |
if ( empty( $block[ 'blockName' ] ) ) { |
| 95 |
|
| 96 |
if ( $this->p->debug->enabled ) { |
| 97 |
|
| 98 |
$this->p->debug->log( 'block name is empty' ); |
| 99 |
} |
| 100 |
|
| 101 |
continue; |
| 102 |
|
| 103 |
} elseif ( empty( $block[ 'attrs' ] ) ) { |
| 104 |
|
| 105 |
if ( $this->p->debug->enabled ) { |
| 106 |
|
| 107 |
$this->p->debug->log( 'block attrs is empty' ); |
| 108 |
} |
| 109 |
|
| 110 |
continue; |
| 111 |
} |
| 112 |
|
| 113 |
/* |
| 114 |
* Example filter name: wpsso_import_block_attrs_yoast_how_to_block |
| 115 |
*/ |
| 116 |
$filter_name = SucomUtil::sanitize_hookname( 'wpsso_import_block_attrs_' . $block[ 'blockName' ] ); |
| 117 |
|
| 118 |
if ( $this->p->debug->enabled ) { |
| 119 |
|
| 120 |
$this->p->debug->log( 'applying filters "' . $filter_name . '"' ); |
| 121 |
} |
| 122 |
|
| 123 |
$md_opts = apply_filters( $filter_name, $md_opts, $block[ 'attrs' ] ); |
| 124 |
} |
| 125 |
|
| 126 |
if ( $this->p->debug->enabled ) { |
| 127 |
|
| 128 |
$this->p->debug->mark( 'importing content blocks' ); // End timer. |
| 129 |
} |
| 130 |
|
| 131 |
return $md_opts; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|