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