| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Actions\Indexing; |
| 4 |
|
| 5 |
/** |
| 6 |
* Base class of indexing actions. |
| 7 |
*/ |
| 8 |
abstract class Abstract_Indexing_Action implements Indexation_Action_Interface, Limited_Indexing_Action_Interface { |
| 9 |
|
| 10 |
/** |
| 11 |
* The transient name. |
| 12 |
* |
| 13 |
* This is a trick to force derived classes to define a transient themselves. |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
public const UNINDEXED_COUNT_TRANSIENT = null; |
| 18 |
|
| 19 |
/** |
| 20 |
* The transient cache key for limited counts. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public const UNINDEXED_LIMITED_COUNT_TRANSIENT = self::UNINDEXED_COUNT_TRANSIENT . '_limited'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Builds a query for selecting the ID's of unindexed posts. |
| 28 |
* |
| 29 |
* @param bool $limit The maximum number of post IDs to return. |
| 30 |
* |
| 31 |
* @return string The prepared query string. |
| 32 |
*/ |
| 33 |
abstract protected function get_select_query( $limit ); |
| 34 |
|
| 35 |
/** |
| 36 |
* Builds a query for counting the number of unindexed posts. |
| 37 |
* |
| 38 |
* @return string The prepared query string. |
| 39 |
*/ |
| 40 |
abstract protected function get_count_query(); |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns a limited number of unindexed posts. |
| 44 |
* |
| 45 |
* @param int $limit Limit the maximum number of unindexed posts that are counted. |
| 46 |
* |
| 47 |
* @return int The limited number of unindexed posts. 0 if the query fails. |
| 48 |
*/ |
| 49 |
public function get_limited_unindexed_count( $limit ) { |
| 50 |
$transient = \get_transient( static::UNINDEXED_LIMITED_COUNT_TRANSIENT ); |
| 51 |
if ( $transient !== false ) { |
| 52 |
return (int) $transient; |
| 53 |
} |
| 54 |
|
| 55 |
\set_transient( static::UNINDEXED_LIMITED_COUNT_TRANSIENT, 0, ( \MINUTE_IN_SECONDS * 15 ) ); |
| 56 |
|
| 57 |
$query = $this->get_select_query( $limit ); |
| 58 |
|
| 59 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Function get_count_query returns a prepared query. |
| 60 |
$unindexed_object_ids = ( $query === '' ) ? [] : $this->wpdb->get_col( $query ); |
| 61 |
$count = (int) \count( $unindexed_object_ids ); |
| 62 |
|
| 63 |
\set_transient( static::UNINDEXED_LIMITED_COUNT_TRANSIENT, $count, ( \MINUTE_IN_SECONDS * 15 ) ); |
| 64 |
|
| 65 |
return $count; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Returns the total number of unindexed posts. |
| 70 |
* |
| 71 |
* @return int|false The total number of unindexed posts. False if the query fails. |
| 72 |
*/ |
| 73 |
public function get_total_unindexed() { |
| 74 |
$transient = \get_transient( static::UNINDEXED_COUNT_TRANSIENT ); |
| 75 |
if ( $transient !== false ) { |
| 76 |
return (int) $transient; |
| 77 |
} |
| 78 |
|
| 79 |
// Store transient before doing the query so multiple requests won't make multiple queries. |
| 80 |
// Only store this for 15 minutes to ensure that if the query doesn't complete a wrong count is not kept too long. |
| 81 |
\set_transient( static::UNINDEXED_COUNT_TRANSIENT, 0, ( \MINUTE_IN_SECONDS * 15 ) ); |
| 82 |
|
| 83 |
$query = $this->get_count_query(); |
| 84 |
|
| 85 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Function get_count_query returns a prepared query. |
| 86 |
$count = ( $query === '' ) ? 0 : $this->wpdb->get_var( $query ); |
| 87 |
|
| 88 |
if ( $count === null ) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
\set_transient( static::UNINDEXED_COUNT_TRANSIENT, $count, \DAY_IN_SECONDS ); |
| 93 |
|
| 94 |
/** |
| 95 |
* Action: 'wpseo_indexables_unindexed_calculated' - sets an option to timestamp when there are no unindexed indexables left. |
| 96 |
* |
| 97 |
* @internal |
| 98 |
*/ |
| 99 |
\do_action( 'wpseo_indexables_unindexed_calculated', static::UNINDEXED_COUNT_TRANSIENT, $count ); |
| 100 |
|
| 101 |
return (int) $count; |
| 102 |
} |
| 103 |
} |
| 104 |
|