PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.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 / commands / cleanup-command.php

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

197 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 *
49 * ## OPTIONS
50 *
51 * [--batch-size=<batch-size>]
52 * : The number of database records to clean up in a single sql query.
53 * ---
54 * default: 1000
55 * ---
56 *
57 * [--interval=<interval>]
58 * : The number of microseconds (millionths of a second) to wait between cleanup batches.
59 * ---
60 * default: 500000
61 * ---
62 *
63 * [--network]
64 * : Performs the cleanup on all sites within the network.
65 *
66 * ## EXAMPLES
67 *
68 * wp yoast cleanup
69 *
70 * @when after_wp_load
71 *
72 * @param array|null $args The arguments.
73 * @param array|null $assoc_args The associative arguments.
74 *
75 * @return void
76 *
77 * @throws ExitException When the input args are invalid.
78 */
79 public function cleanup( $args = null, $assoc_args = null ) {
80 if ( isset( $assoc_args['interval'] ) && (int) $assoc_args['interval'] < 0 ) {
81 WP_CLI::error( \__( 'The value for \'interval\' must be a positive integer.', 'wordpress-seo' ) );
82 }
83 if ( isset( $assoc_args['batch-size'] ) && (int) $assoc_args['batch-size'] < 1 ) {
84 WP_CLI::error( \__( 'The value for \'batch-size\' must be a positive integer higher than equal to 1.', 'wordpress-seo' ) );
85 }
86
87 if ( isset( $assoc_args['network'] ) && \is_multisite() ) {
88 $total_removed = $this->cleanup_network( $assoc_args );
89 }
90 else {
91 $total_removed = $this->cleanup_current_site( $assoc_args );
92 }
93
94 WP_CLI::success(
95 \sprintf(
96 /* translators: %1$d is the number of records that are removed. */
97 \_n(
98 'Cleaned up %1$d record.',
99 'Cleaned up %1$d records.',
100 $total_removed,
101 'wordpress-seo',
102 ),
103 $total_removed,
104 ),
105 );
106 }
107
108 /**
109 * Performs the cleanup for the entire network.
110 *
111 * @param array|null $assoc_args The associative arguments.
112 *
113 * @return int The number of cleaned up records.
114 */
115 private function cleanup_network( $assoc_args ) {
116 $criteria = [
117 'fields' => 'ids',
118 'spam' => 0,
119 'deleted' => 0,
120 'archived' => 0,
121 ];
122 $blog_ids = \get_sites( $criteria );
123 $total_removed = 0;
124 foreach ( $blog_ids as $blog_id ) {
125 \switch_to_blog( $blog_id );
126 $total_removed += $this->cleanup_current_site( $assoc_args );
127 \restore_current_blog();
128 }
129
130 return $total_removed;
131 }
132
133 /**
134 * Performs the cleanup for a single site.
135 *
136 * @param array|null $assoc_args The associative arguments.
137 *
138 * @return int The number of cleaned up records.
139 */
140 private function cleanup_current_site( $assoc_args ) {
141 $site_url = \site_url();
142 $total_removed = 0;
143
144 if ( ! \is_plugin_active( \WPSEO_BASENAME ) ) {
145 /* translators: %1$s is the site url of the site that is skipped. %2$s is Yoast SEO. */
146 WP_CLI::warning( \sprintf( \__( 'Skipping %1$s. %2$s is not active on this site.', 'wordpress-seo' ), $site_url, 'Yoast SEO' ) );
147
148 return $total_removed;
149 }
150
151 // Make sure the DB is up to date first.
152 \do_action( '_yoast_run_migrations' );
153
154 $tasks = $this->cleanup_integration->get_cleanup_tasks();
155 $limit = (int) $assoc_args['batch-size'];
156 $interval = (int) $assoc_args['interval'];
157
158 /* 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. */
159 $progress_bar_title_format = \__( 'Cleaning up %1$s [%2$s]', 'wordpress-seo' );
160 $progress = Utils\make_progress_bar( \sprintf( $progress_bar_title_format, $site_url, \key( $tasks ) ), \count( $tasks ) );
161
162 foreach ( $tasks as $task_name => $task ) {
163 // Update the progressbar title with the current task name.
164 $progress->tick( 0, \sprintf( $progress_bar_title_format, $site_url, $task_name ) );
165 do {
166 $items_cleaned = $task( $limit );
167 if ( \is_int( $items_cleaned ) ) {
168 $total_removed += $items_cleaned;
169 }
170 \usleep( $interval );
171
172 // Update the timer.
173 $progress->tick( 0 );
174 } while ( $items_cleaned !== false && $items_cleaned > 0 );
175 $progress->tick();
176 }
177 $progress->finish();
178
179 $this->cleanup_integration->reset_cleanup();
180 WP_CLI::log(
181 \sprintf(
182 /* translators: %1$d is the number of records that were removed. %2$s is the site url. */
183 \_n(
184 'Cleaned up %1$d record from %2$s.',
185 'Cleaned up %1$d records from %2$s.',
186 $total_removed,
187 'wordpress-seo',
188 ),
189 $total_removed,
190 $site_url,
191 ),
192 );
193
194 return $total_removed;
195 }
196 }
197