| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Actions\Indexing\Indexable_General_Indexation_Action; |
| 6 |
use Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Indexation_Action; |
| 7 |
use Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Type_Archive_Indexation_Action; |
| 8 |
use Yoast\WP\SEO\Actions\Indexing\Indexable_Term_Indexation_Action; |
| 9 |
use Yoast\WP\SEO\Actions\Indexing\Indexation_Action_Interface; |
| 10 |
use Yoast\WP\SEO\Actions\Indexing\Limited_Indexing_Action_Interface; |
| 11 |
use Yoast\WP\SEO\Actions\Indexing\Post_Link_Indexing_Action; |
| 12 |
use Yoast\WP\SEO\Actions\Indexing\Term_Link_Indexing_Action; |
| 13 |
use Yoast\WP\SEO\Config\Indexing_Reasons; |
| 14 |
use Yoast\WP\SEO\Integrations\Admin\Indexing_Notification_Integration; |
| 15 |
use Yoast_Notification_Center; |
| 16 |
|
| 17 |
/** |
| 18 |
* A helper object for indexing. |
| 19 |
*/ |
| 20 |
class Indexing_Helper { |
| 21 |
|
| 22 |
/** |
| 23 |
* The options helper. |
| 24 |
* |
| 25 |
* @var Options_Helper |
| 26 |
*/ |
| 27 |
protected $options_helper; |
| 28 |
|
| 29 |
/** |
| 30 |
* The date helper. |
| 31 |
* |
| 32 |
* @var Date_Helper |
| 33 |
*/ |
| 34 |
protected $date_helper; |
| 35 |
|
| 36 |
/** |
| 37 |
* The notification center. |
| 38 |
* |
| 39 |
* @var Yoast_Notification_Center |
| 40 |
*/ |
| 41 |
protected $notification_center; |
| 42 |
|
| 43 |
/** |
| 44 |
* The indexation actions. |
| 45 |
* |
| 46 |
* @var Indexation_Action_Interface[]|Limited_Indexing_Action_Interface[] |
| 47 |
*/ |
| 48 |
protected $indexing_actions; |
| 49 |
|
| 50 |
/** |
| 51 |
* Indexing_Helper constructor. |
| 52 |
* |
| 53 |
* @param Options_Helper $options_helper The options helper. |
| 54 |
* @param Date_Helper $date_helper The date helper. |
| 55 |
* @param Yoast_Notification_Center $notification_center The notification center. |
| 56 |
*/ |
| 57 |
public function __construct( |
| 58 |
Options_Helper $options_helper, |
| 59 |
Date_Helper $date_helper, |
| 60 |
Yoast_Notification_Center $notification_center |
| 61 |
) { |
| 62 |
$this->options_helper = $options_helper; |
| 63 |
$this->date_helper = $date_helper; |
| 64 |
$this->notification_center = $notification_center; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Sets the actions. |
| 69 |
* |
| 70 |
* @required |
| 71 |
* |
| 72 |
* @param Indexable_Post_Indexation_Action $post_indexation The post indexing action. |
| 73 |
* @param Indexable_Term_Indexation_Action $term_indexation The term indexing action. |
| 74 |
* @param Indexable_Post_Type_Archive_Indexation_Action $post_type_archive_indexation The posttype indexing action. |
| 75 |
* @param Indexable_General_Indexation_Action $general_indexation The general indexing (homepage etc) action. |
| 76 |
* @param Post_Link_Indexing_Action $post_link_indexing_action The post crosslink indexing action. |
| 77 |
* @param Term_Link_Indexing_Action $term_link_indexing_action The term crossling indexing action. |
| 78 |
*/ |
| 79 |
public function set_indexing_actions( |
| 80 |
Indexable_Post_Indexation_Action $post_indexation, |
| 81 |
Indexable_Term_Indexation_Action $term_indexation, |
| 82 |
Indexable_Post_Type_Archive_Indexation_Action $post_type_archive_indexation, |
| 83 |
Indexable_General_Indexation_Action $general_indexation, |
| 84 |
Post_Link_Indexing_Action $post_link_indexing_action, |
| 85 |
Term_Link_Indexing_Action $term_link_indexing_action |
| 86 |
) { |
| 87 |
$this->indexing_actions = [ |
| 88 |
$post_indexation, |
| 89 |
$term_indexation, |
| 90 |
$post_type_archive_indexation, |
| 91 |
$general_indexation, |
| 92 |
$post_link_indexing_action, |
| 93 |
$term_link_indexing_action, |
| 94 |
]; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Sets several database options when the indexing process is started. |
| 99 |
* |
| 100 |
* @deprecated 17.4 This method was renamed to prepare for internal consistency. |
| 101 |
* @codeCoverageIgnore |
| 102 |
* |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function start() { |
| 106 |
$this->prepare(); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Prepares the indexing process by setting several database options and removing the indexing notification. |
| 111 |
* |
| 112 |
* @return void |
| 113 |
*/ |
| 114 |
public function prepare() { |
| 115 |
$this->set_first_time( false ); |
| 116 |
$this->set_started( $this->date_helper->current_time() ); |
| 117 |
$this->remove_indexing_notification(); |
| 118 |
// Do not set_reason here; if the process is cancelled, the reason to start indexing is still valid. |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Sets several database options when the indexing process is finished. |
| 123 |
* |
| 124 |
* @deprecated 17.4 This method was renamed to complete for internal consistency. |
| 125 |
* @codeCoverageIgnore |
| 126 |
* |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
public function finish() { |
| 130 |
$this->complete(); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Sets several database options when the indexing process is finished. |
| 135 |
* |
| 136 |
* @return void |
| 137 |
*/ |
| 138 |
public function complete() { |
| 139 |
$this->set_reason( '' ); |
| 140 |
$this->set_started( null ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Sets appropriate flags when the indexing process fails. |
| 145 |
* |
| 146 |
* @return void |
| 147 |
*/ |
| 148 |
public function indexing_failed() { |
| 149 |
$this->set_reason( Indexing_Reasons::REASON_INDEXING_FAILED ); |
| 150 |
$this->set_started( null ); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Sets the indexing reason. |
| 155 |
* |
| 156 |
* @param string $reason The indexing reason. |
| 157 |
* |
| 158 |
* @return void |
| 159 |
*/ |
| 160 |
public function set_reason( $reason ) { |
| 161 |
$this->options_helper->set( 'indexing_reason', $reason ); |
| 162 |
$this->remove_indexing_notification(); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Removes any pre-existing notification, so that a new notification (with a possible new reason) can be added. |
| 167 |
*/ |
| 168 |
protected function remove_indexing_notification() { |
| 169 |
$this->notification_center->remove_notification_by_id( |
| 170 |
Indexing_Notification_Integration::NOTIFICATION_ID |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Determines whether an indexing reason has been set in the options. |
| 176 |
* |
| 177 |
* @return bool Whether an indexing reason has been set in the options. |
| 178 |
*/ |
| 179 |
public function has_reason() { |
| 180 |
$reason = $this->get_reason(); |
| 181 |
|
| 182 |
return ! empty( $reason ); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Returns the indexing reason. The reason why the site-wide indexing process should be run. |
| 187 |
* |
| 188 |
* @return string The indexing reason, defaults to the empty string if no reason has been set. |
| 189 |
*/ |
| 190 |
public function get_reason() { |
| 191 |
return $this->options_helper->get( 'indexing_reason', '' ); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Sets the start time when the indexing process has started but not completed. |
| 196 |
* |
| 197 |
* @param int|bool $timestamp The start time when the indexing process has started but not completed, false otherwise. |
| 198 |
* |
| 199 |
* @return void |
| 200 |
*/ |
| 201 |
public function set_started( $timestamp ) { |
| 202 |
$this->options_helper->set( 'indexing_started', $timestamp ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Gets the start time when the indexing process has started but not completed. |
| 207 |
* |
| 208 |
* @return int|bool The start time when the indexing process has started but not completed, false otherwise. |
| 209 |
*/ |
| 210 |
public function get_started() { |
| 211 |
return $this->options_helper->get( 'indexing_started' ); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Sets a boolean that indicates whether or not a site still has to be indexed for the first time. |
| 216 |
* |
| 217 |
* @param bool $is_first_time_indexing Whether or not a site still has to be indexed for the first time. |
| 218 |
* |
| 219 |
* @return void |
| 220 |
*/ |
| 221 |
public function set_first_time( $is_first_time_indexing ) { |
| 222 |
$this->options_helper->set( 'indexing_first_time', $is_first_time_indexing ); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Gets a boolean that indicates whether or not the site still has to be indexed for the first time. |
| 227 |
* |
| 228 |
* @return bool Whether the site still has to be indexed for the first time. |
| 229 |
*/ |
| 230 |
public function is_initial_indexing() { |
| 231 |
return $this->options_helper->get( 'indexing_first_time', true ); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Returns the total number of unindexed objects. |
| 236 |
* |
| 237 |
* @return int The total number of unindexed objects. |
| 238 |
*/ |
| 239 |
public function get_unindexed_count() { |
| 240 |
$unindexed_count = 0; |
| 241 |
|
| 242 |
foreach ( $this->indexing_actions as $indexing_action ) { |
| 243 |
$unindexed_count += $indexing_action->get_total_unindexed(); |
| 244 |
} |
| 245 |
|
| 246 |
return $unindexed_count; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Returns the total number of unindexed objects and applies a filter for third party integrations. |
| 251 |
* |
| 252 |
* @return int The total number of unindexed objects. |
| 253 |
*/ |
| 254 |
public function get_filtered_unindexed_count() { |
| 255 |
$unindexed_count = $this->get_unindexed_count(); |
| 256 |
|
| 257 |
/** |
| 258 |
* Filter: 'wpseo_indexing_get_unindexed_count' - Allow changing the amount of unindexed objects. |
| 259 |
* |
| 260 |
* @param int $unindexed_count The amount of unindexed objects. |
| 261 |
*/ |
| 262 |
return \apply_filters( 'wpseo_indexing_get_unindexed_count', $unindexed_count ); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Returns a limited number of unindexed objects. |
| 267 |
* |
| 268 |
* @param int $limit Limit the number of unindexed objects that are counted. |
| 269 |
* |
| 270 |
* @return int The total number of unindexed objects. |
| 271 |
*/ |
| 272 |
public function get_limited_unindexed_count( $limit ) { |
| 273 |
$unindexed_count = 0; |
| 274 |
|
| 275 |
foreach ( $this->indexing_actions as $indexing_action ) { |
| 276 |
$unindexed_count += $indexing_action->get_limited_unindexed_count( $limit - $unindexed_count + 1 ); |
| 277 |
if ( $unindexed_count > $limit ) { |
| 278 |
return $unindexed_count; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
return $unindexed_count; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Returns the total number of unindexed objects and applies a filter for third party integrations. |
| 287 |
* |
| 288 |
* @param int $limit Limit the number of unindexed objects that are counted. |
| 289 |
* |
| 290 |
* @return int The total number of unindexed objects. |
| 291 |
*/ |
| 292 |
public function get_limited_filtered_unindexed_count( $limit ) { |
| 293 |
$unindexed_count = $this->get_limited_unindexed_count( $limit ); |
| 294 |
|
| 295 |
if ( $unindexed_count > $limit ) { |
| 296 |
return $unindexed_count; |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Filter: 'wpseo_indexing_get_limited_unindexed_count' - Allow changing the amount of unindexed objects, |
| 301 |
* and allow for a maximum number of items counted to improve performance. |
| 302 |
* |
| 303 |
* @param int $unindexed_count The amount of unindexed objects. |
| 304 |
* @param int|false $limit Limit the number of unindexed objects that need to be counted. |
| 305 |
* False if it doesn't need to be limited. |
| 306 |
*/ |
| 307 |
return \apply_filters( 'wpseo_indexing_get_limited_unindexed_count', $unindexed_count, $limit ); |
| 308 |
} |
| 309 |
} |
| 310 |
|