PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.5.3
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.5.3
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / vendor / woocommerce / action-scheduler / classes / migration / Controller.php

Controller.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.5.3, at vendor/woocommerce/action-scheduler/classes/migration/Controller.php

228 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Action_Scheduler\Migration;
4
5 use ActionScheduler_DataController;
6 use ActionScheduler_LoggerSchema;
7 use ActionScheduler_StoreSchema;
8 use Action_Scheduler\WP_CLI\ProgressBar;
9
10 /**
11 * Class Controller
12 *
13 * The main plugin/initialization class for migration to custom tables.
14 *
15 * @package Action_Scheduler\Migration
16 *
17 * @since 3.0.0
18 *
19 * @codeCoverageIgnore
20 */
21 class Controller {
22 /** @var self */
23 private static $instance;
24
25 /** @var Action_Scheduler\Migration\Scheduler */
26 private $migration_scheduler;
27
28 /** @var string */
29 private $store_classname;
30
31 /** @var string */
32 private $logger_classname;
33
34 /** @var bool */
35 private $migrate_custom_store;
36
37 /**
38 * Controller constructor.
39 *
40 * @param Scheduler $migration_scheduler Migration scheduler object.
41 */
42 protected function __construct( Scheduler $migration_scheduler ) {
43 $this->migration_scheduler = $migration_scheduler;
44 $this->store_classname = '';
45 }
46
47 /**
48 * Set the action store class name.
49 *
50 * @param string $class Classname of the store class.
51 *
52 * @return string
53 */
54 public function get_store_class( $class ) {
55 if ( \ActionScheduler_DataController::is_migration_complete() ) {
56 return \ActionScheduler_DataController::DATASTORE_CLASS;
57 } elseif ( \ActionScheduler_Store::DEFAULT_CLASS !== $class ) {
58 $this->store_classname = $class;
59 return $class;
60 } else {
61 return 'ActionScheduler_HybridStore';
62 }
63 }
64
65 /**
66 * Set the action logger class name.
67 *
68 * @param string $class Classname of the logger class.
69 *
70 * @return string
71 */
72 public function get_logger_class( $class ) {
73 \ActionScheduler_Store::instance();
74
75 if ( $this->has_custom_datastore() ) {
76 $this->logger_classname = $class;
77 return $class;
78 } else {
79 return \ActionScheduler_DataController::LOGGER_CLASS;
80 }
81 }
82
83 /**
84 * Get flag indicating whether a custom datastore is in use.
85 *
86 * @return bool
87 */
88 public function has_custom_datastore() {
89 return (bool) $this->store_classname;
90 }
91
92 /**
93 * Set up the background migration process.
94 *
95 * @return void
96 */
97 public function schedule_migration() {
98 $logging_tables = new ActionScheduler_LoggerSchema();
99 $store_tables = new ActionScheduler_StoreSchema();
100
101 /*
102 * In some unusual cases, the expected tables may not have been created. In such cases
103 * we do not schedule a migration as doing so will lead to fatal error conditions.
104 *
105 * In such cases the user will likely visit the Tools > Scheduled Actions screen to
106 * investigate, and will see appropriate messaging (this step also triggers an attempt
107 * to rebuild any missing tables).
108 *
109 * @see https://github.com/woocommerce/action-scheduler/issues/653
110 */
111 if (
112 ActionScheduler_DataController::is_migration_complete()
113 || $this->migration_scheduler->is_migration_scheduled()
114 || ! $store_tables->tables_exist()
115 || ! $logging_tables->tables_exist()
116 ) {
117 return;
118 }
119
120 $this->migration_scheduler->schedule_migration();
121 }
122
123 /**
124 * Get the default migration config object
125 *
126 * @return ActionScheduler\Migration\Config
127 */
128 public function get_migration_config_object() {
129 static $config = null;
130
131 if ( ! $config ) {
132 $source_store = $this->store_classname ? new $this->store_classname() : new \ActionScheduler_wpPostStore();
133 $source_logger = $this->logger_classname ? new $this->logger_classname() : new \ActionScheduler_wpCommentLogger();
134
135 $config = new Config();
136 $config->set_source_store( $source_store );
137 $config->set_source_logger( $source_logger );
138 $config->set_destination_store( new \ActionScheduler_DBStoreMigrator() );
139 $config->set_destination_logger( new \ActionScheduler_DBLogger() );
140
141 if ( defined( 'WP_CLI' ) && WP_CLI ) {
142 $config->set_progress_bar( new ProgressBar( '', 0 ) );
143 }
144 }
145
146 return apply_filters( 'action_scheduler/migration_config', $config );
147 }
148
149 /**
150 * Hook dashboard migration notice.
151 */
152 public function hook_admin_notices() {
153 if ( ! $this->allow_migration() || \ActionScheduler_DataController::is_migration_complete() ) {
154 return;
155 }
156 add_action( 'admin_notices', array( $this, 'display_migration_notice' ), 10, 0 );
157 }
158
159 /**
160 * Show a dashboard notice that migration is in progress.
161 */
162 public function display_migration_notice() {
163 printf( '<div class="notice notice-warning"><p>%s</p></div>', esc_html__( 'Action Scheduler migration in progress. The list of scheduled actions may be incomplete.', 'action-scheduler' ) );
164 }
165
166 /**
167 * Add store classes. Hook migration.
168 */
169 private function hook() {
170 add_filter( 'action_scheduler_store_class', array( $this, 'get_store_class' ), 100, 1 );
171 add_filter( 'action_scheduler_logger_class', array( $this, 'get_logger_class' ), 100, 1 );
172 add_action( 'init', array( $this, 'maybe_hook_migration' ) );
173 add_action( 'wp_loaded', array( $this, 'schedule_migration' ) );
174
175 // Action Scheduler may be displayed as a Tools screen or WooCommerce > Status administration screen.
176 add_action( 'load-tools_page_action-scheduler', array( $this, 'hook_admin_notices' ), 10, 0 );
177 add_action( 'load-woocommerce_page_wc-status', array( $this, 'hook_admin_notices' ), 10, 0 );
178 }
179
180 /**
181 * Possibly hook the migration scheduler action.
182 *
183 * @author Jeremy Pry
184 */
185 public function maybe_hook_migration() {
186 if ( ! $this->allow_migration() || \ActionScheduler_DataController::is_migration_complete() ) {
187 return;
188 }
189
190 $this->migration_scheduler->hook();
191 }
192
193 /**
194 * Allow datastores to enable migration to AS tables.
195 */
196 public function allow_migration() {
197 if ( ! \ActionScheduler_DataController::dependencies_met() ) {
198 return false;
199 }
200
201 if ( null === $this->migrate_custom_store ) {
202 $this->migrate_custom_store = apply_filters( 'action_scheduler_migrate_data_store', false );
203 }
204
205 return ( ! $this->has_custom_datastore() ) || $this->migrate_custom_store;
206 }
207
208 /**
209 * Proceed with the migration if the dependencies have been met.
210 */
211 public static function init() {
212 if ( \ActionScheduler_DataController::dependencies_met() ) {
213 self::instance()->hook();
214 }
215 }
216
217 /**
218 * Singleton factory.
219 */
220 public static function instance() {
221 if ( ! isset( self::$instance ) ) {
222 self::$instance = new static( new Scheduler() );
223 }
224
225 return self::$instance;
226 }
227 }
228