| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Watchers; |
| 4 |
|
| 5 |
use WP_CLI; |
| 6 |
use Yoast\WP\SEO\Conditionals\Migrations_Conditional; |
| 7 |
use Yoast\WP\SEO\Config\Indexing_Reasons; |
| 8 |
use Yoast\WP\SEO\Helpers\Indexable_Helper; |
| 9 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 10 |
use Yoast\WP\SEO\Helpers\Post_Type_Helper; |
| 11 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 12 |
|
| 13 |
/** |
| 14 |
* Home url option watcher. |
| 15 |
* |
| 16 |
* Handles updates to the home URL option for the Indexables table. |
| 17 |
*/ |
| 18 |
class Indexable_HomeUrl_Watcher implements Integration_Interface { |
| 19 |
|
| 20 |
/** |
| 21 |
* Represents the options helper. |
| 22 |
* |
| 23 |
* @var Options_Helper |
| 24 |
*/ |
| 25 |
protected $options_helper; |
| 26 |
|
| 27 |
/** |
| 28 |
* The post type helper. |
| 29 |
* |
| 30 |
* @var Post_Type_Helper |
| 31 |
*/ |
| 32 |
private $post_type; |
| 33 |
|
| 34 |
/** |
| 35 |
* The indexable helper. |
| 36 |
* |
| 37 |
* @var Indexable_Helper |
| 38 |
*/ |
| 39 |
protected $indexable_helper; |
| 40 |
|
| 41 |
/** |
| 42 |
* Returns the conditionals based on which this loadable should be active. |
| 43 |
* |
| 44 |
* @return array |
| 45 |
*/ |
| 46 |
public static function get_conditionals() { |
| 47 |
return [ Migrations_Conditional::class ]; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Indexable_HomeUrl_Watcher constructor. |
| 52 |
* |
| 53 |
* @param Post_Type_Helper $post_type The post type helper. |
| 54 |
* @param Options_Helper $options The options helper. |
| 55 |
* @param Indexable_Helper $indexable The indexable helper. |
| 56 |
*/ |
| 57 |
public function __construct( Post_Type_Helper $post_type, Options_Helper $options, Indexable_Helper $indexable ) { |
| 58 |
$this->post_type = $post_type; |
| 59 |
$this->options_helper = $options; |
| 60 |
$this->indexable_helper = $indexable; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Initializes the integration. |
| 65 |
* |
| 66 |
* This is the place to register hooks and filters. |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function register_hooks() { |
| 71 |
\add_action( 'update_option_home', [ $this, 'reset_permalinks' ] ); |
| 72 |
\add_action( 'wpseo_permalink_structure_check', [ $this, 'force_reset_permalinks' ] ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Resets the permalinks for everything that is related to the permalink structure. |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function reset_permalinks() { |
| 81 |
$this->indexable_helper->reset_permalink_indexables( null, null, Indexing_Reasons::REASON_HOME_URL_OPTION ); |
| 82 |
|
| 83 |
// Reset the home_url option. |
| 84 |
$this->options_helper->set( 'home_url', \get_home_url() ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Resets the permalink indexables automatically, if necessary. |
| 89 |
* |
| 90 |
* @return bool Whether the request ran. |
| 91 |
*/ |
| 92 |
public function force_reset_permalinks() { |
| 93 |
if ( $this->should_reset_permalinks() ) { |
| 94 |
$this->reset_permalinks(); |
| 95 |
|
| 96 |
if ( \defined( 'WP_CLI' ) && \WP_CLI ) { |
| 97 |
WP_CLI::success( \__( 'All permalinks were successfully reset', 'wordpress-seo' ) ); |
| 98 |
} |
| 99 |
|
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Checks whether permalinks should be reset. |
| 108 |
* |
| 109 |
* @return bool Whether the permalinks should be reset. |
| 110 |
*/ |
| 111 |
public function should_reset_permalinks() { |
| 112 |
return \get_home_url() !== $this->options_helper->get( 'home_url' ); |
| 113 |
} |
| 114 |
} |
| 115 |
|