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