| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Blocks; |
| 4 |
|
| 5 |
use WPSEO_Replace_Vars; |
| 6 |
use Yoast\WP\SEO\Memoizers\Meta_Tags_Context_Memoizer; |
| 7 |
use Yoast\WP\SEO\Presenters\Breadcrumbs_Presenter; |
| 8 |
use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 9 |
use Yoast\WP\SEO\Surfaces\Helpers_Surface; |
| 10 |
|
| 11 |
/** |
| 12 |
* Siblings block class |
| 13 |
*/ |
| 14 |
class Breadcrumbs_Block extends Dynamic_Block_V3 { |
| 15 |
|
| 16 |
/** |
| 17 |
* The name of the block. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $block_name = 'breadcrumbs'; |
| 22 |
|
| 23 |
/** |
| 24 |
* The editor script for the block. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
protected $script = 'yoast-seo-dynamic-blocks'; |
| 29 |
|
| 30 |
/** |
| 31 |
* The Meta_Tags_Context_Memoizer. |
| 32 |
* |
| 33 |
* @var Meta_Tags_Context_Memoizer |
| 34 |
*/ |
| 35 |
private $context_memoizer; |
| 36 |
|
| 37 |
/** |
| 38 |
* The Replace vars helper. |
| 39 |
* |
| 40 |
* @var WPSEO_Replace_Vars |
| 41 |
*/ |
| 42 |
private $replace_vars; |
| 43 |
|
| 44 |
/** |
| 45 |
* The helpers surface. |
| 46 |
* |
| 47 |
* @var Helpers_Surface |
| 48 |
*/ |
| 49 |
private $helpers; |
| 50 |
|
| 51 |
/** |
| 52 |
* The indexable repository. |
| 53 |
* |
| 54 |
* @var Indexable_Repository |
| 55 |
*/ |
| 56 |
private $indexable_repository; |
| 57 |
|
| 58 |
/** |
| 59 |
* Siblings_Block constructor. |
| 60 |
* |
| 61 |
* @param Meta_Tags_Context_Memoizer $context_memoizer The context. |
| 62 |
* @param WPSEO_Replace_Vars $replace_vars The replace variable helper. |
| 63 |
* @param Helpers_Surface $helpers The Helpers surface. |
| 64 |
* @param Indexable_Repository $indexable_repository The indexable repository. |
| 65 |
*/ |
| 66 |
public function __construct( |
| 67 |
Meta_Tags_Context_Memoizer $context_memoizer, |
| 68 |
WPSEO_Replace_Vars $replace_vars, |
| 69 |
Helpers_Surface $helpers, |
| 70 |
Indexable_Repository $indexable_repository |
| 71 |
) { |
| 72 |
$this->context_memoizer = $context_memoizer; |
| 73 |
$this->replace_vars = $replace_vars; |
| 74 |
$this->helpers = $helpers; |
| 75 |
$this->indexable_repository = $indexable_repository; |
| 76 |
|
| 77 |
$this->base_path = \WPSEO_PATH . 'blocks/dynamic-blocks/'; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Presents the breadcrumbs output for the current page or the available post_id. |
| 82 |
* |
| 83 |
* @param array<string, bool|string|int|array> $attributes The block attributes. |
| 84 |
* |
| 85 |
* @return string The block output. |
| 86 |
*/ |
| 87 |
public function present( $attributes ) { |
| 88 |
$presenter = new Breadcrumbs_Presenter(); |
| 89 |
// $this->context_memoizer->for_current_page only works on the frontend. To render the right breadcrumb in the |
| 90 |
// editor, we need the repository. |
| 91 |
if ( \wp_is_serving_rest_request() || \is_admin() ) { |
| 92 |
$post_id = \get_the_ID(); |
| 93 |
if ( $post_id ) { |
| 94 |
$indexable = $this->indexable_repository->find_by_id_and_type( $post_id, 'post' ); |
| 95 |
|
| 96 |
if ( ! $indexable ) { |
| 97 |
$post = \get_post( $post_id ); |
| 98 |
$indexable = $this->indexable_repository->query()->create( |
| 99 |
[ |
| 100 |
'object_id' => $post_id, |
| 101 |
'object_type' => 'post', |
| 102 |
'object_sub_type' => $post->post_type, |
| 103 |
], |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
$context = $this->context_memoizer->get( $indexable, 'Post_Type' ); |
| 108 |
} |
| 109 |
} |
| 110 |
if ( ! isset( $context ) ) { |
| 111 |
$context = $this->context_memoizer->for_current_page(); |
| 112 |
} |
| 113 |
|
| 114 |
/** This filter is documented in src/integrations/front-end-integration.php */ |
| 115 |
$presentation = \apply_filters( 'wpseo_frontend_presentation', $context->presentation, $context ); |
| 116 |
$presenter->presentation = $presentation; |
| 117 |
$presenter->replace_vars = $this->replace_vars; |
| 118 |
$presenter->helpers = $this->helpers; |
| 119 |
$class_name = 'yoast-breadcrumbs'; |
| 120 |
|
| 121 |
if ( ! empty( $attributes['className'] ) ) { |
| 122 |
$class_name .= ' ' . \esc_attr( $attributes['className'] ); |
| 123 |
} |
| 124 |
|
| 125 |
return '<div class="' . $class_name . '">' . $presenter->present() . '</div>'; |
| 126 |
} |
| 127 |
} |
| 128 |
|