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