AI
1 year ago
AIContent
1 year ago
Assets
1 year ago
BlockTypes
11 months ago
Domain
11 months ago
Images
1 year ago
Integrations
2 years ago
Patterns
11 months ago
Payments
1 year ago
Registry
2 years ago
Shipping
11 months ago
Templates
11 months ago
Utils
11 months ago
Assets.php
2 years ago
AssetsController.php
11 months ago
BlockPatterns.php
11 months ago
BlockTemplatesController.php
1 year ago
BlockTemplatesRegistry.php
11 months ago
BlockTypesController.php
11 months ago
InboxNotifications.php
2 years ago
Installer.php
1 year ago
Library.php
2 years ago
Options.php
2 years ago
Package.php
1 year ago
QueryFilters.php
1 year ago
TemplateOptions.php
1 year ago
Installer.php
133 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks; |
| 3 | |
| 4 | /** |
| 5 | * Installer class. |
| 6 | * Handles installation of Blocks plugin dependencies. |
| 7 | * |
| 8 | * @internal |
| 9 | */ |
| 10 | class Installer { |
| 11 | /** |
| 12 | * Initialize class features. |
| 13 | */ |
| 14 | public function init() { |
| 15 | add_action( 'admin_init', array( $this, 'install' ) ); |
| 16 | add_filter( 'woocommerce_create_pages', array( $this, 'create_pages' ) ); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Installation tasks ran on admin_init callback. |
| 21 | */ |
| 22 | public function install() { |
| 23 | $this->maybe_create_tables(); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Modifies default page content replacing it with classic shortcode block. |
| 28 | * We check for shortcode as default because after WooCommerce 8.3, block-based checkout is used by default. |
| 29 | * This only runs on Tools > Create Pages as the filter is not applied on WooCommerce plugin activation. |
| 30 | * |
| 31 | * @param array $pages Default pages. |
| 32 | * @return array |
| 33 | */ |
| 34 | public function create_pages( $pages ) { |
| 35 | |
| 36 | if ( '<!-- wp:shortcode -->[woocommerce_cart]<!-- /wp:shortcode -->' === ( $pages['cart']['content'] ?? null ) ) { |
| 37 | $pages['cart']['content'] = '<!-- wp:woocommerce/classic-shortcode {"shortcode":"cart"} /-->'; |
| 38 | } |
| 39 | |
| 40 | if ( '<!-- wp:shortcode -->[woocommerce_checkout]<!-- /wp:shortcode -->' === ( $pages['checkout']['content'] ?? null ) ) { |
| 41 | $pages['checkout']['content'] = '<!-- wp:woocommerce/classic-shortcode {"shortcode":"checkout"} /-->'; |
| 42 | } |
| 43 | |
| 44 | return $pages; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Set up the database tables which the plugin needs to function. |
| 49 | */ |
| 50 | public function maybe_create_tables() { |
| 51 | global $wpdb; |
| 52 | |
| 53 | $schema_version = 260; |
| 54 | $db_schema_version = (int) get_option( 'wc_blocks_db_schema_version', 0 ); |
| 55 | |
| 56 | if ( $db_schema_version >= $schema_version && 0 !== $db_schema_version ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | $show_errors = $wpdb->hide_errors(); |
| 61 | $table_name = $wpdb->prefix . 'wc_reserved_stock'; |
| 62 | $collate = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : ''; |
| 63 | $exists = $this->maybe_create_table( |
| 64 | $wpdb->prefix . 'wc_reserved_stock', |
| 65 | " |
| 66 | CREATE TABLE {$wpdb->prefix}wc_reserved_stock ( |
| 67 | `order_id` bigint(20) NOT NULL, |
| 68 | `product_id` bigint(20) NOT NULL, |
| 69 | `stock_quantity` double NOT NULL DEFAULT 0, |
| 70 | `timestamp` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', |
| 71 | `expires` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', |
| 72 | PRIMARY KEY (`order_id`, `product_id`) |
| 73 | ) $collate; |
| 74 | " |
| 75 | ); |
| 76 | |
| 77 | if ( $show_errors ) { |
| 78 | $wpdb->show_errors(); |
| 79 | } |
| 80 | |
| 81 | if ( ! $exists ) { |
| 82 | return $this->add_create_table_notice( $table_name ); |
| 83 | } |
| 84 | |
| 85 | // Update succeeded. This is only updated when successful and validated. |
| 86 | // $schema_version should be incremented when changes to schema are made within this method. |
| 87 | update_option( 'wc_blocks_db_schema_version', $schema_version ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Create database table, if it doesn't already exist. |
| 92 | * |
| 93 | * Based on admin/install-helper.php maybe_create_table function. |
| 94 | * |
| 95 | * @param string $table_name Database table name. |
| 96 | * @param string $create_sql Create database table SQL. |
| 97 | * @return bool False on error, true if already exists or success. |
| 98 | */ |
| 99 | protected function maybe_create_table( $table_name, $create_sql ) { |
| 100 | global $wpdb; |
| 101 | |
| 102 | if ( in_array( $table_name, $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ), 0 ), true ) ) { |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | $wpdb->query( $create_sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 107 | |
| 108 | return in_array( $table_name, $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ), 0 ), true ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Add a notice if table creation fails. |
| 113 | * |
| 114 | * @param string $table_name Name of the missing table. |
| 115 | */ |
| 116 | protected function add_create_table_notice( $table_name ) { |
| 117 | add_action( |
| 118 | 'admin_notices', |
| 119 | function() use ( $table_name ) { |
| 120 | echo '<div class="error"><p>'; |
| 121 | printf( |
| 122 | /* translators: %1$s table name, %2$s database user, %3$s database name. */ |
| 123 | esc_html__( 'WooCommerce %1$s table creation failed. Does the %2$s user have CREATE privileges on the %3$s database?', 'woocommerce' ), |
| 124 | '<code>' . esc_html( $table_name ) . '</code>', |
| 125 | '<code>' . esc_html( DB_USER ) . '</code>', |
| 126 | '<code>' . esc_html( DB_NAME ) . '</code>' |
| 127 | ); |
| 128 | echo '</p></div>'; |
| 129 | } |
| 130 | ); |
| 131 | } |
| 132 | } |
| 133 |