Complex classes like Pods often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Pods, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Pods implements Iterator { |
||
9 | |||
10 | /** |
||
11 | * Whether the Pods object is in a PHP Iterator call. |
||
12 | * |
||
13 | * @var bool |
||
14 | */ |
||
15 | private $iterator = false; |
||
16 | |||
17 | /** |
||
18 | * PodsAPI object. |
||
19 | * |
||
20 | * @var PodsAPI |
||
21 | */ |
||
22 | public $api; |
||
23 | |||
24 | /** |
||
25 | * PodsData object. |
||
26 | * |
||
27 | * @var PodsData |
||
28 | */ |
||
29 | public $data; |
||
30 | |||
31 | /** |
||
32 | * PodsData object for additional calls. |
||
33 | * |
||
34 | * @var PodsData |
||
35 | */ |
||
36 | public $alt_data; |
||
37 | |||
38 | /** |
||
39 | * Array of pod item arrays. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | public $rows; |
||
44 | |||
45 | /** |
||
46 | * Current pod item array. |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | public $row; |
||
51 | |||
52 | /** |
||
53 | * Row number. |
||
54 | * |
||
55 | * @var int |
||
56 | */ |
||
57 | private $row_number; |
||
58 | |||
59 | /** |
||
60 | * Override pod item array. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | public $row_override = array(); |
||
65 | |||
66 | /** |
||
67 | * Whether to display errors on the screen. |
||
68 | * |
||
69 | * @var bool |
||
70 | */ |
||
71 | public $display_errors = true; |
||
72 | |||
73 | /** |
||
74 | * Current pod information. |
||
75 | * |
||
76 | * @var array|bool|mixed|null|void |
||
77 | */ |
||
78 | public $pod_data; |
||
79 | |||
80 | /** |
||
81 | * Last used Pods::find() parameters. |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | public $params = array(); |
||
86 | |||
87 | /** |
||
88 | * Current Pod name. |
||
89 | * |
||
90 | * @var string |
||
91 | */ |
||
92 | public $pod; |
||
93 | |||
94 | /** |
||
95 | * Current Pod ID. |
||
96 | * |
||
97 | * @var int |
||
98 | */ |
||
99 | public $pod_id; |
||
100 | |||
101 | /** |
||
102 | * Pod fields information. |
||
103 | * |
||
104 | * @var array |
||
105 | */ |
||
106 | public $fields; |
||
107 | |||
108 | /** |
||
109 | * Last used filters() parameters. |
||
110 | * |
||
111 | * @var array |
||
112 | */ |
||
113 | public $filters = array(); |
||
114 | |||
115 | /** |
||
116 | * Detail page URL used for Advanced Content Types. |
||
117 | * |
||
118 | * @var string |
||
119 | */ |
||
120 | public $detail_page; |
||
121 | |||
122 | /** |
||
123 | * Current Item ID. |
||
124 | * |
||
125 | * @var int |
||
126 | */ |
||
127 | public $id; |
||
128 | |||
129 | /** |
||
130 | * Last used limit from find() lookup. |
||
131 | * |
||
132 | * @var int |
||
133 | */ |
||
134 | public $limit = 15; |
||
135 | |||
136 | /** |
||
137 | * Last used offset from find() lookup. |
||
138 | * |
||
139 | * @var int |
||
140 | */ |
||
141 | public $offset = 0; |
||
142 | |||
143 | /** |
||
144 | * Query variable used for pagination number. |
||
145 | * |
||
146 | * @var string |
||
147 | */ |
||
148 | public $page_var = 'pg'; |
||
149 | |||
150 | /** |
||
151 | * Last used page from find() lookup. |
||
152 | * |
||
153 | * @var int|mixed |
||
154 | */ |
||
155 | public $page = 1; |
||
156 | |||
157 | /** |
||
158 | * Last used state of whether pagination was enabled from find() lookup. |
||
159 | * |
||
160 | * @var bool |
||
161 | */ |
||
162 | public $pagination = true; |
||
163 | |||
164 | /** |
||
165 | * Last used state of whether search was enabled from find() lookup. |
||
166 | * |
||
167 | * @var bool |
||
168 | */ |
||
169 | public $search = true; |
||
170 | |||
171 | /** |
||
172 | * Query variable used for search string. |
||
173 | * |
||
174 | * @var string |
||
175 | */ |
||
176 | public $search_var = 'search'; |
||
177 | |||
178 | /** |
||
179 | * Search mode (int | text | text_like). |
||
180 | * |
||
181 | * @var string |
||
182 | */ |
||
183 | public $search_mode = 'int'; |
||
184 | |||
185 | /** |
||
186 | * Total number of records returned from find() lookup. |
||
187 | * |
||
188 | * @var int |
||
189 | */ |
||
190 | public $total = 0; |
||
191 | |||
192 | /** |
||
193 | * Total number of records found from find() lookup. |
||
194 | * |
||
195 | * @var int |
||
196 | */ |
||
197 | public $total_found = 0; |
||
198 | |||
199 | /** |
||
200 | * Last used ui options for ui() call. |
||
201 | * |
||
202 | * @var array |
||
203 | */ |
||
204 | public $ui = array(); |
||
|
|||
205 | |||
206 | /** |
||
207 | * Page template to use for SEO feature in Pods Pages. |
||
208 | * |
||
209 | * @var string |
||
210 | */ |
||
211 | public $page_template; |
||
212 | |||
213 | /** |
||
214 | * Body classes to use for SEO feature in Pods Pages. |
||
215 | * |
||
216 | * @var array |
||
217 | */ |
||
218 | public $body_classes; |
||
219 | |||
220 | /** |
||
221 | * Meta tags to use for SEO feature in Pods Pages. |
||
222 | * |
||
223 | * @var array |
||
224 | */ |
||
225 | public $meta = array(); |
||
226 | |||
227 | /** |
||
228 | * Meta properties to use for SEO feature in Pods Pages. |
||
229 | * |
||
230 | * @var array |
||
231 | */ |
||
232 | public $meta_properties = array(); |
||
233 | |||
234 | /** |
||
235 | * Meta custom HTML to use for SEO feature in Pods Pages. |
||
236 | * |
||
237 | * @var string |
||
238 | */ |
||
239 | public $meta_extra = ''; |
||
240 | |||
241 | /** |
||
242 | * Last SQL query used by a find() lookup. |
||
243 | * |
||
244 | * @var string |
||
245 | */ |
||
246 | public $sql; |
||
247 | |||
248 | /** |
||
249 | * Pods_Deprecated object. |
||
250 | * |
||
251 | * @var Pods_Deprecated |
||
252 | */ |
||
253 | public $deprecated; |
||
254 | |||
255 | /** |
||
256 | * Old Pod name variable. |
||
257 | * |
||
258 | * @var string |
||
259 | * |
||
260 | * @deprecated 2.0 |
||
261 | */ |
||
262 | public $datatype; |
||
263 | |||
264 | /** |
||
265 | * Old Pod ID variable. |
||
266 | * |
||
267 | * @var int |
||
268 | * |
||
269 | * @deprecated 2.0 |
||
270 | */ |
||
271 | public $datatype_id; |
||
272 | |||
273 | /** |
||
274 | * Constructor - Pods Framework core. |
||
275 | * |
||
276 | * @param string $pod The pod name. |
||
277 | * @param mixed $id (optional) The ID or slug, to load a single record; Provide array of $params to run 'find'. |
||
278 | * |
||
279 | * @license http://www.gnu.org/licenses/gpl-2.0.html |
||
280 | * @since 1.0.0 |
||
281 | * @link https://pods.io/docs/pods/ |
||
282 | */ |
||
283 | public function __construct( $pod = null, $id = null ) { |
||
388 | |||
389 | /** |
||
390 | * Whether this Pod object is valid or not |
||
391 | * |
||
392 | * @return bool |
||
393 | * |
||
394 | * @since 2.0 |
||
395 | */ |
||
396 | public function valid() { |
||
408 | |||
409 | /** |
||
410 | * Check if in Iterator mode |
||
411 | * |
||
412 | * @return bool |
||
413 | * |
||
414 | * @since 2.3.4 |
||
415 | * |
||
416 | * @link http://www.php.net/manual/en/class.iterator.php |
||
417 | */ |
||
418 | public function is_iterator() { |
||
422 | |||
423 | /** |
||
424 | * Turn off Iterator mode to off |
||
425 | * |
||
426 | * @return void |
||
427 | * |
||
428 | * @since 2.3.4 |
||
429 | * |
||
430 | * @link http://www.php.net/manual/en/class.iterator.php |
||
431 | */ |
||
432 | public function stop_iterator() { |
||
437 | |||
438 | /** |
||
439 | * Rewind Iterator |
||
440 | * |
||
441 | * @since 2.3.4 |
||
442 | * |
||
443 | * @link http://www.php.net/manual/en/class.iterator.php |
||
444 | */ |
||
445 | public function rewind() { |
||
453 | |||
454 | /** |
||
455 | * Get current Iterator row |
||
456 | * |
||
457 | * @return mixed|boolean |
||
458 | * |
||
459 | * @since 2.3.4 |
||
460 | * |
||
461 | * @link http://www.php.net/manual/en/class.iterator.php |
||
462 | */ |
||
463 | public function current() { |
||
471 | |||
472 | /** |
||
473 | * Get current Iterator key |
||
474 | * |
||
475 | * @return int |
||
476 | * |
||
477 | * @since 2.3.4 |
||
478 | * |
||
479 | * @link http://www.php.net/manual/en/class.iterator.php |
||
480 | */ |
||
481 | public function key() { |
||
485 | |||
486 | /** |
||
487 | * Move onto the next Iterator row |
||
488 | * |
||
489 | * @return void |
||
490 | * |
||
491 | * @since 2.3.4 |
||
492 | * |
||
493 | * @link http://www.php.net/manual/en/class.iterator.php |
||
494 | */ |
||
495 | public function next() { |
||
499 | |||
500 | /** |
||
501 | * Whether a Pod item exists or not when using fetch() or construct with an ID or slug |
||
502 | * |
||
503 | * @return bool |
||
504 | * |
||
505 | * @since 2.0 |
||
506 | */ |
||
507 | public function exists() { |
||
515 | |||
516 | /** |
||
517 | * Return an array of all rows returned from a find() call. |
||
518 | * |
||
519 | * Most of the time, you will want to loop through data using fetch() |
||
520 | * instead of using this function. |
||
521 | * |
||
522 | * @return array|bool An array of all rows returned from a find() call, or false if no items returned |
||
523 | * |
||
524 | * @since 2.0 |
||
525 | * @link https://pods.io/docs/data/ |
||
526 | */ |
||
527 | public function data() { |
||
537 | |||
538 | /** |
||
539 | * Return a field input for a specific field |
||
540 | * |
||
541 | * @param string|array $field Field name or Field data array. |
||
542 | * @param string|array|null $input_name Input field name to use (overrides default name). |
||
543 | * @param mixed $value Current value to use. |
||
544 | * |
||
545 | * @return string Field Input HTML |
||
546 | * |
||
547 | * @since 2.3.10 |
||
548 | */ |
||
549 | public function input( $field, $input_name = null, $value = '__null' ) { |
||
581 | |||
582 | /** |
||
583 | * Return field array from a Pod, a field's data, or a field option |
||
584 | * |
||
585 | * @param null $field Field name. |
||
586 | * @param null $option Option name. |
||
587 | * |
||
588 | * @return bool|mixed |
||
589 | * |
||
590 | * @since 2.0 |
||
591 | */ |
||
592 | public function fields( $field = null, $option = null ) { |
||
644 | |||
645 | /** |
||
646 | * Return row array for an item |
||
647 | * |
||
648 | * @return array|false |
||
649 | * |
||
650 | * @since 2.0 |
||
651 | */ |
||
652 | public function row() { |
||
662 | |||
663 | /** |
||
664 | * Return the output for a field. If you want the raw value for use in PHP for custom manipulation, |
||
665 | * you will want to use field() instead. This function will automatically convert arrays into a |
||
666 | * list of text such as "Rick, John, and Gary" |
||
667 | * |
||
668 | * @param string|array|object $name The field name, or an associative array of parameters. |
||
669 | * @param boolean|array|object $single (optional) For tableless fields, to return an array or the first. |
||
670 | * |
||
671 | * @return string|null|false The output from the field, null if the field doesn't exist, false if no value returned |
||
672 | * for tableless fields |
||
673 | * @since 2.0 |
||
674 | * @link https://pods.io/docs/display/ |
||
675 | */ |
||
676 | public function display( $name, $single = null ) { |
||
722 | |||
723 | /** |
||
724 | * Return the raw output for a field If you want the raw value for use in PHP for custom manipulation, |
||
725 | * you will want to use field() instead. This function will automatically convert arrays into a |
||
726 | * list of text such as "Rick, John, and Gary" |
||
727 | * |
||
728 | * @param string|array|object $name The field name, or an associative array of parameters. |
||
729 | * @param boolean|array|object $single (optional) For tableless fields, to return an array or the first. |
||
730 | * |
||
731 | * @return string|null|false The output from the field, null if the field doesn't exist, false if no value returned |
||
732 | * for tableless fields |
||
733 | * @since 2.0 |
||
734 | * @link https://pods.io/docs/display/ |
||
735 | */ |
||
736 | public function raw( $name, $single = null ) { |
||
758 | |||
759 | /** |
||
760 | * Return the value for a field. |
||
761 | * |
||
762 | * If you are getting a field for output in a theme, most of the time you will want to use display() instead. |
||
763 | * |
||
764 | * This function will return arrays for relationship and file fields. |
||
765 | * |
||
766 | * @param string|array|object $name The field name, or an associative array of parameters. |
||
767 | * @param boolean|array|object $single For tableless fields, to return the whole array or the just the first item, |
||
768 | * or an associative array of parameters. |
||
769 | * @param boolean|array|object $raw Whether to return the raw value, or to run through the field type's display |
||
770 | * method, or an associative array of parameters. |
||
771 | * |
||
772 | * @return mixed|null Value returned depends on the field type, null if the field doesn't exist, false if no value |
||
773 | * returned for tableless fields. |
||
774 | * @since 2.0 |
||
775 | * @link https://pods.io/docs/field/ |
||
776 | */ |
||
777 | public function field( $name, $single = null, $raw = false ) { |
||
778 | |||
779 | $defaults = array( |
||
780 | 'name' => $name, |
||
781 | 'orderby' => null, |
||
782 | 'single' => $single, |
||
783 | 'params' => null, |
||
784 | 'in_form' => false, |
||
785 | 'raw' => $raw, |
||
786 | 'raw_display' => false, |
||
787 | 'display' => false, |
||
788 | 'get_meta' => false, |
||
789 | 'output' => null, |
||
790 | 'deprecated' => false, |
||
791 | // extra data to send to field handlers. |
||
792 | 'args' => array(), |
||
793 | ); |
||
794 | |||
795 | if ( is_object( $name ) ) { |
||
796 | $name = get_object_vars( $name ); |
||
797 | } |
||
798 | |||
799 | if ( is_object( $single ) ) { |
||
800 | $single = get_object_vars( $single ); |
||
801 | } |
||
802 | |||
803 | if ( is_object( $raw ) ) { |
||
804 | $raw = get_object_vars( $raw ); |
||
805 | } |
||
806 | |||
807 | if ( is_array( $name ) ) { |
||
808 | $defaults['name'] = null; |
||
809 | |||
810 | $params = (object) array_merge( $defaults, (array) $name ); |
||
811 | } elseif ( is_array( $single ) ) { |
||
812 | $defaults['single'] = null; |
||
813 | |||
814 | $params = (object) array_merge( $defaults, (array) $single ); |
||
815 | } elseif ( is_array( $raw ) ) { |
||
816 | $defaults['raw'] = false; |
||
817 | |||
818 | $params = (object) array_merge( $defaults, (array) $raw ); |
||
819 | } else { |
||
820 | $params = (object) $defaults; |
||
821 | }//end if |
||
822 | |||
823 | if ( $params->in_form ) { |
||
824 | $params->output = 'ids'; |
||
825 | } elseif ( null === $params->output ) { |
||
826 | /** |
||
827 | * Override the way related fields are output |
||
828 | * |
||
829 | * @param string $output How to output related fields. Default is 'arrays'. Options: ids|names|objects|arrays|pods|find |
||
830 | * @param array|object $row Current row being outputted. |
||
831 | * @param array $params Params array passed to field(). |
||
832 | * @param Pods $this Current Pods object. |
||
833 | */ |
||
834 | $params->output = apply_filters( 'pods_pods_field_related_output_type', 'arrays', $this->row, $params, $this ); |
||
835 | } |
||
836 | |||
837 | if ( in_array( $params->output, array( 'id', 'name', 'object', 'array', 'pod' ), true ) ) { |
||
838 | $params->output .= 's'; |
||
839 | } |
||
840 | |||
841 | // Support old $orderby variable. |
||
842 | if ( null !== $params->single && is_string( $params->single ) && empty( $params->orderby ) ) { |
||
843 | if ( ! class_exists( 'Pod' ) || Pod::$deprecated_notice ) { |
||
844 | pods_deprecated( 'Pods::field', '2.0', 'Use $params[ \'orderby\' ] instead' ); |
||
845 | } |
||
846 | |||
847 | $params->orderby = $params->single; |
||
848 | $params->single = false; |
||
849 | } |
||
850 | |||
851 | if ( null !== $params->single ) { |
||
852 | $params->single = (boolean) $params->single; |
||
853 | } |
||
854 | |||
855 | $params->name = trim( $params->name ); |
||
856 | if ( is_array( $params->name ) || '' === $params->name ) { |
||
857 | return null; |
||
858 | } |
||
859 | |||
860 | $params->full_name = $params->name; |
||
861 | |||
862 | $value = null; |
||
863 | |||
864 | if ( isset( $this->row_override[ $params->name ] ) ) { |
||
865 | $value = $this->row_override[ $params->name ]; |
||
866 | } |
||
867 | |||
868 | if ( false === $this->row() ) { |
||
869 | if ( false !== $this->data() ) { |
||
870 | $this->fetch(); |
||
871 | } else { |
||
872 | return $value; |
||
873 | } |
||
874 | } |
||
875 | |||
876 | if ( $this->data->field_id === $params->name ) { |
||
877 | if ( isset( $this->row[ $params->name ] ) ) { |
||
878 | return $this->row[ $params->name ]; |
||
879 | // @codingStandardsIgnoreLine. |
||
880 | } elseif ( null !== $value ) { |
||
881 | return $value; |
||
882 | } |
||
883 | |||
884 | return 0; |
||
885 | } |
||
886 | |||
887 | $tableless_field_types = PodsForm::tableless_field_types(); |
||
888 | $simple_tableless_objects = PodsForm::simple_tableless_objects(); |
||
889 | |||
890 | $params->traverse = array(); |
||
891 | |||
892 | if ( in_array( $params->name, array( |
||
893 | '_link', |
||
894 | 'detail_url', |
||
895 | ), true ) || ( in_array( $params->name, array( |
||
896 | 'permalink', |
||
897 | 'the_permalink', |
||
898 | ), true ) && in_array( $this->pod_data['type'], array( |
||
899 | 'post_type', |
||
900 | 'taxonomy', |
||
901 | 'media', |
||
902 | 'user', |
||
903 | 'comment', |
||
904 | ), true ) ) ) { |
||
905 | if ( 0 < strlen( $this->detail_page ) ) { |
||
906 | $value = get_home_url() . '/' . $this->do_magic_tags( $this->detail_page ); |
||
907 | } elseif ( in_array( $this->pod_data['type'], array( 'post_type', 'media' ), true ) ) { |
||
908 | $value = get_permalink( $this->id() ); |
||
909 | } elseif ( 'taxonomy' === $this->pod_data['type'] ) { |
||
910 | $value = get_term_link( $this->id(), $this->pod_data['name'] ); |
||
911 | } elseif ( 'user' === $this->pod_data['type'] ) { |
||
912 | $value = get_author_posts_url( $this->id() ); |
||
913 | } elseif ( 'comment' === $this->pod_data['type'] ) { |
||
914 | $value = get_comment_link( $this->id() ); |
||
915 | } |
||
916 | } |
||
917 | |||
918 | $field_data = false; |
||
919 | $last_field_data = false; |
||
920 | $field_type = false; |
||
921 | |||
922 | $first_field = explode( '.', $params->name ); |
||
923 | $first_field = $first_field[0]; |
||
924 | |||
925 | if ( isset( $this->fields[ $first_field ] ) ) { |
||
926 | $field_data = $this->fields[ $first_field ]; |
||
927 | $field_type = 'field'; |
||
928 | } elseif ( ! empty( $this->pod_data['object_fields'] ) ) { |
||
929 | if ( isset( $this->pod_data['object_fields'][ $first_field ] ) ) { |
||
930 | $field_data = $this->pod_data['object_fields'][ $first_field ]; |
||
931 | $field_type = 'object_field'; |
||
932 | } else { |
||
933 | $object_fields = (array) $this->pod_data['object_fields']; |
||
934 | |||
935 | foreach ( $object_fields as $object_field => $object_field_opt ) { |
||
936 | if ( in_array( $first_field, $object_field_opt['alias'], true ) ) { |
||
937 | if ( $first_field === $params->name ) { |
||
938 | $params->name = $object_field; |
||
939 | } |
||
940 | |||
941 | $first_field = $object_field; |
||
942 | $field_data = $object_field_opt; |
||
943 | $field_type = 'object_field'; |
||
944 | |||
945 | break; |
||
946 | } |
||
947 | } |
||
948 | } |
||
949 | }//end if |
||
950 | |||
951 | // Simple fields have no other output options. |
||
952 | if ( 'pick' === $field_data['type'] && in_array( $field_data['pick_object'], $simple_tableless_objects, true ) ) { |
||
953 | $params->output = 'arrays'; |
||
954 | } |
||
955 | |||
956 | if ( empty( $value ) && in_array( $field_data['type'], $tableless_field_types, true ) ) { |
||
957 | $params->raw = true; |
||
958 | |||
959 | $value = false; |
||
960 | |||
961 | $row_key = sprintf( '_%s_%s', $params->output, $params->name ); |
||
962 | |||
963 | if ( 'arrays' !== $params->output && isset( $this->row[ $row_key ] ) ) { |
||
964 | $value = $this->row[ '_' . $params->output . '_' . $params->name ]; |
||
965 | } elseif ( 'arrays' === $params->output && isset( $this->row[ $params->name ] ) ) { |
||
966 | $value = $this->row[ $params->name ]; |
||
967 | } |
||
968 | |||
969 | if ( false !== $value && ! is_array( $value ) && 'pick' === $field_data['type'] && in_array( $field_data['pick_object'], $simple_tableless_objects, true ) ) { |
||
970 | $value = PodsForm::field_method( 'pick', 'simple_value', $params->name, $value, $field_data, $this->pod_data, $this->id(), true ); |
||
971 | } |
||
972 | } |
||
973 | |||
974 | if ( empty( $value ) && isset( $this->row[ $params->name ] ) && ( ! in_array( $field_data['type'], $tableless_field_types, true ) || 'arrays' === $params->output ) ) { |
||
975 | if ( empty( $field_data ) || in_array( $field_data['type'], array( |
||
976 | 'boolean', |
||
977 | 'number', |
||
978 | 'currency', |
||
979 | ), true ) ) { |
||
980 | $params->raw = true; |
||
981 | } |
||
982 | |||
983 | if ( null === $params->single ) { |
||
984 | if ( isset( $this->fields[ $params->name ] ) && ! in_array( $this->fields[ $params->name ]['type'], $tableless_field_types, true ) ) { |
||
985 | $params->single = true; |
||
986 | } else { |
||
987 | $params->single = false; |
||
988 | } |
||
989 | } |
||
990 | |||
991 | $value = $this->row[ $params->name ]; |
||
992 | } elseif ( empty( $value ) ) { |
||
993 | $object_field_found = false; |
||
994 | |||
995 | if ( 'object_field' === $field_type ) { |
||
996 | $object_field_found = true; |
||
997 | |||
998 | if ( isset( $this->row[ $first_field ] ) ) { |
||
999 | $value = $this->row[ $first_field ]; |
||
1000 | } elseif ( in_array( $field_data['type'], $tableless_field_types, true ) ) { |
||
1001 | $this->fields[ $first_field ] = $field_data; |
||
1002 | |||
1003 | $object_field_found = false; |
||
1004 | } else { |
||
1005 | return null; |
||
1006 | } |
||
1007 | } |
||
1008 | |||
1009 | if ( 'post_type' === $this->pod_data['type'] && ! isset( $this->fields[ $params->name ] ) ) { |
||
1010 | if ( ! isset( $this->fields['post_thumbnail'] ) && ( 'post_thumbnail' === $params->name || 0 === strpos( $params->name, 'post_thumbnail.' ) ) ) { |
||
1011 | $size = 'thumbnail'; |
||
1012 | |||
1013 | if ( 0 === strpos( $params->name, 'post_thumbnail.' ) ) { |
||
1014 | $field_names = explode( '.', $params->name ); |
||
1015 | |||
1016 | if ( isset( $field_names[1] ) ) { |
||
1017 | $size = $field_names[1]; |
||
1018 | } |
||
1019 | } |
||
1020 | |||
1021 | // Pods will auto-get the thumbnail ID if this isn't an attachment. |
||
1022 | $value = pods_image( $this->id(), $size, 0, null, true ); |
||
1023 | |||
1024 | $object_field_found = true; |
||
1025 | } elseif ( ! isset( $this->fields['post_thumbnail_url'] ) && ( 'post_thumbnail_url' === $params->name || 0 === strpos( $params->name, 'post_thumbnail_url.' ) ) ) { |
||
1026 | $size = 'thumbnail'; |
||
1027 | |||
1028 | if ( 0 === strpos( $params->name, 'post_thumbnail_url.' ) ) { |
||
1029 | $field_names = explode( '.', $params->name ); |
||
1030 | |||
1031 | if ( isset( $field_names[1] ) ) { |
||
1032 | $size = $field_names[1]; |
||
1033 | } |
||
1034 | } |
||
1035 | |||
1036 | // Pods will auto-get the thumbnail ID if this isn't an attachment. |
||
1037 | $value = pods_image_url( $this->id(), $size, 0, true ); |
||
1038 | |||
1039 | $object_field_found = true; |
||
1040 | } elseif ( 0 === strpos( $params->name, 'image_attachment.' ) ) { |
||
1041 | $size = 'thumbnail'; |
||
1042 | |||
1043 | $image_id = 0; |
||
1044 | |||
1045 | $field_names = explode( '.', $params->name ); |
||
1046 | |||
1047 | if ( isset( $field_names[1] ) ) { |
||
1048 | $image_id = $field_names[1]; |
||
1049 | } |
||
1050 | |||
1051 | if ( isset( $field_names[2] ) ) { |
||
1052 | $size = $field_names[2]; |
||
1053 | } |
||
1054 | |||
1055 | if ( ! empty( $image_id ) ) { |
||
1056 | $value = pods_image( $image_id, $size, 0, null, true ); |
||
1057 | |||
1058 | if ( ! empty( $value ) ) { |
||
1059 | $object_field_found = true; |
||
1060 | } |
||
1061 | } |
||
1062 | } elseif ( 0 === strpos( $params->name, 'image_attachment_url.' ) ) { |
||
1063 | $size = 'thumbnail'; |
||
1064 | |||
1065 | $image_id = 0; |
||
1066 | |||
1067 | $field_names = explode( '.', $params->name ); |
||
1068 | |||
1069 | if ( isset( $field_names[1] ) ) { |
||
1070 | $image_id = $field_names[1]; |
||
1071 | } |
||
1072 | |||
1073 | if ( isset( $field_names[2] ) ) { |
||
1074 | $size = $field_names[2]; |
||
1075 | } |
||
1076 | |||
1077 | if ( ! empty( $image_id ) ) { |
||
1078 | $value = pods_image_url( $image_id, $size, 0, true ); |
||
1079 | |||
1080 | if ( ! empty( $value ) ) { |
||
1081 | $object_field_found = true; |
||
1082 | } |
||
1083 | } |
||
1084 | }//end if |
||
1085 | } elseif ( 'user' === $this->pod_data['type'] && ! isset( $this->fields[ $params->name ] ) ) { |
||
1086 | if ( ! isset( $this->fields['avatar'] ) && ( 'avatar' === $params->name || 0 === strpos( $params->name, 'avatar.' ) ) ) { |
||
1087 | $size = null; |
||
1088 | |||
1089 | if ( 0 === strpos( $params->name, 'avatar.' ) ) { |
||
1090 | $field_names = explode( '.', $params->name ); |
||
1091 | |||
1092 | if ( isset( $field_names[1] ) ) { |
||
1093 | $size = (int) $field_names[1]; |
||
1094 | } |
||
1095 | } |
||
1096 | |||
1097 | if ( 0 < $size ) { |
||
1098 | $value = get_avatar( $this->id(), $size ); |
||
1099 | } else { |
||
1100 | $value = get_avatar( $this->id() ); |
||
1101 | } |
||
1102 | |||
1103 | $object_field_found = true; |
||
1104 | } |
||
1105 | } elseif ( 0 === strpos( $params->name, 'image_attachment.' ) ) { |
||
1106 | $size = 'thumbnail'; |
||
1107 | |||
1108 | $image_id = 0; |
||
1109 | |||
1110 | $field_names = explode( '.', $params->name ); |
||
1111 | |||
1112 | if ( isset( $field_names[1] ) ) { |
||
1113 | $image_id = $field_names[1]; |
||
1114 | } |
||
1115 | |||
1116 | if ( isset( $field_names[2] ) ) { |
||
1117 | $size = $field_names[2]; |
||
1118 | } |
||
1119 | |||
1120 | if ( ! empty( $image_id ) ) { |
||
1121 | $value = pods_image( $image_id, $size, 0, null, true ); |
||
1122 | |||
1123 | if ( ! empty( $value ) ) { |
||
1124 | $object_field_found = true; |
||
1125 | } |
||
1126 | } |
||
1127 | } elseif ( 0 === strpos( $params->name, 'image_attachment_url.' ) ) { |
||
1128 | $size = 'thumbnail'; |
||
1129 | |||
1130 | $image_id = 0; |
||
1131 | |||
1132 | $field_names = explode( '.', $params->name ); |
||
1133 | |||
1134 | if ( isset( $field_names[1] ) ) { |
||
1135 | $image_id = $field_names[1]; |
||
1136 | } |
||
1137 | |||
1138 | if ( isset( $field_names[2] ) ) { |
||
1139 | $size = $field_names[2]; |
||
1140 | } |
||
1141 | |||
1142 | if ( ! empty( $image_id ) ) { |
||
1143 | $value = pods_image_url( $image_id, $size, 0, true ); |
||
1144 | |||
1145 | if ( ! empty( $value ) ) { |
||
1146 | $object_field_found = true; |
||
1147 | } |
||
1148 | } |
||
1149 | }//end if |
||
1150 | |||
1151 | if ( false === $object_field_found ) { |
||
1152 | $params->traverse = array( $params->name ); |
||
1153 | |||
1154 | if ( false !== strpos( $params->name, '.' ) ) { |
||
1155 | $params->traverse = explode( '.', $params->name ); |
||
1156 | |||
1157 | $params->name = $params->traverse[0]; |
||
1158 | } |
||
1159 | |||
1160 | if ( isset( $this->fields[ $params->name ], $this->fields[ $params->name ]['type'] ) ) { |
||
1161 | /** |
||
1162 | * Modify value returned by field() after its retrieved, but before its validated or formatted |
||
1163 | * |
||
1164 | * Filter name is set dynamically with name of field: "pods_pods_field_{field_name}" |
||
1165 | * |
||
1166 | * @since unknown |
||
1167 | * |
||
1168 | * @param array|string|null $value Value retrieved. |
||
1169 | * @param array|object $row Current row being outputted. |
||
1170 | * @param array $params Params array passed to field(). |
||
1171 | * @param object|Pods $this Current Pods object. |
||
1172 | */ |
||
1173 | $v = apply_filters( 'pods_pods_field_' . $this->fields[ $params->name ]['type'], null, $this->fields[ $params->name ], $this->row, $params, $this ); |
||
1174 | |||
1175 | if ( null !== $v ) { |
||
1176 | return $v; |
||
1177 | } |
||
1178 | } |
||
1179 | |||
1180 | $simple = false; |
||
1181 | |||
1182 | if ( isset( $this->fields[ $params->name ] ) ) { |
||
1183 | if ( 'meta' === $this->pod_data['storage'] && ! in_array( $this->fields[ $params->name ]['type'], $tableless_field_types, true ) ) { |
||
1184 | $simple = true; |
||
1185 | } |
||
1186 | |||
1187 | if ( in_array( $this->fields[ $params->name ]['type'], $tableless_field_types, true ) ) { |
||
1188 | $params->raw = true; |
||
1189 | |||
1190 | if ( 'pick' === $this->fields[ $params->name ]['type'] && in_array( $this->fields[ $params->name ]['pick_object'], $simple_tableless_objects, true ) ) { |
||
1191 | $simple = true; |
||
1192 | $params->single = true; |
||
1193 | } |
||
1194 | } elseif ( in_array( $this->fields[ $params->name ]['type'], array( |
||
1195 | 'boolean', |
||
1196 | 'number', |
||
1197 | 'currency', |
||
1198 | ), true ) ) { |
||
1199 | $params->raw = true; |
||
1200 | } |
||
1201 | } |
||
1202 | |||
1203 | $is_field_set = isset( $this->fields[ $params->name ] ); |
||
1204 | $is_tableless_field = false; |
||
1205 | |||
1206 | if ( $is_field_set ) { |
||
1207 | $is_tableless_field = in_array( $this->fields[ $params->name ]['type'], $tableless_field_types, true ); |
||
1208 | } |
||
1209 | |||
1210 | if ( $simple || ! $is_field_set || ! $is_tableless_field ) { |
||
1211 | if ( null === $params->single ) { |
||
1212 | if ( $is_field_set && ! $is_tableless_field ) { |
||
1213 | $params->single = true; |
||
1214 | } else { |
||
1215 | $params->single = false; |
||
1216 | } |
||
1217 | } |
||
1218 | |||
1219 | $no_conflict = pods_no_conflict_check( $this->pod_data['type'] ); |
||
1220 | |||
1221 | if ( ! $no_conflict ) { |
||
1222 | pods_no_conflict_on( $this->pod_data['type'] ); |
||
1223 | } |
||
1224 | |||
1225 | if ( in_array( $this->pod_data['type'], array( |
||
1226 | 'post_type', |
||
1227 | 'media', |
||
1228 | 'taxonomy', |
||
1229 | 'user', |
||
1230 | 'comment', |
||
1231 | ), true ) ) { |
||
1232 | $id = $this->id(); |
||
1233 | |||
1234 | $metadata_type = $this->pod_data['type']; |
||
1235 | |||
1236 | if ( in_array( $this->pod_data['type'], array( 'post_type', 'media' ), true ) ) { |
||
1237 | $metadata_type = 'post'; |
||
1238 | |||
1239 | // Support for WPML 'duplicated' translation handling. |
||
1240 | if ( did_action( 'wpml_loaded' ) && apply_filters( 'wpml_is_translated_post_type', false, $this->pod_data['name'] ) ) { |
||
1241 | $master_post_id = (int) apply_filters( 'wpml_master_post_from_duplicate', $id ); |
||
1242 | |||
1243 | if ( 0 < $master_post_id ) { |
||
1244 | $id = $master_post_id; |
||
1245 | } |
||
1246 | } |
||
1247 | } elseif ( 'taxonomy' === $this->pod_data['type'] ) { |
||
1248 | $metadata_type = 'term'; |
||
1249 | } |
||
1250 | |||
1251 | $value = get_metadata( $metadata_type, $id, $params->name, $params->single ); |
||
1252 | |||
1253 | $single_multi = 'single'; |
||
1254 | |||
1255 | if ( $is_field_set ) { |
||
1256 | $single_multi = pods_v( $this->fields[ $params->name ]['type'] . '_format_type', $this->fields[ $params->name ]['options'], $single_multi ); |
||
1257 | } |
||
1258 | |||
1259 | if ( $simple && ! is_array( $value ) && 'single' !== $single_multi ) { |
||
1260 | $value = get_metadata( $metadata_type, $id, $params->name ); |
||
1261 | } |
||
1262 | } elseif ( 'settings' === $this->pod_data['type'] ) { |
||
1263 | $value = get_option( $this->pod_data['name'] . '_' . $params->name, null ); |
||
1264 | }//end if |
||
1265 | |||
1266 | // Handle Simple Relationships. |
||
1267 | if ( $simple ) { |
||
1268 | if ( null === $params->single ) { |
||
1269 | $params->single = false; |
||
1270 | } |
||
1271 | |||
1272 | $value = PodsForm::field_method( 'pick', 'simple_value', $params->name, $value, $this->fields[ $params->name ], $this->pod_data, $this->id(), true ); |
||
1273 | } |
||
1274 | |||
1275 | if ( ! $no_conflict ) { |
||
1276 | pods_no_conflict_off( $this->pod_data['type'] ); |
||
1277 | } |
||
1278 | } else { |
||
1279 | // Dot-traversal. |
||
1280 | $pod = $this->pod; |
||
1281 | $ids = array( $this->id() ); |
||
1282 | $all_fields = array(); |
||
1283 | |||
1284 | $lookup = $params->traverse; |
||
1285 | |||
1286 | // Get fields matching traversal names. |
||
1287 | if ( ! empty( $lookup ) ) { |
||
1288 | $fields = $this->api->load_fields( array( |
||
1289 | 'name' => $lookup, |
||
1290 | 'type' => $tableless_field_types, |
||
1291 | 'object_fields' => true, |
||
1292 | // @todo support object fields too. |
||
1293 | ) ); |
||
1294 | |||
1295 | if ( ! empty( $fields ) ) { |
||
1296 | foreach ( $fields as $field ) { |
||
1297 | if ( ! empty( $field ) ) { |
||
1298 | if ( ! isset( $all_fields[ $field['pod'] ] ) ) { |
||
1299 | $all_fields[ $field['pod'] ] = array(); |
||
1300 | } |
||
1301 | |||
1302 | $all_fields[ $field['pod'] ][ $field['name'] ] = $field; |
||
1303 | } |
||
1304 | } |
||
1305 | } |
||
1306 | |||
1307 | if ( ! empty( $this->pod_data['object_fields'] ) ) { |
||
1308 | $object_fields = (array) $this->pod_data['object_fields']; |
||
1309 | |||
1310 | foreach ( $object_fields as $object_field => $object_field_opt ) { |
||
1311 | if ( in_array( $object_field_opt['type'], $tableless_field_types, true ) ) { |
||
1312 | $all_fields[ $this->pod ][ $object_field ] = $object_field_opt; |
||
1313 | } |
||
1314 | } |
||
1315 | } |
||
1316 | }//end if |
||
1317 | |||
1318 | $last_type = ''; |
||
1319 | $last_object = ''; |
||
1320 | $last_pick_val = ''; |
||
1321 | |||
1322 | $single_multi = pods_v( $this->fields[ $params->name ]['type'] . '_format_type', $this->fields[ $params->name ]['options'], 'single' ); |
||
1323 | |||
1324 | if ( 'multi' === $single_multi ) { |
||
1325 | $limit = (int) pods_v( $this->fields[ $params->name ]['type'] . '_limit', $this->fields[ $params->name ]['options'], 0 ); |
||
1326 | } else { |
||
1327 | $limit = 1; |
||
1328 | } |
||
1329 | |||
1330 | // Loop through each traversal level. |
||
1331 | foreach ( $params->traverse as $key => $field ) { |
||
1332 | $last_loop = false; |
||
1333 | |||
1334 | if ( count( $params->traverse ) <= ( $key + 1 ) ) { |
||
1335 | $last_loop = true; |
||
1336 | } |
||
1337 | |||
1338 | $field_exists = isset( $all_fields[ $pod ][ $field ] ); |
||
1339 | |||
1340 | $simple = false; |
||
1341 | $last_options = array(); |
||
1342 | |||
1343 | if ( $field_exists && 'pick' === $all_fields[ $pod ][ $field ]['type'] && in_array( $all_fields[ $pod ][ $field ]['pick_object'], $simple_tableless_objects, true ) ) { |
||
1344 | $simple = true; |
||
1345 | $last_options = $all_fields[ $pod ][ $field ]; |
||
1346 | } |
||
1347 | |||
1348 | // Tableless handler. |
||
1349 | if ( $field_exists && ( ! $simple || ! in_array( $all_fields[ $pod ][ $field ]['type'], array( |
||
1350 | 'pick', |
||
1351 | 'taxonomy', |
||
1352 | 'comment', |
||
1353 | ), true ) ) ) { |
||
1354 | $type = $all_fields[ $pod ][ $field ]['type']; |
||
1355 | $pick_object = $all_fields[ $pod ][ $field ]['pick_object']; |
||
1356 | $pick_val = $all_fields[ $pod ][ $field ]['pick_val']; |
||
1357 | |||
1358 | if ( 'table' === $pick_object ) { |
||
1359 | $pick_val = pods_v( 'pick_table', $all_fields[ $pod ][ $field ]['options'], $pick_val, true ); |
||
1360 | } elseif ( '__current__' === $pick_val ) { |
||
1361 | $pick_val = $pod; |
||
1362 | } |
||
1363 | |||
1364 | $last_limit = 0; |
||
1365 | |||
1366 | if ( in_array( $type, $tableless_field_types, true ) ) { |
||
1367 | $single_multi = pods_v( "{$type}_format_type", $all_fields[ $pod ][ $field ]['options'], 'single' ); |
||
1368 | |||
1369 | if ( 'multi' === $single_multi ) { |
||
1370 | $last_limit = (int) pods_v( "{$type}_limit", $all_fields[ $pod ][ $field ]['options'], 0 ); |
||
1371 | } else { |
||
1372 | $last_limit = 1; |
||
1373 | } |
||
1374 | } |
||
1375 | |||
1376 | $last_type = $type; |
||
1377 | $last_object = $pick_object; |
||
1378 | $last_pick_val = $pick_val; |
||
1379 | $last_options = $all_fields[ $pod ][ $field ]; |
||
1380 | |||
1381 | // Temporary hack until there's some better handling here. |
||
1382 | $last_limit *= count( $ids ); |
||
1383 | |||
1384 | // Get related IDs. |
||
1385 | if ( ! isset( $all_fields[ $pod ][ $field ]['pod_id'] ) ) { |
||
1386 | $all_fields[ $pod ][ $field ]['pod_id'] = 0; |
||
1387 | } |
||
1388 | |||
1389 | if ( isset( $all_fields[ $pod ][ $field ]['id'] ) ) { |
||
1390 | $ids = $this->api->lookup_related_items( $all_fields[ $pod ][ $field ]['id'], $all_fields[ $pod ][ $field ]['pod_id'], $ids, $all_fields[ $pod ][ $field ] ); |
||
1391 | } |
||
1392 | |||
1393 | // No items found. |
||
1394 | if ( empty( $ids ) ) { |
||
1395 | return false; |
||
1396 | } elseif ( 0 < $last_limit ) { |
||
1397 | // @todo This should return array() if not $params->single. |
||
1398 | $ids = array_slice( $ids, 0, $last_limit ); |
||
1399 | } |
||
1400 | |||
1401 | // Get $pod if related to a Pod. |
||
1402 | if ( ! empty( $pick_object ) && ( ! empty( $pick_val ) || in_array( $pick_object, array( |
||
1403 | 'user', |
||
1404 | 'media', |
||
1405 | 'comment', |
||
1406 | ), true ) ) ) { |
||
1407 | if ( 'pod' === $pick_object ) { |
||
1408 | $pod = $pick_val; |
||
1409 | } else { |
||
1410 | $check = $this->api->get_table_info( $pick_object, $pick_val ); |
||
1411 | |||
1412 | if ( ! empty( $check ) && ! empty( $check['pod'] ) ) { |
||
1413 | $pod = $check['pod']['name']; |
||
1414 | } |
||
1415 | } |
||
1416 | } |
||
1417 | } else { |
||
1418 | // Assume last iteration. |
||
1419 | if ( 0 === $key ) { |
||
1420 | // Invalid field. |
||
1421 | return false; |
||
1422 | } |
||
1423 | |||
1424 | $last_loop = true; |
||
1425 | }//end if |
||
1426 | |||
1427 | if ( $last_loop ) { |
||
1428 | $object_type = $last_object; |
||
1429 | $object = $last_pick_val; |
||
1430 | |||
1431 | if ( in_array( $last_type, PodsForm::file_field_types(), true ) ) { |
||
1432 | $object_type = 'media'; |
||
1433 | $object = 'attachment'; |
||
1434 | } |
||
1435 | |||
1436 | $data = array(); |
||
1437 | |||
1438 | $table = $this->api->get_table_info( $object_type, $object, null, null, $last_options ); |
||
1439 | |||
1440 | $join = array(); |
||
1441 | $where = array(); |
||
1442 | |||
1443 | if ( ! empty( $table['join'] ) ) { |
||
1444 | $join = (array) $table['join']; |
||
1445 | } |
||
1446 | |||
1447 | if ( ! empty( $ids ) || ! empty( $table['where'] ) ) { |
||
1448 | foreach ( $ids as $id ) { |
||
1449 | $where[ $id ] = '`t`.`' . $table['field_id'] . '` = ' . (int) $id; |
||
1450 | } |
||
1451 | |||
1452 | if ( ! empty( $where ) ) { |
||
1453 | $where = array( implode( ' OR ', $where ) ); |
||
1454 | } |
||
1455 | |||
1456 | if ( ! empty( $table['where'] ) ) { |
||
1457 | // @codingStandardsIgnoreLine. |
||
1458 | $where = array_merge( $where, array_values( (array) $table['where'] ) ); |
||
1459 | } |
||
1460 | } |
||
1461 | |||
1462 | /** |
||
1463 | * Related object. |
||
1464 | * |
||
1465 | * @var $related_obj Pods|false |
||
1466 | */ |
||
1467 | $related_obj = false; |
||
1468 | |||
1469 | // Check if we can return the full object/array or if we need to traverse into it. |
||
1470 | $is_field_output_full = false; |
||
1471 | |||
1472 | if ( false !== $field_exists && ( in_array( $last_type, $tableless_field_types, true ) && ! $simple ) ) { |
||
1473 | $is_field_output_full = true; |
||
1474 | } |
||
1475 | |||
1476 | if ( 'pod' === $object_type ) { |
||
1477 | $related_obj = pods( $object, null, false ); |
||
1478 | } elseif ( ! empty( $table['pod'] ) ) { |
||
1479 | $related_obj = pods( $table['pod']['name'], null, false ); |
||
1480 | } |
||
1481 | |||
1482 | if ( $related_obj || ! empty( $table['table'] ) ) { |
||
1483 | $sql = array( |
||
1484 | 'select' => '*, `t`.`' . $table['field_id'] . '` AS `pod_item_id`', |
||
1485 | 'table' => $table['table'], |
||
1486 | 'join' => $join, |
||
1487 | 'where' => $where, |
||
1488 | 'orderby' => $params->orderby, |
||
1489 | 'pagination' => false, |
||
1490 | 'search' => false, |
||
1491 | 'limit' => - 1, |
||
1492 | 'expires' => 180, |
||
1493 | // @todo This could potentially cause issues if someone changes the data within this time and persistent storage is used. |
||
1494 | ); |
||
1495 | |||
1496 | if ( ! empty( $table['where_default'] ) ) { |
||
1497 | $sql['where_default'] = $table['where_default']; |
||
1498 | } |
||
1499 | |||
1500 | // Output types. |
||
1501 | if ( in_array( $params->output, array( 'ids', 'objects', 'pods' ), true ) ) { |
||
1502 | $sql['select'] = '`t`.`' . $table['field_id'] . '` AS `pod_item_id`'; |
||
1503 | } elseif ( 'names' === $params->output && ! empty( $table['field_index'] ) ) { |
||
1504 | $sql['select'] = '`t`.`' . $table['field_index'] . '` AS `pod_item_index`, `t`.`' . $table['field_id'] . '` AS `pod_item_id`'; |
||
1505 | } |
||
1506 | |||
1507 | if ( ! empty( $params->params ) && is_array( $params->params ) ) { |
||
1508 | $where = $sql['where']; |
||
1509 | |||
1510 | // @codingStandardsIgnoreLine. |
||
1511 | $sql = array_merge( $sql, $params->params ); |
||
1512 | |||
1513 | if ( isset( $params->params['where'] ) ) { |
||
1514 | // @codingStandardsIgnoreLine. |
||
1515 | $sql['where'] = array_merge( (array) $where, (array) $params->params['where'] ); |
||
1516 | } |
||
1517 | } |
||
1518 | |||
1519 | $item_data = array(); |
||
1520 | |||
1521 | if ( ! $related_obj ) { |
||
1522 | if ( ! is_object( $this->alt_data ) ) { |
||
1523 | $this->alt_data = pods_data( null, 0, true, true ); |
||
1524 | } |
||
1525 | |||
1526 | $item_data = $this->alt_data->select( $sql ); |
||
1527 | } else { |
||
1528 | // Support 'find' output ordering. |
||
1529 | if ( $ids && 'find' === $params->output && $is_field_output_full && empty( $sql['orderby'] ) ) { |
||
1530 | // Handle default orderby for ordering by the IDs. |
||
1531 | $order_ids = implode( ', ', array_map( 'absint', $ids ) ); |
||
1532 | |||
1533 | $sql['orderby'] = 'FIELD( `t`.`' . $table['field_id'] . '`, ' . $order_ids . ' )'; |
||
1534 | } |
||
1535 | |||
1536 | $related_obj->find( $sql ); |
||
1537 | |||
1538 | // Support 'find' output. |
||
1539 | if ( 'find' === $params->output && $is_field_output_full ) { |
||
1540 | $data = $related_obj; |
||
1541 | |||
1542 | $is_field_output_full = true; |
||
1543 | } else { |
||
1544 | $item_data = $related_obj->data(); |
||
1545 | } |
||
1546 | }//end if |
||
1547 | |||
1548 | $items = array(); |
||
1549 | |||
1550 | if ( ! empty( $item_data ) ) { |
||
1551 | foreach ( $item_data as $item ) { |
||
1552 | if ( is_array( $item ) ) { |
||
1553 | $item = (object) $item; |
||
1554 | } |
||
1555 | |||
1556 | if ( empty( $item->pod_item_id ) ) { |
||
1557 | continue; |
||
1558 | } |
||
1559 | |||
1560 | // Bypass pass field. |
||
1561 | if ( isset( $item->user_pass ) ) { |
||
1562 | unset( $item->user_pass ); |
||
1563 | } |
||
1564 | |||
1565 | // Get Item ID. |
||
1566 | $item_id = $item->pod_item_id; |
||
1567 | |||
1568 | // Output types. |
||
1569 | if ( 'ids' === $params->output ) { |
||
1570 | $item = (int) $item_id; |
||
1571 | } elseif ( 'names' === $params->output && ! empty( $table['field_index'] ) ) { |
||
1572 | $item = $item->pod_item_index; |
||
1573 | } elseif ( 'objects' === $params->output ) { |
||
1574 | if ( in_array( $object_type, array( 'post_type', 'media' ), true ) ) { |
||
1575 | $item = get_post( $item_id ); |
||
1576 | } elseif ( 'taxonomy' === $object_type ) { |
||
1577 | $item = get_term( $item_id, $object ); |
||
1578 | } elseif ( 'user' === $object_type ) { |
||
1579 | $item = get_userdata( $item_id ); |
||
1580 | |||
1581 | if ( ! empty( $item ) ) { |
||
1582 | // Get other vars. |
||
1583 | $roles = $item->roles; |
||
1584 | $caps = $item->caps; |
||
1585 | $allcaps = $item->allcaps; |
||
1586 | |||
1587 | $item = $item->data; |
||
1588 | |||
1589 | // Set other vars. |
||
1590 | $item->roles = $roles; |
||
1591 | $item->caps = $caps; |
||
1592 | $item->allcaps = $allcaps; |
||
1593 | |||
1594 | unset( $item->user_pass ); |
||
1595 | } |
||
1596 | } elseif ( 'comment' === $object_type ) { |
||
1597 | $item = get_comment( $item_id ); |
||
1598 | } else { |
||
1599 | $item = (object) $item; |
||
1600 | }//end if |
||
1601 | } elseif ( 'pods' === $params->output ) { |
||
1602 | if ( in_array( $object_type, array( 'user', 'media' ), true ) ) { |
||
1603 | $item = pods( $object_type, (int) $item_id ); |
||
1604 | } else { |
||
1605 | $item = pods( $object, (int) $item_id ); |
||
1606 | } |
||
1607 | } else { |
||
1608 | // arrays. |
||
1609 | $item = get_object_vars( (object) $item ); |
||
1610 | }//end if |
||
1611 | |||
1612 | // Pass item data into $data. |
||
1613 | $items[ $item_id ] = $item; |
||
1614 | }//end foreach |
||
1615 | |||
1616 | // Cleanup. |
||
1617 | unset( $item_data ); |
||
1618 | |||
1619 | // Return all of the data in the order expected. |
||
1620 | if ( empty( $params->orderby ) ) { |
||
1621 | foreach ( $ids as $id ) { |
||
1622 | if ( isset( $items[ $id ] ) ) { |
||
1623 | $data[ $id ] = $items[ $id ]; |
||
1624 | } |
||
1625 | } |
||
1626 | } else { |
||
1627 | // Use order set by orderby. |
||
1628 | foreach ( $items as $id => $v ) { |
||
1629 | // @codingStandardsIgnoreLine. |
||
1630 | if ( in_array( $id, $ids ) ) { |
||
1631 | $data[ $id ] = $v; |
||
1632 | } |
||
1633 | } |
||
1634 | } |
||
1635 | }//end if |
||
1636 | }//end if |
||
1637 | |||
1638 | if ( in_array( $last_type, $tableless_field_types, true ) || in_array( $last_type, array( |
||
1639 | 'boolean', |
||
1640 | 'number', |
||
1641 | 'currency', |
||
1642 | ), true ) ) { |
||
1643 | $params->raw = true; |
||
1644 | } |
||
1645 | |||
1646 | if ( empty( $data ) ) { |
||
1647 | $value = false; |
||
1648 | } else { |
||
1649 | $object_type = $table['type']; |
||
1650 | |||
1651 | if ( in_array( $table['type'], array( 'post_type', 'attachment', 'media' ), true ) ) { |
||
1652 | $object_type = 'post'; |
||
1653 | } |
||
1654 | |||
1655 | $no_conflict = true; |
||
1656 | |||
1657 | if ( in_array( $object_type, array( |
||
1658 | 'post', |
||
1659 | 'taxonomy', |
||
1660 | 'user', |
||
1661 | 'comment', |
||
1662 | 'settings', |
||
1663 | ), true ) ) { |
||
1664 | $no_conflict = pods_no_conflict_check( $object_type ); |
||
1665 | |||
1666 | if ( ! $no_conflict ) { |
||
1667 | pods_no_conflict_on( $object_type ); |
||
1668 | } |
||
1669 | } |
||
1670 | |||
1671 | if ( $is_field_output_full ) { |
||
1672 | // Return entire array. |
||
1673 | $value = $data; |
||
1674 | } else { |
||
1675 | // Return an array of single column values. |
||
1676 | $value = array(); |
||
1677 | |||
1678 | foreach ( $data as $item_id => $item ) { |
||
1679 | // $field is 123x123, needs to be _src.123x123 |
||
1680 | $full_field = implode( '.', array_splice( $params->traverse, $key ) ); |
||
1681 | |||
1682 | if ( is_array( $item ) && isset( $item[ $field ] ) ) { |
||
1683 | if ( $table['field_id'] === $field ) { |
||
1684 | $value[] = (int) $item[ $field ]; |
||
1685 | } else { |
||
1686 | $value[] = $item[ $field ]; |
||
1687 | } |
||
1688 | } elseif ( is_object( $item ) && isset( $item->{$field} ) ) { |
||
1689 | if ( $table['field_id'] === $field ) { |
||
1690 | $value[] = (int) $item->{$field}; |
||
1691 | } else { |
||
1692 | $value[] = $item->{$field}; |
||
1693 | } |
||
1694 | } elseif ( ! empty( $related_obj ) && 0 === strpos( $full_field, 'post_thumbnail' ) ) { |
||
1695 | // We want to catch post_thumbnail and post_thumbnail_url here |
||
1696 | $value[] = $related_obj->field( $full_field ); |
||
1697 | } elseif ( ( ( false !== strpos( $full_field, '_src' ) || 'guid' === $field ) && ( in_array( $table['type'], array( |
||
1698 | 'attachment', |
||
1699 | 'media', |
||
1700 | ), true ) || in_array( $last_type, PodsForm::file_field_types(), true ) ) ) || ( in_array( $field, array( |
||
1701 | '_link', |
||
1702 | 'detail_url', |
||
1703 | ), true ) || in_array( $field, array( |
||
1704 | 'permalink', |
||
1705 | 'the_permalink', |
||
1706 | ), true ) && in_array( $last_type, PodsForm::file_field_types(), true ) ) ) { |
||
1707 | // @todo Refactor the above condition statement. |
||
1708 | $size = 'full'; |
||
1709 | |||
1710 | if ( false === strpos( 'image', get_post_mime_type( $item_id ) ) ) { |
||
1711 | // No default sizes for non-images. |
||
1712 | // When a size is defined this will be overwritten. |
||
1713 | $size = null; |
||
1714 | } |
||
1715 | |||
1716 | if ( false !== strpos( $full_field, '_src.' ) && 5 < strlen( $full_field ) ) { |
||
1717 | $size = substr( $full_field, 5 ); |
||
1718 | } elseif ( false !== strpos( $full_field, '_src_relative.' ) && 14 < strlen( $full_field ) ) { |
||
1719 | $size = substr( $full_field, 14 ); |
||
1720 | } elseif ( false !== strpos( $full_field, '_src_schemeless.' ) && 16 < strlen( $full_field ) ) { |
||
1721 | $size = substr( $full_field, 16 ); |
||
1722 | } |
||
1723 | |||
1724 | if ( $size ) { |
||
1725 | $value_url = pods_image_url( $item_id, $size ); |
||
1726 | } else { |
||
1727 | $value_url = wp_get_attachment_url( $item_id ); |
||
1728 | } |
||
1729 | |||
1730 | if ( ! empty( $value_url ) ) { |
||
1731 | if ( false !== strpos( $full_field, '_src_relative' ) ) { |
||
1732 | $value_url_parsed = wp_parse_url( $value_url ); |
||
1733 | $value_url = $value_url_parsed['path']; |
||
1734 | } elseif ( false !== strpos( $full_field, '_src_schemeless' ) ) { |
||
1735 | $value_url = str_replace( array( |
||
1736 | 'http://', |
||
1737 | 'https://', |
||
1738 | ), '//', $value_url ); |
||
1739 | } |
||
1740 | } |
||
1741 | |||
1742 | if ( ! empty( $value_url ) ) { |
||
1743 | $value[] = $value_url; |
||
1744 | } |
||
1745 | |||
1746 | $params->raw_display = true; |
||
1747 | } elseif ( false !== strpos( $full_field, '_img' ) && ( in_array( $table['type'], array( |
||
1748 | 'attachment', |
||
1749 | 'media', |
||
1750 | ), true ) || in_array( $last_type, PodsForm::file_field_types(), true ) ) ) { |
||
1751 | $size = 'full'; |
||
1752 | |||
1753 | if ( false !== strpos( $full_field, '_img.' ) && 5 < strlen( $full_field ) ) { |
||
1754 | $size = substr( $full_field, 5 ); |
||
1755 | } |
||
1756 | |||
1757 | $value[] = pods_image( $item_id, $size ); |
||
1758 | |||
1759 | $params->raw_display = true; |
||
1760 | } elseif ( in_array( $field, array( |
||
1761 | '_link', |
||
1762 | 'detail_url', |
||
1763 | ), true ) || in_array( $field, array( 'permalink', 'the_permalink' ), true ) ) { |
||
1764 | if ( 'pod' === $object_type ) { |
||
1765 | if ( is_object( $related_obj ) ) { |
||
1766 | $related_obj->fetch( $item_id ); |
||
1767 | |||
1768 | $value[] = $related_obj->field( 'detail_url' ); |
||
1769 | } else { |
||
1770 | $value[] = ''; |
||
1771 | } |
||
1772 | } elseif ( 'post' === $object_type ) { |
||
1773 | $value[] = get_permalink( $item_id ); |
||
1774 | } elseif ( 'taxonomy' === $object_type ) { |
||
1775 | $value[] = get_term_link( $item_id, $object ); |
||
1776 | } elseif ( 'user' === $object_type ) { |
||
1777 | $value[] = get_author_posts_url( $item_id ); |
||
1778 | } elseif ( 'comment' === $object_type ) { |
||
1779 | $value[] = get_comment_link( $item_id ); |
||
1780 | } else { |
||
1781 | $value[] = ''; |
||
1782 | } |
||
1783 | |||
1784 | $params->raw_display = true; |
||
1785 | } elseif ( in_array( $object_type, array( |
||
1786 | 'post', |
||
1787 | 'taxonomy', |
||
1788 | 'user', |
||
1789 | 'comment', |
||
1790 | ), true ) ) { |
||
1791 | $metadata_object_id = $item_id; |
||
1792 | |||
1793 | $metadata_type = $object_type; |
||
1794 | |||
1795 | if ( 'post' === $object_type ) { |
||
1796 | // Support for WPML 'duplicated' translation handling. |
||
1797 | if ( did_action( 'wpml_loaded' ) && apply_filters( 'wpml_is_translated_post_type', false, $object ) ) { |
||
1798 | $master_post_id = (int) apply_filters( 'wpml_master_post_from_duplicate', $metadata_object_id ); |
||
1799 | |||
1800 | if ( 0 < $master_post_id ) { |
||
1801 | $metadata_object_id = $master_post_id; |
||
1802 | } |
||
1803 | } |
||
1804 | } elseif ( 'taxonomy' === $object_type ) { |
||
1805 | $metadata_type = 'term'; |
||
1806 | } |
||
1807 | |||
1808 | $value[] = get_metadata( $metadata_type, $metadata_object_id, $field, true ); |
||
1809 | } elseif ( 'settings' === $object_type ) { |
||
1810 | $value[] = get_option( $object . '_' . $field ); |
||
1811 | }//end if |
||
1812 | }//end foreach |
||
1813 | }//end if |
||
1814 | |||
1815 | if ( ! $no_conflict && in_array( $object_type, array( |
||
1816 | 'post', |
||
1817 | 'taxonomy', |
||
1818 | 'user', |
||
1819 | 'comment', |
||
1820 | 'settings', |
||
1821 | ), true ) ) { |
||
1822 | pods_no_conflict_off( $object_type ); |
||
1823 | } |
||
1824 | |||
1825 | // Handle Simple Relationships. |
||
1826 | if ( $simple ) { |
||
1827 | if ( null === $params->single ) { |
||
1828 | $params->single = false; |
||
1829 | } |
||
1830 | |||
1831 | $value = PodsForm::field_method( 'pick', 'simple_value', $field, $value, $last_options, $all_fields[ $pod ], 0, true ); |
||
1832 | } elseif ( false === $params->in_form && ! empty( $value ) && is_array( $value ) ) { |
||
1833 | $value = array_values( $value ); |
||
1834 | } |
||
1835 | |||
1836 | // Return a single column value. |
||
1837 | if ( false === $params->in_form && 1 === $limit && ! empty( $value ) && is_array( $value ) && 1 === count( $value ) ) { |
||
1838 | $value = current( $value ); |
||
1839 | } |
||
1840 | }//end if |
||
1841 | |||
1842 | if ( $last_options ) { |
||
1843 | $last_field_data = $last_options; |
||
1844 | } |
||
1845 | |||
1846 | break; |
||
1847 | }//end if |
||
1848 | }//end foreach |
||
1849 | }//end if |
||
1850 | }//end if |
||
1851 | }//end if |
||
1852 | |||
1853 | if ( ! empty( $params->traverse ) && 1 < count( $params->traverse ) ) { |
||
1854 | $field_names = implode( '.', $params->traverse ); |
||
1855 | |||
1856 | $this->row[ $field_names ] = $value; |
||
1857 | } elseif ( 'arrays' !== $params->output && in_array( $field_data['type'], $tableless_field_types, true ) ) { |
||
1858 | $this->row[ '_' . $params->output . '_' . $params->full_name ] = $value; |
||
1859 | } elseif ( 'arrays' === $params->output || ! in_array( $field_data['type'], $tableless_field_types, true ) ) { |
||
1860 | $this->row[ $params->full_name ] = $value; |
||
1861 | } |
||
1862 | |||
1863 | if ( true === $params->single && is_array( $value ) && 1 === count( $value ) ) { |
||
1864 | $value = current( $value ); |
||
1865 | } |
||
1866 | |||
1867 | if ( ! empty( $last_field_data ) ) { |
||
1868 | $field_data = $last_field_data; |
||
1869 | } |
||
1870 | |||
1871 | // @todo Expand this into traversed fields too. |
||
1872 | if ( ! empty( $field_data ) && ( $params->display || ! $params->raw ) && ! $params->in_form && ! $params->raw_display ) { |
||
1873 | if ( $params->display || ( ( $params->get_meta || $params->deprecated ) && ! in_array( $field_data['type'], $tableless_field_types, true ) ) ) { |
||
1874 | $field_data['options'] = pods_v( 'options', $field_data, array(), true ); |
||
1875 | |||
1876 | $post_temp = false; |
||
1877 | $old_post = null; |
||
1878 | $old_post_id = null; |
||
1879 | |||
1880 | if ( empty( $GLOBALS['post'] ) && 'post_type' === pods_v( 'type', $this->pod_data ) && 0 < $this->id() ) { |
||
1881 | global $post_ID, $post; |
||
1882 | |||
1883 | $post_temp = true; |
||
1884 | |||
1885 | $old_post = $post; |
||
1886 | $old_post_id = $post_ID; |
||
1887 | |||
1888 | // @codingStandardsIgnoreLine. |
||
1889 | $post = get_post( $this->id() ); |
||
1890 | // @codingStandardsIgnoreLine. |
||
1891 | $post_ID = $this->id(); |
||
1892 | } |
||
1893 | |||
1894 | $filter = pods_v( 'display_filter', $field_data['options'] ); |
||
1895 | |||
1896 | if ( 0 < strlen( $filter ) ) { |
||
1897 | $args = array( |
||
1898 | $filter, |
||
1899 | $value, |
||
1900 | ); |
||
1901 | |||
1902 | $filter_args = pods_v( 'display_filter_args', $field_data['options'] ); |
||
1903 | |||
1904 | if ( ! empty( $filter_args ) ) { |
||
1905 | $args = array_merge( $args, compact( $filter_args ) ); |
||
1906 | } |
||
1907 | |||
1908 | $value = call_user_func_array( 'apply_filters', $args ); |
||
1909 | } elseif ( 1 === (int) pods_v( 'display_process', $field_data['options'], 1 ) ) { |
||
1910 | $value = PodsForm::display( $field_data['type'], $value, $params->name, array_merge( $field_data, $field_data['options'] ), $this->pod_data, $this->id() ); |
||
1911 | } |
||
1912 | |||
1913 | if ( $post_temp ) { |
||
1914 | // @codingStandardsIgnoreLine. |
||
1915 | $post = $old_post; |
||
1916 | // @codingStandardsIgnoreLine. |
||
1917 | $post_ID = $old_post_id; |
||
1918 | } |
||
1919 | } else { |
||
1920 | $value = PodsForm::value( $field_data['type'], $value, $params->name, array_merge( $field_data, $field_data['options'] ), $this->pod_data, $this->id() ); |
||
1921 | }//end if |
||
1922 | }//end if |
||
1923 | |||
1924 | /** |
||
1925 | * Modify value returned by field() directly before output. |
||
1926 | * |
||
1927 | * Will not run if value was null |
||
1928 | * |
||
1929 | * @since unknown |
||
1930 | * |
||
1931 | * @param array|string|null $value Value to be returned. |
||
1932 | * @param array|object $row Current row being outputted. |
||
1933 | * @param array $params Params array passed to field(). |
||
1934 | * @param object|Pods $this Current Pods object. |
||
1935 | */ |
||
1936 | $value = apply_filters( 'pods_pods_field', $value, $this->row, $params, $this ); |
||
1937 | |||
1938 | return $value; |
||
1939 | } |
||
1940 | |||
1941 | /** |
||
1942 | * Check if an item field has a specific value in it |
||
1943 | * |
||
1944 | * @param string $field Field name. |
||
1945 | * @param mixed $value Value to check. |
||
1946 | * @param int $id (optional) ID of the pod item to check. |
||
1947 | * |
||
1948 | * @return bool Whether the value was found |
||
1949 | * |
||
1950 | * @since 2.3.3 |
||
1951 | */ |
||
1952 | public function has( $field, $value, $id = null ) { |
||
2020 | |||
2021 | /** |
||
2022 | * Check if an item field is a specific value |
||
2023 | * |
||
2024 | * @param string $field Field name. |
||
2025 | * @param mixed $value Value to check. |
||
2026 | * @param int $id (optional) ID of the pod item to check. |
||
2027 | * |
||
2028 | * @return bool Whether the value was found |
||
2029 | * |
||
2030 | * @since 2.3.3 |
||
2031 | */ |
||
2032 | public function is( $field, $value, $id = null ) { |
||
2143 | |||
2144 | /** |
||
2145 | * Return the item ID |
||
2146 | * |
||
2147 | * @return int |
||
2148 | * @since 2.0 |
||
2149 | */ |
||
2150 | public function id() { |
||
2159 | |||
2160 | /** |
||
2161 | * Return the previous item ID, loops at the last id to return the first |
||
2162 | * |
||
2163 | * @param int|null $id ID to start from. |
||
2164 | * @param array|object|null $params_override Override the find() parameters. |
||
2165 | * |
||
2166 | * @return int |
||
2167 | * @since 2.0 |
||
2168 | */ |
||
2169 | public function prev_id( $id = null, $params_override = null ) { |
||
2248 | |||
2249 | /** |
||
2250 | * Return the next item ID, loops at the first id to return the last |
||
2251 | * |
||
2252 | * @param int|null $id ID to start from. |
||
2253 | * @param array|object|null $params_override Override the find() parameters. |
||
2254 | * |
||
2255 | * @return int |
||
2256 | * @since 2.0 |
||
2257 | */ |
||
2258 | public function next_id( $id = null, $params_override = null ) { |
||
2317 | |||
2318 | /** |
||
2319 | * Return the first item ID |
||
2320 | * |
||
2321 | * @param array|object|null $params_override Override the find() parameters. |
||
2322 | * |
||
2323 | * @return int |
||
2324 | * @since 2.3 |
||
2325 | */ |
||
2326 | public function first_id( $params_override = null ) { |
||
2362 | |||
2363 | /** |
||
2364 | * Return the last item ID |
||
2365 | * |
||
2366 | * @param array|object|null $params_override Override the find() parameters. |
||
2367 | * |
||
2368 | * @return int |
||
2369 | * @since 2.3 |
||
2370 | */ |
||
2371 | public function last_id( $params_override = null ) { |
||
2434 | |||
2435 | /** |
||
2436 | * Return the item name |
||
2437 | * |
||
2438 | * @return string |
||
2439 | * @since 2.0 |
||
2440 | */ |
||
2441 | public function index() { |
||
2445 | |||
2446 | /** |
||
2447 | * Find items of a pod, much like WP_Query, but with advanced table handling. |
||
2448 | * |
||
2449 | * @param array|object $params An associative array of parameters. |
||
2450 | * @param int $limit (deprecated) Limit the number of items to find, -1 to return all items with no limit. |
||
2451 | * @param string $where (deprecated) SQL WHERE declaration to use. |
||
2452 | * @param string $sql (deprecated) For advanced use, a custom SQL query to run. |
||
2453 | * |
||
2454 | * @return \Pods The pod object |
||
2455 | * @since 2.0 |
||
2456 | * @link https://pods.io/docs/find/ |
||
2457 | */ |
||
2458 | public function find( $params = null, $limit = 15, $where = null, $sql = null ) { |
||
2686 | |||
2687 | /** |
||
2688 | * Fetch an item from a Pod. If $id is null, it will return the next item in the list after running find(). |
||
2689 | * You can rewind the list back to the start by using reset(). |
||
2690 | * |
||
2691 | * Providing an $id will fetch a specific item from a Pod, much like a call to pods(), and can handle either an id |
||
2692 | * or slug. |
||
2693 | * |
||
2694 | * @see PodsData::fetch |
||
2695 | * |
||
2696 | * @param int $id ID or slug of the item to fetch. |
||
2697 | * @param bool $explicit_set Whether to set explicitly (use false when in loop). |
||
2698 | * |
||
2699 | * @return array An array of fields from the row |
||
2700 | * |
||
2701 | * @since 2.0 |
||
2702 | * @link https://pods.io/docs/fetch/ |
||
2703 | */ |
||
2704 | public function fetch( $id = null, $explicit_set = true ) { |
||
2724 | |||
2725 | /** |
||
2726 | * (Re)set the MySQL result pointer |
||
2727 | * |
||
2728 | * @see PodsData::reset |
||
2729 | * |
||
2730 | * @param int $row ID of the row to reset to. |
||
2731 | * |
||
2732 | * @return \Pods The pod object |
||
2733 | * |
||
2734 | * @since 2.0 |
||
2735 | * @link https://pods.io/docs/reset/ |
||
2736 | */ |
||
2737 | public function reset( $row = null ) { |
||
2753 | |||
2754 | /** |
||
2755 | * Fetch the total row count returned by the last call to find(), based on the 'limit' parameter set. |
||
2756 | * |
||
2757 | * This is different than the total number of rows found in the database, which you can get with total_found(). |
||
2758 | * |
||
2759 | * @see PodsData::total |
||
2760 | * |
||
2761 | * @return int Number of rows returned by find(), based on the 'limit' parameter set |
||
2762 | * @since 2.0 |
||
2763 | * @link https://pods.io/docs/total/ |
||
2764 | */ |
||
2765 | public function total() { |
||
2775 | |||
2776 | /** |
||
2777 | * Fetch the total amount of rows found by the last call to find(), regardless of the 'limit' parameter set. |
||
2778 | * |
||
2779 | * This is different than the total number of rows limited by the current call, which you can get with total(). |
||
2780 | * |
||
2781 | * @see PodsData::total_found |
||
2782 | * |
||
2783 | * @return int Number of rows returned by find(), regardless of the 'limit' parameter |
||
2784 | * @since 2.0 |
||
2785 | * @link https://pods.io/docs/total-found/ |
||
2786 | */ |
||
2787 | public function total_found() { |
||
2804 | |||
2805 | /** |
||
2806 | * Fetch the total number of pages, based on total rows found and the last find() limit |
||
2807 | * |
||
2808 | * @param null|int $limit Rows per page. |
||
2809 | * @param null|int $offset Offset of rows. |
||
2810 | * @param null|int $total Total rows. |
||
2811 | * |
||
2812 | * @return int Number of pages |
||
2813 | * @since 2.3.10 |
||
2814 | */ |
||
2815 | public function total_pages( $limit = null, $offset = null, $total = null ) { |
||
2834 | |||
2835 | /** |
||
2836 | * Fetch the zebra switch |
||
2837 | * |
||
2838 | * @see PodsData::zebra |
||
2839 | * |
||
2840 | * @return bool Zebra state |
||
2841 | * @since 1.12 |
||
2842 | */ |
||
2843 | public function zebra() { |
||
2849 | |||
2850 | /** |
||
2851 | * Fetch the nth state |
||
2852 | * |
||
2853 | * @see PodsData::nth |
||
2854 | * |
||
2855 | * @param int|string $nth The $nth to match on the PodsData::row_number. |
||
2856 | * |
||
2857 | * @return bool Whether $nth matches |
||
2858 | * @since 2.3 |
||
2859 | */ |
||
2860 | public function nth( $nth = null ) { |
||
2866 | |||
2867 | /** |
||
2868 | * Fetch the current position in the loop (starting at 1) |
||
2869 | * |
||
2870 | * @see PodsData::position |
||
2871 | * |
||
2872 | * @return int Current row number (+1) |
||
2873 | * @since 2.3 |
||
2874 | */ |
||
2875 | public function position() { |
||
2881 | |||
2882 | /** |
||
2883 | * Add an item to a Pod by giving an array of field data or set a specific field to |
||
2884 | * a specific value if you're just wanting to add a new item but only set one field. |
||
2885 | * |
||
2886 | * You may be looking for save() in most cases where you're setting a specific field. |
||
2887 | * |
||
2888 | * @see PodsAPI::save_pod_item |
||
2889 | * |
||
2890 | * @param array|string $data Either an associative array of field information or a field name. |
||
2891 | * @param mixed $value (optional) Value of the field, if $data is a field name. |
||
2892 | * |
||
2893 | * @return int The item ID |
||
2894 | * |
||
2895 | * @since 2.0 |
||
2896 | * @link https://pods.io/docs/add/ |
||
2897 | */ |
||
2898 | public function add( $data = null, $value = null ) { |
||
2918 | |||
2919 | /** |
||
2920 | * Add an item to the values of a relationship field, add a value to a number field (field+1), add time to a date |
||
2921 | * field, or add text to a text field |
||
2922 | * |
||
2923 | * @see PodsAPI::save_pod_item |
||
2924 | * |
||
2925 | * @param string $field Field name. |
||
2926 | * @param mixed $value IDs to add, int|float to add to number field, string for dates (+1 day), or string for text. |
||
2927 | * @param int $id (optional) ID of the pod item to update. |
||
2928 | * |
||
2929 | * @return int The item ID |
||
2930 | * |
||
2931 | * @since 2.3 |
||
2932 | */ |
||
2933 | public function add_to( $field, $value, $id = null ) { |
||
3034 | |||
3035 | /** |
||
3036 | * Remove an item from the values of a relationship field, remove a value from a number field (field-1), remove |
||
3037 | * time to a date field |
||
3038 | * |
||
3039 | * @see PodsAPI::save_pod_item |
||
3040 | * |
||
3041 | * @param string $field Field name. |
||
3042 | * @param mixed $value IDs to add, int|float to add to number field, string for dates (-1 day), or string for text. |
||
3043 | * @param int $id (optional) ID of the pod item to update. |
||
3044 | * |
||
3045 | * @return int The item ID |
||
3046 | * |
||
3047 | * @since 2.3.3 |
||
3048 | */ |
||
3049 | public function remove_from( $field, $value = null, $id = null ) { |
||
3172 | |||
3173 | /** |
||
3174 | * Save an item by giving an array of field data or set a specific field to a specific value. |
||
3175 | * |
||
3176 | * Though this function has the capacity to add new items, best practice should direct you |
||
3177 | * to use add() for that instead. |
||
3178 | * |
||
3179 | * @see PodsAPI::save_pod_item |
||
3180 | * |
||
3181 | * @param array|string $data Either an associative array of field information or a field name. |
||
3182 | * @param mixed $value (optional) Value of the field, if $data is a field name. |
||
3183 | * @param int $id (optional) ID of the pod item to update. |
||
3184 | * @param array $params (optional) Additional params to send to save_pod_item. |
||
3185 | * |
||
3186 | * @return int The item ID |
||
3187 | * |
||
3188 | * @since 2.0 |
||
3189 | * @link https://pods.io/docs/save/ |
||
3190 | */ |
||
3191 | public function save( $data = null, $value = null, $id = null, $params = null ) { |
||
3255 | |||
3256 | /** |
||
3257 | * Delete an item |
||
3258 | * |
||
3259 | * @see PodsAPI::delete_pod_item |
||
3260 | * |
||
3261 | * @param int $id ID of the Pod item to delete. |
||
3262 | * |
||
3263 | * @return bool Whether the item was successfully deleted |
||
3264 | * |
||
3265 | * @since 2.0 |
||
3266 | * @link https://pods.io/docs/delete/ |
||
3267 | */ |
||
3268 | public function delete( $id = null ) { |
||
3287 | |||
3288 | /** |
||
3289 | * Reset Pod |
||
3290 | * |
||
3291 | * @see PodsAPI::reset_pod |
||
3292 | * |
||
3293 | * @return bool Whether the Pod was successfully reset |
||
3294 | * |
||
3295 | * @since 2.1.1 |
||
3296 | */ |
||
3297 | public function reset_pod() { |
||
3310 | |||
3311 | /** |
||
3312 | * Duplicate an item |
||
3313 | * |
||
3314 | * @see PodsAPI::duplicate_pod_item |
||
3315 | * |
||
3316 | * @param int $id ID of the pod item to duplicate. |
||
3317 | * |
||
3318 | * @return int|bool ID of the new pod item |
||
3319 | * |
||
3320 | * @since 2.0 |
||
3321 | * @link https://pods.io/docs/duplicate/ |
||
3322 | */ |
||
3323 | public function duplicate( $id = null ) { |
||
3342 | |||
3343 | /** |
||
3344 | * Import data / Save multiple rows of data at once |
||
3345 | * |
||
3346 | * @see PodsAPI::import |
||
3347 | * |
||
3348 | * @param mixed $import_data PHP associative array or CSV input. |
||
3349 | * @param bool $numeric_mode Use IDs instead of the name field when matching. |
||
3350 | * @param string $format Format of import data, options are php or csv. |
||
3351 | * |
||
3352 | * @return array IDs of imported items |
||
3353 | * |
||
3354 | * @since 2.3 |
||
3355 | */ |
||
3356 | public function import( $import_data, $numeric_mode = false, $format = null ) { |
||
3360 | |||
3361 | /** |
||
3362 | * Export an item's data |
||
3363 | * |
||
3364 | * @see PodsAPI::export_pod_item |
||
3365 | * |
||
3366 | * @param array $fields (optional) Fields to export. |
||
3367 | * @param int $id (optional) ID of the pod item to export. |
||
3368 | * @param null|string $format (optional) The format of the export (php | json). |
||
3369 | * |
||
3370 | * @return array|bool Data array of the exported pod item |
||
3371 | * |
||
3372 | * @since 2.0 |
||
3373 | * @link https://pods.io/docs/export/ |
||
3374 | */ |
||
3375 | public function export( $fields = null, $id = null, $format = null ) { |
||
3415 | |||
3416 | /** |
||
3417 | * Export data from all items |
||
3418 | * |
||
3419 | * @see PodsAPI::export |
||
3420 | * |
||
3421 | * @param array $params An associative array of parameters. |
||
3422 | * |
||
3423 | * @return array Data arrays of all exported pod items |
||
3424 | * |
||
3425 | * @since 2.3 |
||
3426 | */ |
||
3427 | public function export_data( $params = null ) { |
||
3443 | |||
3444 | /** |
||
3445 | * Display the pagination controls, types supported by default |
||
3446 | * are simple, paginate and advanced. The base and format parameters |
||
3447 | * are used only for the paginate view. |
||
3448 | * |
||
3449 | * @param array|object $params Associative array of parameters. |
||
3450 | * |
||
3451 | * @return string Pagination HTML |
||
3452 | * @since 2.0 |
||
3453 | * @link https://pods.io/docs/pagination/ |
||
3454 | */ |
||
3455 | public function pagination( $params = null ) { |
||
3523 | |||
3524 | /** |
||
3525 | * Return a filter form for searching a Pod |
||
3526 | * |
||
3527 | * @param array|string $params Comma-separated list of fields or array of parameters. |
||
3528 | * |
||
3529 | * @return string Filters HTML |
||
3530 | * |
||
3531 | * @since 2.0 |
||
3532 | * @link https://pods.io/docs/filters/ |
||
3533 | */ |
||
3534 | public function filters( $params = null ) { |
||
3646 | |||
3647 | /** |
||
3648 | * Run a helper within a Pod Page or WP Template |
||
3649 | * |
||
3650 | * @see Pods_Helpers::helper |
||
3651 | * |
||
3652 | * @param string|array $helper Helper name. |
||
3653 | * @param string $value Value to run the helper on. |
||
3654 | * @param string $name Field name. |
||
3655 | * |
||
3656 | * @return mixed Anything returned by the helper |
||
3657 | * @since 2.0 |
||
3658 | */ |
||
3659 | public function helper( $helper, $value = null, $name = null ) { |
||
3738 | |||
3739 | /** |
||
3740 | * Display the page template |
||
3741 | * |
||
3742 | * @see Pods_Templates::template |
||
3743 | * |
||
3744 | * @param string $template_name The template name. |
||
3745 | * @param string|null $code Custom template code to use instead. |
||
3746 | * @param bool $deprecated Whether to use deprecated functionality based on old function usage. |
||
3747 | * |
||
3748 | * @return mixed Template output |
||
3749 | * |
||
3750 | * @since 2.0 |
||
3751 | * @link https://pods.io/docs/template/ |
||
3752 | */ |
||
3753 | public function template( $template_name, $code = null, $deprecated = false ) { |
||
3869 | |||
3870 | /** |
||
3871 | * Embed a form to add / edit a pod item from within your theme. Provide an array of $fields to include |
||
3872 | * and override options where needed. For WP object based Pods, you can pass through the WP object |
||
3873 | * field names too, such as "post_title" or "post_content" for example. |
||
3874 | * |
||
3875 | * @param array $params (optional) Fields to show on the form, defaults to all fields. |
||
3876 | * @param string $label (optional) Save button label, defaults to "Save Changes". |
||
3877 | * @param string $thank_you (optional) Thank you URL to send to upon success. |
||
3878 | * |
||
3879 | * @return bool|mixed |
||
3880 | * @since 2.0 |
||
3881 | * @link https://pods.io/docs/form/ |
||
3882 | */ |
||
3883 | public function form( $params = null, $label = null, $thank_you = null ) { |
||
4024 | |||
4025 | /** |
||
4026 | * Render a singular view for the Pod item content. |
||
4027 | * |
||
4028 | * @param array|string|null $fields (optional) Fields to show in the view, defaults to all fields. |
||
4029 | * |
||
4030 | * @return mixed |
||
4031 | * @since 2.3.10 |
||
4032 | */ |
||
4033 | public function view( $fields = null ) { |
||
4101 | |||
4102 | /** |
||
4103 | * Replace magic tags with their values |
||
4104 | * |
||
4105 | * @param string $code The content to evaluate. |
||
4106 | * |
||
4107 | * @return string Code with Magic Tags evaluated |
||
4108 | * |
||
4109 | * @since 2.0 |
||
4110 | */ |
||
4111 | public function do_magic_tags( $code ) { |
||
4130 | |||
4131 | /** |
||
4132 | * Replace magic tags with their values |
||
4133 | * |
||
4134 | * @param string|array $tag The magic tag to process. |
||
4135 | * |
||
4136 | * @return string Code with Magic Tags evaluated |
||
4137 | * |
||
4138 | * @since 2.0.2 |
||
4139 | */ |
||
4140 | private function process_magic_tags( $tag ) { |
||
4208 | |||
4209 | /** |
||
4210 | * |
||
4211 | * Generate UI for Data Management |
||
4212 | * |
||
4213 | * @param mixed $options Array or String containing Pod or Options to be used. |
||
4214 | * @param bool $amend Whether to amend the default UI options or replace entirely. |
||
4215 | * |
||
4216 | * @return PodsUI|null UI object or null if custom UI used |
||
4217 | * |
||
4218 | * @since 2.3.10 |
||
4219 | */ |
||
4220 | public function ui( $options = null, $amend = false ) { |
||
4463 | |||
4464 | /** |
||
4465 | * Handle filters / actions for the class |
||
4466 | * |
||
4467 | * @see pods_do_hook |
||
4468 | * |
||
4469 | * @param string $name Hook name. |
||
4470 | * |
||
4471 | * @return mixed Value filtered |
||
4472 | * |
||
4473 | * @since 2.0 |
||
4474 | */ |
||
4475 | private function do_hook( $name ) { |
||
4488 | |||
4489 | /** |
||
4490 | * Handle variables that have been deprecated and PodsData vars |
||
4491 | * |
||
4492 | * @param string $name Property name. |
||
4493 | * |
||
4494 | * @return mixed |
||
4495 | * |
||
4496 | * @since 2.0 |
||
4497 | */ |
||
4498 | public function __get( $name ) { |
||
4529 | |||
4530 | /** |
||
4531 | * Handle methods that have been deprecated and any aliasing. |
||
4532 | * |
||
4533 | * @param string $name Function name. |
||
4534 | * @param array $args Arguments passed to function. |
||
4535 | * |
||
4536 | * @return mixed|null |
||
4537 | * |
||
4538 | * @since 2.0 |
||
4539 | */ |
||
4540 | public function __call( $name, $args ) { |
||
4566 | |||
4567 | /** |
||
4568 | * Handle casting a Pods() object to string |
||
4569 | * |
||
4570 | * @return string Pod type and name in CURIE notation |
||
4571 | */ |
||
4572 | public function __toString() { |
||
4583 | } |
||
4584 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.