| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Indexation_Action; |
| 6 |
use Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Type_Archive_Indexation_Action; |
| 7 |
use Yoast\WP\SEO\Actions\Indexing\Indexable_Term_Indexation_Action; |
| 8 |
use Yoast\WP\SEO\Config\Indexing_Reasons; |
| 9 |
use Yoast\WP\SEO\Models\Indexable; |
| 10 |
use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 11 |
|
| 12 |
/** |
| 13 |
* A helper object for indexables. |
| 14 |
*/ |
| 15 |
class Indexable_Helper { |
| 16 |
|
| 17 |
/** |
| 18 |
* Represents the indexable repository. |
| 19 |
* |
| 20 |
* @var Indexable_Repository |
| 21 |
*/ |
| 22 |
protected $repository; |
| 23 |
|
| 24 |
/** |
| 25 |
* Represents the options helper. |
| 26 |
* |
| 27 |
* @var Options_Helper |
| 28 |
*/ |
| 29 |
protected $options_helper; |
| 30 |
|
| 31 |
/** |
| 32 |
* Represents the environment helper. |
| 33 |
* |
| 34 |
* @var Environment_Helper |
| 35 |
*/ |
| 36 |
protected $environment_helper; |
| 37 |
|
| 38 |
/** |
| 39 |
* Represents the indexing helper. |
| 40 |
* |
| 41 |
* @var Indexing_Helper |
| 42 |
*/ |
| 43 |
protected $indexing_helper; |
| 44 |
|
| 45 |
/** |
| 46 |
* Indexable_Helper constructor. |
| 47 |
* |
| 48 |
* @param Options_Helper $options_helper The options helper. |
| 49 |
* @param Environment_Helper $environment_helper The environment helper. |
| 50 |
* @param Indexing_Helper $indexing_helper The indexing helper. |
| 51 |
*/ |
| 52 |
public function __construct( Options_Helper $options_helper, Environment_Helper $environment_helper, Indexing_Helper $indexing_helper ) { |
| 53 |
$this->options_helper = $options_helper; |
| 54 |
$this->environment_helper = $environment_helper; |
| 55 |
$this->indexing_helper = $indexing_helper; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Sets the indexable repository. Done to avoid circular dependencies. |
| 60 |
* |
| 61 |
* @required |
| 62 |
* |
| 63 |
* @param Indexable_Repository $repository The indexable repository. |
| 64 |
*/ |
| 65 |
public function set_indexable_repository( Indexable_Repository $repository ) { |
| 66 |
$this->repository = $repository; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Returns the page type of an indexable. |
| 71 |
* |
| 72 |
* @param Indexable $indexable The indexable. |
| 73 |
* |
| 74 |
* @return string|false The page type. False if it could not be determined. |
| 75 |
*/ |
| 76 |
public function get_page_type_for_indexable( $indexable ) { |
| 77 |
switch ( $indexable->object_type ) { |
| 78 |
case 'post': |
| 79 |
$front_page_id = (int) \get_option( 'page_on_front' ); |
| 80 |
if ( $indexable->object_id === $front_page_id ) { |
| 81 |
return 'Static_Home_Page'; |
| 82 |
} |
| 83 |
$posts_page_id = (int) \get_option( 'page_for_posts' ); |
| 84 |
if ( $indexable->object_id === $posts_page_id ) { |
| 85 |
return 'Static_Posts_Page'; |
| 86 |
} |
| 87 |
|
| 88 |
return 'Post_Type'; |
| 89 |
case 'term': |
| 90 |
return 'Term_Archive'; |
| 91 |
case 'user': |
| 92 |
return 'Author_Archive'; |
| 93 |
case 'home-page': |
| 94 |
return 'Home_Page'; |
| 95 |
case 'post-type-archive': |
| 96 |
return 'Post_Type_Archive'; |
| 97 |
case 'date-archive': |
| 98 |
return 'Date_Archive'; |
| 99 |
case 'system-page': |
| 100 |
if ( $indexable->object_sub_type === 'search-result' ) { |
| 101 |
return 'Search_Result_Page'; |
| 102 |
} |
| 103 |
if ( $indexable->object_sub_type === '404' ) { |
| 104 |
return 'Error_Page'; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Resets the permalinks of the indexables. |
| 113 |
* |
| 114 |
* @param string|null $type The type of the indexable. |
| 115 |
* @param string|null $subtype The subtype. Can be null. |
| 116 |
* @param string $reason The reason that the permalink has been changed. |
| 117 |
*/ |
| 118 |
public function reset_permalink_indexables( $type = null, $subtype = null, $reason = Indexing_Reasons::REASON_PERMALINK_SETTINGS ) { |
| 119 |
$result = $this->repository->reset_permalink( $type, $subtype ); |
| 120 |
|
| 121 |
$this->indexing_helper->set_reason( $reason ); |
| 122 |
|
| 123 |
if ( $result !== false && $result > 0 ) { |
| 124 |
\delete_transient( Indexable_Post_Indexation_Action::UNINDEXED_COUNT_TRANSIENT ); |
| 125 |
\delete_transient( Indexable_Post_Type_Archive_Indexation_Action::UNINDEXED_COUNT_TRANSIENT ); |
| 126 |
\delete_transient( Indexable_Term_Indexation_Action::UNINDEXED_COUNT_TRANSIENT ); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Determines whether indexing indexables is appropriate at this time. |
| 132 |
* |
| 133 |
* @return bool Whether or not the indexables should be indexed. |
| 134 |
*/ |
| 135 |
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(); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Returns whether or not dynamic permalinks should be used. |
| 142 |
* |
| 143 |
* @return bool Whether or not the dynamic permalinks should be used. |
| 144 |
*/ |
| 145 |
public function dynamic_permalinks_enabled() { |
| 146 |
/** |
| 147 |
* Filters the value of the `dynamic_permalinks` option. |
| 148 |
* |
| 149 |
* @param bool $value The value of the `dynamic_permalinks` option. |
| 150 |
*/ |
| 151 |
return (bool) \apply_filters( 'wpseo_dynamic_permalinks_enabled', $this->options_helper->get( 'dynamic_permalinks', false ) ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Sets a boolean to indicate that the indexing of the indexables has completed. |
| 156 |
* |
| 157 |
* @return void |
| 158 |
*/ |
| 159 |
public function finish_indexing() { |
| 160 |
$this->options_helper->set( 'indexables_indexing_completed', true ); |
| 161 |
} |
| 162 |
} |
| 163 |
|