Skip to content

Commit

Permalink
Merge pull request #20770 from Yoast/add-defensive-coding-term-archiv…
Browse files Browse the repository at this point in the history
…e-presentation

Adds defensive coding to make sure warnings don't happen.
  • Loading branch information
igorschoester authored Oct 17, 2023
2 parents e6375ae + 94f4eac commit 799860e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/helpers/current-page-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,12 @@ protected function get_non_cached_date_archive_permalink() {
* @return int The amoumt of queried terms.
*/
protected function count_queried_terms() {
$wp_query = $this->wp_query_wrapper->get_main_query();
$term = $wp_query->get_queried_object();
$wp_query = $this->wp_query_wrapper->get_main_query();
$term = $wp_query->get_queried_object();


$queried_terms = $wp_query->tax_query->queried_terms;
if ( empty( $queried_terms[ $term->taxonomy ]['terms'] ) ) {
if ( is_null( $term ) || empty( $queried_terms[ $term->taxonomy ]['terms'] ) ) {
return 0;
}

Expand Down
12 changes: 10 additions & 2 deletions src/presentations/indexable-term-archive-presentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function generate_meta_description() {
* @return array The source.
*/
public function generate_source() {
if ( ! empty( $this->model->object_id ) ) {
if ( ! empty( $this->model->object_id ) || is_null( \get_queried_object() ) ) {
return \get_term( $this->model->object_id, $this->model->object_sub_type );
}

Expand Down Expand Up @@ -152,7 +152,7 @@ public function generate_robots() {
* First we get the no index option for this taxonomy, because it can be overwritten the indexable value for
* this specific term.
*/
if ( ! $this->taxonomy->is_indexable( $this->source->taxonomy ) ) {
if ( is_wp_error( $this->source ) || ! $this->taxonomy->is_indexable( $this->source->taxonomy ) ) {
$robots['index'] = 'noindex';
}

Expand All @@ -176,6 +176,10 @@ public function generate_title() {
return $this->model->title;
}

if ( is_wp_error( $this->source ) ) {
return $this->model->title;
}

// Get the SEO title as entered in Search Appearance.
$title = $this->options->get( 'title-tax-' . $this->source->taxonomy );
if ( $title ) {
Expand Down Expand Up @@ -209,6 +213,10 @@ protected function is_multiple_terms_query() {
return false;
}

if ( is_wp_error( $this->source ) ) {
return false;
}

$queried_terms = $query->tax_query->queried_terms;

if ( empty( $queried_terms[ $this->source->taxonomy ]['terms'] ) ) {
Expand Down

0 comments on commit 799860e

Please sign in to comment.