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