PluginProbe
Additional Terms Lite for WooCommerce / 1.6.1
Additional Terms Lite for WooCommerce v1.6.1
1.7.3 1.7.2.1 trunk 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.6.3 All 35 releases
woo-additional-terms / src / Migration / Migration.php

Migration.php in Additional Terms Lite for WooCommerce 1.6.1, at src/Migration/Migration.php

154 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Abstract Migration class.
4 *
5 * @author MyPreview (Github: @mahdiyazdani, @gooklani, @mypreview)
6 *
7 * @since 1.6.0
8 *
9 * @package woo-additional-terms
10 */
11
12 namespace Woo_Additional_Terms\Migration;
13
14 /**
15 * Migration class.
16 */
17 abstract class Migration {
18
19 /**
20 * The version.
21 *
22 * @since 1.6.0
23 *
24 * @var string
25 */
26 private $version;
27
28 /**
29 * The option name.
30 *
31 * @since 1.6.0
32 *
33 * @var string
34 */
35 private $option_name;
36
37 /**
38 * Constructor.
39 *
40 * @since 1.6.0
41 *
42 * @param string $version The version to do the migration.
43 *
44 * @return void
45 */
46 public function __construct( $version ) {
47
48 // Set the migration properties.
49 $this->set_version( $version );
50 $this->set_option_name();
51
52 // Initialize the migration.
53 $this->setup();
54 }
55
56 /**
57 * Setup hooks and filters.
58 *
59 * @since 1.6.0
60 *
61 * @return void
62 */
63 public function setup() {
64
65 add_action( 'woocommerce_init', array( $this, 'migrate_callback' ) );
66 }
67
68 /**
69 * Migrate callback.
70 *
71 * @since 1.6.0
72 *
73 * @return void
74 */
75 abstract public function migrate_callback();
76
77 /**
78 * Check if the migration is eligible.
79 *
80 * @since 1.6.0
81 *
82 * @return bool
83 */
84 protected function is_eligible() {
85
86 // Bail early if the migration is done.
87 if ( $this->is_completed() ) {
88 return false;
89 }
90
91 // Bail early if the version is not set.
92 if ( empty( $this->version ) ) {
93 return false;
94 }
95
96 // Bail early if the version requirement is not met.
97 if ( version_compare( $this->version, woo_additional_terms()->get_version(), '<>' ) ) {
98 return false;
99 }
100
101 return true;
102 }
103
104 /**
105 * Mark the migration as done.
106 *
107 * @since 1.6.0
108 *
109 * @return void
110 */
111 protected function complete() {
112
113 update_option( $this->option_name, true );
114 }
115
116 /**
117 * Check if the migration is completed.
118 *
119 * @since 1.6.0
120 *
121 * @return bool
122 */
123 private function is_completed() {
124
125 return (bool) get_option( $this->option_name, false );
126 }
127
128 /**
129 * Set the version.
130 *
131 * @since 1.6.0
132 *
133 * @param string $version The version.
134 *
135 * @return void
136 */
137 private function set_version( $version ) {
138
139 $this->version = $version;
140 }
141
142 /**
143 * Set the option name.
144 *
145 * @since 1.6.0
146 *
147 * @return void
148 */
149 private function set_option_name() {
150
151 $this->option_name = "_woo_additional_terms_migration_{$this->version}";
152 }
153 }
154