AI
2 years ago
AIContent
1 year ago
Assets
2 years ago
BlockTypes
1 year ago
Domain
1 year ago
Images
2 years ago
Integrations
2 years ago
Interactivity
2 years ago
InteractivityComponents
1 year ago
Patterns
1 year ago
Payments
2 years ago
Registry
2 years ago
Shipping
2 years ago
Templates
1 year ago
Utils
1 year ago
Assets.php
2 years ago
AssetsController.php
1 year ago
BlockPatterns.php
1 year ago
BlockTemplatesController.php
1 year ago
BlockTemplatesRegistry.php
1 year ago
BlockTypesController.php
1 year ago
InboxNotifications.php
2 years ago
Installer.php
1 year ago
Library.php
2 years ago
Migration.php
2 years ago
Options.php
2 years ago
Package.php
1 year ago
QueryFilters.php
2 years ago
TemplateOptions.php
1 year ago
Migration.php
109 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks; |
| 3 | |
| 4 | use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils; |
| 5 | |
| 6 | /** |
| 7 | * Takes care of the migrations. |
| 8 | * |
| 9 | * @since 2.5.0 |
| 10 | */ |
| 11 | class Migration { |
| 12 | /** |
| 13 | * DB updates and callbacks that need to be run per version. |
| 14 | * |
| 15 | * Please note that these functions are invoked when WooCommerce Blocks is updated from a previous version, |
| 16 | * but NOT when WooCommerce Blocks is newly installed. |
| 17 | * |
| 18 | * @var array |
| 19 | */ |
| 20 | private $db_upgrades = array( |
| 21 | '10.3.0' => array( |
| 22 | 'wc_blocks_update_1030_blockified_product_grid_block', |
| 23 | ), |
| 24 | '11.2.0' => array( |
| 25 | 'wc_blocks_update_1120_rename_checkout_template', |
| 26 | 'wc_blocks_update_1120_rename_cart_template', |
| 27 | ), |
| 28 | ); |
| 29 | |
| 30 | /** |
| 31 | * Runs all the necessary migrations. |
| 32 | * |
| 33 | * @var array |
| 34 | */ |
| 35 | public function run_migrations() { |
| 36 | $current_db_version = get_option( Options::WC_BLOCK_VERSION, '' ); |
| 37 | $schema_version = get_option( 'wc_blocks_db_schema_version', '' ); |
| 38 | |
| 39 | // This check is necessary because the version was not being set in the database until 10.3.0. |
| 40 | // Checking wc_blocks_db_schema_version determines if it's a fresh install (value will be empty) |
| 41 | // or an update from WC Blocks older than 10.3.0 (it will have some value). In the latter scenario |
| 42 | // we should run the migration. |
| 43 | // We can remove this check in the next months. |
| 44 | if ( ! empty( $schema_version ) && ( empty( $current_db_version ) ) ) { |
| 45 | $this->wc_blocks_update_1030_blockified_product_grid_block(); |
| 46 | } |
| 47 | |
| 48 | if ( empty( $current_db_version ) ) { |
| 49 | // This is a fresh install, so we don't need to run any migrations. |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | foreach ( $this->db_upgrades as $version => $update_callbacks ) { |
| 54 | if ( version_compare( $current_db_version, $version, '<' ) ) { |
| 55 | foreach ( $update_callbacks as $update_callback ) { |
| 56 | $this->{$update_callback}(); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Set a flag to indicate if the blockified Product Grid Block should be rendered by default. |
| 64 | */ |
| 65 | public static function wc_blocks_update_1030_blockified_product_grid_block() { |
| 66 | update_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE, wc_bool_to_string( false ) ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Rename `checkout` template to `page-checkout`. |
| 71 | */ |
| 72 | public static function wc_blocks_update_1120_rename_checkout_template() { |
| 73 | $template = get_block_template( BlockTemplateUtils::PLUGIN_SLUG . '//checkout', 'wp_template' ); |
| 74 | |
| 75 | if ( $template && ! empty( $template->wp_id ) ) { |
| 76 | if ( ! defined( 'WP_POST_REVISIONS' ) ) { |
| 77 | // This prevents a fatal error when ran outside of admin context. |
| 78 | define( 'WP_POST_REVISIONS', false ); |
| 79 | } |
| 80 | wp_update_post( |
| 81 | array( |
| 82 | 'ID' => $template->wp_id, |
| 83 | 'post_name' => 'page-checkout', |
| 84 | ) |
| 85 | ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Rename `cart` template to `page-cart`. |
| 91 | */ |
| 92 | public static function wc_blocks_update_1120_rename_cart_template() { |
| 93 | $template = get_block_template( BlockTemplateUtils::PLUGIN_SLUG . '//cart', 'wp_template' ); |
| 94 | |
| 95 | if ( $template && ! empty( $template->wp_id ) ) { |
| 96 | if ( ! defined( 'WP_POST_REVISIONS' ) ) { |
| 97 | // This prevents a fatal error when ran outside of admin context. |
| 98 | define( 'WP_POST_REVISIONS', false ); |
| 99 | } |
| 100 | wp_update_post( |
| 101 | array( |
| 102 | 'ID' => $template->wp_id, |
| 103 | 'post_name' => 'page-cart', |
| 104 | ) |
| 105 | ); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 |