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-1.x.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-1.x.php
436 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 global $wpdb;
14
15 if ( version_compare( $old_version, '1.2.6', '<' ) ) {
16 // Add the "required" option
17 pods_query( 'ALTER TABLE @wp_pod_fields ADD COLUMN required BOOL default 0 AFTER sister_field_id' );
18
19 // Add the "label" option
20 pods_query( 'ALTER TABLE @wp_pod_fields ADD COLUMN label VARCHAR(32) AFTER name' );
21
22 // Fix table prefixes
23 if ( ! empty( $table_prefix ) ) {
24 $result = $wpdb->get_results( "SHOW TABLES LIKE 'tbl_%'", ARRAY_N );
25
26 foreach ( $result as $row ) {
27 pods_query( "RENAME TABLE `{$row[0]}` TO `@wp_{$row[0]}`" );
28 }
29 }
30
31 // Change the "post_type" of all pod items
32 $result = pods_query( 'SELECT id, name FROM @wp_pod_types' );
33
34 foreach ( $result as $row ) {
35 $datatypes[ $row->id ] = $row->name;
36 }
37
38 $result = pods_query( 'SELECT post_id, datatype FROM @wp_pod' );
39
40 foreach ( $result as $row ) {
41 $datatype = $datatypes[ $row->datatype ];
42
43 pods_query( "UPDATE @wp_posts SET post_type = '$datatype' WHERE ID = $row[0] LIMIT 1" );
44 }
45
46 update_option( 'pods_version', '126' );
47 }//end if
48
49 if ( version_compare( $old_version, '1.2.7', '<' ) ) {
50 // Add the "comment" option
51 pods_query( 'ALTER TABLE @wp_pod_fields ADD COLUMN comment VARCHAR(128) AFTER label' );
52
53 update_option( 'pods_version', '127' );
54 }
55
56 if ( version_compare( $old_version, '1.3.1', '<' ) ) {
57 $result = $wpdb->get_results( "SHOW TABLES LIKE '{$wpdb->prefix}tbl_%'", ARRAY_N );
58
59 foreach ( $result as $row ) {
60 $rename = explode( 'tbl_', $row[0] );
61 pods_query( "RENAME TABLE `{$row[0]}` TO `@wp_pod_tbl_{$rename[1]}`" );
62 }
63
64 update_option( 'pods_version', '131' );
65 }
66
67 if ( version_compare( $old_version, '1.3.2', '<' ) ) {
68 pods_query( "UPDATE @wp_pod_pages SET phpcode = CONCAT('<" . '?' . "php\n', phpcode) WHERE phpcode NOT LIKE '" . '?' . ">%'" );
69 pods_query( "UPDATE @wp_pod_pages SET phpcode = SUBSTR(phpcode, 3) WHERE phpcode LIKE '" . '?' . ">%'" );
70 pods_query( "UPDATE @wp_pod_widgets SET phpcode = CONCAT('<" . '?' . "php\n', phpcode) WHERE phpcode NOT LIKE '" . '?' . ">%'" );
71 pods_query( "UPDATE @wp_pod_widgets SET phpcode = SUBSTR(phpcode, 3) WHERE phpcode LIKE '" . '?' . ">%'" );
72
73 update_option( 'pods_version', '132' );
74 }
75
76 if ( version_compare( $old_version, '1.4.3', '<' ) ) {
77 $result = pods_query( "SHOW COLUMNS FROM @wp_pod_types LIKE 'description'" );
78
79 if ( 0 < count( $result ) ) {
80 pods_query( 'ALTER TABLE @wp_pod_types CHANGE description label VARCHAR(32)' );
81 }
82
83 pods_query( 'ALTER TABLE @wp_pod_types ADD COLUMN is_toplevel BOOL default 0 AFTER label' );
84
85 update_option( 'pods_version', '143' );
86 }
87
88 if ( version_compare( $old_version, '1.4.5', '<' ) ) {
89 pods_query( 'ALTER TABLE @wp_pod_pages ADD COLUMN title VARCHAR(128) AFTER uri' );
90
91 $sql = '
92 CREATE TABLE @wp_pod_menu (
93 id INT unsigned auto_increment primary key,
94 uri VARCHAR(128),
95 title VARCHAR(128),
96 lft INT unsigned,
97 rgt INT unsigned,
98 weight TINYINT unsigned default 0)';
99 pods_query( $sql );
100
101 pods_query( "INSERT INTO @wp_pod_menu (uri, title, lft, rgt) VALUES ('/', 'Home', 1, 2)" );
102
103 update_option( 'pods_version', '145' );
104 }
105
106 if ( version_compare( $old_version, '1.4.8', '<' ) ) {
107 add_option( 'pods_roles' );
108
109 update_option( 'pods_version', '148' );
110 }
111
112 if ( version_compare( $old_version, '1.4.9', '<' ) ) {
113 pods_query( 'RENAME TABLE @wp_pod_widgets TO @wp_pod_helpers' );
114
115 update_option( 'pods_version', '149' );
116 }
117
118 if ( version_compare( $old_version, '1.5', '<' ) ) {
119 pods_query( 'ALTER TABLE @wp_pod_fields ADD COLUMN `unique` BOOL default 0 AFTER required' );
120 pods_query( 'ALTER TABLE @wp_pod_fields ADD COLUMN `multiple` BOOL default 0 AFTER `unique`' );
121 pods_query( 'ALTER TABLE @wp_pod_pages ADD COLUMN page_template VARCHAR(128) AFTER phpcode' );
122 pods_query( 'ALTER TABLE @wp_pod_helpers ADD COLUMN helper_type VARCHAR(16) AFTER name' );
123 pods_query( 'ALTER TABLE @wp_pod ADD COLUMN name VARCHAR(128) AFTER datatype' );
124 pods_query( 'ALTER TABLE @wp_pod ADD COLUMN created VARCHAR(128) AFTER name' );
125 pods_query( 'ALTER TABLE @wp_pod ADD COLUMN modified VARCHAR(128) AFTER created' );
126 pods_query( 'ALTER TABLE @wp_pod CHANGE row_id tbl_row_id INT unsigned' );
127 pods_query( 'ALTER TABLE @wp_pod_rel CHANGE term_id tbl_row_id INT unsigned' );
128 pods_query( 'ALTER TABLE @wp_pod_rel CHANGE post_id pod_id INT unsigned' );
129 pods_query( 'ALTER TABLE @wp_pod_rel CHANGE sister_post_id sister_pod_id INT unsigned' );
130
131 // Make all pick columns "multiple" for consistency
132 pods_query( "UPDATE @wp_pod_fields SET `multiple` = 1 WHERE coltype = 'pick'" );
133
134 // Use "display" as the default helper type
135 pods_query( "UPDATE @wp_pod_helpers SET helper_type = 'display'" );
136
137 // Replace all post_ids with its associated pod_id
138 $sql = '
139 SELECT
140 p.id, p.post_id, r.post_title AS name, r.post_date AS created, r.post_modified AS modified
141 FROM
142 @wp_pod p
143 INNER JOIN
144 @wp_posts r ON r.ID = p.post_id
145 ';
146
147 $result = pods_query( $sql );
148
149 foreach ( $result as $row ) {
150 $row = get_object_vars( $row );
151
152 foreach ( $row as $key => $val ) {
153 ${$key} = pods_sanitize( trim( $val ) );
154 }
155
156 $posts_to_delete[] = $post_id;
157 $all_pod_ids[ $post_id ] = $id;
158
159 pods_query( "UPDATE @wp_pod SET name = '$name', created = '$created', modified = '$modified' WHERE id = $id LIMIT 1" );
160 }
161
162 // Replace post_id with pod_id
163 $result = pods_query( 'SELECT id, pod_id, sister_pod_id FROM @wp_pod_rel' );
164
165 foreach ( $result as $row ) {
166 $id = $row->id;
167 $new_pod_id = $all_pod_ids[ $row->pod_id ];
168 $new_sister_pod_id = $all_pod_ids[ $row->sister_pod_id ];
169
170 pods_query( "UPDATE @wp_pod_rel SET pod_id = '$new_pod_id', sister_pod_id = '$new_sister_pod_id' WHERE id = '$id' LIMIT 1" );
171 }
172
173 $posts_to_delete = implode( ',', $posts_to_delete );
174
175 // Remove all traces from wp_posts
176 pods_query( 'ALTER TABLE @wp_pod DROP COLUMN post_id' );
177 pods_query( "DELETE FROM @wp_posts WHERE ID IN ($posts_to_delete)" );
178
179 update_option( 'pods_version', '150' );
180 }//end if
181
182 if ( version_compare( $old_version, '1.5.1', '<' ) ) {
183 pods_query( 'ALTER TABLE @wp_pod_fields ADD COLUMN helper VARCHAR(32) AFTER label' );
184 pods_query( 'ALTER TABLE @wp_pod_types ADD COLUMN before_helpers TEXT AFTER tpl_list' );
185 pods_query( 'ALTER TABLE @wp_pod_types ADD COLUMN after_helpers TEXT AFTER before_helpers' );
186
187 update_option( 'pods_version', '151' );
188 }
189
190 if ( version_compare( $old_version, '1.6.0', '<' ) ) {
191 // Add the "templates" table
192 $sql = '
193 CREATE TABLE IF NOT EXISTS @wp_pod_templates (
194 id INT unsigned auto_increment primary key,
195 name VARCHAR(32),
196 code TEXT)';
197 pods_query( $sql );
198
199 // Add list and detail template presets
200 $tpl_list = '<p><a href="{@detail_url}">{@name}</a></p>';
201 $tpl_detail = "<h2>{@name}</h2>\n{@body}";
202 pods_query( "INSERT INTO @wp_pod_templates (name, code) VALUES ('detail', '$tpl_detail'),('list', '$tpl_list')" );
203
204 // Try to route old templates as best as possible
205 $result = pods_query( 'SELECT name, tpl_detail, tpl_list FROM @wp_pod_types' );
206
207 foreach ( $result as $row ) {
208 // Create the new template, e.g. "dtname_list" or "dtname_detail"
209 $row = pods_sanitize( $row );
210
211 pods_query( "INSERT INTO @wp_pod_templates (name, code) VALUES ('{$row->name}_detail', '{$row->tpl_detail}'),('{$row->name}_list', '{$row->tpl_list}')" );
212 }
213
214 // Drop the "tpl_detail" and "tpl_list" columns
215 pods_query( 'ALTER TABLE @wp_pod_types DROP COLUMN tpl_detail, DROP COLUMN tpl_list' );
216
217 // Add the "pick_filter" column
218 pods_query( 'ALTER TABLE @wp_pod_fields ADD COLUMN pick_filter VARCHAR(128) AFTER pickval' );
219
220 update_option( 'pods_version', '160' );
221 }//end if
222
223 if ( version_compare( $old_version, '1.6.2', '<' ) ) {
224 // Remove all beginning and ending slashes from Pod Pages
225 pods_query( "UPDATE @wp_pod_pages SET uri = TRIM(BOTH '/' FROM uri)" );
226
227 update_option( 'pods_version', '162' );
228 }
229
230 if ( version_compare( $old_version, '1.6.4', '<' ) ) {
231 pods_query( 'ALTER TABLE @wp_pod_fields ADD COLUMN pick_orderby TEXT AFTER pick_filter' );
232 pods_query( 'ALTER TABLE @wp_pod_fields CHANGE helper display_helper TEXT' );
233 pods_query( 'ALTER TABLE @wp_pod_fields ADD COLUMN input_helper TEXT AFTER display_helper' );
234
235 update_option( 'pods_version', '164' );
236 }
237
238 if ( version_compare( $old_version, '1.6.7', '<' ) ) {
239 pods_query( 'ALTER TABLE @wp_pod_pages ADD COLUMN precode LONGTEXT AFTER phpcode' );
240
241 update_option( 'pods_version', '167' );
242 }
243
244 if ( version_compare( $old_version, '1.7.3', '<' ) ) {
245 pods_query( 'ALTER TABLE @wp_pod_types ADD COLUMN detail_page VARCHAR(128) AFTER is_toplevel' );
246
247 update_option( 'pods_version', '173' );
248 }
249
250 if ( version_compare( $old_version, '1.7.5', '<' ) ) {
251 if ( empty( $pods_roles ) && ! is_array( $pods_roles ) ) {
252 $pods_roles = pods_maybe_safely_unserialize( get_option( 'pods_roles' ) );
253
254 if ( ! is_array( $pods_roles ) ) {
255 $pods_roles = array();
256 }
257 }
258
259 if ( is_array( $pods_roles ) ) {
260 foreach ( $pods_roles as $role => $privs ) {
261 if ( in_array( 'manage_podpages', $privs, true ) ) {
262 $pods_roles[ $role ][] = 'manage_pod_pages';
263
264 unset( $pods_roles[ $role ]['manage_podpages'] );
265 }
266 }
267 }
268
269 delete_option( 'pods_roles' );
270 add_option( 'pods_roles', serialize( $pods_roles ) );
271
272 update_option( 'pods_version', '175' );
273 }//end if
274
275 if ( version_compare( $old_version, '1.7.6', '<' ) ) {
276 pods_query( 'ALTER TABLE @wp_pod_types CHANGE label label VARCHAR(128)' );
277 pods_query( 'ALTER TABLE @wp_pod_fields CHANGE label label VARCHAR(128)' );
278
279 $result = pods_query( "SELECT f.id AS field_id, f.name AS field_name, f.datatype AS datatype_id, dt.name AS datatype FROM @wp_pod_fields AS f LEFT JOIN @wp_pod_types AS dt ON dt.id = f.datatype WHERE f.coltype='file'" );
280
281 foreach ( $result as $row ) {
282 $items = pods_query( "SELECT t.id AS tbl_row_id, t.{$row->field_name} AS file, p.id AS pod_id FROM @wp_pod_tbl_{$row->datatype} AS t LEFT JOIN @wp_pod AS p ON p.tbl_row_id = t.id AND p.datatype = {$row->datatype_id} WHERE t.{$row->field_name} != '' AND t.{$row->field_name} IS NOT NULL" );
283 $success = false;
284 $rels = array();
285
286 foreach ( (array) $items as $item ) {
287 $filename = (string) $item->file;
288
289 if ( strpos( $filename, get_site_url() ) !== false && 0 === strpos( $filename, get_site_url() ) ) {
290 $filename = ltrim( $filename, get_site_url() );
291 }
292
293 $upload_dir = wp_upload_dir();
294
295 if ( strpos( $filename, str_replace( get_site_url(), '', $upload_dir['baseurl'] ) ) === false ) {
296 $success = false;
297
298 break;
299 }
300
301 $file = str_replace( '//', '/', ( ABSPATH . $filename ) );
302
303 $wp_filetype = wp_check_filetype( basename( $file ), null );
304
305 $attachment = array(
306 'post_mime_type' => $wp_filetype['type'],
307 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file ) ),
308 'guid' => str_replace( '//wp-content/', '/wp-content/', get_site_url() . $filename ),
309 'post_content' => '',
310 'post_status' => 'inherit',
311 );
312
313 $attach_id = wp_insert_attachment( $attachment, $file, 0 );
314
315 if ( $attach_id > 0 ) {
316 require_once ABSPATH . 'wp-admin' . '/includes/image.php';
317
318 $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
319
320 wp_update_attachment_metadata( $attach_id, $attach_data );
321
322 $sizes = array( 'thumb', 'medium', 'large' );
323
324 foreach ( $sizes as $size ) {
325 image_downsize( $attach_id, $size );
326 }
327
328 $rels[] = array(
329 'pod_id' => $item->pod_id,
330 'tbl_row_id' => $item->tbl_row_id,
331 'attach_id' => $attach_id,
332 'field_id' => $row->field_id,
333 );
334
335 $success = true;
336 }//end if
337 }//end foreach
338 if ( false !== $success ) {
339 foreach ( $rels as $rel ) {
340 pods_query( "INSERT INTO @wp_pod_rel (pod_id, field_id, tbl_row_id) VALUES({$rel['pod_id']}, {$rel['field_id']}, {$rel['attach_id']})" );
341 }
342
343 pods_query( "ALTER TABLE @wp_pod_tbl_{$row->datatype} DROP COLUMN {$row->field_name}" );
344 } else {
345 pods_query( "UPDATE @wp_pod_fields SET coltype = 'txt' WHERE id = {$row->field_id}" );
346 }
347 }//end foreach
348
349 update_option( 'pods_version', '176' );
350 }//end if
351
352 if ( version_compare( $old_version, '1.8.1', '<' ) ) {
353 pods_query( 'ALTER TABLE @wp_pod_rel ADD COLUMN weight SMALLINT unsigned AFTER tbl_row_id' );
354 pods_query( 'ALTER TABLE @wp_pod_types CHANGE before_helpers pre_save_helpers TEXT' );
355 pods_query( 'ALTER TABLE @wp_pod_types CHANGE after_helpers post_save_helpers TEXT' );
356 pods_query( 'ALTER TABLE @wp_pod_types ADD COLUMN pre_drop_helpers TEXT AFTER pre_save_helpers' );
357 pods_query( 'ALTER TABLE @wp_pod_types ADD COLUMN post_drop_helpers TEXT AFTER post_save_helpers' );
358 pods_query( "UPDATE @wp_pod_helpers SET helper_type = 'pre_save' WHERE helper_type = 'before'" );
359 pods_query( "UPDATE @wp_pod_helpers SET helper_type = 'post_save' WHERE helper_type = 'after'" );
360
361 update_option( 'pods_version', '181' );
362 }
363
364 if ( version_compare( $old_version, '1.8.2', '<' ) ) {
365 pods_query( 'ALTER TABLE @wp_pod ADD COLUMN author_id INT unsigned AFTER modified' );
366 pods_query( "UPDATE @wp_pod_fields SET pickval = 'wp_taxonomy' WHERE pickval REGEXP '^[0-9]+$'" );
367 pods_query( "UPDATE @wp_pod_menu SET uri = '<root>' WHERE uri = '/' LIMIT 1" );
368
369 // Remove beginning and trailing slashes
370 $result = pods_query( 'SELECT id, uri FROM @wp_pod_menu' );
371
372 foreach ( $result as $row ) {
373 $uri = preg_replace( '@^([/]?)(.*?)([/]?)$@', '$2', $row->uri );
374 $uri = pods_sanitize( $uri );
375 pods_query( "UPDATE @wp_pod_menu SET uri = '$uri' WHERE id = {$row->id} LIMIT 1" );
376 }
377
378 update_option( 'pods_version', '182' );
379 }
380
381 if ( version_compare( $old_version, '1.9.0', '<' ) ) {
382 pods_query( 'ALTER TABLE @wp_pod_templates CHANGE `name` `name` VARCHAR(255)' );
383 pods_query( 'ALTER TABLE @wp_pod_helpers CHANGE `name` `name` VARCHAR(255)' );
384 pods_query( 'ALTER TABLE @wp_pod_fields CHANGE `comment` `comment` VARCHAR(255)' );
385
386 // Remove beginning and trailing slashes
387 $result = pods_query( 'SELECT id, uri FROM @wp_pod_pages' );
388
389 foreach ( $result as $row ) {
390 $uri = trim( $row->uri, '/' );
391 $uri = pods_sanitize( $uri );
392 pods_query( "UPDATE @wp_pod_pages SET uri = '$uri' WHERE id = {$row->id} LIMIT 1" );
393 }
394
395 update_option( 'pods_version', '190' );
396 }
397
398 if ( version_compare( $old_version, '1.9.6', '<' ) ) {
399 add_option( 'pods_disable_file_browser', 0 );
400 add_option( 'pods_files_require_login', 0 );
401 add_option( 'pods_files_require_login_cap', 'upload_files' );
402 add_option( 'pods_disable_file_upload', 0 );
403 add_option( 'pods_upload_require_login', 0 );
404 add_option( 'pods_upload_require_login_cap', 'upload_files' );
405
406 update_option( 'pods_version', '196' );
407 }
408
409 if ( version_compare( $old_version, '1.9.7', '<' ) ) {
410 pods_query( 'ALTER TABLE `@wp_pod` CHANGE `id` `id` BIGINT(15) UNSIGNED NOT NULL AUTO_INCREMENT' );
411 pods_query( 'ALTER TABLE `@wp_pod` CHANGE `tbl_row_id` `tbl_row_id` BIGINT(15) UNSIGNED NULL DEFAULT NULL' );
412 pods_query( 'ALTER TABLE `@wp_pod` CHANGE `author_id` `author_id` BIGINT(15) UNSIGNED NULL DEFAULT NULL' );
413 pods_query( 'ALTER TABLE `@wp_pod_rel` CHANGE `id` `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT' );
414 pods_query( 'ALTER TABLE `@wp_pod_rel` CHANGE `pod_id` `pod_id` BIGINT(15) UNSIGNED NULL DEFAULT NULL' );
415 pods_query( 'ALTER TABLE `@wp_pod_rel` CHANGE `sister_pod_id` `sister_pod_id` BIGINT(15) UNSIGNED NULL DEFAULT NULL' );
416 pods_query( 'ALTER TABLE `@wp_pod_rel` CHANGE `tbl_row_id` `tbl_row_id` BIGINT(15) UNSIGNED NULL DEFAULT NULL' );
417 pods_query( "ALTER TABLE `@wp_pod_rel` CHANGE `weight` `weight` INT(10) UNSIGNED NULL DEFAULT '0'" );
418
419 update_option( 'pods_version', '197' );
420 }
421
422 if ( version_compare( $old_version, '1.11', '<' ) ) {
423 pods_query( 'ALTER TABLE `@wp_pod` CHANGE `datatype` `datatype` INT(10) UNSIGNED NULL DEFAULT NULL' );
424 pods_query( 'ALTER TABLE `@wp_pod` DROP INDEX `datatype_idx`', false );
425 pods_query( 'ALTER TABLE `@wp_pod` ADD INDEX `datatype_row_idx` (`datatype`, `tbl_row_id`)', false );
426 pods_query( 'ALTER TABLE `@wp_pod_rel` DROP INDEX `field_id_idx`', false );
427 pods_query( 'ALTER TABLE `@wp_pod_rel` ADD INDEX `field_pod_idx` (`field_id`, `pod_id`)', false );
428 pods_query( 'ALTER TABLE `@wp_pod_fields` CHANGE `datatype` `datatype` INT(10) UNSIGNED NULL DEFAULT NULL' );
429 $result = pods_query( 'SELECT id, name FROM @wp_pod_types' );
430 foreach ( $result as $row ) {
431 $pod = pods_sanitize( $row->name );
432 pods_query( "ALTER TABLE `@wp_pod_tbl_{$pod}` CHANGE `id` `id` BIGINT(15) UNSIGNED NOT NULL AUTO_INCREMENT" );
433 }
434 update_option( 'pods_version', '001011000' );
435 }
436