breadcrumbs.php
3.2 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
137
138
139
140
141
142
143
144
<?php
/**
* Backwards compatibility class for breadcrumbs.
*
* @package Yoast\YoastSEO\Backwards_Compatibility
*/
use Yoast\WP\SEO\Memoizers\Meta_Tags_Context_Memoizer;
use Yoast\WP\SEO\Presenters\Breadcrumbs_Presenter;
use Yoast\WP\SEO\Surfaces\Helpers_Surface;
/**
* Class WPSEO_Breadcrumbs
*
* @codeCoverageIgnore Because of deprecation.
*/
class WPSEO_Breadcrumbs {
/**
* Instance of this class.
*
* @var WPSEO_Breadcrumbs
*/
public static $instance;
/**
* Last used 'before' string.
*
* @var string
*/
public static $before = '';
/**
* Last used 'after' string.
*
* @var string
*/
public static $after = '';
/**
* The memoizer for the meta tags context.
*
* @var Meta_Tags_Context_Memoizer
*/
protected $context_memoizer;
/**
* The helpers surface.
*
* @var Helpers_Surface
*/
protected $helpers;
/**
* The replace vars helper
*
* @var WPSEO_Replace_Vars
*/
protected $replace_vars;
/**
* WPSEO_Breadcrumbs constructor.
*/
public function __construct() {
$this->context_memoizer = YoastSEO()->classes->get( Meta_Tags_Context_Memoizer::class );
$this->helpers = YoastSEO()->classes->get( Helpers_Surface::class );
$this->replace_vars = YoastSEO()->classes->get( WPSEO_Replace_Vars::class );
}
/**
* Get breadcrumb string using the singleton instance of this class.
*
* @param string $before Optional string to prepend.
* @param string $after Optional string to append.
* @param bool $display Echo or return flag.
*
* @return string Returns the breadcrumbs as a string.
*/
public static function breadcrumb( $before = '', $after = '', $display = true ) {
// Remember the last used before/after for use in case the object goes __toString().
self::$before = $before;
self::$after = $after;
$output = $before . self::get_instance()->render() . $after;
if ( $display === true ) {
echo $output;
return '';
}
return $output;
}
/**
* Magic method to use in case the class would be send to string.
*
* @return string The rendered breadcrumbs.
*/
public function __toString() {
return self::$before . $this->render() . self::$after;
}
/**
* Retrieves an instance of the class.
*
* @return static The instance.
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Returns the collected links for the breadcrumbs.
*
* @return array The collected links.
*/
public function get_links() {
$context = $this->context_memoizer->for_current_page();
return $context->presentation->breadcrumbs;
}
/**
* Renders the breadcrumbs.
*
* @return string The rendered breadcrumbs.
*/
private function render() {
$presenter = new Breadcrumbs_Presenter();
$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;
return $presenter->present();
}
}