| @@ -15,8 +15,11 @@ | ||
| 15 | 15 | * |
| 16 | 16 | * @return bool |
| 17 | 17 | */ |
| 18 | 18 | public function is_needed() { |
| 19 | + if ( $this->context->indexable->object_type === 'unknown' ) { | |
| 20 | + return false; | |
| 21 | + } | |
| 19 | 22 | return ! ( $this->context->indexable->object_type === 'system-page' && $this->context->indexable->object_sub_type === '404' ); |
| 20 | 23 | } |
| 21 | 24 | |
| 22 | 25 | /** |
| @@ -21,14 +24,14 @@ | ||
| 21 | 24 | |
| 22 | 25 | /** |
| 23 | 26 | * Returns WebPage schema data. |
| 24 | 27 | * |
| 25 | - * @return array WebPage schema data. | |
| 28 | + * @return array<string|array<string>> WebPage schema data. | |
| 26 | 29 | */ |
| 27 | 30 | public function generate() { |
| 28 | 31 | $data = [ |
| 29 | 32 | '@type' => $this->context->schema_page_type, |
| 30 | - '@id' => $this->context->canonical . Schema_IDs::WEBPAGE_HASH, | |
| 33 | + '@id' => $this->context->main_schema_id, | |
| 31 | 34 | 'url' => $this->context->canonical, |
| 32 | 35 | 'name' => $this->helpers->schema->html->smart_strip_tags( $this->context->title ), |
| 33 | 36 | 'isPartOf' => [ |
| 34 | 37 | '@id' => $this->context->site_url . Schema_IDs::WEBSITE_HASH, |
| @@ -44,13 +47,16 @@ | ||
| 44 | 47 | $data['about'] = $this->context->site_represents_reference; |
| 45 | 48 | } |
| 46 | 49 | } |
| 47 | 50 | |
| 51 | + $data = $this->add_image( $data ); | |
| 52 | + | |
| 48 | 53 | if ( $this->context->indexable->object_type === 'post' ) { |
| 49 | - $this->add_image( $data ); | |
| 54 | + $data['datePublished'] = $this->helpers->date->format( $this->context->post->post_date_gmt ); | |
| 50 | 55 | |
| 51 | - $data['datePublished'] = $this->helpers->date->format( $this->context->post->post_date_gmt ); | |
| 52 | - $data['dateModified'] = $this->helpers->date->format( $this->context->post->post_modified_gmt ); | |
| 56 | + if ( \strtotime( $this->context->post->post_modified_gmt ) > \strtotime( $this->context->post->post_date_gmt ) ) { | |
| 57 | + $data['dateModified'] = $this->helpers->date->format( $this->context->post->post_modified_gmt ); | |
| 58 | + } | |
| 53 | 59 | |
| 54 | 60 | if ( $this->context->indexable->object_sub_type === 'post' ) { |
| 55 | 61 | $data = $this->add_author( $data, $this->context->post ); |
| 56 | 62 | } |
| @@ -78,12 +84,12 @@ | ||
| 78 | 84 | |
| 79 | 85 | /** |
| 80 | 86 | * Adds an author property to the $data if the WebPage is not represented. |
| 81 | 87 | * |
| 82 | - * @param array $data The WebPage schema. | |
| 83 | - * @param WP_Post $post The post the context is representing. | |
| 88 | + * @param array<string|array<string>> $data The WebPage schema. | |
| 89 | + * @param WP_Post $post The post the context is representing. | |
| 84 | 90 | * |
| 85 | - * @return array The WebPage schema. | |
| 91 | + * @return array<string|array<string>> The WebPage schema. | |
| 86 | 92 | */ |
| 87 | 93 | public function add_author( $data, $post ) { |
| 88 | 94 | if ( $this->context->site_represents === false ) { |
| 89 | 95 | $data['author'] = [ '@id' => $this->helpers->schema->id->get_user_schema_id( $post->post_author, $this->context ) ]; |
| @@ -94,14 +100,19 @@ | ||
| 94 | 100 | |
| 95 | 101 | /** |
| 96 | 102 | * If we have an image, make it the primary image of the page. |
| 97 | 103 | * |
| 98 | - * @param array $data WebPage schema data. | |
| 104 | + * @param array<string|array<string>> $data WebPage schema data. | |
| 105 | + * | |
| 106 | + * @return array<string|array<string>> | |
| 99 | 107 | */ |
| 100 | - public function add_image( &$data ) { | |
| 108 | + public function add_image( $data ) { | |
| 101 | 109 | if ( $this->context->has_image ) { |
| 102 | 110 | $data['primaryImageOfPage'] = [ '@id' => $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH ]; |
| 111 | + $data['image'] = [ '@id' => $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH ]; | |
| 112 | + $data['thumbnailUrl'] = $this->context->main_image_url; | |
| 103 | 113 | } |
| 114 | + return $data; | |
| 104 | 115 | } |
| 105 | 116 | |
| 106 | 117 | /** |
| 107 | 118 | * Determine if we should add a breadcrumb attribute. |
| @@ -118,22 +129,22 @@ | ||
| 118 | 129 | |
| 119 | 130 | /** |
| 120 | 131 | * Adds the potential action property to the WebPage Schema piece. |
| 121 | 132 | * |
| 122 | - * @param array $data The WebPage data. | |
| 133 | + * @param array<string|array<string>> $data The WebPage data. | |
| 123 | 134 | * |
| 124 | - * @return array The WebPage data with the potential action added. | |
| 135 | + * @return array<string|array<string>> The WebPage data with the potential action added. | |
| 125 | 136 | */ |
| 126 | 137 | private function add_potential_action( $data ) { |
| 127 | 138 | $url = $this->context->canonical; |
| 128 | - if ( empty( $url ) && \is_search() ) { | |
| 129 | - $url = $this->build_search_url(); | |
| 139 | + if ( $data['@type'] === 'CollectionPage' || ( \is_array( $data['@type'] ) && \in_array( 'CollectionPage', $data['@type'], true ) ) ) { | |
| 140 | + return $data; | |
| 130 | 141 | } |
| 131 | 142 | |
| 132 | 143 | /** |
| 133 | 144 | * Filter: 'wpseo_schema_webpage_potential_action_target' - Allows filtering of the schema WebPage potentialAction target. |
| 134 | 145 | * |
| 135 | - * @api array $targets The URLs for the WebPage potentialAction target. | |
| 146 | + * @param array<string> $targets The URLs for the WebPage potentialAction target. | |
| 136 | 147 | */ |
| 137 | 148 | $targets = \apply_filters( 'wpseo_schema_webpage_potential_action_target', [ $url ] ); |
| 138 | 149 | |
| 139 | 150 | $data['potentialAction'][] = [ |
| @@ -149,7 +160,7 @@ | ||
| 149 | 160 | * |
| 150 | 161 | * @return string Search URL. |
| 151 | 162 | */ |
| 152 | 163 | private function build_search_url() { |
| 153 | - return $this->context->site_url . '?s=' . \get_search_query(); | |
| 164 | + return $this->context->site_url . '?s=' . \rawurlencode( \get_search_query() ); | |
| 154 | 165 | } |
| 155 | 166 | } |