| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Actions\Indexing; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Models\Indexable; |
| 6 |
use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 7 |
|
| 8 |
/** |
| 9 |
* General reindexing action for indexables. |
| 10 |
*/ |
| 11 |
class Indexable_General_Indexation_Action implements Indexation_Action_Interface, Limited_Indexing_Action_Interface { |
| 12 |
|
| 13 |
/** |
| 14 |
* The transient cache key. |
| 15 |
*/ |
| 16 |
public const UNINDEXED_COUNT_TRANSIENT = 'wpseo_total_unindexed_general_items'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Represents the indexables repository. |
| 20 |
* |
| 21 |
* @var Indexable_Repository |
| 22 |
*/ |
| 23 |
protected $indexable_repository; |
| 24 |
|
| 25 |
/** |
| 26 |
* Indexable_General_Indexation_Action constructor. |
| 27 |
* |
| 28 |
* @param Indexable_Repository $indexable_repository The indexables repository. |
| 29 |
*/ |
| 30 |
public function __construct( Indexable_Repository $indexable_repository ) { |
| 31 |
$this->indexable_repository = $indexable_repository; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Returns the total number of unindexed objects. |
| 36 |
* |
| 37 |
* @return int The total number of unindexed objects. |
| 38 |
*/ |
| 39 |
public function get_total_unindexed() { |
| 40 |
$transient = \get_transient( static::UNINDEXED_COUNT_TRANSIENT ); |
| 41 |
if ( $transient !== false ) { |
| 42 |
return (int) $transient; |
| 43 |
} |
| 44 |
|
| 45 |
$indexables_to_create = $this->query(); |
| 46 |
|
| 47 |
$result = \count( $indexables_to_create ); |
| 48 |
|
| 49 |
\set_transient( static::UNINDEXED_COUNT_TRANSIENT, $result, \DAY_IN_SECONDS ); |
| 50 |
|
| 51 |
/** |
| 52 |
* Action: 'wpseo_indexables_unindexed_calculated' - sets an option to timestamp when there are no unindexed indexables left. |
| 53 |
* |
| 54 |
* @internal |
| 55 |
*/ |
| 56 |
\do_action( 'wpseo_indexables_unindexed_calculated', static::UNINDEXED_COUNT_TRANSIENT, $result ); |
| 57 |
|
| 58 |
return $result; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Returns a limited number of unindexed posts. |
| 63 |
* |
| 64 |
* @param int $limit Limit the maximum number of unindexed posts that are counted. |
| 65 |
* |
| 66 |
* @return int|false The limited number of unindexed posts. False if the query fails. |
| 67 |
*/ |
| 68 |
public function get_limited_unindexed_count( $limit ) { |
| 69 |
return $this->get_total_unindexed(); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Creates indexables for unindexed system pages, the date archive, and the homepage. |
| 74 |
* |
| 75 |
* @return Indexable[] The created indexables. |
| 76 |
*/ |
| 77 |
public function index() { |
| 78 |
$indexables = []; |
| 79 |
$indexables_to_create = $this->query(); |
| 80 |
|
| 81 |
if ( isset( $indexables_to_create['404'] ) ) { |
| 82 |
$indexables[] = $this->indexable_repository->find_for_system_page( '404' ); |
| 83 |
} |
| 84 |
|
| 85 |
if ( isset( $indexables_to_create['search'] ) ) { |
| 86 |
$indexables[] = $this->indexable_repository->find_for_system_page( 'search-result' ); |
| 87 |
} |
| 88 |
|
| 89 |
if ( isset( $indexables_to_create['date_archive'] ) ) { |
| 90 |
$indexables[] = $this->indexable_repository->find_for_date_archive(); |
| 91 |
} |
| 92 |
if ( isset( $indexables_to_create['home_page'] ) ) { |
| 93 |
$indexables[] = $this->indexable_repository->find_for_home_page(); |
| 94 |
} |
| 95 |
|
| 96 |
\set_transient( static::UNINDEXED_COUNT_TRANSIENT, 0, \DAY_IN_SECONDS ); |
| 97 |
|
| 98 |
return $indexables; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Returns the number of objects that will be indexed in a single indexing pass. |
| 103 |
* |
| 104 |
* @return int The limit. |
| 105 |
*/ |
| 106 |
public function get_limit() { |
| 107 |
// This matches the maximum number of indexables created by this action. |
| 108 |
return 4; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Check which indexables already exist and return the values of the ones to create. |
| 113 |
* |
| 114 |
* @return array The indexable types to create. |
| 115 |
*/ |
| 116 |
private function query() { |
| 117 |
$indexables_to_create = []; |
| 118 |
if ( ! $this->indexable_repository->find_for_system_page( '404', false ) ) { |
| 119 |
$indexables_to_create['404'] = true; |
| 120 |
} |
| 121 |
|
| 122 |
if ( ! $this->indexable_repository->find_for_system_page( 'search-result', false ) ) { |
| 123 |
$indexables_to_create['search'] = true; |
| 124 |
} |
| 125 |
|
| 126 |
if ( ! $this->indexable_repository->find_for_date_archive( false ) ) { |
| 127 |
$indexables_to_create['date_archive'] = true; |
| 128 |
} |
| 129 |
|
| 130 |
$need_home_page_indexable = ( (int) \get_option( 'page_on_front' ) === 0 && \get_option( 'show_on_front' ) === 'posts' ); |
| 131 |
|
| 132 |
if ( $need_home_page_indexable && ! $this->indexable_repository->find_for_home_page( false ) ) { |
| 133 |
$indexables_to_create['home_page'] = true; |
| 134 |
} |
| 135 |
|
| 136 |
return $indexables_to_create; |
| 137 |
} |
| 138 |
} |
| 139 |
|