PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / trunk
Pods – Custom Content Types and Fields vtrunk
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 / update-2.0-beta.php
pods / sql Last commit date
upgrade 4 months ago dump.sql 4 years ago update-1.x.php 4 months ago update-2.0-beta.php 4 months ago update.php 4 months ago
update-2.0-beta.php
493 lines
1 <?php
2
3 // Don't load directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 die( '-1' );
6 }
7
8 // phpcs:ignoreFile WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
9
10 /**
11 * @package Pods\Upgrade
12 */
13
14 /**
15 * @var $wpdb WPDB
16 */
17 global $wpdb;
18
19 // Update to 2.0.0-a-31
20 if ( version_compare( $pods_version, '2.0.0-a-31', '<' ) ) {
21 $pages = pods_2_alpha_migrate_pages();
22 $helpers = pods_2_alpha_migrate_helpers();
23 $templates = pods_2_alpha_migrate_templates();
24 $pod_ids = pods_2_alpha_migrate_pods();
25
26 pods_query( 'DROP TABLE @wp_pods', false );
27 pods_query( 'DROP TABLE @wp_pods_fields', false );
28 pods_query( 'DROP TABLE @wp_pods_objects', false );
29
30 update_option( 'pods_framework_version', '2.0.0-a-31' );
31 }
32
33 // Update to 2.0.0-b-10
34 if ( version_compare( $pods_version, '2.0.0-b-10', '<' ) ) {
35 $author_fields = $wpdb->get_results( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_name` = 'author' AND `post_type` = '_pods_field'" );
36
37 if ( ! empty( $author_fields ) ) {
38 foreach ( $author_fields as $author ) {
39 update_post_meta( $author->ID, 'pick_format_type', 'single' );
40 update_post_meta( $author->ID, 'pick_format_single', 'autocomplete' );
41 update_post_meta( $author->ID, 'default_value', '{@user.ID}' );
42 update_post_meta( $author->ID, 'default_evaluate_tags', '1' );
43 }
44 }
45
46 update_option( 'pods_framework_version', '2.0.0-b-10' );
47 }
48
49 // Update to 2.0.0-b-11
50 if ( version_compare( $pods_version, '2.0.0-b-11', '<' ) ) {
51 $date_fields = $wpdb->get_results( "
52 SELECT `ID`
53 FROM `{$wpdb->posts}`
54 WHERE ( `post_name` = 'created' OR `post_name` = 'modified' ) AND `post_type` = '_pods_field'
55 " );
56
57 if ( ! empty( $date_fields ) ) {
58 foreach ( $date_fields as $date ) {
59 update_post_meta( $date->ID, 'date_format_type', 'datetime' );
60 update_post_meta( $date->ID, 'date_format', 'ymd_slash' );
61 update_post_meta( $date->ID, 'date_time_type', '12' );
62 update_post_meta( $date->ID, 'date_time_format', 'h_mm_ss_A' );
63 }
64 }
65
66 update_option( 'pods_framework_version', '2.0.0-b-11' );
67 }
68
69 // Update to 2.0.0-b-12
70 if ( version_compare( $pods_version, '2.0.0-b-12', '<' ) ) {
71 $oldget = $_GET;
72
73 $_GET['toggle'] = 1;
74
75 PodsInit::$components->toggle( 'templates' );
76 PodsInit::$components->toggle( 'pages' );
77 PodsInit::$components->toggle( 'helpers' );
78
79 $_GET = $oldget;
80
81 $number_fields = $wpdb->get_results( "
82 SELECT `p`.`ID`
83 FROM `{$wpdb->posts}` AS `p`
84 LEFT JOIN `{$wpdb->postmeta}` AS `pm` ON `pm`.`post_id` = `p`.`ID`
85 WHERE
86 `p`.`post_type` = '_pods_field'
87 AND `pm`.`meta_key` = 'type'
88 AND `pm`.`meta_value` = 'number'
89 " );
90
91 if ( ! empty( $number_fields ) ) {
92 foreach ( $number_fields as $number ) {
93 update_post_meta( $number->ID, 'number_max_length', '12' );
94 }
95 }
96
97 update_option( 'pods_framework_version', '2.0.0-b-12' );
98 }//end if
99
100 // Update to 2.0.0-b-14
101 if ( version_compare( $pods_version, '2.0.0-b-14', '<' ) ) {
102 $tables = $wpdb->get_results( "SHOW TABLES LIKE '{$wpdb->prefix}pods%'", ARRAY_N );
103
104 $podsrel_found = false;
105
106 if ( ! empty( $tables ) ) {
107 foreach ( $tables as &$table ) {
108 $table = $table[0];
109 $new_table = $table;
110
111 if ( "{$wpdb->prefix}pods_rel" === $table ) {
112 $new_table = "{$wpdb->prefix}podsrel";
113
114 $podsrel_found = true;
115 } elseif ( "{$wpdb->prefix}podsrel" === $table ) {
116 $podsrel_found = true;
117 } else {
118 $new_table = str_replace( 'pods_tbl_', 'pods_', $table );
119 }
120
121 if ( $table !== $new_table ) {
122 $wpdb->query( "ALTER TABLE `{$table}` RENAME `{$new_table}`" );
123 }
124 }
125 }
126
127 if ( ! $podsrel_found ) {
128 // rerun install for any bugged versions
129 $sql = file_get_contents( PODS_DIR . 'sql/dump.sql' );
130 $sql = apply_filters( 'pods_install_sql', $sql, PODS_VERSION, $pods_version, $_blog_id );
131
132 $charset_collate = 'DEFAULT CHARSET utf8';
133
134 if ( ! empty( $wpdb->charset ) ) {
135 $charset_collate = "DEFAULT CHARSET {$wpdb->charset}";
136 }
137
138 if ( ! empty( $wpdb->collate ) ) {
139 $charset_collate .= " COLLATE {$wpdb->collate}";
140 }
141
142 if ( 'DEFAULT CHARSET utf8' !== $charset_collate ) {
143 $sql = str_replace( 'DEFAULT CHARSET utf8', $charset_collate, $sql );
144 }
145
146 $sql = explode( ";\n", str_replace( array( "\r", 'wp_' ), array( "\n", $wpdb->prefix ), $sql ) );
147
148 $z = count( $sql );
149 for ( $i = 0; $i < $z; $i ++ ) {
150 $query = trim( $sql[ $i ] );
151
152 if ( empty( $query ) ) {
153 continue;
154 }
155
156 pods_query( $query, 'Cannot setup SQL tables' );
157 }
158 }//end if
159
160 pods_no_conflict_on( 'post' );
161
162 // convert field types based on options set
163 $fields = $wpdb->get_results( "
164 SELECT `p`.`ID`
165 FROM `{$wpdb->posts}` AS `p`
166 LEFT JOIN `{$wpdb->postmeta}` AS `pm` ON `pm`.`post_id` = `p`.`ID`
167 WHERE
168 `p`.`post_type` = '_pods_field'
169 AND `pm`.`meta_key` = 'type'
170 AND `pm`.`meta_value` = 'date'
171 " );
172
173 if ( ! empty( $fields ) ) {
174 foreach ( $fields as $field ) {
175 $new_type = get_post_meta( $field->ID, 'date_format_type', true );
176
177 if ( 'datetime' === $new_type ) {
178 $new = array(
179 'date_format' => 'datetime_format',
180 'date_time_type' => 'datetime_time_type',
181 'date_time_format' => 'datetime_time_format',
182 'date_html5' => 'datetime_html5',
183 );
184
185 update_post_meta( $field->ID, 'type', $new_type );
186 } elseif ( 'time' === $new_type ) {
187 $new = array(
188 'date_time_type' => 'time_type',
189 'date_time_format' => 'time_format',
190 'date_html5' => 'time_html5',
191 );
192
193 update_post_meta( $field->ID, 'type', $new_type );
194 }
195 }//end foreach
196 }//end if
197
198 $fields = $wpdb->get_results( "
199 SELECT `p`.`ID`
200 FROM `{$wpdb->posts}` AS `p`
201 LEFT JOIN `{$wpdb->postmeta}` AS `pm` ON `pm`.`post_id` = `p`.`ID`
202 WHERE
203 `p`.`post_type` = '_pods_field'
204 AND `pm`.`meta_key` = 'type'
205 AND `pm`.`meta_value` = 'number'
206 " );
207
208 if ( ! empty( $fields ) ) {
209 foreach ( $fields as $field ) {
210 $new_type = get_post_meta( $field->ID, 'number_format_type', true );
211
212 if ( 'currency' === $new_type ) {
213 $new = array(
214 'number_format_currency_sign' => 'currency_format_sign',
215 'number_format_currency_placement' => 'currency_format_placement',
216 'number_format' => 'currency_format',
217 'number_decimals' => 'currency_decimals',
218 'number_max_length' => 'currency_max_length',
219 'number_size' => 'currency_size',
220 );
221
222 update_post_meta( $field->ID, 'type', $new_type );
223 }
224 }
225 }
226
227 $fields = $wpdb->get_results( "
228 SELECT `p`.`ID`
229 FROM `{$wpdb->posts}` AS `p`
230 LEFT JOIN `{$wpdb->postmeta}` AS `pm` ON `pm`.`post_id` = `p`.`ID`
231 WHERE
232 `p`.`post_type` = '_pods_field'
233 AND `pm`.`meta_key` = 'type'
234 AND `pm`.`meta_value` = 'paragraph'
235 " );
236
237 if ( ! empty( $fields ) ) {
238 foreach ( $fields as $field ) {
239 $new_type = get_post_meta( $field->ID, 'paragraph_format_type', true );
240
241 if ( 'plain' !== $new_type ) {
242 $new_type = 'wysiwyg';
243
244 $new = array(
245 'paragraph_format_type' => 'wysiwyg_editor',
246 'paragraph_allow_shortcode' => 'wysiwyg_allow_shortcode',
247 'paragraph_allowed_html_tags' => 'wysiwyg_allowed_html_tags',
248 'paragraph_max_length' => 'wysiwyg_max_length',
249 'paragraph_size' => 'wysiwyg_size',
250 );
251
252 update_post_meta( $field->ID, 'type', $new_type );
253 }
254 }
255 }
256
257 $fields = $wpdb->get_results( "
258 SELECT `p`.`ID`
259 FROM `{$wpdb->posts}` AS `p`
260 LEFT JOIN `{$wpdb->postmeta}` AS `pm` ON `pm`.`post_id` = `p`.`ID`
261 WHERE
262 `p`.`post_type` = '_pods_field'
263 AND `pm`.`meta_key` = 'type'
264 AND `pm`.`meta_value` = 'text'
265 " );
266
267 if ( ! empty( $fields ) ) {
268 foreach ( $fields as $field ) {
269 $new_type = get_post_meta( $field->ID, 'text_format_type', true );
270
271 if ( 'website' === $new_type ) {
272 $new = array(
273 'text_format_website' => 'website_format',
274 'text_max_length' => 'website_max_length',
275 'text_html5' => 'website_html5',
276 'text_size' => 'website_size',
277 );
278
279 update_post_meta( $field->ID, 'type', $new_type );
280 } elseif ( 'phone' === $new_type ) {
281 $new = array(
282 'text_format_phone' => 'phone_format',
283 'text_max_length' => 'phone_max_length',
284 'text_html5' => 'phone_html5',
285 'text_size' => 'phone_size',
286 );
287
288 update_post_meta( $field->ID, 'type', $new_type );
289 } elseif ( 'email' === $new_type ) {
290 $new = array(
291 'text_max_length' => 'email_max_length',
292 'text_html5' => 'email_html5',
293 'text_size' => 'email_size',
294 );
295
296 update_post_meta( $field->ID, 'type', $new_type );
297 } elseif ( 'password' === $new_type ) {
298 $new = array(
299 'text_max_length' => 'password_max_length',
300 'text_size' => 'password_size',
301 );
302
303 update_post_meta( $field->ID, 'type', $new_type );
304 }//end if
305 }//end foreach
306 }//end if
307
308 pods_no_conflict_off( 'post' );
309
310 update_option( 'pods_framework_version', '2.0.0-b-14' );
311 }//end if
312
313 // Update to 2.0.0-b-15
314 if ( version_compare( $pods_version, '2.0.0-b-15', '<' ) ) {
315 $helpers = $wpdb->get_results( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_type` = '_pods_helper'" );
316
317 if ( ! empty( $helpers ) ) {
318 foreach ( $helpers as $helper ) {
319 $wpdb->query(
320 $wpdb->prepare(
321 "UPDATE `{$wpdb->postmeta}` SET `meta_key` = 'helper_type' WHERE `meta_key` = 'type' AND `post_id` = %d",
322 $helper->ID
323 )
324 );
325 }
326 }
327
328 update_option( 'pods_framework_version', '2.0.0-b-15' );
329 }
330
331 /*
332 ===================================
333
334 Old upgrade code from Alpha to Beta
335
336 ===================================
337 */
338 /**
339 * @param $id
340 * @param $options
341 */
342 function pods_2_beta_migrate_type( $id, $options ) {
343 global $wpdb;
344
345 foreach ( $options as $old => $new ) {
346 $wpdb->query( $wpdb->prepare( "UPDATE `{$wpdb->postmeta}` SET `meta_key` = %s WHERE `meta_key` = %s", array(
347 $new,
348 $old,
349 ) ) );
350 }
351 }
352
353 /**
354 * @return array
355 */
356 function pods_2_alpha_migrate_pods() {
357 $api = pods_api();
358
359 $api->display_errors = true;
360
361 $old_pods = pods_query( 'SELECT * FROM `@wp_pods`', false );
362
363 $pod_ids = array();
364
365 if ( empty( $old_pods ) ) {
366 return $pod_ids;
367 }
368
369 foreach ( $old_pods as $pod ) {
370 $api->cache_flush_pods( array( 'name' => $pod->name ) );
371
372 $pod_opts = json_decode( $pod->options, true );
373
374 $field_rows = pods_query( "SELECT * FROM `@wp_pods_fields` where `pod_id` = {$pod->id}" );
375
376 $fields = array();
377
378 foreach ( $field_rows as $row ) {
379 $field_opts = json_decode( $row->options, true );
380
381 if ( 'permalink' === $row->type ) {
382 $row->type = 'slug';
383 }
384
385 $field_params = array(
386 'name' => $row->name,
387 'label' => $row->label,
388 'type' => $row->type,
389 'pick_object' => $row->pick_object,
390 'pick_val' => $row->pick_val,
391 'sister_field_id' => $row->sister_field_id,
392 'weight' => $row->weight,
393 'options' => $field_opts,
394 );
395
396 $fields[] = $field_params;
397 }
398
399 $pod_params = array(
400 'name' => $pod->name,
401 'type' => $pod->type,
402 'storage' => $pod->storage,
403 'fields' => $fields,
404 'options' => $pod_opts,
405 );
406
407 $renamed = false;
408
409 if ( 'table' === $pod->storage ) {
410 try {
411 pods_query( "RENAME TABLE `@wp_pods_tbl_{$pod->name}` TO `@wp_pods_tb_{$pod->name}`" );
412 $renamed = true;
413 } catch ( Exception $e ) {
414 $renamed = false;
415 }
416 }
417
418 $pod_id = $api->save_pod( $pod_params );
419
420 if ( 'table' === $pod->storage && $renamed ) {
421 pods_query( "DROP TABLE `@wp_pods_tbl_{$pod->name}`", false );
422 pods_query( "RENAME TABLE `@wp_pods_tb_{$pod->name}` TO `@wp_pods_tbl_{$pod->name}`" );
423 }
424
425 $pod_ids[] = $pod_id;
426 }//end foreach
427
428 return $pod_ids;
429 }
430
431 /**
432 * @return array
433 */
434 function pods_2_alpha_migrate_helpers() {
435 return [];
436 }
437
438 /**
439 * @return array
440 */
441 function pods_2_alpha_migrate_pages() {
442 $api = pods_api();
443
444 $page_rows = pods_query( "SELECT * FROM `@wp_pods_objects` WHERE `type` = 'page'", false );
445
446 $page_ids = array();
447
448 if ( empty( $page_rows ) ) {
449 return $page_ids;
450 }
451
452 foreach ( $page_rows as $row ) {
453 $opts = json_decode( $row->options );
454
455 $page_params = array(
456 'uri' => $row->name,
457 'phpcode' => $opts->phpcode,
458 );
459
460 $page_ids[] = $api->save_page( $page_params );
461 }
462
463 return $page_ids;
464 }
465
466 /**
467 * @return array
468 */
469 function pods_2_alpha_migrate_templates() {
470 $api = pods_api();
471
472 $tpl_rows = pods_query( "SELECT * FROM `@wp_pods_objects` WHERE `type` = 'template'", false );
473
474 $tpl_ids = array();
475
476 if ( empty( $tpl_rows ) ) {
477 return $tpl_ids;
478 }
479
480 foreach ( $tpl_rows as $row ) {
481 $opts = json_decode( $row->options );
482
483 $tpl_params = array(
484 'name' => $row->name,
485 'code' => $opts->code,
486 );
487
488 $tpl_ids[] = $api->save_template( $tpl_params );
489 }
490
491 return $tpl_ids;
492 }
493