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