| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Given it's a very specific case. |
| 4 |
namespace Yoast\WP\SEO\Actions\Importing\Aioseo; |
| 5 |
|
| 6 |
use wpdb; |
| 7 |
use Yoast\WP\SEO\Actions\Importing\Abstract_Aioseo_Importing_Action; |
| 8 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 9 |
|
| 10 |
/** |
| 11 |
* Importing action for cleaning up AIOSEO data. |
| 12 |
*/ |
| 13 |
class Aioseo_Cleanup_Action extends Abstract_Aioseo_Importing_Action { |
| 14 |
|
| 15 |
/** |
| 16 |
* The plugin of the action. |
| 17 |
*/ |
| 18 |
public const PLUGIN = 'aioseo'; |
| 19 |
|
| 20 |
/** |
| 21 |
* The type of the action. |
| 22 |
*/ |
| 23 |
public const TYPE = 'cleanup'; |
| 24 |
|
| 25 |
/** |
| 26 |
* The AIOSEO meta_keys to be cleaned up. |
| 27 |
* |
| 28 |
* @var array<string> |
| 29 |
*/ |
| 30 |
protected $aioseo_postmeta_keys = [ |
| 31 |
'_aioseo_title', |
| 32 |
'_aioseo_description', |
| 33 |
'_aioseo_og_title', |
| 34 |
'_aioseo_og_description', |
| 35 |
'_aioseo_twitter_title', |
| 36 |
'_aioseo_twitter_description', |
| 37 |
]; |
| 38 |
|
| 39 |
/** |
| 40 |
* The WordPress database instance. |
| 41 |
* |
| 42 |
* @var wpdb |
| 43 |
*/ |
| 44 |
protected $wpdb; |
| 45 |
|
| 46 |
/** |
| 47 |
* Class constructor. |
| 48 |
* |
| 49 |
* @param wpdb $wpdb The WordPress database instance. |
| 50 |
* @param Options_Helper $options The options helper. |
| 51 |
*/ |
| 52 |
public function __construct( wpdb $wpdb, Options_Helper $options ) { |
| 53 |
$this->wpdb = $wpdb; |
| 54 |
$this->options = $options; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Retrieves the postmeta along with the db prefix. |
| 59 |
* |
| 60 |
* @return string The postmeta table name along with the db prefix. |
| 61 |
*/ |
| 62 |
protected function get_postmeta_table() { |
| 63 |
return $this->wpdb->prefix . 'postmeta'; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Just checks if the cleanup has been completed in the past. |
| 68 |
* |
| 69 |
* @return int The total number of unimported objects. |
| 70 |
*/ |
| 71 |
public function get_total_unindexed() { |
| 72 |
if ( ! $this->aioseo_helper->aioseo_exists() ) { |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
return ( ! $this->get_completed() ) ? 1 : 0; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Just checks if the cleanup has been completed in the past. |
| 81 |
* |
| 82 |
* @param int $limit The maximum number of unimported objects to be returned. |
| 83 |
* |
| 84 |
* @return int|false The limited number of unindexed posts. False if the query fails. |
| 85 |
*/ |
| 86 |
public function get_limited_unindexed_count( $limit ) { |
| 87 |
if ( ! $this->aioseo_helper->aioseo_exists() ) { |
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
return ( ! $this->get_completed() ) ? 1 : 0; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Cleans up AIOSEO data. |
| 96 |
* |
| 97 |
* @return Indexable[]|false An array of created indexables or false if aioseo data was not found. |
| 98 |
*/ |
| 99 |
public function index() { |
| 100 |
if ( $this->get_completed() ) { |
| 101 |
return []; |
| 102 |
} |
| 103 |
|
| 104 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared -- Reason: There is no unescaped user input. |
| 105 |
$meta_data = $this->wpdb->query( $this->cleanup_postmeta_query() ); |
| 106 |
$aioseo_table_truncate_done = $this->wpdb->query( $this->truncate_query() ); |
| 107 |
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared |
| 108 |
|
| 109 |
if ( $meta_data === false && $aioseo_table_truncate_done === false ) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
$this->set_completed( true ); |
| 114 |
|
| 115 |
return [ |
| 116 |
'metadata_cleanup' => $meta_data, |
| 117 |
'indexables_cleanup' => $aioseo_table_truncate_done, |
| 118 |
]; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Creates a DELETE query string for deleting AIOSEO postmeta data. |
| 123 |
* |
| 124 |
* @return string The query to use for importing or counting the number of items to import. |
| 125 |
*/ |
| 126 |
public function cleanup_postmeta_query() { |
| 127 |
$table = $this->get_postmeta_table(); |
| 128 |
$meta_keys_to_delete = $this->aioseo_postmeta_keys; |
| 129 |
|
| 130 |
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Reason: There is no unescaped user input. |
| 131 |
return $this->wpdb->prepare( |
| 132 |
"DELETE FROM {$table} WHERE meta_key IN (" . \implode( ', ', \array_fill( 0, \count( $meta_keys_to_delete ), '%s' ) ) . ')', |
| 133 |
$meta_keys_to_delete, |
| 134 |
); |
| 135 |
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Creates a TRUNCATE query string for emptying the AIOSEO indexable table, if it exists. |
| 140 |
* |
| 141 |
* @return string The query to use for importing or counting the number of items to import. |
| 142 |
*/ |
| 143 |
public function truncate_query() { |
| 144 |
if ( ! $this->aioseo_helper->aioseo_exists() ) { |
| 145 |
// If the table doesn't exist, we need a string that will amount to a quick query that doesn't return false when ran. |
| 146 |
return 'SELECT 1'; |
| 147 |
} |
| 148 |
|
| 149 |
$table = $this->aioseo_helper->get_table(); |
| 150 |
|
| 151 |
return "TRUNCATE TABLE {$table}"; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Used nowhere. Exists to comply with the interface. |
| 156 |
* |
| 157 |
* @return int The limit. |
| 158 |
*/ |
| 159 |
public function get_limit() { |
| 160 |
/** |
| 161 |
* Filter 'wpseo_aioseo_cleanup_limit' - Allow filtering the number of posts indexed during each indexing pass. |
| 162 |
* |
| 163 |
* @param int $max_posts The maximum number of posts cleaned up. |
| 164 |
*/ |
| 165 |
$limit = \apply_filters( 'wpseo_aioseo_cleanup_limit', 25 ); |
| 166 |
|
| 167 |
if ( ! \is_int( $limit ) || $limit < 1 ) { |
| 168 |
$limit = 25; |
| 169 |
} |
| 170 |
|
| 171 |
return $limit; |
| 172 |
} |
| 173 |
} |
| 174 |
|