PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.4.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.4.1
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Database / UpdateMigrationServiceProvider.php

UpdateMigrationServiceProvider.php in SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments 4.4.1, at app/src/Database/UpdateMigrationServiceProvider.php

148 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // handle order bump design migration.
46 $this->handleOrderBumpDesignMigration();
47 }
48
49 /**
50 * Set classic order bump design for existing installs.
51 *
52 * Fresh installs have migration_version = '0.0.0' at this point because
53 * updateMigrationVersion() runs at priority 9999999, after this.
54 * add_option() is a no-op if the option already exists (idempotent).
55 *
56 * @return void
57 */
58 public function handleOrderBumpDesignMigration() {
59 $previous_version = get_option( 'surecart_migration_version', '0.0.0' );
60 if ( '0.0.0' !== $previous_version ) {
61 add_option( 'surecart_order_bump_design', 'classic' );
62 }
63 }
64
65 /**
66 * Update the migration version.
67 *
68 * @return void
69 */
70 public function updateMigrationVersion() {
71 if ( ! $this->versionChanged() ) {
72 return;
73 }
74 update_option( 'surecart_migration_version', \SureCart::plugin()->version() );
75 }
76
77 /**
78 * Version has changed?
79 *
80 * @return boolean
81 */
82 public function versionChanged() {
83 return version_compare( \SureCart::plugin()->version(), get_option( 'surecart_migration_version', '0.0.0' ), '!=' );
84 }
85
86 /**
87 * Handle cart migration
88 *
89 * @return \WP_Post|WP_Error
90 */
91 public function handleCartMigration() {
92 // get the existing cart post.
93 $existing_cart_post = \SureCart::cartPost()->get();
94
95 // we don't have a cart post - it's possibly been deleted or not seeded.
96 if ( empty( $existing_cart_post ) || empty( $existing_cart_post->post_content ) ) {
97 return;
98 }
99
100 // get the defined template.
101 $template = get_block_template( 'surecart/surecart//cart', 'wp_template_part' );
102
103 // it has already been modified.
104 if ( ! empty( $template->wp_id ) ) {
105 return;
106 }
107
108 $cart = [
109 'post_name' => 'cart',
110 'post_title' => $template->title,
111 'post_status' => 'publish',
112 'tax_input' => [
113 'wp_template_part_area' => WP_TEMPLATE_PART_AREA_UNCATEGORIZED,
114 'wp_theme' => $template->theme,
115 ],
116 'meta_input' => [
117 'origin' => 'plugin',
118 ],
119 'post_type' => 'wp_template_part',
120 'post_content' => \SureCart\Cart\CartService::removeDeprecatedCartContent( $existing_cart_post->post_content ),
121 'post_excerpt' => $template->description,
122 ];
123
124 // Create the template.
125 $result = wp_insert_post( wp_slash( $cart, false ) );
126
127 // insertion failed.
128 if ( is_wp_error( $result ) ) {
129 return $result;
130 }
131
132 // delete all cart posts.
133 $allposts = get_posts(
134 array(
135 'post_type' => 'sc_cart',
136 'numberposts' => -1,
137 'fields' => 'ids',
138 )
139 );
140 foreach ( $allposts as $eachpost ) {
141 wp_delete_post( $eachpost, true );
142 }
143
144 // return result.
145 return $result;
146 }
147 }
148