PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.7
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.7
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 27.7, at src/actions/importing/aioseo/aioseo-cleanup-action.php

174 lines 4.5 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 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