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