PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.9.2
Pods – Custom Content Types and Fields v3.3.9.2
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / sql / upgrade / PodsUpgrade.php
pods / sql / upgrade Last commit date
PodsUpgrade.php 14 hours ago PodsUpgrade_2_0_0.php 14 hours ago PodsUpgrade_2_1_0.php 14 hours ago
PodsUpgrade.php
335 lines
1 <?php
2
3 // Don't load directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 die( '-1' );
6 }
7
8 /**
9 * @package Pods\Upgrade
10 */
11 class PodsUpgrade {
12
13 /**
14 * @var array
15 */
16 public $tables = [];
17
18 /**
19 * @var array
20 */
21 protected $progress = [];
22
23 /**
24 * @var PodsAPI
25 */
26 protected $api = null;
27
28 /**
29 * @var string
30 */
31 protected $version = null;
32
33 /**
34 *
35 */
36 public function __construct() {
37 $this->api = pods_api();
38
39 $this->get_tables();
40 $this->get_progress();
41 }
42
43 /**
44 * @param null $_blog_id Blog ID to install.
45 */
46 public function install( $_blog_id = null ) {
47 /**
48 * @var $wpdb WPDB
49 */
50 global $wpdb;
51
52 // Switch DB table prefixes
53 if ( null !== $_blog_id && $_blog_id !== $wpdb->blogid ) {
54 switch_to_blog( pods_absint( $_blog_id ) );
55 } else {
56 $_blog_id = null;
57 }
58
59 $pods_version = get_option( 'pods_version' );
60
61 /**
62 * Allow hooking in before Pods install is run.
63 *
64 * @param string $current_version The current version of Pods installed.
65 * @param string $new_version The new version of Pods being installed.
66 * @param int|null $blog_id The blog ID being installed on, or null for
67 */
68 do_action( 'pods_install', PODS_VERSION, $pods_version, $_blog_id );
69
70 /**
71 * Allow filtering of whether the Pods SQL installation should be run. Return false to bypass.
72 *
73 * @param bool $run Whether the Pods SQL installation should be run.
74 * @param string $current_version The current version of Pods installed.
75 * @param string $new_version The new version of Pods being installed.
76 * @param int|null $blog_id The blog ID being installed on, or null for
77 */
78 $run = apply_filters( 'pods_install_run', true, PODS_VERSION, $pods_version, $_blog_id );
79
80 if ( false !== $run && ! pods_tableless() && 0 === (int) pods_v( 'pods_bypass_install' ) ) {
81 $sql = file_get_contents( PODS_DIR . 'sql/dump.sql' );
82 $sql = apply_filters( 'pods_install_sql', $sql, PODS_VERSION, $pods_version, $_blog_id );
83
84 $charset_collate = 'DEFAULT CHARSET utf8';
85
86 if ( ! empty( $wpdb->charset ) ) {
87 $charset_collate = "DEFAULT CHARSET {$wpdb->charset}";
88 }
89
90 if ( ! empty( $wpdb->collate ) ) {
91 $charset_collate .= " COLLATE {$wpdb->collate}";
92 }
93
94 if ( 'DEFAULT CHARSET utf8' !== $charset_collate ) {
95 $sql = str_replace( 'DEFAULT CHARSET utf8', $charset_collate, $sql );
96 }
97
98 $sql = explode( ";\n", str_replace( [ "\r", 'wp_' ], [ "\n", $wpdb->prefix ], $sql ) );
99 $sql = array_map( 'trim', $sql );
100 $sql = array_filter( $sql );
101
102 foreach ( $sql as $query ) {
103 pods_query( $query, 'Cannot setup SQL tables' );
104 }
105
106 // Auto activate component.
107 if ( ! PodsInit::$components ) {
108 if ( ! pods_light() ) {
109 PodsInit::$components = pods_components();
110 }
111 }
112
113 if ( PodsInit::$components ) {
114 PodsInit::$components->activate_component( 'templates' );
115 PodsInit::$components->activate_component( 'migrate-packages' );
116 }
117 }//end if
118
119 /**
120 * Allow hooking in after Pods install is run.
121 *
122 * @param string $current_version The current version of Pods installed.
123 * @param string $new_version The new version of Pods being installed.
124 * @param int|null $blog_id The blog ID being installed on, or null for
125 */
126 do_action( 'pods_install_post', PODS_VERSION, $pods_version, $_blog_id );
127 }
128
129 /**
130 * Handle dbDelta for Pods tables.
131 *
132 * @since 2.8.9
133 */
134 public function delta_tables() {
135 global $wpdb;
136
137 if ( pods_tableless() ) {
138 return;
139 }
140
141 $pods_version = get_option( 'pods_version' );
142
143 $sql = file_get_contents( PODS_DIR . 'sql/dump.sql' );
144 $sql = apply_filters( 'pods_install_sql', $sql, PODS_VERSION, $pods_version, get_current_blog_id() );
145
146 $charset_collate = 'DEFAULT CHARSET utf8';
147
148 if ( ! empty( $wpdb->charset ) ) {
149 $charset_collate = "DEFAULT CHARSET {$wpdb->charset}";
150 }
151
152 if ( ! empty( $wpdb->collate ) ) {
153 $charset_collate .= " COLLATE {$wpdb->collate}";
154 }
155
156 if ( 'DEFAULT CHARSET utf8' !== $charset_collate ) {
157 $sql = str_replace( 'DEFAULT CHARSET utf8', $charset_collate, $sql );
158 }
159
160 $sql = explode( ";\n", str_replace( [ "\r", 'wp_' ], [ "\n", $wpdb->prefix ], $sql ) );
161
162 // Remove empty lines and queries.
163 $sql = array_map( 'trim', $sql );
164 $sql = array_filter( $sql );
165
166 // dbDelta will handle what we need.
167 $sql = str_replace( 'CREATE TABLE IF NOT EXISTS', 'CREATE TABLE', $sql );
168
169 if ( empty( $sql ) ) {
170 return;
171 }
172
173 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
174
175 dbDelta( $sql );
176 }
177
178 /**
179 *
180 */
181 public function get_tables() {
182 /**
183 * @var $wpdb WPDB
184 */
185 global $wpdb;
186
187 $tables = $wpdb->get_results( "SHOW TABLES LIKE '{$wpdb->prefix}pod%'", ARRAY_N );
188
189 if ( ! empty( $tables ) ) {
190 foreach ( $tables as $table ) {
191 $this->tables[] = $table[0];
192 }
193 }
194 }
195
196 /**
197 *
198 */
199 public function get_progress() {
200 $methods = get_class_methods( $this );
201
202 foreach ( $methods as $method ) {
203 if ( 0 === strpos( $method, 'migrate_' ) ) {
204 $this->progress[ str_replace( 'migrate_', '', $method ) ] = false;
205 }
206 }
207
208 if ( empty( $this->version ) ) {
209 return;
210 }
211
212 $progress = (array) get_option( 'pods_framework_upgrade_' . str_replace( '.', '_', $this->version ), [] );
213
214 if ( ! empty( $progress ) ) {
215 $this->progress = array_merge( $this->progress, $progress );
216 }
217 }
218
219 /**
220 * @param $params
221 *
222 * @return mixed|void
223 */
224 public function ajax( $params ) {
225 if ( ! isset( $params->step ) ) {
226 return pods_error( __( 'Invalid upgrade process.', 'pods' ) );
227 }
228
229 if ( ! isset( $params->type ) ) {
230 return pods_error( __( 'Invalid upgrade method.', 'pods' ) );
231 }
232
233 if ( ! method_exists( $this, $params->step . '_' . $params->type ) ) {
234 return pods_error( __( 'Upgrade method not found.', 'pods' ) );
235 }
236
237 return call_user_func( [ $this, $params->step . '_' . $params->type ], $params );
238 }
239
240 /**
241 * @param $method
242 * @param $v
243 * @param null $x
244 */
245 public function update_progress( $method, $v, $x = null ) {
246 if ( empty( $this->version ) ) {
247 return;
248 }
249
250 $method = str_replace( 'migrate_', '', $method );
251
252 if ( null !== $x ) {
253 $this->progress[ $method ][ $x ] = (boolean) $v;
254 } else {
255 $this->progress[ $method ] = $v;
256 }
257
258 update_option( 'pods_framework_upgrade_' . str_replace( '.', '_', $this->version ), $this->progress );
259 }
260
261 /**
262 * @param $method
263 * @param null $x
264 *
265 * @return bool
266 */
267 public function check_progress( $method, $x = null ) {
268 $method = str_replace( 'migrate_', '', $method );
269
270 if ( isset( $this->progress[ $method ] ) ) {
271 if ( null === $x ) {
272 return $this->progress[ $method ];
273 } elseif ( isset( $this->progress[ $method ][ $x ] ) ) {
274 return (boolean) $this->progress[ $method ][ $x ];
275 }
276 }
277
278 return false;
279 }
280
281 /**
282 *
283 */
284 public function upgraded() {
285 if ( empty( $this->version ) ) {
286 return;
287 }
288
289 $upgraded = get_option( 'pods_framework_upgraded' );
290
291 if ( empty( $upgraded ) || ! is_array( $upgraded ) ) {
292 $upgraded = [];
293 }
294
295 delete_option( 'pods_framework_upgrade_' . str_replace( '.', '_', $this->version ) );
296
297 if ( ! in_array( $this->version, $upgraded, true ) ) {
298 $upgraded[] = $this->version;
299 }
300
301 update_option( 'pods_framework_upgraded', $upgraded );
302 }
303
304 /**
305 *
306 */
307 public function cleanup() {
308 /**
309 * @var $wpdb WPDB
310 */
311 global $wpdb;
312
313 foreach ( $this->tables as $table ) {
314 if ( false !== strpos( $table, "{$wpdb->prefix}pod_" ) || "{$wpdb->prefix}pod" === $table ) {
315 pods_query( "DROP TABLE `{$table}`", false );
316 }
317 }
318
319 delete_option( 'pods_roles' );
320 delete_option( 'pods_version' );
321 delete_option( 'pods_framework_upgrade_2_0' );
322 delete_option( 'pods_framework_upgrade_2_0_sister_ids' );
323 delete_option( 'pods_framework_upgraded_1_x' );
324
325 delete_option( 'pods_disable_file_browser' );
326 delete_option( 'pods_files_require_login' );
327 delete_option( 'pods_files_require_login_cap' );
328 delete_option( 'pods_disable_file_upload' );
329 delete_option( 'pods_upload_require_login' );
330 delete_option( 'pods_upload_require_login_cap' );
331
332 pods_query( "DELETE FROM `@wp_postmeta` WHERE `meta_key` LIKE '_pods_1x_%'" );
333 }
334 }
335