| @@ -42,8 +42,55 @@ | ||
| 42 | 42 | */ |
| 43 | 43 | protected $indexing_helper; |
| 44 | 44 | |
| 45 | 45 | /** |
| 46 | + * Default values of certain columns. | |
| 47 | + * | |
| 48 | + * @var array | |
| 49 | + */ | |
| 50 | + protected $default_values = [ | |
| 51 | + 'title' => [ | |
| 52 | + 'default_value' => null, | |
| 53 | + ], | |
| 54 | + 'description' => [ | |
| 55 | + 'default_value' => null, | |
| 56 | + ], | |
| 57 | + 'open_graph_title' => [ | |
| 58 | + 'default_value' => null, | |
| 59 | + ], | |
| 60 | + 'open_graph_description' => [ | |
| 61 | + 'default_value' => null, | |
| 62 | + ], | |
| 63 | + 'twitter_title' => [ | |
| 64 | + 'default_value' => null, | |
| 65 | + ], | |
| 66 | + 'twitter_description' => [ | |
| 67 | + 'default_value' => null, | |
| 68 | + ], | |
| 69 | + 'canonical' => [ | |
| 70 | + 'default_value' => null, | |
| 71 | + ], | |
| 72 | + 'primary_focus_keyword' => [ | |
| 73 | + 'default_value' => null, | |
| 74 | + ], | |
| 75 | + 'is_robots_noindex' => [ | |
| 76 | + 'default_value' => null, | |
| 77 | + ], | |
| 78 | + 'is_robots_nofollow' => [ | |
| 79 | + 'default_value' => false, | |
| 80 | + ], | |
| 81 | + 'is_robots_noarchive' => [ | |
| 82 | + 'default_value' => null, | |
| 83 | + ], | |
| 84 | + 'is_robots_noimageindex' => [ | |
| 85 | + 'default_value' => null, | |
| 86 | + ], | |
| 87 | + 'is_robots_nosnippet' => [ | |
| 88 | + 'default_value' => null, | |
| 89 | + ], | |
| 90 | + ]; | |
| 91 | + | |
| 92 | + /** | |
| 46 | 93 | * Indexable_Helper constructor. |
| 47 | 94 | * |
| 48 | 95 | * @param Options_Helper $options_helper The options helper. |
| 49 | 96 | * @param Environment_Helper $environment_helper The environment helper. |
| @@ -60,8 +107,10 @@ | ||
| 60 | 107 | * |
| 61 | 108 | * @required |
| 62 | 109 | * |
| 63 | 110 | * @param Indexable_Repository $repository The indexable repository. |
| 111 | + * | |
| 112 | + * @return void | |
| 64 | 113 | */ |
| 65 | 114 | public function set_indexable_repository( Indexable_Repository $repository ) { |
| 66 | 115 | $this->repository = $repository; |
| 67 | 116 | } |
| @@ -113,8 +162,10 @@ | ||
| 113 | 162 | * |
| 114 | 163 | * @param string|null $type The type of the indexable. |
| 115 | 164 | * @param string|null $subtype The subtype. Can be null. |
| 116 | 165 | * @param string $reason The reason that the permalink has been changed. |
| 166 | + * | |
| 167 | + * @return void | |
| 117 | 168 | */ |
| 118 | 169 | public function reset_permalink_indexables( $type = null, $subtype = null, $reason = Indexing_Reasons::REASON_PERMALINK_SETTINGS ) { |
| 119 | 170 | $result = $this->repository->reset_permalink( $type, $subtype ); |
| 120 | 171 | |
| @@ -127,15 +178,49 @@ | ||
| 127 | 178 | } |
| 128 | 179 | } |
| 129 | 180 | |
| 130 | 181 | /** |
| 182 | + * Determines whether indexing the specific indexable is appropriate at this time. | |
| 183 | + * | |
| 184 | + * @param Indexable $indexable The indexable. | |
| 185 | + * | |
| 186 | + * @return bool Whether indexing the specific indexable is appropriate at this time. | |
| 187 | + */ | |
| 188 | + public function should_index_indexable( $indexable ) { | |
| 189 | + $intend_to_save = $this->should_index_indexables(); | |
| 190 | + | |
| 191 | + /** | |
| 192 | + * Filter: 'wpseo_should_save_indexable' - Allow developers to enable / disable | |
| 193 | + * saving the indexable when the indexable is updated. Warning: overriding | |
| 194 | + * the intended action may cause problems when moving from a staging to a | |
| 195 | + * production environment because indexable permalinks may get set incorrectly. | |
| 196 | + * | |
| 197 | + * @param bool $intend_to_save True if YoastSEO intends to save the indexable. | |
| 198 | + * @param Indexable $indexable The indexable to be saved. | |
| 199 | + */ | |
| 200 | + return \apply_filters( 'wpseo_should_save_indexable', $intend_to_save, $indexable ); | |
| 201 | + } | |
| 202 | + | |
| 203 | + /** | |
| 131 | 204 | * Determines whether indexing indexables is appropriate at this time. |
| 132 | 205 | * |
| 133 | - * @return bool Whether or not the indexables should be indexed. | |
| 206 | + * @return bool Whether the indexables should be indexed. | |
| 134 | 207 | */ |
| 135 | 208 | public function should_index_indexables() { |
| 136 | - // Currently the only reason to index is when we're on a production website. | |
| 137 | - return $this->environment_helper->is_production_mode(); | |
| 209 | + // Currently, the only reason to index is when we're on a production website. | |
| 210 | + $should_index = $this->environment_helper->is_production_mode(); | |
| 211 | + | |
| 212 | + /** | |
| 213 | + * Filter: 'Yoast\WP\SEO\should_index_indexables' - Allow developers to enable / disable | |
| 214 | + * creating indexables. Warning: overriding | |
| 215 | + * the intended action may cause problems when moving from a staging to a | |
| 216 | + * production environment because indexable permalinks may get set incorrectly. | |
| 217 | + * | |
| 218 | + * @since 18.2 | |
| 219 | + * | |
| 220 | + * @param bool $should_index Whether the site's indexables should be created. | |
| 221 | + */ | |
| 222 | + return (bool) \apply_filters( 'Yoast\WP\SEO\should_index_indexables', $should_index ); | |
| 138 | 223 | } |
| 139 | 224 | |
| 140 | 225 | /** |
| 141 | 226 | * Returns whether or not dynamic permalinks should be used. |
| @@ -157,6 +242,86 @@ | ||
| 157 | 242 | * @return void |
| 158 | 243 | */ |
| 159 | 244 | public function finish_indexing() { |
| 160 | 245 | $this->options_helper->set( 'indexables_indexing_completed', true ); |
| 246 | + } | |
| 247 | + | |
| 248 | + /** | |
| 249 | + * Checks whether the indexable has default values in given fields. | |
| 250 | + * | |
| 251 | + * @param Indexable $indexable The Yoast indexable that we're checking. | |
| 252 | + * @param array $fields The Yoast indexable fields that we're checking against. | |
| 253 | + * | |
| 254 | + * @return bool Whether the indexable has default values. | |
| 255 | + */ | |
| 256 | + public function check_if_default_indexable( $indexable, $fields ) { | |
| 257 | + foreach ( $fields as $field ) { | |
| 258 | + $is_default = $this->check_if_default_field( $indexable, $field ); | |
| 259 | + if ( ! $is_default ) { | |
| 260 | + break; | |
| 261 | + } | |
| 262 | + } | |
| 263 | + | |
| 264 | + return $is_default; | |
| 265 | + } | |
| 266 | + | |
| 267 | + /** | |
| 268 | + * Checks if an indexable field contains the default value. | |
| 269 | + * | |
| 270 | + * @param Indexable $indexable The Yoast indexable that we're checking. | |
| 271 | + * @param string $field The field that we're checking. | |
| 272 | + * | |
| 273 | + * @return bool True if default value. | |
| 274 | + */ | |
| 275 | + public function check_if_default_field( $indexable, $field ) { | |
| 276 | + $defaults = $this->default_values; | |
| 277 | + if ( ! isset( $defaults[ $field ] ) ) { | |
| 278 | + return false; | |
| 279 | + } | |
| 280 | + | |
| 281 | + if ( $indexable->$field === $defaults[ $field ]['default_value'] ) { | |
| 282 | + return true; | |
| 283 | + } | |
| 284 | + | |
| 285 | + return false; | |
| 286 | + } | |
| 287 | + | |
| 288 | + /** | |
| 289 | + * Saves and returns an indexable (on production environments only). | |
| 290 | + * | |
| 291 | + * Moved from Yoast\WP\SEO\Builders\Indexable_Builder. | |
| 292 | + * | |
| 293 | + * @param Indexable $indexable The indexable. | |
| 294 | + * @param Indexable|null $indexable_before The indexable before possible changes. | |
| 295 | + * | |
| 296 | + * @return bool True if default value. | |
| 297 | + */ | |
| 298 | + public function save_indexable( $indexable, $indexable_before = null ) { | |
| 299 | + if ( ! $this->should_index_indexable( $indexable ) ) { | |
| 300 | + return $indexable; | |
| 301 | + } | |
| 302 | + | |
| 303 | + // Save the indexable before running the WordPress hook. | |
| 304 | + $indexable->save(); | |
| 305 | + | |
| 306 | + if ( $indexable_before ) { | |
| 307 | + /** | |
| 308 | + * Action: 'wpseo_save_indexable' - Allow developers to perform an action | |
| 309 | + * when the indexable is updated. | |
| 310 | + * | |
| 311 | + * @param Indexable $indexable The saved indexable. | |
| 312 | + * @param Indexable $indexable_before The indexable before saving. | |
| 313 | + */ | |
| 314 | + \do_action( 'wpseo_save_indexable', $indexable, $indexable_before ); | |
| 315 | + } | |
| 316 | + | |
| 317 | + /** | |
| 318 | + * Action: 'wpseo_save_indexable' - Allow developers to perform an action | |
| 319 | + * right after the indexable is created or updated. | |
| 320 | + * | |
| 321 | + * @param Indexable $indexable The saved indexable. | |
| 322 | + */ | |
| 323 | + \do_action( 'wpseo_saved_indexable', $indexable ); | |
| 324 | + | |
| 325 | + return $indexable; | |
| 161 | 326 | } |
| 162 | 327 | } |