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