PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 2.8.16
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v2.8.16
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / packages / action-scheduler / classes / migration / Controller.php

Controller.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 2.8.16, at packages/action-scheduler/classes/migration/Controller.php

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