| @@ -88,9 +88,9 @@ | ||
| 88 | 88 | * Generates the breadcrumbs. |
| 89 | 89 | * |
| 90 | 90 | * @param Meta_Tags_Context $context The meta tags context. |
| 91 | 91 | * |
| 92 | - * @return array An array of associative arrays that each have a 'text' and a 'url'. | |
| 92 | + * @return array<array<int, string>> An array of associative arrays that each have a 'text' and a 'url'. | |
| 93 | 93 | */ |
| 94 | 94 | public function generate( Meta_Tags_Context $context ) { |
| 95 | 95 | $static_ancestors = []; |
| 96 | 96 | $breadcrumbs_home = $this->options->get( 'breadcrumbs-home' ); |
| @@ -96,13 +96,16 @@ | ||
| 96 | 96 | $breadcrumbs_home = $this->options->get( 'breadcrumbs-home' ); |
| 97 | 97 | if ( $breadcrumbs_home !== '' && ! \in_array( $this->current_page_helper->get_page_type(), [ 'Home_Page', 'Static_Home_Page' ], true ) ) { |
| 98 | 98 | $front_page_id = $this->current_page_helper->get_front_page_id(); |
| 99 | 99 | if ( $front_page_id === 0 ) { |
| 100 | - $static_ancestors[] = $this->repository->find_for_home_page(); | |
| 100 | + $home_page_ancestor = $this->repository->find_for_home_page(); | |
| 101 | + if ( \is_a( $home_page_ancestor, Indexable::class ) ) { | |
| 102 | + $static_ancestors[] = $home_page_ancestor; | |
| 103 | + } | |
| 101 | 104 | } |
| 102 | 105 | else { |
| 103 | 106 | $static_ancestor = $this->repository->find_by_id_and_type( $front_page_id, 'post' ); |
| 104 | - if ( $static_ancestor->post_status !== 'unindexed' ) { | |
| 107 | + if ( \is_a( $static_ancestor, Indexable::class ) && $static_ancestor->post_status !== 'unindexed' ) { | |
| 105 | 108 | $static_ancestors[] = $static_ancestor; |
| 106 | 109 | } |
| 107 | 110 | } |
| 108 | 111 | } |
| @@ -108,9 +111,9 @@ | ||
| 108 | 111 | } |
| 109 | 112 | $page_for_posts = \get_option( 'page_for_posts' ); |
| 110 | 113 | if ( $this->should_have_blog_crumb( $page_for_posts, $context ) ) { |
| 111 | 114 | $static_ancestor = $this->repository->find_by_id_and_type( $page_for_posts, 'post' ); |
| 112 | - if ( $static_ancestor->post_status !== 'unindexed' ) { | |
| 115 | + if ( \is_a( $static_ancestor, Indexable::class ) && $static_ancestor->post_status !== 'unindexed' ) { | |
| 113 | 116 | $static_ancestors[] = $static_ancestor; |
| 114 | 117 | } |
| 115 | 118 | } |
| 116 | 119 | if ( |
| @@ -118,19 +121,27 @@ | ||
| 118 | 121 | && $context->indexable->object_sub_type !== 'post' |
| 119 | 122 | && $context->indexable->object_sub_type !== 'page' |
| 120 | 123 | && $this->post_type_helper->has_archive( $context->indexable->object_sub_type ) |
| 121 | 124 | ) { |
| 122 | - $static_ancestors[] = $this->repository->find_for_post_type_archive( $context->indexable->object_sub_type ); | |
| 125 | + $static_ancestor = $this->repository->find_for_post_type_archive( $context->indexable->object_sub_type ); | |
| 126 | + if ( \is_a( $static_ancestor, Indexable::class ) ) { | |
| 127 | + $static_ancestors[] = $static_ancestor; | |
| 128 | + } | |
| 123 | 129 | } |
| 124 | 130 | if ( $context->indexable->object_type === 'term' ) { |
| 125 | 131 | $parent = $this->get_taxonomy_post_type_parent( $context->indexable->object_sub_type ); |
| 126 | 132 | if ( $parent && $parent !== 'post' && $this->post_type_helper->has_archive( $parent ) ) { |
| 127 | - $static_ancestors[] = $this->repository->find_for_post_type_archive( $parent ); | |
| 133 | + $static_ancestor = $this->repository->find_for_post_type_archive( $parent ); | |
| 134 | + if ( \is_a( $static_ancestor, Indexable::class ) ) { | |
| 135 | + $static_ancestors[] = $static_ancestor; | |
| 136 | + } | |
| 128 | 137 | } |
| 129 | 138 | } |
| 130 | - | |
| 131 | - // Get all ancestors of the indexable and append itself to get all indexables in the full crumb. | |
| 132 | - $indexables = $this->repository->get_ancestors( $context->indexable ); | |
| 139 | + $indexables = []; | |
| 140 | + if ( ! \in_array( $this->current_page_helper->get_page_type(), [ 'Home_Page', 'Static_Home_Page' ], true ) ) { | |
| 141 | + // Get all ancestors of the indexable and append itself to get all indexables in the full crumb. | |
| 142 | + $indexables = $this->repository->get_ancestors( $context->indexable ); | |
| 143 | + } | |
| 133 | 144 | $indexables[] = $context->indexable; |
| 134 | 145 | |
| 135 | 146 | if ( ! empty( $static_ancestors ) ) { |
| 136 | 147 | \array_unshift( $indexables, ...$static_ancestors ); |
| @@ -136,37 +147,17 @@ | ||
| 136 | 147 | \array_unshift( $indexables, ...$static_ancestors ); |
| 137 | 148 | } |
| 138 | 149 | |
| 139 | 150 | $indexables = \apply_filters( 'wpseo_breadcrumb_indexables', $indexables, $context ); |
| 151 | + $indexables = \is_array( $indexables ) ? $indexables : []; | |
| 152 | + $indexables = \array_filter( | |
| 153 | + $indexables, | |
| 154 | + static function ( $indexable ) { | |
| 155 | + return \is_a( $indexable, Indexable::class ); | |
| 156 | + }, | |
| 157 | + ); | |
| 140 | 158 | |
| 141 | - $callback = function ( Indexable $ancestor ) { | |
| 142 | - $crumb = [ | |
| 143 | - 'url' => $ancestor->permalink, | |
| 144 | - 'text' => $ancestor->breadcrumb_title, | |
| 145 | - ]; | |
| 146 | - switch ( $ancestor->object_type ) { | |
| 147 | - case 'post': | |
| 148 | - $crumb = $this->get_post_crumb( $crumb, $ancestor ); | |
| 149 | - break; | |
| 150 | - case 'post-type-archive': | |
| 151 | - $crumb = $this->get_post_type_archive_crumb( $crumb, $ancestor ); | |
| 152 | - break; | |
| 153 | - case 'term': | |
| 154 | - $crumb = $this->get_term_crumb( $crumb, $ancestor ); | |
| 155 | - break; | |
| 156 | - case 'system-page': | |
| 157 | - $crumb = $this->get_system_page_crumb( $crumb, $ancestor ); | |
| 158 | - break; | |
| 159 | - case 'user': | |
| 160 | - $crumb = $this->get_user_crumb( $crumb, $ancestor ); | |
| 161 | - break; | |
| 162 | - case 'date-archive': | |
| 163 | - $crumb = $this->get_date_archive_crumb( $crumb ); | |
| 164 | - break; | |
| 165 | - } | |
| 166 | - return $crumb; | |
| 167 | - }; | |
| 168 | - $crumbs = \array_map( $callback, $indexables ); | |
| 159 | + $crumbs = \array_map( [ $this, 'get_post_type_crumb' ], $indexables ); | |
| 169 | 160 | |
| 170 | 161 | if ( $breadcrumbs_home !== '' ) { |
| 171 | 162 | $crumbs[0]['text'] = $breadcrumbs_home; |
| 172 | 163 | } |
| @@ -175,20 +166,31 @@ | ||
| 175 | 166 | |
| 176 | 167 | /** |
| 177 | 168 | * Filter: 'wpseo_breadcrumb_links' - Allow the developer to filter the Yoast SEO breadcrumb links, add to them, change order, etc. |
| 178 | 169 | * |
| 179 | - * @api array $crumbs The crumbs array. | |
| 170 | + * @param array $crumbs The crumbs array. | |
| 180 | 171 | */ |
| 181 | - $crumbs = \apply_filters( 'wpseo_breadcrumb_links', $crumbs ); | |
| 172 | + $filtered_crumbs = \apply_filters( 'wpseo_breadcrumb_links', $crumbs ); | |
| 182 | 173 | |
| 183 | - $filter_callback = static function( $link_info, $index ) use ( $crumbs ) { | |
| 174 | + // Basic check to make sure the filtered crumbs are in an array. | |
| 175 | + if ( ! \is_array( $filtered_crumbs ) ) { | |
| 176 | + \_doing_it_wrong( | |
| 177 | + 'Filter: \'wpseo_breadcrumb_links\'', | |
| 178 | + 'The `wpseo_breadcrumb_links` filter should return a multi-dimensional array.', | |
| 179 | + 'YoastSEO v20.0', | |
| 180 | + ); | |
| 181 | + } | |
| 182 | + else { | |
| 183 | + $crumbs = $filtered_crumbs; | |
| 184 | + } | |
| 185 | + | |
| 186 | + $filter_callback = static function ( $link_info, $index ) use ( $crumbs ) { | |
| 184 | 187 | /** |
| 185 | 188 | * Filter: 'wpseo_breadcrumb_single_link_info' - Allow developers to filter the Yoast SEO Breadcrumb link information. |
| 186 | 189 | * |
| 187 | - * @api array $link_info The breadcrumb link information. | |
| 188 | - * | |
| 189 | - * @param int $index The index of the breadcrumb in the list. | |
| 190 | - * @param array $crumbs The complete list of breadcrumbs. | |
| 190 | + * @param array $link_info The breadcrumb link information. | |
| 191 | + * @param int $index The index of the breadcrumb in the list. | |
| 192 | + * @param array $crumbs The complete list of breadcrumbs. | |
| 191 | 193 | */ |
| 192 | 194 | return \apply_filters( 'wpseo_breadcrumb_single_link_info', $link_info, $index, $crumbs ); |
| 193 | 195 | }; |
| 194 | 196 | return \array_map( $filter_callback, $crumbs, \array_keys( $crumbs ) ); |
| @@ -196,12 +198,12 @@ | ||
| 196 | 198 | |
| 197 | 199 | /** |
| 198 | 200 | * Returns the modified post crumb. |
| 199 | 201 | * |
| 200 | - * @param array $crumb The crumb. | |
| 202 | + * @param string[] $crumb The crumb. | |
| 201 | 203 | * @param Indexable $ancestor The indexable. |
| 202 | 204 | * |
| 203 | - * @return array The crumb. | |
| 205 | + * @return array<int, string> The crumb. | |
| 204 | 206 | */ |
| 205 | 207 | private function get_post_crumb( $crumb, $ancestor ) { |
| 206 | 208 | $crumb['id'] = $ancestor->object_id; |
| 207 | 209 | |
| @@ -208,14 +210,54 @@ | ||
| 208 | 210 | return $crumb; |
| 209 | 211 | } |
| 210 | 212 | |
| 211 | 213 | /** |
| 214 | + * Adds the correct ID to the crumb array based on the ancestor provided. | |
| 215 | + * | |
| 216 | + * @param Indexable $ancestor The ancestor indexable. | |
| 217 | + * | |
| 218 | + * @return string[] | |
| 219 | + */ | |
| 220 | + private function get_post_type_crumb( Indexable $ancestor ) { | |
| 221 | + $crumb = [ | |
| 222 | + 'url' => $ancestor->permalink, | |
| 223 | + 'text' => $ancestor->breadcrumb_title, | |
| 224 | + ]; | |
| 225 | + | |
| 226 | + switch ( $ancestor->object_type ) { | |
| 227 | + case 'post': | |
| 228 | + $crumb = $this->get_post_crumb( $crumb, $ancestor ); | |
| 229 | + break; | |
| 230 | + case 'post-type-archive': | |
| 231 | + $crumb = $this->get_post_type_archive_crumb( $crumb, $ancestor ); | |
| 232 | + break; | |
| 233 | + case 'term': | |
| 234 | + $crumb = $this->get_term_crumb( $crumb, $ancestor ); | |
| 235 | + break; | |
| 236 | + case 'system-page': | |
| 237 | + $crumb = $this->get_system_page_crumb( $crumb, $ancestor ); | |
| 238 | + break; | |
| 239 | + case 'user': | |
| 240 | + $crumb = $this->get_user_crumb( $crumb, $ancestor ); | |
| 241 | + break; | |
| 242 | + case 'date-archive': | |
| 243 | + $crumb = $this->get_date_archive_crumb( $crumb ); | |
| 244 | + break; | |
| 245 | + default: | |
| 246 | + // Handle unknown object types (optional). | |
| 247 | + break; | |
| 248 | + } | |
| 249 | + | |
| 250 | + return $crumb; | |
| 251 | + } | |
| 252 | + | |
| 253 | + /** | |
| 212 | 254 | * Returns the modified post type crumb. |
| 213 | 255 | * |
| 214 | - * @param array $crumb The crumb. | |
| 256 | + * @param string[] $crumb The crumb. | |
| 215 | 257 | * @param Indexable $ancestor The indexable. |
| 216 | 258 | * |
| 217 | - * @return array The crumb. | |
| 259 | + * @return string[] The crumb. | |
| 218 | 260 | */ |
| 219 | 261 | private function get_post_type_archive_crumb( $crumb, $ancestor ) { |
| 220 | 262 | $crumb['ptarchive'] = $ancestor->object_sub_type; |
| 221 | 263 | |
| @@ -224,15 +266,16 @@ | ||
| 224 | 266 | |
| 225 | 267 | /** |
| 226 | 268 | * Returns the modified term crumb. |
| 227 | 269 | * |
| 228 | - * @param array $crumb The crumb. | |
| 270 | + * @param string[] $crumb The crumb. | |
| 229 | 271 | * @param Indexable $ancestor The indexable. |
| 230 | 272 | * |
| 231 | - * @return array The crumb. | |
| 273 | + * @return array<int, string> The crumb. | |
| 232 | 274 | */ |
| 233 | 275 | private function get_term_crumb( $crumb, $ancestor ) { |
| 234 | - $crumb['term_id'] = $ancestor->object_id; | |
| 276 | + $crumb['term_id'] = $ancestor->object_id; | |
| 277 | + $crumb['taxonomy'] = $ancestor->object_sub_type; | |
| 235 | 278 | |
| 236 | 279 | return $crumb; |
| 237 | 280 | } |
| 238 | 281 | |
| @@ -238,12 +281,12 @@ | ||
| 238 | 281 | |
| 239 | 282 | /** |
| 240 | 283 | * Returns the modified system page crumb. |
| 241 | 284 | * |
| 242 | - * @param array $crumb The crumb. | |
| 285 | + * @param string[] $crumb The crumb. | |
| 243 | 286 | * @param Indexable $ancestor The indexable. |
| 244 | 287 | * |
| 245 | - * @return array The crumb. | |
| 288 | + * @return string[] The crumb. | |
| 246 | 289 | */ |
| 247 | 290 | private function get_system_page_crumb( $crumb, $ancestor ) { |
| 248 | 291 | if ( $ancestor->object_sub_type === 'search-result' ) { |
| 249 | 292 | $crumb['text'] = $this->options->get( 'breadcrumbs-searchprefix' ) . ' ' . \esc_html( \get_search_query() ); |
| @@ -258,12 +301,12 @@ | ||
| 258 | 301 | |
| 259 | 302 | /** |
| 260 | 303 | * Returns the modified user crumb. |
| 261 | 304 | * |
| 262 | - * @param array $crumb The crumb. | |
| 305 | + * @param string[] $crumb The crumb. | |
| 263 | 306 | * @param Indexable $ancestor The indexable. |
| 264 | 307 | * |
| 265 | - * @return array The crumb. | |
| 308 | + * @return string[] The crumb. | |
| 266 | 309 | */ |
| 267 | 310 | private function get_user_crumb( $crumb, $ancestor ) { |
| 268 | 311 | $display_name = \get_the_author_meta( 'display_name', $ancestor->object_id ); |
| 269 | 312 | $crumb['text'] = $this->options->get( 'breadcrumbs-archiveprefix' ) . ' ' . $display_name; |
| @@ -273,11 +316,11 @@ | ||
| 273 | 316 | |
| 274 | 317 | /** |
| 275 | 318 | * Returns the modified date archive crumb. |
| 276 | 319 | * |
| 277 | - * @param array $crumb The crumb. | |
| 320 | + * @param string[] $crumb The crumb. | |
| 278 | 321 | * |
| 279 | - * @return array The crumb. | |
| 322 | + * @return string[] The crumb. | |
| 280 | 323 | */ |
| 281 | 324 | protected function get_date_archive_crumb( $crumb ) { |
| 282 | 325 | $home_url = $this->url_helper->home(); |
| 283 | 326 | $prefix = $this->options->get( 'breadcrumbs-archiveprefix' ); |
| @@ -351,12 +394,12 @@ | ||
| 351 | 394 | |
| 352 | 395 | /** |
| 353 | 396 | * Adds a crumb for the current page, if we're on an archive page or paginated post. |
| 354 | 397 | * |
| 355 | - * @param array $crumbs The array of breadcrumbs. | |
| 398 | + * @param string[] $crumbs The array of breadcrumbs. | |
| 356 | 399 | * @param Indexable $current_indexable The current indexable. |
| 357 | 400 | * |
| 358 | - * @return array The breadcrumbs. | |
| 401 | + * @return string[] The breadcrumbs. | |
| 359 | 402 | */ |
| 360 | 403 | protected function add_paged_crumb( array $crumbs, $current_indexable ) { |
| 361 | 404 | $is_simple_page = $this->current_page_helper->is_simple_page(); |
| 362 | 405 | |
| @@ -378,9 +421,9 @@ | ||
| 378 | 421 | $crumbs[] = [ |
| 379 | 422 | 'text' => \sprintf( |
| 380 | 423 | /* translators: %s expands to the current page number */ |
| 381 | 424 | \__( 'Page %s', 'wordpress-seo' ), |
| 382 | - $current_page_number | |
| 425 | + $current_page_number, | |
| 383 | 426 | ), |
| 384 | 427 | ]; |
| 385 | 428 | |
| 386 | 429 | return $crumbs; |