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