| 1 |
<?php |
| 2 |
|
| 3 |
namespace SureCart\Database; |
| 4 |
|
| 5 |
use SureCartCore\ServiceProviders\ServiceProviderInterface; |
| 6 |
|
| 7 |
/** |
| 8 |
* This service provider runs on every single update. |
| 9 |
*/ |
| 10 |
class UpdateMigrationServiceProvider implements ServiceProviderInterface { |
| 11 |
/** |
| 12 |
* {@inheritDoc} |
| 13 |
* |
| 14 |
* @param \Pimple\Container $container Service Container. |
| 15 |
*/ |
| 16 |
public function register( $container ) { |
| 17 |
// nothing to register. |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* {@inheritDoc} |
| 22 |
* |
| 23 |
* @param \Pimple\Container $container Service Container. |
| 24 |
*/ |
| 25 |
public function bootstrap( $container ) { |
| 26 |
// only run the migration if the version changes. |
| 27 |
add_action( 'admin_init', [ $this, 'run' ] ); |
| 28 |
// update the migration version on admin_init lower priority, after all migrations have run. |
| 29 |
add_action( 'admin_init', [ $this, 'updateMigrationVersion' ], 9999999 ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Run the migration. |
| 34 |
*/ |
| 35 |
public function run() { |
| 36 |
if ( ! $this->versionChanged() ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
// flush roles on every update. |
| 40 |
\SureCart::plugin()->roles()->create(); |
| 41 |
// make sure to check for and create cart post on every update. |
| 42 |
\SureCart::page_seeder()->createShopPage(); |
| 43 |
// make sure to check for and create cart post on every update. |
| 44 |
$this->handleCartMigration(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Update the migration version. |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public function updateMigrationVersion() { |
| 53 |
if ( ! $this->versionChanged() ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
update_option( 'surecart_migration_version', \SureCart::plugin()->version() ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Version has changed? |
| 61 |
* |
| 62 |
* @return boolean |
| 63 |
*/ |
| 64 |
public function versionChanged() { |
| 65 |
return version_compare( \SureCart::plugin()->version(), get_option( 'surecart_migration_version', '0.0.0' ), '!=' ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Handle cart migration |
| 70 |
* |
| 71 |
* @return \WP_Post|WP_Error |
| 72 |
*/ |
| 73 |
public function handleCartMigration() { |
| 74 |
// get the existing cart post. |
| 75 |
$existing_cart_post = \SureCart::cartPost()->get(); |
| 76 |
|
| 77 |
// we don't have a cart post - it's possibly been deleted or not seeded. |
| 78 |
if ( empty( $existing_cart_post ) || empty( $existing_cart_post->post_content ) ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
// get the defined template. |
| 83 |
$template = get_block_template( 'surecart/surecart//cart', 'wp_template_part' ); |
| 84 |
|
| 85 |
// it has already been modified. |
| 86 |
if ( ! empty( $template->wp_id ) ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
$cart = [ |
| 91 |
'post_name' => 'cart', |
| 92 |
'post_title' => $template->title, |
| 93 |
'post_status' => 'publish', |
| 94 |
'tax_input' => [ |
| 95 |
'wp_template_part_area' => WP_TEMPLATE_PART_AREA_UNCATEGORIZED, |
| 96 |
'wp_theme' => $template->theme, |
| 97 |
], |
| 98 |
'meta_input' => [ |
| 99 |
'origin' => 'plugin', |
| 100 |
], |
| 101 |
'post_type' => 'wp_template_part', |
| 102 |
'post_content' => \SureCart\Cart\CartService::removeDeprecatedCartContent( $existing_cart_post->post_content ), |
| 103 |
'post_excerpt' => $template->description, |
| 104 |
]; |
| 105 |
|
| 106 |
// Create the template. |
| 107 |
$result = wp_insert_post( wp_slash( $cart, false ) ); |
| 108 |
|
| 109 |
// insertion failed. |
| 110 |
if ( is_wp_error( $result ) ) { |
| 111 |
return $result; |
| 112 |
} |
| 113 |
|
| 114 |
// delete all cart posts. |
| 115 |
$allposts = get_posts( |
| 116 |
array( |
| 117 |
'post_type' => 'sc_cart', |
| 118 |
'numberposts' => -1, |
| 119 |
'fields' => 'ids', |
| 120 |
) |
| 121 |
); |
| 122 |
foreach ( $allposts as $eachpost ) { |
| 123 |
wp_delete_post( $eachpost, true ); |
| 124 |
} |
| 125 |
|
| 126 |
// return result. |
| 127 |
return $result; |
| 128 |
} |
| 129 |
} |
| 130 |
|