4747 */
4848class Breadcrumbs {
4949
50+ /**
51+ * @since 5.1.4
52+ * @var array The options for breadcrumb generation.
53+ */
54+ private static $ options = [];
55+
5056 /**
5157 * Returns a list of breadcrumbs by URL and name.
5258 *
5359 * @since 5.0.0
60+ * @since 5.1.4 Added the `$options` parameter.
5461 * @todo consider wp_force_plain_post_permalink()
55- * @todo add extra parameter for $options; create (class?) constants for them.
56- * -> Is tsf()->breadcrumbs()::CONSTANT possible?
57- * -> Then, forward the options to a class variable, and build therefrom. Use as argument for memo().
62+ * @todo add extra parameters for $options?
5863 * -> Requested features (for shortcode): Remove home, remove current page.
59- * -> Requested features (globally): Remove/show archive prefixes, hide PTA/terms, select home name, select SEO vs custom title (popular) .
64+ * -> Requested features (globally): Remove/show archive prefixes, hide PTA/terms, select home name.
6065 * -> Add generation args to every crumb; this way we can perform custom lookups for titles after the crumb is generated.
6166 *
62- * @param array|null $args The query arguments. Accepts 'id', 'tax', 'pta', and 'uid'.
63- * Leave null to autodetermine query.
67+ * @param array|null $args The query arguments. Accepts 'id', 'tax', 'pta', and 'uid'.
68+ * Leave null to autodetermine query.
69+ * @param array $options Optional. {
70+ * The options for breadcrumb generation
71+ *
72+ * @type ?bool $use_meta_title Whether to consider meta titles before using page titles.
73+ * Default determined by 'breadcrumb_use_meta_title' option.
74+ * }
6475 * @return array[] {
6576 * The breadcrumb list items in order of appearance.
6677 *
6778 * @type string $url The breadcrumb URL.
6879 * @type string $name The breadcrumb page title.
6980 * }
7081 */
71- public static function get_breadcrumb_list ( $ args = null ) {
82+ public static function get_breadcrumb_list ( $ args = null , $ options = [] ) {
83+
84+ // The default options must be ordered alphabetically to ensure consistent cache keys.
85+ self ::$ options = array_merge (
86+ [
87+ 'use_meta_title ' => (bool ) Data \Plugin::get_option ( 'breadcrumb_use_meta_title ' ),
88+ ],
89+ array_filter (
90+ $ options ,
91+ fn ( $ v ) => null !== $ v ,
92+ ),
93+ );
94+
95+ // Sort options alphabetically to ensure consistent cache keys regardless of input order.
96+ ksort ( self ::$ options );
7297
7398 if ( isset ( $ args ) ) {
7499 normalize_generation_args ( $ args );
75100 $ list = self ::get_breadcrumb_list_from_args ( $ args );
76101 } else {
77- $ list = memo () ?? memo ( self ::get_breadcrumb_list_from_query () );
102+ $ list = memo ( null , self :: $ options ) ?? memo ( self ::get_breadcrumb_list_from_query (), self :: $ options );
78103 }
79104
80105 /**
81106 * @since 5.0.0
107+ * @since 5.1.4 Added the `$options` parameter.
82108 * @param array[] {
83109 * The breadcrumb list items in order of appearance.
84110 *
85111 * @type string $url The breadcrumb URL.
86112 * @type string $name The breadcrumb page title.
87113 * }
88- * @param array|null $args The query arguments. Contains 'id', 'tax', 'pta', and 'uid'.
89- * Is null when the query is auto-determined.
114+ * @param array|null $args The query arguments. Contains 'id', 'tax', 'pta', and 'uid'.
115+ * Is null when the query is auto-determined.
116+ * @param array $options The options used for breadcrumb generation.
90117 */
91118 return (array ) \apply_filters (
92119 'the_seo_framework_breadcrumb_list ' ,
93120 $ list ,
94121 $ args ,
122+ self ::$ options ,
95123 );
96124 }
97125
126+ /**
127+ * Returns the breadcrumb title based on the current setting.
128+ *
129+ * @since 5.1.4
130+ *
131+ * @param array|null $args The query arguments. Accepts 'id', 'tax', 'pta', and 'uid'.
132+ * Leave null to autodetermine query.
133+ * @return string The breadcrumb title.
134+ */
135+ private static function get_breadcrumb_title ( $ args = null ) {
136+
137+ if ( self ::$ options ['use_meta_title ' ] )
138+ return Meta \Title::get_bare_title ( $ args );
139+
140+ return Meta \Title::get_bare_generated_title ( $ args );
141+ }
142+
98143 /**
99144 * Gets a list of breadcrumbs, based on expected or current query.
100145 *
@@ -216,7 +261,7 @@ private static function get_singular_breadcrumb_list( $id = null ) {
216261 if ( \get_post_type_object ( $ post_type )->has_archive ?? false ) {
217262 $ crumbs [] = [
218263 'url ' => Meta \URI ::get_bare_pta_url ( $ post_type ),
219- 'name ' => Meta \Title:: get_bare_title ( [ 'pta ' => $ post_type ] ),
264+ 'name ' => self :: get_breadcrumb_title ( [ 'pta ' => $ post_type ] ),
220265 ];
221266 }
222267
@@ -237,7 +282,7 @@ private static function get_singular_breadcrumb_list( $id = null ) {
237282 ) as $ parent ) {
238283 $ crumbs [] = [
239284 'url ' => Meta \URI ::get_bare_term_url ( $ parent ->term_id , $ parent ->taxonomy ),
240- 'name ' => Meta \Title:: get_bare_title ( [
285+ 'name ' => self :: get_breadcrumb_title ( [
241286 'id ' => $ parent ->term_id ,
242287 'tax ' => $ parent ->taxonomy ,
243288 ] ),
@@ -249,19 +294,19 @@ private static function get_singular_breadcrumb_list( $id = null ) {
249294 foreach ( Data \Post::get_post_parents ( $ post ->ID ) as $ parent ) {
250295 $ crumbs [] = [
251296 'url ' => Meta \URI ::get_bare_singular_url ( $ parent ->ID ),
252- 'name ' => Meta \Title:: get_bare_title ( [ 'id ' => $ parent ->ID ] ),
297+ 'name ' => self :: get_breadcrumb_title ( [ 'id ' => $ parent ->ID ] ),
253298 ];
254299 }
255300
256301 if ( isset ( $ id ) ) {
257302 $ crumbs [] = [
258303 'url ' => Meta \URI ::get_bare_singular_url ( $ post ->ID ),
259- 'name ' => Meta \Title:: get_bare_title ( [ 'id ' => $ post ->ID ] ),
304+ 'name ' => self :: get_breadcrumb_title ( [ 'id ' => $ post ->ID ] ),
260305 ];
261306 } else {
262307 $ crumbs [] = [
263308 'url ' => Meta \URI ::get_bare_singular_url (),
264- 'name ' => Meta \Title:: get_bare_title (),
309+ 'name ' => self :: get_breadcrumb_title (),
265310 ];
266311 }
267312
@@ -303,7 +348,7 @@ private static function get_term_breadcrumb_list( $term_id = null, $taxonomy = '
303348 ) as $ parent ) {
304349 $ crumbs [] = [
305350 'url ' => Meta \URI ::get_bare_term_url ( $ parent ->term_id , $ parent ->taxonomy ),
306- 'name ' => Meta \Title:: get_bare_title ( [
351+ 'name ' => self :: get_breadcrumb_title ( [
307352 'id ' => $ parent ->term_id ,
308353 'tax ' => $ parent ->taxonomy ,
309354 ] ),
@@ -313,15 +358,15 @@ private static function get_term_breadcrumb_list( $term_id = null, $taxonomy = '
313358 if ( isset ( $ term_id ) ) {
314359 $ crumbs [] = [
315360 'url ' => Meta \URI ::get_bare_term_url ( $ term_id , $ taxonomy ),
316- 'name ' => Meta \Title:: get_bare_title ( [
361+ 'name ' => self :: get_breadcrumb_title ( [
317362 'id ' => $ term_id ,
318363 'tax ' => $ taxonomy ,
319364 ] ),
320365 ];
321366 } else {
322367 $ crumbs [] = [
323368 'url ' => Meta \URI ::get_bare_term_url (),
324- 'name ' => Meta \Title:: get_bare_title (),
369+ 'name ' => self :: get_breadcrumb_title (),
325370 ];
326371 }
327372
@@ -352,12 +397,12 @@ private static function get_pta_breadcrumb_list( $post_type = null ) {
352397 if ( isset ( $ post_type ) ) {
353398 $ crumbs [] = [
354399 'url ' => Meta \URI ::get_pta_url ( $ post_type ),
355- 'name ' => Meta \Title:: get_bare_title ( [ 'pta ' => $ post_type ] ),
400+ 'name ' => self :: get_breadcrumb_title ( [ 'pta ' => $ post_type ] ),
356401 ];
357402 } else {
358403 $ crumbs [] = [
359404 'url ' => Meta \URI ::get_bare_pta_url (),
360- 'name ' => Meta \Title:: get_bare_title (),
405+ 'name ' => self :: get_breadcrumb_title (),
361406 ];
362407 }
363408
@@ -387,12 +432,12 @@ private static function get_author_breadcrumb_list( $id = null ) {
387432 if ( isset ( $ id ) ) {
388433 $ crumbs [] = [
389434 'url ' => Meta \URI ::get_author_url ( $ id ),
390- 'name ' => Meta \Title:: get_bare_title ( [ 'uid ' => $ id ] ),
435+ 'name ' => self :: get_breadcrumb_title ( [ 'uid ' => $ id ] ),
391436 ];
392437 } else {
393438 $ crumbs [] = [
394439 'url ' => Meta \URI ::get_bare_author_url (),
395- 'name ' => Meta \Title:: get_bare_title (),
440+ 'name ' => self :: get_breadcrumb_title (), // NOTE: has no meta title (yet), but we'll add that in 5.2.0
396441 ];
397442 }
398443
@@ -426,7 +471,7 @@ private static function get_date_breadcrumb_list() {
426471 \get_query_var ( 'monthnum ' ),
427472 \get_query_var ( 'day ' ),
428473 ),
429- 'name ' => Meta \Title::get_bare_title (),
474+ 'name ' => Meta \Title::get_bare_generated_title (), // discrepancy, has no meta title
430475 ],
431476 ];
432477 }
@@ -448,7 +493,7 @@ private static function get_search_breadcrumb_list() {
448493 self ::get_front_breadcrumb (),
449494 [
450495 'url ' => Meta \URI ::get_search_url (),
451- 'name ' => Meta \Title::get_search_query_title (), // discrepancy
496+ 'name ' => Meta \Title::get_search_query_title (), // discrepancy, has no meta title
452497 ],
453498 ];
454499 }
@@ -470,7 +515,7 @@ private static function get_404_breadcrumb_list() {
470515 self ::get_front_breadcrumb (),
471516 [
472517 'url ' => '' ,
473- 'name ' => Meta \Title::get_404_title (), // discrepancy
518+ 'name ' => Meta \Title::get_404_title (), // discrepancy, has no meta title
474519 ],
475520 ];
476521 }
@@ -490,7 +535,7 @@ private static function get_404_breadcrumb_list() {
490535 private static function get_front_breadcrumb () {
491536 return [
492537 'url ' => Meta \URI ::get_bare_front_page_url (),
493- 'name ' => Meta \Title::get_front_page_title (), // discrepancy
538+ 'name ' => Meta \Title::get_front_page_title (), // discrepancy, has no meta title
494539 ];
495540 }
496541}
0 commit comments