| 1 |
<?php |
| 2 |
/** |
| 3 |
* Backwards compatibility class for breadcrumbs. |
| 4 |
* |
| 5 |
* @package Yoast\YoastSEO\Backwards_Compatibility |
| 6 |
*/ |
| 7 |
|
| 8 |
use Yoast\WP\SEO\Memoizers\Meta_Tags_Context_Memoizer; |
| 9 |
use Yoast\WP\SEO\Presenters\Breadcrumbs_Presenter; |
| 10 |
use Yoast\WP\SEO\Surfaces\Helpers_Surface; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class WPSEO_Breadcrumbs |
| 14 |
* |
| 15 |
* @codeCoverageIgnore Because of deprecation. |
| 16 |
*/ |
| 17 |
class WPSEO_Breadcrumbs { |
| 18 |
|
| 19 |
/** |
| 20 |
* Instance of this class. |
| 21 |
* |
| 22 |
* @var WPSEO_Breadcrumbs |
| 23 |
*/ |
| 24 |
public static $instance; |
| 25 |
|
| 26 |
/** |
| 27 |
* Last used 'before' string. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
public static $before = ''; |
| 32 |
|
| 33 |
/** |
| 34 |
* Last used 'after' string. |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
public static $after = ''; |
| 39 |
|
| 40 |
/** |
| 41 |
* The memoizer for the meta tags context. |
| 42 |
* |
| 43 |
* @var Meta_Tags_Context_Memoizer |
| 44 |
*/ |
| 45 |
protected $context_memoizer; |
| 46 |
|
| 47 |
/** |
| 48 |
* The helpers surface. |
| 49 |
* |
| 50 |
* @var Helpers_Surface |
| 51 |
*/ |
| 52 |
protected $helpers; |
| 53 |
|
| 54 |
/** |
| 55 |
* The replace vars helper |
| 56 |
* |
| 57 |
* @var WPSEO_Replace_Vars |
| 58 |
*/ |
| 59 |
protected $replace_vars; |
| 60 |
|
| 61 |
/** |
| 62 |
* WPSEO_Breadcrumbs constructor. |
| 63 |
*/ |
| 64 |
public function __construct() { |
| 65 |
$this->context_memoizer = YoastSEO()->classes->get( Meta_Tags_Context_Memoizer::class ); |
| 66 |
$this->helpers = YoastSEO()->classes->get( Helpers_Surface::class ); |
| 67 |
$this->replace_vars = YoastSEO()->classes->get( WPSEO_Replace_Vars::class ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get breadcrumb string using the singleton instance of this class. |
| 72 |
* |
| 73 |
* @param string $before Optional string to prepend. |
| 74 |
* @param string $after Optional string to append. |
| 75 |
* @param bool $display Echo or return flag. |
| 76 |
* |
| 77 |
* @return string Returns the breadcrumbs as a string. |
| 78 |
*/ |
| 79 |
public static function breadcrumb( $before = '', $after = '', $display = true ) { |
| 80 |
// Remember the last used before/after for use in case the object goes __toString(). |
| 81 |
self::$before = $before; |
| 82 |
self::$after = $after; |
| 83 |
$output = $before . self::get_instance()->render() . $after; |
| 84 |
|
| 85 |
if ( $display === true ) { |
| 86 |
echo $output; |
| 87 |
|
| 88 |
return ''; |
| 89 |
} |
| 90 |
|
| 91 |
return $output; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Magic method to use in case the class would be send to string. |
| 96 |
* |
| 97 |
* @return string The rendered breadcrumbs. |
| 98 |
*/ |
| 99 |
public function __toString() { |
| 100 |
return self::$before . $this->render() . self::$after; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Retrieves an instance of the class. |
| 105 |
* |
| 106 |
* @return static The instance. |
| 107 |
*/ |
| 108 |
public static function get_instance() { |
| 109 |
if ( is_null( self::$instance ) ) { |
| 110 |
self::$instance = new self(); |
| 111 |
} |
| 112 |
|
| 113 |
return self::$instance; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Returns the collected links for the breadcrumbs. |
| 118 |
* |
| 119 |
* @return array The collected links. |
| 120 |
*/ |
| 121 |
public function get_links() { |
| 122 |
$context = $this->context_memoizer->for_current_page(); |
| 123 |
|
| 124 |
return $context->presentation->breadcrumbs; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Renders the breadcrumbs. |
| 129 |
* |
| 130 |
* @return string The rendered breadcrumbs. |
| 131 |
*/ |
| 132 |
private function render() { |
| 133 |
$presenter = new Breadcrumbs_Presenter(); |
| 134 |
$context = $this->context_memoizer->for_current_page(); |
| 135 |
/** This filter is documented in src/integrations/front-end-integration.php */ |
| 136 |
$presentation = apply_filters( 'wpseo_frontend_presentation', $context->presentation, $context ); |
| 137 |
$presenter->presentation = $presentation; |
| 138 |
$presenter->replace_vars = $this->replace_vars; |
| 139 |
$presenter->helpers = $this->helpers; |
| 140 |
|
| 141 |
return $presenter->present(); |
| 142 |
} |
| 143 |
} |
| 144 |
|