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