PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.2
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / integrations / blocks / breadcrumbs-block.php

breadcrumbs-block.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.2, at src/integrations/blocks/breadcrumbs-block.php

128 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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