| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Blocks; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 6 |
|
| 7 |
/** |
| 8 |
* Dynamic_Block class. |
| 9 |
*/ |
| 10 |
abstract class Dynamic_Block_V3 implements Integration_Interface { |
| 11 |
|
| 12 |
/** |
| 13 |
* The name of the block. |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
protected $block_name; |
| 18 |
|
| 19 |
/** |
| 20 |
* The editor script for the block. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $script; |
| 25 |
|
| 26 |
/** |
| 27 |
* The base path for the block. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected $base_path; |
| 32 |
|
| 33 |
/** |
| 34 |
* Returns the conditionals based on which this loadable should be active. |
| 35 |
* |
| 36 |
* @return array<string> |
| 37 |
*/ |
| 38 |
public static function get_conditionals() { |
| 39 |
return []; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Initializes the integration. |
| 44 |
* |
| 45 |
* Integrations hooking on `init` need to have a priority of 11 or higher to |
| 46 |
* ensure that they run, as priority 10 is used by the loader to load the integrations. |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function register_hooks() { |
| 51 |
\add_action( 'init', [ $this, 'register_block' ], 11 ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Registers the block. |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function register_block() { |
| 60 |
\register_block_type( |
| 61 |
$this->base_path . $this->block_name . '/block.json', |
| 62 |
[ |
| 63 |
'editor_script' => $this->script, |
| 64 |
'render_callback' => [ $this, 'present' ], |
| 65 |
], |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Presents the block output. This is abstract because in the loop we need to be able to build the data for the |
| 71 |
* presenter in the last moment. |
| 72 |
* |
| 73 |
* @param array<string, bool|string|int|array> $attributes The block attributes. |
| 74 |
* |
| 75 |
* @return string The block output. |
| 76 |
*/ |
| 77 |
abstract public function present( $attributes ); |
| 78 |
|
| 79 |
/** |
| 80 |
* Checks whether the links in the block should have target="blank". |
| 81 |
* |
| 82 |
* This is needed because when the editor is loaded in an Iframe the link needs to open in a different browser window. |
| 83 |
* We don't want this behaviour in the front-end and the way to check this is to check if the block is rendered in a REST request with the `context` set as 'edit'. Thus being in the editor. |
| 84 |
* |
| 85 |
* @return bool returns if the block should be opened in another window. |
| 86 |
*/ |
| 87 |
protected function should_link_target_blank(): bool { |
| 88 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. |
| 89 |
if ( isset( $_GET['context'] ) && \is_string( $_GET['context'] ) ) { |
| 90 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are not processing form information, We are only strictly comparing. |
| 91 |
if ( \wp_unslash( $_GET['context'] ) === 'edit' ) { |
| 92 |
return true; |
| 93 |
} |
| 94 |
} |
| 95 |
return false; |
| 96 |
} |
| 97 |
} |
| 98 |
|