PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.6
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / actions / importing / aioseo / aioseo-cleanup-action.php

aioseo-cleanup-action.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.6, at src/actions/importing/aioseo/aioseo-cleanup-action.php

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