PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.3
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 19.1 All 128 releases
wordpress-seo / src / commands / cleanup-command.php

cleanup-command.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.3, at src/commands/cleanup-command.php

198 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Commands;
4
5 use WP_CLI;
6 use WP_CLI\ExitException;
7 use WP_CLI\Utils;
8 use Yoast\WP\SEO\Integrations\Cleanup_Integration;
9 use Yoast\WP\SEO\Main;
10
11 /**
12 * A WP CLI command that helps with cleaning up unwanted records from our custom tables.
13 */
14 final class Cleanup_Command implements Command_Interface {
15
16 /**
17 * The integration that cleans up on cron.
18 *
19 * @var Cleanup_Integration
20 */
21 private $cleanup_integration;
22
23 /**
24 * The constructor.
25 *
26 * @param Cleanup_Integration $cleanup_integration The integration that cleans up on cron.
27 */
28 public function __construct( Cleanup_Integration $cleanup_integration ) {
29 $this->cleanup_integration = $cleanup_integration;
30 }
31
32 /**
33 * Returns the namespace of this command.
34 *
35 * @return string
36 */
37 public static function get_namespace() {
38 return Main::WP_CLI_NAMESPACE;
39 }
40
41 /**
42 * Performs a cleanup of custom Yoast tables.
43 *
44 * This removes unused, unwanted or orphaned database records, which ensures the best performance. Including:
45 * - Indexables
46 * - Indexable hierarchy
47 * - SEO links
48 * - Expired entries from the expiring store
49 *
50 * ## OPTIONS
51 *
52 * [--batch-size=<batch-size>]
53 * : The number of database records to clean up in a single sql query.
54 * ---
55 * default: 1000
56 * ---
57 *
58 * [--interval=<interval>]
59 * : The number of microseconds (millionths of a second) to wait between cleanup batches.
60 * ---
61 * default: 500000
62 * ---
63 *
64 * [--network]
65 * : Performs the cleanup on all sites within the network.
66 *
67 * ## EXAMPLES
68 *
69 * wp yoast cleanup
70 *
71 * @when after_wp_load
72 *
73 * @param array|null $args The arguments.
74 * @param array|null $assoc_args The associative arguments.
75 *
76 * @return void
77 *
78 * @throws ExitException When the input args are invalid.
79 */
80 public function cleanup( $args = null, $assoc_args = null ) {
81 if ( isset( $assoc_args['interval'] ) && (int) $assoc_args['interval'] < 0 ) {
82 WP_CLI::error( \__( 'The value for \'interval\' must be a positive integer.', 'wordpress-seo' ) );
83 }
84 if ( isset( $assoc_args['batch-size'] ) && (int) $assoc_args['batch-size'] < 1 ) {
85 WP_CLI::error( \__( 'The value for \'batch-size\' must be a positive integer higher than equal to 1.', 'wordpress-seo' ) );
86 }
87
88 if ( isset( $assoc_args['network'] ) && \is_multisite() ) {
89 $total_removed = $this->cleanup_network( $assoc_args );
90 }
91 else {
92 $total_removed = $this->cleanup_current_site( $assoc_args );
93 }
94
95 WP_CLI::success(
96 \sprintf(
97 /* translators: %1$d is the number of records that are removed. */
98 \_n(
99 'Cleaned up %1$d record.',
100 'Cleaned up %1$d records.',
101 $total_removed,
102 'wordpress-seo',
103 ),
104 $total_removed,
105 ),
106 );
107 }
108
109 /**
110 * Performs the cleanup for the entire network.
111 *
112 * @param array|null $assoc_args The associative arguments.
113 *
114 * @return int The number of cleaned up records.
115 */
116 private function cleanup_network( $assoc_args ) {
117 $criteria = [
118 'fields' => 'ids',
119 'spam' => 0,
120 'deleted' => 0,
121 'archived' => 0,
122 ];
123 $blog_ids = \get_sites( $criteria );
124 $total_removed = 0;
125 foreach ( $blog_ids as $blog_id ) {
126 \switch_to_blog( $blog_id );
127 $total_removed += $this->cleanup_current_site( $assoc_args );
128 \restore_current_blog();
129 }
130
131 return $total_removed;
132 }
133
134 /**
135 * Performs the cleanup for a single site.
136 *
137 * @param array|null $assoc_args The associative arguments.
138 *
139 * @return int The number of cleaned up records.
140 */
141 private function cleanup_current_site( $assoc_args ) {
142 $site_url = \site_url();
143 $total_removed = 0;
144
145 if ( ! \is_plugin_active( \WPSEO_BASENAME ) ) {
146 /* translators: %1$s is the site url of the site that is skipped. %2$s is Yoast SEO. */
147 WP_CLI::warning( \sprintf( \__( 'Skipping %1$s. %2$s is not active on this site.', 'wordpress-seo' ), $site_url, 'Yoast SEO' ) );
148
149 return $total_removed;
150 }
151
152 // Make sure the DB is up to date first.
153 \do_action( '_yoast_run_migrations' );
154
155 $tasks = $this->cleanup_integration->get_cleanup_tasks();
156 $limit = (int) $assoc_args['batch-size'];
157 $interval = (int) $assoc_args['interval'];
158
159 /* translators: %1$s is the site url of the site that is cleaned up. %2$s is the name of the cleanup task that is currently running. */
160 $progress_bar_title_format = \__( 'Cleaning up %1$s [%2$s]', 'wordpress-seo' );
161 $progress = Utils\make_progress_bar( \sprintf( $progress_bar_title_format, $site_url, \key( $tasks ) ), \count( $tasks ) );
162
163 foreach ( $tasks as $task_name => $task ) {
164 // Update the progressbar title with the current task name.
165 $progress->tick( 0, \sprintf( $progress_bar_title_format, $site_url, $task_name ) );
166 do {
167 $items_cleaned = $task( $limit );
168 if ( \is_int( $items_cleaned ) ) {
169 $total_removed += $items_cleaned;
170 }
171 \usleep( $interval );
172
173 // Update the timer.
174 $progress->tick( 0 );
175 } while ( $items_cleaned !== false && $items_cleaned > 0 );
176 $progress->tick();
177 }
178 $progress->finish();
179
180 $this->cleanup_integration->reset_cleanup();
181 WP_CLI::log(
182 \sprintf(
183 /* translators: %1$d is the number of records that were removed. %2$s is the site url. */
184 \_n(
185 'Cleaned up %1$d record from %2$s.',
186 'Cleaned up %1$d records from %2$s.',
187 $total_removed,
188 'wordpress-seo',
189 ),
190 $total_removed,
191 $site_url,
192 ),
193 );
194
195 return $total_removed;
196 }
197 }
198