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 / deprecated / classes / Pods.php
pods / deprecated / classes Last commit date
Pods.php 4 months ago PodsAPI.php 4 months ago
Pods.php
722 lines
1 <?php
2
3 // Don't load directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 die( '-1' );
6 }
7
8 require_once PODS_DIR . 'deprecated/deprecated.php';
9
10 use Pods\Whatsit\Field;
11
12 /**
13 * @package Pods\Deprecated
14 */
15 class Pods_Deprecated {
16
17 private $obj;
18
19 public $id;
20
21 public $data;
22
23 public $datatype;
24
25 public $datatype_id;
26
27 /**
28 * Constructor - Pods Deprecated functionality (pre 2.0)
29 *
30 * @param object $obj The Pods object
31 *
32 * @license http://www.gnu.org/licenses/gpl-2.0.html
33 * @since 2.0.0
34 */
35 public function __construct( $obj ) {
36 // backwards-compatibility with references to $this->var_name
37 $vars = get_object_vars( $obj );
38
39 foreach ( (array) $vars as $key => $val ) {
40 $this->{$key} = $val;
41 }
42
43 // keeping references pointing back to the source
44 $this->obj =& $obj;
45 }
46
47 /**
48 * Set a custom data value (no database changes)
49 *
50 * @param string $name The field name
51 * @param mixed $data The value to set
52 *
53 * @return mixed The value of $data
54 * @since 1.2.0
55 */
56 public function set_field( $name, $data = null ) {
57 if ( Pod::$deprecated_notice ) {
58 pods_deprecated( 'Pods::set_field', '2.0' );
59 }
60
61 $this->obj->row[ $name ] = $data;
62
63 return $this->obj->row[ $name ];
64 }
65
66 /**
67 * Display HTML for all datatype fields
68 *
69 * @deprecated 2.0.0
70 *
71 * @param null $id
72 * @param null $public_fields
73 * @param string $label
74 */
75 public function showform( $id = null, $public_fields = null, $label = 'Save changes' ) {
76 if ( Pod::$deprecated_notice ) {
77 pods_deprecated( 'Pods::showform', '2.0' );
78 }
79
80 $public_columns =& $public_fields;
81
82 $pod = $this->obj->pod;
83 $pod_id = $this->obj->pod_id;
84 $this->obj->type_counter = array();
85
86 $attributes = array();
87
88 if ( ! empty( $public_fields ) ) {
89 foreach ( $public_fields as $key => $value ) {
90 if ( is_array( $public_fields[ $key ] ) ) {
91 $attributes[ $key ] = $value;
92 } else {
93 $attributes[ $value ] = array();
94 }
95 }
96 }
97
98 $fields = $this->obj->fields;
99
100 // Re-order the fields if a public form
101 if ( ! empty( $attributes ) ) {
102 $fields = array();
103
104 foreach ( $attributes as $key => $value ) {
105 if ( isset( $this->obj->fields[ $key ] ) ) {
106 $fields[ $key ] = $this->obj->fields[ $key ];
107 }
108 }
109 }
110
111 do_action( 'pods_showform_pre', $pod_id, $public_fields, $label, $this );
112
113 foreach ( $fields as $key => $field ) {
114 $is_field_object = $field instanceof Field;
115
116 if (
117 (
118 ! is_array( $field )
119 && ! $is_field_object
120 )
121 || in_array( $key, array( 'created', 'modified' ), true )
122 ) {
123 continue;
124 }
125
126 // Replace field attributes with public form attributes
127 if ( ! empty( $attributes ) && is_array( $attributes[ $key ] ) ) {
128 $field = pods_config_merge_data( $field, $attributes[ $key ] );
129 }
130
131 // Replace the input helper name with the helper code
132 if ( ! empty( $field['input_helper'] ) ) {
133 $helper = $this->obj->api->load_helper( array( 'name' => $field['input_helper'] ) );
134 $field['input_helper'] = '';
135
136 if ( ! empty( $helper ) ) {
137 $field['input_helper'] = $helper['code'];
138 }
139 }
140
141 if ( empty( $field['label'] ) ) {
142 $field['label'] = ucwords( $key );
143 }
144
145 if ( 1 == $field['required'] ) {
146 $field['label'] .= ' <span class="red">*</span>';
147 }
148
149 if ( ! empty( $field['pick_val'] ) ) {
150 $selected_ids = array();
151 $pick_object = $field['pick_object'];
152 $pick_val = $field['pick_val'];
153
154 if ( 'pod' === $pick_object ) {
155 $pick_pod = $this->obj->api->load_pod( array( 'name' => $pick_val ) );
156 $pick_object = $pick_pod['type'];
157 $pick_val = $pick_pod['name'];
158 }
159
160 $pick_table = '';
161 $pick_join = '';
162 $pick_where = '';
163
164 $pick_field_id = 'id';
165 $pick_field_name = 'name';
166 switch ( $pick_object ) {
167 case 'pod':
168 $pick_table = "@wp_pods_{$pick_val}";
169 $pick_field_id = 'id';
170 $pick_field_name = 'name';
171 break;
172 case 'post_type':
173 $pick_table = '@wp_posts';
174 $pick_field_id = 'ID';
175 $pick_field_name = 'post_title';
176 $pick_where = "t.`post_type` = '{$pick_val}'";
177 break;
178 case 'taxonomy':
179 $pick_table = '@wp_terms';
180 $pick_field_id = 'term_id';
181 $pick_field_name = 'name';
182 $pick_join = '`@wp_term_taxonomy` AS tx ON tx.`term_id` = t.`term_id';
183 $pick_where = "tx.`taxonomy` = '{$pick_val}' AND tx.`taxonomy` IS NOT NULL";
184 break;
185 case 'user':
186 $pick_table = '@wp_users';
187 $pick_field_id = 'ID';
188 $pick_field_name = 'user_login';
189 break;
190 case 'comment':
191 $pick_table = '@wp_comments';
192 $pick_field_id = 'comment_ID';
193 $pick_field_name = 'comment_date';
194 $pick_where = "t.`comment_type` = '{$pick_val}'";
195 break;
196 case 'table':
197 $pick_table = "{$pick_val}";
198 $pick_field_id = 'id';
199 $pick_field_name = 'name';
200 break;
201 }//end switch
202
203 $sql = 'SELECT `related_item_id` FROM `@wp_podsrel` WHERE `item_id` = %d AND `field_id` = %d';
204
205 $sql = array( $sql, array( $id, $field['id'] ) );
206
207 $result = pods_query( $sql, $this );
208
209 foreach ( $result as $row ) {
210 $selected_ids[] = $row->related_item_id;
211 }
212
213 // Use default values for public forms
214 if ( empty( $selected_ids ) && ! empty( $field['default'] ) ) {
215 $default_ids = $field['default'];
216
217 if ( ! is_array( $field['default'] ) ) {
218 $default_ids = explode( ',', $default_ids );
219 }
220
221 foreach ( $default_ids as $default_id ) {
222 $default_id = pods_absint( $default_id );
223
224 if ( 0 < $default_id ) {
225 $selected_ids[] = $default_id;
226 }
227 }
228 }
229
230 // If the PICK field is unique, get values already chosen
231 $exclude = false;
232
233 if ( 1 == $field['unique'] ) {
234 $unique_where = ( empty( $id ) ) ? '' : ' AND `item_id` != %d';
235
236 $sql = "SELECT `related_item_id` FROM `@wp_podsrel` WHERE `field_id` = %d {$unique_where}";
237
238 $sql = array( $sql, array( $field['id'] ) );
239
240 if ( ! empty( $id ) ) {
241 $sql[1][] = $id;
242 }
243
244 $result = pods_query( $sql, $this );
245
246 if ( ! empty( $result ) ) {
247 $exclude = array();
248
249 foreach ( $result as $row ) {
250 $exclude[] = (int) $row->related_item_id;
251 }
252
253 $exclude = implode( ',', $exclude );
254 }
255 }//end if
256
257 if ( ! empty( $field['options']['pick_filter'] ) ) {
258 $pick_where .= ' AND ' . $field['options']['pick_filter'];
259 }
260
261 $params = array(
262 'exclude' => $exclude, // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
263 'selected_ids' => $selected_ids,
264 'table' => $pick_table,
265 'field_id' => $pick_field_id,
266 'field_name' => $pick_field_name,
267 'join' => $pick_join,
268 'orderby' => $field['options']['pick_orderby'],
269 'where' => $pick_where,
270 );
271
272 $this->obj->row[ $key ] = $this->get_dropdown_values( $params );
273 } else {
274 // Set a default value if no value is entered
275 if ( ! isset( $this->obj->row[ $key ] ) || ( null === $this->obj->row[ $key ] || false === $this->obj->row[ $key ] ) ) {
276 if ( ! empty( $field['default'] ) ) {
277 $this->obj->row[ $key ] = $field['default'];
278 } else {
279 $this->obj->row[ $key ] = null;
280 }
281 }
282 }//end if
283
284 $this->obj->build_field_html( $field );
285 }//end foreach
286
287 $uri_hash = wp_hash( $_SERVER['REQUEST_URI'] );
288
289 $save_button_atts = array(
290 'type' => 'button',
291 'class' => 'button btn_save',
292 'value' => $label,
293 'onclick' => 'saveForm(1)',
294 );
295
296 $save_button_atts = apply_filters( 'pods_showform_save_button_atts', $save_button_atts, $this );
297 ?>
298 <div>
299 <input type="hidden" class="form num id" value="<?php echo esc_attr( $id ); ?>" />
300 <input type="hidden" class="form txt pod" value="<?php echo esc_attr( $pod ); ?>" />
301 <input type="hidden" class="form txt pod_id" value="<?php echo esc_attr( $pod_id ); ?>" />
302 <input type="hidden" class="form txt form_count" value="1" />
303 <input type="hidden" class="form txt token" value="<?php echo esc_attr( pods_generate_key( $pod, $uri_hash, $public_fields, 1 ) ); ?>" />
304 <input type="hidden" class="form txt uri_hash" value="<?php echo esc_attr( $uri_hash ); ?>" />
305 <input <?php foreach ( $save_button_atts as $att => $value ) : ?>
306 <?php echo esc_html( sanitize_title( $att ) ); ?>="<?php echo esc_attr( $value ); ?>
307 <?php endforeach; ?> />
308 </div>
309 <?php
310 do_action( 'pods_showform_post', $pod_id, $public_fields, $label, $this );
311 }
312
313 /**
314 * Get pod or category drop-down values
315 *
316 * @param array $params
317 *
318 * @return array
319 */
320 public function get_dropdown_values( $params ) {
321 if ( Pod::$deprecated_notice ) {
322 pods_deprecated( 'Pods::get_dropdown_values', '2.0' );
323 }
324
325 global $wpdb;
326
327 $params = (object) $params;
328
329 $params->orderby = empty( $params->orderby ) ? '' : ' ORDER BY ' . $params->orderby;
330 $params->join = empty( $params->join ) ? '' : ' LEFT JOIN ' . $params->join;
331
332 $where = ( false !== $params->exclude ) ? "WHERE `t`.term_id NOT IN ({$params->exclude})" : '';
333
334 if ( ! empty( $params->pick_filter ) ) {
335 $where .= ( empty( $where ) ? ' WHERE ' : ' AND ' ) . $params->pick_filter;
336 }
337
338 if ( ! empty( $params->where ) ) {
339 $where .= ( empty( $where ) ? ' WHERE ' : ' AND ' ) . $params->where;
340 }
341
342 $sql = "
343 SELECT
344 `t`.`{$params->field_id}` AS `id`,
345 `t`.`{$params->field_name}` AS `name`
346 FROM `{$params->table}` AS `t`
347 {$params->join}
348 {$where}
349 {$params->orderby}
350 ";
351
352 // override with custom dropdown values
353 $sql = apply_filters( 'pods_get_dropdown_values', $sql, $params, $this );
354
355 $val = array();
356 $result = pods_query( $sql );
357
358 foreach ( $result as $row ) {
359 $row = get_object_vars( $row );
360 $row['active'] = false;
361
362 if ( ! empty( $params->selected_ids ) ) {
363 $row['active'] = in_array( $row['id'], $params->selected_ids );
364 }
365
366 $val[] = $row;
367 }
368
369 return $val;
370 }
371
372 /**
373 * Build public input form
374 *
375 * @deprecated 2.0.0
376 *
377 * @param null $fields
378 * @param string $label
379 * @param null $thankyou_url
380 */
381 public function publicForm( $fields = null, $label = 'Save Changes', $thankyou_url = null ) {
382 if ( Pod::$deprecated_notice ) {
383 pods_deprecated( 'Pods::publicForm', '2.0', 'Pods::form' );
384 }
385
386 if ( ! empty( $fields ) ) {
387 // Just update field name here, form() will handle the rest
388 foreach ( $fields as $k => $field ) {
389 $name = $k;
390
391 $is_field_object = $field instanceof Field;
392
393 if ( ! is_array( $field ) && ! $is_field_object ) {
394 $name = $field;
395 $field = array();
396 } elseif ( isset( $field['name'] ) ) {
397 $name = $field['name'];
398 }
399
400 if ( in_array( $name, array(
401 'created',
402 'modified',
403 'author',
404 ), true ) && isset( $this->obj->fields[ $name . '2' ] ) ) {
405 $name .= '2';
406 }
407
408 $field['name'] = $name;
409
410 $fields[ $k ] = $field;
411 }//end foreach
412 }//end if
413
414 $this->obj->output_form( $fields, $label, $thankyou_url );
415 }
416
417 /**
418 * Build HTML for a single field
419 *
420 * @deprecated 2.0.0
421 *
422 * @param array $field Field data.
423 */
424 public function build_field_html( $field ) {
425 if ( Pod::$deprecated_notice ) {
426 pods_deprecated( 'Pods::build_field_html', '2.0' );
427 }
428
429 include PODS_DIR . 'deprecated/input_fields.php';
430 }
431
432 /**
433 * Fetch a row of results from the DB
434 *
435 * @since 1.2.0
436 * @deprecated 2.0.0
437 */
438 public function fetchRecord() {
439 if ( Pod::$deprecated_notice ) {
440 pods_deprecated( 'Pods::fetchRecord', '2.0', 'Pods::fetch' );
441 }
442
443 return $this->obj->fetch();
444 }
445
446 /**
447 * Return a field's value(s)
448 *
449 * @param string $name The field name
450 * @param string $orderby (optional) The orderby string, for PICK fields
451 *
452 * @since 1.2.0
453 * @deprecated 2.0.0
454 * @return array|mixed
455 */
456 public function get_field( $name, $orderby = null ) {
457 if ( Pod::$deprecated_notice ) {
458 pods_deprecated( 'Pods::get_field', '2.0', 'Pods::field' );
459 }
460
461 $value = $this->obj->field( array(
462 'name' => $name,
463 'orderby' => $orderby,
464 'deprecated' => true,
465 ) );
466
467 if ( is_array( $value ) && ! empty( $value ) ) {
468 if ( false === strpos( (string) $name, '.' ) && ! isset( $value[0] ) ) {
469 $value = array( $value );
470 } elseif ( false !== strpos( (string) $name, '.' ) && 1 == count( $value ) ) {
471 // fix for single tableless fields
472 $value = current( $value );
473 }
474 }
475
476 return $value;
477 }
478
479 /**
480 * Get the current item's pod ID from its datatype ID and tbl_row_id
481 *
482 * @return int The ID from the wp_pod table
483 * @since 1.2.0
484 * @deprecated 2.0.0
485 */
486 public function get_pod_id() {
487 if ( Pod::$deprecated_notice ) {
488 pods_deprecated( 'Pods::get_pod_id', '2.0' );
489 }
490
491 if ( ! empty( $this->obj->row ) ) {
492 return $this->obj->row[ $this->obj->data->field_id ];
493 }
494
495 return false;
496 }
497
498 /**
499 * Search and filter records
500 *
501 * @since 1.x.x
502 * @deprecated 2.0.0
503 *
504 * @param null $orderby
505 * @param int $rows_per_page
506 * @param null $where
507 * @param null $sql
508 *
509 * @return
510 */
511 public function findRecords( $orderby = null, $rows_per_page = 15, $where = null, $sql = null ) {
512 if ( Pod::$deprecated_notice ) {
513 pods_deprecated( 'Pods::findRecords', '2.0', 'Pods::find' );
514 }
515
516 $find = array(
517 ' p.created',
518 '`p`.`created`',
519 '`p`.created',
520 ' p.`created`',
521 ' p.modified',
522 '`p`.`modified`',
523 '`p`.modified',
524 ' p.`modified`',
525 ' p.id',
526 '`p`.`id`',
527 '`p`.id',
528 ' p.`id`',
529 ' p.pod_id',
530 '`p`.`pod_id`',
531 '`p`.pod_id',
532 ' p.`pod_id`',
533 );
534
535 $replace = array(
536 ' t.created',
537 '`t`.`created`',
538 '`t`.created',
539 ' t.`created`',
540 ' t.modified',
541 '`t`.`modified`',
542 '`t`.modified',
543 ' t.`modified`',
544 ' t.id',
545 '`t`.`id`',
546 '`t`.id',
547 ' t.`id`',
548 ' t.id',
549 '`t`.`id`',
550 '`t`.id',
551 ' t.`id`',
552 );
553
554 $params = array(
555 'where' => $where,
556 'orderby' => "`t`.`{$this->obj->data->field_id}` DESC",
557 'limit' => (int) $rows_per_page,
558 'page' => $this->obj->page,
559 'search' => $this->obj->search,
560 'search_across' => true,
561 'search_across_picks' => false,
562 'sql' => $sql,
563 );
564
565 if ( is_array( $orderby ) ) {
566 $params = array_merge( $params, $orderby );
567 } elseif ( ! empty( $orderby ) ) {
568 $params['orderby'] = $orderby;
569 }
570
571 $params['where'] = trim( str_replace( $find, $replace, ' ' . $params['where'] ) );
572 $params['orderby'] = trim( str_replace( $find, $replace, ' ' . $params['orderby'] ) );
573
574 $params = (object) $params;
575
576 $this->obj->limit = $params->limit;
577 $this->obj->page = $params->page;
578 $this->obj->search = $params->search;
579
580 return $this->obj->find( $params );
581 }
582
583 /**
584 * Return a single record
585 *
586 * @since 1.x.x
587 * @deprecated 2.0.0
588 *
589 * @param int $id Item ID.
590 *
591 * @return
592 */
593 public function getRecordById( $id ) {
594 if ( Pod::$deprecated_notice ) {
595 pods_deprecated( 'Pods::getRecordById', '2.0', 'Pods::fetch_item' );
596 }
597
598 return $this->obj->fetch_item( $id );
599 }
600
601 /**
602 * Fetch the total row count
603 *
604 * @deprecated 2.0.0
605 */
606 public function getTotalRows() {
607 if ( Pod::$deprecated_notice ) {
608 pods_deprecated( 'Pods::getTotalRows', '2.0', 'Pods::total_found' );
609 }
610
611 return $this->obj->total_found();
612 }
613
614 /**
615 * (Re)set the MySQL result pointer
616 *
617 * @deprecated 2.0.0
618 *
619 * @param int $row_number
620 *
621 * @return
622 */
623 public function resetPointer( $row_number = 0 ) {
624 if ( Pod::$deprecated_notice ) {
625 pods_deprecated( 'Pods::resetPointer', '2.0', 'Pods::reset' );
626 }
627
628 return $this->obj->reset( $row_number );
629 }
630
631 /**
632 * Display the pagination controls
633 *
634 * @deprecated 2.0.0
635 *
636 * @param string $label
637 */
638 public function getPagination( $label = 'Go to page:' ) {
639 if ( Pod::$deprecated_notice ) {
640 pods_deprecated( 'Pods::getPagination', '2.0', 'Pods::pagination' );
641 }
642
643 $this->obj->output_pagination( array(
644 'type' => 'advanced',
645 'label' => $label,
646 ) );
647 }
648
649 /**
650 * Display the list filters
651 *
652 * @deprecated 2.0.0
653 *
654 * @param null $filters
655 * @param string $label
656 * @param string $action
657 */
658 public function getFilters( $filters = null, $label = 'Filter', $action = '' ) {
659 if ( Pod::$deprecated_notice ) {
660 pods_deprecated( 'Pods::getFilters', '2.0', 'Pods::filters' );
661 }
662
663 $params = array(
664 'fields' => $filters,
665 'label' => $label,
666 'action' => $action,
667 );
668
669 if ( is_array( $filters ) ) {
670 $params = array_merge( $params, $filters );
671 }
672
673 $this->obj->output_filters( $params );
674 }
675
676 /**
677 * Run a helper within a Pod Page or WP Template
678 *
679 * @param string $helper_name Helper name.
680 * @param null|string|array $value Field value.
681 * @param null|string $name Field name.
682 *
683 * @return mixed Anything returned by the helper
684 * @internal param string $helper The helper name
685 *
686 * @since 1.2.0
687 * @deprecated 2.0.0
688 */
689 public function pod_helper( $helper_name, $value = null, $name = null ) {
690 if ( Pod::$deprecated_notice ) {
691 pods_deprecated( 'Pods::pod_helper', '2.0', 'Pods::helper' );
692 }
693
694 $params = array(
695 'helper' => $helper_name,
696 'value' => $value,
697 'name' => $name,
698 'deprecated' => true,
699 );
700
701 return $this->obj->helper( $params );
702 }
703
704 /**
705 * Display the page template
706 *
707 * @deprecated 2.0.0
708 *
709 * @param string $template_name Template name.
710 * @param null|string $code Template code override.
711 *
712 * @return
713 */
714 public function showTemplate( $template_name, $code = null ) {
715 if ( Pod::$deprecated_notice ) {
716 pods_deprecated( 'Pods::showTemplate', '2.0', 'Pods::template' );
717 }
718
719 return $this->obj->template( $template_name, $code, true );
720 }
721 }
722