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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / class-wc-install.php
woocommerce / includes Last commit date
abstracts 5 years ago admin 5 years ago cli 6 years ago customizer 6 years ago data-stores 5 years ago emails 6 years ago export 5 years ago gateways 6 years ago import 5 years ago integrations 6 years ago interfaces 6 years ago legacy 6 years ago libraries 6 years ago log-handlers 6 years ago payment-tokens 6 years ago queue 7 years ago shipping 6 years ago shortcodes 5 years ago theme-support 6 years ago tracks 6 years ago traits 6 years ago walkers 8 years ago wccom-site 6 years ago widgets 6 years ago class-wc-ajax.php 6 years ago class-wc-api.php 6 years ago class-wc-auth.php 7 years ago class-wc-autoloader.php 6 years ago class-wc-background-emailer.php 6 years ago class-wc-background-updater.php 7 years ago class-wc-breadcrumb.php 7 years ago class-wc-cache-helper.php 6 years ago class-wc-cart-fees.php 6 years ago class-wc-cart-session.php 6 years ago class-wc-cart-totals.php 6 years ago class-wc-cart.php 5 years ago class-wc-checkout.php 5 years ago class-wc-cli.php 8 years ago class-wc-comments.php 5 years ago class-wc-countries.php 5 years ago class-wc-coupon.php 6 years ago class-wc-customer-download-log.php 8 years ago class-wc-customer-download.php 6 years ago class-wc-customer.php 7 years ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 6 years ago class-wc-datetime.php 7 years ago class-wc-deprecated-action-hooks.php 8 years ago class-wc-deprecated-filter-hooks.php 7 years ago class-wc-discounts.php 6 years ago class-wc-download-handler.php 6 years ago class-wc-emails.php 6 years ago class-wc-embed.php 8 years ago class-wc-form-handler.php 5 years ago class-wc-frontend-scripts.php 6 years ago class-wc-geo-ip.php 8 years ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 6 years ago class-wc-https.php 8 years ago class-wc-install.php 5 years ago class-wc-integrations.php 6 years ago class-wc-log-levels.php 7 years ago class-wc-logger.php 6 years ago class-wc-meta-data.php 7 years ago class-wc-order-factory.php 6 years ago class-wc-order-item-coupon.php 7 years ago class-wc-order-item-fee.php 7 years ago class-wc-order-item-meta.php 7 years ago class-wc-order-item-product.php 6 years ago class-wc-order-item-shipping.php 7 years ago class-wc-order-item-tax.php 6 years ago class-wc-order-item.php 6 years ago class-wc-order-query.php 7 years ago class-wc-order-refund.php 8 years ago class-wc-order.php 6 years ago class-wc-payment-gateways.php 6 years ago class-wc-payment-tokens.php 6 years ago class-wc-post-data.php 6 years ago class-wc-post-types.php 7 years ago class-wc-privacy-background-process.php 8 years ago class-wc-privacy-erasers.php 6 years ago class-wc-privacy-exporters.php 6 years ago class-wc-privacy.php 6 years ago class-wc-product-attribute.php 7 years ago class-wc-product-download.php 6 years ago class-wc-product-external.php 8 years ago class-wc-product-factory.php 7 years ago class-wc-product-grouped.php 8 years ago class-wc-product-query.php 7 years ago class-wc-product-simple.php 6 years ago class-wc-product-variable.php 6 years ago class-wc-product-variation.php 7 years ago class-wc-query.php 6 years ago class-wc-rate-limiter.php 6 years ago class-wc-regenerate-images-request.php 7 years ago class-wc-regenerate-images.php 7 years ago class-wc-register-wp-admin-settings.php 8 years ago class-wc-rest-authentication.php 6 years ago class-wc-rest-exception.php 6 years ago class-wc-session-handler.php 6 years ago class-wc-shipping-rate.php 8 years ago class-wc-shipping-zone.php 6 years ago class-wc-shipping-zones.php 6 years ago class-wc-shipping.php 5 years ago class-wc-shortcodes.php 6 years ago class-wc-structured-data.php 6 years ago class-wc-tax.php 6 years ago class-wc-template-loader.php 6 years ago class-wc-tracker.php 6 years ago class-wc-validation.php 6 years ago class-wc-webhook.php 6 years ago class-woocommerce.php 5 years ago wc-account-functions.php 6 years ago wc-attribute-functions.php 5 years ago wc-cart-functions.php 5 years ago wc-conditional-functions.php 6 years ago wc-core-functions.php 5 years ago wc-coupon-functions.php 7 years ago wc-deprecated-functions.php 6 years ago wc-formatting-functions.php 5 years ago wc-notice-functions.php 6 years ago wc-order-functions.php 6 years ago wc-order-item-functions.php 6 years ago wc-page-functions.php 6 years ago wc-product-functions.php 6 years ago wc-rest-functions.php 6 years ago wc-stock-functions.php 5 years ago wc-template-functions.php 5 years ago wc-template-hooks.php 6 years ago wc-term-functions.php 6 years ago wc-update-functions.php 6 years ago wc-user-functions.php 6 years ago wc-webhook-functions.php 7 years ago wc-widget-functions.php 8 years ago
class-wc-install.php
1622 lines
1 <?php
2 /**
3 * Installation related functions and actions.
4 *
5 * @package WooCommerce/Classes
6 * @version 3.0.0
7 */
8
9 use Automattic\Jetpack\Constants;
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * WC_Install Class.
15 */
16 class WC_Install {
17
18 /**
19 * DB updates and callbacks that need to be run per version.
20 *
21 * @var array
22 */
23 private static $db_updates = array(
24 '2.0.0' => array(
25 'wc_update_200_file_paths',
26 'wc_update_200_permalinks',
27 'wc_update_200_subcat_display',
28 'wc_update_200_taxrates',
29 'wc_update_200_line_items',
30 'wc_update_200_images',
31 'wc_update_200_db_version',
32 ),
33 '2.0.9' => array(
34 'wc_update_209_brazillian_state',
35 'wc_update_209_db_version',
36 ),
37 '2.1.0' => array(
38 'wc_update_210_remove_pages',
39 'wc_update_210_file_paths',
40 'wc_update_210_db_version',
41 ),
42 '2.2.0' => array(
43 'wc_update_220_shipping',
44 'wc_update_220_order_status',
45 'wc_update_220_variations',
46 'wc_update_220_attributes',
47 'wc_update_220_db_version',
48 ),
49 '2.3.0' => array(
50 'wc_update_230_options',
51 'wc_update_230_db_version',
52 ),
53 '2.4.0' => array(
54 'wc_update_240_options',
55 'wc_update_240_shipping_methods',
56 'wc_update_240_api_keys',
57 'wc_update_240_refunds',
58 'wc_update_240_db_version',
59 ),
60 '2.4.1' => array(
61 'wc_update_241_variations',
62 'wc_update_241_db_version',
63 ),
64 '2.5.0' => array(
65 'wc_update_250_currency',
66 'wc_update_250_db_version',
67 ),
68 '2.6.0' => array(
69 'wc_update_260_options',
70 'wc_update_260_termmeta',
71 'wc_update_260_zones',
72 'wc_update_260_zone_methods',
73 'wc_update_260_refunds',
74 'wc_update_260_db_version',
75 ),
76 '3.0.0' => array(
77 'wc_update_300_grouped_products',
78 'wc_update_300_settings',
79 'wc_update_300_product_visibility',
80 'wc_update_300_db_version',
81 ),
82 '3.1.0' => array(
83 'wc_update_310_downloadable_products',
84 'wc_update_310_old_comments',
85 'wc_update_310_db_version',
86 ),
87 '3.1.2' => array(
88 'wc_update_312_shop_manager_capabilities',
89 'wc_update_312_db_version',
90 ),
91 '3.2.0' => array(
92 'wc_update_320_mexican_states',
93 'wc_update_320_db_version',
94 ),
95 '3.3.0' => array(
96 'wc_update_330_image_options',
97 'wc_update_330_webhooks',
98 'wc_update_330_product_stock_status',
99 'wc_update_330_set_default_product_cat',
100 'wc_update_330_clear_transients',
101 'wc_update_330_set_paypal_sandbox_credentials',
102 'wc_update_330_db_version',
103 ),
104 '3.4.0' => array(
105 'wc_update_340_states',
106 'wc_update_340_state',
107 'wc_update_340_last_active',
108 'wc_update_340_db_version',
109 ),
110 '3.4.3' => array(
111 'wc_update_343_cleanup_foreign_keys',
112 'wc_update_343_db_version',
113 ),
114 '3.4.4' => array(
115 'wc_update_344_recreate_roles',
116 'wc_update_344_db_version',
117 ),
118 '3.5.0' => array(
119 'wc_update_350_reviews_comment_type',
120 'wc_update_350_db_version',
121 ),
122 '3.5.2' => array(
123 'wc_update_352_drop_download_log_fk',
124 ),
125 '3.5.4' => array(
126 'wc_update_354_modify_shop_manager_caps',
127 'wc_update_354_db_version',
128 ),
129 '3.6.0' => array(
130 'wc_update_360_product_lookup_tables',
131 'wc_update_360_term_meta',
132 'wc_update_360_downloadable_product_permissions_index',
133 'wc_update_360_db_version',
134 ),
135 '3.7.0' => array(
136 'wc_update_370_tax_rate_classes',
137 'wc_update_370_mro_std_currency',
138 'wc_update_370_db_version',
139 ),
140 '3.9.0' => array(
141 'wc_update_390_move_maxmind_database',
142 'wc_update_390_change_geolocation_database_update_cron',
143 'wc_update_390_db_version',
144 ),
145 '4.0.0' => array(
146 'wc_update_product_lookup_tables',
147 'wc_update_400_increase_size_of_column',
148 'wc_update_400_reset_action_scheduler_migration_status',
149 'wc_update_400_db_version',
150 ),
151 );
152
153 /**
154 * Hook in tabs.
155 */
156 public static function init() {
157 add_action( 'init', array( __CLASS__, 'check_version' ), 5 );
158 add_action( 'init', array( __CLASS__, 'manual_database_update' ), 20 );
159 add_action( 'admin_init', array( __CLASS__, 'wc_admin_db_update_notice' ) );
160 add_action( 'woocommerce_run_update_callback', array( __CLASS__, 'run_update_callback' ) );
161 add_action( 'admin_init', array( __CLASS__, 'install_actions' ) );
162 add_filter( 'plugin_action_links_' . WC_PLUGIN_BASENAME, array( __CLASS__, 'plugin_action_links' ) );
163 add_filter( 'plugin_row_meta', array( __CLASS__, 'plugin_row_meta' ), 10, 2 );
164 add_filter( 'wpmu_drop_tables', array( __CLASS__, 'wpmu_drop_tables' ) );
165 add_filter( 'cron_schedules', array( __CLASS__, 'cron_schedules' ) );
166 }
167
168 /**
169 * Check WooCommerce version and run the updater is required.
170 *
171 * This check is done on all requests and runs if the versions do not match.
172 */
173 public static function check_version() {
174 if ( ! Constants::is_defined( 'IFRAME_REQUEST' ) && version_compare( get_option( 'woocommerce_version' ), WC()->version, '<' ) ) {
175 self::install();
176 do_action( 'woocommerce_updated' );
177 }
178 }
179
180 /**
181 * Performan manual database update when triggered by WooCommerce System Tools.
182 *
183 * @since 3.6.5
184 */
185 public static function manual_database_update() {
186 $blog_id = get_current_blog_id();
187
188 add_action( 'wp_' . $blog_id . '_wc_updater_cron', array( __CLASS__, 'run_manual_database_update' ) );
189 }
190
191 /**
192 * Add WC Admin based db update notice.
193 *
194 * @since 4.0.0
195 */
196 public static function wc_admin_db_update_notice() {
197 if (
198 WC()->is_wc_admin_active() &&
199 false !== get_option( 'woocommerce_admin_install_timestamp' )
200 ) {
201 new WC_Notes_Run_Db_Update();
202 }
203 }
204
205 /**
206 * Run manual database update.
207 */
208 public static function run_manual_database_update() {
209 self::update();
210 }
211
212 /**
213 * Run an update callback when triggered by ActionScheduler.
214 *
215 * @since 3.6.0
216 * @param string $callback Callback name.
217 */
218 public static function run_update_callback( $callback ) {
219 include_once dirname( __FILE__ ) . '/wc-update-functions.php';
220
221 if ( is_callable( $callback ) ) {
222 self::run_update_callback_start( $callback );
223 $result = (bool) call_user_func( $callback );
224 self::run_update_callback_end( $callback, $result );
225 }
226 }
227
228 /**
229 * Triggered when a callback will run.
230 *
231 * @since 3.6.0
232 * @param string $callback Callback name.
233 */
234 protected static function run_update_callback_start( $callback ) {
235 wc_maybe_define_constant( 'WC_UPDATING', true );
236 }
237
238 /**
239 * Triggered when a callback has ran.
240 *
241 * @since 3.6.0
242 * @param string $callback Callback name.
243 * @param bool $result Return value from callback. Non-false need to run again.
244 */
245 protected static function run_update_callback_end( $callback, $result ) {
246 if ( $result ) {
247 WC()->queue()->add(
248 'woocommerce_run_update_callback',
249 array(
250 'update_callback' => $callback,
251 ),
252 'woocommerce-db-updates'
253 );
254 }
255 }
256
257 /**
258 * Install actions when a update button is clicked within the admin area.
259 *
260 * This function is hooked into admin_init to affect admin only.
261 */
262 public static function install_actions() {
263 if ( ! empty( $_GET['do_update_woocommerce'] ) ) { // WPCS: input var ok.
264 check_admin_referer( 'wc_db_update', 'wc_db_update_nonce' );
265 self::update();
266 WC_Admin_Notices::add_notice( 'update', true );
267 }
268 }
269
270 /**
271 * Install WC.
272 */
273 public static function install() {
274 if ( ! is_blog_installed() ) {
275 return;
276 }
277
278 // Check if we are not already running this routine.
279 if ( 'yes' === get_transient( 'wc_installing' ) ) {
280 return;
281 }
282
283 // If we made it till here nothing is running yet, lets set the transient now.
284 set_transient( 'wc_installing', 'yes', MINUTE_IN_SECONDS * 10 );
285 wc_maybe_define_constant( 'WC_INSTALLING', true );
286
287 WC()->wpdb_table_fix();
288 self::remove_admin_notices();
289 self::create_tables();
290 self::verify_base_tables();
291 self::create_options();
292 self::create_roles();
293 self::setup_environment();
294 self::create_terms();
295 self::create_cron_jobs();
296 self::create_files();
297 self::maybe_enable_setup_wizard();
298 self::update_wc_version();
299 self::maybe_update_db_version();
300 self::maybe_enable_homescreen();
301
302 delete_transient( 'wc_installing' );
303
304 do_action( 'woocommerce_flush_rewrite_rules' );
305 do_action( 'woocommerce_installed' );
306 }
307
308 /**
309 * Check if all the base tables are present.
310 *
311 * @param bool $modify_notice Whether to modify notice based on if all tables are present.
312 * @param bool $execute Whether to execute get_schema queries as well.
313 *
314 * @return array List of querues.
315 */
316 public static function verify_base_tables( $modify_notice = true, $execute = false ) {
317 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
318
319 if ( $execute ) {
320 self::create_tables();
321 }
322 $queries = dbDelta( self::get_schema(), false );
323 $missing_tables = array();
324 foreach ( $queries as $table_name => $result ) {
325 if ( "Created table $table_name" === $result ) {
326 $missing_tables[] = $table_name;
327 }
328 }
329
330 if ( 0 < count( $missing_tables ) ) {
331 if ( $modify_notice ) {
332 WC_Admin_Notices::add_notice( 'base_tables_missing' );
333 }
334 update_option( 'woocommerce_schema_missing_tables', $missing_tables );
335 } else {
336 if ( $modify_notice ) {
337 WC_Admin_Notices::remove_notice( 'base_tables_missing' );
338 }
339 update_option( 'woocommerce_schema_version', WC()->db_version );
340 delete_option( 'woocommerce_schema_missing_tables' );
341 }
342 return $missing_tables;
343 }
344
345 /**
346 * Check if the homepage should be enabled and set the appropriate option if thats the case.
347 *
348 * @since 4.3.0
349 */
350 private static function maybe_enable_homescreen() {
351 if ( self::is_new_install() && ! get_option( 'woocommerce_homescreen_enabled' ) ) {
352 add_option( 'woocommerce_homescreen_enabled', 'yes' );
353 }
354 }
355
356 /**
357 * Reset any notices added to admin.
358 *
359 * @since 3.2.0
360 */
361 private static function remove_admin_notices() {
362 include_once dirname( __FILE__ ) . '/admin/class-wc-admin-notices.php';
363 WC_Admin_Notices::remove_all_notices();
364 }
365
366 /**
367 * Setup WC environment - post types, taxonomies, endpoints.
368 *
369 * @since 3.2.0
370 */
371 private static function setup_environment() {
372 WC_Post_types::register_post_types();
373 WC_Post_types::register_taxonomies();
374 WC()->query->init_query_vars();
375 WC()->query->add_endpoints();
376 WC_API::add_endpoint();
377 WC_Auth::add_endpoint();
378 }
379
380 /**
381 * Is this a brand new WC install?
382 *
383 * A brand new install has no version yet. Also treat empty installs as 'new'.
384 *
385 * @since 3.2.0
386 * @return boolean
387 */
388 public static function is_new_install() {
389 $product_count = array_sum( (array) wp_count_posts( 'product' ) );
390
391 return is_null( get_option( 'woocommerce_version', null ) ) || ( 0 === $product_count && -1 === wc_get_page_id( 'shop' ) );
392 }
393
394 /**
395 * Is a DB update needed?
396 *
397 * @since 3.2.0
398 * @return boolean
399 */
400 public static function needs_db_update() {
401 $current_db_version = get_option( 'woocommerce_db_version', null );
402 $updates = self::get_db_update_callbacks();
403 $update_versions = array_keys( $updates );
404 usort( $update_versions, 'version_compare' );
405
406 return ! is_null( $current_db_version ) && version_compare( $current_db_version, end( $update_versions ), '<' );
407 }
408
409 /**
410 * See if we need the wizard or not.
411 *
412 * @since 3.2.0
413 */
414 private static function maybe_enable_setup_wizard() {
415 if ( apply_filters( 'woocommerce_enable_setup_wizard', true ) && self::is_new_install() ) {
416 WC_Admin_Notices::add_notice( 'install', true );
417 set_transient( '_wc_activation_redirect', 1, 30 );
418 }
419 }
420
421 /**
422 * See if we need to show or run database updates during install.
423 *
424 * @since 3.2.0
425 */
426 private static function maybe_update_db_version() {
427 if ( self::needs_db_update() ) {
428 if ( apply_filters( 'woocommerce_enable_auto_update_db', false ) ) {
429 self::update();
430 } else {
431 WC_Admin_Notices::add_notice( 'update', true );
432 }
433 } else {
434 self::update_db_version();
435 }
436 }
437
438 /**
439 * Update WC version to current.
440 */
441 private static function update_wc_version() {
442 delete_option( 'woocommerce_version' );
443 add_option( 'woocommerce_version', WC()->version );
444 }
445
446 /**
447 * Get list of DB update callbacks.
448 *
449 * @since 3.0.0
450 * @return array
451 */
452 public static function get_db_update_callbacks() {
453 return self::$db_updates;
454 }
455
456 /**
457 * Push all needed DB updates to the queue for processing.
458 */
459 private static function update() {
460 $current_db_version = get_option( 'woocommerce_db_version' );
461 $loop = 0;
462
463 foreach ( self::get_db_update_callbacks() as $version => $update_callbacks ) {
464 if ( version_compare( $current_db_version, $version, '<' ) ) {
465 foreach ( $update_callbacks as $update_callback ) {
466 WC()->queue()->schedule_single(
467 time() + $loop,
468 'woocommerce_run_update_callback',
469 array(
470 'update_callback' => $update_callback,
471 ),
472 'woocommerce-db-updates'
473 );
474 $loop++;
475 }
476 }
477 }
478 }
479
480 /**
481 * Update DB version to current.
482 *
483 * @param string|null $version New WooCommerce DB version or null.
484 */
485 public static function update_db_version( $version = null ) {
486 delete_option( 'woocommerce_db_version' );
487 add_option( 'woocommerce_db_version', is_null( $version ) ? WC()->version : $version );
488 }
489
490 /**
491 * Add more cron schedules.
492 *
493 * @param array $schedules List of WP scheduled cron jobs.
494 *
495 * @return array
496 */
497 public static function cron_schedules( $schedules ) {
498 $schedules['monthly'] = array(
499 'interval' => 2635200,
500 'display' => __( 'Monthly', 'woocommerce' ),
501 );
502 $schedules['fifteendays'] = array(
503 'interval' => 1296000,
504 'display' => __( 'Every 15 Days', 'woocommerce' ),
505 );
506 return $schedules;
507 }
508
509 /**
510 * Create cron jobs (clear them first).
511 */
512 private static function create_cron_jobs() {
513 wp_clear_scheduled_hook( 'woocommerce_scheduled_sales' );
514 wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' );
515 wp_clear_scheduled_hook( 'woocommerce_cleanup_sessions' );
516 wp_clear_scheduled_hook( 'woocommerce_cleanup_personal_data' );
517 wp_clear_scheduled_hook( 'woocommerce_cleanup_logs' );
518 wp_clear_scheduled_hook( 'woocommerce_geoip_updater' );
519 wp_clear_scheduled_hook( 'woocommerce_tracker_send_event' );
520
521 $ve = get_option( 'gmt_offset' ) > 0 ? '-' : '+';
522
523 wp_schedule_event( strtotime( '00:00 tomorrow ' . $ve . absint( get_option( 'gmt_offset' ) ) . ' HOURS' ), 'daily', 'woocommerce_scheduled_sales' );
524
525 $held_duration = get_option( 'woocommerce_hold_stock_minutes', '60' );
526
527 if ( '' !== $held_duration ) {
528 wp_schedule_single_event( time() + ( absint( $held_duration ) * 60 ), 'woocommerce_cancel_unpaid_orders' );
529 }
530
531 // Delay the first run of `woocommerce_cleanup_personal_data` by 10 seconds
532 // so it doesn't occur in the same request. WooCommerce Admin also schedules
533 // a daily cron that gets lost due to a race condition. WC_Privacy's background
534 // processing instance updates the cron schedule from within a cron job.
535 wp_schedule_event( time() + 10, 'daily', 'woocommerce_cleanup_personal_data' );
536 wp_schedule_event( time() + ( 3 * HOUR_IN_SECONDS ), 'daily', 'woocommerce_cleanup_logs' );
537 wp_schedule_event( time() + ( 6 * HOUR_IN_SECONDS ), 'twicedaily', 'woocommerce_cleanup_sessions' );
538 wp_schedule_event( time() + MINUTE_IN_SECONDS, 'fifteendays', 'woocommerce_geoip_updater' );
539 wp_schedule_event( time() + 10, apply_filters( 'woocommerce_tracker_event_recurrence', 'daily' ), 'woocommerce_tracker_send_event' );
540 }
541
542 /**
543 * Create pages that the plugin relies on, storing page IDs in variables.
544 */
545 public static function create_pages() {
546 include_once dirname( __FILE__ ) . '/admin/wc-admin-functions.php';
547
548 $pages = apply_filters(
549 'woocommerce_create_pages',
550 array(
551 'shop' => array(
552 'name' => _x( 'shop', 'Page slug', 'woocommerce' ),
553 'title' => _x( 'Shop', 'Page title', 'woocommerce' ),
554 'content' => '',
555 ),
556 'cart' => array(
557 'name' => _x( 'cart', 'Page slug', 'woocommerce' ),
558 'title' => _x( 'Cart', 'Page title', 'woocommerce' ),
559 'content' => '<!-- wp:shortcode -->[' . apply_filters( 'woocommerce_cart_shortcode_tag', 'woocommerce_cart' ) . ']<!-- /wp:shortcode -->',
560 ),
561 'checkout' => array(
562 'name' => _x( 'checkout', 'Page slug', 'woocommerce' ),
563 'title' => _x( 'Checkout', 'Page title', 'woocommerce' ),
564 'content' => '<!-- wp:shortcode -->[' . apply_filters( 'woocommerce_checkout_shortcode_tag', 'woocommerce_checkout' ) . ']<!-- /wp:shortcode -->',
565 ),
566 'myaccount' => array(
567 'name' => _x( 'my-account', 'Page slug', 'woocommerce' ),
568 'title' => _x( 'My account', 'Page title', 'woocommerce' ),
569 'content' => '<!-- wp:shortcode -->[' . apply_filters( 'woocommerce_my_account_shortcode_tag', 'woocommerce_my_account' ) . ']<!-- /wp:shortcode -->',
570 ),
571 )
572 );
573
574 foreach ( $pages as $key => $page ) {
575 wc_create_page( esc_sql( $page['name'] ), 'woocommerce_' . $key . '_page_id', $page['title'], $page['content'], ! empty( $page['parent'] ) ? wc_get_page_id( $page['parent'] ) : '' );
576 }
577 }
578
579 /**
580 * Default options.
581 *
582 * Sets up the default options used on the settings page.
583 */
584 private static function create_options() {
585 // Include settings so that we can run through defaults.
586 include_once dirname( __FILE__ ) . '/admin/class-wc-admin-settings.php';
587
588 $settings = WC_Admin_Settings::get_settings_pages();
589
590 foreach ( $settings as $section ) {
591 if ( ! method_exists( $section, 'get_settings' ) ) {
592 continue;
593 }
594 $subsections = array_unique( array_merge( array( '' ), array_keys( $section->get_sections() ) ) );
595
596 foreach ( $subsections as $subsection ) {
597 foreach ( $section->get_settings( $subsection ) as $value ) {
598 if ( isset( $value['default'] ) && isset( $value['id'] ) ) {
599 $autoload = isset( $value['autoload'] ) ? (bool) $value['autoload'] : true;
600 add_option( $value['id'], $value['default'], '', ( $autoload ? 'yes' : 'no' ) );
601 }
602 }
603 }
604 }
605
606 // Define other defaults if not in setting screens.
607 add_option( 'woocommerce_single_image_width', '600', '', 'yes' );
608 add_option( 'woocommerce_thumbnail_image_width', '300', '', 'yes' );
609 add_option( 'woocommerce_checkout_highlight_required_fields', 'yes', '', 'yes' );
610 add_option( 'woocommerce_demo_store', 'no', '', 'no' );
611
612 // Define initial tax classes.
613 WC_Tax::create_tax_class( __( 'Reduced rate', 'woocommerce' ) );
614 WC_Tax::create_tax_class( __( 'Zero rate', 'woocommerce' ) );
615 }
616
617 /**
618 * Add the default terms for WC taxonomies - product types and order statuses. Modify this at your own risk.
619 */
620 public static function create_terms() {
621 $taxonomies = array(
622 'product_type' => array(
623 'simple',
624 'grouped',
625 'variable',
626 'external',
627 ),
628 'product_visibility' => array(
629 'exclude-from-search',
630 'exclude-from-catalog',
631 'featured',
632 'outofstock',
633 'rated-1',
634 'rated-2',
635 'rated-3',
636 'rated-4',
637 'rated-5',
638 ),
639 );
640
641 foreach ( $taxonomies as $taxonomy => $terms ) {
642 foreach ( $terms as $term ) {
643 if ( ! get_term_by( 'name', $term, $taxonomy ) ) { // @codingStandardsIgnoreLine.
644 wp_insert_term( $term, $taxonomy );
645 }
646 }
647 }
648
649 $woocommerce_default_category = (int) get_option( 'default_product_cat', 0 );
650
651 if ( ! $woocommerce_default_category || ! term_exists( $woocommerce_default_category, 'product_cat' ) ) {
652 $default_product_cat_id = 0;
653 $default_product_cat_slug = sanitize_title( _x( 'Uncategorized', 'Default category slug', 'woocommerce' ) );
654 $default_product_cat = get_term_by( 'slug', $default_product_cat_slug, 'product_cat' ); // @codingStandardsIgnoreLine.
655
656 if ( $default_product_cat ) {
657 $default_product_cat_id = absint( $default_product_cat->term_taxonomy_id );
658 } else {
659 $result = wp_insert_term( _x( 'Uncategorized', 'Default category slug', 'woocommerce' ), 'product_cat', array( 'slug' => $default_product_cat_slug ) );
660
661 if ( ! is_wp_error( $result ) && ! empty( $result['term_taxonomy_id'] ) ) {
662 $default_product_cat_id = absint( $result['term_taxonomy_id'] );
663 }
664 }
665
666 if ( $default_product_cat_id ) {
667 update_option( 'default_product_cat', $default_product_cat_id );
668 }
669 }
670 }
671
672 /**
673 * Set up the database tables which the plugin needs to function.
674 * WARNING: If you are modifying this method, make sure that its safe to call regardless of the state of database.
675 *
676 * This is called from `install` method and is executed in-sync when WC is installed or updated. This can also be called optionally from `verify_base_tables`.
677 *
678 * TODO: Add all crucial tables that we have created from workers in the past.
679 *
680 * Tables:
681 * woocommerce_attribute_taxonomies - Table for storing attribute taxonomies - these are user defined
682 * woocommerce_downloadable_product_permissions - Table for storing user and guest download permissions.
683 * KEY(order_id, product_id, download_id) used for organizing downloads on the My Account page
684 * woocommerce_order_items - Order line items are stored in a table to make them easily queryable for reports
685 * woocommerce_order_itemmeta - Order line item meta is stored in a table for storing extra data.
686 * woocommerce_tax_rates - Tax Rates are stored inside 2 tables making tax queries simple and efficient.
687 * woocommerce_tax_rate_locations - Each rate can be applied to more than one postcode/city hence the second table.
688 */
689 private static function create_tables() {
690 global $wpdb;
691
692 $wpdb->hide_errors();
693
694 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
695
696 /**
697 * Before updating with DBDELTA, remove any primary keys which could be
698 * modified due to schema updates.
699 */
700 if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_downloadable_product_permissions';" ) ) {
701 if ( ! $wpdb->get_var( "SHOW COLUMNS FROM `{$wpdb->prefix}woocommerce_downloadable_product_permissions` LIKE 'permission_id';" ) ) {
702 $wpdb->query( "ALTER TABLE {$wpdb->prefix}woocommerce_downloadable_product_permissions DROP PRIMARY KEY, ADD `permission_id` BIGINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT;" );
703 }
704 }
705
706 /**
707 * Change wp_woocommerce_sessions schema to use a bigint auto increment field instead of char(32) field as
708 * the primary key as it is not a good practice to use a char(32) field as the primary key of a table and as
709 * there were reports of issues with this table (see https://github.com/woocommerce/woocommerce/issues/20912).
710 *
711 * This query needs to run before dbDelta() as this WP function is not able to handle primary key changes
712 * (see https://github.com/woocommerce/woocommerce/issues/21534 and https://core.trac.wordpress.org/ticket/40357).
713 */
714 if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_sessions'" ) ) {
715 if ( ! $wpdb->get_var( "SHOW KEYS FROM {$wpdb->prefix}woocommerce_sessions WHERE Key_name = 'PRIMARY' AND Column_name = 'session_id'" ) ) {
716 $wpdb->query(
717 "ALTER TABLE `{$wpdb->prefix}woocommerce_sessions` DROP PRIMARY KEY, DROP KEY `session_id`, ADD PRIMARY KEY(`session_id`), ADD UNIQUE KEY(`session_key`)"
718 );
719 }
720 }
721
722 dbDelta( self::get_schema() );
723
724 $index_exists = $wpdb->get_row( "SHOW INDEX FROM {$wpdb->comments} WHERE column_name = 'comment_type' and key_name = 'woo_idx_comment_type'" );
725
726 if ( is_null( $index_exists ) ) {
727 // Add an index to the field comment_type to improve the response time of the query
728 // used by WC_Comments::wp_count_comments() to get the number of comments by type.
729 $wpdb->query( "ALTER TABLE {$wpdb->comments} ADD INDEX woo_idx_comment_type (comment_type)" );
730 }
731
732 // Get tables data types and check it matches before adding constraint.
733 $download_log_columns = $wpdb->get_results( "SHOW COLUMNS FROM {$wpdb->prefix}wc_download_log WHERE Field = 'permission_id'", ARRAY_A );
734 $download_log_column_type = '';
735 if ( isset( $download_log_columns[0]['Type'] ) ) {
736 $download_log_column_type = $download_log_columns[0]['Type'];
737 }
738
739 $download_permissions_columns = $wpdb->get_results( "SHOW COLUMNS FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE Field = 'permission_id'", ARRAY_A );
740 $download_permissions_column_type = '';
741 if ( isset( $download_permissions_columns[0]['Type'] ) ) {
742 $download_permissions_column_type = $download_permissions_columns[0]['Type'];
743 }
744
745 // Add constraint to download logs if the columns matches.
746 if ( ! empty( $download_permissions_column_type ) && ! empty( $download_log_column_type ) && $download_permissions_column_type === $download_log_column_type ) {
747 $fk_result = $wpdb->get_row(
748 "SELECT COUNT(*) AS fk_count
749 FROM information_schema.TABLE_CONSTRAINTS
750 WHERE CONSTRAINT_SCHEMA = '{$wpdb->dbname}'
751 AND CONSTRAINT_NAME = 'fk_{$wpdb->prefix}wc_download_log_permission_id'
752 AND CONSTRAINT_TYPE = 'FOREIGN KEY'
753 AND TABLE_NAME = '{$wpdb->prefix}wc_download_log'"
754 ); // WPCS: unprepared SQL ok.
755 if ( 0 === (int) $fk_result->fk_count ) {
756 $wpdb->query(
757 "ALTER TABLE `{$wpdb->prefix}wc_download_log`
758 ADD CONSTRAINT `fk_{$wpdb->prefix}wc_download_log_permission_id`
759 FOREIGN KEY (`permission_id`)
760 REFERENCES `{$wpdb->prefix}woocommerce_downloadable_product_permissions` (`permission_id`) ON DELETE CASCADE;"
761 ); // WPCS: unprepared SQL ok.
762 }
763 }
764
765 // Clear table caches.
766 delete_transient( 'wc_attribute_taxonomies' );
767 }
768
769 /**
770 * Get Table schema.
771 *
772 * See https://github.com/woocommerce/woocommerce/wiki/Database-Description/
773 *
774 * A note on indexes; Indexes have a maximum size of 767 bytes. Historically, we haven't need to be concerned about that.
775 * As of WordPress 4.2, however, we moved to utf8mb4, which uses 4 bytes per character. This means that an index which
776 * used to have room for floor(767/3) = 255 characters, now only has room for floor(767/4) = 191 characters.
777 *
778 * Changing indexes may cause duplicate index notices in logs due to https://core.trac.wordpress.org/ticket/34870 but dropping
779 * indexes first causes too much load on some servers/larger DB.
780 *
781 * When adding or removing a table, make sure to update the list of tables in WC_Install::get_tables().
782 *
783 * @return string
784 */
785 private static function get_schema() {
786 global $wpdb;
787
788 $collate = '';
789
790 if ( $wpdb->has_cap( 'collation' ) ) {
791 $collate = $wpdb->get_charset_collate();
792 }
793
794 /*
795 * Indexes have a maximum size of 767 bytes. Historically, we haven't need to be concerned about that.
796 * As of WP 4.2, however, they moved to utf8mb4, which uses 4 bytes per character. This means that an index which
797 * used to have room for floor(767/3) = 255 characters, now only has room for floor(767/4) = 191 characters.
798 */
799 $max_index_length = 191;
800
801 $tables = "
802 CREATE TABLE {$wpdb->prefix}woocommerce_sessions (
803 session_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
804 session_key char(32) NOT NULL,
805 session_value longtext NOT NULL,
806 session_expiry BIGINT UNSIGNED NOT NULL,
807 PRIMARY KEY (session_id),
808 UNIQUE KEY session_key (session_key)
809 ) $collate;
810 CREATE TABLE {$wpdb->prefix}woocommerce_api_keys (
811 key_id BIGINT UNSIGNED NOT NULL auto_increment,
812 user_id BIGINT UNSIGNED NOT NULL,
813 description varchar(200) NULL,
814 permissions varchar(10) NOT NULL,
815 consumer_key char(64) NOT NULL,
816 consumer_secret char(43) NOT NULL,
817 nonces longtext NULL,
818 truncated_key char(7) NOT NULL,
819 last_access datetime NULL default null,
820 PRIMARY KEY (key_id),
821 KEY consumer_key (consumer_key),
822 KEY consumer_secret (consumer_secret)
823 ) $collate;
824 CREATE TABLE {$wpdb->prefix}woocommerce_attribute_taxonomies (
825 attribute_id BIGINT UNSIGNED NOT NULL auto_increment,
826 attribute_name varchar(200) NOT NULL,
827 attribute_label varchar(200) NULL,
828 attribute_type varchar(20) NOT NULL,
829 attribute_orderby varchar(20) NOT NULL,
830 attribute_public int(1) NOT NULL DEFAULT 1,
831 PRIMARY KEY (attribute_id),
832 KEY attribute_name (attribute_name(20))
833 ) $collate;
834 CREATE TABLE {$wpdb->prefix}woocommerce_downloadable_product_permissions (
835 permission_id BIGINT UNSIGNED NOT NULL auto_increment,
836 download_id varchar(36) NOT NULL,
837 product_id BIGINT UNSIGNED NOT NULL,
838 order_id BIGINT UNSIGNED NOT NULL DEFAULT 0,
839 order_key varchar(200) NOT NULL,
840 user_email varchar(200) NOT NULL,
841 user_id BIGINT UNSIGNED NULL,
842 downloads_remaining varchar(9) NULL,
843 access_granted datetime NOT NULL default '0000-00-00 00:00:00',
844 access_expires datetime NULL default null,
845 download_count BIGINT UNSIGNED NOT NULL DEFAULT 0,
846 PRIMARY KEY (permission_id),
847 KEY download_order_key_product (product_id,order_id,order_key(16),download_id),
848 KEY download_order_product (download_id,order_id,product_id),
849 KEY order_id (order_id),
850 KEY user_order_remaining_expires (user_id,order_id,downloads_remaining,access_expires)
851 ) $collate;
852 CREATE TABLE {$wpdb->prefix}woocommerce_order_items (
853 order_item_id BIGINT UNSIGNED NOT NULL auto_increment,
854 order_item_name TEXT NOT NULL,
855 order_item_type varchar(200) NOT NULL DEFAULT '',
856 order_id BIGINT UNSIGNED NOT NULL,
857 PRIMARY KEY (order_item_id),
858 KEY order_id (order_id)
859 ) $collate;
860 CREATE TABLE {$wpdb->prefix}woocommerce_order_itemmeta (
861 meta_id BIGINT UNSIGNED NOT NULL auto_increment,
862 order_item_id BIGINT UNSIGNED NOT NULL,
863 meta_key varchar(255) default NULL,
864 meta_value longtext NULL,
865 PRIMARY KEY (meta_id),
866 KEY order_item_id (order_item_id),
867 KEY meta_key (meta_key(32))
868 ) $collate;
869 CREATE TABLE {$wpdb->prefix}woocommerce_tax_rates (
870 tax_rate_id BIGINT UNSIGNED NOT NULL auto_increment,
871 tax_rate_country varchar(2) NOT NULL DEFAULT '',
872 tax_rate_state varchar(200) NOT NULL DEFAULT '',
873 tax_rate varchar(8) NOT NULL DEFAULT '',
874 tax_rate_name varchar(200) NOT NULL DEFAULT '',
875 tax_rate_priority BIGINT UNSIGNED NOT NULL,
876 tax_rate_compound int(1) NOT NULL DEFAULT 0,
877 tax_rate_shipping int(1) NOT NULL DEFAULT 1,
878 tax_rate_order BIGINT UNSIGNED NOT NULL,
879 tax_rate_class varchar(200) NOT NULL DEFAULT '',
880 PRIMARY KEY (tax_rate_id),
881 KEY tax_rate_country (tax_rate_country),
882 KEY tax_rate_state (tax_rate_state(2)),
883 KEY tax_rate_class (tax_rate_class(10)),
884 KEY tax_rate_priority (tax_rate_priority)
885 ) $collate;
886 CREATE TABLE {$wpdb->prefix}woocommerce_tax_rate_locations (
887 location_id BIGINT UNSIGNED NOT NULL auto_increment,
888 location_code varchar(200) NOT NULL,
889 tax_rate_id BIGINT UNSIGNED NOT NULL,
890 location_type varchar(40) NOT NULL,
891 PRIMARY KEY (location_id),
892 KEY tax_rate_id (tax_rate_id),
893 KEY location_type_code (location_type(10),location_code(20))
894 ) $collate;
895 CREATE TABLE {$wpdb->prefix}woocommerce_shipping_zones (
896 zone_id BIGINT UNSIGNED NOT NULL auto_increment,
897 zone_name varchar(200) NOT NULL,
898 zone_order BIGINT UNSIGNED NOT NULL,
899 PRIMARY KEY (zone_id)
900 ) $collate;
901 CREATE TABLE {$wpdb->prefix}woocommerce_shipping_zone_locations (
902 location_id BIGINT UNSIGNED NOT NULL auto_increment,
903 zone_id BIGINT UNSIGNED NOT NULL,
904 location_code varchar(200) NOT NULL,
905 location_type varchar(40) NOT NULL,
906 PRIMARY KEY (location_id),
907 KEY location_id (location_id),
908 KEY location_type_code (location_type(10),location_code(20))
909 ) $collate;
910 CREATE TABLE {$wpdb->prefix}woocommerce_shipping_zone_methods (
911 zone_id BIGINT UNSIGNED NOT NULL,
912 instance_id BIGINT UNSIGNED NOT NULL auto_increment,
913 method_id varchar(200) NOT NULL,
914 method_order BIGINT UNSIGNED NOT NULL,
915 is_enabled tinyint(1) NOT NULL DEFAULT '1',
916 PRIMARY KEY (instance_id)
917 ) $collate;
918 CREATE TABLE {$wpdb->prefix}woocommerce_payment_tokens (
919 token_id BIGINT UNSIGNED NOT NULL auto_increment,
920 gateway_id varchar(200) NOT NULL,
921 token text NOT NULL,
922 user_id BIGINT UNSIGNED NOT NULL DEFAULT '0',
923 type varchar(200) NOT NULL,
924 is_default tinyint(1) NOT NULL DEFAULT '0',
925 PRIMARY KEY (token_id),
926 KEY user_id (user_id)
927 ) $collate;
928 CREATE TABLE {$wpdb->prefix}woocommerce_payment_tokenmeta (
929 meta_id BIGINT UNSIGNED NOT NULL auto_increment,
930 payment_token_id BIGINT UNSIGNED NOT NULL,
931 meta_key varchar(255) NULL,
932 meta_value longtext NULL,
933 PRIMARY KEY (meta_id),
934 KEY payment_token_id (payment_token_id),
935 KEY meta_key (meta_key(32))
936 ) $collate;
937 CREATE TABLE {$wpdb->prefix}woocommerce_log (
938 log_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
939 timestamp datetime NOT NULL,
940 level smallint(4) NOT NULL,
941 source varchar(200) NOT NULL,
942 message longtext NOT NULL,
943 context longtext NULL,
944 PRIMARY KEY (log_id),
945 KEY level (level)
946 ) $collate;
947 CREATE TABLE {$wpdb->prefix}wc_webhooks (
948 webhook_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
949 status varchar(200) NOT NULL,
950 name text NOT NULL,
951 user_id BIGINT UNSIGNED NOT NULL,
952 delivery_url text NOT NULL,
953 secret text NOT NULL,
954 topic varchar(200) NOT NULL,
955 date_created datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
956 date_created_gmt datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
957 date_modified datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
958 date_modified_gmt datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
959 api_version smallint(4) NOT NULL,
960 failure_count smallint(10) NOT NULL DEFAULT '0',
961 pending_delivery tinyint(1) NOT NULL DEFAULT '0',
962 PRIMARY KEY (webhook_id),
963 KEY user_id (user_id)
964 ) $collate;
965 CREATE TABLE {$wpdb->prefix}wc_download_log (
966 download_log_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
967 timestamp datetime NOT NULL,
968 permission_id BIGINT UNSIGNED NOT NULL,
969 user_id BIGINT UNSIGNED NULL,
970 user_ip_address VARCHAR(100) NULL DEFAULT '',
971 PRIMARY KEY (download_log_id),
972 KEY permission_id (permission_id),
973 KEY timestamp (timestamp)
974 ) $collate;
975 CREATE TABLE {$wpdb->prefix}wc_product_meta_lookup (
976 `product_id` bigint(20) NOT NULL,
977 `sku` varchar(100) NULL default '',
978 `virtual` tinyint(1) NULL default 0,
979 `downloadable` tinyint(1) NULL default 0,
980 `min_price` decimal(19,4) NULL default NULL,
981 `max_price` decimal(19,4) NULL default NULL,
982 `onsale` tinyint(1) NULL default 0,
983 `stock_quantity` double NULL default NULL,
984 `stock_status` varchar(100) NULL default 'instock',
985 `rating_count` bigint(20) NULL default 0,
986 `average_rating` decimal(3,2) NULL default 0.00,
987 `total_sales` bigint(20) NULL default 0,
988 `tax_status` varchar(100) NULL default 'taxable',
989 `tax_class` varchar(100) NULL default '',
990 PRIMARY KEY (`product_id`),
991 KEY `virtual` (`virtual`),
992 KEY `downloadable` (`downloadable`),
993 KEY `stock_status` (`stock_status`),
994 KEY `stock_quantity` (`stock_quantity`),
995 KEY `onsale` (`onsale`),
996 KEY min_max_price (`min_price`, `max_price`)
997 ) $collate;
998 CREATE TABLE {$wpdb->prefix}wc_tax_rate_classes (
999 tax_rate_class_id BIGINT UNSIGNED NOT NULL auto_increment,
1000 name varchar(200) NOT NULL DEFAULT '',
1001 slug varchar(200) NOT NULL DEFAULT '',
1002 PRIMARY KEY (tax_rate_class_id),
1003 UNIQUE KEY slug (slug($max_index_length))
1004 ) $collate;
1005 CREATE TABLE {$wpdb->prefix}wc_reserved_stock (
1006 `order_id` bigint(20) NOT NULL,
1007 `product_id` bigint(20) NOT NULL,
1008 `stock_quantity` double NOT NULL DEFAULT 0,
1009 `timestamp` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
1010 `expires` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
1011 PRIMARY KEY (`order_id`, `product_id`)
1012 ) $collate;
1013 ";
1014
1015 return $tables;
1016 }
1017
1018 /**
1019 * Return a list of WooCommerce tables. Used to make sure all WC tables are dropped when uninstalling the plugin
1020 * in a single site or multi site environment.
1021 *
1022 * @return array WC tables.
1023 */
1024 public static function get_tables() {
1025 global $wpdb;
1026
1027 $tables = array(
1028 "{$wpdb->prefix}wc_download_log",
1029 "{$wpdb->prefix}wc_product_meta_lookup",
1030 "{$wpdb->prefix}wc_tax_rate_classes",
1031 "{$wpdb->prefix}wc_webhooks",
1032 "{$wpdb->prefix}woocommerce_api_keys",
1033 "{$wpdb->prefix}woocommerce_attribute_taxonomies",
1034 "{$wpdb->prefix}woocommerce_downloadable_product_permissions",
1035 "{$wpdb->prefix}woocommerce_log",
1036 "{$wpdb->prefix}woocommerce_order_itemmeta",
1037 "{$wpdb->prefix}woocommerce_order_items",
1038 "{$wpdb->prefix}woocommerce_payment_tokenmeta",
1039 "{$wpdb->prefix}woocommerce_payment_tokens",
1040 "{$wpdb->prefix}woocommerce_sessions",
1041 "{$wpdb->prefix}woocommerce_shipping_zone_locations",
1042 "{$wpdb->prefix}woocommerce_shipping_zone_methods",
1043 "{$wpdb->prefix}woocommerce_shipping_zones",
1044 "{$wpdb->prefix}woocommerce_tax_rate_locations",
1045 "{$wpdb->prefix}woocommerce_tax_rates",
1046 "{$wpdb->prefix}wc_reserved_stock",
1047 );
1048
1049 /**
1050 * Filter the list of known WooCommerce tables.
1051 *
1052 * If WooCommerce plugins need to add new tables, they can inject them here.
1053 *
1054 * @param array $tables An array of WooCommerce-specific database table names.
1055 */
1056 $tables = apply_filters( 'woocommerce_install_get_tables', $tables );
1057
1058 return $tables;
1059 }
1060
1061 /**
1062 * Drop WooCommerce tables.
1063 *
1064 * @return void
1065 */
1066 public static function drop_tables() {
1067 global $wpdb;
1068
1069 $tables = self::get_tables();
1070
1071 foreach ( $tables as $table ) {
1072 $wpdb->query( "DROP TABLE IF EXISTS {$table}" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
1073 }
1074 }
1075
1076 /**
1077 * Uninstall tables when MU blog is deleted.
1078 *
1079 * @param array $tables List of tables that will be deleted by WP.
1080 *
1081 * @return string[]
1082 */
1083 public static function wpmu_drop_tables( $tables ) {
1084 return array_merge( $tables, self::get_tables() );
1085 }
1086
1087 /**
1088 * Create roles and capabilities.
1089 */
1090 public static function create_roles() {
1091 global $wp_roles;
1092
1093 if ( ! class_exists( 'WP_Roles' ) ) {
1094 return;
1095 }
1096
1097 if ( ! isset( $wp_roles ) ) {
1098 $wp_roles = new WP_Roles(); // @codingStandardsIgnoreLine
1099 }
1100
1101 // Dummy gettext calls to get strings in the catalog.
1102 /* translators: user role */
1103 _x( 'Customer', 'User role', 'woocommerce' );
1104 /* translators: user role */
1105 _x( 'Shop manager', 'User role', 'woocommerce' );
1106
1107 // Customer role.
1108 add_role(
1109 'customer',
1110 'Customer',
1111 array(
1112 'read' => true,
1113 )
1114 );
1115
1116 // Shop manager role.
1117 add_role(
1118 'shop_manager',
1119 'Shop manager',
1120 array(
1121 'level_9' => true,
1122 'level_8' => true,
1123 'level_7' => true,
1124 'level_6' => true,
1125 'level_5' => true,
1126 'level_4' => true,
1127 'level_3' => true,
1128 'level_2' => true,
1129 'level_1' => true,
1130 'level_0' => true,
1131 'read' => true,
1132 'read_private_pages' => true,
1133 'read_private_posts' => true,
1134 'edit_posts' => true,
1135 'edit_pages' => true,
1136 'edit_published_posts' => true,
1137 'edit_published_pages' => true,
1138 'edit_private_pages' => true,
1139 'edit_private_posts' => true,
1140 'edit_others_posts' => true,
1141 'edit_others_pages' => true,
1142 'publish_posts' => true,
1143 'publish_pages' => true,
1144 'delete_posts' => true,
1145 'delete_pages' => true,
1146 'delete_private_pages' => true,
1147 'delete_private_posts' => true,
1148 'delete_published_pages' => true,
1149 'delete_published_posts' => true,
1150 'delete_others_posts' => true,
1151 'delete_others_pages' => true,
1152 'manage_categories' => true,
1153 'manage_links' => true,
1154 'moderate_comments' => true,
1155 'upload_files' => true,
1156 'export' => true,
1157 'import' => true,
1158 'list_users' => true,
1159 'edit_theme_options' => true,
1160 )
1161 );
1162
1163 $capabilities = self::get_core_capabilities();
1164
1165 foreach ( $capabilities as $cap_group ) {
1166 foreach ( $cap_group as $cap ) {
1167 $wp_roles->add_cap( 'shop_manager', $cap );
1168 $wp_roles->add_cap( 'administrator', $cap );
1169 }
1170 }
1171 }
1172
1173 /**
1174 * Get capabilities for WooCommerce - these are assigned to admin/shop manager during installation or reset.
1175 *
1176 * @return array
1177 */
1178 private static function get_core_capabilities() {
1179 $capabilities = array();
1180
1181 $capabilities['core'] = array(
1182 'manage_woocommerce',
1183 'view_woocommerce_reports',
1184 );
1185
1186 $capability_types = array( 'product', 'shop_order', 'shop_coupon' );
1187
1188 foreach ( $capability_types as $capability_type ) {
1189
1190 $capabilities[ $capability_type ] = array(
1191 // Post type.
1192 "edit_{$capability_type}",
1193 "read_{$capability_type}",
1194 "delete_{$capability_type}",
1195 "edit_{$capability_type}s",
1196 "edit_others_{$capability_type}s",
1197 "publish_{$capability_type}s",
1198 "read_private_{$capability_type}s",
1199 "delete_{$capability_type}s",
1200 "delete_private_{$capability_type}s",
1201 "delete_published_{$capability_type}s",
1202 "delete_others_{$capability_type}s",
1203 "edit_private_{$capability_type}s",
1204 "edit_published_{$capability_type}s",
1205
1206 // Terms.
1207 "manage_{$capability_type}_terms",
1208 "edit_{$capability_type}_terms",
1209 "delete_{$capability_type}_terms",
1210 "assign_{$capability_type}_terms",
1211 );
1212 }
1213
1214 return $capabilities;
1215 }
1216
1217 /**
1218 * Remove WooCommerce roles.
1219 */
1220 public static function remove_roles() {
1221 global $wp_roles;
1222
1223 if ( ! class_exists( 'WP_Roles' ) ) {
1224 return;
1225 }
1226
1227 if ( ! isset( $wp_roles ) ) {
1228 $wp_roles = new WP_Roles(); // @codingStandardsIgnoreLine
1229 }
1230
1231 $capabilities = self::get_core_capabilities();
1232
1233 foreach ( $capabilities as $cap_group ) {
1234 foreach ( $cap_group as $cap ) {
1235 $wp_roles->remove_cap( 'shop_manager', $cap );
1236 $wp_roles->remove_cap( 'administrator', $cap );
1237 }
1238 }
1239
1240 remove_role( 'customer' );
1241 remove_role( 'shop_manager' );
1242 }
1243
1244 /**
1245 * Create files/directories.
1246 */
1247 private static function create_files() {
1248 // Bypass if filesystem is read-only and/or non-standard upload system is used.
1249 if ( apply_filters( 'woocommerce_install_skip_create_files', false ) ) {
1250 return;
1251 }
1252
1253 // Install files and folders for uploading files and prevent hotlinking.
1254 $upload_dir = wp_get_upload_dir();
1255 $download_method = get_option( 'woocommerce_file_download_method', 'force' );
1256
1257 $files = array(
1258 array(
1259 'base' => $upload_dir['basedir'] . '/woocommerce_uploads',
1260 'file' => 'index.html',
1261 'content' => '',
1262 ),
1263 array(
1264 'base' => WC_LOG_DIR,
1265 'file' => '.htaccess',
1266 'content' => 'deny from all',
1267 ),
1268 array(
1269 'base' => WC_LOG_DIR,
1270 'file' => 'index.html',
1271 'content' => '',
1272 ),
1273 array(
1274 'base' => $upload_dir['basedir'] . '/woocommerce_uploads',
1275 'file' => '.htaccess',
1276 'content' => 'redirect' === $download_method ? 'Options -Indexes' : 'deny from all',
1277 ),
1278 );
1279
1280 foreach ( $files as $file ) {
1281 if ( wp_mkdir_p( $file['base'] ) && ! file_exists( trailingslashit( $file['base'] ) . $file['file'] ) ) {
1282 $file_handle = @fopen( trailingslashit( $file['base'] ) . $file['file'], 'wb' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_read_fopen
1283 if ( $file_handle ) {
1284 fwrite( $file_handle, $file['content'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fwrite
1285 fclose( $file_handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
1286 }
1287 }
1288 }
1289
1290 // Create attachment for placeholders.
1291 self::create_placeholder_image();
1292 }
1293
1294 /**
1295 * Create a placeholder image in the media library.
1296 *
1297 * @since 3.5.0
1298 */
1299 private static function create_placeholder_image() {
1300 $placeholder_image = get_option( 'woocommerce_placeholder_image', 0 );
1301
1302 // Validate current setting if set. If set, return.
1303 if ( ! empty( $placeholder_image ) ) {
1304 if ( ! is_numeric( $placeholder_image ) ) {
1305 return;
1306 } elseif ( $placeholder_image && wp_attachment_is_image( $placeholder_image ) ) {
1307 return;
1308 }
1309 }
1310
1311 $upload_dir = wp_upload_dir();
1312 $source = WC()->plugin_path() . '/assets/images/placeholder-attachment.png';
1313 $filename = $upload_dir['basedir'] . '/woocommerce-placeholder.png';
1314
1315 if ( ! file_exists( $filename ) ) {
1316 copy( $source, $filename ); // @codingStandardsIgnoreLine.
1317 }
1318
1319 if ( ! file_exists( $filename ) ) {
1320 update_option( 'woocommerce_placeholder_image', 0 );
1321 return;
1322 }
1323
1324 $filetype = wp_check_filetype( basename( $filename ), null );
1325 $attachment = array(
1326 'guid' => $upload_dir['url'] . '/' . basename( $filename ),
1327 'post_mime_type' => $filetype['type'],
1328 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
1329 'post_content' => '',
1330 'post_status' => 'inherit',
1331 );
1332 $attach_id = wp_insert_attachment( $attachment, $filename );
1333
1334 update_option( 'woocommerce_placeholder_image', $attach_id );
1335
1336 // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
1337 require_once ABSPATH . 'wp-admin/includes/image.php';
1338
1339 // Generate the metadata for the attachment, and update the database record.
1340 $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
1341 wp_update_attachment_metadata( $attach_id, $attach_data );
1342 }
1343
1344 /**
1345 * Show action links on the plugin screen.
1346 *
1347 * @param mixed $links Plugin Action links.
1348 *
1349 * @return array
1350 */
1351 public static function plugin_action_links( $links ) {
1352 $action_links = array(
1353 'settings' => '<a href="' . admin_url( 'admin.php?page=wc-settings' ) . '" aria-label="' . esc_attr__( 'View WooCommerce settings', 'woocommerce' ) . '">' . esc_html__( 'Settings', 'woocommerce' ) . '</a>',
1354 );
1355
1356 return array_merge( $action_links, $links );
1357 }
1358
1359 /**
1360 * Show row meta on the plugin screen.
1361 *
1362 * @param mixed $links Plugin Row Meta.
1363 * @param mixed $file Plugin Base file.
1364 *
1365 * @return array
1366 */
1367 public static function plugin_row_meta( $links, $file ) {
1368 if ( WC_PLUGIN_BASENAME === $file ) {
1369 $row_meta = array(
1370 'docs' => '<a href="' . esc_url( apply_filters( 'woocommerce_docs_url', 'https://docs.woocommerce.com/documentation/plugins/woocommerce/' ) ) . '" aria-label="' . esc_attr__( 'View WooCommerce documentation', 'woocommerce' ) . '">' . esc_html__( 'Docs', 'woocommerce' ) . '</a>',
1371 'apidocs' => '<a href="' . esc_url( apply_filters( 'woocommerce_apidocs_url', 'https://docs.woocommerce.com/wc-apidocs/' ) ) . '" aria-label="' . esc_attr__( 'View WooCommerce API docs', 'woocommerce' ) . '">' . esc_html__( 'API docs', 'woocommerce' ) . '</a>',
1372 'support' => '<a href="' . esc_url( apply_filters( 'woocommerce_support_url', 'https://woocommerce.com/my-account/tickets/' ) ) . '" aria-label="' . esc_attr__( 'Visit premium customer support', 'woocommerce' ) . '">' . esc_html__( 'Premium support', 'woocommerce' ) . '</a>',
1373 );
1374
1375 return array_merge( $links, $row_meta );
1376 }
1377
1378 return (array) $links;
1379 }
1380
1381 /**
1382 * Get slug from path and associate it with the path.
1383 *
1384 * @param array $plugins Associative array of plugin files to paths.
1385 * @param string $key Plugin relative path. Example: woocommerce/woocommerce.php.
1386 */
1387 private static function associate_plugin_file( $plugins, $key ) {
1388 $path = explode( '/', $key );
1389 $filename = end( $path );
1390 $plugins[ $filename ] = $key;
1391 return $plugins;
1392 }
1393
1394 /**
1395 * Install a plugin from .org in the background via a cron job (used by
1396 * installer - opt in).
1397 *
1398 * @param string $plugin_to_install_id Plugin ID.
1399 * @param array $plugin_to_install Plugin information.
1400 *
1401 * @throws Exception If unable to proceed with plugin installation.
1402 * @since 2.6.0
1403 */
1404 public static function background_installer( $plugin_to_install_id, $plugin_to_install ) {
1405 // Explicitly clear the event.
1406 $args = func_get_args();
1407
1408 if ( ! empty( $plugin_to_install['repo-slug'] ) ) {
1409 require_once ABSPATH . 'wp-admin/includes/file.php';
1410 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
1411 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
1412 require_once ABSPATH . 'wp-admin/includes/plugin.php';
1413
1414 WP_Filesystem();
1415
1416 $skin = new Automatic_Upgrader_Skin();
1417 $upgrader = new WP_Upgrader( $skin );
1418 $installed_plugins = array_reduce( array_keys( get_plugins() ), array( __CLASS__, 'associate_plugin_file' ) );
1419 if ( empty( $installed_plugins ) ) {
1420 $installed_plugins = array();
1421 }
1422 $plugin_slug = $plugin_to_install['repo-slug'];
1423 $plugin_file = isset( $plugin_to_install['file'] ) ? $plugin_to_install['file'] : $plugin_slug . '.php';
1424 $installed = false;
1425 $activate = false;
1426
1427 // See if the plugin is installed already.
1428 if ( isset( $installed_plugins[ $plugin_file ] ) ) {
1429 $installed = true;
1430 $activate = ! is_plugin_active( $installed_plugins[ $plugin_file ] );
1431 }
1432
1433 // Install this thing!
1434 if ( ! $installed ) {
1435 // Suppress feedback.
1436 ob_start();
1437
1438 try {
1439 $plugin_information = plugins_api(
1440 'plugin_information',
1441 array(
1442 'slug' => $plugin_slug,
1443 'fields' => array(
1444 'short_description' => false,
1445 'sections' => false,
1446 'requires' => false,
1447 'rating' => false,
1448 'ratings' => false,
1449 'downloaded' => false,
1450 'last_updated' => false,
1451 'added' => false,
1452 'tags' => false,
1453 'homepage' => false,
1454 'donate_link' => false,
1455 'author_profile' => false,
1456 'author' => false,
1457 ),
1458 )
1459 );
1460
1461 if ( is_wp_error( $plugin_information ) ) {
1462 throw new Exception( $plugin_information->get_error_message() );
1463 }
1464
1465 $package = $plugin_information->download_link;
1466 $download = $upgrader->download_package( $package );
1467
1468 if ( is_wp_error( $download ) ) {
1469 throw new Exception( $download->get_error_message() );
1470 }
1471
1472 $working_dir = $upgrader->unpack_package( $download, true );
1473
1474 if ( is_wp_error( $working_dir ) ) {
1475 throw new Exception( $working_dir->get_error_message() );
1476 }
1477
1478 $result = $upgrader->install_package(
1479 array(
1480 'source' => $working_dir,
1481 'destination' => WP_PLUGIN_DIR,
1482 'clear_destination' => false,
1483 'abort_if_destination_exists' => false,
1484 'clear_working' => true,
1485 'hook_extra' => array(
1486 'type' => 'plugin',
1487 'action' => 'install',
1488 ),
1489 )
1490 );
1491
1492 if ( is_wp_error( $result ) ) {
1493 throw new Exception( $result->get_error_message() );
1494 }
1495
1496 $activate = true;
1497
1498 } catch ( Exception $e ) {
1499 WC_Admin_Notices::add_custom_notice(
1500 $plugin_to_install_id . '_install_error',
1501 sprintf(
1502 // translators: 1: plugin name, 2: error message, 3: URL to install plugin manually.
1503 __( '%1$s could not be installed (%2$s). <a href="%3$s">Please install it manually by clicking here.</a>', 'woocommerce' ),
1504 $plugin_to_install['name'],
1505 $e->getMessage(),
1506 esc_url( admin_url( 'index.php?wc-install-plugin-redirect=' . $plugin_slug ) )
1507 )
1508 );
1509 }
1510
1511 // Discard feedback.
1512 ob_end_clean();
1513 }
1514
1515 wp_clean_plugins_cache();
1516
1517 // Activate this thing.
1518 if ( $activate ) {
1519 try {
1520 add_action( 'add_option_mailchimp_woocommerce_plugin_do_activation_redirect', array( __CLASS__, 'remove_mailchimps_redirect' ), 10, 2 );
1521 $result = activate_plugin( $installed ? $installed_plugins[ $plugin_file ] : $plugin_slug . '/' . $plugin_file );
1522
1523 if ( is_wp_error( $result ) ) {
1524 throw new Exception( $result->get_error_message() );
1525 }
1526 } catch ( Exception $e ) {
1527 WC_Admin_Notices::add_custom_notice(
1528 $plugin_to_install_id . '_install_error',
1529 sprintf(
1530 // translators: 1: plugin name, 2: URL to WP plugin page.
1531 __( '%1$s was installed but could not be activated. <a href="%2$s">Please activate it manually by clicking here.</a>', 'woocommerce' ),
1532 $plugin_to_install['name'],
1533 admin_url( 'plugins.php' )
1534 )
1535 );
1536 }
1537 }
1538 }
1539 }
1540
1541 /**
1542 * Removes redirect added during MailChimp plugin's activation.
1543 *
1544 * @param string $option Option name.
1545 * @param string $value Option value.
1546 */
1547 public static function remove_mailchimps_redirect( $option, $value ) {
1548 // Remove this action to prevent infinite looping.
1549 remove_action( 'add_option_mailchimp_woocommerce_plugin_do_activation_redirect', array( __CLASS__, 'remove_mailchimps_redirect' ) );
1550
1551 // Update redirect back to false.
1552 update_option( 'mailchimp_woocommerce_plugin_do_activation_redirect', false );
1553 }
1554
1555 /**
1556 * Install a theme from .org in the background via a cron job (used by installer - opt in).
1557 *
1558 * @param string $theme_slug Theme slug.
1559 *
1560 * @throws Exception If unable to proceed with theme installation.
1561 * @since 3.1.0
1562 */
1563 public static function theme_background_installer( $theme_slug ) {
1564 // Explicitly clear the event.
1565 $args = func_get_args();
1566
1567 if ( ! empty( $theme_slug ) ) {
1568 // Suppress feedback.
1569 ob_start();
1570
1571 try {
1572 $theme = wp_get_theme( $theme_slug );
1573
1574 if ( ! $theme->exists() ) {
1575 require_once ABSPATH . 'wp-admin/includes/file.php';
1576 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
1577 include_once ABSPATH . 'wp-admin/includes/theme.php';
1578
1579 WP_Filesystem();
1580
1581 $skin = new Automatic_Upgrader_Skin();
1582 $upgrader = new Theme_Upgrader( $skin );
1583 $api = themes_api(
1584 'theme_information',
1585 array(
1586 'slug' => $theme_slug,
1587 'fields' => array( 'sections' => false ),
1588 )
1589 );
1590 $result = $upgrader->install( $api->download_link );
1591
1592 if ( is_wp_error( $result ) ) {
1593 throw new Exception( $result->get_error_message() );
1594 } elseif ( is_wp_error( $skin->result ) ) {
1595 throw new Exception( $skin->result->get_error_message() );
1596 } elseif ( is_null( $result ) ) {
1597 throw new Exception( 'Unable to connect to the filesystem. Please confirm your credentials.' );
1598 }
1599 }
1600
1601 switch_theme( $theme_slug );
1602 } catch ( Exception $e ) {
1603 WC_Admin_Notices::add_custom_notice(
1604 $theme_slug . '_install_error',
1605 sprintf(
1606 // translators: 1: theme slug, 2: error message, 3: URL to install theme manually.
1607 __( '%1$s could not be installed (%2$s). <a href="%3$s">Please install it manually by clicking here.</a>', 'woocommerce' ),
1608 $theme_slug,
1609 $e->getMessage(),
1610 esc_url( admin_url( 'update.php?action=install-theme&theme=' . $theme_slug . '&_wpnonce=' . wp_create_nonce( 'install-theme_' . $theme_slug ) ) )
1611 )
1612 );
1613 }
1614
1615 // Discard feedback.
1616 ob_end_clean();
1617 }
1618 }
1619 }
1620
1621 WC_Install::init();
1622