PluginProbe
Additional Terms Lite for WooCommerce / trunk
Additional Terms Lite for WooCommerce vtrunk
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 trunk, at src/Migration/Migration.php

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