Pods – Custom Content Types and Fields - Version 1.14.8

Version Description

  • August 19th, 2015 =
  • Fixed: MySQLi results had issues with is_resource checks in some places, those have been fixed for further compatibility with WP 3.9 through 4.3
  • Security Update Reminder: In Pods 1.14.7, we fixed Pods UI orderby, a potential security risk
  • Security Update Reminder: As of Pods 1.12+, AJAX API calls all utilize wpnonce hashes, update your customized publicForm / input helper code AJAX (api.php and misc.php expect `wpcreate_nonce('pods-' . $action)` usage)
  • Security Update Reminder: 1.9.6 Security Update information can be found at: http://pods.io/2011/06/22/pods-1-9-6-security-update-new-features-bug-fixes/
  • Q & A: Is Pods 1.x dead? Not completely, we'll continue releasing maintenance updates to Pods 1.14.x until it becomes unfeasible to maintain compatibility with a future version of WordPress. That may be WordPress 4.0, or it could be later. The best idea is to consider upgrading to the latest stable Pods version at http://wordpress.org/plugins/pods/
Download this release

Release Info

Developer sc0ttkclark
Plugin Icon 128x128 Pods – Custom Content Types and Fields
Version 1.14.8
Comparing to
See all releases

Code changes from version 2.5.5 to 1.14.8

classes/Pod.php ADDED
@@ -0,0 +1,1325 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Pod
3
+ {
4
+ var $id;
5
+ var $sql;
6
+ var $raw_sql;
7
+ var $data;
8
+ var $row_number = -1;
9
+ var $zebra = false;
10
+ var $result;
11
+ var $datatype;
12
+ var $datatype_id;
13
+ var $calc_found_rows = true;
14
+ var $count_found_rows = false;
15
+ var $total;
16
+ var $total_rows;
17
+ var $detail_page;
18
+ var $rpp = 15;
19
+ var $page_var = 'pg';
20
+ var $page = 1;
21
+ var $pagination = true;
22
+ var $search = true;
23
+ var $search_var = 'search';
24
+ var $search_where = '';
25
+ var $search_mode = 'int'; // int | text | text_like
26
+
27
+ var $traverse = array();
28
+ var $rabit_hole = array();
29
+
30
+ /**
31
+ * Constructor - Pods CMS core
32
+ *
33
+ * @param string $datatype The pod name
34
+ * @param mixed $id (optional) The ID or slug, to load a single record; Provide array of $params to run findRecords immediately
35
+ * @license http://www.gnu.org/licenses/gpl-2.0.html
36
+ * @since 1.0.0
37
+ */
38
+ function __construct($datatype = null, $id = null) {
39
+ $datatype = pods_sanitize($datatype);
40
+
41
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE) {
42
+ $this->page = 1;
43
+ $this->pagination = false;
44
+ $this->search = false;
45
+ }
46
+ else {
47
+ // Get the page variable
48
+ $this->page = pods_var($this->page_var, 'get');
49
+ $this->page = empty($this->page) ? 1 : max((int) $this->page, 1);
50
+ if (defined('PODS_GLOBAL_POD_PAGINATION') && !PODS_GLOBAL_POD_PAGINATION) {
51
+ $this->page = 1;
52
+ $this->pagination = false;
53
+ }
54
+
55
+ if (defined('PODS_GLOBAL_POD_SEARCH') && !PODS_GLOBAL_POD_SEARCH)
56
+ $this->search = false;
57
+ if (defined('PODS_GLOBAL_POD_SEARCH_MODE') && in_array(PODS_GLOBAL_POD_SEARCH_MODE, array('int', 'text', 'text_like')))
58
+ $this->search_mode = PODS_GLOBAL_POD_SEARCH_MODE;
59
+ }
60
+
61
+ if (!empty($datatype)) {
62
+ $result = pod_query("SELECT id, name, detail_page FROM @wp_pod_types WHERE name = '$datatype' LIMIT 1");
63
+ if (0 < pods_mysql_num_rows($result)) {
64
+ $row = pods_mysql_fetch_assoc($result);
65
+ $this->datatype = $row['name'];
66
+ $this->datatype_id = $row['id'];
67
+ $this->detail_page = $row['detail_page'];
68
+
69
+ if (null !== $id) {
70
+ if (is_array($id) || is_object($id))
71
+ $this->findRecords($id);
72
+ else {
73
+ $this->getRecordById($id);
74
+ if (!empty($this->data))
75
+ $this->id = $this->get_field('id');
76
+ }
77
+ }
78
+ }
79
+ else
80
+ echo "<e>Error: Pod '{$datatype}' does not exist</e>";
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Fetch a row of results from the DB
86
+ *
87
+ * @since 1.2.0
88
+ */
89
+ function fetchRecord() {
90
+ if ($this->data = pods_mysql_fetch_assoc($this->result)) {
91
+ $this->row_number++;
92
+ if (true === $this->zebra)
93
+ $this->zebra = false;
94
+ else
95
+ $this->zebra = true;
96
+ return $this->data;
97
+ }
98
+ return false;
99
+ }
100
+
101
+ /**
102
+ * Return a field's value(s)
103
+ *
104
+ * @param string $name The field name
105
+ * @param string $orderby (optional) The orderby string, for PICK fields
106
+ * @since 1.2.0
107
+ */
108
+ function get_field($name, $orderby = null) {
109
+ if (empty($this->data) && empty($this->datatype_id)) {
110
+ echo "<e>Error: Pod name invalid, no data available</e>";
111
+ return null;
112
+ }
113
+ // No data
114
+ elseif ( !isset( $this->data[ 'id' ] ) )
115
+ return null;
116
+
117
+ if (isset($this->data[$name])) {
118
+ return $this->data[$name];
119
+ }
120
+ elseif ('created' == $name || 'modified' == $name) {
121
+ $pod_id = (int) $this->get_pod_id();
122
+ if (!empty($pod_id))
123
+ $result = pod_query("SELECT id, created, modified FROM @wp_pod WHERE id = {$pod_id} LIMIT 1");
124
+ else {
125
+ $tbl_row_id = (int) $this->get_field('id');
126
+ if (!empty($tbl_row_id))
127
+ $result = pod_query("SELECT id, created, modified FROM @wp_pod WHERE tbl_row_id = {$tbl_row_id} LIMIT 1");
128
+ else
129
+ return;
130
+ }
131
+ $row = pods_mysql_fetch_assoc($result);
132
+ $this->data['pod_id'] = $row['pod_id'];
133
+ $this->data['created'] = $row['created'];
134
+ $this->data['modified'] = $row['modified'];
135
+ return $this->data[$name];
136
+ }
137
+ elseif ('detail_url' == $name) {
138
+ return $this->parse_magic_tags(array('', '', 'detail_url'));
139
+ }
140
+ else {
141
+ // Dot-traversal
142
+ $last_loop = false;
143
+ $datatype_id = $this->datatype_id;
144
+ $tbl_row_ids = ( isset( $this->data[ 'id' ] ) ? $this->data[ 'id' ] : 0 ); // allow for custom selects
145
+
146
+ $traverse = (false !== strpos($name, '.')) ? explode('.', $name) : array($name);
147
+ $traverse_fields = implode("','", $traverse);
148
+
149
+ // Get columns matching traversal names
150
+ $result = pod_query("SELECT id, datatype, name, coltype, pickval FROM @wp_pod_fields WHERE name IN ('$traverse_fields')");
151
+ if (0 < pods_mysql_num_rows($result)) {
152
+ while ($row = pods_mysql_fetch_assoc($result)) {
153
+ $all_fields[$row['datatype']][$row['name']] = $row;
154
+ }
155
+ }
156
+ // No matching columns
157
+ else {
158
+ return false;
159
+ }
160
+
161
+ $last_coltype = $last_pickval = '';
162
+ // Loop through each traversal level
163
+ foreach ($traverse as $key => $column_name) {
164
+ $last_loop = (1 < count($traverse) - $key) ? false : true;
165
+ $column_exists = isset($all_fields[$datatype_id][$column_name]);
166
+
167
+ if ($column_exists) {
168
+ $col = $all_fields[$datatype_id][$column_name];
169
+ $field_id = $col['id'];
170
+ $coltype = $col['coltype'];
171
+ $pickval = $col['pickval'];
172
+
173
+ if ('pick' == $coltype || 'file' == $coltype) {
174
+ $last_coltype = $coltype;
175
+ $last_pickval = $pickval;
176
+ $tbl_row_ids = $this->lookup_row_ids($field_id, $datatype_id, $tbl_row_ids);
177
+
178
+ if (false === $tbl_row_ids) {
179
+ return false;
180
+ }
181
+
182
+ // Get datatype ID for non-WP PICK columns
183
+ if (
184
+ !empty($pickval) &&
185
+ !in_array($pickval, array('wp_taxonomy', 'wp_post', 'wp_page', 'wp_user')))
186
+ {
187
+ $result = pod_query("SELECT id FROM @wp_pod_types WHERE name = '$pickval' LIMIT 1");
188
+ $datatype_id = pods_mysql_result($result, 0);
189
+ }
190
+ }
191
+ else {
192
+ $last_loop = true;
193
+ }
194
+ }
195
+ // Assume last iteration
196
+ else {
197
+ // Invalid column name
198
+ if (0 == $key) {
199
+ return false;
200
+ }
201
+ $last_loop = true;
202
+ }
203
+
204
+ if ($last_loop) {
205
+ $table = ('file' == $last_coltype) ? 'file' : $last_pickval;
206
+
207
+ if (!empty($table)) {
208
+ $data = $this->rel_lookup($tbl_row_ids, $table, $orderby);
209
+ }
210
+
211
+ if (empty($data)) {
212
+ $results = false;
213
+ }
214
+ // Return entire array
215
+ elseif (false !== $column_exists && ('pick' == $coltype || 'file' == $coltype)) {
216
+ $results = $data;
217
+ }
218
+ // Return a single column value
219
+ elseif (1 == count($data)) {
220
+ $results = $data[0][$column_name];
221
+ }
222
+ // Return an array of single column values
223
+ else {
224
+ foreach ($data as $key => $val) {
225
+ $results[] = $val[$column_name];
226
+ }
227
+ }
228
+ return $results;
229
+ }
230
+ }
231
+ }
232
+ }
233
+
234
+ /**
235
+ * Find items related to a parent field
236
+ *
237
+ * @param int $field_id The field ID
238
+ * @param int $datatype_id The datatype ID
239
+ * @param mixed $tbl_row_ids A comma-separated string (or array) of row IDs
240
+ * @since 1.2.0
241
+ */
242
+ function lookup_row_ids($field_id, $datatype_id, $tbl_row_ids) {
243
+ if (empty($tbl_row_ids))
244
+ $tbl_row_ids = 0;
245
+ else {
246
+ if (!is_array($tbl_row_ids))
247
+ $tbl_row_ids = explode(',', $tbl_row_ids);
248
+ foreach ($tbl_row_ids as $key => $id) {
249
+ $tbl_row_ids[$key] = (int) $id;
250
+ }
251
+ $tbl_row_ids = implode(',', $tbl_row_ids);
252
+ }
253
+
254
+ $field_id = (int) $field_id;
255
+ $datatype_id = (int) $datatype_id;
256
+
257
+ $sql = "
258
+ SELECT
259
+ `r`.`tbl_row_id`
260
+ FROM
261
+ `@wp_pod` AS `p`
262
+ INNER JOIN
263
+ `@wp_pod_rel` AS `r` ON `r`.`field_id` = {$field_id} AND `r`.`pod_id` = `p`.`id`
264
+ WHERE
265
+ `p`.`datatype` = {$datatype_id} AND `p`.`tbl_row_id` IN ($tbl_row_ids)
266
+ ORDER BY
267
+ `r`.`weight`
268
+ ";
269
+ $result = pod_query($sql);
270
+ if (0 < pods_mysql_num_rows($result)) {
271
+ while ($row = pods_mysql_fetch_assoc($result)) {
272
+ $out[] = $row['tbl_row_id'];
273
+ }
274
+ return implode(',', $out);
275
+ }
276
+ return false;
277
+ }
278
+
279
+ /**
280
+ * Lookup values from a single relationship field
281
+ *
282
+ * @param mixed $tbl_row_ids A comma-separated string of row IDs
283
+ * @param string $table The database table alias
284
+ * @param string $orderby (optional) MySQL "ORDER BY" clause
285
+ * @return array $data Associative array of table column values
286
+ * @since 1.2.0
287
+ */
288
+ function rel_lookup($tbl_row_ids, $table, $orderby = null) {
289
+ global $wpdb;
290
+ $orderby = empty($orderby) ? '' : "ORDER BY $orderby";
291
+
292
+ // WP taxonomy item
293
+ if ('wp_taxonomy' == $table) {
294
+ $result = pod_query("SELECT * FROM `$wpdb->terms` WHERE `term_id` IN ($tbl_row_ids) $orderby");
295
+ }
296
+ // WP page, post, or attachment
297
+ elseif ('wp_page' == $table || 'wp_post' == $table || 'file' == $table) {
298
+ $result = pod_query("SELECT * FROM `$wpdb->posts` WHERE `ID` IN ($tbl_row_ids) $orderby");
299
+ }
300
+ // WP user
301
+ elseif ('wp_user' == $table) {
302
+ $result = pod_query("SELECT * FROM `$wpdb->users` WHERE `ID` IN ($tbl_row_ids) $orderby");
303
+ }
304
+ // Pod table
305
+ else {
306
+ $result = pod_query("SELECT * FROM `@wp_pod_tbl_$table` WHERE `id` IN ($tbl_row_ids) $orderby");
307
+ }
308
+
309
+ //override with custom relational query
310
+ $result = apply_filters('pods_rel_lookup', $result, $tbl_row_ids, $table, $orderby, $this);
311
+
312
+ // Put all related items into an array
313
+ if (!is_array($result)) {
314
+ while ($row = pods_mysql_fetch_assoc($result)) {
315
+ $data[] = $row;
316
+ }
317
+ }
318
+ else
319
+ $data = $result;
320
+
321
+ //override with custom data
322
+ $data = apply_filters('pods_rel_lookup_data', $data, $tbl_row_ids, $table, $orderby, $this);
323
+
324
+ return $data;
325
+ }
326
+
327
+ /**
328
+ * Get the current item's pod ID from its datatype ID and tbl_row_id
329
+ *
330
+ * @todo pod_id should NEVER be needed by users - fix code so tbl_row_id is used instead
331
+ * @return int The ID from the wp_pod table
332
+ * @since 1.2.0
333
+ */
334
+ function get_pod_id() {
335
+ if (empty($this->data) && empty($this->datatype_id)) {
336
+ echo "<e>Error: Pod name invalid, no data available</e>";
337
+ return null;
338
+ }
339
+ elseif (empty($this->data))
340
+ return 0;
341
+ elseif ((!isset($this->data['pod_id']) ||empty($this->data['pod_id'])) && isset($this->data['id']) && 0 < $this->data['id']) {
342
+ $this->data['pod_id'] = 0;
343
+ $tbl_row_id = (isset($this->data['id']) ? (int) $this->data['id'] : 0);
344
+ $result = pod_query("SELECT `id` FROM `@wp_pod` WHERE `datatype` = '$this->datatype_id' AND `tbl_row_id` = '$tbl_row_id' LIMIT 1");
345
+ if (0 < pods_mysql_num_rows($result)) {
346
+ $this->data['pod_id'] = pods_mysql_result($result, 0);
347
+ }
348
+ }
349
+ return $this->data['pod_id'];
350
+ }
351
+
352
+ /**
353
+ * Set a custom data value (no database changes)
354
+ *
355
+ * @param string $name The field name
356
+ * @param mixed $data The value to set
357
+ * @return mixed The value of $data
358
+ * @since 1.2.0
359
+ */
360
+ function set_field($name, $data) {
361
+ return $this->data[$name] = $data;
362
+ }
363
+
364
+ /**
365
+ * Run a helper within a Pod Page or WP Template or Magic Tags
366
+ *
367
+ * @param string $helper The helper name
368
+ * @return mixed Anything returned by the helper
369
+ * @since 1.2.0
370
+ */
371
+ function pod_helper($helper, $value = null, $name = null) {
372
+ ob_start();
373
+
374
+ do_action('pods_pre_pod_helper', $helper, $value, $name, $this);
375
+ do_action("pods_pre_pod_helper_{$helper}", $helper, $value, $name, $this);
376
+
377
+ $function_or_file = $helper;
378
+ $check_function = $function_or_file;
379
+ if ((!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) && (!defined('PODS_HELPER_FUNCTIONS') || !PODS_HELPER_FUNCTIONS))
380
+ $check_function = false;
381
+ $check_file = null;
382
+ if ((!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) && (!defined('PODS_HELPER_FILES') || !PODS_HELPER_FILES))
383
+ $check_file = false;
384
+ if (false !== $check_function && false !== $check_file)
385
+ $function_or_file = pods_function_or_file($function_or_file, $check_function, 'helper', $check_file);
386
+ else
387
+ $function_or_file = false;
388
+
389
+ $content = false;
390
+ if (!$function_or_file) {
391
+ $api = new PodAPI();
392
+ $params = array('name' => $helper, 'type' => 'display');
393
+ if (!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE)
394
+ $params = pods_sanitize($params);
395
+ $content = $api->load_helper($params);
396
+ if (false !== $content && 0 < strlen(trim($content['phpcode'])))
397
+ $content = $content['phpcode'];
398
+ else
399
+ $content = false;
400
+ }
401
+
402
+ if (false === $content && false !== $function_or_file && isset($function_or_file['function']))
403
+ echo $function_or_file['function']($value, $name, $this);
404
+ elseif (false === $content && false !== $function_or_file && isset($function_or_file['file']))
405
+ locate_template($function_or_file['file'], true, true);
406
+ elseif (false !== $content) {
407
+ if (!defined('PODS_DISABLE_EVAL') || !PODS_DISABLE_EVAL)
408
+ eval("?>$content");
409
+ else
410
+ echo $content;
411
+ }
412
+
413
+ //post-helper hooks
414
+ do_action('pods_post_pod_helper', $helper, $value, $name, $this);
415
+ do_action("pods_post_pod_helper_{$helper}", $helper, $value, $name, $this);
416
+
417
+ return apply_filters('pods_helper', ob_get_clean(), $helper, $value, $this);
418
+ }
419
+
420
+ /**
421
+ * Get pod or category dropdown values
422
+ */
423
+ function get_dropdown_values($params) {
424
+ global $wpdb;
425
+
426
+ $params = (object) $params;
427
+
428
+ $params->orderby = empty($params->pick_orderby) ? '`name` ASC' : $params->pick_orderby;
429
+
430
+ // WP taxonomy dropdown
431
+ if ('wp_taxonomy' == $params->table) {
432
+ $where = (false !== $params->unique_vals) ? "WHERE `t`.term_id NOT IN ({$params->unique_vals})" : '';
433
+ if (!empty($params->pick_filter)) {
434
+ $where .= (empty($where) ? ' WHERE ' : ' AND ') . $params->pick_filter;
435
+ }
436
+
437
+ $sql = "SELECT `t`.term_id AS id, `t`.`name` FROM `{$wpdb->term_taxonomy}` AS `tx` INNER JOIN `{$wpdb->terms}` AS `t` ON `t`.`term_id` = `tx`.`term_id` {$where} ORDER BY {$params->orderby}";
438
+ }
439
+ // WP page or post dropdown
440
+ elseif ('wp_page' == $params->table || 'wp_post' == $params->table) {
441
+ $post_type = substr($params->table, 3);
442
+ $where = (false !== $params->unique_vals) ? "AND `id` NOT IN ({$params->unique_vals})" : '';
443
+ if (!empty($params->pick_filter)) {
444
+ $where .= " AND {$params->pick_filter}";
445
+ }
446
+
447
+ $sql = "SELECT `t`.ID AS id, `t`.post_title AS `name` FROM `{$wpdb->posts}` AS `t` WHERE `t`.`post_type` = '{$post_type}' {$where} ORDER BY {$params->orderby}";
448
+ }
449
+ // WP user dropdown
450
+ elseif ('wp_user' == $params->table) {
451
+ $where = (false !== $params->unique_vals) ? "WHERE `id` NOT IN ({$params->unique_vals})" : '';
452
+ if (!empty($params->pick_filter)) {
453
+ $where .= (empty($where) ? ' WHERE ' : ' AND ') . $params->pick_filter;
454
+ }
455
+
456
+ $sql = "SELECT `t`.`ID` AS `id`, `t`.`display_name` AS `name` FROM `{$wpdb->users}` AS `t` {$where} ORDER BY {$params->orderby}";
457
+ }
458
+ // Pod table dropdown
459
+ else {
460
+ $where = (false !== $params->unique_vals) ? "WHERE id NOT IN ({$params->unique_vals})" : '';
461
+ if (!empty($params->pick_filter)) {
462
+ $where .= (empty($where) ? ' WHERE ' : ' AND ') . $params->pick_filter;
463
+ }
464
+
465
+ $sql = "SELECT * FROM `@wp_pod_tbl_{$params->table}` AS `t` {$where} ORDER BY {$params->orderby}";
466
+ }
467
+
468
+ //override with custom dropdown values
469
+ $sql = apply_filters('pods_get_dropdown_values', $sql, $params, $this);
470
+
471
+ $val = array();
472
+ $result = pod_query($sql);
473
+ while ($row = pods_mysql_fetch_assoc($result)) {
474
+ $row['active'] = false;
475
+ if (!empty($params->tbl_row_ids)) {
476
+ $row['active'] = in_array($row['id'], $params->tbl_row_ids);
477
+ }
478
+ elseif (isset($_GET[$params->field_name])) {
479
+ if ('int' == $this->search_mode && $row['id'] == (int) $_GET[$params->field_name])
480
+ $row['active'] = true;
481
+ elseif ('text' == $this->search_mode && $row['name'] == $_GET[$params->field_name])
482
+ $row['active'] = true;
483
+ }
484
+ $val[] = $row;
485
+ }
486
+ return $val;
487
+ }
488
+
489
+ /**
490
+ * Setup fields for rabit hole
491
+ */
492
+ function feed_rabit ($fields) {
493
+ $feed = array();
494
+ foreach ($fields as $field => $data) {
495
+ if (!is_array($data))
496
+ $field = $data;
497
+ if (isset($_GET[$field]))
498
+ $feed['traverse_' . $field] = array($field);
499
+ }
500
+ return $feed;
501
+ }
502
+
503
+ /**
504
+ * Recursively join tables based on fields
505
+ */
506
+ function recurse_rabit_hole ($pod, $fields, $joined = 't', $depth = 0) {
507
+ global $wpdb;
508
+
509
+ if (!isset($this->rabit_hole[$pod]))
510
+ $this->rabit_hole[$pod] = array();
511
+ $api = new PodAPI($pod);
512
+ $pod_id = (int) $api->dt;
513
+ foreach ($api->fields as $field) {
514
+ if (!in_array($field['coltype'], array('pick', 'file')) && !isset($this->rabit_hole[$pod][$field['name']]))
515
+ continue;
516
+ $the_pod = null;
517
+ $table = $field['pickval'];
518
+ $on = 'id';
519
+ $name = 'name';
520
+ $recurse = true;
521
+ if ('file' == $field['coltype'] || 'wp_page' == $table || 'wp_post' == $table) {
522
+ $table = $wpdb->posts;
523
+ $on = 'ID';
524
+ $name = 'post_title';
525
+ $recurse = false;
526
+ }
527
+ elseif ('wp_taxonomy' == $table) {
528
+ $table = $wpdb->terms;
529
+ $on = 'term_id';
530
+ $recurse = false;
531
+ }
532
+ elseif ('wp_user' == $table) {
533
+ $table = $wpdb->users;
534
+ $on = 'ID';
535
+ $name = 'display_name';
536
+ $recurse = false;
537
+ }
538
+ elseif (!empty($table)) {
539
+ $the_pod = $table;
540
+ $table = '@wp_pod_tbl_' . $table;
541
+ }
542
+ $rabit_hole = array_merge($field,
543
+ array('table' => $table,
544
+ 'pod' => $the_pod,
545
+ 'on' => $on,
546
+ 'name' => $name,
547
+ 'recurse' => $recurse));
548
+ if (isset($this->rabit_hole[$pod][$field['name']]))
549
+ $rabit_hole = array_merge($rabit_hole, (array) $this->rabit_hole[$pod][$field['name']]);
550
+ $this->rabit_hole[$pod][$field['name']] = apply_filters('pods_rabit_hole', $rabit_hole, $pod, $fields, $joined, $depth);
551
+ }
552
+ unset($api);
553
+
554
+ $joins = array();
555
+ if (!isset($fields[$depth]) || empty($fields[$depth]))
556
+ return $joins;
557
+ $field = $fields[$depth];
558
+ if (!isset($this->rabit_hole[$pod][$field]))
559
+ return $joins;
560
+ $this->rabit_hole[$pod][$field] = array_merge(array('table' => null,
561
+ 'pod' => null,
562
+ 'on' => 'id',
563
+ 'name' => 'name',
564
+ 'recurse' => false,
565
+ 'id' => 0,
566
+ 'coltype' => null),
567
+ $this->rabit_hole[$pod][$field]);
568
+ $this->rabit_hole[$pod][$field]['id'] = (int) $this->rabit_hole[$pod][$field]['id'];
569
+ $field_joined = $field;
570
+ if (0 < $depth && 't' != $joined)
571
+ $field_joined = $joined . '_' . $field;
572
+ if (false !== $this->search) {
573
+ if (0 < strlen(pods_var($field_joined, 'get'))) {
574
+ $val = absint(pods_var($field_joined, 'get'));
575
+ $on = $this->rabit_hole[$pod][$field]['on'];
576
+ $search = "`{$field_joined}`.`{$on}` = {$val}";
577
+ if ('text' == $this->search_mode) {
578
+ $val = pods_var($field_joined, 'get');
579
+ $on = $this->rabit_hole[$pod][$field]['name'];
580
+ $search = "`{$field_joined}`.`{$on}` = '{$val}'";
581
+ }
582
+ elseif ('text_like' == $this->search_mode) {
583
+ $val = pods_sanitize(like_escape($_GET[$field_joined]));
584
+ $on = $this->rabit_hole[$pod][$field]['name'];
585
+ $search = "`{$field_joined}`.`{$on}` LIKE '%{$val}%'";
586
+ }
587
+ $this->search_where .= " AND {$search} ";
588
+ }
589
+ }
590
+ $p_alias = 'p';
591
+ $p_join = '';
592
+ if (0 < $depth && 't' != $joined) {
593
+ $p_alias = 'p_' . $joined;
594
+ $p_join = "
595
+ LEFT JOIN `@wp_pod` AS `{$p_alias}` ON `{$p_alias}`.`datatype` = {$pod_id} AND `{$p_alias}`.`tbl_row_id` = `{$joined}`.`id`";
596
+ }
597
+ $rel_alias = 'rel_' . $field_joined;
598
+ $the_join = "{$p_join}
599
+ LEFT JOIN `@wp_pod_rel` AS `{$rel_alias}` ON `{$rel_alias}`.`field_id` = {$this->rabit_hole[$pod][$field]['id']} AND `{$rel_alias}`.`pod_id` = `{$p_alias}`.id
600
+ LEFT JOIN `{$this->rabit_hole[$pod][$field]['table']}` AS `{$field_joined}` ON `{$field_joined}`.`{$this->rabit_hole[$pod][$field]['on']}` = `{$rel_alias}`.`tbl_row_id`
601
+ ";
602
+ if (!in_array($this->rabit_hole[$pod][$field]['coltype'], array('pick', 'file'))) {
603
+ $the_join = "
604
+ LEFT JOIN `{$this->rabit_hole[$pod][$field]['table']}` AS `{$field_joined}` ON `{$field_joined}`.`{$this->rabit_hole[$pod][$field]['on']}` = CONVERT(`{$joined}`.`{$field_joined}`, SIGNED)
605
+ ";
606
+ }
607
+ $joins[$pod . '_' . $depth . '_' . $this->rabit_hole[$pod][$field]['id']] = apply_filters('pods_rabit_hole_the_join', $the_join, $pod, $fields, $joined, $depth, $this);
608
+ if (($depth + 1) < count($fields) && null !== $this->rabit_hole[$pod][$field]['pod'] && false !== $this->rabit_hole[$pod][$field]['recurse'])
609
+ $joins = array_merge($joins, $this->recurse_rabit_hole($this->rabit_hole[$pod][$field]['pod'], $fields, $field_joined, ($depth + 1)));
610
+ return $joins;
611
+ }
612
+
613
+ /**
614
+ * Recursively join tables based on fields
615
+ */
616
+ function rabit_hole ($pod, $fields = null) {
617
+ $joins = array();
618
+ if (null === $fields) {
619
+ $api = new PodAPI($pod);
620
+ $fields = $this->feed_rabit($api->fields);
621
+ }
622
+ foreach ((array) $fields as $field_group) {
623
+ if (is_array($field_group))
624
+ $joins = array_merge($joins, $this->recurse_rabit_hole($pod, $field_group));
625
+ else {
626
+ $joins = array_merge($joins, $this->recurse_rabit_hole($pod, $fields));
627
+ $joins = array_filter($joins);
628
+ return $joins;
629
+ }
630
+ }
631
+ $joins = array_filter($joins);
632
+ return $joins;
633
+ }
634
+
635
+ /**
636
+ * Return a single record
637
+ */
638
+ function getRecordById($id) {
639
+ if (empty($this->datatype_id)) {
640
+ echo "<e>Error: Pod name invalid</e>";
641
+ return null;
642
+ }
643
+ global $wpdb;
644
+ $datatype = $this->datatype;
645
+ $datatype_id = $this->datatype_id;
646
+ if ($this->is_val($datatype)) {
647
+ $this->result = null;
648
+ if (is_numeric($id)) {
649
+ $id = (int) $id;
650
+ $sql = "
651
+ SELECT
652
+ DISTINCT `t`.*, `p`.`id` AS `pod_id`, `p`.`created`, `p`.`modified`
653
+ FROM
654
+ `@wp_pod` AS `p`
655
+ INNER JOIN
656
+ `@wp_pod_tbl_{$datatype}` AS `t` ON `t`.`id` = `p`.`tbl_row_id`
657
+ WHERE
658
+ `p`.`datatype` = {$datatype_id}
659
+ AND `t`.`id` = {$id}
660
+ LIMIT 1
661
+ ";
662
+ $this->raw_sql = $sql;
663
+ $this->sql = str_replace('@wp_', $wpdb->prefix, $sql);
664
+ $this->result = pod_query($sql);
665
+ $this->row_number = 0;
666
+ $this->zebra = false;
667
+ $this->total = $this->total_rows = 1;
668
+ }
669
+ else {
670
+ // Get the slug column
671
+ $fields_result = pod_query("SELECT name FROM @wp_pod_fields WHERE coltype = 'slug' AND datatype = $this->datatype_id LIMIT 1");
672
+ if (0 < pods_mysql_num_rows($fields_result)) {
673
+ $field_name = pods_mysql_result($fields_result, 0);
674
+ $sql = "
675
+ SELECT
676
+ DISTINCT `t`.*, `p`.`id` AS `pod_id`, `p`.`created`, `p`.`modified`
677
+ FROM
678
+ `@wp_pod` AS `p`
679
+ INNER JOIN
680
+ `@wp_pod_tbl_{$datatype}` AS `t` ON `t`.`id` = `p`.`tbl_row_id`
681
+ WHERE
682
+ `p`.`datatype` = {$datatype_id}
683
+ AND `t`.`{$field_name}` = '{$id}'
684
+ LIMIT 1
685
+ ";
686
+ $this->raw_sql = $sql;
687
+ $this->sql = str_replace('@wp_', $wpdb->prefix, $sql);
688
+ $this->result = pod_query($sql);
689
+ $this->row_number = 0;
690
+ $this->zebra = false;
691
+ $this->total = $this->total_rows = 1;
692
+ }
693
+ }
694
+
695
+ if (0 < pods_mysql_num_rows($this->result)) {
696
+ $this->data = pods_mysql_fetch_assoc($this->result);
697
+ $this->data['type'] = $datatype;
698
+ return $this->data;
699
+ }
700
+ $this->data = false;
701
+ }
702
+ else
703
+ die('<e>Datatype not set');
704
+ }
705
+
706
+ /**
707
+ * Search and filter records
708
+ */
709
+ function findRecords($orderby = '`t`.`id` DESC', $rows_per_page = 15, $where = null, $sql = null) {
710
+ if (empty($this->datatype_id)) {
711
+ echo "<e>Error: Pod name invalid</e>";
712
+ return null;
713
+ }
714
+ global $wpdb;
715
+ $join = $groupby = $having = '';
716
+ $params = null;
717
+ $select = '`t`.*, `p`.`id` AS `pod_id`, `p`.`created`, `p`.`modified`';
718
+ $this->traverse = array();
719
+
720
+ $defaults = array('select' => $select,
721
+ 'join' => $join,
722
+ 'where' => $where,
723
+ 'groupby' => $groupby,
724
+ 'having' => $having,
725
+ 'orderby' => (is_array($orderby) ? '`t`.`id` DESC' : $orderby),
726
+ 'limit' => $rows_per_page,
727
+ 'offset' => null,
728
+ 'search' => $this->search,
729
+ 'search_var' => $this->search_var,
730
+ 'search_mode' => $this->search_mode,
731
+ 'traverse' => $this->traverse,
732
+ 'page' => $this->page,
733
+ 'page_var' => null,
734
+ 'pagination' => $this->pagination,
735
+ 'calc_found_rows' => $this->calc_found_rows,
736
+ 'count_found_rows' => $this->count_found_rows,
737
+ 'sql' => $sql);
738
+ $defaults = (array) apply_filters('pods_findrecords_defaults', $defaults, $orderby, $this);
739
+ $params = (object) $defaults;
740
+ if (is_array($orderby) && !empty($orderby))
741
+ $params = (object) array_merge($defaults, $orderby);
742
+
743
+ if (0 < strlen($params->select))
744
+ $select = $params->select;
745
+ $join = $params->join;
746
+ $this->search = (boolean) $params->search;
747
+ $this->search_var = $params->search_var;
748
+ $this->search_mode = (in_array($params->search_mode, array('int', 'text')) ? $params->search_mode : 'int');
749
+ $this->traverse = (array) $params->traverse;
750
+ $where = $params->where;
751
+ $groupby = $params->groupby;
752
+ $having = $params->having;
753
+ $orderby = $params->orderby;
754
+ $rows_per_page = (int) $params->limit;
755
+ $this->page = (int) $params->page;
756
+ if (null !== $params->page_var)
757
+ $this->page_var = (int) $params->page_var;
758
+ if ($this->page < 1 || null !== $params->page_var)
759
+ $this->page = max(pods_var($this->page_var, 'get'), 1);
760
+ $this->pagination = (bool) $params->pagination;
761
+ $this->calc_found_rows = (boolean) $params->calc_found_rows;
762
+ $this->count_found_rows = (boolean) $params->count_found_rows;
763
+ $sql = $params->sql;
764
+ if (true === $this->count_found_rows && empty($sql))
765
+ $this->calc_found_rows = false;
766
+ $page = (int) $this->page;
767
+ if ($rows_per_page < 0 || false === $this->pagination)
768
+ $page = $this->page = 1;
769
+ $datatype = $this->datatype;
770
+ $datatype_id = (int) $this->datatype_id;
771
+ $this->rpp = (int) $rows_per_page;
772
+ $this->offset = ($this->rpp * ($page - 1));
773
+ if (null !== $params->offset)
774
+ $this->offset += (int) $params->offset;
775
+
776
+ $sql_builder = false;
777
+ if (empty($sql)) {
778
+ $sql_builder = true;
779
+ $limit = $this->search_where = '';
780
+
781
+ // ctype_digit expects a string, or it returns FALSE
782
+ if (ctype_digit("$rows_per_page") && 0 <= $rows_per_page) {
783
+ $limit = $this->offset . ',' . $rows_per_page;
784
+ }
785
+ elseif (false !== strpos($rows_per_page, ',')) {
786
+ // Custom offset
787
+ $limit = $rows_per_page;
788
+ }
789
+ $where = empty($where) ? '' : " AND ( $where )";
790
+
791
+ if (false !== $this->search) {
792
+ // Handle search
793
+ if (0 < strlen(pods_var($this->search_var, 'get'))) {
794
+ $val = pods_sanitize(like_escape($_GET[$this->search_var]));
795
+ $this->search_where = " AND (`t`.`name` LIKE '%{$val}%') ";
796
+ }
797
+ }
798
+
799
+ // Add "`t`." prefix to $orderby if needed
800
+ if (!empty($orderby) && false === strpos($orderby, ',') && false === strpos($orderby, '(') && false === strpos($orderby, '.')) {
801
+ if (false !== strpos($orderby, ' ASC'))
802
+ $orderby = '`t`.`' . trim(str_replace(array('`', ' ASC'), '', $orderby)) . '` ASC';
803
+ else
804
+ $orderby = '`t`.`' . trim(str_replace(array('`', ' DESC'), '', $orderby)) . '` DESC';
805
+ }
806
+
807
+ $haystack = preg_replace('/\s/', ' ', "$select $where $groupby $having $orderby");
808
+ $haystack = preg_replace('/\w\(/', ' ', $haystack);
809
+ $haystack = str_replace(array('(', ')', ' '), ' ', $haystack);
810
+
811
+ preg_match_all('/`?[\w]+`?(?:\\.`?[\w]+`?)+(?=[^"\']*(?:"[^"]*"[^"]*|\'[^\']*\'[^\']*)*$)/', $haystack, $found, PREG_PATTERN_ORDER);
812
+
813
+ $found = (array) @current($found);
814
+ $find = $replace = array();
815
+ foreach ($found as $key => $value) {
816
+ $value = str_replace('`', '', $value);
817
+ $value = explode('.', $value);
818
+ $dot = array_pop($value);
819
+ if (in_array('/\b' . trim($found[$key], '`') . '\b(?=[^"\']*(?:"[^"]*"[^"]*|\'[^\']*\'[^\']*)*$)/', $find)) {
820
+ unset($found[$key]);
821
+ continue;
822
+ }
823
+ $find[$key] = '/\b' . trim($found[$key], '`') . '\b(?=[^"\']*(?:"[^"]*"[^"]*|\'[^\']*\'[^\']*)*$)/';
824
+ $esc_start = $esc_end = '`';
825
+ if (strlen(ltrim($found[$key], '`')) < strlen($found[$key]))
826
+ $esc_start = '';
827
+ if (strlen(rtrim($found[$key], '`')) < strlen($found[$key]))
828
+ $esc_end = '';
829
+ if ('*' != $dot)
830
+ $dot = '`' . $dot . $esc_end;
831
+ $replace[$key] = $esc_start . implode('_', $value) . '`.' . $dot;
832
+ if (in_array($value[0], array('t', 'p'))) {
833
+ unset($found[$key]);
834
+ continue;
835
+ }
836
+ unset($found[$key]);
837
+ if (!in_array($value, $found))
838
+ $found[$key] = $value;
839
+ }
840
+
841
+ if (!empty($this->traverse)) {
842
+ foreach ((array) $this->traverse as $key => $traverse) {
843
+ $traverse = str_replace('`', '', $traverse);
844
+ $already_found = false;
845
+ foreach ($found as $traversal) {
846
+ if (is_array($traversal))
847
+ $traversal = implode('.', $traversal);
848
+ if ($traversal == $traverse) {
849
+ $already_found = true;
850
+ break;
851
+ }
852
+ }
853
+ if (!$already_found)
854
+ $found['traverse_' . $key] = explode('.', $traverse);
855
+ }
856
+ }
857
+
858
+ $joins = array();
859
+ if (!empty($find)) {
860
+ $select = preg_replace($find, $replace, $select);
861
+ $where = preg_replace($find, $replace, $where);
862
+ $groupby = preg_replace($find, $replace, $groupby);
863
+ $having = preg_replace($find, $replace, $having);
864
+ $orderby = preg_replace($find, $replace, $orderby);
865
+
866
+ if (!empty($found))
867
+ $joins = $this->rabit_hole($this->datatype, $found);
868
+ elseif (false !== $this->search)
869
+ $joins = $this->rabit_hole($this->datatype);
870
+ }
871
+
872
+ if (0 < strlen($join)) {
873
+ $joins[] = "
874
+ {$join}
875
+ ";
876
+ }
877
+ $join = apply_filters('pods_findrecords_join', implode(' ', $joins), $params, $this);
878
+
879
+ $groupby = trim($groupby);
880
+ $orderby = trim($orderby);
881
+ $limit = trim($limit);
882
+
883
+ $calc_found_rows = '';
884
+ if (false !== $this->calc_found_rows)
885
+ $calc_found_rows = 'SQL_CALC_FOUND_ROWS';
886
+ $sql = "
887
+ SELECT
888
+ {$calc_found_rows} DISTINCT {$select}
889
+ FROM
890
+ `@wp_pod` AS `p`
891
+ INNER JOIN
892
+ `@wp_pod_tbl_{$datatype}` AS `t` ON `t`.`id` = `p`.`tbl_row_id`
893
+ {$join}
894
+ WHERE
895
+ `p`.`datatype` = {$datatype_id}";
896
+ if (!empty($this->search_where)) {
897
+ $sql .= "
898
+ {$this->search_where}";
899
+ }
900
+ if (!empty($where)) {
901
+ $sql .= "
902
+ {$where}";
903
+ }
904
+ if (!empty($groupby)) {
905
+ $sql .= "
906
+ GROUP BY {$groupby}";
907
+ }
908
+ if (!empty($having)) {
909
+ $sql .= "
910
+ HAVING {$having}";
911
+ }
912
+ if (!empty($orderby)) {
913
+ $sql .= "
914
+ ORDER BY {$orderby}";
915
+ }
916
+ if (!empty($limit)) {
917
+ $sql .= "
918
+ LIMIT {$limit}";
919
+ }
920
+ }
921
+ $this->raw_sql = $sql;
922
+ $this->sql = str_replace('@wp_', $wpdb->prefix, $sql);
923
+ $this->result = pod_query($sql);
924
+ $this->row_number = -1;
925
+ $this->zebra = false;
926
+ $this->total = absint(@pods_mysql_num_rows($this->result));
927
+ if (false !== $this->calc_found_rows) {
928
+ $this->total_rows = pod_query("SELECT FOUND_ROWS()");
929
+ $this->getTotalRows();
930
+ }
931
+ elseif (false !== $this->count_found_rows && false !== $sql_builder) {
932
+ $sql = "
933
+ SELECT
934
+ COUNT(*) AS `found_rows`
935
+ FROM
936
+ `@wp_pod` AS `p`
937
+ INNER JOIN
938
+ `@wp_pod_tbl_{$datatype}` AS `t` ON `t`.`id` = `p`.`tbl_row_id`
939
+ {$join}
940
+ WHERE
941
+ `p`.`datatype` = {$datatype_id}";
942
+ if (!empty($this->search_where)) {
943
+ $sql .= "
944
+ {$this->search_where}";
945
+ }
946
+ if (!empty($where)) {
947
+ $sql .= "
948
+ {$where}";
949
+ }
950
+ if (!empty($groupby)) {
951
+ $sql .= "
952
+ GROUP BY {$groupby}";
953
+ }
954
+ if (!empty($having)) {
955
+ $sql .= "
956
+ HAVING {$having}";
957
+ }
958
+ $this->total_rows = pod_query($sql);
959
+ $this->getTotalRows();
960
+ }
961
+ }
962
+
963
+ /**
964
+ * Fetch the total row count
965
+ */
966
+ function getTotalRows() {
967
+ if (false === is_numeric($this->total_rows)) {
968
+ if ($row = pods_mysql_fetch_array($this->total_rows))
969
+ $this->total_rows = $row[0];
970
+ else
971
+ $this->total_rows = 0;
972
+ }
973
+ return (int) $this->total_rows;
974
+ }
975
+
976
+ /**
977
+ * Fetch the current row number
978
+ */
979
+ function getRowNumber() {
980
+ return (int) $this->row_number;
981
+ }
982
+
983
+ /**
984
+ * Fetch the current row number
985
+ */
986
+ function getZebra() {
987
+ return (boolean) $this->zebra;
988
+ }
989
+
990
+ /**
991
+ * (Re)set the MySQL result pointer
992
+ */
993
+ function resetPointer($row_number = 0) {
994
+ $row_number = absint($row_number);
995
+ if (0 < pods_mysql_num_rows($this->result)) {
996
+ $this->row_number = $row_number;
997
+ $this->zebra = false;
998
+ return pods_mysql_data_seek($this->result, $row_number);
999
+ }
1000
+ return false;
1001
+ }
1002
+
1003
+ /**
1004
+ * Display HTML for all datatype fields
1005
+ */
1006
+ function showform($pod_id = null, $public_columns = null, $label = 'Save changes') {
1007
+ if (empty($this->datatype_id)) {
1008
+ echo "<e>Error: Pod name invalid</e>";
1009
+ return null;
1010
+ }
1011
+ $pod_id = absint($pod_id);
1012
+ $cache = PodCache::instance();
1013
+
1014
+ $datatype = $this->datatype;
1015
+ $datatype_id = (int) $this->datatype_id;
1016
+ $this->data['pod_id'] = $pod_id = (int) $pod_id;
1017
+
1018
+ $where = '';
1019
+ if (!empty($public_columns)) {
1020
+ foreach ($public_columns as $key => $val) {
1021
+ if (is_array($public_columns[$key])) {
1022
+ $where[] = $key;
1023
+ $attributes[$key] = $val;
1024
+ }
1025
+ else {
1026
+ $where[] = $val;
1027
+ $attributes[$val] = array();
1028
+ }
1029
+ }
1030
+ $where = "AND name IN ('" . implode("','", $where) . "')";
1031
+ }
1032
+
1033
+ $result = pod_query("SELECT * FROM @wp_pod_fields WHERE datatype = $datatype_id $where ORDER BY weight ASC");
1034
+ $public_columns = array();
1035
+ while ($row = pods_mysql_fetch_assoc($result)) {
1036
+ $fields[$row['name']] = $row;
1037
+ $public_columns[] = $row['name'];
1038
+ }
1039
+
1040
+ // Re-order the fields if a public form
1041
+ if (!empty($attributes)) {
1042
+ $tmp = $fields;
1043
+ $fields = array();
1044
+ foreach ($attributes as $key => $val) {
1045
+ if (isset($tmp[$key]))
1046
+ $fields[$key] = $tmp[$key];
1047
+ }
1048
+ unset($tmp);
1049
+ }
1050
+
1051
+ // Edit an existing item
1052
+ if (!empty($pod_id)) {
1053
+ $sql = "
1054
+ SELECT
1055
+ `t`.*
1056
+ FROM
1057
+ @wp_pod `p`
1058
+ INNER JOIN
1059
+ `@wp_pod_tbl_{$datatype}` AS `t` ON `t`.`id` = `p`.`tbl_row_id`
1060
+ WHERE
1061
+ `p`.`id` = {$pod_id}
1062
+ LIMIT
1063
+ 1
1064
+ ";
1065
+ $result = pod_query($sql);
1066
+ if (0 < pods_mysql_num_rows($result)) {
1067
+ $tbl_cols = pods_mysql_fetch_assoc($result);
1068
+ }
1069
+ }
1070
+ $uri_hash = wp_hash($_SERVER['REQUEST_URI']);
1071
+
1072
+ do_action('pods_showform_pre', $pod_id, $public_columns, $label, $this);
1073
+
1074
+ foreach ($fields as $key => $field) {
1075
+ // Replace field attributes with public form attributes
1076
+ if (!empty($attributes) && is_array($attributes[$key])) {
1077
+ $field = array_merge($field, $attributes[$key]);
1078
+ }
1079
+
1080
+ if (empty($field['label'])) {
1081
+ $field['label'] = ucwords($key);
1082
+ }
1083
+
1084
+ if (1 == $field['required']) {
1085
+ $field['label'] .= ' <span class="red">*</span>';
1086
+ }
1087
+
1088
+ if (!empty($field['pickval'])) {
1089
+ $val = array();
1090
+ $tbl_row_ids = array();
1091
+ $table = $field['pickval'];
1092
+
1093
+ $result = pod_query("SELECT `id` FROM `@wp_pod_fields` WHERE `datatype` = {$datatype_id} AND `name` = '$key' LIMIT 1");
1094
+ $field_id = (int) pods_mysql_result($result, 0);
1095
+
1096
+ $result = pod_query("SELECT `tbl_row_id` FROM `@wp_pod_rel` WHERE `field_id` = {$field_id} AND `pod_id` = {$pod_id}");
1097
+ while ($row = pods_mysql_fetch_assoc($result)) {
1098
+ $tbl_row_ids[] = (int) $row['tbl_row_id'];
1099
+ }
1100
+
1101
+ // Use default values for public forms
1102
+ if (empty($tbl_row_ids) && !empty($field['default'])) {
1103
+ $tbl_row_ids = $field['default'];
1104
+ if (!is_array($field['default'])) {
1105
+ $tbl_row_ids = explode(',', $tbl_row_ids);
1106
+ foreach ($tbl_row_ids as $row_key => $row_val) {
1107
+ $tbl_row_ids[$row_key] = (int) trim($row_val);
1108
+ }
1109
+ }
1110
+ }
1111
+
1112
+ // If the PICK column is unique, get values already chosen
1113
+ $unique_vals = false;
1114
+ if (1 == $field['unique']) {
1115
+ $exclude = empty($pod_id) ? '' : "AND `pod_id` != {$pod_id}";
1116
+ $result = pod_query("SELECT `tbl_row_id` FROM `@wp_pod_rel` WHERE `field_id` = {$field_id} {$exclude}");
1117
+ if (0 < pods_mysql_num_rows($result)) {
1118
+ $unique_vals = array();
1119
+ while ($row = pods_mysql_fetch_assoc($result)) {
1120
+ $unique_vals[] = (int) $row['tbl_row_id'];
1121
+ }
1122
+ $unique_vals = implode(',', $unique_vals);
1123
+ }
1124
+ }
1125
+
1126
+ $params = array(
1127
+ 'table' => $table,
1128
+ 'field_name' => null,
1129
+ 'tbl_row_ids' => $tbl_row_ids,
1130
+ 'unique_vals' => $unique_vals,
1131
+ 'pick_filter' => $field['pick_filter'],
1132
+ 'pick_orderby' => $field['pick_orderby']
1133
+ );
1134
+ $this->data[$key] = $this->get_dropdown_values($params);
1135
+ }
1136
+ else {
1137
+ // Set a default value if no value is entered
1138
+ if (!isset($this->data[$key]) && !empty($field['default'])) {
1139
+ $this->data[$key] = $field['default'];
1140
+ }
1141
+ else {
1142
+ $this->data[$key] = isset($tbl_cols[$key]) && $this->is_val($tbl_cols[$key]) ? $tbl_cols[$key] : null;
1143
+ }
1144
+ }
1145
+ $this->build_field_html($field);
1146
+ }
1147
+ $uri_hash = wp_hash($_SERVER['REQUEST_URI']);
1148
+
1149
+ $save_button_atts = array(
1150
+ 'type' => 'button',
1151
+ 'class' => 'button btn_save',
1152
+ 'value' => $label,
1153
+ 'onclick' => "saveForm($cache->form_count)"
1154
+ );
1155
+ $save_button_atts = apply_filters('pods_showform_save_button_atts', $save_button_atts, $this);
1156
+ $atts = '';
1157
+ foreach ($save_button_atts as $att => $value) {
1158
+ $atts .= $att.'="'.$value.'" ';
1159
+ }
1160
+ $save_button = '<input '.$atts.'/>';
1161
+ ?>
1162
+ <div>
1163
+ <input type="hidden" class="form num pod_id" value="<?php echo $pod_id; ?>" />
1164
+ <input type="hidden" class="form num tbl_row_id" value="<?php echo (!empty($tbl_cols) ? $tbl_cols['id'] : 0); ?>" />
1165
+ <input type="hidden" class="form txt datatype" value="<?php echo $datatype; ?>" />
1166
+ <input type="hidden" class="form txt form_count" value="<?php echo $cache->form_count; ?>" />
1167
+ <input type="hidden" class="form txt token" value="<?php echo pods_generate_key($datatype, $uri_hash, $public_columns, $cache->form_count); ?>" />
1168
+ <input type="hidden" class="form txt uri_hash" value="<?php echo $uri_hash; ?>" />
1169
+ <?php echo apply_filters('pods_showform_save_button', $save_button, $save_button_atts, $this); ?>
1170
+ </div>
1171
+ <?php
1172
+ do_action('pods_showform_post', $pod_id, $public_columns, $label, $this);
1173
+ }
1174
+
1175
+ /**
1176
+ * Does the field have a value? (incl. 0)
1177
+ */
1178
+ function is_val($val) {
1179
+ return (null != $val && false !== $val) ? true : false;
1180
+ }
1181
+
1182
+ /**
1183
+ * Display the pagination controls
1184
+ */
1185
+ function getPagination($label = 'Go to page:') {
1186
+ if ($this->rpp < $this->getTotalRows() && 0 < $this->rpp && false !== $this->pagination) {
1187
+ include PODS_DIR . '/ui/pagination.php';
1188
+ }
1189
+ }
1190
+
1191
+ /**
1192
+ * Display the list filters
1193
+ */
1194
+ function getFilters($filters = null, $label = 'Filter', $action = '') {
1195
+ include PODS_DIR . '/ui/list_filters.php';
1196
+ }
1197
+
1198
+ /**
1199
+ * Build public input form
1200
+ */
1201
+ function publicForm($public_columns = null, $label = 'Save changes', $thankyou_url = null) {
1202
+ include PODS_DIR . '/ui/input_form.php';
1203
+ }
1204
+
1205
+ /**
1206
+ * Build HTML for a single field
1207
+ */
1208
+ function build_field_html($field) {
1209
+ include PODS_DIR . '/ui/input_fields.php';
1210
+ }
1211
+
1212
+ /**
1213
+ * Display the page template
1214
+ */
1215
+ function showTemplate($template, $code = null) {
1216
+ ob_start();
1217
+
1218
+ //pre-template hooks
1219
+ do_action('pods_pre_showtemplate', $template, $code, $this);
1220
+ do_action("pods_pre_showtemplate_$template", $template, $code, $this);
1221
+
1222
+ if (!empty($code))
1223
+ $function_or_file = false;
1224
+ else {
1225
+ $function_or_file = $template;
1226
+ $check_function = false;
1227
+ $check_file = null;
1228
+ if ((!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) && (!defined('PODS_TEMPLATE_FILES') || !PODS_TEMPLATE_FILES))
1229
+ $check_file = false;
1230
+ if (false !== $check_function && false !== $check_file)
1231
+ $function_or_file = pods_function_or_file($function_or_file, $check_function, 'template', $check_file);
1232
+ else
1233
+ $function_or_file = false;
1234
+
1235
+ if (!$function_or_file) {
1236
+ $api = new PodAPI();
1237
+ $params = array('name' => $template);
1238
+ if (!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE)
1239
+ $params = pods_sanitize($params);
1240
+ $code = $api->load_template($params);
1241
+ if (false !== $code && 0 < strlen(trim($code['code'])))
1242
+ $code = $code['code'];
1243
+ else
1244
+ $code = false;
1245
+ }
1246
+ }
1247
+
1248
+ if (empty($code) && false !== $function_or_file && isset($function_or_file['file'])) {
1249
+ // Only detail templates need $this->id
1250
+ if (empty($this->id)) {
1251
+ while ($this->fetchRecord()) {
1252
+ locate_template($function_or_file['file'], true, true);
1253
+ }
1254
+ }
1255
+ else
1256
+ locate_template($function_or_file['file'], true, true);
1257
+ }
1258
+ elseif (!empty($code)) {
1259
+ // Only detail templates need $this->id
1260
+ if (empty($this->id)) {
1261
+ while ($this->fetchRecord()) {
1262
+ echo $this->parse_template_string($code);
1263
+ }
1264
+ }
1265
+ else
1266
+ echo $this->parse_template_string($code);
1267
+ }
1268
+
1269
+ //post-template hooks
1270
+ do_action('pods_post_showtemplate', $template, $code, $this);
1271
+ do_action("pods_post_showtemplate_$template", $template, $code, $this);
1272
+
1273
+ return apply_filters('pods_showtemplate', ob_get_clean(), $template, $code, $this);
1274
+ }
1275
+
1276
+ /**
1277
+ * Parse a template string
1278
+ *
1279
+ * @param string $in The template string to parse
1280
+ * @since 1.8.5
1281
+ */
1282
+ function parse_template_string($in) {
1283
+ ob_start();
1284
+ if (!defined('PODS_DISABLE_EVAL') || !PODS_DISABLE_EVAL)
1285
+ eval("?>$in");
1286
+ else
1287
+ echo $in;
1288
+ $out = ob_get_clean();
1289
+ $out = preg_replace_callback("/({@(.*?)})/m", array($this, "parse_magic_tags"), $out);
1290
+ return apply_filters('pods_parse_template_string', $out, $in, $this);
1291
+ }
1292
+
1293
+ /**
1294
+ * Replace magic tags with their values
1295
+ */
1296
+ function parse_magic_tags($in) {
1297
+ $name = $in[2];
1298
+ $before = $after = $helper = '';
1299
+ if (false !== strpos($name, ',')) {
1300
+ @list($name, $helper, $before, $after) = explode(',', $name);
1301
+ $name = trim($name);
1302
+ $helper = trim($helper);
1303
+ $before = trim($before);
1304
+ $after = trim($after);
1305
+ }
1306
+
1307
+ if ('type' == $name)
1308
+ $value = $this->datatype;
1309
+ elseif ('detail_url' == $name)
1310
+ $value = get_bloginfo('url') . '/' . $this->parse_template_string($this->detail_page);
1311
+ else
1312
+ $value = $this->get_field($name);
1313
+
1314
+ // Use helper if necessary
1315
+ if (!empty($helper))
1316
+ $value = $this->pod_helper($helper, $value, $name);
1317
+
1318
+ // Clean out PHP in case it exists
1319
+ $value = str_replace(array('<' . '?php', '<' . '?', '?' .'>'), array('&lt;?php', '&lt;?', '?&gt;'), $value);
1320
+
1321
+ $value = apply_filters('pods_parse_magic_tags', $value, $name, $helper, $before, $after);
1322
+ if (null != $value && false !== $value)
1323
+ return $before . $value . $after;
1324
+ }
1325
+ }
classes/PodAPI.php ADDED
@@ -0,0 +1,2255 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class PodAPI
3
+ {
4
+ var $snap = false;
5
+ var $dt;
6
+ var $dtname;
7
+ var $format;
8
+ var $fields;
9
+ var $use_pod_id = false; // set to true for save_pod_item to operate off of pod_id (for backwards compatibility with functions using pod_ids)
10
+
11
+ /**
12
+ * Store and retrieve data programatically
13
+ *
14
+ * @param string $dtname (optional) The pod name
15
+ * @param string $format (optional) Format for import/export, "php" or "csv"
16
+ * @since 1.7.1
17
+ */
18
+ function __construct($dtname = null, $format = 'php') {
19
+ $this->dtname = pods_sanitize(trim($dtname));
20
+ $this->format = pods_sanitize(trim($format));
21
+
22
+ if (!empty($this->dtname)) {
23
+ $pod = $this->load_pod(array('name' => $this->dtname));
24
+ if (is_array($pod)) {
25
+ $this->dt = $pod['id'];
26
+ $this->fields = $pod['fields'];
27
+ }
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Throw an error or die (cake or death?)
33
+ *
34
+ * @param string $error Error message
35
+ * @since 1.9.0
36
+ */
37
+ function oh_snap($error) {
38
+ if (false!==$this->snap) {
39
+ throw new Exception($error);
40
+ return false;
41
+ }
42
+ die($error);
43
+ }
44
+
45
+ /**
46
+ * Add or edit a content type
47
+ *
48
+ * $params['id'] int The datatype ID
49
+ * $params['name'] string The datatype name
50
+ * $params['label'] string The datatype label
51
+ * $params['is_toplevel'] bool Display the pod as a top-level admin menu
52
+ * $params['detail_page'] string The URI for single pod items
53
+ * $params['pre_save_helpers'] string Comma-separated list of helper names
54
+ * $params['pre_drop_helpers'] string Comma-separated list of helper names
55
+ * $params['post_save_helpers'] string Comma-separated list of helper names
56
+ * $params['post_drop_helpers'] string Comma-separated list of helper names
57
+ * $params['order'] string Comma-separated list of field IDs
58
+ *
59
+ * @param array $params An associative array of parameters
60
+ * @since 1.7.9
61
+ */
62
+ function save_pod($params) {
63
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
64
+ $params = pods_sanitize($params);
65
+ $params = (object) str_replace('@wp_', '{prefix}', $params);
66
+
67
+ // Set defaults
68
+ $params = (object) array_merge(array('id' => '',
69
+ 'name' => '',
70
+ 'label' => '',
71
+ 'is_toplevel' => 0,
72
+ 'detail_page' => '',
73
+ 'pre_save_helpers' => '',
74
+ 'pre_drop_helpers' => '',
75
+ 'post_save_helpers' => '',
76
+ 'post_drop_helpers' => '',
77
+ 'order' => 0),
78
+ (array) $params);
79
+ if (isset($params->action))
80
+ unset($params->action);
81
+ if (isset($params->_wpnonce))
82
+ unset($params->_wpnonce);
83
+
84
+ // Add new pod
85
+ if (empty($params->id)) {
86
+ $params->name = pods_clean_name($params->name);
87
+ if (empty($params->name)) {
88
+ return $this->oh_snap('<e>Enter a pod name');
89
+ }
90
+ $sql = "SELECT id FROM @wp_pod_types WHERE name = '{$params->name}' LIMIT 1";
91
+ pod_query($sql, 'Duplicate pod name', 'Pod name already exists');
92
+
93
+ $set = array();
94
+ $columns = array();
95
+ foreach ($params as $column => $value) {
96
+ if (in_array($column, array('id', 'order', 'return_pod')))
97
+ continue;
98
+ $columns[] = "`{$column}`";
99
+ $set[] = "'{$value}'";
100
+ }
101
+ $columns = implode(', ', $columns);
102
+ $set = implode(', ', $set);
103
+ $pod_id = pod_query("INSERT INTO @wp_pod_types ({$columns}) VALUES ({$set})", 'Cannot add new pod');
104
+ pod_query("CREATE TABLE `@wp_pod_tbl_{$params->name}` (id int unsigned auto_increment primary key, name varchar(128), slug varchar(128)) DEFAULT CHARSET utf8", 'Cannot add pod database table');
105
+ pod_query("INSERT INTO @wp_pod_fields (datatype, name, label, comment, coltype, required, weight) VALUES ({$pod_id}, 'name', 'Name', '', 'txt', 1, 0),({$pod_id}, 'slug', 'Permalink', 'Leave blank to auto-generate', 'slug', 0, 1)");
106
+ if (!isset($params->return_pod) || false === $params->return_pod)
107
+ return $pod_id;
108
+ }
109
+ // Edit existing pod
110
+ else {
111
+ $pod_id = $params->id;
112
+ $set = array();
113
+ foreach ($params as $column => $value) {
114
+ if (in_array($column, array('id', 'name', 'order', 'return_pod')))
115
+ continue;
116
+ $set[] = "`{$column}` = '{$value}'";
117
+ }
118
+ if (!empty($set)) {
119
+ $set = implode(', ', $set);
120
+ $sql = "
121
+ UPDATE
122
+ `@wp_pod_types`
123
+ SET
124
+ {$set}
125
+ WHERE
126
+ `id` = {$pod_id}
127
+ LIMIT
128
+ 1
129
+ ";
130
+ pod_query($sql, 'Cannot change Pod settings');
131
+ }
132
+
133
+ $weight = 0;
134
+ $order = (false !== strpos($params->order, ',')) ? explode(',', $params->order) : array($params->order);
135
+ foreach ($order as $field_id) {
136
+ pod_query("UPDATE `@wp_pod_fields` SET `weight` = '{$weight}' WHERE `id` = '{$field_id}' LIMIT 1", 'Cannot change column order');
137
+ $weight++;
138
+ }
139
+ if (!isset($params->return_pod) || false === $params->return_pod)
140
+ return $pod_id;
141
+ }
142
+ return $this->load_pod(array('id' => $pod_id));
143
+ }
144
+
145
+ /**
146
+ * Add or edit a column within a content type
147
+ *
148
+ * $params['id'] int The field ID
149
+ * $params['name'] string The field name
150
+ * $params['datatype'] int The datatype ID
151
+ * $params['dtname'] string The datatype name
152
+ * $params['coltype'] string The column type ("txt", "desc", "pick", etc)
153
+ * $params['sister_field_id'] int (optional) The related field ID
154
+ * $params['pickval'] string The related PICK pod name
155
+ * $params['label'] string The field label
156
+ * $params['comment'] string The field comment
157
+ * $params['display_helper'] string (optional) The display helper name
158
+ * $params['input_helper'] string (optional) The input helper name
159
+ * $params['pick_filter'] string (optional) WHERE clause for PICK fields
160
+ * $params['pick_orderby'] string (optional) ORDER BY clause for PICK fields
161
+ * $params['required'] bool Is the field required?
162
+ * $params['unique'] bool Is the field unique?
163
+ * $params['multiple'] bool Is the PICK dropdown a multi-select?
164
+ *
165
+ * @param array $params An associative array of parameters
166
+ * @since 1.7.9
167
+ */
168
+ function save_column($params) {
169
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
170
+ $params = pods_sanitize($params);
171
+ $params = (object) str_replace('@wp_', '{prefix}', $params);
172
+
173
+ // Set defaults
174
+ $params = (object) array_merge(array('id' => '',
175
+ 'name' => '',
176
+ 'datatype' => '',
177
+ 'dtname' => '',
178
+ 'coltype' => 'txt',
179
+ 'sister_field_id' => 0,
180
+ 'pickval' => '',
181
+ 'label' => '',
182
+ 'comment' => '',
183
+ 'display_helper' => '',
184
+ 'input_helper' => '',
185
+ 'pick_filter' => '',
186
+ 'pick_orderby' => '',
187
+ 'required' => 0,
188
+ 'unique' => 0,
189
+ 'multiple' => 0),
190
+ (array) $params);
191
+
192
+ // Force Types
193
+ $params->id = absint($params->id);
194
+ $params->sister_field_id = absint($params->sister_field_id);
195
+ $params->required = absint($params->required);
196
+ $params->unique = absint($params->unique);
197
+ $params->multiple = absint($params->multiple);
198
+
199
+ $dbtypes = array(
200
+ 'bool' => 'bool default 0',
201
+ 'date' => 'datetime',
202
+ 'num' => 'decimal(12,2)',
203
+ 'txt' => 'varchar(128)',
204
+ 'slug' => 'varchar(128)',
205
+ 'code' => 'longtext',
206
+ 'desc' => 'longtext'
207
+ );
208
+ $dbtypes = apply_filters('pods_column_dbtypes', $dbtypes);
209
+
210
+ // Add new column
211
+ if (empty($params->id)) {
212
+ $params->name = pods_clean_name($params->name);
213
+ if (empty($params->name)) {
214
+ return $this->oh_snap('<e>Enter a column name');
215
+ }
216
+ elseif (in_array($params->name, array('id', 'name', 'type', 'created', 'modified', 'p', 't'))) {
217
+ return $this->oh_snap("<e>$params->name is a reserved name");
218
+ }
219
+ $sql = "SELECT id FROM @wp_pod_fields WHERE datatype = $params->datatype AND name = '$params->name' LIMIT 1";
220
+ pod_query($sql, 'Cannot get fields', 'Column by this name already exists');
221
+
222
+ if ('slug' == $params->coltype) {
223
+ $sql = "SELECT id FROM @wp_pod_fields WHERE datatype = $params->datatype AND coltype = 'slug' LIMIT 1";
224
+ pod_query($sql, 'Too many permalinks', 'This pod already has a permalink column');
225
+ }
226
+
227
+ // Sink the new column to the bottom of the list
228
+ $weight = 0;
229
+ $result = pod_query("SELECT weight FROM @wp_pod_fields WHERE datatype = $params->datatype ORDER BY weight DESC LIMIT 1");
230
+ if (0 < pods_mysql_num_rows($result)) {
231
+ $row = pods_mysql_fetch_assoc($result);
232
+ $weight = (int) $row['weight'] + 1;
233
+ }
234
+
235
+ if ('pick' != $params->coltype) {
236
+ $params->pickval = '';
237
+ $params->pick_filter = '';
238
+ $params->pick_orderby = '';
239
+ $params->sister_field_id = 0;
240
+ $params->multiple = 0;
241
+ }
242
+
243
+ $field_id = pod_query("INSERT INTO @wp_pod_fields (datatype, name, label, comment, display_helper, input_helper, coltype, pickval, pick_filter, pick_orderby, sister_field_id, required, `unique`, `multiple`, weight) VALUES ('$params->datatype', '$params->name', '$params->label', '$params->comment', '$params->display_helper', '$params->input_helper', '$params->coltype', '$params->pickval', '$params->pick_filter', '$params->pick_orderby', '$params->sister_field_id', '$params->required', '$params->unique', '$params->multiple', '$weight')", 'Cannot add new field');
244
+
245
+ if ('pick' != $params->coltype && 'file' != $params->coltype) {
246
+ $dbtype = $dbtypes[$params->coltype];
247
+ pod_query("ALTER TABLE `@wp_pod_tbl_$params->dtname` ADD COLUMN `$params->name` $dbtype", 'Cannot create new column');
248
+ }
249
+ else {
250
+ pod_query("UPDATE @wp_pod_fields SET sister_field_id = '{$field_id}' WHERE id = '{$params->sister_field_id}' LIMIT 1", 'Cannot update sister field');
251
+ }
252
+ }
253
+ // Edit existing column
254
+ else {
255
+ if ('id' == $params->name) {
256
+ return $this->oh_snap("<e>$params->name is not editable.");
257
+ }
258
+
259
+ $sql = "SELECT id FROM @wp_pod_fields WHERE datatype = $params->datatype AND id != $params->id AND name = '$params->name' LIMIT 1";
260
+ pod_query($sql, 'Column already exists', "$params->name already exists.");
261
+
262
+ $sql = "SELECT name, coltype FROM @wp_pod_fields WHERE id = $params->id LIMIT 1";
263
+ $result = pod_query($sql);
264
+
265
+ if (0 < pods_mysql_num_rows($result)) {
266
+ $row = pods_mysql_fetch_assoc($result);
267
+ $old_coltype = $row['coltype'];
268
+ $old_name = $row['name'];
269
+
270
+ $dbtype = $dbtypes[$params->coltype];
271
+ $pickval = ('pick' != $params->coltype || empty($params->pickval)) ? '' : "$params->pickval";
272
+
273
+ if ($params->coltype != $old_coltype) {
274
+ if ('pick' == $params->coltype || 'file' == $params->coltype) {
275
+ if ('pick' != $old_coltype && 'file' != $old_coltype) {
276
+ pod_query("ALTER TABLE `@wp_pod_tbl_$params->dtname` DROP COLUMN `$old_name`");
277
+ }
278
+ }
279
+ elseif ('pick' == $old_coltype || 'file' == $old_coltype) {
280
+ pod_query("ALTER TABLE `@wp_pod_tbl_$params->dtname` ADD COLUMN `$params->name` $dbtype", 'Cannot create column');
281
+ pod_query("UPDATE @wp_pod_fields SET sister_field_id = NULL WHERE sister_field_id = $params->id");
282
+ pod_query("DELETE FROM @wp_pod_rel WHERE field_id = $params->id");
283
+ }
284
+ else {
285
+ pod_query("ALTER TABLE `@wp_pod_tbl_$params->dtname` CHANGE `$old_name` `$params->name` $dbtype");
286
+ }
287
+ }
288
+ elseif ($params->name != $old_name && 'pick' != $params->coltype && 'file' != $params->coltype) {
289
+ pod_query("ALTER TABLE `@wp_pod_tbl_$params->dtname` CHANGE `$old_name` `$params->name` $dbtype");
290
+ }
291
+
292
+ if ('pick' != $params->coltype) {
293
+ $params->pickval = '';
294
+ $params->pick_filter = '';
295
+ $params->pick_orderby = '';
296
+ $params->sister_field_id = 0;
297
+ $params->multiple = 0;
298
+ }
299
+
300
+ $sql = "
301
+ UPDATE
302
+ @wp_pod_fields
303
+ SET
304
+ name = '$params->name',
305
+ label = '$params->label',
306
+ comment = '$params->comment',
307
+ coltype = '$params->coltype',
308
+ pickval = '$params->pickval',
309
+ display_helper = '$params->display_helper',
310
+ input_helper = '$params->input_helper',
311
+ pick_filter = '$params->pick_filter',
312
+ pick_orderby = '$params->pick_orderby',
313
+ sister_field_id = '$params->sister_field_id',
314
+ required = '$params->required',
315
+ `unique` = '$params->unique',
316
+ `multiple` = '$params->multiple'
317
+ WHERE
318
+ id = $params->id
319
+ LIMIT
320
+ 1
321
+ ";
322
+ pod_query($sql, 'Cannot edit column');
323
+ }
324
+ }
325
+ }
326
+
327
+ /**
328
+ * Add or edit a Pod Template
329
+ *
330
+ * $params['id'] int The template ID
331
+ * $params['name'] string The template name
332
+ * $params['code'] string The template code
333
+ *
334
+ * @param array $params An associative array of parameters
335
+ * @since 1.7.9
336
+ */
337
+ function save_template($params) {
338
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
339
+ $params = pods_sanitize($params);
340
+ $params = (object) str_replace('@wp_', '{prefix}', $params);
341
+
342
+ // Set defaults
343
+ $params = (object) array_merge(array('id' => '',
344
+ 'name' => '',
345
+ 'code' => ''),
346
+ (array) $params);
347
+
348
+ // Force Types
349
+ $params->id = absint($params->id);
350
+
351
+ // Add new template
352
+ if (empty($params->id)) {
353
+ if (empty($params->name)) {
354
+ return $this->oh_snap('<e>Enter a template name');
355
+ }
356
+
357
+ $sql = "SELECT id FROM @wp_pod_templates WHERE name = '$params->name' LIMIT 1";
358
+ pod_query($sql, 'Cannot get Templates', 'Template by this name already exists');
359
+ $template_id = pod_query("INSERT INTO @wp_pod_templates (name, code) VALUES ('$params->name', '$params->code')", 'Cannot add new template');
360
+
361
+ return $template_id; // return
362
+ }
363
+ // Edit existing template
364
+ else {
365
+ $maybename = '';
366
+ if (!empty($params->name))
367
+ $maybename = "name = '$params->name',";
368
+ pod_query("UPDATE @wp_pod_templates SET $maybename code = '$params->code' WHERE id = $params->id LIMIT 1");
369
+ }
370
+ }
371
+
372
+ /**
373
+ * Add or edit a Pod Page
374
+ *
375
+ * $params['id'] int The page ID
376
+ * $params['uri'] string The page URI
377
+ * $params['title'] string The page title
378
+ * $params['page_template'] string The page template
379
+ * $params['phpcode'] string The page code
380
+ * $params['precode'] string The page pre code
381
+ *
382
+ * @param array $params An associative array of parameters
383
+ * @since 1.7.9
384
+ */
385
+ function save_page($params) {
386
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
387
+ $params = pods_sanitize($params);
388
+ $params = (object) str_replace('@wp_', '{prefix}', $params);
389
+
390
+ // Set defaults
391
+ $params = (object) array_merge(array('id' => '',
392
+ 'uri' => '',
393
+ 'page_title' => '',
394
+ 'page_template' => '',
395
+ 'phpcode' => '',
396
+ 'precode' => ''),
397
+ (array) $params);
398
+
399
+ // Force Types
400
+ $params->id = absint($params->id);
401
+
402
+ // Add new page
403
+ if (empty($params->id)) {
404
+ if (empty($params->uri)) {
405
+ return $this->oh_snap('<e>Enter a page URI');
406
+ }
407
+ // normalize URI (remove outside /
408
+ $params->uri = trim($params->uri,'/');
409
+ $sql = "SELECT id FROM @wp_pod_pages WHERE uri = '$params->uri' LIMIT 1";
410
+ pod_query($sql, 'Cannot get Pod Pages', 'Page by this URI already exists');
411
+ $page_id = pod_query("INSERT INTO @wp_pod_pages (uri, title, page_template, phpcode, precode) VALUES ('$params->uri', '$params->page_title', '$params->page_template', '$params->phpcode', '$params->precode')", 'Cannot add new page');
412
+ return $page_id; // return
413
+ }
414
+ // Edit existing page
415
+ else {
416
+ $maybename = '';
417
+ if (!empty($params->uri))
418
+ $maybename = "uri = '$params->uri',";
419
+ pod_query("UPDATE @wp_pod_pages SET $maybename title = '$params->page_title', page_template = '$params->page_template', phpcode = '$params->phpcode', precode = '$params->precode' WHERE id = $params->id LIMIT 1");
420
+ }
421
+ }
422
+
423
+ /**
424
+ * Add or edit a Pod Helper
425
+ *
426
+ * $params['id'] int The helper ID
427
+ * $params['name'] string The helper name
428
+ * $params['helper_type'] string The helper type ("pre_save", "display", etc)
429
+ * $params['phpcode'] string The helper code
430
+ *
431
+ * @param array $params An associative array of parameters
432
+ * @since 1.7.9
433
+ */
434
+ function save_helper($params) {
435
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
436
+ $params = pods_sanitize($params);
437
+ $params = (object) str_replace('@wp_', '{prefix}', $params);
438
+
439
+ // Set defaults
440
+ $params = (object) array_merge(array('id' => '',
441
+ 'name' => '',
442
+ //'helper_type' => 'display',
443
+ 'phpcode' => ''),
444
+ (array) $params);
445
+
446
+ // Force Types
447
+ $params->id = absint($params->id);
448
+
449
+ // Add new helper
450
+ if (empty($params->id)) {
451
+ if (empty($params->name)) {
452
+ return $this->oh_snap('<e>Enter a helper name');
453
+ }
454
+ if (!isset($params->helper_type) || empty($params->helper_type))
455
+ $params->helper_type = 'display';
456
+
457
+ $sql = "SELECT id FROM @wp_pod_helpers WHERE name = '$params->name' LIMIT 1";
458
+ pod_query($sql, 'Cannot get helpers', 'helper by this name already exists');
459
+ $helper_id = pod_query("INSERT INTO @wp_pod_helpers (name, helper_type, phpcode) VALUES ('$params->name', '$params->helper_type', '$params->phpcode')", 'Cannot add new helper');
460
+ return $helper_id; // return
461
+ }
462
+ // Edit existing helper
463
+ else {
464
+ $maybename = '';
465
+ if (isset($params->name) && !empty($params->name))
466
+ $maybename = "name = '$params->name',";
467
+ $maybetype = '';
468
+ if (isset($params->helper_type) && !empty($params->helper_type))
469
+ $maybetype = "helper_type = '$params->helper_type',";
470
+ pod_query("UPDATE @wp_pod_helpers SET {$maybename} {$maybetype} phpcode = '$params->phpcode' WHERE id = $params->id LIMIT 1");
471
+ }
472
+ }
473
+
474
+ /**
475
+ * Save the entire role structure
476
+ *
477
+ * @param array $params An associative array of parameters
478
+ * @since 1.7.9
479
+ */
480
+ function save_roles($params) {
481
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
482
+ $params = pods_sanitize($params);
483
+ $roles = array();
484
+ foreach ($params as $key => $val) {
485
+ if ('action' != $key) {
486
+ $tmp = empty($val) ? array() : explode(',', $val);
487
+ $roles[$key] = $tmp;
488
+ }
489
+ }
490
+ delete_option('pods_roles');
491
+ add_option('pods_roles', serialize($roles));
492
+ }
493
+
494
+ /**
495
+ * Retrieve an associative array of table values
496
+ *
497
+ * $params['table'] string The table name (default: "types")
498
+ * $params['columns'] string Comma-separated string of columns (default: "*")
499
+ * $params['orderby'] string MySQL ORDER BY clause (default: "id ASC")
500
+ * $params['where'] string MySQL WHERE clause (default: 1)
501
+ * $params['array_key'] string The key column for the returned associative array (default: "id")
502
+ *
503
+ * @param array $params An associative array of parameters
504
+ * @return array The table data array
505
+ * @since 1.8.5
506
+ */
507
+ function get_table_data($params) {
508
+ $params = is_array($params) ? $params : array();
509
+ $defaults = array(
510
+ 'table' => 'types',
511
+ 'columns' => '*',
512
+ 'orderby' => 'id ASC',
513
+ 'where' => 1,
514
+ 'array_key' => 'id'
515
+ );
516
+ $params = (object) array_merge($defaults, $params);
517
+ $result = pod_query("SELECT $params->columns FROM @wp_pod_$params->table WHERE $params->where ORDER BY $params->orderby");
518
+ if (0 < pods_mysql_num_rows($result)) {
519
+ while ($row = pods_mysql_fetch_assoc($result)) {
520
+ $data[$row[$params->array_key]] = $row;
521
+ }
522
+ return $data;
523
+ }
524
+ }
525
+
526
+ /**
527
+ * Add or edit a single pod item
528
+ *
529
+ * $params['datatype'] string The datatype name
530
+ * $params['columns'] array (optional) Associative array of column names + values
531
+ * $params['data'] array (optional) Associative array of a set of associative arrays of column names + values (for bulk operations)
532
+ * $params['pod_id'] int The item's ID from the wp_pod table (or alternatively use the tbl_row_id parameter instead)
533
+ * $params['tbl_row_id'] int The item's ID from the wp_pod_tbl_* table (or alternatively use the pod_id parameter instead)
534
+ *
535
+ * @param array $params An associative array of parameters
536
+ * @return int The table row ID
537
+ * @since 1.7.9
538
+ */
539
+ function save_pod_item($params) {
540
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
541
+ $params = pods_sanitize($params);
542
+ $params = (object) str_replace('@wp_', '{prefix}', $params);
543
+
544
+ // Support for multiple save_pod_item operations at the same time
545
+ if (isset($params->data) && !empty($params->data) && is_array($params->data)) {
546
+ $ids = array();
547
+ $new_params = $params;
548
+ unset($new_params->data);
549
+ foreach ($params->data as $columns){
550
+ $new_params->columns = $columns;
551
+ $ids[] = $this->save_pod_item($new_params);
552
+ }
553
+ return $ids;
554
+ }
555
+
556
+ // Support for bulk edit
557
+ if (isset($params->tbl_row_id) && !empty($params->tbl_row_id) && is_array($params->tbl_row_id)) {
558
+ $ids = array();
559
+ $new_params = $params;
560
+ foreach ($params->tbl_row_id as $tbl_row_id){
561
+ $new_params->tbl_row_id = $tbl_row_id;
562
+ $ids[] = $this->save_pod_item($new_params);
563
+ }
564
+ return $ids;
565
+ }
566
+
567
+ // Allow Helpers to bypass subsequent helpers in recursive save_pod_item calls
568
+ $bypass_helpers = false;
569
+ if (isset($params->bypass_helpers) && true === (boolean) $params->bypass_helpers) {
570
+ $bypass_helpers = true;
571
+ }
572
+
573
+ // Get array of datatypes
574
+ $datatypes = $this->get_table_data(array('array_key' => 'name', 'columns' => 'id, name'));
575
+ $params->datatype_id = (int) $datatypes[$params->datatype]['id'];
576
+
577
+ // Get the datatype fields
578
+ $opts = array('table' => 'fields', 'where' => "datatype = {$params->datatype_id}", 'orderby' => 'weight', 'array_key' => 'name');
579
+ $columns = $this->get_table_data($opts);
580
+
581
+ // Find the active columns (loop through $params->columns to retain order)
582
+ if (!empty($params->columns) && is_array($params->columns)) {
583
+ foreach ($params->columns as $column_name => $column_val) {
584
+ // Support for Pre Key/Value Parameters in previous Pods versions
585
+ if (isset($params->name)&&isset($params->$column_val)) {
586
+ $column_name = $column_val;
587
+ $column_val = $params->$column_name;
588
+ }
589
+ if (isset($columns[$column_name])) {
590
+ $columns[$column_name]['value'] = $column_val;
591
+ $active_columns[] = $column_name;
592
+ }
593
+ }
594
+ unset($params->columns);
595
+ }
596
+
597
+ // Load all helpers
598
+ $result = pod_query("SELECT pre_save_helpers, post_save_helpers FROM @wp_pod_types WHERE id = {$params->datatype_id}");
599
+ $row = pods_mysql_fetch_assoc($result);
600
+ $params->pre_save_helpers = explode(',', $row['pre_save_helpers']);
601
+ $params->post_save_helpers = explode(',', $row['post_save_helpers']);
602
+
603
+ // Allow Helpers to know what's going on, are we adding or saving?
604
+ $is_new_item = false;
605
+ if (!empty($params->tbl_row_id)) {
606
+ $result = pod_query("SELECT p.id FROM @wp_pod p INNER JOIN @wp_pod_types t ON t.id = p.datatype WHERE p.tbl_row_id = $params->tbl_row_id AND t.name = '$params->datatype' LIMIT 1",'Pod item not found',null,'Pod item not found');
607
+ $params->pod_id = pods_mysql_result($result, 0);
608
+ }
609
+ elseif (!empty($params->pod_id)) {
610
+ $result = pod_query("SELECT tbl_row_id FROM @wp_pod WHERE id = $params->pod_id LIMIT 1",'Item not found',null,'Item not found');
611
+ $params->tbl_row_id = pods_mysql_result($result, 0);
612
+ }
613
+ else
614
+ $is_new_item = true;
615
+
616
+ // Plugin hook
617
+ do_action('pods_pre_save_pod_item', $params, $columns);
618
+
619
+ // Call any pre-save helpers (if not bypassed)
620
+ if(!$bypass_helpers && !empty($params->pre_save_helpers)) {
621
+ foreach ($params->pre_save_helpers as $helper) {
622
+ $function_or_file = $helper;
623
+ $check_function = $function_or_file;
624
+ if ((!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) && (!defined('PODS_HELPER_FUNCTIONS') || !PODS_HELPER_FUNCTIONS))
625
+ $check_function = false;
626
+ $check_file = null;
627
+ if ((!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) && (!defined('PODS_HELPER_FILES') || !PODS_HELPER_FILES))
628
+ $check_file = false;
629
+ if (false !== $check_function && false !== $check_file)
630
+ $function_or_file = pods_function_or_file($function_or_file, $check_function, 'helper', $check_file);
631
+ else
632
+ $function_or_file = false;
633
+
634
+ $content = false;
635
+ if (!$function_or_file) {
636
+ $api = new PodAPI();
637
+ $params_helper = array('name' => $helper, 'type' => 'pre_save');
638
+ if (!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE)
639
+ $params_helper = pods_sanitize($params_helper);
640
+ $content = $api->load_helper($params_helper);
641
+ if (false !== $content && 0 < strlen(trim($content['phpcode'])))
642
+ $content = $content['phpcode'];
643
+ else
644
+ $content = false;
645
+ }
646
+
647
+ if (false === $content && false !== $function_or_file && isset($function_or_file['function']))
648
+ $function_or_file['function']($params, $columns, $this);
649
+ elseif (false === $content && false !== $function_or_file && isset($function_or_file['file']))
650
+ locate_template($function_or_file['file'], true, true);
651
+ elseif (false !== $content) {
652
+ if (!defined('PODS_DISABLE_EVAL') || PODS_DISABLE_EVAL)
653
+ eval("?>$content");
654
+ else
655
+ echo $content;
656
+ }
657
+ }
658
+ }
659
+
660
+ // Loop through each active column, validating and preparing the table data
661
+ foreach ($active_columns as $key) {
662
+ $val = $columns[$key]['value'];
663
+ $type = $columns[$key]['coltype'];
664
+ $label = $columns[$key]['label'];
665
+ $label = empty($label) ? $key : $label;
666
+
667
+ // Verify required fields
668
+ if (1 == $columns[$key]['required']) {
669
+ if ('' == $val || null == $val) {
670
+ return $this->oh_snap("<e>$label is empty.");
671
+ }
672
+ elseif ('num' == $type && !is_numeric($val)) {
673
+ return $this->oh_snap("<e>$label is not numeric.");
674
+ }
675
+ }
676
+ // Verify unique fields
677
+ if (1 == $columns[$key]['unique'] && !in_array($type, array('pick', 'file'))) {
678
+ $exclude = '';
679
+ if (!empty($params->pod_id)) {
680
+ $result = pod_query("SELECT tbl_row_id FROM @wp_pod WHERE id = '$params->pod_id' AND datatype = '{$params->datatype_id}' LIMIT 1");
681
+ if (0 < pods_mysql_num_rows($result)) {
682
+ $exclude = 'AND id != ' . pods_mysql_result($result, 0);
683
+ }
684
+ }
685
+
686
+ // Trigger an error if not unique
687
+ $sql = "SELECT id FROM `@wp_pod_tbl_$params->datatype` WHERE `$key` = '$val' $exclude LIMIT 1";
688
+ pod_query($sql, 'Not unique', "$label needs to be unique.");
689
+ }
690
+ // Verify slug columns
691
+ if ('slug' == $type) {
692
+ $slug_val = empty($val) ? $columns['name']['value'] : $val;
693
+ $val = pods_unique_slug($slug_val, $key, $params);
694
+ }
695
+
696
+ // Prepare all table (non-relational) data
697
+ if (!in_array($type, array('pick', 'file'))) {
698
+ if ('num' == $type)
699
+ $val = floatval($val);
700
+ elseif ('bool' == $type)
701
+ $val = min(absint($val), 1);
702
+ if ('num' != $type)
703
+ $val = "'$val'";
704
+ $table_data[] = "`$key` = $val";
705
+ }
706
+ // Store relational column data to be looped through later
707
+ else {
708
+ $rel_columns[$type][$key] = $val;
709
+ }
710
+ }
711
+
712
+ // Create the pod_id if it doesn't exist
713
+ if (empty($params->pod_id)&&empty($params->tbl_row_id)) {
714
+ $current_time = current_time('mysql');
715
+ $user = 0;
716
+ if (is_user_logged_in()) {
717
+ global $user_ID;
718
+ get_currentuserinfo();
719
+ $user = $user_ID;
720
+ }
721
+ $name = $params->datatype;
722
+ if (isset($params->name))
723
+ $name = $params->name;
724
+ if (isset($params->columns) && is_array($params->columns) && isset($params->columns['name']))
725
+ $name = $params->columns['name'];
726
+ $sql = "INSERT INTO @wp_pod (datatype, name, created, modified, author_id) VALUES ('{$params->datatype_id}', '$name', '$current_time', '$current_time', '$user')";
727
+ $params->pod_id = pod_query($sql, 'Cannot add new content');
728
+ $params->tbl_row_id = pod_query("INSERT INTO `@wp_pod_tbl_$params->datatype` (name) VALUES (NULL)", 'Cannot add new table row');
729
+ }
730
+
731
+ // Save the table row
732
+ if (isset($table_data)) {
733
+ $table_data = implode(',', $table_data);
734
+ pod_query("UPDATE `@wp_pod_tbl_$params->datatype` SET $table_data WHERE id = $params->tbl_row_id LIMIT 1");
735
+ }
736
+
737
+ // Update wp_pod
738
+ $item_name = isset($columns['name']['value']) ? ", name = '" . $columns['name']['value'] . "'" : '';
739
+ pod_query("UPDATE @wp_pod SET tbl_row_id = $params->tbl_row_id, datatype = $params->datatype_id, modified = '" . current_time('mysql') . "' $item_name WHERE id = $params->pod_id LIMIT 1");
740
+
741
+ // Save relational column data
742
+ if (isset($rel_columns)) {
743
+ // E.g. $rel_columns['pick']['related_events'] = '3,15';
744
+ foreach ($rel_columns as $rel_type => $rel_data) {
745
+ foreach ($rel_data as $rel_name => $rel_values) {
746
+ $field_id = $columns[$rel_name]['id'];
747
+
748
+ // Convert values from a comma-separated string into an array
749
+ if (empty($rel_values))
750
+ $rel_values = array();
751
+ elseif (!is_array($rel_values))
752
+ $rel_values = explode(',', $rel_values);
753
+
754
+ // Remove existing relationships
755
+ pod_query("DELETE FROM @wp_pod_rel WHERE pod_id = $params->pod_id AND field_id = $field_id");
756
+
757
+ // File relationships
758
+ if ('file' == $rel_type) {
759
+ $rel_weight = 0;
760
+ foreach ($rel_values as $related_id) {
761
+ $related_id = absint($related_id);
762
+ if (empty($related_id))
763
+ continue;
764
+ pod_query("INSERT INTO @wp_pod_rel (pod_id, field_id, tbl_row_id, weight) VALUES ($params->pod_id, $field_id, $related_id, $rel_weight)");
765
+ $rel_weight++;
766
+ }
767
+ }
768
+ // Pick relationships
769
+ elseif ('pick' == $rel_type) {
770
+ $pickval = $columns[$rel_name]['pickval'];
771
+ $sister_datatype_id = 0;
772
+ $sister_field_id = 0;
773
+ if (!in_array($pickval, array('wp_taxonomy', 'wp_post', 'wp_page', 'wp_user')) && isset($datatypes[$pickval])) {
774
+ $sister_datatype_id = $datatypes[$pickval]['id'];
775
+ if (!empty($columns[$rel_name]['sister_field_id']))
776
+ $sister_field_id = $columns[$rel_name]['sister_field_id'];
777
+ }
778
+ $sister_pod_ids = array();
779
+
780
+ // Delete parent and sister rels
781
+ if (!empty($sister_field_id)) {
782
+ // Get sister pod IDs (a sister pod's sister pod is the parent pod)
783
+ $result = pod_query("SELECT pod_id FROM @wp_pod_rel WHERE sister_pod_id = $params->pod_id");
784
+ if (0 < pods_mysql_num_rows($result)) {
785
+ while ($row = pods_mysql_fetch_assoc($result)) {
786
+ $sister_pod_ids[] = $row['pod_id'];
787
+ }
788
+ $sister_pod_ids = implode(',', $sister_pod_ids);
789
+
790
+ // Delete the sister pod relationship
791
+ pod_query("DELETE FROM @wp_pod_rel WHERE pod_id IN ($sister_pod_ids) AND sister_pod_id = $params->pod_id AND field_id = $sister_field_id");
792
+ }
793
+ }
794
+
795
+ // Add rel values
796
+ $rel_weight = 0;
797
+ foreach ($rel_values as $related_id) {
798
+ $related_id = absint($related_id);
799
+ if (empty($related_id))
800
+ continue;
801
+ $sister_pod_id = 0;
802
+ if (!empty($sister_field_id) && !empty($sister_datatype_id)) {
803
+ $result = pod_query("SELECT id FROM @wp_pod WHERE datatype = $sister_datatype_id AND tbl_row_id = $related_id LIMIT 1");
804
+ if (0 < pods_mysql_num_rows($result)) {
805
+ $sister_pod_id = pods_mysql_result($result, 0);
806
+ pod_query("INSERT INTO @wp_pod_rel (pod_id, sister_pod_id, field_id, tbl_row_id, weight) VALUES ($sister_pod_id, $params->pod_id, $sister_field_id, $params->tbl_row_id, $rel_weight)", 'Cannot add sister relationships');
807
+ }
808
+ }
809
+ pod_query("INSERT INTO @wp_pod_rel (pod_id, sister_pod_id, field_id, tbl_row_id, weight) VALUES ($params->pod_id, $sister_pod_id, $field_id, $related_id, $rel_weight)", 'Cannot add relationships');
810
+ $rel_weight++;
811
+ }
812
+ }
813
+ }
814
+ }
815
+ }
816
+
817
+ // Plugin hook
818
+ do_action('pods_post_save_pod_item', $params, $columns);
819
+
820
+ // Call any post-save helpers (if not bypassed)
821
+ if(!$bypass_helpers && !empty($params->post_save_helpers)) {
822
+ foreach ($params->post_save_helpers as $helper) {
823
+ $function_or_file = $helper;
824
+ $check_function = $function_or_file;
825
+ if ((!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) && (!defined('PODS_HELPER_FUNCTIONS') || !PODS_HELPER_FUNCTIONS))
826
+ $check_function = false;
827
+ $check_file = null;
828
+ if ((!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) && (!defined('PODS_HELPER_FILES') || !PODS_HELPER_FILES))
829
+ $check_file = false;
830
+ if (false !== $check_function && false !== $check_file)
831
+ $function_or_file = pods_function_or_file($function_or_file, $check_function, 'helper', $check_file);
832
+ else
833
+ $function_or_file = false;
834
+
835
+ $content = false;
836
+ if (!$function_or_file) {
837
+ $api = new PodAPI();
838
+ $params_helper = array('name' => $helper, 'type' => 'post_save');
839
+ if (!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE)
840
+ $params_helper = pods_sanitize($params_helper);
841
+ $content = $api->load_helper($params_helper);
842
+ if (false !== $content && 0 < strlen(trim($content['phpcode'])))
843
+ $content = $content['phpcode'];
844
+ else
845
+ $content = false;
846
+ }
847
+
848
+ if (false === $content && false !== $function_or_file && isset($function_or_file['function']))
849
+ $function_or_file['function']($params, $columns, $this);
850
+ elseif (false === $content && false !== $function_or_file && isset($function_or_file['file']))
851
+ locate_template($function_or_file['file'], true, true);
852
+ elseif (false !== $content) {
853
+ if (!defined('PODS_DISABLE_EVAL') || PODS_DISABLE_EVAL)
854
+ eval("?>$content");
855
+ else
856
+ echo $content;
857
+ }
858
+ }
859
+ }
860
+
861
+ // Success! Return the id
862
+ if (false===$this->use_pod_id) {
863
+ return $params->tbl_row_id;
864
+ }
865
+ return $params->pod_id;
866
+ }
867
+
868
+ /**
869
+ * Duplicate a pod item
870
+ *
871
+ * $params['datatype'] string The datatype name
872
+ * $params['tbl_row_id'] int The item's ID from the wp_pod_tbl_* table
873
+ *
874
+ * @param array $params An associative array of parameters
875
+ * @return int The table row ID
876
+ * @since 1.12
877
+ */
878
+ function duplicate_pod_item($params) {
879
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
880
+ $params = pods_sanitize($params);
881
+ $params = (object) $params;
882
+
883
+ $id = false;
884
+ $columns = $this->fields;
885
+ if (empty($columns) || $this->dtname != $params->datatype) {
886
+ $pod = $this->load_pod(array('name' => $params->datatype));
887
+ $columns = $pod['fields'];
888
+ if (null === $this->dtname) {
889
+ $this->dtname = $pod['name'];
890
+ $this->dt = $pod['id'];
891
+ $this->fields = $pod['fields'];
892
+ }
893
+ }
894
+ $pod = new Pod($params->datatype, $params->tbl_row_id);
895
+ if (!empty($pod->data)) {
896
+ $params = array('datatype' => $params->datatype,
897
+ 'columns' => array());
898
+ foreach ($columns as $column) {
899
+ $field = $column['name'];
900
+ if ('pick' == $column['coltype']) {
901
+ $field = $column . '.id';
902
+ if ('wp_taxonomy' == $column['pickval'])
903
+ $field = $column . '.term_id';
904
+ }
905
+ if ('file' == $column['coltype'])
906
+ $field = $column . '.ID';
907
+ $value = $pod->get_field($field);
908
+ if (0 < strlen($value))
909
+ $params['columns'][$column['name']] = $value;
910
+ }
911
+ $params = apply_filters('duplicate_pod_item', $params, $pod->datatype, $pod->get_field('id'));
912
+ $id = $this->save_pod_item(pods_sanitize($params));
913
+ }
914
+ return $id;
915
+ }
916
+
917
+ /**
918
+ * Export a pod item
919
+ *
920
+ * $params['datatype'] string The datatype name
921
+ * $params['tbl_row_id'] int The item's ID from the wp_pod_tbl_* table
922
+ *
923
+ * @param array $params An associative array of parameters
924
+ * @return int The table row ID
925
+ * @since 1.12
926
+ */
927
+ function export_pod_item($params) {
928
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
929
+ $params = pods_sanitize($params);
930
+ $params = (object) $params;
931
+
932
+ $data = false;
933
+ $columns = $this->fields;
934
+ if (empty($columns) || $this->dtname != $params->datatype) {
935
+ $pod = $this->load_pod(array('name' => $params->datatype));
936
+ $columns = $pod['fields'];
937
+ if (null === $this->dtname) {
938
+ $this->dtname = $pod['name'];
939
+ $this->dt = $pod['id'];
940
+ $this->fields = $pod['fields'];
941
+ }
942
+ }
943
+ $pod = new Pod($params->datatype, $params->tbl_row_id);
944
+ if (!empty($pod->data)) {
945
+ $data = array();
946
+ foreach ($columns as $column) {
947
+ $value = $pod->get_field($column['name']);
948
+ if (0 < strlen($value))
949
+ $data[$column['name']] = $value;
950
+ }
951
+ $data = apply_filters('export_pod_item', $data, $pod->datatype, $pod->get_field('id'));
952
+ }
953
+ return $data;
954
+ }
955
+
956
+ /**
957
+ * Reorder a Pod
958
+ *
959
+ * $params['datatype'] string The datatype name
960
+ * $params['field'] string The column name of the field to reorder
961
+ * $params['order'] array The key=>value array of items to reorder (key should be an integer)
962
+ *
963
+ * @param array $params An associative array of parameters
964
+ * @since 1.9.0
965
+ */
966
+ function reorder_pod_item($params) {
967
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
968
+ $params = pods_sanitize($params);
969
+ $params = (object) $params;
970
+
971
+ if (!is_array($params->order)) {
972
+ $params->order = explode(',', $params->order);
973
+ }
974
+ foreach ($params->order as $order => $id) {
975
+ pod_query("UPDATE @wp_pod_tbl_{$params->datatype} SET `{$params->field}`={$order} WHERE id={$id}");
976
+ }
977
+ }
978
+
979
+ /**
980
+ * Delete all content for a content type
981
+ *
982
+ * $params['id'] int The datatype ID
983
+ * $params['name'] string The datatype name
984
+ *
985
+ * @param array $params An associative array of parameters
986
+ * @since 1.9.0
987
+ */
988
+ function reset_pod($params) {
989
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
990
+ $params = pods_sanitize($params);
991
+ $params = (object) $params;
992
+
993
+ $pod = $this->load_pod($params);
994
+ if (false === $pod)
995
+ return false;
996
+
997
+ $params->id = $pod['id'];
998
+ $params->name = $pod['name'];
999
+
1000
+ $fields = array();
1001
+ foreach ($pod['fields'] as $field) {
1002
+ $fields[] = $field['id'];
1003
+ }
1004
+ $fields = implode(',',$fields);
1005
+ if (!empty($fields))
1006
+ pod_query("UPDATE @wp_pod_fields SET sister_field_id = NULL WHERE sister_field_id IN ($fields)");
1007
+
1008
+ $sql = "DELETE FROM p, r
1009
+ USING @wp_pod_types AS t
1010
+ LEFT JOIN @wp_pod AS p ON p.datatype = t.id
1011
+ LEFT JOIN @wp_pod_fields AS f ON f.datatype = t.id
1012
+ LEFT JOIN @wp_pod_rel AS r ON r.field_id = f.id
1013
+ WHERE t.name = '$params->name'";
1014
+
1015
+ pod_query($sql);
1016
+
1017
+ $sql = "DELETE FROM r
1018
+ USING @wp_pod_fields AS f
1019
+ INNER JOIN @wp_pod_rel AS r ON r.field_id = f.id
1020
+ WHERE f.pickval = '$params->name'";
1021
+
1022
+ pod_query($sql);
1023
+ pod_query("TRUNCATE `@wp_pod_tbl_$params->name`");
1024
+ }
1025
+
1026
+ /**
1027
+ * Drop a content type and all its content
1028
+ *
1029
+ * $params['id'] int The datatype ID
1030
+ * $params['name'] string The datatype name
1031
+ *
1032
+ * @param array $params An associative array of parameters
1033
+ * @since 1.7.9
1034
+ */
1035
+ function drop_pod($params) {
1036
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
1037
+ $params = pods_sanitize($params);
1038
+ $params = (object) $params;
1039
+
1040
+ $pod = $this->load_pod($params);
1041
+ if (false === $pod)
1042
+ return false;
1043
+
1044
+ $params->id = $pod['id'];
1045
+ $params->name = $pod['name'];
1046
+
1047
+ pod_query("DELETE FROM @wp_pod_types WHERE id = $params->id LIMIT 1");
1048
+
1049
+ $fields = array();
1050
+ foreach ($pod['fields'] as $field) {
1051
+ $fields[] = $field['id'];
1052
+ }
1053
+ $fields = implode(',',$fields);
1054
+ if (!empty($fields))
1055
+ pod_query("UPDATE @wp_pod_fields SET sister_field_id = NULL WHERE sister_field_id IN ($fields)");
1056
+
1057
+ $sql = "DELETE FROM @wp_pod,@wp_pod_rel
1058
+ USING @wp_pod_fields
1059
+ INNER JOIN @wp_pod_rel ON @wp_pod_rel.field_id = @wp_pod_fields.id
1060
+ INNER JOIN @wp_pod ON @wp_pod.datatype = @wp_pod_fields.datatype
1061
+ WHERE @wp_pod_fields.datatype = $params->id";
1062
+
1063
+ pod_query($sql);
1064
+ pod_query("DELETE FROM @wp_pod_fields WHERE datatype = $params->id");
1065
+ pod_query("DROP TABLE `@wp_pod_tbl_$params->name`");
1066
+ }
1067
+
1068
+ /**
1069
+ * Drop a column within a content type
1070
+ *
1071
+ * $params['id'] int The column ID
1072
+ * $params['dtname'] string The datatype name
1073
+ *
1074
+ * @param array $params An associative array of parameters
1075
+ * @since 1.7.9
1076
+ */
1077
+ function drop_column($params) {
1078
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
1079
+ $params = pods_sanitize($params);
1080
+ $params = (object) $params;
1081
+ $result = pod_query("SELECT name, coltype FROM @wp_pod_fields WHERE id = $params->id LIMIT 1");
1082
+ list($field_name, $coltype) = pods_mysql_fetch_array($result);
1083
+
1084
+ if ('pick' == $coltype) {
1085
+ // Remove any orphans
1086
+ $result = pod_query("SELECT id FROM @wp_pod_fields WHERE sister_field_id = $params->id");
1087
+ if (0 < pods_mysql_num_rows($result)) {
1088
+ while ($row = pods_mysql_fetch_assoc($result)) {
1089
+ $related_fields[] = $row['id'];
1090
+ }
1091
+ $related_fields = implode(',', $related_fields);
1092
+ pod_query("DELETE FROM @wp_pod_rel WHERE field_id IN ($related_fields)");
1093
+ pod_query("UPDATE @wp_pod_fields SET sister_field_id = NULL WHERE sister_field_id IN ($related_fields)");
1094
+ }
1095
+ }
1096
+ elseif ('file' != $coltype) {
1097
+ pod_query("ALTER TABLE `@wp_pod_tbl_$params->dtname` DROP COLUMN `$field_name`");
1098
+ }
1099
+
1100
+ pod_query("DELETE FROM @wp_pod_fields WHERE id = $params->id LIMIT 1");
1101
+ pod_query("DELETE FROM @wp_pod_rel WHERE field_id = $params->id");
1102
+ }
1103
+
1104
+ /**
1105
+ * Drop a Pod Template
1106
+ *
1107
+ * $params['id'] int The template ID
1108
+ * $params['name'] string The template name
1109
+ *
1110
+ * @param array $params An associative array of parameters
1111
+ * @since 1.7.9
1112
+ */
1113
+ function drop_template($params) {
1114
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
1115
+ $params = pods_sanitize($params);
1116
+ $params = (object) $params;
1117
+ $where = empty($params->id) ? "name = '$params->name'" : "id = $params->id";
1118
+ pod_query("DELETE FROM @wp_pod_templates WHERE $where LIMIT 1");
1119
+ }
1120
+
1121
+ /**
1122
+ * Drop a Pod Page
1123
+ *
1124
+ * $params['id'] int The page ID
1125
+ * $params['uri'] string The page URI
1126
+ *
1127
+ * @param array $params An associative array of parameters
1128
+ * @since 1.7.9
1129
+ */
1130
+ function drop_page($params) {
1131
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
1132
+ $params = pods_sanitize($params);
1133
+ $params = (object) $params;
1134
+ $where = empty($params->id) ? "uri = '$params->uri'" : "id = $params->id";
1135
+ pod_query("DELETE FROM @wp_pod_pages WHERE $where LIMIT 1");
1136
+ }
1137
+
1138
+ /**
1139
+ * Drop a Pod Helper
1140
+ *
1141
+ * $params['id'] int The helper ID
1142
+ * $params['name'] string The helper name
1143
+ *
1144
+ * @param array $params An associative array of parameters
1145
+ * @since 1.7.9
1146
+ */
1147
+ function drop_helper($params) {
1148
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
1149
+ $params = pods_sanitize($params);
1150
+ $params = (object) $params;
1151
+ $where = empty($params->id) ? "name = '$params->name'" : "id = $params->id";
1152
+ pod_query("DELETE FROM @wp_pod_helpers WHERE $where LIMIT 1");
1153
+ }
1154
+
1155
+ /**
1156
+ * Drop a single pod item
1157
+ *
1158
+ * $params['pod_id'] int The item's ID from the wp_pod table
1159
+ * $params['tbl_row_id'] int (optional) The item's ID from the wp_pod_tbl_* table (used with datatype parameter)
1160
+ * $params['datatype'] string (optional) The datatype name (used with tbl_row_id parameter)
1161
+ * $params['datatype_id'] int (optional) The datatype ID (used with tbl_row_id parameter)
1162
+ *
1163
+ * @param array $params An associative array of parameters
1164
+ * @since 1.7.9
1165
+ */
1166
+ function drop_pod_item($params) {
1167
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
1168
+ $params = pods_sanitize($params);
1169
+ $params = (object) $params;
1170
+
1171
+ if (isset($params->tbl_row_id)) {
1172
+ if (!empty($params->tbl_row_id) && is_array($params->tbl_row_id)) {
1173
+ $new_params = $params;
1174
+ foreach ($params->tbl_row_id as $tbl_row_id) {
1175
+ $new_params->tbl_row_id = $tbl_row_id;
1176
+ $this->drop_pod_item($new_params);
1177
+ }
1178
+ return;
1179
+ }
1180
+ if (isset($params->datatype_id)) {
1181
+ $select_dt = "p.datatype = '$params->datatype_id'";
1182
+ }
1183
+ else {
1184
+ $select_dt = "t.name = '$params->datatype'";
1185
+ }
1186
+ $sql = "
1187
+ SELECT
1188
+ p.id AS pod_id, p.tbl_row_id, t.id, t.name
1189
+ FROM
1190
+ @wp_pod p
1191
+ INNER JOIN
1192
+ @wp_pod_types t ON t.id = p.datatype
1193
+ WHERE
1194
+ p.tbl_row_id = $params->tbl_row_id AND
1195
+ $select_dt
1196
+ LIMIT
1197
+ 1
1198
+ ";
1199
+ }
1200
+ else {
1201
+ $sql = "
1202
+ SELECT
1203
+ p.id AS pod_id, p.tbl_row_id, t.id, t.name
1204
+ FROM
1205
+ @wp_pod p
1206
+ INNER JOIN
1207
+ @wp_pod_types t ON t.id = p.datatype
1208
+ WHERE
1209
+ p.id = $params->pod_id
1210
+ LIMIT
1211
+ 1
1212
+ ";
1213
+ }
1214
+
1215
+ $result = pod_query($sql);
1216
+ $row = pods_mysql_fetch_assoc($result);
1217
+ $params->datatype_id = $row['id'];
1218
+ $params->datatype = $row['name'];
1219
+ $params->pod_id = $row['pod_id'];
1220
+ $params->tbl_row_id = $row['tbl_row_id'];
1221
+
1222
+ // Get helper code
1223
+ $result = pod_query("SELECT pre_drop_helpers, post_drop_helpers FROM @wp_pod_types WHERE id = $params->datatype_id");
1224
+ $row = pods_mysql_fetch_assoc($result);
1225
+ $params->pre_drop_helpers = explode(',', $row['pre_drop_helpers']);
1226
+ $params->post_drop_helpers = explode(',', $row['post_drop_helpers']);
1227
+
1228
+ // Plugin hook
1229
+ do_action('pods_pre_drop_pod_item', $params);
1230
+
1231
+ // Pre-drop helpers
1232
+ if (0 < count($params->pre_drop_helpers)) {
1233
+ foreach ($params->pre_drop_helpers as $helper) {
1234
+ $function_or_file = $helper;
1235
+ $check_function = $function_or_file;
1236
+ if ((!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) && (!defined('PODS_HELPER_FUNCTIONS') || !PODS_HELPER_FUNCTIONS))
1237
+ $check_function = false;
1238
+ $check_file = null;
1239
+ if ((!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) && (!defined('PODS_HELPER_FILES') || !PODS_HELPER_FILES))
1240
+ $check_file = false;
1241
+ if (false !== $check_function && false !== $check_file)
1242
+ $function_or_file = pods_function_or_file($function_or_file, $check_function, 'helper', $check_file);
1243
+ else
1244
+ $function_or_file = false;
1245
+
1246
+ $content = false;
1247
+ if (!$function_or_file) {
1248
+ $api = new PodAPI();
1249
+ $params_helper = array('name' => $helper, 'type' => 'pre_drop');
1250
+ if (!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE)
1251
+ $params_helper = pods_sanitize($params_helper);
1252
+ $content = $api->load_helper($params_helper);
1253
+ if (false !== $content && 0 < strlen(trim($content['phpcode'])))
1254
+ $content = $content['phpcode'];
1255
+ else
1256
+ $content = false;
1257
+ }
1258
+
1259
+ if (false === $content && false !== $function_or_file && isset($function_or_file['function']))
1260
+ $function_or_file['function']($params, $this);
1261
+ elseif (false === $content && false !== $function_or_file && isset($function_or_file['file']))
1262
+ locate_template($function_or_file['file'], true, true);
1263
+ elseif (false !== $content) {
1264
+ if (!defined('PODS_DISABLE_EVAL') || PODS_DISABLE_EVAL)
1265
+ eval("?>$content");
1266
+ else
1267
+ echo $content;
1268
+ }
1269
+ }
1270
+ }
1271
+
1272
+ pod_query("DELETE FROM `@wp_pod_tbl_$params->datatype` WHERE id = $params->tbl_row_id LIMIT 1");
1273
+ pod_query("UPDATE @wp_pod_rel SET sister_pod_id = NULL WHERE sister_pod_id = $params->pod_id");
1274
+ pod_query("DELETE FROM @wp_pod WHERE id = $params->pod_id LIMIT 1");
1275
+ pod_query("DELETE FROM @wp_pod_rel WHERE pod_id = $params->pod_id");
1276
+
1277
+ // Plugin hook
1278
+ do_action('pods_post_drop_pod_item', $params);
1279
+
1280
+ // Post-drop helpers
1281
+ if (0 < count($params->post_drop_helpers)) {
1282
+ foreach ($params->post_drop_helpers as $helper) {
1283
+ $function_or_file = $helper;
1284
+ $check_function = $function_or_file;
1285
+ if ((!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) && (!defined('PODS_HELPER_FUNCTIONS') || !PODS_HELPER_FUNCTIONS))
1286
+ $check_function = false;
1287
+ $check_file = null;
1288
+ if ((!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) && (!defined('PODS_HELPER_FILES') || !PODS_HELPER_FILES))
1289
+ $check_file = false;
1290
+ if (false !== $check_function && false !== $check_file)
1291
+ $function_or_file = pods_function_or_file($function_or_file, $check_function, 'helper', $check_file);
1292
+ else
1293
+ $function_or_file = false;
1294
+
1295
+ $content = false;
1296
+ if (!$function_or_file) {
1297
+ $api = new PodAPI();
1298
+ $params_helper = array('name' => $helper, 'type' => 'post_drop');
1299
+ if (!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE)
1300
+ $params_helper = pods_sanitize($params_helper);
1301
+ $content = $api->load_helper($params_helper);
1302
+ if (false !== $content && 0 < strlen(trim($content['phpcode'])))
1303
+ $content = $content['phpcode'];
1304
+ else
1305
+ $content = false;
1306
+ }
1307
+
1308
+ if (false === $content && false !== $function_or_file && isset($function_or_file['function']))
1309
+ $function_or_file['function']($params, $this);
1310
+ elseif (false === $content && false !== $function_or_file && isset($function_or_file['file']))
1311
+ locate_template($function_or_file['file'], true, true);
1312
+ elseif (false !== $content) {
1313
+ if (!defined('PODS_DISABLE_EVAL') || PODS_DISABLE_EVAL)
1314
+ eval("?>$content");
1315
+ else
1316
+ echo $content;
1317
+ }
1318
+ }
1319
+ }
1320
+ }
1321
+
1322
+ /**
1323
+ * Check if a Pod exists
1324
+ *
1325
+ * $params['id'] int The datatype ID
1326
+ * $params['name'] string The datatype name
1327
+ *
1328
+ * @param array $params An associative array of parameters
1329
+ * @since 1.12
1330
+ */
1331
+ function pod_exists($params) {
1332
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
1333
+ $params = pods_sanitize($params);
1334
+ $params = (object) $params;
1335
+ if (!empty($params->id) || !empty($params->name)) {
1336
+ $where = empty($params->id) ? "name = '{$params->name}'" : "id = {$params->id}";
1337
+ $result = pod_query("SELECT id, name FROM @wp_pod_types WHERE {$where} LIMIT 1");
1338
+ if (0 < pods_mysql_num_rows($result)) {
1339
+ $pod = pods_mysql_fetch_assoc($result);
1340
+ return $pod;
1341
+ }
1342
+ }
1343
+ return false;
1344
+ }
1345
+
1346
+ /**
1347
+ * Load a content type and all of its fields
1348
+ *
1349
+ * $params['id'] int The datatype ID
1350
+ * $params['name'] string The datatype name
1351
+ *
1352
+ * @param array $params An associative array of parameters
1353
+ * @since 1.7.9
1354
+ */
1355
+ function load_pod($params) {
1356
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
1357
+ $params = pods_sanitize($params);
1358
+ $params = (object) $params;
1359
+ if (!empty($params->id) || !empty($params->name)) {
1360
+ $where = empty($params->id) ? "name = '$params->name'" : "id = $params->id";
1361
+ $result = pod_query("SELECT * FROM @wp_pod_types WHERE $where LIMIT 1");
1362
+ if (0 < pods_mysql_num_rows($result)) {
1363
+ $pod = pods_mysql_fetch_assoc($result);
1364
+ $pod['fields'] = array();
1365
+ $result = pod_query("SELECT id, name, coltype, pickval, required, weight FROM @wp_pod_fields WHERE datatype = {$pod['id']} ORDER BY weight");
1366
+ while ($row = pods_mysql_fetch_assoc($result)) {
1367
+ $pod['fields'][$row['name']] = $row;
1368
+ }
1369
+
1370
+ return $pod;
1371
+ }
1372
+ }
1373
+ return false;
1374
+ }
1375
+
1376
+ /**
1377
+ * Load a column
1378
+ *
1379
+ * $params['id'] int The field ID
1380
+ * $params['name'] string The field name
1381
+ * $params['datatype'] int The Pod ID
1382
+ *
1383
+ * @param array $params An associative array of parameters
1384
+ * @since 1.7.9
1385
+ */
1386
+ function load_column($params) {
1387
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
1388
+ $params = pods_sanitize($params);
1389
+ $params = (object) $params;
1390
+ if (isset($params->id))
1391
+ $params->id = absint($params->id);
1392
+ $where = empty($params->id) ? "`name` = '{$params->name}' AND `datatype` = {$params->datatype}" : "`id` = {$params->id}";
1393
+ $result = pod_query("SELECT * FROM @wp_pod_fields WHERE {$where} LIMIT 1");
1394
+ return @pods_mysql_fetch_assoc($result);
1395
+ }
1396
+
1397
+ /**
1398
+ * Load a Pod Template
1399
+ *
1400
+ * $params['id'] int The template ID
1401
+ * $params['name'] string The template name
1402
+ *
1403
+ * @param array $params An associative array of parameters
1404
+ * @since 1.7.9
1405
+ */
1406
+ function load_template($params) {
1407
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
1408
+ $params = pods_sanitize($params);
1409
+ $params = (object) $params;
1410
+ if (isset($params->id))
1411
+ $params->id = absint($params->id);
1412
+ $where = empty($params->id) ? "`name` = '{$params->name}'" : "`id` = {$params->id}";
1413
+ $result = pod_query("SELECT * FROM @wp_pod_templates WHERE {$where} LIMIT 1");
1414
+ return @pods_mysql_fetch_assoc($result);
1415
+ }
1416
+
1417
+ /**
1418
+ * Load a Pod Page
1419
+ *
1420
+ * $params['id'] int The page ID
1421
+ * $params['uri'] string The page URI
1422
+ *
1423
+ * @param array $params An associative array of parameters
1424
+ * @since 1.7.9
1425
+ */
1426
+ function load_page($params) {
1427
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
1428
+ $params = pods_sanitize($params);
1429
+ $params = (object) $params;
1430
+ if (isset($params->id))
1431
+ $params->id = absint($params->id);
1432
+ $where = empty($params->id) ? "`uri` = '{$params->uri}'" : "`id` = {$params->id}";
1433
+ $result = pod_query("SELECT * FROM @wp_pod_pages WHERE {$where} LIMIT 1");
1434
+ return @pods_mysql_fetch_assoc($result);
1435
+ }
1436
+
1437
+ /**
1438
+ * Load a Pod Helper
1439
+ *
1440
+ * $params['id'] int The helper ID
1441
+ * $params['name'] string The helper name
1442
+ * $params['type'] string The helper type
1443
+ *
1444
+ * @param array $params An associative array of parameters
1445
+ * @since 1.7.9
1446
+ */
1447
+ function load_helper($params) {
1448
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
1449
+ $params = pods_sanitize($params);
1450
+ $params = (object) $params;
1451
+ if (isset($params->id))
1452
+ $params->id = absint($params->id);
1453
+ $where = empty($params->id) ? "`name` = '{$params->name}'" : "`id` = {$params->id}";
1454
+ if (isset($params->type) && !empty($params->type))
1455
+ $where .= " AND `helper_type` = '{$params->type}'";
1456
+ $result = pod_query("SELECT * FROM @wp_pod_helpers WHERE {$where} LIMIT 1");
1457
+ return @pods_mysql_fetch_assoc($result);
1458
+ }
1459
+
1460
+ /**
1461
+ * Load the input form for a pod item
1462
+ *
1463
+ * $params['datatype'] string The datatype name
1464
+ * $params['pod_id'] int The item's pod ID
1465
+ * $params['tbl_row_id'] int (optional) The item's ID
1466
+ * $params['public_columns'] array An associative array of columns
1467
+ *
1468
+ * @param array $params An associative array of parameters
1469
+ * @since 1.7.9
1470
+ */
1471
+ function load_pod_item($params) {
1472
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
1473
+ $params = pods_sanitize($params);
1474
+ $params = (object) $params;
1475
+
1476
+ $params->tbl_row_id = (int) (isset($params->tbl_row_id)?$params->tbl_row_id:null);
1477
+ $params->pod_id = (int) (isset($params->pod_id)?$params->pod_id:null);
1478
+ if (empty($params->tbl_row_id)) {
1479
+ $params->tbl_row_id = null;
1480
+ if (!empty($params->pod_id)) {
1481
+ $result = pod_query("SELECT tbl_row_id FROM @wp_pod WHERE id = $params->pod_id LIMIT 1",'Item not found',null,'Item not found');
1482
+ $params->tbl_row_id = pods_mysql_result($result, 0);
1483
+ }
1484
+ }
1485
+ $obj = new Pod($params->datatype,$params->tbl_row_id);
1486
+ $pod_id = 0;
1487
+ if (!empty($params->tbl_row_id) && !empty($obj->data)) {
1488
+ $pod_id = $obj->get_pod_id();
1489
+ }
1490
+ return $obj->showform($pod_id, $params->public_columns = null);
1491
+ }
1492
+
1493
+ /**
1494
+ * Load a bi-directional (sister) column
1495
+ *
1496
+ * $params['pickval'] string The related PICK pod name
1497
+ * $params['datatype'] int The datatype ID
1498
+ *
1499
+ * @param array $params An associative array of parameters
1500
+ * @since 1.7.9
1501
+ */
1502
+ function load_sister_fields($params) {
1503
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
1504
+ $params = pods_sanitize($params);
1505
+ $params = (object) $params;
1506
+
1507
+ if (!empty($params->pickval) && is_string($params->pickval)) {
1508
+ $result = pod_query("SELECT id FROM @wp_pod_types WHERE name = '$params->pickval' LIMIT 1");
1509
+ if (0 < pods_mysql_num_rows($result)) {
1510
+ $sister_datatype = pods_mysql_result($result, 0);
1511
+
1512
+ $result = pod_query("SELECT name FROM @wp_pod_types WHERE id = $params->datatype LIMIT 1");
1513
+ if (0 < pods_mysql_num_rows($result)) {
1514
+ $datatype_name = pods_mysql_result($result, 0);
1515
+
1516
+ $result = pod_query("SELECT id, name FROM @wp_pod_fields WHERE datatype = $sister_datatype AND pickval = '$datatype_name'");
1517
+ if (0 < pods_mysql_num_rows($result)) {
1518
+ while ($row = pods_mysql_fetch_assoc($result)) {
1519
+ $sister_fields[] = $row;
1520
+ }
1521
+ return $sister_fields;
1522
+ }
1523
+ }
1524
+ }
1525
+ }
1526
+ }
1527
+
1528
+ /**
1529
+ * Export a package
1530
+ *
1531
+ * $params['pod'] string Pod Type IDs to export
1532
+ * $params['template'] string Template IDs to export
1533
+ * $params['podpage'] string Pod Page IDs to export
1534
+ * $params['helper'] string Helper IDs to export
1535
+ *
1536
+ * @param array $params An associative array of parameters
1537
+ * @since 1.9.0
1538
+ */
1539
+ function export_package($params) {
1540
+ if (defined('PODS_STRICT_MODE') && PODS_STRICT_MODE)
1541
+ $params = pods_sanitize($params);
1542
+ $export = array(
1543
+ 'meta' => array(
1544
+ 'version' => PODS_VERSION,
1545
+ 'build' => date('U'),
1546
+ )
1547
+ );
1548
+
1549
+ $pod_ids = $params['pod'];
1550
+ $template_ids = $params['template'];
1551
+ $pod_page_ids = $params['podpage'];
1552
+ $helper_ids = $params['helper'];
1553
+
1554
+ // Get pods
1555
+ if (!empty($pod_ids)) {
1556
+ $result = pod_query("SELECT * FROM @wp_pod_types WHERE id IN ($pod_ids)");
1557
+ while ($row = pods_mysql_fetch_assoc($result)) {
1558
+ $dt = $row['id'];
1559
+ unset($row['id']);
1560
+ $export['pods'][$dt] = $row;
1561
+ }
1562
+
1563
+ // Get pod fields
1564
+ $result = pod_query("SELECT * FROM @wp_pod_fields WHERE datatype IN ($pod_ids)");
1565
+ while ($row = pods_mysql_fetch_assoc($result)) {
1566
+ unset($row['id']);
1567
+ $dt = $row['datatype'];
1568
+ unset($row['datatype']);
1569
+ unset($row['sister_field_id']); // impossible to reference this correctly until all pods / fields have been added
1570
+ $export['pods'][$dt]['fields'][] = $row;
1571
+ }
1572
+ }
1573
+
1574
+ // Get templates
1575
+ if (!empty($template_ids)) {
1576
+ $result = pod_query("SELECT * FROM @wp_pod_templates WHERE id IN ($template_ids)");
1577
+ while ($row = pods_mysql_fetch_assoc($result)) {
1578
+ unset($row['id']);
1579
+ $export['templates'][] = $row;
1580
+ }
1581
+ }
1582
+
1583
+ // Get pod pages
1584
+ if (!empty($pod_page_ids)) {
1585
+ $result = pod_query("SELECT * FROM @wp_pod_pages WHERE id IN ($pod_page_ids)");
1586
+ while ($row = pods_mysql_fetch_assoc($result)) {
1587
+ unset($row['id']);
1588
+ $export['pod_pages'][] = $row;
1589
+ }
1590
+ }
1591
+
1592
+ // Get helpers
1593
+ if (!empty($helper_ids)) {
1594
+ $result = pod_query("SELECT * FROM @wp_pod_helpers WHERE id IN ($helper_ids)");
1595
+ while ($row = pods_mysql_fetch_assoc($result)) {
1596
+ unset($row['id']);
1597
+ $export['helpers'][] = $row;
1598
+ }
1599
+ }
1600
+
1601
+ if (1 == count($export))
1602
+ return false;
1603
+
1604
+ return $export;
1605
+ }
1606
+
1607
+ /**
1608
+ * Replace an existing package
1609
+ *
1610
+ *
1611
+ * @param mixed $data (optional) An associative array containing a package, or the json encoded package
1612
+ * @since 1.9.8
1613
+ */
1614
+ function replace_package($data = false) {
1615
+ return $this->import_package($data, true);
1616
+ }
1617
+
1618
+ /**
1619
+ * Import a package
1620
+ *
1621
+ *
1622
+ * @param mixed $data (optional) An associative array containing a package, or the json encoded package
1623
+ * @param bool $replace (optional) Replace existing items when found
1624
+ * @since 1.9.0
1625
+ */
1626
+ function import_package($data = false, $replace = false) {
1627
+ $output = false;
1628
+ if (false===$data || isset($data['action'])) {
1629
+ $data = get_option('pods_package');
1630
+ $output = true;
1631
+ }
1632
+ if (!is_array($data)) {
1633
+ $json_data = @json_decode($data, true);
1634
+ if (!is_array($json_data))
1635
+ $json_data = @json_decode(stripslashes($data), true);
1636
+ $data = $json_data;
1637
+ }
1638
+ if (!is_array($data) || empty($data)) {
1639
+ return false;
1640
+ }
1641
+
1642
+ $dbtypes = array(
1643
+ 'bool' => 'bool default 0',
1644
+ 'date' => 'datetime',
1645
+ 'num' => 'decimal(9,2)',
1646
+ 'txt' => 'varchar(128)',
1647
+ 'slug' => 'varchar(128)',
1648
+ 'code' => 'mediumtext',
1649
+ 'desc' => 'mediumtext'
1650
+ );
1651
+ $dbtypes = apply_filters('pods_column_dbtypes', $dbtypes, $this);
1652
+
1653
+ $found = array();
1654
+
1655
+ if (isset($data['pods'])) {
1656
+ $pod_columns = '';
1657
+ foreach ($data['pods'] as $pod) {
1658
+ $pod = pods_sanitize($pod);
1659
+
1660
+ $table_columns = array();
1661
+ $pod_fields = $pod['fields'];
1662
+ unset($pod['fields']);
1663
+
1664
+ if (false !== $replace) {
1665
+ $existing = $this->load_pod(array('name' => $pod['name']));
1666
+ if (is_array($existing))
1667
+ $this->drop_pod(array('id' => $existing['id']));
1668
+ }
1669
+
1670
+ if (empty($pod_columns))
1671
+ $pod_columns = implode("`,`", array_keys($pod));
1672
+ // Backward-compatibility (before/after helpers)
1673
+ $pod_columns = str_replace('before_helpers', 'pre_save_helpers', $pod_columns);
1674
+ $pod_columns = str_replace('after_helpers', 'post_save_helpers', $pod_columns);
1675
+
1676
+ $values = implode("','", $pod);
1677
+ $dt = pod_query("INSERT INTO @wp_pod_types (`$pod_columns`) VALUES ('$values')");
1678
+
1679
+ $tupples = array();
1680
+ $field_columns = '';
1681
+ foreach ($pod_fields as $fieldval) {
1682
+ // Escape the values
1683
+ foreach ($fieldval as $k => $v) {
1684
+ if (empty($v))
1685
+ $v = 'null';
1686
+ else
1687
+ $v = pods_sanitize($v);
1688
+ $fieldval[$k] = $v;
1689
+ }
1690
+
1691
+ // Store all table columns
1692
+ if ('pick' != $fieldval['coltype'] && 'file' != $fieldval['coltype'])
1693
+ $table_columns[$fieldval['name']] = $fieldval['coltype'];
1694
+
1695
+ $fieldval['datatype'] = $dt;
1696
+ if (empty($field_columns))
1697
+ $field_columns = implode("`,`", array_keys($fieldval));
1698
+ $tupples[] = implode("','", $fieldval);
1699
+ }
1700
+ $tupples = implode("'),('", $tupples);
1701
+ $tupples = str_replace("'null'", 'null', $tupples);
1702
+ pod_query("INSERT INTO @wp_pod_fields (`$field_columns`) VALUES ('$tupples')");
1703
+
1704
+ // Create the actual table with any non-PICK columns
1705
+ $definitions = array("id INT unsigned auto_increment primary key");
1706
+ foreach ($table_columns as $colname => $coltype) {
1707
+ $definitions[] = "`$colname` {$dbtypes[$coltype]}";
1708
+ }
1709
+ $definitions = implode(',', $definitions);
1710
+ pod_query("CREATE TABLE @wp_pod_tbl_{$pod['name']} ($definitions)");
1711
+ if (!isset($found['pods']))
1712
+ $found['pods'] = array();
1713
+ $found['pods'][] = esc_textarea($pod['name']);
1714
+ }
1715
+ }
1716
+
1717
+ if (isset($data['templates'])) {
1718
+ foreach ($data['templates'] as $template) {
1719
+ $defaults = array('name' => '', 'code' => '');
1720
+ $params = array_merge($defaults, $template);
1721
+ if (!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE)
1722
+ $params = pods_sanitize($params);
1723
+ if (false !== $replace) {
1724
+ $existing = $this->load_template(array('name' => $params['name']));
1725
+ if (is_array($existing))
1726
+ $params['id'] = $existing['id'];
1727
+ }
1728
+ $this->save_template($params);
1729
+ if (!isset($found['templates']))
1730
+ $found['templates'] = array();
1731
+ $found['templates'][] = esc_textarea($params['name']);
1732
+ }
1733
+ }
1734
+
1735
+ if (isset($data['pod_pages'])) {
1736
+ foreach ($data['pod_pages'] as $pod_page) {
1737
+ $defaults = array('uri' => '', 'title' => '', 'phpcode' => '', 'precode' => '', 'page_template' => '');
1738
+ $params = array_merge($defaults, $pod_page);
1739
+ if (!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE)
1740
+ $params = pods_sanitize($params);
1741
+ if (false !== $replace) {
1742
+ $existing = $this->load_page(array('uri' => $params['uri']));
1743
+ if (is_array($existing))
1744
+ $params['id'] = $existing['id'];
1745
+ }
1746
+ $this->save_page($params);
1747
+ if (!isset($found['pod_pages']))
1748
+ $found['pod_pages'] = array();
1749
+ $found['pod_pages'][] = esc_textarea($params['uri']);
1750
+ }
1751
+ }
1752
+
1753
+ if (isset($data['helpers'])) {
1754
+ foreach ($data['helpers'] as $helper) {
1755
+ // backwards compatibility
1756
+ if (isset($helper['helper_type'])) {
1757
+ if ('before' == $helper['helper_type'])
1758
+ $helper['helper_type'] = 'pre_save';
1759
+ if ('after' == $helper['helper_type'])
1760
+ $helper['helper_type'] = 'post_save';
1761
+ }
1762
+ $defaults = array('name' => '', 'helper_type' => 'display', 'phpcode' => '');
1763
+ $params = array_merge($defaults, $helper);
1764
+ if (!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE)
1765
+ $params = pods_sanitize($params);
1766
+ if (false !== $replace) {
1767
+ $existing = $this->load_helper(array('name' => $params['name']));
1768
+ if (is_array($existing))
1769
+ $params['id'] = $existing['id'];
1770
+ }
1771
+ $this->save_helper($params);
1772
+ if (!isset($found['helpers']))
1773
+ $found['helpers'] = array();
1774
+ $found['helpers'][] = esc_textarea($params['name']);
1775
+ }
1776
+ }
1777
+ if (true===$output) {
1778
+ if (!empty($found)) {
1779
+ echo '<br /><div id="message" class="updated fade">';
1780
+ echo '<h3 style="margin-top:10px;">Package Imported:</h3>';
1781
+ if (isset($found['pods'])) {
1782
+ echo '<h4>Pod(s)</h4>';
1783
+ echo '<ul class="pretty"><li>' . implode('</li><li>', $found['pods']) . '</li></ul>';
1784
+ }
1785
+ if (isset($found['templates'])) {
1786
+ echo '<h4>Template(s)</h4>';
1787
+ echo '<ul class="pretty"><li>' . implode('</li><li>', $found['templates']) . '</li></ul>';
1788
+ }
1789
+ if (isset($found['pod_pages'])) {
1790
+ echo '<h4>Pod Page(s)</h4>';
1791
+ echo '<ul class="pretty"><li>' . implode('</li><li>', $found['pod_pages']) . '</li></ul>';
1792
+ }
1793
+ if (isset($found['helpers'])) {
1794
+ echo '<h4>Helper(s)</h4>';
1795
+ echo '<ul class="pretty"><li>' . implode('</li><li>', $found['helpers']) . '</li></ul>';
1796
+ }
1797
+ echo '</div>';
1798
+ }
1799
+ else
1800
+ echo '<e><br /><div id="message" class="error fade"><p>Error: Package not imported, try again.</p></div></e>';
1801
+ }
1802
+ if (!empty($found))
1803
+ return true;
1804
+ return false;
1805
+ }
1806
+
1807
+ /**
1808
+ * Validate a package
1809
+ *
1810
+ *
1811
+ * @param mixed $data (optional) An associative array containing a package, or the json encoded package
1812
+ * @since 1.9.0
1813
+ */
1814
+ function validate_package($data = false, $output = false) {
1815
+ if (is_array($data)&&isset($data['data'])) {
1816
+ $data = $data['data'];
1817
+ $output = true;
1818
+ }
1819
+ if (is_array($data))
1820
+ $data = esc_textarea(json_encode($data));
1821
+
1822
+ $found = array();
1823
+ $warnings = array();
1824
+
1825
+ update_option('pods_package', $data);
1826
+
1827
+ $json_data = @json_decode($data, true);
1828
+ if (!is_array($json_data))
1829
+ $json_data = @json_decode(stripslashes($data), true);
1830
+
1831
+ if (!is_array($json_data) || empty($json_data)) {
1832
+ $warnings[] = "This is not a valid package. Please try again.";
1833
+ if (true===$output) {
1834
+ echo '<e><br /><div id="message" class="error fade"><p>This is not a valid package. Please try again.</p></div></e>';
1835
+ return false;
1836
+ }
1837
+ else
1838
+ return $warnings;
1839
+ }
1840
+ $data = $json_data;
1841
+
1842
+ if (0 < strlen($data['meta']['version']) && false === strpos($data['meta']['version'], '.') && (int) $data['meta']['version'] < 1000) { // older style
1843
+ $data['meta']['version'] = implode('.', str_split($data['meta']['version']));
1844
+ }
1845
+ elseif (0 < strlen($data['meta']['version']) && false === strpos($data['meta']['version'], '.')) { // old style
1846
+ $data['meta']['version'] = pods_version_to_point($data['meta']['version']);
1847
+ }
1848
+
1849
+ if (isset($data['meta']['compatible_from'])) {
1850
+ if (0 < strlen($data['meta']['compatible_from']) && false === strpos($data['meta']['compatible_from'], '.')) { // old style
1851
+ $data['meta']['compatible_from'] = pods_version_to_point($data['meta']['compatible_from']);
1852
+ }
1853
+ if (version_compare(PODS_VERSION, $data['meta']['compatible_from'], '<')) {
1854
+ $compatible_from = explode('.', $data['meta']['compatible_from']);
1855
+ $compatible_from = $compatible_from[0] . '.' . $compatible_from[1];
1856
+ $pods_version = explode('.', PODS_VERSION);
1857
+ $pods_version = $pods_version[0] . '.' . $pods_version[1];
1858
+ if (version_compare($pods_version, $compatible_from, '<'))
1859
+ $warnings['version'] = 'This package may only compatible with the newer <strong>Pods ' . pods_version_to_point($data['meta']['compatible_from']) . '+</strong>, but you are currently running the older <strong>Pods ' . PODS_VERSION . '</strong><br />Unless the package author has specified it is compatible, it may not have been tested to work with your installed version of Pods.';
1860
+ }
1861
+ }
1862
+ if (isset($data['meta']['compatible_to'])) {
1863
+ if (0 < strlen($data['meta']['compatible_to']) && false === strpos($data['meta']['compatible_to'], '.')) { // old style
1864
+ $data['meta']['compatible_to'] = pods_version_to_point($data['meta']['compatible_to']);
1865
+ }
1866
+ if (version_compare($data['meta']['compatible_to'], PODS_VERSION, '<')) {
1867
+ $compatible_to = explode('.', $data['meta']['compatible_to']);
1868
+ $compatible_to = $compatible_to[0] . '.' . $compatible_to[1];
1869
+ $pods_version = explode('.', PODS_VERSION);
1870
+ $pods_version = $pods_version[0] . '.' . $pods_version[1];
1871
+ if (version_compare($compatible_to, $pods_version, '<'))
1872
+ $warnings['version'] = 'This package may only compatible with the older <strong>Pods ' . $data['meta']['compatible_to'] . '</strong>, but you are currently running the newer <strong>Pods ' . PODS_VERSION . '</strong><br />Unless the package author has specified it is compatible, it may not have been tested to work with your installed version of Pods.';
1873
+ }
1874
+ }
1875
+ if (!isset($data['meta']['compatible_from']) && !isset($data['meta']['compatible_to'])) {
1876
+ if (version_compare(PODS_VERSION, $data['meta']['version'], '<')) {
1877
+ $compatible_from = explode('.', $data['meta']['version']);
1878
+ $compatible_from = $compatible_from[0] . '.' . $compatible_from[1];
1879
+ $pods_version = explode('.', PODS_VERSION);
1880
+ $pods_version = $pods_version[0] . '.' . $pods_version[1];
1881
+ if (version_compare($pods_version, $compatible_from, '<'))
1882
+ $warnings['version'] = 'This package was built using the newer <strong>Pods ' . $data['meta']['version'] . '</strong>, but you are currently running the older <strong>Pods ' . PODS_VERSION . '</strong><br />Unless the package author has specified it is compatible, it may not have been tested to work with your installed version of Pods.';
1883
+ }
1884
+ elseif (version_compare($data['meta']['version'], PODS_VERSION, '<')) {
1885
+ $compatible_to = explode('.', $data['meta']['version']);
1886
+ $compatible_to = $compatible_to[0] . '.' . $compatible_to[1];
1887
+ $pods_version = explode('.', PODS_VERSION);
1888
+ $pods_version = $pods_version[0] . '.' . $pods_version[1];
1889
+ if (version_compare($compatible_to, $pods_version, '<'))
1890
+ $warnings['version'] = 'This package was built using the older <strong>Pods ' . $data['meta']['version'] . '</strong>, but you are currently running the newer <strong>Pods ' . PODS_VERSION . '</strong><br />Unless the package author has specified it is compatible, it may not have been tested to work with your installed version of Pods.';
1891
+ }
1892
+ }
1893
+
1894
+ if (isset($data['pods'])) {
1895
+ foreach ($data['pods'] as $pod) {
1896
+ $pod = pods_sanitize($pod);
1897
+ $existing = $this->load_pod(array('name' => $pod['name']));
1898
+ if (is_array($existing)) {
1899
+ if (!isset($warnings['pods']))
1900
+ $warnings['pods'] = array();
1901
+ $warnings['pods'][] = esc_textarea($pod['name']);
1902
+ }
1903
+ if (!isset($found['pods']))
1904
+ $found['pods'] = array();
1905
+ $found['pods'][] = esc_textarea($pod['name']);
1906
+ }
1907
+ }
1908
+
1909
+ if (isset($data['templates'])) {
1910
+ foreach ($data['templates'] as $template) {
1911
+ $template = pods_sanitize($template);
1912
+ $existing = $this->load_template(array('name' => $template['name']));
1913
+ if (is_array($existing)) {
1914
+ if (!isset($warnings['templates']))
1915
+ $warnings['templates'] = array();
1916
+ $warnings['templates'][] = esc_textarea($template['name']);
1917
+ }
1918
+ if (!isset($found['templates']))
1919
+ $found['templates'] = array();
1920
+ $found['templates'][] = esc_textarea($template['name']);
1921
+ }
1922
+ }
1923
+
1924
+ if (isset($data['pod_pages'])) {
1925
+ foreach ($data['pod_pages'] as $pod_page) {
1926
+ $pod_page = pods_sanitize($pod_page);
1927
+ $existing = $this->load_page(array('uri' => $pod_page['uri']));
1928
+ if (is_array($existing)) {
1929
+ if (!isset($warnings['pod_pages']))
1930
+ $warnings['pod_pages'] = array();
1931
+ $warnings['pod_pages'][] = esc_textarea($pod_page['uri']);
1932
+ }
1933
+ if (!isset($found['pod_pages']))
1934
+ $found['pod_pages'] = array();
1935
+ $found['pod_pages'][] = esc_textarea($pod_page['uri']);
1936
+ }
1937
+ }
1938
+
1939
+ if (isset($data['helpers'])) {
1940
+ foreach ($data['helpers'] as $helper) {
1941
+ $helper = pods_sanitize($helper);
1942
+ $existing = $this->load_helper(array('name' => $helper['name']));
1943
+ if (is_array($existing)) {
1944
+ if (!isset($warnings['helpers']))
1945
+ $warnings['helpers'] = array();
1946
+ $warnings['helpers'][] = esc_textarea($helper['name']);
1947
+ }
1948
+ if (!isset($found['helpers']))
1949
+ $found['helpers'] = array();
1950
+ $found['helpers'][] = esc_textarea($helper['name']);
1951
+ }
1952
+ }
1953
+
1954
+ if (true===$output) {
1955
+ if (!empty($found)) {
1956
+ echo '<hr />';
1957
+ echo '<h3>Package Contents:</h3>';
1958
+ if (isset($warnings['version']))
1959
+ echo '<p><em><strong>NOTICE:</strong> ' . $warnings['version'] . '</em></p>';
1960
+ if (isset($found['pods'])) {
1961
+ echo '<h4>Pod(s)</h4>';
1962
+ echo '<ul class="pretty"><li>' . implode('</li><li>', $found['pods']) . '</li></ul>';
1963
+ }
1964
+ if (isset($found['templates'])) {
1965
+ echo '<h4>Template(s)</h4>';
1966
+ echo '<ul class="pretty"><li>' . implode('</li><li>', $found['templates']) . '</li></ul>';
1967
+ }
1968
+ if (isset($found['pod_pages'])) {
1969
+ echo '<h4>Pod Page(s)</h4>';
1970
+ echo '<ul class="pretty"><li>' . implode('</li><li>', $found['pod_pages']) . '</li></ul>';
1971
+ }
1972
+ if (isset($found['helpers'])) {
1973
+ echo '<h4>Helper(s)</h4>';
1974
+ echo '<ul class="pretty"><li>' . implode('</li><li>', $found['helpers']) . '</li></ul>';
1975
+ }
1976
+ }
1977
+ if (0 < count($warnings) && (!isset($warnings['version']) || 1 < count($warnings))) {
1978
+ echo '<hr />';
1979
+ echo '<h3 class="red">WARNING: There are portions of this package that already exist</h3>';
1980
+ if (isset($warnings['pods'])) {
1981
+ echo '<h4>Pod(s)</h4>';
1982
+ echo '<ul class="pretty"><li>' . implode('</li><li>', $warnings['pods']) . '</li></ul>';
1983
+ }
1984
+ if (isset($warnings['templates'])) {
1985
+ echo '<h4>Template(s)</h4>';
1986
+ echo '<ul class="pretty"><li>' . implode('</li><li>', $warnings['templates']) . '</li></ul>';
1987
+ }
1988
+ if (isset($warnings['pod_pages'])) {
1989
+ echo '<h4>Pod Page(s)</h4>';
1990
+ echo '<ul class="pretty"><li>' . implode('</li><li>', $warnings['pod_pages']) . '</li></ul>';
1991
+ }
1992
+ if (isset($warnings['helpers'])) {
1993
+ echo '<h4>Helper(s)</h4>';
1994
+ echo '<ul class="pretty"><li>' . implode('</li><li>', $warnings['helpers']) . '</li></ul>';
1995
+ }
1996
+ echo '<p><input type="button" class="button-primary" style="background:#f39400;border-color:#d56500;" onclick="podsImport(\'replace_package\')" value=" Overwrite the existing package (Step 2 of 2) " />&nbsp;&nbsp;&nbsp;<input type="button" class="button-secondary" onclick="podsImportCancel()" value=" Cancel " /></p>';
1997
+ return false;
1998
+ }
1999
+ elseif (!empty($found)) {
2000
+ echo '<p><input type="button" class="button-primary" onclick="podsImport(\'import_package\')" value=" Import Package (Step 2 of 2) " />&nbsp;&nbsp;&nbsp;<input type="button" class="button-secondary" onclick="podsImportCancel()" value=" Cancel " /></p>';
2001
+ return false;
2002
+ }
2003
+ echo '<e><br /><div id="message" class="error fade"><p>Error: This package is empty, there is nothing to import.</p></div></e>';
2004
+ return false;
2005
+ }
2006
+ if (0 < count($warnings))
2007
+ return $warnings;
2008
+ elseif (!empty($found))
2009
+ return true;
2010
+ return false;
2011
+ }
2012
+
2013
+ /**
2014
+ * Import data
2015
+ *
2016
+ * @param mixed $data PHP associative array or CSV input
2017
+ * @param bool $numeric_mode Use IDs instead of the name field when matching
2018
+ * @since 1.7.1
2019
+ */
2020
+ function import($data, $numeric_mode = false) {
2021
+ global $wpdb;
2022
+ if ('csv' == $this->format) {
2023
+ $data = $this->csv_to_php($data);
2024
+ }
2025
+
2026
+ pod_query("SET NAMES utf8");
2027
+ pod_query("SET CHARACTER SET utf8");
2028
+
2029
+ // Get the id/name pairs of all associated pick/file tables
2030
+ $pick_values = $file_values = array();
2031
+ foreach ($this->fields as $field_name => $field_data) {
2032
+ $pickval = $field_data['pickval'];
2033
+ if ('file' == $field_data['coltype']) {
2034
+ $res = pod_query("SELECT ID as id, guid as name FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY id");
2035
+ while ($item = pods_mysql_fetch_assoc($res)) {
2036
+ $file_url = str_replace(get_bloginfo('url'), '', $item['name']);
2037
+ $file_values[$field_name][$file_url] = $item['id'];
2038
+ $file_values[$field_name][$item['name']] = $item['id'];
2039
+ }
2040
+ }
2041
+ elseif ('pick' == $field_data['coltype']) {
2042
+ if ('wp_taxonomy' == $pickval) {
2043
+ $res = pod_query("SELECT term_id AS id, name FROM $wpdb->terms ORDER BY id");
2044
+ while ($item = pods_mysql_fetch_assoc($res)) {
2045
+ $pick_values[$field_name][$item['name']] = $item['id'];
2046
+ }
2047
+ }
2048
+ elseif ('wp_page' == $pickval || 'wp_post' == $pickval) {
2049
+ $pickval = str_replace('wp_', '', $pickval);
2050
+ $res = pod_query("SELECT ID as id, post_title as name FROM $wpdb->posts WHERE post_type = '$pickval' ORDER BY id");
2051
+ while ($item = pods_mysql_fetch_assoc($res)) {
2052
+ $pick_values[$field_name][$item['name']] = $item['id'];
2053
+ }
2054
+ }
2055
+ elseif ('wp_user' == $pickval) {
2056
+ $res = pod_query("SELECT ID as id, display_name as name FROM $wpdb->users ORDER BY id");
2057
+ while ($item = pods_mysql_fetch_assoc($res)) {
2058
+ $pick_values[$field_name][$item['name']] = $item['id'];
2059
+ }
2060
+ }
2061
+ else {
2062
+ $res = pod_query("SELECT id, name FROM @wp_pod_tbl_{$pickval} ORDER BY id");
2063
+ while ($item = pods_mysql_fetch_assoc($res)) {
2064
+ $pick_values[$field_name][$item['name']] = $item['id'];
2065
+ }
2066
+ }
2067
+ }
2068
+ }
2069
+
2070
+ // Loop through the array of items
2071
+ $ids = array();
2072
+
2073
+ // Test to see if it's an array of arrays
2074
+ foreach ($data as $key => $data_row) {
2075
+ if(!is_array($data_row)){
2076
+ $data = array($data);
2077
+ }
2078
+ break;
2079
+ }
2080
+ foreach ($data as $key => $data_row) {
2081
+ $columns = array();
2082
+
2083
+ // Loop through each field (use $this->fields so only valid columns get parsed)
2084
+ foreach ($this->fields as $field_name => $field_data) {
2085
+ $field_id = $field_data['id'];
2086
+ $coltype = $field_data['coltype'];
2087
+ $pickval = $field_data['pickval'];
2088
+ $field_value = $data_row[$field_name];
2089
+
2090
+ if (null != $field_value && false !== $field_value) {
2091
+ if ('pick' == $coltype || 'file' == $coltype) {
2092
+ $field_values = is_array($field_value) ? $field_value : array($field_value);
2093
+ $pick_value = array();
2094
+ foreach ($field_values as $key => $pick_title) {
2095
+ if (is_int($pick_title) && false !== $numeric_mode) {
2096
+ $pick_value[] = $pick_title;
2097
+ }
2098
+ elseif (!empty($pick_values[$field_name][$pick_title])) {
2099
+ $pick_value[] = $pick_values[$field_name][$pick_title];
2100
+ }
2101
+ }
2102
+ $field_value = implode(',',$pick_value);
2103
+ }
2104
+ $columns[$field_name] = esc_sql(trim($field_value));
2105
+ }
2106
+ }
2107
+ if (!empty($columns)) {
2108
+ $params = array('datatype'=>$this->dtname,'columns'=>$columns);
2109
+ $ids[] = $this->save_pod_item($params);
2110
+ }
2111
+ }
2112
+ return $ids;
2113
+ }
2114
+
2115
+ /**
2116
+ * Export data
2117
+ *
2118
+ * @since 1.7.1
2119
+ */
2120
+ function export() {
2121
+ global $wpdb;
2122
+ $data = array();
2123
+ $fields = array();
2124
+ $pick_values = array();
2125
+
2126
+ // Find all pick/file fields
2127
+ $result = pod_query("SELECT id, name, coltype, pickval FROM @wp_pod_fields WHERE datatype = {$this->dt} ORDER BY weight");
2128
+ while ($row = pods_mysql_fetch_assoc($result)) {
2129
+ $field_id = $row['id'];
2130
+ $field_name = $row['name'];
2131
+ $coltype = $row['coltype'];
2132
+ $pickval = $row['pickval'];
2133
+
2134
+ // Store all pick/file values into an array
2135
+ if ('file' == $coltype) {
2136
+ $res = pod_query("SELECT ID AS id, guid AS name FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY id");
2137
+ while ($item = pods_mysql_fetch_assoc($res)) {
2138
+ $pick_values[$field_name][$item['id']] = $item['name'];
2139
+ }
2140
+ }
2141
+ elseif ('pick' == $coltype) {
2142
+ if ('wp_taxonomy' == $pickval) {
2143
+ $res = pod_query("SELECT term_id AS id, name FROM $wpdb->terms ORDER BY id");
2144
+ while ($item = pods_mysql_fetch_assoc($res)) {
2145
+ $pick_values[$field_name][$item['id']] = $item['name'];
2146
+ }
2147
+ }
2148
+ elseif ('wp_page' == $pickval || 'wp_post' == $pickval) {
2149
+ $pickval = str_replace('wp_', '', $pickval);
2150
+ $res = pod_query("SELECT ID as id, post_title as name FROM $wpdb->posts WHERE post_type = '$pickval' ORDER BY id");
2151
+ while ($item = pods_mysql_fetch_assoc($res)) {
2152
+ $pick_values[$field_name][$item['id']] = $item['name'];
2153
+ }
2154
+ }
2155
+ elseif ('wp_user' == $pickval) {
2156
+ $res = pod_query("SELECT ID as id, display_name as name FROM $wpdb->users ORDER BY id");
2157
+ while ($item = pods_mysql_fetch_assoc($res)) {
2158
+ $pick_values[$field_name][$item['id']] = $item['name'];
2159
+ }
2160
+ }
2161
+ else {
2162
+ $res = pod_query("SELECT id, name FROM @wp_pod_tbl_{$pickval} ORDER BY id");
2163
+ while ($item = pods_mysql_fetch_assoc($res)) {
2164
+ $pick_values[$field_name][$item['id']] = $item['name'];
2165
+ }
2166
+ }
2167
+ }
2168
+ $fields[$field_id] = $field_name;
2169
+ }
2170
+
2171
+ // Get all pick rel values
2172
+ $sql = "
2173
+ SELECT
2174
+ p.tbl_row_id, r.field_id, r.tbl_row_id AS item_id
2175
+ FROM
2176
+ @wp_pod_rel r
2177
+ INNER JOIN
2178
+ @wp_pod p ON p.id = r.pod_id AND p.datatype = {$this->dt}
2179
+ ORDER BY
2180
+ p.tbl_row_id
2181
+ ";
2182
+ $result = pod_query($sql);
2183
+ while ($row = pods_mysql_fetch_assoc($result)) {
2184
+ $item_id = $row['item_id'];
2185
+ $tbl_row_id = $row['tbl_row_id'];
2186
+ $field_name = $fields[$row['field_id']];
2187
+ $pick_array[$field_name][$tbl_row_id][] = $pick_values[$field_name][$item_id];
2188
+ }
2189
+
2190
+ // Access the current datatype
2191
+ $result = pod_query("SELECT * FROM @wp_pod_tbl_{$this->dtname} ORDER BY id");
2192
+ while ($row = pods_mysql_fetch_assoc($result)) {
2193
+ $tmp = array();
2194
+ $row_id = $row['id'];
2195
+
2196
+ foreach ($fields as $junk => $fname) {
2197
+ if (isset($pick_array[$fname][$row_id])) {
2198
+ $tmp[$fname] = $pick_array[$fname][$row_id];
2199
+ }
2200
+ else {
2201
+ $tmp[$fname] = $row[$fname];
2202
+ }
2203
+ }
2204
+ $data[] = $tmp;
2205
+ }
2206
+ return $data;
2207
+ }
2208
+
2209
+ /**
2210
+ * Convert CSV to a PHP array
2211
+ *
2212
+ * @param string $data The CSV input
2213
+ * @since 1.7.1
2214
+ */
2215
+ function csv_to_php($data) {
2216
+ $delimiter = ",";
2217
+ $expr = "/$delimiter(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
2218
+ $data = str_replace("\r\n", "\n", $data);
2219
+ $data = str_replace("\r", "\n", $data);
2220
+ $lines = explode("\n", $data);
2221
+ $field_names = explode($delimiter, array_shift($lines));
2222
+ $field_names = preg_replace("/^\"(.*)\"$/s", "$1", $field_names);
2223
+ foreach ($lines as $line) {
2224
+ // Skip the empty line
2225
+ if (empty($line)) continue;
2226
+ $fields = preg_split($expr, trim($line));
2227
+ $fields = preg_replace("/^\"(.*)\"$/s", "$1", $fields);
2228
+ foreach ($field_names as $key => $field) {
2229
+ $tmp[$field] = $fields[$key];
2230
+ }
2231
+ $out[] = $tmp;
2232
+ }
2233
+ return $out;
2234
+ }
2235
+
2236
+ /**
2237
+ * Resync wp_pod and wp_pod_tbl_* tables
2238
+ *
2239
+ * wp_pod_tbl_* is assumed the primary source
2240
+ * (if not found there, it'll get deleted from wp_pod)
2241
+ *
2242
+ * This might take a bit!
2243
+ *
2244
+ * @since 1.10.1
2245
+ */
2246
+ function fix_wp_pod() {
2247
+ $result = pod_query("SELECT id, name FROM @wp_pod_types ORDER BY name");
2248
+ while ($row = pods_mysql_fetch_array($result)) {
2249
+ $id = (int) $row['id'];
2250
+ $name = pods_sanitize($row['name']);
2251
+ pod_query("DELETE p FROM `@wp_pod` AS p LEFT JOIN `@wp_pod_tbl_{$name}` AS t ON t.id = p.tbl_row_id WHERE p.datatype = {$id} AND t.id IS NULL");
2252
+ pod_query("INSERT INTO `@wp_pod` (tbl_row_id, name, datatype, created, modified, author_id) SELECT t.id AS tbl_row_id, t.name AS name, {$id} AS datatype, '" . current_time('mysql') . "' AS created, '" . current_time('mysql') . "' AS modified, 0 AS author_id FROM `@wp_pod_tbl_{$name}` AS t LEFT JOIN `@wp_pod` AS p ON p.datatype = {$id} AND p.tbl_row_id = t.id WHERE p.id IS NULL");
2253
+ }
2254
+ }
2255
+ }
classes/PodCache.php ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class PodCache
3
+ {
4
+ var $debug = false;
5
+ var $cache_enabled = true;
6
+ var $form_count = 0;
7
+ var $results;
8
+
9
+ /**
10
+ * PodCache singleton
11
+ *
12
+ * @since 1.8.3
13
+ */
14
+ public static function instance()
15
+ {
16
+ static $instance = null;
17
+ if (null == $instance)
18
+ {
19
+ $instance = new PodCache();
20
+ }
21
+ return $instance;
22
+ }
23
+ }
classes/PodInit.php ADDED
@@ -0,0 +1,412 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class PodInit
3
+ {
4
+ /**
5
+ * Constructor - Pods Initialization
6
+ *
7
+ * @license http://www.gnu.org/licenses/gpl-2.0.html
8
+ */
9
+ function __construct() {
10
+ global $pod_page_exists, $pods;
11
+
12
+ // Activate and Install
13
+ register_activation_hook(__FILE__, array($this, 'activate'));
14
+ add_action('wpmu_new_blog', array($this, 'new_blog'), 10, 6);
15
+
16
+ $installed = get_option('pods_version');
17
+ if (0 < strlen($installed) && false === strpos($installed, '.'))
18
+ $installed = pods_version_to_point($installed);
19
+ if (version_compare($installed, PODS_VERSION, '<'))
20
+ $this->setup();
21
+ elseif (version_compare($installed, PODS_VERSION, '>')) {
22
+ delete_option('pods_version');
23
+ add_option('pods_version', PODS_VERSION);
24
+ }
25
+
26
+ add_action('init', array($this, 'init'));
27
+ add_action('after_setup_theme', array($this, 'deprecated'));
28
+ add_action('admin_menu', array($this, 'admin_menu'), 99);
29
+ add_action('template_redirect', array($this, 'template_redirect'));
30
+ add_action('delete_attachment', array($this, 'delete_attachment'));
31
+ add_shortcode('pods', 'pods_shortcode');
32
+
33
+ if (!defined('PODS_DISABLE_POD_PAGE_CHECK')) {
34
+ $pod_page_exists = pod_page_exists();
35
+
36
+ if (false !== $pod_page_exists) {
37
+ add_action('wp_head', array($this, 'wp_head'));
38
+ add_filter('redirect_canonical', array($this, 'kill_redirect'));
39
+ add_filter('wp_title', array($this, 'wp_title'), 0, 3);
40
+ add_filter('body_class', array($this, 'body_class'), 0, 1);
41
+ add_filter('status_header', array($this, 'status_header'));
42
+ if (defined('PODS_PAGE_PRECODE_TIMING') && false !== PODS_PAGE_PRECODE_TIMING)
43
+ add_action('after_setup_theme', array($this, 'precode'));
44
+ else
45
+ add_action('plugins_loaded', array($this, 'precode'));
46
+ add_action('wp', array($this, 'silence_404'));
47
+ }
48
+ }
49
+ }
50
+
51
+ function activate () {
52
+ global $wpdb;
53
+ if (function_exists('is_multisite') && is_multisite() && isset($_GET['networkwide']) && 1 == $_GET['networkwide']) {
54
+ $blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs"));
55
+ foreach ($blogids as $blogid)
56
+ $this->setup($blogid);
57
+ }
58
+ else
59
+ $this->setup();
60
+ }
61
+
62
+ function new_blog ($blogid, $user_id, $domain, $path, $site_id, $meta) {
63
+ if (function_exists('is_multisite') && is_multisite() && is_plugin_active_for_network('pods/init.php'))
64
+ $this->setup($blogid);
65
+ }
66
+
67
+ function setup ($blogid = null) {
68
+ global $wpdb;
69
+ if (null !== $blogid && $blogid != $wpdb->blogid) {
70
+ $old_blogid = $wpdb->blogid;
71
+ switch_to_blog($blogid);
72
+ }
73
+ // Setup DB tables
74
+ $installed = get_option('pods_version');
75
+ if (0 < strlen($installed) && false === strpos($installed, '.'))
76
+ $installed = pods_version_to_point($installed);
77
+ if (0 < strlen($installed)) {
78
+ if (version_compare($installed, PODS_VERSION, '<')) {
79
+ do_action('pods_update', PODS_VERSION, $installed, $blogid);
80
+ if (null === apply_filters('pods_update_run', null, PODS_VERSION, $installed, $blogid) && !isset($_GET['pods_bypass_update']))
81
+ include(PODS_DIR . '/sql/update.php');
82
+ do_action('pods_update_post', PODS_VERSION, $installed, $blogid);
83
+ }
84
+ }
85
+ else {
86
+ do_action('pods_install', PODS_VERSION, $installed, $blogid);
87
+ if (null === apply_filters('pods_install_run', null, PODS_VERSION, $installed, $blogid) && !isset($_GET['pods_bypass_install'])) {
88
+ $sql = file_get_contents(PODS_DIR . '/sql/dump.sql');
89
+ $sql = str_replace("\r", "\n", $sql);
90
+ $sql = str_replace("\n\n", "\n", $sql);
91
+ $sql = apply_filters('pods_install_sql', $sql, PODS_VERSION, $installed, $blogid);
92
+ $charset_collate = 'DEFAULT CHARSET utf8';
93
+ if (!empty($wpdb->charset))
94
+ $charset_collate = "DEFAULT CHARSET {$wpdb->charset}";
95
+ if (!empty($wpdb->collate))
96
+ $charset_collate .= " COLLATE {$wpdb->collate}";
97
+ if ('DEFAULT CHARSET utf8' != $charset_collate)
98
+ $sql = str_replace('DEFAULT CHARSET utf8', $charset_collate, $sql);
99
+ $sql = explode(";\n", str_replace('wp_', '@wp_', $sql));
100
+ for ($i = 0, $z = count($sql); $i < $z; $i++) {
101
+ $sql[$i] = trim($sql[$i]);
102
+ if (empty($sql[$i]))
103
+ continue;
104
+ pod_query($sql[$i], 'Cannot setup SQL tables');
105
+ }
106
+ }
107
+ delete_option('pods_version');
108
+ add_option('pods_version', PODS_VERSION);
109
+ do_action('pods_install_post', PODS_VERSION, $installed, $blogid);
110
+ }
111
+ if (null !== $blogid && $blogid != $wpdb->blogid)
112
+ switch_to_blog($old_blogid);
113
+ }
114
+
115
+ function init() {
116
+ // Session start
117
+ if (((defined('WP_DEBUG') && WP_DEBUG) || false === headers_sent()) && '' == session_id())
118
+ @session_start();
119
+
120
+ // Load necessary JS
121
+ wp_register_script('jqmodal', PODS_URL . '/ui/js/jqmodal.js', array('jquery'));
122
+ wp_register_script('pods-ui', PODS_URL . '/ui/js/pods.ui.js', array('jquery', 'jqmodal'));
123
+
124
+ $additional_settings = array('pods_disable_file_browser' => 0,
125
+ 'pods_files_require_login' => 1,
126
+ 'pods_files_require_login_cap' => '',
127
+ 'pods_disable_file_upload' => 0,
128
+ 'pods_upload_require_login' => 1,
129
+ 'pods_upload_require_login_cap' => '',
130
+ 'pods_page_precode_timing' => 0);
131
+ foreach ($additional_settings as $additional_setting => $setting) {
132
+ $setting = get_option($additional_setting, false);
133
+ if ( false !== $setting )
134
+ $additional_settings[$additional_setting] = $setting;
135
+ }
136
+ foreach ($additional_settings as $additional_setting => $setting) {
137
+ if (0 == $setting)
138
+ $setting = false;
139
+ elseif (1 == $setting)
140
+ $setting = true;
141
+ if ( in_array($additional_setting, array('pods_files_require_login', 'pods_upload_require_login') ) ) {
142
+ if ( 0 < strlen($additional_settings[$additional_setting.'_cap'] ) )
143
+ $setting = $additional_settings[$additional_setting.'_cap'];
144
+ }
145
+ elseif ( in_array($additional_setting, array('pods_files_require_login_cap', 'pods_upload_require_login_cap') ) )
146
+ continue;
147
+ if (!defined(strtoupper($additional_setting)))
148
+ define(strtoupper($additional_setting), $setting);
149
+ }
150
+ }
151
+
152
+ function precode() {
153
+ global $pods, $pod_page_exists;
154
+
155
+ $function_or_file = str_replace('*', 'w', $pod_page_exists['uri']);
156
+ $check_function = false;
157
+ $check_file = 'precode-' . $function_or_file;
158
+ if ((!defined('PODS_STRICT_MODE') || !PODS_STRICT_MODE) && (!defined('PODS_PAGE_FILES') || !PODS_PAGE_FILES))
159
+ $check_file = false;
160
+ if (false !== $check_function && false !== $check_file)
161
+ $function_or_file = pods_function_or_file($function_or_file, $check_function, 'page', $check_file);
162
+ else
163
+ $function_or_file = false;
164
+
165
+ $content = false;
166
+ if (!$function_or_file && 0 < strlen(trim($pod_page_exists['precode'])))
167
+ $content = $pod_page_exists['precode'];
168
+
169
+ if (false === $content && false !== $function_or_file && isset($function_or_file['file']))
170
+ locate_template($function_or_file['file'], true, true);
171
+ elseif (false !== $content) {
172
+ if (!defined('PODS_DISABLE_EVAL') || PODS_DISABLE_EVAL)
173
+ eval("?>$content");
174
+ }
175
+
176
+ do_action( 'pods_page_precode', $pod_page_exists, $pods );
177
+ do_action( 'pods_page_precode_' . $pod_page_exists[ 'uri' ], $pod_page_exists, $pods );
178
+
179
+ if (!is_object($pods) && (404 == $pods || is_wp_error($pods))) {
180
+ remove_action('template_redirect', array($this, 'template_redirect'));
181
+ remove_action('wp_head', array($this, 'wp_head'));
182
+ remove_filter('redirect_canonical', array($this, 'kill_redirect'));
183
+ remove_filter('wp_title', array($this, 'wp_title'));
184
+ remove_filter('body_class', array($this, 'body_class'));
185
+ remove_filter('status_header', array($this, 'status_header'));
186
+ remove_action('wp', array($this, 'silence_404'));
187
+ }
188
+ }
189
+
190
+ function admin_menu() {
191
+ $submenu = array();
192
+ $result = pod_query("SELECT name, label, is_toplevel FROM @wp_pod_types ORDER BY label, name");
193
+ while ($row = pods_mysql_fetch_array($result)) {
194
+ $name = apply_filters('pods_admin_menu_name', $row['name'], $row);
195
+ $label = trim($row['label']);
196
+ $label = ('' != $label) ? $label : $name;
197
+ $label = apply_filters('pods_admin_menu_label', $label, $row);
198
+ $row['name'] = $name;
199
+ $row['label'] = $label;
200
+ if (pods_access("pod_{$name}")) {
201
+ if (1 == $row['is_toplevel']) {
202
+ add_object_page($label, $label, 'read', "pods-manage-$name");
203
+ add_submenu_page("pods-manage-$name", 'Edit', 'Edit', 'read', "pods-manage-$name", array($this, 'pods_content_page'));
204
+ add_submenu_page("pods-manage-$name", 'Add New', 'Add New', 'read', "pods-add-$name", array($this, 'pods_content_page'));
205
+ }
206
+ else {
207
+ $submenu[trim($row['label'].$row['name'])] = $row;
208
+ }
209
+ }
210
+ }
211
+ $priv_check = array('manage_pods','manage_templates','manage_pod_pages','manage_helpers','manage_roles','manage_settings','manage_content','manage_packages');
212
+ if ((!defined('PODS_DISABLE_ADMIN_MENU') || !PODS_DISABLE_ADMIN_MENU) && (!empty($submenu) || pods_access($priv_check))) {
213
+ add_object_page('Pods', 'Pods', 'read', 'pods', null, PODS_URL.'/ui/images/icon16.png');
214
+ if (pods_access(array('manage_pods','manage_templates','manage_pod_pages','manage_helpers','manage_roles','manage_settings'))) {
215
+ add_submenu_page('pods', 'Setup', 'Setup', 'read', 'pods', array($this, 'pods_setup_page'));
216
+ }
217
+ if (pods_access('manage_packages')) {
218
+ add_submenu_page('pods', 'Package Manager', 'Package Manager', 'read', 'pods-package', array($this, 'pods_packages_page'));
219
+ }
220
+ if (pods_access('manage_content')) {
221
+ add_submenu_page('pods', 'Manage Content', 'Manage Content', 'read', 'pods-manage', array($this, 'pods_content_page'));
222
+ }
223
+ ksort($submenu);
224
+ foreach ($submenu as $item) {
225
+ $name = apply_filters('pods_admin_submenu_name', $item['name'], $item);
226
+ $label = trim($item['label']);
227
+ $label = ('' != $label) ? $label : $name;
228
+ $label = apply_filters('pods_admin_submenu_label', $label, $item);
229
+ add_submenu_page('pods', "Add $label", "Add $label", 'read', "pod-$name", array($this, 'pods_content_page'));
230
+ }
231
+ }
232
+ }
233
+
234
+ function wp_head() {
235
+ global $pods;
236
+ do_action('pods_wp_head');
237
+ if (!defined('PODS_DISABLE_VERSION_OUTPUT') || !PODS_DISABLE_VERSION_OUTPUT) {
238
+ ?>
239
+ <!-- Pods CMS <?php echo PODS_VERSION; ?> -->
240
+ <?php
241
+ }
242
+ if ((!defined('PODS_DISABLE_META') || !PODS_DISABLE_META) && is_object($pods) && !is_wp_error($pods)) {
243
+ if (isset($pods->meta) && is_array($pods->meta)) {
244
+ foreach ($pods->meta as $name => $content) {
245
+ if ('title' == $name)
246
+ continue;
247
+ ?>
248
+ <meta name="<?php echo esc_attr($name); ?>" content="<?php echo esc_attr($content); ?>" />
249
+ <?php
250
+ }
251
+ }
252
+ if (isset($pods->meta_properties) && is_array($pods->meta_properties)) {
253
+ foreach ($pods->meta_properties as $property => $content) {
254
+ ?>
255
+ <meta property="<?php echo esc_attr($property); ?>" content="<?php echo esc_attr($content); ?>" />
256
+ <?php
257
+ }
258
+ }
259
+ if (isset($pods->meta_extra) && 0 < strlen($pods->meta_extra))
260
+ echo $pods->meta_extra;
261
+ }
262
+ }
263
+
264
+ function kill_redirect() {
265
+ return false;
266
+ }
267
+
268
+ function wp_title($title, $sep = '&raquo;', $seplocation = '') {
269
+ global $pods, $pod_page_exists;
270
+
271
+ $page_title = $pod_page_exists['title'];
272
+
273
+ if (0 < strlen(trim($page_title))) {
274
+ if (is_object($pods) && !is_wp_error($pods))
275
+ $page_title = preg_replace_callback("/({@(.*?)})/m", array($pods, "parse_magic_tags"), $page_title);
276
+ $title = $page_title . " " . $sep . " ";
277
+ if ('right' == $seplocation)
278
+ $title = " " . $sep . " " . $page_title;
279
+ }
280
+ else {
281
+ $home_path = @parse_url(home_url());
282
+ if ( empty( $home_path ) )
283
+ $home_path = parse_url( pods_get_current_url() );
284
+ if ( !isset( $home_path[ 'path' ] ) )
285
+ $home_path[ 'path' ] = '/';
286
+ $uri = preg_replace('|^' . preg_quote($home_path['path'], '|') . '|', '', $_SERVER['REQUEST_URI']);
287
+ $uri = explode('?', $uri);
288
+ $uri = preg_replace("@^([/]?)(.*?)([/]?)$@", "$2", $uri[0]);
289
+ $uri = preg_replace("@(-|_)@", " ", $uri);
290
+ $uri = explode('/', $uri);
291
+
292
+ $title = '';
293
+ foreach ($uri as $page_title) {
294
+ $title .= ('right' == $seplocation) ? ucwords($page_title) . " $sep " : " $sep " . ucwords($page_title);
295
+ }
296
+ }
297
+ if ((!defined('PODS_DISABLE_META') || !PODS_DISABLE_META) && is_object($pods) && !is_wp_error($pods) && isset($pods->meta) && is_array($pods->meta) && isset($pods->meta['title']))
298
+ $title = $pods->meta['title'];
299
+ return apply_filters('pods_title', $title, $sep, $seplocation);
300
+ }
301
+
302
+ function body_class($classes) {
303
+ global $pods, $pod_page_exists;
304
+ $classes[] = 'pods';
305
+ $uri = explode('?',$pod_page_exists['uri']);
306
+ $uri = explode('#',$uri[0]);
307
+ $classes[] = 'pod-page-'.trim(str_replace('--','-',str_replace('--','-',str_replace('_','-',sanitize_title(str_replace('/','-',str_replace('*','_w_',$uri[0])))))), '-');
308
+ if (is_object($pods) && !is_wp_error($pods)) {
309
+ $classes[] = 'pod-'.trim(str_replace('--','-',str_replace('_','-',$pods->datatype)), '-');
310
+ }
311
+ if ((!defined('PODS_DISABLE_BODY_CLASSES') || !PODS_DISABLE_BODY_CLASSES) && is_object($pods) && !is_wp_error($pods) && isset($pods->body_classes))
312
+ $classes[] = $pods->body_classes;
313
+ return apply_filters('pods_body_class', $classes, $uri);
314
+ }
315
+
316
+ function status_header() {
317
+ return $_SERVER['SERVER_PROTOCOL'] . ' 200 OK';
318
+ }
319
+
320
+ function silence_404() {
321
+ global $wp_query;
322
+ $wp_query->query_vars['error'] = '';
323
+ $wp_query->is_404 = false;
324
+ }
325
+
326
+ function template_redirect() {
327
+ global $pods, $pod_page_exists;
328
+
329
+ if (false !== $pod_page_exists) {
330
+ /*
331
+ * Create pods.php in your theme directory, and
332
+ * style it to suit your needs. Some helpful functions:
333
+ *
334
+ * get_header()
335
+ * pods_content()
336
+ * get_sidebar()
337
+ * get_footer()
338
+ */
339
+ $template = $pod_page_exists['page_template'];
340
+ $template = apply_filters('pods_page_template', $template, $pod_page_exists);
341
+
342
+ $render_function = apply_filters('pods_template_redirect', 'false', $template, $pod_page_exists);
343
+
344
+ do_action('pods_page', $template, $pod_page_exists);
345
+ if (is_callable($render_function))
346
+ call_user_func($render_function);
347
+ elseif ((!defined('PODS_DISABLE_DYNAMIC_TEMPLATE') || !PODS_DISABLE_DYNAMIC_TEMPLATE) && is_object($pods) && !is_wp_error($pods) && isset($pods->page_template) && !empty($pods->page_template) && '' != locate_template(array($pods->page_template), true)) {
348
+ $template = $pods->page_template;
349
+ // found the template and included it, we're good to go!
350
+ }
351
+ elseif (!empty($pod_page_exists['page_template']) && '' != locate_template(array($pod_page_exists['page_template']), true)) {
352
+ $template = $pod_page_exists['page_template'];
353
+ // found the template and included it, we're good to go!
354
+ }
355
+ elseif ('' != locate_template(apply_filters('pods_page_default_templates', array('pods.php')), true)) {
356
+ $template = 'pods.php';
357
+ // found the template and included it, we're good to go!
358
+ }
359
+ else {
360
+ // templates not found in theme, default output
361
+ do_action('pods_page_default', $template, $pod_page_exists);
362
+ get_header();
363
+ pods_content();
364
+ get_sidebar();
365
+ get_footer();
366
+ }
367
+ do_action('pods_page_end', $template, $pod_page_exists);
368
+ exit;
369
+ }
370
+ }
371
+
372
+ function delete_attachment($attachment_id) {
373
+ $result = pod_query("SELECT id FROM @wp_pod_fields WHERE coltype = 'file'");
374
+ if (0 < pods_mysql_num_rows($result)) {
375
+ while ($row = pods_mysql_fetch_assoc($result)) {
376
+ $field_ids[] = $row['id'];
377
+ }
378
+ $field_ids = implode(',', $field_ids);
379
+
380
+ // Remove all references to the deleted attachment
381
+ do_action('pods_delete_attachment', $attachment_id, $field_ids);
382
+ pod_query("DELETE FROM @wp_pod_rel WHERE field_id IN ({$field_ids}) AND tbl_row_id = {$attachment_id}");
383
+ }
384
+ }
385
+
386
+ function pods_setup_page() {
387
+ if (!wp_script_is('jquery-ui-core', 'queue') && !wp_script_is('jquery-ui-core', 'to_do') && !wp_script_is('jquery-ui-core', 'done'))
388
+ wp_print_scripts('jquery-ui-core');
389
+ if (!wp_script_is('jquery-ui-sortable', 'queue') && !wp_script_is('jquery-ui-sortable', 'to_do') && !wp_script_is('jquery-ui-sortable', 'done'))
390
+ wp_print_scripts('jquery-ui-sortable');
391
+ if (null === apply_filters('pods_admin_setup', null))
392
+ include PODS_DIR . '/ui/manage.php';
393
+ }
394
+
395
+ function pods_packages_page() {
396
+ if (null === apply_filters('pods_admin_packages', null))
397
+ include PODS_DIR . '/ui/manage_packages.php';
398
+ }
399
+
400
+ function pods_content_page() {
401
+ if (!wp_script_is('jquery-ui-core', 'queue') && !wp_script_is('jquery-ui-core', 'to_do') && !wp_script_is('jquery-ui-core', 'done'))
402
+ wp_print_scripts('jquery-ui-core');
403
+ if (!wp_script_is('jquery-ui-sortable', 'queue') && !wp_script_is('jquery-ui-sortable', 'to_do') && !wp_script_is('jquery-ui-sortable', 'done'))
404
+ wp_print_scripts('jquery-ui-sortable');
405
+ if (null === apply_filters('pods_admin_content', null))
406
+ include PODS_DIR . '/ui/manage_content.php';
407
+ }
408
+
409
+ function deprecated() {
410
+ require_once(PODS_DIR . '/deprecated.php'); // DEPRECATED IN 2.0
411
+ }
412
+ }
classes/Pods.php DELETED
@@ -1,3936 +0,0 @@
1
- <?php
2
- /**
3
- * @package Pods
4
- */
5
- class Pods implements Iterator {
6
-
7
- /**
8
- * @var bool
9
- */
10
- private $iterator = false;
11
-
12
- /**
13
- * @var PodsAPI
14
- */
15
- public $api;
16
-
17
- /**
18
- * @var PodsData
19
- */
20
- public $data;
21
-
22
- /**
23
- * @var PodsData
24
- */
25
- public $alt_data;
26
-
27
- /**
28
- * @var array Array of pod item arrays
29
- */
30
- public $rows = array();
31
-
32
- /**
33
- * @var array Current pod item array
34
- */
35
- public $row = array();
36
-
37
- /**
38
- * @var int
39
- */
40
- private $row_number = -1;
41
-
42
- /**
43
- * @var array Override pod item array
44
- */
45
- public $row_override = array();
46
-
47
- /**
48
- * @var bool
49
- */
50
- public $display_errors = true;
51
-
52
- /**
53
- * @var array|bool|mixed|null|void
54
- */
55
- public $pod_data;
56
-
57
- /**
58
- * @var array
59
- */
60
- public $params = array();
61
-
62
- /**
63
- * @var string
64
- */
65
- public $pod = '';
66
-
67
- /**
68
- * @var int
69
- */
70
- public $pod_id = 0;
71
-
72
- /**
73
- * @var array
74
- */
75
- public $fields = array();
76
-
77
- /**
78
- * @var array
79
- */
80
- public $filters = array();
81
-
82
- /**
83
- * @var string
84
- */
85
- public $detail_page;
86
-
87
- /**
88
- * @var int
89
- */
90
- public $id = 0;
91
-
92
- /**
93
- * @var int
94
- */
95
- public $limit = 15;
96
-
97
- /**
98
- * @var int
99
- */
100
- public $offset = 0;
101
-
102
- /**
103
- * @var string
104
- */
105
- public $page_var = 'pg';
106
-
107
- /**
108
- * @var int|mixed
109
- */
110
- public $page = 1;
111
-
112
- /**
113
- * @var bool
114
- */
115
- public $pagination = true;
116
-
117
- /**
118
- * @var bool
119
- */
120
- public $search = true;
121
-
122
- /**
123
- * @var string
124
- */
125
- public $search_var = 'search';
126
-
127
- /**
128
- * @var string
129
- */
130
- public $search_mode = 'int'; // int | text | text_like
131
-
132
- /**
133
- * @var int
134
- */
135
- public $total = 0;
136
-
137
- /**
138
- * @var int
139
- */
140
- public $total_found = 0;
141
-
142
- /**
143
- * @var array
144
- */
145
- public $ui = array();
146
-
147
- /**
148
- * @var mixed SEO related vars for Pod Pages
149
- */
150
- public $page_template;
151
- public $body_classes;
152
- public $meta = array();
153
- public $meta_properties = array();
154
- public $meta_extra = '';
155
-
156
- /**
157
- * @var string Last SQL query used by a find()
158
- */
159
- public $sql;
160
-
161
- /**
162
- * @var
163
- */
164
- public $deprecated;
165
-
166
- public $datatype;
167
-
168
- public $datatype_id;
169
-
170
- /**
171
- * Constructor - Pods Framework core
172
- *
173
- * @param string $pod The pod name
174
- * @param mixed $id (optional) The ID or slug, to load a single record; Provide array of $params to run 'find'
175
- *
176
- * @return \Pods
177
- *
178
- * @license http://www.gnu.org/licenses/gpl-2.0.html
179
- * @since 1.0.0
180
- * @link http://pods.io/docs/pods/
181
- */
182
- public function __construct ( $pod = null, $id = null ) {
183
- if ( null === $pod ) {
184
- $queried_object = get_queried_object();
185
-
186
- if ( $queried_object ) {
187
- $id_lookup = true;
188
-
189
- // Post Type Singular
190
- if ( isset( $queried_object->post_type ) ) {
191
- $pod = $queried_object->post_type;
192
- }
193
- // Term Archive
194
- elseif ( isset( $queried_object->taxonomy ) ) {
195
- $pod = $queried_object->taxonomy;
196
- }
197
- // Author Archive
198
- elseif ( isset( $queried_object->user_login ) ) {
199
- $pod = 'user';
200
- }
201
- // Post Type Archive
202
- elseif ( isset( $queried_object->public ) && isset( $queried_object->name ) ) {
203
- $pod = $queried_object->name;
204
-
205
- $id_lookup = false;
206
- }
207
-
208
- if ( null === $id && $id_lookup ) {
209
- $id = get_queried_object_id();
210
- }
211
- }
212
- }
213
-
214
- $this->api = pods_api( $pod );
215
- $this->api->display_errors =& $this->display_errors;
216
-
217
- $this->data = pods_data( $this->api, $id, false );
218
- PodsData::$display_errors =& $this->display_errors;
219
-
220
- // Set up page variable
221
- if ( pods_strict( false ) ) {
222
- $this->page = 1;
223
- $this->pagination = false;
224
- $this->search = false;
225
- }
226
- else {
227
- // Get the page variable
228
- $this->page = pods_var( $this->page_var, 'get' );
229
- $this->page = ( empty( $this->page ) ? 1 : max( pods_absint( $this->page ), 1 ) );
230
- }
231
-
232
- // Set default pagination handling to on/off
233
- if ( defined( 'PODS_GLOBAL_POD_PAGINATION' ) ) {
234
- if ( !PODS_GLOBAL_POD_PAGINATION ) {
235
- $this->page = 1;
236
- $this->pagination = false;
237
- }
238
- else
239
- $this->pagination = true;
240
- }
241
-
242
- // Set default search to on/off
243
- if ( defined( 'PODS_GLOBAL_POD_SEARCH' ) ) {
244
- if ( PODS_GLOBAL_POD_SEARCH )
245
- $this->search = true;
246
- else
247
- $this->search = false;
248
- }
249
-
250
- // Set default search mode
251
- $allowed_search_modes = array( 'int', 'text', 'text_like' );
252
-
253
- if ( defined( 'PODS_GLOBAL_POD_SEARCH_MODE' ) && in_array( PODS_GLOBAL_POD_SEARCH_MODE, $allowed_search_modes ) )
254
- $this->search_mode = PODS_GLOBAL_POD_SEARCH_MODE;
255
-
256
- // Sync Settings
257
- $this->data->page =& $this->page;
258
- $this->data->limit =& $this->limit;
259
- $this->data->pagination =& $this->pagination;
260
- $this->data->search =& $this->search;
261
- $this->data->search_mode =& $this->search_mode;
262
-
263
- // Sync Pod Data
264
- $this->api->pod_data =& $this->data->pod_data;
265
- $this->pod_data =& $this->api->pod_data;
266
- $this->api->pod_id =& $this->data->pod_id;
267
- $this->pod_id =& $this->api->pod_id;
268
- $this->datatype_id =& $this->pod_id;
269
- $this->api->pod =& $this->data->pod;
270
- $this->pod =& $this->api->pod;
271
- $this->datatype =& $this->pod;
272
- $this->api->fields =& $this->data->fields;
273
- $this->fields =& $this->api->fields;
274
- $this->detail_page =& $this->data->detail_page;
275
- $this->id =& $this->data->id;
276
- $this->row =& $this->data->row;
277
- $this->rows =& $this->data->data;
278
- $this->row_number =& $this->data->row_number;
279
- $this->sql =& $this->data->sql;
280
-
281
- if ( is_array( $id ) || is_object( $id ) )
282
- $this->find( $id );
283
- }
284
-
285
- /**
286
- * Whether this Pod object is valid or not
287
- *
288
- * @return bool
289
- *
290
- * @since 2.0
291
- */
292
- public function valid () {
293
- if ( empty( $this->pod_id ) )
294
- return false;
295
-
296
- if ( $this->iterator )
297
- return isset( $this->rows[ $this->row_number ] );
298
-
299
- return true;
300
- }
301
-
302
- /**
303
- * Check if in Iterator mode
304
- *
305
- * @return bool
306
- *
307
- * @since 2.3.4
308
- *
309
- * @link http://www.php.net/manual/en/class.iterator.php
310
- */
311
- public function is_iterator () {
312
- return $this->iterator;
313
- }
314
-
315
- /**
316
- * Turn off Iterator mode to off
317
- *
318
- * @return void
319
- *
320
- * @since 2.3.4
321
- *
322
- * @link http://www.php.net/manual/en/class.iterator.php
323
- */
324
- public function stop_iterator () {
325
- $this->iterator = false;
326
-
327
- return;
328
- }
329
-
330
- /**
331
- * Rewind Iterator
332
- *
333
- * @return void|boolean
334
- *
335
- * @since 2.3.4
336
- *
337
- * @link http://www.php.net/manual/en/class.iterator.php
338
- */
339
- public function rewind () {
340
- if ( $this->iterator ) {
341
- $this->row_number = 0;
342
-
343
- return;
344
- }
345
-
346
- return false;
347
- }
348
-
349
- /**
350
- * Get current Iterator row
351
- *
352
- * @return mixed|boolean
353
- *
354
- * @since 2.3.4
355
- *
356
- * @link http://www.php.net/manual/en/class.iterator.php
357
- */
358
- public function current () {
359
- if ( $this->iterator && $this->fetch() )
360
- return $this;
361
-
362
- return false;
363
- }
364
-
365
- /**
366
- * Get current Iterator key
367
- *
368
- * @return int|boolean
369
- *
370
- * @since 2.3.4
371
- *
372
- * @link http://www.php.net/manual/en/class.iterator.php
373
- */
374
- public function key () {
375
- if ( $this->iterator )
376
- return $this->row_number;
377
-
378
- return false;
379
- }
380
-
381
- /**
382
- * Move onto the next Iterator row
383
- *
384
- * @return void|boolean
385
- *
386
- * @since 2.3.4
387
- *
388
- * @link http://www.php.net/manual/en/class.iterator.php
389
- */
390
- public function next () {
391
- if ( $this->iterator ) {
392
- $this->row_number++;
393
-
394
- return;
395
- }
396
-
397
- return false;
398
- }
399
-
400
- /**
401
- * Whether a Pod item exists or not when using fetch() or construct with an ID or slug
402
- *
403
- * @return bool
404
- *
405
- * @since 2.0
406
- */
407
- public function exists () {
408
- if ( empty( $this->row ) )
409
- return false;
410
-
411
- return true;
412
- }
413
-
414
- /**
415
- * Return an array of all rows returned from a find() call.
416
- *
417
- * Most of the time, you will want to loop through data using fetch()
418
- * instead of using this function.
419
- *
420
- * @return array|bool An array of all rows returned from a find() call, or false if no items returned
421
- *
422
- * @since 2.0
423
- * @link http://pods.io/docs/data/
424
- */
425
- public function data () {
426
- do_action( 'pods_pods_data', $this );
427
-
428
- if ( empty( $this->rows ) )
429
- return false;
430
-
431
- return (array) $this->rows;
432
- }
433
-
434
- /**
435
- * Return a field input for a specific field
436
- *
437
- * @param string|array $field Field name or Field data array
438
- * @param string $field Input field name to use (overrides default name)
439
- * @param mixed $value Current value to use
440
- *
441
- * @return string Field Input HTML
442
- *
443
- * @since 2.3.10
444
- */
445
- public function input( $field, $input_name = null, $value = '__null' ) {
446
-
447
- // Field data override
448
- if ( is_array( $field ) ) {
449
- $field_data = $field;
450
- $field = pods_var_raw( 'name', $field );
451
- }
452
- // Get field data from field name
453
- else {
454
- $field_data = $this->fields( $field );
455
- }
456
-
457
- if ( !empty( $field_data ) ) {
458
- $field_type = pods_var_raw( 'type', $field_data );
459
-
460
- if ( empty( $input_name ) ) {
461
- $input_name = $field;
462
- }
463
-
464
- if ( '__null' == $value ) {
465
- $value = $this->field( array( 'name' => $field, 'in_form' => true ) );
466
- }
467
-
468
- return PodsForm::field( $input_name, $value, $field_type, $field_data, $this, $this->id() );
469
- }
470
-
471
- return '';
472
-
473
- }
474
-
475
- /**
476
- * Return field array from a Pod, a field's data, or a field option
477
- *
478
- * @param null $field
479
- * @param null $option
480
- *
481
- * @return bool|mixed
482
- *
483
- * @since 2.0
484
- */
485
- public function fields ( $field = null, $option = null ) {
486
- // No fields found
487
- if ( empty( $this->fields ) )
488
- $field_data = array();
489
- // Return all fields
490
- elseif ( empty( $field ) )
491
- $field_data = (array) $this->fields;
492
- // Field not found
493
- elseif ( !isset( $this->fields[ $field ] ) )
494
- $field_data = array();
495
- // Return all field data
496
- elseif ( empty( $option ) )
497
- $field_data = $this->fields[ $field ];
498
- else {
499
- // Merge options
500
- $options = array_merge( $this->fields[ $field ], $this->fields[ $field ][ 'options' ] );
501
-
502
- $field_data = null;
503
-
504
- // Get a list of available items from a relationship field
505
- if ( 'data' == $option && in_array( pods_var_raw( 'type', $options ), PodsForm::tableless_field_types() ) ) {
506
- $field_data = PodsForm::field_method( 'pick', 'get_field_data', $options );
507
- }
508
- // Return option
509
- elseif ( isset( $options[ $option ] ) ) {
510
- $field_data = $options[ $option ];
511
- }
512
- }
513
-
514
- /**
515
- * Modify the field data before returning
516
- *
517
- * @since unknown
518
- *
519
- * @param array $field_data The data for the field.
520
- * @param string|null $field The specific field that data is being return for, if set when method is called or null.
521
- * @param string|null $option Value of option param when method was called. Can be used to get a list of available items from a relationship field.
522
- * @param Pods|object $this The current Pods class instance.
523
- */
524
- return apply_filters( 'pods_pods_fields', $field_data, $field, $option, $this );
525
-
526
- }
527
-
528
- /**
529
- * Return row array for an item
530
- *
531
- * @return array
532
- *
533
- * @since 2.0
534
- */
535
- public function row () {
536
- do_action( 'pods_pods_row', $this );
537
-
538
- if ( !is_array( $this->row ) )
539
- return false;
540
-
541
- return (array) $this->row;
542
- }
543
-
544
- /**
545
- * Return the output for a field. If you want the raw value for use in PHP for custom manipulation,
546
- * you will want to use field() instead. This function will automatically convert arrays into a
547
- * list of text such as "Rick, John, and Gary"
548
- *
549
- * @param string|array $name The field name, or an associative array of parameters
550
- * @param boolean $single (optional) For tableless fields, to return an array or the first
551
- *
552
- * @return string|null|false The output from the field, null if the field doesn't exist, false if no value returned for tableless fields
553
- * @since 2.0
554
- * @link http://pods.io/docs/display/
555
- */
556
- public function display ( $name, $single = null ) {
557
- $defaults = array(
558
- 'name' => $name,
559
- 'single' => $single,
560
- 'display' => true,
561
- 'serial_params' => null
562
- );
563
-
564
- if ( is_array( $name ) || is_object( $name ) ) {
565
- $defaults[ 'name' ] = null;
566
- $params = (object) array_merge( $defaults, (array) $name );
567
- }
568
- elseif ( is_array( $single ) || is_object( $single ) ) {
569
- $defaults[ 'single' ] = null;
570
- $params = (object) array_merge( $defaults, (array) $single );
571
- }
572
- else
573
- $params = $defaults;
574
-
575
- $params = (object) $params;
576
-
577
- $value = $this->field( $params );
578
-
579
- if ( is_array( $value ) ) {
580
- $fields = $this->fields;
581
-
582
- if ( isset( $this->pod_data[ 'object_fields' ] ) ) {
583
- $fields = array_merge( $fields, $this->pod_data[ 'object_fields' ] );
584
- }
585
-
586
- $serial_params = array(
587
- 'field' => $params->name,
588
- 'fields' => $fields
589
- );
590
-
591
- if ( !empty( $params->serial_params ) && is_array( $params->serial_params ) )
592
- $serial_params = array_merge( $serial_params, $params->serial_params );
593
-
594
- $value = pods_serial_comma( $value, $serial_params );
595
- }
596
-
597
- return $value;
598
- }
599
-
600
- /**
601
- * Return the raw output for a field If you want the raw value for use in PHP for custom manipulation,
602
- * you will want to use field() instead. This function will automatically convert arrays into a
603
- * list of text such as "Rick, John, and Gary"
604
- *
605
- * @param string|array $name The field name, or an associative array of parameters
606
- * @param boolean $single (optional) For tableless fields, to return an array or the first
607
- *
608
- * @return string|null|false The output from the field, null if the field doesn't exist, false if no value returned for tableless fields
609
- * @since 2.0
610
- * @link http://pods.io/docs/display/
611
- */
612
- public function raw ( $name, $single = null ) {
613
- $defaults = array(
614
- 'name' => $name,
615
- 'single' => $single,
616
- 'raw' => true
617
- );
618
-
619
- if ( is_array( $name ) || is_object( $name ) ) {
620
- $defaults[ 'name' ] = null;
621
- $params = (object) array_merge( $defaults, (array) $name );
622
- }
623
- elseif ( is_array( $single ) || is_object( $single ) ) {
624
- $defaults[ 'single' ] = null;
625
- $params = (object) array_merge( $defaults, (array) $single );
626
- }
627
- else
628
- $params = (object) $defaults;
629
-
630
- $value = $this->field( $params );
631
-
632
- return $value;
633
- }
634
-
635
- /**
636
- * Return the value for a field.
637
- *
638
- * If you are getting a field for output in a theme, most of the time you will want to use display() instead.
639
- *
640
- * This function will return arrays for relationship and file fields.
641
- *
642
- * @param string|array $name The field name, or an associative array of parameters
643
- * @param boolean $single (optional) For tableless fields, to return the whole array or the just the first item, or an associative array of parameters
644
- * @param boolean $raw (optional) Whether to return the raw value, or to run through the field type's display method, or an associative array of parameters
645
- *
646
- * @return mixed|null Value returned depends on the field type, null if the field doesn't exist, false if no value returned for tableless fields
647
- * @since 2.0
648
- * @link http://pods.io/docs/field/
649
- */
650
- public function field ( $name, $single = null, $raw = false ) {
651
- global $sitepress;
652
-
653
- $defaults = array(
654
- 'name' => $name,
655
- 'orderby' => null,
656
- 'single' => $single,
657
- 'params' => null,
658
- 'in_form' => false,
659
- 'raw' => $raw,
660
- 'raw_display' => false,
661
- 'display' => false,
662
- 'get_meta' => false,
663
- 'output' => null,
664
- 'deprecated' => false,
665
- 'args' => array() // extra data to send to field handlers
666
- );
667
-
668
- if ( is_array( $name ) || is_object( $name ) ) {
669
- $defaults[ 'name' ] = null;
670
- $params = (object) array_merge( $defaults, (array) $name );
671
- }
672
- elseif ( is_array( $single ) || is_object( $single ) ) {
673
- $defaults[ 'single' ] = null;
674
- $params = (object) array_merge( $defaults, (array) $single );
675
- }
676
- elseif ( is_array( $raw ) || is_object( $raw ) ) {
677
- $defaults[ 'raw' ] = false;
678
- $params = (object) array_merge( $defaults, (array) $raw );
679
- }
680
- else
681
- $params = (object) $defaults;
682
-
683
- if ( $params->in_form ) {
684
- $params->output = 'ids';
685
- }
686
- elseif ( null === $params->output ) {
687
- /**
688
- * Override the way realted fields are output
689
- *
690
- * @param string $output How to output related fields. Default is 'arrays'. Options: id|name|object|array|pod
691
- * @param array|object $row Current row being outputted.
692
- * @param array $params Params array passed to field().
693
- * @param object|Pods $this Current Pods object.
694
- */
695
- $params->output = apply_filters( 'pods_pods_field_related_output_type', 'arrays', $this->row, $params, $this );
696
- }
697
-
698
- if ( in_array( $params->output, array( 'id', 'name', 'object', 'array', 'pod' ) ) )
699
- $params->output .= 's';
700
-
701
- // Support old $orderby variable
702
- if ( null !== $params->single && is_string( $params->single ) && empty( $params->orderby ) ) {
703
- if ( ! class_exists( 'Pod' ) || Pod::$deprecated_notice ) {
704
- pods_deprecated( 'Pods::field', '2.0', 'Use $params[ \'orderby\' ] instead' );
705
- }
706
-
707
- $params->orderby = $params->single;
708
- $params->single = false;
709
- }
710
-
711
- if ( null !== $params->single )
712
- $params->single = (boolean) $params->single;
713
-
714
- $params->name = trim( $params->name );
715
- if ( is_array( $params->name ) || strlen( $params->name ) < 1 )
716
- return null;
717
-
718
- $params->full_name = $params->name;
719
-
720
- $value = null;
721
-
722
- if ( isset( $this->row_override[ $params->name ] ) )
723
- $value = $this->row_override[ $params->name ];
724
-
725
- if ( false === $this->row() ) {
726
- if ( false !== $this->data() )
727
- $this->fetch();
728
- else
729
- return $value;
730
- }
731
-
732
- if ( $this->data->field_id == $params->name ) {
733
- if ( isset( $this->row[ $params->name ] ) )
734
- return $this->row[ $params->name ];
735
- elseif ( null !== $value )
736
- return $value;
737
-
738
- return 0;
739
- }
740
-
741
- $tableless_field_types = PodsForm::tableless_field_types();
742
- $simple_tableless_objects = PodsForm::simple_tableless_objects();
743
-
744
- $params->traverse = array();
745
-
746
- if ( in_array( $params->name, array( '_link', 'detail_url' ) ) || ( in_array( $params->name, array( 'permalink', 'the_permalink' ) ) && in_array( $this->pod_data[ 'type' ], array( 'post_type', 'media' ) ) ) ) {
747
- if ( 0 < strlen( $this->detail_page ) )
748
- $value = get_home_url() . '/' . $this->do_magic_tags( $this->detail_page );
749
- elseif ( in_array( $this->pod_data[ 'type' ], array( 'post_type', 'media' ) ) )
750
- $value = get_permalink( $this->id() );
751
- elseif ( 'taxonomy' == $this->pod_data[ 'type' ] )
752
- $value = get_term_link( $this->id(), $this->pod_data[ 'name' ] );
753
- elseif ( 'user' == $this->pod_data[ 'type' ] )
754
- $value = get_author_posts_url( $this->id() );
755
- elseif ( 'comment' == $this->pod_data[ 'type' ] )
756
- $value = get_comment_link( $this->id() );
757
- }
758
-
759
- $field_data = $last_field_data = false;
760
- $field_type = false;
761
-
762
- $first_field = explode( '.', $params->name );
763
- $first_field = $first_field[ 0 ];
764
-
765
- if ( isset( $this->fields[ $first_field ] ) ) {
766
- $field_data = $this->fields[ $first_field ];
767
- $field_type = 'field';
768
- }
769
- elseif ( !empty( $this->pod_data[ 'object_fields' ] ) ) {
770
- if ( isset( $this->pod_data[ 'object_fields' ][ $first_field ] ) ) {
771
- $field_data = $this->pod_data[ 'object_fields' ][ $first_field ];
772
- $field_type = 'object_field';
773
- }
774
- else {
775
- foreach ( $this->pod_data[ 'object_fields' ] as $object_field => $object_field_opt ) {
776
- if ( in_array( $first_field, $object_field_opt[ 'alias' ] ) ) {
777
- if ( $first_field == $params->name )
778
- $params->name = $object_field;
779
-
780
- $first_field = $object_field;
781
- $field_data = $object_field_opt;
782
- $field_type = 'object_field';
783
-
784
- break;
785
- }
786
- }
787
- }
788
- }
789
-
790
- // Simple fields have no other output options
791
- if ( 'pick' == $field_data[ 'type' ] && in_array( $field_data[ 'pick_object' ], $simple_tableless_objects ) ) {
792
- $params->output = 'arrays';
793
- }
794
-
795
- if ( empty( $value ) && in_array( $field_data[ 'type' ], $tableless_field_types ) ) {
796
- $params->raw = true;
797
-
798
- $value = false;
799
-
800
- if ( 'arrays' != $params->output && isset( $this->row[ '_' . $params->output . '_' . $params->name ] ) ) {
801
- $value = $this->row[ '_' . $params->output . '_' . $params->name ];
802
- }
803
- elseif ( 'arrays' == $params->output && isset( $this->row[ $params->name ] ) ) {
804
- $value = $this->row[ $params->name ];
805
- }
806
-
807
- if ( false !== $value && !is_array( $value ) && 'pick' == $field_data[ 'type' ] && in_array( $field_data[ 'pick_object' ], $simple_tableless_objects ) )
808
- $value = PodsForm::field_method( 'pick', 'simple_value', $params->name, $value, $field_data, $this->pod_data, $this->id(), true );
809
- }
810
-
811
- if ( empty( $value ) && isset( $this->row[ $params->name ] ) && ( !in_array( $field_data[ 'type' ], $tableless_field_types ) || 'arrays' == $params->output ) ) {
812
- if ( empty( $field_data ) || in_array( $field_data[ 'type' ], array( 'boolean', 'number', 'currency' ) ) )
813
- $params->raw = true;
814
-
815
- if ( null === $params->single ) {
816
- if ( isset( $this->fields[ $params->name ] ) && !in_array( $this->fields[ $params->name ][ 'type' ], $tableless_field_types ) )
817
- $params->single = true;
818
- else
819
- $params->single = false;
820
- }
821
-
822
- $value = $this->row[ $params->name ];
823
- }
824
- elseif ( empty( $value ) ) {
825
- $object_field_found = false;
826
-
827
- if ( 'object_field' == $field_type ) {
828
- $object_field_found = true;
829
-
830
- if ( isset( $this->row[ $first_field ] ) )
831
- $value = $this->row[ $first_field ];
832
- elseif ( in_array( $field_data[ 'type' ], $tableless_field_types ) ) {
833
- $this->fields[ $first_field ] = $field_data;
834
-
835
- $object_field_found = false;
836
- }
837
- else
838
- return null;
839
- }
840
-
841
- if ( 'post_type' == $this->pod_data[ 'type' ] && !isset( $this->fields[ $params->name ] ) ) {
842
- if ( !isset( $this->fields[ 'post_thumbnail' ] ) && ( 'post_thumbnail' == $params->name || 0 === strpos( $params->name, 'post_thumbnail.' ) ) ) {
843
- $size = 'thumbnail';
844
-
845
- if ( 0 === strpos( $params->name, 'post_thumbnail.' ) ) {
846
- $field_names = explode( '.', $params->name );
847
-
848
- if ( isset( $field_names[ 1 ] ) )
849
- $size = $field_names[ 1 ];
850
- }
851
-
852
- // Pods will auto-get the thumbnail ID if this isn't an attachment
853
- $value = pods_image( $this->id(), $size, 0, null, true );
854
-
855
- $object_field_found = true;
856
- }
857
- elseif ( !isset( $this->fields[ 'post_thumbnail_url' ] ) && ( 'post_thumbnail_url' == $params->name || 0 === strpos( $params->name, 'post_thumbnail_url.' ) ) ) {
858
- $size = 'thumbnail';
859
-
860
- if ( 0 === strpos( $params->name, 'post_thumbnail_url.' ) ) {
861
- $field_names = explode( '.', $params->name );
862
-
863
- if ( isset( $field_names[ 1 ] ) )
864
- $size = $field_names[ 1 ];
865
- }
866
-
867
- // Pods will auto-get the thumbnail ID if this isn't an attachment
868
- $value = pods_image_url( $this->id(), $size, 0, true );
869
-
870
- $object_field_found = true;
871
- }
872
- elseif ( 0 === strpos( $params->name, 'image_attachment.' ) ) {
873
- $size = 'thumbnail';
874
-
875
- $image_id = 0;
876
-
877
- $field_names = explode( '.', $params->name );
878
-
879
- if ( isset( $field_names[ 1 ] ) )
880
- $image_id = $field_names[ 1 ];
881
-
882
- if ( isset( $field_names[ 2 ] ) )
883
- $size = $field_names[ 2 ];
884
-
885
- if ( !empty( $image_id ) ) {
886
- $value = pods_image( $image_id, $size, 0, null, true );
887
-
888
- if ( !empty( $value ) )
889
- $object_field_found = true;
890
- }
891
- }
892
- elseif ( 0 === strpos( $params->name, 'image_attachment_url.' ) ) {
893
- $size = 'thumbnail';
894
-
895
- $image_id = 0;
896
-
897
- $field_names = explode( '.', $params->name );
898
-
899
- if ( isset( $field_names[ 1 ] ) )
900
- $image_id = $field_names[ 1 ];
901
-
902
- if ( isset( $field_names[ 2 ] ) )
903
- $size = $field_names[ 2 ];
904
-
905
- if ( !empty( $image_id ) ) {
906
- $value = pods_image_url( $image_id, $size, 0, true );
907
-
908
- if ( !empty( $value ) )
909
- $object_field_found = true;
910
- }
911
- }
912
- }
913
- elseif ( 'user' == $this->pod_data[ 'type' ] && !isset( $this->fields[ $params->name ] ) ) {
914
- if ( !isset( $this->fields[ 'avatar' ] ) && ( 'avatar' == $params->name || 0 === strpos( $params->name, 'avatar.' ) ) ) {
915
- $size = null;
916
-
917
- if ( 0 === strpos( $params->name, 'avatar.' ) ) {
918
- $field_names = explode( '.', $params->name );
919
-
920
- if ( isset( $field_names[ 1 ] ) )
921
- $size = (int) $field_names[ 1 ];
922
- }
923
-
924
- if ( !empty( $size ) )
925
- $value = get_avatar( $this->id(), $size );
926
- else
927
- $value = get_avatar( $this->id() );
928
-
929
- $object_field_found = true;
930
- }
931
- }
932
- elseif ( 0 === strpos( $params->name, 'image_attachment.' ) ) {
933
- $size = 'thumbnail';
934
-
935
- $image_id = 0;
936
-
937
- $field_names = explode( '.', $params->name );
938
-
939
- if ( isset( $field_names[ 1 ] ) )
940
- $image_id = $field_names[ 1 ];
941
-
942
- if ( isset( $field_names[ 2 ] ) )
943
- $size = $field_names[ 2 ];
944
-
945
- if ( !empty( $image_id ) ) {
946
- $value = pods_image( $image_id, $size, 0, null, true );
947
-
948
- if ( !empty( $value ) )
949
- $object_field_found = true;
950
- }
951
- }
952
- elseif ( 0 === strpos( $params->name, 'image_attachment_url.' ) ) {
953
- $size = 'thumbnail';
954
-
955
- $image_id = 0;
956
-
957
- $field_names = explode( '.', $params->name );
958
-
959
- if ( isset( $field_names[ 1 ] ) )
960
- $image_id = $field_names[ 1 ];
961
-
962
- if ( isset( $field_names[ 2 ] ) )
963
- $size = $field_names[ 2 ];
964
-
965
- if ( !empty( $image_id ) ) {
966
- $value = pods_image_url( $image_id, $size, 0, true );
967
-
968
- if ( !empty( $value ) )
969
- $object_field_found = true;
970
- }
971
- }
972
-
973
- if ( false === $object_field_found ) {
974
- $params->traverse = array( $params->name );
975
-
976
- if ( false !== strpos( $params->name, '.' ) ) {
977
- $params->traverse = explode( '.', $params->name );
978
-
979
- $params->name = $params->traverse[ 0 ];
980
- }
981
-
982
- if ( isset( $this->fields[ $params->name ] ) && isset( $this->fields[ $params->name ][ 'type' ] ) ) {
983
- /**
984
- * Modify value returned by field() after its retrieved, but before its validated or formatted
985
- *
986
- * Filter name is set dynamically with name of field: "pods_pods_field_{field_name}"
987
- *
988
- * @since unknown
989
- *
990
- * @param array|string|null $value Value retrieved.
991
- * @param array|object $row Current row being outputted.
992
- * @param array $params Params array passed to field().
993
- * @param object|Pods $this Current Pods object.
994
- *
995
- */
996
- $v = apply_filters( 'pods_pods_field_' . $this->fields[ $params->name ][ 'type' ], null, $this->fields[ $params->name ], $this->row, $params, $this );
997
-
998
- if ( null !== $v )
999
- return $v;
1000
- }
1001
-
1002
- $simple = false;
1003
- $simple_data = array();
1004
-
1005
- if ( isset( $this->fields[ $params->name ] ) ) {
1006
- if ( 'meta' == $this->pod_data[ 'storage' ] ) {
1007
- if ( !in_array( $this->fields[ $params->name ][ 'type' ], $tableless_field_types ) )
1008
- $simple = true;
1009
- }
1010
-
1011
- if ( in_array( $this->fields[ $params->name ][ 'type' ], $tableless_field_types ) ) {
1012
- $params->raw = true;
1013
-
1014
- if ( 'pick' == $this->fields[ $params->name ][ 'type' ] && in_array( $this->fields[ $params->name ][ 'pick_object' ], $simple_tableless_objects ) ) {
1015
- $simple = true;
1016
- $params->single = true;
1017
- }
1018
- }
1019
- elseif ( in_array( $this->fields[ $params->name ][ 'type' ], array( 'boolean', 'number', 'currency' ) ) )
1020
- $params->raw = true;
1021
- }
1022
-
1023
- if ( !isset( $this->fields[ $params->name ] ) || !in_array( $this->fields[ $params->name ][ 'type' ], $tableless_field_types ) || $simple ) {
1024
- if ( null === $params->single ) {
1025
- if ( isset( $this->fields[ $params->name ] ) && !in_array( $this->fields[ $params->name ][ 'type' ], $tableless_field_types ) )
1026
- $params->single = true;
1027
- else
1028
- $params->single = false;
1029
- }
1030
-
1031
- $no_conflict = pods_no_conflict_check( $this->pod_data[ 'type' ] );
1032
-
1033
- if ( !$no_conflict )
1034
- pods_no_conflict_on( $this->pod_data[ 'type' ] );
1035
-
1036
- if ( in_array( $this->pod_data[ 'type' ], array( 'post_type', 'media' ) ) ) {
1037
- $id = $this->id();
1038
-
1039
- // Support for WPML 'duplicated' translation handling
1040
- if ( is_object( $sitepress ) && $sitepress->is_translated_post_type( $this->pod_data[ 'name' ] ) ) {
1041
- $master_post_id = (int) get_post_meta( $id, '_icl_lang_duplicate_of', true );
1042
-
1043
- if ( 0 < $master_post_id )
1044
- $id = $master_post_id;
1045
- }
1046
-
1047
- $value = get_post_meta( $id, $params->name, $params->single );
1048
-
1049
- $single_multi = 'single';
1050
-
1051
- if ( isset( $this->fields[ $params->name ] ) ) {
1052
- $single_multi = pods_v( $this->fields[ $params->name ][ 'type' ] . '_format_type', $this->fields[ $params->name ][ 'options' ], 'single' );
1053
- }
1054
-
1055
- if ( $simple && !is_array( $value ) && 'single' != $single_multi ) {
1056
- $value = get_post_meta( $id, $params->name );
1057
- }
1058
- }
1059
- elseif ( in_array( $this->pod_data[ 'type' ], array( 'user', 'comment' ) ) ) {
1060
- $value = get_metadata( $this->pod_data[ 'type' ], $this->id(), $params->name, $params->single );
1061
-
1062
- $single_multi = 'single';
1063
-
1064
- if ( isset( $this->fields[ $params->name ] ) ) {
1065
- $single_multi = pods_v( $this->fields[ $params->name ][ 'type' ] . '_format_type', $this->fields[ $params->name ][ 'options' ], 'single' );
1066
- }
1067
-
1068
- if ( $simple && !is_array( $value ) && 'single' != $single_multi ) {
1069
- $value = get_metadata( $this->pod_data[ 'type' ], $this->id(), $params->name );
1070
- }
1071
- }
1072
- elseif ( 'settings' == $this->pod_data[ 'type' ] )
1073
- $value = get_option( $this->pod_data[ 'name' ] . '_' . $params->name, null );
1074
-
1075
- // Handle Simple Relationships
1076
- if ( $simple ) {
1077
- if ( null === $params->single )
1078
- $params->single = false;
1079
-
1080
-
1081
- $value = PodsForm::field_method( 'pick', 'simple_value', $params->name, $value, $this->fields[ $params->name ], $this->pod_data, $this->id(), true );
1082
- }
1083
-
1084
- if ( !$no_conflict )
1085
- pods_no_conflict_off( $this->pod_data[ 'type' ] );
1086
- }
1087
- else {
1088
- // Dot-traversal
1089
- $pod = $this->pod;
1090
- $ids = array( $this->id() );
1091
- $all_fields = array();
1092
-
1093
- $lookup = $params->traverse;
1094
-
1095
- // Get fields matching traversal names
1096
- if ( !empty( $lookup ) ) {
1097
- $fields = $this->api->load_fields( array(
1098
- 'name' => $lookup,
1099
- 'type' => $tableless_field_types,
1100
- 'object_fields' => true // @todo support object fields too
1101
- ) );
1102
-
1103
- if ( !empty( $fields ) ) {
1104
- foreach ( $fields as $field ) {
1105
- if ( !empty( $field ) ) {
1106
- if ( !isset( $all_fields[ $field[ 'pod' ] ] ) )
1107
- $all_fields[ $field[ 'pod' ] ] = array();
1108
-
1109
- $all_fields[ $field[ 'pod' ] ][ $field[ 'name' ] ] = $field;
1110
- }
1111
- }
1112
- }
1113
-
1114
- if ( !empty( $this->pod_data[ 'object_fields' ] ) ) {
1115
- foreach ( $this->pod_data[ 'object_fields' ] as $object_field => $object_field_opt ) {
1116
- if ( in_array( $object_field_opt[ 'type' ], $tableless_field_types ) ) {
1117
- $all_fields[ $this->pod ][ $object_field ] = $object_field_opt;
1118
- }
1119
- }
1120
- }
1121
- }
1122
-
1123
- $last_type = $last_object = $last_pick_val = '';
1124
- $last_options = array();
1125
-
1126
- $single_multi = pods_v( $this->fields[ $params->name ][ 'type' ] . '_format_type', $this->fields[ $params->name ][ 'options' ], 'single' );
1127
-
1128
- if ( 'multi' == $single_multi )
1129
- $limit = (int) pods_v( $this->fields[ $params->name ][ 'type' ] . '_limit', $this->fields[ $params->name ][ 'options' ], 0 );
1130
- else
1131
- $limit = 1;
1132
-
1133
- $last_limit = 0;
1134
-
1135
- // Loop through each traversal level
1136
- foreach ( $params->traverse as $key => $field ) {
1137
- $last_loop = false;
1138
-
1139
- if ( count( $params->traverse ) <= ( $key + 1 ) )
1140
- $last_loop = true;
1141
-
1142
- $field_exists = isset( $all_fields[ $pod ][ $field ] );
1143
-
1144
- $simple = false;
1145
- $last_options = array();
1146
-
1147
- if ( $field_exists && 'pick' == $all_fields[ $pod ][ $field ][ 'type' ] && in_array( $all_fields[ $pod ][ $field ][ 'pick_object' ], $simple_tableless_objects ) ) {
1148
- $simple = true;
1149
- $last_options = $all_fields[ $pod ][ $field ];
1150
- }
1151
-
1152
- // Tableless handler
1153
- if ( $field_exists && ( !in_array( $all_fields[ $pod ][ $field ][ 'type' ], array( 'pick', 'taxonomy' ) ) || !$simple ) ) {
1154
- $type = $all_fields[ $pod ][ $field ][ 'type' ];
1155
- $pick_object = $all_fields[ $pod ][ $field ][ 'pick_object' ];
1156
- $pick_val = $all_fields[ $pod ][ $field ][ 'pick_val' ];
1157
-
1158
- if ( 'table' == $pick_object ) {
1159
- $pick_val = pods_v( 'pick_table', $all_fields[ $pod ][ $field ][ 'options' ], $pick_val, true );
1160
- }
1161
- elseif ( '__current__' == $pick_val ) {
1162
- $pick_val = $pod;
1163
- }
1164
-
1165
- $last_limit = 0;
1166
-
1167
- if ( in_array( $type, $tableless_field_types ) ) {
1168
- $single_multi = pods_v( "{$type}_format_type", $all_fields[ $pod ][ $field ][ 'options' ], 'single' );
1169
-
1170
- if ( 'multi' == $single_multi ) {
1171
- $last_limit = (int) pods_v( "{$type}_limit", $all_fields[ $pod ][ $field ][ 'options' ], 0 );
1172
- }
1173
- else {
1174
- $last_limit = 1;
1175
- }
1176
- }
1177
-
1178
- $last_type = $type;
1179
- $last_object = $pick_object;
1180
- $last_pick_val = $pick_val;
1181
- $last_options = $all_fields[ $pod ][ $field ];
1182
-
1183
- // Temporary hack until there's some better handling here
1184
- $last_limit = $last_limit * count( $ids );
1185
-
1186
- // Get related IDs
1187
- if ( !isset( $all_fields[ $pod ][ $field ][ 'pod_id' ] ) ) {
1188
- $all_fields[ $pod ][ $field ][ 'pod_id' ] = 0;
1189
- }
1190
-
1191
- if ( isset( $all_fields[ $pod ][ $field ][ 'id' ] ) ) {
1192
- $ids = $this->api->lookup_related_items(
1193
- $all_fields[ $pod ][ $field ][ 'id' ],
1194
- $all_fields[ $pod ][ $field ][ 'pod_id' ],
1195
- $ids,
1196
- $all_fields[ $pod ][ $field ]
1197
- );
1198
- }
1199
-
1200
- // No items found
1201
- if ( empty( $ids ) ) {
1202
- return false;
1203
- } // @todo This should return array() if not $params->single
1204
- elseif ( 0 < $last_limit ) {
1205
- $ids = array_slice( $ids, 0, $last_limit );
1206
- }
1207
-
1208
- // Get $pod if related to a Pod
1209
- if ( !empty( $pick_object ) && ( !empty( $pick_val ) || in_array( $pick_object, array( 'user', 'media', 'comment' ) ) ) ) {
1210
- if ( 'pod' == $pick_object ) {
1211
- $pod = $pick_val;
1212
- }
1213
- else {
1214
- $check = $this->api->get_table_info( $pick_object, $pick_val );
1215
-
1216
- if ( !empty( $check ) && !empty( $check[ 'pod' ] ) ) {
1217
- $pod = $check[ 'pod' ][ 'name' ];
1218
- }
1219
- }
1220
- }
1221
- }
1222
- // Assume last iteration
1223
- else {
1224
- // Invalid field
1225
- if ( 0 == $key ) {
1226
- return false;
1227
- }
1228
-
1229
- $last_loop = true;
1230
- }
1231
-
1232
- if ( $last_loop ) {
1233
- $object_type = $last_object;
1234
- $object = $last_pick_val;
1235
-
1236
- if ( in_array( $last_type, PodsForm::file_field_types() ) ) {
1237
- $object_type = 'media';
1238
- $object = 'attachment';
1239
- }
1240
-
1241
- $data = array();
1242
-
1243
- $table = $this->api->get_table_info( $object_type, $object, null, null, $last_options );
1244
-
1245
- $join = $where = array();
1246
-
1247
- if ( !empty( $table[ 'join' ] ) )
1248
- $join = (array) $table[ 'join' ];
1249
-
1250
- if ( !empty( $table[ 'where' ] ) || !empty( $ids ) ) {
1251
- foreach ( $ids as $id ) {
1252
- $where[ $id ] = '`t`.`' . $table[ 'field_id' ] . '` = ' . (int) $id;
1253
- }
1254
-
1255
- if ( !empty( $where ) ) {
1256
- $where = array( implode( ' OR ', $where ) );
1257
- }
1258
-
1259
- if ( !empty( $table[ 'where' ] ) ) {
1260
- $where = array_merge( $where, array_values( (array) $table[ 'where' ] ) );
1261
- }
1262
- }
1263
-
1264
- /**
1265
- * @var $related_obj Pods
1266
- */
1267
- $related_obj = false;
1268
-
1269
- if ( 'pod' == $object_type )
1270
- $related_obj = pods( $object, null, false );
1271
- elseif ( !empty( $table[ 'pod' ] ) )
1272
- $related_obj = pods( $table[ 'pod' ][ 'name' ], null, false );
1273
-
1274
- if ( !empty( $table[ 'table' ] ) || !empty( $related_obj ) ) {
1275
- $sql = array(
1276
- 'select' => '*, `t`.`' . $table[ 'field_id' ] . '` AS `pod_item_id`',
1277
- 'table' => $table[ 'table' ],
1278
- 'join' => $join,
1279
- 'where' => $where,
1280
- 'orderby' => $params->orderby,
1281
- 'pagination' => false,
1282
- 'search' => false,
1283
- 'limit' => -1,
1284
- 'expires' => 180 // @todo This could potentially cause issues if someone changes the data within this time and persistent storage is used
1285
- );
1286
-
1287
- // Output types
1288
- if ( in_array( $params->output, array( 'ids', 'objects', 'pods' ) ) )
1289
- $sql[ 'select' ] = '`t`.`' . $table[ 'field_id' ] . '` AS `pod_item_id`';
1290
- elseif ( 'names' == $params->output && !empty( $table[ 'field_index' ] ) )
1291
- $sql[ 'select' ] = '`t`.`' . $table[ 'field_index' ] . '` AS `pod_item_index`, `t`.`' . $table[ 'field_id' ] . '` AS `pod_item_id`';
1292
-
1293
- if ( !empty( $params->params ) && is_array( $params->params ) ) {
1294
- $where = $sql[ 'where' ];
1295
-
1296
- $sql = array_merge( $sql, $params->params );
1297
-
1298
- if ( isset( $params->params[ 'where' ] ) )
1299
- $sql[ 'where' ] = array_merge( (array) $where, (array) $params->params['where' ] );
1300
- }
1301
-
1302
- if ( empty( $related_obj ) ) {
1303
- if ( !is_object( $this->alt_data ) )
1304
- $this->alt_data = pods_data( null, 0, true, true );
1305
-
1306
- $item_data = $this->alt_data->select( $sql );
1307
- }
1308
- else
1309
- $item_data = $related_obj->find( $sql )->data();
1310
-
1311
- $items = array();
1312
-
1313
- if ( !empty( $item_data ) ) {
1314
- foreach ( $item_data as $item ) {
1315
- if ( is_array( $item ) )
1316
- $item = (object) $item;
1317
-
1318
- if ( empty( $item->pod_item_id ) )
1319
- continue;
1320
-
1321
- // Bypass pass field
1322
- if ( isset( $item->user_pass ) )
1323
- unset( $item->user_pass );
1324
-
1325
- // Get Item ID
1326
- $item_id = $item->pod_item_id;
1327
-
1328
- // Output types
1329
- if ( 'ids' == $params->output )
1330
- $item = (int) $item_id;
1331
- elseif ( 'names' == $params->output && !empty( $table[ 'field_index' ] ) )
1332
- $item = $item->pod_item_index;
1333
- elseif ( 'objects' == $params->output ) {
1334
- if ( in_array( $object_type, array( 'post_type', 'media' ) ) )
1335
- $item = get_post( $item_id );
1336
- elseif ( 'taxonomy' == $object_type )
1337
- $item = get_term( $item_id, $object );
1338
- elseif ( 'user' == $object_type ) {
1339
- $item = get_userdata( $item_id );
1340
-
1341
- if ( !empty( $item ) ) {
1342
- // Get other vars
1343
- $roles = $item->roles;
1344
- $caps = $item->caps;
1345
- $allcaps = $item->allcaps;
1346
-
1347
- $item = $item->data;
1348
-
1349
- // Set other vars
1350
- $item->roles = $roles;
1351
- $item->caps = $caps;
1352
- $item->allcaps = $allcaps;
1353
-
1354
- unset( $item->user_pass );
1355
- }
1356
- }
1357
- elseif ( 'comment' == $object_type )
1358
- $item = get_comment( $item_id );
1359
- else
1360
- $item = (object) $item;
1361
- }
1362
- elseif ( 'pods' == $params->output ) {
1363
- $item = pods( $object, (int) $item_id );
1364
- }
1365
- else // arrays
1366
- $item = get_object_vars( (object) $item );
1367
-
1368
- // Pass item data into $data
1369
- $items[ $item_id ] = $item;
1370
- }
1371
-
1372
- // Cleanup
1373
- unset( $item_data );
1374
-
1375
- // Return all of the data in the order expected
1376
- if ( empty( $params->orderby ) ) {
1377
- foreach ( $ids as $id ) {
1378
- if ( isset( $items[ $id ] ) ) {
1379
- $data[ $id ] = $items[ $id ];
1380
- }
1381
- }
1382
- } else {
1383
- // Use order set by orderby
1384
- foreach ( $items as $id => $v ) {
1385
- if ( in_array( $id, $ids ) ) {
1386
- $data[ $id ] = $v;
1387
- }
1388
- }
1389
- }
1390
- }
1391
- }
1392
-
1393
- if ( in_array( $last_type, $tableless_field_types ) || in_array( $last_type, array( 'boolean', 'number', 'currency' ) ) )
1394
- $params->raw = true;
1395
-
1396
- if ( empty( $data ) )
1397
- $value = false;
1398
- else {
1399
- $object_type = $table[ 'type' ];
1400
-
1401
- if ( in_array( $table[ 'type' ], array( 'post_type', 'attachment', 'media' ) ) )
1402
- $object_type = 'post';
1403
-
1404
- $no_conflict = true;
1405
-
1406
- if ( in_array( $object_type, array( 'post', 'user', 'comment', 'settings' ) ) ) {
1407
- $no_conflict = pods_no_conflict_check( $object_type );
1408
-
1409
- if ( !$no_conflict )
1410
- pods_no_conflict_on( $object_type );
1411
- }
1412
-
1413
- // Return entire array
1414
- if ( false !== $field_exists && ( in_array( $last_type, $tableless_field_types ) && !$simple ) )
1415
- $value = $data;
1416
- // Return an array of single column values
1417
- else {
1418
- $value = array();
1419
-
1420
- foreach ( $data as $item_id => $item ) {
1421
- // $field is 123x123, needs to be _src.123x123
1422
- $full_field = implode( '.', array_splice( $params->traverse, $key ) );
1423
-
1424
- if ( ( ( false !== strpos( $full_field, '_src' ) || 'guid' == $field ) && ( in_array( $table[ 'type' ], array( 'attachment', 'media' ) ) || in_array( $last_type, PodsForm::file_field_types() ) ) ) || ( in_array( $field, array( '_link', 'detail_url' ) ) || in_array( $field, array( 'permalink', 'the_permalink' ) ) && in_array( $last_type, PodsForm::file_field_types() ) ) ) {
1425
- $size = 'full';
1426
-
1427
- if ( false !== strpos( $full_field, '_src.' ) && 5 < strlen( $full_field ) )
1428
- $size = substr( $full_field, 5 );
1429
- elseif ( false !== strpos( $full_field, '_src_relative.' ) && 14 < strlen( $full_field ) )
1430
- $size = substr( $full_field, 14 );
1431
- elseif ( false !== strpos( $full_field, '_src_schemeless.' ) && 16 < strlen( $full_field ) )
1432
- $size = substr( $full_field, 16 );
1433
-
1434
- $value_url = pods_image_url( $item_id, $size );
1435
-
1436
- if ( false !== strpos( $full_field, '_src_relative' ) && !empty( $value_url ) ) {
1437
- $value_url_parsed = parse_url( $value_url );
1438
- $value_url = $value_url_parsed[ 'path' ];
1439
- }
1440
- elseif ( false !== strpos( $full_field, '_src_schemeless' ) && !empty( $value_url ) )
1441
- $value_url = str_replace( array( 'http://', 'https://' ), '//', $value_url );
1442
-
1443
- if ( !empty( $value_url ) )
1444
- $value[] = $value_url;
1445
-
1446
- $params->raw_display = true;
1447
- }
1448
- elseif ( false !== strpos( $full_field, '_img' ) && ( in_array( $table[ 'type' ], array( 'attachment', 'media' ) ) || in_array( $last_type, PodsForm::file_field_types() ) ) ) {
1449
- $size = 'full';
1450
-
1451
- if ( false !== strpos( $full_field, '_img.' ) && 5 < strlen( $full_field ) )
1452
- $size = substr( $full_field, 5 );
1453
-
1454
- $value[] = pods_image( $item_id, $size );
1455
-
1456
- $params->raw_display = true;
1457
- }
1458
- elseif ( in_array( $field, array( '_link', 'detail_url' ) ) || in_array( $field, array( 'permalink', 'the_permalink' ) ) ) {
1459
- if ( 'pod' == $object_type ) {
1460
- if ( is_object( $related_obj ) ) {
1461
- $related_obj->fetch( $item_id );
1462
-
1463
- $value[] = $related_obj->field( 'detail_url' );
1464
- }
1465
- else
1466
- $value[] = '';
1467
- }
1468
- elseif ( 'post' == $object_type )
1469
- $value[] = get_permalink( $item_id );
1470
- elseif ( 'taxonomy' == $object_type )
1471
- $value[] = get_term_link( $item_id, $object );
1472
- elseif ( 'user' == $object_type )
1473
- $value[] = get_author_posts_url( $item_id );
1474
- elseif ( 'comment' == $object_type )
1475
- $value[] = get_comment_link( $item_id );
1476
- else
1477
- $value[] = '';
1478
-
1479
- $params->raw_display = true;
1480
- }
1481
- elseif ( is_array( $item ) && isset( $item[ $field ] ) ) {
1482
- if ( $table[ 'field_id' ] == $field )
1483
- $value[] = (int) $item[ $field ];
1484
- else
1485
- $value[] = $item[ $field ];
1486
- }
1487
- elseif ( is_object( $item ) && isset( $item->{$field} ) ) {
1488
- if ( $table[ 'field_id' ] == $field )
1489
- $value[] = (int) $item->{$field};
1490
- else
1491
- $value[] = $item->{$field};
1492
- }
1493
- elseif ( 'post' == $object_type ) {
1494
- // Support for WPML 'duplicated' translation handling
1495
- if ( is_object( $sitepress ) && $sitepress->is_translated_post_type( $object ) ) {
1496
- $master_post_id = (int) get_post_meta( $item_id, '_icl_lang_duplicate_of', true );
1497
-
1498
- if ( 0 < $master_post_id )
1499
- $item_id = $master_post_id;
1500
- }
1501
-
1502
- $value[] = get_post_meta( $item_id, $field, true );
1503
- }
1504
- elseif ( in_array( $object_type, array( 'post', 'user', 'comment' ) ) )
1505
- $value[] = get_metadata( $object_type, $item_id, $field, true );
1506
- elseif ( 'settings' == $object_type )
1507
- $value[] = get_option( $object . '_' . $field );
1508
- }
1509
- }
1510
-
1511
- if ( in_array( $object_type, array( 'post', 'user', 'comment', 'settings' ) ) && !$no_conflict )
1512
- pods_no_conflict_off( $object_type );
1513
-
1514
- // Handle Simple Relationships
1515
- if ( $simple ) {
1516
- if ( null === $params->single )
1517
- $params->single = false;
1518
-
1519
- $value = PodsForm::field_method( 'pick', 'simple_value', $field, $value, $last_options, $all_fields[ $pod ], 0, true );
1520
- }
1521
- elseif ( false === $params->in_form && !empty( $value ) )
1522
- $value = array_values( $value );
1523
-
1524
- // Return a single column value
1525
- if ( false === $params->in_form && 1 == $limit && !empty( $value ) && is_array( $value ) && 1 == count( $value ) )
1526
- $value = current( $value );
1527
- }
1528
-
1529
- if ( $last_options ) {
1530
- $last_field_data = $last_options;
1531
- }
1532
-
1533
- break;
1534
- }
1535
- }
1536
- }
1537
- }
1538
- }
1539
-
1540
- if ( !empty( $params->traverse ) && 1 < count( $params->traverse ) ) {
1541
- $field_names = implode( '.', $params->traverse );
1542
-
1543
- $this->row[ $field_names ] = $value;
1544
- }
1545
- elseif ( 'arrays' != $params->output && in_array( $field_data[ 'type' ], $tableless_field_types ) ) {
1546
- $this->row[ '_' . $params->output . '_' . $params->full_name ] = $value;
1547
- }
1548
- elseif ( 'arrays' == $params->output || !in_array( $field_data[ 'type' ], $tableless_field_types ) ) {
1549
- $this->row[ $params->full_name ] = $value;
1550
- }
1551
-
1552
- if ( $params->single && is_array( $value ) && 1 == count( $value ) )
1553
- $value = current( $value );
1554
-
1555
- if ( ! empty( $last_field_data ) ) {
1556
- $field_data = $last_field_data;
1557
- }
1558
-
1559
- // @todo Expand this into traversed fields too
1560
- if ( !empty( $field_data ) && ( $params->display || !$params->raw ) && !$params->in_form && !$params->raw_display ) {
1561
- if ( $params->display || ( ( $params->get_meta || $params->deprecated ) && !in_array( $field_data[ 'type' ], $tableless_field_types ) ) ) {
1562
- $field_data[ 'options' ] = pods_var_raw( 'options', $field_data, array(), null, true );
1563
-
1564
- $post_temp = false;
1565
-
1566
- if ( 'post_type' == pods_v( 'type', $this->pod_data ) && 0 < $this->id() && ( !isset( $GLOBALS[ 'post' ] ) || empty( $GLOBALS[ 'post' ] ) ) ) {
1567
- global $post_ID, $post;
1568
-
1569
- $post_temp = true;
1570
-
1571
- $old_post = $GLOBALS[ 'post' ];
1572
- $old_ID = $GLOBALS[ 'post_ID' ];
1573
-
1574
- $post = get_post( $this->id() );
1575
- $post_ID = $this->id();
1576
- }
1577
-
1578
- $filter = pods_var_raw( 'display_filter', $field_data[ 'options' ] );
1579
-
1580
- if ( 0 < strlen( $filter ) ) {
1581
- $args = array(
1582
- $filter,
1583
- $value
1584
- );
1585
-
1586
- $filter_args = pods_var_raw( 'display_filter_args', $field_data[ 'options' ] );
1587
-
1588
- if ( !empty( $filter_args ) )
1589
- $args = array_merge( $args, compact( $filter_args ) );
1590
-
1591
- $value = call_user_func_array( 'apply_filters', $args );
1592
- }
1593
- elseif ( 1 == pods_v( 'display_process', $field_data[ 'options' ], 1 ) ) {
1594
- $value = PodsForm::display(
1595
- $field_data[ 'type' ],
1596
- $value,
1597
- $params->name,
1598
- array_merge( $field_data, $field_data[ 'options' ] ),
1599
- $this->pod_data,
1600
- $this->id()
1601
- );
1602
- }
1603
-
1604
- if ( $post_temp ) {
1605
- $post = $old_post;
1606
- $post_ID = $old_ID;
1607
- }
1608
- }
1609
- else {
1610
- $value = PodsForm::value(
1611
- $field_data[ 'type' ],
1612
- $value,
1613
- $params->name,
1614
- array_merge( $field_data, $field_data[ 'options' ] ),
1615
- $this->pod_data,
1616
- $this->id()
1617
- );
1618
- }
1619
- }
1620
-
1621
- /**
1622
- * Modify value returned by field() directly before output.
1623
- *
1624
- * Will not run if value was null
1625
- *
1626
- * @since unknown
1627
- *
1628
- * @param array|string|null $value Value to be returned.
1629
- * @param array|object $row Current row being outputted.
1630
- * @param array $params Params array passed to field().
1631
- * @param object|Pods $this Current Pods object.
1632
- *
1633
- */
1634
- $value = apply_filters( 'pods_pods_field', $value, $this->row, $params, $this );
1635
-
1636
- return $value;
1637
- }
1638
-
1639
- /**
1640
- * Check if an item field has a specific value in it
1641
- *
1642
- * @param string $field Field name
1643
- * @param mixed $value Value to check
1644
- * @param int $id (optional) ID of the pod item to check
1645
- *
1646
- * @return bool Whether the value was found
1647
- *
1648
- * @since 2.3.3
1649
- */
1650
- public function has ( $field, $value, $id = null ) {
1651
- $pod =& $this;
1652
-
1653
- if ( null === $id )
1654
- $id = $this->id();
1655
- elseif ( $id != $this->id() )
1656
- $pod = pods( $this->pod, $id );
1657
-
1658
- $this->do_hook( 'has', $field, $value, $id );
1659
-
1660
- if ( !isset( $this->fields[ $field ] ) )
1661
- return false;
1662
-
1663
- // Tableless fields
1664
- if ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::tableless_field_types() ) ) {
1665
- if ( !is_array( $value ) )
1666
- $value = explode( ',', $value );
1667
-
1668
- if ( 'pick' == $this->fields[ $field ][ 'type' ] && in_array( $this->fields[ $field ][ 'pick_object' ], PodsForm::simple_tableless_objects() ) ) {
1669
- $current_value = $pod->raw( $field );
1670
-
1671
- if ( !empty( $current_value ) )
1672
- $current_value = (array) $current_value;
1673
-
1674
- foreach ( $current_value as $v ) {
1675
- if ( in_array( $v, $value ) )
1676
- return true;
1677
- }
1678
- }
1679
- else {
1680
- $related_ids = $this->api->lookup_related_items( $this->fields[ $field ][ 'id' ], $this->pod_data[ 'id' ], $id, $this->fields[ $field ], $this->pod_data );
1681
-
1682
- foreach ( $value as $k => $v ) {
1683
- if ( !preg_match( '/[^0-9]/', $v ) )
1684
- $value[ $k ] = (int) $v;
1685
- // @todo Convert slugs into IDs
1686
- else {
1687
-
1688
- }
1689
- }
1690
-
1691
- foreach ( $related_ids as $v ) {
1692
- if ( in_array( $v, $value ) )
1693
- return true;
1694
- }
1695
- }
1696
- }
1697
- // Text fields
1698
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::text_field_types() ) ) {
1699
- $current_value = $pod->raw( $field );
1700
-
1701
- if ( 0 < strlen( $current_value ) )
1702
- return stripos( $current_value, $value );
1703
- }
1704
- // All other fields
1705
- else
1706
- return $this->is( $field, $value, $id );
1707
-
1708
- return false;
1709
- }
1710
-
1711
- /**
1712
- * Check if an item field is a specific value
1713
- *
1714
- * @param string $field Field name
1715
- * @param mixed $value Value to check
1716
- * @param int $id (optional) ID of the pod item to check
1717
- *
1718
- * @return bool Whether the value was found
1719
- *
1720
- * @since 2.3.3
1721
- */
1722
- public function is ( $field, $value, $id = null ) {
1723
- $pod =& $this;
1724
-
1725
- if ( null === $id )
1726
- $id = $this->id();
1727
- elseif ( $id != $this->id() )
1728
- $pod = pods( $this->pod, $id );
1729
-
1730
- $this->do_hook( 'is', $field, $value, $id );
1731
-
1732
- if ( !isset( $this->fields[ $field ] ) )
1733
- return false;
1734
-
1735
- // Tableless fields
1736
- if ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::tableless_field_types() ) ) {
1737
- if ( !is_array( $value ) )
1738
- $value = explode( ',', $value );
1739
-
1740
- $current_value = array();
1741
-
1742
- if ( 'pick' == $this->fields[ $field ][ 'type' ] && in_array( $this->fields[ $field ][ 'pick_object' ], PodsForm::simple_tableless_objects() ) ) {
1743
- $current_value = $pod->raw( $field );
1744
-
1745
- if ( !empty( $current_value ) )
1746
- $current_value = (array) $current_value;
1747
-
1748
- foreach ( $current_value as $v ) {
1749
- if ( in_array( $v, $value ) )
1750
- return true;
1751
- }
1752
- }
1753
- else {
1754
- $related_ids = $this->api->lookup_related_items( $this->fields[ $field ][ 'id' ], $this->pod_data[ 'id' ], $id, $this->fields[ $field ], $this->pod_data );
1755
-
1756
- foreach ( $value as $k => $v ) {
1757
- if ( !preg_match( '/[^0-9]/', $v ) )
1758
- $value[ $k ] = (int) $v;
1759
- // @todo Convert slugs into IDs
1760
- else {
1761
-
1762
- }
1763
- }
1764
-
1765
- foreach ( $related_ids as $v ) {
1766
- if ( in_array( $v, $value ) )
1767
- return true;
1768
- }
1769
- }
1770
-
1771
- if ( !empty( $current_value ) )
1772
- $current_value = array_filter( array_unique( $current_value ) );
1773
- else
1774
- $current_value = array();
1775
-
1776
- if ( !empty( $value ) )
1777
- $value = array_filter( array_unique( $value ) );
1778
- else
1779
- $value = array();
1780
-
1781
- sort( $current_value );
1782
- sort( $value );
1783
-
1784
- if ( $value === $current_value )
1785
- return true;
1786
- }
1787
- // Number fields
1788
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::number_field_types() ) ) {
1789
- $current_value = $pod->raw( $field );
1790
-
1791
- if ( (float) $current_value === (float) $value )
1792
- return true;
1793
- }
1794
- // Date fields
1795
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::date_field_types() ) ) {
1796
- $current_value = $pod->raw( $field );
1797
-
1798
- if ( 0 < strlen( $current_value ) ) {
1799
- if ( strtotime( $current_value ) == strtotime( $value ) )
1800
- return true;
1801
- }
1802
- elseif ( empty( $value ) )
1803
- return true;
1804
- }
1805
- // Text fields
1806
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::text_field_types() ) ) {
1807
- $current_value = $pod->raw( $field );
1808
-
1809
- if ( (string) $current_value === (string) $value )
1810
- return true;
1811
- }
1812
- // All other fields
1813
- else {
1814
- $current_value = $pod->raw( $field );
1815
-
1816
- if ( $current_value === $value )
1817
- return true;
1818
- }
1819
-
1820
- return false;
1821
- }
1822
-
1823
- /**
1824
- * Return the item ID
1825
- *
1826
- * @return int
1827
- * @since 2.0
1828
- */
1829
- public function id () {
1830
- if ( isset( $this->data->row ) && isset( $this->data->row[ 'id' ] ) ) {
1831
- // If we already have data loaded return that ID
1832
- return $this->data->row[ 'id' ];
1833
- }
1834
- return $this->field( $this->data->field_id );
1835
- }
1836
-
1837
- /**
1838
- * Return the previous item ID, loops at the last id to return the first
1839
- *
1840
- * @param int $id
1841
- * @param array $params_override
1842
- *
1843
- * @return int
1844
- * @since 2.0
1845
- */
1846
- public function prev_id ( $id = null, $params_override = null ) {
1847
- if ( null === $id )
1848
- $id = $this->id();
1849
-
1850
- $id = (int) $id;
1851
-
1852
- $params = array(
1853
- 'select' => "`t`.`{$this->data->field_id}`",
1854
- 'where' => "`t`.`{$this->data->field_id}` < {$id}",
1855
- 'orderby' => "`t`.`{$this->data->field_id}` DESC",
1856
- 'limit' => 1
1857
- );
1858
-
1859
- if ( !empty( $params_override ) || !empty( $this->params ) ) {
1860
- if ( !empty( $params_override ) )
1861
- $params = $params_override;
1862
- elseif ( !empty( $this->params ) )
1863
- $params = $this->params;
1864
-
1865
- if ( is_object( $params ) ) {
1866
- $params = get_object_vars( $params );
1867
- }
1868
-
1869
- if ( 0 < $id ) {
1870
- if ( isset( $params[ 'where' ] ) && !empty( $params[ 'where' ] ) ) {
1871
- $params[ 'where' ] = (array) $params[ 'where' ];
1872
- $params[ 'where' ][] = "`t`.`{$this->data->field_id}` < {$id}";
1873
- }
1874
- else {
1875
- $params[ 'where' ] = "`t`.`{$this->data->field_id}` < {$id}";
1876
- }
1877
- }
1878
- elseif ( isset( $params[ 'offset' ] ) && 0 < $params[ 'offset' ] )
1879
- $params[ 'offset' ] -= 1;
1880
- elseif ( !isset( $params[ 'offset' ] ) && !empty( $this->params ) && 0 < $this->row_number )
1881
- $params[ 'offset' ] = $this->row_number - 1;
1882
- else
1883
- return 0;
1884
-
1885
- if ( isset( $params[ 'orderby' ] ) && !empty( $params[ 'orderby' ] ) ) {
1886
- if ( is_array( $params[ 'orderby' ] ) ) {
1887
- foreach ( $params[ 'orderby' ] as $orderby => $dir ) {
1888
- $dir = strtoupper( $dir );
1889
-
1890
- if ( !in_array( $dir, array( 'ASC', 'DESC' ) ) ) {
1891
- continue;
1892
- }
1893
-
1894
- if ( 'ASC' == $dir ) {
1895
- $params[ 'orderby' ][ $orderby ] = 'DESC';
1896
- }
1897
- else {
1898
- $params[ 'orderby' ][ $orderby ] = 'ASC';
1899
- }
1900
- }
1901
-
1902
- $params[ 'orderby' ][ $this->data->field_id ] = 'DESC';
1903
- }
1904
- elseif ( "`t`.`{$this->data->field_id}` DESC" != $params[ 'orderby' ] ) {
1905
- $params[ 'orderby' ] .= ", `t`.`{$this->data->field_id}` DESC";
1906
- }
1907
- }
1908
-
1909
- $params[ 'select' ] = "`t`.`{$this->data->field_id}`";
1910
- $params[ 'limit' ] = 1;
1911
- }
1912
-
1913
- $pod = pods( $this->pod, $params );
1914
-
1915
- $new_id = 0;
1916
-
1917
- if ( $pod->fetch() )
1918
- $new_id = $pod->id();
1919
-
1920
- $new_id = $this->do_hook( 'prev_id', $new_id, $id, $pod, $params_override );
1921
-
1922
- return $new_id;
1923
- }
1924
-
1925
- /**
1926
- * Return the next item ID, loops at the first id to return the last
1927
- *
1928
- * @param int $id
1929
- * @param array $find_params
1930
- *
1931
- * @return int
1932
- * @since 2.0
1933
- */
1934
- public function next_id ( $id = null, $params_override = null ) {
1935
- if ( null === $id )
1936
- $id = $this->id();
1937
-
1938
- $id = (int) $id;
1939
-
1940
- $params = array(
1941
- 'select' => "`t`.`{$this->data->field_id}`",
1942
- 'where' => "{$id} < `t`.`{$this->data->field_id}`",
1943
- 'orderby' => "`t`.`{$this->data->field_id}` ASC",
1944
- 'limit' => 1
1945
- );
1946
-
1947
- if ( !empty( $params_override ) || !empty( $this->params ) ) {
1948
- if ( !empty( $params_override ) )
1949
- $params = $params_override;
1950
- elseif ( !empty( $this->params ) )
1951
- $params = $this->params;
1952
-
1953
- if ( is_object( $params ) ) {
1954
- $params = get_object_vars( $params );
1955
- }
1956
-
1957
- if ( 0 < $id ) {
1958
- if ( isset( $params[ 'where' ] ) && !empty( $params[ 'where' ] ) ) {
1959
- $params[ 'where' ] = (array) $params[ 'where' ];
1960
- $params[ 'where' ][] = "{$id} < `t`.`{$this->data->field_id}`";
1961
- }
1962
- else {
1963
- $params[ 'where' ] = "{$id} < `t`.`{$this->data->field_id}`";
1964
- }
1965
- }
1966
- elseif ( !isset( $params[ 'offset' ] ) ) {
1967
- if ( !empty( $this->params ) && -1 < $this->row_number )
1968
- $params[ 'offset' ] = $this->row_number + 1;
1969
- else
1970
- $params[ 'offset' ] = 1;
1971
- }
1972
- else
1973
- $params[ 'offset' ] += 1;
1974
-
1975
- $params[ 'select' ] = "`t`.`{$this->data->field_id}`";
1976
- $params[ 'limit' ] = 1;
1977
- }
1978
-
1979
- $pod = pods( $this->pod, $params );
1980
-
1981
- $new_id = 0;
1982
-
1983
- if ( $pod->fetch() )
1984
- $new_id = $pod->id();
1985
-
1986
- $new_id = $this->do_hook( 'next_id', $new_id, $id, $pod, $params_override );
1987
-
1988
- return $new_id;
1989
- }
1990
-
1991
- /**
1992
- * Return the first item ID
1993
- *
1994
- * @param array $params_override
1995
- *
1996
- * @return int
1997
- * @since 2.3
1998
- */
1999
- public function first_id ( $params_override = null ) {
2000
- $params = array(
2001
- 'select' => "`t`.`{$this->data->field_id}`",
2002
- 'orderby' => "`t`.`{$this->data->field_id}` ASC",
2003
- 'limit' => 1
2004
- );
2005
-
2006
- if ( !empty( $params_override ) || !empty( $this->params ) ) {
2007
- if ( !empty( $params_override ) )
2008
- $params = $params_override;
2009
- elseif ( !empty( $this->params ) )
2010
- $params = $this->params;
2011
-
2012
- if ( is_object( $params ) ) {
2013
- $params = get_object_vars( $params );
2014
- }
2015
-
2016
- $params[ 'select' ] = "`t`.`{$this->data->field_id}`";
2017
- $params[ 'offset' ] = 0;
2018
- $params[ 'limit' ] = 1;
2019
- }
2020
-
2021
- $pod = pods( $this->pod, $params );
2022
-
2023
- $new_id = 0;
2024
-
2025
- if ( $pod->fetch() )
2026
- $new_id = $pod->id();
2027
-
2028
- $new_id = $this->do_hook( 'first_id', $new_id, $pod, $params_override );
2029
-
2030
- return $new_id;
2031
- }
2032
-
2033
- /**
2034
- * Return the last item ID
2035
- *
2036
- * @param array $params_override
2037
- *
2038
- * @return int
2039
- * @since 2.3
2040
- */
2041
- public function last_id ( $params_override = null ) {
2042
- $params = array(
2043
- 'select' => "`t`.`{$this->data->field_id}`",
2044
- 'orderby' => "`t`.`{$this->data->field_id}` DESC",
2045
- 'limit' => 1
2046
- );
2047
-
2048
- if ( !empty( $params_override ) || !empty( $this->params ) ) {
2049
- if ( !empty( $params_override ) )
2050
- $params = $params_override;
2051
- elseif ( !empty( $this->params ) )
2052
- $params = $this->params;
2053
-
2054
- if ( is_object( $params ) ) {
2055
- $params = get_object_vars( $params );
2056
- }
2057
-
2058
- if ( isset( $params[ 'total_found' ] ) )
2059
- $params[ 'offset' ] = $params[ 'total_found' ] - 1;
2060
- else
2061
- $params[ 'offset' ] = $this->total_found() - 1;
2062
-
2063
- if ( isset( $params[ 'orderby' ] ) && !empty( $params[ 'orderby' ] ) ) {
2064
- if ( is_array( $params[ 'orderby' ] ) ) {
2065
- foreach ( $params[ 'orderby' ] as $orderby => $dir ) {
2066
- $dir = strtoupper( $dir );
2067
-
2068
- if ( !in_array( $dir, array( 'ASC', 'DESC' ) ) ) {
2069
- continue;
2070
- }
2071
-
2072
- if ( 'ASC' == $dir ) {
2073
- $params[ 'orderby' ][ $orderby ] = 'DESC';
2074
- }
2075
- else {
2076
- $params[ 'orderby' ][ $orderby ] = 'ASC';
2077
- }
2078
- }
2079
-
2080
- $params[ 'orderby' ][ $this->data->field_id ] = 'DESC';
2081
- }
2082
- elseif ( "`t`.`{$this->data->field_id}` DESC" != $params[ 'orderby' ] ) {
2083
- $params[ 'orderby' ] .= ", `t`.`{$this->data->field_id}` DESC";
2084
- }
2085
- }
2086
-
2087
- $params[ 'select' ] = "`t`.`{$this->data->field_id}`";
2088
- $params[ 'limit' ] = 1;
2089
- }
2090
-
2091
- $pod = pods( $this->pod, $params );
2092
-
2093
- $new_id = 0;
2094
-
2095
- if ( $pod->fetch() )
2096
- $new_id = $pod->id();
2097
-
2098
- $new_id = $this->do_hook( 'last_id', $new_id, $pod, $params_override );
2099
- return $new_id;
2100
- }
2101
-
2102
- /**
2103
- * Return the item name
2104
- *
2105
- * @return string
2106
- * @since 2.0
2107
- */
2108
- public function index () {
2109
- return $this->field( $this->data->field_index );
2110
- }
2111
-
2112
- /**
2113
- * Find items of a pod, much like WP_Query, but with advanced table handling.
2114
- *
2115
- * @param array $params An associative array of parameters
2116
- * @param int $limit (optional) (deprecated) Limit the number of items to find, use -1 to return all items with no limit
2117
- * @param string $where (optional) (deprecated) SQL WHERE declaration to use
2118
- * @param string $sql (optional) (deprecated) For advanced use, a custom SQL query to run
2119
- *
2120
- * @return \Pods The pod object
2121
- * @since 2.0
2122
- * @link http://pods.io/docs/find/
2123
- */
2124
- public function find ( $params = null, $limit = 15, $where = null, $sql = null ) {
2125
-
2126
- $tableless_field_types = PodsForm::tableless_field_types();
2127
- $simple_tableless_objects = PodsForm::simple_tableless_objects();
2128
-
2129
-
2130
- $this->params = $params;
2131
-
2132
- $select = '`t`.*';
2133
-
2134
- if ( !in_array( $this->pod_data[ 'type' ], array( 'pod', 'table' ) ) && 'table' == $this->pod_data[ 'storage' ] )
2135
- $select .= ', `d`.*';
2136
-
2137
- if ( empty( $this->data->table ) )
2138
- return $this;
2139
-
2140
- $defaults = array(
2141
- 'table' => $this->data->table,
2142
- 'select' => $select,
2143
- 'join' => null,
2144
-
2145
- 'where' => $where,
2146
- 'groupby' => null,
2147
- 'having' => null,
2148
- 'orderby' => null,
2149
-
2150
- 'limit' => (int) $limit,
2151
- 'offset' => null,
2152
- 'page' => (int) $this->page,
2153
- 'page_var' => $this->page_var,
2154
- 'pagination' => (boolean) $this->pagination,
2155
-
2156
- 'search' => (boolean) $this->search,
2157
- 'search_var' => $this->search_var,
2158
- 'search_query' => null,
2159
- 'search_mode' => $this->search_mode,
2160
- 'search_across' => false,
2161
- 'search_across_picks' => false,
2162
- 'search_across_files' => false,
2163
-
2164
- 'filters' => $this->filters,
2165
- 'sql' => $sql,
2166
-
2167
- 'expires' => null,
2168
- 'cache_mode' => 'cache'
2169
- );
2170
-
2171
- if ( is_array( $params ) )
2172
- $params = (object) array_merge( $defaults, $params );
2173
- if ( is_object( $params ) )
2174
- $params = (object) array_merge( $defaults, get_object_vars( $params ) );
2175
- else {
2176
- $defaults[ 'orderby' ] = $params;
2177
- $params = (object) $defaults;
2178
- }
2179
-
2180
- $params = apply_filters( 'pods_pods_find', $params );
2181
-
2182
- $params->limit = (int) $params->limit;
2183
-
2184
- if ( 0 == $params->limit )
2185
- $params->limit = -1;
2186
-
2187
- $this->limit = (int) $params->limit;
2188
- $this->offset = (int) $params->offset;
2189
- $this->page = (int) $params->page;
2190
- $this->page_var = $params->page_var;
2191
- $this->pagination = (boolean) $params->pagination;
2192
- $this->search = (boolean) $params->search;
2193
- $this->search_var = $params->search_var;
2194
- $params->join = (array) $params->join;
2195
-
2196
- if ( empty( $params->search_query ) )
2197
- $params->search_query = pods_var( $this->search_var, 'get', '' );
2198
-
2199
- // Allow orderby array ( 'field' => 'asc|desc' )
2200
- if ( !empty( $params->orderby ) && is_array( $params->orderby ) ) {
2201
- foreach ( $params->orderby as $k => &$orderby ) {
2202
- if ( !is_numeric( $k ) ) {
2203
- $key = '';
2204
-
2205
- $order = 'ASC';
2206
-
2207
- if ( 'DESC' == strtoupper( $orderby ) )
2208
- $order = 'DESC';
2209
-
2210
- if ( isset( $this->fields[ $k ] ) && in_array( $this->fields[ $k ][ 'type' ], $tableless_field_types ) ) {
2211
- if ( in_array( $this->fields[ $k ][ 'pick_object' ], $simple_tableless_objects ) ) {
2212
- if ( 'table' == $this->pod_data[ 'storage' ] ) {
2213
- if ( !in_array( $this->pod_data[ 'type' ], array( 'pod', 'table' ) ) )
2214
- $key = "`d`.`{$k}`";
2215
- else
2216
- $key = "`t`.`{$k}`";
2217
- }
2218
- else
2219
- $key = "`{$k}`.`meta_value`";
2220
- }
2221
- else {
2222
- $pick_val = $this->fields[ $k ][ 'pick_val' ];
2223
-
2224
- if ( '__current__' == $pick_val )
2225
- $pick_val = $this->pod;
2226
-
2227
- $table = $this->api->get_table_info( $this->fields[ $k ][ 'pick_object' ], $pick_val );
2228
-
2229
- if ( !empty( $table ) )
2230
- $key = "`{$k}`.`" . $table[ 'field_index' ] . '`';
2231
- }
2232
- }
2233
-
2234
- if ( empty( $key ) ) {
2235
- if ( !in_array( $this->pod_data[ 'type' ], array( 'pod', 'table' ) ) ) {
2236
- if ( isset( $this->pod_data[ 'object_fields' ][ $k ] ) )
2237
- $key = "`t`.`{$k}`";
2238
- elseif ( isset( $this->fields[ $k ] ) ) {
2239
- if ( 'table' == $this->pod_data[ 'storage' ] )
2240
- $key = "`d`.`{$k}`";
2241
- else
2242
- $key = "`{$k}`.`meta_value`";
2243
- }
2244
- else {
2245
- foreach ( $this->pod_data[ 'object_fields' ] as $object_field => $object_field_opt ) {
2246
- if ( $object_field == $k || in_array( $k, $object_field_opt[ 'alias' ] ) )
2247
- $key = "`t`.`{$object_field}`";
2248
- }
2249
- }
2250
- }
2251
- elseif ( isset( $this->fields[ $k ] ) ) {
2252
- if ( 'table' == $this->pod_data[ 'storage' ] )
2253
- $key = "`t`.`{$k}`";
2254
- else
2255
- $key = "`{$k}`.`meta_value`";
2256
- }
2257
-
2258
- if ( empty( $key ) ) {
2259
- $key = $k;
2260
-
2261
- if ( false === strpos( $key, ' ' ) && false === strpos( $key, '`' ) )
2262
- $key = '`' . str_replace( '.', '`.`', $key ) . '`';
2263
- }
2264
- }
2265
-
2266
- $orderby = $key;
2267
-
2268
- if ( false === strpos( $orderby, ' ' ) )
2269
- $orderby .= ' ' . $order;
2270
- }
2271
- }
2272
- }
2273
-
2274
- // Add prefix to $params->orderby if needed
2275
- if ( !empty( $params->orderby ) ) {
2276
- if ( !is_array( $params->orderby ) )
2277
- $params->orderby = array( $params->orderby );
2278
-
2279
- foreach ( $params->orderby as &$prefix_orderby ) {
2280
- if ( false === strpos( $prefix_orderby, ',' ) && false === strpos( $prefix_orderby, '(' ) && false === stripos( $prefix_orderby, ' AS ' ) && false === strpos( $prefix_orderby, '`' ) && false === strpos( $prefix_orderby, '.' ) ) {
2281
- if ( false !== stripos( $prefix_orderby, ' DESC' ) ) {
2282
- $k = trim( str_ireplace( array( '`', ' DESC' ), '', $prefix_orderby ) );
2283
- $dir = 'DESC';
2284
- }
2285
- else {
2286
- $k = trim( str_ireplace( array( '`', ' ASC' ), '', $prefix_orderby ) );
2287
- $dir = 'ASC';
2288
- }
2289
-
2290
- $key = $k;
2291
-
2292
- if ( !in_array( $this->pod_data[ 'type' ], array( 'pod', 'table' ) ) ) {
2293
- if ( isset( $this->pod_data[ 'object_fields' ][ $k ] ) )
2294
- $key = "`t`.`{$k}`";
2295
- elseif ( isset( $this->fields[ $k ] ) ) {
2296
- if ( 'table' == $this->pod_data[ 'storage' ] )
2297
- $key = "`d`.`{$k}`";
2298
- else
2299
- $key = "`{$k}`.`meta_value`";
2300
- }
2301
- else {
2302
- foreach ( $this->pod_data[ 'object_fields' ] as $object_field => $object_field_opt ) {
2303
- if ( $object_field == $k || in_array( $k, $object_field_opt[ 'alias' ] ) )
2304
- $key = "`t`.`{$object_field}`";
2305
- }
2306
- }
2307
- }
2308
- elseif ( isset( $this->fields[ $k ] ) ) {
2309
- if ( 'table' == $this->pod_data[ 'storage' ] )
2310
- $key = "`t`.`{$k}`";
2311
- else
2312
- $key = "`{$k}`.`meta_value`";
2313
- }
2314
-
2315
- $prefix_orderby = "{$key} {$dir}";
2316
- }
2317
- }
2318
- }
2319
-
2320
- $this->data->select( $params );
2321
-
2322
- return $this;
2323
- }
2324
-
2325
- /**
2326
- * Fetch an item from a Pod. If $id is null, it will return the next item in the list after running find().
2327
- * You can rewind the list back to the start by using reset().
2328
- *
2329
- * Providing an $id will fetch a specific item from a Pod, much like a call to pods(), and can handle either an id or slug.
2330
- *
2331
- * @see PodsData::fetch
2332
- *
2333
- * @param int $id ID or slug of the item to fetch
2334
- * @param bool $explicit_set Whether to set explicitly (use false when in loop)
2335
- *
2336
- * @return array An array of fields from the row
2337
- *
2338
- * @since 2.0
2339
- * @link http://pods.io/docs/fetch/
2340
- */
2341
- public function fetch ( $id = null, $explicit_set = true ) {
2342
- /**
2343
- * Runs directly before an item is fetched by fetch()
2344
- *
2345
- * @since unknown
2346
- *
2347
- * @param int|string|null $id Item ID being fetched or null.
2348
- * @param object|Pods $this Current Pods object.
2349
- */
2350
- do_action( 'pods_pods_fetch', $id, $this );
2351
-
2352
- if ( !empty( $id ) )
2353
- $this->params = array();
2354
-
2355
- $this->data->fetch( $id, $explicit_set );
2356
-
2357
- return $this->row;
2358
- }
2359
-
2360
- /**
2361
- * (Re)set the MySQL result pointer
2362
- *
2363
- * @see PodsData::reset
2364
- *
2365
- * @param int $row ID of the row to reset to
2366
- *
2367
- * @return \Pods The pod object
2368
- *
2369
- * @since 2.0
2370
- * @link http://pods.io/docs/reset/
2371
- */
2372
- public function reset ( $row = null ) {
2373
- /**
2374
- * Runs directly before the Pods object is reset by reset()
2375
- *
2376
- * @since unknown
2377
- *
2378
- * @param int|string|null The ID of the row being reset to or null if being reset to the beginningg.
2379
- * @param object|Pods $this Current Pods object.
2380
- */
2381
- do_action( 'pods_pods_reset', $row, $this );
2382
-
2383
- $this->data->reset( $row );
2384
-
2385
- return $this;
2386
- }
2387
-
2388
- /**
2389
- * Fetch the total row count returned by the last call to find(), based on the 'limit' parameter set.
2390
- *
2391
- * This is different than the total number of rows found in the database, which you can get with total_found().
2392
- *
2393
- * @see PodsData::total
2394
- *
2395
- * @return int Number of rows returned by find(), based on the 'limit' parameter set
2396
- * @since 2.0
2397
- * @link http://pods.io/docs/total/
2398
- */
2399
- public function total () {
2400
- do_action( 'pods_pods_total', $this );
2401
-
2402
- $this->data->total();
2403
-
2404
- $this->total =& $this->data->total;
2405
-
2406
- return $this->total;
2407
- }
2408
-
2409
- /**
2410
- * Fetch the total amount of rows found by the last call to find(), regardless of the 'limit' parameter set.
2411
- *
2412
- * This is different than the total number of rows limited by the current call, which you can get with total().
2413
- *
2414
- * @see PodsData::total_found
2415
- *
2416
- * @return int Number of rows returned by find(), regardless of the 'limit' parameter
2417
- * @since 2.0
2418
- * @link http://pods.io/docs/total-found/
2419
- */
2420
- public function total_found () {
2421
- /**
2422
- * Runs directly before the value of total_found() is determined and returned.
2423
- *
2424
- * @since unknown
2425
- *
2426
- * @param object|Pods $this Current Pods object.
2427
- *
2428
- */
2429
- do_action( 'pods_pods_total_found', $this );
2430
-
2431
- $this->data->total_found();
2432
-
2433
- $this->total_found =& $this->data->total_found;
2434
-
2435
- return $this->total_found;
2436
- }
2437
-
2438
- /**
2439
- * Fetch the total number of pages, based on total rows found and the last find() limit
2440
- *
2441
- * @param null|int $limit Rows per page
2442
- * @param null|int $offset Offset of rows
2443
- * @param null|int $total Total rows
2444
- *
2445
- * @return int Number of pages
2446
- * @since 2.3.10
2447
- */
2448
- public function total_pages( $limit = null, $offset = null, $total = null ) {
2449
-
2450
- $this->do_hook( 'total_pages' );
2451
-
2452
- if ( null === $limit ) {
2453
- $limit = $this->limit;
2454
- }
2455
-
2456
- if ( null === $offset ) {
2457
- $offset = $this->offset;
2458
- }
2459
-
2460
- if ( null === $total ) {
2461
- $total = $this->total_found();
2462
- }
2463
-
2464
- $total_pages = ceil( ( $total - $offset ) / $limit );
2465
-
2466
- return $total_pages;
2467
-
2468
- }
2469
-
2470
- /**
2471
- * Fetch the zebra switch
2472
- *
2473
- * @see PodsData::zebra
2474
- *
2475
- * @return bool Zebra state
2476
- * @since 1.12
2477
- */
2478
- public function zebra () {
2479
- $this->do_hook( 'zebra' );
2480
-
2481
- return $this->data->zebra();
2482
- }
2483
-
2484
- /**
2485
- * Fetch the nth state
2486
- *
2487
- * @see PodsData::nth
2488
- *
2489
- * @param int|string $nth The $nth to match on the PodsData::row_number
2490
- *
2491
- * @return bool Whether $nth matches
2492
- * @since 2.3
2493
- */
2494
- public function nth ( $nth = null ) {
2495
- $this->do_hook( 'nth', $nth );
2496
-
2497
- return $this->data->nth( $nth );
2498
- }
2499
-
2500
- /**
2501
- * Fetch the current position in the loop (starting at 1)
2502
- *
2503
- * @see PodsData::position
2504
- *
2505
- * @return int Current row number (+1)
2506
- * @since 2.3
2507
- */
2508
- public function position () {
2509
- $this->do_hook( 'position' );
2510
-
2511
- return $this->data->position();
2512
- }
2513
-
2514
- /**
2515
- * Add an item to a Pod by giving an array of field data or set a specific field to
2516
- * a specific value if you're just wanting to add a new item but only set one field.
2517
- *
2518
- * You may be looking for save() in most cases where you're setting a specific field.
2519
- *
2520
- * @see PodsAPI::save_pod_item
2521
- *
2522
- * @param array|string $data Either an associative array of field information or a field name
2523
- * @param mixed $value (optional) Value of the field, if $data is a field name
2524
- *
2525
- * @return int The item ID
2526
- *
2527
- * @since 2.0
2528
- * @link http://pods.io/docs/add/
2529
- */
2530
- public function add ( $data = null, $value = null ) {
2531
- if ( null !== $value )
2532
- $data = array( $data => $value );
2533
-
2534
- $data = (array) $this->do_hook( 'add', $data );
2535
-
2536
- if ( empty( $data ) )
2537
- return 0;
2538
-
2539
- $params = array(
2540
- 'pod' => $this->pod,
2541
- 'data' => $data,
2542
- 'allow_custom_fields' => true
2543
- );
2544
-
2545
- return $this->api->save_pod_item( $params );
2546
- }
2547
-
2548
- /**
2549
- * Add an item to the values of a relationship field, add a value to a number field (field+1), add time to a date field, or add text to a text field
2550
- *
2551
- * @see PodsAPI::save_pod_item
2552
- *
2553
- * @param string $field Field name
2554
- * @param mixed $value ID(s) to add, int|float to add to number field, string for dates (+1 week), or string for text
2555
- * @param int $id (optional) ID of the pod item to update
2556
- *
2557
- * @return int The item ID
2558
- *
2559
- * @since 2.3
2560
- */
2561
- public function add_to ( $field, $value, $id = null ) {
2562
- $pod =& $this;
2563
-
2564
- $fetch = false;
2565
-
2566
- if ( null === $id ) {
2567
- $fetch = true;
2568
-
2569
- $id = $this->id();
2570
- }
2571
- elseif ( $id != $this->id() )
2572
- $pod = pods( $this->pod, $id );
2573
-
2574
- $this->do_hook( 'add_to', $field, $value, $id );
2575
-
2576
- if ( !isset( $this->fields[ $field ] ) )
2577
- return $id;
2578
-
2579
- // Tableless fields
2580
- if ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::tableless_field_types() ) ) {
2581
- if ( !is_array( $value ) )
2582
- $value = explode( ',', $value );
2583
-
2584
- if ( 'pick' == $this->fields[ $field ][ 'type' ] && in_array( $this->fields[ $field ][ 'pick_object' ], PodsForm::simple_tableless_objects() ) ) {
2585
- $current_value = $pod->raw( $field );
2586
-
2587
- if ( !empty( $current_value ) || ( !is_array( $current_value ) && 0 < strlen( $current_value ) ) )
2588
- $current_value = (array) $current_value;
2589
- else
2590
- $current_value = array();
2591
-
2592
- $value = array_merge( $current_value, $value );
2593
- }
2594
- else {
2595
- $related_ids = $this->api->lookup_related_items( $this->fields[ $field ][ 'id' ], $this->pod_data[ 'id' ], $id, $this->fields[ $field ], $this->pod_data );
2596
-
2597
- foreach ( $value as $k => $v ) {
2598
- if ( !preg_match( '/[^0-9]/', $v ) )
2599
- $value[ $k ] = (int) $v;
2600
- }
2601
-
2602
- $value = array_merge( $related_ids, $value );
2603
- }
2604
-
2605
- if ( !empty( $value ) )
2606
- $value = array_filter( array_unique( $value ) );
2607
- else
2608
- $value = array();
2609
-
2610
- if ( empty( $value ) )
2611
- return $id;
2612
- }
2613
- // Number fields
2614
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::number_field_types() ) ) {
2615
- $current_value = (float) $pod->raw( $field );
2616
-
2617
- $value = ( $current_value + (float) $value );
2618
- }
2619
- // Date fields
2620
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::date_field_types() ) ) {
2621
- $current_value = $pod->raw( $field );
2622
-
2623
- if ( 0 < strlen( $current_value ) )
2624
- $value = strtotime( $value, strtotime( $current_value ) );
2625
- else
2626
- $value = strtotime( $value );
2627
- }
2628
- // Text fields
2629
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::text_field_types() ) ) {
2630
- $current_value = $pod->raw( $field );
2631
-
2632
- if ( 0 < strlen( $current_value ) )
2633
- $value = $current_value . $value;
2634
- }
2635
-
2636
- // @todo handle object fields and taxonomies
2637
-
2638
- $params = array(
2639
- 'pod' => $this->pod,
2640
- 'id' => $id,
2641
- 'data' => array(
2642
- $field => $value
2643
- )
2644
- );
2645
-
2646
- $id = $this->api->save_pod_item( $params );
2647
-
2648
- if ( 0 < $id && $fetch )
2649
- $pod->fetch( $id, false );
2650
-
2651
- return $id;
2652
- }
2653
-
2654
- /**
2655
- * Remove an item from the values of a relationship field, remove a value from a number field (field-1), remove time to a date field
2656
- *
2657
- * @see PodsAPI::save_pod_item
2658
- *
2659
- * @param string $field Field name
2660
- * @param mixed $value ID(s) to add, int|float to add to number field, string for dates (-1 week), or string for text
2661
- * @param int $id (optional) ID of the pod item to update
2662
- *
2663
- * @return int The item ID
2664
- *
2665
- * @since 2.3.3
2666
- */
2667
- public function remove_from( $field, $value = null, $id = null ) {
2668
-
2669
- $pod =& $this;
2670
-
2671
- $fetch = false;
2672
-
2673
- if ( null === $id ) {
2674
- $fetch = true;
2675
-
2676
- $id = $this->id();
2677
- }
2678
- elseif ( $id != $this->id() ) {
2679
- $pod = pods( $this->pod, $id );
2680
- }
2681
-
2682
- $this->do_hook( 'remove_from', $field, $value, $id );
2683
-
2684
- if ( !isset( $this->fields[ $field ] ) ) {
2685
- return $id;
2686
- }
2687
-
2688
- // Tableless fields
2689
- if ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::tableless_field_types() ) ) {
2690
- if ( empty( $value ) ) {
2691
- $value = array();
2692
- }
2693
-
2694
- if ( !empty( $value ) ) {
2695
- if ( !is_array( $value ) ) {
2696
- $value = explode( ',', $value );
2697
- }
2698
-
2699
- if ( 'pick' == $this->fields[ $field ][ 'type' ] && in_array( $this->fields[ $field ][ 'pick_object' ], PodsForm::simple_tableless_objects() ) ) {
2700
- $current_value = $pod->raw( $field );
2701
-
2702
- if ( !empty( $current_value ) ) {
2703
- $current_value = (array) $current_value;
2704
- }
2705
-
2706
- foreach ( $current_value as $k => $v ) {
2707
- if ( in_array( $v, $value ) ) {
2708
- unset( $current_value[ $k ] );
2709
- }
2710
- }
2711
-
2712
- $value = $current_value;
2713
- }
2714
- else {
2715
- $related_ids = $this->api->lookup_related_items( $this->fields[ $field ][ 'id' ], $this->pod_data[ 'id' ], $id, $this->fields[ $field ], $this->pod_data );
2716
-
2717
- foreach ( $value as $k => $v ) {
2718
- if ( !preg_match( '/[^0-9]/', $v ) ) {
2719
- $value[ $k ] = (int) $v;
2720
- }
2721
- // @todo Convert slugs into IDs
2722
- else {
2723
-
2724
- }
2725
- }
2726
-
2727
- foreach ( $related_ids as $k => $v ) {
2728
- if ( in_array( $v, $value ) ) {
2729
- unset( $related_ids[ $k ] );
2730
- }
2731
- }
2732
-
2733
- $value = $related_ids;
2734
- }
2735
-
2736
- if ( !empty( $value ) ) {
2737
- $value = array_filter( array_unique( $value ) );
2738
- }
2739
- else {
2740
- $value = array();
2741
- }
2742
- }
2743
- }
2744
- // Number fields
2745
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::number_field_types() ) ) {
2746
- // Date fields don't support empty for removing
2747
- if ( empty( $value ) ) {
2748
- return $id;
2749
- }
2750
-
2751
- $current_value = (float) $pod->raw( $field );
2752
-
2753
- $value = ( $current_value - (float) $value );
2754
- }
2755
- // Date fields
2756
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::date_field_types() ) ) {
2757
- // Date fields don't support empty for removing
2758
- if ( empty( $value ) ) {
2759
- return $id;
2760
- }
2761
-
2762
- $current_value = $pod->raw( $field );
2763
-
2764
- if ( 0 < strlen( $current_value ) ) {
2765
- $value = strtotime( $value, strtotime( $current_value ) );
2766
- }
2767
- else {
2768
- $value = strtotime( $value );
2769
- }
2770
-
2771
- $value = date_i18n( 'Y-m-d h:i:s', $value );
2772
- }
2773
-
2774
- // @todo handle object fields and taxonomies
2775
-
2776
- $params = array(
2777
- 'pod' => $this->pod,
2778
- 'id' => $id,
2779
- 'data' => array(
2780
- $field => $value
2781
- )
2782
- );
2783
-
2784
- $id = $this->api->save_pod_item( $params );
2785
-
2786
- if ( 0 < $id && $fetch ) {
2787
- $pod->fetch( $id, false );
2788
- }
2789
-
2790
- return $id;
2791
-
2792
- }
2793
-
2794
- /**
2795
- * Save an item by giving an array of field data or set a specific field to a specific value.
2796
- *
2797
- * Though this function has the capacity to add new items, best practice should direct you
2798
- * to use add() for that instead.
2799
- *
2800
- * @see PodsAPI::save_pod_item
2801
- *
2802
- * @param array|string $data Either an associative array of field information or a field name
2803
- * @param mixed $value (optional) Value of the field, if $data is a field name
2804
- * @param int $id (optional) ID of the pod item to update
2805
- * @param array $params (optional) Additional params to send to save_pod_item
2806
- *
2807
- * @return int The item ID
2808
- *
2809
- * @since 2.0
2810
- * @link http://pods.io/docs/save/
2811
- */
2812
- public function save ( $data = null, $value = null, $id = null, $params = null ) {
2813
- if ( null !== $value )
2814
- $data = array( $data => $value );
2815
-
2816
- $fetch = false;
2817
-
2818
- if ( null === $id || ( $this->row && $id == $this->id() ) ) {
2819
- $fetch = true;
2820
-
2821
- if ( null === $id ) {
2822
- $id = $this->id();
2823
- }
2824
- }
2825
-
2826
- $data = (array) $this->do_hook( 'save', $data, $id );
2827
-
2828
- if ( empty( $data ) )
2829
- return $id;
2830
-
2831
- $default = array();
2832
-
2833
- if ( !empty( $params ) && is_array( $params ) )
2834
- $default = $params;
2835
-
2836
- $params = array(
2837
- 'pod' => $this->pod,
2838
- 'id' => $id,
2839
- 'data' => $data,
2840
- 'allow_custom_fields' => true,
2841
- 'clear_slug_cache' => false
2842
- );
2843
-
2844
- if ( !empty( $default ) )
2845
- $params = array_merge( $params, $default );
2846
-
2847
- $id = $this->api->save_pod_item( $params );
2848
-
2849
- if ( 0 < $id && $fetch )
2850
- $this->fetch( $id, false );
2851
-
2852
- if ( !empty( $this->pod_data[ 'field_slug' ] ) ) {
2853
- if ( 0 < $id && $fetch ) {
2854
- $slug = $this->field( $this->pod_data[ 'field_slug' ] );
2855
- }
2856
- else {
2857
- $slug = pods( $this->pod, $id )->field( $this->pod_data[ 'field_slug' ] );
2858
- }
2859
-
2860
- if ( 0 < strlen( $slug ) ) {
2861
- pods_cache_clear( $slug, 'pods_items_' . $this->pod );
2862
- }
2863
- }
2864
-
2865
- return $id;
2866
- }
2867
-
2868
- /**
2869
- * Delete an item
2870
- *
2871
- * @see PodsAPI::delete_pod_item
2872
- *
2873
- * @param int $id ID of the Pod item to delete
2874
- *
2875
- * @return bool Whether the item was successfully deleted
2876
- *
2877
- * @since 2.0
2878
- * @link http://pods.io/docs/delete/
2879
- */
2880
- public function delete ( $id = null ) {
2881
- if ( null === $id )
2882
- $id = $this->id();
2883
-
2884
- $id = (int) $this->do_hook( 'delete', $id );
2885
-
2886
- if ( empty( $id ) )
2887
- return false;
2888
-
2889
- $params = array(
2890
- 'pod' => $this->pod,
2891
- 'id' => $id
2892
- );
2893
-
2894
- return $this->api->delete_pod_item( $params );
2895
- }
2896
-
2897
- /**
2898
- * Reset Pod
2899
- *
2900
- * @see PodsAPI::reset_pod
2901
- *
2902
- * @return bool Whether the Pod was successfully reset
2903
- *
2904
- * @since 2.1.1
2905
- */
2906
- public function reset_pod () {
2907
- $params = array( 'id' => $this->pod_id );
2908
-
2909
- $this->data->id = null;
2910
- $this->data->row = array();
2911
- $this->data->data = array();
2912
-
2913
- $this->data->total = 0;
2914
- $this->data->total_found = 0;
2915
-
2916
- return $this->api->reset_pod( $params );
2917
- }
2918
-
2919
- /**
2920
- * Duplicate an item
2921
- *
2922
- * @see PodsAPI::duplicate_pod_item
2923
- *
2924
- * @param int $id ID of the pod item to duplicate
2925
- *
2926
- * @return int|bool ID of the new pod item
2927
- *
2928
- * @since 2.0
2929
- * @link http://pods.io/docs/duplicate/
2930
- */
2931
- public function duplicate ( $id = null ) {
2932
- if ( null === $id )
2933
- $id = $this->id();
2934
-
2935
- $id = (int) $this->do_hook( 'duplicate', $id );
2936
-
2937
- if ( empty( $id ) )
2938
- return false;
2939
-
2940
- $params = array(
2941
- 'pod' => $this->pod,
2942
- 'id' => $id
2943
- );
2944
-
2945
- return $this->api->duplicate_pod_item( $params );
2946
- }
2947
-
2948
- /**
2949
- * Import data / Save multiple rows of data at once
2950
- *
2951
- * @see PodsAPI::import
2952
- *
2953
- * @param mixed $import_data PHP associative array or CSV input
2954
- * @param bool $numeric_mode Use IDs instead of the name field when matching
2955
- * @param string $format Format of import data, options are php or csv
2956
- *
2957
- * @return array IDs of imported items
2958
- *
2959
- * @since 2.3
2960
- */
2961
- public function import ( $import_data, $numeric_mode = false, $format = null ) {
2962
- return $this->api->import( $import_data, $numeric_mode, $format );
2963
- }
2964
-
2965
- /**
2966
- * Export an item's data
2967
- *
2968
- * @see PodsAPI::export_pod_item
2969
- *
2970
- * @param array $fields (optional) Fields to export
2971
- * @param int $id (optional) ID of the pod item to export
2972
- *
2973
- * @return array|bool Data array of the exported pod item
2974
- *
2975
- * @since 2.0
2976
- * @link http://pods.io/docs/export/
2977
- */
2978
- public function export ( $fields = null, $id = null, $format = null ) {
2979
- $params = array(
2980
- 'pod' => $this->pod,
2981
- 'id' => $id,
2982
- 'fields' => null,
2983
- 'depth' => 2,
2984
- 'flatten' => false
2985
- );
2986
-
2987
- if ( is_array( $fields ) && ( isset( $fields[ 'fields' ] ) || isset( $fields[ 'depth' ] ) ) )
2988
- $params = array_merge( $params, $fields );
2989
- else
2990
- $params[ 'fields' ] = $fields;
2991
-
2992
- if ( isset( $params[ 'fields' ] ) && is_array( $params[ 'fields' ] ) && !in_array( $this->pod_data[ 'field_id' ], $params[ 'fields' ] ) )
2993
- $params[ 'fields' ] = array_merge( array( $this->pod_data[ 'field_id' ] ), $params[ 'fields' ] );
2994
-
2995
- if ( null === $params[ 'id' ] )
2996
- $params[ 'id' ] = $this->id();
2997
-
2998
- $params = (array) $this->do_hook( 'export', $params );
2999
-
3000
- if ( empty( $params[ 'id' ] ) )
3001
- return false;
3002
-
3003
- $data = $this->api->export_pod_item( $params );
3004
-
3005
- if ( !empty( $format ) ) {
3006
- if ( 'json' == $format )
3007
- $data = json_encode( (array) $data );
3008
- // @todo more formats
3009
- }
3010
-
3011
- return $data;
3012
- }
3013
-
3014
- /**
3015
- * Export data from all items
3016
- *
3017
- * @see PodsAPI::export
3018
- *
3019
- * @param array $params An associative array of parameters
3020
- *
3021
- * @return array Data arrays of all exported pod items
3022
- *
3023
- * @since 2.3
3024
- */
3025
- public function export_data ( $params = null ) {
3026
- $defaults = array(
3027
- 'fields' => null,
3028
- 'depth' => 2,
3029
- 'params' => null
3030
- );
3031
-
3032
- if ( empty( $params ) )
3033
- $params = $defaults;
3034
- else
3035
- $params = array_merge( $defaults, (array) $params );
3036
-
3037
- return $this->api->export( $this, $params );
3038
- }
3039
-
3040
- /**
3041
- * Display the pagination controls, types supported by default
3042
- * are simple, paginate and advanced. The base and format parameters
3043
- * are used only for the paginate view.
3044
- *
3045
- * @var array $params Associative array of parameters
3046
- *
3047
- * @return string Pagination HTML
3048
- * @since 2.0
3049
- * @link http://pods.io/docs/pagination/
3050
- */
3051
- public function pagination( $params = null ) {
3052
-
3053
- if ( empty( $params ) ) {
3054
- $params = array();
3055
- }
3056
- elseif ( !is_array( $params ) ) {
3057
- $params = array( 'label' => $params );
3058
- }
3059
-
3060
- $this->page_var = pods_var_raw( 'page_var', $params, $this->page_var );
3061
-
3062
- $url = pods_query_arg( null, null, $this->page_var );
3063
-
3064
- $append = '?';
3065
-
3066
- if ( false !== strpos( $url, '?' ) ) {
3067
- $append = '&';
3068
- }
3069
-
3070
- $defaults = array(
3071
- 'type' => 'advanced',
3072
- 'label' => __( 'Go to page:', 'pods' ),
3073
- 'show_label' => true,
3074
- 'first_text' => __( '&laquo; First', 'pods' ),
3075
- 'prev_text' => __( '&lsaquo; Previous', 'pods' ),
3076
- 'next_text' => __( 'Next &rsaquo;', 'pods' ),
3077
- 'last_text' => __( 'Last &raquo;', 'pods' ),
3078
- 'prev_next' => true,
3079
- 'first_last' => true,
3080
- 'limit' => (int) $this->limit,
3081
- 'offset' => (int) $this->offset,
3082
- 'page' => max( 1, (int) $this->page ),
3083
- 'mid_size' => 2,
3084
- 'end_size' => 1,
3085
- 'total_found' => $this->total_found(),
3086
- 'page_var' => $this->page_var,
3087
- 'base' => "{$url}{$append}%_%",
3088
- 'format' => "{$this->page_var}=%#%",
3089
- 'class' => '',
3090
- 'link_class' => ''
3091
- );
3092
-
3093
- $params = (object) array_merge( $defaults, $params );
3094
-
3095
- $params->total = $this->total_pages( $params->limit, $params->offset, $params->total_found );
3096
-
3097
- if ( $params->limit < 1 || $params->total_found < 1 || 1 == $params->total || $params->total_found <= $params->offset ) {
3098
- return $this->do_hook( 'pagination', $this->do_hook( 'pagination_' . $params->type, '', $params ), $params );
3099
- }
3100
-
3101
- $pagination = $params->type;
3102
-
3103
- if ( !in_array( $params->type, array( 'simple', 'advanced', 'paginate', 'list' ) ) ) {
3104
- $pagination = 'advanced';
3105
- }
3106
-
3107
- ob_start();
3108
-
3109
- pods_view( PODS_DIR . 'ui/front/pagination/' . $pagination . '.php', compact( array_keys( get_defined_vars() ) ) );
3110
-
3111
- $output = ob_get_clean();
3112
-
3113
- return $this->do_hook( 'pagination', $this->do_hook( 'pagination_' . $params->type, $output, $params ), $params );
3114
-
3115
- }
3116
-
3117
- /**
3118
- * Return a filter form for searching a Pod
3119
- *
3120
- * @var array|string $params Comma-separated list of fields or array of parameters
3121
- *
3122
- * @return string Filters HTML
3123
- *
3124
- * @since 2.0
3125
- * @link http://pods.io/docs/filters/
3126
- */
3127
- public function filters ( $params = null ) {
3128
- $defaults = array(
3129
- 'fields' => $params,
3130
- 'label' => '',
3131
- 'action' => '',
3132
- 'search' => ''
3133
- );
3134
-
3135
- if ( is_array( $params ) )
3136
- $params = array_merge( $defaults, $params );
3137
- else
3138
- $params = $defaults;
3139
-
3140
- $pod =& $this;
3141
-
3142
- $params = apply_filters( 'pods_filters_params', $params, $pod );
3143
-
3144
- $fields = $params[ 'fields' ];
3145
-
3146
- if ( null !== $fields && !is_array( $fields ) && 0 < strlen( $fields ) )
3147
- $fields = explode( ',', $fields );
3148
-
3149
- $object_fields = (array) pods_var_raw( 'object_fields', $this->pod_data, array(), null, true );
3150
-
3151
- // Force array
3152
- if ( empty( $fields ) )
3153
- $fields = array();
3154
- else {
3155
- $filter_fields = $fields; // Temporary
3156
-
3157
- $fields = array();
3158
-
3159
- foreach ( $filter_fields as $k => $field ) {
3160
- $name = $k;
3161
-
3162
- $defaults = array(
3163
- 'name' => $name
3164
- );
3165
-
3166
- if ( !is_array( $field ) ) {
3167
- $name = $field;
3168
-
3169
- $field = array(
3170
- 'name' => $name
3171
- );
3172
- }
3173
-
3174
- $field = array_merge( $defaults, $field );
3175
-
3176
- $field[ 'name' ] = trim( $field[ 'name' ] );
3177
-
3178
- if ( pods_var_raw( 'hidden', $field, false, null, true ) )
3179
- $field[ 'type' ] = 'hidden';
3180
-
3181
- if ( isset( $object_fields[ $field[ 'name' ] ] ) )
3182
- $fields[ $field[ 'name' ] ] = array_merge( $object_fields[ $field[ 'name' ] ], $field );
3183
- elseif ( isset( $this->fields[ $field[ 'name' ] ] ) )
3184
- $fields[ $field[ 'name' ] ] = array_merge( $this->fields[ $field[ 'name' ] ], $field );
3185
- }
3186
-
3187
- unset( $filter_fields ); // Cleanup
3188
- }
3189
-
3190
- $this->filters = array_keys( $fields );
3191
-
3192
- $label = $params[ 'label' ];
3193
-
3194
- if ( strlen( $label ) < 1 )
3195
- $label = __( 'Search', 'pods' );
3196
-
3197
- $action = $params[ 'action' ];
3198
-
3199
- $search = trim( $params[ 'search' ] );
3200
-
3201
- if ( strlen( $search ) < 1 )
3202
- $search = pods_var_raw( $pod->search_var, 'get', '' );
3203
-
3204
- ob_start();
3205
-
3206
- pods_view( PODS_DIR . 'ui/front/filters.php', compact( array_keys( get_defined_vars() ) ) );
3207
-
3208
- $output = ob_get_clean();
3209
-
3210
- /**
3211
- * Filter the HTML output of filters()
3212
- *
3213
- * @since unknown
3214
- *
3215
- * @param string $output
3216
- * @param array $params Params array passed to filters().
3217
- * @param object|Pods $this Current Pods object.
3218
- */
3219
- return apply_filters( 'pods_pods_filters', $output, $params, $this );
3220
- }
3221
-
3222
- /**
3223
- * Run a helper within a Pod Page or WP Template
3224
- *
3225
- * @see Pods_Helpers::helper
3226
- *
3227
- * @param string $helper Helper name
3228
- * @param string $value Value to run the helper on
3229
- * @param string $name Field name
3230
- *
3231
- * @return mixed Anything returned by the helper
3232
- * @since 2.0
3233
- */
3234
- public function helper ( $helper, $value = null, $name = null ) {
3235
- $params = array(
3236
- 'helper' => $helper,
3237
- 'value' => $value,
3238
- 'name' => $name,
3239
- 'deprecated' => false
3240
- );
3241
-
3242
- if ( class_exists( 'Pods_Templates' ) )
3243
- $params[ 'deprecated' ] = Pods_Templates::$deprecated;
3244
-
3245
- if ( is_array( $helper ) )
3246
- $params = array_merge( $params, $helper );
3247
-
3248
- if ( class_exists( 'Pods_Helpers' ) )
3249
- $value = Pods_Helpers::helper( $params, $this );
3250
- elseif ( function_exists( $params[ 'helper' ] ) )
3251
- $value = call_user_func( $params[ 'helper' ], $value );
3252
- else
3253
- $value = apply_filters( $params[ 'helper' ], $value );
3254
-
3255
- return $value;
3256
- }
3257
-
3258
- /**
3259
- * Display the page template
3260
- *
3261
- * @see Pods_Templates::template
3262
- *
3263
- * @param string $template The template name
3264
- * @param string $code Custom template code to use instead
3265
- * @param bool $deprecated Whether to use deprecated functionality based on old function usage
3266
- *
3267
- * @return mixed Template output
3268
- *
3269
- * @since 2.0
3270
- * @link http://pods.io/docs/template/
3271
- */
3272
- public function template ( $template_name, $code = null, $deprecated = false ) {
3273
- $out = null;
3274
-
3275
- $obj =& $this;
3276
-
3277
- if ( !empty( $code ) ) {
3278
- $code = str_replace( '$this->', '$obj->', $code ); // backwards compatibility
3279
-
3280
- $code = apply_filters( 'pods_templates_pre_template', $code, $template_name, $this );
3281
- $code = apply_filters( "pods_templates_pre_template_{$template_name}", $code, $template_name, $this );
3282
-
3283
- ob_start();
3284
-
3285
- if ( !empty( $code ) ) {
3286
- // Only detail templates need $this->id
3287
- if ( empty( $this->id ) ) {
3288
- while ( $this->fetch() ) {
3289
- echo $this->do_magic_tags( $code );
3290
- }
3291
- }
3292
- else
3293
- echo $this->do_magic_tags( $code );
3294
- }
3295
-
3296
- $out = ob_get_clean();
3297
-
3298
- $out = apply_filters( 'pods_templates_post_template', $out, $code, $template_name, $this );
3299
- $out = apply_filters( "pods_templates_post_template_{$template_name}", $out, $code, $template_name, $this );
3300
- }
3301
- elseif ( class_exists( 'Pods_Templates' ) )
3302
- $out = Pods_Templates::template( $template_name, $code, $this, $deprecated );
3303
- elseif ( $template_name == trim( preg_replace( '/[^a-zA-Z0-9_\-\/]/', '', $template_name ), ' /-' ) ) {
3304
- ob_start();
3305
-
3306
- $default_templates = array(
3307
- 'pods/' . $template_name,
3308
- 'pods-' . $template_name,
3309
- $template_name
3310
- );
3311
-
3312
- $default_templates = apply_filters( 'pods_template_default_templates', $default_templates );
3313
-
3314
- // Only detail templates need $this->id
3315
- if ( empty( $this->id ) ) {
3316
- while ( $this->fetch() ) {
3317
- pods_template_part( $default_templates, compact( array_keys( get_defined_vars() ) ) );
3318
- }
3319
- }
3320
- else
3321
- pods_template_part( $default_templates, compact( array_keys( get_defined_vars() ) ) );
3322
-
3323
- $out = ob_get_clean();
3324
-
3325
- $out = apply_filters( 'pods_templates_post_template', $out, $code, $template_name, $this );
3326
- $out = apply_filters( "pods_templates_post_template_{$template_name}", $out, $code, $template_name, $this );
3327
- }
3328
-
3329
- return $out;
3330
- }
3331
-
3332
- /**
3333
- * Embed a form to add / edit a pod item from within your theme. Provide an array of $fields to include
3334
- * and override options where needed. For WP object based Pods, you can pass through the WP object
3335
- * field names too, such as "post_title" or "post_content" for example.
3336
- *
3337
- * @param array $params (optional) Fields to show on the form, defaults to all fields
3338
- * @param string $label (optional) Save button label, defaults to "Save Changes"
3339
- * @param string $thank_you (optional) Thank you URL to send to upon success
3340
- *
3341
- * @return bool|mixed
3342
- * @since 2.0
3343
- * @link http://pods.io/docs/form/
3344
- */
3345
- public function form ( $params = null, $label = null, $thank_you = null ) {
3346
- $defaults = array(
3347
- 'fields' => $params,
3348
- 'label' => $label,
3349
- 'thank_you' => $thank_you,
3350
- 'fields_only' => false
3351
- );
3352
-
3353
- if ( is_array( $params ) )
3354
- $params = array_merge( $defaults, $params );
3355
- else
3356
- $params = $defaults;
3357
-
3358
- $pod =& $this;
3359
-
3360
- $params = $this->do_hook( 'form_params', $params );
3361
-
3362
- $fields = $params[ 'fields' ];
3363
-
3364
- if ( null !== $fields && !is_array( $fields ) && 0 < strlen( $fields ) )
3365
- $fields = explode( ',', $fields );
3366
-
3367
- $object_fields = (array) pods_var_raw( 'object_fields', $this->pod_data, array(), null, true );
3368
-
3369
- if ( empty( $fields ) ) {
3370
- // Add core object fields if $fields is empty
3371
- $fields = array_merge( $object_fields, $this->fields );
3372
- }
3373
-
3374
- $form_fields = $fields; // Temporary
3375
-
3376
- $fields = array();
3377
-
3378
- foreach ( $form_fields as $k => $field ) {
3379
- $name = $k;
3380
-
3381
- $defaults = array(
3382
- 'name' => $name
3383
- );
3384
-
3385
- if ( !is_array( $field ) ) {
3386
- $name = $field;
3387
-
3388
- $field = array(
3389
- 'name' => $name
3390
- );
3391
- }
3392
-
3393
- $field = array_merge( $defaults, $field );
3394
-
3395
- $field[ 'name' ] = trim( $field[ 'name' ] );
3396
-
3397
- $default_value = pods_var_raw( 'default', $field );
3398
- $value = pods_var_raw( 'value', $field );
3399
-
3400
- if ( empty( $field[ 'name' ] ) )
3401
- $field[ 'name' ] = trim( $name );
3402
-
3403
- if ( isset( $object_fields[ $field[ 'name' ] ] ) ) {
3404
- $field = array_merge( $object_fields[ $field[ 'name' ] ], $field );
3405
- }
3406
- elseif ( isset( $this->fields[ $field[ 'name' ] ] ) ) {
3407
- $field = array_merge( $this->fields[ $field[ 'name' ] ], $field );
3408
- }
3409
-
3410
- if ( pods_var_raw( 'hidden', $field, false, null, true ) )
3411
- $field[ 'type' ] = 'hidden';
3412
-
3413
- $fields[ $field[ 'name' ] ] = $field;
3414
-
3415
- if ( empty( $this->id ) && null !== $default_value ) {
3416
- $this->row_override[ $field[ 'name' ] ] = $default_value;
3417
- }
3418
- elseif ( !empty( $this->id ) && null !== $value ) {
3419
- $this->row[ $field[ 'name' ] ] = $value;
3420
- }
3421
- }
3422
-
3423
- unset( $form_fields ); // Cleanup
3424
-
3425
- $fields = $this->do_hook( 'form_fields', $fields, $params );
3426
-
3427
- $label = $params[ 'label' ];
3428
-
3429
- if ( empty( $label ) )
3430
- $label = __( 'Save Changes', 'pods' );
3431
-
3432
- $thank_you = $params[ 'thank_you' ];
3433
- $fields_only = $params[ 'fields_only' ];
3434
-
3435
- PodsForm::$form_counter++;
3436
-
3437
- ob_start();
3438
-
3439
- if ( empty( $thank_you ) ) {
3440
- $success = 'success';
3441
-
3442
- if ( 1 < PodsForm::$form_counter )
3443
- $success .= PodsForm::$form_counter;
3444
-
3445
- $thank_you = pods_query_arg( array( 'success*' => null, $success => 1 ) );
3446
-
3447
- if ( 1 == pods_v( $success, 'get', 0 ) ) {
3448
- $message = __( 'Form submitted successfully', 'pods' );
3449
- /**
3450
- * Change the text of the message that appears on succesful form submission.
3451
- *
3452
- * @param string $message
3453
- *
3454
- * @returns string the message
3455
- *
3456
- * @since 3.0.0
3457
- */
3458
- $message = apply_filters( 'pods_pod_form_success_message', $message );
3459
-
3460
- echo '<div id="message" class="pods-form-front-success">' . $message . '</div>';
3461
- }
3462
- }
3463
-
3464
- pods_view( PODS_DIR . 'ui/front/form.php', compact( array_keys( get_defined_vars() ) ) );
3465
-
3466
- $output = ob_get_clean();
3467
-
3468
- if ( empty( $this->id ) )
3469
- $this->row_override = array();
3470
-
3471
- return $this->do_hook( 'form', $output, $fields, $label, $thank_you, $this, $this->id() );
3472
- }
3473
-
3474
- /**
3475
- * @param array $fields (optional) Fields to show in the view, defaults to all fields
3476
- *
3477
- * @return mixed
3478
- * @since 2.3.10
3479
- */
3480
- public function view( $fields = null ) {
3481
-
3482
- $pod =& $this;
3483
-
3484
- // Convert comma separated list of fields to an array
3485
- if ( null !== $fields && !is_array( $fields ) && 0 < strlen( $fields ) ) {
3486
- $fields = explode( ',', $fields );
3487
- }
3488
-
3489
- $object_fields = (array) pods_v( 'object_fields', $this->pod_data, array(), true );
3490
-
3491
- if ( empty( $fields ) ) {
3492
- // Add core object fields if $fields is empty
3493
- $fields = array_merge( $object_fields, $this->fields );
3494
- }
3495
-
3496
- $view_fields = $fields; // Temporary
3497
-
3498
- $fields = array();
3499
-
3500
- foreach ( $view_fields as $name => $field ) {
3501
-
3502
- $defaults = array(
3503
- 'name' => $name
3504
- );
3505
-
3506
- if ( !is_array( $field ) ) {
3507
- $name = $field;
3508
-
3509
- $field = array(
3510
- 'name' => $name
3511
- );
3512
- }
3513
-
3514
- $field = array_merge( $defaults, $field );
3515
-
3516
- $field[ 'name' ] = trim( $field[ 'name' ] );
3517
-
3518
- if ( empty( $field[ 'name' ] ) ) {
3519
- $field[ 'name' ] = trim( $name );
3520
- }
3521
-
3522
- if ( isset( $object_fields[ $field[ 'name' ] ] ) )
3523
- $field = array_merge( $field, $object_fields[ $field[ 'name' ] ] );
3524
- elseif ( isset( $this->fields[ $field[ 'name' ] ] ) )
3525
- $field = array_merge( $this->fields[ $field[ 'name' ] ], $field );
3526
-
3527
- if ( pods_v( 'hidden', $field, false, null, true ) || 'hidden' == $field[ 'type' ] ) {
3528
- continue;
3529
- }
3530
- elseif ( !PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field[ 'options' ], $fields, $pod, $pod->id() ) ) {
3531
- continue;
3532
- }
3533
-
3534
- $fields[ $field[ 'name' ] ] = $field;
3535
- }
3536
-
3537
- unset( $view_fields ); // Cleanup
3538
-
3539
- $output = pods_view( PODS_DIR . 'ui/front/view.php', compact( array_keys( get_defined_vars() ) ), false, 'cache', true );
3540
-
3541
- return $this->do_hook( 'view', $output, $fields, $this->id() );
3542
-
3543
- }
3544
-
3545
- /**
3546
- * Replace magic tags with their values
3547
- *
3548
- * @param string $code The content to evaluate
3549
- * @param object $obj The Pods object
3550
- *
3551
- * @return string Code with Magic Tags evaluated
3552
- *
3553
- * @since 2.0
3554
- */
3555
- public function do_magic_tags ( $code ) {
3556
- return preg_replace_callback( '/({@(.*?)})/m', array( $this, 'process_magic_tags' ), $code );
3557
- }
3558
-
3559
- /**
3560
- * Replace magic tags with their values
3561
- *
3562
- * @param string $tag The magic tag to process
3563
- * @param object $obj The Pods object
3564
- *
3565
- * @return string Code with Magic Tags evaluated
3566
- *
3567
- * @since 2.0.2
3568
- */
3569
- private function process_magic_tags ( $tag ) {
3570
-
3571
- if ( is_array( $tag ) ) {
3572
- if ( !isset( $tag[ 2 ] ) && strlen( trim( $tag[ 2 ] ) ) < 1 )
3573
- return '';
3574
-
3575
- $tag = $tag[ 2 ];
3576
- }
3577
-
3578
- $tag = trim( $tag, ' {@}' );
3579
- $tag = explode( ',', $tag );
3580
-
3581
- if ( empty( $tag ) || !isset( $tag[ 0 ] ) || strlen( trim( $tag[ 0 ] ) ) < 1 )
3582
- return '';
3583
-
3584
- foreach ( $tag as $k => $v ) {
3585
- $tag[ $k ] = trim( $v );
3586
- }
3587
-
3588
- $field_name = $tag[ 0 ];
3589
-
3590
- $helper_name = $before = $after = '';
3591
-
3592
- if ( isset( $tag[ 1 ] ) && !empty( $tag[ 1 ] ) ) {
3593
- $value = $this->field( $field_name );
3594
-
3595
- $helper_name = $tag[ 1 ];
3596
-
3597
- $value = $this->helper( $helper_name, $value, $field_name );
3598
- }
3599
- else
3600
- $value = $this->display( $field_name );
3601
-
3602
- if ( isset( $tag[ 2 ] ) && !empty( $tag[ 2 ] ) )
3603
- $before = $tag[ 2 ];
3604
-
3605
- if ( isset( $tag[ 3 ] ) && !empty( $tag[ 3 ] ) )
3606
- $after = $tag[ 3 ];
3607
-
3608
- $value = apply_filters( 'pods_do_magic_tags', $value, $field_name, $helper_name, $before, $after );
3609
-
3610
- if ( is_array( $value ) )
3611
- $value = pods_serial_comma( $value, array( 'field' => $field_name, 'fields' => $this->fields ) );
3612
-
3613
- if ( null !== $value && false !== $value )
3614
- return $before . $value . $after;
3615
-
3616
- return '';
3617
- }
3618
-
3619
- /**
3620
- *
3621
- * Generate UI for Data Management
3622
- *
3623
- * @param mixed $options Array or String containing Pod or Options to be used
3624
- * @param bool $amend Whether to amend the default UI options or replace entirely
3625
- *
3626
- * @return PodsUI|void UI object or void if custom UI used
3627
- *
3628
- * @since 2.3.10
3629
- */
3630
- public function ui ( $options = null, $amend = false ) {
3631
- $num = '';
3632
-
3633
- if ( empty( $options ) )
3634
- $options = array();
3635
- else {
3636
- $num = pods_var( 'num', $options, '' );
3637
-
3638
- if ( empty( $num ) ) {
3639
- $num = '';
3640
- }
3641
- }
3642
-
3643
- if ( $this->id() != pods_var( 'id' . $num, 'get', null, null, true ) )
3644
- $this->fetch( pods_var( 'id' . $num, 'get', null, null, true ) );
3645
-
3646
- if ( !empty( $options ) && !$amend ) {
3647
- $this->ui = $options;
3648
-
3649
- return pods_ui( $this );
3650
- }
3651
- elseif ( !empty( $options ) || 'custom' != pods_var( 'ui_style', $this->pod_data[ 'options' ], 'post_type', null, true ) ) {
3652
- $actions_enabled = pods_var_raw( 'ui_actions_enabled', $this->pod_data[ 'options' ] );
3653
-
3654
- if ( !empty( $actions_enabled ) )
3655
- $actions_enabled = (array) $actions_enabled;
3656
- else
3657
- $actions_enabled = array();
3658
-
3659
- $available_actions = array(
3660
- 'add',
3661
- 'edit',
3662
- 'duplicate',
3663
- 'delete',
3664
- 'reorder',
3665
- 'export'
3666
- );
3667
-
3668
- if ( !empty( $actions_enabled ) ) {
3669
- $actions_disabled = array(
3670
- 'view' => 'view'
3671
- );
3672
-
3673
- foreach ( $available_actions as $action ) {
3674
- if ( !in_array( $action, $actions_enabled ) )
3675
- $actions_disabled[ $action ] = $action;
3676
- }
3677
- }
3678
- else {
3679
- $actions_disabled = array(
3680
- 'duplicate' => 'duplicate',
3681
- 'view' => 'view',
3682
- 'export' => 'export'
3683
- );
3684
-
3685
- if ( 1 == pods_var( 'ui_export', $this->pod_data[ 'options' ], 0 ) )
3686
- unset( $actions_disabled[ 'export' ] );
3687
- }
3688
-
3689
- if ( empty( $options ) ) {
3690
- $author_restrict = false;
3691
-
3692
- if ( isset( $this->fields[ 'author' ] ) && 'pick' == $this->fields[ 'author' ][ 'type' ] && 'user' == $this->fields[ 'author' ][ 'pick_object' ] )
3693
- $author_restrict = 'author.ID';
3694
-
3695
- if ( !pods_is_admin( array( 'pods', 'pods_content' ) ) ) {
3696
- if ( !current_user_can( 'pods_add_' . $this->pod ) ) {
3697
- $actions_disabled[ 'add' ] = 'add';
3698
-
3699
- if ( 'add' == pods_var( 'action' . $num, 'get' ) )
3700
- $_GET[ 'action' . $num ] = 'manage';
3701
- }
3702
-
3703
- if ( !$author_restrict && !current_user_can( 'pods_edit_' . $this->pod ) && !current_user_can( 'pods_edit_others_' . $this->pod ) )
3704
- $actions_disabled[ 'edit' ] = 'edit';
3705
-
3706
- if ( !$author_restrict && !current_user_can( 'pods_delete_' . $this->pod ) && !current_user_can( 'pods_delete_others_' . $this->pod ) )
3707
- $actions_disabled[ 'delete' ] = 'delete';
3708
-
3709
- if ( !current_user_can( 'pods_reorder_' . $this->pod ) )
3710
- $actions_disabled[ 'reorder' ] = 'reorder';
3711
-
3712
- if ( !current_user_can( 'pods_export_' . $this->pod ) )
3713
- $actions_disabled[ 'export' ] = 'export';
3714
- }
3715
- }
3716
-
3717
- $_GET[ 'action' . $num ] = pods_var( 'action' . $num, 'get', pods_var( 'action', $options, 'manage' ) );
3718
-
3719
- $index = $this->pod_data[ 'field_id' ];
3720
- $label = __( 'ID', 'pods' );
3721
-
3722
- if ( isset( $this->pod_data[ 'fields' ][ $this->pod_data[ 'field_index' ] ] ) ) {
3723
- $index = $this->pod_data[ 'field_index' ];
3724
- $label = $this->pod_data[ 'fields' ][ $this->pod_data[ 'field_index' ] ];
3725
- }
3726
-
3727
- $manage = array(
3728
- $index => $label
3729
- );
3730
-
3731
- if ( isset( $this->pod_data[ 'fields' ][ 'modified' ] ) )
3732
- $manage[ 'modified' ] = $this->pod_data[ 'fields' ][ 'modified' ][ 'label' ];
3733
-
3734
- $manage_fields = pods_var_raw( 'ui_fields_manage', $this->pod_data[ 'options' ] );
3735
-
3736
- if ( !empty( $manage_fields ) ) {
3737
- $manage_new = array();
3738
-
3739
- foreach ( $manage_fields as $manage_field ) {
3740
- if ( isset( $this->pod_data[ 'fields' ][ $manage_field ] ) )
3741
- $manage_new[ $manage_field ] = $this->pod_data[ 'fields' ][ $manage_field ];
3742
- elseif ( isset( $this->pod_data[ 'object_fields' ][ $manage_field ] ) )
3743
- $manage_new[ $manage_field ] = $this->pod_data[ 'object_fields' ][ $manage_field ];
3744
- elseif ( $manage_field == $this->pod_data[ 'field_id' ] ) {
3745
- $field = array(
3746
- 'name' => $manage_field,
3747
- 'label' => 'ID',
3748
- 'type' => 'number',
3749
- 'width' => '8%'
3750
- );
3751
-
3752
- $manage_new[ $manage_field ] = PodsForm::field_setup( $field, null, $field[ 'type' ] );
3753
- }
3754
- }
3755
-
3756
- if ( !empty( $manage_new ) )
3757
- $manage = $manage_new;
3758
- }
3759
-
3760
- $manage = apply_filters( 'pods_admin_ui_fields_' . $this->pod, apply_filters( 'pods_admin_ui_fields', $manage, $this->pod, $this ), $this->pod, $this );
3761
-
3762
- $icon = pods_var_raw( 'ui_icon', $this->pod_data[ 'options' ] );
3763
-
3764
- if ( !empty( $icon ) )
3765
- $icon = pods_image_url( $icon, '32x32' );
3766
-
3767
- $filters = pods_var_raw( 'ui_filters', $this->pod_data[ 'options' ] );
3768
-
3769
- if ( !empty( $filters ) ) {
3770
- $filters_new = array();
3771
-
3772
- foreach ( $filters as $filter_field ) {
3773
- if ( isset( $this->pod_data[ 'fields' ][ $filter_field ] ) )
3774
- $filters_new[ $filter_field ] = $this->pod_data[ 'fields' ][ $filter_field ];
3775
- elseif ( isset( $this->pod_data[ 'object_fields' ][ $filter_field ] ) )
3776
- $filters_new[ $filter_field ] = $this->pod_data[ 'object_fields' ][ $filter_field ];
3777
- }
3778
-
3779
- $filters = $filters_new;
3780
- }
3781
-
3782
- $ui = array(
3783
- 'fields' => array(
3784
- 'manage' => $manage,
3785
- 'add' => $this->pod_data[ 'fields' ],
3786
- 'edit' => $this->pod_data[ 'fields' ],
3787
- 'duplicate' => $this->pod_data[ 'fields' ]
3788
- ),
3789
- 'icon' => $icon,
3790
- 'actions_disabled' => $actions_disabled
3791
- );
3792
-
3793
- if ( !empty( $filters ) ) {
3794
- $ui[ 'fields' ][ 'search' ] = $filters;
3795
- $ui[ 'filters' ] = array_keys( $filters );
3796
- $ui[ 'filters_enhanced' ] = true;
3797
- }
3798
-
3799
- $reorder_field = pods_var_raw( 'ui_reorder_field', $this->pod_data[ 'options' ] );
3800
-
3801
- if ( in_array( 'reorder', $actions_enabled ) && !in_array( 'reorder', $actions_disabled ) && !empty( $reorder_field ) && ( ( !empty( $this->pod_data[ 'object_fields' ] ) && isset( $this->pod_data[ 'object_fields' ][ $reorder_field ] ) ) || isset( $this->pod_data[ 'fields' ][ $reorder_field ] ) ) ) {
3802
- $ui[ 'reorder' ] = array( 'on' => $reorder_field );
3803
- $ui[ 'orderby' ] = $reorder_field;
3804
- $ui[ 'orderby_dir' ] = 'ASC';
3805
- }
3806
-
3807
- if ( !empty( $author_restrict ) )
3808
- $ui[ 'restrict' ] = array( 'author_restrict' => $author_restrict );
3809
-
3810
- if ( !in_array( 'delete', $ui[ 'actions_disabled' ] ) ) {
3811
- $ui[ 'actions_bulk' ] = array(
3812
- 'delete' => array(
3813
- 'label' => __( 'Delete', 'pods' )
3814
- // callback not needed, Pods has this built-in for delete
3815
- )
3816
- );
3817
- }
3818
-
3819
- $detail_url = pods_var( 'detail_url', $this->pod_data[ 'options' ] );
3820
-
3821
- if ( 0 < strlen( $detail_url ) ) {
3822
- $ui[ 'actions_custom' ] = array(
3823
- 'view_url' => array(
3824
- 'label' => 'View',
3825
- 'link' => get_site_url() . '/' . $detail_url
3826
- )
3827
- );
3828
- }
3829
-
3830
- // @todo Customize the Add New / Manage links to point to their correct menu items
3831
-
3832
- $ui = apply_filters( 'pods_admin_ui_' . $this->pod, apply_filters( 'pods_admin_ui', $ui, $this->pod, $this ), $this->pod, $this );
3833
-
3834
- // Override UI options
3835
- foreach ( $options as $option => $value ) {
3836
- $ui[ $option ] = $value;
3837
- }
3838
-
3839
- $this->ui = $ui;
3840
-
3841
- return pods_ui( $this );
3842
- }
3843
-
3844
- do_action( 'pods_admin_ui_custom', $this );
3845
- do_action( 'pods_admin_ui_custom_' . $this->pod, $this );
3846
- }
3847
-
3848
- /**
3849
- * Handle filters / actions for the class
3850
- *
3851
- * @see pods_do_hook
3852
- *
3853
- * @return mixed Value filtered
3854
- *
3855
- * @since 2.0
3856
- */
3857
- private function do_hook () {
3858
- $args = func_get_args();
3859
-
3860
- if ( empty( $args ) )
3861
- return false;
3862
-
3863
- $name = array_shift( $args );
3864
-
3865
- return pods_do_hook( 'pods', $name, $args, $this );
3866
- }
3867
-
3868
- /**
3869
- * Handle variables that have been deprecated and PodsData vars
3870
- *
3871
- * @var $name
3872
- *
3873
- * @return mixed
3874
- *
3875
- * @since 2.0
3876
- */
3877
- public function __get ( $name ) {
3878
- $name = (string) $name;
3879
-
3880
- // PodsData vars
3881
- if ( 0 === strpos( $name, 'field_' ) && isset( $this->data->{$name} ) ) {
3882
- return $this->data->{$name};
3883
- }
3884
-
3885
- if ( !isset( $this->deprecated ) ) {
3886
- require_once( PODS_DIR . 'deprecated/classes/Pods.php' );
3887
- $this->deprecated = new Pods_Deprecated( $this );
3888
- }
3889
-
3890
- $var = null;
3891
-
3892
- if ( isset( $this->deprecated->{$name} ) ) {
3893
- if ( ! class_exists( 'Pod' ) || Pod::$deprecated_notice ) {
3894
- pods_deprecated( "Pods->{$name}", '2.0' );
3895
- }
3896
-
3897
- $var = $this->deprecated->{$name};
3898
- }
3899
- elseif ( ! class_exists( 'Pod' ) || Pod::$deprecated_notice ) {
3900
- pods_deprecated( "Pods->{$name}", '2.0' );
3901
- }
3902
-
3903
- return $var;
3904
- }
3905
-
3906
- /**
3907
- * Handle methods that have been deprecated and any aliasing
3908
- *
3909
- * @var $name
3910
- * @var $args
3911
- *
3912
- * @return mixed
3913
- *
3914
- * @since 2.0
3915
- */
3916
- public function __call ( $name, $args ) {
3917
- $name = (string) $name;
3918
-
3919
- // select > find alias
3920
- if ( 'select' == $name ) {
3921
- return call_user_func_array( array( $this, 'find' ), $args );
3922
- }
3923
-
3924
- if ( !isset( $this->deprecated ) ) {
3925
- require_once( PODS_DIR . 'deprecated/classes/Pods.php' );
3926
- $this->deprecated = new Pods_Deprecated( $this );
3927
- }
3928
-
3929
- if ( method_exists( $this->deprecated, $name ) ) {
3930
- return call_user_func_array( array( $this->deprecated, $name ), $args );
3931
- }
3932
- elseif ( ! class_exists( 'Pod' ) || Pod::$deprecated_notice ) {
3933
- pods_deprecated( "Pods::{$name}", '2.0' );
3934
- }
3935
- }
3936
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
classes/PodsAPI.php DELETED
@@ -1,8043 +0,0 @@
1
- <?php
2
- /**
3
- * @package Pods
4
- */
5
- class PodsAPI {
6
-
7
- /**
8
- * @var PodsAPI
9
- */
10
- static $instance = null;
11
-
12
- /**
13
- * @var array PodsAPI
14
- */
15
- static $instances = array();
16
-
17
-
18
- /**
19
- * @var bool
20
- */
21
- public $display_errors = false;
22
-
23
- /**
24
- * @var array|bool|mixed|null|void
25
- */
26
- public $pod_data;
27
-
28
- /**
29
- * @var
30
- */
31
- public $pod;
32
-
33
- /**
34
- * @var
35
- */
36
- public $pod_id;
37
-
38
- /**
39
- * @var
40
- */
41
- public $fields;
42
-
43
- /**
44
- * @var
45
- * @deprecated 2.0
46
- */
47
- public $format = null;
48
-
49
- /**
50
- * @var
51
- */
52
- private $deprecated;
53
-
54
- /**
55
- * @var array
56
- * @since 2.5
57
- */
58
- private $fields_cache = array();
59
-
60
- /**
61
- * @var array
62
- * @since 2.5
63
- *
64
- */
65
- private static $table_info_cache = array();
66
-
67
- /**
68
- * @var array
69
- * @since 2.5
70
- *
71
- */
72
- private static $related_item_cache = array();
73
-
74
- /**
75
- * Singleton-ish handling for a basic pods_api() request
76
- *
77
- * @param string $pod (optional) The pod name
78
- * @param string $format (deprecated) Format for import/export, "php" or "csv"
79
- *
80
- * @return \PodsAPI
81
- *
82
- * @since 2.3.5
83
- */
84
- public static function init ( $pod = null, $format = null ) {
85
- if ( null !== $pod || null !== $format ) {
86
- if ( ! isset( self::$instances[ $pod ] ) ) {
87
- // Cache API singleton per Pod
88
- self::$instances[ $pod ] = new PodsAPI( $pod, $format );
89
- }
90
- return self::$instances[ $pod ];
91
- }
92
- elseif ( !is_object( self::$instance ) ) {
93
- self::$instance = new PodsAPI();
94
- }
95
-
96
- return self::$instance;
97
- }
98
-
99
-
100
- /**
101
- * Store and retrieve data programatically
102
- *
103
- * @param string $pod (optional) The pod name
104
- * @param string $format (deprecated) Format for import/export, "php" or "csv"
105
- *
106
- * @return \PodsAPI
107
- *
108
- * @license http://www.gnu.org/licenses/gpl-2.0.html
109
- * @since 1.7.1
110
- */
111
- public function __construct ( $pod = null, $format = null ) {
112
- if ( null !== $pod && 0 < strlen( (string) $pod ) ) {
113
- if ( null !== $format ) {
114
- $this->format = $format;
115
-
116
- pods_deprecated( 'pods_api( $pod, $format )', '2.0', 'pods_api( $pod )' );
117
- }
118
-
119
- $pod = pods_clean_name( $pod );
120
-
121
- $pod = $this->load_pod( array( 'name' => $pod, 'table_info' => true ), false );
122
-
123
- if ( !empty( $pod ) ) {
124
- $this->pod_data = $pod;
125
- $this->pod = $pod[ 'name' ];
126
- $this->pod_id = $pod[ 'id' ];
127
- $this->fields = $pod[ 'fields' ];
128
- }
129
- }
130
- }
131
-
132
- /**
133
- * Save a WP object and its meta
134
- *
135
- * @param string $object_type Object type: post|user|comment|setting
136
- * @param array $data All post data to be saved
137
- * @param array $meta (optional) Associative array of meta keys and values
138
- * @param bool $strict (optional) Decides whether the previous saved meta should be deleted or not
139
- * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
140
- * @param array $fields (optional) The array of fields and their options, for further processing with
141
- *
142
- * @return bool|mixed
143
- *
144
- * @since 2.0
145
- */
146
- public function save_wp_object ( $object_type, $data, $meta = array(), $strict = false, $sanitized = false, $fields = array() ) {
147
- if ( in_array( $object_type, array( 'post_type', 'media' ) ) )
148
- $object_type = 'post';
149
-
150
- if ( $sanitized ) {
151
- $data = pods_unsanitize( $data );
152
- $meta = pods_unsanitize( $meta );
153
- }
154
-
155
- if ( in_array( $object_type, array( 'post', 'user', 'comment' ) ) )
156
- return call_user_func( array( $this, 'save_' . $object_type ), $data, $meta, $strict, false, $fields );
157
- elseif ( 'settings' == $object_type ) {
158
- // Nothing to save
159
- if ( empty( $meta ) )
160
- return true;
161
-
162
- return $this->save_setting( pods_var( 'option_id', $data ), $meta, false );
163
- }
164
-
165
- return false;
166
- }
167
-
168
- /**
169
- * Delete a WP object
170
- *
171
- * @param string $object_type Object type: post|user|comment
172
- * @param int $id Object ID
173
- * @param bool $force_delete (optional) Force deletion instead of trashing (post types only)
174
- *
175
- * @return bool|mixed
176
- *
177
- * @since 2.0
178
- */
179
- public function delete_wp_object ( $object_type, $id, $force_delete = true ) {
180
- if ( in_array( $object_type, array( 'post_type', 'media' ) ) )
181
- $object_type = 'post';
182
-
183
- if ( empty( $id ) )
184
- return false;
185
-
186
- if ( in_array( $object_type, array( 'post' ) ) )
187
- return wp_delete_post( $id, $force_delete );
188
-
189
- if ( function_exists( 'wp_delete_' . $object_type ) )
190
- return call_user_func( 'wp_delete_' . $object_type, $id );
191
-
192
- return false;
193
- }
194
-
195
- /**
196
- * Save a post and it's meta
197
- *
198
- * @param array $post_data All post data to be saved (using wp_insert_post / wp_update_post)
199
- * @param array $post_meta (optional) All meta to be saved (set value to null to delete)
200
- * @param bool $strict (optional) Whether to delete previously saved meta not in $post_meta
201
- * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
202
- * @param array $fields (optional) The array of fields and their options, for further processing with
203
- *
204
- * @return mixed|void
205
- *
206
- * @since 2.0
207
- */
208
- public function save_post ( $post_data, $post_meta = null, $strict = false, $sanitized = false, $fields = array() ) {
209
- $conflicted = pods_no_conflict_check( 'post' );
210
-
211
- if ( !$conflicted )
212
- pods_no_conflict_on( 'post' );
213
-
214
- if ( !is_array( $post_data ) || empty( $post_data ) )
215
- $post_data = array( 'post_title' => '' );
216
-
217
- if ( !is_array( $post_meta ) )
218
- $post_meta = array();
219
-
220
- if ( $sanitized ) {
221
- $post_data = pods_unsanitize( $post_data );
222
- $post_meta = pods_unsanitize( $post_meta );
223
- }
224
-
225
- if ( !isset( $post_data[ 'ID' ] ) || empty( $post_data[ 'ID' ] ) )
226
- $post_data[ 'ID' ] = wp_insert_post( $post_data, true );
227
- elseif ( 2 < count( $post_data ) || !isset( $post_data[ 'post_type' ] ) )
228
- wp_update_post( $post_data );
229
-
230
- if ( is_wp_error( $post_data[ 'ID' ] ) ) {
231
- if ( !$conflicted )
232
- pods_no_conflict_off( 'post' );
233
-
234
- /**
235
- * @var $post_error WP_Error
236
- */
237
- $post_error = $post_data[ 'ID' ];
238
-
239
- return pods_error( $post_error->get_error_message(), $this );
240
- }
241
-
242
- $this->save_post_meta( $post_data[ 'ID' ], $post_meta, $strict, $fields );
243
-
244
- if ( !$conflicted )
245
- pods_no_conflict_off( 'post' );
246
-
247
- return $post_data[ 'ID' ];
248
- }
249
-
250
- /**
251
- * Save a post's meta
252
- *
253
- * @param int $id Post ID
254
- * @param array $post_meta All meta to be saved (set value to null to delete)
255
- * @param bool $strict Whether to delete previously saved meta not in $post_meta
256
- * @param array $fields (optional) The array of fields and their options, for further processing with
257
- *
258
- * @return int Id of the post with the meta
259
- *
260
- * @since 2.0
261
- */
262
- public function save_post_meta ( $id, $post_meta = null, $strict = false, $fields = array() ) {
263
- $simple_tableless_objects = PodsForm::simple_tableless_objects();
264
-
265
- $conflicted = pods_no_conflict_check( 'post' );
266
-
267
- if ( !$conflicted )
268
- pods_no_conflict_on( 'post' );
269
-
270
- if ( !is_array( $post_meta ) )
271
- $post_meta = array();
272
-
273
- $id = (int) $id;
274
-
275
- $meta = get_post_meta( $id );
276
-
277
- foreach ( $meta as $k => $value ) {
278
- if ( is_array( $value ) && 1 == count( $value ) )
279
- $meta[ $k ] = current( $value );
280
- }
281
-
282
- foreach ( $post_meta as $meta_key => $meta_value ) {
283
- if ( null === $meta_value || ( $strict && '' === $post_meta[ $meta_key ] ) ) {
284
- $old_meta_value = '';
285
-
286
- if ( isset( $meta[ $meta_key ] ) )
287
- $old_meta_value = $meta[ $meta_key ];
288
-
289
- delete_post_meta( $id, $meta_key, $old_meta_value );
290
- }
291
- else {
292
- $simple = false;
293
-
294
- if ( isset( $fields[ $meta_key ] ) ) {
295
- $field_data = $fields[ $meta_key ];
296
-
297
- $simple = ( 'pick' == $field_data[ 'type' ] && in_array( pods_var( 'pick_object', $field_data ), $simple_tableless_objects ) );
298
- }
299
-
300
- if ( $simple ) {
301
- delete_post_meta( $id, $meta_key );
302
-
303
- update_post_meta( $id, '_pods_' . $meta_key, $meta_value );
304
-
305
- if ( ! is_array( $meta_value ) ) {
306
- $meta_value = array( $meta_value );
307
- }
308
-
309
- foreach ( $meta_value as $value ) {
310
- add_post_meta( $id, $meta_key, $value );
311
- }
312
- }
313
- else {
314
- update_post_meta( $id, $meta_key, $meta_value );
315
- }
316
- }
317
- }
318
-
319
- if ( $strict ) {
320
- foreach ( $meta as $meta_key => $meta_value ) {
321
- if ( !isset( $post_meta[ $meta_key ] ) )
322
- delete_post_meta( $id, $meta_key, $meta_value );
323
- }
324
- }
325
-
326
- if ( !$conflicted )
327
- pods_no_conflict_off( 'post' );
328
-
329
- return $id;
330
- }
331
-
332
- /**
333
- * Save a user and it's meta
334
- *
335
- * @param array $user_data All user data to be saved (using wp_insert_user / wp_update_user)
336
- * @param array $user_meta (optional) All meta to be saved (set value to null to delete)
337
- * @param bool $strict (optional) Whether to delete previously saved meta not in $user_meta
338
- * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
339
- * @param array $fields (optional) The array of fields and their options, for further processing with
340
- *
341
- * @return int Returns user id on success
342
- *
343
- * @since 2.0
344
- */
345
- public function save_user ( $user_data, $user_meta = null, $strict = false, $sanitized = false, $fields = array() ) {
346
- if ( !is_array( $user_data ) || empty( $user_data ) )
347
- return pods_error( __( 'User data is required but is either invalid or empty', 'pods' ), $this );
348
-
349
- $conflicted = pods_no_conflict_check( 'user' );
350
-
351
- if ( !$conflicted )
352
- pods_no_conflict_on( 'user' );
353
-
354
- if ( !is_array( $user_meta ) )
355
- $user_meta = array();
356
-
357
- if ( $sanitized ) {
358
- $user_data = pods_unsanitize( $user_data );
359
- $user_meta = pods_unsanitize( $user_meta );
360
- }
361
-
362
- // Set role
363
- if ( isset( $user_meta[ 'role' ] ) ) {
364
- $user_data[ 'role' ] = $user_meta[ 'role' ];
365
-
366
- unset( $user_meta[ 'role' ] );
367
- }
368
-
369
- if ( !isset( $user_data[ 'ID' ] ) || empty( $user_data[ 'ID' ] ) )
370
- $user_data[ 'ID' ] = wp_insert_user( $user_data );
371
- elseif ( 1 < count( $user_data ) )
372
- wp_update_user( $user_data );
373
-
374
- if ( is_wp_error( $user_data[ 'ID' ] ) ) {
375
- if ( !$conflicted )
376
- pods_no_conflict_off( 'user' );
377
-
378
- /**
379
- * @var $user_error WP_Error
380
- */
381
- $user_error = $user_data[ 'ID' ];
382
-
383
- return pods_error( $user_error->get_error_message(), $this );
384
- }
385
-
386
- $this->save_user_meta( $user_data[ 'ID' ], $user_meta, $strict, $fields );
387
-
388
- if ( !$conflicted )
389
- pods_no_conflict_off( 'user' );
390
-
391
- return $user_data[ 'ID' ];
392
- }
393
-
394
- /**
395
- * Save a user meta
396
- *
397
- * @param int $id User ID
398
- * @param array $user_meta (optional) All meta to be saved (set value to null to delete)
399
- * @param bool $strict (optional) Whether to delete previously saved meta not in $user_meta
400
- * @param array $fields (optional) The array of fields and their options, for further processing with
401
- *
402
- * @return int User ID
403
- *
404
- * @since 2.0
405
- *
406
- */
407
- public function save_user_meta ( $id, $user_meta = null, $strict = false, $fields = array() ) {
408
- $simple_tableless_objects = PodsForm::simple_tableless_objects();
409
-
410
- $conflicted = pods_no_conflict_check( 'user' );
411
-
412
- if ( !$conflicted )
413
- pods_no_conflict_on( 'user' );
414
-
415
- if ( !is_array( $user_meta ) )
416
- $user_meta = array();
417
-
418
- $id = (int) $id;
419
-
420
- $meta = get_user_meta( $id );
421
-
422
- foreach ( $user_meta as $meta_key => $meta_value ) {
423
- if ( null === $meta_value ) {
424
- $old_meta_value = '';
425
-
426
- if ( isset( $meta[ $meta_key ] ) )
427
- $old_meta_value = $meta[ $meta_key ];
428
-
429
- delete_user_meta( $id, $meta_key, $old_meta_value );
430
- }
431
- else {
432
- $simple = false;
433
-
434
- if ( isset( $fields[ $meta_key ] ) ) {
435
- $field_data = $fields[ $meta_key ];
436
-
437
- $simple = ( 'pick' == $field_data[ 'type' ] && in_array( pods_var( 'pick_object', $field_data ), $simple_tableless_objects ) );
438
- }
439
-
440
- if ( $simple ) {
441
- delete_user_meta( $id, $meta_key );
442
-
443
- if ( ! is_array( $meta_value ) ) {
444
- $meta_value = array( $meta_value );
445
- }
446
-
447
- foreach ( $meta_value as $value ) {
448
- add_user_meta( $id, $meta_key, $value );
449
- }
450
- }
451
- else {
452
- update_user_meta( $id, $meta_key, $meta_value );
453
- }
454
- }
455
- }
456
-
457
- if ( $strict ) {
458
- foreach ( $meta as $meta_key => $meta_value ) {
459
- if ( !isset( $user_meta[ $meta_key ] ) )
460
- delete_user_meta( $id, $meta_key, $user_meta[ $meta_key ] );
461
- }
462
- }
463
-
464
- if ( !$conflicted )
465
- pods_no_conflict_off( 'user' );
466
-
467
- return $id;
468
- }
469
-
470
- /**
471
- * Save a comment and it's meta
472
- *
473
- * @param array $comment_data All comment data to be saved (using wp_insert_comment / wp_update_comment)
474
- * @param array $comment_meta (optional) All meta to be saved (set value to null to delete)
475
- * @param bool $strict (optional) Whether to delete previously saved meta not in $comment_meta
476
- * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
477
- * @param array $fields (optional) The array of fields and their options, for further processing with
478
- *
479
- * @return int Comment ID
480
- *
481
- * @since 2.0
482
- */
483
- public function save_comment ( $comment_data, $comment_meta = null, $strict = false, $sanitized = false, $fields = array() ) {
484
- if ( !is_array( $comment_data ) || empty( $comment_data ) )
485
- return pods_error( __( 'Comment data is required but is either invalid or empty', 'pods' ), $this );
486
-
487
- $conflicted = pods_no_conflict_check( 'comment' );
488
-
489
- if ( !$conflicted )
490
- pods_no_conflict_on( 'comment' );
491
-
492
- if ( !is_array( $comment_meta ) )
493
- $comment_meta = array();
494
-
495
- if ( $sanitized ) {
496
- $comment_data = pods_unsanitize( $comment_data );
497
- $comment_meta = pods_unsanitize( $comment_meta );
498
- }
499
-
500
- if ( !isset( $comment_data[ 'comment_ID' ] ) || empty( $comment_data[ 'comment_ID' ] ) )
501
- $comment_data[ 'comment_ID' ] = wp_insert_comment( pods_slash( $comment_data ) ); // Expects slashed
502
- elseif ( 1 < count( $comment_data ) )
503
- wp_update_comment( $comment_data );
504
-
505
- if ( is_wp_error( $comment_data[ 'comment_ID' ] ) ) {
506
- if ( !$conflicted )
507
- pods_no_conflict_off( 'comment' );
508
-
509
- /**
510
- * @var $comment_error WP_Error
511
- */
512
- $comment_error = $comment_data[ 'comment_ID' ];
513
-
514
- return pods_error( $comment_error->get_error_message(), $this );
515
- }
516
-
517
- $this->save_comment_meta( $comment_data[ 'comment_ID' ], $comment_meta, $strict, $fields );
518
-
519
- if ( !$conflicted )
520
- pods_no_conflict_off( 'comment' );
521
-
522
- return $comment_data[ 'comment_ID' ];
523
- }
524
-
525
- /**
526
- * Save a comment meta
527
- *
528
- * @param int $id Comment ID
529
- * @param array $comment_meta (optional) All meta to be saved (set value to null to delete)
530
- * @param bool $strict (optional) Whether to delete previously saved meta not in $comment_meta
531
- * @param array $fields (optional) The array of fields and their options, for further processing with
532
- *
533
- * @return int Comment ID
534
- *
535
- * @since 2.0
536
- */
537
- public function save_comment_meta ( $id, $comment_meta = null, $strict = false, $fields = array() ) {
538
- $simple_tableless_objects = PodsForm::simple_tableless_objects();
539
-
540
- $conflicted = pods_no_conflict_check( 'comment' );
541
-
542
- if ( !$conflicted )
543
- pods_no_conflict_on( 'comment' );
544
-
545
- if ( !is_array( $comment_meta ) )
546
- $comment_meta = array();
547
-
548
- $id = (int) $id;
549
-
550
- $meta = get_comment_meta( $id );
551
-
552
- foreach ( $comment_meta as $meta_key => $meta_value ) {
553
- if ( null === $meta_value ) {
554
- $old_meta_value = '';
555
-
556
- if ( isset( $meta[ $meta_key ] ) )
557
- $old_meta_value = $meta[ $meta_key ];
558
-
559
- delete_comment_meta( $id, $meta_key, $old_meta_value );
560
- }
561
- else {
562
- $simple = false;
563
-
564
- if ( isset( $fields[ $meta_key ] ) ) {
565
- $field_data = $fields[ $meta_key ];
566
-
567
- $simple = ( 'pick' == $field_data[ 'type' ] && in_array( pods_var( 'pick_object', $field_data ), $simple_tableless_objects ) );
568
- }
569
-
570
- if ( $simple ) {
571
- delete_comment_meta( $id, $meta_key );
572
-
573
- if ( ! is_array( $meta_value ) ) {
574
- $meta_value = array( $meta_value );
575
- }
576
-
577
- foreach ( $meta_value as $value ) {
578
- add_comment_meta( $id, $meta_key, $value );
579
- }
580
- }
581
- else {
582
- update_comment_meta( $id, $meta_key, $meta_value );
583
- }
584
- }
585
- }
586
-
587
- if ( $strict ) {
588
- foreach ( $meta as $meta_key => $meta_value ) {
589
- if ( !isset( $comment_meta[ $meta_key ] ) )
590
- delete_comment_meta( (int) $id, $meta_key, $comment_meta[ $meta_key ] );
591
- }
592
- }
593
-
594
- if ( !$conflicted )
595
- pods_no_conflict_off( 'comment' );
596
-
597
- return $id;
598
- }
599
-
600
- /**
601
- * Save a taxonomy's term
602
- *
603
- * @param int $term_ID Term ID, leave empty to add
604
- * @param string $term Term name
605
- * @param string $taxonomy Taxonomy name
606
- * @param array $term_data All term data to be saved (using wp_insert_term / wp_update_term)
607
- * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
608
- * @param array $fields (optional) The array of fields and their options, for further processing with
609
- *
610
- * @return int Term ID
611
- *
612
- * @since 2.0
613
- */
614
- public function save_term ( $term_ID, $term, $taxonomy, $term_data, $sanitized = false, $fields = array() ) {
615
- $conflicted = pods_no_conflict_check( 'taxonomy' );
616
-
617
- if ( !$conflicted )
618
- pods_no_conflict_on( 'taxonomy' );
619
-
620
- if ( !is_array( $term_data ) )
621
- $term_data = array();
622
-
623
- $term_ID = (int) $term_ID;
624
-
625
- if ( $sanitized ) {
626
- $term = pods_unsanitize( $term );
627
- $taxonomy = pods_unsanitize( $taxonomy );
628
- $term_data = pods_unsanitize( $term_data );
629
- }
630
-
631
- if ( empty( $term_ID ) )
632
- $term_ID = wp_insert_term( $term, $taxonomy, $term_data );
633
- else {
634
- if ( 0 < strlen( $term ) )
635
- $term_data[ 'term' ] = $term;
636
-
637
- if ( empty( $term_data ) ) {
638
- if ( !$conflicted )
639
- pods_no_conflict_off( 'taxonomy' );
640
-
641
- return pods_error( __( 'Taxonomy term data is required but is either invalid or empty', 'pods' ), $this );
642
- }
643
-
644
- wp_update_term( $term_ID, $taxonomy, $term_data );
645
- }
646
-
647
- if ( is_wp_error( $term_ID ) ) {
648
- if ( !$conflicted )
649
- pods_no_conflict_off( 'taxonomy' );
650
-
651
- return pods_error( $term_ID->get_error_message(), $this );
652
- }
653
- elseif ( is_array( $term_ID ) )
654
- $term_ID = $term_ID[ 'term_id' ];
655
-
656
- if ( !$conflicted )
657
- pods_no_conflict_off( 'taxonomy' );
658
-
659
- return $term_ID;
660
- }
661
-
662
- /**
663
- * Save a set of options
664
- *
665
- * @param string $setting Setting group name
666
- * @param array $option_data All option data to be saved
667
- * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
668
- *
669
- * @return bool
670
- *
671
- * @since 2.3
672
- */
673
- public function save_setting ( $setting, $option_data, $sanitized = false ) {
674
- if ( !is_array( $option_data ) || empty( $option_data ) )
675
- return pods_error( __( 'Setting data is required but is either invalid or empty', 'pods' ), $this );
676
-
677
- $conflicted = pods_no_conflict_check( 'settings' );
678
-
679
- if ( !$conflicted )
680
- pods_no_conflict_on( 'settings' );
681
-
682
- if ( $sanitized )
683
- $option_data = pods_unsanitize( $option_data );
684
-
685
- foreach ( $option_data as $option => $value ) {
686
- if ( !empty( $setting ) )
687
- $option = $setting . '_' . $option;
688
-
689
- update_option( $option, $value );
690
- }
691
-
692
- if ( !$conflicted )
693
- pods_no_conflict_off( 'settings' );
694
-
695
- return true;
696
- }
697
-
698
- /**
699
- * Rename a WP object's type
700
- *
701
- * @param string $object_type Object type: post|taxonomy|comment|setting
702
- * @param string $old_name The old name
703
- * @param string $new_name The new name
704
- *
705
- * @return bool
706
- *
707
- * @since 2.0
708
- */
709
- public function rename_wp_object_type ( $object_type, $old_name, $new_name ) {
710
- /**
711
- * @var $wpdb wpdb
712
- */
713
- global $wpdb;
714
-
715
- if ( 'post_type' == $object_type )
716
- $object_type = 'post';
717
-
718
- if ( 'post' == $object_type ) {
719
- pods_query( "UPDATE `{$wpdb->posts}` SET `post_type` = %s WHERE `post_type` = %s", array(
720
- $new_name,
721
- $old_name
722
- ) );
723
- }
724
- elseif ( 'taxonomy' == $object_type ) {
725
- pods_query( "UPDATE `{$wpdb->term_taxonomy}` SET `taxonomy` = %s WHERE `taxonomy` = %s", array(
726
- $new_name,
727
- $old_name
728
- ) );
729
- }
730
- elseif ( 'comment' == $object_type ) {
731
- pods_query( "UPDATE `{$wpdb->comments}` SET `comment_type` = %s WHERE `comment_type` = %s", array(
732
- $new_name,
733
- $old_name
734
- ) );
735
- }
736
- elseif ( 'settings' == $object_type ) {
737
- pods_query( "UPDATE `{$wpdb->options}` SET `option_name` = REPLACE( `option_name`, %s, %s ) WHERE `option_name` LIKE '" . pods_sanitize_like( $old_name ) . "_%'", array(
738
- $new_name . '_',
739
- $old_name . '_'
740
- ) );
741
- }
742
-
743
- return true;
744
- }
745
-
746
- /**
747
- * Get a list of core WP object fields for a specific object
748
- *
749
- * @param string $object The pod type to look for, possible values: post_type, user, comment, taxonomy
750
- * @param array $pod Array of Pod data
751
- * @param boolean $refresh Whether to force refresh the information
752
- *
753
- * @return array Array of fields
754
- *
755
- * @since 2.0
756
- */
757
- public function get_wp_object_fields ( $object = 'post_type', $pod = null, $refresh = false ) {
758
- $pod_name = pods_var_raw( 'name', $pod, $object, null, true );
759
-
760
- if ( 'media' == $pod_name ) {
761
- $object = 'post_type';
762
- $pod_name = 'attachment';
763
- }
764
-
765
- $fields = false;
766
-
767
- if ( pods_api_cache() )
768
- $fields = pods_transient_get( trim( 'pods_api_object_fields_' . $object . $pod_name . '_', '_' ) );
769
-
770
- if ( false !== $fields && !$refresh )
771
- return $this->do_hook( 'get_wp_object_fields', $fields, $object, $pod );
772
-
773
- $fields = array();
774
-
775
- if ( 'post_type' == $object ) {
776
- $fields = array(
777
- 'ID' => array(
778
- 'name' => 'ID',
779
- 'label' => 'ID',
780
- 'type' => 'number',
781
- 'alias' => array( 'id' ),
782
- 'options' => array(
783
- 'number_format' => '9999.99'
784
- )
785
- ),
786
- 'post_title' => array(
787
- 'name' => 'post_title',
788
- 'label' => 'Title',
789
- 'type' => 'text',
790
- 'alias' => array( 'title', 'name' ),
791
- 'options' => array(
792
- 'display_filter' => 'the_title',
793
- 'display_filter_args' => array( 'post_ID' )
794
- )
795
- ),
796
- 'post_content' => array(
797
- 'name' => 'post_content',
798
- 'label' => 'Content',
799
- 'type' => 'wysiwyg',
800
- 'alias' => array( 'content' ),
801
- 'options' => array(
802
- 'wysiwyg_allowed_html_tags' => '',
803
- 'display_filter' => 'the_content',
804
- 'pre_save' => 0
805
- )
806
- ),
807
- 'post_excerpt' => array(
808
- 'name' => 'post_excerpt',
809
- 'label' => 'Excerpt',
810
- 'type' => 'paragraph',
811
- 'alias' => array( 'excerpt' ),
812
- 'options' => array(
813
- 'paragraph_allow_html' => 1,
814
- 'paragraph_allowed_html_tags' => '',
815
- 'display_filter' => 'the_excerpt',
816
- 'pre_save' => 0
817
- )
818
- ),
819
- 'post_author' => array(
820
- 'name' => 'post_author',
821
- 'label' => 'Author',
822
- 'type' => 'pick',
823
- 'alias' => array( 'author' ),
824
- 'pick_object' => 'user',
825
- 'options' => array(
826
- 'pick_format_type' => 'single',
827
- 'pick_format_single' => 'autocomplete',
828
- 'default_value' => '{@user.ID}'
829
- )
830
- ),
831
- 'post_date' => array(
832
- 'name' => 'post_date',
833
- 'label' => 'Publish Date',
834
- 'type' => 'datetime',
835
- 'alias' => array( 'created', 'date' )
836
- ),
837
- 'post_date_gmt' => array(
838
- 'name' => 'post_date_gmt',
839
- 'label' => 'Publish Date (GMT)',
840
- 'type' => 'datetime',
841
- 'alias' => array(),
842
- 'hidden' => true
843
- ),
844
- 'post_status' => array(
845
- 'name' => 'post_status',
846
- 'label' => 'Status',
847
- 'type' => 'pick',
848
- 'pick_object' => 'post-status',
849
- 'default' => $this->do_hook( 'default_status_' . $pod_name, pods_var( 'default_status', pods_var_raw( 'options', $pod ), 'draft', null, true ), $pod ),
850
- 'alias' => array( 'status' )
851
- ),
852
- 'comment_status' => array(
853
- 'name' => 'comment_status',
854
- 'label' => 'Comment Status',
855
- 'type' => 'text',
856
- 'default' => get_option( 'default_comment_status', 'open' ),
857
- 'alias' => array(),
858
- 'data' => array(
859
- 'open' => __( 'Open', 'pods' ),
860
- 'closed' => __( 'Closed', 'pods' )
861
- )
862
- ),
863
- 'ping_status' => array(
864
- 'name' => 'ping_status',
865
- 'label' => 'Ping Status',
866
- 'default' => get_option( 'default_ping_status', 'open' ),
867
- 'type' => 'text',
868
- 'alias' => array(),
869
- 'data' => array(
870
- 'open' => __( 'Open', 'pods' ),
871
- 'closed' => __( 'Closed', 'pods' )
872
- )
873
- ),
874
- 'post_password' => array(
875
- 'name' => 'post_password',
876
- 'label' => 'Password',
877
- 'type' => 'text',
878
- 'alias' => array()
879
- ),
880
- 'post_name' => array(
881
- 'name' => 'post_name',
882
- 'label' => 'Permalink',
883
- 'type' => 'slug',
884
- 'alias' => array( 'slug', 'permalink' )
885
- ),
886
- 'to_ping' => array(
887
- 'name' => 'to_ping',
888
- 'label' => 'To Ping',
889
- 'type' => 'text',
890
- 'alias' => array(),
891
- 'hidden' => true
892
- ),
893
- 'pinged' => array(
894
- 'name' => 'pinged',
895
- 'label' => 'Pinged',
896
- 'type' => 'text',
897
- 'alias' => array(),
898
- 'hidden' => true
899
- ),
900
- 'post_modified' => array(
901
- 'name' => 'post_modified',
902
- 'label' => 'Last Modified Date',
903
- 'type' => 'datetime',
904
- 'alias' => array( 'modified' ),
905
- 'hidden' => true
906
- ),
907
- 'post_modified_gmt' => array(
908
- 'name' => 'post_modified_gmt',
909
- 'label' => 'Last Modified Date (GMT)',
910
- 'type' => 'datetime',
911
- 'alias' => array(),
912
- 'hidden' => true
913
- ),
914
- 'post_content_filtered' => array(
915
- 'name' => 'post_content_filtered',
916
- 'label' => 'Content (filtered)',
917
- 'type' => 'paragraph',
918
- 'alias' => array(),
919
- 'hidden' => true,
920
- 'options' => array(
921
- 'paragraph_allow_html' => 1,
922
- 'paragraph_oembed' => 1,
923
- 'paragraph_wptexturize' => 1,
924
- 'paragraph_convert_chars' => 1,
925
- 'paragraph_wpautop' => 1,
926
- 'paragraph_allow_shortcode' => 1,
927
- 'paragraph_allowed_html_tags' => ''
928
- )
929
- ),
930
- 'post_parent' => array(
931
- 'name' => 'post_parent',
932
- 'label' => 'Parent',
933
- 'type' => 'pick',
934
- 'pick_object' => 'post_type',
935
- 'pick_val' => '__current__',
936
- 'alias' => array( 'parent' ),
937
- 'data' => array(),
938
- 'hidden' => true
939
- ),
940
- 'guid' => array(
941
- 'name' => 'guid',
942
- 'label' => 'GUID',
943
- 'type' => 'text',
944
- 'alias' => array(),
945
- 'hidden' => true
946
- ),
947
- 'menu_order' => array(
948
- 'name' => 'menu_order',
949
- 'label' => 'Menu Order',
950
- 'type' => 'number',
951
- 'alias' => array(),
952
- 'options' => array(
953
- 'number_format' => '9999.99'
954
- )
955
- ),
956
- 'post_type' => array(
957
- 'name' => 'post_type',
958
- 'label' => 'Type',
959
- 'type' => 'text',
960
- 'alias' => array( 'type' ),
961
- 'hidden' => true
962
- ),
963
- 'post_mime_type' => array(
964
- 'name' => 'post_mime_type',
965
- 'label' => 'Mime Type',
966
- 'type' => 'text',
967
- 'alias' => array(),
968
- 'hidden' => true
969
- ),
970
- 'comment_count' => array(
971
- 'name' => 'comment_count',
972
- 'label' => 'Comment Count',
973
- 'type' => 'number',
974
- 'alias' => array(),
975
- 'hidden' => true
976
- )
977
- );
978
-
979
- if ( !empty( $pod ) ) {
980
- $taxonomies = get_object_taxonomies( $pod_name, 'objects' );
981
-
982
- foreach ( $taxonomies as $taxonomy ) {
983
- $fields[ $taxonomy->name ] = array(
984
- 'name' => $taxonomy->name,
985
- 'label' => $taxonomy->labels->name,
986
- 'type' => 'taxonomy',
987
- 'pick_object' => 'taxonomy',
988
- 'pick_val' => $taxonomy->name,
989
- 'alias' => array(),
990
- 'hidden' => true,
991
- 'options' => array(
992
- 'taxonomy_format_type' => 'multi'
993
- )
994
- );
995
- }
996
- }
997
- }
998
- elseif ( 'user' == $object ) {
999
- $fields = array(
1000
- 'ID' => array(
1001
- 'name' => 'ID',
1002
- 'label' => 'ID',
1003
- 'type' => 'number',
1004
- 'alias' => array( 'id' ),
1005
- 'options' => array(
1006
- 'number_format' => '9999.99'
1007
- )
1008
- ),
1009
- 'user_login' => array(
1010
- 'name' => 'user_login',
1011
- 'label' => 'Title',
1012
- 'type' => 'text',
1013
- 'alias' => array( 'login' ),
1014
- 'options' => array(
1015
- 'required' => 1
1016
- )
1017
- ),
1018
- 'user_nicename' => array(
1019
- 'name' => 'user_nicename',
1020
- 'label' => 'Permalink',
1021
- 'type' => 'slug',
1022
- 'alias' => array( 'nicename', 'slug', 'permalink' )
1023
- ),
1024
- 'display_name' => array(
1025
- 'name' => 'display_name',
1026
- 'label' => 'Display Name',
1027
- 'type' => 'text',
1028
- 'alias' => array( 'title', 'name' )
1029
- ),
1030
- 'user_pass' => array(
1031
- 'name' => 'user_pass',
1032
- 'label' => 'Password',
1033
- 'type' => 'text',
1034
- 'alias' => array( 'password', 'pass' ),
1035
- 'options' => array(
1036
- 'required' => 1,
1037
- 'text_format_type' => 'password'
1038
- )
1039
- ),
1040
- 'user_email' => array(
1041
- 'name' => 'user_email',
1042
- 'label' => 'E-mail',
1043
- 'type' => 'text',
1044
- 'alias' => array( 'email' ),
1045
- 'options' => array(
1046
- 'required' => 1,
1047
- 'text_format_type' => 'email'
1048
- )
1049
- ),
1050
- 'user_url' => array(
1051
- 'name' => 'user_url',
1052
- 'label' => 'URL',
1053
- 'type' => 'text',
1054
- 'alias' => array( 'url', 'website' ),
1055
- 'options' => array(
1056
- 'required' => 0,
1057
- 'text_format_type' => 'website',
1058
- 'text_format_website' => 'normal'
1059
- )
1060
- ),
1061
- 'user_registered' => array(
1062
- 'name' => 'user_registered',
1063
- 'label' => 'Registration Date',
1064
- 'type' => 'date',
1065
- 'alias' => array( 'created', 'date', 'registered' ),
1066
- 'options' => array(
1067
- 'date_format_type' => 'datetime'
1068
- )
1069
- )
1070
- );
1071
- }
1072
- elseif ( 'comment' == $object ) {
1073
- $fields = array(
1074
- 'comment_ID' => array(
1075
- 'name' => 'comment_ID',
1076
- 'label' => 'ID',
1077
- 'type' => 'number',
1078
- 'alias' => array( 'id', 'ID', 'comment_id' ),
1079
- 'options' => array(
1080
- 'number_format' => '9999.99'
1081
- )
1082
- ),
1083
- 'comment_content' => array(
1084
- 'name' => 'comment_content',
1085
- 'label' => 'Content',
1086
- 'type' => 'wysiwyg',
1087
- 'alias' => array( 'content' )
1088
- ),
1089
- 'comment_approved' => array(
1090
- 'name' => 'comment_approved',
1091
- 'label' => 'Approved',
1092
- 'type' => 'number',
1093
- 'alias' => array( 'approved' ),
1094
- 'options' => array(
1095
- 'number_format' => '9999.99'
1096
- )
1097
- ),
1098
- 'comment_post_ID' => array(
1099
- 'name' => 'comment_post_ID',
1100
- 'label' => 'Post',
1101
- 'type' => 'pick',
1102
- 'alias' => array( 'post', 'post_id' ),
1103
- 'data' => array()
1104
- ),
1105
- 'user_id' => array(
1106
- 'name' => 'user_id',
1107
- 'label' => 'Author',
1108
- 'type' => 'pick',
1109
- 'alias' => array( 'author' ),
1110
- 'pick_object' => 'user',
1111
- 'data' => array()
1112
- ),
1113
- 'comment_date' => array(
1114
- 'name' => 'comment_date',
1115
- 'label' => 'Date',
1116
- 'type' => 'date',
1117
- 'alias' => array( 'created', 'date' ),
1118
- 'options' => array(
1119
- 'date_format_type' => 'datetime'
1120
- )
1121
- ),
1122
- 'comment_author' => array(
1123
- 'name' => 'comment_author',
1124
- 'label' => 'Author',
1125
- 'type' => 'text',
1126
- 'alias' => array( 'author' )
1127
- ),
1128
- 'comment_author_email' => array(
1129
- 'name' => 'comment_author_email',
1130
- 'label' => 'Author E-mail',
1131
- 'type' => 'email',
1132
- 'alias' => array( 'author_email' )
1133
- ),
1134
- 'comment_author_url' => array(
1135
- 'name' => 'comment_author_url',
1136
- 'label' => 'Author URL',
1137
- 'type' => 'text',
1138
- 'alias' => array( 'author_url' )
1139
- ),
1140
- 'comment_author_IP' => array(
1141
- 'name' => 'comment_author_IP',
1142
- 'label' => 'Author IP',
1143
- 'type' => 'text',
1144
- 'alias' => array( 'author_IP' )
1145
- ),
1146
- 'comment_type' => array(
1147
- 'name' => 'comment_type',
1148
- 'label' => 'Type',
1149
- 'type' => 'text',
1150
- 'alias' => array( 'type' ),
1151
- 'hidden' => true
1152
- ),
1153
- 'comment_parent' => array(
1154
- 'name' => 'comment_parent',
1155
- 'label' => 'Parent',
1156
- 'type' => 'pick',
1157
- 'pick_object' => 'comment',
1158
- 'pick_val' => '__current__',
1159
- 'alias' => array( 'parent' ),
1160
- 'data' => array(),
1161
- 'hidden' => true
1162
- )
1163
- );
1164
- }
1165
- elseif ( 'taxonomy' == $object ) {
1166
- $fields = array(
1167
- 'term_id' => array(
1168
- 'name' => 'term_id',
1169
- 'label' => 'ID',
1170
- 'type' => 'number',
1171
- 'alias' => array( 'id', 'ID' ),
1172
- 'options' => array(
1173
- 'number_format' => '9999.99'
1174
- )
1175
- ),
1176
- 'name' => array(
1177
- 'name' => 'name',
1178
- 'label' => 'Title',
1179
- 'type' => 'text',
1180
- 'alias' => array( 'title' )
1181
- ),
1182
- 'slug' => array(
1183
- 'name' => 'slug',
1184
- 'label' => 'Permalink',
1185
- 'type' => 'slug',
1186
- 'alias' => array( 'permalink' )
1187
- ),
1188
- 'description' => array(
1189
- 'name' => 'description',
1190
- 'label' => 'Description',
1191
- 'type' => 'wysiwyg',
1192
- 'alias' => array( 'content' )
1193
- ),
1194
- 'taxonomy' => array(
1195
- 'name' => 'taxonomy',
1196
- 'label' => 'Taxonomy',
1197
- 'type' => 'pick',
1198
- 'alias' => array()
1199
- ),
1200
- 'parent' => array(
1201
- 'name' => 'parent',
1202
- 'label' => 'Parent',
1203
- 'type' => 'pick',
1204
- 'pick_object' => 'taxonomy',
1205
- 'pick_val' => '__current__',
1206
- 'alias' => array( 'parent' ),
1207
- 'data' => array(),
1208
- 'hidden' => true
1209
- ),
1210
- 'term_taxonomy_id' => array(
1211
- 'name' => 'term_taxonomy_id',
1212
- 'label' => 'Term Taxonomy ID',
1213
- 'type' => 'number',
1214
- 'alias' => array(),
1215
- 'hidden' => true,
1216
- 'options' => array(
1217
- 'number_format' => '9999.99'
1218
- )
1219
- ),
1220
- 'term_group' => array(
1221
- 'name' => 'term_group',
1222
- 'label' => 'Term Group',
1223
- 'type' => 'number',
1224
- 'alias' => array( 'group' ),
1225
- 'hidden' => true,
1226
- 'options' => array(
1227
- 'number_format' => '9999.99'
1228
- )
1229
- ),
1230
- 'count' => array(
1231
- 'name' => 'count',
1232
- 'label' => 'Count',
1233
- 'type' => 'number',
1234
- 'alias' => array(),
1235
- 'hidden' => true,
1236
- 'options' => array(
1237
- 'number_format' => '9999.99'
1238
- )
1239
- )
1240
- );
1241
- }
1242
-
1243
- $fields = $this->do_hook( 'get_wp_object_fields', $fields, $object, $pod );
1244
-
1245
- foreach ( $fields as $field => $options ) {
1246
- if ( !isset( $options[ 'alias' ] ) )
1247
- $options[ 'alias' ] = array();
1248
- else
1249
- $options[ 'alias' ] = (array) $options[ 'alias' ];
1250
-
1251
- if ( !isset( $options[ 'name' ] ) )
1252
- $options[ 'name' ] = $field;
1253
-
1254
- $fields[ $field ] = $options;
1255
- }
1256
-
1257
- $fields = PodsForm::fields_setup( $fields );
1258
-
1259
- if ( did_action( 'init' ) && pods_api_cache() )
1260
- pods_transient_set( trim( 'pods_api_object_fields_' . $object . $pod_name . '_', '_' ), $fields );
1261
-
1262
- return $fields;
1263
- }
1264
-
1265
- /**
1266
- *
1267
- * @see PodsAPI::save_pod
1268
- *
1269
- * Add a Pod via the Wizard
1270
- *
1271
- * $params['create_extend'] string Create or Extend a Content Type
1272
- * $params['create_pod_type'] string Pod Type (for Creating)
1273
- * $params['create_name'] string Pod Name (for Creating)
1274
- * $params['create_label_plural'] string Plural Label (for Creating)
1275
- * $params['create_label_singular'] string Singular Label (for Creating)
1276
- * $params['create_storage'] string Storage Type (for Creating Post Types)
1277
- * $params['create_storage_taxonomy'] string Storage Type (for Creating Taxonomies)
1278
- * $params['extend_pod_type'] string Pod Type (for Extending)
1279
- * $params['extend_post_type'] string Post Type (for Extending Post Types)
1280
- * $params['extend_taxonomy'] string Taxonomy (for Extending Taxonomies)
1281
- * $params['extend_storage'] string Storage Type (for Extending Post Types / Users / Comments)
1282
- *
1283
- * @param array $params An associative array of parameters
1284
- *
1285
- * @return bool|int Pod ID
1286
- * @since 2.0
1287
- */
1288
- public function add_pod ( $params ) {
1289
- $defaults = array(
1290
- 'create_extend' => 'create',
1291
- 'create_pod_type' => 'post_type',
1292
-
1293
- 'create_name' => '',
1294
- 'create_label_singular' => '',
1295
- 'create_label_plural' => '',
1296
- 'create_storage' => 'meta',
1297
- 'create_storage_taxonomy' => 'none',
1298
-
1299
- 'create_setting_name' => '',
1300
- 'create_label_title' => '',
1301
- 'create_label_menu' => '',
1302
- 'create_menu_location' => 'settings',
1303
-
1304
- 'extend_pod_type' => 'post_type',
1305
- 'extend_post_type' => 'post',
1306
- 'extend_taxonomy' => 'category',
1307
- 'extend_table' => '',
1308
- 'extend_storage_taxonomy' => 'table',
1309
- 'extend_storage' => 'meta'
1310
- );
1311
-
1312
- $params = (object) array_merge( $defaults, (array) $params );
1313
-
1314
- if ( empty( $params->create_extend ) || !in_array( $params->create_extend, array( 'create', 'extend' ) ) )
1315
- return pods_error( __( 'Please choose whether to Create or Extend a Content Type', 'pods' ), $this );
1316
-
1317
- $pod_params = array(
1318
- 'name' => '',
1319
- 'label' => '',
1320
- 'type' => '',
1321
- 'storage' => 'table',
1322
- 'object' => '',
1323
- 'options' => array()
1324
- );
1325
-
1326
- if ( 'create' == $params->create_extend ) {
1327
- $label = ucwords( str_replace( '_', ' ', $params->create_name ) );
1328
-
1329
- if ( !empty( $params->create_label_singular ) )
1330
- $label = $params->create_label_singular;
1331
-
1332
- $pod_params[ 'name' ] = $params->create_name;
1333
- $pod_params[ 'label' ] = ( !empty( $params->create_label_plural ) ? $params->create_label_plural : $label );
1334
- $pod_params[ 'type' ] = $params->create_pod_type;
1335
- $pod_params[ 'options' ] = array(
1336
- 'label_singular' => ( !empty( $params->create_label_singular ) ? $params->create_label_singular : $pod_params[ 'label' ] ),
1337
- 'public' => 1,
1338
- 'show_ui' => 1
1339
- );
1340
-
1341
- // Auto-generate name if not provided
1342
- if ( empty( $pod_params[ 'name' ] ) && !empty( $pod_params[ 'options' ][ 'label_singular' ] ) )
1343
- $pod_params[ 'name' ] = pods_clean_name( $pod_params[ 'options' ][ 'label_singular' ] );
1344
-
1345
- if ( 'post_type' == $pod_params[ 'type' ] ) {
1346
- if ( empty( $pod_params[ 'name' ] ) )
1347
- return pods_error( 'Please enter a Name for this Pod', $this );
1348
-
1349
- $pod_params[ 'storage' ] = $params->create_storage;
1350
-
1351
- if ( pods_tableless() )
1352
- $pod_params[ 'storage' ] = 'meta';
1353
- }
1354
- elseif ( 'taxonomy' == $pod_params[ 'type' ] ) {
1355
- if ( empty( $pod_params[ 'name' ] ) )
1356
- return pods_error( 'Please enter a Name for this Pod', $this );
1357
-
1358
- $pod_params[ 'storage' ] = $params->create_storage_taxonomy;
1359
-
1360
- if ( pods_tableless() )
1361
- $pod_params[ 'storage' ] = 'none';
1362
- }
1363
- elseif ( 'pod' == $pod_params[ 'type' ] ) {
1364
- if ( empty( $pod_params[ 'name' ] ) )
1365
- return pods_error( 'Please enter a Name for this Pod', $this );
1366
-
1367
- if ( pods_tableless() ) {
1368
- $pod_params[ 'type' ] = 'post_type';
1369
- $pod_params[ 'storage' ] = 'meta';
1370
- }
1371
- }
1372
- elseif ( 'settings' == $pod_params[ 'type' ] ) {
1373
- $pod_params[ 'name' ] = $params->create_setting_name;
1374
- $pod_params[ 'label' ] = ( !empty( $params->create_label_title ) ? $params->create_label_title : ucwords( str_replace( '_', ' ', $params->create_setting_name ) ) );
1375
- $pod_params[ 'options' ] = array(
1376
- 'menu_name' => ( !empty( $params->create_label_menu ) ? $params->create_label_menu : $pod_params[ 'label' ] ),
1377
- 'menu_location' => $params->create_menu_location
1378
- );
1379
- $pod_params[ 'storage' ] = 'none';
1380
-
1381
- // Auto-generate name if not provided
1382
- if ( empty( $pod_params[ 'name' ] ) && !empty( $pod_params[ 'label' ] ) )
1383
- $pod_params[ 'name' ] = pods_clean_name( $pod_params[ 'label' ] );
1384
-
1385
- if ( empty( $pod_params[ 'name' ] ) )
1386
- return pods_error( 'Please enter a Name for this Pod', $this );
1387
- }
1388
- }
1389
- elseif ( 'extend' == $params->create_extend ) {
1390
- $pod_params[ 'type' ] = $params->extend_pod_type;
1391
-
1392
- if ( 'post_type' == $pod_params[ 'type' ] ) {
1393
- $pod_params[ 'storage' ] = $params->extend_storage;
1394
-
1395
- if ( pods_tableless() )
1396
- $pod_params[ 'storage' ] = 'meta';
1397
-
1398
- $pod_params[ 'name' ] = $params->extend_post_type;
1399
- }
1400
- elseif ( 'taxonomy' == $pod_params[ 'type' ] ) {
1401
- $pod_params[ 'storage' ] = $params->extend_storage_taxonomy;
1402
-
1403
- if ( pods_tableless() )
1404
- $pod_params[ 'storage' ] = 'none';
1405
-
1406
- $pod_params[ 'name' ] = $params->extend_taxonomy;
1407
- }
1408
- elseif ( 'table' == $pod_params[ 'type' ] ) {
1409
- $pod_params[ 'storage' ] = 'table';
1410
- $pod_params[ 'name' ] = $params->extend_table;
1411
- }
1412
- else {
1413
- $pod_params[ 'storage' ] = $params->extend_storage;
1414
-
1415
- if ( pods_tableless() )
1416
- $pod_params[ 'storage' ] = 'meta';
1417
-
1418
- $pod_params[ 'name' ] = $params->extend_pod_type;
1419
- }
1420
-
1421
- $pod_params[ 'label' ] = ucwords( str_replace( '_', ' ', $pod_params[ 'name' ] ) );
1422
- $pod_params[ 'object' ] = $pod_params[ 'name' ];
1423
- }
1424
-
1425
- if ( empty( $pod_params[ 'object' ] ) ) {
1426
- if ( 'post_type' == $pod_params[ 'type' ] ) {
1427
- $check = get_post_type_object( $pod_params[ 'name' ] );
1428
-
1429
- if ( !empty( $check ) )
1430
- return pods_error( sprintf( __( 'Post Type %s already exists, try extending it instead', 'pods' ), $pod_params[ 'name' ] ), $this );
1431
-
1432
- $pod_params[ 'options' ][ 'supports_title' ] = 1;
1433
- $pod_params[ 'options' ][ 'supports_editor' ] = 1;
1434
- }
1435
- elseif ( 'taxonomy' == $pod_params[ 'type' ] ) {
1436
- $check = get_taxonomy( $pod_params[ 'name' ] );
1437
-
1438
- if ( !empty( $check ) )
1439
- return pods_error( sprintf( __( 'Taxonomy %s already exists, try extending it instead', 'pods' ), $pod_params[ 'name' ] ), $this );
1440
- }
1441
- }
1442
-
1443
- if ( !empty( $pod_params ) )
1444
- return $this->save_pod( $pod_params );
1445
-
1446
- return false;
1447
- }
1448
-
1449
- /**
1450
- * Add or edit a Pod
1451
- *
1452
- * $params['id'] int The Pod ID
1453
- * $params['name'] string The Pod name
1454
- * $params['label'] string The Pod label
1455
- * $params['type'] string The Pod type
1456
- * $params['object'] string The object being extended (if any)
1457
- * $params['storage'] string The Pod storage
1458
- * $params['options'] array Options
1459
- *
1460
- * @param array $params An associative array of parameters
1461
- * @param bool $sanitized (optional) Decides whether the params have been sanitized before being passed, will sanitize them if false.
1462
- * @param bool|int $db (optional) Whether to save into the DB or just return Pod array.
1463
- *
1464
- * @return int Pod ID
1465
- * @since 1.7.9
1466
- */
1467
- public function save_pod ( $params, $sanitized = false, $db = true ) {
1468
- $tableless_field_types = PodsForm::tableless_field_types();
1469
- $simple_tableless_objects = PodsForm::simple_tableless_objects();
1470
-
1471
- $load_params = (object) $params;
1472
-
1473
- if ( isset( $load_params->id ) && isset( $load_params->name ) )
1474
- unset( $load_params->name );
1475
-
1476
- if ( isset( $load_params->old_name ) )
1477
- $load_params->name = $load_params->old_name;
1478
-
1479
- $load_params->table_info = true;
1480
-
1481
- $pod = $this->load_pod( $load_params, false );
1482
-
1483
- $params = (object) $params;
1484
-
1485
- if ( false === $sanitized )
1486
- $params = pods_sanitize( $params );
1487
-
1488
- $old_id = $old_name = $old_storage = null;
1489
-
1490
- $old_fields = $old_options = array();
1491
-
1492
- if ( isset( $params->name ) && ! isset( $params->object ) ) {
1493
- $params->name = pods_clean_name( $params->name );
1494
- }
1495
-
1496
- if ( !empty( $pod ) ) {
1497
- if ( isset( $params->id ) && 0 < $params->id )
1498
- $old_id = $params->id;
1499
-
1500
- $params->id = $pod[ 'id' ];
1501
-
1502
- $old_name = $pod[ 'name' ];
1503
- $old_storage = $pod[ 'storage' ];
1504
- $old_fields = $pod[ 'fields' ];
1505
- $old_options = $pod[ 'options' ];
1506
-
1507
- if ( !isset( $params->name ) && empty( $params->name ) )
1508
- $params->name = $pod[ 'name' ];
1509
-
1510
- if ( $old_name != $params->name && false !== $this->pod_exists( array( 'name' => $params->name ) ) )
1511
- return pods_error( sprintf( __( 'Pod %s already exists, you cannot rename %s to that', 'pods' ), $params->name, $old_name ), $this );
1512
-
1513
- if ( $old_name != $params->name && in_array( $pod[ 'type' ], array( 'user', 'comment', 'media' ) ) && in_array( $pod[ 'object' ], array( 'user', 'comment', 'media' ) ) )
1514
- return pods_error( sprintf( __( 'Pod %s cannot be renamed, it extends an existing WP Object', 'pods' ), $old_name ), $this );
1515
-
1516
- if ( $old_name != $params->name && in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && !empty( $pod[ 'object' ] ) && $pod[ 'object' ] == $old_name )
1517
- return pods_error( sprintf( __( 'Pod %s cannot be renamed, it extends an existing WP Object', 'pods' ), $old_name ), $this );
1518
-
1519
- if ( $old_id != $params->id ) {
1520
- if ( $params->type == $pod[ 'type' ] && isset( $params->object ) && $params->object == $pod[ 'object' ] )
1521
- return pods_error( sprintf( __( 'Pod using %s already exists, you can not reuse an object across multiple pods', 'pods' ), $params->object ), $this );
1522
- else
1523
- return pods_error( sprintf( __( 'Pod %s already exists', 'pods' ), $params->name ), $this );
1524
- }
1525
- }
1526
- elseif ( in_array( $params->name, array( 'order','orderby','post_type' ) ) && 'post_type' == pods_var( 'type', $params ) ) {
1527
- return pods_error( sprintf( 'There are certain names that a Custom Post Types cannot be named and unfortunately, %s is one of them.', $params->name ), $this );
1528
- }
1529
- else {
1530
- $pod = array(
1531
- 'id' => 0,
1532
- 'name' => $params->name,
1533
- 'label' => $params->name,
1534
- 'description' => '',
1535
- 'type' => 'pod',
1536
- 'storage' => 'table',
1537
- 'object' => '',
1538
- 'alias' => '',
1539
- 'options' => array(),
1540
- 'fields' => array()
1541
- );
1542
- }
1543
-
1544
- // Blank out fields and options for AJAX calls (everything should be sent to it for a full overwrite)
1545
- if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
1546
- $pod[ 'fields' ] = array();
1547
- $pod[ 'options' ] = array();
1548
- }
1549
-
1550
- // Setup options
1551
- $options = get_object_vars( $params );
1552
-
1553
- if ( isset( $options[ 'method' ] ) )
1554
- unset( $options[ 'method' ] );
1555
-
1556
- $options_ignore = array(
1557
- 'object_type',
1558
- 'object_name',
1559
- 'table',
1560
- 'meta_table',
1561
- 'pod_table',
1562
- 'field_id',
1563
- 'field_index',
1564
- 'field_slug',
1565
- 'field_type',
1566
- 'field_parent',
1567
- 'field_parent_select',
1568
- 'meta_field_id',
1569
- 'meta_field_index',
1570
- 'meta_field_value',
1571
- 'pod_field_id',
1572
- 'pod_field_index',
1573
- 'object_fields',
1574
- 'join',
1575
- 'where',
1576
- 'where_default',
1577
- 'orderby',
1578
- 'pod',
1579
- 'recurse',
1580
- 'table_info',
1581
- 'attributes',
1582
- 'group',
1583
- 'grouped',
1584
- 'developer_mode',
1585
- 'dependency',
1586
- 'depends-on',
1587
- 'excludes-on'
1588
- );
1589
-
1590
- foreach ( $options_ignore as $ignore ) {
1591
- if ( isset( $options[ $ignore ] ) )
1592
- unset( $options[ $ignore ] );
1593
- }
1594
-
1595
- $exclude = array(
1596
- 'id',
1597
- 'name',
1598
- 'label',
1599
- 'description',
1600
- 'type',
1601
- 'storage',
1602
- 'object',
1603
- 'alias',
1604
- 'options',
1605
- 'fields'
1606
- );
1607
-
1608
- foreach ( $exclude as $k => $exclude_field ) {
1609
- $aliases = array( $exclude_field );
1610
-
1611
- if ( is_array( $exclude_field ) ) {
1612
- $aliases = array_merge( array( $k ), $exclude_field );
1613
- $exclude_field = $k;
1614
- }
1615
-
1616
- foreach ( $aliases as $alias ) {
1617
- if ( isset( $options[ $alias ] ) ) {
1618
- $pod[ $exclude_field ] = pods_trim( $options[ $alias ] );
1619
-
1620
- unset( $options[ $alias ] );
1621
- }
1622
- }
1623
- }
1624
-
1625
- if ( pods_tableless() && !in_array( $pod[ 'type' ], array( 'settings', 'table' ) ) ) {
1626
- if ( 'pod' == $pod[ 'type' ] )
1627
- $pod[ 'type' ] = 'post_type';
1628
-
1629
- if ( 'table' == $pod[ 'storage' ] ) {
1630
- if ( 'taxonomy' == $pod[ 'type' ] )
1631
- $pod[ 'storage' ] = 'none';
1632
- else
1633
- $pod[ 'storage' ] = 'meta';
1634
- }
1635
- }
1636
-
1637
- $pod[ 'options' ][ 'type' ] = $pod[ 'type' ];
1638
- $pod[ 'options' ][ 'storage' ] = $pod[ 'storage' ];
1639
- $pod[ 'options' ][ 'object' ] = $pod[ 'object' ];
1640
- $pod[ 'options' ][ 'alias' ] = $pod[ 'alias' ];
1641
-
1642
- $pod[ 'options' ] = array_merge( $pod[ 'options' ], $options );
1643
-
1644
- /**
1645
- * @var WP_Query
1646
- */
1647
- global $wp_query;
1648
-
1649
- $reserved_query_vars = array(
1650
- 'post_type',
1651
- 'taxonomy',
1652
- 'output'
1653
- );
1654
-
1655
- if ( is_object( $wp_query ) ) {
1656
- $reserved_query_vars = array_merge( $reserved_query_vars, array_keys( $wp_query->fill_query_vars( array() ) ) );
1657
- }
1658
-
1659
- if ( isset( $pod[ 'options' ][ 'query_var_string' ] ) ) {
1660
- if ( in_array( $pod[ 'options' ][ 'query_var_string' ], $reserved_query_vars ) ) {
1661
- $pod[ 'options' ][ 'query_var_string' ] = $pod[ 'options' ][ 'type' ] . '_' . $pod[ 'options' ][ 'query_var_string' ];
1662
- }
1663
- }
1664
-
1665
- if ( isset( $pod[ 'options' ][ 'query_var' ] ) ) {
1666
- if ( in_array( $pod[ 'options' ][ 'query_var' ], $reserved_query_vars ) ) {
1667
- $pod[ 'options' ][ 'query_var' ] = $pod[ 'options' ][ 'type' ] . '_' . $pod[ 'options' ][ 'query_var' ];
1668
- }
1669
- }
1670
-
1671
- if ( strlen( $pod[ 'label' ] ) < 1 )
1672
- $pod[ 'label' ] = $pod[ 'name' ];
1673
-
1674
- if ( 'post_type' == $pod[ 'type' ] ) {
1675
- // Max length for post types are 20 characters
1676
- $pod[ 'name' ] = substr( $pod[ 'name' ], 0, 20 );
1677
- }
1678
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
1679
- // Max length for taxonomies are 32 characters
1680
- $pod[ 'name' ] = substr( $pod[ 'name' ], 0, 32 );
1681
- }
1682
-
1683
- $params->id = $pod[ 'id' ];
1684
- $params->name = $pod[ 'name' ];
1685
-
1686
- if ( null !== $old_name && $old_name != $params->name && empty( $pod[ 'object' ] ) ) {
1687
- if ( 'post_type' == $pod[ 'type' ] ) {
1688
- $check = get_post_type_object( $params->name );
1689
-
1690
- if ( !empty( $check ) )
1691
- return pods_error( sprintf( __( 'Post Type %s already exists, you cannot rename %s to that', 'pods' ), $params->name, $old_name ), $this );
1692
- }
1693
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
1694
- $check = get_taxonomy( $params->name );
1695
-
1696
- if ( !empty( $check ) )
1697
- return pods_error( sprintf( __( 'Taxonomy %s already exists, you cannot rename %s to that', 'pods' ), $params->name, $old_name ), $this );
1698
- }
1699
- }
1700
-
1701
- $field_table_operation = true;
1702
-
1703
- // Add new pod
1704
- if ( empty( $params->id ) ) {
1705
- if ( strlen( $params->name ) < 1 )
1706
- return pods_error( __( 'Pod name cannot be empty', 'pods' ), $this );
1707
-
1708
- $post_data = array(
1709
- 'post_name' => $pod[ 'name' ],
1710
- 'post_title' => $pod[ 'label' ],
1711
- 'post_content' => $pod[ 'description' ],
1712
- 'post_type' => '_pods_pod',
1713
- 'post_status' => 'publish'
1714
- );
1715
-
1716
- if ( 'pod' == $pod[ 'type' ] && ( !is_array( $pod[ 'fields' ] ) || empty( $pod[ 'fields' ] ) ) ) {
1717
- $pod[ 'fields' ] = array();
1718
-
1719
- $pod[ 'fields' ][ 'name' ] = array(
1720
- 'name' => 'name',
1721
- 'label' => 'Name',
1722
- 'type' => 'text',
1723
- 'options' => array(
1724
- 'required' => '1'
1725
- )
1726
- );
1727
-
1728
- $pod[ 'fields' ][ 'created' ] = array(
1729
- 'name' => 'created',
1730
- 'label' => 'Date Created',
1731
- 'type' => 'datetime',
1732
- 'options' => array(
1733
- 'datetime_format' => 'ymd_slash',
1734
- 'datetime_time_type' => '12',
1735
- 'datetime_time_format' => 'h_mm_ss_A'
1736
- )
1737
- );
1738
-
1739
- $pod[ 'fields' ][ 'modified' ] = array(
1740
- 'name' => 'modified',
1741
- 'label' => 'Date Modified',
1742
- 'type' => 'datetime',
1743
- 'options' => array(
1744
- 'datetime_format' => 'ymd_slash',
1745
- 'datetime_time_type' => '12',
1746
- 'datetime_time_format' => 'h_mm_ss_A'
1747
- )
1748
- );
1749
-
1750
- $pod[ 'fields' ][ 'author' ] = array(
1751
- 'name' => 'author',
1752
- 'label' => 'Author',
1753
- 'type' => 'pick',
1754
- 'pick_object' => 'user',
1755
- 'options' => array(
1756
- 'pick_format_type' => 'single',
1757
- 'pick_format_single' => 'autocomplete',
1758
- 'default_value' => '{@user.ID}'
1759
- )
1760
- );
1761
-
1762
- $pod[ 'fields' ][ 'permalink' ] = array(
1763
- 'name' => 'permalink',
1764
- 'label' => 'Permalink',
1765
- 'type' => 'slug',
1766
- 'description' => 'Leave blank to auto-generate from Name'
1767
- );
1768
-
1769
- if ( !isset( $pod[ 'options' ][ 'pod_index' ] ) )
1770
- $pod[ 'options' ][ 'pod_index' ] = 'name';
1771
- }
1772
-
1773
- $pod = $this->do_hook( 'save_pod_default_pod', $pod, $params, $sanitized, $db );
1774
-
1775
- $field_table_operation = false;
1776
- }
1777
- else {
1778
- $post_data = array(
1779
- 'ID' => $pod[ 'id' ],
1780
- 'post_name' => $pod[ 'name' ],
1781
- 'post_title' => $pod[ 'label' ],
1782
- 'post_content' => $pod[ 'description' ],
1783
- 'post_status' => 'publish'
1784
- );
1785
- }
1786
-
1787
- if ( true === $db ) {
1788
- if ( !has_filter( 'wp_unique_post_slug', array( $this, 'save_slug_fix' ) ) )
1789
- add_filter( 'wp_unique_post_slug', array( $this, 'save_slug_fix' ), 100, 6 );
1790
-
1791
- $conflicted = false;
1792
-
1793
- // Headway compatibility fix
1794
- if ( has_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 ) ) {
1795
- remove_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 );
1796
-
1797
- $conflicted = true;
1798
- }
1799
-
1800
- $params->id = $this->save_wp_object( 'post', $post_data, $pod[ 'options' ], true, true );
1801
-
1802
- if ( $conflicted )
1803
- add_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 );
1804
-
1805
- if ( false === $params->id )
1806
- return pods_error( __( 'Cannot save Pod', 'pods' ), $this );
1807
- }
1808
- elseif ( empty( $params->id ) )
1809
- $params->id = (int) $db;
1810
-
1811
- $pod[ 'id' ] = $params->id;
1812
-
1813
- // Setup / update tables
1814
- if ( 'table' != $pod[ 'type' ] && 'table' == $pod[ 'storage' ] && $old_storage != $pod[ 'storage' ] && $db ) {
1815
- $definitions = array( "`id` BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY" );
1816
-
1817
- $defined_fields = array();
1818
-
1819
- foreach ( $pod[ 'fields' ] as $field ) {
1820
- if ( !is_array( $field ) || !isset( $field[ 'name' ] ) || in_array( $field[ 'name' ], $defined_fields ) )
1821
- continue;
1822
-
1823
- $defined_fields[] = $field[ 'name' ];
1824
-
1825
- if ( !in_array( $field[ 'type' ], $tableless_field_types ) || ( 'pick' == $field[ 'type' ] && in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) ) ) {
1826
- $definition = $this->get_field_definition( $field[ 'type' ], array_merge( $field, pods_var_raw( 'options', $field, array() ) ) );
1827
-
1828
- if ( 0 < strlen( $definition ) )
1829
- $definitions[] = "`{$field['name']}` " . $definition;
1830
- }
1831
- }
1832
-
1833
- pods_query( "DROP TABLE IF EXISTS `@wp_pods_{$params->name}`" );
1834
-
1835
- $result = pods_query( "CREATE TABLE `@wp_pods_{$params->name}` (" . implode( ', ', $definitions ) . ") DEFAULT CHARSET utf8", $this );
1836
-
1837
- if ( empty( $result ) )
1838
- return pods_error( __( 'Cannot add Database Table for Pod', 'pods' ), $this );
1839
-
1840
- }
1841
- elseif ( 'table' != $pod[ 'type' ] && 'table' == $pod[ 'storage' ] && $pod[ 'storage' ] == $old_storage && null !== $old_name && $old_name != $params->name && $db ) {
1842
- $result = pods_query( "ALTER TABLE `@wp_pods_{$old_name}` RENAME `@wp_pods_{$params->name}`", $this );
1843
-
1844
- if ( empty( $result ) )
1845
- return pods_error( __( 'Cannot update Database Table for Pod', 'pods' ), $this );
1846
- }
1847
-
1848
- /**
1849
- * @var $wpdb wpdb
1850
- */
1851
- global $wpdb;
1852
-
1853
- if ( null !== $old_name && $old_name != $params->name && $db ) {
1854
- // Rename items in the DB pointed at the old WP Object names
1855
- if ( 'post_type' == $pod[ 'type' ] && empty( $pod[ 'object' ] ) ) {
1856
- $this->rename_wp_object_type( 'post', $old_name, $params->name );
1857
- }
1858
- elseif ( 'taxonomy' == $pod[ 'type' ] && empty( $pod[ 'object' ] ) ) {
1859
- $this->rename_wp_object_type( 'taxonomy', $old_name, $params->name );
1860
- }
1861
- elseif ( 'comment' == $pod[ 'type' ] && empty( $pod[ 'object' ] ) ) {
1862
- $this->rename_wp_object_type( 'comment', $old_name, $params->name );
1863
- }
1864
- elseif ( 'settings' == $pod[ 'type' ] ) {
1865
- $this->rename_wp_object_type( 'settings', $old_name, $params->name );
1866
- }
1867
-
1868
- // Sync any related fields if the name has changed
1869
- $fields = pods_query( "
1870
- SELECT `p`.`ID`
1871
- FROM `{$wpdb->posts}` AS `p`
1872
- LEFT JOIN `{$wpdb->postmeta}` AS `pm` ON `pm`.`post_id` = `p`.`ID`
1873
- LEFT JOIN `{$wpdb->postmeta}` AS `pm2` ON `pm2`.`post_id` = `p`.`ID`
1874
- WHERE
1875
- `p`.`post_type` = '_pods_field'
1876
- AND `pm`.`meta_key` = 'pick_object'
1877
- AND (
1878
- `pm`.`meta_value` = 'pod'
1879
- OR `pm`.`meta_value` = '" . $pod[ 'type' ] . "'
1880
- )
1881
- AND `pm2`.`meta_key` = 'pick_val'
1882
- AND `pm2`.`meta_value` = '{$old_name}'
1883
- " );
1884
-
1885
- if ( !empty( $fields ) ) {
1886
- foreach ( $fields as $field ) {
1887
- update_post_meta( $field->ID, 'pick_object', $pod[ 'type' ] );
1888
- update_post_meta( $field->ID, 'pick_val', $params->name );
1889
- }
1890
- }
1891
-
1892
- $fields = pods_query( "
1893
- SELECT `p`.`ID`
1894
- FROM `{$wpdb->posts}` AS `p`
1895
- LEFT JOIN `{$wpdb->postmeta}` AS `pm` ON `pm`.`post_id` = `p`.`ID`
1896
- WHERE
1897
- `p`.`post_type` = '_pods_field'
1898
- AND `pm`.`meta_key` = 'pick_object'
1899
- AND (
1900
- `pm`.`meta_value` = 'pod-{$old_name}'
1901
- OR `pm`.`meta_value` = '" . $pod[ 'type' ] . "-{$old_name}'
1902
- )
1903
- " );
1904
-
1905
- if ( !empty( $fields ) ) {
1906
- foreach ( $fields as $field ) {
1907
- update_post_meta( $field->ID, 'pick_object', $pod[ 'type' ] );
1908
- update_post_meta( $field->ID, 'pick_val', $params->name );
1909
- }
1910
- }
1911
- }
1912
-
1913
- // Sync built-in options for post types and taxonomies
1914
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && empty( $pod[ 'object' ] ) && $db ) {
1915
- // Build list of 'built_in' for later
1916
- $built_in = array();
1917
-
1918
- foreach ( $pod[ 'options' ] as $key => $val ) {
1919
- if ( false === strpos( $key, 'built_in_' ) )
1920
- continue;
1921
- elseif ( false !== strpos( $key, 'built_in_post_types_' ) )
1922
- $built_in_type = 'post_type';
1923
- elseif ( false !== strpos( $key, 'built_in_taxonomies_' ) )
1924
- $built_in_type = 'taxonomy';
1925
- else
1926
- continue;
1927
-
1928
- if ( $built_in_type == $pod[ 'type' ] )
1929
- continue;
1930
-
1931
- if ( !isset( $built_in[ $built_in_type ] ) )
1932
- $built_in[ $built_in_type ] = array();
1933
-
1934
- $built_in_object = str_replace( array( 'built_in_post_types_', 'built_in_taxonomies_' ), '', $key );
1935
-
1936
- $built_in[ $built_in_type ][ $built_in_object ] = (int) $val;
1937
- }
1938
-
1939
- $lookup_option = $lookup_built_in = false;
1940
-
1941
- $lookup_name = $pod[ 'name' ];
1942
-
1943
- if ( 'post_type' == $pod[ 'type' ] ) {
1944
- $lookup_option = 'built_in_post_types_' . $lookup_name;
1945
- $lookup_built_in = 'taxonomy';
1946
- }
1947
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
1948
- $lookup_option = 'built_in_taxonomies_' . $lookup_name;
1949
- $lookup_built_in = 'post_type';
1950
- }
1951
-
1952
- if ( !empty( $lookup_option ) && !empty( $lookup_built_in ) && isset( $built_in[ $lookup_built_in ] ) ) {
1953
- foreach ( $built_in[ $lookup_built_in ] as $built_in_object => $val ) {
1954
- $search_val = 1;
1955
-
1956
- if ( 1 == $val )
1957
- $search_val = 0;
1958
-
1959
- $query = "SELECT p.ID FROM {$wpdb->posts} AS p
1960
- LEFT JOIN {$wpdb->postmeta} AS pm ON pm.post_id = p.ID AND pm.meta_key = '{$lookup_option}'
1961
- LEFT JOIN {$wpdb->postmeta} AS pm2 ON pm2.post_id = p.ID AND pm2.meta_key = 'type' AND pm2.meta_value = '{$lookup_built_in}'
1962
- LEFT JOIN {$wpdb->postmeta} AS pm3 ON pm3.post_id = p.ID AND pm3.meta_key = 'object' AND pm3.meta_value = ''
1963
- WHERE p.post_type = '_pods_pod' AND p.post_name = '{$built_in_object}'
1964
- AND pm2.meta_id IS NOT NULL
1965
- AND ( pm.meta_id IS NULL OR pm.meta_value = {$search_val} )";
1966
-
1967
- $results = pods_query( $query );
1968
-
1969
- if ( !empty( $results ) ) {
1970
- foreach ( $results as $the_pod ) {
1971
- delete_post_meta( $the_pod->ID, $lookup_option );
1972
-
1973
- add_post_meta( $the_pod->ID, $lookup_option, $val );
1974
- }
1975
- }
1976
- }
1977
- }
1978
- }
1979
-
1980
- $saved = array();
1981
- $errors = array();
1982
-
1983
- $field_index_change = false;
1984
- $field_index_id = 0;
1985
-
1986
- $id_required = false;
1987
-
1988
- $field_index = pods_var( 'pod_index', $pod[ 'options' ], 'id', null, true );
1989
-
1990
- if ( 'pod' == $pod[ 'type' ] && !empty( $pod[ 'fields' ] ) && isset( $pod[ 'fields' ][ $field_index ] ) )
1991
- $field_index_id = $pod[ 'fields' ][ $field_index ];
1992
-
1993
- if ( isset( $params->fields ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
1994
- $fields = array();
1995
-
1996
- if ( isset( $params->fields ) ) {
1997
- $params->fields = (array) $params->fields;
1998
-
1999
- $weight = 0;
2000
-
2001
- foreach ( $params->fields as $field ) {
2002
- if ( !is_array( $field ) || !isset( $field[ 'name' ] ) )
2003
- continue;
2004
-
2005
- if ( !isset( $field[ 'weight' ] ) ) {
2006
- $field[ 'weight' ] = $weight;
2007
-
2008
- $weight++;
2009
- }
2010
-
2011
- $fields[ $field[ 'name' ] ] = $field;
2012
- }
2013
- }
2014
-
2015
- $weight = 0;
2016
-
2017
- $saved_field_ids = array();
2018
-
2019
- foreach ( $pod[ 'fields' ] as $k => $field ) {
2020
- if ( !empty( $old_id ) && ( !is_array( $field ) || !isset( $field[ 'name' ] ) || !isset( $fields[ $field[ 'name' ] ] ) ) ) {
2021
- // Iterative change handling for setup-edit.php
2022
- if ( !is_array( $field ) && isset( $old_fields[ $k ] ) )
2023
- $saved[ $old_fields[ $k ][ 'name' ] ] = true;
2024
-
2025
- continue;
2026
- }
2027
-
2028
- if ( !empty( $old_id ) )
2029
- $field = array_merge( $field, $fields[ $field[ 'name' ] ] );
2030
-
2031
- $field[ 'pod' ] = $pod;
2032
-
2033
- if ( !isset( $field[ 'weight' ] ) ) {
2034
- $field[ 'weight' ] = $weight;
2035
-
2036
- $weight++;
2037
- }
2038
-
2039
- if ( 0 < $field_index_id && pods_var( 'id', $field ) == $field_index_id )
2040
- $field_index_change = $field[ 'name' ];
2041
-
2042
- if ( 0 < pods_var( 'id', $field ) )
2043
- $id_required = true;
2044
-
2045
- if ( $id_required )
2046
- $field[ 'id_required' ] = true;
2047
-
2048
- $field_data = $field;
2049
-
2050
- $field = $this->save_field( $field_data, $field_table_operation, true, $db );
2051
-
2052
- if ( true !== $db ) {
2053
- $pod[ 'fields' ][ $k ] = $field;
2054
- $saved_field_ids[] = $field[ 'id' ];
2055
- }
2056
- else {
2057
- if ( !empty( $field ) && 0 < $field ) {
2058
- $saved[ $field_data[ 'name' ] ] = true;
2059
- $saved_field_ids[] = $field;
2060
- }
2061
- else
2062
- $errors[] = sprintf( __( 'Cannot save the %s field', 'pods' ), $field_data[ 'name' ] );
2063
- }
2064
- }
2065
-
2066
- if ( true === $db ) {
2067
- foreach ( $old_fields as $field ) {
2068
- if ( isset( $pod[ 'fields' ][ $field[ 'name' ] ] ) || isset( $saved[ $field[ 'name' ] ] ) || in_array( $field[ 'id' ], $saved_field_ids ) )
2069
- continue;
2070
-
2071
- if ( $field[ 'id' ] == $field_index_id )
2072
- $field_index_change = 'id';
2073
- elseif ( $field[ 'name' ] == $field_index )
2074
- $field_index_change = 'id';
2075
-
2076
- $this->delete_field( array(
2077
- 'id' => (int) $field[ 'id' ],
2078
- 'name' => $field[ 'name' ],
2079
- 'pod' => $pod
2080
- ), $field_table_operation );
2081
- }
2082
- }
2083
-
2084
- // Update field index if the name has changed or the field has been removed
2085
- if ( false !== $field_index_change && true === $db )
2086
- update_post_meta( $pod[ 'id' ], 'pod_index', $field_index_change );
2087
- }
2088
-
2089
- if ( !empty( $errors ) )
2090
- return pods_error( $errors, $this );
2091
-
2092
- $this->cache_flush_pods( $pod );
2093
-
2094
- if ( 'post_type' == $pod[ 'type' ] )
2095
- PodsMeta::$post_types[ $pod[ 'id' ] ] = $this->load_pod( array( 'name' => $pod[ 'name' ] ) );
2096
- elseif ( 'taxonomy' == $pod[ 'type' ] )
2097
- PodsMeta::$taxonomies[ $pod[ 'id' ] ] = $this->load_pod( array( 'name' => $pod[ 'name' ] ) );
2098
- elseif ( 'media' == $pod[ 'type' ] )
2099
- PodsMeta::$media[ $pod[ 'id' ] ] = $this->load_pod( array( 'name' => $pod[ 'name' ] ) );
2100
- elseif ( 'user' == $pod[ 'type' ] )
2101
- PodsMeta::$user[ $pod[ 'id' ] ] = $this->load_pod( array( 'name' => $pod[ 'name' ] ) );
2102
- elseif ( 'comment' == $pod[ 'type' ] )
2103
- PodsMeta::$comment[ $pod[ 'id' ] ] = $this->load_pod( array( 'name' => $pod[ 'name' ] ) );
2104
-
2105
- // Register Post Types / Taxonomies post-registration from PodsInit
2106
- if ( !empty( PodsInit::$content_types_registered ) && in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && empty( $pod[ 'object' ] ) ) {
2107
- global $pods_init;
2108
-
2109
- $pods_init->setup_content_types( true );
2110
- }
2111
-
2112
- if ( true === $db )
2113
- return $pod[ 'id' ];
2114
- else
2115
- return $pod;
2116
- }
2117
-
2118
- /**
2119
- * Add or edit a field within a Pod
2120
- *
2121
- * $params['id'] int Field ID (id OR pod_id+pod+name required)
2122
- * $params['pod_id'] int Pod ID (id OR pod_id+pod+name required)
2123
- * $params['pod'] string Pod name (id OR pod_id+pod+name required)
2124
- * $params['name'] string Field name (id OR pod_id+pod+name required)
2125
- * $params['label'] string (optional) Field label
2126
- * $params['type'] string (optional) Field type (avatar, boolean, code, color, currency, date, datetime, email, file, number, paragraph, password, phone, pick, slug, text, time, website, wysiwyg)
2127
- * $params['pick_object'] string (optional) Related Object (for relationships)
2128
- * $params['pick_val'] string (optional) Related Object name (for relationships)
2129
- * $params['sister_id'] int (optional) Related Field ID (for bidirectional relationships)
2130
- * $params['weight'] int (optional) Order in which the field appears
2131
- * $params['options'] array (optional) Options
2132
- *
2133
- * @param array $params An associative array of parameters
2134
- * @param bool $table_operation (optional) Whether or not to handle table operations
2135
- * @param bool $sanitized (optional) Decides wether the params have been sanitized before being passed, will sanitize them if false.
2136
- * @param bool|int $db (optional) Whether to save into the DB or just return field array.
2137
- *
2138
- * @return int|array The field ID or field array (if !$db)
2139
- * @since 1.7.9
2140
- */
2141
- public function save_field ( $params, $table_operation = true, $sanitized = false, $db = true ) {
2142
- /**
2143
- * @var $wpdb wpdb
2144
- */
2145
- global $wpdb;
2146
-
2147
- if ( true !== $db )
2148
- $table_operation = false;
2149
-
2150
- $tableless_field_types = PodsForm::tableless_field_types();
2151
- $simple_tableless_objects = PodsForm::simple_tableless_objects();
2152
-
2153
- $params = (object) $params;
2154
-
2155
- if ( false === $sanitized )
2156
- $params = pods_sanitize( $params );
2157
-
2158
- if ( isset( $params->pod_id ) )
2159
- $params->pod_id = pods_absint( $params->pod_id );
2160
-
2161
- if ( true !== $db )
2162
- $params->pod_id = (int) $db;
2163
-
2164
- $pod = null;
2165
- $save_pod = false;
2166
- $id_required = false;
2167
-
2168
- if ( isset( $params->id_required ) ) {
2169
- unset( $params->id_required );
2170
-
2171
- $id_required = true;
2172
- }
2173
-
2174
- if ( ( !isset( $params->pod ) || empty( $params->pod ) ) && ( !isset( $params->pod_id ) || empty( $params->pod_id ) ) )
2175
- return pods_error( __( 'Pod ID or name is required', 'pods' ), $this );
2176
-
2177
- if ( isset( $params->pod ) && is_array( $params->pod ) ) {
2178
- $pod = $params->pod;
2179
-
2180
- $save_pod = true;
2181
- }
2182
- elseif ( ( !isset( $params->pod_id ) || empty( $params->pod_id ) ) && ( true === $db || 0 < $db ) )
2183
- $pod = $this->load_pod( array( 'name' => $params->pod, 'table_info' => true ) );
2184
- elseif ( !isset( $params->pod ) && ( true === $db || 0 < $db ) )
2185
- $pod = $this->load_pod( array( 'id' => $params->pod_id, 'table_info' => true ) );
2186
- elseif ( true === $db || 0 < $db )
2187
- $pod = $this->load_pod( array( 'id' => $params->pod_id, 'name' => $params->pod, 'table_info' => true ) );
2188
-
2189
- if ( empty( $pod ) && true === $db )
2190
- return pods_error( __( 'Pod not found', 'pods' ), $this );
2191
-
2192
- $params->pod_id = $pod[ 'id' ];
2193
- $params->pod = $pod[ 'name' ];
2194
- $params->pod_data = $pod;
2195
-
2196
- $params->name = pods_clean_name( $params->name, true, ( 'meta' == $pod[ 'storage' ] ? false : true ) );
2197
-
2198
- if ( !isset( $params->id ) )
2199
- $params->id = 0;
2200
-
2201
- if ( empty( $params->name ) )
2202
- return pods_error( 'Pod field name is required', $this );
2203
-
2204
- $field = $this->load_field( $params );
2205
-
2206
- unset( $params->pod_data );
2207
-
2208
- $old_id = $old_name = $old_type = $old_definition = $old_simple = $old_options = $old_sister_id = null;
2209
-
2210
- if ( !empty( $field ) ) {
2211
- $old_id = pods_var( 'id', $field );
2212
- $old_name = pods_clean_name( $field[ 'name' ], true, ( 'meta' == $pod[ 'storage' ] ? false : true ) );
2213
- $old_type = $field[ 'type' ];
2214
- $old_options = $field[ 'options' ];
2215
- $old_sister_id = (int) pods_var( 'sister_id', $old_options, 0 );
2216
-
2217
- $old_simple = ( 'pick' == $old_type && in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) );
2218
-
2219
- if ( isset( $params->name ) && !empty( $params->name ) )
2220
- $field[ 'name' ] = $params->name;
2221
-
2222
- if ( $old_name != $field[ 'name' ] && false !== $this->field_exists( $params ) )
2223
- return pods_error( sprintf( __( 'Field %s already exists, you cannot rename %s to that', 'pods' ), $field[ 'name' ], $old_name ), $this );
2224
-
2225
- if ( ( $id_required || !empty( $params->id ) ) && ( empty( $old_id ) || $old_id != $params->id ) )
2226
- return pods_error( sprintf( __( 'Field %s already exists', 'pods' ), $field[ 'name' ] ), $this );
2227
-
2228
- if ( empty( $params->id ) )
2229
- $params->id = $old_id;
2230
-
2231
- if ( !in_array( $old_type, $tableless_field_types ) || $old_simple ) {
2232
- $definition = $this->get_field_definition( $old_type, array_merge( $field, $old_options ) );
2233
-
2234
- if ( 0 < strlen( $definition ) )
2235
- $old_definition = "`{$old_name}` " . $definition;
2236
- }
2237
- }
2238
- else {
2239
- $field = array(
2240
- 'id' => 0,
2241
- 'pod_id' => $params->pod_id,
2242
- 'name' => $params->name,
2243
- 'label' => $params->name,
2244
- 'description' => '',
2245
- 'type' => 'text',
2246
- 'pick_object' => '',
2247
- 'pick_val' => '',
2248
- 'sister_id' => '',
2249
- 'weight' => null,
2250
- 'options' => array()
2251
- );
2252
- }
2253
-
2254
- // Setup options
2255
- $options = get_object_vars( $params );
2256
-
2257
- $options_ignore = array(
2258
- 'method',
2259
- 'table_info',
2260
- 'attributes',
2261
- 'group',
2262
- 'grouped',
2263
- 'developer_mode',
2264
- 'dependency',
2265
- 'depends-on',
2266
- 'excludes-on'
2267
- );
2268
-
2269
- foreach ( $options_ignore as $ignore ) {
2270
- if ( isset( $options[ $ignore ] ) )
2271
- unset( $options[ $ignore ] );
2272
- }
2273
-
2274
- if ( isset( $options[ 'method' ] ) )
2275
- unset( $options[ 'method' ] );
2276
- elseif ( isset( $options[ 'table_info' ] ) )
2277
- unset( $options[ 'table_info' ] );
2278
-
2279
- $exclude = array(
2280
- 'id',
2281
- 'pod_id',
2282
- 'pod',
2283
- 'name',
2284
- 'label',
2285
- 'description',
2286
- 'type',
2287
- 'pick_object',
2288
- 'pick_val',
2289
- 'sister_id',
2290
- 'weight',
2291
- 'options'
2292
- );
2293
-
2294
- foreach ( $exclude as $k => $exclude_field ) {
2295
- $aliases = array( $exclude_field );
2296
-
2297
- if ( is_array( $exclude_field ) ) {
2298
- $aliases = array_merge( array( $k ), $exclude_field );
2299
- $exclude_field = $k;
2300
- }
2301
-
2302
- foreach ( $aliases as $alias ) {
2303
- if ( isset( $options[ $alias ] ) ) {
2304
- $field[ $exclude_field ] = pods_trim( $options[ $alias ] );
2305
-
2306
- unset( $options[ $alias ] );
2307
- }
2308
- }
2309
- }
2310
-
2311
- if ( strlen( $field[ 'label' ] ) < 1 )
2312
- $field[ 'label' ] = $field[ 'name' ];
2313
-
2314
- $field[ 'options' ][ 'type' ] = $field[ 'type' ];
2315
-
2316
- if ( in_array( $field[ 'options' ][ 'type' ], $tableless_field_types ) ) {
2317
- // Clean up special drop-down in field editor and save out pick_val
2318
- $field[ 'pick_object' ] = pods_var( 'pick_object', $field, '', null, true );
2319
-
2320
- if ( 0 === strpos( $field[ 'pick_object' ], 'pod-' ) ) {
2321
- $field[ 'pick_val' ] = pods_str_replace( 'pod-', '', $field[ 'pick_object' ], 1 );
2322
- $field[ 'pick_object' ] = 'pod';
2323
- }
2324
- elseif ( 0 === strpos( $field[ 'pick_object' ], 'post_type-' ) ) {
2325
- $field[ 'pick_val' ] = pods_str_replace( 'post_type-', '', $field[ 'pick_object' ], 1 );
2326
- $field[ 'pick_object' ] = 'post_type';
2327
- }
2328
- elseif ( 0 === strpos( $field[ 'pick_object' ], 'taxonomy-' ) ) {
2329
- $field[ 'pick_val' ] = pods_str_replace( 'taxonomy-', '', $field[ 'pick_object' ], 1 );
2330
- $field[ 'pick_object' ] = 'taxonomy';
2331
- }
2332
- elseif ( 'table' == $field[ 'pick_object' ] && 0 < strlen( pods_var_raw( 'pick_table', $field[ 'options' ] ) ) ) {
2333
- $field[ 'pick_val' ] = $field[ 'options' ][ 'pick_table' ];
2334
- $field[ 'pick_object' ] = 'table';
2335
- }
2336
- elseif ( false === strpos( $field[ 'pick_object' ], '-' ) && !in_array( $field[ 'pick_object' ], array( 'pod', 'post_type', 'taxonomy' ) ) ) {
2337
- $field[ 'pick_val' ] = '';
2338
- }
2339
- elseif ( 'custom-simple' == $field[ 'pick_object' ] ) {
2340
- $field[ 'pick_val' ] = '';
2341
- }
2342
-
2343
- $field[ 'options' ][ 'pick_object' ] = $field[ 'pick_object' ];
2344
- $field[ 'options' ][ 'pick_val' ] = $field[ 'pick_val' ];
2345
- $field[ 'options' ][ 'sister_id' ] = pods_var( 'sister_id', $field );
2346
-
2347
- unset( $field[ 'pick_object' ] );
2348
- unset( $field[ 'pick_val' ] );
2349
-
2350
- if ( isset( $field[ 'sister_id' ] ) )
2351
- unset( $field[ 'sister_id' ] );
2352
- }
2353
-
2354
- $field[ 'options' ] = array_merge( $field[ 'options' ], $options );
2355
-
2356
- $object_fields = (array) pods_var_raw( 'object_fields', $pod, array(), null, true );
2357
-
2358
- if ( 0 < $old_id && defined( 'PODS_FIELD_STRICT' ) && !PODS_FIELD_STRICT )
2359
- $params->id = $field[ 'id' ] = $old_id;
2360
-
2361
- // Add new field
2362
- if ( !isset( $params->id ) || empty( $params->id ) || empty( $field ) ) {
2363
- if ( $table_operation && in_array( $field[ 'name' ], array( 'created', 'modified' ) ) && !in_array( $field[ 'type' ], array( 'date', 'datetime' ) ) && ( !defined( 'PODS_FIELD_STRICT' ) || PODS_FIELD_STRICT ) )
2364
- return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2365
-
2366
- if ( $table_operation && 'author' == $field[ 'name' ] && 'pick' != $field[ 'type' ] && ( !defined( 'PODS_FIELD_STRICT' ) || PODS_FIELD_STRICT ) )
2367
- return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2368
-
2369
- if ( in_array( $field[ 'name' ], array( 'id', 'ID' ) ) )
2370
- return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2371
-
2372
- foreach ( $object_fields as $object_field => $object_field_opt ) {
2373
- if ( $object_field == $field[ 'name' ] || in_array( $field[ 'name' ], $object_field_opt[ 'alias' ] ) )
2374
- return pods_error( sprintf( __( '%s is reserved for internal WordPress or Pods usage, please try a different name. Also consider what WordPress and Pods provide you built-in.', 'pods' ), $field[ 'name' ] ), $this );
2375
- }
2376
-
2377
- if ( in_array( $field[ 'name' ], array( 'rss' ) ) ) // Reserved post_name values that can't be used as field names
2378
- $field[ 'name' ] .= '2';
2379
-
2380
- if ( 'slug' == $field[ 'type' ] && true === $db ) {
2381
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy', 'user' ) ) )
2382
- return pods_error( __( 'This pod already has an internal WordPress permalink field', 'pods' ), $this );
2383
-
2384
- $slug_field = get_posts( array(
2385
- 'post_type' => '_pods_field',
2386
- 'orderby' => 'menu_order',
2387
- 'order' => 'ASC',
2388
- 'posts_per_page' => 1,
2389
- 'post_parent' => $field[ 'pod_id' ],
2390
- 'meta_query' => array(
2391
- array(
2392
- 'key' => 'type',
2393
- 'value' => 'slug'
2394
- )
2395
- )
2396
- ) );
2397
-
2398
- if ( !empty( $slug_field ) )
2399
- return pods_error( __( 'This pod already has a permalink field', 'pods' ), $this );
2400
- }
2401
-
2402
- // Sink the new field to the bottom of the list
2403
- if ( null === $field[ 'weight' ] ) {
2404
- $field[ 'weight' ] = 0;
2405
-
2406
- $bottom_most_field = get_posts( array(
2407
- 'post_type' => '_pods_field',
2408
- 'orderby' => 'menu_order',
2409
- 'order' => 'DESC',
2410
- 'posts_per_page' => 1,
2411
- 'post_parent' => $field[ 'pod_id' ]
2412
- ) );
2413
-
2414
- if ( !empty( $bottom_most_field ) )
2415
- $field[ 'weight' ] = pods_absint( $bottom_most_field[ 0 ]->menu_order ) + 1;
2416
- }
2417
-
2418
- $field[ 'weight' ] = pods_absint( $field[ 'weight' ] );
2419
-
2420
- $post_data = array(
2421
- 'post_name' => $field[ 'name' ],
2422
- 'post_title' => $field[ 'label' ],
2423
- 'post_content' => $field[ 'description' ],
2424
- 'post_type' => '_pods_field',
2425
- 'post_parent' => $field[ 'pod_id' ],
2426
- 'post_status' => 'publish',
2427
- 'menu_order' => $field[ 'weight' ]
2428
- );
2429
- }
2430
- else {
2431
- if ( in_array( $field[ 'name' ], array( 'id', 'ID' ) ) ) {
2432
- if ( null !== $old_name )
2433
- return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2434
- else
2435
- return pods_error( sprintf( __( '%s is not editable', 'pods' ), $field[ 'name' ] ), $this );
2436
- }
2437
-
2438
- if ( null !== $old_name && $field[ 'name' ] != $old_name && in_array( $field[ 'name' ], array( 'created', 'modified' ) ) && !in_array( $field[ 'type' ], array( 'date', 'datetime' ) ) && ( !defined( 'PODS_FIELD_STRICT' ) || PODS_FIELD_STRICT ) )
2439
- return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2440
-
2441
- if ( null !== $old_name && $field[ 'name' ] != $old_name && 'author' == $field[ 'name' ] && 'pick' != $field[ 'type' ] && ( !defined( 'PODS_FIELD_STRICT' ) || PODS_FIELD_STRICT ) )
2442
- return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2443
-
2444
- foreach ( $object_fields as $object_field => $object_field_opt ) {
2445
- if ( $object_field != $field[ 'name' ] && !in_array( $field[ 'name' ], $object_field_opt[ 'alias' ] ) )
2446
- continue;
2447
-
2448
- if ( null !== $old_name )
2449
- return pods_error( sprintf( __( '%s is reserved for internal WordPress or Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2450
- else
2451
- return pods_error( sprintf( __( '%s is not editable', 'pods' ), $field[ 'name' ] ), $this );
2452
- }
2453
-
2454
- $post_data = array(
2455
- 'ID' => $field[ 'id' ],
2456
- 'post_name' => $field[ 'name' ],
2457
- 'post_title' => $field[ 'label' ],
2458
- 'post_content' => $field[ 'description' ]
2459
- );
2460
-
2461
- if ( null !== $field[ 'weight' ] ) {
2462
- $field[ 'weight' ] = pods_absint( $field[ 'weight' ] );
2463
-
2464
- $post_data[ 'menu_order' ] = $field[ 'weight' ];
2465
- }
2466
- }
2467
-
2468
- if ( true === $db ) {
2469
- if ( !has_filter( 'wp_unique_post_slug', array( $this, 'save_slug_fix' ) ) )
2470
- add_filter( 'wp_unique_post_slug', array( $this, 'save_slug_fix' ), 100, 6 );
2471
-
2472
- $conflicted = false;
2473
-
2474
- // Headway compatibility fix
2475
- if ( has_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 ) ) {
2476
- remove_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 );
2477
-
2478
- $conflicted = true;
2479
- }
2480
-
2481
- $params->id = $this->save_wp_object( 'post', $post_data, $field[ 'options' ], true, true );
2482
-
2483
- if ( $conflicted )
2484
- add_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 );
2485
-
2486
- if ( false === $params->id )
2487
- return pods_error( __( 'Cannot save Field', 'pods' ), $this );
2488
- }
2489
- else
2490
- $params->id = $field[ 'name' ];
2491
-
2492
- $field[ 'id' ] = $params->id;
2493
-
2494
- $simple = ( 'pick' == $field[ 'type' ] && in_array( pods_var( 'pick_object', $field[ 'options' ] ), $simple_tableless_objects ) );
2495
-
2496
- $definition = false;
2497
-
2498
- if ( !in_array( $field[ 'type' ], $tableless_field_types ) || $simple ) {
2499
- $field_definition = $this->get_field_definition( $field[ 'type' ], array_merge( $field, $field[ 'options' ] ) );
2500
-
2501
- if ( 0 < strlen( $field_definition ) )
2502
- $definition = '`' . $field[ 'name' ] . '` ' . $field_definition;
2503
- }
2504
-
2505
- $sister_id = (int) pods_var( 'sister_id', $field[ 'options' ], 0 );
2506
-
2507
- if ( $table_operation && 'table' == $pod[ 'storage' ] && !pods_tableless() ) {
2508
- if ( !empty( $old_id ) ) {
2509
- if ( ( $field[ 'type' ] != $old_type || $old_simple != $simple ) && empty( $definition ) )
2510
- pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` DROP COLUMN `{$old_name}`", false );
2511
- elseif ( 0 < strlen( $definition ) ) {
2512
- if ( $old_name != $field[ 'name' ] || $old_simple != $simple ) {
2513
- $test = false;
2514
-
2515
- if ( 0 < strlen( $old_definition ) )
2516
- $test = pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` CHANGE `{$old_name}` {$definition}", false );
2517
-
2518
- // If the old field doesn't exist, continue to add a new field
2519
- if ( false === $test )
2520
- pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` ADD COLUMN {$definition}", __( 'Cannot create new field', 'pods' ) );
2521
- }
2522
- elseif ( null !== $old_definition && $definition != $old_definition ) {
2523
- $test = pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` CHANGE `{$old_name}` {$definition}", false );
2524
-
2525
- // If the old field doesn't exist, continue to add a new field
2526
- if ( false === $test )
2527
- pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` ADD COLUMN {$definition}", __( 'Cannot create new field', 'pods' ) );
2528
- }
2529
- }
2530
- }
2531
- elseif ( 0 < strlen( $definition ) ) {
2532
- $test = false;
2533
-
2534
- if ( 0 < strlen( $old_definition ) )
2535
- $test = pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` CHANGE `" . $field[ 'name' ] . "` {$definition}", false );
2536
-
2537
- // If the old field doesn't exist, continue to add a new field
2538
- if ( false === $test )
2539
- pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` ADD COLUMN {$definition}", __( 'Cannot create new field', 'pods' ) );
2540
- }
2541
- }
2542
-
2543
- if ( !empty( $old_id ) && 'meta' == $pod[ 'storage' ] && $old_name != $field[ 'name' ] && $pod[ 'meta_table' ] != $pod[ 'table' ] ) {
2544
- $prepare = array(
2545
- $field[ 'name' ],
2546
- $old_name
2547
- );
2548
-
2549
- // Users don't have a type
2550
- if ( !empty( $pod[ 'field_type' ] ) )
2551
- $prepare[] = $pod[ 'name' ];
2552
-
2553
- pods_query( "
2554
- UPDATE `{$pod[ 'meta_table' ]}` AS `m`
2555
- LEFT JOIN `{$pod[ 'table' ]}` AS `t`
2556
- ON `t`.`{$pod[ 'field_id' ]}` = `m`.`{$pod[ 'meta_field_id' ]}`
2557
- SET
2558
- `m`.`{$pod[ 'meta_field_index' ]}` = %s
2559
- WHERE
2560
- `m`.`{$pod[ 'meta_field_index' ]}` = %s
2561
- " . ( !empty( $pod[ 'field_type' ] ) ? " AND `t`.`{$pod[ 'field_type' ]}` = %s" : "" ),
2562
- $prepare
2563
- );
2564
- }
2565
-
2566
- if ( $field[ 'type' ] != $old_type && in_array( $old_type, $tableless_field_types ) ) {
2567
- delete_post_meta( $old_sister_id, 'sister_id' );
2568
-
2569
- if ( true === $db ) {
2570
- pods_query( "
2571
- DELETE pm
2572
- FROM {$wpdb->postmeta} AS pm
2573
- LEFT JOIN {$wpdb->posts} AS p
2574
- ON p.post_type = '_pods_field'
2575
- AND p.ID = pm.post_id
2576
- WHERE
2577
- p.ID IS NOT NULL
2578
- AND pm.meta_key = 'sister_id'
2579
- AND pm.meta_value = %d
2580
- ", array(
2581
- $params->id
2582
- )
2583
- );
2584
-
2585
- if ( !pods_tableless() ) {
2586
- pods_query( "DELETE FROM @wp_podsrel WHERE `field_id` = {$params->id}", false );
2587
-
2588
- pods_query( "
2589
- UPDATE `@wp_podsrel`
2590
- SET `related_field_id` = 0
2591
- WHERE `field_id` = %d
2592
- ", array(
2593
- $old_sister_id
2594
- )
2595
- );
2596
- }
2597
- }
2598
- }
2599
- elseif ( 0 < $sister_id ) {
2600
- update_post_meta( $sister_id, 'sister_id', $params->id );
2601
-
2602
- if ( true === $db && ( !pods_tableless() ) ) {
2603
- pods_query( "
2604
- UPDATE `@wp_podsrel`
2605
- SET `related_field_id` = %d
2606
- WHERE `field_id` = %d
2607
- ",
2608
- array(
2609
- $params->id,
2610
- $sister_id
2611
- )
2612
- );
2613
- }
2614
- }
2615
- elseif ( 0 < $old_sister_id ) {
2616
- delete_post_meta( $old_sister_id, 'sister_id' );
2617
-
2618
- if ( true === $db && ( !pods_tableless() ) ) {
2619
- pods_query( "
2620
- UPDATE `@wp_podsrel`
2621
- SET `related_field_id` = 0
2622
- WHERE `field_id` = %d
2623
- ", array(
2624
- $old_sister_id
2625
- )
2626
- );
2627
- }
2628
- }
2629
-
2630
- if ( !empty( $old_id ) && $old_name != $field[ 'name' ] && true === $db ) {
2631
- pods_query( "
2632
- UPDATE `@wp_postmeta`
2633
- SET `meta_value` = %s
2634
- WHERE
2635
- `post_id` = %d
2636
- AND `meta_key` = 'pod_index'
2637
- AND `meta_value` = %s
2638
- ", array(
2639
- $field[ 'name' ],
2640
- $pod[ 'id' ],
2641
- $old_name
2642
- )
2643
- );
2644
- }
2645
-
2646
- if ( !$save_pod )
2647
- $this->cache_flush_pods( $pod );
2648
- else {
2649
- pods_transient_clear( 'pods_field_' . $pod[ 'name' ] . '_' . $field[ 'name' ] );
2650
-
2651
- if ( !empty( $old_id ) && $old_name != $field[ 'name' ] )
2652
- pods_transient_clear( 'pods_field_' . $pod[ 'name' ] . '_' . $old_name );
2653
- }
2654
-
2655
- if ( true === $db )
2656
- return $params->id;
2657
- else
2658
- return $field;
2659
- }
2660
-
2661
- /**
2662
- * Fix Pod / Field post_name to ensure they are exactly as saved (allow multiple posts w/ same post_name)
2663
- *
2664
- * @param string $slug Unique slug value
2665
- * @param int $post_ID Post ID
2666
- * @param string $post_status Post Status
2667
- * @param string $post_type Post Type
2668
- * @param int $post_parent Post Parent ID
2669
- * @param string $original_slug Original slug value
2670
- *
2671
- * @return string Final slug value
2672
- *
2673
- * @since 2.3.3
2674
- */
2675
- public function save_slug_fix ( $slug, $post_ID, $post_status, $post_type, $post_parent = 0, $original_slug = null ) {
2676
- if ( in_array( $post_type, array( '_pods_field', '_pods_pod' ) ) && false !== strpos( $slug, '-' ) )
2677
- $slug = $original_slug;
2678
-
2679
- return $slug;
2680
- }
2681
-
2682
- /**
2683
- * Add or Edit a Pods Object
2684
- *
2685
- * $params['id'] int The Object ID
2686
- * $params['name'] string The Object name
2687
- * $params['type'] string The Object type
2688
- * $params['options'] Associative array of Object options
2689
- *
2690
- * @param array|object $params An associative array of parameters
2691
- * @param bool $sanitized (optional) Decides whether the params have been sanitized before being passed, will sanitize them if false.
2692
- *
2693
- * @return int The Object ID
2694
- * @since 2.0
2695
- */
2696
- public function save_object ( $params, $sanitized = false ) {
2697
- $params = (object) $params;
2698
-
2699
- if ( false === $sanitized )
2700
- $params = pods_sanitize( $params );
2701
-
2702
- if ( !isset( $params->name ) || empty( $params->name ) )
2703
- return pods_error( __( 'Name must be given to save an Object', 'pods' ), $this );
2704
-
2705
- if ( !isset( $params->type ) || empty( $params->type ) )
2706
- return pods_error( __( 'Type must be given to save an Object', 'pods' ), $this );
2707
-
2708
- $object = array(
2709
- 'id' => 0,
2710
- 'name' => $params->name,
2711
- 'type' => $params->type,
2712
- 'code' => '',
2713
- 'options' => array()
2714
- );
2715
-
2716
- // Setup options
2717
- $options = get_object_vars( $params );
2718
-
2719
- if ( isset( $options[ 'method' ] ) )
2720
- unset( $options[ 'method' ] );
2721
-
2722
- $exclude = array(
2723
- 'id',
2724
- 'name',
2725
- 'helper_type',
2726
- 'code',
2727
- 'options',
2728
- 'status'
2729
- );
2730
-
2731
- foreach ( $exclude as $k => $exclude_field ) {
2732
- $aliases = array( $exclude_field );
2733
-
2734
- if ( is_array( $exclude_field ) ) {
2735
- $aliases = array_merge( array( $k ), $exclude_field );
2736
- $exclude_field = $k;
2737
- }
2738
-
2739
- foreach ( $aliases as $alias ) {
2740
- if ( isset( $options[ $alias ] ) ) {
2741
- $object[ $exclude_field ] = pods_trim( $options[ $alias ] );
2742
-
2743
- unset( $options[ $alias ] );
2744
- }
2745
- }
2746
- }
2747
-
2748
- if ( 'helper' == $object[ 'type' ] )
2749
- $object[ 'options' ][ 'helper_type' ] = $object[ 'helper_type' ];
2750
-
2751
- if ( isset( $object[ 'options' ][ 'code' ] ) )
2752
- unset( $object[ 'options' ][ 'code' ] );
2753
-
2754
- $object[ 'options' ] = array_merge( $object[ 'options' ], $options );
2755
-
2756
- $post_data = array(
2757
- 'post_name' => pods_clean_name( $object[ 'name' ], true),
2758
- 'post_title' => $object[ 'name' ],
2759
- 'post_content' => $object[ 'code' ],
2760
- 'post_type' => '_pods_' . $object[ 'type' ],
2761
- 'post_status' => 'publish'
2762
- );
2763
-
2764
- if ( !empty( $object[ 'id' ] ) )
2765
- $post_data[ 'ID' ] = $object[ 'id' ];
2766
-
2767
- if ( null !== pods_var( 'status', $object, null, null, true ) )
2768
- $post_data[ 'post_status' ] = pods_var( 'status', $object, null, null, true );
2769
-
2770
- remove_filter( 'content_save_pre', 'balanceTags', 50 );
2771
-
2772
- $post_data = pods_sanitize( $post_data );
2773
-
2774
- $params->id = $this->save_post( $post_data, $object[ 'options' ], true, true );
2775
-
2776
- pods_transient_clear( 'pods_objects_' . $params->type );
2777
- pods_transient_clear( 'pods_objects_' . $params->type . '_get' );
2778
-
2779
- return $params->id;
2780
- }
2781
-
2782
- /**
2783
- * @see PodsAPI::save_object
2784
- *
2785
- * Add or edit a Pod Template
2786
- *
2787
- * $params['id'] int The template ID
2788
- * $params['name'] string The template name
2789
- * $params['code'] string The template code
2790
- *
2791
- * @param array|object $params An associative array of parameters
2792
- * @param bool $sanitized (optional) Decides wether the params have been sanitized before being passed, will sanitize them if false.
2793
- *
2794
- * @return int The Template ID
2795
- *
2796
- * @since 1.7.9
2797
- */
2798
- public function save_template ( $params, $sanitized = false ) {
2799
- $params = (object) $params;
2800
-
2801
- $params->type = 'template';
2802
-
2803
- return $this->save_object( $params, $sanitized );
2804
- }
2805
-
2806
- /**
2807
- * @see PodsAPI::save_object
2808
- *
2809
- * Add or edit a Pod Page
2810
- *
2811
- * $params['id'] int The page ID
2812
- * $params['name'] string The page URI
2813
- * $params['code'] string The page code
2814
- *
2815
- * @param array|object $params An associative array of parameters
2816
- * @param bool $sanitized (optional) Decides wether the params have been sanitized before being passed, will sanitize them if false.
2817
- *
2818
- * @return int The page ID
2819
- * @since 1.7.9
2820
- */
2821
- public function save_page ( $params, $sanitized = false ) {
2822
- $params = (object) $params;
2823
-
2824
- if ( !isset( $params->name ) ) {
2825
- $params->name = $params->uri;
2826
- unset( $params->uri );
2827
- }
2828
-
2829
- if ( isset( $params->phpcode ) ) {
2830
- $params->code = $params->phpcode;
2831
- unset( $params->phpcode );
2832
- }
2833
-
2834
- $params->name = trim( $params->name, '/' );
2835
- $params->type = 'page';
2836
-
2837
- return $this->save_object( $params, $sanitized );
2838
- }
2839
-
2840
- /**
2841
- * @see PodsAPI::save_object
2842
- *
2843
- * Add or edit a Pod Helper
2844
- *
2845
- * $params['id'] int The helper ID
2846
- * $params['name'] string The helper name
2847
- * $params['helper_type'] string The helper type ("pre_save", "display", etc)
2848
- * $params['code'] string The helper code
2849
- *
2850
- * @param array $params An associative array of parameters
2851
- * @param bool $sanitized (optional) Decides wether the params have been sanitized before being passed, will sanitize them if false.
2852
- *
2853
- * @return int The helper ID
2854
- * @since 1.7.9
2855
- */
2856
- public function save_helper ( $params, $sanitized = false ) {
2857
- $params = (object) $params;
2858
-
2859
- if ( isset( $params->phpcode ) ) {
2860
- $params->code = $params->phpcode;
2861
- unset( $params->phpcode );
2862
- }
2863
-
2864
- if ( isset( $params->type ) ) {
2865
- $params->helper_type = $params->type;
2866
- unset( $params->type );
2867
- }
2868
-
2869
- $params->type = 'helper';
2870
-
2871
- return $this->save_object( $params, $sanitized );
2872
- }
2873
-
2874
- /**
2875
- * Add or edit a single pod item
2876
- *
2877
- * $params['pod'] string The Pod name (pod or pod_id is required)
2878
- * $params['pod_id'] string The Pod ID (pod or pod_id is required)
2879
- * $params['id'] int The item ID
2880
- * $params['data'] array (optional) Associative array of field names + values
2881
- * $params['bypass_helpers'] bool Set to true to bypass running pre-save and post-save helpers
2882
- * $params['track_changed_fields'] bool Set to true to enable tracking of saved fields via PodsAPI::get_changed_fields()
2883
- *
2884
- * @param array|object $params An associative array of parameters
2885
- *
2886
- * @return int The item ID
2887
- *
2888
- * @since 1.7.9
2889
- */
2890
- public function save_pod_item ( $params ) {
2891
-
2892
- global $wpdb;
2893
-
2894
- $params = (object) pods_str_replace( '@wp_', '{prefix}', $params );
2895
-
2896
- $tableless_field_types = PodsForm::tableless_field_types();
2897
- $repeatable_field_types = PodsForm::repeatable_field_types();
2898
- $simple_tableless_objects = PodsForm::simple_tableless_objects();
2899
-
2900
- // @deprecated 2.0
2901
- if ( isset( $params->datatype ) ) {
2902
- pods_deprecated( '$params->pod instead of $params->datatype', '2.0' );
2903
-
2904
- $params->pod = $params->datatype;
2905
-
2906
- unset( $params->datatype );
2907
-
2908
- if ( isset( $params->pod_id ) ) {
2909
- pods_deprecated( '$params->id instead of $params->pod_id', '2.0' );
2910
-
2911
- $params->id = $params->pod_id;
2912
-
2913
- unset( $params->pod_id );
2914
- }
2915
-
2916
- if ( isset( $params->data ) && !empty( $params->data ) && is_array( $params->data ) ) {
2917
- $check = current( $params->data );
2918
-
2919
- if ( is_array( $check ) ) {
2920
- pods_deprecated( 'PodsAPI::save_pod_items', '2.0' );
2921
-
2922
- return $this->save_pod_items( $params, $params->data );
2923
- }
2924
- }
2925
- }
2926
-
2927
- // @deprecated 2.0
2928
- if ( isset( $params->tbl_row_id ) ) {
2929
- pods_deprecated( '$params->id instead of $params->tbl_row_id', '2.0' );
2930
-
2931
- $params->id = $params->tbl_row_id;
2932
-
2933
- unset( $params->tbl_row_id );
2934
- }
2935
-
2936
- // @deprecated 2.0
2937
- if ( isset( $params->columns ) ) {
2938
- pods_deprecated( '$params->data instead of $params->columns', '2.0' );
2939
-
2940
- $params->data = $params->columns;
2941
-
2942
- unset( $params->columns );
2943
- }
2944
-
2945
- if ( !isset( $params->pod ) )
2946
- $params->pod = false;
2947
- if ( isset( $params->pod_id ) )
2948
- $params->pod_id = pods_absint( $params->pod_id );
2949
- else
2950
- $params->pod_id = 0;
2951
-
2952
- if ( isset( $params->id ) )
2953
- $params->id = pods_absint( $params->id );
2954
- else
2955
- $params->id = 0;
2956
-
2957
- if ( !isset( $params->from ) )
2958
- $params->from = 'save';
2959
-
2960
- if ( !isset( $params->location ) )
2961
- $params->location = null;
2962
-
2963
- if ( !isset( $params->track_changed_fields ) )
2964
- $params->track_changed_fields = false;
2965
-
2966
- /**
2967
- * Override $params['track_changed_fields']
2968
- *
2969
- * Use for globally setting field change tracking.
2970
- *
2971
- * @param bool
2972
- *
2973
- * @since 2.3.19
2974
- */
2975
- $track_changed_fields = apply_filters( 'pods_api_save_pod_item_track_changed_fields_' . $params->pod, (boolean) $params->track_changed_fields, $params );
2976
- $changed_fields = array();
2977
-
2978
- if ( !isset( $params->clear_slug_cache ) ) {
2979
- $params->clear_slug_cache = true;
2980
- }
2981
-
2982
- // Support for bulk edit
2983
- if ( isset( $params->id ) && !empty( $params->id ) && is_array( $params->id ) ) {
2984
- $ids = array();
2985
- $new_params = $params;
2986
-
2987
- foreach ( $params->id as $id ) {
2988
- $new_params->id = $id;
2989
-
2990
- $ids[] = $this->save_pod_item( $new_params );
2991
- }
2992
-
2993
- return $ids;
2994
- }
2995
-
2996
- // Allow Helpers to know what's going on, are we adding or saving?
2997
- $is_new_item = false;
2998
-
2999
- if ( empty( $params->id ) )
3000
- $is_new_item = true;
3001
-
3002
- if ( isset( $params->is_new_item ) )
3003
- $is_new_item = (boolean) $params->is_new_item;
3004
-
3005
- // Allow Helpers to bypass subsequent helpers in recursive save_pod_item calls
3006
- $bypass_helpers = false;
3007
-
3008
- if ( isset( $params->bypass_helpers ) && false !== $params->bypass_helpers )
3009
- $bypass_helpers = true;
3010
-
3011
- // Allow Custom Fields not defined by Pods to be saved
3012
- $allow_custom_fields = false;
3013
-
3014
- if ( isset( $params->allow_custom_fields ) && false !== $params->allow_custom_fields )
3015
- $allow_custom_fields = true;
3016
-
3017
- // Get array of Pods
3018
- $pod = $this->load_pod( array( 'id' => $params->pod_id, 'name' => $params->pod, 'table_info' => true ) );
3019
-
3020
- if ( false === $pod )
3021
- return pods_error( __( 'Pod not found', 'pods' ), $this );
3022
-
3023
- $params->pod = $pod[ 'name' ];
3024
- $params->pod_id = $pod[ 'id' ];
3025
-
3026
- if ( 'settings' == $pod[ 'type' ] )
3027
- $params->id = $pod[ 'id' ];
3028
-
3029
- $fields = $pod[ 'fields' ];
3030
-
3031
- $object_fields = (array) pods_var_raw( 'object_fields', $pod, array(), null, true );
3032
-
3033
- $fields_active = array();
3034
- $custom_data = array();
3035
-
3036
- // Find the active fields (loop through $params->data to retain order)
3037
- if ( !empty( $params->data ) && is_array( $params->data ) ) {
3038
- $custom_fields = array();
3039
-
3040
- foreach ( $params->data as $field => $value ) {
3041
- if ( isset( $object_fields[ $field ] ) ) {
3042
- $object_fields[ $field ][ 'value' ] = $value;
3043
- $fields_active[] = $field;
3044
- }
3045
- elseif ( isset( $fields[ $field ] ) ) {
3046
- if ( 'save' == $params->from || true === PodsForm::permission( $fields[ $field ][ 'type' ], $field, $fields[ $field ], $fields, $pod, $params->id, $params ) ) {
3047
- $fields[ $field ][ 'value' ] = $value;
3048
- $fields_active[] = $field;
3049
- }
3050
- elseif ( !pods_has_permissions( $fields[ $field ][ 'options' ] ) && pods_var( 'hidden', $fields[ $field ][ 'options' ], false ) ) {
3051
- $fields[ $field ][ 'value' ] = $value;
3052
- $fields_active[] = $field;
3053
- }
3054
- }
3055
- else {
3056
- $found = false;
3057
-
3058
- foreach ( $object_fields as $object_field => $object_field_opt ) {
3059
- if ( in_array( $field, $object_field_opt[ 'alias' ] ) ) {
3060
- $object_fields[ $object_field ][ 'value' ] = $value;
3061
- $fields_active[] = $object_field;
3062
-
3063
- $found = true;
3064
-
3065
- break;
3066
- }
3067
- }
3068
-
3069
- if ( $allow_custom_fields && !$found )
3070
- $custom_fields[] = $field;
3071
- }
3072
- }
3073
-
3074
- if ( $allow_custom_fields && !empty( $custom_fields ) ) {
3075
- foreach ( $custom_fields as $field ) {
3076
- $custom_data[ $field ] = $params->data[ $field ];
3077
- }
3078
- }
3079
-
3080
- if ( $pod[ 'type' ] === 'taxonomy' && isset( $params->data ) && !empty( $params->data ) ) {
3081
- $term_data = $params->data;
3082
- }
3083
-
3084
- unset( $params->data );
3085
- }
3086
-
3087
- if ( empty( $params->id ) && !in_array( 'created', $fields_active ) && isset( $fields[ 'created' ] ) && in_array( $fields[ 'created' ][ 'type' ], array( 'date', 'datetime' ) ) ) {
3088
- $fields[ 'created' ][ 'value' ] = current_time( 'mysql' );
3089
- $fields_active[] = 'created';
3090
- }
3091
-
3092
- if ( !in_array( 'modified', $fields_active ) && isset( $fields[ 'modified' ] ) && in_array( $fields[ 'modified' ][ 'type' ], array( 'date', 'datetime' ) ) ) {
3093
- $fields[ 'modified' ][ 'value' ] = current_time( 'mysql' );
3094
- $fields_active[] = 'modified';
3095
- }
3096
-
3097
- if ( in_array( $pod[ 'type' ], array( 'pod', 'table' ) ) && empty( $params->id ) && !empty( $pod[ 'pod_field_index' ] ) && in_array( $pod[ 'pod_field_index' ], $fields_active ) && !in_array( $pod[ 'pod_field_slug' ], $fields_active ) && isset( $fields[ $pod[ 'pod_field_slug' ] ] ) ) {
3098
- $fields[ $pod[ 'pod_field_slug' ] ][ 'value' ] = ''; // this will get picked up by slug pre_save method
3099
- $fields_active[] = $pod[ 'pod_field_slug' ];
3100
- }
3101
-
3102
- // Handle hidden fields
3103
- if ( empty( $params->id ) ) {
3104
- foreach ( $fields as $field => $field_data ) {
3105
- if ( in_array( $field, $fields_active ) )
3106
- continue;
3107
-
3108
- if ( in_array( $params->from, array( 'save', 'process_form' ) ) || true === PodsForm::permission( $fields[ $field ][ 'type' ], $field, $fields[ $field ], $fields, $pod, $params->id, $params ) ) {
3109
- $value = PodsForm::default_value( pods_var_raw( $field, 'post' ), $field_data[ 'type' ], $field, pods_var_raw( 'options', $field_data, $field_data, null, true ), $pod, $params->id );
3110
-
3111
- if ( null !== $value && '' !== $value && false !== $value ) {
3112
- $fields[ $field ][ 'value' ] = $value;
3113
- $fields_active[] = $field;
3114
- }
3115
- }
3116
- }
3117
-
3118
- // Set default field values for object fields
3119
- if ( !empty( $object_fields ) ) {
3120
- foreach ( $object_fields as $field => $field_data ) {
3121
- if ( in_array( $field, $fields_active ) ) {
3122
- continue;
3123
- }
3124
- elseif ( !isset( $field_data[ 'default' ] ) || strlen( $field_data[ 'default' ] ) < 1 ) {
3125
- continue;
3126
- }
3127
-
3128
- $value = PodsForm::default_value( pods_var_raw( $field, 'post' ), $field_data[ 'type' ], $field, pods_var_raw( 'options', $field_data, $field_data, null, true ), $pod, $params->id );
3129
-
3130
- if ( null !== $value && '' !== $value && false !== $value ) {
3131
- $object_fields[ $field ][ 'value' ] = $value;
3132
- $fields_active[] = $field;
3133
- }
3134
- }
3135
- }
3136
-
3137
- // Set default field values for Pod fields
3138
- foreach ( $fields as $field => $field_data ) {
3139
- if ( in_array( $field, $fields_active ) ) {
3140
- continue;
3141
- }
3142
- elseif ( !isset( $field_data[ 'default' ] ) || strlen( $field_data[ 'default' ] ) < 1 ) {
3143
- continue;
3144
- }
3145
-
3146
- $value = PodsForm::default_value( pods_var_raw( $field, 'post' ), $field_data[ 'type' ], $field, pods_var_raw( 'options', $field_data, $field_data, null, true ), $pod, $params->id );
3147
-
3148
- if ( null !== $value && '' !== $value && false !== $value ) {
3149
- $fields[ $field ][ 'value' ] = $value;
3150
- $fields_active[] = $field;
3151
- }
3152
- }
3153
- }
3154
-
3155
- $columns =& $fields; // @deprecated 2.0
3156
- $active_columns =& $fields_active; // @deprecated 2.0
3157
- $params->tbl_row_id =& $params->id; // @deprecated 2.0
3158
-
3159
- $pre_save_helpers = $post_save_helpers = array();
3160
-
3161
- $pieces = array(
3162
- 'fields',
3163
- 'params',
3164
- 'pod',
3165
- 'fields_active',
3166
- 'object_fields',
3167
- 'custom_fields',
3168
- 'custom_data',
3169
- 'track_changed_fields',
3170
- 'changed_fields'
3171
- );
3172
-
3173
- if ( false === $bypass_helpers ) {
3174
- // Plugin hooks
3175
- $hooked = $this->do_hook( 'pre_save_pod_item', compact( $pieces ), $is_new_item, $params->id );
3176
-
3177
- if ( is_array( $hooked ) && !empty( $hooked ) )
3178
- extract( $hooked );
3179
-
3180
- $hooked = $this->do_hook( "pre_save_pod_item_{$params->pod}", compact( $pieces ), $is_new_item, $params->id );
3181
-
3182
- if ( is_array( $hooked ) && !empty( $hooked ) )
3183
- extract( $hooked );
3184
-
3185
- if ( $is_new_item ) {
3186
- $hooked = $this->do_hook( 'pre_create_pod_item', compact( $pieces ) );
3187
-
3188
- if ( is_array( $hooked ) && !empty( $hooked ) )
3189
- extract( $hooked );
3190
-
3191
- $hooked = $this->do_hook( "pre_create_pod_item_{$params->pod}", compact( $pieces ) );
3192
-
3193
- if ( is_array( $hooked ) && !empty( $hooked ) )
3194
- extract( $hooked );
3195
- }
3196
- else {
3197
- $hooked = $this->do_hook( 'pre_edit_pod_item', compact( $pieces ), $params->id );
3198
-
3199
- if ( is_array( $hooked ) && !empty( $hooked ) )
3200
- extract( $hooked );
3201
-
3202
- $hooked = $this->do_hook( "pre_edit_pod_item_{$params->pod}", compact( $pieces ), $params->id );
3203
-
3204
- if ( is_array( $hooked ) && !empty( $hooked ) )
3205
- extract( $hooked );
3206
- }
3207
-
3208
- // Call any pre-save helpers (if not bypassed)
3209
- if ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) {
3210
- if ( !empty( $pod[ 'options' ] ) && is_array( $pod[ 'options' ] ) ) {
3211
- $helpers = array( 'pre_save_helpers', 'post_save_helpers' );
3212
-
3213
- foreach ( $helpers as $helper ) {
3214
- if ( isset( $pod[ 'options' ][ $helper ] ) && !empty( $pod[ 'options' ][ $helper ] ) )
3215
- ${$helper} = explode( ',', $pod[ 'options' ][ $helper ] );
3216
- }
3217
- }
3218
-
3219
- if ( !empty( $pre_save_helpers ) ) {
3220
- pods_deprecated( sprintf( __( 'Pre-save helpers are deprecated, use the action pods_pre_save_pod_item_%s instead', 'pods' ), $params->pod ), '2.0' );
3221
-
3222
- foreach ( $pre_save_helpers as $helper ) {
3223
- $helper = $this->load_helper( array( 'name' => $helper ) );
3224
-
3225
- if ( false !== $helper )
3226
- eval( '?>' . $helper[ 'code' ] );
3227
- }
3228
- }
3229
- }
3230
- }
3231
-
3232
- if ( $track_changed_fields ) {
3233
- $changed_fields = $this->get_changed_fields( compact( $pieces ) );
3234
- }
3235
-
3236
- $table_data = $table_formats = $update_values = $rel_fields = $rel_field_ids = array();
3237
-
3238
- $object_type = $pod[ 'type' ];
3239
-
3240
- $object_ID = 'ID';
3241
-
3242
- if ( 'comment' == $object_type )
3243
- $object_ID = 'comment_ID';
3244
-
3245
- $object_data = $object_meta = $post_term_data = array();
3246
-
3247
- if ( 'settings' == $object_type )
3248
- $object_data[ 'option_id' ] = $pod[ 'name' ];
3249
- elseif ( !empty( $params->id ) )
3250
- $object_data[ $object_ID ] = $params->id;
3251
-
3252
- $fields_active = array_unique( $fields_active );
3253
-
3254
- // Loop through each active field, validating and preparing the table data
3255
- foreach ( $fields_active as $field ) {
3256
- if ( isset( $object_fields[ $field ] ) )
3257
- $field_data = $object_fields[ $field ];
3258
- elseif ( isset( $fields[ $field ] ) )
3259
- $field_data = $fields[ $field ];
3260
- else
3261
- continue;
3262
-
3263
- $value = $field_data[ 'value' ];
3264
- $type = $field_data[ 'type' ];
3265
- $options = pods_var( 'options', $field_data, array() );
3266
-
3267
- // WPML AJAX compatibility
3268
- if ( is_admin() && isset( $_GET[ 'page' ] ) && false !== strpos( $_GET[ 'page' ], '/menu/languages.php' ) && isset( $_POST[ 'icl_ajx_action' ] ) && isset( $_POST[ '_icl_nonce' ] ) && wp_verify_nonce( $_POST[ '_icl_nonce' ], $_POST[ 'icl_ajx_action' ] . '_nonce' ) )
3269
- $options[ 'unique' ] = $fields[ $field ][ 'options' ][ 'unique' ] = $options[ 'required' ] = $fields[ $field ][ 'options' ][ 'required' ] = 0;
3270
- else {
3271
- // Validate value
3272
- $validate = $this->handle_field_validation( $value, $field, $object_fields, $fields, $pod, $params );
3273
-
3274
- if ( false === $validate )
3275
- $validate = sprintf( __( 'There was an issue validating the field %s', 'pods' ), $field_data[ 'label' ] );
3276
- elseif ( true !== $validate )
3277
- $validate = (array) $validate;
3278
-
3279
- if ( !is_bool( $validate ) && !empty( $validate ) )
3280
- return pods_error( $validate, $this );
3281
- }
3282
-
3283
- $value = PodsForm::pre_save( $field_data[ 'type' ], $value, $params->id, $field, array_merge( $field_data, $options ), array_merge( $fields, $object_fields ), $pod, $params );
3284
-
3285
- $field_data[ 'value' ] = $value;
3286
-
3287
- if ( isset( $object_fields[ $field ] ) ) {
3288
- if ( 'taxonomy' == $object_fields[ $field ][ 'type' ] ) {
3289
- $post_term_data[ $field ] = $value;
3290
- }
3291
- else {
3292
- $object_data[ $field ] = $value;
3293
- }
3294
- }
3295
- else {
3296
- $simple = ( 'pick' == $type && in_array( pods_var( 'pick_object', $field_data ), $simple_tableless_objects ) );
3297
- $simple = (boolean) $this->do_hook( 'tableless_custom', $simple, $field_data, $field, $fields, $pod, $params );
3298
-
3299
- // Handle Simple Relationships
3300
- if ( $simple ) {
3301
- if ( !is_array( $value ) )
3302
- $value = explode( ',', $value );
3303
-
3304
- $pick_limit = (int) pods_var_raw( 'pick_limit', $options, 0 );
3305
-
3306
- if ( 'single' == pods_var_raw( 'pick_format_type', $options ) )
3307
- $pick_limit = 1;
3308
-
3309
- if ( 'custom-simple' == pods_var( 'pick_object', $field_data ) ) {
3310
- $custom = pods_var_raw( 'pick_custom', $options, '' );
3311
-
3312
- $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $field_data[ 'name' ], $value, array_merge( $field_data, $options ), $pod, $params->id );
3313
-
3314
- if ( empty( $value ) || empty( $custom ) )
3315
- $value = '';
3316
- elseif ( !empty( $custom ) ) {
3317
- if ( !is_array( $custom ) ) {
3318
- $custom = explode( "\n", $custom );
3319
-
3320
- $custom_values = array();
3321
-
3322
- foreach ( $custom as $c => $cv ) {
3323
- if ( 0 < strlen( $cv ) ) {
3324
- $custom_label = explode( '|', $cv );
3325
-
3326
- if ( !isset( $custom_label[ 1 ] ) )
3327
- $custom_label[ 1 ] = $custom_label[ 0 ];
3328
-
3329
- $custom_label[ 0 ] = trim( (string) $custom_label[ 0 ] );
3330
- $custom_label[ 1 ] = trim( (string) $custom_label[ 1 ] );
3331
- $custom_values[ $custom_label[ 0 ] ] = $custom_label[ 1 ];
3332
- }
3333
- }
3334
- }
3335
- else
3336
- $custom_values = $custom;
3337
-
3338
- $values = array();
3339
-
3340
- foreach ( $value as $k => $v ) {
3341
- $v = pods_unsanitize( $v );
3342
-
3343
- if ( isset( $custom_values[ $v ] ) )
3344
- $values[ $k ] = $v;
3345
- }
3346
-
3347
- $value = $values;
3348
- }
3349
- }
3350
-
3351
- if ( 0 < $pick_limit && !empty( $value ) )
3352
- $value = array_slice( $value, 0, $pick_limit );
3353
-
3354
- // Don't save an empty array, just make it an empty string
3355
- if ( empty( $value ) )
3356
- $value = '';
3357
- elseif ( is_array( $value ) ) {
3358
- // If there's just one item, don't save as an array, save the string
3359
- if ( 1 == $pick_limit || 1 == count( $value ) )
3360
- $value = implode( '', $value );
3361
- // If storage is set to table, json encode, otherwise WP will serialize automatically
3362
- elseif ( 'table' == pods_var( 'storage', $pod ) )
3363
- $value = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $value, JSON_UNESCAPED_UNICODE ) : json_encode( $value );
3364
- }
3365
- }
3366
-
3367
- // Prepare all table / meta data
3368
- if ( !in_array( $type, $tableless_field_types ) || $simple ) {
3369
- if ( in_array( $type, $repeatable_field_types ) && 1 == pods_var( $type . '_repeatable', $field_data, 0 ) ) {
3370
- // Don't save an empty array, just make it an empty string
3371
- if ( empty( $value ) )
3372
- $value = '';
3373
- elseif ( is_array( $value ) ) {
3374
- // If there's just one item, don't save as an array, save the string
3375
- if ( 1 == count( $value ) )
3376
- $value = implode( '', $value );
3377
- // If storage is set to table, json encode, otherwise WP will serialize automatically
3378
- elseif ( 'table' == pods_var( 'storage', $pod ) )
3379
- $value = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $value, JSON_UNESCAPED_UNICODE ) : json_encode( $value );
3380
- }
3381
- }
3382
-
3383
- $table_data[ $field ] = str_replace( array( '{prefix}', '@wp_' ), array( '{/prefix/}', '{prefix}' ), $value ); // Fix for pods_query
3384
- $table_formats[] = PodsForm::prepare( $type, $options );
3385
-
3386
- $object_meta[ $field ] = $value;
3387
- }
3388
- // Store relational field data to be looped through later
3389
- else {
3390
- // Convert values from a comma-separated string into an array
3391
- if ( !is_array( $value ) )
3392
- $value = explode( ',', $value );
3393
-
3394
- $rel_fields[ $type ][ $field ] = $value;
3395
- $rel_field_ids[] = $field_data[ 'id' ];
3396
- }
3397
- }
3398
- }
3399
-
3400
- if ( 'post_type' == $pod[ 'type' ] ) {
3401
- $post_type = $pod[ 'name' ];
3402
-
3403
- if ( !empty( $pod[ 'object' ] ) )
3404
- $post_type = $pod[ 'object' ];
3405
-
3406
- $object_data[ 'post_type' ] = $post_type;
3407
- }
3408
-
3409
- if ( ( 'meta' == $pod[ 'storage' ] || 'settings' == $pod[ 'type' ] ) && !in_array( $pod[ 'type' ], array( 'taxonomy', 'pod', 'table', '' ) ) ) {
3410
- if ( $allow_custom_fields && !empty( $custom_data ) )
3411
- $object_meta = array_merge( $custom_data, $object_meta );
3412
-
3413
- $fields_to_send = array_flip( array_keys( $object_meta ) );
3414
-
3415
- foreach ( $fields_to_send as $field => $field_data ) {
3416
- if ( isset( $object_fields[ $field ] ) ) {
3417
- $field_data = $object_fields[ $field ];
3418
- }
3419
- elseif ( isset( $fields[ $field ] ) ) {
3420
- $field_data = $fields[ $field ];
3421
- }
3422
- else {
3423
- unset( $fields_to_send[ $field ] );
3424
- }
3425
-
3426
- $fields_to_send[ $field ] = $field_data;
3427
- }
3428
-
3429
- $params->id = $this->save_wp_object( $object_type, $object_data, $object_meta, false, true, $fields_to_send );
3430
-
3431
- if ( !empty( $params->id ) && 'settings' == $object_type )
3432
- $params->id = $pod[ 'id' ];
3433
- }
3434
- else {
3435
- if ( !in_array( $pod[ 'type' ], array( 'taxonomy', 'pod', 'table', '' ) ) )
3436
- $params->id = $this->save_wp_object( $object_type, $object_data, array(), false, true );
3437
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
3438
- $term = pods_v( $object_fields[ 'name' ][ 'name' ], $object_data, '', null, true );
3439
- if ( !isset( $term_data ) ) {
3440
- $term_data = array ();
3441
- }
3442
-
3443
- if ( empty( $params->id ) || !empty( $term_data ) ) {
3444
- $taxonomy = $pod[ 'name' ];
3445
-
3446
- if ( !empty( $pod[ 'object' ] ) )
3447
- $taxonomy = $pod[ 'object' ];
3448
-
3449
- $params->id = $this->save_term( $params->id, $term, $taxonomy, $term_data, true );
3450
- }
3451
- }
3452
-
3453
- if ( 'table' == $pod[ 'storage' ] ) {
3454
- // Every row should have an id set here, otherwise Pods with nothing
3455
- // but relationship fields won't get properly ID'd
3456
- if ( empty( $params->id ) )
3457
- $params->id = 0;
3458
-
3459
- $table_data = array( 'id' => $params->id ) + $table_data;
3460
- array_unshift( $table_formats, '%d' );
3461
-
3462
- if ( !empty( $table_data ) ) {
3463
- $sql = pods_data()->insert_on_duplicate( "@wp_pods_{$params->pod}", $table_data, $table_formats );
3464
-
3465
- $id = pods_query( $sql, 'Cannot add/save table row' );
3466
-
3467
- if ( empty( $params->id ) )
3468
- $params->id = $id;
3469
- }
3470
- }
3471
- }
3472
-
3473
- $params->id = (int) $params->id;
3474
-
3475
- // Save terms for taxonomies associated to a post type
3476
- if ( 0 < $params->id && 'post_type' == $pod[ 'type' ] && !empty( $post_term_data ) ) {
3477
- foreach ( $post_term_data as $post_taxonomy => $post_terms ) {
3478
- $post_terms = (array) $post_terms;
3479
-
3480
- foreach ( $post_terms as $k => $v ) {
3481
- if ( ! preg_match( '/[^0-9]/', $v ) ) {
3482
- $v = (int) $v;
3483
- }
3484
-
3485
- $post_terms[ $k ] = $v;
3486
- }
3487
-
3488
- wp_set_object_terms( $params->id, $post_terms, $post_taxonomy );
3489
- }
3490
- }
3491
-
3492
- $no_conflict = pods_no_conflict_check( $pod[ 'type' ] );
3493
-
3494
- if ( !$no_conflict )
3495
- pods_no_conflict_on( $pod[ 'type' ] );
3496
-
3497
- // Save relationship / file data
3498
- if ( !empty( $rel_fields ) ) {
3499
- foreach ( $rel_fields as $type => $data ) {
3500
- // Only handle tableless fields
3501
- if ( !in_array( $type, $tableless_field_types ) ) {
3502
- continue;
3503
- }
3504
-
3505
- foreach ( $data as $field => $values ) {
3506
- $pick_val = pods_var( 'pick_val', $fields[ $field ] );
3507
-
3508
- if ( 'table' == pods_var( 'pick_object', $fields[ $field ] ) ) {
3509
- $pick_val = pods_var( 'pick_table', $fields[ $field ][ 'options' ], $pick_val, null, true );
3510
- }
3511
-
3512
- if ( '__current__' == $pick_val ) {
3513
- if ( is_object( $pod ) ) {
3514
- $pick_val = $pod->pod;
3515
- }
3516
- elseif ( is_array( $pod ) ) {
3517
- $pick_val = $pod[ 'name' ];
3518
- }
3519
- elseif ( 0 < strlen( $pod ) ) {
3520
- $pick_val = $pod;
3521
- }
3522
- }
3523
-
3524
- $fields[ $field ][ 'options' ][ 'table_info' ] = pods_api()->get_table_info( pods_var( 'pick_object', $fields[ $field ] ), $pick_val, null, null, $fields[ $field ][ 'options' ] );
3525
-
3526
- if ( isset( $fields[ $field ][ 'options' ][ 'table_info' ][ 'pod' ] ) && !empty( $fields[ $field ][ 'options' ][ 'table_info' ][ 'pod' ] ) && isset( $fields[ $field ][ 'options' ][ 'table_info' ][ 'pod' ][ 'name' ] ) ) {
3527
- $search_data = pods( $fields[ $field ][ 'options' ][ 'table_info' ][ 'pod' ][ 'name' ] );
3528
-
3529
- $data_mode = 'pods';
3530
- }
3531
- else {
3532
- $search_data = pods_data();
3533
- $search_data->table( $fields[ $field ][ 'options' ][ 'table_info' ] );
3534
-
3535
- $data_mode = 'data';
3536
- }
3537
-
3538
- $find_rel_params = array(
3539
- 'select' => "`t`.`{$search_data->field_id}`",
3540
- 'where' => "`t`.`{$search_data->field_slug}` = %s OR `t`.`{$search_data->field_index}` = %s",
3541
- 'limit' => 1,
3542
- 'pagination' => false,
3543
- 'search' => false
3544
- );
3545
-
3546
- if ( empty( $search_data->field_slug ) && !empty( $search_data->field_index ) ) {
3547
- $find_rel_params[ 'where' ] = "`t`.`{$search_data->field_index}` = %s";
3548
- }
3549
- elseif ( empty( $search_data->field_slug ) && empty( $search_data->field_index ) ) {
3550
- $find_rel_params = false;
3551
- }
3552
-
3553
- $related_limit = (int) pods_var_raw( $type . '_limit', $fields[ $field ][ 'options' ], 0 );
3554
-
3555
- if ( 'single' == pods_var_raw( $type . '_format_type', $fields[ $field ][ 'options' ] ) ) {
3556
- $related_limit = 1;
3557
- }
3558
-
3559
- // Enforce integers / unique values for IDs
3560
- $value_ids = array();
3561
-
3562
- $is_file_field = in_array( $type, PodsForm::file_field_types() );
3563
- $is_taggable = ( in_array( $type, PodsForm::tableless_field_types() ) && 1 == pods_v( $type . '_taggable', $fields[ $field ][ 'options' ] ) );
3564
-
3565
- // @todo Handle simple relationships eventually
3566
- foreach ( $values as $v ) {
3567
- if ( !empty( $v ) ) {
3568
- if ( !is_array( $v ) ) {
3569
- if ( !preg_match( '/[^0-9]/', $v ) ) {
3570
- $v = (int) $v;
3571
- }
3572
- // File handling
3573
- elseif ( $is_file_field ) {
3574
- // Get ID from GUID
3575
- $v = pods_image_id_from_field( $v );
3576
-
3577
- // If file not found, add it
3578
- if ( empty( $v ) ) {
3579
- $v = pods_attachment_import( $v );
3580
- }
3581
- }
3582
- // Reference by slug
3583
- else {
3584
- $v_data = false;
3585
-
3586
- if ( false !== $find_rel_params ) {
3587
- $rel_params = $find_rel_params;
3588
- $rel_params[ 'where' ] = $wpdb->prepare( $rel_params[ 'where' ], array( $v, $v ) );
3589
-
3590
- $search_data->select( $rel_params );
3591
-
3592
- $v_data = $search_data->fetch( $v );
3593
- }
3594
-
3595
- if ( !empty( $v_data ) && isset( $v_data[ $search_data->field_id ] ) ) {
3596
- $v = (int) $v_data[ $search_data->field_id ];
3597
- }
3598
- // Allow tagging for Pods objects
3599
- elseif ( $is_taggable && 'pods' == $data_mode ) {
3600
- $tag_data = array(
3601
- $search_data->field_index => $v
3602
- );
3603
-
3604
- if ( 'post_type' == $search_data->pod_data[ 'type' ] ) {
3605
- $tag_data[ 'post_status' ] = 'publish';
3606
- }
3607
-
3608
- /**
3609
- * Filter for changing tag before adding new item.
3610
- *
3611
- * @param array $tag_data Fields for creating new item.
3612
- * @param int $v Field ID of tag.
3613
- * @param obj $search_data Search object for tag.
3614
- * @param string $field Table info for field.
3615
- * @param array $pieces Field array.
3616
- *
3617
- * @since 2.3.19
3618
- */
3619
- $tag_data = apply_filters( 'pods_api_save_pod_item_taggable_data', $tag_data, $v, $search_data, $field, compact( $pieces ) );
3620
-
3621
- // Save $v to a new item on related object
3622
- $v = $search_data->add( $tag_data );
3623
-
3624
- // @todo Support non-Pods for tagging
3625
- }
3626
- }
3627
- }
3628
- elseif ( $is_file_field && isset( $v[ 'id' ] ) ) {
3629
- $v = (int) $v[ 'id' ];
3630
- }
3631
- else {
3632
- continue;
3633
- }
3634
-
3635
- if ( !empty( $v ) && !in_array( $v, $value_ids ) ) {
3636
- $value_ids[] = $v;
3637
- }
3638
- }
3639
- }
3640
-
3641
- $value_ids = array_unique( array_filter( $value_ids ) );
3642
-
3643
- // Limit values
3644
- if ( 0 < $related_limit && !empty( $value_ids ) )
3645
- $value_ids = array_slice( $value_ids, 0, $related_limit );
3646
-
3647
- // Get current values
3648
- if ( 'pick' == $type && isset( PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ] ) && isset( PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ][ 'current_ids' ] ) )
3649
- $related_ids = PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ][ 'current_ids' ];
3650
- else
3651
- $related_ids = $this->lookup_related_items( $fields[ $field ][ 'id' ], $pod[ 'id' ], $params->id, $fields[ $field ], $pod );
3652
-
3653
- // Get ids to remove
3654
- $remove_ids = array_diff( $related_ids, $value_ids );
3655
-
3656
- // Delete relationships
3657
- if ( !empty( $remove_ids ) )
3658
- $this->delete_relationships( $params->id, $remove_ids, $pod, $fields[ $field ] );
3659
-
3660
- // Save relationships
3661
- if ( !empty( $value_ids ) )
3662
- $this->save_relationships( $params->id, $value_ids, $pod, $fields[ $field ] );
3663
-
3664
- // Run save function for field type (where needed)
3665
- PodsForm::save( $type, $values, $params->id, $field, array_merge( $fields[ $field ], $fields[ $field ][ 'options' ] ), array_merge( $fields, $object_fields ), $pod, $params );
3666
- }
3667
-
3668
- // Unset data no longer needed
3669
- if ( 'pick' == $type ) {
3670
- foreach ( $data as $field => $values ) {
3671
- if ( isset( PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ] ) ) {
3672
- unset( PodsField_Pick::$related_data[ PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ][ 'related_field' ][ 'id' ] ] );
3673
- unset( PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ] );
3674
- }
3675
- }
3676
- }
3677
- }
3678
- }
3679
-
3680
- if ( !$no_conflict )
3681
- pods_no_conflict_off( $pod[ 'type' ] );
3682
-
3683
- if ( false === $bypass_helpers ) {
3684
- $pieces = compact( $pieces );
3685
-
3686
- // Plugin hooks
3687
- $this->do_hook( 'post_save_pod_item', $pieces, $is_new_item, $params->id );
3688
- $this->do_hook( "post_save_pod_item_{$params->pod}", $pieces, $is_new_item, $params->id );
3689
-
3690
- if ( $is_new_item ) {
3691
- $this->do_hook( 'post_create_pod_item', $pieces, $params->id );
3692
- $this->do_hook( "post_create_pod_item_{$params->pod}", $pieces, $params->id );
3693
- }
3694
- else {
3695
- $this->do_hook( 'post_edit_pod_item', $pieces, $params->id );
3696
- $this->do_hook( "post_edit_pod_item_{$params->pod}", $pieces, $params->id );
3697
- }
3698
-
3699
- // Call any post-save helpers (if not bypassed)
3700
- if ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) {
3701
- if ( !empty( $post_save_helpers ) ) {
3702
- pods_deprecated( sprintf( __( 'Post-save helpers are deprecated, use the action pods_post_save_pod_item_%s instead', 'pods' ), $params->pod ), '2.0' );
3703
-
3704
- foreach ( $post_save_helpers as $helper ) {
3705
- $helper = $this->load_helper( array( 'name' => $helper ) );
3706
-
3707
- if ( false !== $helper && ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) )
3708
- eval( '?>' . $helper[ 'code' ] );
3709
- }
3710
- }
3711
- }
3712
- }
3713
-
3714
- // Clear cache
3715
- pods_cache_clear( $params->id, 'pods_items_' . $pod[ 'name' ] );
3716
-
3717
- if ( $params->clear_slug_cache && !empty( $pod[ 'field_slug' ] ) ) {
3718
- $slug = pods( $pod[ 'name' ], $params->id )->field( $pod[ 'field_slug' ] );
3719
-
3720
- if ( 0 < strlen( $slug ) ) {
3721
- pods_cache_clear( $slug, 'pods_items_' . $pod[ 'name' ] );
3722
- }
3723
- }
3724
-
3725
- // Clear WP meta cache
3726
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy', 'user', 'comment' ) ) ) {
3727
- $meta_type = $pod[ 'type' ];
3728
-
3729
- if ( 'post_type' == $meta_type )
3730
- $meta_type = 'post';
3731
-
3732
- wp_cache_delete( $params->id, $meta_type . '_meta' );
3733
- wp_cache_delete( $params->id, 'pods_' . $meta_type . '_meta' );
3734
- }
3735
-
3736
- // Success! Return the id
3737
- return $params->id;
3738
-
3739
- }
3740
-
3741
- /**
3742
- * @see PodsAPI::save_pod_item
3743
- * Add multiple pod items
3744
- *
3745
- * $params['pod'] string The Pod name (pod or pod_id is required)
3746
- * $params['pod_id'] string The Pod ID (pod or pod_id is required)
3747
- * $params['bypass_helpers'] bool Set to true to bypass running pre-save and post-save helpers
3748
- *
3749
- * $data['id'] int The item ID (optional)
3750
- * $data['data'] array An associative array of field names + values
3751
- *
3752
- * @param array|object $params An associative array of parameters, data excluded
3753
- * @param array $data An associative array of pod ids and field names + values (arrays of field data)
3754
- *
3755
- * @return int The item ID
3756
- * @since 2.0
3757
- */
3758
- public function save_pod_items ( $params, $data ) {
3759
- $params = (object) $params;
3760
-
3761
- $ids = array();
3762
-
3763
- foreach ( $data as $fields ) {
3764
- $params->data = $fields;
3765
-
3766
- if ( isset( $fields[ 'id' ] ) && isset( $fields[ 'data' ] ) ) {
3767
- $params->id = $fields[ 'id' ];
3768
- $params->data = $fields[ 'data' ];
3769
- }
3770
-
3771
- $ids[] = $this->save_pod_item( $params );
3772
- }
3773
-
3774
- return $ids;
3775
- }
3776
-
3777
- /**
3778
- * Get the fields that have changed during a save
3779
- *
3780
- * @param array $pieces Pieces array from save_pod_item
3781
- *
3782
- * @return array Array of fields and values that have changed
3783
- */
3784
- public function get_changed_fields( $pieces ) {
3785
-
3786
- $fields = $pieces[ 'fields' ];
3787
- $fields_active = $pieces[ 'fields_active' ];
3788
-
3789
- $fields_changed = array();
3790
-
3791
- if ( 0 < $pieces[ 'params' ]->id ) {
3792
- $pod = pods( $pieces[ 'params' ]->pod, $pieces[ 'params' ]->id );
3793
-
3794
- foreach ( $fields_active as $field ) {
3795
- if ( isset( $fields[ $field ] ) && $pod->raw( $field ) != $fields[ $field ][ 'value' ] ) {
3796
- $fields_changed[ $field ] = $fields[ $field ][ 'value' ];
3797
- }
3798
- }
3799
- }
3800
-
3801
- return $fields_changed;
3802
-
3803
- }
3804
-
3805
- /**
3806
- * Save relationships
3807
- *
3808
- * @param int $id ID of item
3809
- * @param int|array $related_id ID or IDs to save
3810
- * @param array $pod Pod data
3811
- * @param array $field Field data
3812
- */
3813
- public function save_relationships ( $id, $related_ids, $pod, $field ) {
3814
- // Get current values
3815
- if ( 'pick' == $field[ 'type' ] && isset( PodsField_Pick::$related_data[ $field[ 'id' ] ] ) && isset( PodsField_Pick::$related_data[ $field[ 'id' ] ][ 'current_ids' ] ) )
3816
- $current_ids = PodsField_Pick::$related_data[ $field[ 'id' ] ][ 'current_ids' ];
3817
- else
3818
- $current_ids = $this->lookup_related_items( $field[ 'id' ], $pod[ 'id' ], $id, $field, $pod );
3819
-
3820
- if ( !is_array( $related_ids ) )
3821
- $related_ids = implode( ',', $related_ids );
3822
-
3823
- foreach ( $related_ids as $k => $related_id ) {
3824
- $related_ids[ $k ] = (int) $related_id;
3825
- }
3826
-
3827
- $related_ids = array_unique( array_filter( $related_ids ) );
3828
-
3829
- $related_limit = (int) pods_var_raw( $field[ 'type' ] . '_limit', $field[ 'options' ], 0 );
3830
-
3831
- if ( 'single' == pods_var_raw( $field[ 'type' ] . '_format_type', $field[ 'options' ] ) )
3832
- $related_limit = 1;
3833
-
3834
- // Limit values
3835
- if ( 0 < $related_limit && !empty( $related_ids ) )
3836
- $related_ids = array_slice( $related_ids, 0, $related_limit );
3837
-
3838
- // Post Types, Media, Users, and Comments (meta-based)
3839
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'media', 'user', 'comment' ) ) ) {
3840
- $object_type = $pod[ 'type' ];
3841
-
3842
- if ( 'post_type' == $object_type || 'media' == $object_type )
3843
- $object_type = 'post';
3844
-
3845
- delete_metadata( $object_type, $id, $field[ 'name' ] );
3846
-
3847
- if ( !empty( $related_ids ) ) {
3848
- update_metadata( $object_type, $id, '_pods_' . $field[ 'name' ], $related_ids );
3849
-
3850
- foreach ( $related_ids as $related_id ) {
3851
- add_metadata( $object_type, $id, $field[ 'name' ], $related_id );
3852
- }
3853
- }
3854
- else
3855
- delete_metadata( $object_type, $id, '_pods_' . $field[ 'name' ] );
3856
- }
3857
- // Custom Settings Pages (options-based)
3858
- elseif ( 'settings' == $pod[ 'type' ] ) {
3859
- if ( !empty( $related_ids ) )
3860
- update_option( $pod[ 'name' ] . '_' . $field[ 'name' ], $related_ids );
3861
- else
3862
- delete_option( $pod[ 'name' ] . '_' . $field[ 'name' ] );
3863
- }
3864
-
3865
- $related_pod_id = $related_field_id = 0;
3866
-
3867
- if ( 'pick' == $field[ 'type' ] && isset( PodsField_Pick::$related_data[ $field[ 'id' ] ] ) && !empty( PodsField_Pick::$related_data[ $field[ 'id' ] ][ 'related_field' ] ) ) {
3868
- $related_pod_id = PodsField_Pick::$related_data[ $field[ 'id' ] ][ 'related_pod' ][ 'id' ];
3869
- $related_field_id = PodsField_Pick::$related_data[ $field[ 'id' ] ][ 'related_field' ][ 'id' ];
3870
- }
3871
-
3872
- // Relationships table
3873
- if ( !pods_tableless() ) {
3874
- $related_weight = 0;
3875
-
3876
- foreach ( $related_ids as $related_id ) {
3877
- if ( in_array( $related_id, $current_ids ) ) {
3878
- pods_query( "
3879
- UPDATE `@wp_podsrel`
3880
- SET
3881
- `pod_id` = %d,
3882
- `field_id` = %d,
3883
- `item_id` = %d,
3884
- `related_pod_id` = %d,
3885
- `related_field_id` = %d,
3886
- `related_item_id` = %d,
3887
- `weight` = %d
3888
- WHERE
3889
- `pod_id` = %d
3890
- AND `field_id` = %d
3891
- AND `item_id` = %d
3892
- AND `related_item_id` = %d
3893
- ", array(
3894
- $pod[ 'id' ],
3895
- $field[ 'id' ],
3896
- $id,
3897
- $related_pod_id,
3898
- $related_field_id,
3899
- $related_id,
3900
- $related_weight,
3901
-
3902
- $pod[ 'id' ],
3903
- $field[ 'id' ],
3904
- $id,
3905
- $related_id,
3906
- ) );
3907
- }
3908
- else {
3909
- pods_query( "
3910
- INSERT INTO `@wp_podsrel`
3911
- (
3912
- `pod_id`,
3913
- `field_id`,
3914
- `item_id`,
3915
- `related_pod_id`,
3916
- `related_field_id`,
3917
- `related_item_id`,
3918
- `weight`
3919
- )
3920
- VALUES ( %d, %d, %d, %d, %d, %d, %d )
3921
- ", array(
3922
- $pod[ 'id' ],
3923
- $field[ 'id' ],
3924
- $id,
3925
- $related_pod_id,
3926
- $related_field_id,
3927
- $related_id,
3928
- $related_weight
3929
- ) );
3930
- }
3931
-
3932
- $related_weight++;
3933
- }
3934
- }
3935
- }
3936
-
3937
- /**
3938
- * Duplicate a Pod
3939
- *
3940
- * $params['id'] int The Pod ID
3941
- * $params['name'] string The Pod name
3942
- * $params['new_name'] string The new Pod name
3943
- *
3944
- * @param array $params An associative array of parameters
3945
- * @param bool $strict (optional) Makes sure a pod exists, if it doesn't throws an error
3946
- *
3947
- * @return int New Pod ID
3948
- * @since 2.3
3949
- */
3950
- public function duplicate_pod ( $params, $strict = false ) {
3951
- if ( !is_object( $params ) && !is_array( $params ) ) {
3952
- if ( is_numeric( $params ) )
3953
- $params = array( 'id' => $params );
3954
- else
3955
- $params = array( 'name' => $params );
3956
-
3957
- $params = (object) pods_sanitize( $params );
3958
- }
3959
- else
3960
- $params = (object) pods_sanitize( $params );
3961
-
3962
- $params->table_info = false;
3963
-
3964
- $pod = $this->load_pod( $params, $strict );
3965
-
3966
- if ( empty( $pod ) ) {
3967
- if ( false !== $strict )
3968
- return pods_error( __( 'Pod not found', 'pods' ), $this );
3969
-
3970
- return false;
3971
- }
3972
- elseif ( in_array( $pod[ 'type' ], array( 'media', 'user', 'comment' ) ) ) {
3973
- if ( false !== $strict )
3974
- return pods_error( __( 'Pod not allowed to be duplicated', 'pods' ), $this );
3975
-
3976
- return false;
3977
- }
3978
- elseif ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && 0 < strlen( $pod[ 'object' ] ) ) {
3979
- $pod[ 'object' ] = '';
3980
- }
3981
-
3982
- unset( $pod[ 'id' ] );
3983
-
3984
- if ( isset( $params->new_name ) )
3985
- $pod[ 'name' ] = $params->new_name;
3986
-
3987
- $try = 1;
3988
-
3989
- $check_name = $pod[ 'name' ];
3990
- $new_label = $pod[ 'label' ];
3991
-
3992
- while ( $this->load_pod( array( 'name' => $check_name, 'table_info' => false ), false ) ) {
3993
- $try++;
3994
-
3995
- $check_name = $pod[ 'name' ] . $try;
3996
- $new_label = $pod[ 'label' ] . $try;
3997
- }
3998
-
3999
- $pod[ 'name' ] = $check_name;
4000
- $pod[ 'label' ] = $new_label;
4001
-
4002
- foreach ( $pod[ 'fields' ] as $field => $field_data ) {
4003
- unset( $pod[ 'fields' ][ $field ][ 'id' ] );
4004
- }
4005
-
4006
- return $this->save_pod( $pod );
4007
- }
4008
-
4009
- /**
4010
- * Duplicate a Field
4011
- *
4012
- * $params['pod_id'] int The Pod ID
4013
- * $params['pod'] string The Pod name
4014
- * $params['id'] int The Field ID
4015
- * $params['name'] string The Field name
4016
- * $params['new_name'] string The new Field name
4017
- *
4018
- * @param array $params An associative array of parameters
4019
- * @param bool $strict (optional) Makes sure a field exists, if it doesn't throws an error
4020
- *
4021
- * @return int New Field ID
4022
- * @since 2.3.10
4023
- */
4024
- public function duplicate_field( $params, $strict = false ) {
4025
-
4026
- if ( !is_object( $params ) && !is_array( $params ) ) {
4027
- if ( is_numeric( $params ) ) {
4028
- $params = array( 'id' => $params );
4029
- }
4030
- else {
4031
- $params = array( 'name' => $params );
4032
- }
4033
- }
4034
-
4035
- $params = (object) pods_sanitize( $params );
4036
-
4037
- $params->table_info = false;
4038
-
4039
- $field = $this->load_field( $params, $strict );
4040
-
4041
- if ( empty( $field ) ) {
4042
- if ( false !== $strict ) {
4043
- return pods_error( __( 'Field not found', 'pods' ), $this );
4044
- }
4045
-
4046
- return false;
4047
- }
4048
-
4049
- unset( $field[ 'id' ] );
4050
-
4051
- if ( isset( $params->new_name ) ) {
4052
- $field[ 'name' ] = $params->new_name;
4053
- }
4054
-
4055
- $try = 1;
4056
-
4057
- $check_name = $field[ 'name' ];
4058
- $new_label = $field[ 'label' ];
4059
-
4060
- while ( $this->load_field( array( 'pod_id' => $field[ 'pod_id' ], 'name' => $check_name, 'table_info' => false ), false ) ) {
4061
- $try++;
4062
-
4063
- $check_name = $field[ 'name' ] . $try;
4064
- $new_label = $field[ 'label' ] . $try;
4065
- }
4066
-
4067
- $field[ 'name' ] = $check_name;
4068
- $field[ 'label' ] = $new_label;
4069
-
4070
- return $this->save_field( $field );
4071
-
4072
- }
4073
-
4074
- /**
4075
- * @see PodsAPI::save_pod_item
4076
- *
4077
- * Duplicate a pod item
4078
- *
4079
- * $params['pod'] string The Pod name
4080
- * $params['id'] int The item's ID from the wp_pods_* table
4081
- *
4082
- * @param array $params An associative array of parameters
4083
- *
4084
- * @return int The table row ID
4085
- * @since 1.12
4086
- */
4087
- public function duplicate_pod_item ( $params ) {
4088
- $params = (object) pods_sanitize( $params );
4089
-
4090
- $pod = $this->load_pod( array( 'name' => $params->pod, 'table_info' => false ) );
4091
-
4092
- if ( false === $pod )
4093
- return pods_error( __( 'Pod not found', 'pods' ), $this );
4094
-
4095
- $pod = pods( $params->pod, $params->id );
4096
-
4097
- $params->pod = $pod->pod;
4098
- $params->pod_id = $pod->pod_id;
4099
-
4100
- $fields = (array) pods_var_raw( 'fields', $pod->pod_data, array(), null, true );
4101
- $object_fields = (array) pods_var_raw( 'object_fields', $pod->pod_data, array(), null, true );
4102
-
4103
- if ( !empty( $object_fields ) )
4104
- $fields = array_merge( $object_fields, $fields );
4105
-
4106
- $save_params = array(
4107
- 'pod' => $params->pod,
4108
- 'data' => array()
4109
- );
4110
-
4111
- foreach ( $fields as $field ) {
4112
- $value = $pod->field( array( 'name' => $field[ 'name' ], 'output' => 'ids' ) );
4113
-
4114
- if ( !empty( $value ) || ( !is_array( $value ) && 0 < strlen( $value ) ) )
4115
- $save_params[ 'data' ][ $field[ 'name' ] ] = $value;
4116
- }
4117
-
4118
- $save_params = $this->do_hook( 'duplicate_pod_item', $save_params, $pod->pod, $pod->id(), $params );
4119
-
4120
- $id = $this->save_pod_item( $save_params );
4121
-
4122
- return $id;
4123
- }
4124
-
4125
- /**
4126
- * @see pods()
4127
- *
4128
- * Export a pod item
4129
- *
4130
- * $params['pod'] string The Pod name
4131
- * $params['id'] int The item's ID from the wp_pods_* table
4132
- * $params['fields'] array The fields to export
4133
- * $params['depth'] int How many levels deep to export data
4134
- *
4135
- * @param array $params An associative array of parameters
4136
- * @param object $pod (optional) Pods object
4137
- *
4138
- * @return int The table row ID
4139
- * @since 1.12
4140
- */
4141
- public function export_pod_item ( $params, $pod = null ) {
4142
- if ( !is_object( $pod ) || 'Pods' != get_class( $pod ) ) {
4143
- if ( empty( $params ) )
4144
- return false;
4145
-
4146
- $params = (object) pods_sanitize( $params );
4147
-
4148
- $pod = pods( $params->pod, $params->id, false );
4149
-
4150
- if ( empty( $pod ) )
4151
- return false;
4152
- }
4153
-
4154
- $fields = (array) pods_var_raw( 'fields', $params, array(), null, true );
4155
- $depth = (int) pods_var_raw( 'depth', $params, 2, null, true );
4156
- $object_fields = (array) pods_var_raw( 'object_fields', $pod->pod_data, array(), null, true );
4157
- $flatten = (boolean) pods_var( 'flatten', $params, false, null, true );
4158
-
4159
- if ( empty( $fields ) ) {
4160
- $fields = $pod->fields;
4161
- $fields = array_merge( $fields, $object_fields );
4162
- }
4163
-
4164
- $data = $this->export_pod_item_level( $pod, $fields, $depth, $flatten );
4165
-
4166
- $data = $this->do_hook( 'export_pod_item', $data, $pod->pod, $pod->id(), $pod, $fields, $depth, $flatten );
4167
-
4168
- return $data;
4169
- }
4170
-
4171
- /**
4172
- * Export a pod item by depth level
4173
- *
4174
- * @param Pods $pod Pods object
4175
- * @param array $fields Fields to export
4176
- * @param int $depth Depth limit
4177
- * @param boolean $flatten Whether to flatten arrays for display
4178
- * @param int $current_depth Current depth level
4179
- *
4180
- * @return array Data array
4181
- *
4182
- * @since 2.3
4183
- */
4184
- private function export_pod_item_level ( $pod, $fields, $depth, $flatten = false, $current_depth = 1 ) {
4185
- $tableless_field_types = PodsForm::tableless_field_types();
4186
- $simple_tableless_objects = PodsForm::simple_tableless_objects();
4187
-
4188
- $object_fields = (array) pods_var_raw( 'object_fields', $pod->pod_data, array(), null, true );
4189
-
4190
- $export_fields = array();
4191
-
4192
- foreach ( $fields as $k => $field ) {
4193
- if ( !is_array( $field ) ) {
4194
- $field = array(
4195
- 'id' => 0,
4196
- 'name' => $field
4197
- );
4198
- }
4199
-
4200
- if ( isset( $pod->fields[ $field[ 'name' ] ] ) ) {
4201
- $field = $pod->fields[ $field[ 'name' ] ];
4202
- $field[ 'lookup_name' ] = $field[ 'name' ];
4203
-
4204
- if ( in_array( $field[ 'type' ], $tableless_field_types ) && !in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) ) {
4205
- if ( 'pick' == $field[ 'type' ] ) {
4206
- if ( empty( $field[ 'table_info' ] ) )
4207
- $field[ 'table_info' ] = $this->get_table_info( pods_var_raw( 'pick_object', $field ), pods_var_raw( 'pick_val', $field ), null, null, $field );
4208
-
4209
- if ( !empty( $field[ 'table_info' ] ) )
4210
- $field[ 'lookup_name' ] .= '.' . $field[ 'table_info' ][ 'field_id' ];
4211
- }
4212
- elseif ( in_array( $field[ 'type' ], PodsForm::file_field_types() ) )
4213
- $field[ 'lookup_name' ] .= '.guid';
4214
- }
4215
-
4216
- $export_fields[ $field[ 'name' ] ] = $field;
4217
- }
4218
- elseif ( isset( $object_fields[ $field[ 'name' ] ] ) ) {
4219
- $field = $object_fields[ $field[ 'name' ] ];
4220
- $field[ 'lookup_name' ] = $field[ 'name' ];
4221
-
4222
- $export_fields[ $field[ 'name' ] ] = $field;
4223
- }
4224
- elseif ( $field[ 'name' ] == $pod->pod_data[ 'field_id' ] ) {
4225
- $field[ 'type' ] = 'number';
4226
- $field[ 'lookup_name' ] = $field[ 'name' ];
4227
-
4228
- $export_fields[ $field[ 'name' ] ] = $field;
4229
- }
4230
- }
4231
-
4232
- $data = array();
4233
-
4234
- foreach ( $export_fields as $field ) {
4235
- // Return IDs (or guid for files) if only one level deep
4236
- if ( 1 == $depth )
4237
- $data[ $field[ 'name' ] ] = $pod->field( array( 'name' => $field[ 'lookup_name' ], 'output' => 'arrays' ) );
4238
- // Recurse depth levels for pick fields if $depth allows
4239
- elseif ( ( -1 == $depth || $current_depth < $depth ) && 'pick' == $field[ 'type' ] && !in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) ) {
4240
- $related_data = array();
4241
-
4242
- $related_ids = $pod->field( array( 'name' => $field[ 'name' ], 'output' => 'ids' ) );
4243
-
4244
- if ( !empty( $related_ids ) ) {
4245
- $related_ids = (array) $related_ids;
4246
-
4247
- $pick_object = pods_var_raw( 'pick_object', $field );
4248
-
4249
- $related_pod = pods( pods_var_raw( 'pick_val', $field ), null, false );
4250
-
4251
- // If this isn't a Pod, return data exactly as Pods does normally
4252
- if ( empty( $related_pod ) || ( 'pod' != $pick_object && $pick_object != $related_pod->pod_data[ 'type' ] ) || $related_pod->pod == $pod->pod )
4253
- $related_data = $pod->field( array( 'name' => $field[ 'name' ], 'output' => 'arrays' ) );
4254
- else {
4255
- $related_object_fields = (array) pods_var_raw( 'object_fields', $related_pod->pod_data, array(), null, true );
4256
-
4257
- $related_fields = array_merge( $related_pod->fields, $related_object_fields );
4258
-
4259
- foreach ( $related_ids as $related_id ) {
4260
- if ( $related_pod->fetch( $related_id ) ) {
4261
- $related_item = $this->export_pod_item_level( $related_pod, $related_fields, $depth, $flatten, ( $current_depth + 1 ) );
4262
-
4263
- $related_data[ $related_id ] = $this->do_hook( 'export_pod_item_level', $related_item, $related_pod->pod, $related_pod->id(), $related_pod, $related_fields, $depth, $flatten, ( $current_depth + 1 ) );
4264
- }
4265
- }
4266
-
4267
- if ( $flatten && !empty( $related_data ) )
4268
- $related_data = pods_serial_comma( array_values( $related_data ), array( 'and' => '', 'field_index' => $related_pod->pod_data[ 'field_index' ] ) );
4269
- }
4270
- }
4271
-
4272
- $data[ $field[ 'name' ] ] = $related_data;
4273
- }
4274
- // Return data exactly as Pods does normally
4275
- else
4276
- $data[ $field[ 'name' ] ] = $pod->field( array( 'name' => $field[ 'name' ], 'output' => 'arrays' ) );
4277
-
4278
- if ( $flatten && is_array( $data[ $field[ 'name' ] ] ) )
4279
- $data[ $field[ 'name' ] ] = pods_serial_comma( $data[ $field[ 'name' ] ], array( 'field' => $field[ 'name' ], 'fields' => $export_fields, 'and' => '' ) );
4280
- }
4281
-
4282
- $data[ 'id' ] = (int) $pod->id();
4283
- return $data;
4284
- }
4285
-
4286
- /**
4287
- * Reorder a Pod
4288
- *
4289
- * $params['pod'] string The Pod name
4290
- * $params['field'] string The field name of the field to reorder
4291
- * $params['order'] array The key => value array of items to reorder (key should be an integer)
4292
- *
4293
- * @param array $params An associative array of parameters
4294
- *
4295
- * @return bool
4296
- *
4297
- * @since 1.9.0
4298
- */
4299
- public function reorder_pod_item ( $params ) {
4300
- $params = (object) pods_sanitize( $params );
4301
-
4302
- // @deprecated 2.0
4303
- if ( isset( $params->datatype ) ) {
4304
- pods_deprecated( __( '$params->pod instead of $params->datatype', 'pods' ), '2.0' );
4305
-
4306
- $params->pod = $params->datatype;
4307
-
4308
- unset( $params->datatype );
4309
- }
4310
-
4311
- if ( null === pods_var_raw( 'pod', $params, null, null, true ) )
4312
- return pods_error( __( '$params->pod is required', 'pods' ), $this );
4313
-
4314
- if ( !is_array( $params->order ) )
4315
- $params->order = explode( ',', $params->order );
4316
-
4317
- $pod = $this->load_pod( array( 'name' => $params->pod, 'table_info' => true ) );
4318
-
4319
- $params->name = $pod[ 'name' ];
4320
-
4321
- if ( false === $pod )
4322
- return pods_error( __( 'Pod is required', 'pods' ), $this );
4323
-
4324
- foreach ( $params->order as $order => $id ) {
4325
- if ( isset( $pod[ 'fields' ][ $params->field ] ) || isset( $pod[ 'object_fields' ][ $params->field ] ) ) {
4326
- if ( 'table' == $pod[ 'storage' ] && ( !pods_tableless() ) ) {
4327
- if ( isset( $pod[ 'fields' ][ $params->field ] ) )
4328
- pods_query( "UPDATE `@wp_pods_{$params->name}` SET `{$params->field}` = " . pods_absint( $order ) . " WHERE `id` = " . pods_absint( $id ) . " LIMIT 1" );
4329
- else
4330
- pods_query( "UPDATE `{$pod['table']}` SET `{$params->field}` = " . pods_absint( $order ) . " WHERE `{$pod['field_id']}` = " . pods_absint( $id ) . " LIMIT 1" );
4331
- }
4332
- else
4333
- $this->save_pod_item( array( 'pod' => $params->pod, 'pod_id' => $params->pod_id, 'id' => $id, 'data' => array( $params->field => pods_absint( $order ) ) ) );
4334
- }
4335
- }
4336
-
4337
- return true;
4338
- }
4339
-
4340
- /**
4341
- *
4342
- * Delete all content for a Pod
4343
- *
4344
- * $params['id'] int The Pod ID
4345
- * $params['name'] string The Pod name
4346
- *
4347
- * @param array $params An associative array of parameters
4348
- * @param array $pod Pod data
4349
- *
4350
- * @return bool
4351
- *
4352
- * @uses pods_query
4353
- * @uses pods_cache_clear
4354
- *
4355
- * @since 1.9.0
4356
- */
4357
- public function reset_pod ( $params, $pod = false ) {
4358
- $params = (object) pods_sanitize( $params );
4359
-
4360
- $params->table_info = true;
4361
-
4362
- if ( empty( $pod ) )
4363
- $pod = $this->load_pod( $params );
4364
-
4365
- if ( false === $pod )
4366
- return pods_error( __( 'Pod not found', 'pods' ), $this );
4367
-
4368
- $params->id = $pod[ 'id' ];
4369
- $params->name = $pod[ 'name' ];
4370
-
4371
- if ( !pods_tableless() ) {
4372
- if ( 'table' == $pod[ 'storage' ] ) {
4373
- try {
4374
- pods_query( "TRUNCATE `@wp_pods_{$params->name}`", false );
4375
- }
4376
- catch ( Exception $e ) {
4377
- // Allow pod to be reset if the table doesn't exist
4378
- if ( false === strpos( $e->getMessage(), 'Unknown table' ) )
4379
- return pods_error( $e->getMessage(), $this );
4380
- }
4381
- }
4382
-
4383
- pods_query( "DELETE FROM `@wp_podsrel` WHERE `pod_id` = {$params->id} OR `related_pod_id` = {$params->id}", false );
4384
- }
4385
-
4386
- // @todo Delete relationships from tableless relationships
4387
-
4388
- // Delete all posts/revisions from this post type
4389
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'media' ) ) ) {
4390
- $type = pods_var( 'object', $pod, $pod[ 'name' ], null, true );
4391
-
4392
- $sql = "
4393
- DELETE `t`, `r`, `m`
4394
- FROM `{$pod['table']}` AS `t`
4395
- LEFT JOIN `{$pod['meta_table']}` AS `m`
4396
- ON `m`.`{$pod['meta_field_id']}` = `t`.`{$pod['field_id']}`
4397
- LEFT JOIN `{$pod['table']}` AS `r`
4398
- ON `r`.`post_parent` = `t`.`{$pod['field_id']}` AND `r`.`post_status` = 'inherit'
4399
- WHERE `t`.`{$pod['field_type']}` = '{$type}'
4400
- ";
4401
-
4402
- pods_query( $sql, false );
4403
- }
4404
- // Delete all terms from this taxonomy
4405
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
4406
- $type = pods_var( 'object', $pod, $pod[ 'name' ], null, true );
4407
-
4408
- $sql = "
4409
- DELETE FROM `{$pod['table']}` AS `t`
4410
- " . $pod['join']['tt'] . "
4411
- WHERE " . implode( ' AND ', $pod['where'] ) . "
4412
- ";
4413
-
4414
- pods_query( $sql, false );
4415
- }
4416
- // Delete all users except the current one
4417
- elseif ( 'user' == $pod[ 'type' ] ) {
4418
- $sql = "
4419
- DELETE `t`, `m`
4420
- FROM `{$pod['table']}` AS `t`
4421
- LEFT JOIN `{$pod['meta_table']}` AS `m`
4422
- ON `m`.`{$pod['meta_field_id']}` = `t`.`{$pod['field_id']}`
4423
- WHERE `t`.`{$pod['field_id']}` != " . (int) get_current_user_id() . "
4424
- ";
4425
-
4426
- pods_query( $sql, false );
4427
- }
4428
- // Delete all comments
4429
- elseif ( 'comment' == $pod[ 'type' ] ) {
4430
- $type = pods_var( 'object', $pod, $pod[ 'name' ], null, true );
4431
-
4432
- $sql = "
4433
- DELETE `t`, `m`
4434
- FROM `{$pod['table']}` AS `t`
4435
- LEFT JOIN `{$pod['meta_table']}` AS `m`
4436
- ON `m`.`{$pod['meta_field_id']}` = `t`.`{$pod['field_id']}`
4437
- WHERE `t`.`{$pod['field_type']}` = '{$type}'
4438
- ";
4439
-
4440
- pods_query( $sql, false );
4441
- }
4442
-
4443
- pods_cache_clear( true ); // only way to reliably clear out cached data across an entire group
4444
-
4445
- return true;
4446
- }
4447
-
4448
- /**
4449
- * Delete a Pod and all its content
4450
- *
4451
- * $params['id'] int The Pod ID
4452
- * $params['name'] string The Pod name
4453
- *
4454
- * @param array $params An associative array of parameters
4455
- * @param bool $strict (optional) Makes sure a pod exists, if it doesn't throws an error
4456
- * @param bool $delete_all (optional) Whether to delete all content from a WP object
4457
- *
4458
- * @uses PodsAPI::load_pod
4459
- * @uses wp_delete_post
4460
- * @uses pods_query
4461
- *
4462
- * @return bool
4463
- * @since 1.7.9
4464
- */
4465
- public function delete_pod ( $params, $strict = false, $delete_all = false ) {
4466
- /**
4467
- * @var $wpdb wpdb
4468
- */
4469
- global $wpdb;
4470
-
4471
- if ( !is_object( $params ) && !is_array( $params ) ) {
4472
- if ( is_numeric( $params ) )
4473
- $params = array( 'id' => $params );
4474
- else
4475
- $params = array( 'name' => $params );
4476
-
4477
- $params = (object) pods_sanitize( $params );
4478
- }
4479
- else
4480
- $params = (object) pods_sanitize( $params );
4481
-
4482
- $params->table_info = false;
4483
-
4484
- $pod = $this->load_pod( $params, $strict );
4485
-
4486
- if ( empty( $pod ) ) {
4487
- if ( false !== $strict )
4488
- return pods_error( __( 'Pod not found', 'pods' ), $this );
4489
-
4490
- return false;
4491
- }
4492
-
4493
- $params->id = (int) $pod[ 'id' ];
4494
- $params->name = $pod[ 'name' ];
4495
-
4496
- foreach ( $pod[ 'fields' ] as $field ) {
4497
- $field[ 'pod' ] = $pod;
4498
-
4499
- $this->delete_field( $field, false );
4500
- }
4501
-
4502
- // Only delete the post once the fields are taken care of, it's not required anymore
4503
- $success = wp_delete_post( $params->id );
4504
-
4505
- if ( !$success )
4506
- return pods_error( __( 'Pod unable to be deleted', 'pods' ), $this );
4507
-
4508
- // Reset content
4509
- if ( $delete_all )
4510
- $this->reset_pod( $params, $pod );
4511
-
4512
- if ( !pods_tableless() ) {
4513
- if ( 'table' == $pod[ 'storage' ] ) {
4514
- try {
4515
- pods_query( "DROP TABLE IF EXISTS `@wp_pods_{$params->name}`", false );
4516
- }
4517
- catch ( Exception $e ) {
4518
- // Allow pod to be deleted if the table doesn't exist
4519
- if ( false === strpos( $e->getMessage(), 'Unknown table' ) )
4520
- return pods_error( $e->getMessage(), $this );
4521
- }
4522
- }
4523
-
4524
- pods_query( "DELETE FROM `@wp_podsrel` WHERE `pod_id` = {$params->id} OR `related_pod_id` = {$params->id}", false );
4525
- }
4526
-
4527
- // @todo Delete relationships from tableless relationships
4528
-
4529
- // Delete any relationship references
4530
- $sql = "
4531
- DELETE `pm`
4532
- FROM `{$wpdb->postmeta}` AS `pm`
4533
- LEFT JOIN `{$wpdb->posts}` AS `p`
4534
- ON `p`.`post_type` = '_pods_field'
4535
- AND `p`.`ID` = `pm`.`post_id`
4536
- LEFT JOIN `{$wpdb->postmeta}` AS `pm2`
4537
- ON `pm2`.`meta_key` = 'pick_object'
4538
- AND `pm2`.`meta_value` = 'pod'
4539
- AND `pm2`.`post_id` = `pm`.`post_id`
4540
- WHERE
4541
- `p`.`ID` IS NOT NULL
4542
- AND `pm2`.`meta_id` IS NOT NULL
4543
- AND `pm`.`meta_key` = 'pick_val'
4544
- AND `pm`.`meta_value` = '{$params->name}'
4545
- ";
4546
-
4547
- pods_query( $sql );
4548
-
4549
- $this->cache_flush_pods( $pod );
4550
-
4551
- return true;
4552
- }
4553
-
4554
- /**
4555
- * Drop a field within a Pod
4556
- *
4557
- * $params['id'] int The field ID
4558
- * $params['name'] int The field name
4559
- * $params['pod'] string The Pod name
4560
- * $params['pod_id'] string The Pod name
4561
- *
4562
- * @param array $params An associative array of parameters
4563
- * @param bool $table_operation Whether or not to handle table operations
4564
- *
4565
- * @uses PodsAPI::load_field
4566
- * @uses wp_delete_post
4567
- * @uses pods_query
4568
- *
4569
- * @return bool
4570
- * @since 1.7.9
4571
- */
4572
- public function delete_field ( $params, $table_operation = true ) {
4573
- /**
4574
- * @var $wpdb wpdb
4575
- */
4576
- global $wpdb;
4577
-
4578
- $tableless_field_types = PodsForm::tableless_field_types();
4579
- $simple_tableless_objects = PodsForm::simple_tableless_objects();
4580
-
4581
- $params = (object) pods_sanitize( $params );
4582
-
4583
- if ( !isset( $params->pod ) )
4584
- $params->pod = '';
4585
-
4586
- if ( !isset( $params->pod_id ) )
4587
- $params->pod_id = 0;
4588
-
4589
- $pod = $params->pod;
4590
-
4591
- $save_pod = false;
4592
-
4593
- if ( !is_array( $pod ) )
4594
- $pod = $this->load_pod( array( 'name' => $pod, 'id' => $params->pod_id, 'table_info' => false ) );
4595
- else
4596
- $save_pod = true;
4597
-
4598
- if ( empty( $pod ) )
4599
- return pods_error( __( 'Pod not found', 'pods' ), $this );
4600
-
4601
- $params->pod_id = $pod[ 'id' ];
4602
- $params->pod = $pod[ 'name' ];
4603
-
4604
- if ( !isset( $params->name ) )
4605
- $params->name = '';
4606
-
4607
- if ( !isset( $params->id ) )
4608
- $params->id = 0;
4609
-
4610
- $field = $this->load_field( array( 'name' => $params->name, 'id' => $params->id, 'pod' => $params->pod, 'pod_id' => $params->pod_id ) );
4611
-
4612
- if ( false === $field )
4613
- return pods_error( __( 'Field not found', 'pods' ), $this );
4614
-
4615
- $params->id = $field[ 'id' ];
4616
- $params->name = $field[ 'name' ];
4617
-
4618
- $simple = ( 'pick' == $field[ 'type' ] && in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) );
4619
- $simple = (boolean) $this->do_hook( 'tableless_custom', $simple, $field, $pod, $params );
4620
-
4621
- if ( $table_operation && 'table' == $pod[ 'storage' ] && ( !in_array( $field[ 'type' ], $tableless_field_types ) || $simple ) )
4622
- pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` DROP COLUMN `{$params->name}`", false );
4623
-
4624
- $success = wp_delete_post( $params->id );
4625
-
4626
- if ( !$success )
4627
- return pods_error( __( 'Field unable to be deleted', 'pods' ), $this );
4628
-
4629
- $wpdb->query( $wpdb->prepare( "DELETE pm FROM {$wpdb->postmeta} AS pm
4630
- LEFT JOIN {$wpdb->posts} AS p
4631
- ON p.post_type = '_pods_field' AND p.ID = pm.post_id
4632
- WHERE p.ID IS NOT NULL AND pm.meta_key = 'sister_id' AND pm.meta_value = %d", $params->id ) );
4633
-
4634
- if ( ( !pods_tableless() ) && $table_operation ) {
4635
- pods_query( "DELETE FROM `@wp_podsrel` WHERE (`pod_id` = {$params->pod_id} AND `field_id` = {$params->id}) OR (`related_pod_id` = {$params->pod_id} AND `related_field_id` = {$params->id})", false );
4636
- }
4637
-
4638
- // @todo Delete tableless relationship meta
4639
-
4640
- if ( true === $save_pod )
4641
- $this->cache_flush_pods( $pod );
4642
-
4643
- return true;
4644
- }
4645
-
4646
- /**
4647
- * Drop a Pod Object
4648
- *
4649
- * $params['id'] int The object ID
4650
- * $params['name'] string The object name
4651
- * $params['type'] string The object type
4652
- *
4653
- * @param array|object $params An associative array of parameters
4654
- *
4655
- * @uses wp_delete_post
4656
- *
4657
- * @return bool
4658
- * @since 2.0
4659
- */
4660
- public function delete_object ( $params ) {
4661
- $params = (object) $params;
4662
- $object = $this->load_object( $params );
4663
-
4664
- if ( empty( $object ) )
4665
- return pods_error( sprintf( __( "%s Object not found", 'pods' ), ucwords( $params->type ) ), $this );
4666
-
4667
- $success = wp_delete_post( $params->id );
4668
-
4669
- if ( !$success )
4670
- return pods_error( sprintf( __( "%s Object not deleted", 'pods' ), ucwords( $params->type ) ), $this );
4671
-
4672
- pods_transient_clear( 'pods_objects_' . $params->type );
4673
-
4674
- return true;
4675
- }
4676
-
4677
- /**
4678
- * @see PodsAPI::delete_object
4679
- *
4680
- * Drop a Pod Template
4681
- *
4682
- * $params['id'] int The template ID
4683
- * $params['name'] string The template name
4684
- *
4685
- * @param array $params An associative array of parameters
4686
- *
4687
- * @return bool
4688
- * @since 1.7.9
4689
- */
4690
- public function delete_template ( $params ) {
4691
- $params = (object) $params;
4692
- $params->type = 'template';
4693
- return $this->delete_object( $params );
4694
- }
4695
-
4696
- /**
4697
- * @see PodsAPI::delete_object
4698
- *
4699
- * Drop a Pod Page
4700
- *
4701
- * $params['id'] int The page ID
4702
- * $params['uri'] string The page URI
4703
- *
4704
- * @param array $params An associative array of parameters
4705
- *
4706
- * @return bool
4707
- * @since 1.7.9
4708
- */
4709
- public function delete_page ( $params ) {
4710
- $params = (object) $params;
4711
- if ( isset( $params->uri ) ) {
4712
- $params->name = $params->uri;
4713
- unset( $params->uri );
4714
- }
4715
- if ( isset( $params->name ) )
4716
- $params->name = trim( $params->name, '/' );
4717
- $params->type = 'page';
4718
- return $this->delete_object( $params );
4719
- }
4720
-
4721
- /**
4722
- * @see PodsAPI::delete_object
4723
- *
4724
- * Drop a Pod Helper
4725
- *
4726
- * $params['id'] int The helper ID
4727
- * $params['name'] string The helper name
4728
- *
4729
- * @param array $params An associative array of parameters
4730
- *
4731
- * @return bool
4732
- * @since 1.7.9
4733
- */
4734
- public function delete_helper ( $params ) {
4735
- $params = (object) $params;
4736
- $params->type = 'helper';
4737
- return $this->delete_object( $params );
4738
- }
4739
-
4740
- /**
4741
- * Drop a single pod item
4742
- *
4743
- * $params['id'] int (optional) The item's ID from the wp_pod_* table (used with datatype parameter)
4744
- * $params['pod'] string (optional) The Pod name (used with id parameter)
4745
- * $params['pod_id'] int (optional) The Pod ID (used with id parameter)
4746
- * $params['bypass_helpers'] bool Set to true to bypass running pre-save and post-save helpers
4747
- *
4748
- * @param array $params An associative array of parameters
4749
- * @param bool $wp Whether to run WP object delete action
4750
- *
4751
- * @return bool
4752
- * @since 1.7.9
4753
- */
4754
- public function delete_pod_item ( $params, $wp = true ) {
4755
- $params = (object) pods_sanitize( $params );
4756
-
4757
- // @deprecated 2.0
4758
- if ( isset( $params->datatype_id ) || isset( $params->datatype ) || isset( $params->tbl_row_id ) ) {
4759
- if ( isset( $params->tbl_row_id ) ) {
4760
- pods_deprecated( __( '$params->id instead of $params->tbl_row_id', 'pods' ), '2.0' );
4761
- $params->id = $params->tbl_row_id;
4762
- unset( $params->tbl_row_id );
4763
- }
4764
-
4765
- if ( isset( $params->pod_id ) ) {
4766
- pods_deprecated( __( '$params->id instead of $params->pod_id', 'pods' ), '2.0' );
4767
- $params->id = $params->pod_id;
4768
- unset( $params->pod_id );
4769
- }
4770
-
4771
- if ( isset( $params->dataype_id ) ) {
4772
- pods_deprecated( __( '$params->pod_id instead of $params->datatype_id', 'pods' ), '2.0' );
4773
- $params->pod_id = $params->dataype_id;
4774
- unset( $params->dataype_id );
4775
- }
4776
-
4777
- if ( isset( $params->datatype ) ) {
4778
- pods_deprecated( __( '$params->pod instead of $params->datatype', 'pods' ), '2.0' );
4779
- $params->pod = $params->datatype;
4780
- unset( $params->datatype );
4781
- }
4782
- }
4783
-
4784
- if ( !isset( $params->id ) )
4785
- return pods_error( __( 'Pod Item not found', 'pods' ), $this );
4786
-
4787
- $params->id = pods_absint( $params->id );
4788
-
4789
- if ( !isset( $params->pod ) )
4790
- $params->pod = '';
4791
-
4792
- if ( !isset( $params->pod_id ) )
4793
- $params->pod_id = 0;
4794
-
4795
- $pod = $this->load_pod( array( 'name' => $params->pod, 'id' => $params->pod_id, 'table_info' => false ) );
4796
-
4797
- if ( false === $pod )
4798
- return pods_error( __( 'Pod not found', 'pods' ), $this );
4799
-
4800
- $params->pod_id = $pod[ 'id' ];
4801
- $params->pod = $pod[ 'name' ];
4802
-
4803
- // Allow Helpers to bypass subsequent helpers in recursive delete_pod_item calls
4804
- $bypass_helpers = false;
4805
-
4806
- if ( isset( $params->bypass_helpers ) && false !== $params->bypass_helpers )
4807
- $bypass_helpers = true;
4808
-
4809
- $pre_delete_helpers = $post_delete_helpers = array();
4810
-
4811
- if ( false === $bypass_helpers ) {
4812
- // Plugin hook
4813
- $this->do_hook( 'pre_delete_pod_item', $params, $pod );
4814
- $this->do_hook( "pre_delete_pod_item_{$params->pod}", $params, $pod );
4815
-
4816
- // Call any pre-save helpers (if not bypassed)
4817
- if ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) {
4818
- if ( !empty( $pod[ 'options' ] ) && is_array( $pod[ 'options' ] ) ) {
4819
- $helpers = array( 'pre_delete_helpers', 'post_delete_helpers' );
4820
-
4821
- foreach ( $helpers as $helper ) {
4822
- if ( isset( $pod[ 'options' ][ $helper ] ) && !empty( $pod[ 'options' ][ $helper ] ) )
4823
- ${$helper} = explode( ',', $pod[ 'options' ][ $helper ] );
4824
- }
4825
- }
4826
-
4827
- if ( !empty( $pre_delete_helpers ) ) {
4828
- pods_deprecated( sprintf( __( 'Pre-delete helpers are deprecated, use the action pods_pre_delete_pod_item_%s instead', 'pods' ), $params->pod ), '2.0' );
4829
-
4830
- foreach ( $pre_delete_helpers as $helper ) {
4831
- $helper = $this->load_helper( array( 'name' => $helper ) );
4832
-
4833
- if ( false !== $helper )
4834
- eval( '?>' . $helper[ 'code' ] );
4835
- }
4836
- }
4837
- }
4838
- }
4839
-
4840
- // Delete object from relationship fields
4841
- $this->delete_object_from_relationships( $params->id, $pod );
4842
-
4843
- if ( 'table' == $pod[ 'storage' ] )
4844
- pods_query( "DELETE FROM `@wp_pods_{$params->pod}` WHERE `id` = {$params->id} LIMIT 1" );
4845
-
4846
- if ( $wp && 'taxonomy' == $pod[ 'type' ] ) {
4847
- $taxonomy = $pod[ 'name' ];
4848
-
4849
- if ( !empty( $pod[ 'object' ] ) )
4850
- $taxonomy = $pod[ 'object' ];
4851
-
4852
- wp_delete_term( $params->id, $taxonomy );
4853
- }
4854
- elseif ( $wp && !in_array( $pod[ 'type' ], array( 'pod', 'table', '', 'taxonomy' ) ) )
4855
- $this->delete_wp_object( $pod[ 'type' ], $params->id );
4856
-
4857
- if ( false === $bypass_helpers ) {
4858
- // Plugin hook
4859
- $this->do_hook( 'post_delete_pod_item', $params, $pod );
4860
- $this->do_hook( "post_delete_pod_item_{$params->pod}", $params, $pod );
4861
-
4862
- // Call any post-save helpers (if not bypassed)
4863
- if ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) {
4864
- if ( !empty( $post_delete_helpers ) ) {
4865
- pods_deprecated( sprintf( __( 'Post-delete helpers are deprecated, use the action pods_post_delete_pod_item_%s instead', 'pods' ), $params->pod ), '2.0' );
4866
-
4867
- foreach ( $post_delete_helpers as $helper ) {
4868
- $helper = $this->load_helper( array( 'name' => $helper ) );
4869
-
4870
- if ( false !== $helper )
4871
- eval( '?>' . $helper[ 'code' ] );
4872
- }
4873
- }
4874
- }
4875
- }
4876
-
4877
- pods_cache_clear( $params->id, 'pods_items_' . $params->pod );
4878
-
4879
- return true;
4880
- }
4881
-
4882
- /**
4883
- * Delete an object from tableless fields
4884
- *
4885
- * @param int $id
4886
- * @param string $type
4887
- * @param string $name
4888
- *
4889
- * @return bool
4890
- *
4891
- * @since 2.3
4892
- */
4893
- public function delete_object_from_relationships ( $id, $object, $name = null ) {
4894
- /**
4895
- * @var $pods_init \PodsInit
4896
- */
4897
- global $pods_init;
4898
-
4899
- $pod = false;
4900
-
4901
- // Run any bidirectional delete operations
4902
- if ( is_array( $object ) )
4903
- $pod = $object;
4904
- elseif ( is_object( $pods_init ) )
4905
- $pod = PodsInit::$meta->get_object( $object, $name );
4906
-
4907
- if ( !empty( $pod ) ) {
4908
- $object = $pod[ 'type' ];
4909
- $name = $pod[ 'name' ];
4910
-
4911
- foreach ( $pod[ 'fields' ] as $field ) {
4912
- PodsForm::delete( $field[ 'type' ], $id, $field[ 'name' ], array_merge( $field, $field[ 'options' ] ), $pod );
4913
- }
4914
- }
4915
-
4916
- // Lookup related fields (non-bidirectional)
4917
- $params = array(
4918
- 'where' => array(
4919
- array(
4920
- 'key' => 'type',
4921
- 'value' => 'pick'
4922
- ),
4923
- array(
4924
- 'key' => 'pick_object',
4925
- 'value' => $object
4926
- )
4927
- )
4928
- );
4929
-
4930
- if ( !empty( $name ) && $name != $object ) {
4931
- $params[ 'where' ][] = array(
4932
- 'key' => 'pick_val',
4933
- 'value' => $name
4934
- );
4935
- }
4936
-
4937
- $fields = $this->load_fields( $params, false );
4938
-
4939
- if ( !empty( $pod ) && 'media' == $pod[ 'type' ] ) {
4940
- $params[ 'where' ] = array(
4941
- array(
4942
- 'key' => 'type',
4943
- 'value' => 'file'
4944
- )
4945
- );
4946
-
4947
- $fields = array_merge( $fields, $this->load_fields( $params, false ) );
4948
- }
4949
-
4950
- if ( is_array( $fields ) && !empty( $fields ) ) {
4951
- foreach ( $fields as $related_field ) {
4952
- $related_pod = $this->load_pod( array( 'id' => $related_field[ 'pod_id' ], 'fields' => false ), false );
4953
-
4954
- if ( empty( $related_pod ) )
4955
- continue;
4956
-
4957
- $related_from = $this->lookup_related_items_from( $related_field[ 'id' ], $related_pod[ 'id' ], $id, $related_field, $related_pod );
4958
-
4959
- $this->delete_relationships( $related_from, $id, $related_pod, $related_field );
4960
- }
4961
- }
4962
-
4963
- if ( !empty( $pod ) && !pods_tableless() ) {
4964
- pods_query( "
4965
- DELETE FROM `@wp_podsrel`
4966
- WHERE
4967
- (
4968
- `pod_id` = %d
4969
- AND `item_id` = %d
4970
- )
4971
- OR (
4972
- `related_pod_id` = %d
4973
- AND `related_item_id` = %d
4974
- )
4975
- ", array(
4976
- $pod[ 'id' ],
4977
- $id,
4978
-
4979
- $pod[ 'id' ],
4980
- $id
4981
- ) );
4982
- }
4983
-
4984
- return true;
4985
- }
4986
-
4987
- /**
4988
- * Delete relationships
4989
- *
4990
- * @param int|array $related_id IDs for items to save
4991
- * @param int|array $id ID or IDs to remove
4992
- * @param array $related_pod Pod data
4993
- * @param array $related_field Field data
4994
- *
4995
- * @return void
4996
- *
4997
- * @since 2.3
4998
- */
4999
- public function delete_relationships ( $related_id, $id, $related_pod, $related_field ) {
5000
- if ( is_array( $related_id ) ) {
5001
- foreach ( $related_id as $rid ) {
5002
- $this->delete_relationships( $rid, $id, $related_pod, $related_field );
5003
- }
5004
-
5005
- return;
5006
- }
5007
-
5008
- if ( is_array( $id ) ) {
5009
- foreach ( $id as $rid ) {
5010
- $this->delete_relationships( $related_id, $rid, $related_pod, $related_field );
5011
- }
5012
-
5013
- return;
5014
- }
5015
-
5016
- $id = (int) $id;
5017
-
5018
- if ( empty( $id ) )
5019
- return;
5020
-
5021
- $related_ids = $this->lookup_related_items( $related_field[ 'id' ], $related_pod[ 'id' ], $related_id, $related_field, $related_pod );
5022
-
5023
- if ( empty( $related_ids ) )
5024
- return;
5025
- elseif ( !in_array( $id, $related_ids ) )
5026
- return;
5027
-
5028
- if ( isset( self::$related_item_cache[ $related_pod[ 'id' ] ][ $related_field[ 'id' ] ] ) ) {
5029
- // Delete relationship from cache
5030
- unset( self::$related_item_cache[ $related_pod[ 'id' ] ][ $related_field[ 'id' ] ] );
5031
- }
5032
- unset( $related_ids[ array_search( $id, $related_ids ) ] );
5033
-
5034
- $no_conflict = pods_no_conflict_check( $related_pod[ 'type' ] );
5035
-
5036
- if ( !$no_conflict )
5037
- pods_no_conflict_on( $related_pod[ 'type' ] );
5038
-
5039
- // Post Types, Media, Users, and Comments (meta-based)
5040
- if ( in_array( $related_pod[ 'type' ], array( 'post_type', 'media', 'user', 'comment' ) ) ) {
5041
- $object_type = $related_pod[ 'type' ];
5042
-
5043
- if ( 'post_type' == $object_type || 'media' == $object_type )
5044
- $object_type = 'post';
5045
-
5046
- delete_metadata( $object_type, $related_id, $related_field[ 'name' ] );
5047
-
5048
- if ( !empty( $related_ids ) ) {
5049
- update_metadata( $object_type, $related_id, '_pods_' . $related_field[ 'name' ], $related_ids );
5050
-
5051
- foreach ( $related_ids as $rel_id ) {
5052
- add_metadata( $object_type, $related_id, $related_field[ 'name' ], $rel_id );
5053
- }
5054
- }
5055
- else
5056
- delete_metadata( $object_type, $related_id, '_pods_' . $related_field[ 'name' ] );
5057
- }
5058
- // Custom Settings Pages (options-based)
5059
- elseif ( 'settings' == $related_pod[ 'type' ] ) {
5060
- if ( !empty( $related_ids ) )
5061
- update_option( $related_pod[ 'name' ] . '_' . $related_field[ 'name' ], $related_ids );
5062
- else
5063
- delete_option( $related_pod[ 'name' ] . '_' . $related_field[ 'name' ] );
5064
- }
5065
-
5066
- // Relationships table
5067
- if ( !pods_tableless() ) {
5068
- pods_query( "
5069
- DELETE FROM `@wp_podsrel`
5070
- WHERE
5071
- (
5072
- `pod_id` = %d
5073
- AND `field_id` = %d
5074
- AND `item_id` = %d
5075
- AND `related_item_id` = %d
5076
- )
5077
- OR (
5078
- `related_pod_id` = %d
5079
- AND `related_field_id` = %d
5080
- AND `related_item_id` = %d
5081
- AND `item_id` = %d
5082
- )
5083
- ", array(
5084
- $related_pod[ 'id' ],
5085
- $related_field[ 'id' ],
5086
- $related_id,
5087
- $id,
5088
-
5089
- $related_pod[ 'id' ],
5090
- $related_field[ 'id' ],
5091
- $related_id,
5092
- $id
5093
- ) );
5094
- }
5095
-
5096
- if ( !$no_conflict )
5097
- pods_no_conflict_off( $related_pod[ 'type' ] );
5098
- }
5099
-
5100
- /**
5101
- * Check if a Pod exists
5102
- *
5103
- * $params['id'] int Pod ID
5104
- * $params['name'] string Pod name
5105
- *
5106
- * @param array $params An associative array of parameters
5107
- *
5108
- * @return bool True if exists
5109
- *
5110
- * @since 1.12
5111
- */
5112
- public function pod_exists ( $params, $type = null ) {
5113
- if ( is_string( $params ) )
5114
- $params = array( 'name' => $params );
5115
-
5116
- $params = (object) pods_sanitize( $params );
5117
-
5118
- if ( !empty( $params->id ) || !empty( $params->name ) ) {
5119
- if ( !isset( $params->name ) )
5120
- $pod = get_post( $dummy = (int) $params->id );
5121
- else {
5122
- $pod = get_posts( array(
5123
- 'name' => $params->name,
5124
- 'post_type' => '_pods_pod',
5125
- 'posts_per_page' => 1
5126
- ) );
5127
- }
5128
-
5129
- if ( !empty( $pod ) && ( empty( $type ) || $type == get_post_meta( $pod->ID, 'type', true ) ) )
5130
- return true;
5131
- }
5132
-
5133
- return false;
5134
- }
5135
-
5136
- /**
5137
- * Load a Pod and all of its fields
5138
- *
5139
- * $params['id'] int The Pod ID
5140
- * $params['name'] string The Pod name
5141
- * $params['fields'] bool Whether to load fields (default is true)
5142
- *
5143
- * @param array|object $params An associative array of parameters or pod name as a string
5144
- * @param bool $strict Makes sure the pod exists, throws an error if it doesn't work
5145
- *
5146
- * @return array|bool|mixed|void
5147
- * @since 1.7.9
5148
- */
5149
- public function load_pod ( $params, $strict = true ) {
5150
-
5151
- /**
5152
- * @var $sitepress SitePress
5153
- * @var $wpdb wpdb
5154
- */
5155
- global $sitepress, $icl_adjust_id_url_filter_off, $wpdb;
5156
-
5157
- $current_language = false;
5158
- $load_fields = true;
5159
-
5160
- // WPML support
5161
- if ( is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
5162
- $current_language = pods_sanitize( ICL_LANGUAGE_CODE );
5163
- // Polylang support
5164
- elseif ( function_exists( 'pll_current_language' ) )
5165
- $current_language = pll_current_language( 'slug' );
5166
-
5167
- if ( !is_array( $params ) && !is_object( $params ) )
5168
- $params = array( 'name' => $params, 'table_info' => false, 'fields' => true );
5169
-
5170
- if ( is_object( $params ) && isset( $params->fields ) && !$params->fields )
5171
- $load_fields = false;
5172
- elseif ( is_array( $params ) && isset( $params[ 'fields' ] ) && !$params[ 'fields' ] )
5173
- $load_fields = false;
5174
-
5175
- $table_info = false;
5176
-
5177
- if ( is_object( $params ) && ! empty( $params->table_info ) )
5178
- $table_info = true;
5179
- elseif ( is_array( $params ) && ! empty( $params[ 'table_info' ] ) )
5180
- $table_info = true;
5181
-
5182
- $transient = 'pods_' . $wpdb->prefix . '_pod';
5183
-
5184
- if ( !empty( $current_language ) )
5185
- $transient .= '_' . $current_language;
5186
-
5187
- if ( !$load_fields )
5188
- $transient .= '_nofields';
5189
-
5190
- if ( $table_info )
5191
- $transient .= '_tableinfo';
5192
-
5193
- if ( is_object( $params ) && isset( $params->post_name ) ) {
5194
- $pod = false;
5195
-
5196
- if ( pods_api_cache() )
5197
- $pod = pods_transient_get( $transient . '_' . $params->post_name );
5198
-
5199
- if ( false !== $pod && ( ! $table_info || isset( $pod[ 'table' ] ) ) ) {
5200
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
5201
- $pod = array_merge( $pod, $this->get_table_info( $pod[ 'type' ], $pod[ 'object' ], $pod[ 'name' ], $pod ) );
5202
-
5203
- return $pod;
5204
- }
5205
-
5206
- $_pod = get_object_vars( $params );
5207
- }
5208
- else {
5209
- $params = (object) pods_sanitize( $params );
5210
-
5211
- if ( ( !isset( $params->id ) || empty( $params->id ) ) && ( !isset( $params->name ) || empty( $params->name ) ) ) {
5212
- if ( $strict )
5213
- return pods_error( 'Either Pod ID or Name are required', $this );
5214
-
5215
- return false;
5216
- }
5217
-
5218
- if ( isset( $params->name ) ) {
5219
- $pod = false;
5220
-
5221
- if ( '_pods_pod' == $params->name ) {
5222
- $pod = array(
5223
- 'id' => 0,
5224
- 'name' => $params->name,
5225
- 'label' => __( 'Pods', 'pods' ),
5226
- 'type' => 'post_type',
5227
- 'storage' => 'meta',
5228
- 'options' => array(
5229
- 'label_singular' => __( 'Pod', 'pods' )
5230
- ),
5231
- 'fields' => array()
5232
- );
5233
- }
5234
- elseif ( '_pods_field' == $params->name ) {
5235
- $pod = array(
5236
- 'id' => 0,
5237
- 'name' => $params->name,
5238
- 'label' => __( 'Pod Fields', 'pods' ),
5239
- 'type' => 'post_type',
5240
- 'storage' => 'meta',
5241
- 'options' => array(
5242
- 'label_singular' => __( 'Pod Field', 'pods' )
5243
- ),
5244
- 'fields' => array()
5245
- );
5246
- }
5247
- elseif ( pods_api_cache() )
5248
- $pod = pods_transient_get( $transient . '_' . $params->name );
5249
-
5250
- if ( false !== $pod && ( ! $table_info || isset( $pod[ 'table' ] ) ) ) {
5251
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
5252
- $pod = array_merge( $pod, $this->get_table_info( $pod[ 'type' ], $pod[ 'object' ], $pod[ 'name' ], $pod ) );
5253
-
5254
- return $pod;
5255
- }
5256
- }
5257
-
5258
- if ( !isset( $params->name ) )
5259
- $pod = get_post( $dummy = (int) $params->id );
5260
- else {
5261
- $pod = get_posts( array(
5262
- 'name' => $params->name,
5263
- 'post_type' => '_pods_pod',
5264
- 'posts_per_page' => 1
5265
- ) );
5266
- }
5267
-
5268
- if ( empty( $pod ) ) {
5269
- if ( $strict )
5270
- return pods_error( __( 'Pod not found', 'pods' ), $this );
5271
-
5272
- return false;
5273
- }
5274
-
5275
- if ( is_array( $pod ) )
5276
- $pod = $pod[ 0 ];
5277
-
5278
- $_pod = get_object_vars( $pod );
5279
- }
5280
-
5281
- $pod = false;
5282
-
5283
- if ( pods_api_cache() )
5284
- $pod = pods_transient_get( $transient . '_' . $_pod[ 'post_name' ] );
5285
-
5286
- if ( false !== $pod && ( ! $table_info || isset( $pod[ 'table' ] ) ) ) {
5287
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
5288
- $pod = array_merge( $pod, $this->get_table_info( $pod[ 'type' ], $pod[ 'object' ], $pod[ 'name' ], $pod ) );
5289
-
5290
- return $pod;
5291
- }
5292
-
5293
- $pod = array(
5294
- 'id' => $_pod[ 'ID' ],
5295
- 'name' => $_pod[ 'post_name' ],
5296
- 'label' => $_pod[ 'post_title' ],
5297
- 'description' => $_pod[ 'post_content' ]
5298
- );
5299
-
5300
- if ( strlen( $pod[ 'label' ] ) < 1 )
5301
- $pod[ 'label' ] = $pod[ 'name' ];
5302
-
5303
- // @todo update with a method to put all options in
5304
- $defaults = array(
5305
- 'show_in_menu' => 1,
5306
- 'type' => 'post_type',
5307
- 'storage' => 'meta',
5308
- 'object' => '',
5309
- 'alias' => ''
5310
- );
5311
-
5312
- $pod[ 'options' ] = get_post_meta( $pod[ 'id' ] );
5313
-
5314
- foreach ( $pod[ 'options' ] as $option => $value ) {
5315
- if ( is_array( $value ) ) {
5316
- foreach ( $value as $k => $v ) {
5317
- if ( !is_array( $v ) )
5318
- $value[ $k ] = maybe_unserialize( $v );
5319
- }
5320
-
5321
- if ( 1 == count( $value ) )
5322
- $value = current( $value );
5323
- }
5324
- else
5325
- $value = maybe_unserialize( $value );
5326
-
5327
- $pod[ 'options' ][ $option ] = $value;
5328
- }
5329
-
5330
- $pod[ 'options' ] = array_merge( $defaults, $pod[ 'options' ] );
5331
-
5332
- $pod[ 'type' ] = $pod[ 'options' ][ 'type' ];
5333
- $pod[ 'storage' ] = $pod[ 'options' ][ 'storage' ];
5334
- $pod[ 'object' ] = $pod[ 'options' ][ 'object' ];
5335
- $pod[ 'alias' ] = $pod[ 'options' ][ 'alias' ];
5336
-
5337
- unset( $pod[ 'options' ][ 'type' ] );
5338
- unset( $pod[ 'options' ][ 'storage' ] );
5339
- unset( $pod[ 'options' ][ 'object' ] );
5340
- unset( $pod[ 'options' ][ 'alias' ] );
5341
-
5342
- if ( $table_info )
5343
- $pod = array_merge( $this->get_table_info( $pod[ 'type' ], $pod[ 'object' ], $pod[ 'name' ], $pod ), $pod );
5344
-
5345
- if ( isset( $pod[ 'pod' ] ) )
5346
- unset( $pod[ 'pod' ] );
5347
-
5348
- $pod[ 'fields' ] = array();
5349
-
5350
- $pod[ 'object_fields' ] = array();
5351
-
5352
- if ( 'pod' != $pod[ 'type' ] )
5353
- $pod[ 'object_fields' ] = $this->get_wp_object_fields( $pod[ 'type' ], $pod );
5354
-
5355
- $fields = get_posts( array(
5356
- 'post_type' => '_pods_field',
5357
- 'posts_per_page' => -1,
5358
- 'nopaging' => true,
5359
- 'post_parent' => $pod[ 'id' ],
5360
- 'orderby' => 'menu_order',
5361
- 'order' => 'ASC'
5362
- ) );
5363
-
5364
- if ( !empty( $fields ) ) {
5365
- foreach ( $fields as $field ) {
5366
- $field->pod = $pod[ 'name' ];
5367
- $field->table_info = $table_info;
5368
-
5369
- if ( $load_fields ) {
5370
- $field = $this->load_field( $field );
5371
-
5372
- $field = PodsForm::field_setup( $field, null, $field[ 'type' ] );
5373
- }
5374
- else {
5375
- $field = array(
5376
- 'id' => $field->ID,
5377
- 'name' => $field->post_name,
5378
- 'label' => $field->post_title,
5379
- 'type' => get_post_meta( $field->ID, 'type', true )
5380
- );
5381
- }
5382
-
5383
- $pod[ 'fields' ][ $field[ 'name' ] ] = $field;
5384
- }
5385
- }
5386
-
5387
- if ( did_action( 'init' ) && pods_api_cache() )
5388
- pods_transient_set( $transient . '_' . $pod[ 'name' ], $pod );
5389
-
5390
- return $pod;
5391
- }
5392
-
5393
- /**
5394
- * Load a list of Pods based on filters specified.
5395
- *
5396
- * $params['type'] string/array Pod Type(s) to filter by
5397
- * $params['object'] string/array Pod Object(s) to filter by
5398
- * $params['options'] array Pod Option(s) key=>value array to filter by
5399
- * $params['orderby'] string ORDER BY clause of query
5400
- * $params['limit'] string Number of Pods to return
5401
- * $params['where'] string WHERE clause of query
5402
- * $params['ids'] string|array IDs of Objects
5403
- * $params['count'] boolean Return only a count of Pods
5404
- * $params['names'] boolean Return only an array of name => label
5405
- * $params['ids'] boolean Return only an array of ID => label
5406
- * $params['fields'] boolean Return pod fields with Pods (default is true)
5407
- * $params['key_names'] boolean Return pods keyed by name
5408
- *
5409
- * @param array $params An associative array of parameters
5410
- *
5411
- * @return array|mixed
5412
- *
5413
- * @uses PodsAPI::load_pod
5414
- *
5415
- * @since 2.0
5416
- */
5417
- public function load_pods ( $params = null ) {
5418
-
5419
- /**
5420
- * @var $sitepress SitePress
5421
- */
5422
- global $sitepress, $icl_adjust_id_url_filter_off;
5423
-
5424
- $current_language = false;
5425
-
5426
- // WPML support
5427
- if ( is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
5428
- $current_language = pods_sanitize( ICL_LANGUAGE_CODE );
5429
- // Polylang support
5430
- elseif ( function_exists( 'pll_current_language' ) )
5431
- $current_language = pll_current_language( 'slug' );
5432
-
5433
- $params = (object) pods_sanitize( $params );
5434
-
5435
- $order = 'ASC';
5436
- $orderby = 'menu_order title';
5437
- $limit = -1;
5438
- $ids = false;
5439
-
5440
- $meta_query = array();
5441
- $cache_key = '';
5442
-
5443
- if ( isset( $params->type ) && !empty( $params->type ) ) {
5444
- if ( !is_array( $params->type ) )
5445
- $params->type = array( trim( $params->type ) );
5446
-
5447
- sort( $params->type );
5448
-
5449
- $meta_query[] = array(
5450
- 'key' => 'type',
5451
- 'value' => $params->type,
5452
- 'compare' => 'IN'
5453
- );
5454
-
5455
- if ( 0 < count( $params->type ) )
5456
- $cache_key .= '_type_' . trim( implode( '_', $params->type ) );
5457
- }
5458
-
5459
- if ( isset( $params->object ) && !empty( $params->object ) ) {
5460
- if ( !is_array( $params->object ) )
5461
- $params->object = array( $params->object );
5462
-
5463
- $params->object = pods_trim( $params->object );
5464
-
5465
- sort( $params->object );
5466
-
5467
- $meta_query[] = array(
5468
- 'key' => 'object',
5469
- 'value' => $params->object,
5470
- 'compare' => 'IN'
5471
- );
5472
-
5473
- if ( 1 == count( $params->object ) )
5474
- $cache_key .= '_object_' . trim( implode( '', $params->object ) );
5475
- }
5476
-
5477
- if ( isset( $params->options ) && !empty( $params->options ) && is_array( $params->options ) ) {
5478
- foreach ( $params->options as $option => $value ) {
5479
- if ( !is_array( $value ) )
5480
- $value = array( $value );
5481
-
5482
- $value = pods_trim( $value );
5483
-
5484
- sort( $value );
5485
-
5486
- $meta_query[] = array(
5487
- 'key' => $option,
5488
- 'value' => pods_sanitize( $value ),
5489
- 'compare' => 'IN'
5490
- );
5491
- }
5492
-
5493
- $cache_key = '';
5494
- }
5495
-
5496
- if ( isset( $params->where ) && is_array( $params->where ) )
5497
- $meta_query = array_merge( $meta_query, (array) $params->where );
5498
-
5499
- if ( isset( $params->order ) && !empty( $params->order ) && in_array( strtoupper( $params->order ), array( 'ASC', 'DESC' ) ) )
5500
- $order = strtoupper( $params->order );
5501
-
5502
- if ( isset( $params->orderby ) && !empty( $params->orderby ) )
5503
- $orderby = strtoupper( $params->orderby );
5504
-
5505
- if ( isset( $params->limit ) && !empty( $params->limit ) )
5506
- $limit = pods_absint( $params->limit );
5507
-
5508
- if ( isset( $params->ids ) && !empty( $params->ids ) ) {
5509
- $ids = $params->ids;
5510
-
5511
- if ( !is_array( $ids ) )
5512
- $ids = explode( ',', $ids );
5513
- }
5514
-
5515
- if ( empty( $ids ) )
5516
- $ids = false;
5517
-
5518
- $pre_key = '';
5519
-
5520
- if ( !empty( $current_language ) )
5521
- $pre_key .= '_' . $current_language;
5522
-
5523
- if ( isset( $params->count ) && $params->count )
5524
- $pre_key .= '_count';
5525
-
5526
- if ( isset( $params->ids ) && $params->ids && !empty( $ids ) )
5527
- $pre_key .= '_ids_' . implode( '_', $ids );
5528
-
5529
- if ( isset( $params->names ) && $params->names )
5530
- $pre_key .= '_names';
5531
- elseif ( isset( $params->names_ids ) && $params->names_ids )
5532
- $pre_key .= '_names_ids';
5533
-
5534
- if ( isset( $params->key_names ) && $params->key_names )
5535
- $pre_key .= '_namekeys';
5536
-
5537
- if ( isset( $params->fields ) && !$params->fields )
5538
- $pre_key .= '_nofields';
5539
-
5540
- if ( isset( $params->table_info ) && $params->table_info )
5541
- $pre_key .= '_tableinfo';
5542
-
5543
-
5544
- $pre_key .= '_get';
5545
-
5546
- if ( empty( $cache_key ) )
5547
- $cache_key = 'pods' . $pre_key . '_all';
5548
- else
5549
- $cache_key = 'pods' . $pre_key . $cache_key;
5550
-
5551
- if ( pods_api_cache() && !empty( $cache_key ) && ( 'pods' . ( !empty( $current_language ) ? '_' . $current_language : '' ) . '_get_all' != $cache_key || empty( $meta_query ) ) && $limit < 1 && ( empty( $orderby ) || 'menu_order title' == $orderby ) && empty( $ids ) ) {
5552
- $the_pods = pods_transient_get( $cache_key );
5553
-
5554
- if ( false === $the_pods )
5555
- $the_pods = pods_cache_get( $cache_key, 'pods' );
5556
-
5557
- if ( !is_array( $the_pods ) && 'none' == $the_pods )
5558
- return array();
5559
- elseif ( false !== $the_pods )
5560
- return $the_pods;
5561
- }
5562
-
5563
- $the_pods = array();
5564
-
5565
- $args = array(
5566
- 'post_type' => '_pods_pod',
5567
- 'nopaging' => true,
5568
- 'posts_per_page' => $limit,
5569
- 'order' => $order,
5570
- 'orderby' => $orderby,
5571
- 'meta_query' => $meta_query,
5572
- );
5573
-
5574
- // Only set post__in if there are ids to filter (see https://core.trac.wordpress.org/ticket/28099)
5575
- if ( false != $ids ) {
5576
- $args[ 'post__in' ] = $ids;
5577
- }
5578
-
5579
- $_pods = get_posts( $args );
5580
-
5581
- $export_ignore = array(
5582
- 'object_type',
5583
- 'object_name',
5584
- 'table',
5585
- 'meta_table',
5586
- 'pod_table',
5587
- 'field_id',
5588
- 'field_index',
5589
- 'field_slug',
5590
- 'field_type',
5591
- 'field_parent',
5592
- 'field_parent_select',
5593
- 'meta_field_id',
5594
- 'meta_field_index',
5595
- 'meta_field_value',
5596
- 'pod_field_id',
5597
- 'pod_field_index',
5598
- 'object_fields',
5599
- 'join',
5600
- 'where',
5601
- 'where_default',
5602
- 'orderby',
5603
- 'pod',
5604
- 'recurse',
5605
- 'table_info',
5606
- 'attributes',
5607
- 'group',
5608
- 'grouped',
5609
- 'developer_mode',
5610
- 'dependency',
5611
- 'depends-on',
5612
- 'excludes-on'
5613
- );
5614
-
5615
- $total_fields = 0;
5616
-
5617
- if ( isset( $params->count ) && $params->count )
5618
- $the_pods = count( $_pods );
5619
- else {
5620
- foreach ( $_pods as $pod ) {
5621
- if ( isset( $params->names ) && $params->names )
5622
- $the_pods[ $pod->post_name ] = $pod->post_title;
5623
- elseif ( isset( $params->names_ids ) && $params->names_ids )
5624
- $the_pods[ $pod->ID ] = $pod->post_name;
5625
- else {
5626
- if ( isset( $params->fields ) && !$params->fields )
5627
- $pod->fields = false;
5628
-
5629
- $pod = $this->load_pod( $pod );
5630
-
5631
- // Remove extra data not needed
5632
- if ( pods_var( 'export', $params, false ) && ( !isset( $params->fields ) || $params->fields ) ) {
5633
- foreach ( $export_ignore as $ignore ) {
5634
- if ( isset( $pod[ $ignore ] ) )
5635
- unset( $pod[ $ignore ] );
5636
- }
5637
-
5638
- foreach ( $pod[ 'fields' ] as $field => $field_data ) {
5639
- if ( isset( $pod[ 'fields' ][ $field ][ 'table_info' ] ) )
5640
- unset( $pod[ 'fields' ][ $field ][ 'table_info' ] );
5641
- }
5642
- }
5643
-
5644
- $total_fields += count( $pod[ 'fields' ] );
5645
-
5646
- if ( isset( $params->key_names ) && $params->key_names )
5647
- $the_pods[ $pod[ 'name' ] ] = $pod;
5648
- else
5649
- $the_pods[ $pod[ 'id' ] ] = $pod;
5650
- }
5651
- }
5652
- }
5653
-
5654
- if ( ( !function_exists( 'pll_current_language' ) || ( isset( $params->refresh ) && $params->refresh ) ) && !empty( $cache_key ) && ( 'pods' != $cache_key || empty( $meta_query ) ) && $limit < 1 && ( empty( $orderby ) || 'menu_order title' == $orderby ) && empty( $ids ) ) {
5655
- // Too many Pods can cause issues with the DB when caching is not enabled
5656
- if ( 15 < count( $the_pods ) || 75 < count( $total_fields ) ) {
5657
- pods_transient_clear( $cache_key );
5658
-
5659
- if ( pods_api_cache() ) {
5660
- if ( empty( $the_pods ) && ( !isset( $params->count ) || !$params->count ) )
5661
- pods_cache_set( $cache_key, 'none', 'pods' );
5662
- else
5663
- pods_cache_set( $cache_key, $the_pods, 'pods' );
5664
- }
5665
- }
5666
- else {
5667
- pods_cache_clear( $cache_key, 'pods' );
5668
-
5669
- if ( pods_api_cache() ) {
5670
- if ( empty( $the_pods ) && ( !isset( $params->count ) || !$params->count ) )
5671
- pods_transient_set( $cache_key, 'none' );
5672
- else
5673
- pods_transient_set( $cache_key, $the_pods );
5674
- }
5675
- }
5676
- }
5677
-
5678
- return $the_pods;
5679
- }
5680
-
5681
- /**
5682
- * Check if a Pod's field exists
5683
- *
5684
- * $params['pod_id'] int The Pod ID
5685
- * $params['id'] int The field ID
5686
- * $params['name'] string The field name
5687
- *
5688
- * @param array $params An associative array of parameters
5689
- *
5690
- * @return bool
5691
- *
5692
- * @since 1.12
5693
- */
5694
- public function field_exists ( $params ) {
5695
- $params = (object) pods_sanitize( $params );
5696
-
5697
- if ( ( !empty( $params->id ) || !empty( $params->name ) ) && isset( $params->pod_id ) && !empty( $params->pod_id ) ) {
5698
- if ( !isset( $params->name ) )
5699
- $field = get_post( $dummy = (int) $params->id );
5700
- else {
5701
- $field = get_posts( array(
5702
- 'name' => $params->name,
5703
- 'post_type' => '_pods_field',
5704
- 'posts_per_page' => 1,
5705
- 'post_parent' => $params->pod_id
5706
- ) );
5707
- }
5708
-
5709
- if ( !empty( $field ) )
5710
- return true;
5711
- }
5712
-
5713
- return false;
5714
- }
5715
-
5716
- /**
5717
- * Load a field
5718
- *
5719
- * $params['pod_id'] int The Pod ID
5720
- * $params['pod'] string The Pod name
5721
- * $params['id'] int The field ID
5722
- * $params['name'] string The field name
5723
- * $params['table_info'] boolean Whether to lookup a pick field's table info
5724
- *
5725
- * @param array $params An associative array of parameters
5726
- * @param boolean $strict Whether to require a field exist or not when loading the info
5727
- *
5728
- * @return array|bool Array with field data, false if field not found
5729
- * @since 1.7.9
5730
- */
5731
- public function load_field ( $params, $strict = false ) {
5732
- $params = (object) $params;
5733
-
5734
- if ( !isset( $params->table_info ) )
5735
- $params->table_info = false;
5736
-
5737
- $pod = array();
5738
- $field = array();
5739
-
5740
- if ( isset( $params->post_title ) )
5741
- $_field = $params;
5742
- elseif ( isset( $params->id ) && !empty( $params->id ) )
5743
- $_field = get_post( $dumb = (int) $params->id );
5744
- else {
5745
- if ( !isset( $params->pod ) )
5746
- $params->pod = '';
5747
-
5748
- if ( !isset( $params->pod_id ) )
5749
- $params->pod_id = 0;
5750
-
5751
- if ( isset( $params->pod_data ) )
5752
- $pod = $params->pod_data;
5753
- else {
5754
- $pod = $this->load_pod( array( 'name' => $params->pod, 'id' => $params->pod_id, 'table_info' => false ), false );
5755
-
5756
- if ( false === $pod ) {
5757
- if ( $strict )
5758
- return pods_error( __( 'Pod not found', 'pods' ), $this );
5759
-
5760
- return false;
5761
- }
5762
- }
5763
-
5764
- $params->pod_id = $pod[ 'id' ];
5765
- $params->pod = $pod[ 'name' ];
5766
-
5767
- if ( empty( $params->name ) && empty( $params->pod ) && empty( $params->pod_id ) )
5768
- return pods_error( __( 'Either Field Name or Field ID / Pod ID are required', 'pods' ), $this );
5769
-
5770
- $params->name = pods_clean_name( $params->name, true, ( 'meta' == $pod[ 'storage' ] ? false : true ) );
5771
-
5772
- if ( isset( $pod[ 'fields' ][ $params->name ] ) && isset( $pod[ 'fields' ][ $params->name ][ 'id' ] ) )
5773
- return $pod[ 'fields' ][ $params->name ];
5774
-
5775
- $field = false;
5776
-
5777
- if ( pods_api_cache() )
5778
- $field = pods_transient_get( 'pods_field_' . $params->pod . '_' . $params->name );
5779
-
5780
- if ( empty( $field ) ) {
5781
- $field = get_posts( array(
5782
- 'name' => $params->name,
5783
- 'post_type' => '_pods_field',
5784
- 'posts_per_page' => 1,
5785
- 'post_parent' => $params->pod_id
5786
- ) );
5787
-
5788
- if ( empty( $field ) ) {
5789
- if ( $strict )
5790
- return pods_error( __( 'Field not found', 'pods' ), $this );
5791
-
5792
- return false;
5793
- }
5794
-
5795
- $_field = $field[ 0 ];
5796
-
5797
- $field = array();
5798
- }
5799
- }
5800
-
5801
- if ( empty( $_field ) ) {
5802
- if ( $strict )
5803
- return pods_error( __( 'Field not found', 'pods' ), $this );
5804
-
5805
- return false;
5806
- }
5807
-
5808
- $_field = get_object_vars( $_field );
5809
-
5810
- if ( !isset( $pod[ 'name' ] ) && !isset( $_field[ 'pod' ] ) ) {
5811
- if ( 0 < $_field[ 'post_parent' ] )
5812
- $pod = $this->load_pod( array( 'id' => $_field[ 'post_parent' ], 'table_info' => false ), false );
5813
-
5814
- if ( empty( $pod ) ) {
5815
- if ( $strict )
5816
- return pods_error( __( 'Pod for field not found', 'pods' ), $this );
5817
-
5818
- return false;
5819
- }
5820
- }
5821
-
5822
- if ( empty( $field ) ) {
5823
- if ( pods_api_cache() && ( isset( $pod[ 'name' ] ) || isset( $_field[ 'pod' ] ) ) )
5824
- $field = pods_transient_get( 'pods_field_' . pods_var( 'name', $pod, pods_var( 'pod', $_field ), null, true ) . '_' . $_field[ 'post_name' ] );
5825
-
5826
- if ( empty( $field ) ) {
5827
- $defaults = array(
5828
- 'type' => 'text'
5829
- );
5830
-
5831
- $field = array(
5832
- 'id' => $_field[ 'ID' ],
5833
- 'name' => $_field[ 'post_name' ],
5834
- 'label' => $_field[ 'post_title' ],
5835
- 'description' => $_field[ 'post_content' ],
5836
- 'weight' => $_field[ 'menu_order' ],
5837
- 'pod_id' => $_field[ 'post_parent' ],
5838
- 'pick_object' => '',
5839
- 'pick_val' => '',
5840
- 'sister_id' => '',
5841
- 'table_info' => array()
5842
- );
5843
-
5844
- if ( isset( $pod[ 'name' ] ) )
5845
- $field[ 'pod' ] = $pod[ 'name' ];
5846
- elseif ( isset( $_field[ 'pod' ] ) )
5847
- $field[ 'pod' ] = $_field[ 'pod' ];
5848
-
5849
- $field[ 'options' ] = get_post_meta( $field[ 'id' ] );
5850
-
5851
- $options_ignore = array(
5852
- 'method',
5853
- 'table_info',
5854
- 'attributes',
5855
- 'group',
5856
- 'grouped',
5857
- 'developer_mode',
5858
- 'dependency',
5859
- 'depends-on',
5860
- 'excludes-on'
5861
- );
5862
-
5863
- foreach ( $options_ignore as $ignore ) {
5864
- if ( isset( $field[ 'options' ][ $ignore ] ) )
5865
- unset( $field[ 'options' ][ $ignore ] );
5866
- }
5867
-
5868
- foreach ( $field[ 'options' ] as $option => $value ) {
5869
- if ( is_array( $value ) ) {
5870
- foreach ( $value as $k => $v ) {
5871
- if ( !is_array( $v ) )
5872
- $value[ $k ] = maybe_unserialize( $v );
5873
- }
5874
-
5875
- if ( 1 == count( $value ) )
5876
- $value = current( $value );
5877
- }
5878
- else
5879
- $value = maybe_unserialize( $value );
5880
-
5881
- $field[ 'options' ][ $option ] = $value;
5882
- }
5883
-
5884
- $field[ 'options' ] = array_merge( $defaults, $field[ 'options' ] );
5885
-
5886
- $field[ 'type' ] = $field[ 'options' ][ 'type' ];
5887
-
5888
- unset( $field[ 'options' ][ 'type' ] );
5889
-
5890
- if ( isset( $field[ 'options' ][ 'pick_object' ] ) ) {
5891
- $field[ 'pick_object' ] = $field[ 'options' ][ 'pick_object' ];
5892
-
5893
- unset( $field[ 'options' ][ 'pick_object' ] );
5894
- }
5895
-
5896
- if ( isset( $field[ 'options' ][ 'pick_val' ] ) ) {
5897
- $field[ 'pick_val' ] = $field[ 'options' ][ 'pick_val' ];
5898
-
5899
- unset( $field[ 'options' ][ 'pick_val' ] );
5900
- }
5901
-
5902
- if ( isset( $field[ 'options' ][ 'sister_id' ] ) ) {
5903
- $field[ 'sister_id' ] = $field[ 'options' ][ 'sister_id' ];
5904
-
5905
- unset( $field[ 'options' ][ 'sister_id' ] );
5906
- }
5907
-
5908
- if ( isset( $field[ 'options' ][ 'sister_field_id' ] ) )
5909
- unset( $field[ 'options' ][ 'sister_field_id' ] );
5910
-
5911
- if ( pods_api_cache() && ( isset( $pod[ 'name' ] ) || isset( $_field[ 'pod' ] ) ) )
5912
- pods_transient_set( 'pods_field_' . pods_var( 'name', $pod, pods_var( 'pod', $_field ), null, true ) . '_' . $field[ 'name' ], $field );
5913
- }
5914
- }
5915
-
5916
- $field[ 'table_info' ] = array();
5917
-
5918
- if ( 'pick' == $field[ 'type' ] && $params->table_info )
5919
- $field[ 'table_info' ] = $this->get_table_info( $field[ 'pick_object' ], $field[ 'pick_val' ], null, null, $field );
5920
-
5921
- return $field;
5922
- }
5923
-
5924
- /**
5925
- * Load fields by Pod, ID, Name, and/or Type
5926
- *
5927
- * $params['pod_id'] int The Pod ID
5928
- * $params['pod'] string The Pod name
5929
- * $params['id'] array The field IDs
5930
- * $params['name'] array The field names
5931
- * $params['type'] array The field types
5932
- * $params['options'] array Field Option(s) key=>value array to filter by
5933
- * $params['where'] string WHERE clause of query
5934
- *
5935
- * @param array $params An associative array of parameters
5936
- * @param bool $strict Whether to require a field exist or not when loading the info
5937
- *
5938
- * @return array Array of field data.
5939
- *
5940
- * @since 1.7.9
5941
- */
5942
- public function load_fields ( $params, $strict = false ) {
5943
- // @todo Get away from using md5/serialize, I'm sure we can cache specific parts
5944
- $cache_key = md5( serialize( $params ) );
5945
- if ( isset( $this->fields_cache[ $cache_key ] ) ) {
5946
- return $this->fields_cache[ $cache_key ];
5947
- }
5948
-
5949
- $params = (object) pods_sanitize( $params );
5950
-
5951
- if ( !isset( $params->pod ) || empty( $params->pod ) )
5952
- $params->pod = '';
5953
-
5954
- if ( !isset( $params->pod_id ) || empty( $params->pod_id ) )
5955
- $params->pod_id = 0;
5956
-
5957
- if ( !isset( $params->name ) || empty( $params->name ) )
5958
- $params->name = array();
5959
- else
5960
- $params->name = (array) $params->name;
5961
-
5962
- if ( !isset( $params->id ) || empty( $params->id ) )
5963
- $params->id = array();
5964
- else {
5965
- $params->id = (array) $params->id;
5966
-
5967
- foreach ( $params->id as &$id ) {
5968
- $id = pods_absint( $id );
5969
- }
5970
- }
5971
-
5972
- if ( !isset( $params->type ) || empty( $params->type ) )
5973
- $params->type = array();
5974
- else
5975
- $params->type = (array) $params->type;
5976
-
5977
- $fields = array();
5978
-
5979
- if ( !empty( $params->pod ) || !empty( $params->pod_id ) ) {
5980
- $pod = $this->load_pod( array( 'name' => $params->pod, 'id' => $params->pod_id, 'table_info' => true ), false );
5981
-
5982
- if ( false === $pod ) {
5983
- if ( $strict )
5984
- return pods_error( __( 'Pod not found', 'pods' ), $this );
5985
-
5986
- return $fields;
5987
- }
5988
-
5989
- $pod[ 'fields' ] = array_merge( pods_var_raw( 'object_fields', $pod, array() ), $pod[ 'fields' ] );
5990
-
5991
- foreach ( $pod[ 'fields' ] as $field ) {
5992
- if ( empty( $params->name ) && empty( $params->id ) && empty( $params->type ) )
5993
- $fields[ $field[ 'name' ] ] = $field;
5994
- elseif ( in_array( $fields[ 'name' ], $params->name ) || in_array( $fields[ 'id' ], $params->id ) || in_array( $fields[ 'type' ], $params->type ) )
5995
- $fields[ $field[ 'name' ] ] = $field;
5996
- }
5997
- }
5998
- elseif ( ( isset( $params->options ) && !empty( $params->options ) && is_array( $params->options ) ) || ( isset( $params->where ) && !empty( $params->where ) && is_array( $params->where ) ) ) {
5999
- $order = 'ASC';
6000
- $orderby = 'menu_order title';
6001
- $limit = -1;
6002
- $ids = false;
6003
-
6004
- $meta_query = array();
6005
-
6006
- if ( isset( $params->options ) && !empty( $params->options ) && is_array( $params->options ) ) {
6007
- foreach ( $params->options as $option => $value ) {
6008
- if ( !is_array( $value ) )
6009
- $value = array( $value );
6010
-
6011
- $value = pods_trim( $value );
6012
-
6013
- sort( $value );
6014
-
6015
- $meta_query[] = array(
6016
- 'key' => $option,
6017
- 'value' => pods_sanitize( $value ),
6018
- 'compare' => 'IN'
6019
- );
6020
- }
6021
- }
6022
-
6023
- if ( isset( $params->where ) && !empty( $params->where ) && is_array( $params->where ) )
6024
- $meta_query = array_merge( $meta_query, (array) $params->where );
6025
-
6026
- $args = array(
6027
- 'post_type' => '_pods_field',
6028
- 'nopaging' => true,
6029
- 'posts_per_page' => $limit,
6030
- 'order' => $order,
6031
- 'orderby' => $orderby,
6032
- 'meta_query' => $meta_query,
6033
- );
6034
-
6035
- // Only set post__in if there are ids to filter (see https://core.trac.wordpress.org/ticket/28099)
6036
- if ( false != $ids ) {
6037
- $args[ 'post__in' ] = $ids;
6038
- }
6039
-
6040
- $fields = array();
6041
-
6042
- $_fields = get_posts( $args );
6043
-
6044
- foreach ( $_fields as $field ) {
6045
- $field = $this->load_field( $field, false );
6046
-
6047
- if ( !empty( $field ) )
6048
- $fields[ $field[ 'id' ] ] = $field;
6049
- }
6050
- }
6051
- else {
6052
- if ( empty( $params->name ) && empty( $params->id ) && empty( $params->type ) )
6053
- return pods_error( __( 'Either Field Name / Field ID / Field Type, or Pod Name / Pod ID are required', 'pods' ), $this );
6054
-
6055
- $lookup = array();
6056
-
6057
- if ( !empty( $params->name ) ) {
6058
- $fields = implode( "', '", $params->name );
6059
-
6060
- $lookup[] = "`post_name` IN ( '{$fields}' )";
6061
- }
6062
-
6063
- if ( !empty( $params->id ) ) {
6064
- $fields = implode( ", ", $params->id );
6065
-
6066
- $lookup[] = "`ID` IN ( {$fields} )";
6067
- }
6068
-
6069
- $lookup = implode( ' AND ', $lookup );
6070
-
6071
- $result = pods_query( "SELECT `ID`, `post_name`, `post_parent` FROM `@wp_posts` WHERE `post_type` = '_pods_field' AND ( {$lookup} )" );
6072
-
6073
- $fields = array();
6074
-
6075
- if ( !empty( $result ) ) {
6076
- foreach ( $result as $field ) {
6077
- $field = $this->load_field( array(
6078
- 'id' => $field->ID,
6079
- 'name' => $field->post_name,
6080
- 'pod_id' => $field->post_parent
6081
- ), false );
6082
-
6083
- if ( !empty( $field ) && ( empty( $params->type ) || in_array( $field[ 'type' ], $params->type ) ) )
6084
- $fields[ $field[ 'id' ] ] = $field;
6085
- }
6086
- }
6087
- }
6088
- if ( isset( $cache_key ) ) {
6089
- $this->fields_cache[ $cache_key ] = $fields;
6090
- }
6091
- return $fields;
6092
- }
6093
-
6094
- /**
6095
- * Load a Pods Object
6096
- *
6097
- * $params['id'] int The Object ID
6098
- * $params['name'] string The Object name
6099
- * $params['type'] string The Object type
6100
- *
6101
- * @param array|object $params An associative array of parameters
6102
- * @param bool $strict
6103
- *
6104
- * @return array|bool
6105
- * @since 2.0
6106
- */
6107
- public function load_object ( $params, $strict = false ) {
6108
- if ( is_object( $params ) && isset( $params->post_title ) )
6109
- $_object = get_object_vars( $params );
6110
- else {
6111
- $params = (object) pods_sanitize( $params );
6112
-
6113
- if ( !isset( $params->type ) || empty( $params->type ) )
6114
- return pods_error( __( 'Object type is required', 'pods' ), $this );
6115
-
6116
- if ( ( !isset( $params->id ) || empty( $params->id ) ) && ( !isset( $params->name ) || empty( $params->name ) ) )
6117
- return pods_error( __( 'Either Object ID or Name are required', 'pods' ), $this );
6118
-
6119
- /**
6120
- * @var $wpdb wpdb
6121
- */
6122
- global $wpdb;
6123
-
6124
- if ( isset( $params->name ) )
6125
- $_object = pods_by_title( $params->name, ARRAY_A, '_pods_' . $params->type, 'publish' );
6126
- else {
6127
- $object = $params->id;
6128
-
6129
- $_object = get_post( $object, ARRAY_A );
6130
- }
6131
-
6132
- if ( empty( $_object ) ) {
6133
- if ( $strict )
6134
- return pods_error( __( 'Object not found', 'pods' ), $this );
6135
-
6136
- return false;
6137
- }
6138
- }
6139
-
6140
- $object = array(
6141
- 'id' => $_object[ 'ID' ],
6142
- 'name' => $_object[ 'post_title' ],
6143
- 'code' => $_object[ 'post_content' ],
6144
- 'type' => str_replace( '_pods_', '', $_object[ 'post_type' ] ),
6145
- 'slug' => $_object[ 'post_name' ]
6146
- );
6147
-
6148
- $object[ 'options' ] = get_post_meta( $object[ 'id' ] );
6149
-
6150
- foreach ( $object[ 'options' ] as $option => &$value ) {
6151
- if ( is_array( $value ) && 1 == count( $value ) )
6152
- $value = current( $value );
6153
- }
6154
-
6155
- return $object;
6156
- }
6157
-
6158
- /**
6159
- * Load Multiple Pods Objects
6160
- *
6161
- * $params['type'] string The Object type
6162
- * $params['options'] array Pod Option(s) key=>value array to filter by
6163
- * $params['orderby'] string ORDER BY clause of query
6164
- * $params['limit'] string Number of objects to return
6165
- * $params['where'] string WHERE clause of query
6166
- * $params['ids'] string|array IDs of Objects
6167
- *
6168
- * @param array|object $params An associative array of parameters
6169
- *
6170
- * @return array
6171
- * @since 2.0
6172
- */
6173
- public function load_objects ( $params ) {
6174
- $params = (object) pods_sanitize( $params );
6175
-
6176
- if ( !isset( $params->type ) || empty( $params->type ) )
6177
- return pods_error( __( 'Pods Object type is required', 'pods' ), $this );
6178
-
6179
- $order = 'ASC';
6180
- $orderby = 'menu_order';
6181
- $limit = -1;
6182
- $ids = false;
6183
-
6184
- $meta_query = array();
6185
- $cache_key = '';
6186
-
6187
- if ( isset( $params->options ) && !empty( $params->options ) && is_array( $params->options ) ) {
6188
- foreach ( $params->options as $option => $value ) {
6189
- if ( !is_array( $value ) )
6190
- $value = array( $value );
6191
-
6192
- $value = pods_trim( $value );
6193
-
6194
- sort( $value );
6195
-
6196
- $meta_query[] = array(
6197
- 'key' => $option,
6198
- 'value' => pods_sanitize( $value ),
6199
- 'compare' => 'IN'
6200
- );
6201
- }
6202
- }
6203
-
6204
- if ( isset( $params->where ) && is_array( $params->where ) )
6205
- $meta_query = array_merge( $meta_query, (array) $params->where );
6206
-
6207
- if ( isset( $params->order ) && !empty( $params->order ) && in_array( strtoupper( $params->order ), array( 'ASC', 'DESC' ) ) )
6208
- $order = strtoupper( $params->order );
6209
-
6210
- if ( isset( $params->orderby ) && !empty( $params->orderby ) )
6211
- $orderby = strtoupper( $params->orderby );
6212
-
6213
- if ( isset( $params->limit ) && !empty( $params->limit ) )
6214
- $limit = pods_absint( $params->limit );
6215
-
6216
- if ( isset( $params->ids ) && !empty( $params->ids ) ) {
6217
- $ids = $params->ids;
6218
-
6219
- if ( !is_array( $ids ) )
6220
- $ids = explode( ',', $ids );
6221
- }
6222
-
6223
- if ( empty( $ids ) )
6224
- $ids = false;
6225
-
6226
- if ( pods_api_cache() && empty( $meta_query ) && empty( $limit ) && ( empty( $orderby ) || 'menu_order' == $orderby ) && empty( $ids ) ) {
6227
- $cache_key = 'pods_objects_' . $params->type;
6228
-
6229
- $the_objects = pods_transient_get( $cache_key );
6230
-
6231
- if ( false !== $the_objects )
6232
- return $the_objects;
6233
- }
6234
-
6235
- $the_objects = array();
6236
-
6237
- $args = array(
6238
- 'post_type' => '_pods_' . $params->type,
6239
- 'nopaging' => true,
6240
- 'posts_per_page' => $limit,
6241
- 'order' => $order,
6242
- 'orderby' => $orderby,
6243
- 'meta_query' => $meta_query,
6244
- );
6245
-
6246
- // Only set post__in if there are ids to filter (see https://core.trac.wordpress.org/ticket/28099)
6247
- if ( false != $ids ) {
6248
- $args[ 'post__in' ] = $ids;
6249
- }
6250
-
6251
- $objects = get_posts( $args );
6252
-
6253
- foreach ( $objects as $object ) {
6254
- $object = $this->load_object( $object );
6255
-
6256
- $the_objects[ $object[ 'name' ] ] = $object;
6257
- }
6258
-
6259
- if ( pods_api_cache() && !empty( $cache_key ) )
6260
- pods_transient_set( $cache_key, $the_objects );
6261
-
6262
- return $the_objects;
6263
- }
6264
-
6265
- /**
6266
- * @see PodsAPI::load_object
6267
- *
6268
- * Load a Pod Template
6269
- *
6270
- * $params['id'] int The template ID
6271
- * $params['name'] string The template name
6272
- *
6273
- * @param array $params An associative array of parameters
6274
- *
6275
- * @return array|bool
6276
- * @since 1.7.9
6277
- */
6278
- public function load_template ( $params ) {
6279
- if ( !class_exists( 'Pods_Templates' ) )
6280
- return false;
6281
-
6282
- $params = (object) $params;
6283
- $params->type = 'template';
6284
- return $this->load_object( $params );
6285
- }
6286
-
6287
- /**
6288
- * @see PodsAPI::load_objects
6289
- *
6290
- * Load Multiple Pod Templates
6291
- *
6292
- * $params['where'] string The WHERE clause of query
6293
- * $params['options'] array Pod Option(s) key=>value array to filter by
6294
- * $params['orderby'] string ORDER BY clause of query
6295
- * $params['limit'] string Number of templates to return
6296
- *
6297
- * @param array $params (optional) An associative array of parameters
6298
- *
6299
- * @return array
6300
- *
6301
- * @since 2.0
6302
- */
6303
- public function load_templates ( $params = null ) {
6304
- if ( !class_exists( 'Pods_Templates' ) )
6305
- return array();
6306
-
6307
- $params = (object) $params;
6308
- $params->type = 'template';
6309
- return $this->load_objects( $params );
6310
- }
6311
-
6312
- /**
6313
- * @see PodsAPI::load_object
6314
- *
6315
- * Load a Pod Page
6316
- *
6317
- * $params['id'] int The page ID
6318
- * $params['name'] string The page URI
6319
- *
6320
- * @param array $params An associative array of parameters
6321
- *
6322
- * @return array|bool
6323
- *
6324
- * @since 1.7.9
6325
- */
6326
- public function load_page ( $params ) {
6327
- if ( !class_exists( 'Pods_Pages' ) )
6328
- return false;
6329
-
6330
- $params = (object) $params;
6331
- if ( !isset( $params->name ) && isset( $params->uri ) ) {
6332
- $params->name = $params->uri;
6333
- unset( $params->uri );
6334
- }
6335
- $params->type = 'page';
6336
- return $this->load_object( $params );
6337
- }
6338
-
6339
- /**
6340
- * @see PodsAPI::load_objects
6341
- *
6342
- * Load Multiple Pod Pages
6343
- *
6344
- * $params['where'] string The WHERE clause of query
6345
- * $params['options'] array Pod Option(s) key=>value array to filter by
6346
- * $params['orderby'] string ORDER BY clause of query
6347
- * $params['limit'] string Number of pages to return
6348
- *
6349
- * @param array $params (optional) An associative array of parameters
6350
- *
6351
- * @return array
6352
- *
6353
- * @since 2.0
6354
- */
6355
- public function load_pages ( $params = null ) {
6356
- if ( !class_exists( 'Pods_Pages' ) )
6357
- return array();
6358
-
6359
- $params = (object) $params;
6360
- $params->type = 'page';
6361
- return $this->load_objects( $params );
6362
- }
6363
-
6364
- /**
6365
- * @see PodsAPI::load_object
6366
- *
6367
- * Load a Pod Helper
6368
- *
6369
- * $params['id'] int The helper ID
6370
- * $params['name'] string The helper name
6371
- *
6372
- * @param array $params An associative array of parameters
6373
- *
6374
- * @return array|bool
6375
- *
6376
- * @since 1.7.9
6377
- */
6378
- public function load_helper ( $params ) {
6379
- if ( !class_exists( 'Pods_Helpers' ) )
6380
- return false;
6381
-
6382
- $params = (object) $params;
6383
- $params->type = 'helper';
6384
- return $this->load_object( $params );
6385
- }
6386
-
6387
- /**
6388
- * @see PodsAPI::load_objects
6389
- *
6390
- * Load Multiple Pod Helpers
6391
- *
6392
- * $params['where'] string The WHERE clause of query
6393
- * $params['options'] array Pod Option(s) key=>value array to filter by
6394
- * $params['orderby'] string ORDER BY clause of query
6395
- * $params['limit'] string Number of pages to return
6396
- *
6397
- * @param array $params (optional) An associative array of parameters
6398
- *
6399
- * @return array
6400
- *
6401
- * @since 2.0
6402
- */
6403
- public function load_helpers ( $params = null ) {
6404
- if ( !class_exists( 'Pods_Helpers' ) )
6405
- return array();
6406
-
6407
- $params = (object) $params;
6408
- $params->type = 'helper';
6409
- return $this->load_objects( $params );
6410
- }
6411
-
6412
- /**
6413
- * Load the pod item object
6414
- *
6415
- * $params['pod'] string The datatype name
6416
- * $params['id'] int (optional) The item's ID
6417
- *
6418
- * @param array $params An associative array of parameters
6419
- *
6420
- * @return bool|\Pods
6421
- *
6422
- * @uses pods()
6423
- *
6424
- * @since 2.0
6425
- */
6426
- public function load_pod_item ( $params ) {
6427
- $params = (object) pods_sanitize( $params );
6428
-
6429
- if ( !isset( $params->pod ) || empty( $params->pod ) )
6430
- return pods_error( __( 'Pod name required', 'pods' ), $this );
6431
- if ( !isset( $params->id ) || empty( $params->id ) )
6432
- return pods_error( __( 'Item ID required', 'pods' ), $this );
6433
-
6434
- $pod = false;
6435
-
6436
- if ( pods_api_cache() )
6437
- $pod = pods_cache_get( $params->id, 'pods_item_object_' . $params->pod );
6438
-
6439
- if ( false !== $pod )
6440
- return $pod;
6441
-
6442
- $pod = pods( $params->pod, $params->id );
6443
-
6444
- if ( pods_api_cache() )
6445
- pods_cache_set( $params->id, $pod, 'pods_item_object_' . $params->pod );
6446
-
6447
- return $pod;
6448
- }
6449
-
6450
- /**
6451
- * Load potential sister fields for a specific field
6452
- *
6453
- * $params['pod'] int The Pod name
6454
- * $params['related_pod'] string The related Pod name
6455
- *
6456
- * @param array $params An associative array of parameters
6457
- * @param array $pod (optional) Array of Pod data to use (to avoid lookup)
6458
- *
6459
- * @return array|bool
6460
- *
6461
- * @since 1.7.9
6462
- *
6463
- * @uses PodsAPI::load_pod
6464
- */
6465
- public function load_sister_fields ( $params, $pod = null ) {
6466
- $params = (object) pods_sanitize( $params );
6467
-
6468
- if ( empty( $pod ) ) {
6469
- $pod = $this->load_pod( array( 'name' => $params->pod, 'table_info' => false ), false );
6470
-
6471
- if ( false === $pod )
6472
- return pods_error( __( 'Pod not found', 'pods' ), $this );
6473
- }
6474
-
6475
- $params->pod_id = $pod[ 'id' ];
6476
- $params->pod = $pod[ 'name' ];
6477
-
6478
- $type = false;
6479
-
6480
- if ( 0 === strpos( $params->related_pod, 'pod-' ) ) {
6481
- $params->related_pod = pods_str_replace( 'pod-', '', $params->related_pod, 1 );
6482
- $type = 'pod';
6483
- }
6484
- elseif ( 0 === strpos( $params->related_pod, 'post_type-' ) ) {
6485
- $params->related_pod = pods_str_replace( 'post_type-', '', $params->related_pod, 1 );
6486
- $type = 'post_type';
6487
- }
6488
- elseif ( 0 === strpos( $params->related_pod, 'taxonomy-' ) ) {
6489
- $params->related_pod = pods_str_replace( 'taxonomy-', '', $params->related_pod, 1 );
6490
- $type = 'taxonomy';
6491
- }
6492
-
6493
- $related_pod = $this->load_pod( array( 'name' => $params->related_pod, 'table_info' => false ), false );
6494
-
6495
- if ( false === $related_pod || ( false !== $type && 'pod' != $type && $type != $related_pod[ 'type' ] ) )
6496
- return pods_error( __( 'Related Pod not found', 'pods' ), $this );
6497
-
6498
- $params->related_pod_id = $related_pod[ 'id' ];
6499
- $params->related_pod = $related_pod[ 'name' ];
6500
-
6501
- $sister_fields = array();
6502
-
6503
- foreach ( $related_pod[ 'fields' ] as $field ) {
6504
- if ( 'pick' == $field[ 'type' ] && in_array( $field[ 'pick_object' ], array( $pod[ 'type' ], 'pod' ) ) && ( $params->pod == $field[ 'pick_object' ] || $params->pod == $field[ 'pick_val' ] ) )
6505
- $sister_fields[ $field[ 'id' ] ] = esc_html( $field[ 'label' ] . ' (' . $field[ 'name' ] . ')' );
6506
- }
6507
-
6508
- return $sister_fields;
6509
- }
6510
-
6511
- /**
6512
- * Takes a sql field such as tinyint and returns the pods field type, such as num.
6513
- *
6514
- * @param string $sql_field The SQL field to look for
6515
- *
6516
- * @return string The field type
6517
- *
6518
- * @since 2.0
6519
- */
6520
- public static function detect_pod_field_from_sql_data_type ( $sql_field ) {
6521
- $sql_field = strtolower( $sql_field );
6522
-
6523
- $field_to_field_map = array(
6524
- 'tinyint' => 'number',
6525
- 'smallint' => 'number',
6526
- 'mediumint' => 'number',
6527
- 'int' => 'number',
6528
- 'bigint' => 'number',
6529
- 'float' => 'number',
6530
- 'double' => 'number',
6531
- 'decimal' => 'number',
6532
- 'date' => 'date',
6533
- 'datetime' => 'datetime',
6534
- 'timestamp' => 'datetime',
6535
- 'time' => 'time',
6536
- 'year' => 'date',
6537
- 'varchar' => 'text',
6538
- 'text' => 'paragraph',
6539
- 'mediumtext' => 'paragraph',
6540
- 'longtext' => 'paragraph'
6541
- );
6542
-
6543
- return ( array_key_exists( $sql_field, $field_to_field_map ) ) ? $field_to_field_map[ $sql_field ] : 'paragraph';
6544
- }
6545
-
6546
- /**
6547
- * Gets all field types
6548
- *
6549
- * @return array Array of field types
6550
- *
6551
- * @uses PodsForm::field_loader
6552
- *
6553
- * @since 2.0
6554
- * @deprecated
6555
- */
6556
- public function get_field_types () {
6557
- return PodsForm::field_types();
6558
- }
6559
-
6560
- /**
6561
- * Gets the schema definition of a field.
6562
- *
6563
- * @param string $type Field type to look for
6564
- * @param array $options (optional) Options of the field to pass to the schema function.
6565
- *
6566
- * @return array|bool|mixed|null
6567
- *
6568
- * @since 2.0
6569
- */
6570
- private function get_field_definition ( $type, $options = null ) {
6571
- $definition = PodsForm::field_method( $type, 'schema', $options );
6572
-
6573
- return $this->do_hook( 'field_definition', $definition, $type, $options );
6574
- }
6575
-
6576
- /**
6577
- * @see PodsForm:validate
6578
- *
6579
- * Validates the value of a field.
6580
- *
6581
- * @param mixed $value The value to validate
6582
- * @param string $field Field to use for validation
6583
- * @param array $object_fields Fields of the object we're validating
6584
- * @param array $fields Array of all fields data
6585
- * @param array|Pods $pod Array of pod data (or Pods object)
6586
- * @param array|object $params Extra parameters to pass to the validation function of the field.
6587
- *
6588
- * @return array|bool
6589
- *
6590
- * @uses PodsForm::validate
6591
- *
6592
- * @since 2.0
6593
- */
6594
- public function handle_field_validation ( &$value, $field, $object_fields, $fields, $pod, $params ) {
6595
- $tableless_field_types = PodsForm::tableless_field_types();
6596
-
6597
- $fields = array_merge( $fields, $object_fields );
6598
-
6599
- $options = $fields[ $field ];
6600
-
6601
- $id = ( is_object( $params ) ? $params->id : ( is_object( $pod ) ? $pod->id() : 0 ) );
6602
-
6603
- if ( is_object( $pod ) )
6604
- $pod = $pod->pod_data;
6605
-
6606
- $type = $options[ 'type' ];
6607
- $label = $options[ 'label' ];
6608
- $label = empty( $label ) ? $field : $label;
6609
-
6610
- // Verify required fields
6611
- if ( 1 == pods_var( 'required', $options[ 'options' ], 0 ) && 'slug' != $type ) {
6612
- if ( '' == $value || null === $value || array() === $value )
6613
- return pods_error( sprintf( __( '%s is empty', 'pods' ), $label ), $this );
6614
-
6615
- if ( 'multi' == pods_var( 'pick_format_type', $options[ 'options' ] ) && 'autocomplete' != pods_var( 'pick_format_multi', $options[ 'options' ] ) ) {
6616
- $has_value = false;
6617
-
6618
- $check_value = (array) $value;
6619
-
6620
- foreach ( $check_value as $val ) {
6621
- if ( '' != $val && null !== $val && 0 !== $val && '0' !== $val ) {
6622
- $has_value = true;
6623
-
6624
- continue;
6625
- }
6626
- }
6627
-
6628
- if ( !$has_value )
6629
- return pods_error( sprintf( __( '%s is required', 'pods' ), $label ), $this );
6630
- }
6631
-
6632
- }
6633
-
6634
- // @todo move this to after pre-save preparations
6635
- // Verify unique fields
6636
- if ( 1 == pods_var( 'unique', $options[ 'options' ], 0 ) && '' !== $value && null !== $value && array() !== $value ) {
6637
- if ( empty( $pod ) )
6638
- return false;
6639
-
6640
- if ( !in_array( $type, $tableless_field_types ) ) {
6641
- $exclude = '';
6642
-
6643
- if ( !empty( $id ) )
6644
- $exclude = "AND `id` != {$id}";
6645
-
6646
- $check = false;
6647
-
6648
- $check_value = pods_sanitize( $value );
6649
-
6650
- // @todo handle meta-based fields
6651
- // Trigger an error if not unique
6652
- if ( 'table' == $pod[ 'storage' ] )
6653
- $check = pods_query( "SELECT `id` FROM `@wp_pods_" . $pod[ 'name' ] . "` WHERE `{$field}` = '{$check_value}' {$exclude} LIMIT 1", $this );
6654
-
6655
- if ( !empty( $check ) )
6656
- return pods_error( sprintf( __( '%s needs to be unique', 'pods' ), $label ), $this );
6657
- }
6658
- else {
6659
- // @todo handle tableless check
6660
- }
6661
- }
6662
-
6663
- $validate = PodsForm::validate( $options[ 'type' ], $value, $field, array_merge( $options, pods_var( 'options', $options, array() ) ), $fields, $pod, $id, $params );
6664
-
6665
- $validate = $this->do_hook( 'field_validation', $validate, $value, $field, $object_fields, $fields, $pod, $params );
6666
-
6667
- return $validate;
6668
- }
6669
-
6670
- /**
6671
- * Find items related to a parent field
6672
- *
6673
- * @param int $field_id The Field ID
6674
- * @param int $pod_id The Pod ID
6675
- * @param mixed $ids A comma-separated string (or array) of item IDs
6676
- * @param array $field Field data array
6677
- * @param array $pod Pod data array
6678
- *
6679
- * @return array
6680
- *
6681
- * @since 2.0
6682
- *
6683
- * @uses pods_query()
6684
- */
6685
- public function lookup_related_items ( $field_id, $pod_id, $ids, $field = null, $pod = null ) {
6686
- $related_ids = array();
6687
-
6688
- if ( !is_array( $ids ) )
6689
- $ids = explode( ',', $ids );
6690
-
6691
- foreach ( $ids as $k => $id ) {
6692
- $ids[ $k ] = (int) $id;
6693
- }
6694
-
6695
- $ids = array_unique( array_filter( $ids ) );
6696
-
6697
- $idstring = implode( ',', $ids );
6698
- if ( 0 != $pod_id && 0 != $field_id && isset( self::$related_item_cache[ $pod_id ][ $field_id ][ $idstring ] ) ) {
6699
- // Check cache first, no point in running the same query multiple times
6700
- return self::$related_item_cache[ $pod_id ][ $field_id ][ $idstring ];
6701
- }
6702
-
6703
- $tableless_field_types = PodsForm::tableless_field_types();
6704
-
6705
- $field_type = pods_v( 'type', $field );
6706
-
6707
- if ( empty( $ids ) || !in_array( $field_type, $tableless_field_types ) )
6708
- return array();
6709
-
6710
- $related_pick_limit = 0;
6711
-
6712
- if ( empty( $field ) ) {
6713
- $field = $this->load_field( array( 'id' => $field_id ) );
6714
- }
6715
-
6716
- if ( !empty( $field ) ) {
6717
- $options = (array) pods_var_raw( 'options', $field, $field, null, true );
6718
-
6719
- $related_pick_limit = (int) pods_v( $field_type . '_limit', $options, 0 );
6720
-
6721
- if ( 'single' == pods_var_raw( $field_type . '_format_type', $options ) )
6722
- $related_pick_limit = 1;
6723
-
6724
- // Temporary hack until there's some better handling here
6725
- $related_pick_limit = $related_pick_limit * count( $ids );
6726
- }
6727
-
6728
- if ( 'taxonomy' == $field_type ) {
6729
- $related = wp_get_object_terms( $ids, pods_v( 'name', $field ), array( 'fields' => 'ids' ) );
6730
-
6731
- if ( !is_wp_error( $related ) ) {
6732
- $related_ids = $related;
6733
- }
6734
- }
6735
- elseif ( !pods_tableless() ) {
6736
- $ids = implode( ', ', $ids );
6737
-
6738
- $field_id = (int) $field_id;
6739
- $sister_id = (int) pods_var_raw( 'sister_id', $field, 0 );
6740
-
6741
- $related_where = "
6742
- `field_id` = {$field_id}
6743
- AND `item_id` IN ( {$ids} )
6744
- ";
6745
-
6746
- $sql = "
6747
- SELECT item_id, related_item_id, related_field_id
6748
- FROM `@wp_podsrel`
6749
- WHERE
6750
- {$related_where}
6751
- ORDER BY `weight`
6752
- ";
6753
-
6754
- $relationships = pods_query( $sql );
6755
-
6756
- if ( !empty( $relationships ) ) {
6757
- foreach ( $relationships as $relation ) {
6758
- if ( !in_array( $relation->related_item_id, $related_ids ) )
6759
- $related_ids[] = (int) $relation->related_item_id;
6760
- elseif ( 0 < $sister_id && $field_id == $relation->related_field_id && !in_array( $relation->item_id, $related_ids ) )
6761
- $related_ids[] = (int) $relation->item_id;
6762
- }
6763
- }
6764
- }
6765
- else {
6766
- if ( !is_array( $pod ) )
6767
- $pod = $this->load_pod( array( 'id' => $pod_id, 'table_info' => false ), false );
6768
-
6769
- if ( !empty( $pod ) && in_array( $pod[ 'type' ], array( 'post_type', 'media', 'user', 'comment', 'settings' ) ) ) {
6770
- $meta_type = $pod[ 'type' ];
6771
-
6772
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'media' ) ) )
6773
- $meta_type = 'post';
6774
-
6775
- $no_conflict = pods_no_conflict_check( $meta_type );
6776
-
6777
- if ( !$no_conflict )
6778
- pods_no_conflict_on( $meta_type );
6779
-
6780
- foreach ( $ids as $id ) {
6781
- if ( 'settings' == $meta_type ) {
6782
- $related_id = get_option( '_pods_' . $pod[ 'name' ] . '_' . $field[ 'name' ] );
6783
-
6784
- if ( empty( $related_id ) )
6785
- $related_id = get_option( $pod[ 'name' ] . '_' . $field[ 'name' ] );
6786
-
6787
- if ( is_array( $related_id ) && !empty( $related_id ) ) {
6788
- foreach ( $related_id as $related ) {
6789
- if ( is_array( $related ) && !empty( $related ) ) {
6790
- if ( isset( $related[ 'id' ] ) )
6791
- $related_ids[] = (int) $related[ 'id' ];
6792
- else {
6793
- foreach ( $related as $r ) {
6794
- $related_ids[] = (int) $r;
6795
- }
6796
- }
6797
- }
6798
- else
6799
- $related_ids[] = (int) $related;
6800
- }
6801
- }
6802
- }
6803
- else {
6804
- $related_id = get_metadata( $meta_type, $id, '_pods_' . $field[ 'name' ], true );
6805
-
6806
- if ( empty( $related_id ) )
6807
- $related_id = get_metadata( $meta_type, $id, $field[ 'name' ] );
6808
-
6809
- if ( is_array( $related_id ) && !empty( $related_id ) ) {
6810
- foreach ( $related_id as $related ) {
6811
- if ( is_array( $related ) && !empty( $related ) ) {
6812
- if ( isset( $related[ 'id' ] ) )
6813
- $related_ids[] = (int) $related[ 'id' ];
6814
- else {
6815
- foreach ( $related as $r ) {
6816
- if ( isset( $related[ 'id' ] ) )
6817
- $related_ids[] = (int) $r[ 'id' ];
6818
- else
6819
- $related_ids[] = (int) $r;
6820
- }
6821
- }
6822
- }
6823
- else
6824
- $related_ids[] = (int) $related;
6825
- }
6826
- }
6827
- }
6828
- }
6829
-
6830
- if ( !$no_conflict )
6831
- pods_no_conflict_off( $meta_type );
6832
- }
6833
- }
6834
-
6835
- if ( is_array( $related_ids ) ) {
6836
- $related_ids = array_unique( array_filter( $related_ids ) );
6837
-
6838
- if ( 0 < $related_pick_limit && !empty( $related_ids ) )
6839
- $related_ids = array_slice( $related_ids, 0, $related_pick_limit );
6840
- }
6841
- if ( 0 != $pod_id && 0 != $field_id && ! empty( $related_ids ) ) {
6842
- // Only cache if $pod_id and $field_id were passed
6843
- self::$related_item_cache[ $pod_id ][ $field_id ][ $idstring ] = $related_ids;
6844
- }
6845
-
6846
- return $related_ids;
6847
- }
6848
-
6849
- /**
6850
- * Find related items related to an item
6851
- *
6852
- * @param int $field_id The Field ID
6853
- * @param int $pod_id The Pod ID
6854
- * @param int $id Item ID to get related IDs from
6855
- * @param array $field Field data array
6856
- * @param array $pod Pod data array
6857
- *
6858
- * @return array|bool
6859
- *
6860
- * @since 2.3
6861
- *
6862
- * @uses pods_query()
6863
- */
6864
- public function lookup_related_items_from ( $field_id, $pod_id, $id, $field = null, $pod = null ) {
6865
- $related_ids = false;
6866
-
6867
- $id = (int) $id;
6868
-
6869
- $tableless_field_types = PodsForm::tableless_field_types();
6870
-
6871
- if ( empty( $id ) || !in_array( pods_v( 'type', $field ), $tableless_field_types ) )
6872
- return false;
6873
-
6874
- $related_pick_limit = 0;
6875
-
6876
- if ( !empty( $field ) ) {
6877
- $options = (array) pods_var_raw( 'options', $field, $field, null, true );
6878
-
6879
- $related_pick_limit = (int) pods_v( 'pick_limit', $options, 0 );
6880
-
6881
- if ( 'single' == pods_var_raw( 'pick_format_type', $options ) )
6882
- $related_pick_limit = 1;
6883
- }
6884
-
6885
- if ( !pods_tableless() ) {
6886
- $field_id = (int) $field_id;
6887
- $sister_id = (int) pods_var_raw( 'sister_id', $field, 0 );
6888
-
6889
- $related_where = "
6890
- `field_id` = {$field_id}
6891
- AND `related_item_id` = {$id}
6892
- ";
6893
-
6894
- $sql = "
6895
- SELECT *
6896
- FROM `@wp_podsrel`
6897
- WHERE
6898
- {$related_where}
6899
- ORDER BY `weight`
6900
- ";
6901
-
6902
- $relationships = pods_query( $sql );
6903
-
6904
- if ( !empty( $relationships ) ) {
6905
- $related_ids = array();
6906
-
6907
- foreach ( $relationships as $relation ) {
6908
- if ( $field_id == $relation->field_id && !in_array( $relation->item_id, $related_ids ) )
6909
- $related_ids[] = (int) $relation->item_id;
6910
- elseif ( 0 < $sister_id && $field_id == $relation->related_field_id && !in_array( $relation->related_item_id, $related_ids ) )
6911
- $related_ids[] = (int) $relation->related_item_id;
6912
- }
6913
- }
6914
- }
6915
- else {
6916
- // @todo handle meta-based lookups
6917
- return false;
6918
-
6919
- if ( !is_array( $pod ) )
6920
- $pod = $this->load_pod( array( 'id' => $pod_id, 'table_info' => false ), false );
6921
-
6922
- if ( !empty( $pod ) && in_array( $pod[ 'type' ], array( 'post_type', 'media', 'user', 'comment', 'settings' ) ) ) {
6923
- $related_ids = array();
6924
-
6925
- $meta_type = $pod[ 'type' ];
6926
-
6927
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'media' ) ) )
6928
- $meta_type = 'post';
6929
-
6930
- $no_conflict = pods_no_conflict_check( $meta_type );
6931
-
6932
- if ( !$no_conflict )
6933
- pods_no_conflict_on( $meta_type );
6934
-
6935
- if ( 'settings' == $meta_type ) {
6936
- $related_id = get_option( '_pods_' . $pod[ 'name' ] . '_' . $field[ 'name' ] );
6937
-
6938
- if ( empty( $related_id ) )
6939
- $related_id = get_option( $pod[ 'name' ] . '_' . $field[ 'name' ] );
6940
-
6941
- if ( is_array( $related_id ) && !empty( $related_id ) ) {
6942
- foreach ( $related_id as $related ) {
6943
- if ( is_array( $related ) && !empty( $related ) ) {
6944
- if ( isset( $related[ 'id' ] ) )
6945
- $related_ids[] = (int) $related[ 'id' ];
6946
- else {
6947
- foreach ( $related as $r ) {
6948
- $related_ids[] = (int) $r;
6949
- }
6950
- }
6951
- }
6952
- else
6953
- $related_ids[] = (int) $related;
6954
- }
6955
- }
6956
- }
6957
- else {
6958
- $related_id = get_metadata( $meta_type, $id, '_pods_' . $field[ 'name' ], true );
6959
-
6960
- if ( empty( $related_id ) )
6961
- $related_id = get_metadata( $meta_type, $id, $field[ 'name' ] );
6962
-
6963
- if ( is_array( $related_id ) && !empty( $related_id ) ) {
6964
- foreach ( $related_id as $related ) {
6965
- if ( is_array( $related ) && !empty( $related ) ) {
6966
- if ( isset( $related[ 'id' ] ) )
6967
- $related_ids[] = (int) $related[ 'id' ];
6968
- else {
6969
- foreach ( $related as $r ) {
6970
- if ( isset( $related[ 'id' ] ) )
6971
- $related_ids[] = (int) $r[ 'id' ];
6972
- else
6973
- $related_ids[] = (int) $r;
6974
- }
6975
- }
6976
- }
6977
- else
6978
- $related_ids[] = (int) $related;
6979
- }
6980
- }
6981
- }
6982
-
6983
- if ( !$no_conflict )
6984
- pods_no_conflict_off( $meta_type );
6985
- }
6986
- }
6987
-
6988
- if ( is_array( $related_ids ) )
6989
- $related_ids = array_unique( array_filter( $related_ids ) );
6990
-
6991
- return $related_ids;
6992
- }
6993
-
6994
- /**
6995
- *
6996
- * Load the information about an objects MySQL table
6997
- *
6998
- * @param $object_type
6999
- * @param string $object The object to look for
7000
- * @param null $name (optional) Name of the pod to load
7001
- * @param array $pod (optional) Array with pod information
7002
- *
7003
- * @return array
7004
- *
7005
- * @since 2.5
7006
- */
7007
- public function get_table_info_load ( $object_type, $object, $name = null, $pod = null ) {
7008
-
7009
- $info = array();
7010
-
7011
- if ( 'pod' == $object_type && null === $pod ) {
7012
- if ( empty( $name ) ) {
7013
- $prefix = 'pod-';
7014
-
7015
- // Make sure we actually have the prefix before trying anything with the name
7016
- if ( 0 === strpos( $object_type, $prefix ) )
7017
- $name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
7018
- }
7019
-
7020
- if ( empty( $name ) && !empty( $object ) )
7021
- $name = $object;
7022
-
7023
- $pod = $this->load_pod( array( 'name' => $name, 'table_info' => false ), false );
7024
-
7025
- if ( !empty( $pod ) ) {
7026
- $object_type = $pod[ 'type' ];
7027
- $name = $pod[ 'name' ];
7028
- $object = $pod[ 'object' ];
7029
-
7030
- $info[ 'pod' ] = $pod;
7031
- }
7032
- }
7033
- elseif ( null === $pod ) {
7034
- if ( empty( $name ) ) {
7035
- $prefix = $object_type . '-';
7036
-
7037
- // Make sure we actually have the prefix before trying anything with the name
7038
- if ( 0 === strpos( $object_type, $prefix ) )
7039
- $name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
7040
- }
7041
-
7042
- if ( empty( $name ) && !empty( $object ) )
7043
- $name = $object;
7044
-
7045
- if ( !empty( $name ) ) {
7046
- $pod = $this->load_pod( array( 'name' => $name, 'table_info' => false ), false );
7047
-
7048
- if ( !empty( $pod ) && ( null === $object_type || $object_type == $pod[ 'type' ] ) ) {
7049
- $object_type = $pod[ 'type' ];
7050
- $name = $pod[ 'name' ];
7051
- $object = $pod[ 'object' ];
7052
-
7053
- $info[ 'pod' ] = $pod;
7054
- }
7055
- }
7056
- }
7057
- elseif ( !empty( $pod ) )
7058
- $info[ 'pod' ] = $pod;
7059
-
7060
- if ( 0 === strpos( $object_type, 'pod' ) ) {
7061
- if ( empty( $name ) ) {
7062
- $prefix = 'pod-';
7063
-
7064
- // Make sure we actually have the prefix before trying anything with the name
7065
- if ( 0 === strpos( $object_type, $prefix ) )
7066
- $name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
7067
- }
7068
-
7069
- $info[ 'type' ] = 'pod';
7070
- global $wpdb;
7071
-
7072
- $info[ 'table' ] = $info[ 'meta_table' ] = $wpdb->prefix . 'pods_' . ( empty( $object ) ? $name : $object );
7073
-
7074
- if ( is_array( $info[ 'pod' ] ) && 'pod' == pods_v( 'type', $info[ 'pod' ] ) ) {
7075
- $info[ 'pod_field_index' ] = $info[ 'field_index' ] = $info[ 'meta_field_index' ] = $info[ 'meta_field_value' ] = pods_v( 'pod_index', $info[ 'pod' ][ 'options' ], 'id', true );
7076
-
7077
- $slug_field = get_posts( array(
7078
- 'post_type' => '_pods_field',
7079
- 'posts_per_page' => 1,
7080
- 'nopaging' => true,
7081
- 'post_parent' => $info[ 'pod' ][ 'id' ],
7082
- 'orderby' => 'menu_order',
7083
- 'order' => 'ASC',
7084
- 'meta_query' => array(
7085
- array(
7086
- 'key' => 'type',
7087
- 'value' => 'slug',
7088
- )
7089
- )
7090
- ) );
7091
-
7092
- if ( !empty( $slug_field ) ) {
7093
- $slug_field = $slug_field[ 0 ];
7094
-
7095
- $info[ 'field_slug' ] = $info[ 'pod_field_slug' ] = $slug_field->post_name;
7096
- }
7097
-
7098
- if ( 1 == pods_v( 'hierarchical', $info[ 'pod' ][ 'options' ], 0 ) ) {
7099
- $parent_field = pods_v( 'pod_parent', $info[ 'pod' ][ 'options' ], 'id', true );
7100
-
7101
- if ( !empty( $parent_field ) && isset( $info[ 'pod' ][ 'fields' ][ $parent_field ] ) ) {
7102
- $info[ 'object_hierarchical' ] = true;
7103
-
7104
- $info[ 'pod_field_parent' ] = $info[ 'field_parent' ] = $parent_field . '_select';
7105
- $info[ 'field_parent_select' ] = '`' . $parent_field . '`.`id` AS `' . $info[ 'field_parent' ] . '`';
7106
- }
7107
- }
7108
- }
7109
- }
7110
- return $info;
7111
- }
7112
-
7113
- /**
7114
- * Get information about an objects MySQL table
7115
- *
7116
- * @param string $object_type
7117
- * @param string $object The object to look for
7118
- * @param null $name (optional) Name of the pod to load
7119
- * @param array $pod (optional) Array with pod information
7120
- * @param array $field (optional) Array with field information
7121
- *
7122
- * @return array|bool
7123
- *
7124
- * @since 2.0
7125
- */
7126
- public function get_table_info ( $object_type, $object, $name = null, $pod = null, $field = null ) {
7127
- /**
7128
- * @var $wpdb wpdb
7129
- * @var $sitepress SitePress
7130
- * @var $icl_adjust_id_url_filter_off boolean
7131
- */
7132
- global $wpdb, $sitepress, $icl_adjust_id_url_filter_off, $polylang;
7133
-
7134
- // @todo Handle $object arrays for Post Types, Taxonomies, Comments (table pulled from first object in array)
7135
-
7136
- $info = array(
7137
- //'select' => '`t`.*',
7138
- 'object_type' => $object_type,
7139
- 'type' => null,
7140
- 'object_name' => $object,
7141
- 'object_hierarchical' => false,
7142
-
7143
- 'table' => $object,
7144
- 'meta_table' => $object,
7145
- 'pod_table' => $wpdb->prefix . 'pods_' . ( empty( $object ) ? $name : $object ),
7146
-
7147
- 'field_id' => 'id',
7148
- 'field_index' => 'name',
7149
- 'field_slug' => null,
7150
- 'field_type' => null,
7151
- 'field_parent' => null,
7152
- 'field_parent_select' => null,
7153
-
7154
- 'meta_field_id' => 'id',
7155
- 'meta_field_index' => 'name',
7156
- 'meta_field_value' => 'name',
7157
-
7158
- 'pod_field_id' => 'id',
7159
- 'pod_field_index' => 'name',
7160
- 'pod_field_slug' => null,
7161
- 'pod_field_parent' => null,
7162
-
7163
- 'join' => array(),
7164
-
7165
- 'where' => null,
7166
- 'where_default' => null,
7167
-
7168
- 'orderby' => null,
7169
-
7170
- 'pod' => null,
7171
- 'recurse' => false
7172
- );
7173
-
7174
- if ( empty( $object_type ) ) {
7175
- $object_type = 'post_type';
7176
- $object = 'post';
7177
- }
7178
- elseif ( empty( $object ) && in_array( $object_type, array( 'user', 'media', 'comment' ) ) ) {
7179
- $object = $object_type;
7180
- }
7181
-
7182
- $pod_name = $pod;
7183
-
7184
- if ( is_array( $pod_name ) )
7185
- $pod_name = pods_var_raw( 'name', $pod_name, ( version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $pod_name, JSON_UNESCAPED_UNICODE ) : json_encode( $pod_name ) ), null, true );
7186
- else {
7187
- $pod_name = $object;
7188
- }
7189
-
7190
- $field_name = $field;
7191
-
7192
- if ( is_array( $field_name ) )
7193
- $field_name = pods_var_raw( 'name', $field_name, ( version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $pod_name, JSON_UNESCAPED_UNICODE ) : json_encode( $field_name ) ), null, true );
7194
-
7195
- $transient = 'pods_' . $wpdb->prefix . '_get_table_info_' . md5( $object_type . '_object_' . $object . '_name_' . $name . '_pod_' . $pod_name . '_field_' . $field_name );
7196
-
7197
- $current_language = false;
7198
- $current_language_t_id = $current_language_tt_id = 0;
7199
-
7200
- // WPML support
7201
- if ( is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
7202
- $current_language = pods_sanitize( ICL_LANGUAGE_CODE );
7203
- // Polylang support
7204
- elseif ( is_object( $polylang ) && function_exists( 'pll_current_language' ) ) {
7205
- $current_language = pods_sanitize( pll_current_language( 'slug' ) );
7206
-
7207
- if ( !empty( $current_language ) ) {
7208
- $current_language_t_id = (int) $polylang->get_language( $current_language )->term_id;
7209
- $current_language_tt_id = (int) $polylang->get_language( $current_language )->term_taxonomy_id;
7210
- }
7211
- }
7212
-
7213
- if ( !empty( $current_language ) )
7214
- $transient = 'pods_' . $wpdb->prefix . '_get_table_info_' . $current_language . '_' . md5( $object_type . '_object_' . $object . '_name_' . $name . '_pod_' . $pod_name . '_field_' . $field_name );
7215
-
7216
- $_info = false;
7217
-
7218
- if ( isset( self::$table_info_cache[ $transient ] ) ) {
7219
- // Prefer info from the object internal cache
7220
- $_info = self::$table_info_cache[ $transient ];
7221
- } elseif ( pods_api_cache() ) {
7222
- $_info = pods_transient_get( $transient );
7223
- if ( false === $_info && ! did_action( 'init' ) ) {
7224
- $_info = pods_transient_get( $transient . '_pre_init' );
7225
- }
7226
- }
7227
-
7228
- if ( false !== $_info ) {
7229
- // Data was cached, use that
7230
- $info = $_info;
7231
- } else {
7232
- // Data not cached, load it up
7233
- $_info = $this->get_table_info_load( $object_type, $object, $name, $pod );
7234
- if ( isset( $_info[ 'type' ] ) ) {
7235
- // Allow function to override $object_type
7236
- $object_type = $_info[ 'type' ];
7237
- }
7238
- $info = array_merge( $info, $_info );
7239
- }
7240
-
7241
- if ( 0 === strpos( $object_type, 'post_type' ) || 'media' == $object_type || in_array( pods_var_raw( 'type', $info[ 'pod' ] ), array( 'post_type', 'media' ) ) ) {
7242
- $info[ 'table' ] = $wpdb->posts;
7243
- $info[ 'meta_table' ] = $wpdb->postmeta;
7244
-
7245
- $info[ 'field_id' ] = 'ID';
7246
- $info[ 'field_index' ] = 'post_title';
7247
- $info[ 'field_slug' ] = 'post_name';
7248
- $info[ 'field_type' ] = 'post_type';
7249
- $info[ 'field_parent' ] = 'post_parent';
7250
- $info[ 'field_parent_select' ] = '`t`.`' . $info[ 'field_parent' ] . '`';
7251
-
7252
- $info[ 'meta_field_id' ] = 'post_id';
7253
- $info[ 'meta_field_index' ] = 'meta_key';
7254
- $info[ 'meta_field_value' ] = 'meta_value';
7255
-
7256
- if ( 'media' == $object_type )
7257
- $object = 'attachment';
7258
-
7259
- if ( empty( $name ) ) {
7260
- $prefix = 'post_type-';
7261
-
7262
- // Make sure we actually have the prefix before trying anything with the name
7263
- if ( 0 === strpos( $object_type, $prefix ) )
7264
- $name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
7265
- }
7266
-
7267
- if ( 'media' != $object_type )
7268
- $object_type = 'post_type';
7269
-
7270
- $post_type = pods_sanitize( ( empty( $object ) ? $name : $object ) );
7271
-
7272
- if ( 'attachment' == $post_type || 'media' == $object_type )
7273
- $info[ 'pod_table' ] = $wpdb->prefix . 'pods_media';
7274
- else
7275
- $info[ 'pod_table' ] = $wpdb->prefix . 'pods_' . pods_clean_name( $post_type, true, false );
7276
-
7277
- $post_type_object = get_post_type_object( $post_type );
7278
-
7279
- if ( is_object( $post_type_object ) && $post_type_object->hierarchical )
7280
- $info[ 'object_hierarchical' ] = true;
7281
-
7282
- /**
7283
- * Default Post Status to query for.
7284
- *
7285
- * Use to change "default" post status from publish to any other status or statuses.
7286
- *
7287
- * @param array $post_status List of post statuses. Default is 'publish'
7288
- * @param string $post_type Post type of current object
7289
- * @param array $info Array of information about the object.
7290
- * @param string $object Type of object
7291
- * @param string $name Name of pod to load
7292
- * @param array $pod Array with Pod information. Result of PodsAPI::load_pod()
7293
- * @param array $field Array with field information
7294
- *
7295
- * @since unknown
7296
- */
7297
- $post_status = apply_filters( 'pods_api_get_table_info_default_post_status', array( 'publish' ), $post_type, $info, $object_type, $object, $name, $pod, $field );
7298
-
7299
- $info[ 'where' ] = array(
7300
- //'post_status' => '`t`.`post_status` IN ( "inherit", "publish" )', // @todo Figure out what statuses Attachments can be
7301
- 'post_type' => '`t`.`' . $info[ 'field_type' ] . '` = "' . $post_type . '"'
7302
- );
7303
-
7304
- if ( 'post_type' == $object_type )
7305
- $info[ 'where_default' ] = '`t`.`post_status` IN ( "' . implode( '", "', $post_status ) . '" )';
7306
-
7307
- $info[ 'orderby' ] = '`t`.`menu_order`, `t`.`' . $info[ 'field_index' ] . '`, `t`.`post_date`';
7308
-
7309
- // WPML support
7310
- if ( is_object( $sitepress ) && $sitepress->is_translated_post_type( $post_type ) && !$icl_adjust_id_url_filter_off ) {
7311
- $info[ 'join' ][ 'wpml_translations' ] = "
7312
- LEFT JOIN `{$wpdb->prefix}icl_translations` AS `wpml_translations`
7313
- ON `wpml_translations`.`element_id` = `t`.`ID`
7314
- AND `wpml_translations`.`element_type` = 'post_{$post_type}'
7315
- AND `wpml_translations`.`language_code` = '{$current_language}'
7316
- ";
7317
-
7318
- $info[ 'join' ][ 'wpml_languages' ] = "
7319
- LEFT JOIN `{$wpdb->prefix}icl_languages` AS `wpml_languages`
7320
- ON `wpml_languages`.`code` = `wpml_translations`.`language_code` AND `wpml_languages`.`active` = 1
7321
- ";
7322
-
7323
- $info[ 'where' ][ 'wpml_languages' ] = "`wpml_languages`.`code` IS NOT NULL";
7324
- }
7325
- // Polylang support
7326
- elseif( is_object( $polylang ) && !empty( $current_language ) && function_exists( 'pll_is_translated_post_type' ) && pll_is_translated_post_type( $post_type ) ) {
7327
- $info[ 'join' ][ 'polylang_languages' ] = "
7328
- LEFT JOIN `{$wpdb->term_relationships}` AS `polylang_languages`
7329
- ON `polylang_languages`.`object_id` = `t`.`ID`
7330
- AND `polylang_languages`.`term_taxonomy_id` = {$current_language_tt_id}
7331
- ";
7332
-
7333
- $info[ 'where' ][ 'polylang_languages' ] = "`polylang_languages`.`object_id` IS NOT NULL";
7334
- }
7335
-
7336
- $info[ 'object_fields' ] = $this->get_wp_object_fields( $object_type, $info[ 'pod' ] );
7337
- }
7338
- elseif ( 0 === strpos( $object_type, 'taxonomy' ) || in_array( $object_type, array( 'nav_menu', 'post_format' ) ) || 'taxonomy' == pods_var_raw( 'type', $info[ 'pod' ] ) ) {
7339
- $info[ 'table' ] = $info[ 'meta_table' ] = $wpdb->terms;
7340
-
7341
- $info[ 'join' ][ 'tt' ] = "LEFT JOIN `{$wpdb->term_taxonomy}` AS `tt` ON `tt`.`term_id` = `t`.`term_id`";
7342
- $info[ 'field_id' ] = $info[ 'meta_field_id' ] = 'term_id';
7343
- $info[ 'field_index' ] = $info[ 'meta_field_index' ] = $info[ 'meta_field_value' ] = 'name';
7344
- $info[ 'field_slug' ] = 'slug';
7345
- $info[ 'field_type' ] = 'taxonomy';
7346
- $info[ 'field_parent' ] = 'parent';
7347
- $info[ 'field_parent_select' ] = '`tt`.`' . $info[ 'field_parent' ] . '`';
7348
-
7349
- if ( 'nav_menu' == $object_type )
7350
- $object = 'nav_menu';
7351
- elseif ( 'post_format' == $object_type )
7352
- $object = 'post_format';
7353
-
7354
- if ( empty( $name ) ) {
7355
- $prefix = 'taxonomy-';
7356
-
7357
- // Make sure we actually have the prefix before trying anything with the name
7358
- if ( 0 === strpos( $object_type, $prefix ) )
7359
- $name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
7360
- }
7361
-
7362
- if ( !in_array( $object_type, array( 'nav_menu', 'post_format' ) ) )
7363
- $object_type = 'taxonomy';
7364
-
7365
- $taxonomy = pods_sanitize( ( empty( $object ) ? $name : $object ) );
7366
-
7367
- $info[ 'pod_table' ] = $wpdb->prefix . 'pods_' . pods_clean_name( $taxonomy, true, false );
7368
-
7369
- $taxonomy_object = get_taxonomy( $taxonomy );
7370
-
7371
- if ( is_object( $taxonomy_object ) && $taxonomy_object->hierarchical )
7372
- $info[ 'object_hierarchical' ] = true;
7373
-
7374
- $info[ 'where' ] = array(
7375
- 'tt.taxonomy' => '`tt`.`' . $info[ 'field_type' ] . '` = "' . $taxonomy . '"'
7376
- );
7377
-
7378
- // WPML Support
7379
- if ( is_object( $sitepress ) && $sitepress->is_translated_taxonomy( $taxonomy ) && !$icl_adjust_id_url_filter_off ) {
7380
- $info[ 'join' ][ 'wpml_translations' ] = "
7381
- LEFT JOIN `{$wpdb->prefix}icl_translations` AS `wpml_translations`
7382
- ON `wpml_translations`.`element_id` = `tt`.`term_taxonomy_id`
7383
- AND `wpml_translations`.`element_type` = 'tax_{$taxonomy}'
7384
- AND `wpml_translations`.`language_code` = '{$current_language}'
7385
- ";
7386
-
7387
- $info[ 'join' ][ 'wpml_languages' ] = "
7388
- LEFT JOIN `{$wpdb->prefix}icl_languages` AS `wpml_languages`
7389
- ON `wpml_languages`.`code` = `wpml_translations`.`language_code` AND `wpml_languages`.`active` = 1
7390
- ";
7391
-
7392
- $info[ 'where' ][ 'wpml_languages' ] = "`wpml_languages`.`code` IS NOT NULL";
7393
- }
7394
- // Polylang support
7395
- elseif ( is_object( $polylang ) && !empty( $current_language ) && function_exists( 'pll_is_translated_taxonomy' ) && pll_is_translated_taxonomy( $taxonomy ) ) {
7396
- $info[ 'join' ][ 'polylang_languages' ] = "
7397
- LEFT JOIN `{$wpdb->termmeta}` AS `polylang_languages`
7398
- ON `polylang_languages`.`term_id` = `t`.`term_id`
7399
- AND `polylang_languages`.`meta_value` = {$current_language_t_id}
7400
- ";
7401
-
7402
- $info[ 'where' ][ 'polylang_languages' ] = "`polylang_languages`.`term_id` IS NOT NULL";
7403
- }
7404
-
7405
- $info[ 'object_fields' ] = $this->get_wp_object_fields( $object_type, $info[ 'pod' ] );
7406
- }
7407
- elseif ( 'user' == $object_type || 'user' == pods_var_raw( 'type', $info[ 'pod' ] ) ) {
7408
- $info[ 'table' ] = $wpdb->users;
7409
- $info[ 'meta_table' ] = $wpdb->usermeta;
7410
- $info[ 'pod_table' ] = $wpdb->prefix . 'pods_user';
7411
-
7412
- $info[ 'field_id' ] = 'ID';
7413
- $info[ 'field_index' ] = 'display_name';
7414
- $info[ 'field_slug' ] = 'user_nicename';
7415
-
7416
- $info[ 'meta_field_id' ] = 'user_id';
7417
- $info[ 'meta_field_index' ] = 'meta_key';
7418
- $info[ 'meta_field_value' ] = 'meta_value';
7419
-
7420
- $info[ 'where' ] = array(
7421
- 'user_status' => '`t`.`user_status` = 0'
7422
- );
7423
-
7424
- $info[ 'object_fields' ] = $this->get_wp_object_fields( $object_type, $info[ 'pod' ] );
7425
- }
7426
- elseif ( 'comment' == $object_type || 'comment' == pods_var_raw( 'type', $info[ 'pod' ] ) ) {
7427
- //$info[ 'object_hierarchical' ] = true;
7428
-
7429
- $info[ 'table' ] = $wpdb->comments;
7430
- $info[ 'meta_table' ] = $wpdb->commentmeta;
7431
- $info[ 'pod_table' ] = $wpdb->prefix . 'pods_comment';
7432
-
7433
- $info[ 'field_id' ] = 'comment_ID';
7434
- $info[ 'field_index' ] = 'comment_date';
7435
- $info[ 'field_type' ] = 'comment_type';
7436
- $info[ 'field_parent' ] = 'comment_parent';
7437
- $info[ 'field_parent_select' ] = '`t`.`' . $info[ 'field_parent' ] . '`';
7438
-
7439
- $info[ 'meta_field_id' ] = 'comment_id';
7440
- $info[ 'meta_field_index' ] = 'meta_key';
7441
- $info[ 'meta_field_value' ] = 'meta_value';
7442
-
7443
- $object = 'comment';
7444
-
7445
- $comment_type = ( empty( $object ) ? $name : $object );
7446
-
7447
- $comment_type_clause = '`t`.`' . $info[ 'field_type' ] . '` = "' . $comment_type . '"';
7448
-
7449
- if ( 'comment' == $comment_type ) {
7450
- $comment_type_clause = '( ' . $comment_type_clause . ' OR `t`.`' . $info[ 'field_type' ] . '` = "" )';
7451
- }
7452
-
7453
- $info[ 'where' ] = array(
7454
- 'comment_approved' => '`t`.`comment_approved` = 1',
7455
- 'comment_type' => $comment_type_clause
7456
- );
7457
-
7458
- $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '` DESC, `t`.`' . $info[ 'field_id' ] . '`';
7459
- }
7460
- elseif ( in_array( $object_type, array( 'option', 'settings' ) ) || 'settings' == pods_var_raw( 'type', $info[ 'pod' ] ) ) {
7461
- $info[ 'table' ] = $wpdb->options;
7462
- $info[ 'meta_table' ] = $wpdb->options;
7463
-
7464
- $info[ 'field_id' ] = 'option_id';
7465
- $info[ 'field_index' ] = 'option_name';
7466
-
7467
- $info[ 'meta_field_id' ] = 'option_id';
7468
- $info[ 'meta_field_index' ] = 'option_name';
7469
- $info[ 'meta_field_value' ] = 'option_value';
7470
-
7471
- $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '` ASC';
7472
- }
7473
- elseif ( is_multisite() && ( in_array( $object_type, array( 'site_option', 'site_settings' ) ) || 'site_settings' == pods_var_raw( 'type', $info[ 'pod' ] ) ) ) {
7474
- $info[ 'table' ] = $wpdb->sitemeta;
7475
- $info[ 'meta_table' ] = $wpdb->sitemeta;
7476
-
7477
- $info[ 'field_id' ] = 'site_id';
7478
- $info[ 'field_index' ] = 'meta_key';
7479
-
7480
- $info[ 'meta_field_id' ] = 'site_id';
7481
- $info[ 'meta_field_index' ] = 'meta_key';
7482
- $info[ 'meta_field_value' ] = 'meta_value';
7483
-
7484
- $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '` ASC';
7485
- }
7486
- elseif ( is_multisite() && 'network' == $object_type ) { // Network = Site
7487
- $info[ 'table' ] = $wpdb->site;
7488
- $info[ 'meta_table' ] = $wpdb->sitemeta;
7489
-
7490
- $info[ 'field_id' ] = 'id';
7491
- $info[ 'field_index' ] = 'domain';
7492
-
7493
- $info[ 'meta_field_id' ] = 'site_id';
7494
- $info[ 'meta_field_index' ] = 'meta_key';
7495
- $info[ 'meta_field_value' ] = 'meta_value';
7496
-
7497
- $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '` ASC, `t`.`path` ASC, `t`.`' . $info[ 'field_id' ] . '`';
7498
- }
7499
- elseif ( is_multisite() && 'site' == $object_type ) { // Site = Blog
7500
- $info[ 'table' ] = $wpdb->blogs;
7501
-
7502
- $info[ 'field_id' ] = 'blog_id';
7503
- $info[ 'field_index' ] = 'domain';
7504
- $info[ 'field_type' ] = 'site_id';
7505
-
7506
- $info[ 'where' ] = array(
7507
- 'archived' => '`t`.`archived` = 0',
7508
- 'spam' => '`t`.`spam` = 0',
7509
- 'deleted' => '`t`.`deleted` = 0',
7510
- 'site_id' => '`t`.`' . $info[ 'field_type' ] . '` = ' . (int) get_current_site()->id
7511
- );
7512
-
7513
- $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '` ASC, `t`.`path` ASC, `t`.`' . $info[ 'field_id' ] . '`';
7514
- }
7515
- elseif ( 'table' == $object_type || 'table' == pods_var_raw( 'type', $info[ 'pod' ] ) ) {
7516
- $info[ 'table' ] = ( empty( $object ) ? $name : $object );
7517
- $info[ 'pod_table' ] = $wpdb->prefix . 'pods_' . $info[ 'table' ];
7518
-
7519
- if ( !empty( $field ) && is_array( $field ) ) {
7520
- $info[ 'table' ] = pods_var_raw( 'pick_table', pods_var_raw( 'options', $field, $field ) );
7521
- $info[ 'field_id' ] = pods_var_raw( 'pick_table_id', pods_var_raw( 'options', $field, $field ) );
7522
- $info[ 'field_index' ] = $info[ 'meta_field_index' ] = $info[ 'meta_field_value' ] = pods_var_raw( 'pick_table_index', pods_var_raw( 'options', $field, $field ) );
7523
- }
7524
- }
7525
-
7526
- $info[ 'table' ] = pods_clean_name( $info[ 'table' ], false, false );
7527
- $info[ 'meta_table' ] = pods_clean_name( $info[ 'meta_table' ], false, false );
7528
- $info[ 'pod_table' ] = pods_clean_name( $info[ 'pod_table' ], false, false );
7529
-
7530
- $info[ 'field_id' ] = pods_clean_name( $info[ 'field_id' ], false, false );
7531
- $info[ 'field_index' ] = pods_clean_name( $info[ 'field_index' ], false, false );
7532
- $info[ 'field_slug' ] = pods_clean_name( $info[ 'field_slug' ], false, false );
7533
-
7534
- $info[ 'meta_field_id' ] = pods_clean_name( $info[ 'meta_field_id' ], false, false );
7535
- $info[ 'meta_field_index' ] = pods_clean_name( $info[ 'meta_field_index' ], false, false );
7536
- $info[ 'meta_field_value' ] = pods_clean_name( $info[ 'meta_field_value' ], false, false );
7537
-
7538
- if ( empty( $info[ 'orderby' ] ) )
7539
- $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '`, `t`.`' . $info[ 'field_id' ] . '`';
7540
-
7541
- if ( 'table' == pods_var_raw( 'storage', $info[ 'pod' ] ) && !in_array( $object_type, array( 'pod', 'table' ) ) ) {
7542
- $info[ 'join' ][ 'd' ] = 'LEFT JOIN `' . $info[ 'pod_table' ] . '` AS `d` ON `d`.`id` = `t`.`' . $info[ 'field_id' ] . '`';
7543
- //$info[ 'select' ] .= ', `d`.*';
7544
- }
7545
-
7546
- if ( !empty( $info[ 'pod' ] ) && is_array( $info[ 'pod' ] ) )
7547
- $info[ 'recurse' ] = true;
7548
-
7549
- $info[ 'type' ] = $object_type;
7550
- $info[ 'object_name' ] = $object;
7551
-
7552
- if ( pods_api_cache() ) {
7553
- if ( ! did_action( 'init' ) ) {
7554
- $transient .= '_pre_init';
7555
- }
7556
- pods_transient_set( $transient, $info );
7557
- }
7558
-
7559
-
7560
- self::$table_info_cache[ $transient ] = apply_filters( 'pods_api_get_table_info', $info, $object_type, $object, $name, $pod, $field, $this );
7561
-
7562
- return self::$table_info_cache[ $transient ];
7563
- }
7564
-
7565
- /**
7566
- * Export a package
7567
- *
7568
- * $params['pod'] string Pod Type IDs to export
7569
- * $params['template'] string Template IDs to export
7570
- * $params['podpage'] string Pod Page IDs to export
7571
- * $params['helper'] string Helper IDs to export
7572
- *
7573
- * @param array $params An associative array of parameters
7574
- *
7575
- * @return array|bool
7576
- *
7577
- * @since 1.9.0
7578
- * @deprecated 2.0
7579
- */
7580
- public function export_package ( $params ) {
7581
- if ( class_exists( 'Pods_Migrate_Packages' ) )
7582
- return Pods_Migrate_Packages::export( $params );
7583
-
7584
- return false;
7585
- }
7586
-
7587
- /**
7588
- * Replace an existing package
7589
- *
7590
- * @param mixed $data (optional) An associative array containing a package, or the json encoded package
7591
- *
7592
- * @return bool
7593
- *
7594
- * @since 1.9.8
7595
- * @deprecated 2.0
7596
- */
7597
- public function replace_package ( $data = false ) {
7598
- return $this->import_package( $data, true );
7599
- }
7600
-
7601
- /**
7602
- * Import a package
7603
- *
7604
- * @param mixed $data (optional) An associative array containing a package, or the json encoded package
7605
- * @param bool $replace (optional) Replace existing items when found
7606
- *
7607
- * @return bool
7608
- *
7609
- * @since 1.9.0
7610
- * @deprecated 2.0
7611
- */
7612
- public function import_package ( $data = false, $replace = false ) {
7613
- if ( class_exists( 'Pods_Migrate_Packages' ) )
7614
- return Pods_Migrate_Packages::import( $data, $replace );
7615
-
7616
- return false;
7617
- }
7618
-
7619
- /**
7620
- * Validate a package
7621
- *
7622
- * @param array|string $data (optional) An associative array containing a package, or the json encoded package
7623
- * @param bool $output (optional)
7624
- *
7625
- * @return array|bool
7626
- *
7627
- * @since 1.9.0
7628
- * @deprecated 2.0
7629
- */
7630
- public function validate_package ( $data = false, $output = false ) {
7631
- return true;
7632
- }
7633
-
7634
- /**
7635
- * Import data from an array or a CSV file.
7636
- *
7637
- * @param mixed $import_data PHP associative array or CSV input
7638
- * @param bool $numeric_mode Use IDs instead of the name field when matching
7639
- * @param string $format Format of import data, options are php or csv
7640
- *
7641
- * @return array IDs of imported items
7642
- *
7643
- * @since 1.7.1
7644
- * @todo This needs some love and use of table_info etc for relationships
7645
- */
7646
- public function import ( $import_data, $numeric_mode = false, $format = null ) {
7647
- /**
7648
- * @var $wpdb wpdb
7649
- */
7650
- global $wpdb;
7651
-
7652
- if ( null === $format && null !== $this->format )
7653
- $format = $this->format;
7654
-
7655
- if ( 'csv' == $format && !is_array( $import_data ) ) {
7656
- $data = pods_migrate( 'sv', ',' )->parse( $import_data );
7657
-
7658
- $import_data = $data[ 'items' ];
7659
- }
7660
-
7661
- pods_query( "SET NAMES utf8" );
7662
- pods_query( "SET CHARACTER SET utf8" );
7663
-
7664
- // Loop through the array of items
7665
- $ids = array();
7666
-
7667
- // Test to see if it's an array of arrays
7668
- if ( !is_array( @current( $import_data ) ) )
7669
- $import_data = array( $import_data );
7670
-
7671
- $pod = $this->load_pod( array( 'name' => $this->pod ) );
7672
-
7673
- if ( false === $pod )
7674
- return pods_error( __( 'Pod not found', 'pods' ), $this );
7675
-
7676
- $fields = array_merge( $pod[ 'fields' ], $pod[ 'object_fields' ] );
7677
-
7678
- $simple_tableless_objects = PodsForm::simple_tableless_objects();
7679
-
7680
- foreach ( $import_data as $key => $data_row ) {
7681
- $data = array();
7682
-
7683
- // Loop through each field (use $fields so only valid fields get parsed)
7684
- foreach ( $fields as $field_name => $field_data ) {
7685
- if ( !isset( $data_row[ $field_name ] ) && !isset( $data_row[ $field_data[ 'label' ] ] ) )
7686
- continue;
7687
-
7688
- $field_id = $field_data[ 'id' ];
7689
- $type = $field_data[ 'type' ];
7690
- $pick_object = isset( $field_data[ 'pick_object' ] ) ? $field_data[ 'pick_object' ] : '';
7691
- $pick_val = isset( $field_data[ 'pick_val' ] ) ? $field_data[ 'pick_val' ] : '';
7692
-
7693
- if ( isset( $data_row[ $field_name] ) )
7694
- $field_value = $data_row[ $field_name ];
7695
- else
7696
- $field_value = $data_row[ $field_data[ 'label' ] ];
7697
-
7698
- if ( null !== $field_value && false !== $field_value && '' !== $field_value ) {
7699
- if ( 'pick' == $type || in_array( $type, PodsForm::file_field_types() ) ) {
7700
- $field_values = is_array( $field_value ) ? $field_value : array( $field_value );
7701
- $pick_values = array();
7702
-
7703
- foreach ( $field_values as $pick_value ) {
7704
- if ( in_array( $type, PodsForm::file_field_types() ) || 'media' == $pick_object ) {
7705
- $where = "`guid` = '" . pods_sanitize( $pick_value ) . "'";
7706
-
7707
- if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode )
7708
- $where = "`ID` = " . pods_absint( $pick_value );
7709
-
7710
- $result = pods_query( "SELECT `ID` AS `id` FROM `{$wpdb->posts}` WHERE `post_type` = 'attachment' AND {$where} ORDER BY `ID`", $this );
7711
-
7712
- if ( !empty( $result ) )
7713
- $pick_values[] = $result[ 0 ]->id;
7714
- }
7715
- // @todo This could and should be abstracted better and simplified
7716
- elseif ( 'pick' == $type ) {
7717
- $related_pod = false;
7718
-
7719
- if ( 'pod' == $pick_object )
7720
- $related_pod = $this->load_pod( array( 'name' => $pick_val, 'table_info' => true ), false );
7721
-
7722
- if ( empty( $related_pod ) ) {
7723
- $related_pod = array(
7724
- 'id' => 0,
7725
- 'type' => $pick_object
7726
- );
7727
- }
7728
-
7729
- if ( in_array( 'taxonomy', array( $pick_object, $related_pod[ 'type' ] ) ) ) {
7730
- $where = "`t`.`name` = '" . pods_sanitize( $pick_value ) . "'";
7731
-
7732
- if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode )
7733
- $where = "`tt`.`term_id` = " . pods_absint( $pick_value );
7734
-
7735
- $result = pods_query( "SELECT `t`.`term_id` AS `id` FROM `{$wpdb->term_taxonomy}` AS `tt` LEFT JOIN `{$wpdb->terms}` AS `t` ON `t`.`term_id` = `tt`.`term_id` WHERE `taxonomy` = '{$pick_val}' AND {$where} ORDER BY `t`.`term_id`", $this );
7736
-
7737
- if ( !empty( $result ) )
7738
- $pick_values[] = $result[ 0 ]->id;
7739
- }
7740
- elseif ( in_array( 'post_type', array( $pick_object, $related_pod[ 'type' ] ) ) || in_array( 'media', array( $pick_object, $related_pod[ 'type' ] ) ) ) {
7741
- $where = "`post_title` = '" . pods_sanitize( $pick_value ) . "'";
7742
-
7743
- if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode )
7744
- $where = "`ID` = " . pods_absint( $pick_value );
7745
-
7746
- $result = pods_query( "SELECT `ID` AS `id` FROM `{$wpdb->posts}` WHERE `post_type` = '{$pick_val}' AND {$where} ORDER BY `ID`", $this );
7747
-
7748
- if ( !empty( $result ) )
7749
- $pick_values[] = $result[ 0 ]->id;
7750
- }
7751
- elseif ( in_array( 'user', array( $pick_object, $related_pod[ 'type' ] ) ) ) {
7752
- $where = "`user_login` = '" . pods_sanitize( $pick_value ) . "'";
7753
-
7754
- if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode )
7755
- $where = "`ID` = " . pods_absint( $pick_value );
7756
-
7757
- $result = pods_query( "SELECT `ID` AS `id` FROM `{$wpdb->users}` WHERE {$where} ORDER BY `ID`", $this );
7758
-
7759
- if ( !empty( $result ) )
7760
- $pick_values[] = $result[ 0 ]->id;
7761
- }
7762
- elseif ( in_array( 'comment', array( $pick_object, $related_pod[ 'type' ] ) ) ) {
7763
- $where = "`comment_ID` = " . pods_absint( $pick_value );
7764
-
7765
- $result = pods_query( "SELECT `comment_ID` AS `id` FROM `{$wpdb->comments}` WHERE {$where} ORDER BY `ID`", $this );
7766
-
7767
- if ( !empty( $result ) )
7768
- $pick_values[] = $result[ 0 ]->id;
7769
- }
7770
- elseif ( in_array( $pick_object, $simple_tableless_objects ) )
7771
- $pick_values[] = $pick_value;
7772
- elseif ( !empty( $related_pod[ 'id' ] ) ) {
7773
- $where = "`" . $related_pod[ 'field_index' ] . "` = '" . pods_sanitize( $pick_value ) . "'";
7774
-
7775
- if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode )
7776
- $where = "`" . $related_pod[ 'field_id' ] . "` = " . pods_absint( $pick_value );
7777
-
7778
- $result = pods_query( "SELECT `" . $related_pod[ 'field_id' ] . "` AS `id` FROM `" . $related_pod[ 'table' ] . "` WHERE {$where} ORDER BY `" . $related_pod[ 'field_id' ] . "`", $this );
7779
-
7780
- if ( !empty( $result ) )
7781
- $pick_values[] = $result[ 0 ]->id;
7782
- }
7783
- }
7784
- }
7785
-
7786
- $field_value = implode( ',', $pick_values );
7787
- }
7788
-
7789
- $data[ $field_name ] = $field_value;
7790
- }
7791
- }
7792
-
7793
- if ( !empty( $data ) ) {
7794
- $params = array(
7795
- 'pod' => $this->pod,
7796
- 'data' => $data
7797
- );
7798
-
7799
- $ids[] = $this->save_pod_item( $params );
7800
- }
7801
- }
7802
-
7803
- return $ids;
7804
- }
7805
-
7806
- /**
7807
- * Export data from a Pod
7808
- *
7809
- * @param string|object $pod The pod name or Pods object
7810
- * @param array $params An associative array of parameters
7811
- *
7812
- * @return array Data arrays of all exported pod items
7813
- * @since 1.7.1
7814
- */
7815
- public function export ( $pod = null, $params = null ) {
7816
-
7817
- if ( empty( $pod ) ) {
7818
- $pod = $this->pod;
7819
- }
7820
-
7821
- $find = array(
7822
- 'limit' => -1,
7823
- 'search' => false,
7824
- 'pagination' => false
7825
- );
7826
-
7827
- if ( !empty( $params ) && isset( $params[ 'params' ] ) ) {
7828
- $find = array_merge( $find, (array) $params[ 'params' ] );
7829
-
7830
- unset( $params[ 'params' ] );
7831
-
7832
- $pod = pods( $pod, $find );
7833
- }
7834
- elseif ( !is_object( $pod ) ) {
7835
- $pod = pods( $pod, $find );
7836
- }
7837
-
7838
- $data = array();
7839
-
7840
- while ( $pod->fetch() ) {
7841
- $data[ $pod->id() ] = $this->export_pod_item( $params, $pod );
7842
- }
7843
-
7844
- $data = $this->do_hook( 'export', $data, $pod->pod, $pod );
7845
-
7846
- return $data;
7847
- }
7848
-
7849
- /**
7850
- * Convert CSV to a PHP array
7851
- *
7852
- * @param string $data The CSV input
7853
- *
7854
- * @return array
7855
- * @since 1.7.1
7856
- *
7857
- * @deprecated 2.3.5
7858
- */
7859
- public function csv_to_php ( $data, $delimiter = ',' ) {
7860
- pods_deprecated( "PodsAPI->csv_to_php", '2.3.5' );
7861
-
7862
- $data = pods_migrate( 'sv', $delimiter, $data )->parse();
7863
-
7864
- return $data[ 'items' ];
7865
- }
7866
-
7867
- /**
7868
- * Clear Pod-related cache
7869
- *
7870
- * @param array $pod
7871
- *
7872
- * @return void
7873
- *
7874
- * @since 2.0
7875
- */
7876
- public function cache_flush_pods ( $pod = null ) {
7877
- /**
7878
- * @var $wpdb wpdb
7879
- */
7880
- global $wpdb;
7881
-
7882
- pods_transient_clear( 'pods' );
7883
- pods_transient_clear( 'pods_components' );
7884
-
7885
- if ( null !== $pod && is_array( $pod ) ) {
7886
- pods_transient_clear( 'pods_pod_' . $pod[ 'name' ] );
7887
- pods_cache_clear( $pod[ 'name' ], 'pods-class' );
7888
-
7889
- foreach ( $pod[ 'fields' ] as $field ) {
7890
- pods_transient_clear( 'pods_field_' . $pod[ 'name' ] . '_' . $field[ 'name' ] );
7891
- }
7892
-
7893
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) )
7894
- pods_transient_clear( 'pods_wp_cpt_ct' );
7895
- }
7896
- else
7897
- pods_transient_clear( 'pods_wp_cpt_ct' );
7898
-
7899
- $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE '_transient_pods%'" );
7900
- $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE '_transient_timeout_pods%'" );
7901
-
7902
- pods_cache_clear( true );
7903
-
7904
- pods_transient_set( 'pods_flush_rewrites', 1 );
7905
- }
7906
-
7907
- /**
7908
- * Process a Pod-based form
7909
- *
7910
- * @param mixed $params
7911
- * @param object $obj Pod object
7912
- * @param array $fields Fields being submitted in form ( key => settings )
7913
- * @param string $thank_you URL to send to upon success
7914
- *
7915
- * @return mixed
7916
- *
7917
- * @since 2.0
7918
- */
7919
- public function process_form ( $params, $obj = null, $fields = null, $thank_you = null ) {
7920
- $this->display_errors = false;
7921
-
7922
- $form = null;
7923
-
7924
- $nonce = pods_var( '_pods_nonce', $params );
7925
- $pod = pods_var( '_pods_pod', $params );
7926
- $id = pods_var( '_pods_id', $params );
7927
- $uri = pods_var( '_pods_uri', $params );
7928
- $form = pods_var( '_pods_form', $params );
7929
- $location = pods_var( '_pods_location', $params );
7930
-
7931
- if ( is_object( $obj ) ) {
7932
- $pod = $obj->pod;
7933
- $id = $obj->id();
7934
- }
7935
-
7936
- if ( !empty( $fields ) ) {
7937
- $fields = array_keys( $fields );
7938
- $form = implode( ',', $fields );
7939
- }
7940
- else
7941
- $fields = explode( ',', $form );
7942
-
7943
- if ( empty( $nonce ) || empty( $pod ) || empty( $uri ) || empty( $fields ) )
7944
- return pods_error( __( 'Invalid submission', 'pods' ), $this );
7945
-
7946
- $uid = @session_id();
7947
-
7948
- if ( is_user_logged_in() )
7949
- $uid = 'user_' . get_current_user_id();
7950
-
7951
- $field_hash = wp_create_nonce( 'pods_fields_' . $form );
7952
-
7953
- $action = 'pods_form_' . $pod . '_' . $uid . '_' . $id . '_' . $uri . '_' . $field_hash;
7954
-
7955
- if ( empty( $uid ) )
7956
- return pods_error( __( 'Access denied for your session, please refresh and try again.', 'pods' ), $this );
7957
-
7958
- if ( false === wp_verify_nonce( $nonce, $action ) )
7959
- return pods_error( __( 'Access denied, please refresh and try again.', 'pods' ), $this );
7960
-
7961
- $data = array();
7962
-
7963
- foreach ( $fields as $field ) {
7964
- $data[ $field ] = pods_var_raw( 'pods_field_' . $field, $params, '' );
7965
- }
7966
-
7967
- $params = array(
7968
- 'pod' => $pod,
7969
- 'id' => $id,
7970
- 'data' => $data,
7971
- 'from' => 'process_form',
7972
- 'location' => $location
7973
- );
7974
-
7975
- $id = $this->save_pod_item( $params );
7976
-
7977
- if ( 0 < $id && !empty( $thank_you ) ) {
7978
- $thank_you = str_replace( 'X_ID_X', $id, $thank_you );
7979
-
7980
- pods_redirect( $thank_you );
7981
- }
7982
-
7983
- return $id;
7984
- }
7985
-
7986
- /**
7987
- * Handle filters / actions for the class
7988
- *
7989
- * @since 2.0
7990
- */
7991
- private function do_hook () {
7992
- $args = func_get_args();
7993
- if ( empty( $args ) )
7994
- return false;
7995
- $name = array_shift( $args );
7996
- return pods_do_hook( "api", $name, $args, $this );
7997
- }
7998
-
7999
- /**
8000
- * Handle variables that have been deprecated
8001
- *
8002
- * @since 2.0
8003
- */
8004
- public function __get ( $name ) {
8005
- $name = (string) $name;
8006
-
8007
- if ( !isset( $this->deprecated ) ) {
8008
- require_once( PODS_DIR . 'deprecated/classes/PodsAPI.php' );
8009
- $this->deprecated = new PodsAPI_Deprecated( $this );
8010
- }
8011
-
8012
- $var = null;
8013
-
8014
- if ( isset( $this->deprecated->{$name} ) ) {
8015
- pods_deprecated( "PodsAPI->{$name}", '2.0' );
8016
-
8017
- $var = $this->deprecated->{$name};
8018
- }
8019
- else
8020
- pods_deprecated( "PodsAPI->{$name}", '2.0' );
8021
-
8022
- return $var;
8023
- }
8024
-
8025
- /**
8026
- * Handle methods that have been deprecated
8027
- *
8028
- * @since 2.0
8029
- */
8030
- public function __call ( $name, $args ) {
8031
- $name = (string) $name;
8032
-
8033
- if ( !isset( $this->deprecated ) ) {
8034
- require_once( PODS_DIR . 'deprecated/classes/PodsAPI.php' );
8035
- $this->deprecated = new PodsAPI_Deprecated( $this );
8036
- }
8037
-
8038
- if ( method_exists( $this->deprecated, $name ) )
8039
- return call_user_func_array( array( $this->deprecated, $name ), $args );
8040
- else
8041
- pods_deprecated( "PodsAPI::{$name}", '2.0' );
8042
- }
8043
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
classes/PodsAdmin.php DELETED
@@ -1,2409 +0,0 @@
1
- <?php
2
- /**
3
- * @package Pods
4
- */
5
- class PodsAdmin {
6
-
7
- /**
8
- * @var PodsAdmin
9
- */
10
- static $instance = null;
11
-
12
- /**
13
- * Singleton handling for a basic pods_admin() request
14
- *
15
- * @return \PodsAdmin
16
- *
17
- * @since 2.3.5
18
- */
19
- public static function init () {
20
- if ( !is_object( self::$instance ) )
21
- self::$instance = new PodsAdmin();
22
-
23
- return self::$instance;
24
- }
25
-
26
- /**
27
- * Setup and Handle Admin functionality
28
- *
29
- * @return \PodsAdmin
30
- *
31
- * @license http://www.gnu.org/licenses/gpl-2.0.html
32
- * @since 2.0
33
- */
34
- public function __construct () {
35
- // Scripts / Stylesheets
36
- add_action( 'admin_enqueue_scripts', array( $this, 'admin_head' ), 20 );
37
-
38
- // AJAX $_POST fix
39
- add_action( 'admin_init', array( $this, 'admin_init' ), 9 );
40
-
41
- // Menus
42
- add_action( 'admin_menu', array( $this, 'admin_menu' ), 99 );
43
-
44
- // AJAX for Admin
45
- add_action( 'wp_ajax_pods_admin', array( $this, 'admin_ajax' ) );
46
- add_action( 'wp_ajax_nopriv_pods_admin', array( $this, 'admin_ajax' ) );
47
-
48
- // Add Media Bar button for Shortcode
49
- add_action( 'media_buttons', array( $this, 'media_button' ), 12 );
50
-
51
- // Add the Pods capabilities
52
- add_filter( 'members_get_capabilities', array( $this, 'admin_capabilities' ) );
53
-
54
- add_action( 'admin_head-media-upload-popup', array( $this, 'register_media_assets' ) );
55
- }
56
-
57
- /**
58
- * Init the admin area
59
- *
60
- * @since 2.0
61
- */
62
- public function admin_init () {
63
- // Fix for plugins that *don't do it right* so we don't cause issues for users
64
- if ( defined( 'DOING_AJAX' ) && !empty( $_POST ) ) {
65
- $pods_admin_ajax_actions = array(
66
- 'pods_admin',
67
- 'pods_relationship',
68
- 'pods_upload',
69
- 'pods_admin_components'
70
- );
71
-
72
- $pods_admin_ajax_actions = apply_filters( 'pods_admin_ajax_actions', $pods_admin_ajax_actions );
73
-
74
- if ( in_array( pods_var( 'action', 'get' ), $pods_admin_ajax_actions ) || in_array( pods_var( 'action', 'post' ), $pods_admin_ajax_actions ) ) {
75
- foreach ( $_POST as $key => $value ) {
76
- if ( 'action' == $key || 0 === strpos( $key, '_podsfix_' ) )
77
- continue;
78
-
79
- unset( $_POST[ $key ] );
80
-
81
- $_POST[ '_podsfix_' . $key ] = $value;
82
- }
83
- }
84
- }
85
- }
86
-
87
- /**
88
- * Attach requirements to admin header
89
- *
90
- * @since 2.0
91
- */
92
- public function admin_head () {
93
- wp_register_style( 'pods-admin', PODS_URL . 'ui/css/pods-admin.css', array(), PODS_VERSION );
94
-
95
- wp_register_style( 'pods-font', PODS_URL . 'ui/css/pods-font.css', array(), PODS_VERSION );
96
-
97
- wp_register_script( 'pods-floatmenu', PODS_URL . 'ui/js/floatmenu.js', array(), PODS_VERSION );
98
-
99
- wp_register_script( 'pods-admin-importer', PODS_URL . 'ui/js/admin-importer.js', array(), PODS_VERSION );
100
-
101
- wp_register_style( 'pods-manage', PODS_URL . 'ui/css/pods-manage.css', array(), PODS_VERSION );
102
-
103
- wp_register_style( 'pods-wizard', PODS_URL . 'ui/css/pods-wizard.css', array(), PODS_VERSION );
104
-
105
- wp_register_script( 'pods-upgrade', PODS_URL . 'ui/js/jquery.pods.upgrade.js', array(), PODS_VERSION );
106
-
107
- wp_register_script( 'pods-migrate', PODS_URL . 'ui/js/jquery.pods.migrate.js', array(), PODS_VERSION );
108
-
109
- if ( isset( $_GET[ 'page' ] ) ) {
110
- $page = $_GET[ 'page' ];
111
- if ( 'pods' == $page || ( false !== strpos( $page, 'pods-' ) && 0 === strpos( $page, 'pods-' ) ) ) {
112
- ?>
113
- <script type="text/javascript">
114
- var PODS_URL = "<?php echo esc_js( PODS_URL ); ?>";
115
- </script>
116
- <?php
117
- wp_enqueue_script( 'jquery' );
118
- wp_enqueue_script( 'jquery-ui-core' );
119
- wp_enqueue_script( 'jquery-ui-sortable' );
120
-
121
- wp_enqueue_style( 'jquery-ui' );
122
-
123
- wp_enqueue_script( 'pods-floatmenu' );
124
-
125
- wp_enqueue_style( 'jquery-qtip2' );
126
- wp_enqueue_script( 'jquery-qtip2' );
127
- wp_enqueue_script( 'pods-qtip-init' );
128
-
129
- wp_enqueue_script( 'pods' );
130
-
131
- if ( 0 === strpos( $page, 'pods-manage-' ) || 0 === strpos( $page, 'pods-add-new-' ) )
132
- wp_enqueue_script( 'post' );
133
- elseif ( 0 === strpos( $page, 'pods-settings-' ) ) {
134
- wp_enqueue_script( 'post' );
135
- wp_enqueue_style( 'pods-admin' );
136
- }
137
- else
138
- wp_enqueue_style( 'pods-admin' );
139
-
140
- if ( 'pods-advanced' == $page ) {
141
- wp_register_style( 'pods-advanced', PODS_URL . 'ui/css/pods-advanced.css', array(), '1.0' );
142
- wp_enqueue_style( 'pods-advanced' );
143
-
144
- wp_enqueue_script( 'jquery-ui-effects-core', PODS_URL . 'ui/js/jquery-ui/jquery.effects.core.js', array( 'jquery' ), '1.8.8' );
145
- wp_enqueue_script( 'jquery-ui-effects-fade', PODS_URL . 'ui/js/jquery-ui/jquery.effects.fade.js', array( 'jquery' ), '1.8.8' );
146
- wp_enqueue_script( 'jquery-ui-dialog' );
147
-
148
- wp_register_script( 'pods-advanced', PODS_URL . 'ui/js/advanced.js', array(), PODS_VERSION );
149
- wp_enqueue_script( 'pods-advanced' );
150
- }
151
- elseif ( 'pods-packages' == $page )
152
- wp_enqueue_style( 'pods-wizard' );
153
- elseif ( 'pods-wizard' == $page || 'pods-upgrade' == $page || ( in_array( $page, array( 'pods', 'pods-add-new' ) ) && in_array( pods_var( 'action', 'get', 'manage' ), array( 'add', 'manage' ) ) ) ) {
154
- wp_enqueue_style( 'pods-wizard' );
155
-
156
- if ( 'pods-upgrade' == $page )
157
- wp_enqueue_script( 'pods-upgrade' );
158
- }
159
- }
160
- }
161
-
162
- wp_enqueue_style( 'pods-font' );
163
- }
164
-
165
- /**
166
- * Build the admin menus
167
- *
168
- * @since 2.0
169
- */
170
- public function admin_menu () {
171
- $advanced_content_types = PodsMeta::$advanced_content_types;
172
- $taxonomies = PodsMeta::$taxonomies;
173
- $settings = PodsMeta::$settings;
174
-
175
- $all_pods = pods_api()->load_pods( array( 'count' => true ) );
176
-
177
- if ( !PodsInit::$upgrade_needed || ( pods_is_admin() && 1 == pods_var( 'pods_upgrade_bypass' ) ) ) {
178
- $submenu_items = array();
179
-
180
- if ( !empty( $advanced_content_types ) ) {
181
- $submenu = array();
182
-
183
- $pods_pages = 0;
184
-
185
- foreach ( (array) $advanced_content_types as $pod ) {
186
- if ( !isset( $pod[ 'name' ] ) || !isset( $pod[ 'options' ] ) || empty( $pod[ 'fields' ] ) )
187
- continue;
188
- elseif ( !pods_is_admin( array( 'pods', 'pods_content', 'pods_add_' . $pod[ 'name' ], 'pods_edit_' . $pod[ 'name' ], 'pods_delete_' . $pod[ 'name' ] ) ) )
189
- continue;
190
-
191
- if ( 1 == pods_var( 'show_in_menu', $pod[ 'options' ], 0 ) ) {
192
- $page_title = pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod[ 'name' ] ) ), null, true );
193
- $page_title = apply_filters( 'pods_admin_menu_page_title', $page_title, $pod );
194
-
195
- $menu_label = pods_var_raw( 'menu_name', $pod[ 'options' ], $page_title, null, true );
196
- $menu_label = apply_filters( 'pods_admin_menu_label', $menu_label, $pod );
197
-
198
- $singular_label = pods_var_raw( 'label_singular', $pod[ 'options' ], pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod[ 'name' ] ) ), null, true ), null, true );
199
- $plural_label = pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod[ 'name' ] ) ), null, true );
200
-
201
- $menu_location = pods_var( 'menu_location', $pod[ 'options' ], 'objects' );
202
- $menu_location_custom = pods_var( 'menu_location_custom', $pod[ 'options' ], '' );
203
-
204
- $menu_position = pods_var_raw( 'menu_position', $pod[ 'options' ], '', null, true );
205
- $menu_icon = pods_evaluate_tags( pods_var_raw( 'menu_icon', $pod[ 'options' ], '', null, true ), true );
206
-
207
- if ( empty( $menu_position ) )
208
- $menu_position = null;
209
-
210
- $parent_page = null;
211
-
212
- if ( pods_is_admin( array( 'pods', 'pods_content', 'pods_edit_' . $pod[ 'name' ], 'pods_delete_' . $pod[ 'name' ] ) ) ) {
213
- if ( !empty( $menu_location_custom ) ) {
214
- if ( !isset( $submenu_items[ $menu_location_custom ] ) )
215
- $submenu_items[ $menu_location_custom ] = array();
216
-
217
- $submenu_items[ $menu_location_custom ][] = array( $menu_location_custom, $page_title, $menu_label, 'read', 'pods-manage-' . $pod[ 'name' ], array( $this, 'admin_content' ) );
218
-
219
- continue;
220
- }
221
- else {
222
- $pods_pages++;
223
-
224
- $parent_page = $page = 'pods-manage-' . $pod[ 'name' ];
225
-
226
- if ( empty( $menu_position ) )
227
- $menu_position = null;
228
- add_menu_page( $page_title, $menu_label, 'read', $parent_page, '', $menu_icon, $menu_position );
229
-
230
- $all_title = $plural_label;
231
- $all_label = __( 'All', 'pods' ) . ' ' . $plural_label;
232
-
233
- if ( $page == pods_var( 'page', 'get' ) ) {
234
- if ( 'edit' == pods_var( 'action', 'get', 'manage' ) )
235
- $all_title = __( 'Edit', 'pods' ) . ' ' . $singular_label;
236
- elseif ( 'add' == pods_var( 'action', 'get', 'manage' ) )
237
- $all_title = __( 'Add New', 'pods' ) . ' ' . $singular_label;
238
- }
239
-
240
- add_submenu_page( $parent_page, $all_title, $all_label, 'read', $page, array( $this, 'admin_content' ) );
241
- }
242
- }
243
-
244
- if ( pods_is_admin( array( 'pods', 'pods_content', 'pods_add_' . $pod[ 'name' ] ) ) ) {
245
- $page = 'pods-add-new-' . $pod[ 'name' ];
246
-
247
- if ( null === $parent_page ) {
248
- $pods_pages++;
249
-
250
- $parent_page = $page;
251
-
252
- if ( empty( $menu_position ) )
253
- $menu_position = null;
254
- add_menu_page( $page_title, $menu_label, 'read', $parent_page, '', $menu_icon, $menu_position );
255
- }
256
-
257
- $add_title = __( 'Add New', 'pods' ) . ' ' . $singular_label;
258
- $add_label = __( 'Add New', 'pods' );
259
-
260
- add_submenu_page( $parent_page, $add_title, $add_label, 'read', $page, array( $this, 'admin_content' ) );
261
- }
262
- }
263
- else
264
- $submenu[] = $pod;
265
- }
266
-
267
- $submenu = apply_filters( 'pods_admin_menu_secondary_content', $submenu );
268
-
269
- if ( !empty( $submenu ) && ( !defined( 'PODS_DISABLE_CONTENT_MENU' ) || !PODS_DISABLE_CONTENT_MENU ) ) {
270
- $parent_page = null;
271
-
272
- foreach ( $submenu as $item ) {
273
- $singular_label = pods_var_raw( 'label_singular', $item[ 'options' ], pods_var_raw( 'label', $item, ucwords( str_replace( '_', ' ', $item[ 'name' ] ) ), null, true ), null, true );
274
- $plural_label = pods_var_raw( 'label', $item, ucwords( str_replace( '_', ' ', $item[ 'name' ] ) ), null, true );
275
-
276
- if ( pods_is_admin( array( 'pods', 'pods_content', 'pods_edit_' . $item[ 'name' ], 'pods_delete_' . $item[ 'name' ] ) ) ) {
277
- $page = 'pods-manage-' . $item[ 'name' ];
278
-
279
- if ( null === $parent_page ) {
280
- $parent_page = $page;
281
-
282
- add_menu_page( 'Pods', 'Pods', 'read', $parent_page, null, 'dashicons-pods', '58.5' );
283
- }
284
-
285
- $all_title = $plural_label;
286
- $all_label = __( 'Manage', 'pods' ) . ' ' . $plural_label;
287
-
288
- if ( $page == pods_var( 'page', 'get' ) ) {
289
- if ( 'edit' == pods_var( 'action', 'get', 'manage' ) )
290
- $all_title = __( 'Edit', 'pods' ) . ' ' . $singular_label;
291
- elseif ( 'add' == pods_var( 'action', 'get', 'manage' ) )
292
- $all_title = __( 'Add New', 'pods' ) . ' ' . $singular_label;
293
- }
294
-
295
- add_submenu_page( $parent_page, $all_title, $all_label, 'read', $page, array( $this, 'admin_content' ) );
296
- }
297
- elseif ( current_user_can( 'pods_add_' . $item[ 'name' ] ) ) {
298
- $page = 'pods-add-new-' . $item[ 'name' ];
299
-
300
- if ( null === $parent_page ) {
301
- $parent_page = $page;
302
-
303
- add_menu_page( 'Pods', 'Pods', 'read', $parent_page, null, 'dashicons-pods', '58.5' );
304
- }
305
-
306
- $add_title = __( 'Add New', 'pods' ) . ' ' . $singular_label;
307
- $add_label = __( 'Manage', 'pods' ) . ' ' . $plural_label;
308
-
309
- add_submenu_page( $parent_page, $add_title, $add_label, 'read', $page, array( $this, 'admin_content' ) );
310
- }
311
- }
312
- }
313
- }
314
-
315
- if ( !empty( $taxonomies ) ) {
316
- foreach ( (array) $taxonomies as $pod ) {
317
- if ( !pods_is_admin( array( 'pods', 'pods_content', 'pods_edit_' . $pod[ 'name' ] ) ) )
318
- continue;
319
-
320
- $page_title = pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod[ 'name' ] ) ), null, true );
321
- $page_title = apply_filters( 'pods_admin_menu_page_title', $page_title, $pod );
322
-
323
- $menu_label = pods_var_raw( 'menu_name', $pod[ 'options' ], $page_title, null, true );
324
- $menu_label = apply_filters( 'pods_admin_menu_label', $menu_label, $pod );
325
-
326
- $menu_position = pods_var_raw( 'menu_position', $pod[ 'options' ], '', null, true );
327
- $menu_icon = pods_evaluate_tags( pods_var_raw( 'menu_icon', $pod[ 'options' ], '', null, true ), true );
328
-
329
- if ( empty( $menu_position ) )
330
- $menu_position = null;
331
-
332
- $menu_slug = 'edit-tags.php?taxonomy=' . $pod[ 'name' ];
333
- $menu_location = pods_var( 'menu_location', $pod[ 'options' ], 'default' );
334
- $menu_location_custom = pods_var( 'menu_location_custom', $pod[ 'options' ], '' );
335
-
336
- if ( 'default' == $menu_location )
337
- continue;
338
-
339
- $taxonomy_data = get_taxonomy( $pod[ 'name' ] );
340
-
341
- foreach ( (array) $taxonomy_data->object_type as $post_type ) {
342
- if ( 'post' == $post_type )
343
- remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=' . $pod[ 'name' ] );
344
- elseif ( 'attachment' == $post_type )
345
- remove_submenu_page( 'upload.php', 'edit-tags.php?taxonomy=' . $pod[ 'name' ] . '&amp;post_type=' . $post_type );
346
- else
347
- remove_submenu_page( 'edit.php?post_type=' . $post_type, 'edit-tags.php?taxonomy=' . $pod[ 'name' ] . '&amp;post_type=' . $post_type );
348
- }
349
-
350
- if ( 'settings' == $menu_location )
351
- add_options_page( $page_title, $menu_label, 'read', $menu_slug );
352
- elseif ( 'appearances' == $menu_location )
353
- add_theme_page( $page_title, $menu_label, 'read', $menu_slug );
354
- elseif ( 'objects' == $menu_location ) {
355
- if ( empty( $menu_position ) )
356
- $menu_position = null;
357
- add_menu_page( $page_title, $menu_label, 'read', $menu_slug, '', $menu_icon, $menu_position );
358
- }
359
- elseif ( 'top' == $menu_location )
360
- add_menu_page( $page_title, $menu_label, 'read', $menu_slug, '', $menu_icon, $menu_position );
361
- elseif ( 'submenu' == $menu_location && !empty( $menu_location_custom ) ) {
362
- if ( !isset( $submenu_items[ $menu_location_custom ] ) )
363
- $submenu_items[ $menu_location_custom ] = array();
364
-
365
- $submenu_items[ $menu_location_custom ][] = array( $menu_location_custom, $page_title, $menu_label, 'read', $menu_slug, '' );
366
- }
367
- }
368
- }
369
-
370
- if ( !empty( $settings ) ) {
371
- foreach ( (array) $settings as $pod ) {
372
- if ( !pods_is_admin( array( 'pods', 'pods_content', 'pods_edit_' . $pod[ 'name' ] ) ) )
373
- continue;
374
-
375
- $page_title = pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod[ 'name' ] ) ), null, true );
376
- $page_title = apply_filters( 'pods_admin_menu_page_title', $page_title, $pod );
377
-
378
- $menu_label = pods_var_raw( 'menu_name', $pod[ 'options' ], $page_title, null, true );
379
- $menu_label = apply_filters( 'pods_admin_menu_label', $menu_label, $pod );
380
-
381
- $menu_position = pods_var_raw( 'menu_position', $pod[ 'options' ], '', null, true );
382
- $menu_icon = pods_evaluate_tags( pods_var_raw( 'menu_icon', $pod[ 'options' ], '', null, true ), true );
383
-
384
- if ( empty( $menu_position ) )
385
- $menu_position = null;
386
-
387
- $menu_slug = 'pods-settings-' . $pod[ 'name' ];
388
- $menu_location = pods_var( 'menu_location', $pod[ 'options' ], 'settings' );
389
- $menu_location_custom = pods_var( 'menu_location_custom', $pod[ 'options' ], '' );
390
-
391
- if ( 'settings' == $menu_location )
392
- add_options_page( $page_title, $menu_label, 'read', $menu_slug, array( $this, 'admin_content_settings' ) );
393
- elseif ( 'appearances' == $menu_location )
394
- add_theme_page( $page_title, $menu_label, 'read', $menu_slug, array( $this, 'admin_content_settings' ) );
395
- elseif ( 'objects' == $menu_location ) {
396
- if ( empty( $menu_position ) )
397
- $menu_position = null;
398
- add_menu_page( $page_title, $menu_label, 'read', $menu_slug, array( $this, 'admin_content_settings' ), $menu_icon, $menu_position );
399
- }
400
- elseif ( 'top' == $menu_location )
401
- add_menu_page( $page_title, $menu_label, 'read', $menu_slug, array( $this, 'admin_content_settings' ), $menu_icon, $menu_position );
402
- elseif ( 'submenu' == $menu_location && !empty( $menu_location_custom ) ) {
403
- if ( !isset( $submenu_items[ $menu_location_custom ] ) )
404
- $submenu_items[ $menu_location_custom ] = array();
405
-
406
- $submenu_items[ $menu_location_custom ][] = array( $menu_location_custom, $page_title, $menu_label, 'read', $menu_slug, array( $this, 'admin_content_settings' ) );
407
- }
408
- }
409
- }
410
-
411
- foreach ( $submenu_items as $items ) {
412
- foreach ( $items as $item ) {
413
- call_user_func_array( 'add_submenu_page', $item );
414
- }
415
- }
416
-
417
- $admin_menus = array(
418
- 'pods' => array(
419
- 'label' => __( 'Edit Pods', 'pods' ),
420
- 'function' => array( $this, 'admin_setup' ),
421
- 'access' => 'pods'
422
- ),
423
- 'pods-add-new' => array(
424
- 'label' => __( 'Add New', 'pods' ),
425
- 'function' => array( $this, 'admin_setup' ),
426
- 'access' => 'pods'
427
- ),
428
- 'pods-components' => array(
429
- 'label' => __( 'Components', 'pods' ),
430
- 'function' => array( $this, 'admin_components' ),
431
- 'access' => 'pods_components'
432
- ),
433
- 'pods-settings' => array(
434
- 'label' => __( 'Settings', 'pods' ),
435
- 'function' => array( $this, 'admin_settings' ),
436
- 'access' => 'pods_settings'
437
- ),
438
- 'pods-help' => array(
439
- 'label' => __( 'Help', 'pods' ),
440
- 'function' => array( $this, 'admin_help' )
441
- )
442
- );
443
-
444
- if ( empty( $all_pods ) )
445
- unset( $admin_menus[ 'pods' ] );
446
-
447
- add_filter( 'parent_file' , array( $this, 'parent_file' ) );
448
- }
449
- else {
450
- $admin_menus = array(
451
- 'pods-upgrade' => array(
452
- 'label' => __( 'Upgrade', 'pods' ),
453
- 'function' => array( $this, 'admin_upgrade' ),
454
- 'access' => 'manage_options'
455
- ),
456
- 'pods-settings' => array(
457
- 'label' => __( 'Settings', 'pods' ),
458
- 'function' => array( $this, 'admin_settings' ),
459
- 'access' => 'pods_settings'
460
- ),
461
- 'pods-help' => array(
462
- 'label' => __( 'Help', 'pods' ),
463
- 'function' => array( $this, 'admin_help' )
464
- )
465
- );
466
-
467
- add_action( 'admin_notices', array( $this, 'upgrade_notice' ) );
468
- }
469
-
470
- /**
471
- * Add or change Pods Admin menu items
472
- *
473
- * @params array $admin_menus The submenu items in Pods Admin menu.
474
- *
475
- * @since unknown
476
- */
477
- $admin_menus = apply_filters( 'pods_admin_menu', $admin_menus );
478
-
479
- $parent = false;
480
-
481
- if ( !empty( $admin_menus ) && ( !defined( 'PODS_DISABLE_ADMIN_MENU' ) || !PODS_DISABLE_ADMIN_MENU ) ) {
482
- foreach ( $admin_menus as $page => $menu_item ) {
483
- if ( !pods_is_admin( pods_var_raw( 'access', $menu_item ) ) )
484
- continue;
485
-
486
- // Don't just show the help page
487
- if ( false === $parent && 'pods-help' == $page )
488
- continue;
489
-
490
- if ( !isset( $menu_item[ 'label' ] ) )
491
- $menu_item[ 'label' ] = $page;
492
-
493
- if ( false === $parent ) {
494
- $parent = $page;
495
-
496
- $menu = __( 'Pods Admin', 'pods' );
497
-
498
- if ( 'pods-upgrade' == $parent )
499
- $menu = __( 'Pods Upgrade', 'pods' );
500
-
501
- add_menu_page( $menu, $menu, 'read', $parent, null, 'dashicons-pods' );
502
- }
503
-
504
- add_submenu_page( $parent, $menu_item[ 'label' ], $menu_item[ 'label' ], 'read', $page, $menu_item[ 'function' ] );
505
-
506
- if ( 'pods-components' == $page )
507
- PodsInit::$components->menu( $parent );
508
- }
509
- }
510
- }
511
-
512
- /**
513
- * Set the correct parent_file to highlight the correct top level menu
514
- *
515
- * @param $parent_file The parent file
516
- *
517
- * @return mixed|string
518
- *
519
- * @since unknown
520
- */
521
- public function parent_file ( $parent_file ) {
522
- global $current_screen;
523
-
524
- if ( isset( $current_screen ) && ! empty( $current_screen->taxonomy ) ) {
525
- $taxonomies = PodsMeta::$taxonomies;
526
- if ( !empty( $taxonomies ) ) {
527
- foreach ( (array) $taxonomies as $pod ) {
528
- if ( $current_screen->taxonomy !== $pod[ 'name' ] )
529
- continue;
530
-
531
- $menu_slug = 'edit-tags.php?taxonomy=' . $pod[ 'name' ];
532
- $menu_location = pods_var( 'menu_location', $pod[ 'options' ], 'default' );
533
- $menu_location_custom = pods_var( 'menu_location_custom', $pod[ 'options' ], '' );
534
-
535
- if ( 'settings' == $menu_location )
536
- $parent_file = 'options-general.php';
537
- elseif ( 'appearances' == $menu_location )
538
- $parent_file = 'themes.php';
539
- elseif ( 'objects' == $menu_location )
540
- $parent_file = $menu_slug;
541
- elseif ( 'top' == $menu_location )
542
- $parent_file = $menu_slug;
543
- elseif ( 'submenu' == $menu_location && !empty( $menu_location_custom ) ) {
544
- $parent_file = $menu_location_custom;
545
- }
546
-
547
- break;
548
- }
549
- }
550
- }
551
-
552
- if ( isset( $current_screen ) && ! empty( $current_screen->post_type ) ) {
553
- global $submenu_file;
554
- $components = PodsInit::$components->components;
555
- foreach ( $components as $component => $component_data ) {
556
- if ( ! empty( $component_data[ 'MenuPage' ] ) && $parent_file === $component_data[ 'MenuPage' ] ) {
557
- $parent_file = 'pods';
558
- $submenu_file = $component_data[ 'MenuPage' ];
559
- }
560
- }
561
- }
562
-
563
- return $parent_file;
564
- }
565
-
566
- public function upgrade_notice () {
567
- echo '<div class="error fade"><p>';
568
- echo sprintf(
569
- __( '<strong>NOTICE:</strong> Pods %s requires your action to complete the upgrade. Please run the <a href="%s">Upgrade Wizard</a>.', 'pods' ),
570
- esc_html( PODS_VERSION ),
571
- esc_url( admin_url( 'admin.php?page=pods-upgrade' ) )
572
- );
573
- echo '</p></div>';
574
- }
575
-
576
- /**
577
- * Create PodsUI content for the administration pages
578
- */
579
- public function admin_content () {
580
- $pod_name = str_replace( array( 'pods-manage-', 'pods-add-new-' ), '', $_GET[ 'page' ] );
581
-
582
- $pod = pods( $pod_name, pods_var( 'id', 'get', null, null, true ) );
583
-
584
- if ( false !== strpos( $_GET[ 'page' ], 'pods-add-new-' ) )
585
- $_GET[ 'action' ] = pods_var( 'action', 'get', 'add' );
586
-
587
- $pod->ui();
588
- }
589
-
590
- /**
591
- * Create PodsUI content for the settings administration pages
592
- */
593
- public function admin_content_settings () {
594
- $pod_name = str_replace( 'pods-settings-', '', $_GET[ 'page' ] );
595
-
596
- $pod = pods( $pod_name );
597
-
598
- if ( 'custom' != pods_var( 'ui_style', $pod->pod_data[ 'options' ], 'settings', null, true ) ) {
599
- $actions_disabled = array(
600
- 'manage' => 'manage',
601
- 'add' => 'add',
602
- 'delete' => 'delete',
603
- 'duplicate' => 'duplicate',
604
- 'view' => 'view',
605
- 'export' => 'export'
606
- );
607
-
608
- $_GET[ 'action' ] = 'edit';
609
-
610
- $page_title = pods_var_raw( 'label', $pod->pod_data, ucwords( str_replace( '_', ' ', $pod->pod_data[ 'name' ] ) ), null, true );
611
-
612
- $ui = array(
613
- 'pod' => $pod,
614
- 'fields' => array(
615
- 'edit' => $pod->pod_data[ 'fields' ]
616
- ),
617
- 'header' => array(
618
- 'edit' => $page_title
619
- ),
620
- 'label' => array(
621
- 'edit' => __( 'Save Changes', 'pods' )
622
- ),
623
- 'style' => pods_var( 'ui_style', $pod->pod_data[ 'options' ], 'settings', null, true ),
624
- 'icon' => pods_evaluate_tags( pods_var_raw( 'menu_icon', $pod->pod_data[ 'options' ] ), true ),
625
- 'actions_disabled' => $actions_disabled
626
- );
627
-
628
- $ui = apply_filters( 'pods_admin_ui_' . $pod->pod, apply_filters( 'pods_admin_ui', $ui, $pod->pod, $pod ), $pod->pod, $pod );
629
-
630
- // Force disabled actions, do not pass go, do not collect $two_hundred
631
- $ui[ 'actions_disabled' ] = $actions_disabled;
632
-
633
- pods_ui( $ui );
634
- }
635
- else {
636
- do_action( 'pods_admin_ui_custom', $pod );
637
- do_action( 'pods_admin_ui_custom_' . $pod->pod, $pod );
638
- }
639
- }
640
-
641
- /**
642
- * Add media button for Pods shortcode
643
- *
644
- * @param $context
645
- *
646
- * @return string
647
- */
648
- public function media_button ( $context = null ) {
649
- // If shortcodes are disabled don't show the button
650
- if ( defined( 'PODS_DISABLE_SHORTCODE' ) && PODS_DISABLE_SHORTCODE ) {
651
- return '';
652
- }
653
-
654
- /**
655
- * Filter to remove Pods shortcode button from the post editor.
656
- *
657
- * @param bool. Set to false to block the shortcode button from appearing.
658
- * @param string $context
659
- *
660
- * @since 2.3.19
661
- */
662
- if ( !apply_filters( 'pods_admin_media_button', true, $context ) ) {
663
- return '';
664
- }
665
-
666
- $current_page = basename( $_SERVER[ 'PHP_SELF' ] );
667
- $current_page = explode( '?', $current_page );
668
- $current_page = explode( '#', $current_page[ 0 ] );
669
- $current_page = $current_page[ 0 ];
670
-
671
- // Only show the button on post type pages
672
- if ( !in_array( $current_page, array( 'post-new.php', 'post.php' ) ) )
673
- return '';
674
-
675
- add_action( 'admin_footer', array( $this, 'mce_popup' ) );
676
-
677
- echo '<a href="#TB_inline?width=640&inlineId=pods_shortcode_form" class="thickbox button" id="add_pod_button" title="Pods Shortcode"><img style="padding: 0px 6px 0px 0px; margin: -3px 0px 0px;" src="' . PODS_URL . 'ui/images/icon16.png" alt="' . __('Pods Shortcode' ,'pods') . '" />' . __('Pods Shortcode' ,'pods') . '</a>';
678
- }
679
-
680
- /**
681
- * Enqueue assets for Media Library Popup
682
- */
683
- public function register_media_assets () {
684
- if ( 'pods_media_attachment' == pods_var( 'inlineId', 'get' ) )
685
- wp_enqueue_style( 'pods-attach' );
686
- }
687
-
688
- /**
689
- * Output Pods shortcode popup window
690
- */
691
- public function mce_popup () {
692
- pods_view( PODS_DIR . 'ui/admin/shortcode.php', compact( array_keys( get_defined_vars() ) ) );
693
- }
694
-
695
- /**
696
- * Handle main Pods Setup area for managing Pods and Fields
697
- */
698
- public function admin_setup () {
699
- $pods = pods_api()->load_pods( array( 'fields' => false ) );
700
-
701
- $view = pods_var( 'view', 'get', 'all', null, true );
702
-
703
- if ( empty( $pods ) && !isset( $_GET[ 'action' ] ) )
704
- $_GET[ 'action' ] = 'add';
705
-
706
- if ( 'pods-add-new' == $_GET[ 'page' ] ) {
707
- if ( isset( $_GET[ 'action' ] ) && 'add' != $_GET[ 'action' ] )
708
- pods_redirect( pods_query_arg( array( 'page' => 'pods', 'action' => $_GET[ 'action' ] ) ) );
709
- else
710
- $_GET[ 'action' ] = 'add';
711
- }
712
- elseif ( isset( $_GET[ 'action' ] ) && 'add' == $_GET[ 'action' ] )
713
- pods_redirect( pods_query_arg( array( 'page' => 'pods-add-new', 'action' => '' ) ) );
714
-
715
- $types = array(
716
- 'post_type' => __( 'Post Type (extended)', 'pods' ),
717
- 'taxonomy' => __( 'Taxonomy (extended)', 'pods' ),
718
- 'cpt' => __( 'Custom Post Type', 'pods' ),
719
- 'ct' => __( 'Custom Taxonomy', 'pods' ),
720
- 'user' => __( 'User (extended)', 'pods' ),
721
- 'media' => __( 'Media (extended)', 'pods' ),
722
- 'comment' => __( 'Comments (extended)', 'pods' ),
723
- 'pod' => __( 'Advanced Content Type', 'pods' ),
724
- 'settings' => __( 'Custom Settings Page', 'pods' )
725
- );
726
-
727
- $row = false;
728
-
729
- $pod_types_found = array();
730
-
731
- $fields = array(
732
- 'label' => array( 'label' => __( 'Label', 'pods' ) ),
733
- 'name' => array( 'label' => __( 'Name', 'pods' ) ),
734
- 'type' => array( 'label' => __( 'Type', 'pods' ) ),
735
- 'storage' => array(
736
- 'label' => __( 'Storage Type', 'pods' ),
737
- 'width' => '10%'
738
- ),
739
- 'field_count' => array(
740
- 'label' => __( 'Number of Fields', 'pods' ),
741
- 'width' => '8%'
742
- )
743
- );
744
-
745
- $total_fields = 0;
746
-
747
- foreach ( $pods as $k => $pod ) {
748
- if ( isset( $types[ $pod[ 'type' ] ] ) ) {
749
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) ) {
750
- if ( empty( $pod[ 'object' ] ) ) {
751
- if ( 'post_type' == $pod[ 'type' ] )
752
- $pod[ 'type' ] = 'cpt';
753
- else
754
- $pod[ 'type' ] = 'ct';
755
- }
756
- }
757
-
758
- if ( !isset( $pod_types_found[ $pod[ 'type' ] ] ) )
759
- $pod_types_found[ $pod[ 'type' ] ] = 1;
760
- else
761
- $pod_types_found[ $pod[ 'type' ] ]++;
762
-
763
- if ( 'all' != $view && $view != $pod[ 'type' ] ) {
764
- unset( $pods[ $k ] );
765
-
766
- continue;
767
- }
768
-
769
- $pod[ 'real_type' ] = $pod[ 'type' ];
770
- $pod[ 'type' ] = $types[ $pod[ 'type' ] ];
771
- }
772
- elseif ( 'all' != $view )
773
- continue;
774
-
775
- $pod[ 'storage' ] = ucwords( $pod[ 'storage' ] );
776
-
777
- if ( $pod[ 'id' ] == pods_var( 'id' ) && 'delete' != pods_var( 'action' ) )
778
- $row = $pod;
779
-
780
- $pod = array(
781
- 'id' => $pod[ 'id' ],
782
- 'label' => pods_var_raw( 'label', $pod ),
783
- 'name' => pods_var_raw( 'name', $pod ),
784
- 'object' => pods_var_raw( 'object', $pod ),
785
- 'type' => pods_var_raw( 'type', $pod ),
786
- 'real_type' => pods_var_raw( 'real_type', $pod ),
787
- 'storage' => pods_var_raw( 'storage', $pod ),
788
- 'field_count' => count( $pod[ 'fields' ] )
789
- );
790
-
791
- $total_fields += $pod[ 'field_count' ];
792
-
793
- $pods[ $k ] = $pod;
794
- }
795
-
796
- if ( false === $row && 0 < pods_var( 'id' ) && 'delete' != pods_var( 'action' ) ) {
797
- pods_message( 'Pod not found', 'error' );
798
-
799
- unset( $_GET[ 'id' ] );
800
- unset( $_GET[ 'action' ] );
801
- }
802
-
803
- $ui = array(
804
- 'data' => $pods,
805
- 'row' => $row,
806
- 'total' => count( $pods ),
807
- 'total_found' => count( $pods ),
808
- 'items' => 'Pods',
809
- 'item' => 'Pod',
810
- 'fields' => array(
811
- 'manage' => $fields
812
- ),
813
- 'actions_disabled' => array( 'view', 'export' ),
814
- 'actions_custom' => array(
815
- 'add' => array( $this, 'admin_setup_add' ),
816
- 'edit' => array( $this, 'admin_setup_edit' ),
817
- 'duplicate' => array(
818
- 'callback' => array( $this, 'admin_setup_duplicate' ),
819
- 'restrict_callback' => array( $this, 'admin_setup_duplicate_restrict' )
820
- ),
821
- 'reset' => array(
822
- 'label' => __( 'Delete All Items', 'pods' ),
823
- 'confirm' => __( 'Are you sure you want to delete all items from this Pod? If this is an extended Pod, it will remove the original items extended too.', 'pods' ),
824
- 'callback' => array( $this, 'admin_setup_reset' ),
825
- 'restrict_callback' => array( $this, 'admin_setup_reset_restrict' ),
826
- 'nonce' => true
827
- ),
828
- 'delete' => array( $this, 'admin_setup_delete' )
829
- ),
830
- 'action_links' => array(
831
- 'add' => pods_query_arg( array( 'page' => 'pods-add-new', 'action' => '', 'id' => '', 'do' => '' ) )
832
- ),
833
- 'search' => false,
834
- 'searchable' => false,
835
- 'sortable' => true,
836
- 'pagination' => false,
837
- 'extra' => array(
838
- 'total' => ', ' . number_format_i18n( $total_fields ) . ' ' . _n( 'field', 'fields', $total_fields, 'pods' )
839
- )
840
- );
841
-
842
- if ( 1 < count( $pod_types_found ) ) {
843
- $ui[ 'views' ] = array( 'all' => __( 'All', 'pods' ) );
844
- $ui[ 'view' ] = $view;
845
- $ui[ 'heading' ] = array( 'views' => __( 'Type', 'pods' ) );
846
- $ui[ 'filters_enhanced' ] = true;
847
-
848
- foreach ( $pod_types_found as $pod_type => $number_found ) {
849
- $ui[ 'views' ][ $pod_type ] = $types[ $pod_type ];
850
- }
851
- }
852
-
853
- pods_ui( $ui );
854
- }
855
-
856
- /**
857
- * Get the add page of an object
858
- *
859
- * @param $obj
860
- */
861
- public function admin_setup_add ( $obj ) {
862
- pods_view( PODS_DIR . 'ui/admin/setup-add.php', compact( array_keys( get_defined_vars() ) ) );
863
- }
864
-
865
- /**
866
- * Get the edit page of an object
867
- *
868
- * @param $duplicate
869
- * @param $obj
870
- */
871
- public function admin_setup_edit ( $duplicate, $obj ) {
872
- pods_view( PODS_DIR . 'ui/admin/setup-edit.php', compact( array_keys( get_defined_vars() ) ) );
873
- }
874
-
875
- /**
876
- * Get list of Pod option tabs
877
- *
878
- * @return array
879
- */
880
- public function admin_setup_edit_tabs ( $pod ) {
881
- $fields = true;
882
- $labels = false;
883
- $admin_ui = false;
884
- $advanced = false;
885
-
886
- if ( 'post_type' == pods_var( 'type', $pod ) && strlen( pods_var( 'object', $pod ) ) < 1 ) {
887
- $labels = true;
888
- $admin_ui = true;
889
- $advanced = true;
890
- }
891
- elseif ( 'taxonomy' == pods_var( 'type', $pod ) && strlen( pods_var( 'object', $pod ) ) < 1 ) {
892
- $labels = true;
893
- $admin_ui = true;
894
- $advanced = true;
895
- }
896
- elseif ( 'pod' == pods_var( 'type', $pod ) ) {
897
- $labels = true;
898
- $admin_ui = true;
899
- $advanced = true;
900
- }
901
- elseif ( 'settings' == pods_var( 'type', $pod ) ) {
902
- $labels = true;
903
- $admin_ui = true;
904
- }
905
-
906
- if ( 'none' == pods_var( 'storage', $pod, 'none', null, true ) && 'settings' != pods_var( 'type', $pod ) )
907
- $fields = false;
908
-
909
- $tabs = array();
910
-
911
- if ( $fields )
912
- $tabs[ 'manage-fields' ] = __( 'Manage Fields', 'pods' );
913
-
914
- if ( $labels )
915
- $tabs[ 'labels' ] = __( 'Labels', 'pods' );
916
-
917
- if ( $admin_ui )
918
- $tabs[ 'admin-ui' ] = __( 'Admin UI', 'pods' );
919
-
920
- if ( $advanced )
921
- $tabs[ 'advanced' ] = __( 'Advanced Options', 'pods' );
922
-
923
- if ( 'taxonomy' == pods_var( 'type', $pod ) && !$fields )
924
- $tabs[ 'extra-fields' ] = __( 'Extra Fields', 'pods' );
925
-
926
- $addtl_args = compact( array( 'fields', 'labels', 'admin_ui', 'advanced' ) );
927
-
928
- /**
929
- * Add or modify tabs in Pods editor for a specific Pod
930
- *
931
- * @params array $tabs Tabs to set.
932
- * @params object $pod Current Pods object
933
- * @params array $addtl_args Additional args.
934
- *
935
- * @since unknown
936
- */
937
- $tabs = apply_filters( 'pods_admin_setup_edit_tabs_' . $pod[ 'type' ] . '_' . $pod[ 'name' ], $tabs, $pod, $addtl_args );
938
-
939
- /**
940
- * Add or modify tabs for any Pod in Pods editor of a specific post type.
941
- */
942
- $tabs = apply_filters( 'pods_admin_setup_edit_tabs_' . $pod[ 'type' ], $tabs, $pod, $addtl_args );
943
-
944
- /**
945
- * Add or modify tabs in Pods editor for all pods.
946
- */
947
- $tabs = apply_filters( 'pods_admin_setup_edit_tabs', $tabs, $pod, $addtl_args );
948
-
949
- return $tabs;
950
- }
951
-
952
- /**
953
- * Get list of Pod options
954
- *
955
- * @return array
956
- */
957
- public function admin_setup_edit_options ( $pod ) {
958
- $options = array();
959
-
960
- // @todo fill this in
961
- $options[ 'labels' ] = array(
962
- 'temporary' => 'This has the fields hardcoded' // :(
963
- );
964
-
965
- if ( 'post_type' == $pod[ 'type' ] ) {
966
- $options[ 'admin-ui' ] = array(
967
- 'description' => array(
968
- 'label' => __( 'Post Type Description', 'pods' ),
969
- 'help' => __( 'A short descriptive summary of what the post type is.', 'pods' ),
970
- 'type' => 'text',
971
- 'default' => ''
972
- ),
973
- 'show_ui' => array(
974
- 'label' => __( 'Show Admin UI', 'pods' ),
975
- 'help' => __( 'help', 'pods' ),
976
- 'type' => 'boolean',
977
- 'default' => pods_var_raw( 'public', $pod, true ),
978
- 'boolean_yes_label' => ''
979
- ),
980
- 'show_in_menu' => array(
981
- 'label' => __( 'Show Admin Menu in Dashboard', 'pods' ),
982
- 'help' => __( 'help', 'pods' ),
983
- 'type' => 'boolean',
984
- 'default' => pods_var_raw( 'public', $pod, true ),
985
- 'dependency' => true,
986
- 'boolean_yes_label' => ''
987
- ),
988
- 'menu_location_custom' => array(
989
- 'label' => __( 'Parent Menu ID (optional)', 'pods' ),
990
- 'help' => __( 'help', 'pods' ),
991
- 'type' => 'text',
992
- 'depends-on' => array( 'show_in_menu' => true )
993
- ),
994
- 'menu_name' => array(
995
- 'label' => __( 'Menu Name', 'pods' ),
996
- 'help' => __( 'help', 'pods' ),
997
- 'type' => 'text',
998
- 'default' => '',
999
- 'depends-on' => array( 'show_in_menu' => true )
1000
- ),
1001
- 'menu_position' => array(
1002
- 'label' => __( 'Menu Position', 'pods' ),
1003
- 'help' => __( 'help', 'pods' ),
1004
- 'type' => 'number',
1005
- 'default' => 0,
1006
- 'depends-on' => array( 'show_in_menu' => true )
1007
- ),
1008
- 'menu_icon' => array(
1009
- 'label' => __( 'Menu Icon', 'pods' ),
1010
- 'help' => __( 'URL or Dashicon name for the menu icon. You may specify the path to the icon using one of the <a href="http://pods.io/docs/build/special-magic-tags/#site-tags" target="_blank">site tag</a> type <a href="http://pods.io/docs/build/special-magic-tags/" target="_blank">special magic tags</a>. For example, for a file in your theme directory, use "{@template-url}/path/to/image.png". You may also use the name of a <a href="http://melchoyce.github.io/dashicons/" target="_blank">Dashicon</a>. For example, to use the empty star icon, use "dashicons-star-empty".', 'pods' ),
1011
- 'type' => 'text',
1012
- 'default' => '',
1013
- 'depends-on' => array( 'show_in_menu' => true )
1014
- ),
1015
- 'show_in_nav_menus' => array(
1016
- 'label' => __( 'Show in Navigation Menus', 'pods' ),
1017
- 'help' => __( 'help', 'pods' ),
1018
- 'type' => 'boolean',
1019
- 'default' => true,
1020
- 'boolean_yes_label' => ''
1021
- ),
1022
- 'show_in_admin_bar' => array(
1023
- 'label' => __( 'Show in Admin Bar "New" Menu', 'pods' ),
1024
- 'help' => __( 'help', 'pods' ),
1025
- 'type' => 'boolean',
1026
- 'default' => true,
1027
- 'boolean_yes_label' => ''
1028
- )
1029
- );
1030
-
1031
- $options[ 'advanced' ] = array(
1032
- 'public' => array(
1033
- 'label' => __( 'Public', 'pods' ),
1034
- 'help' => __( 'help', 'pods' ),
1035
- 'type' => 'boolean',
1036
- 'default' => true,
1037
- 'boolean_yes_label' => ''
1038
- ),
1039
- 'publicly_queryable' => array(
1040
- 'label' => __( 'Publicly Queryable', 'pods' ),
1041
- 'help' => __( 'help', 'pods' ),
1042
- 'type' => 'boolean',
1043
- 'default' => pods_var_raw( 'public', $pod, true ),
1044
- 'boolean_yes_label' => ''
1045
- ),
1046
- 'exclude_from_search' => array(
1047
- 'label' => __( 'Exclude from Search', 'pods' ),
1048
- 'help' => __( 'help', 'pods' ),
1049
- 'type' => 'boolean',
1050
- 'default' => !pods_var_raw( 'public', $pod, true ),
1051
- 'boolean_yes_label' => ''
1052
- ),
1053
- 'capability_type' => array(
1054
- 'label' => __( 'User Capability', 'pods' ),
1055
- 'help' => __( 'Uses these capabilties for access to this post type: edit_{capability}, read_{capability}, and delete_{capability}', 'pods' ),
1056
- 'type' => 'pick',
1057
- 'default' => 'post',
1058
- 'data' => array(
1059
- 'post' => 'post',
1060
- 'page' => 'page',
1061
- 'custom' => __( 'Custom Capability', 'pods' )
1062
- ),
1063
- 'dependency' => true
1064
- ),
1065
- 'capability_type_custom' => array(
1066
- 'label' => __( 'Custom User Capability', 'pods' ),
1067
- 'help' => __( 'help', 'pods' ),
1068
- 'type' => 'text',
1069
- 'default' => pods_var_raw( 'name', $pod ),
1070
- 'depends-on' => array( 'capability_type' => 'custom' )
1071
- ),
1072
- 'capability_type_extra' => array(
1073
- 'label' => __( 'Additional User Capabilities', 'pods' ),
1074
- 'help' => __( 'Enables additional capabilities for this Post Type including: delete_{capability}s, delete_private_{capability}s, delete_published_{capability}s, delete_others_{capability}s, edit_private_{capability}s, and edit_published_{capability}s', 'pods' ),
1075
- 'type' => 'boolean',
1076
- 'default' => true,
1077
- 'boolean_yes_label' => ''
1078
- ),
1079
- 'has_archive' => array(
1080
- 'label' => __( 'Enable Archive Page', 'pods' ),
1081
- 'help' => __( 'If enabled, creates an archive page with list of of items in this custom post type. Functions like a category page for posts. Can be controlled with a template in your theme called "archive-{$post-type}.php".', 'pods' ),
1082
- 'type' => 'boolean',
1083
- 'default' => false,
1084
- 'dependency' => true,
1085
- 'boolean_yes_label' => ''
1086
- ),
1087
- 'has_archive_slug' => array(
1088
- 'label' => __( 'Archive Page Slug Override', 'pods' ),
1089
- 'help' => __( 'If archive page is enabled, you can override the slug used by WordPress, which defaults to the name of the post type.', 'pods' ),
1090
- 'type' => 'text',
1091
- 'default' => '',
1092
- 'depends-on' => array( 'has_archive' => true )
1093
- ),
1094
- 'hierarchical' => array(
1095
- 'label' => __( 'Hierarchical', 'pods' ),
1096
- 'help' => __( 'Allows for parent/ child relationships between items, just like with Pages. Note: To edit relationships in the post editor, you must enable "Page Attributes" in the "Supports" section below.', 'pods' ),
1097
- 'type' => 'boolean',
1098
- 'default' => false,
1099
- 'dependency' => true,
1100
- 'boolean_yes_label' => ''
1101
- ),
1102
- 'label_parent_item_colon' => array(
1103
- 'label' => __( '<strong>Label: </strong> Parent <span class="pods-slugged" data-sluggable="label_singular">Item</span>', 'pods' ),
1104
- 'help' => __( 'help', 'pods' ),
1105
- 'type' => 'text',
1106
- 'default' => '',
1107
- 'depends-on' => array( 'hierarchical' => true )
1108
- ),
1109
- 'label_parent' => array(
1110
- 'label' => __( '<strong>Label: </strong> Parent', 'pods' ),
1111
- 'help' => __( 'help', 'pods' ),
1112
- 'type' => 'text',
1113
- 'default' => '',
1114
- 'depends-on' => array( 'hierarchical' => true )
1115
- ),
1116
- 'rewrite' => array(
1117
- 'label' => __( 'Rewrite', 'pods' ),
1118
- 'help' => __( 'Allows you to use pretty permalinks, if set in WordPress Settings->Reading. If not enbabled, your links will be in the form of "example.com/?pod_name=post_slug" regardless of your permalink settings.', 'pods' ),
1119
- 'type' => 'boolean',
1120
- 'default' => true,
1121
- 'dependency' => true,
1122
- 'boolean_yes_label' => ''
1123
- ),
1124
- 'rewrite_custom_slug' => array(
1125
- 'label' => __( 'Custom Rewrite Slug', 'pods' ),
1126
- 'help' => __( 'Changes the first segment of the URL, which by default is the name of the Pod. For example, if your Pod is called "foo", if this field is left blank, your link will be "example.com/foo/post_slug", but if you were to enter "bar" your link will be "example.com/bar/post_slug".', 'pods' ),
1127
- 'type' => 'text',
1128
- 'default' => '',
1129
- 'depends-on' => array( 'rewrite' => true )
1130
- ),
1131
- 'rewrite_with_front' => array(
1132
- 'label' => __( 'Rewrite with Front', 'pods' ),
1133
- 'help' => __( 'Allows permalinks to be prepended with your front base (example: if your permalink structure is /blog/, then your links will be: Unchecked->/news/, Checked->/blog/news/)', 'pods' ),
1134
- 'type' => 'boolean',
1135
- 'default' => true,
1136
- 'depends-on' => array( 'rewrite' => true ),
1137
- 'boolean_yes_label' => ''
1138
- ),
1139
- 'rewrite_feeds' => array(
1140
- 'label' => __( 'Rewrite Feeds', 'pods' ),
1141
- 'help' => __( 'help', 'pods' ),
1142
- 'type' => 'boolean',
1143
- 'default' => false,
1144
- 'depends-on' => array( 'rewrite' => true ),
1145
- 'boolean_yes_label' => ''
1146
- ),
1147
- 'rewrite_pages' => array(
1148
- 'label' => __( 'Rewrite Pages', 'pods' ),
1149
- 'help' => __( 'help', 'pods' ),
1150
- 'type' => 'boolean',
1151
- 'default' => true,
1152
- 'depends-on' => array( 'rewrite' => true ),
1153
- 'boolean_yes_label' => ''
1154
- ),
1155
- 'query_var' => array(
1156
- 'label' => __( 'Query Var', 'pods' ),
1157
- 'help' => __( 'The Query Var is used in the URL and underneath the WordPress Rewrite API to tell WordPress what page or post type you are on. For a list of reserved Query Vars, read <a href="http://codex.wordpress.org/WordPress_Query_Vars">WordPress Query Vars</a> from the WordPress Codex.', 'pods' ),
1158
- 'type' => 'boolean',
1159
- 'default' => true,
1160
- 'boolean_yes_label' => ''
1161
- ),
1162
- 'can_export' => array(
1163
- 'label' => __( 'Exportable', 'pods' ),
1164
- 'help' => __( 'help', 'pods' ),
1165
- 'type' => 'boolean',
1166
- 'default' => true,
1167
- 'boolean_yes_label' => ''
1168
- ),
1169
- 'default_status' => array(
1170
- 'label' => __( 'Default Status', 'pods' ),
1171
- 'help' => __( 'help', 'pods' ),
1172
- 'type' => 'pick',
1173
- 'pick_object' => 'post-status',
1174
- 'default' => apply_filters( 'pods_api_default_status_' . pods_var_raw( 'name', $pod, 'post_type', null, true ), 'draft', $pod )
1175
- )
1176
- );
1177
- }
1178
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
1179
- $options[ 'admin-ui' ] = array(
1180
- 'show_ui' => array(
1181
- 'label' => __( 'Show Admin UI', 'pods' ),
1182
- 'help' => __( 'help', 'pods' ),
1183
- 'type' => 'boolean',
1184
- 'default' => pods_var_raw( 'public', $pod, true ),
1185
- 'dependency' => true,
1186
- 'boolean_yes_label' => ''
1187
- ),
1188
- 'menu_name' => array(
1189
- 'label' => __( 'Menu Name', 'pods' ),
1190
- 'help' => __( 'help', 'pods' ),
1191
- 'type' => 'text',
1192
- 'default' => '',
1193
- 'depends-on' => array( 'show_ui' => true )
1194
- ),
1195
- 'menu_location' => array(
1196
- 'label' => __( 'Menu Location', 'pods' ),
1197
- 'help' => __( 'help', 'pods' ),
1198
- 'type' => 'pick',
1199
- 'default' => 'default',
1200
- 'depends-on' => array( 'show_ui' => true ),
1201
- 'data' => array(
1202
- 'default' => __( 'Default - Add to associated Post Type(s) menus', 'pods' ),
1203
- 'settings' => __( 'Add to Settings menu', 'pods' ),
1204
- 'appearances' => __( 'Add to Appearances menu', 'pods' ),
1205
- 'objects' => __( 'Make a top-level menu item', 'pods' ),
1206
- 'top' => __( 'Make a new top-level menu item below Settings', 'pods' ),
1207
- 'submenu' => __( 'Add a submenu item to another menu', 'pods' )
1208
- ),
1209
- 'dependency' => true
1210
- ),
1211
- 'menu_location_custom' => array(
1212
- 'label' => __( 'Custom Menu Location', 'pods' ),
1213
- 'help' => __( 'help', 'pods' ),
1214
- 'type' => 'text',
1215
- 'depends-on' => array( 'menu_location' => 'submenu' )
1216
- ),
1217
- 'menu_position' => array(
1218
- 'label' => __( 'Menu Position', 'pods' ),
1219
- 'help' => __( 'help', 'pods' ),
1220
- 'type' => 'number',
1221
- 'default' => 0,
1222
- 'depends-on' => array( 'menu_location' => array( 'objects', 'top' ) )
1223
- ),
1224
- 'menu_icon' => array(
1225
- 'label' => __( 'Menu Icon URL', 'pods' ),
1226
- 'help' => __( 'help', 'pods' ),
1227
- 'type' => 'text',
1228
- 'default' => '',
1229
- 'depends-on' => array( 'menu_location' => array( 'objects', 'top' ) )
1230
- ),
1231
- 'show_in_nav_menus' => array(
1232
- 'label' => __( 'Show in Navigation Menus', 'pods' ),
1233
- 'help' => __( 'help', 'pods' ),
1234
- 'type' => 'boolean',
1235
- 'default' => pods_var_raw( 'public', $pod, true ),
1236
- 'boolean_yes_label' => ''
1237
- ),
1238
- 'show_tagcloud' => array(
1239
- 'label' => __( 'Allow in Tagcloud Widget', 'pods' ),
1240
- 'help' => __( 'help', 'pods' ),
1241
- 'type' => 'boolean',
1242
- 'default' => pods_var_raw( 'show_ui', $pod, pods_var_raw( 'public', $pod, true ) ),
1243
- 'boolean_yes_label' => ''
1244
- )
1245
- );
1246
-
1247
- if ( pods_version_check( 'wp', '3.5' ) ) {
1248
- $options[ 'admin-ui' ][ 'show_admin_column' ] = array(
1249
- 'label' => __( 'Show Taxonomy column on Post Types', 'pods' ),
1250
- 'help' => __( 'Whether to add a column for this taxonomy on the associated post types manage screens', 'pods' ),
1251
- 'type' => 'boolean',
1252
- 'default' => false,
1253
- 'boolean_yes_label' => ''
1254
- );
1255
- }
1256
-
1257
- // Integration for Single Value Taxonomy UI
1258
- if ( function_exists( 'tax_single_value_meta_box' ) ) {
1259
- $options[ 'admin-ui' ][ 'single_value' ] = array(
1260
- 'label' => __( 'Single Value Taxonomy', 'pods' ),
1261
- 'help' => __( 'Use a drop-down for the input instead of the WordPress default', 'pods' ),
1262
- 'type' => 'boolean',
1263
- 'default' => false,
1264
- 'boolean_yes_label' => ''
1265
- );
1266
-
1267
- $options[ 'admin-ui' ][ 'single_value_required' ] = array(
1268
- 'label' => __( 'Single Value Taxonomy - Required', 'pods' ),
1269
- 'help' => __( 'A term will be selected by default in the Post Editor, not optional', 'pods' ),
1270
- 'type' => 'boolean',
1271
- 'default' => false,
1272
- 'boolean_yes_label' => ''
1273
- );
1274
- }
1275
-
1276
- // @todo fill this in
1277
- $options[ 'advanced' ] = array(
1278
- 'temporary' => 'This type has the fields hardcoded' // :(
1279
- );
1280
- }
1281
- elseif ( 'settings' == $pod[ 'type' ] ) {
1282
- $options[ 'admin-ui' ] = array(
1283
- 'ui_style' => array(
1284
- 'label' => __( 'Admin UI Style', 'pods' ),
1285
- 'help' => __( 'help', 'pods' ),
1286
- 'type' => 'pick',
1287
- 'default' => 'settings',
1288
- 'data' => array(
1289
- 'settings' => __( 'Normal Settings Form', 'pods' ),
1290
- 'post_type' => __( 'Post Type UI', 'pods' ),
1291
- 'custom' => __( 'Custom (hook into pods_admin_ui_custom or pods_admin_ui_custom_{podname} action)', 'pods' )
1292
- ),
1293
- 'dependency' => true
1294
- ),
1295
- 'menu_location' => array(
1296
- 'label' => __( 'Menu Location', 'pods' ),
1297
- 'help' => __( 'help', 'pods' ),
1298
- 'type' => 'pick',
1299
- 'default' => 'settings',
1300
- 'data' => array(
1301
- 'settings' => __( 'Add to Settings menu', 'pods' ),
1302
- 'appearances' => __( 'Add to Appearances menu', 'pods' ),
1303
- 'top' => __( 'Make a new top-level menu item below Settings', 'pods' ),
1304
- 'submenu' => __( 'Add a submenu item to another menu', 'pods' )
1305
- ),
1306
- 'dependency' => true
1307
- ),
1308
- 'menu_location_custom' => array(
1309
- 'label' => __( 'Custom Menu Location', 'pods' ),
1310
- 'help' => __( 'help', 'pods' ),
1311
- 'type' => 'text',
1312
- 'depends-on' => array( 'menu_location' => 'submenu' )
1313
- ),
1314
- 'menu_position' => array(
1315
- 'label' => __( 'Menu Position', 'pods' ),
1316
- 'help' => __( 'help', 'pods' ),
1317
- 'type' => 'number',
1318
- 'default' => 0,
1319
- 'depends-on' => array( 'menu_location' => 'top' )
1320
- ),
1321
- 'menu_icon' => array(
1322
- 'label' => __( 'Menu Icon URL', 'pods' ),
1323
- 'help' => __( 'help', 'pods' ),
1324
- 'type' => 'text',
1325
- 'default' => '',
1326
- 'depends-on' => array( 'menu_location' => 'top' )
1327
- )
1328
- );
1329
-
1330
- // @todo fill this in
1331
- $options[ 'advanced' ] = array(
1332
- 'temporary' => 'This type has the fields hardcoded' // :(
1333
- );
1334
- }
1335
- elseif ( 'pod' == $pod[ 'type' ] ) {
1336
- $options[ 'admin-ui' ] = array(
1337
- 'ui_style' => array(
1338
- 'label' => __( 'Admin UI Style', 'pods' ),
1339
- 'help' => __( 'help', 'pods' ),
1340
- 'type' => 'pick',
1341
- 'default' => 'settings',
1342
- 'data' => array(
1343
- 'post_type' => __( 'Normal (Looks like the Post Type UI)', 'pods' ),
1344
- 'custom' => __( 'Custom (hook into pods_admin_ui_custom or pods_admin_ui_custom_{podname} action)', 'pods' )
1345
- ),
1346
- 'dependency' => true
1347
- ),
1348
- 'show_in_menu' => array(
1349
- 'label' => __( 'Show Admin Menu in Dashboard', 'pods' ),
1350
- 'help' => __( 'help', 'pods' ),
1351
- 'type' => 'boolean',
1352
- 'default' => false,
1353
- 'boolean_yes_label' => '',
1354
- 'dependency' => true
1355
- ),
1356
- 'menu_location_custom' => array(
1357
- 'label' => __( 'Parent Menu ID (optional)', 'pods' ),
1358
- 'help' => __( 'help', 'pods' ),
1359
- 'type' => 'text',
1360
- 'depends-on' => array( 'show_in_menu' => true )
1361
- ),
1362
- 'menu_position' => array(
1363
- 'label' => __( 'Menu Position', 'pods' ),
1364
- 'help' => __( 'help', 'pods' ),
1365
- 'type' => 'number',
1366
- 'default' => 0,
1367
- 'depends-on' => array( 'show_in_menu' => true )
1368
- ),
1369
- 'menu_icon' => array(
1370
- 'label' => __( 'Menu Icon URL', 'pods' ),
1371
- 'help' => __( 'This is the icon shown to the left of the menu text for this content type.', 'pods' ),
1372
- 'type' => 'text',
1373
- 'default' => '',
1374
- 'depends-on' => array( 'show_in_menu' => true )
1375
- ),
1376
- 'ui_icon' => array(
1377
- 'label' => __( 'Header Icon', 'pods' ),
1378
- 'help' => __( 'This is the icon shown to the left of the heading text at the top of the manage pages for this content type.', 'pods' ),
1379
- 'type' => 'file',
1380
- 'default' => '',
1381
- 'file_edit_title' => 0,
1382
- 'depends-on' => array( 'show_in_menu' => true )
1383
- ),
1384
- 'ui_actions_enabled' => array(
1385
- 'label' => __( 'Actions Available', 'pods' ),
1386
- 'help' => __( 'help', 'pods' ),
1387
- 'type' => 'pick',
1388
- 'default' => ( 1 == pods_var( 'ui_export', $pod ) ? array( 'add', 'edit', 'duplicate', 'delete', 'export' ) : array( 'add', 'edit', 'duplicate', 'delete' ) ),
1389
- 'data' => array(
1390
- 'add' => __( 'Add New', 'pods' ),
1391
- 'edit' => __( 'Edit', 'pods' ),
1392
- 'duplicate' => __( 'Duplicate', 'pods' ),
1393
- 'delete' => __( 'Delete', 'pods' ),
1394
- 'reorder' => __( 'Reorder', 'pods' ),
1395
- 'export' => __( 'Export', 'pods' )
1396
- ),
1397
- 'pick_format_type' => 'multi',
1398
- 'dependency' => true
1399
- ),
1400
- 'ui_reorder_field' => array(
1401
- 'label' => __( 'Reorder Field', 'pods' ),
1402
- 'help' => __( 'This is the field that will be reordered on, it should be numeric.', 'pods' ),
1403
- 'type' => 'text',
1404
- 'default' => 'menu_order',
1405
- 'depends-on' => array( 'ui_actions_enabled' => 'reorder' )
1406
- ),
1407
- 'ui_fields_manage' => array(
1408
- 'label' => __( 'Admin Table Columns', 'pods' ),
1409
- 'help' => __( 'help', 'pods' ),
1410
- 'type' => 'pick',
1411
- 'default' => array(),
1412
- 'data' => array(),
1413
- 'pick_format_type' => 'multi'
1414
- ),
1415
- 'ui_filters' => array(
1416
- 'label' => __( 'Search Filters', 'pods' ),
1417
- 'help' => __( 'help', 'pods' ),
1418
- 'type' => 'pick',
1419
- 'default' => array(),
1420
- 'data' => array(),
1421
- 'pick_format_type' => 'multi'
1422
- )
1423
- );
1424
-
1425
- if ( !empty( $pod[ 'fields' ] ) ) {
1426
- if ( isset( $pod[ 'fields' ][ pods_var_raw( 'pod_index', $pod, 'name' ) ] ) )
1427
- $options[ 'admin-ui' ][ 'ui_fields_manage' ][ 'default' ][] = pods_var_raw( 'pod_index', $pod, 'name' );
1428
-
1429
- if ( isset( $pod[ 'fields' ][ 'modified' ] ) )
1430
- $options[ 'admin-ui' ][ 'ui_fields_manage' ][ 'default' ][] = 'modified';
1431
-
1432
- foreach ( $pod[ 'fields' ] as $field ) {
1433
- $type = '';
1434
-
1435
- if ( isset( $field_types[ $field[ 'type' ] ] ) )
1436
- $type = ' <small>(' . $field_types[ $field[ 'type' ] ][ 'label' ] . ')</small>';
1437
-
1438
- $options[ 'admin-ui' ][ 'ui_fields_manage' ][ 'data' ][ $field[ 'name' ] ] = $field[ 'label' ] . $type;
1439
- $options[ 'admin-ui' ][ 'ui_filters' ][ 'data' ][ $field[ 'name' ] ] = $field[ 'label' ] . $type;
1440
- }
1441
-
1442
- $options[ 'admin-ui' ][ 'ui_fields_manage' ][ 'data' ][ 'id' ] = 'ID';
1443
- }
1444
- else {
1445
- unset( $options[ 'admin-ui' ][ 'ui_fields_manage' ] );
1446
- unset( $options[ 'admin-ui' ][ 'ui_filters' ] );
1447
- }
1448
-
1449
- // @todo fill this in
1450
- $options[ 'advanced' ] = array(
1451
- 'temporary' => 'This type has the fields hardcoded' // :(
1452
- );
1453
- }
1454
-
1455
- /**
1456
- * Add admin fields to the Pods editor for a specific Pod
1457
- *
1458
- * @params array $options The Options fields
1459
- * @params object $pod Current Pods object
1460
- *
1461
- * @since unkown
1462
- */
1463
- $options = apply_filters( 'pods_admin_setup_edit_options_' . $pod[ 'type' ] . '_' . $pod[ 'name' ], $options, $pod );
1464
-
1465
- /**
1466
- * Add admin fields to the Pods editor for any Pod of a specific content type.
1467
- */
1468
- $options = apply_filters( 'pods_admin_setup_edit_options_' . $pod[ 'type' ], $options, $pod );
1469
-
1470
- /**
1471
- * Add admin fields to the Pods editor for all Pods
1472
- */
1473
- $options = apply_filters( 'pods_admin_setup_edit_options', $options, $pod );
1474
-
1475
- return $options;
1476
- }
1477
-
1478
- /**
1479
- * Get list of Pod field option tabs
1480
- *
1481
- * @return array
1482
- */
1483
- public function admin_setup_edit_field_tabs ( $pod ) {
1484
- $core_tabs = array(
1485
- 'basic' => __( 'Basic', 'pods' ),
1486
- 'additional-field' => __( 'Additional Field Options', 'pods' ),
1487
- 'advanced' => __( 'Advanced', 'pods' )
1488
- );
1489
-
1490
- $tabs = apply_filters( 'pods_admin_setup_edit_field_tabs', array(), $pod );
1491
-
1492
- $tabs = array_merge( $core_tabs, $tabs );
1493
-
1494
- return $tabs;
1495
- }
1496
-
1497
- /**
1498
- * Get list of Pod field options
1499
- *
1500
- * @return array
1501
- */
1502
- public function admin_setup_edit_field_options ( $pod ) {
1503
- $options = array();
1504
-
1505
- $options[ 'additional-field' ] = array();
1506
-
1507
- $field_types = PodsForm::field_types();
1508
-
1509
- foreach ( $field_types as $type => $field_type_data ) {
1510
- /**
1511
- * @var $field_type PodsField
1512
- */
1513
- $field_type = PodsForm::field_loader( $type, $field_type_data[ 'file' ] );
1514
-
1515
- $field_type_vars = get_class_vars( get_class( $field_type ) );
1516
-
1517
- if ( !isset( $field_type_vars[ 'pod_types' ] ) )
1518
- $field_type_vars[ 'pod_types' ] = true;
1519
-
1520
- $options[ 'additional-field' ][ $type ] = array();
1521
-
1522
- // Only show supported field types
1523
- if ( true !== $field_type_vars[ 'pod_types' ] ) {
1524
- if ( empty( $field_type_vars[ 'pod_types' ] ) )
1525
- continue;
1526
- elseif ( is_array( $field_type_vars[ 'pod_types' ] ) && !in_array( pods_var( 'type', $pod ), $field_type_vars[ 'pod_types' ] ) )
1527
- continue;
1528
- elseif ( !is_array( $field_type_vars[ 'pod_types' ] ) && pods_var( 'type', $pod ) != $field_type_vars[ 'pod_types' ] )
1529
- continue;
1530
- }
1531
-
1532
- $options[ 'additional-field' ][ $type ] = PodsForm::ui_options( $type );
1533
- }
1534
-
1535
- $input_helpers = array(
1536
- '' => '-- Select --'
1537
- );
1538
-
1539
- if ( class_exists( 'Pods_Helpers' ) ) {
1540
- $helpers = pods_api()->load_helpers( array( 'options' => array( 'helper_type' => 'input' ) ) );
1541
-
1542
- foreach ( $helpers as $helper ) {
1543
- $input_helpers[ $helper[ 'name' ] ] = $helper[ 'name' ];
1544
- }
1545
- }
1546
-
1547
- $options[ 'advanced' ] = array(
1548
- __( 'Visual', 'pods' ) => array(
1549
- 'class' => array(
1550
- 'name' => 'class',
1551
- 'label' => __( 'Additional CSS Classes', 'pods' ),
1552
- 'help' => __( 'help', 'pods' ),
1553
- 'type' => 'text',
1554
- 'default' => ''
1555
- ),
1556
- 'input_helper' => array(
1557
- 'name' => 'input_helper',
1558
- 'label' => __( 'Input Helper', 'pods' ),
1559
- 'help' => __( 'help', 'pods' ),
1560
- 'type' => 'pick',
1561
- 'default' => '',
1562
- 'data' => $input_helpers
1563
- )
1564
- ),
1565
- __( 'Values', 'pods' ) => array(
1566
- 'default_value' => array(
1567
- 'name' => 'default_value',
1568
- 'label' => __( 'Default Value', 'pods' ),
1569
- 'help' => __( 'help', 'pods' ),
1570
- 'type' => 'text',
1571
- 'default' => ''
1572
- ),
1573
- 'default_value_parameter' => array(
1574
- 'name' => 'default_value_parameter',
1575
- 'label' => __( 'Set Default Value via Parameter', 'pods' ),
1576
- 'help' => __( 'help', 'pods' ),
1577
- 'type' => 'text',
1578
- 'default' => ''
1579
- )
1580
- ),
1581
- __( 'Visibility', 'pods' ) => array(
1582
- 'restrict_access' => array(
1583
- 'name' => 'restrict_access',
1584
- 'label' => __( 'Restrict Access', 'pods' ),
1585
- 'group' => array(
1586
- 'admin_only' => array(
1587
- 'name' => 'admin_only',
1588
- 'label' => __( 'Restrict access to Admins?', 'pods' ),
1589
- 'default' => 0,
1590
- 'type' => 'boolean',
1591
- 'dependency' => true,
1592
- 'help' => __( 'This field will only be able to be edited by users with the ability to manage_options or delete_users, or super admins of a WordPress Multisite network', 'pods' )
1593
- ),
1594
- 'restrict_role' => array(
1595
- 'name' => 'restrict_role',
1596
- 'label' => __( 'Restrict access by Role?', 'pods' ),
1597
- 'default' => 0,
1598
- 'type' => 'boolean',
1599
- 'dependency' => true
1600
- ),
1601
- 'restrict_capability' => array(
1602
- 'name' => 'restrict_capability',
1603
- 'label' => __( 'Restrict access by Capability?', 'pods' ),
1604
- 'default' => 0,
1605
- 'type' => 'boolean',
1606
- 'dependency' => true
1607
- ),
1608
- 'hidden' => array(
1609
- 'name' => 'hidden',
1610
- 'label' => __( 'Hide field from UI', 'pods' ),
1611
- 'default' => 0,
1612
- 'type' => 'boolean',
1613
- 'help' => __( 'This option is overriden by access restrictions. If the user does not have access to edit this field, it will be hidden. If no access restrictions are set, this field will always be hidden.', 'pods' )
1614
- ),
1615
- 'read_only' => array(
1616
- 'name' => 'read_only',
1617
- 'label' => __( 'Make field "Read Only" in UI', 'pods' ),
1618
- 'default' => 0,
1619
- 'type' => 'boolean',
1620
- 'help' => __( 'This option is overriden by access restrictions. If the user does not have access to edit this field, it will be read only. If no access restrictions are set, this field will always be read only.', 'pods' ),
1621
- 'depends-on' => array(
1622
- 'type' => array(
1623
- 'boolean',
1624
- 'color',
1625
- 'currency',
1626
- 'date',
1627
- 'datetime',
1628
- 'email',
1629
- 'number',
1630
- 'paragraph',
1631
- 'password',
1632
- 'phone',
1633
- 'slug',
1634
- 'text',
1635
- 'time',
1636
- 'website'
1637
- )
1638
- )
1639
- )
1640
- )
1641
- ),
1642
- 'roles_allowed' => array(
1643
- 'name' => 'roles_allowed',
1644
- 'label' => __( 'Role(s) Allowed', 'pods' ),
1645
- 'help' => __( 'help', 'pods' ),
1646
- 'type' => 'pick',
1647
- 'pick_object' => 'role',
1648
- 'pick_format_type' => 'multi',
1649
- 'default' => 'administrator',
1650
- 'depends-on' => array(
1651
- 'restrict_role' => true
1652
- )
1653
- ),
1654
- 'capability_allowed' => array(
1655
- 'name' => 'capability_allowed',
1656
- 'label' => __( 'Capability Allowed', 'pods' ),
1657
- 'help' => __( 'Comma-separated list of cababilities, for example add_podname_item, please see the Roles and Capabilities component for the complete list and a way to add your own.', 'pods' ),
1658
- 'type' => 'text',
1659
- 'default' => '',
1660
- 'depends-on' => array(
1661
- 'restrict_capability' => true
1662
- )
1663
- )
1664
- /*,
1665
- 'search' => array(
1666
- 'label' => __( 'Include in searches', 'pods' ),
1667
- 'help' => __( 'help', 'pods' ),
1668
- 'default' => 1,
1669
- 'type' => 'boolean',
1670
- )*/
1671
- )
1672
- /*,
1673
- __( 'Validation', 'pods' ) => array(
1674
- 'regex_validation' => array(
1675
- 'label' => __( 'RegEx Validation', 'pods' ),
1676
- 'help' => __( 'help', 'pods' ),
1677
- 'type' => 'text',
1678
- 'default' => ''
1679
- ),
1680
- 'message_regex' => array(
1681
- 'label' => __( 'Message if field does not pass RegEx', 'pods' ),
1682
- 'help' => __( 'help', 'pods' ),
1683
- 'type' => 'text',
1684
- 'default' => ''
1685
- ),
1686
- 'message_required' => array(
1687
- 'label' => __( 'Message if field is blank', 'pods' ),
1688
- 'help' => __( 'help', 'pods' ),
1689
- 'type' => 'text',
1690
- 'default' => '',
1691
- 'depends-on' => array( 'required' => true )
1692
- ),
1693
- 'message_unique' => array(
1694
- 'label' => __( 'Message if field is not unique', 'pods' ),
1695
- 'help' => __( 'help', 'pods' ),
1696
- 'type' => 'text',
1697
- 'default' => '',
1698
- 'depends-on' => array( 'unique' => true )
1699
- )
1700
- )*/
1701
- );
1702
-
1703
- if ( !class_exists( 'Pods_Helpers' ) )
1704
- unset( $options[ 'advanced-options' ][ 'input_helper' ] );
1705
-
1706
- $options = apply_filters( 'pods_admin_setup_edit_field_options', $options, $pod );
1707
-
1708
- return $options;
1709
- }
1710
-
1711
- /**
1712
- * Duplicate a pod
1713
- *
1714
- * @param $id
1715
- * @param $obj
1716
- *
1717
- * @return mixed
1718
- */
1719
- public function admin_setup_duplicate ( $obj ) {
1720
- $new_id = pods_api()->duplicate_pod( array( 'id' => $obj->id ) );
1721
-
1722
- if ( 0 < $new_id )
1723
- pods_redirect( pods_query_arg( array( 'action' => 'edit', 'id' => $new_id, 'do' => 'duplicate' ) ) );
1724
- }
1725
-
1726
- /**
1727
- * Restrict Duplicate action to custom types, not extended
1728
- *
1729
- * @param bool $restricted
1730
- * @param array $restrict
1731
- * @param string $action
1732
- * @param array $row
1733
- * @param PodsUI $obj
1734
- *
1735
- * @since 2.3.10
1736
- */
1737
- public function admin_setup_duplicate_restrict( $restricted, $restrict, $action, $row, $obj ) {
1738
-
1739
- if ( in_array( $row[ 'real_type' ], array( 'user', 'media', 'comment' ) ) ) {
1740
- $restricted = true;
1741
- }
1742
-
1743
- return $restricted;
1744
-
1745
- }
1746
-
1747
- /**
1748
- * Reset a pod
1749
- *
1750
- * @param $obj
1751
- *
1752
- * @return mixed
1753
- */
1754
- public function admin_setup_reset ( $obj, $id ) {
1755
- $pod = pods_api()->load_pod( array( 'id' => $id ), false );
1756
-
1757
- if ( empty( $pod ) )
1758
- return $obj->error( __( 'Pod not found.', 'pods' ) );
1759
-
1760
- pods_api()->reset_pod( array( 'id' => $id ) );
1761
-
1762
- $obj->message( __( 'Pod reset successfully.', 'pods' ) );
1763
-
1764
- $obj->manage();
1765
- }
1766
-
1767
- /**
1768
- * Restrict Reset action from users and media
1769
- *
1770
- * @param bool $restricted
1771
- * @param array $restrict
1772
- * @param string $action
1773
- * @param array $row
1774
- * @param PodsUI $obj
1775
- *
1776
- * @since 2.3.10
1777
- */
1778
- public function admin_setup_reset_restrict( $restricted, $restrict, $action, $row, $obj ) {
1779
-
1780
- if ( in_array( $row[ 'real_type' ], array( 'user', 'media' ) ) ) {
1781
- $restricted = true;
1782
- }
1783
-
1784
- return $restricted;
1785
-
1786
- }
1787
-
1788
- /**
1789
- * Delete a pod
1790
- *
1791
- * @param $id
1792
- * @param $obj
1793
- *
1794
- * @return mixed
1795
- */
1796
- public function admin_setup_delete ( $id, $obj ) {
1797
- $pod = pods_api()->load_pod( array( 'id' => $id ), false );
1798
-
1799
- if ( empty( $pod ) )
1800
- return $obj->error( __( 'Pod not found.', 'pods' ) );
1801
-
1802
- pods_api()->delete_pod( array( 'id' => $id ) );
1803
-
1804
- unset( $obj->data[ $pod[ 'id' ] ] );
1805
-
1806
- $obj->total = count( $obj->data );
1807
- $obj->total_found = count( $obj->data );
1808
-
1809
- $obj->message( __( 'Pod deleted successfully.', 'pods' ) );
1810
- }
1811
-
1812
- /**
1813
- * Get advanced administration view.
1814
- */
1815
- public function admin_advanced () {
1816
- pods_view( PODS_DIR . 'ui/admin/advanced.php', compact( array_keys( get_defined_vars() ) ) );
1817
- }
1818
-
1819
- /**
1820
- * Get settings administration view
1821
- */
1822
- public function admin_settings () {
1823
- pods_view( PODS_DIR . 'ui/admin/settings.php', compact( array_keys( get_defined_vars() ) ) );
1824
- }
1825
-
1826
- /**
1827
- * Get components administration UI
1828
- */
1829
- public function admin_components () {
1830
- $components = PodsInit::$components->components;
1831
-
1832
- $view = pods_var( 'view', 'get', 'all', null, true );
1833
-
1834
- $recommended = array(
1835
- 'advanced-relationships',
1836
- 'advanced-content-types',
1837
- 'migrate-packages',
1838
- 'roles-and-capabilities',
1839
- 'pages',
1840
- 'table-storage',
1841
- 'templates'
1842
- );
1843
-
1844
- foreach ( $components as $component => &$component_data ) {
1845
- if ( !in_array( $view, array( 'all', 'recommended', 'dev' ) ) && ( !isset( $component_data[ 'Category' ] ) || $view != sanitize_title( $component_data[ 'Category' ] ) ) ) {
1846
- unset( $components[ $component ] );
1847
-
1848
- continue;
1849
- }
1850
- elseif ( 'recommended' == $view && !in_array( $component_data[ 'ID' ], $recommended ) ) {
1851
- unset( $components[ $component ] );
1852
-
1853
- continue;
1854
- }
1855
- elseif ( 'dev' == $view && pods_developer() && !pods_var_raw( 'DeveloperMode', $component_data, false ) ) {
1856
- unset( $components[ $component ] );
1857
-
1858
- continue;
1859
- }
1860
- elseif ( pods_var_raw( 'DeveloperMode', $component_data, false ) && !pods_developer() ) {
1861
- unset( $components[ $component ] );
1862
-
1863
- continue;
1864
- }
1865
- elseif ( !pods_var_raw( 'TablelessMode', $component_data, false ) && pods_tableless() ) {
1866
- unset( $components[ $component ] );
1867
-
1868
- continue;
1869
- }
1870
-
1871
- $component_data[ 'Name' ] = strip_tags( $component_data[ 'Name' ] );
1872
-
1873
- if ( pods_var_raw( 'DeveloperMode', $component_data, false ) )
1874
- $component_data[ 'Name' ] .= ' <em style="font-weight: normal; color:#333;">(Developer Preview)</em>';
1875
-
1876
- $meta = array();
1877
-
1878
- if ( !empty( $component_data[ 'Version' ] ) )
1879
- $meta[] = 'Version ' . $component_data[ 'Version' ];
1880
-
1881
- if ( empty( $component_data[ 'Author' ] ) ) {
1882
- $component_data[ 'Author' ] = 'Pods Framework Team';
1883
- $component_data[ 'AuthorURI' ] = 'http://pods.io/';
1884
- }
1885
-
1886
- if ( !empty( $component_data[ 'AuthorURI' ] ) )
1887
- $component_data[ 'Author' ] = '<a href="' . $component_data[ 'AuthorURI' ] . '">' . $component_data[ 'Author' ] . '</a>';
1888
-
1889
- $meta[] = sprintf( __( 'by %s', 'pods' ), $component_data[ 'Author' ] );
1890
-
1891
- if ( !empty( $component_data[ 'URI' ] ) )
1892
- $meta[] = '<a href="' . $component_data[ 'URI' ] . '">' . __( 'Visit component site', 'pods' ) . '</a>';
1893
-
1894
- $component_data[ 'Description' ] = wpautop( trim( make_clickable( strip_tags( $component_data[ 'Description' ], 'em,strong' ) ) ) );
1895
-
1896
- if ( !empty( $meta ) )
1897
- $component_data[ 'Description' ] .= '<div class="pods-component-meta" ' . ( !empty( $component_data[ 'Description' ] ) ? ' style="padding:8px 0 4px;"' : '' ) . '>' . implode( '&nbsp;&nbsp;|&nbsp;&nbsp;', $meta ) . '</div>';
1898
-
1899
- $component_data = array(
1900
- 'id' => $component_data[ 'ID' ],
1901
- 'name' => $component_data[ 'Name' ],
1902
- 'category' => $component_data[ 'Category' ],
1903
- 'version' => '',
1904
- 'description' => $component_data[ 'Description' ],
1905
- 'mustuse' => pods_var_raw( 'MustUse', $component_data, false ),
1906
- 'toggle' => 0
1907
- );
1908
-
1909
- if ( !empty( $component_data[ 'category' ] ) ) {
1910
- $category_url = pods_query_arg( array( 'view' => sanitize_title( $component_data[ 'category' ] ), 'pg' => '', 'page' => $_GET[ 'page' ] ) );
1911
-
1912
- $component_data[ 'category' ] = '<a href="' . esc_url( $category_url ) . '">' . $component_data[ 'category' ] . '</a>';
1913
- }
1914
-
1915
- if ( isset( PodsInit::$components->settings[ 'components' ][ $component_data[ 'id' ] ] ) && 0 != PodsInit::$components->settings[ 'components' ][ $component_data[ 'id' ] ] )
1916
- $component_data[ 'toggle' ] = 1;
1917
- elseif ( $component_data[ 'mustuse' ] )
1918
- $component_data[ 'toggle' ] = 1;
1919
- }
1920
-
1921
- $ui = array(
1922
- 'data' => $components,
1923
- 'total' => count( $components ),
1924
- 'total_found' => count( $components ),
1925
- 'items' => 'Components',
1926
- 'item' => 'Component',
1927
- 'fields' => array(
1928
- 'manage' => array(
1929
- 'name' => array(
1930
- 'label' => __( 'Name', 'pods' ),
1931
- 'width' => '30%',
1932
- 'type' => 'text',
1933
- 'options' => array(
1934
- 'text_allow_html' => true
1935
- )
1936
- ),
1937
- 'category' => array(
1938
- 'label' => __( 'Category', 'pods' ),
1939
- 'width' => '10%',
1940
- 'type' => 'text',
1941
- 'options' => array(
1942
- 'text_allow_html' => true
1943
- )
1944
- ),
1945
- 'description' => array(
1946
- 'label' => __( 'Description', 'pods' ),
1947
- 'width' => '60%',
1948
- 'type' => 'text',
1949
- 'options' => array(
1950
- 'text_allow_html' => true,
1951
- 'text_allowed_html_tags' => 'strong em a ul ol li b i br div'
1952
- )
1953
- )
1954
- )
1955
- ),
1956
- 'actions_disabled' => array( 'duplicate', 'view', 'export', 'add', 'edit', 'delete' ),
1957
- 'actions_custom' => array(
1958
- 'toggle' => array(
1959
- 'callback' => array( $this, 'admin_components_toggle' ),
1960
- 'nonce' => true
1961
- )
1962
- ),
1963
- 'filters_enhanced' => true,
1964
- 'views' => array(
1965
- 'all' => __( 'All', 'pods' ),
1966
- //'recommended' => __( 'Recommended', 'pods' ),
1967
- 'field-types' => __( 'Field Types', 'pods' ),
1968
- 'tools' => __( 'Tools', 'pods' ),
1969
- 'integration' => __( 'Integration', 'pods' ),
1970
- 'migration' => __( 'Migration', 'pods' ),
1971
- 'advanced' => __( 'Advanced', 'pods' )
1972
- ),
1973
- 'view' => $view,
1974
- 'heading' => array(
1975
- 'views' => __( 'Category', 'pods' )
1976
- ),
1977
- 'search' => false,
1978
- 'searchable' => false,
1979
- 'sortable' => false,
1980
- 'pagination' => false
1981
- );
1982
-
1983
- if ( pods_developer() )
1984
- $ui[ 'views' ][ 'dev' ] = __( 'Developer Preview', 'pods' );
1985
-
1986
- pods_ui( $ui );
1987
- }
1988
-
1989
- /**
1990
- * Toggle a component on or off
1991
- *
1992
- * @param PodsUI $ui
1993
- *
1994
- * @return bool
1995
- */
1996
- public function admin_components_toggle ( PodsUI $ui ) {
1997
- $component = $_GET[ 'id' ];
1998
-
1999
- if ( !empty( PodsInit::$components->components[ $component ][ 'PluginDependency' ] ) ) {
2000
- $dependency = explode( '|', PodsInit::$components->components[ $component ][ 'PluginDependency' ] );
2001
-
2002
- if ( !pods_is_plugin_active( $dependency[ 1 ] ) ) {
2003
- $website = 'http://wordpress.org/extend/plugins/' . dirname( $dependency[ 1 ] ) . '/';
2004
-
2005
- if ( isset( $dependency[ 2 ] ) )
2006
- $website = $dependency[ 2 ];
2007
-
2008
- if ( !empty( $website ) )
2009
- $website = ' ' . sprintf( __( 'You can find it at %s', 'pods' ), '<a href="' . $website . '" target="_blank">' . $website . '</a>' );
2010
-
2011
- $message = sprintf( __( 'The %s component requires that you have the <strong>%s</strong> plugin installed and activated.', 'pods' ), PodsInit::$components->components[ $component ][ 'Name' ], $dependency[ 0 ] ) . $website;
2012
-
2013
- $ui->error( $message );
2014
-
2015
- $ui->manage();
2016
-
2017
- return;
2018
- }
2019
- }
2020
-
2021
- if ( !empty( PodsInit::$components->components[ $component ][ 'ThemeDependency' ] ) ) {
2022
- $dependency = explode( '|', PodsInit::$components->components[ $component ][ 'ThemeDependency' ] );
2023
-
2024
- if ( strtolower( $dependency[ 1 ] ) != strtolower( get_template() ) && strtolower( $dependency[ 1 ] ) != strtolower( get_stylesheet() ) ) {
2025
- $website = '';
2026
-
2027
- if ( isset( $dependency[ 2 ] ) )
2028
- $website = ' ' . sprintf( __( 'You can find it at %s', 'pods' ), '<a href="' . $dependency[ 2 ] . '" target="_blank">' . $dependency[ 2 ] . '</a>' );
2029
-
2030
- $message = sprintf( __( 'The %s component requires that you have the <strong>%s</strong> theme installed and activated.', 'pods' ), PodsInit::$components->components[ $component ][ 'Name' ], $dependency[ 0 ] ) . $website;
2031
-
2032
- $ui->error( $message );
2033
-
2034
- $ui->manage();
2035
-
2036
- return;
2037
- }
2038
- }
2039
-
2040
- if ( !empty( PodsInit::$components->components[ $component ][ 'MustUse' ] ) ) {
2041
- $message = sprintf( __( 'The %s component can not be disabled from here. You must deactivate the plugin or theme that added it.', 'pods' ), PodsInit::$components->components[ $component ][ 'Name' ] );
2042
-
2043
- $ui->error( $message );
2044
-
2045
- $ui->manage();
2046
-
2047
- return;
2048
- }
2049
-
2050
- if ( '1' == pods_v( 'toggled' ) ) {
2051
- $toggle = PodsInit::$components->toggle( $component );
2052
-
2053
- if ( true === $toggle )
2054
- $ui->message( PodsInit::$components->components[ $component ][ 'Name' ] . ' ' . __( 'Component enabled', 'pods' ) );
2055
- elseif ( false === $toggle )
2056
- $ui->message( PodsInit::$components->components[ $component ][ 'Name' ] . ' ' . __( 'Component disabled', 'pods' ) );
2057
-
2058
- $components = PodsInit::$components->components;
2059
-
2060
- foreach ( $components as $component => &$component_data ) {
2061
- $toggle = 0;
2062
-
2063
- if ( isset( PodsInit::$components->settings[ 'components' ][ $component_data[ 'ID' ] ] ) ) {
2064
- if ( 0 != PodsInit::$components->settings[ 'components' ][ $component_data[ 'ID' ] ] )
2065
- $toggle = 1;
2066
- }
2067
- if ( true === $component_data[ 'DeveloperMode' ] ) {
2068
- if ( !pods_developer() ) {
2069
- unset( $components[ $component ] );
2070
- continue;
2071
- }
2072
- }
2073
-
2074
- $component_data = array(
2075
- 'id' => $component_data[ 'ID' ],
2076
- 'name' => $component_data[ 'Name' ],
2077
- 'description' => make_clickable( $component_data[ 'Description' ] ),
2078
- 'version' => $component_data[ 'Version' ],
2079
- 'author' => $component_data[ 'Author' ],
2080
- 'toggle' => $toggle
2081
- );
2082
- }
2083
-
2084
- $ui->data = $components;
2085
-
2086
- pods_transient_clear( 'pods_components' );
2087
-
2088
- $url = pods_query_arg( array( 'toggled' => null ) );
2089
-
2090
- pods_redirect( $url );
2091
- }
2092
- elseif ( 1 == pods_var( 'toggle' ) )
2093
- $ui->message( PodsInit::$components->components[ $component ][ 'Name' ] . ' ' . __( 'Component enabled', 'pods' ) );
2094
- else
2095
- $ui->message( PodsInit::$components->components[ $component ][ 'Name' ] . ' ' . __( 'Component disabled', 'pods' ) );
2096
-
2097
- $ui->manage();
2098
- }
2099
-
2100
- /**
2101
- * Get the admin upgrade page
2102
- */
2103
- public function admin_upgrade () {
2104
- foreach ( PodsInit::$upgrades as $old_version => $new_version ) {
2105
- if ( version_compare( $old_version, PodsInit::$version_last, '<=' ) && version_compare( PodsInit::$version_last, $new_version, '<' ) ) {
2106
- $new_version = str_replace( '.', '_', $new_version );
2107
-
2108
- pods_view( PODS_DIR . 'ui/admin/upgrade/upgrade_' . $new_version . '.php', compact( array_keys( get_defined_vars() ) ) );
2109
-
2110
- break;
2111
- }
2112
- }
2113
- }
2114
-
2115
- /**
2116
- * Get the admin help page
2117
- */
2118
- public function admin_help () {
2119
- pods_view( PODS_DIR . 'ui/admin/help.php', compact( array_keys( get_defined_vars() ) ) );
2120
- }
2121
-
2122
- /**
2123
- * Add pods specific capabilities.
2124
- *
2125
- * @param $capabilities List of extra capabilities to add
2126
- *
2127
- * @return array
2128
- */
2129
- public function admin_capabilities ( $capabilities ) {
2130
- $pods = pods_api()->load_pods( array( 'type' => array( 'pod', 'table', 'post_type', 'taxonomy', 'settings' ) ) );
2131
-
2132
- $capabilities[] = 'pods';
2133
- $capabilities[] = 'pods_content';
2134
- $capabilities[] = 'pods_settings';
2135
- $capabilities[] = 'pods_components';
2136
-
2137
- foreach ( $pods as $pod ) {
2138
- if ( 'settings' == $pod[ 'type' ] ) {
2139
- $capabilities[] = 'pods_edit_' . $pod[ 'name' ];
2140
- }
2141
- elseif ( 'post_type' == $pod[ 'type' ] ) {
2142
- $capability_type = pods_var( 'capability_type_custom', $pod[ 'options' ], pods_var_raw( 'name', $pod ) );
2143
-
2144
- if ( 'custom' == pods_var( 'capability_type', $pod[ 'options' ] ) && 0 < strlen( $capability_type ) ) {
2145
- $capabilities[] = 'read_' . $capability_type;
2146
- $capabilities[] = 'edit_' . $capability_type;
2147
- $capabilities[] = 'delete_' . $capability_type;
2148
-
2149
- if ( 1 == pods_var( 'capability_type_extra', $pod[ 'options' ], 1 ) ) {
2150
- $capabilities[] = 'read_private_' . $capability_type . 's';
2151
- $capabilities[] = 'edit_' . $capability_type . 's';
2152
- $capabilities[] = 'edit_others_' . $capability_type . 's';
2153
- $capabilities[] = 'edit_private_' . $capability_type . 's';
2154
- $capabilities[] = 'edit_published_' . $capability_type . 's';
2155
- $capabilities[] = 'publish_' . $capability_type . 's';
2156
- $capabilities[] = 'delete_' . $capability_type . 's';
2157
- $capabilities[] = 'delete_private_' . $capability_type . 's';
2158
- $capabilities[] = 'delete_published_' . $capability_type . 's';
2159
- $capabilities[] = 'delete_others_' . $capability_type . 's';
2160
- }
2161
- }
2162
- }
2163
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
2164
- if ( 1 == pods_var( 'capabilities', $pod[ 'options' ], 0 ) ) {
2165
- $capability_type = pods_var( 'capability_type_custom', $pod[ 'options' ], pods_var_raw( 'name', $pod ) . 's' );
2166
-
2167
- $capabilities[] = 'manage_' . $capability_type;
2168
- $capabilities[] = 'edit_' . $capability_type;
2169
- $capabilities[] = 'delete_' . $capability_type;
2170
- $capabilities[] = 'assign_' . $capability_type;
2171
- }
2172
- }
2173
- else {
2174
- $capabilities[] = 'pods_add_' . $pod[ 'name' ];
2175
- $capabilities[] = 'pods_edit_' . $pod[ 'name' ];
2176
-
2177
- if ( isset( $pod[ 'fields' ][ 'author' ] ) && 'pick' == $pod[ 'fields' ][ 'author' ][ 'type' ] && 'user' == $pod[ 'fields' ][ 'author' ][ 'pick_object' ] )
2178
- $capabilities[] = 'pods_edit_others_' . $pod[ 'name' ];
2179
-
2180
- $capabilities[] = 'pods_delete_' . $pod[ 'name' ];
2181
-
2182
- if ( isset( $pod[ 'fields' ][ 'author' ] ) && 'pick' == $pod[ 'fields' ][ 'author' ][ 'type' ] && 'user' == $pod[ 'fields' ][ 'author' ][ 'pick_object' ] )
2183
- $capabilities[] = 'pods_delete_others_' . $pod[ 'name' ];
2184
-
2185
- $actions_enabled = pods_var_raw( 'ui_actions_enabled', $pod[ 'options' ] );
2186
-
2187
- if ( !empty( $actions_enabled ) )
2188
- $actions_enabled = (array) $actions_enabled;
2189
- else
2190
- $actions_enabled = array();
2191
-
2192
- $available_actions = array(
2193
- 'add',
2194
- 'edit',
2195
- 'duplicate',
2196
- 'delete',
2197
- 'reorder',
2198
- 'export'
2199
- );
2200
-
2201
- if ( !empty( $actions_enabled ) ) {
2202
- $actions_disabled = array(
2203
- 'view' => 'view'
2204
- );
2205
-
2206
- foreach ( $available_actions as $action ) {
2207
- if ( !in_array( $action, $actions_enabled ) )
2208
- $actions_disabled[ $action ] = $action;
2209
- }
2210
-
2211
- if ( !in_array( 'export', $actions_disabled ) )
2212
- $capabilities[] = 'pods_export_' . $pod[ 'name' ];
2213
-
2214
- if ( !in_array( 'reorder', $actions_disabled ) )
2215
- $capabilities[] = 'pods_reorder_' . $pod[ 'name' ];
2216
- }
2217
- elseif ( 1 == pods_var( 'ui_export', $pod[ 'options' ], 0 ) )
2218
- $capabilities[] = 'pods_export_' . $pod[ 'name' ];
2219
- }
2220
- }
2221
-
2222
- return $capabilities;
2223
- }
2224
-
2225
- /**
2226
- * Handle ajax calls for the administration
2227
- */
2228
- public function admin_ajax () {
2229
- if ( false === headers_sent() ) {
2230
- pods_session_start();
2231
-
2232
- header( 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' ) );
2233
- }
2234
-
2235
- // Sanitize input
2236
- $params = pods_unslash( (array) $_POST );
2237
-
2238
- foreach ( $params as $key => $value ) {
2239
- if ( 'action' == $key )
2240
- continue;
2241
-
2242
- // Fixup $_POST data
2243
- $_POST[ str_replace( '_podsfix_', '', $key ) ] = $_POST[ $key ];
2244
-
2245
- // Fixup $params with unslashed data
2246
- $params[ str_replace( '_podsfix_', '', $key ) ] = $value;
2247
-
2248
- // Unset the _podsfix_* keys
2249
- unset( $params[ $key ] );
2250
- }
2251
-
2252
- $params = (object) $params;
2253
-
2254
- $methods = array(
2255
- 'add_pod' => array( 'priv' => true ),
2256
- 'save_pod' => array( 'priv' => true ),
2257
- 'load_sister_fields' => array( 'priv' => true ),
2258
- 'process_form' => array( 'custom_nonce' => true ), // priv handled through nonce
2259
- 'upgrade' => array( 'priv' => true ),
2260
- 'migrate' => array( 'priv' => true )
2261
- );
2262
-
2263
- $methods = apply_filters( 'pods_admin_ajax_methods', $methods, $this );
2264
-
2265
- if ( !isset( $params->method ) || !isset( $methods[ $params->method ] ) )
2266
- pods_error( 'Invalid AJAX request', $this );
2267
-
2268
- $defaults = array(
2269
- 'priv' => null,
2270
- 'name' => $params->method,
2271
- 'custom_nonce' => null
2272
- );
2273
-
2274
- $method = (object) array_merge( $defaults, (array) $methods[ $params->method ] );
2275
-
2276
- if ( true !== $method->custom_nonce && ( !isset( $params->_wpnonce ) || false === wp_verify_nonce( $params->_wpnonce, 'pods-' . $params->method ) ) )
2277
- pods_error( __( 'Unauthorized request', 'pods' ), $this );
2278
-
2279
- // Cleaning up $params
2280
- unset( $params->action );
2281
- unset( $params->method );
2282
-
2283
- if ( true !== $method->custom_nonce )
2284
- unset( $params->_wpnonce );
2285
-
2286
- // Check permissions (convert to array to support multiple)
2287
- if ( !empty( $method->priv ) && !pods_is_admin( array( 'pods' ) ) && true !== $method->priv && !pods_is_admin( $method->priv ) )
2288
- pods_error( __( 'Access denied', 'pods' ), $this );
2289
-
2290
- $params->method = $method->name;
2291
-
2292
- $params = apply_filters( 'pods_api_' . $method->name, $params, $method );
2293
-
2294
- $api = pods_api();
2295
-
2296
- $api->display_errors = false;
2297
-
2298
- if ( 'upgrade' == $method->name )
2299
- $output = (string) pods_upgrade( $params->version )->ajax( $params );
2300
- elseif ( 'migrate' == $method->name )
2301
- $output = (string) apply_filters( 'pods_api_migrate_run', $params );
2302
- else {
2303
- if ( !method_exists( $api, $method->name ) )
2304
- pods_error( 'API method does not exist', $this );
2305
- elseif ( 'save_pod' == $method->name ) {
2306
- if ( isset( $params->field_data_json ) && is_array( $params->field_data_json ) ) {
2307
- $params->fields = $params->field_data_json;
2308
-
2309
- unset( $params->field_data_json );
2310
-
2311
- foreach ( $params->fields as $k => $v ) {
2312
- if ( empty( $v ) )
2313
- unset( $params->fields[ $k ] );
2314
- elseif ( !is_array( $v ) )
2315
- $params->fields[ $k ] = (array) @json_decode( $v, true );
2316
- }
2317
- }
2318
- }
2319
-
2320
- // Dynamically call the API method
2321
- $params = (array) $params;
2322
-
2323
- $output = call_user_func( array( $api, $method->name ), $params );
2324
- }
2325
-
2326
- // Output in json format
2327
- if ( false !== $output ) {
2328
- if ( is_array( $output ) || is_object( $output ) )
2329
- wp_send_json( $output );
2330
- else
2331
- echo $output;
2332
- }
2333
- else
2334
- pods_error( 'There was a problem with your request.' );
2335
-
2336
- die(); // KBAI!
2337
- }
2338
-
2339
- /**
2340
- * Profiles the Pods configuration
2341
- *
2342
- * @param null|string|array $pod. Optional. Which Pod(s) to get configuration for. Can be a the name of one Pod, or an array of names of Pods, or null, which is the default, to profile all Pods.
2343
- * @param bool $full_field_info Optional. If true all info about each field is returned. If false, which is the default only name and type, will be returned.
2344
- *
2345
- * @return array
2346
- *
2347
- * @since 3.0.0
2348
- */
2349
- function configuration( $pod = null, $full_field_info = false ){
2350
- $api = pods_api();
2351
-
2352
- if ( is_null( $pod ) ) {
2353
- $the_pods = $api->load_pods();
2354
- }
2355
- elseif( is_array( $pod ) ) {
2356
- foreach ( $pod as $p ) {
2357
- $the_pods[] = $api->load_pod( $p );
2358
- }
2359
- }
2360
- else {
2361
- $the_pods[] = $api->load_pod( $pod );
2362
- }
2363
-
2364
- foreach( $the_pods as $pod ) {
2365
- $configuration[ $pod[ 'name' ] ] = array(
2366
- 'name' => $pod['name'],
2367
- 'ID' => $pod[ 'id' ],
2368
- 'storage' => $pod[ 'storage' ],
2369
- 'fields' => $pod[ 'fields' ],
2370
- );
2371
- }
2372
-
2373
- if ( ! $full_field_info ) {
2374
- foreach ( $the_pods as $pod ) {
2375
- $fields = $configuration[ $pod['name'] ][ 'fields' ];
2376
- unset( $configuration[ $pod['name'] ][ 'fields' ] );
2377
- foreach ( $fields as $field ) {
2378
- $info = array (
2379
- 'name' => $field[ 'name' ],
2380
- 'type' => $field[ 'type' ],
2381
- );
2382
-
2383
- if ( $info[ 'type' ] === 'pick' ) {
2384
- $info[ 'pick_object' ] = $field[ 'pick_object' ];
2385
- if ( isset ( $field[ 'pick_val' ] ) && $field[ 'pick_val' ] !== '' ) {
2386
- $info[ 'pick_val' ] = $field[ 'pick_val' ];
2387
- }
2388
- }
2389
-
2390
- if ( is_array( $info ) ) {
2391
- $configuration[ $pod[ 'name' ] ][ 'fields' ][ $field[ 'name' ] ] = $info;
2392
- }
2393
-
2394
- unset( $info );
2395
-
2396
- }
2397
-
2398
- }
2399
-
2400
- }
2401
-
2402
- if ( is_array ( $configuration ) ) {
2403
- return $configuration;
2404
-
2405
- }
2406
-
2407
- }
2408
-
2409
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
classes/PodsArray.php DELETED
@@ -1,213 +0,0 @@
1
- <?php
2
- /**
3
- * @package Pods
4
- */
5
- class PodsArray implements ArrayAccess {
6
-
7
- /**
8
- * @var array|mixed
9
- */
10
- private $__container = array();
11
-
12
- /**
13
- * Alternative to get_object_vars to access an object as an array with simple functionality and accepts arrays to
14
- * add additional functionality. Additional functionality includes validation and setting default data.
15
- *
16
- * @param mixed $container Object (or existing Array)
17
- *
18
- * @return \PodsArray
19
- *
20
- * @license http://www.gnu.org/licenses/gpl-2.0.html
21
- * @since 2.0
22
- */
23
- public function __construct ( $container ) {
24
- if ( is_array( $container ) || is_object( $container ) )
25
- $this->__container = &$container;
26
- }
27
-
28
- /**
29
- * Set value from array usage $object['offset'] = 'value';
30
- *
31
- * @param mixed $offset Used to set index of Array or Variable name on Object
32
- * @param mixed $value Value to be set
33
- *
34
- * @return mixed|void
35
- * @since 2.0
36
- */
37
- public function offsetSet ( $offset, $value ) {
38
- if ( is_array( $this->__container ) )
39
- $this->__container[ $offset ] = $value;
40
- else
41
- $this->__container->$offset = $value;
42
- return $value;
43
- }
44
-
45
- /**
46
- * Get value from array usage $object['offset'];
47
- *
48
- * @param mixed $offset Used to get value of Array or Variable on Object
49
- *
50
- * @return mixed|null
51
- * @since 2.0
52
- */
53
- public function offsetGet ( $offset ) {
54
- if ( is_array( $this->__container ) ) {
55
- if ( isset( $this->__container[ $offset ] ) )
56
- return $this->__container[ $offset ];
57
- return null;
58
- }
59
- if ( isset( $this->__container->$offset ) )
60
- return $this->__container->$offset;
61
- return null;
62
- }
63
-
64
- /**
65
- * Get value from array usage $object['offset'];
66
- *
67
- * @param mixed $offset Used to get value of Array or Variable on Object
68
- *
69
- * @return bool
70
- * @since 2.0
71
- */
72
- public function offsetExists ( $offset ) {
73
- if ( is_array( $this->__container ) )
74
- return isset( $this->__container[ $offset ] );
75
- return isset( $this->__container->$offset );
76
- }
77
-
78
- /**
79
- * Get value from array usage $object['offset'];
80
- *
81
- * @param mixed $offset Used to unset index of Array or Variable on Object
82
- *
83
- * @since 2.0
84
- */
85
- public function offsetUnset ( $offset ) {
86
- if ( is_array( $this->__container ) )
87
- unset( $this->__container[ $offset ] );
88
- else
89
- unset( $this->__container->$offset );
90
- }
91
-
92
- /**
93
- * Validate value on a specific type and set default (if empty)
94
- *
95
- * @param mixed $offset Used to get value of Array or Variable on Object
96
- * @param mixed $default Used to set default value if it doesn't exist
97
- * @param string $type Used to force a specific type of variable (allowed: array, object, integer, absint, boolean)
98
- * @param mixed $extra Used in advanced types of variables
99
- *
100
- * @return array|bool|int|mixed|null|number|object
101
- * @since 2.0
102
- */
103
- public function validate ( $offset, $default = null, $type = null, $extra = null ) {
104
- if ( !$this->offsetExists( $offset ) )
105
- $this->offsetSet( $offset, $default );
106
-
107
- $value = $this->offsetGet( $offset );
108
-
109
- if ( empty( $value ) && null !== $default && false !== $value )
110
- $value = $default;
111
- if ( 'array' == $type || 'array_merge' == $type ) {
112
- if ( !is_array( $value ) )
113
- $value = explode( ',', $value );
114
-
115
- if ( 'array_merge' == $type && $value !== $default )
116
- $value = array_merge( $default, $value );
117
- }
118
- elseif ( 'object' == $type || 'object_merge' == $type ) {
119
- if ( !is_object( $value ) ) {
120
- if ( !is_array( $value ) )
121
- $value = explode( ',', $value );
122
- $value = (object) $value;
123
- }
124
-
125
- if ( 'object_merge' == $type && $value !== $default )
126
- $value = (object) array_merge( (array) $default, (array) $value );
127
- }
128
- elseif ( 'integer' == $type || 'int' == $type || 'absint' == $type ) {
129
- if ( !is_numeric( trim( $value ) ) )
130
- $value = 0;
131
- else
132
- $value = intval( $value );
133
-
134
- if ( 'absint' == $type )
135
- $value = abs( $value );
136
- }
137
- elseif ( 'boolean' == $type || 'bool' == $type )
138
- $value = (boolean) $value;
139
- elseif ( 'in_array' == $type && is_array( $default ) ) {
140
- if ( is_array( $value ) ) {
141
- foreach ( $value as $k => $v ) {
142
- if ( !in_array( $v, $extra ) )
143
- unset( $value[ $k ] );
144
- }
145
- }
146
- elseif ( !in_array( $value, $extra ) )
147
- $value = $default;
148
- }
149
- elseif ( 'isset' == $type && is_array( $default ) ) {
150
- if ( is_array( $value ) ) {
151
- foreach ( $value as $k => $v ) {
152
- if ( !isset( $extra[ $v ] ) )
153
- unset( $value[ $k ] );
154
- }
155
- }
156
- elseif ( !isset( $extra[ $value ] ) )
157
- $value = $default;
158
- }
159
-
160
- $this->offsetSet( $offset, $value );
161
-
162
- return $value;
163
- }
164
-
165
- /**
166
- * Dump the PodsArray object to array
167
- *
168
- * @return array Array version of the object
169
- *
170
- * @since 2.0
171
- */
172
- public function dump () {
173
- if ( is_array( $this->__container ) )
174
- return $this->__container;
175
- return get_object_vars( $this->__container );
176
- }
177
-
178
- /**
179
- * Mapping >> offsetSet
180
- *
181
- * @since 2.0
182
- */
183
- public function __set ( $offset, $value ) {
184
- return $this->offsetSet( $offset, $value );
185
- }
186
-
187
- /**
188
- * Mapping >> offsetGet
189
- *
190
- * @since 2.0
191
- */
192
- public function __get ( $offset ) {
193
- return $this->offsetGet( $offset );
194
- }
195
-
196
- /**
197
- * Mapping >> offsetExists
198
- *
199
- * @since 2.0
200
- */
201
- public function __isset ( $offset ) {
202
- return $this->offsetExists( $offset );
203
- }
204
-
205
- /**
206
- * Mapping >> offsetUnset
207
- *
208
- * @since 2.0
209
- */
210
- public function __unset ( $offset ) {
211
- $this->offsetUnset( $offset );
212
- }
213
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
classes/PodsComponent.php DELETED
@@ -1,90 +0,0 @@
1
- <?php
2
- /**
3
- * The base component class, all components should extend this.
4
- *
5
- * @package Pods
6
- */
7
- class PodsComponent {
8
-
9
- /**
10
- * Do things like register/enqueue scripts and stylesheets
11
- *
12
- * @return \PodsComponent
13
- *
14
- * @since 2.0
15
- */
16
- public function __construct () {
17
-
18
- }
19
-
20
- /**
21
- * Add options and set defaults for component settings, shows in admin area
22
- *
23
- * @return array $options
24
- *
25
- * @since 2.0
26
- public function options () {
27
- $options = array(
28
- 'option_name' => array(
29
- 'label' => 'Option Label',
30
- 'depends-on' => array( 'another_option' => 'specific-value' ),
31
- 'default' => 'default-value',
32
- 'type' => 'field_type',
33
- 'data' => array(
34
- 'value1' => 'Label 1',
35
-
36
- // Group your options together
37
- 'Option Group' => array(
38
- 'gvalue1' => 'Option Label 1',
39
- 'gvalue2' => 'Option Label 2'
40
- ),
41
-
42
- // below is only if the option_name above is the "{$fieldtype}_format_type"
43
- 'value2' => array(
44
- 'label' => 'Label 2',
45
- 'regex' => '[a-zA-Z]' // Uses JS regex validation for the value saved if this option selected
46
- )
47
- ),
48
-
49
- // below is only for a boolean group
50
- 'group' => array(
51
- 'option_boolean1' => array(
52
- 'label' => 'Option boolean 1?',
53
- 'default' => 1,
54
- 'type' => 'boolean'
55
- ),
56
- 'option_boolean2' => array(
57
- 'label' => 'Option boolean 2?',
58
- 'default' => 0,
59
- 'type' => 'boolean'
60
- )
61
- )
62
- )
63
- );
64
-
65
- return $options;
66
- }
67
- */
68
-
69
- /**
70
- * Handler to run code based on $options
71
- *
72
- * @param $options
73
- *
74
- * @since 2.0
75
- */
76
- public function handler ( $options ) {
77
- // run code based on $options set
78
- }
79
-
80
- /**
81
- * Build admin area
82
- *
83
- * @param $options
84
- *
85
- * @since 2.0
86
- public function admin ( $options ) {
87
- // run code based on $options set
88
- }
89
- */
90
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
classes/PodsComponents.php DELETED
@@ -1,667 +0,0 @@
1
- <?php
2
- /**
3
- * Component managing class
4
- *
5
- * @package Pods
6
- */
7
- class PodsComponents {
8
-
9
- /**
10
- * @var PodsComponents
11
- */
12
- static $instance = null;
13
-
14
- /**
15
- * Root of Components directory
16
- *
17
- * @var string
18
- *
19
- * @private
20
- * @since 2.0
21
- */
22
- private $components_dir = null;
23
-
24
- /**
25
- * Available components
26
- *
27
- * @var string
28
- *
29
- * @since 2.0
30
- */
31
- public $components = array();
32
-
33
- /**
34
- * Components settings
35
- *
36
- * @var string
37
- *
38
- * @since 2.0
39
- */
40
- public $settings = array();
41
-
42
- /**
43
- * Singleton handling for a basic pods_components() request
44
- *
45
- * @return \PodsComponents
46
- *
47
- * @since 2.3.5
48
- */
49
- public static function init () {
50
- if ( !is_object( self::$instance ) )
51
- self::$instance = new PodsComponents();
52
-
53
- return self::$instance;
54
- }
55
-
56
- /**
57
- * Setup actions and get options
58
- *
59
- * @return \PodsComponents
60
- *
61
- * @since 2.0
62
- */
63
- public function __construct () {
64
- $this->components_dir = realpath( apply_filters( 'pods_components_dir', PODS_DIR . 'components' ) ) . '/';
65
-
66
- $settings = get_option( 'pods_component_settings', '' );
67
-
68
- if ( !empty( $settings ) )
69
- $this->settings = (array) json_decode( $settings, true );
70
-
71
- if ( !isset( $this->settings[ 'components' ] ) )
72
- $this->settings[ 'components' ] = array();
73
-
74
- // Get components (give it access to theme)
75
- add_action( 'setup_theme', array( $this, 'get_components' ), 11 );
76
-
77
- // Load in components
78
- add_action( 'setup_theme', array( $this, 'load' ), 12 );
79
-
80
- // AJAX handling
81
- if ( is_admin() ) {
82
- add_action( 'wp_ajax_pods_admin_components', array( $this, 'admin_ajax' ) );
83
- add_action( 'wp_ajax_nopriv_pods_admin_components', array( $this, 'admin_ajax' ) );
84
-
85
- // Add the Pods Components capabilities
86
- add_filter( 'members_get_capabilities', array( $this, 'admin_capabilities' ) );
87
- }
88
- }
89
-
90
- /**
91
- * Add menu item
92
- *
93
- * @param string $parent The parent slug.
94
- *
95
- * @since 2.0
96
- *
97
- * @uses add_submenu_page
98
- */
99
- public function menu ( $parent ) {
100
- global $submenu;
101
-
102
- $custom_component_menus = array();
103
-
104
- $pods_component_menu_items = array();
105
-
106
- foreach ( $this->components as $component => $component_data ) {
107
- $component_data[ 'MustUse' ] = apply_filters( 'pods_component_require_' . $component_data[ 'ID' ], $component_data[ 'MustUse' ], $component_data );
108
-
109
- if ( empty( $component_data[ 'MustUse' ] ) && ( !isset( $this->settings[ 'components' ][ $component ] ) || 0 == $this->settings[ 'components' ][ $component ] ) )
110
- continue;
111
-
112
- if ( !empty( $component_data[ 'Hide' ] ) )
113
- continue;
114
-
115
- if ( !empty( $component_data[ 'DeveloperMode' ] ) && !pods_developer() )
116
- continue;
117
-
118
- if ( empty( $component_data[ 'TablelessMode' ] ) && pods_tableless() )
119
- continue;
120
-
121
- if ( empty( $component_data[ 'MenuPage' ] ) ) {
122
- if ( !isset( $component_data[ 'object' ] ) )
123
- continue;
124
- elseif ( !method_exists( $component_data[ 'object' ], 'admin' ) && !method_exists( $component_data[ 'object' ], 'options' ) )
125
- continue;
126
- }
127
-
128
- if ( false === $component_data[ 'External' ] )
129
- $component_data[ 'File' ] = realpath( $this->components_dir . $component_data[ 'File' ] );
130
-
131
- if ( !file_exists( $component_data[ 'File' ] ) ) {
132
- pods_message( 'Pods Component not found: ' . $component_data[ 'File' ] );
133
-
134
- pods_transient_clear( 'pods_components' );
135
-
136
- continue;
137
- }
138
-
139
- $capability = 'pods_component_' . str_replace( '-', '_', sanitize_title( $component ) );
140
-
141
- if ( 0 < strlen( $component_data[ 'Capability' ] ) )
142
- $capability = $component_data[ 'Capability' ];
143
-
144
- if ( !pods_is_admin( array( 'pods', 'pods_components', $capability ) ) )
145
- continue;
146
-
147
- $menu_page = 'pods-component-' . $component;
148
-
149
- if ( !empty( $component_data[ 'MenuPage' ] ) )
150
- $custom_component_menus[ $menu_page ] = $component_data;
151
-
152
- $pods_component_menu_items[ $component_data[ 'MenuName' ] ] = array(
153
- 'menu_page' => $menu_page,
154
- 'page_title' => $component_data[ 'Name' ],
155
- 'capability' => 'read',
156
- 'callback' => array( $this, 'admin_handler' )
157
- );
158
-
159
- if ( isset( $component_data[ 'object' ] ) && method_exists( $component_data[ 'object' ], 'admin_assets' ) ) {
160
- $pods_component_menu_items[ $component_data[ 'MenuName' ] ][ 'assets' ] = array( $component_data[ 'object' ], 'admin_assets' );
161
- }
162
- }
163
-
164
- /**
165
- * Add or change the items in the Pods Components Submenu.
166
- *
167
- * Can also be used to change which menu components is a submenu of or change title of menu.
168
- *
169
- * @params array $pods_component_menu_items {
170
- * An array of arguments for add_submenu_page
171
- *
172
- * @param string $parent_slug The slug name for the parent menu (or the file name of a standard WordPress admin page)
173
- * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
174
- * @param $menu_title The text to be used for the menu
175
- * @param $capability The capability required for this menu to be displayed to the user.
176
- * @param $menu_slug The slug name to refer to this menu by (should be unique for this menu)
177
- * @param $function The function to be called to output the content for this page.
178
- * }
179
- *
180
- * @returns array Array of submenu pages to be passed to add_submenu_page()
181
- *
182
- * @since 2.4.1
183
- */
184
- $pods_component_menu_items = apply_filters( 'pods_admin_components_menu', $pods_component_menu_items );
185
-
186
- ksort( $pods_component_menu_items );
187
-
188
- foreach ( $pods_component_menu_items as $menu_title => $menu_data ) {
189
- if ( !is_callable( $menu_data[ 'callback' ] ) ) {
190
- continue;
191
- }
192
-
193
- $page = add_submenu_page(
194
- $parent,
195
- strip_tags( $menu_data[ 'page_title' ] ),
196
- '- ' . strip_tags( $menu_title ),
197
- pods_v( 'capability', $menu_data, 'read', true ),
198
- $menu_data[ 'menu_page' ],
199
- $menu_data[ 'callback' ]
200
- );
201
-
202
- if ( isset( $menu_data[ 'assets' ] ) && is_callable( $menu_data[ 'assets' ] ) ) {
203
- add_action( 'admin_print_styles-' . $page, $menu_data[ 'assets' ] );
204
- }
205
- }
206
-
207
- if ( !empty( $custom_component_menus ) ) {
208
- foreach ( $custom_component_menus as $menu_page => $component_data ) {
209
- if ( isset( $submenu[ $parent ] ) ) {
210
- foreach ( $submenu[ $parent ] as $sub => &$menu ) {
211
- if ( $menu[ 2 ] == $menu_page ) {
212
- $menu_page = $component_data[ 'MenuPage' ];
213
-
214
- /*if ( !empty( $component_data[ 'MenuAddPage' ] ) ) {
215
- if ( false !== strpos( $_SERVER[ 'REQUEST_URI' ], $component_data[ 'MenuAddPage' ] ) )
216
- $menu_page = $component_data[ 'MenuAddPage' ];
217
- }*/
218
-
219
- $menu[ 2 ] = $menu_page;
220
-
221
- $page = current( explode( '?', $menu[ 2 ] ) );
222
-
223
- if ( isset( $component_data[ 'object' ] ) && method_exists( $component_data[ 'object' ], 'admin_assets' ) )
224
- add_action( 'admin_print_styles-' . $page, array( $component_data[ 'object' ], 'admin_assets' ) );
225
-
226
- break;
227
- }
228
- }
229
- }
230
- }
231
- }
232
- }
233
-
234
- /**
235
- * Load activated components and init component
236
- *
237
- * @since 2.0
238
- */
239
- public function load () {
240
- do_action( 'pods_components_load' );
241
-
242
- foreach ( (array) $this->components as $component => $component_data ) {
243
- $component_data[ 'MustUse' ] = apply_filters( 'pods_component_require_' . $component_data[ 'ID' ], $component_data[ 'MustUse' ], $component_data );
244
-
245
- if ( false === $component_data[ 'MustUse' ] && ( !isset( $this->settings[ 'components' ][ $component ] ) || 0 == $this->settings[ 'components' ][ $component ] ) )
246
- continue;
247
-
248
- if ( !empty( $component_data[ 'PluginDependency' ] ) ) {
249
- $dependency = explode( '|', $component_data[ 'PluginDependency' ] );
250
-
251
- if ( !pods_is_plugin_active( $dependency[ 1 ] ) )
252
- continue;
253
- }
254
-
255
- if ( !empty( $component_data[ 'ThemeDependency' ] ) ) {
256
- $dependency = explode( '|', $component_data[ 'ThemeDependency' ] );
257
-
258
- if ( strtolower( $dependency[ 1 ] ) != strtolower( get_template() ) && strtolower( $dependency[ 1 ] ) != strtolower( get_stylesheet() ) )
259
- continue;
260
- }
261
-
262
- if ( false === $component_data[ 'External' ] )
263
- $component_data[ 'File' ] = realpath( $this->components_dir . $component_data[ 'File' ] );
264
-
265
- if ( empty( $component_data[ 'File' ] ) ) {
266
- pods_transient_clear( 'pods_components' );
267
-
268
- continue;
269
- }
270
-
271
- if ( !file_exists( $component_data[ 'File' ] ) ) {
272
- pods_message( 'Pods Component not found: ' . $component_data[ 'File' ] );
273
-
274
- pods_transient_clear( 'pods_components' );
275
-
276
- continue;
277
- }
278
-
279
- include_once $component_data[ 'File' ];
280
-
281
- if ( ( !empty( $component_data[ 'Class' ] ) && class_exists( $component_data[ 'Class' ] ) ) || isset( $component_data[ 'object' ] ) ) {
282
- if ( !isset( $this->components[ $component ][ 'object' ] ) )
283
- $this->components[ $component ][ 'object' ] = new $component_data[ 'Class' ];
284
-
285
- if ( method_exists( $this->components[ $component ][ 'object' ], 'options' ) ) {
286
- if ( isset( $this->settings[ 'components' ][ $component ] ) )
287
- $this->components[ $component ][ 'options' ] = $this->components[ $component ][ 'object' ]->options( $this->settings[ 'components' ][ $component ] );
288
- else
289
- $this->components[ $component ][ 'options' ] = $this->components[ $component ][ 'object' ]->options( array() );
290
-
291
- $this->options( $component, $this->components[ $component ][ 'options' ] );
292
- }
293
- else
294
- $this->options( $component, array() );
295
-
296
- if ( method_exists( $this->components[ $component ][ 'object' ], 'handler' ) )
297
- $this->components[ $component ][ 'object' ]->handler( $this->settings[ 'components' ][ $component ] );
298
- }
299
- }
300
- }
301
-
302
- /**
303
- * Get list of components available
304
- *
305
- * @since 2.0
306
- */
307
- public function get_components () {
308
- $components = pods_transient_get( 'pods_components' );
309
-
310
- if ( 1 == pods_var( 'pods_debug_components', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) )
311
- $components = array();
312
-
313
- if ( PodsInit::$version != PODS_VERSION || !is_array( $components ) || empty( $components ) || ( is_admin() && isset( $_GET[ 'page' ] ) && 'pods-components' == $_GET[ 'page' ] && 1 !== pods_transient_get( 'pods_components_refresh' ) ) ) {
314
- do_action( 'pods_components_get' );
315
-
316
- $component_dir = @opendir( untrailingslashit( $this->components_dir ) );
317
- $component_files = array();
318
-
319
- if ( false !== $component_dir ) {
320
- while ( false !== ( $file = readdir( $component_dir ) ) ) {
321
- if ( '.' == substr( $file, 0, 1 ) )
322
- continue;
323
- elseif ( is_dir( $this->components_dir . $file ) ) {
324
- $component_subdir = @opendir( $this->components_dir . $file );
325
-
326
- if ( $component_subdir ) {
327
- while ( false !== ( $subfile = readdir( $component_subdir ) ) ) {
328
- if ( '.' == substr( $subfile, 0, 1 ) )
329
- continue;
330
- elseif ( '.php' == substr( $subfile, -4 ) )
331
- $component_files[] = str_replace( '\\', '/', $file . '/' . $subfile );
332
- }
333
-
334
- closedir( $component_subdir );
335
- }
336
- }
337
- elseif ( '.php' == substr( $file, -4 ) )
338
- $component_files[] = $file;
339
- }
340
-
341
- closedir( $component_dir );
342
- }
343
-
344
- $default_headers = array(
345
- 'ID' => 'ID',
346
- 'Name' => 'Name',
347
- 'ShortName' => 'Short Name',
348
- 'PluginName' => 'Plugin Name',
349
- 'ComponentName' => 'Component Name',
350
- 'URI' => 'URI',
351
- 'MenuName' => 'Menu Name',
352
- 'MenuPage' => 'Menu Page',
353
- 'MenuAddPage' => 'Menu Add Page',
354
- 'MustUse' => 'Must Use',
355
- 'Description' => 'Description',
356
- 'Version' => 'Version',
357
- 'Category' => 'Category',
358
- 'Author' => 'Author',
359
- 'AuthorURI' => 'Author URI',
360
- 'Class' => 'Class',
361
- 'Hide' => 'Hide',
362
- 'PluginDependency' => 'Plugin Dependency',
363
- 'ThemeDependency' => 'Theme Dependency',
364
- 'DeveloperMode' => 'Developer Mode',
365
- 'TablelessMode' => 'Tableless Mode',
366
- 'Capability' => 'Capability',
367
- 'Plugin' => 'Plugin'
368
- );
369
-
370
- $component_files = apply_filters( 'pods_components_register', $component_files );
371
-
372
- $components = array();
373
-
374
- foreach ( $component_files as $component_file ) {
375
- $external = false;
376
-
377
- if ( is_array( $component_file ) && isset( $component_file[ 'File' ] ) ) {
378
- $component = $component_file = $component_file[ 'File' ];
379
-
380
- $external = true;
381
- }
382
- else
383
- $component = $this->components_dir . $component_file;
384
-
385
- if ( !is_readable( $component ) )
386
- continue;
387
-
388
- $component_data = get_file_data( $component, $default_headers, 'pods_component' );
389
-
390
- if ( ( empty( $component_data[ 'Name' ] ) && empty( $component_data[ 'ComponentName' ] ) && empty( $component_data[ 'PluginName' ] ) ) || 'yes' == $component_data[ 'Hide' ] )
391
- continue;
392
-
393
- if ( isset( $component_data[ 'Plugin' ] ) && pods_is_plugin_active( $component_data[ 'Plugin' ] ) )
394
- continue;
395
-
396
- if ( empty( $component_data[ 'Name' ] ) ) {
397
- if ( !empty( $component_data[ 'ComponentName' ] ) )
398
- $component_data[ 'Name' ] = $component_data[ 'ComponentName' ];
399
- elseif ( !empty( $component_data[ 'PluginName' ] ) )
400
- $component_data[ 'Name' ] = $component_data[ 'PluginName' ];
401
- }
402
-
403
- if ( empty( $component_data[ 'ShortName' ] ) )
404
- $component_data[ 'ShortName' ] = $component_data[ 'Name' ];
405
-
406
- if ( empty( $component_data[ 'MenuName' ] ) )
407
- $component_data[ 'MenuName' ] = $component_data[ 'Name' ];
408
-
409
- if ( empty( $component_data[ 'Class' ] ) )
410
- $component_data[ 'Class' ] = 'Pods_' . pods_js_name( basename( $component, '.php' ), false );
411
-
412
- if ( empty( $component_data[ 'ID' ] ) )
413
- $component_data[ 'ID' ] = $component_data[ 'Name' ];
414
-
415
- $component_data[ 'ID' ] = sanitize_title( $component_data[ 'ID' ] );
416
-
417
- if ( 'on' == strtolower( $component_data[ 'DeveloperMode' ] ) || 1 == $component_data[ 'DeveloperMode' ] )
418
- $component_data[ 'DeveloperMode' ] = true;
419
- else
420
- $component_data[ 'DeveloperMode' ] = false;
421
-
422
- if ( 'on' == strtolower( $component_data[ 'TablelessMode' ] ) || 1 == $component_data[ 'TablelessMode' ] )
423
- $component_data[ 'TablelessMode' ] = true;
424
- else
425
- $component_data[ 'TablelessMode' ] = false;
426
-
427
- $component_data[ 'External' ] = (boolean) $external;
428
-
429
- if ( 'on' == strtolower($component_data[ 'MustUse' ] ) || '1' == $component_data[ 'MustUse' ] )
430
- $component_data[ 'MustUse' ] = true;
431
- elseif ( 'off' == strtolower($component_data[ 'MustUse' ] ) || '0' == $component_data[ 'MustUse' ] )
432
- $component_data[ 'MustUse' ] = false;
433
- else
434
- $component_data[ 'MustUse' ] = $component_data[ 'External' ];
435
-
436
- $component_data[ 'File' ] = $component_file;
437
-
438
- $components[ $component_data[ 'ID' ] ] = $component_data;
439
- }
440
-
441
- ksort( $components );
442
-
443
- pods_transient_set( 'pods_components_refresh', 1, ( 60 * 60 * 12 ) );
444
-
445
- pods_transient_set( 'pods_components', $components );
446
- }
447
-
448
- if ( 1 == pods_var( 'pods_debug_components', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) )
449
- pods_debug( $components );
450
-
451
- $this->components = $components;
452
-
453
- return $this->components;
454
- }
455
-
456
- /**
457
- * Set component options
458
- *
459
- * @param $component
460
- * @param $options
461
- *
462
- * @since 2.0
463
- */
464
- public function options ( $component, $options ) {
465
- if ( !isset( $this->settings[ 'components' ][ $component ] ) || !is_array( $this->settings[ 'components' ][ $component ] ) )
466
- $this->settings[ 'components' ][ $component ] = array();
467
-
468
- foreach ( $options as $option => $data ) {
469
- if ( !isset( $this->settings[ 'components' ][ $component ][ $option ] ) && isset( $data[ 'default' ] ) )
470
- $this->settings[ 'components' ][ $component ][ $option ] = $data[ 'default' ];
471
- }
472
- }
473
-
474
- /**
475
- * Call component specific admin functions
476
- *
477
- * @since 2.0
478
- */
479
- public function admin_handler () {
480
- $component = str_replace( 'pods-component-', '', $_GET[ 'page' ] );
481
-
482
- if ( isset( $this->components[ $component ] ) && isset( $this->components[ $component ][ 'object' ] ) && is_object( $this->components[ $component ][ 'object' ] ) ) {
483
- // Component init
484
- if ( method_exists( $this->components[ $component ][ 'object' ], 'init' ) )
485
- $this->components[ $component ][ 'object' ]->init( $this->settings[ 'components' ][ $component ], $component );
486
-
487
- // Component Admin handler
488
- if ( method_exists( $this->components[ $component ][ 'object' ], 'admin' ) )
489
- $this->components[ $component ][ 'object' ]->admin( $this->settings[ 'components' ][ $component ], $component );
490
- // Built-in Admin Handler
491
- elseif ( method_exists( $this->components[ $component ][ 'object' ], 'options' ) )
492
- $this->admin( $this->components[ $component ][ 'object' ]->options( $this->settings[ 'components' ][ $component ] ), $this->settings[ 'components' ][ $component ], $component );
493
- }
494
- }
495
-
496
- public function admin ( $options, $settings, $component ) {
497
- if ( !isset( $this->components[ $component ] ) )
498
- wp_die( 'Invalid Component' );
499
-
500
- $component_label = $this->components[ $component ][ 'Name' ];
501
-
502
- include PODS_DIR . 'ui/admin/components-admin.php';
503
- }
504
-
505
- /**
506
- * Toggle a component on or off
507
- *
508
- * @param string $component The component name to toggle
509
- *
510
- * @return bool
511
- *
512
- * @since 2.0
513
- */
514
- public function toggle ( $component ) {
515
- $toggle = null;
516
-
517
- if ( isset( $this->components[ $component ] ) ) {
518
- if ( 1 == pods_var( 'toggle', 'get' ) && ( !isset( $this->settings[ 'components' ][ $component ] ) || 0 == $this->settings[ 'components' ][ $component ] ) ) {
519
- $this->settings[ 'components' ][ $component ] = array();
520
- $toggle = true;
521
- }
522
- elseif ( 0 == pods_var( 'toggle', 'get' ) ) {
523
- $this->settings[ 'components' ][ $component ] = 0;
524
- $toggle = false;
525
- }
526
- }
527
-
528
- $settings = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $this->settings, JSON_UNESCAPED_UNICODE ) : json_encode( $this->settings );
529
-
530
- update_option( 'pods_component_settings', $settings );
531
-
532
- return $toggle;
533
- }
534
-
535
- /**
536
- * Add pods specific capabilities.
537
- *
538
- * @param $capabilities List of extra capabilities to add
539
- *
540
- * @return array
541
- */
542
- public function admin_capabilities ( $capabilities ) {
543
- foreach ( $this->components as $component => $component_data ) {
544
- if ( !empty( $component_data[ 'Hide' ] ) )
545
- continue;
546
-
547
- if ( true === (boolean) pods_var( 'DeveloperMode', $component_data, false ) && !pods_developer() )
548
- continue;
549
-
550
- if ( true === (boolean) pods_var( 'TablelessMode', $component_data, false ) && !pods_developer() )
551
- continue;
552
-
553
- if ( empty( $component_data[ 'MenuPage' ] ) && ( !isset( $component_data[ 'object' ] ) || !method_exists( $component_data[ 'object' ], 'admin' ) ) )
554
- continue;
555
-
556
- $capability = 'pods_component_' . str_replace( '-', '_', sanitize_title( str_replace( ' and ', ' ', strip_tags( $component_data[ 'Name' ] ) ) ) );
557
-
558
- if ( 0 < strlen ( $component_data[ 'Capability' ] ) )
559
- $capability = $component_data[ 'Capability' ];
560
-
561
- if ( !in_array( $capability, $capabilities ) )
562
- $capabilities[] = $capability;
563
- }
564
-
565
- return $capabilities;
566
- }
567
-
568
- /**
569
- * Handle admin ajax
570
- *
571
- * @since 2.0
572
- */
573
- public function admin_ajax () {
574
- if ( false === headers_sent() ) {
575
- pods_session_start();
576
-
577
- header( 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' ) );
578
- }
579
-
580
- // Sanitize input
581
- $params = pods_unslash( (array) $_POST );
582
-
583
- foreach ( $params as $key => $value ) {
584
- if ( 'action' == $key )
585
- continue;
586
-
587
- unset( $params[ $key ] );
588
-
589
- $params[ str_replace( '_podsfix_', '', $key ) ] = $value;
590
- }
591
-
592
- $params = (object) $params;
593
-
594
- $component = $params->component;
595
- $method = $params->method;
596
-
597
- if ( !isset( $component ) || !isset( $this->components[ $component ] ) || !isset( $this->settings[ 'components' ][ $component ] ) )
598
- pods_error( 'Invalid AJAX request', $this );
599
-
600
- if ( !isset( $params->_wpnonce ) || false === wp_verify_nonce( $params->_wpnonce, 'pods-component-' . $component . '-' . $method ) )
601
- pods_error( 'Unauthorized request', $this );
602
-
603
- // Cleaning up $params
604
- unset( $params->action );
605
- unset( $params->component );
606
- unset( $params->method );
607
- unset( $params->_wpnonce );
608
-
609
- $params = (object) apply_filters( 'pods_component_ajax_' . $component . '_' . $method, $params, $component, $method );
610
-
611
- $output = false;
612
-
613
- // Component init
614
- if ( isset( $this->components[ $component ][ 'object' ] ) && method_exists( $this->components[ $component ][ 'object' ], 'init' ) )
615
- $this->components[ $component ][ 'object' ]->init( $this->settings[ 'components' ][ $component ], $component );
616
-
617
- // Handle internal methods
618
- if ( isset( $this->components[ $component ][ 'object' ] ) && !method_exists( $this->components[ $component ][ 'object' ], 'ajax_' . $method ) && method_exists( $this, 'admin_ajax_' . $method ) )
619
- $output = call_user_func( array( $this, 'admin_ajax_' . $method ), $component, $params );
620
- // Make sure method exists
621
- elseif ( !isset( $this->components[ $component ][ 'object' ] ) || !method_exists( $this->components[ $component ][ 'object' ], 'ajax_' . $method ) )
622
- pods_error( 'API method does not exist', $this );
623
- // Dynamically call the component method
624
- else
625
- $output = call_user_func( array( $this->components[ $component ][ 'object' ], 'ajax_' . $method ), $params );
626
-
627
- if ( !is_bool( $output ) )
628
- echo $output;
629
-
630
- die(); // KBAI!
631
- }
632
-
633
- public function admin_ajax_settings ( $component, $params ) {
634
- if ( !isset( $this->components[ $component ] ) )
635
- wp_die( 'Invalid Component' );
636
- elseif ( !method_exists( $this->components[ $component ][ 'object' ], 'options' ) )
637
- pods_error( 'Component options method does not exist', $this );
638
-
639
- $options = $this->components[ $component ][ 'object' ]->options( $this->settings[ 'components' ][ $component ] );
640
-
641
- if ( empty( $this->settings[ 'components' ][ $component ] ) )
642
- $this->settings[ 'components' ][ $component ] = array();
643
-
644
- foreach ( $options as $field_name => $field_option ) {
645
- $field_option = PodsForm::field_setup( $field_option, null, $field_option[ 'type' ] );
646
-
647
- if ( !is_array( $field_option[ 'group' ] ) ) {
648
- $field_value = pods_var_raw( 'pods_setting_' . $field_name, $params );
649
-
650
- $this->settings[ 'components' ][ $component ][ $field_name ] = $field_value;
651
- }
652
- else {
653
- foreach ( $field_option[ 'group' ] as $field_group_name => $field_group_option ) {
654
- $field_value = pods_var_raw( 'pods_setting_' . $field_group_name, $params );
655
-
656
- $this->settings[ 'components' ][ $component ][ $field_group_name ] = $field_value;
657
- }
658
- }
659
- }
660
-
661
- $settings = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $this->settings, JSON_UNESCAPED_UNICODE ) : json_encode( $this->settings );
662
-
663
- update_option( 'pods_component_settings', $settings );
664
-
665
- return '1';
666
- }
667
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
classes/PodsData.php DELETED
@@ -1,3063 +0,0 @@
1
- <?php
2
- /**
3
- * @package Pods
4
- */
5
- class PodsData {
6
-
7
- /**
8
- * @var PodsData
9
- */
10
- static $instance = null;
11
-
12
- /**
13
- * @var string
14
- */
15
- static protected $prefix = 'pods_';
16
-
17
- /**
18
- * @var array
19
- */
20
- static protected $field_types = array();
21
-
22
- /**
23
- * @var bool
24
- */
25
- public static $display_errors = true;
26
-
27
- /**
28
- * @var PodsAPI
29
- */
30
- public $api = null;
31
-
32
- /**
33
- * @var null
34
- */
35
- public $select = null;
36
-
37
- /**
38
- * @var null
39
- */
40
- public $table = null;
41
-
42
- /**
43
- * @var null
44
- */
45
- public $pod = null;
46
-
47
- /**
48
- * @var array|bool|mixed|null|void
49
- */
50
- public $pod_data = null;
51
-
52
- /**
53
- * @var int
54
- */
55
- public $id = 0;
56
-
57
- /**
58
- * @var string
59
- */
60
- public $field_id = 'id';
61
-
62
- /**
63
- * @var string
64
- */
65
- public $field_index = 'name';
66
-
67
- /**
68
- * @var string
69
- */
70
- public $field_slug = '';
71
-
72
- /**
73
- * @var string
74
- */
75
- public $join = '';
76
-
77
- /**
78
- * @var array
79
- */
80
- public $where = array();
81
-
82
- /**
83
- * @var array
84
- */
85
- public $where_default = array();
86
-
87
- /**
88
- * @var string
89
- */
90
- public $orderby = '';
91
-
92
- /**
93
- * @var array
94
- */
95
- public $fields = array();
96
-
97
- /**
98
- * @var array
99
- */
100
- public $aliases = array();
101
-
102
- /**
103
- * @var
104
- */
105
- public $detail_page;
106
-
107
- // data
108
- /**
109
- * @var int
110
- */
111
- public $row_number = -1;
112
-
113
- /**
114
- * @var
115
- */
116
- public $data;
117
-
118
- /**
119
- * @var
120
- */
121
- public $row;
122
-
123
- /**
124
- * @var
125
- */
126
- public $insert_id;
127
-
128
- /**
129
- * @var
130
- */
131
- public $total;
132
-
133
- /**
134
- * @var
135
- */
136
- public $total_found;
137
-
138
- /**
139
- * @var bool
140
- */
141
- public $total_found_calculated;
142
-
143
- // pagination
144
- /**
145
- * @var string
146
- */
147
- public $page_var = 'pg';
148
-
149
- /**
150
- * @var int
151
- */
152
- public $page = 1;
153
-
154
- /**
155
- * @var int
156
- */
157
- public $limit = 15;
158
-
159
- /**
160
- * @var bool
161
- */
162
- public $pagination = true;
163
-
164
- // search
165
- /**
166
- * @var bool
167
- */
168
- public $search = true;
169
-
170
- /**
171
- * @var string
172
- */
173
- public $search_var = 'search';
174
-
175
- /**
176
- * @var string
177
- */
178
- public $search_mode = 'int'; // int | text | text_like
179
- /**
180
- * @var string
181
- */
182
- public $search_query = '';
183
-
184
- /**
185
- * @var array
186
- */
187
- public $search_fields = array();
188
-
189
- /**
190
- * @var string
191
- */
192
- public $search_where = array();
193
-
194
- /**
195
- * @var array
196
- */
197
- public $filters = array();
198
-
199
- /**
200
- * Holds Traversal information about Pods
201
- *
202
- * @var array
203
- */
204
- public $traversal = array();
205
-
206
- /**
207
- * Holds custom Traversals to be included
208
- *
209
- * @var array
210
- */
211
- public $traverse = array();
212
-
213
- /**
214
- * Last select() query SQL
215
- *
216
- * @var string
217
- */
218
- public $sql = false;
219
-
220
- /**
221
- * Last total sql
222
- *
223
- * @var string
224
- */
225
- public $total_sql = false;
226
-
227
- /**
228
- * Singleton handling for a basic pods_data() request
229
- *
230
- * @param string $pod Pod name
231
- * @param integer $id Pod Item ID
232
- * @param bool $strict If true throws an error if a pod is not found.
233
- *
234
- * @return \PodsData
235
- *
236
- * @since 2.3.5
237
- */
238
- public static function init ( $pod = null, $id = 0, $strict = true ) {
239
- if ( ( true !== $pod && null !== $pod ) || 0 != $id )
240
- return new PodsData( $pod, $id, $strict );
241
- elseif ( !is_object( self::$instance ) )
242
- self::$instance = new PodsData();
243
- else {
244
- $vars = get_class_vars( __CLASS__ );
245
-
246
- foreach ( $vars as $var => $default ) {
247
- if ( 'api' == $var )
248
- continue;
249
-
250
- self::$instance->{$var} = $default;
251
- }
252
- }
253
-
254
- return self::$instance;
255
- }
256
-
257
- /**
258
- * Data Abstraction Class for Pods
259
- *
260
- * @param string $pod Pod name
261
- * @param integer $id Pod Item ID
262
- * @param bool $strict If true throws an error if a pod is not found.
263
- *
264
- * @return \PodsData
265
- *
266
- * @license http://www.gnu.org/licenses/gpl-2.0.html
267
- * @since 2.0
268
- */
269
- public function __construct ( $pod = null, $id = 0, $strict = true ) {
270
- global $wpdb;
271
-
272
- if ( is_object( $pod ) && 'PodsAPI' == get_class( $pod ) ) {
273
- $this->api = $pod;
274
- $pod = $this->api->pod;
275
- }
276
- else
277
- $this->api = pods_api( $pod );
278
-
279
- $this->api->display_errors =& self::$display_errors;
280
-
281
- if ( !empty( $pod ) ) {
282
- $this->pod_data =& $this->api->pod_data;
283
-
284
- if ( false === $this->pod_data ) {
285
- if ( true === $strict )
286
- return pods_error( 'Pod not found', $this );
287
- else
288
- return $this;
289
- }
290
-
291
- $this->pod_id = $this->pod_data[ 'id' ];
292
- $this->pod = $this->pod_data[ 'name' ];
293
- $this->fields = $this->pod_data[ 'fields' ];
294
-
295
- if ( isset( $this->pod_data[ 'options' ][ 'detail_url' ] ) )
296
- $this->detail_page = $this->pod_data[ 'options' ][ 'detail_url' ];
297
-
298
- if ( isset( $this->pod_data[ 'select' ] ) )
299
- $this->select = $this->pod_data[ 'select' ];
300
-
301
- if ( isset( $this->pod_data[ 'table' ] ) )
302
- $this->table = $this->pod_data[ 'table' ];
303
-
304
- if ( isset( $this->pod_data[ 'join' ] ) )
305
- $this->join = $this->pod_data[ 'join' ];
306
-
307
- if ( isset( $this->pod_data[ 'field_id' ] ) )
308
- $this->field_id = $this->pod_data[ 'field_id' ];
309
-
310
- if ( isset( $this->pod_data[ 'field_index' ] ) )
311
- $this->field_index = $this->pod_data[ 'field_index' ];
312
-
313
- if ( isset( $this->pod_data[ 'field_slug' ] ) )
314
- $this->field_slug = $this->pod_data[ 'field_slug' ];
315
-
316
- if ( isset( $this->pod_data[ 'where' ] ) )
317
- $this->where = $this->pod_data[ 'where' ];
318
-
319
- if ( isset( $this->pod_data[ 'where_default' ] ) )
320
- $this->where_default = $this->pod_data[ 'where_default' ];
321
-
322
- if ( isset( $this->pod_data[ 'orderby' ] ) )
323
- $this->orderby = $this->pod_data[ 'orderby' ];
324
-
325
- if ( 'settings' == $this->pod_data[ 'type' ] ) {
326
- $this->id = $this->pod_data[ 'id' ];
327
-
328
- $this->fetch( $this->id );
329
- }
330
- elseif ( null !== $id && !is_array( $id ) && !is_object( $id ) ) {
331
- $this->id = $id;
332
-
333
- $this->fetch( $this->id );
334
- }
335
- }
336
- }
337
-
338
- /**
339
- * Handle tables like they are Pods (for traversal in select/build)
340
- *
341
- * @param array|string $table
342
- * @param string $object
343
- */
344
- public function table ( $table, $object = '' ) {
345
- global $wpdb;
346
-
347
- if ( !is_array( $table ) ) {
348
- $object_type = '';
349
-
350
- if ( $wpdb->users == $table )
351
- $object_type = 'user';
352
- elseif ( $wpdb->posts == $table )
353
- $object_type = 'post_type';
354
- elseif ( $wpdb->terms == $table )
355
- $object_type = 'taxonomy';
356
- elseif ( $wpdb->options == $table )
357
- $object_type = 'settings';
358
- }
359
-
360
- if ( !empty( $object_type ) )
361
- $table = $this->api->get_table_info( $object_type, $object );
362
-
363
- if ( !empty( $table ) && is_array( $table ) ) {
364
- $table[ 'id' ] = pods_var( 'id', $table[ 'pod' ], 0, null, true );
365
- $table[ 'name' ] = pods_var( 'name', $table[ 'pod' ], $table[ 'object_type' ], null, true );
366
- $table[ 'type' ] = pods_var_raw( 'type', $table[ 'pod' ], $table[ 'object_type' ], null, true );
367
- $table[ 'storage' ] = pods_var_raw( 'storage', $table[ 'pod' ], ( 'taxonomy' == $table[ 'object_type' ] ? 'none' : 'meta' ), null, true );
368
- $table[ 'fields' ] = pods_var_raw( 'fields', $table[ 'pod' ], array() );
369
- $table[ 'object_fields' ] = pods_var_raw( 'object_fields', $table[ 'pod' ], $this->api->get_wp_object_fields( $table[ 'object_type' ] ), null, true );
370
-
371
- $this->pod_data = $table;
372
- $this->pod_id = $this->pod_data[ 'id' ];
373
- $this->pod = $this->pod_data[ 'name' ];
374
- $this->fields = $this->pod_data[ 'fields' ];
375
-
376
- if ( isset( $this->pod_data[ 'select' ] ) )
377
- $this->select = $this->pod_data[ 'select' ];
378
-
379
- if ( isset( $this->pod_data[ 'table' ] ) )
380
- $this->table = $this->pod_data[ 'table' ];
381
-
382
- if ( isset( $this->pod_data[ 'join' ] ) )
383
- $this->join = $this->pod_data[ 'join' ];
384
-
385
- if ( isset( $this->pod_data[ 'field_id' ] ) )
386
- $this->field_id = $this->pod_data[ 'field_id' ];
387
-
388
- if ( isset( $this->pod_data[ 'field_index' ] ) )
389
- $this->field_index = $this->pod_data[ 'field_index' ];
390
-
391
- if ( isset( $this->pod_data[ 'field_slug' ] ) )
392
- $this->field_slug = $this->pod_data[ 'field_slug' ];
393
-
394
- if ( isset( $this->pod_data[ 'where' ] ) )
395
- $this->where = $this->pod_data[ 'where' ];
396
-
397
- if ( isset( $this->pod_data[ 'where_default' ] ) )
398
- $this->where_default = $this->pod_data[ 'where_default' ];
399
-
400
- if ( isset( $this->pod_data[ 'orderby' ] ) )
401
- $this->orderby = $this->pod_data[ 'orderby' ];
402
- }
403
- }
404
-
405
- /**
406
- * Insert an item, eventually mapping to WPDB::insert
407
- *
408
- * @param string $table Table name
409
- * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
410
- * @param array $format (optional) An array of formats to be mapped to each of the value in $data.
411
- *
412
- * @return int|bool The ID of the item
413
- *
414
- * @uses wpdb::insert
415
- *
416
- * @since 2.0
417
- */
418
- public function insert ( $table, $data, $format = null ) {
419
- /**
420
- * @var $wpdb wpdb
421
- */
422
- global $wpdb;
423
-
424
- if ( strlen( $table ) < 1 || empty( $data ) || !is_array( $data ) )
425
- return false;
426
-
427
- if ( empty( $format ) ) {
428
- $format = array();
429
-
430
- foreach ( $data as $field ) {
431
- if ( isset( self::$field_types[ $field ] ) )
432
- $format[] = self::$field_types[ $field ];
433
- elseif ( isset( $wpdb->field_types[ $field ] ) )
434
- $format[] = $wpdb->field_types[ $field ];
435
- else
436
- break;
437
- }
438
- }
439
-
440
- list( $table, $data, $format ) = $this->do_hook( 'insert', array( $table, $data, $format ) );
441
-
442
- $result = $wpdb->insert( $table, $data, $format );
443
- $this->insert_id = $wpdb->insert_id;
444
-
445
- if ( false !== $result )
446
- return $this->insert_id;
447
-
448
- return false;
449
- }
450
-
451
- /**
452
- * @static
453
- *
454
- * Insert into a table, if unique key exists just update values.
455
- *
456
- * Data must be a key value pair array, keys act as table rows.
457
- *
458
- * Returns the prepared query from wpdb or false for errors
459
- *
460
- * @param string $table Name of the table to update
461
- * @param array $data column => value pairs
462
- * @param array $formats For $wpdb->prepare, uses sprintf formatting
463
- *
464
- * @return mixed Sanitized query string
465
- *
466
- * @uses wpdb::prepare
467
- *
468
- * @since 2.0
469
- */
470
- public static function insert_on_duplicate ( $table, $data, $formats = array() ) {
471
- /**
472
- * @var $wpdb wpdb
473
- */
474
- global $wpdb;
475
-
476
- $columns = array_keys( $data );
477
-
478
- $update = array();
479
- $values = array();
480
-
481
- foreach ( $columns as $column ) {
482
- $update[] = "`{$column}` = VALUES( `{$column}` )";
483
- $values[] = "%s";
484
- }
485
-
486
- if ( empty( $formats ) )
487
- $formats = $values;
488
-
489
- $columns_data = implode( '`, `', $columns );
490
- $formats = implode( ", ", $formats );
491
- $update = implode( ', ', $update );
492
-
493
- $sql = "INSERT INTO `{$table}` ( `{$columns_data}` ) VALUES ( {$formats} ) ON DUPLICATE KEY UPDATE {$update}";
494
-
495
- return $wpdb->prepare( $sql, $data );
496
- }
497
-
498
- /**
499
- * Update an item, eventually mapping to WPDB::update
500
- *
501
- * @param string $table Table name
502
- * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
503
- * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw".
504
- * @param array $format (optional) An array of formats to be mapped to each of the values in $data.
505
- * @param array $where_format (optional) An array of formats to be mapped to each of the values in $where.
506
- *
507
- * @return bool
508
- * @since 2.0
509
- */
510
- public function update ( $table, $data, $where, $format = null, $where_format = null ) {
511
- /**
512
- * @var $wpdb wpdb
513
- */
514
- global $wpdb;
515
-
516
- if ( strlen( $table ) < 1 || empty( $data ) || !is_array( $data ) )
517
- return false;
518
-
519
- if ( empty( $format ) ) {
520
- $format = array();
521
-
522
- foreach ( $data as $field ) {
523
- if ( isset( self::$field_types[ $field ] ) )
524
- $form = self::$field_types[ $field ];
525
- elseif ( isset( $wpdb->field_types[ $field ] ) )
526
- $form = $wpdb->field_types[ $field ];
527
- else
528
- $form = '%s';
529
-
530
- $format[] = $form;
531
- }
532
- }
533
-
534
- if ( empty( $where_format ) ) {
535
- $where_format = array();
536
-
537
- foreach ( (array) array_keys( $where ) as $field ) {
538
- if ( isset( self::$field_types[ $field ] ) )
539
- $form = self::$field_types[ $field ];
540
- elseif ( isset( $wpdb->field_types[ $field ] ) )
541
- $form = $wpdb->field_types[ $field ];
542
- else
543
- $form = '%s';
544
-
545
- $where_format[] = $form;
546
- }
547
- }
548
-
549
- list( $table, $data, $where, $format, $where_format ) = $this->do_hook( 'update', array(
550
- $table,
551
- $data,
552
- $where,
553
- $format,
554
- $where_format
555
- ) );
556
-
557
- $result = $wpdb->update( $table, $data, $where, $format, $where_format );
558
-
559
- if ( false !== $result )
560
- return true;
561
-
562
- return false;
563
- }
564
-
565
- /**
566
- * Delete an item
567
- *
568
- * @param string $table Table name
569
- * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw".
570
- * @param array $where_format (optional) An array of formats to be mapped to each of the values in $where.
571
- *
572
- * @return array|bool|mixed|null|void
573
- *
574
- * @uses PodsData::query
575
- * @uses PodsData::prepare
576
- *
577
- * @since 2.0
578
- */
579
- public function delete ( $table, $where, $where_format = null ) {
580
- /**
581
- * @var $wpdb wpdb
582
- */
583
- global $wpdb;
584
-
585
- if ( strlen( $table ) < 1 || empty( $where ) || !is_array( $where ) )
586
- return false;
587
-
588
- $wheres = array();
589
- $where_formats = $where_format = (array) $where_format;
590
-
591
- foreach ( (array) array_keys( $where ) as $field ) {
592
- if ( !empty( $where_format ) )
593
- $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[ 0 ];
594
- elseif ( isset( self::$field_types[ $field ] ) )
595
- $form = self::$field_types[ $field ];
596
- elseif ( isset( $wpdb->field_types[ $field ] ) )
597
- $form = $wpdb->field_types[ $field ];
598
- else
599
- $form = '%s';
600
-
601
- $wheres[] = "`{$field}` = {$form}";
602
- }
603
-
604
- $sql = "DELETE FROM `$table` WHERE " . implode( ' AND ', $wheres );
605
-
606
- list( $sql, $where ) = $this->do_hook( 'delete', array(
607
- $sql,
608
- array_values( $where )
609
- ), $table, $where, $where_format, $wheres );
610
-
611
- return $this->query( self::prepare( $sql, $where ) );
612
- }
613
-
614
- /**
615
- * Select items, eventually building dynamic query
616
- *
617
- * @param array $params
618
- *
619
- * @return array|bool|mixed
620
- * @since 2.0
621
- */
622
- public function select ( $params ) {
623
- global $wpdb;
624
-
625
- $cache_key = $results = false;
626
-
627
- $params = apply_filters( 'pods_data_pre_select_params', $params );
628
-
629
- // Debug purposes
630
- if ( 1 == pods_v( 'pods_debug_params', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) )
631
- pods_debug( $params );
632
-
633
- // Get from cache if enabled
634
- if ( null !== pods_v( 'expires', $params, null, false ) ) {
635
- $cache_key = md5( (string) $this->pod . serialize( $params ) );
636
-
637
- $results = pods_view_get( $cache_key, pods_v( 'cache_mode', $params, 'cache', true ), 'pods_data_select' );
638
-
639
- if ( empty( $results ) )
640
- $results = false;
641
- }
642
-
643
- if ( empty( $results ) ) {
644
- // Build
645
- $this->sql = $this->build( $params );
646
-
647
- // Debug purposes
648
- if ( ( 1 == pods_v( 'pods_debug_sql', 'get', 0 ) || 1 == pods_v( 'pods_debug_sql_all', 'get', 0 ) ) && pods_is_admin( array( 'pods' ) ) )
649
- echo '<textarea cols="100" rows="24">' . esc_textarea( str_replace( array( '@wp_users', '@wp_' ), array( $wpdb->users, $wpdb->prefix ), $this->sql ) ) . '</textarea>';
650
-
651
- if ( empty( $this->sql ) )
652
- return array();
653
-
654
- // Get Data
655
- $results = pods_query( $this->sql, $this );
656
-
657
- // Cache if enabled
658
- if ( false !== $cache_key )
659
- pods_view_set( $cache_key, $results, pods_v( 'expires', $params, 0, false ), pods_v( 'cache_mode', $params, 'cache', true ), 'pods_data_select' );
660
- }
661
-
662
- $results = apply_filters( 'pods_data_select', $results, $params, $this );
663
-
664
- $this->data = $results;
665
-
666
- $this->row_number = -1;
667
- $this->row = null;
668
-
669
- // Fill in empty field data (if none provided)
670
- if ( ( !isset( $this->fields ) || empty( $this->fields ) ) && !empty( $this->data ) ) {
671
- $this->fields = array();
672
- $data = (array) @current( $this->data );
673
-
674
- foreach ( $data as $data_key => $data_value ) {
675
- $this->fields[ $data_key ] = array( 'label' => ucwords( str_replace( '-', ' ', str_replace( '_', ' ', $data_key ) ) ) );
676
- if ( isset( $this->pod_data[ 'object_fields' ][ $data_key ] ) ) {
677
- $this->fields[ $data_key ] = $this->pod_data[ 'object_fields' ][ $data_key ];
678
- }
679
- }
680
-
681
- $this->fields = PodsForm::fields_setup( $this->fields );
682
- }
683
-
684
- $this->total_found_calculated = false;
685
-
686
- $this->total = 0;
687
-
688
- if ( ! empty( $this->data ) ) {
689
- $this->total = count( (array) $this->data );
690
- }
691
-
692
- return $this->data;
693
- }
694
-
695
- public function calculate_totals () {
696
- /**
697
- * @var $wpdb wpdb
698
- */
699
- global $wpdb;
700
-
701
- // Set totals
702
- if ( false !== $this->total_sql )
703
- $total = @current( $wpdb->get_col( $this->get_sql( $this->total_sql ) ) );
704
- else
705
- $total = @current( $wpdb->get_col( "SELECT FOUND_ROWS()" ) );
706
-
707
- $total = $this->do_hook( 'select_total', $total );
708
- $this->total_found = 0;
709
- $this->total_found_calculated = true;
710
-
711
- if ( is_numeric( $total ) )
712
- $this->total_found = $total;
713
- }
714
-
715
- /**
716
- * Build/Rewrite dynamic SQL and handle search/filter/sort
717
- *
718
- * @param array $params
719
- *
720
- * @return bool|mixed|string
721
- * @since 2.0
722
- */
723
- public function build ( $params ) {
724
- $simple_tableless_objects = PodsForm::simple_tableless_objects();
725
- $file_field_types = PodsForm::file_field_types();
726
-
727
- $defaults = array(
728
- 'select' => '*',
729
- 'calc_rows' => false,
730
- 'distinct' => true,
731
- 'table' => null,
732
- 'join' => null,
733
- 'where' => null,
734
- 'groupby' => null,
735
- 'having' => null,
736
- 'orderby' => null,
737
- 'limit' => -1,
738
- 'offset' => null,
739
-
740
- 'id' => null,
741
- 'index' => null,
742
-
743
- 'page' => 1,
744
- 'pagination' => $this->pagination,
745
- 'search' => $this->search,
746
- 'search_query' => null,
747
- 'search_mode' => null,
748
- 'search_across' => false,
749
- 'search_across_picks' => false,
750
- 'search_across_files' => false,
751
- 'filters' => array(),
752
-
753
- 'fields' => array(),
754
- 'object_fields' => array(),
755
- 'pod_table_prefix' => null,
756
-
757
- 'traverse' => array(),
758
-
759
- 'sql' => null,
760
-
761
- 'strict' => false
762
- );
763
-
764
- $params = (object) array_merge( $defaults, (array) $params );
765
-
766
- if ( 0 < strlen( $params->sql ) )
767
- return $params->sql;
768
-
769
- $pod = false;
770
-
771
- if ( is_array( $this->pod_data ) ) {
772
- $pod = $this->pod_data;
773
- }
774
-
775
- // Validate
776
- $params->page = pods_absint( $params->page );
777
-
778
- $params->pagination = (boolean) $params->pagination;
779
-
780
- if ( 0 == $params->page || !$params->pagination )
781
- $params->page = 1;
782
-
783
- $params->limit = (int) $params->limit;
784
-
785
- if ( 0 == $params->limit )
786
- $params->limit = -1;
787
-
788
- $this->limit = $params->limit;
789
-
790
- $offset = ( $params->limit * ( $params->page - 1 ) );
791
-
792
- if ( 0 < (int) $params->offset )
793
- $params->offset += $offset;
794
- else
795
- $params->offset = $offset;
796
-
797
- if ( !$params->pagination || -1 == $params->limit ) {
798
- $params->page = 1;
799
- $params->offset = 0;
800
- }
801
-
802
- if ( ( empty( $params->fields ) || !is_array( $params->fields ) ) && !empty( $pod ) && isset( $this->fields ) && !empty( $this->fields ) )
803
- $params->fields = $this->fields;
804
-
805
- if ( ( empty( $params->object_fields ) || !is_array( $params->object_fields ) ) && !empty( $pod ) && isset( $pod[ 'object_fields' ] ) && !empty( $pod[ 'object_fields' ] ) )
806
- $params->object_fields = $pod[ 'object_fields' ];
807
-
808
- if ( empty( $params->filters ) && $params->search )
809
- $params->filters = array_keys( $params->fields );
810
- elseif ( empty( $params->filters ) )
811
- $params->filters = array();
812
-
813
- if ( empty( $params->index ) )
814
- $params->index = $this->field_index;
815
-
816
- if ( empty( $params->id ) )
817
- $params->id = $this->field_id;
818
-
819
- if ( empty( $params->table ) && !empty( $pod ) && isset( $this->table ) && !empty( $this->table ) )
820
- $params->table = $this->table;
821
-
822
- if ( empty( $params->pod_table_prefix ) )
823
- $params->pod_table_prefix = 't';
824
-
825
- if ( !empty( $pod ) && !in_array( $pod[ 'type' ], array( 'pod', 'table' ) ) && 'table' == $pod[ 'storage' ] )
826
- $params->pod_table_prefix = 'd';
827
-
828
- $params->meta_fields = false;
829
-
830
- if ( !empty( $pod ) && !in_array( $pod[ 'type' ], array( 'pod', 'table', 'taxonomy' ) ) && 'meta' == $pod[ 'storage' ] )
831
- $params->meta_fields = true;
832
-
833
- if ( empty( $params->table ) )
834
- return false;
835
-
836
- if ( false === strpos( $params->table, '(' ) && false === strpos( $params->table, '`' ) )
837
- $params->table = '`' . $params->table . '`';
838
-
839
- if ( !empty( $params->join ) )
840
- $params->join = array_merge( (array) $this->join, (array) $params->join );
841
- elseif ( false === $params->strict )
842
- $params->join = $this->join;
843
-
844
- $params->where_defaulted = false;
845
- $params->where_default = $this->where_default;
846
-
847
- if ( false === $params->strict ) {
848
- // Set default where
849
- if ( !empty( $this->where_default ) && empty( $params->where ) ) {
850
- $params->where = array_values( (array) $this->where_default );
851
-
852
- $params->where_defaulted = true;
853
- }
854
-
855
- if ( !empty( $this->where ) ) {
856
- if ( is_array( $params->where ) && isset( $params->where[ 'relation' ] ) && 'OR' == strtoupper( $params->where[ 'relation' ] ) ) {
857
- $params->where = array_merge( array( $params->where ), array_values( (array) $this->where ) );
858
- }
859
- else {
860
- $params->where = array_merge( (array) $params->where, array_values( (array) $this->where ) );
861
- }
862
- }
863
- }
864
-
865
- // Allow where array ( 'field' => 'value' ) and WP_Query meta_query syntax
866
- if ( ! empty( $params->where ) ) {
867
- $params->where = $this->query_fields( (array) $params->where, $pod, $params );
868
- }
869
-
870
- if ( empty( $params->where ) ) {
871
- $params->where = array();
872
- } else {
873
- $params->where = (array) $params->where;
874
- }
875
-
876
- // Allow having array ( 'field' => 'value' ) and WP_Query meta_query syntax
877
- if ( ! empty( $params->having ) ) {
878
- $params->having = $this->query_fields( (array) $params->having, $pod, $params );
879
- }
880
-
881
- if ( empty( $params->having ) ) {
882
- $params->having = array();
883
- } else {
884
- $params->having = (array) $params->having;
885
- }
886
-
887
- if ( !empty( $params->orderby ) ) {
888
- if ( 'post_type' == $pod[ 'type' ] && 'meta' == $pod[ 'storage' ] && is_array( $params->orderby ) ) {
889
-
890
- foreach ( $params->orderby as $i => $orderby ) {
891
- if ( strpos( $orderby, '.meta_value_num' ) ) {
892
- $params->orderby[ $i ] = 'CAST(' . str_replace( '.meta_value_num', '.meta_value', $orderby ) . ' AS DECIMAL)';
893
- } elseif ( strpos( $orderby, '.meta_value_date' ) ) {
894
- $params->orderby[ $i ] = 'CAST(' . str_replace( '.meta_value_date', '.meta_value', $orderby ) . ' AS DATE)';
895
- }
896
-
897
- }
898
-
899
- }
900
-
901
- $params->orderby = (array) $params->orderby;
902
- } else {
903
- $params->orderby = array();
904
- }
905
-
906
-
907
- if ( false === $params->strict && !empty( $this->orderby ) )
908
- $params->orderby = array_merge( $params->orderby, (array) $this->orderby );
909
-
910
- if ( !empty( $params->traverse ) )
911
- $this->traverse = $params->traverse;
912
-
913
- $allowed_search_modes = array( 'int', 'text', 'text_like' );
914
-
915
- if ( !empty( $params->search_mode ) && in_array( $params->search_mode, $allowed_search_modes ) )
916
- $this->search_mode = $params->search_mode;
917
-
918
- $params->search = (boolean) $params->search;
919
-
920
- if ( 1 == pods_v( 'pods_debug_params_all', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) )
921
- pods_debug( $params );
922
-
923
- $params->field_table_alias = 't';
924
-
925
- // Get Aliases for future reference
926
- $selectsfound = '';
927
-
928
- if ( !empty( $params->select ) ) {
929
- if ( is_array( $params->select ) )
930
- $selectsfound = implode( ', ', $params->select );
931
- else
932
- $selectsfound = $params->select;
933
- }
934
-
935
- // Pull Aliases from SQL query too
936
- if ( null !== $params->sql ) {
937
- $temp_sql = ' ' . trim( str_replace( array( "\n", "\r" ), ' ', $params->sql ) );
938
- $temp_sql = preg_replace( array(
939
- '/\sSELECT\sSQL_CALC_FOUND_ROWS\s/i',
940
- '/\sSELECT\s/i'
941
- ),
942
- array(
943
- ' SELECT ',
944
- ' SELECT SQL_CALC_FOUND_ROWS '
945
- ),
946
- $temp_sql );
947
- preg_match( '/\sSELECT SQL_CALC_FOUND_ROWS\s(.*)\sFROM/i', $temp_sql, $selectmatches );
948
- if ( isset( $selectmatches[ 1 ] ) && !empty( $selectmatches[ 1 ] ) && false !== stripos( $selectmatches[ 1 ], ' AS ' ) )
949
- $selectsfound .= ( !empty( $selectsfound ) ? ', ' : '' ) . $selectmatches[ 1 ];
950
- }
951
-
952
- // Build Alias list
953
- $this->aliases = array();
954
-
955
- if ( !empty( $selectsfound ) && false !== stripos( $selectsfound, ' AS ' ) ) {
956
- $theselects = array_filter( explode( ', ', $selectsfound ) );
957
-
958
- if ( empty( $theselects ) )
959
- $theselects = array_filter( explode( ',', $selectsfound ) );
960
-
961
- foreach ( $theselects as $selected ) {
962
- $selected = trim( $selected );
963
-
964
- if ( strlen( $selected ) < 1 )
965
- continue;
966
-
967
- $selectfield = explode( ' AS ', str_replace( ' as ', ' AS ', $selected ) );
968
-
969
- if ( 2 == count( $selectfield ) ) {
970
- $field = trim( trim( $selectfield[ 1 ] ), '`' );
971
- $real_field = trim( trim( $selectfield[ 0 ] ), '`' );
972
- $this->aliases[ $field ] = $real_field;
973
- }
974
- }
975
- }
976
-
977
- // Search
978
- if ( !empty( $params->search ) && !empty( $params->fields ) ) {
979
- if ( false !== $params->search_query && 0 < strlen( $params->search_query ) ) {
980
- $where = $having = array();
981
-
982
- if ( false !== $params->search_across ) {
983
- foreach ( $params->fields as $key => $field ) {
984
- if ( is_array( $field ) ) {
985
- $attributes = $field;
986
- $field = pods_v( 'name', $field, $key, true );
987
- }
988
- else {
989
- $attributes = array(
990
- 'type' => '',
991
- 'options' => array()
992
- );
993
- }
994
-
995
- if ( isset( $attributes[ 'options' ][ 'search' ] ) && !$attributes[ 'options' ][ 'search' ] )
996
- continue;
997
-
998
- if ( in_array( $attributes[ 'type' ], array( 'date', 'time', 'datetime', 'number', 'decimal', 'currency', 'phone', 'password', 'boolean' ) ) )
999
- continue;
1000
-
1001
- $fieldfield = '`' . $field . '`';
1002
-
1003
- if ( 'pick' == $attributes[ 'type' ] && !in_array( pods_v( 'pick_object', $attributes ), $simple_tableless_objects ) ) {
1004
- if ( false === $params->search_across_picks )
1005
- continue;
1006
- else {
1007
- if ( empty( $attributes[ 'table_info' ] ) )
1008
- $attributes[ 'table_info' ] = $this->api->get_table_info( pods_v( 'pick_object', $attributes ), pods_v( 'pick_val', $attributes ) );
1009
-
1010
- if ( empty( $attributes[ 'table_info' ][ 'field_index' ] ) )
1011
- continue;
1012
-
1013
- $fieldfield = $fieldfield . '.`' . $attributes[ 'table_info' ][ 'field_index' ] . '`';
1014
- }
1015
- }
1016
- elseif ( in_array( $attributes[ 'type' ], $file_field_types ) ) {
1017
- if ( false === $params->search_across_files )
1018
- continue;
1019
- else
1020
- $fieldfield = $fieldfield . '.`post_title`';
1021
- }
1022
- elseif ( isset( $params->fields[ $field ] ) ) {
1023
- if ( $params->meta_fields )
1024
- $fieldfield = $fieldfield . '.`' . $params->pod_table_prefix . '`';
1025
- else
1026
- $fieldfield = '`' . $params->pod_table_prefix . '`.' . $fieldfield;
1027
- }
1028
- elseif ( !empty( $params->object_fields ) && !isset( $params->object_fields[ $field ] ) && 'meta' == $pod['storage'] )
1029
- $fieldfield = $fieldfield . '.`meta_value`';
1030
- else
1031
- $fieldfield = '`t`.' . $fieldfield;
1032
-
1033
- if ( isset( $this->aliases[ $field ] ) )
1034
- $fieldfield = '`' . $this->aliases[ $field ] . '`';
1035
-
1036
- if ( !empty( $attributes[ 'real_name' ] ) )
1037
- $fieldfield = $attributes[ 'real_name' ];
1038
-
1039
- if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] )
1040
- $having[] = "{$fieldfield} LIKE '%" . pods_sanitize_like( $params->search_query ) . "%'";
1041
- else
1042
- $where[] = "{$fieldfield} LIKE '%" . pods_sanitize_like( $params->search_query ) . "%'";
1043
- }
1044
- }
1045
- elseif ( !empty( $params->index ) ) {
1046
- $attributes = array();
1047
-
1048
- $fieldfield = '`t`.`' . $params->index . '`';
1049
-
1050
- if ( isset( $params->fields[ $params->index ] ) ) {
1051
- if ( $params->meta_fields )
1052
- $fieldfield = '`' . $params->index . '`.`' . $params->pod_table_prefix . '`';
1053
- else
1054
- $fieldfield = '`' . $params->pod_table_prefix . '`.`' . $params->index . '`';
1055
- }
1056
- elseif ( !empty( $params->object_fields ) && !isset( $params->object_fields[ $params->index ] ) && 'meta' == $pod['storage'] )
1057
- $fieldfield = '`' . $params->index . '`.`meta_value`';
1058
-
1059
- if ( isset( $attributes[ 'real_name' ] ) && false !== $attributes[ 'real_name' ] && !empty( $attributes[ 'real_name' ] ) )
1060
- $fieldfield = $attributes[ 'real_name' ];
1061
-
1062
- if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] )
1063
- $having[] = "{$fieldfield} LIKE '%" . pods_sanitize_like( $params->search_query ) . "%'";
1064
- else
1065
- $where[] = "{$fieldfield} LIKE '%" . pods_sanitize_like( $params->search_query ) . "%'";
1066
- }
1067
-
1068
- if ( !empty( $where ) )
1069
- $params->where[] = '( ' . implode( ' OR ', $where ) . ' )';
1070
-
1071
- if ( !empty( $having ) )
1072
- $params->having[] = '( ' . implode( ' OR ', $having ) . ' )';
1073
- }
1074
-
1075
- // Filter
1076
- foreach ( $params->filters as $filter ) {
1077
- $where = $having = array();
1078
-
1079
- if ( !isset( $params->fields[ $filter ] ) )
1080
- continue;
1081
-
1082
- $attributes = $params->fields[ $filter ];
1083
- $field = pods_v( 'name', $attributes, $filter, true );
1084
-
1085
- $filterfield = '`' . $field . '`';
1086
-
1087
- if ( 'pick' == $attributes[ 'type' ] && !in_array( pods_v( 'pick_object', $attributes ), $simple_tableless_objects ) ) {
1088
- if ( empty( $attributes[ 'table_info' ] ) )
1089
- $attributes[ 'table_info' ] = $this->api->get_table_info( pods_v( 'pick_object', $attributes ), pods_v( 'pick_val', $attributes ) );
1090
-
1091
- if ( empty( $attributes[ 'table_info' ][ 'field_index' ] ) )
1092
- continue;
1093
-
1094
- $filterfield = $filterfield . '.`' . $attributes[ 'table_info' ][ 'field_index' ] . '`';
1095
- }
1096
- elseif ( in_array( $attributes[ 'type' ], $file_field_types ) )
1097
- $filterfield = $filterfield . '.`post_title`';
1098
- elseif ( isset( $params->fields[ $field ] ) ) {
1099
- if ( $params->meta_fields && 'meta' == $pod['storage'] )
1100
- $filterfield = $filterfield . '.`meta_value`';
1101
- else
1102
- $filterfield = '`' . $params->pod_table_prefix . '`.' . $filterfield;
1103
- }
1104
- elseif ( !empty( $params->object_fields ) && !isset( $params->object_fields[ $field ] ) && 'meta' == $pod['storage'] )
1105
- $filterfield = $filterfield . '.`meta_value`';
1106
- else
1107
- $filterfield = '`t`.' . $filterfield;
1108
-
1109
- if ( isset( $this->aliases[ $field ] ) )
1110
- $filterfield = '`' . $this->aliases[ $field ] . '`';
1111
-
1112
- if ( !empty( $attributes[ 'real_name' ] ) )
1113
- $filterfield = $attributes[ 'real_name' ];
1114
-
1115
- if ( 'pick' == $attributes[ 'type' ] ) {
1116
- $filter_value = pods_v( 'filter_' . $field, 'get' );
1117
-
1118
- if ( !is_array( $filter_value ) )
1119
- $filter_value = (array) $filter_value;
1120
-
1121
- foreach ( $filter_value as $filter_v ) {
1122
- if ( in_array( pods_v( 'pick_object', $attributes ), $simple_tableless_objects ) ) {
1123
- if ( strlen( $filter_v ) < 1 )
1124
- continue;
1125
-
1126
- if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] ) {
1127
- $having[] = "( {$filterfield} = '" . pods_sanitize( $filter_v ) . "'"
1128
- . " OR {$filterfield} LIKE '%\"" . pods_sanitize_like( $filter_v ) . "\"%' )";
1129
- }
1130
- else {
1131
- $where[] = "( {$filterfield} = '" . pods_sanitize( $filter_v ) . "'"
1132
- . " OR {$filterfield} LIKE '%\"" . pods_sanitize_like( $filter_v ) . "\"%' )";
1133
- }
1134
- }
1135
- else {
1136
- $filter_v = (int) $filter_v;
1137
-
1138
- if ( empty( $filter_v ) || empty( $attributes[ 'table_info' ] ) || empty( $attributes[ 'table_info' ][ 'field_id' ] ) )
1139
- continue;
1140
-
1141
- $filterfield = '`' . $field . '`.`' . $attributes[ 'table_info' ][ 'field_id' ] . '`';
1142
-
1143
- if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] )
1144
- $having[] = "{$filterfield} = " . $filter_v;
1145
- else
1146
- $where[] = "{$filterfield} = " . $filter_v;
1147
- }
1148
- }
1149
- }
1150
- elseif ( in_array( $attributes[ 'type' ], array( 'date', 'datetime' ) ) ) {
1151
- $start_value = pods_v( 'filter_' . $field . '_start', 'get', false );
1152
- $end_value = pods_v( 'filter_' . $field . '_end', 'get', false );
1153
-
1154
- if ( empty( $start_value ) && empty( $end_value ) )
1155
- continue;
1156
-
1157
- if ( !empty( $start_value ) )
1158
- $start = date_i18n( 'Y-m-d', strtotime( $start_value ) ) . ( 'datetime' == $attributes[ 'type' ] ? ' 00:00:00' : '' );
1159
- else
1160
- $start = date_i18n( 'Y-m-d' ) . ( 'datetime' == $attributes[ 'type' ] ) ? ' 00:00:00' : '';
1161
-
1162
- if ( !empty( $end_value ) )
1163
- $end = date_i18n( 'Y-m-d', strtotime( $end_value ) ) . ( 'datetime' == $attributes[ 'type' ] ? ' 23:59:59' : '' );
1164
- else
1165
- $end = date_i18n( 'Y-m-d' ) . ( 'datetime' == $attributes[ 'type' ] ) ? ' 23:59:59' : '';
1166
-
1167
- if ( isset( $attributes[ 'date_ongoing' ] ) && true === $attributes[ 'date_ongoing' ] ) {
1168
- $date_ongoing = '`' . $attributes[ 'date_ongoing' ] . '`';
1169
-
1170
- if ( isset( $this->aliases[ $date_ongoing ] ) )
1171
- $date_ongoing = '`' . $this->aliases[ $date_ongoing ] . '`';
1172
-
1173
- if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] )
1174
- $having[] = "(({$filterfield} <= '$start' OR ({$filterfield} >= '$start' AND {$filterfield} <= '$end')) AND ({$date_ongoing} >= '$start' OR ({$date_ongoing} >= '$start' AND {$date_ongoing} <= '$end')))";
1175
- else
1176
- $where[] = "(({$filterfield} <= '$start' OR ({$filterfield} >= '$start' AND {$filterfield} <= '$end')) AND ({$date_ongoing} >= '$start' OR ({$date_ongoing} >= '$start' AND {$date_ongoing} <= '$end')))";
1177
- }
1178
- else {
1179
- if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] )
1180
- $having[] = "({$filterfield} BETWEEN '$start' AND '$end')";
1181
- else
1182
- $where[] = "({$filterfield} BETWEEN '$start' AND '$end')";
1183
- }
1184
- }
1185
- else {
1186
- $filter_value = pods_v( 'filter_' . $field, 'get', '' );
1187
-
1188
- if ( strlen( $filter_value ) < 1 )
1189
- continue;
1190
-
1191
- if ( isset( $attributes[ 'group_related' ] ) && false !== $attributes[ 'group_related' ] )
1192
- $having[] = "{$filterfield} LIKE '%" . pods_sanitize_like( $filter_value ) . "%'";
1193
- else
1194
- $where[] = "{$filterfield} LIKE '%" . pods_sanitize_like( $filter_value ) . "%'";
1195
- }
1196
-
1197
- if ( !empty( $where ) )
1198
- $params->where[] = implode( ' AND ', $where );
1199
-
1200
- if ( !empty( $having ) )
1201
- $params->having[] = implode( ' AND ', $having );
1202
- }
1203
- }
1204
-
1205
- // Traverse the Rabbit Hole
1206
- if ( !empty( $this->pod ) ) {
1207
- $haystack = implode( ' ', (array) $params->select )
1208
- . ' ' . implode( ' ', (array) $params->where )
1209
- . ' ' . implode( ' ', (array) $params->groupby )
1210
- . ' ' . implode( ' ', (array) $params->having )
1211
- . ' ' . implode( ' ', (array) $params->orderby );
1212
- $haystack = preg_replace( '/\s/', ' ', $haystack );
1213
- $haystack = preg_replace( '/\w\(/', ' ', $haystack );
1214
- $haystack = str_replace( array( '(', ')', ' ', '\\\'', "\\\"" ), ' ', $haystack );
1215
-
1216
- preg_match_all( '/`?[\w\-]+`?(?:\\.`?[\w\-]+`?)+(?=[^"\']*(?:"[^"]*"[^"]*|\'[^\']*\'[^\']*)*$)/', $haystack, $found, PREG_PATTERN_ORDER );
1217
-
1218
- $found = (array) @current( $found );
1219
- $find = $replace = $traverse = array();
1220
-
1221
- foreach ( $found as $key => $value ) {
1222
- $value = str_replace( '`', '', $value );
1223
- $value = explode( '.', $value );
1224
- $dot = $last_value = array_pop( $value );
1225
-
1226
- if ( 't' == $value[ 0 ] )
1227
- continue;
1228
- elseif ( array_key_exists( $value[ 0 ], $params->join ) )
1229
- // Don't traverse for tables we are already going to join
1230
- continue;
1231
- elseif ( 1 == count( $value ) && '' == preg_replace( '/[0-9]*/', '', $value[ 0 ] ) && '' == preg_replace( '/[0-9]*/', '', $last_value ) )
1232
- continue;
1233
-
1234
- $found_value = str_replace( '`', '', $found[ $key ] );
1235
- $found_value = '([`]{1}|\b)' . str_replace( '.', '[`]*\.[`]*', $found_value ) . '([`]{1}|\b)';
1236
- $found_value = '/' . $found_value . '(?=[^"\']*(?:"[^"]*"[^"]*|\'[^\']*\'[^\']*)*$)/';
1237
-
1238
- if ( in_array( $found_value, $find ) ) {
1239
- continue;
1240
- }
1241
-
1242
- $find[ $key ] = $found_value;
1243
-
1244
- if ( '*' != $dot )
1245
- $dot = '`' . $dot . '`';
1246
-
1247
- $replace[ $key ] = '`' . implode( '_', $value ) . '`.' . $dot;
1248
-
1249
- $value[] = $last_value;
1250
-
1251
- if ( !in_array( $value, $traverse ) )
1252
- $traverse[ $key ] = $value;
1253
- }
1254
-
1255
- if ( !empty( $this->traverse ) ) {
1256
- foreach ( (array) $this->traverse as $key => $traverse ) {
1257
- $traverse = str_replace( '`', '', $traverse );
1258
- $already_found = false;
1259
-
1260
- foreach ( $traverse as $traversal ) {
1261
- if ( is_array( $traversal ) )
1262
- $traversal = implode( '.', $traversal );
1263
-
1264
- if ( $traversal == $traverse ) {
1265
- $already_found = true;
1266
- break;
1267
- }
1268
- }
1269
-
1270
- if ( !$already_found )
1271
- $traverse[ 'traverse_' . $key ] = explode( '.', $traverse );
1272
- }
1273
- }
1274
-
1275
- $joins = array();
1276
-
1277
- if ( !empty( $find ) ) {
1278
- $params->select = preg_replace( $find, $replace, $params->select );
1279
- $params->where = preg_replace( $find, $replace, $params->where );
1280
- $params->groupby = preg_replace( $find, $replace, $params->groupby );
1281
- $params->having = preg_replace( $find, $replace, $params->having );
1282
- $params->orderby = preg_replace( $find, $replace, $params->orderby );
1283
-
1284
- if ( !empty( $traverse ) )
1285
- $joins = $this->traverse( $traverse, $params->fields, $params );
1286
- elseif ( false !== $params->search )
1287
- $joins = $this->traverse( null, $params->fields, $params );
1288
- }
1289
- }
1290
-
1291
- // Traversal Search
1292
- if ( !empty( $params->search ) && !empty( $this->search_where ) )
1293
- $params->where = array_merge( (array) $this->search_where, $params->where );
1294
-
1295
- if ( !empty( $params->join ) && !empty( $joins ) )
1296
- $params->join = array_merge( $joins, (array) $params->join );
1297
- elseif ( !empty( $joins ) )
1298
- $params->join = $joins;
1299
-
1300
- // Build
1301
- if ( null === $params->sql ) {
1302
- $sql = "
1303
- SELECT
1304
- " . ( $params->calc_rows ? 'SQL_CALC_FOUND_ROWS' : '' ) . "
1305
- " . ( $params->distinct ? 'DISTINCT' : '' ) . "
1306
- " . ( !empty( $params->select ) ? ( is_array( $params->select ) ? implode( ', ', $params->select ) : $params->select ) : '*' ) . "
1307
- FROM {$params->table} AS `t`
1308
- " . ( !empty( $params->join ) ? ( is_array( $params->join ) ? implode( "\n ", $params->join ) : $params->join ) : '' ) . "
1309
- " . ( !empty( $params->where ) ? 'WHERE ' . ( is_array( $params->where ) ? implode( ' AND ', $params->where ) : $params->where ) : '' ) . "
1310
- " . ( !empty( $params->groupby ) ? 'GROUP BY ' . ( is_array( $params->groupby ) ? implode( ', ', $params->groupby ) : $params->groupby ) : '' ) . "
1311
- " . ( !empty( $params->having ) ? 'HAVING ' . ( is_array( $params->having ) ? implode( ' AND ', $params->having ) : $params->having ) : '' ) . "
1312
- " . ( !empty( $params->orderby ) ? 'ORDER BY ' . ( is_array( $params->orderby ) ? implode( ', ', $params->orderby ) : $params->orderby ) : '' ) . "
1313
- " . ( ( 0 < $params->page && 0 < $params->limit ) ? 'LIMIT ' . $params->offset . ', ' . ( $params->limit ) : '' ) . "
1314
- ";
1315
-
1316
- if ( !$params->calc_rows ) {
1317
- // Handle COUNT() SELECT
1318
- $total_sql_select = "COUNT( " . ( $params->distinct ? 'DISTINCT `t`.`' . $params->id . '`' : '*' ) . " )";
1319
-
1320
- // If 'having' is set, we have to select all so it has access to anything it needs
1321
- if ( ! empty( $params->having ) ) {
1322
- $total_sql_select .= ', ' . ( !empty( $params->select ) ? ( is_array( $params->select ) ? implode( ', ', $params->select ) : $params->select ) : '*' );
1323
- }
1324
-
1325
- $this->total_sql = "
1326
- SELECT {$total_sql_select}
1327
- FROM {$params->table} AS `t`
1328
- " . ( !empty( $params->join ) ? ( is_array( $params->join ) ? implode( "\n ", $params->join ) : $params->join ) : '' ) . "
1329
- " . ( !empty( $params->where ) ? 'WHERE ' . ( is_array( $params->where ) ? implode( ' AND ', $params->where ) : $params->where ) : '' ) . "
1330
- " . ( !empty( $params->groupby ) ? 'GROUP BY ' . ( is_array( $params->groupby ) ? implode( ', ', $params->groupby ) : $params->groupby ) : '' ) . "
1331
- " . ( !empty( $params->having ) ? 'HAVING ' . ( is_array( $params->having ) ? implode( ' AND ', $params->having ) : $params->having ) : '' ) . "
1332
- ";
1333
- }
1334
- }
1335
- // Rewrite
1336
- else {
1337
- $sql = ' ' . trim( str_replace( array( "\n", "\r" ), ' ', $params->sql ) );
1338
- $sql = preg_replace( array(
1339
- '/\sSELECT\sSQL_CALC_FOUND_ROWS\s/i',
1340
- '/\sSELECT\s/i'
1341
- ),
1342
- array(
1343
- ' SELECT ',
1344
- ' SELECT SQL_CALC_FOUND_ROWS '
1345
- ),
1346
- $sql );
1347
-
1348
- // Insert variables based on existing statements
1349
- if ( false === stripos( $sql, '%%SELECT%%' ) )
1350
- $sql = preg_replace( '/\sSELECT\sSQL_CALC_FOUND_ROWS\s/i', ' SELECT SQL_CALC_FOUND_ROWS %%SELECT%% ', $sql );
1351
- if ( false === stripos( $sql, '%%WHERE%%' ) )
1352
- $sql = preg_replace( '/\sWHERE\s(?!.*\sWHERE\s)/i', ' WHERE %%WHERE%% ', $sql );
1353
- if ( false === stripos( $sql, '%%GROUPBY%%' ) )
1354
- $sql = preg_replace( '/\sGROUP BY\s(?!.*\sGROUP BY\s)/i', ' GROUP BY %%GROUPBY%% ', $sql );
1355
- if ( false === stripos( $sql, '%%HAVING%%' ) )
1356
- $sql = preg_replace( '/\sHAVING\s(?!.*\sHAVING\s)/i', ' HAVING %%HAVING%% ', $sql );
1357
- if ( false === stripos( $sql, '%%ORDERBY%%' ) )
1358
- $sql = preg_replace( '/\sORDER BY\s(?!.*\sORDER BY\s)/i', ' ORDER BY %%ORDERBY%% ', $sql );
1359
-
1360
- // Insert variables based on other existing statements
1361
- if ( false === stripos( $sql, '%%JOIN%%' ) ) {
1362
- if ( false !== stripos( $sql, ' WHERE ' ) )
1363
- $sql = preg_replace( '/\sWHERE\s(?!.*\sWHERE\s)/i', ' %%JOIN%% WHERE ', $sql );
1364
- elseif ( false !== stripos( $sql, ' GROUP BY ' ) )
1365
- $sql = preg_replace( '/\sGROUP BY\s(?!.*\sGROUP BY\s)/i', ' %%WHERE%% GROUP BY ', $sql );
1366
- elseif ( false !== stripos( $sql, ' ORDER BY ' ) )
1367
- $sql = preg_replace( '/\ORDER BY\s(?!.*\ORDER BY\s)/i', ' %%WHERE%% ORDER BY ', $sql );
1368
- else
1369
- $sql .= ' %%JOIN%% ';
1370
- }
1371
- if ( false === stripos( $sql, '%%WHERE%%' ) ) {
1372
- if ( false !== stripos( $sql, ' GROUP BY ' ) )
1373
- $sql = preg_replace( '/\sGROUP BY\s(?!.*\sGROUP BY\s)/i', ' %%WHERE%% GROUP BY ', $sql );
1374
- elseif ( false !== stripos( $sql, ' ORDER BY ' ) )
1375
- $sql = preg_replace( '/\ORDER BY\s(?!.*\ORDER BY\s)/i', ' %%WHERE%% ORDER BY ', $sql );
1376
- else
1377
- $sql .= ' %%WHERE%% ';
1378
- }
1379
- if ( false === stripos( $sql, '%%GROUPBY%%' ) ) {
1380
- if ( false !== stripos( $sql, ' HAVING ' ) )
1381
- $sql = preg_replace( '/\sHAVING\s(?!.*\sHAVING\s)/i', ' %%GROUPBY%% HAVING ', $sql );
1382
- elseif ( false !== stripos( $sql, ' ORDER BY ' ) )
1383
- $sql = preg_replace( '/\ORDER BY\s(?!.*\ORDER BY\s)/i', ' %%GROUPBY%% ORDER BY ', $sql );
1384
- else
1385
- $sql .= ' %%GROUPBY%% ';
1386
- }
1387
- if ( false === stripos( $sql, '%%HAVING%%' ) ) {
1388
- if ( false !== stripos( $sql, ' ORDER BY ' ) )
1389
- $sql = preg_replace( '/\ORDER BY\s(?!.*\ORDER BY\s)/i', ' %%HAVING%% ORDER BY ', $sql );
1390
- else
1391
- $sql .= ' %%HAVING%% ';
1392
- }
1393
- if ( false === stripos( $sql, '%%ORDERBY%%' ) )
1394
- $sql .= ' %%ORDERBY%% ';
1395
- if ( false === stripos( $sql, '%%LIMIT%%' ) )
1396
- $sql .= ' %%LIMIT%% ';
1397
-
1398
- // Replace variables
1399
- if ( 0 < strlen( $params->select ) ) {
1400
- if ( false === stripos( $sql, '%%SELECT%% FROM ' ) )
1401
- $sql = str_ireplace( '%%SELECT%%', $params->select . ', ', $sql );
1402
- else
1403
- $sql = str_ireplace( '%%SELECT%%', $params->select, $sql );
1404
- }
1405
- if ( 0 < strlen( $params->join ) )
1406
- $sql = str_ireplace( '%%JOIN%%', $params->join, $sql );
1407
- if ( 0 < strlen( $params->where ) ) {
1408
- if ( false !== stripos( $sql, ' WHERE ' ) ) {
1409
- if ( false !== stripos( $sql, ' WHERE %%WHERE%% ' ) )
1410
- $sql = str_ireplace( '%%WHERE%%', $params->where . ' AND ', $sql );
1411
- else
1412
- $sql = str_ireplace( '%%WHERE%%', ' AND ' . $params->where, $sql );
1413
- }
1414
- else
1415
- $sql = str_ireplace( '%%WHERE%%', ' WHERE ' . $params->where, $sql );
1416
- }
1417
- if ( 0 < strlen( $params->groupby ) ) {
1418
- if ( false !== stripos( $sql, ' GROUP BY ' ) ) {
1419
- if ( false !== stripos( $sql, ' GROUP BY %%GROUPBY%% ' ) )
1420
- $sql = str_ireplace( '%%GROUPBY%%', $params->groupby . ', ', $sql );
1421
- else
1422
- $sql = str_ireplace( '%%GROUPBY%%', ', ' . $params->groupby, $sql );
1423
- }
1424
- else
1425
- $sql = str_ireplace( '%%GROUPBY%%', ' GROUP BY ' . $params->groupby, $sql );
1426
- }
1427
- if ( 0 < strlen( $params->having ) && false !== stripos( $sql, ' GROUP BY ' ) ) {
1428
- if ( false !== stripos( $sql, ' HAVING ' ) ) {
1429
- if ( false !== stripos( $sql, ' HAVING %%HAVING%% ' ) )
1430
- $sql = str_ireplace( '%%HAVING%%', $params->having . ' AND ', $sql );
1431
- else
1432
- $sql = str_ireplace( '%%HAVING%%', ' AND ' . $params->having, $sql );
1433
- }
1434
- else
1435
- $sql = str_ireplace( '%%HAVING%%', ' HAVING ' . $params->having, $sql );
1436
- }
1437
- if ( 0 < strlen( $params->orderby ) ) {
1438
- if ( false !== stripos( $sql, ' ORDER BY ' ) ) {
1439
- if ( false !== stripos( $sql, ' ORDER BY %%ORDERBY%% ' ) )
1440
- $sql = str_ireplace( '%%ORDERBY%%', $params->groupby . ', ', $sql );
1441
- else
1442
- $sql = str_ireplace( '%%ORDERBY%%', ', ' . $params->groupby, $sql );
1443
- }
1444
- else
1445
- $sql = str_ireplace( '%%ORDERBY%%', ' ORDER BY ' . $params->groupby, $sql );
1446
- }
1447
- if ( 0 < $params->page && 0 < $params->limit ) {
1448
- $start = ( $params->page - 1 ) * $params->limit;
1449
- $end = $start + $params->limit;
1450
- $sql .= 'LIMIT ' . (int) $start . ', ' . (int) $end;
1451
- }
1452
-
1453
- // Clear any unused variables
1454
- $sql = str_ireplace( array(
1455
- '%%SELECT%%',
1456
- '%%JOIN%%',
1457
- '%%WHERE%%',
1458
- '%%GROUPBY%%',
1459
- '%%HAVING%%',
1460
- '%%ORDERBY%%',
1461
- '%%LIMIT%%'
1462
- ), '', $sql );
1463
- $sql = str_replace( array( '``', '`' ), array( ' ', ' ' ), $sql );
1464
- }
1465
-
1466
- return $sql;
1467
- }
1468
-
1469
- /**
1470
- * Fetch the total row count returned
1471
- *
1472
- * @return int Number of rows returned by select()
1473
- * @since 2.0
1474
- */
1475
- public function total () {
1476
- return (int) $this->total;
1477
- }
1478
-
1479
- /**
1480
- * Fetch the total row count total
1481
- *
1482
- * @return int Number of rows found by select()
1483
- * @since 2.0
1484
- */
1485
- public function total_found () {
1486
- if(false === $this->total_found_calculated)
1487
- $this->calculate_totals();
1488
-
1489
- return (int) $this->total_found;
1490
- }
1491
-
1492
- /**
1493
- * Fetch the zebra state
1494
- *
1495
- * @return bool Zebra state
1496
- * @since 1.12
1497
- * @see PodsData::nth
1498
- */
1499
- public function zebra () {
1500
- return $this->nth( 'odd' ); // Odd numbers
1501
- }
1502
-
1503
- /**
1504
- * Fetch the nth state
1505
- *
1506
- * @param int|string $nth The $nth to match on the PodsData::row_number
1507
- *
1508
- * @return bool Whether $nth matches
1509
- * @since 2.3
1510
- */
1511
- public function nth ( $nth ) {
1512
- if ( empty( $nth ) )
1513
- $nth = 2;
1514
-
1515
- $offset = 0;
1516
- $negative = false;
1517
-
1518
- if ( 'even' == $nth )
1519
- $nth = 2;
1520
- elseif ( 'odd' == $nth ) {
1521
- $negative = true;
1522
- $nth = 2;
1523
- }
1524
- elseif ( false !== strpos( $nth, '+' ) ) {
1525
- $nth = explode( '+', $nth );
1526
-
1527
- if ( isset( $nth[ 1 ] ) )
1528
- $offset += (int) trim( $nth[ 1 ] );
1529
-
1530
- $nth = (int) trim( $nth[ 0 ], ' n' );
1531
- }
1532
- elseif ( false !== strpos( $nth, '-' ) ) {
1533
- $nth = explode( '-', $nth );
1534
-
1535
- if ( isset( $nth[ 1 ] ) )
1536
- $offset -= (int) trim( $nth[ 1 ] );
1537
-
1538
- $nth = (int) trim( $nth[ 0 ], ' n' );
1539
- }
1540
-
1541
- $nth = (int) $nth;
1542
- $offset = (int) $offset;
1543
-
1544
- if ( 0 == ( ( $this->row_number % $nth ) + $offset ) )
1545
- return ( $negative ? false: true );
1546
-
1547
- return ( $negative ? true : false );
1548
- }
1549
-
1550
- /**
1551
- * Fetch the current position in the loop (starting at 1)
1552
- *
1553
- * @return int Current row number (+1)
1554
- * @since 2.3
1555
- */
1556
- public function position () {
1557
- return $this->row_number + 1;
1558
- }
1559
-
1560
- /**
1561
- * Create a Table
1562
- *
1563
- * @param string $table Table name
1564
- * @param string $fields
1565
- * @param boolean $if_not_exists Check if the table exists.
1566
- *
1567
- * @return array|bool|mixed|null|void
1568
- *
1569
- * @uses PodsData::query
1570
- *
1571
- * @since 2.0
1572
- */
1573
- public static function table_create ( $table, $fields, $if_not_exists = false ) {
1574
- /**
1575
- * @var $wpdb wpdb
1576
- */
1577
- global $wpdb;
1578
-
1579
- $sql = "CREATE TABLE";
1580
-
1581
- if ( true === $if_not_exists )
1582
- $sql .= " IF NOT EXISTS";
1583
-
1584
- $sql .= " `{$wpdb->prefix}" . self::$prefix . "{$table}` ({$fields})";
1585
-
1586
- if ( !empty( $wpdb->charset ) )
1587
- $sql .= " DEFAULT CHARACTER SET {$wpdb->charset}";
1588
-
1589
- if ( !empty( $wpdb->collate ) )
1590
- $sql .= " COLLATE {$wpdb->collate}";
1591
-
1592
- return self::query( $sql );
1593
- }
1594
-
1595
- /**
1596
- * Alter a Table
1597
- *
1598
- * @param string $table Table name
1599
- * @param string $changes
1600
- *
1601
- * @return array|bool|mixed|null|void
1602
- *
1603
- * @uses PodsData::query
1604
- *
1605
- * @since 2.0
1606
- */
1607
- public static function table_alter ( $table, $changes ) {
1608
- /**
1609
- * @var $wpdb wpdb
1610
- */
1611
- global $wpdb;
1612
-
1613
- $sql = "ALTER TABLE `{$wpdb->prefix}" . self::$prefix . "{$table}` {$changes}";
1614
-
1615
- return self::query( $sql );
1616
- }
1617
-
1618
- /**
1619
- * Truncate a Table
1620
- *
1621
- * @param string $table Table name
1622
- *
1623
- * @return array|bool|mixed|null|void
1624
- *
1625
- * @uses PodsData::query
1626
- *
1627
- * @since 2.0
1628
- */
1629
- public static function table_truncate ( $table ) {
1630
- /**
1631
- * @var $wpdb wpdb
1632
- */
1633
- global $wpdb;
1634
-
1635
- $sql = "TRUNCATE TABLE `{$wpdb->prefix}" . self::$prefix . "{$table}`";
1636
-
1637
- return self::query( $sql );
1638
- }
1639
-
1640
- /**
1641
- * Drop a Table
1642
- *
1643
- * @param string $table Table name
1644
- *
1645
- * @uses PodsData::query
1646
- *
1647
- * @return array|bool|mixed|null|void
1648
- *
1649
- * @uses PodsData::query
1650
- *
1651
- * @since 2.0
1652
- */
1653
- public static function table_drop ( $table ) {
1654
- /**
1655
- * @var $wpdb wpdb
1656
- */
1657
- global $wpdb;
1658
-
1659
- $sql = "DROP TABLE `{$wpdb->prefix}" . self::$prefix . "{$table}`";
1660
-
1661
- return self::query( $sql );
1662
- }
1663
-
1664
- /**
1665
- * Reorder Items
1666
- *
1667
- * @param string $table Table name
1668
- * @param string $weight_field
1669
- * @param string $id_field
1670
- * @param array $ids
1671
- *
1672
- * @return bool
1673
- *
1674
- * @uses PodsData::update
1675
- *
1676
- * @since 2.0
1677
- */
1678
- public function reorder ( $table, $weight_field, $id_field, $ids ) {
1679
- $success = false;
1680
- $ids = (array) $ids;
1681
-
1682
- list( $table, $weight_field, $id_field, $ids ) = $this->do_hook( 'reorder', array(
1683
- $table,
1684
- $weight_field,
1685
- $id_field,
1686
- $ids
1687
- ) );
1688
-
1689
- if ( !empty( $ids ) ) {
1690
- $success = true;
1691
-
1692
- foreach ( $ids as $weight => $id ) {
1693
- $updated = $this->update( $table, array( $weight_field => $weight ), array( $id_field => $id ), array( '%d' ), array( '%d' ) );
1694
-
1695
- if ( false === $updated )
1696
- $success = false;
1697
- }
1698
- }
1699
-
1700
- return $success;
1701
- }
1702
-
1703
- /**
1704
- * Fetch a new row for the current pod_data
1705
- *
1706
- * @param int $row Row number to fetch
1707
- * @param bool $explicit_set Whether to set explicitly (use false when in loop)
1708
- *
1709
- * @return mixed
1710
- *
1711
- * @since 2.0
1712
- */
1713
- public function fetch ( $row = null, $explicit_set = true ) {
1714
- global $wpdb;
1715
-
1716
- if ( null === $row )
1717
- $explicit_set = false;
1718
-
1719
- $already_cached = false;
1720
- $id = $row;
1721
-
1722
- $tableless_field_types = PodsForm::tableless_field_types();
1723
-
1724
- if ( null === $row ) {
1725
- $this->row_number++;
1726
-
1727
- $this->row = false;
1728
-
1729
- if ( isset( $this->data[ $this->row_number ] ) ) {
1730
- $this->row = get_object_vars( $this->data[ $this->row_number ] );
1731
-
1732
- $current_row_id = false;
1733
-
1734
- if ( in_array( $this->pod_data[ 'type' ], array( 'post_type', 'media' ) ) )
1735
- $current_row_id = pods_var_raw( 'ID', $this->row );
1736
- elseif ( 'taxonomy' == $this->pod_data[ 'type' ] )
1737
- $current_row_id = pods_var_raw( 'term_id', $this->row );
1738
- elseif ( 'user' == $this->pod_data[ 'type' ] )
1739
- $current_row_id = pods_var_raw( 'ID', $this->row );
1740
- elseif ( 'comment' == $this->pod_data[ 'type' ] )
1741
- $current_row_id = pods_var_raw( 'comment_ID', $this->row );
1742
- elseif ( 'settings' == $this->pod_data[ 'type' ] )
1743
- $current_row_id = $this->pod_data[ 'id' ];
1744
-
1745
- if ( 0 < $current_row_id )
1746
- $row = $current_row_id;
1747
- }
1748
- }
1749
-
1750
- if ( null !== $row || 'settings' == $this->pod_data[ 'type' ] ) {
1751
- if ( $explicit_set )
1752
- $this->row_number = -1;
1753
-
1754
- $mode = 'id';
1755
- $id = pods_absint( $row );
1756
-
1757
- if ( !is_numeric( $row ) || 0 === strpos( $row, '0' ) || $row != preg_replace( '/[^0-9]/', '', $row ) ) {
1758
- $mode = 'slug';
1759
- $id = $row;
1760
- }
1761
-
1762
- $row = false;
1763
-
1764
- if ( !empty( $this->pod ) ) {
1765
- $row = pods_cache_get( $id, 'pods_items_' . $this->pod );
1766
- if ( false !== $row ) {
1767
- $already_cached = true;
1768
- }
1769
- }
1770
-
1771
- $current_row_id = false;
1772
- $get_table_data = false;
1773
-
1774
- $old_row = $this->row;
1775
-
1776
- if ( false !== $row && is_array( $row ) )
1777
- $this->row = $row;
1778
- elseif ( in_array( $this->pod_data[ 'type' ], array( 'post_type', 'media' ) ) ) {
1779
- if ( 'post_type' == $this->pod_data[ 'type' ] ) {
1780
- if ( empty( $this->pod_data[ 'object' ] ) ) {
1781
- $post_type = $this->pod_data[ 'name' ];
1782
- }
1783
- else {
1784
- $post_type = $this->pod_data[ 'object' ];
1785
- }
1786
- }
1787
- else
1788
- $post_type = 'attachment';
1789
-
1790
- if ( 'id' == $mode ) {
1791
- $this->row = get_post( $id, ARRAY_A );
1792
-
1793
- if ( is_array( $this->row ) && $this->row[ 'post_type' ] != $post_type )
1794
- $this->row = false;
1795
- }
1796
- else {
1797
- $args = array(
1798
- 'post_type' => $post_type,
1799
- 'name' => $id,
1800
- 'numberposts' => 5
1801
- );
1802
-
1803
- $find = get_posts( $args );
1804
-
1805
- if ( !empty( $find ) )
1806
- $this->row = get_object_vars( $find[ 0 ] );
1807
- }
1808
-
1809
- if ( is_wp_error( $this->row ) || empty( $this->row ) )
1810
- $this->row = false;
1811
- else
1812
- $current_row_id = $this->row[ 'ID' ];
1813
-
1814
- $get_table_data = true;
1815
- }
1816
- elseif ( 'taxonomy' == $this->pod_data[ 'type' ] ) {
1817
- $taxonomy = $this->pod_data[ 'object' ];
1818
-
1819
- if ( empty( $taxonomy ) )
1820
- $taxonomy = $this->pod_data[ 'name' ];
1821
-
1822
- // Taxonomies are registered during init, so they aren't available before then
1823
- if ( !did_action( 'init' ) ) {
1824
- // hackaround :(
1825
- if ( 'id' == $mode )
1826
- $term_where = 't.term_id = %d';
1827
- else
1828
- $term_where = 't.slug = %s';
1829
-
1830
- $filter = 'raw';
1831
- $term = $id;
1832
-
1833
- if ( 'id' != $mode || !$_term = wp_cache_get( $term, $taxonomy ) ) {
1834
- $_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND {$term_where} LIMIT 1", $taxonomy, $term ) );
1835
-
1836
- if ( $_term )
1837
- wp_cache_add( $term, $_term, $taxonomy );
1838
- }
1839
-
1840
- $_term = apply_filters( 'get_term', $_term, $taxonomy );
1841
- $_term = apply_filters( "get_$taxonomy", $_term, $taxonomy );
1842
- $_term = sanitize_term( $_term, $taxonomy, $filter );
1843
-
1844
- if ( is_object( $_term ) )
1845
- $this->row = get_object_vars( $_term );
1846
- }
1847
- elseif ( 'id' == $mode )
1848
- $this->row = get_term( $id, $taxonomy, ARRAY_A );
1849
- else
1850
- $this->row = get_term_by( 'slug', $id, $taxonomy, ARRAY_A );
1851
-
1852
- if ( is_wp_error( $this->row ) || empty( $this->row ) )
1853
- $this->row = false;
1854
- else
1855
- $current_row_id = $this->row[ 'term_id' ];
1856
-
1857
- $get_table_data = true;
1858
- }
1859
- elseif ( 'user' == $this->pod_data[ 'type' ] ) {
1860
- if ( 'id' == $mode )
1861
- $this->row = get_userdata( $id );
1862
- else
1863
- $this->row = get_user_by( 'slug', $id );
1864
-
1865
- if ( is_wp_error( $this->row ) || empty( $this->row ) )
1866
- $this->row = false;
1867
- else {
1868
- // Get other vars
1869
- $roles = $this->row->roles;
1870
- $caps = $this->row->caps;
1871
- $allcaps = $this->row->allcaps;
1872
-
1873
- $this->row = get_object_vars( $this->row->data );
1874
-
1875
- // Set other vars
1876
- $this->row[ 'roles' ] = $roles;
1877
- $this->row[ 'caps' ] = $caps;
1878
- $this->row[ 'allcaps' ] = $allcaps;
1879
-
1880
- unset( $this->row[ 'user_pass' ] );
1881
-
1882
- $current_row_id = $this->row[ 'ID' ];
1883
- }
1884
-
1885
- $get_table_data = true;
1886
- }
1887
- elseif ( 'comment' == $this->pod_data[ 'type' ] ) {
1888
- $this->row = get_comment( $id, ARRAY_A );
1889
-
1890
- // No slug handling here
1891
-
1892
- if ( is_wp_error( $this->row ) || empty( $this->row ) )
1893
- $this->row = false;
1894
- else
1895
- $current_row_id = $this->row[ 'comment_ID' ];
1896
-
1897
- $get_table_data = true;
1898
- }
1899
- elseif ( 'settings' == $this->pod_data[ 'type' ] ) {
1900
- $this->row = array();
1901
-
1902
- if ( empty( $this->fields ) )
1903
- $this->row = false;
1904
- else {
1905
- foreach ( $this->fields as $field ) {
1906
- if ( !in_array( $field[ 'type' ], $tableless_field_types ) )
1907
- $this->row[ $field[ 'name' ] ] = get_option( $this->pod_data[ 'name' ] . '_' . $field[ 'name' ], null );
1908
- }
1909
-
1910
- // Force ID
1911
- $this->id = $this->pod_data[ 'id' ];
1912
- $this->row[ 'option_id' ] = $this->id;
1913
- }
1914
- }
1915
- else {
1916
- $params = array(
1917
- 'table' => $this->table,
1918
- 'where' => "`t`.`{$this->field_id}` = " . (int) $id,
1919
- 'orderby' => "`t`.`{$this->field_id}` DESC",
1920
- 'page' => 1,
1921
- 'limit' => 1,
1922
- 'search' => false
1923
- );
1924
-
1925
- if ( 'slug' == $mode && !empty( $this->field_slug ) ) {
1926
- $id = pods_sanitize( $id );
1927
- $params[ 'where' ] = "`t`.`{$this->field_slug}` = '{$id}'";
1928
- }
1929
-
1930
- $this->row = pods_data()->select( $params );
1931
-
1932
- if ( empty( $this->row ) )
1933
- $this->row = false;
1934
- else {
1935
- $current_row = (array) $this->row;
1936
- $this->row = get_object_vars( (object) @current( $current_row ) );
1937
- }
1938
- }
1939
-
1940
- if ( !$explicit_set && !empty( $this->row ) && is_array( $this->row ) && !empty( $old_row ) ) {
1941
- $this->row = array_merge( $old_row, $this->row );
1942
- }
1943
-
1944
- if ( 'table' == $this->pod_data[ 'storage' ] && false !== $get_table_data && is_numeric( $current_row_id ) ) {
1945
- $params = array(
1946
- 'table' => $wpdb->prefix . "pods_",
1947
- 'where' => "`t`.`id` = {$current_row_id}",
1948
- 'orderby' => "`t`.`id` DESC",
1949
- 'page' => 1,
1950
- 'limit' => 1,
1951
- 'search' => false,
1952
- 'strict' => true
1953
- );
1954
-
1955
- if ( empty( $this->pod_data[ 'object' ] ) )
1956
- $params[ 'table' ] .= $this->pod_data[ 'name' ];
1957
- else
1958
- $params[ 'table' ] .= $this->pod_data[ 'object' ];
1959
-
1960
- $row = pods_data()->select( $params );
1961
-
1962
- if ( !empty( $row ) ) {
1963
- $current_row = (array) $row;
1964
- $row = get_object_vars( (object) @current( $current_row ) );
1965
-
1966
- if ( is_array( $this->row ) && !empty( $this->row ) )
1967
- $this->row = array_merge( $this->row, $row );
1968
- else
1969
- $this->row = $row;
1970
- }
1971
- }
1972
-
1973
- if ( !empty( $this->pod ) && ! $already_cached ) {
1974
- pods_cache_set( $id, $this->row, 'pods_items_' . $this->pod, 0 );
1975
- }
1976
- }
1977
-
1978
- $this->row = apply_filters( 'pods_data_fetch', $this->row, $id, $this->row_number, $this );
1979
-
1980
- return $this->row;
1981
- }
1982
-
1983
- /**
1984
- * Reset the current data
1985
- *
1986
- * @param int $row Row number to reset to
1987
- *
1988
- * @return mixed
1989
- *
1990
- * @since 2.0
1991
- */
1992
- public function reset ( $row = null ) {
1993
- $row = pods_absint( $row );
1994
-
1995
- $this->row = false;
1996
-
1997
- if ( isset( $this->data[ $row ] ) )
1998
- $this->row = get_object_vars( $this->data[ $row ] );
1999
-
2000
- if ( empty( $row ) )
2001
- $this->row_number = -1;
2002
- else
2003
- $this->row_number = $row - 1;
2004
-
2005
- return $this->row;
2006
- }
2007
-
2008
- /**
2009
- * @static
2010
- *
2011
- * Do a query on the database
2012
- *
2013
- * @param string|array $sql The SQL to execute
2014
- * @param string $error Error to throw on problems
2015
- * @param null $results_error (optional)
2016
- * @param null $no_results_error (optional)
2017
- *
2018
- * @return array|bool|mixed|null|void Result of the query
2019
- *
2020
- * @since 2.0
2021
- */
2022
- public static function query ( $sql, $error = 'Database Error', $results_error = null, $no_results_error = null ) {
2023
- /**
2024
- * @var $wpdb wpdb
2025
- */
2026
- global $wpdb;
2027
-
2028
- if ( $wpdb->show_errors )
2029
- self::$display_errors = true;
2030
-
2031
- $display_errors = self::$display_errors;
2032
-
2033
- if ( is_object( $error ) ) {
2034
- if ( isset( $error->display_errors ) && false === $error->display_errors )
2035
- $display_errors = false;
2036
-
2037
- $error = 'Database Error';
2038
- }
2039
- elseif ( is_bool( $error ) ) {
2040
- $display_errors = $error;
2041
-
2042
- if ( false !== $error )
2043
- $error = 'Database Error';
2044
- }
2045
-
2046
- $params = (object) array(
2047
- 'sql' => $sql,
2048
- 'error' => $error,
2049
- 'results_error' => $results_error,
2050
- 'no_results_error' => $no_results_error,
2051
- 'display_errors' => $display_errors
2052
- );
2053
-
2054
- // Handle Preparations of Values (sprintf format)
2055
- if ( is_array( $sql ) ) {
2056
- if ( isset( $sql[ 0 ] ) && 1 < count( $sql ) ) {
2057
- if ( 2 == count( $sql ) ) {
2058
- if ( !is_array( $sql[ 1 ] ) )
2059
- $sql[ 1 ] = array( $sql[ 1 ] );
2060
-
2061
- $params->sql = self::prepare( $sql[ 0 ], $sql[ 1 ] );
2062
- }
2063
- elseif ( 3 == count( $sql ) )
2064
- $params->sql = self::prepare( $sql[ 0 ], array( $sql[ 1 ], $sql[ 2 ] ) );
2065
- else
2066
- $params->sql = self::prepare( $sql[ 0 ], array( $sql[ 1 ], $sql[ 2 ], $sql[ 3 ] ) );
2067
- }
2068
- else
2069
- $params = array_merge( $params, $sql );
2070
-
2071
- if ( 1 == pods_var( 'pods_debug_sql_all', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) )
2072
- echo '<textarea cols="100" rows="24">' . esc_textarea( str_replace( array( '@wp_users', '@wp_' ), array( $wpdb->users, $wpdb->prefix ), $params->sql ) ) . '</textarea>';
2073
- }
2074
-
2075
- $params->sql = trim( $params->sql );
2076
-
2077
- // Run Query
2078
- $params->sql = apply_filters( 'pods_data_query', $params->sql, $params );
2079
-
2080
- $result = $wpdb->query( $params->sql );
2081
-
2082
- $result = apply_filters( 'pods_data_query_result', $result, $params );
2083
-
2084
- if ( false === $result && !empty( $params->error ) && !empty( $wpdb->last_error ) )
2085
- return pods_error( "{$params->error}; SQL: {$params->sql}; Response: {$wpdb->last_error}", $params->display_errors );
2086
-
2087
- if ( 'INSERT' == strtoupper( substr( $params->sql, 0, 6 ) ) || 'REPLACE' == strtoupper( substr( $params->sql, 0, 7 ) ) )
2088
- $result = $wpdb->insert_id;
2089
- elseif ( preg_match( '/^[\s\r\n\(]*SELECT/', strtoupper( $params->sql ) ) ) {
2090
- $result = (array) $wpdb->last_result;
2091
-
2092
- if ( !empty( $result ) && !empty( $params->results_error ) )
2093
- return pods_error( $params->results_error, $params->display_errors );
2094
- elseif ( empty( $result ) && !empty( $params->no_results_error ) )
2095
- return pods_error( $params->no_results_error, $params->display_errors );
2096
- }
2097
-
2098
- return $result;
2099
- }
2100
-
2101
- /**
2102
- * Gets all tables in the WP database, optionally exclude WP core
2103
- * tables, and/or Pods table by settings the parameters to false.
2104
- *
2105
- * @param boolean $wp_core
2106
- * @param boolean $pods_tables restrict Pods 2.x tables
2107
- *
2108
- * @return array
2109
- *
2110
- * @since 2.0
2111
- */
2112
- public static function get_tables ( $wp_core = true, $pods_tables = true ) {
2113
- global $wpdb;
2114
-
2115
- $core_wp_tables = array(
2116
- $wpdb->options,
2117
- $wpdb->comments,
2118
- $wpdb->commentmeta,
2119
- $wpdb->posts,
2120
- $wpdb->postmeta,
2121
- $wpdb->users,
2122
- $wpdb->usermeta,
2123
- $wpdb->links,
2124
- $wpdb->terms,
2125
- $wpdb->term_taxonomy,
2126
- $wpdb->term_relationships
2127
- );
2128
-
2129
- $showTables = mysql_list_tables( DB_NAME );
2130
-
2131
- $finalTables = array();
2132
-
2133
- while ( $table = mysql_fetch_row( $showTables ) ) {
2134
- if ( !$pods_tables && 0 === ( strpos( $table[ 0 ], $wpdb->prefix . rtrim( self::$prefix, '_' ) ) ) ) // don't include pods tables
2135
- continue;
2136
- elseif ( !$wp_core && in_array( $table[ 0 ], $core_wp_tables ) )
2137
- continue;
2138
- else
2139
- $finalTables[] = $table[ 0 ];
2140
- }
2141
-
2142
- return $finalTables;
2143
- }
2144
-
2145
- /**
2146
- * Gets column information from a table
2147
- *
2148
- * @param string $table Table Name
2149
- *
2150
- * @return array
2151
- *
2152
- * @since 2.0
2153
- */
2154
- public static function get_table_columns ( $table ) {
2155
- global $wpdb;
2156
-
2157
- self::query( "SHOW COLUMNS FROM `{$table}` " );
2158
-
2159
- $table_columns = $wpdb->last_result;
2160
-
2161
- $table_cols_and_types = array();
2162
-
2163
- foreach ( $table_columns as $table_col ) {
2164
- // Get only the type, not the attributes
2165
- if ( false === strpos( $table_col->Type, '(' ) )
2166
- $modified_type = $table_col->Type;
2167
- else
2168
- $modified_type = substr( $table_col->Type, 0, ( strpos( $table_col->Type, '(' ) ) );
2169
-
2170
- $table_cols_and_types[ $table_col->Field ] = $modified_type;
2171
- }
2172
-
2173
- return $table_cols_and_types;
2174
- }
2175
-
2176
- /**
2177
- * Gets column data information from a table
2178
- *
2179
- * @param string $column_name Column name
2180
- * @param string $table Table name
2181
- *
2182
- * @return array
2183
- *
2184
- * @since 2.0
2185
- */
2186
- public static function get_column_data ( $column_name, $table ) {
2187
- $describe_data = mysql_query( 'DESCRIBE ' . $table );
2188
-
2189
- $column_data = array();
2190
-
2191
- while ( $column_row = mysql_fetch_assoc( $describe_data ) ) {
2192
- $column_data[] = $column_row;
2193
- }
2194
-
2195
- foreach ( $column_data as $single_column ) {
2196
- if ( $column_name == $single_column[ 'Field' ] )
2197
- return $single_column;
2198
- }
2199
-
2200
- return $column_data;
2201
- }
2202
-
2203
- /**
2204
- * Prepare values for the DB
2205
- *
2206
- * @param string $sql SQL to prepare
2207
- * @param array $data Data to add to the sql prepare statement
2208
- *
2209
- * @return bool|null|string
2210
- *
2211
- * @since 2.0
2212
- */
2213
- public static function prepare ( $sql, $data ) {
2214
- /**
2215
- * @var $wpdb wpdb
2216
- */
2217
- global $wpdb;
2218
- list( $sql, $data ) = apply_filters( 'pods_data_prepare', array( $sql, $data ) );
2219
- return $wpdb->prepare( $sql, $data );
2220
- }
2221
-
2222
- /**
2223
- * Get the string to use in a query for WHERE/HAVING, uses WP_Query meta_query arguments
2224
- *
2225
- * @param array $fields Array of field matches for querying
2226
- * @param array $pod Related Pod
2227
- * @param object $params Parameters passed from select()
2228
- *
2229
- * @return string|null Query string for WHERE/HAVING
2230
- *
2231
- * @static
2232
- * @since 2.3
2233
- */
2234
- public static function query_fields ( $fields, $pod = null, &$params = null ) {
2235
- $query_fields = array();
2236
-
2237
- if ( !is_object( $params ) ) {
2238
- $params = new stdClass();
2239
- }
2240
-
2241
- if ( !isset( $params->query_field_level ) || 0 == $params->query_field_level ) {
2242
- $params->query_fields = array();
2243
- $params->query_field_syntax = false;
2244
- $params->query_field_level = 1;
2245
-
2246
- if ( !isset( $params->where_default ) ) {
2247
- $params->where_default = array();
2248
- }
2249
-
2250
- if ( !isset( $params->where_defaulted ) ) {
2251
- $params->where_defaulted = false;
2252
- }
2253
- }
2254
-
2255
- $current_level = $params->query_field_level;
2256
-
2257
- $relation = 'AND';
2258
-
2259
- if ( isset( $fields[ 'relation' ] ) ) {
2260
- $relation = strtoupper( trim( pods_var( 'relation', $fields, 'AND', null, true ) ) );
2261
-
2262
- if ( 'AND' != $relation )
2263
- $relation = 'OR';
2264
-
2265
- unset( $fields[ 'relation' ] );
2266
- }
2267
-
2268
- foreach ( $fields as $field => $match ) {
2269
- if ( is_array( $match ) && isset( $match[ 'relation' ] ) ) {
2270
- $params->query_field_level = $current_level + 1;
2271
-
2272
- $query_field = self::query_fields( $match, $pod, $params );
2273
-
2274
- $params->query_field_level = $current_level;
2275
-
2276
- if ( !empty( $query_field ) ) {
2277
- $query_fields[] = $query_field;
2278
- }
2279
- }
2280
- else {
2281
- $query_field = self::query_field( $field, $match, $pod, $params );
2282
-
2283
- if ( !empty( $query_field ) ) {
2284
- $query_fields[] = $query_field;
2285
- }
2286
- }
2287
- }
2288
-
2289
- if ( !empty( $query_fields ) ) {
2290
- // If post_status not sent, detect it
2291
- if ( !empty( $pod ) && 'post_type' == $pod[ 'type' ] && 1 == $current_level && !$params->where_defaulted && !empty( $params->where_default ) ) {
2292
- $post_status_found = false;
2293
-
2294
- if ( !$params->query_field_syntax ) {
2295
- $haystack = implode( ' ', (array) $query_fields );
2296
- $haystack = preg_replace( '/\s/', ' ', $haystack );
2297
- $haystack = preg_replace( '/\w\(/', ' ', $haystack );
2298
- $haystack = str_replace( array( '(', ')', ' ', '\\\'', "\\\"" ), ' ', $haystack );
2299
-
2300
- preg_match_all( '/`?[\w\-]+`?(?:\\.`?[\w\-]+`?)+(?=[^"\']*(?:"[^"]*"[^"]*|\'[^\']*\'[^\']*)*$)/', $haystack, $found, PREG_PATTERN_ORDER );
2301
-
2302
- $found = (array) @current( $found );
2303
-
2304
- foreach ( $found as $value ) {
2305
- $value = str_replace( '`', '', $value );
2306
- $value = explode( '.', $value );
2307
-
2308
- if ( ( 'post_status' == $value[ 0 ] && 1 == count( $value ) ) || ( 2 == count( $value ) && 't' == $value[ 0 ] && 'post_status' == $value[ 1 ] ) ) {
2309
- $post_status_found = true;
2310
-
2311
- break;
2312
- }
2313
- }
2314
- }
2315
- elseif ( !empty( $params->query_fields ) && in_array( 'post_status', $params->query_fields ) ) {
2316
- $post_status_found = true;
2317
- }
2318
-
2319
- if ( !$post_status_found ) {
2320
- $query_fields[] = $params->where_default;
2321
- }
2322
- }
2323
-
2324
- if ( 1 < count( $query_fields ) )
2325
- $query_fields = '( ( ' . implode( ' ) ' . $relation . ' ( ', $query_fields ) . ' ) )';
2326
- else
2327
- $query_fields = '( ' . implode( ' ' . $relation . ' ', $query_fields ) . ' )';
2328
- }
2329
- else
2330
- $query_fields = null;
2331
-
2332
- // query_fields level complete
2333
- if ( 1 == $params->query_field_level ) {
2334
- $params->query_field_level = 0;
2335
- }
2336
-
2337
- return $query_fields;
2338
- }
2339
-
2340
- /**
2341
- * Get the string to use in a query for matching, uses WP_Query meta_query arguments
2342
- *
2343
- * @param string|int $field Field name or array index
2344
- * @param array|string $q Query array (meta_query) or string for matching
2345
- * @param array $pod Related Pod
2346
- * @param object $params Parameters passed from select()
2347
- *
2348
- * @return string|null Query field string
2349
- *
2350
- * @see PodsData::query_fields
2351
- * @static
2352
- * @since 2.3
2353
- */
2354
- public static function query_field ( $field, $q, $pod = null, &$params = null ) {
2355
- global $wpdb;
2356
-
2357
- $simple_tableless_objects = PodsForm::simple_tableless_objects();
2358
-
2359
- $field_query = null;
2360
-
2361
- // Plain queries
2362
- if ( is_numeric( $field ) && !is_array( $q ) ) {
2363
- return $q;
2364
- }
2365
- // key => value queries (backwards compatibility)
2366
- elseif ( !is_numeric( $field ) && ( !is_array( $q ) || ( !isset( $q[ 'key' ] ) && !isset( $q[ 'field' ] ) ) ) ) {
2367
- $new_q = array(
2368
- 'field' => $field,
2369
- 'compare' => pods_var_raw( 'compare', $q, '=', null, true ),
2370
- 'value' => pods_var_raw( 'value', $q, $q, null, true ),
2371
- 'sanitize' => pods_var_raw( 'sanitize', $q, true ),
2372
- 'sanitize_format' => pods_var_raw( 'sanitize_format', $q ),
2373
- 'cast' => pods_var_raw( 'cast', $q )
2374
- );
2375
-
2376
- if ( is_array( $new_q[ 'value' ] ) ) {
2377
- if ( '=' == $new_q[ 'compare' ] )
2378
- $new_q[ 'compare' ] = 'IN';
2379
-
2380
- if ( isset( $new_q[ 'value' ][ 'compare' ] ) )
2381
- unset( $new_q[ 'value' ][ 'compare' ] );
2382
- }
2383
-
2384
- $q = $new_q;
2385
- }
2386
-
2387
- $field_name = trim( pods_var_raw( 'field', $q, pods_var_raw( 'key', $q, $field, null, true ), null, true ) );
2388
- $field_type = strtoupper( trim( pods_var_raw( 'type', $q, 'CHAR', null, true ) ) );
2389
- $field_value = pods_var_raw( 'value', $q );
2390
- $field_compare = strtoupper( trim( pods_var_raw( 'compare', $q, ( is_array( $field_value ? 'IN' : '=' ) ), null, true ) ) );
2391
- $field_sanitize = (boolean) pods_var( 'sanitize', $q, true );
2392
- $field_sanitize_format = pods_var_raw( 'sanitize_format', $q, null, null, true );
2393
- $field_cast = pods_var_raw( 'cast', $q, null, null, true );
2394
-
2395
- if ( is_object( $params ) ) {
2396
- $params->meta_query_syntax = true;
2397
- $params->query_fields[] = $field_name;
2398
- }
2399
-
2400
- // Deprecated WP type
2401
- if ( 'NUMERIC' == $field_type )
2402
- $field_type = 'SIGNED';
2403
- // Restrict to supported types
2404
- elseif ( !in_array( $field_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
2405
- $field_type = 'CHAR';
2406
-
2407
- // Alias / Casting
2408
- if ( empty( $field_cast ) ) {
2409
- // Setup field casting from field name
2410
- if ( false === strpos( $field_name, '`' ) && false === strpos( $field_name, '(' ) && false === strpos( $field_name, ' ' ) ) {
2411
- // Handle field naming if Pod-based
2412
- if ( !empty( $pod ) && false === strpos( $field_name, '.' ) ) {
2413
- $field_cast = '';
2414
-
2415
- $tableless_field_types = PodsForm::tableless_field_types();
2416
-
2417
- if ( isset( $pod[ 'fields' ][ $field_name ] ) && in_array( $pod[ 'fields' ][ $field_name ][ 'type' ], $tableless_field_types ) ) {
2418
- if ( in_array( $pod[ 'fields' ][ $field_name ][ 'pick_object' ], $simple_tableless_objects ) ) {
2419
- if ( 'meta' == $pod[ 'storage' ] )
2420
- $field_cast = "`{$field_name}`.`meta_value`";
2421
- else
2422
- $field_cast = "`t`.`{$field_name}`";
2423
- }
2424
- else {
2425
- $table = pods_api()->get_table_info( $pod[ 'fields' ][ $field_name ][ 'pick_object' ], $pod[ 'fields' ][ $field_name ][ 'pick_val' ] );
2426
-
2427
- if ( !empty( $table ) )
2428
- $field_cast = "`{$field_name}`.`" . $table[ 'field_index' ] . '`';
2429
- }
2430
- }
2431
-
2432
- if ( empty( $field_cast ) ) {
2433
- if ( !in_array( $pod[ 'type' ], array( 'pod', 'table' ) ) ) {
2434
- if ( isset( $pod[ 'object_fields' ][ $field_name ] ) )
2435
- $field_cast = "`t`.`{$field_name}`";
2436
- elseif ( isset( $pod[ 'fields' ][ $field_name ] ) ) {
2437
- if ( 'meta' == $pod['storage'] )
2438
- $field_cast = "`{$field_name}`.`meta_value`";
2439
- else
2440
- $field_cast = "`d`.`{$field_name}`";
2441
- }
2442
- else {
2443
- foreach ( $pod[ 'object_fields' ] as $object_field => $object_field_opt ) {
2444
- if ( $object_field == $field_name || in_array( $field_name, $object_field_opt[ 'alias' ] ) ) {
2445
- $field_cast = "`t`.`{$object_field}`";
2446
-
2447
- break;
2448
- }
2449
- }
2450
- }
2451
- }
2452
- elseif ( isset( $pod[ 'fields' ][ $field_name ] ) ) {
2453
- if ( 'meta' == $pod['storage'] )
2454
- $field_cast = "`{$field_name}`.`meta_value`";
2455
- else
2456
- $field_cast = "`t`.`{$field_name}`";
2457
- }
2458
-
2459
- if ( empty( $field_cast ) ) {
2460
- if ( 'meta' == $pod['storage'] ) {
2461
- $field_cast = "`{$field_name}`.`meta_value`";
2462
- }
2463
- else
2464
- $field_cast = "`t`.`{$field_name}`";
2465
- }
2466
- }
2467
- }
2468
- else {
2469
- $field_cast = '`' . str_replace( '.', '`.`', $field_name ) . '`';
2470
- }
2471
- }
2472
- else {
2473
- $field_cast = $field_name;
2474
- }
2475
-
2476
- // Cast field if needed
2477
- if ( 'CHAR' != $field_type ) {
2478
- $field_cast = 'CAST( ' . $field_cast . ' AS ' . $field_type .' )';
2479
- }
2480
- }
2481
-
2482
- // Setup string sanitizing for $wpdb->prepare()
2483
- if ( empty( $field_sanitize_format ) ) {
2484
- // Sanitize as string
2485
- $field_sanitize_format = '%s';
2486
-
2487
- // Sanitize as integer if needed
2488
- if ( in_array( $field_type, array( 'UNSIGNED', 'SIGNED' ) ) )
2489
- $field_sanitize_format = '%d';
2490
- }
2491
-
2492
- // Restrict to supported comparisons
2493
- if ( !in_array( $field_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'ALL', 'BETWEEN', 'NOT BETWEEN', 'EXISTS', 'NOT EXISTS', 'REGEXP', 'NOT REGEXP', 'RLIKE' ) ) )
2494
- $field_compare = '=';
2495
-
2496
- // Restrict to supported array comparisons
2497
- if ( is_array( $field_value ) && !in_array( $field_compare, array( 'IN', 'NOT IN', 'ALL', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
2498
- if ( in_array( $field_compare, array( '!=', 'NOT LIKE' ) ) )
2499
- $field_compare = 'NOT IN';
2500
- else
2501
- $field_compare = 'IN';
2502
- }
2503
- // Restrict to supported string comparisons
2504
- elseif ( !is_array( $field_value ) && in_array( $field_compare, array( 'IN', 'NOT IN', 'ALL', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
2505
- $check_value = preg_split( '/[,\s]+/', $field_value );
2506
-
2507
- if ( 1 < count( $check_value ) )
2508
- $field_value = $check_value;
2509
- elseif ( in_array( $field_compare, array( 'NOT IN', 'NOT BETWEEN' ) ) )
2510
- $field_compare = '!=';
2511
- else
2512
- $field_compare = '=';
2513
- }
2514
-
2515
- // Restrict to two values, force = and != if only one value provided
2516
- if ( in_array( $field_compare, array( 'BETWEEN', 'NOT BETWEEN' ) ) ) {
2517
- $field_value = array_values( array_slice( $field_value, 0, 2 ) );
2518
-
2519
- if ( 1 == count( $field_value ) ) {
2520
- if ( 'NOT IN' == $field_compare )
2521
- $field_compare = '!=';
2522
- else
2523
- $field_compare = '=';
2524
- }
2525
- }
2526
-
2527
- // Single array handling
2528
- if ( 1 == count( $field_value ) && $field_compare == 'ALL' ) {
2529
- $field_compare = '=';
2530
- }
2531
- // Empty array handling
2532
- elseif ( empty( $field_value ) && in_array( $field_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
2533
- $field_compare = 'EXISTS';
2534
- }
2535
-
2536
- // Rebuild $q
2537
- $q = array(
2538
- 'field' => $field_name,
2539
- 'type' => $field_type,
2540
- 'value' => $field_value,
2541
- 'compare' => $field_compare,
2542
- 'sanitize' => $field_sanitize,
2543
- 'sanitize_format' => $field_sanitize_format,
2544
- 'cast' => $field_cast
2545
- );
2546
-
2547
- // Make the query
2548
- if ( in_array( $field_compare, array( '=', '!=', '>', '>=', '<', '<=', 'REGEXP', 'NOT REGEXP', 'RLIKE' ) ) ) {
2549
- if ( $field_sanitize ) {
2550
- $field_query = $wpdb->prepare( $field_cast . ' ' . $field_compare . ' ' . $field_sanitize_format, $field_value );
2551
- }
2552
- else {
2553
- $field_query = $field_cast . ' ' . $field_compare . ' "' . $field_value . '"';
2554
- }
2555
- }
2556
- elseif ( in_array( $field_compare, array( 'LIKE', 'NOT LIKE' ) ) ) {
2557
- if ( $field_sanitize ) {
2558
- $field_query = $field_cast . ' ' . $field_compare . ' "%' . pods_sanitize_like( $field_value ) . '%"';
2559
- }
2560
- else {
2561
- $field_query = $field_cast . ' ' . $field_compare . ' "' . $field_value . '"';
2562
- }
2563
- }
2564
- elseif ( in_array( $field_compare, array( 'IN', 'NOT IN', 'ALL' ) ) ) {
2565
- if ( $field_compare == 'ALL' ) {
2566
- $field_compare = 'IN';
2567
-
2568
- if ( ! empty( $pod ) ) {
2569
- $params->having[] = 'COUNT( DISTINCT ' . $field_cast . ' ) = ' . count( $field_value );
2570
-
2571
- if ( empty( $params->groupby ) || ( ! in_array( '`t`.`' . $pod['field_id'] . '`', $params->groupby ) && ! in_array( 't.' . $pod['field_id'] . '', $params->groupby ) ) ) {
2572
- $params->groupby[] = '`t`.`' . $pod['field_id'] . '`';
2573
- }
2574
- }
2575
- }
2576
-
2577
- if ( $field_sanitize ) {
2578
- $field_query = $wpdb->prepare( $field_cast . ' ' . $field_compare . ' ( ' . substr( str_repeat( ', ' . $field_sanitize_format, count( $field_value ) ), 1 ) . ' )', $field_value );
2579
- }
2580
- else {
2581
- $field_query = $field_cast . ' ' . $field_compare . ' ( "' . implode( '", "', $field_value ) . '" )';
2582
- }
2583
- }
2584
- elseif ( in_array( $field_compare, array( 'BETWEEN', 'NOT BETWEEN' ) ) ) {
2585
- if ( $field_sanitize ) {
2586
- $field_query = $wpdb->prepare( $field_cast . ' ' . $field_compare . ' ' . $field_sanitize_format . ' AND ' . $field_sanitize_format, $field_value );
2587
- }
2588
- else {
2589
- $field_query = $field_cast . ' ' . $field_compare . ' "' . $field_value[ 0 ] . '" AND "' . $field_value[ 1 ] . '"';
2590
- }
2591
- }
2592
- elseif ( 'EXISTS' == $field_compare )
2593
- $field_query = $field_cast . ' IS NOT NULL';
2594
- elseif ( 'NOT EXISTS' == $field_compare )
2595
- $field_query = $field_cast . ' IS NULL';
2596
-
2597
- $field_query = apply_filters( 'pods_data_field_query', $field_query, $q );
2598
-
2599
- return $field_query;
2600
- }
2601
-
2602
- /**
2603
- * Setup fields for traversal
2604
- *
2605
- * @param array $fields Associative array of fields data
2606
- *
2607
- * @return array Traverse feed
2608
- * @param object $params (optional) Parameters from build()
2609
- *
2610
- * @since 2.0
2611
- */
2612
- function traverse_build ( $fields = null, $params = null ) {
2613
- if ( null === $fields )
2614
- $fields = $this->fields;
2615
-
2616
- $feed = array();
2617
-
2618
- foreach ( $fields as $field => $data ) {
2619
- if ( !is_array( $data ) )
2620
- $field = $data;
2621
-
2622
- if ( !isset( $_GET[ 'filter_' . $field ] ) )
2623
- continue;
2624
-
2625
- $field_value = pods_var( 'filter_' . $field, 'get', false, null, true );
2626
-
2627
- if ( !empty( $field_value ) || 0 < strlen( $field_value ) )
2628
- $feed[ 'traverse_' . $field ] = array( $field );
2629
- }
2630
-
2631
- return $feed;
2632
- }
2633
-
2634
- /**
2635
- * Recursively join tables based on fields
2636
- *
2637
- * @param array $traverse_recurse Array of traversal options
2638
- *
2639
- * @return array Array of table joins
2640
- *
2641
- * @since 2.0
2642
- */
2643
- function traverse_recurse ( $traverse_recurse ) {
2644
- global $wpdb;
2645
-
2646
- $defaults = array(
2647
- 'pod' => null,
2648
- 'fields' => array(),
2649
- 'joined' => 't',
2650
- 'depth' => 0,
2651
- 'joined_id' => 'id',
2652
- 'joined_index' => 'id',
2653
- 'params' => new stdClass(),
2654
- 'last_table_info' => array()
2655
- );
2656
-
2657
- $traverse_recurse = array_merge( $defaults, $traverse_recurse );
2658
-
2659
- $joins = array();
2660
-
2661
- if ( 0 == $traverse_recurse[ 'depth' ] && !empty( $traverse_recurse[ 'pod' ] ) && !empty( $traverse_recurse [ 'last_table_info' ] ) && isset( $traverse_recurse [ 'last_table_info' ][ 'id' ] ) )
2662
- $pod_data = $traverse_recurse [ 'last_table_info' ];
2663
- elseif ( empty( $traverse_recurse[ 'pod' ] ) ) {
2664
- if ( !empty( $traverse_recurse[ 'params' ] ) && !empty( $traverse_recurse[ 'params' ]->table ) && 0 === strpos( $traverse_recurse[ 'params' ]->table, $wpdb->prefix ) ) {
2665
- if ( $wpdb->posts == $traverse_recurse[ 'params' ]->table )
2666
- $traverse_recurse[ 'pod' ] = 'post_type';
2667
- elseif ( $wpdb->terms == $traverse_recurse[ 'params' ]->table )
2668
- $traverse_recurse[ 'pod' ] = 'taxonomy';
2669
- elseif ( $wpdb->users == $traverse_recurse[ 'params' ]->table )
2670
- $traverse_recurse[ 'pod' ] = 'user';
2671
- elseif ( $wpdb->comments == $traverse_recurse[ 'params' ]->table )
2672
- $traverse_recurse[ 'pod' ] = 'comment';
2673
- else
2674
- return $joins;
2675
-
2676
- $pod_data = array();
2677
-
2678
- if ( in_array( $traverse_recurse[ 'pod' ], array( 'user', 'comment' ) ) ) {
2679
- $pod = $this->api->load_pod( array( 'name' => $traverse_recurse[ 'pod' ], 'table_info' => true ) );
2680
-
2681
- if ( !empty( $pod ) && $pod[ 'type' ] == $pod )
2682
- $pod_data = $pod;
2683
- }
2684
-
2685
- if ( empty( $pod_data ) ) {
2686
- $pod_data = array(
2687
- 'id' => 0,
2688
- 'name' => '_table_' . $traverse_recurse[ 'pod' ],
2689
- 'type' => $traverse_recurse[ 'pod' ],
2690
- 'storage' => ( 'taxonomy' == $traverse_recurse[ 'pod' ] ? 'none' : 'meta' ),
2691
- 'fields' => array(),
2692
- 'object_fields' => $this->api->get_wp_object_fields( $traverse_recurse[ 'pod' ] )
2693
- );
2694
-
2695
- $pod_data = array_merge( $this->api->get_table_info( $traverse_recurse[ 'pod' ], '' ), $pod_data );
2696
- }
2697
-
2698
- $traverse_recurse[ 'pod' ] = $pod_data[ 'name' ];
2699
- }
2700
- else
2701
- return $joins;
2702
- }
2703
- else {
2704
- $pod_data = $this->api->load_pod( array( 'name' => $traverse_recurse[ 'pod' ], 'table_info' => true ), false );
2705
-
2706
- if ( empty( $pod_data ) )
2707
- return $joins;
2708
- }
2709
-
2710
- if ( isset( $pod_data[ 'object_fields' ] ) ) {
2711
- $pod_data[ 'fields' ] = array_merge( $pod_data[ 'fields' ], $pod_data[ 'object_fields' ] );
2712
- }
2713
-
2714
- $tableless_field_types = PodsForm::tableless_field_types();
2715
- $simple_tableless_objects = PodsForm::simple_tableless_objects();
2716
- $file_field_types = PodsForm::file_field_types();
2717
-
2718
- if ( !isset( $this->traversal[ $traverse_recurse[ 'pod' ] ] ) )
2719
- $this->traversal[ $traverse_recurse[ 'pod' ] ] = array();
2720
-
2721
- if ( ( empty( $pod_data[ 'meta_table' ] ) || $pod_data[ 'meta_table' ] == $pod_data[ 'table' ] ) && ( empty( $traverse_recurse[ 'fields' ] ) || empty( $traverse_recurse[ 'fields' ][ $traverse_recurse[ 'depth' ] ] ) ) )
2722
- return $joins;
2723
-
2724
- $field = $traverse_recurse[ 'fields' ][ $traverse_recurse[ 'depth' ] ];
2725
-
2726
- $ignore_aliases = array(
2727
- 'wpml_languages',
2728
- 'polylang_languages'
2729
- );
2730
-
2731
- $ignore_aliases = apply_filters( 'pods_data_traverse_recurse_ignore_aliases', $ignore_aliases, $field, $traverse_recurse, $this );
2732
-
2733
- if ( in_array( $field, $ignore_aliases ) )
2734
- return $joins;
2735
-
2736
- $meta_data_table = false;
2737
-
2738
- if ( !isset( $pod_data[ 'fields' ][ $field ] ) && 'd' == $field && isset( $traverse_recurse[ 'fields' ][ $traverse_recurse[ 'depth' ] - 1 ] ) ) {
2739
- $field = $traverse_recurse[ 'fields' ][ $traverse_recurse[ 'depth' ] - 1 ];
2740
-
2741
- $field_type = 'pick';
2742
-
2743
- if ( isset( $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'fields' ][ $field ] ) ) {
2744
- $field_type = $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'fields' ][ $field ][ 'type' ];
2745
- }
2746
- elseif ( isset( $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'object_fields' ][ $field ] ) ) {
2747
- $field_type = $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'object_fields' ][ $field ][ 'type' ];
2748
- }
2749
-
2750
- $pod_data[ 'fields' ][ $field ] = array(
2751
- 'id' => 0,
2752
- 'name' => $field,
2753
- 'type' => $field_type,
2754
- 'pick_object' => $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'type' ],
2755
- 'pick_val' => $traverse_recurse[ 'last_table_info' ][ 'pod' ][ 'name' ]
2756
- );
2757
-
2758
- $meta_data_table = true;
2759
- }
2760
-
2761
- // Fallback to meta table if the pod type supports it
2762
- if ( !isset( $pod_data[ 'fields' ][ $field ] ) ) {
2763
- $last = end( $traverse_recurse[ 'fields' ] );
2764
-
2765
- if ( 'post_type' == $pod_data[ 'type' ] && !isset( $pod_data[ 'object_fields' ] ) )
2766
- $pod_data[ 'object_fields' ] = $this->api->get_wp_object_fields( 'post_type', $pod_data );
2767
-
2768
- if ( 'post_type' == $pod_data[ 'type' ] && isset( $pod_data[ 'object_fields'][ $field ] ) && in_array( $pod_data[ 'object_fields' ][ $field ][ 'type' ], $tableless_field_types ) )
2769
- $pod_data[ 'fields' ][ $field ] = $pod_data[ 'object_fields' ][ $field ];
2770
- elseif ( 'meta_value' === $last && in_array( $pod_data[ 'type' ], array( 'post_type', 'media', 'user', 'comment' ) ) )
2771
- $pod_data[ 'fields' ][ $field ] = PodsForm::field_setup( array( 'name' => $field ) );
2772
- else {
2773
- if ( 'post_type' == $pod_data[ 'type' ] ) {
2774
- $pod_data[ 'object_fields' ] = $this->api->get_wp_object_fields( 'post_type', $pod_data, true );
2775
-
2776
- if ( 'post_type' == $pod_data[ 'type' ] && isset( $pod_data[ 'object_fields' ][ $field ] ) && in_array( $pod_data[ 'object_fields' ][ $field ][ 'type' ], $tableless_field_types ) )
2777
- $pod_data[ 'fields' ][ $field ] = $pod_data[ 'object_fields' ][ $field ];
2778
- else
2779
- return $joins;
2780
- }
2781
- else
2782
- return $joins;
2783
- }
2784
- } elseif ( isset( $pod_data[ 'object_fields' ] ) && isset( $pod_data[ 'object_fields' ][ $field ] ) && ! in_array( $pod_data[ 'object_fields' ][ $field ][ 'type' ], $tableless_field_types ) ) {
2785
- return $joins;
2786
- }
2787
-
2788
- $traverse = $pod_data[ 'fields' ][ $field ];
2789
-
2790
- if ( 'taxonomy' == $traverse[ 'type' ] )
2791
- $traverse[ 'table_info' ] = $this->api->get_table_info( $traverse[ 'type' ], $traverse[ 'name' ] );
2792
- elseif ( in_array( $traverse[ 'type' ], $file_field_types ) )
2793
- $traverse[ 'table_info' ] = $this->api->get_table_info( 'post_type', 'attachment' );
2794
- elseif ( !in_array( $traverse[ 'type' ], $tableless_field_types ) )
2795
- $traverse[ 'table_info' ] = $this->api->get_table_info( $pod_data[ 'type' ], $pod_data[ 'name' ], $pod_data[ 'name' ], $pod_data );
2796
- elseif ( empty( $traverse[ 'table_info' ] ) || ( in_array( $traverse[ 'pick_object' ], $simple_tableless_objects ) && !empty( $traverse_recurse[ 'last_table_info' ] ) ) ) {
2797
- if ( in_array( $traverse[ 'pick_object' ], $simple_tableless_objects ) && !empty( $traverse_recurse[ 'last_table_info' ] ) ) {
2798
- $traverse[ 'table_info' ] = $traverse_recurse[ 'last_table_info' ];
2799
-
2800
- if ( !empty( $traverse[ 'table_info' ][ 'meta_table' ] ) )
2801
- $meta_data_table = true;
2802
- }
2803
- elseif ( !in_array( $traverse[ 'type' ], $tableless_field_types ) && !empty( $traverse_recurse[ 'last_table_info' ] ) && 0 == $traverse_recurse[ 'depth' ] )
2804
- $traverse[ 'table_info' ] = $traverse_recurse[ 'last_table_info' ];
2805
- else {
2806
- if ( ! isset( $traverse[ 'pod' ] ) ) {
2807
- $traverse[ 'pod' ] = null;
2808
- }
2809
-
2810
- $traverse[ 'table_info' ] = $this->api->get_table_info( $traverse[ 'pick_object' ], $traverse[ 'pick_val' ], null, $traverse[ 'pod' ], $traverse );
2811
- }
2812
- }
2813
-
2814
- if ( isset( $this->traversal[ $traverse_recurse[ 'pod' ] ][ $traverse[ 'name' ] ] ) )
2815
- $traverse = array_merge( $traverse, (array) $this->traversal[ $traverse_recurse[ 'pod' ] ][ $traverse[ 'name' ] ] );
2816
-
2817
- $traverse = apply_filters( 'pods_data_traverse', $traverse, compact( 'pod', 'fields', 'joined', 'depth', 'joined_id', 'params' ), $this );
2818
-
2819
- if ( empty( $traverse ) )
2820
- return $joins;
2821
-
2822
- $traverse[ 'id' ] = (int) $traverse[ 'id' ];
2823
-
2824
- if ( empty( $traverse[ 'id' ] ) )
2825
- $traverse[ 'id' ] = $field;
2826
-
2827
- $table_info = $traverse[ 'table_info' ];
2828
-
2829
- $this->traversal[ $traverse_recurse[ 'pod' ] ][ $field ] = $traverse;
2830
-
2831
- $field_joined = $field;
2832
-
2833
- if ( 0 < $traverse_recurse[ 'depth' ] && 't' != $traverse_recurse[ 'joined' ] ) {
2834
- if ( $meta_data_table && ( 'pick' != $traverse[ 'type' ] || !in_array( pods_var( 'pick_object', $traverse ), $simple_tableless_objects ) ) )
2835
- $field_joined = $traverse_recurse[ 'joined' ] . '_d';
2836
- else
2837
- $field_joined = $traverse_recurse[ 'joined' ] . '_' . $field;
2838
- }
2839
-
2840
- $rel_alias = 'rel_' . $field_joined;
2841
-
2842
- if ( pods_v( 'search', $traverse_recurse[ 'params' ], false ) && empty( $traverse_recurse[ 'params' ]->filters ) ) {
2843
- if ( 0 < strlen( pods_var( 'filter_' . $field_joined, 'get' ) ) ) {
2844
- $val = absint( pods_var( 'filter_' . $field_joined, 'get' ) );
2845
-
2846
- $search = "`{$field_joined}`.`{$table_info[ 'field_id' ]}` = {$val}";
2847
-
2848
- if ( 'text' == $this->search_mode ) {
2849
- $val = pods_var( 'filter_' . $field_joined, 'get' );
2850
-
2851
- $search = "`{$field_joined}`.`{$traverse[ 'name' ]}` = '{$val}'";
2852
- }
2853
- elseif ( 'text_like' == $this->search_mode ) {
2854
- $val = pods_sanitize( pods_sanitize_like( pods_var_raw( 'filter_' . $field_joined ) ) );
2855
-
2856
- $search = "`{$field_joined}`.`{$traverse[ 'name' ]}` LIKE '%{$val}%'";
2857
- }
2858
-
2859
- $this->search_where[] = " {$search} ";
2860
- }
2861
- }
2862
-
2863
- $the_join = null;
2864
-
2865
- $joined_id = $table_info[ 'field_id' ];
2866
- $joined_index = $table_info[ 'field_index' ];
2867
-
2868
- if ( 'taxonomy' == $traverse[ 'type' ] ) {
2869
- $rel_tt_alias = 'rel_tt_' . $field_joined;
2870
-
2871
- if ( $meta_data_table ) {
2872
- $the_join = "
2873
- LEFT JOIN `{$table_info[ 'pod_table' ]}` AS `{$field_joined}` ON
2874
- `{$field_joined}`.`{$table_info[ 'pod_field_id' ]}` = `{$traverse_recurse[ 'rel_alias' ]}`.`{$traverse_recurse[ 'joined_id' ]}`
2875
- ";
2876
- }
2877
- else {
2878
- $the_join = "
2879
- LEFT JOIN `{$wpdb->term_relationships}` AS `{$rel_alias}` ON
2880
- `{$rel_alias}`.`object_id` = `{$traverse_recurse[ 'joined' ]}`.`ID`
2881
-
2882
- LEFT JOIN `{$wpdb->term_taxonomy}` AS `{$rel_tt_alias}` ON
2883
- `{$rel_tt_alias}`.`taxonomy` = '{$traverse[ 'name' ]}'
2884
- AND `{$rel_tt_alias}`.`term_taxonomy_id` = `{$rel_alias}`.`term_taxonomy_id`
2885
-
2886
- LEFT JOIN `{$table_info[ 'table' ]}` AS `{$field_joined}` ON
2887
- `{$field_joined}`.`{$table_info[ 'field_id' ]}` = `{$rel_tt_alias}`.`{$table_info[ 'field_id' ]}`
2888
- ";
2889
-
2890
- // Override $rel_alias
2891
- $rel_alias = $field_joined;
2892
-
2893
- $joined_id = $table_info[ 'field_id' ];
2894
- $joined_index = $table_info[ 'field_index' ];
2895
- }
2896
- }
2897
- elseif ( in_array( $traverse[ 'type' ], $tableless_field_types ) && ( 'pick' != $traverse[ 'type' ] || !in_array( pods_v( 'pick_object', $traverse ), $simple_tableless_objects ) ) ) {
2898
- if ( pods_tableless() ) {
2899
- $the_join = "
2900
- LEFT JOIN `{$table_info[ 'meta_table' ]}` AS `{$rel_alias}` ON
2901
- `{$rel_alias}`.`{$table_info[ 'meta_field_index' ]}` = '{$traverse[ 'name' ]}'
2902
- AND `{$rel_alias}`.`{$table_info[ 'meta_field_id' ]}` = `{$traverse_recurse[ 'joined' ]}`.`{$traverse_recurse[ 'joined_id' ]}`
2903
-
2904
- LEFT JOIN `{$table_info[ 'meta_table' ]}` AS `{$field_joined}` ON
2905
- `{$field_joined}`.`{$table_info[ 'meta_field_index' ]}` = '{$traverse[ 'name' ]}'
2906
- AND `{$field_joined}`.`{$table_info[ 'meta_field_id' ]}` = CONVERT( `{$rel_alias}`.`{$table_info[ 'meta_field_value' ]}`, SIGNED )
2907
- ";
2908
-
2909
- $joined_id = $table_info[ 'meta_field_id' ];
2910
- $joined_index = $table_info[ 'meta_field_index' ];
2911
- }
2912
- elseif ( $meta_data_table ) {
2913
- if ( $traverse[ 'id' ] !== $traverse[ 'pick_val' ] ) {
2914
- // This must be a relationship
2915
- $joined_id = 'related_item_id';
2916
- } else {
2917
- $joined_id = $traverse_recurse[ 'joined_id' ];
2918
- }
2919
-
2920
- $the_join = "
2921
- LEFT JOIN `{$table_info['pod_table']}` AS `{$field_joined}` ON
2922
- `{$field_joined}`.`{$table_info['pod_field_id']}` = `{$traverse_recurse['rel_alias']}`.`{$joined_id}`
2923
- ";
2924
- }
2925
- else {
2926
- $the_join = "
2927
- LEFT JOIN `@wp_podsrel` AS `{$rel_alias}` ON
2928
- `{$rel_alias}`.`field_id` = {$traverse[ 'id' ]}
2929
- AND `{$rel_alias}`.`item_id` = `{$traverse_recurse[ 'joined' ]}`.`{$traverse_recurse[ 'joined_id' ]}`
2930
-
2931
- LEFT JOIN `{$table_info[ 'table' ]}` AS `{$field_joined}` ON
2932
- `{$field_joined}`.`{$table_info[ 'field_id' ]}` = `{$rel_alias}`.`related_item_id`
2933
- ";
2934
- }
2935
- }
2936
- elseif ( 'meta' == $pod_data[ 'storage' ] ) {
2937
- if (
2938
- ( $traverse_recurse[ 'depth' ] + 2 ) == count( $traverse_recurse[ 'fields' ] )
2939
- && ( 'pick' != $traverse[ 'type' ] || !in_array( pods_var( 'pick_object', $traverse ), $simple_tableless_objects ) )
2940
- && $table_info[ 'meta_field_value' ] == $traverse_recurse[ 'fields' ][ $traverse_recurse[ 'depth' ] + 1 ] ) {
2941
- $the_join = "
2942
- LEFT JOIN `{$table_info[ 'meta_table' ]}` AS `{$field_joined}` ON
2943
- `{$field_joined}`.`{$table_info[ 'meta_field_index' ]}` = '{$traverse[ 'name' ]}'
2944
- AND `{$field_joined}`.`{$table_info[ 'meta_field_id' ]}` = `{$traverse_recurse[ 'joined' ]}`.`{$traverse_recurse[ 'joined_id' ]}`
2945
- ";
2946
-
2947
- $table_info[ 'recurse' ] = false;
2948
- }
2949
- else {
2950
- $the_join = "
2951
- LEFT JOIN `{$table_info[ 'meta_table' ]}` AS `{$field_joined}` ON
2952
- `{$field_joined}`.`{$table_info[ 'meta_field_index' ]}` = '{$traverse[ 'name' ]}'
2953
- AND `{$field_joined}`.`{$table_info[ 'meta_field_id' ]}` = `{$traverse_recurse[ 'joined' ]}`.`{$traverse_recurse[ 'joined_id' ]}`
2954
- ";
2955
-
2956
- $joined_id = $table_info[ 'meta_field_id' ];
2957
- $joined_index = $table_info[ 'meta_field_index' ];
2958
- }
2959
- }
2960
-
2961
- $traverse_recursive = array(
2962
- 'pod' => pods_var_raw( 'name', pods_var_raw( 'pod', $table_info ) ),
2963
- 'fields' => $traverse_recurse[ 'fields' ],
2964
- 'joined' => $field_joined,
2965
- 'depth' => ( $traverse_recurse[ 'depth' ] + 1 ),
2966
- 'joined_id' => $joined_id,
2967
- 'joined_index' => $joined_index,
2968
- 'params' => $traverse_recurse[ 'params' ],
2969
- 'rel_alias' => $rel_alias,
2970
- 'last_table_info' => $table_info
2971
- );
2972
-
2973
- $the_join = apply_filters( 'pods_data_traverse_the_join', $the_join, $traverse_recurse, $traverse_recursive, $this );
2974
-
2975
- if ( empty( $the_join ) )
2976
- return $joins;
2977
-
2978
- $joins[ $traverse_recurse[ 'pod' ] . '_' . $traverse_recurse[ 'depth' ] . '_' . $traverse[ 'id' ] ] = $the_join;
2979
-
2980
- if ( ( $traverse_recurse[ 'depth' ] + 1 ) < count( $traverse_recurse[ 'fields' ] ) && !empty( $traverse_recurse[ 'pod' ] ) && false !== $table_info[ 'recurse' ] )
2981
- $joins = array_merge( $joins, $this->traverse_recurse( $traverse_recursive ) );
2982
-
2983
- return $joins;
2984
- }
2985
-
2986
- /**
2987
- * Recursively join tables based on fields
2988
- *
2989
- * @param array $fields Fields to recurse
2990
- * @param null $all_fields (optional) If $fields is empty then traverse all fields, argument does not need to be passed
2991
- * @param object $params (optional) Parameters from build()
2992
- *
2993
- * @return array Array of joins
2994
- */
2995
- function traverse ( $fields = null, $all_fields = null, $params = null ) {
2996
- $joins = array();
2997
-
2998
- if ( null === $fields )
2999
- $fields = $this->traverse_build( $all_fields, $params );
3000
-
3001
- foreach ( (array) $fields as $field_group ) {
3002
- $traverse_recurse = array(
3003
- 'pod' => $this->pod,
3004
- 'fields' => $fields,
3005
- 'params' => $params,
3006
- 'last_table_info' => $this->pod_data,
3007
- 'joined_id' => $this->pod_data[ 'field_id' ],
3008
- 'joined_index' => $this->pod_data[ 'field_index' ]
3009
- );
3010
-
3011
- if ( is_array( $field_group ) ) {
3012
- $traverse_recurse[ 'fields' ] = $field_group;
3013
-
3014
- $joins = array_merge( $joins, $this->traverse_recurse( $traverse_recurse ) );
3015
- }
3016
- else {
3017
- $joins = array_merge( $joins, $this->traverse_recurse( $traverse_recurse ) );
3018
- $joins = array_filter( $joins );
3019
-
3020
- return $joins;
3021
- }
3022
- }
3023
-
3024
- $joins = array_filter( $joins );
3025
-
3026
- return $joins;
3027
- }
3028
-
3029
- /**
3030
- * Handle filters / actions for the class
3031
- *
3032
- * @since 2.0
3033
- */
3034
- private static function do_hook () {
3035
- $args = func_get_args();
3036
-
3037
- if ( empty( $args ) )
3038
- return false;
3039
-
3040
- $name = array_shift( $args );
3041
-
3042
- return pods_do_hook( 'data', $name, $args );
3043
- }
3044
-
3045
- /**
3046
- * Get the complete sql
3047
- *
3048
- * @since 2.0.5
3049
- */
3050
- public function get_sql ( $sql ) {
3051
- global $wpdb;
3052
-
3053
- if ( empty( $sql ) )
3054
- $sql = $this->sql;
3055
-
3056
- $sql = str_replace( array( '@wp_users', '@wp_' ), array( $wpdb->users, $wpdb->prefix ), $sql );
3057
-
3058
- $sql = str_replace( '{prefix}', '@wp_', $sql );
3059
- $sql = str_replace( '{/prefix/}', '{prefix}', $sql );
3060
-
3061
- return $sql;
3062
- }
3063
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
classes/PodsField.php DELETED
@@ -1,384 +0,0 @@
1
- <?php
2
- /**
3
- * @package Pods
4
- */
5
- class PodsField {
6
-
7
- /**
8
- * Whether this field is running under 1.x deprecated forms
9
- *
10
- * @var bool
11
- * @since 2.0
12
- */
13
- public static $deprecated = false;
14
-
15
- /**
16
- * Field Type Identifier
17
- *
18
- * @var string
19
- * @since 2.0
20
- */
21
- public static $type = 'text';
22
-
23
- /**
24
- * Field Type Label
25
- *
26
- * @var string
27
- * @since 2.0
28
- */
29
- public static $label = 'Unknown';
30
-
31
- /**
32
- * Field Type Preparation
33
- *
34
- * @var string
35
- * @since 2.0
36
- */
37
- public static $prepare = '%s';
38
-
39
- /**
40
- * Pod Types supported on (true for all, false for none, or give array of specific types supported)
41
- *
42
- * @var array|bool
43
- * @since 2.1
44
- */
45
- public static $pod_types = true;
46
-
47
- /**
48
- * API caching for fields that need it during validate/save
49
- *
50
- * @var \PodsAPI
51
- * @since 2.3
52
- */
53
- private static $api = false;
54
-
55
- /**
56
- * Do things like register/enqueue scripts and stylesheets
57
- *
58
- * @return \PodsField
59
- *
60
- * @since 2.0
61
- */
62
- public function __construct () {
63
-
64
- }
65
-
66
- /**
67
- * Add options and set defaults for field type, shows in admin area
68
- *
69
- * @return array $options
70
- *
71
- * @since 2.0
72
- * @see PodsField::ui_options
73
- */
74
- public function options () {
75
- $options = array( /*
76
- 'option_name' => array(
77
- 'label' => 'Option Label',
78
- 'depends-on' => array( 'another_option' => 'specific-value' ),
79
- 'default' => 'default-value',
80
- 'type' => 'field_type',
81
- 'data' => array(
82
- 'value1' => 'Label 1',
83
-
84
- // Group your options together
85
- 'Option Group' => array(
86
- 'gvalue1' => 'Option Label 1',
87
- 'gvalue2' => 'Option Label 2'
88
- ),
89
-
90
- // below is only if the option_name above is the "{$fieldtype}_format_type"
91
- 'value2' => array(
92
- 'label' => 'Label 2',
93
- 'regex' => '[a-zA-Z]' // Uses JS regex validation for the value saved if this option selected
94
- )
95
- ),
96
-
97
- // below is only for a boolean group
98
- 'group' => array(
99
- 'option_boolean1' => array(
100
- 'label' => 'Option boolean 1?',
101
- 'default' => 1,
102
- 'type' => 'boolean'
103
- ),
104
- 'option_boolean2' => array(
105
- 'label' => 'Option boolean 2?',
106
- 'default' => 0,
107
- 'type' => 'boolean'
108
- )
109
- )
110
- ) */
111
- );
112
-
113
- return $options;
114
- }
115
-
116
- /**
117
- * Options for the Admin area, defaults to $this->options()
118
- *
119
- * @return array $options
120
- *
121
- * @since 2.0
122
- * @see PodsField::options
123
- */
124
- public function ui_options () {
125
- return $this->options();
126
- }
127
-
128
- /**
129
- * Define the current field's schema for DB table storage
130
- *
131
- * @param array $options
132
- *
133
- * @return string
134
- * @since 2.0
135
- */
136
- public function schema ( $options = null ) {
137
- $schema = 'VARCHAR(255)';
138
-
139
- return $schema;
140
- }
141
-
142
- /**
143
- * Define the current field's preparation for sprintf
144
- *
145
- * @param array $options
146
- *
147
- * @return array
148
- * @since 2.0
149
- */
150
- public function prepare ( $options = null ) {
151
- $format = self::$prepare;
152
-
153
- return $format;
154
- }
155
-
156
- /**
157
- * Change the value of the field
158
- *
159
- * @param mixed $value
160
- * @param string $name
161
- * @param array $options
162
- * @param array $pod
163
- * @param int $id
164
- *
165
- * @return mixed|null|string
166
- * @since 2.3
167
- */
168
- public function value ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
169
- return $value;
170
- }
171
-
172
- /**
173
- * Change the way the value of the field is displayed with Pods::get
174
- *
175
- * @param mixed $value
176
- * @param string $name
177
- * @param array $options
178
- * @param array $pod
179
- * @param int $id
180
- *
181
- * @return mixed|null|string
182
- * @since 2.0
183
- */
184
- public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
185
- return $value;
186
- }
187
-
188
- /**
189
- * Customize output of the form field
190
- *
191
- * @param string $name
192
- * @param mixed $value
193
- * @param array $options
194
- * @param array $pod
195
- * @param int $id
196
- *
197
- * @return void
198
- *
199
- * @since 2.0
200
- */
201
- public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
202
- $options = (array) $options;
203
- $form_field_type = PodsForm::$field_type;
204
-
205
- if ( is_array( $value ) )
206
- $value = implode( ' ', $value );
207
-
208
- pods_view( PODS_DIR . 'ui/fields/text.php', compact( array_keys( get_defined_vars() ) ) );
209
- }
210
-
211
- /**
212
- * Get the data from the field
213
- *
214
- * @param string $name The name of the field
215
- * @param string|array $value The value of the field
216
- * @param array $options
217
- * @param array $pod
218
- * @param int $id
219
- * @param boolean $in_form
220
- *
221
- * @return array Array of possible field data
222
- *
223
- * @since 2.0
224
- */
225
- public function data ( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) {
226
- return (array) $value;
227
- }
228
-
229
- /**
230
- * Build regex necessary for JS validation
231
- *
232
- * @param mixed $value
233
- * @param string $name
234
- * @param array $options
235
- * @param string $pod
236
- * @param int $id
237
- *
238
- * @return bool
239
- * @since 2.0
240
- */
241
- public function regex ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
242
- return false;
243
- }
244
-
245
- /**
246
- * Validate a value before it's saved
247
- *
248
- * @param mixed $value
249
- * @param string $name
250
- * @param array $options
251
- * @param array $fields
252
- * @param array $pod
253
- * @param int $id
254
- * @param array $params
255
- *
256
- * @return bool
257
- * @since 2.0
258
- */
259
- public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
260
- return true;
261
- }
262
-
263
- /**
264
- * Change the value or perform actions after validation but before saving to the DB
265
- *
266
- * @param mixed $value
267
- * @param int $id
268
- * @param string $name
269
- * @param array $options
270
- * @param array $fields
271
- * @param array $pod
272
- * @param object $params
273
- *
274
- * @return mixed
275
- * @since 2.0
276
- */
277
- public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
278
- return $value;
279
- }
280
-
281
- /**
282
- * Save the value to the DB
283
- *
284
- * @param mixed $value
285
- * @param int $id
286
- * @param string $name
287
- * @param array $options
288
- * @param array $fields
289
- * @param array $pod
290
- * @param object $params
291
- *
292
- * @return bool|void Whether the value was saved
293
- *
294
- * @since 2.3
295
- */
296
- public function save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
297
- return null;
298
- }
299
-
300
- /**
301
- * Perform actions after saving to the DB
302
- *
303
- * @param mixed $value
304
- * @param int $id
305
- * @param string $name
306
- * @param array $options
307
- * @param array $fields
308
- * @param array $pod
309
- * @param object $params
310
- *
311
- * @since void
312
- *
313
- * @since 2.0
314
- */
315
- public function post_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
316
-
317
- }
318
-
319
- /**
320
- * Perform actions before deleting from the DB
321
- *
322
- * @param int $id
323
- * @param string $name
324
- * @param null $options
325
- * @param string $pod
326
- *
327
- * @since void
328
- *
329
- * @since 2.0
330
- */
331
- public function pre_delete ( $id = null, $name = null, $options = null, $pod = null ) {
332
-
333
- }
334
-
335
- /**
336
- * Delete the value from the DB
337
- *
338
- * @param int $id
339
- * @param string $name
340
- * @param array $options
341
- * @param array $pod
342
- *
343
- * @since void
344
- *
345
- * @since 2.3
346
- */
347
- public function delete ( $id = null, $name = null, $options = null, $pod = null ) {
348
-
349
- }
350
-
351
- /**
352
- * Perform actions after deleting from the DB
353
- *
354
- * @param int $id
355
- * @param string $name
356
- * @param array $options
357
- * @param array $pod
358
- *
359
- * @since void
360
- *
361
- * @since 2.0
362
- */
363
- public function post_delete ( $id = null, $name = null, $options = null, $pod = null ) {
364
-
365
- }
366
-
367
- /**
368
- * Customize the Pods UI manage table column output
369
- *
370
- * @param int $id
371
- * @param mixed $value
372
- * @param string $name
373
- * @param array $options
374
- * @param array $fields
375
- * @param array $pod
376
- *
377
- * @since string Value to be shown in the UI
378
- *
379
- * @since 2.0
380
- */
381
- public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
382
- return $value;
383
- }
384
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
classes/PodsForm.php DELETED
@@ -1,1515 +0,0 @@
1
- <?php
2
- /**
3
- * @package Pods
4
- */
5
- class PodsForm {
6
-
7
- /**
8
- * @var PodsForm
9
- */
10
- protected static $instance = null;
11
-
12
- /**
13
- * @var string
14
- */
15
- static $field = null;
16
-
17
- /**
18
- * @var string
19
- */
20
- static $field_group = null;
21
-
22
- /**
23
- * @var string
24
- */
25
- static $field_type = null;
26
-
27
- /**
28
- * @var array
29
- */
30
- static $field_types = array();
31
-
32
- /**
33
- * @var array
34
- */
35
- static $loaded = array();
36
-
37
- /**
38
- * @var int
39
- */
40
- static $form_counter = 0;
41
-
42
- /**
43
- * Singleton handling for a basic pods_form() request
44
- *
45
- * @return \PodsForm
46
- *
47
- * @since 2.3.5
48
- */
49
- public static function init () {
50
- if ( !is_object( self::$instance ) )
51
- self::$instance = new PodsForm();
52
-
53
- return self::$instance;
54
- }
55
-
56
- /**
57
- * Master handler for all field / form methods
58
- *
59
- * @return \PodsForm
60
- *
61
- * @license http://www.gnu.org/licenses/gpl-2.0.html
62
- * @since 2.0
63
- */
64
- private function __construct() {
65
- add_action( 'admin_init', array( $this, 'admin_init' ), 14 );
66
- }
67
-
68
- /**
69
- * Prevent clones
70
- *
71
- * @since 2.3
72
- */
73
- private function __clone() {
74
- // Hulk smash
75
- }
76
-
77
- /**
78
- * Output a field's label
79
- *
80
- * @since 2.0
81
- */
82
-
83
- /**
84
- * Output a field's label
85
- *
86
- * @param string $name Field name
87
- * @param string $label Label text
88
- * @param string $help Help text
89
- * @param array $options Field options
90
- *
91
- * @return string Label HTML
92
- *
93
- * @since 2.0
94
- */
95
- public static function label( $name, $label, $help = '', $options = null ) {
96
- if ( is_array( $label ) || is_object( $label ) ) {
97
- $options = $label;
98
- $label = $options[ 'label' ];
99
-
100
- if ( empty( $label ) )
101
- $label = ucwords( str_replace( '_', ' ', $name ) );
102
-
103
- $help = $options[ 'help' ];
104
- }
105
- else
106
- $options = self::options( null, $options );
107
-
108
- $label = apply_filters( 'pods_form_ui_label_text', $label, $name, $help, $options );
109
- $help = apply_filters( 'pods_form_ui_label_help', $help, $name, $label, $options );
110
-
111
- ob_start();
112
-
113
- $name_clean = self::clean( $name );
114
- $name_more_clean = self::clean( $name, true );
115
-
116
- $type = 'label';
117
- $attributes = array();
118
- $attributes[ 'class' ] = 'pods-form-ui-' . $type . ' pods-form-ui-' . $type . '-' . $name_more_clean;
119
- $attributes[ 'for' ] = ( false === strpos( $name_clean, 'pods-form-ui-' ) ? 'pods-form-ui-' : '' ) . $name_clean;
120
- $attributes = self::merge_attributes( $attributes, $name, $type, $options, false );
121
-
122
- pods_view( PODS_DIR . 'ui/fields/_label.php', compact( array_keys( get_defined_vars() ) ) );
123
-
124
- $output = ob_get_clean();
125
-
126
- return apply_filters( 'pods_form_ui_' . $type, $output, $name, $label, $help, $attributes, $options );
127
- }
128
-
129
- /**
130
- * Output a Field Comment Paragraph
131
- *
132
- * @param string $name Field name
133
- * @param string $message Field comments
134
- * @param array $options Field options
135
- *
136
- * @return string Comment HTML
137
- *
138
- * @since 2.0
139
- */
140
- public static function comment( $name, $message = null, $options = null ) {
141
- $options = self::options( null, $options );
142
-
143
- $name_more_clean = self::clean( $name, true );
144
-
145
- if ( isset( $options[ 'description' ] ) && !empty( $options[ 'description' ] ) )
146
- $message = $options[ 'description' ];
147
- elseif ( empty( $message ) )
148
- return '';
149
-
150
- $message = apply_filters( 'pods_form_ui_comment_text', $message, $name, $options );
151
-
152
- ob_start();
153
-
154
- $type = 'comment';
155
- $attributes = array();
156
- $attributes[ 'class' ] = 'pods-form-ui-' . $type . ' pods-form-ui-' . $type . '-' . $name_more_clean;
157
- $attributes = self::merge_attributes( $attributes, $name, $type, $options, false );
158
-
159
- pods_view( PODS_DIR . 'ui/fields/_comment.php', compact( array_keys( get_defined_vars() ) ) );
160
-
161
- $output = ob_get_clean();
162
-
163
- return apply_filters( 'pods_form_ui_' . $type, $output, $name, $message, $attributes, $options );
164
- }
165
-
166
- /**
167
- * Output a field
168
- *
169
- * @param string $name Field name
170
- * @param mixed $value Field value
171
- * @param string $type Field type
172
- * @param array $options Field options
173
- * @param array $pod Pod data
174
- * @param int $id Item ID
175
- *
176
- * @return string Field HTML
177
- *
178
- * @since 2.0
179
- */
180
- public static function field( $name, $value, $type = 'text', $options = null, $pod = null, $id = null ) {
181
- // Take a field array
182
- if ( is_array( $name ) || is_object( $name ) ) {
183
- $options = $name;
184
-
185
- if ( is_object( $type ) ) {
186
- $pod = $type;
187
- $id = $options;
188
- }
189
-
190
- $name = pods_v( 'name', $options );
191
- $type = pods_v( 'type', $options );
192
- }
193
-
194
- $options = self::options( $type, $options );
195
-
196
- if ( null === $value || ( '' === $value && 'boolean' == $type ) || ( !empty( $pod ) && empty( $id ) ) )
197
- $value = self::default_value( $value, $type, $name, $options, $pod, $id );
198
-
199
- if ( false === self::permission( $type, $name, $options, null, $pod, $id ) )
200
- return false;
201
-
202
- $value = apply_filters( 'pods_form_ui_field_' . $type . '_value', $value, $name, $options, $pod, $id );
203
- $form_field_type = self::$field_type;
204
-
205
- ob_start();
206
-
207
- $helper = false;
208
-
209
- if ( 0 < strlen( pods_v( 'input_helper', $options ) ) )
210
- $helper = pods_api()->load_helper( array( 'name' => $options[ 'input_helper' ] ) );
211
-
212
- if ( ( !isset( $options[ 'data' ] ) || empty( $options[ 'data' ] ) ) && is_object( self::$loaded[ $type ] ) && method_exists( self::$loaded[ $type ], 'data' ) )
213
- $data = $options[ 'data' ] = self::$loaded[ $type ]->data( $name, $value, $options, $pod, $id, true );
214
-
215
- if ( true === apply_filters( 'pods_form_ui_field_' . $type . '_override', false, $name, $value, $options, $pod, $id ) )
216
- do_action( 'pods_form_ui_field_' . $type, $name, $value, $options, $pod, $id );
217
- elseif ( !empty( $helper ) && 0 < strlen( pods_v( 'code', $helper ) ) && false === strpos( $helper[ 'code' ], '$this->' ) && ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) )
218
- eval( '?>' . $helper[ 'code' ] );
219
- elseif ( method_exists( get_class(), 'field_' . $type ) )
220
- echo call_user_func( array( get_class(), 'field_' . $type ), $name, $value, $options );
221
- elseif ( is_object( self::$loaded[ $type ] ) && method_exists( self::$loaded[ $type ], 'input' ) )
222
- self::$loaded[ $type ]->input( $name, $value, $options, $pod, $id );
223
- else
224
- do_action( 'pods_form_ui_field_' . $type, $name, $value, $options, $pod, $id );
225
-
226
- $output = ob_get_clean();
227
-
228
- return apply_filters( 'pods_form_ui_field_' . $type, $output, $name, $value, $options, $pod, $id );
229
- }
230
-
231
- /**
232
- * Output field type 'db'
233
- *
234
- * Used for field names and other places where only [a-z0-9_] is accepted
235
- *
236
- * @since 2.0
237
- */
238
- protected static function field_db( $name, $value = null, $options = null ) {
239
- $form_field_type = self::$field_type;
240
-
241
- ob_start();
242
-
243
- pods_view( PODS_DIR . 'ui/fields/_db.php', compact( array_keys( get_defined_vars() ) ) );
244
-
245
- $output = ob_get_clean();
246
-
247
- return apply_filters( 'pods_form_ui_field_db', $output, $name, $value, $options );
248
- }
249
-
250
- /**
251
- * Output a hidden field
252
- */
253
- protected static function field_hidden( $name, $value = null, $options = null ) {
254
- $form_field_type = self::$field_type;
255
-
256
- ob_start();
257
-
258
- pods_view( PODS_DIR . 'ui/fields/_hidden.php', compact( array_keys( get_defined_vars() ) ) );
259
-
260
- $output = ob_get_clean();
261
-
262
- return apply_filters( 'pods_form_ui_field_hidden', $output, $name, $value, $options );
263
- }
264
-
265
- /**
266
- * Returns a submit button, with provided text and appropriate class, copied from WP Core for use on the frontend
267
- *
268
- * @see get_submit_button
269
- *
270
- * @param string $text The text of the button (defaults to 'Save Changes')
271
- * @param string $type The type of button. One of: primary, secondary, delete
272
- * @param string $name The HTML name of the submit button. Defaults to "submit". If no id attribute
273
- * is given in $other_attributes below, $name will be used as the button's id.
274
- * @param bool $wrap True if the output button should be wrapped in a paragraph tag,
275
- * false otherwise. Defaults to true
276
- * @param array|string $other_attributes Other attributes that should be output with the button,
277
- * mapping attributes to their values, such as array( 'tabindex' => '1' ).
278
- * These attributes will be output as attribute="value", such as tabindex="1".
279
- * Defaults to no other attributes. Other attributes can also be provided as a
280
- * string such as 'tabindex="1"', though the array format is typically cleaner.
281
- *
282
- * @since 3.0
283
- */
284
- public static function submit_button( $text = null, $type = 'primary large', $name = 'submit', $wrap = true, $other_attributes = null ) {
285
-
286
- if ( function_exists( 'get_submit_button' ) ) {
287
- return get_submit_button( $text, $type, $name, $wrap, $other_attributes );
288
- }
289
-
290
- if ( !is_array( $type ) ) {
291
- $type = explode( ' ', $type );
292
- }
293
-
294
- $button_shorthand = array(
295
- 'primary',
296
- 'small',
297
- 'large'
298
- );
299
-
300
- $classes = array(
301
- 'button'
302
- );
303
-
304
- foreach ( $type as $t ) {
305
- if ( 'secondary' === $t || 'button-secondary' === $t ) {
306
- continue;
307
- }
308
-
309
- $classes[] = in_array( $t, $button_shorthand ) ? 'button-' . $t : $t;
310
- }
311
-
312
- $class = implode( ' ', array_unique( $classes ) );
313
-
314
- if ( 'delete' === $type ) {
315
- $class = 'button-secondary delete';
316
- }
317
-
318
- $text = $text ? $text : __( 'Save Changes' );
319
-
320
- // Default the id attribute to $name unless an id was specifically provided in $other_attributes
321
- $id = $name;
322
-
323
- if ( is_array( $other_attributes ) && isset( $other_attributes[ 'id' ] ) ) {
324
- $id = $other_attributes[ 'id' ];
325
- unset( $other_attributes[ 'id' ] );
326
- }
327
-
328
- $attributes = '';
329
-
330
- if ( is_array( $other_attributes ) ) {
331
- foreach ( $other_attributes as $attribute => $value ) {
332
- $attributes .= $attribute . '="' . esc_attr( $value ) . '" '; // Trailing space is important
333
- }
334
- }
335
- elseif ( !empty( $other_attributes ) ) { // Attributes provided as a string
336
- $attributes = $other_attributes;
337
- }
338
-
339
- $button = '<input type="submit" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" class="' . esc_attr( $class );
340
- $button .= '" value="' . esc_attr( $text ) . '" ' . $attributes . ' />';
341
-
342
- if ( $wrap ) {
343
- $button = '<p class="submit">' . $button . '</p>';
344
- }
345
-
346
- return $button;
347
-
348
- }
349
-
350
- /**
351
- * Output a row (label, field, and comment)
352
- *
353
- * @param string $name Field name
354
- * @param mixed $value Field value
355
- * @param string $type Field type
356
- * @param array $options Field options
357
- * @param array $pod Pod data
358
- * @param int $id Item ID
359
- *
360
- * @return string Row HTML
361
- *
362
- * @since 2.0
363
- */
364
- public static function row( $name, $value, $type = 'text', $options = null, $pod = null, $id = null ) {
365
- $options = self::options( null, $options );
366
-
367
- ob_start();
368
-
369
- pods_view( PODS_DIR . 'ui/fields/_row.php', compact( array_keys( get_defined_vars() ) ) );
370
-
371
- $output = ob_get_clean();
372
-
373
- return apply_filters( 'pods_form_ui_field_row', $output, $name, $value, $options, $pod, $id );
374
- }
375
-
376
- /**
377
- * Output a field's attributes
378
- *
379
- * @since 2.0
380
- */
381
- public static function attributes( $attributes, $name = null, $type = null, $options = null ) {
382
- $attributes = (array) apply_filters( 'pods_form_ui_field_' . $type . '_attributes', $attributes, $name, $options );
383
-
384
- foreach ( $attributes as $attribute => $value ) {
385
- if ( null === $value )
386
- continue;
387
-
388
- echo ' ' . esc_attr( (string) $attribute ) . '="' . esc_attr( (string) $value ) . '"';
389
- }
390
- }
391
-
392
- /**
393
- * Output a field's data (for use with jQuery)
394
- *
395
- * @since 2.0
396
- */
397
- public static function data( $data, $name = null, $type = null, $options = null ) {
398
- $data = (array) apply_filters( 'pods_form_ui_field_' . $type . '_data', $data, $name, $options );
399
-
400
- foreach ( $data as $key => $value ) {
401
- if ( null === $value )
402
- continue;
403
-
404
- $key = sanitize_title( $key );
405
-
406
- if ( is_array( $value ) )
407
- $value = implode( ',', $value );
408
-
409
- echo ' data-' . esc_attr( (string) $key ) . '="' . esc_attr( (string) $value ) . '"';
410
- }
411
- }
412
-
413
- /**
414
- * Merge attributes and handle classes
415
- *
416
- * @since 2.0
417
- */
418
- public static function merge_attributes( $attributes, $name = null, $type = null, $options = null, $classes = '' ) {
419
- $options = (array) $options;
420
-
421
- if ( !in_array( $type, array( 'label', 'comment' ) ) ) {
422
- $name_clean = self::clean( $name );
423
- $name_more_clean = self::clean( $name, true );
424
- $_attributes = array();
425
- $_attributes[ 'name' ] = $name;
426
- $_attributes[ 'data-name-clean' ] = $name_more_clean;
427
-
428
- if ( 0 < strlen( pods_v( 'label', $options, '' ) ) )
429
- $_attributes[ 'data-label' ] = strip_tags( pods_v( 'label', $options ) );
430
-
431
- $_attributes[ 'id' ] = 'pods-form-ui-' . $name_clean;
432
- $_attributes[ 'class' ] = 'pods-form-ui-field-type-' . $type . ' pods-form-ui-field-name-' . $name_more_clean;
433
-
434
- if ( isset( $options[ 'dependency' ] ) && false !== $options[ 'dependency' ] )
435
- $_attributes[ 'class' ] .= ' pods-dependent-toggle';
436
-
437
- $attributes = array_merge( $_attributes, (array) $attributes );
438
-
439
- if ( isset( $options[ 'attributes' ] ) && is_array( $options[ 'attributes' ] ) && !empty( $options[ 'attributes' ] ) )
440
- $attributes = array_merge( $attributes, $options[ 'attributes' ] );
441
- }
442
- elseif ( isset( $options[ $type . '_attributes' ] ) && is_array( $options[ $type . '_attributes' ] ) && !empty( $options[ $type . '_attributes' ] ) )
443
- $attributes = array_merge( $attributes, $options[ $type . '_attributes' ] );
444
-
445
- if ( isset( $options[ 'class' ] ) && !empty( $options[ 'class' ] ) ) {
446
- if ( is_array( $options[ 'class' ] ) )
447
- $options[ 'class' ] = implode( ' ', $options[ 'class' ] );
448
-
449
- $options[ 'class' ] = (string) $options[ 'class' ];
450
- if ( isset( $attributes[ 'class' ] ) )
451
- $attributes[ 'class' ] = $attributes[ 'class' ] . ' ' . $options[ 'class' ];
452
- else
453
- $attributes[ 'class' ] = $options[ 'class' ];
454
-
455
- $attributes[ 'class' ] = trim( $attributes[ 'class' ] );
456
- }
457
-
458
- if ( !empty( $classes ) ) {
459
- if ( isset( $attributes[ 'class' ] ) )
460
- $attributes[ 'class' ] = $attributes[ 'class' ] . ' ' . $classes;
461
- else
462
- $attributes[ 'class' ] = $classes;
463
- }
464
-
465
- if ( isset( $options[ 'placeholder' ] ) && !empty( $options[ 'placeholder' ] ) ) {
466
- if ( is_array( $options[ 'placeholder' ] ) )
467
- $options[ 'placeholder' ] = implode( ' ', $options[ 'placeholder' ] );
468
-
469
- $options[ 'placeholder' ] = (string) $options[ 'placeholder' ];
470
- $attributes[ 'placeholder' ] = trim( $options[ 'placeholder' ] );
471
- }
472
-
473
- if ( 1 == pods_v( 'required', $options, 0 ) )
474
- $attributes[ 'class' ] .= ' pods-validate pods-validate-required';
475
-
476
- $max_length = (int) pods_var( 'maxlength', $options, pods_v( $type . '_max_length', $options, 0 ), null, true );
477
-
478
- if ( 0 < $max_length )
479
- $attributes[ 'maxlength' ] = $max_length;
480
-
481
- $attributes = (array) apply_filters( 'pods_form_ui_field_' . $type . '_merge_attributes', $attributes, $name, $options );
482
- return $attributes;
483
- }
484
-
485
- /**
486
- * Setup options for a field and store them for later use
487
- *
488
- * @param $type
489
- * @param $options
490
- *
491
- * @return array
492
- *
493
- * @static
494
- *
495
- * @since 2.0
496
- */
497
- public static function options( $type, $options ) {
498
- $options = (array) $options;
499
-
500
- if ( !is_object( $options ) && isset( $options[ 'options' ] ) ) {
501
- $options_temp = $options[ 'options' ];
502
-
503
- unset( $options[ 'options' ] );
504
-
505
- $options = array_merge( $options_temp, $options );
506
-
507
- $override = array(
508
- 'class'
509
- );
510
-
511
- foreach ( $override as $check ) {
512
- if ( isset( $options_temp[ $check ] ) )
513
- $options[ $check ] = $options_temp[ $check ];
514
- }
515
- }
516
-
517
- $defaults = self::options_setup( $type, $options );
518
-
519
- $core_defaults = array(
520
- 'id' => 0,
521
- 'label' => '',
522
- 'description' => '',
523
- 'help' => '',
524
- 'default' => null,
525
- 'attributes' => array(),
526
- 'class' => '',
527
- 'grouped' => 0,
528
- );
529
-
530
- $defaults = array_merge( $core_defaults, $defaults );
531
-
532
- foreach ( $defaults as $option => $settings ) {
533
- $default = $settings;
534
-
535
- if ( is_array( $settings ) && isset( $settings[ 'default' ] ) )
536
- $default = $settings[ 'default' ];
537
-
538
- if ( !isset( $options[ $option ] ) )
539
- $options[ $option ] = $default;
540
- }
541
-
542
- return $options;
543
- }
544
-
545
- /**
546
- * Get options for a field type and setup defaults
547
- *
548
- * @static
549
- *
550
- * @param $type
551
- *
552
- * @return array|null
553
- *
554
- * @since 2.0
555
- */
556
- public static function options_setup( $type = null, $options = null ) {
557
- $core_defaults = array(
558
- 'id' => 0,
559
- 'name' => '',
560
- 'label' => '',
561
- 'description' => '',
562
- 'help' => '',
563
- 'default' => null,
564
- 'attributes' => array(),
565
- 'class' => '',
566
- 'type' => 'text',
567
- 'group' => 0,
568
- 'grouped' => 0,
569
- 'developer_mode' => false,
570
- 'dependency' => false,
571
- 'depends-on' => array(),
572
- 'excludes-on' => array(),
573
- 'options' => array()
574
- );
575
-
576
- if ( !empty( $options ) && is_array( $options ) )
577
- $core_defaults = array_merge( $core_defaults, $options );
578
-
579
- if ( null === $type )
580
- return $core_defaults;
581
- else
582
- self::field_loader( $type );
583
-
584
- $options = apply_filters( 'pods_field_' . $type . '_options', (array) self::$loaded[ $type ]->options(), $type );
585
-
586
- $first_field = current( $options );
587
-
588
- if ( !empty( $options ) && !isset( $first_field[ 'name' ] ) && !isset( $first_field[ 'label' ] ) ) {
589
- $all_options = array();
590
-
591
- foreach ( $options as $group => $group_options ) {
592
- $all_options = array_merge( $all_options, self::fields_setup( $group_options, $core_defaults ) );
593
- }
594
-
595
- $options = $all_options;
596
- }
597
- else
598
- $options = self::fields_setup( $options, $core_defaults );
599
-
600
- return $options;
601
- }
602
-
603
- /**
604
- * Get Admin options for a field type and setup defaults
605
- *
606
- * @static
607
- *
608
- * @param $type
609
- *
610
- * @return array|null
611
- *
612
- * @since 2.0
613
- */
614
- public static function ui_options( $type ) {
615
- $core_defaults = array(
616
- 'id' => 0,
617
- 'name' => '',
618
- 'label' => '',
619
- 'description' => '',
620
- 'help' => '',
621
- 'default' => null,
622
- 'attributes' => array(),
623
- 'class' => '',
624
- 'type' => 'text',
625
- 'group' => 0,
626
- 'grouped' => 0,
627
- 'developer_mode' => false,
628
- 'dependency' => false,
629
- 'depends-on' => array(),
630
- 'excludes-on' => array(),
631
- 'options' => array()
632
- );
633
-
634
- self::field_loader( $type );
635
-
636
- $options = apply_filters( 'pods_field_' . $type . '_ui_options', (array) self::$loaded[ $type ]->ui_options(), $type );
637
-
638
- $first_field = current( $options );
639
-
640
- if ( !empty( $options ) && !isset( $first_field[ 'name' ] ) && !isset( $first_field[ 'label' ] ) ) {
641
- foreach ( $options as $group => $group_options ) {
642
- $options[ $group ] = self::fields_setup( $group_options, $core_defaults );
643
- }
644
- }
645
- else
646
- $options = self::fields_setup( $options, $core_defaults );
647
-
648
- return $options;
649
- }
650
-
651
- /**
652
- * Get options for a field and setup defaults
653
- *
654
- *
655
- * @param null $fields
656
- * @param null $core_defaults
657
- * @param bool $single
658
- *
659
- * @return array|null
660
- *
661
- * @static
662
- * @since 2.0
663
- */
664
- public static function fields_setup( $fields = null, $core_defaults = null, $single = false ) {
665
- if ( empty( $core_defaults ) ) {
666
- $core_defaults = array(
667
- 'id' => 0,
668
- 'name' => '',
669
- 'label' => '',
670
- 'description' => '',
671
- 'help' => '',
672
- 'default' => null,
673
- 'attributes' => array(),
674
- 'class' => '',
675
- 'type' => 'text',
676
- 'group' => 0,
677
- 'grouped' => 0,
678
- 'developer_mode' => false,
679
- 'dependency' => false,
680
- 'depends-on' => array(),
681
- 'excludes-on' => array(),
682
- 'options' => array()
683
- );
684
- }
685
-
686
- if ( $single )
687
- $fields = array( $fields );
688
-
689
- foreach ( $fields as $f => $field ) {
690
- $fields[ $f ] = self::field_setup( $field, $core_defaults, pods_v( 'type', $field, 'text' ) );
691
-
692
- if ( !$single && strlen( $fields[ $f ][ 'name' ] ) < 1 )
693
- $fields[ $f ][ 'name' ] = $f;
694
- }
695
-
696
- if ( $single )
697
- $fields = $fields[ 0 ];
698
-
699
- return $fields;
700
- }
701
-
702
- /**
703
- * Get options for a field and setup defaults
704
- *
705
- * @static
706
- *
707
- * @param null $field
708
- * @param null $core_defaults
709
- * @param null $type
710
- *
711
- * @return array|null
712
- *
713
- * @since 2.0
714
- */
715
- public static function field_setup( $field = null, $core_defaults = null, $type = null ) {
716
- $options = array();
717
-
718
- if ( empty( $core_defaults ) ) {
719
- $core_defaults = array(
720
- 'id' => 0,
721
- 'name' => '',
722
- 'label' => '',
723
- 'description' => '',
724
- 'help' => '',
725
- 'default' => null,
726
- 'attributes' => array(),
727
- 'class' => '',
728
- 'type' => 'text',
729
- 'group' => 0,
730
- 'grouped' => 0,
731
- 'developer_mode' => false,
732
- 'dependency' => false,
733
- 'depends-on' => array(),
734
- 'excludes-on' => array(),
735
- 'options' => array()
736
- );
737
-
738
- if ( null !== $type ) {
739
- self::field_loader( $type );
740
-
741
- if ( method_exists( self::$loaded[ $type ], 'options' ) )
742
- $options = apply_filters( 'pods_field_' . $type . '_options', (array) self::$loaded[ $type ]->options(), $type );
743
- }
744
- }
745
-
746
- if ( !is_array( $field ) )
747
- $field = array( 'default' => $field );
748
-
749
- if ( isset( $field[ 'group' ] ) && is_array( $field[ 'group' ] ) ) {
750
- foreach ( $field[ 'group' ] as $g => $group_option ) {
751
- $field[ 'group' ][ $g ] = array_merge( $core_defaults, $group_option );
752
-
753
- if ( strlen( $field[ 'group' ][ $g ][ 'name' ] ) < 1 )
754
- $field[ 'group' ][ $g ][ 'name' ] = $g;
755
- }
756
- }
757
-
758
- $field = array_merge( $core_defaults, $field );
759
-
760
- foreach ( $options as $option => $settings ) {
761
- $v = null;
762
-
763
- if ( isset( $settings[ 'default' ] ) )
764
- $v = $settings[ 'default' ];
765
-
766
- if ( !isset( $field[ 'options' ][ $option ] ) )
767
- $field[ 'options' ][ $option ] = $v;
768
- }
769
-
770
- return $field;
771
- }
772
-
773
- /**
774
- * Setup dependency / exclusion classes
775
- *
776
- * @param array $options array( 'depends-on' => ..., 'excludes-on' => ...)
777
- * @param string $prefix
778
- *
779
- * @return string
780
- * @static
781
- * @since 2.0
782
- */
783
- public static function dependencies( $options, $prefix = '' ) {
784
- $options = (array) $options;
785
-
786
- $depends_on = $excludes_on = array();
787
- if ( isset( $options[ 'depends-on' ] ) )
788
- $depends_on = (array) $options[ 'depends-on' ];
789
-
790
- if ( isset( $options[ 'excludes-on' ] ) )
791
- $excludes_on = (array) $options[ 'excludes-on' ];
792
-
793
- $classes = array();
794
-
795
- if ( !empty( $depends_on ) ) {
796
- $classes[] = 'pods-depends-on';
797
-
798
- foreach ( $depends_on as $depends => $on ) {
799
- $classes[] = 'pods-depends-on-' . $prefix . self::clean( $depends, true );
800
-
801
- if ( !is_bool( $on ) ) {
802
- $on = (array) $on;
803
-
804
- foreach ( $on as $o ) {
805
- $classes[] = 'pods-depends-on-' . $prefix . self::clean( $depends, true ) . '-' . self::clean( $o, true );
806
- }
807
- }
808
- }
809
- }
810
-
811
- if ( !empty( $excludes_on ) ) {
812
- $classes[] = 'pods-excludes-on';
813
- foreach ( $excludes_on as $excludes => $on ) {
814
- $classes[] = 'pods-excludes-on-' . $prefix . self::clean( $excludes, true );
815
-
816
- $on = (array) $on;
817
-
818
- foreach ( $on as $o ) {
819
- $classes[] = 'pods-excludes-on-' . $prefix . self::clean( $excludes, true ) . '-' . self::clean( $o, true );
820
- }
821
- }
822
- }
823
-
824
- $classes = implode( ' ', $classes );
825
-
826
- return $classes;
827
- }
828
-
829
- /**
830
- * Change the value of the field
831
- *
832
- * @param mixed $value
833
- * @param string $name
834
- * @param array $options
835
- * @param array $fields
836
- * @param array $pod
837
- * @param int $id
838
- * @param array $traverse
839
- *
840
- * @since 2.3
841
- */
842
- public static function value( $type, $value = null, $name = null, $options = null, $pod = null, $id = null, $traverse = null ) {
843
- self::field_loader( $type );
844
-
845
- if ( in_array( $type, self::repeatable_field_types() ) && 1 == pods_v( $type . '_repeatable', $options, 0 ) && !is_array( $value ) ) {
846
- if ( 0 < strlen( $value ) ) {
847
- $simple = @json_decode( $value, true );
848
-
849
- if ( is_array( $simple ) )
850
- $value = $simple;
851
- else
852
- $value = (array) $value;
853
- }
854
- else
855
- $value = array();
856
- }
857
-
858
- if ( method_exists( self::$loaded[ $type ], 'value' ) ) {
859
- if ( is_array( $value ) && in_array( $type, self::tableless_field_types() ) ) {
860
- foreach ( $value as &$display_value ) {
861
- $display_value = call_user_func_array( array( self::$loaded[ $type ], 'value' ), array( $display_value, $name, $options, $pod, $id, $traverse ) );
862
- }
863
- }
864
- else
865
- $value = call_user_func_array( array( self::$loaded[ $type ], 'value' ), array( $value, $name, $options, $pod, $id, $traverse ) );
866
- }
867
-
868
- return $value;
869
- }
870
-
871
- /**
872
- * Change the way the value of the field is displayed with Pods::get
873
- *
874
- * @param mixed $value
875
- * @param string $name
876
- * @param array $options
877
- * @param array $fields
878
- * @param array $pod
879
- * @param int $id
880
- * @param array $traverse
881
- *
882
- * @since 2.0
883
- */
884
- public static function display( $type, $value = null, $name = null, $options = null, $pod = null, $id = null, $traverse = null ) {
885
- self::field_loader( $type );
886
-
887
- $tableless_field_types = self::tableless_field_types();
888
-
889
- if ( method_exists( self::$loaded[ $type ], 'display' ) ) {
890
- if ( is_array( $value ) && !in_array( $type, $tableless_field_types ) ) {
891
- foreach ( $value as $k => $display_value ) {
892
- $value[ $k ] = call_user_func_array( array( self::$loaded[ $type ], 'display' ), array( $display_value, $name, $options, $pod, $id, $traverse ) );
893
- }
894
- }
895
- else
896
- $value = call_user_func_array( array( self::$loaded[ $type ], 'display' ), array( $value, $name, $options, $pod, $id, $traverse ) );
897
- }
898
-
899
- $value = apply_filters( 'pods_form_display_' . $type, $value, $name, $options, $pod, $id, $traverse );
900
-
901
- return $value;
902
- }
903
-
904
- /**
905
- * Setup regex for JS / PHP
906
- *
907
- * @static
908
- *
909
- * @param $type
910
- * @param $options
911
- *
912
- * @return mixed|void
913
- * @since 2.0
914
- */
915
- public static function regex( $type, $options ) {
916
- self::field_loader( $type );
917
-
918
- $regex = false;
919
-
920
- if ( method_exists( self::$loaded[ $type ], 'regex' ) )
921
- $regex = self::$loaded[ $type ]->regex( $options );
922
-
923
- $regex = apply_filters( 'pods_field_' . $type . '_regex', $regex, $options, $type );
924
-
925
- return $regex;
926
- }
927
-
928
- /**
929
- * Setup value preparation for sprintf
930
- *
931
- * @static
932
- *
933
- * @param $type
934
- * @param $options
935
- *
936
- * @return mixed|void
937
- * @since 2.0
938
- */
939
- public static function prepare( $type, $options ) {
940
- self::field_loader( $type );
941
-
942
- $prepare = '%s';
943
-
944
- if ( method_exists( self::$loaded[ $type ], 'prepare' ) )
945
- $prepare = self::$loaded[ $type ]->prepare( $options );
946
-
947
- $prepare = apply_filters( 'pods_field_' . $type . '_prepare', $prepare, $options, $type );
948
-
949
- return $prepare;
950
- }
951
-
952
- /**
953
- * Validate a value before it's saved
954
- *
955
- * @param string $type
956
- * @param mixed $value
957
- * @param string $name
958
- * @param array $options
959
- * @param array $fields
960
- * @param array $pod
961
- * @param int $id
962
- * @param array|object $params
963
- *
964
- * @static
965
- *
966
- * @since 2.0
967
- */
968
- public static function validate( $type, $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
969
- self::field_loader( $type );
970
-
971
- $validate = true;
972
-
973
- if ( 1 == pods_v( 'pre_save', $options, 1 ) && method_exists( self::$loaded[ $type ], 'validate' ) )
974
- $validate = self::$loaded[ $type ]->validate( $value, $name, $options, $fields, $pod, $id, $params );
975
-
976
- $validate = apply_filters( 'pods_field_' . $type . '_validate', $validate, $value, $name, $options, $fields, $pod, $id, $type, $params );
977
-
978
- return $validate;
979
- }
980
-
981
- /**
982
- * Change the value or perform actions after validation but before saving to the DB
983
- *
984
- * @param string $type
985
- * @param mixed $value
986
- * @param int $id
987
- * @param string $name
988
- * @param array $options
989
- * @param array $fields
990
- * @param array $pod
991
- * @param object $params
992
- *
993
- * @static
994
- *
995
- * @since 2.0
996
- */
997
- public static function pre_save( $type, $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
998
- self::field_loader( $type );
999
-
1000
- if ( 1 == pods_v( 'field_pre_save', $options, 1 ) && method_exists( self::$loaded[ $type ], 'pre_save' ) )
1001
- $value = self::$loaded[ $type ]->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
1002
-
1003
- return $value;
1004
- }
1005
-
1006
- /**
1007
- * Save the value to the DB
1008
- *
1009
- * @param string $type
1010
- * @param mixed $value
1011
- * @param int $id
1012
- * @param string $name
1013
- * @param array $options
1014
- * @param array $fields
1015
- * @param array $pod
1016
- * @param object $params
1017
- *
1018
- * @static
1019
- *
1020
- * @since 2.3
1021
- */
1022
- public static function save( $type, $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
1023
- self::field_loader( $type );
1024
-
1025
- $saved = null;
1026
-
1027
- if ( 1 == pods_v( 'field_save', $options, 1 ) && method_exists( self::$loaded[ $type ], 'save' ) )
1028
- $saved = self::$loaded[ $type ]->save( $value, $id, $name, $options, $fields, $pod, $params );
1029
-
1030
- return $saved;
1031
- }
1032
-
1033
- /**
1034
- * Delete the value from the DB
1035
- *
1036
- * @param string $type
1037
- * @param int $id
1038
- * @param string $name
1039
- * @param array $options
1040
- * @param array $pod
1041
- *
1042
- * @static
1043
- *
1044
- * @since 2.3
1045
- */
1046
- public static function delete( $type, $id = null, $name = null, $options = null, $pod = null ) {
1047
- self::field_loader( $type );
1048
-
1049
- $deleted = null;
1050
-
1051
- if ( 1 == pods_v( 'field_delete', $options, 1 ) && method_exists( self::$loaded[ $type ], 'delete' ) )
1052
- $deleted = self::$loaded[ $type ]->delete( $id, $name, $options, $pod );
1053
-
1054
- return $deleted;
1055
- }
1056
-
1057
- /**
1058
- * Check if a user has permission to be editing a field
1059
- *
1060
- * @param $type
1061
- * @param null $name
1062
- * @param null $options
1063
- * @param null $fields
1064
- * @param null $pod
1065
- * @param null $id
1066
- * @param null $params
1067
- *
1068
- * @static
1069
- *
1070
- * @since 2.0
1071
- */
1072
- public static function permission( $type, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
1073
- $permission = pods_permission( $options );
1074
-
1075
- $permission = (boolean) apply_filters( 'pods_form_field_permission', $permission, $type, $name, $options, $fields, $pod, $id, $params );
1076
-
1077
- return $permission;
1078
- }
1079
-
1080
- /**
1081
- * Parse the default the value
1082
- *
1083
- * @since 2.0
1084
- */
1085
- public static function default_value( $value, $type = 'text', $name = null, $options = null, $pod = null, $id = null ) {
1086
- $default_value = pods_v( 'default_value', $options );
1087
-
1088
- if ( '' === $default_value || null === $default_value ) {
1089
- $default_value = $value;
1090
- }
1091
-
1092
- $default = pods_v( 'default', $options, $default_value, true );
1093
-
1094
- if ( is_string( $default ) ) {
1095
- $default_value = str_replace( array( '{@', '}' ), '', trim( $default ) );
1096
- }
1097
-
1098
- if ( $default != $default_value && 1 == (int) pods_v( 'default_evaluate_tags', $options, 1 ) )
1099
- $default = pods_evaluate_tags( $default );
1100
-
1101
- $default = pods_var_raw( pods_v( 'default_value_parameter', $options ), 'request', $default, null, true );
1102
-
1103
- if ( $default != $value )
1104
- $value = $default;
1105
-
1106
- if ( is_array( $value ) )
1107
- $value = pods_serial_comma( $value );
1108
-
1109
- return apply_filters( 'pods_form_field_default_value', $value, $default, $type, $options, $pod, $id );
1110
- }
1111
-
1112
- /**
1113
- * Clean a value for use in class / id
1114
- *
1115
- * @since 2.0
1116
- */
1117
- public static function clean( $input, $noarray = false, $db_field = false ) {
1118
- $input = str_replace( array( '--1', '__1' ), '00000', (string) $input );
1119
- if ( false !== $noarray )
1120
- $input = preg_replace( '/\[\d*\]/', '-', $input );
1121
- $output = str_replace( array( '[', ']' ), '-', strtolower( $input ) );
1122
- $output = preg_replace( '/([^a-z0-9\-_])/', '', $output );
1123
- $output = trim( str_replace( array( '__', '_', '--' ), '-', $output ), '-' );
1124
- $output = str_replace( '00000', '--1', $output );
1125
- if ( false !== $db_field )
1126
- $output = str_replace( '-', '_', $output );
1127
- return $output;
1128
- }
1129
-
1130
- /**
1131
- * Run admin_init methods for each field type
1132
- *
1133
- * @since 2.3
1134
- */
1135
- public function admin_init() {
1136
- $admin_field_types = pods_transient_get( 'pods_form_admin_init_field_types' );
1137
-
1138
- if ( empty( $admin_field_types ) ) {
1139
- $admin_field_types = array();
1140
-
1141
- $field_types = self::field_types();
1142
-
1143
- foreach ( $field_types as $field_type => $field_type_data ) {
1144
- $has_ajax = self::field_method( $field_type_data[ 'type' ], 'admin_init' );
1145
-
1146
- if ( false !== $has_ajax )
1147
- $admin_field_types[] = $field_type;
1148
- }
1149
-
1150
- pods_transient_set( 'pods_form_admin_init_field_types', $admin_field_types );
1151
- }
1152
- else {
1153
- foreach ( $admin_field_types as $field_type ) {
1154
- self::field_method( $field_type, 'admin_init' );
1155
- }
1156
- }
1157
- }
1158
-
1159
- /**
1160
- * Autoload a Field Type's class
1161
- *
1162
- * @param string $field_type Field Type indentifier
1163
- * @param string $file The Field Type class file location
1164
- *
1165
- * @return string
1166
- * @access public
1167
- * @static
1168
- * @since 2.0
1169
- */
1170
- public static function field_loader( $field_type, $file = '' ) {
1171
- if ( isset( self::$loaded[ $field_type ] ) ) {
1172
- $class_vars = get_class_vars( get_class( self::$loaded[ $field_type ] ) ); // PHP 5.2.x workaround
1173
-
1174
- self::$field_group = ( isset( $class_vars[ 'group' ] ) ? $class_vars[ 'group' ] : '' );
1175
- self::$field_type = $class_vars[ 'type' ];
1176
-
1177
- if ( 'Unknown' != $class_vars[ 'label' ] )
1178
- return self::$loaded[ $field_type ];
1179
- }
1180
-
1181
- include_once PODS_DIR . 'classes/PodsField.php';
1182
-
1183
- $field_type = self::clean( $field_type, true, true );
1184
-
1185
- $class_name = ucfirst( $field_type );
1186
- $class_name = "PodsField_{$class_name}";
1187
-
1188
- $content_dir = realpath( WP_CONTENT_DIR );
1189
- $plugins_dir = realpath( WP_PLUGIN_DIR );
1190
- $muplugins_dir = realpath( WPMU_PLUGIN_DIR );
1191
- $abspath_dir = realpath( ABSPATH );
1192
-
1193
- if ( !class_exists( $class_name ) ) {
1194
- if ( isset( self::$field_types[ $field_type ] ) && !empty( self::$field_types[ $field_type ][ 'file' ] ) )
1195
- $file = realpath( self::$field_types[ $field_type ][ 'file' ] );
1196
-
1197
- if ( !empty( $file ) && 0 === strpos( $file, $abspath_dir ) && file_exists( $file ) )
1198
- include_once $file;
1199
- else {
1200
- $file = str_replace( '../', '', apply_filters( 'pods_form_field_include', PODS_DIR . 'classes/fields/' . basename( $field_type ) . '.php', $field_type ) );
1201
- $file = realpath( $file );
1202
-
1203
- if ( file_exists( $file ) && ( 0 === strpos( $file, $content_dir ) || 0 === strpos( $file, $plugins_dir ) || 0 === strpos( $file, $muplugins_dir ) || 0 === strpos( $file, $abspath_dir ) ) )
1204
- include_once $file;
1205
- }
1206
- }
1207
-
1208
- if ( class_exists( $class_name ) )
1209
- $class = new $class_name();
1210
- else {
1211
- $class = new PodsField();
1212
- $class_name = 'PodsField';
1213
- }
1214
-
1215
- $class_vars = get_class_vars( $class_name ); // PHP 5.2.x workaround
1216
-
1217
- self::$field_group = ( isset( $class_vars[ 'group' ] ) ? $class_vars[ 'group' ] : '' );
1218
- self::$field_type = $class_vars[ 'type' ];
1219
-
1220
- self::$loaded[ $field_type ] =& $class;
1221
-
1222
- return self::$loaded[ $field_type ];
1223
- }
1224
-
1225
- /**
1226
- * Run a method from a Field Type's class
1227
- *
1228
- * @param string $field_type Field Type indentifier
1229
- * @param string $method Method name
1230
- * @param mixed $arg More arguments
1231
- *
1232
- * @return mixed
1233
- * @access public
1234
- * @static
1235
- * @since 2.0
1236
- */
1237
- public static function field_method() {
1238
- $args = func_get_args();
1239
-
1240
- if ( empty( $args ) && count( $args ) < 2 )
1241
- return false;
1242
-
1243
- $field_type = array_shift( $args );
1244
- $method = array_shift( $args );
1245
-
1246
- $class = self::field_loader( $field_type );
1247
-
1248
- if ( method_exists( $class, $method ) )
1249
- return call_user_func_array( array( $class, $method ), $args );
1250
-
1251
- return false;
1252
- }
1253
-
1254
- /**
1255
- * Add a new Pod field type
1256
- *
1257
- * @param string $type The new field type identifier
1258
- * @param string $file The new field type class file location
1259
- *
1260
- * @return array Field Type data
1261
- *
1262
- * @since 2.3
1263
- */
1264
- public static function register_field_type( $type, $file = null ) {
1265
- $field_type = pods_transient_get( 'pods_field_type_' . $type );
1266
-
1267
- if ( empty( $field_type ) || $field_type[ 'type' ] != $type || $field_type[ 'file' ] != $file ) {
1268
- self::field_loader( $type, $file );
1269
-
1270
- $class_vars = get_class_vars( get_class( self::$loaded[ $type ] ) ); // PHP 5.2.x workaround
1271
-
1272
- self::$field_types[ $type ] = $class_vars;
1273
- self::$field_types[ $type ][ 'file' ] = $file;
1274
-
1275
- pods_transient_set( 'pods_field_type_' . $type, self::$field_types[ $type ] );
1276
- }
1277
- else
1278
- self::$field_types[ $type ] = $field_type;
1279
-
1280
- return self::$field_types[ $type ];
1281
- }
1282
-
1283
- /**
1284
- * Get a list of all available field types and include
1285
- *
1286
- * @return array Registered Field Types data
1287
- *
1288
- * @since 2.3
1289
- */
1290
- public static function field_types() {
1291
- $field_types = array(
1292
- 'text',
1293
- 'website',
1294
- 'phone',
1295
- 'email',
1296
- 'password',
1297
- 'paragraph',
1298
- 'wysiwyg',
1299
- 'code',
1300
- 'datetime',
1301
- 'date',
1302
- 'time',
1303
- 'number',
1304
- 'currency',
1305
- 'file',
1306
- 'avatar',
1307
- 'pick',
1308
- 'boolean',
1309
- 'color',
1310
- 'slug'
1311
- );
1312
-
1313
- $field_types = array_merge( $field_types, array_keys( self::$field_types ) );
1314
-
1315
- $field_types = array_filter( array_unique( $field_types ) );
1316
-
1317
- $types = apply_filters( 'pods_api_field_types', $field_types );
1318
-
1319
- $field_types = pods_transient_get( 'pods_field_types' );
1320
-
1321
- if ( empty( $field_types ) || count( $types ) != count( $field_types ) ) {
1322
- $field_types = array();
1323
-
1324
- foreach ( $types as $field_type ) {
1325
- $file = null;
1326
-
1327
- if ( isset( self::$field_types[ $field_type ] ) )
1328
- $file = self::$field_types[ $field_type ][ 'file' ];
1329
-
1330
- self::field_loader( $field_type, $file );
1331
-
1332
- if ( !isset( self::$loaded[ $field_type ] ) || !is_object( self::$loaded[ $field_type ] ) )
1333
- continue;
1334
-
1335
- $class_vars = get_class_vars( get_class( self::$loaded[ $field_type ] ) ); // PHP 5.2.x workaround
1336
-
1337
- $field_types[ $field_type ] = $class_vars;
1338
- $field_types[ $field_type ][ 'file' ] = $file;
1339
- }
1340
-
1341
- self::$field_types = $field_types;
1342
-
1343
- pods_transient_set( 'pods_field_types', self::$field_types );
1344
- }
1345
- else
1346
- self::$field_types = array_merge( $field_types, self::$field_types );
1347
-
1348
- return self::$field_types;
1349
- }
1350
-
1351
- /**
1352
- * Get list of available tableless field types
1353
- *
1354
- * @return array Tableless field types
1355
- *
1356
- * @since 2.3
1357
- */
1358
- public static function tableless_field_types() {
1359
- static $field_types = null;
1360
-
1361
- if ( null === $field_types ) {
1362
- $field_types = array( 'pick', 'file', 'avatar', 'taxonomy' );
1363
-
1364
- $field_types = apply_filters( 'pods_tableless_field_types', $field_types );
1365
- }
1366
- return $field_types;
1367
- }
1368
-
1369
- /**
1370
- * Get list of available file field types
1371
- *
1372
- * @return array File field types
1373
- *
1374
- * @since 2.3
1375
- */
1376
- public static function file_field_types() {
1377
- static $field_types = null;
1378
-
1379
- if ( null === $field_types ) {
1380
- $field_types = array( 'file', 'avatar' );
1381
-
1382
- $field_types = apply_filters( 'pods_file_field_types', $field_types );
1383
- }
1384
- return $field_types;
1385
- }
1386
-
1387
- /**
1388
- * Get list of available repeatable field types
1389
- *
1390
- * @return array Repeatable field types
1391
- *
1392
- * @since 2.3
1393
- */
1394
- public static function repeatable_field_types() {
1395
- static $field_types = null;
1396
-
1397
- if ( null === $field_types ) {
1398
- $field_types = array(
1399
- 'code',
1400
- 'color',
1401
- 'currency',
1402
- 'date',
1403
- 'datetime',
1404
- 'email',
1405
- 'number',
1406
- 'paragraph',
1407
- 'phone',
1408
- 'text',
1409
- 'time',
1410
- 'website',
1411
- 'wysiwyg'
1412
- );
1413
-
1414
- $field_types = apply_filters( 'pods_repeatable_field_types', $field_types );
1415
- }
1416
- return $field_types;
1417
- }
1418
-
1419
- /**
1420
- * Get list of available number field types
1421
- *
1422
- * @return array Number field types
1423
- *
1424
- * @since 2.3
1425
- */
1426
- public static function number_field_types() {
1427
- static $field_types = null;
1428
-
1429
- if ( null === $field_types ) {
1430
- $field_types = array( 'currency', 'number' );
1431
-
1432
- $field_types = apply_filters( 'pods_tableless_field_types', $field_types );
1433
- }
1434
- return $field_types;
1435
- }
1436
-
1437
- /**
1438
- * Get list of available date field types
1439
- *
1440
- * @return array Date field types
1441
- *
1442
- * @since 2.3
1443
- */
1444
- public static function date_field_types() {
1445
- static $field_types = null;
1446
-
1447
- if ( null === $field_types ) {
1448
- $field_types = array( 'date', 'datetime', 'time' );
1449
-
1450
- $field_types = apply_filters( 'pods_tableless_field_types', $field_types );
1451
- }
1452
- return $field_types;
1453
- }
1454
-
1455
- /**
1456
- * Get list of available text field types
1457
- *
1458
- * @return array Text field types
1459
- *
1460
- * @since 2.3
1461
- */
1462
- public static function text_field_types() {
1463
- static $field_types = null;
1464
-
1465
- if ( null === $field_types ) {
1466
- $field_types = array( 'code', 'paragraph', 'slug', 'password', 'text', 'wysiwyg' );
1467
-
1468
- $field_types = apply_filters( 'pods_text_field_types', $field_types );
1469
- }
1470
- return $field_types;
1471
- }
1472
-
1473
- /**
1474
- * Get list of available text field types
1475
- *
1476
- * @return array Text field types
1477
- *
1478
- * @since 2.3
1479
- */
1480
- public static function block_field_types() {
1481
- static $field_types = null;
1482
-
1483
- if ( null === $field_types ) {
1484
- $field_types = array( 'heading', 'html' );
1485
-
1486
- /**
1487
- * Returns the available text field types
1488
- *
1489
- * @since unknown
1490
- *
1491
- * @param object $field_types Outputs the field types
1492
- */
1493
-
1494
- $field_types = apply_filters( 'pods_block_field_types', $field_types );
1495
- }
1496
- return $field_types;
1497
- }
1498
-
1499
- /**
1500
- * Get list of available text field types
1501
- *
1502
- * @return array Text field types
1503
- *
1504
- * @since 2.3
1505
- */
1506
- public static function simple_tableless_objects() {
1507
- static $object_types = null;
1508
-
1509
- if ( null === $object_types ) {
1510
- $object_types = PodsForm::field_method( 'pick', 'simple_objects' );
1511
- }
1512
- return $object_types;
1513
- }
1514
-
1515
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
classes/PodsInit.php DELETED
@@ -1,1334 +0,0 @@
1
- <?php
2
- /**
3
- * @package Pods
4
- */
5
- class PodsInit {
6
-
7
- /**
8
- * @var PodsInit
9
- */
10
- static $instance = null;
11
-
12
- /**
13
- * @var array
14
- */
15
- static $no_conflict = array();
16
-
17
- /**
18
- * @var array
19
- */
20
- static $content_types_registered = array();
21
-
22
- /**
23
- * @var PodsComponents
24
- */
25
- static $components;
26
-
27
- /**
28
- * @var PodsMeta
29
- */
30
- static $meta;
31
-
32
- /**
33
- * @var PodsAdmin
34
- */
35
- static $admin;
36
-
37
- /**
38
- * @var mixed|void
39
- */
40
- static $version;
41
-
42
- /**
43
- * @var mixed|void
44
- */
45
- static $version_last;
46
-
47
- /**
48
- * @var mixed|void
49
- */
50
- static $db_version;
51
-
52
- /**
53
- * Upgrades to trigger (last installed version => upgrade version)
54
- *
55
- * @var array
56
- */
57
- static $upgrades = array(
58
- '1.0.0' => '2.0.0'
59
- //'2.0.0' => '2.1.0'
60
- );
61
-
62
- /**
63
- * Whether an Upgrade for 1.x has happened
64
- *
65
- * @var bool
66
- */
67
- static $upgraded;
68
-
69
- /**
70
- * Whether an Upgrade is needed
71
- *
72
- * @var bool
73
- */
74
- static $upgrade_needed = false;
75
-
76
- /**
77
- * Singleton handling for a basic pods_init() request
78
- *
79
- * @return \PodsInit
80
- *
81
- * @since 2.3.5
82
- */
83
- public static function init () {
84
- if ( !is_object( self::$instance ) )
85
- self::$instance = new PodsInit();
86
-
87
- return self::$instance;
88
- }
89
-
90
- /**
91
- * Setup and Initiate Pods
92
- *
93
- * @return \PodsInit
94
- *
95
- * @license http://www.gnu.org/licenses/gpl-2.0.html
96
- * @since 1.8.9
97
- */
98
- function __construct () {
99
- self::$version = get_option( 'pods_framework_version' );
100
- self::$version_last = get_option( 'pods_framework_version_last' );
101
- self::$db_version = get_option( 'pods_framework_db_version' );
102
- self::$upgraded = (int) get_option( 'pods_framework_upgraded_1_x' );
103
-
104
- if ( empty( self::$version_last ) && 0 < strlen( get_option( 'pods_version' ) ) ) {
105
- $old_version = get_option( 'pods_version' );
106
-
107
- if ( !empty( $old_version ) ) {
108
- if ( false === strpos( $old_version, '.' ) )
109
- $old_version = pods_version_to_point( $old_version );
110
-
111
- update_option( 'pods_framework_version_last', $old_version );
112
-
113
- self::$version_last = $old_version;
114
- }
115
- }
116
-
117
- self::$upgrade_needed = $this->needs_upgrade();
118
-
119
- add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
120
-
121
- add_action( 'init', array( $this, 'activate_install' ), 9 );
122
- add_action( 'wp_loaded', array( $this, 'flush_rewrite_rules' ) );
123
-
124
- if ( !empty( self::$version ) ) {
125
- $this->run();
126
- }
127
- }
128
-
129
- /**
130
- * Load the plugin textdomain and set default constants
131
- */
132
- public function plugins_loaded () {
133
- if ( !defined( 'PODS_LIGHT' ) )
134
- define( 'PODS_LIGHT', false );
135
-
136
- if ( !defined( 'PODS_TABLELESS' ) )
137
- define( 'PODS_TABLELESS', false );
138
-
139
- load_plugin_textdomain( 'pods', false, dirname( plugin_basename( PODS_DIR . 'init.php' ) ) . '/languages/' );
140
- }
141
-
142
- /**
143
- * Load Pods Components
144
- */
145
- public function load_components () {
146
- if ( !defined( 'PODS_LIGHT' ) || !PODS_LIGHT )
147
- self::$components = pods_components();
148
- }
149
-
150
- /**
151
- * Load Pods Meta
152
- */
153
- public function load_meta () {
154
- self::$meta = pods_meta()->core();
155
- }
156
-
157
- /**
158
- * Set up the Pods core
159
- */
160
- public function core () {
161
- // Session start
162
- pods_session_start();
163
-
164
- add_shortcode( 'pods', 'pods_shortcode' );
165
- add_shortcode( 'pods-form', 'pods_shortcode_form' );
166
-
167
- $security_settings = array(
168
- 'pods_disable_file_browser' => 0,
169
- 'pods_files_require_login' => 1,
170
- 'pods_files_require_login_cap' => '',
171
- 'pods_disable_file_upload' => 0,
172
- 'pods_upload_require_login' => 1,
173
- 'pods_upload_require_login_cap' => ''
174
- );
175
-
176
- foreach ( $security_settings as $security_setting => $setting ) {
177
- $setting = get_option( $security_setting );
178
- if ( !empty( $setting ) )
179
- $security_settings[ $security_setting ] = $setting;
180
- }
181
-
182
- foreach ( $security_settings as $security_setting => $setting ) {
183
- if ( 0 == $setting )
184
- $setting = false;
185
- elseif ( 1 == $setting )
186
- $setting = true;
187
-
188
- if ( in_array( $security_setting, array( 'pods_files_require_login', 'pods_upload_require_login' ) ) ) {
189
- if ( 0 < strlen( $security_settings[ $security_setting . '_cap' ] ) )
190
- $setting = $security_settings[ $security_setting . '_cap' ];
191
- }
192
- elseif ( in_array( $security_setting, array( 'pods_files_require_login_cap', 'pods_upload_require_login_cap' ) ) )
193
- continue;
194
-
195
- if ( !defined( strtoupper( $security_setting ) ) )
196
- define( strtoupper( $security_setting ), $setting );
197
- }
198
-
199
- $this->register_pods();
200
-
201
- $avatar = PodsForm::field_loader( 'avatar' );
202
-
203
- if ( method_exists( $avatar, 'get_avatar' ) )
204
- add_filter( 'get_avatar', array( $avatar, 'get_avatar' ), 10, 4 );
205
- }
206
-
207
- /**
208
- * Register Scripts and Styles
209
- */
210
- public function register_assets () {
211
- if ( !wp_style_is( 'jquery-ui', 'registered' ) )
212
- wp_register_style( 'jquery-ui', PODS_URL . 'ui/css/smoothness/jquery-ui.custom.css', array(), '1.8.16' );
213
-
214
- wp_register_script( 'pods-json', PODS_URL . 'ui/js/jquery.json.js', array( 'jquery' ), '2.3' );
215
-
216
- if ( ! wp_style_is( 'jquery-qtip2', 'registered' ) ) {
217
- wp_register_style( 'jquery-qtip2', PODS_URL . 'ui/css/jquery.qtip.min.css', array(), '2.2' );
218
- }
219
-
220
- if ( ! wp_script_is( 'jquery-qtip2', 'registered' ) ) {
221
- wp_register_script( 'jquery-qtip2', PODS_URL . 'ui/js/jquery.qtip.min.js', array( 'jquery' ), '2.2' );
222
- }
223
-
224
- wp_register_script( 'pods', PODS_URL . 'ui/js/jquery.pods.js', array( 'jquery', 'pods-json', 'jquery-qtip2' ), PODS_VERSION );
225
-
226
- wp_register_style( 'pods-form', PODS_URL . 'ui/css/pods-form.css', array(), PODS_VERSION );
227
-
228
- wp_register_style( 'pods-cleditor', PODS_URL . 'ui/css/jquery.cleditor.css', array(), '1.3.0' );
229
- wp_register_script( 'pods-cleditor', PODS_URL . 'ui/js/jquery.cleditor.min.js', array( 'jquery' ), '1.3.0' );
230
-
231
- wp_register_style( 'pods-codemirror', PODS_URL . 'ui/css/codemirror.css', array(), '4.8' );
232
- wp_register_script( 'pods-codemirror', PODS_URL . 'ui/js/codemirror.js', array(), '4.8', true );
233
- wp_register_script( 'pods-codemirror-loadmode', PODS_URL . 'ui/js/codemirror/addon/mode/loadmode.js', array( 'pods-codemirror' ), '4.8', true );
234
- wp_register_script( 'pods-codemirror-overlay', PODS_URL . 'ui/js/codemirror/addon/mode/overlay.js', array( 'pods-codemirror' ), '4.8', true );
235
- wp_register_script( 'pods-codemirror-hints', PODS_URL . 'ui/js/codemirror/addon/mode/show-hint.js', array( 'pods-codemirror' ), '4.8', true );
236
- wp_register_script( 'pods-codemirror-mode-xml', PODS_URL . 'ui/js/codemirror/mode/xml/xml.js', array( 'pods-codemirror' ), '4.8', true );
237
- wp_register_script( 'pods-codemirror-mode-html', PODS_URL . 'ui/js/codemirror/mode/htmlmixed/htmlmixed.js', array( 'pods-codemirror' ), '4.8', true );
238
- wp_register_script( 'pods-codemirror-mode-css', PODS_URL . 'ui/js/codemirror/mode/css/css.js', array( 'pods-codemirror' ), '4.8', true );
239
-
240
- if ( !wp_style_is( 'jquery-ui-timepicker', 'registered' ) )
241
- wp_register_style( 'jquery-ui-timepicker', PODS_URL . 'ui/css/jquery.ui.timepicker.css', array(), '1.1.1' );
242
-
243
- if ( !wp_script_is( 'jquery-ui-timepicker', 'registered' ) ) {
244
- wp_register_script( 'jquery-ui-timepicker', PODS_URL . 'ui/js/jquery.ui.timepicker.min.js', array(
245
- 'jquery',
246
- 'jquery-ui-core',
247
- 'jquery-ui-datepicker',
248
- 'jquery-ui-slider'
249
- ), '1.1.1' );
250
- }
251
-
252
- wp_register_style( 'pods-attach', PODS_URL . 'ui/css/jquery.pods.attach.css', array(), PODS_VERSION );
253
- wp_register_script( 'pods-attach', PODS_URL . 'ui/js/jquery.pods.attach.js', array(), PODS_VERSION );
254
-
255
- wp_register_style( 'pods-select2', PODS_URL . 'ui/js/select2/select2.css', array(), '3.3.1' );
256
- wp_register_script( 'pods-select2', PODS_URL . 'ui/js/select2/select2.min.js', array( 'jquery' ), '3.3.1' );
257
-
258
- wp_register_script( 'pods-handlebars', PODS_URL . 'ui/js/handlebars.js', array(), '1.0.0.beta.6' );
259
- }
260
-
261
- /**
262
- * Register internal Post Types
263
- */
264
- public function register_pods () {
265
- $args = array(
266
- 'label' => 'Pods',
267
- 'labels' => array( 'singular_name' => 'Pod' ),
268
- 'public' => false,
269
- 'can_export' => false,
270
- 'query_var' => false,
271
- 'rewrite' => false,
272
- 'capability_type' => 'pods_pod',
273
- 'has_archive' => false,
274
- 'hierarchical' => false,
275
- 'supports' => array( 'title', 'author' ),
276
- 'menu_icon' => 'dashicons-pods'
277
- );
278
-
279
- $args = self::object_label_fix( $args, 'post_type' );
280
-
281
- register_post_type( '_pods_pod', apply_filters( 'pods_internal_register_post_type_pod', $args ) );
282
-
283
- $args = array(
284
- 'label' => 'Pod Fields',
285
- 'labels' => array( 'singular_name' => 'Pod Field' ),
286
- 'public' => false,
287
- 'can_export' => false,
288
- 'query_var' => false,
289
- 'rewrite' => false,
290
- 'capability_type' => 'pods_pod',
291
- 'has_archive' => false,
292
- 'hierarchical' => true,
293
- 'supports' => array( 'title', 'editor', 'author' ),
294
- 'menu_icon' => 'dashicons-pods'
295
- );
296
-
297
- $args = self::object_label_fix( $args, 'post_type' );
298
-
299
- register_post_type( '_pods_field', apply_filters( 'pods_internal_register_post_type_field', $args ) );
300
- }
301
-
302
- /**
303
- * Include Admin
304
- */
305
- public function admin_init () {
306
- self::$admin = pods_admin();
307
- }
308
-
309
- /**
310
- * Register Post Types and Taxonomies
311
- */
312
- public function setup_content_types ( $force = false ) {
313
- $post_types = PodsMeta::$post_types;
314
- $taxonomies = PodsMeta::$taxonomies;
315
-
316
- $existing_post_types = get_post_types();
317
- $existing_taxonomies = get_taxonomies();
318
-
319
- $pods_cpt_ct = pods_transient_get( 'pods_wp_cpt_ct' );
320
-
321
- $cpt_positions = array();
322
-
323
- if ( empty( $pods_cpt_ct ) && ( !empty( $post_types ) || !empty( $taxonomies ) ) )
324
- $force = true;
325
- elseif ( !empty( $pods_cpt_ct ) && empty( $pods_cpt_ct[ 'post_types' ] ) && !empty( $post_types ) )
326
- $force = true;
327
- elseif ( !empty( $pods_cpt_ct ) && empty( $pods_cpt_ct[ 'taxonomies' ] ) && !empty( $taxonomies ) )
328
- $force = true;
329
-
330
- if ( false === $pods_cpt_ct || $force ) {
331
- /**
332
- * @var WP_Query
333
- */
334
- global $wp_query;
335
-
336
- $reserved_query_vars = array(
337
- 'post_type',
338
- 'taxonomy',
339
- 'output'
340
- );
341
-
342
- if ( is_object( $wp_query ) ) {
343
- $reserved_query_vars = array_merge( $reserved_query_vars, array_keys( $wp_query->fill_query_vars( array() ) ) );
344
- }
345
-
346
- $pods_cpt_ct = array(
347
- 'post_types' => array(),
348
- 'taxonomies' => array()
349
- );
350
-
351
- $pods_post_types = $pods_taxonomies = array();
352
- $supported_post_types = $supported_taxonomies = array();
353
-
354
- foreach ( $post_types as $post_type ) {
355
- // Post Type exists already
356
- if ( isset( $pods_cpt_ct[ 'post_types' ][ $post_type[ 'name' ] ] ) )
357
- continue;
358
- elseif ( !empty( $post_type[ 'object' ] ) && isset( $existing_post_types[ $post_type[ 'object' ] ] ) )
359
- continue;
360
- elseif ( !$force && isset( $existing_post_types[ $post_type[ 'name' ] ] ) )
361
- continue;
362
-
363
- $post_type[ 'options' ][ 'name' ] = $post_type[ 'name' ];
364
- $post_type = array_merge( $post_type, (array) $post_type[ 'options' ] );
365
-
366
- $post_type_name = pods_var( 'name', $post_type );
367
-
368
- // Labels
369
- $cpt_label = esc_html( pods_var_raw( 'label', $post_type, ucwords( str_replace( '_', ' ', pods_var_raw( 'name', $post_type ) ) ), null, true ) );
370
- $cpt_singular = esc_html( pods_var_raw( 'label_singular', $post_type, ucwords( str_replace( '_', ' ', pods_var_raw( 'label', $post_type, $post_type_name, null, true ) ) ), null, true ) );
371
-
372
- $cpt_labels = array();
373
- $cpt_labels[ 'name' ] = $cpt_label;
374
- $cpt_labels[ 'singular_name' ] = $cpt_singular;
375
- $cpt_labels[ 'menu_name' ] = pods_var_raw( 'menu_name', $post_type, '', null, true );
376
- $cpt_labels[ 'add_new' ] = pods_var_raw( 'label_add_new', $post_type, '', null, true );
377
- $cpt_labels[ 'add_new_item' ] = pods_var_raw( 'label_add_new_item', $post_type, '', null, true );
378
- $cpt_labels[ 'new_item' ] = pods_var_raw( 'label_new_item', $post_type, '', null, true );
379
- $cpt_labels[ 'edit' ] = pods_var_raw( 'label_edit', $post_type, '', null, true );
380
- $cpt_labels[ 'edit_item' ] = pods_var_raw( 'label_edit_item', $post_type, '', null, true );
381
- $cpt_labels[ 'view' ] = pods_var_raw( 'label_view', $post_type, '', null, true );
382
- $cpt_labels[ 'view_item' ] = pods_var_raw( 'label_view_item', $post_type, '', null, true );
383
- $cpt_labels[ 'all_items' ] = pods_var_raw( 'label_all_items', $post_type, '', null, true );
384
- $cpt_labels[ 'search_items' ] = pods_var_raw( 'label_search_items', $post_type, '', null, true );
385
- $cpt_labels[ 'not_found' ] = pods_var_raw( 'label_not_found', $post_type, '', null, true );
386
- $cpt_labels[ 'not_found_in_trash' ] = pods_var_raw( 'label_not_found_in_trash', $post_type, '', null, true );
387
- $cpt_labels[ 'parent' ] = pods_var_raw( 'label_parent', $post_type, '', null, true );
388
- $cpt_labels[ 'parent_item_colon' ] = pods_var_raw( 'label_parent_item_colon', $post_type, '', null, true );
389
-
390
- // Supported
391
- $cpt_supported = array(
392
- 'title' => (boolean) pods_var( 'supports_title', $post_type, false ),
393
- 'editor' => (boolean) pods_var( 'supports_editor', $post_type, false ),
394
- 'author' => (boolean) pods_var( 'supports_author', $post_type, false ),
395
- 'thumbnail' => (boolean) pods_var( 'supports_thumbnail', $post_type, false ),
396
- 'excerpt' => (boolean) pods_var( 'supports_excerpt', $post_type, false ),
397
- 'trackbacks' => (boolean) pods_var( 'supports_trackbacks', $post_type, false ),
398
- 'custom-fields' => (boolean) pods_var( 'supports_custom_fields', $post_type, false ),
399
- 'comments' => (boolean) pods_var( 'supports_comments', $post_type, false ),
400
- 'revisions' => (boolean) pods_var( 'supports_revisions', $post_type, false ),
401
- 'page-attributes' => (boolean) pods_var( 'supports_page_attributes', $post_type, false ),
402
- 'post-formats' => (boolean) pods_var( 'supports_post_formats', $post_type, false )
403
- );
404
-
405
- // Custom Supported
406
- $cpt_supported_custom = pods_var( 'supports_custom', $post_type, '' );
407
-
408
- if ( !empty( $cpt_supported_custom ) ) {
409
- $cpt_supported_custom = explode( ',', $cpt_supported_custom );
410
- $cpt_supported_custom = array_filter( array_unique( $cpt_supported_custom ) );
411
-
412
- foreach ( $cpt_supported_custom as $cpt_support ) {
413
- $cpt_supported[ $cpt_support ] = true;
414
- }
415
- }
416
-
417
- // Genesis Support
418
- if ( function_exists( 'genesis' ) ) {
419
- $cpt_supported[ 'genesis-seo' ] = (boolean) pods_var( 'supports_genesis_seo', $post_type, false );
420
- $cpt_supported[ 'genesis-layouts' ] = (boolean) pods_var( 'supports_genesis_layouts', $post_type, false );
421
- $cpt_supported[ 'genesis-simple-sidebars' ] = (boolean) pods_var( 'supports_genesis_simple_sidebars', $post_type, false );
422
- }
423
-
424
- // YARPP Support
425
- if ( defined( 'YARPP_VERSION' ) ) {
426
- $cpt_supported[ 'yarpp_support' ] = (boolean) pods_var( 'supports_yarpp_support', $post_type, false );
427
- }
428
-
429
- // Jetpack Support
430
- if ( class_exists( 'Jetpack' ) ) {
431
- $cpt_supported[ 'supports_jetpack_publicize' ] = (boolean) pods_var( 'supports_jetpack_publicize', $post_type, false );
432
- $cpt_supported[ 'supports_jetpack_markdown' ] = (boolean) pods_var( 'supports_jetpack_markdown', $post_type, false );
433
- }
434
-
435
- // WP needs something, if this was empty and none were enabled, it would show title+editor pre 3.5 :(
436
- $cpt_supports = array();
437
-
438
- if ( !pods_version_check( 'wp', '3.5' ) )
439
- $cpt_supports = array( '_bug_fix_pre_35' );
440
-
441
- foreach ( $cpt_supported as $cpt_support => $supported ) {
442
- if ( true === $supported )
443
- $cpt_supports[] = $cpt_support;
444
- }
445
-
446
- if ( empty( $cpt_supports ) && pods_version_check( 'wp', '3.5' ) )
447
- $cpt_supports = false;
448
-
449
- // Rewrite
450
- $cpt_rewrite = (boolean) pods_var( 'rewrite', $post_type, true );
451
- $cpt_rewrite_array = array(
452
- 'slug' => pods_var( 'rewrite_custom_slug', $post_type, str_replace( '_', '-', $post_type_name ), null, true ),
453
- 'with_front' => (boolean) pods_var( 'rewrite_with_front', $post_type, true ),
454
- 'feeds' => (boolean) pods_var( 'rewrite_feeds', $post_type, (boolean) pods_var( 'has_archive', $post_type, false ) ),
455
- 'pages' => (boolean) pods_var( 'rewrite_pages', $post_type, true )
456
- );
457
-
458
- if ( false !== $cpt_rewrite )
459
- $cpt_rewrite = $cpt_rewrite_array;
460
-
461
- $capability_type = pods_var( 'capability_type', $post_type, 'post' );
462
-
463
- if ( 'custom' == $capability_type )
464
- $capability_type = pods_var( 'capability_type_custom', $post_type, 'post' );
465
-
466
- $show_in_menu = (boolean) pods_var( 'show_in_menu', $post_type, true );
467
-
468
- if ( $show_in_menu && 0 < strlen( pods_var_raw( 'menu_location_custom', $post_type ) ) )
469
- $show_in_menu = pods_var_raw( 'menu_location_custom', $post_type );
470
-
471
- $menu_icon = pods_var( 'menu_icon', $post_type, null, null, true );
472
-
473
- if ( !empty( $menu_icon ) ) {
474
- $menu_icon = pods_evaluate_tags( $menu_icon );
475
- }
476
-
477
- // Register Post Type
478
- $pods_post_types[ $post_type_name ] = array(
479
- 'label' => $cpt_label,
480
- 'labels' => $cpt_labels,
481
- 'description' => esc_html( pods_var_raw( 'description', $post_type ) ),
482
- 'public' => (boolean) pods_var( 'public', $post_type, true ),
483
- 'publicly_queryable' => (boolean) pods_var( 'publicly_queryable', $post_type, (boolean) pods_var( 'public', $post_type, true ) ),
484
- 'exclude_from_search' => (boolean) pods_var( 'exclude_from_search', $post_type, ( (boolean) pods_var( 'public', $post_type, true ) ? false : true ) ),
485
- 'show_ui' => (boolean) pods_var( 'show_ui', $post_type, (boolean) pods_var( 'public', $post_type, true ) ),
486
- 'show_in_menu' => $show_in_menu,
487
- 'show_in_nav_menus' => (boolean) pods_var( 'show_in_nav_menus', $post_type, (boolean) pods_var( 'public', $post_type, true ) ),
488
- 'show_in_admin_bar' => (boolean) pods_var( 'show_in_admin_bar', $post_type, (boolean) pods_var( 'show_in_menu', $post_type, true ) ),
489
- 'menu_position' => (int) pods_var( 'menu_position', $post_type, 0, null, true ),
490
- 'menu_icon' => $menu_icon,
491
- 'capability_type' => $capability_type,
492
- //'capabilities' => $cpt_capabilities,
493
- 'map_meta_cap' => (boolean) pods_var( 'capability_type_extra', $post_type, true ),
494
- 'hierarchical' => (boolean) pods_var( 'hierarchical', $post_type, false ),
495
- 'supports' => $cpt_supports,
496
- //'register_meta_box_cb' => array($this, 'manage_meta_box'),
497
- //'permalink_epmask' => EP_PERMALINK,
498
- 'has_archive' => pods_v( 'has_archive_slug', $post_type, (boolean) pods_v( 'has_archive', $post_type, false ), true ),
499
- 'rewrite' => $cpt_rewrite,
500
- 'query_var' => ( false !== (boolean) pods_var( 'query_var', $post_type, true ) ? pods_var( 'query_var_string', $post_type, $post_type_name, null, true ) : false ),
501
- 'can_export' => (boolean) pods_var( 'can_export', $post_type, true )
502
- );
503
-
504
- // YARPP doesn't use 'supports' array option (yet)
505
- if ( ! empty( $cpt_supports[ 'yarpp_support' ] ) ) {
506
- $pods_post_types[ $post_type_name ][ 'yarpp_support' ] = true;
507
- }
508
-
509
- // Prevent reserved query_var issues
510
- if ( in_array( $pods_post_types[ $post_type_name ][ 'query_var' ], $reserved_query_vars ) ) {
511
- $pods_post_types[ $post_type_name ][ 'query_var' ] = 'post_type_' . $pods_post_types[ $post_type_name ][ 'query_var' ];
512
- }
513
-
514
- if ( 25 == $pods_post_types[ $post_type_name ][ 'menu_position' ] )
515
- $pods_post_types[ $post_type_name ][ 'menu_position' ]++;
516
-
517
- if ( $pods_post_types[ $post_type_name ][ 'menu_position' ] < 1 || in_array( $pods_post_types[ $post_type_name ][ 'menu_position' ], $cpt_positions ) )
518
- unset( $pods_post_types[ $post_type_name ][ 'menu_position' ] );
519
- else {
520
- $cpt_positions[] = $pods_post_types[ $post_type_name ][ 'menu_position' ];
521
-
522
- // This would be nice if WP supported floats in menu_position
523
- // $pods_post_types[ $post_type_name ][ 'menu_position' ] = $pods_post_types[ $post_type_name ][ 'menu_position' ] . '.1';
524
- }
525
-
526
- // Taxonomies
527
- $cpt_taxonomies = array();
528
- $_taxonomies = get_taxonomies();
529
- $_taxonomies = array_merge_recursive( $_taxonomies, $pods_taxonomies );
530
- $ignore = array( 'nav_menu', 'link_category', 'post_format' );
531
-
532
- foreach ( $_taxonomies as $taxonomy => $label ) {
533
- if ( in_array( $taxonomy, $ignore ) )
534
- continue;
535
-
536
- if ( false !== (boolean) pods_var( 'built_in_taxonomies_' . $taxonomy, $post_type, false ) ) {
537
- $cpt_taxonomies[] = $taxonomy;
538
-
539
- if ( isset( $supported_post_types[ $taxonomy ] ) && !in_array( $post_type_name, $supported_post_types[ $taxonomy ] ) )
540
- $supported_post_types[ $taxonomy ][] = $post_type_name;
541
- }
542
- }
543
-
544
- if ( isset( $supported_taxonomies[ $post_type_name ] ) )
545
- $supported_taxonomies[ $post_type_name ] = array_merge( (array) $supported_taxonomies[ $post_type_name ], $cpt_taxonomies );
546
- else
547
- $supported_taxonomies[ $post_type_name ] = $cpt_taxonomies;
548
- }
549
-
550
- foreach ( $taxonomies as $taxonomy ) {
551
- // Taxonomy Type exists already
552
- if ( isset( $pods_cpt_ct[ 'taxonomies' ][ $taxonomy[ 'name' ] ] ) )
553
- continue;
554
- elseif ( !empty( $taxonomy[ 'object' ] ) && isset( $existing_taxonomies[ $taxonomy[ 'object' ] ] ) )
555
- continue;
556
- elseif ( !$force && isset( $existing_taxonomies[ $taxonomy[ 'name' ] ] ) )
557
- continue;
558
-
559
- $taxonomy[ 'options' ][ 'name' ] = $taxonomy[ 'name' ];
560
- $taxonomy = array_merge( $taxonomy, (array) $taxonomy[ 'options' ] );
561
-
562
- $taxonomy_name = pods_var( 'name', $taxonomy );
563
-
564
- // Labels
565
- $ct_label = esc_html( pods_var_raw( 'label', $taxonomy, ucwords( str_replace( '_', ' ', pods_var_raw( 'name', $taxonomy ) ) ), null, true ) );
566
- $ct_singular = esc_html( pods_var_raw( 'label_singular', $taxonomy, ucwords( str_replace( '_', ' ', pods_var_raw( 'label', $taxonomy, pods_var_raw( 'name', $taxonomy ), null, true ) ) ), null, true ) );
567
-
568
- $ct_labels = array();
569
- $ct_labels[ 'name' ] = $ct_label;
570
- $ct_labels[ 'singular_name' ] = $ct_singular;
571
- $ct_labels[ 'menu_name' ] = pods_var_raw( 'menu_name', $taxonomy, '', null, true );
572
- $ct_labels[ 'search_items' ] = pods_var_raw( 'label_search_items', $taxonomy, '', null, true );
573
- $ct_labels[ 'popular_items' ] = pods_var_raw( 'label_popular_items', $taxonomy, '', null, true );
574
- $ct_labels[ 'all_items' ] = pods_var_raw( 'label_all_items', $taxonomy, '', null, true );
575
- $ct_labels[ 'parent_item' ] = pods_var_raw( 'label_parent_item', $taxonomy, '', null, true );
576
- $ct_labels[ 'parent_item_colon' ] = pods_var_raw( 'label_parent_item_colon', $taxonomy, '', null, true );
577
- $ct_labels[ 'edit_item' ] = pods_var_raw( 'label_edit_item', $taxonomy, '', null, true );
578
- $ct_labels[ 'update_item' ] = pods_var_raw( 'label_update_item', $taxonomy, '', null, true );
579
- $ct_labels[ 'add_new_item' ] = pods_var_raw( 'label_add_new_item', $taxonomy, '', null, true );
580
- $ct_labels[ 'new_item_name' ] = pods_var_raw( 'label_new_item_name', $taxonomy, '', null, true );
581
- $ct_labels[ 'separate_items_with_commas' ] = pods_var_raw( 'label_separate_items_with_commas', $taxonomy, '', null, true );
582
- $ct_labels[ 'add_or_remove_items' ] = pods_var_raw( 'label_add_or_remove_items', $taxonomy, '', null, true );
583
- $ct_labels[ 'choose_from_most_used' ] = pods_var_raw( 'label_choose_from_the_most_used', $taxonomy, '', null, true );
584
-
585
- // Rewrite
586
- $ct_rewrite = (boolean) pods_var( 'rewrite', $taxonomy, true );
587
- $ct_rewrite_array = array(
588
- 'slug' => pods_var( 'rewrite_custom_slug', $taxonomy, str_replace( '_', '-', $taxonomy_name ), null, true ),
589
- 'with_front' => (boolean) pods_var( 'rewrite_with_front', $taxonomy, true ),
590
- 'hierarchical' => (boolean) pods_var( 'rewrite_hierarchical', $taxonomy, (boolean) pods_var( 'hierarchical', $taxonomy, false ) )
591
- );
592
-
593
- if ( false !== $ct_rewrite )
594
- $ct_rewrite = $ct_rewrite_array;
595
-
596
- // Register Taxonomy
597
- $pods_taxonomies[ $taxonomy_name ] = array(
598
- 'label' => $ct_label,
599
- 'labels' => $ct_labels,
600
- 'public' => (boolean) pods_var( 'public', $taxonomy, true ),
601
- 'show_in_nav_menus' => (boolean) pods_var( 'show_in_nav_menus', $taxonomy, (boolean) pods_var( 'public', $taxonomy, true ) ),
602
- 'show_ui' => (boolean) pods_var( 'show_ui', $taxonomy, (boolean) pods_var( 'public', $taxonomy, true ) ),
603
- 'show_tagcloud' => (boolean) pods_var( 'show_tagcloud', $taxonomy, (boolean) pods_var( 'show_ui', $taxonomy, (boolean) pods_var( 'public', $taxonomy, true ) ) ),
604
- 'hierarchical' => (boolean) pods_var( 'hierarchical', $taxonomy, false ),
605
- 'update_count_callback' => pods_var( 'update_count_callback', $taxonomy, null, null, true ),
606
- 'query_var' => ( false !== (boolean) pods_var( 'query_var', $taxonomy, true ) ? pods_var( 'query_var_string', $taxonomy, $taxonomy_name, null, true ) : false ),
607
- 'rewrite' => $ct_rewrite,
608
- 'show_admin_column' => (boolean) pods_var( 'show_admin_column', $taxonomy, false ),
609
- 'sort' => (boolean) pods_var( 'sort', $taxonomy, false )
610
- );
611
-
612
- if ( is_array( $ct_rewrite ) && !$pods_taxonomies[ $taxonomy_name ][ 'query_var' ] )
613
- $pods_taxonomies[ $taxonomy_name ]['query_var'] = pods_var( 'query_var_string', $taxonomy, $taxonomy_name, null, true );;
614
-
615
- // Prevent reserved query_var issues
616
- if ( in_array( $pods_taxonomies[ $taxonomy_name ][ 'query_var' ], $reserved_query_vars ) ) {
617
- $pods_taxonomies[ $taxonomy_name ][ 'query_var' ] = 'taxonomy_' . $pods_taxonomies[ $taxonomy_name ][ 'query_var' ];
618
- }
619
-
620
- // Integration for Single Value Taxonomy UI
621
- if ( function_exists( 'tax_single_value_meta_box' ) ) {
622
- $pods_taxonomies[ $taxonomy_name ][ 'single_value' ] = (boolean) pods_var( 'single_value', $taxonomy, false );
623
- $pods_taxonomies[ $taxonomy_name ][ 'required' ] = (boolean) pods_var( 'single_value_required', $taxonomy, false );
624
- }
625
-
626
- // Post Types
627
- $ct_post_types = array();
628
- $_post_types = get_post_types();
629
- $_post_types = array_merge_recursive( $_post_types, $pods_post_types );
630
- $ignore = array( 'revision' );
631
-
632
- foreach ( $_post_types as $post_type => $options ) {
633
- if ( in_array( $post_type, $ignore ) )
634
- continue;
635
-
636
- if ( false !== (boolean) pods_var( 'built_in_post_types_' . $post_type, $taxonomy, false ) ) {
637
- $ct_post_types[] = $post_type;
638
-
639
- if ( isset( $supported_taxonomies[ $post_type ] ) && !in_array( $taxonomy_name, $supported_taxonomies[ $post_type ] ) )
640
- $supported_taxonomies[ $post_type ][] = $taxonomy_name;
641
- }
642
- }
643
-
644
- if ( isset( $supported_post_types[ $taxonomy_name ] ) )
645
- $supported_post_types[ $taxonomy_name ] = array_merge( $supported_post_types[ $taxonomy_name ], $ct_post_types );
646
- else
647
- $supported_post_types[ $taxonomy_name ] = $ct_post_types;
648
- }
649
-
650
- $pods_post_types = apply_filters( 'pods_wp_post_types', $pods_post_types );
651
- $pods_taxonomies = apply_filters( 'pods_wp_taxonomies', $pods_taxonomies );
652
-
653
- $supported_post_types = apply_filters( 'pods_wp_supported_post_types', $supported_post_types );
654
- $supported_taxonomies = apply_filters( 'pods_wp_supported_taxonomies', $supported_taxonomies );
655
-
656
- foreach ( $pods_taxonomies as $taxonomy => $options ) {
657
- $ct_post_types = null;
658
-
659
- if ( isset( $supported_post_types[ $taxonomy ] ) && !empty( $supported_post_types[ $taxonomy ] ) )
660
- $ct_post_types = $supported_post_types[ $taxonomy ];
661
-
662
- $pods_cpt_ct[ 'taxonomies' ][ $taxonomy ] = array(
663
- 'post_types' => $ct_post_types,
664
- 'options' => $options
665
- );
666
- }
667
-
668
- foreach ( $pods_post_types as $post_type => $options ) {
669
- if ( isset( $supported_taxonomies[ $post_type ] ) && !empty( $supported_taxonomies[ $post_type ] ) )
670
- $options[ 'taxonomies' ] = $supported_taxonomies[ $post_type ];
671
-
672
- $pods_cpt_ct[ 'post_types' ][ $post_type ] = $options;
673
- }
674
-
675
- pods_transient_set( 'pods_wp_cpt_ct', $pods_cpt_ct );
676
- }
677
-
678
- foreach ( $pods_cpt_ct[ 'taxonomies' ] as $taxonomy => $options ) {
679
- if ( isset( self::$content_types_registered[ 'taxonomies' ] ) && in_array( $taxonomy, self::$content_types_registered[ 'taxonomies' ] ) )
680
- continue;
681
-
682
- $ct_post_types = $options[ 'post_types' ];
683
- $options = $options[ 'options' ];
684
-
685
- $options = apply_filters( 'pods_register_taxonomy_' . $taxonomy, $options, $taxonomy );
686
- $options = apply_filters( 'pods_register_taxonomy', $options, $taxonomy );
687
-
688
- $options = self::object_label_fix( $options, 'taxonomy' );
689
-
690
- // Max length for taxonomies are 32 characters
691
- $taxonomy = substr( $taxonomy, 0, 32 );
692
-
693
- // i18n compatibility for plugins that override it
694
- if ( is_array( $options[ 'rewrite' ] ) && isset( $options[ 'rewrite' ][ 'slug' ] ) && !empty( $options[ 'rewrite' ][ 'slug' ] ) )
695
- $options[ 'rewrite' ][ 'slug' ] = _x( $options[ 'rewrite' ][ 'slug' ], 'URL taxonomy slug', 'pods' );
696
-
697
- if ( 1 == pods_var( 'pods_debug_register', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) )
698
- pods_debug( array( $taxonomy, $ct_post_types, $options ) );
699
-
700
- register_taxonomy( $taxonomy, $ct_post_types, $options );
701
-
702
- if ( !isset( self::$content_types_registered[ 'taxonomies' ] ) )
703
- self::$content_types_registered[ 'taxonomies' ] = array();
704
-
705
- self::$content_types_registered[ 'taxonomies' ][] = $taxonomy;
706
- }
707
-
708
- foreach ( $pods_cpt_ct[ 'post_types' ] as $post_type => $options ) {
709
- if ( isset( self::$content_types_registered[ 'post_types' ] ) && in_array( $post_type, self::$content_types_registered[ 'post_types' ] ) )
710
- continue;
711
-
712
- $options = apply_filters( 'pods_register_post_type_' . $post_type, $options, $post_type );
713
- $options = apply_filters( 'pods_register_post_type', $options, $post_type );
714
-
715
- $options = self::object_label_fix( $options, 'post_type' );
716
-
717
- // Max length for post types are 20 characters
718
- $post_type = substr( $post_type, 0, 20 );
719
-
720
- // i18n compatibility for plugins that override it
721
- if ( is_array( $options[ 'rewrite' ] ) && isset( $options[ 'rewrite' ][ 'slug' ] ) && !empty( $options[ 'rewrite' ][ 'slug' ] ) )
722
- $options[ 'rewrite' ][ 'slug' ] = _x( $options[ 'rewrite' ][ 'slug' ], 'URL slug', 'pods' );
723
-
724
- if ( 1 == pods_var( 'pods_debug_register', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) )
725
- pods_debug( array( $post_type, $options ) );
726
-
727
- register_post_type( $post_type, $options );
728
-
729
- if ( !isset( self::$content_types_registered[ 'post_types' ] ) )
730
- self::$content_types_registered[ 'post_types' ] = array();
731
-
732
- self::$content_types_registered[ 'post_types' ][] = $post_type;
733
- }
734
-
735
- }
736
-
737
- /**
738
- * Check if we need to flush WordPress rewrite rules
739
- * This gets run during 'init' action late in the game to give other plugins time to register their rewrite rules
740
- *
741
- */
742
- public function flush_rewrite_rules() {
743
- $flush = pods_transient_get( 'pods_flush_rewrites' );
744
-
745
- if ( 1 == $flush ) {
746
- global $wp_rewrite;
747
- $wp_rewrite->flush_rules();
748
- $wp_rewrite->init();
749
-
750
- pods_transient_set( 'pods_flush_rewrites', 0 );
751
- }
752
- }
753
-
754
- /**
755
- * Update Post Type messages
756
- *
757
- * @param array $messages
758
- *
759
- * @return array
760
- * @since 2.0.2
761
- */
762
- public function setup_updated_messages ( $messages ) {
763
- global $post, $post_ID;
764
-
765
- $post_types = PodsMeta::$post_types;
766
- $existing_post_types = get_post_types();
767
-
768
- $pods_cpt_ct = pods_transient_get( 'pods_wp_cpt_ct' );
769
-
770
- if ( empty( $pods_cpt_ct ) || empty( $post_types ) )
771
- return $messages;
772
-
773
- foreach ( $post_types as $post_type ) {
774
- if ( !isset( $pods_cpt_ct[ 'post_types' ][ $post_type[ 'name' ] ] ) )
775
- continue;
776
-
777
- $labels = self::object_label_fix( $pods_cpt_ct[ 'post_types' ][ $post_type[ 'name' ] ], 'post_type' );
778
- $labels = $labels[ 'labels' ];
779
-
780
- $messages[ $post_type[ 'name' ] ] = array(
781
- 1 => sprintf( __( '%s updated. <a href="%s">%s</a>', 'pods' ), $labels[ 'singular_name' ], esc_url( get_permalink( $post_ID ) ), $labels[ 'view_item' ] ),
782
- 2 => __( 'Custom field updated.', 'pods' ),
783
- 3 => __( 'Custom field deleted.', 'pods' ),
784
- 4 => sprintf( __( '%s updated.', 'pods' ), $labels[ 'singular_name' ] ),
785
- /* translators: %s: date and time of the revision */
786
- 5 => isset( $_GET[ 'revision' ] ) ? sprintf( __( '%s restored to revision from %s', 'pods' ), $labels[ 'singular_name' ], wp_post_revision_title( (int) $_GET[ 'revision' ], false ) ) : false,
787
- 6 => sprintf( __( '%s published. <a href="%s">%s</a>', 'pods' ), $labels[ 'singular_name' ], esc_url( get_permalink( $post_ID ) ), $labels[ 'view_item' ] ),
788
- 7 => sprintf( __( '%s saved.', 'pods' ), $labels[ 'singular_name' ] ),
789
- 8 => sprintf( __( '%s submitted. <a target="_blank" href="%s">Preview %s</a>', 'pods' ), $labels[ 'singular_name' ], esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ), $labels[ 'singular_name' ] ),
790
- 9 => sprintf( __( '%s scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview %s</a>', 'pods' ),
791
- $labels[ 'singular_name' ],
792
- // translators: Publish box date format, see http://php.net/date
793
- date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ),
794
- esc_url( get_permalink( $post_ID ) ),
795
- $labels[ 'singular_name' ]
796
- ),
797
- 10 => sprintf( __( '%s draft updated. <a target="_blank" href="%s">Preview %s</a>', 'pods' ), $labels[ 'singular_name' ], esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ), $labels[ 'singular_name' ] )
798
- );
799
-
800
- if ( false === (boolean) $pods_cpt_ct[ 'post_types' ][ $post_type[ 'name' ] ][ 'public' ] ) {
801
- $messages[ $post_type[ 'name' ] ][ 1 ] = sprintf( __( '%s updated.', 'pods' ), $labels[ 'singular_name' ] );
802
- $messages[ $post_type[ 'name' ] ][ 6 ] = sprintf( __( '%s published.', 'pods' ), $labels[ 'singular_name' ] );
803
- $messages[ $post_type[ 'name' ] ][ 8 ] = sprintf( __( '%s submitted.', 'pods' ), $labels[ 'singular_name' ] );
804
- $messages[ $post_type[ 'name' ] ][ 9 ] = sprintf( __( '%s scheduled for: <strong>%1$s</strong>.', 'pods' ),
805
- $labels[ 'singular_name' ],
806
- // translators: Publish box date format, see http://php.net/date
807
- date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) )
808
- );
809
- $messages[ $post_type[ 'name' ] ][ 10 ] = sprintf( __( '%s draft updated.', 'pods' ), $labels[ 'singular_name' ] );
810
- }
811
- }
812
-
813
- return $messages;
814
- }
815
-
816
- /**
817
- * @param $args
818
- * @param string $type
819
- *
820
- * @return array
821
- */
822
- public static function object_label_fix ( $args, $type = 'post_type' ) {
823
- if ( empty( $args ) || !is_array( $args ) )
824
- $args = array();
825
-
826
- if ( !isset( $args[ 'labels' ] ) || !is_array( $args[ 'labels' ] ) )
827
- $args[ 'labels' ] = array();
828
-
829
- $label = pods_var_raw( 'name', $args[ 'labels' ], pods_var_raw( 'label', $args, __( 'Items', 'pods' ), null, true ), null, true );
830
- $singular_label = pods_var_raw( 'singular_name', $args[ 'labels' ], pods_var_raw( 'label_singular', $args, __( 'Item', 'pods' ), null, true ), null, true );
831
-
832
- $labels = $args[ 'labels' ];
833
-
834
- $labels[ 'name' ] = $label;
835
- $labels[ 'singular_name' ] = $singular_label;
836
-
837
- if ( 'post_type' == $type ) {
838
- $labels[ 'menu_name' ] = pods_var_raw( 'menu_name', $labels, $label, null, true );
839
- $labels[ 'add_new' ] = pods_var_raw( 'add_new', $labels, __( 'Add New', 'pods' ), null, true );
840
- $labels[ 'add_new_item' ] = pods_var_raw( 'add_new_item', $labels, sprintf( __( 'Add New %s', 'pods' ), $singular_label ), null, true );
841
- $labels[ 'new_item' ] = pods_var_raw( 'new_item', $labels, sprintf( __( 'New %s', 'pods' ), $singular_label ), null, true );
842
- $labels[ 'edit' ] = pods_var_raw( 'edit', $labels, __( 'Edit', 'pods' ), null, true );
843
- $labels[ 'edit_item' ] = pods_var_raw( 'edit_item', $labels, sprintf( __( 'Edit %s', 'pods' ), $singular_label ), null, true );
844
- $labels[ 'view' ] = pods_var_raw( 'view', $labels, sprintf( __( 'View %s', 'pods' ), $singular_label ), null, true );
845
- $labels[ 'view_item' ] = pods_var_raw( 'view_item', $labels, sprintf( __( 'View %s', 'pods' ), $singular_label ), null, true );
846
- $labels[ 'all_items' ] = pods_var_raw( 'all_items', $labels, sprintf( __( 'All %s', 'pods' ), $label ), null, true );
847
- $labels[ 'search_items' ] = pods_var_raw( 'search_items', $labels, sprintf( __( 'Search %s', 'pods' ), $label ), null, true );
848
- $labels[ 'not_found' ] = pods_var_raw( 'not_found', $labels, sprintf( __( 'No %s Found', 'pods' ), $label ), null, true );
849
- $labels[ 'not_found_in_trash' ] = pods_var_raw( 'not_found_in_trash', $labels, sprintf( __( 'No %s Found in Trash', 'pods' ), $label ), null, true );
850
- $labels[ 'parent' ] = pods_var_raw( 'parent', $labels, sprintf( __( 'Parent %s', 'pods' ), $singular_label ), null, true );
851
- $labels[ 'parent_item_colon' ] = pods_var_raw( 'parent_item_colon', $labels, sprintf( __( 'Parent %s:', 'pods' ), $singular_label ), null, true );
852
- }
853
- elseif ( 'taxonomy' == $type ) {
854
- $labels[ 'menu_name' ] = pods_var_raw( 'menu_name', $labels, $label, null, true );
855
- $labels[ 'search_items' ] = pods_var_raw( 'search_items', $labels, sprintf( __( 'Search %s', 'pods' ), $label ), null, true );
856
- $labels[ 'popular_items' ] = pods_var_raw( 'popular_items', $labels, sprintf( __( 'Popular %s', 'pods' ), $label ), null, true );
857
- $labels[ 'all_items' ] = pods_var_raw( 'all_items', $labels, sprintf( __( 'All %s', 'pods' ), $label ), null, true );
858
- $labels[ 'parent_item' ] = pods_var_raw( 'parent_item', $labels, sprintf( __( 'Parent %s', 'pods' ), $singular_label ), null, true );
859
- $labels[ 'parent_item_colon' ] = pods_var_raw( 'parent_item_colon', $labels, sprintf( __( 'Parent %s :', 'pods' ), $singular_label ), null, true );
860
- $labels[ 'edit_item' ] = pods_var_raw( 'edit_item', $labels, sprintf( __( 'Edit %s', 'pods' ), $singular_label ), null, true );
861
- $labels[ 'update_item' ] = pods_var_raw( 'update_item', $labels, sprintf( __( 'Update %s', 'pods' ), $singular_label ), null, true );
862
- $labels[ 'add_new_item' ] = pods_var_raw( 'add_new_item', $labels, sprintf( __( 'Add New %s', 'pods' ), $singular_label ), null, true );
863
- $labels[ 'new_item_name' ] = pods_var_raw( 'new_item_name', $labels, sprintf( __( 'New %s Name', 'pods' ), $singular_label ), null, true );
864
- $labels[ 'separate_items_with_commas' ] = pods_var_raw( 'separate_items_with_commas', $labels, sprintf( __( 'Separate %s with commas', 'pods' ), $label ), null, true );
865
- $labels[ 'add_or_remove_items' ] = pods_var_raw( 'add_or_remove_items', $labels, sprintf( __( 'Add or remove %s', 'pods' ), $label ), null, true );
866
- $labels[ 'choose_from_most_used' ] = pods_var_raw( 'choose_from_most_used', $labels, sprintf( __( 'Choose from the most used %s', 'pods' ), $label ), null, true );
867
- }
868
-
869
- $args[ 'labels' ] = $labels;
870
-
871
- return $args;
872
- }
873
-
874
- /**
875
- * Activate and Install
876
- */
877
- public function activate_install () {
878
- register_activation_hook( PODS_DIR . 'init.php', array( $this, 'activate' ) );
879
- register_deactivation_hook( PODS_DIR . 'init.php', array( $this, 'deactivate' ) );
880
-
881
- add_action( 'wpmu_new_blog', array( $this, 'new_blog' ), 10, 6 );
882
-
883
- if ( empty( self::$version ) || version_compare( self::$version, PODS_VERSION, '<' ) || version_compare( self::$version, PODS_DB_VERSION, '<=' ) || self::$upgrade_needed )
884
- $this->setup();
885
- elseif ( self::$version != PODS_VERSION ) {
886
- delete_option( 'pods_framework_version' );
887
- add_option( 'pods_framework_version', PODS_VERSION, '', 'yes' );
888
-
889
- pods_api()->cache_flush_pods();
890
- }
891
- }
892
-
893
- /**
894
- *
895
- */
896
- public function activate () {
897
- global $wpdb;
898
-
899
- if ( function_exists( 'is_multisite' ) && is_multisite() && isset( $_GET[ 'networkwide' ] ) && 1 == $_GET[ 'networkwide' ] ) {
900
- $_blog_ids = $wpdb->get_col( "SELECT `blog_id` FROM `{$wpdb->blogs}`" );
901
-
902
- foreach ( $_blog_ids as $_blog_id ) {
903
- $this->setup( $_blog_id );
904
- }
905
- }
906
- else
907
- $this->setup();
908
- }
909
-
910
- /**
911
- *
912
- */
913
- public function deactivate () {
914
- pods_api()->cache_flush_pods();
915
- }
916
-
917
- /**
918
- *
919
- */
920
- public function needs_upgrade ( $current = null, $last = null ) {
921
- if ( null === $current )
922
- $current = self::$version;
923
-
924
- if ( null === $last )
925
- $last = self::$version_last;
926
-
927
- $upgrade_needed = false;
928
-
929
- if ( !empty( $current ) ) {
930
- foreach ( self::$upgrades as $old_version => $new_version ) {
931
- /*if ( '2.1.0' == $new_version && ( is_developer() ) )
932
- continue;*/
933
-
934
- if ( version_compare( $last, $old_version, '>=' )
935
- && version_compare( $last, $new_version, '<' )
936
- && version_compare( $current, $new_version, '>=' )
937
- && 1 != self::$upgraded
938
- ) {
939
- $upgrade_needed = true;
940
-
941
- break;
942
- }
943
- }
944
- }
945
-
946
- return $upgrade_needed;
947
- }
948
-
949
- /**
950
- * @param $_blog_id
951
- * @param $user_id
952
- * @param $domain
953
- * @param $path
954
- * @param $site_id
955
- * @param $meta
956
- */
957
- public function new_blog ( $_blog_id, $user_id, $domain, $path, $site_id, $meta ) {
958
- if ( function_exists( 'is_multisite' ) && is_multisite() && is_plugin_active_for_network( basename( PODS_DIR ) . '/init.php' ) )
959
- $this->setup( $_blog_id );
960
- }
961
-
962
- /**
963
- * @param null $_blog_id
964
- */
965
- public function setup ( $_blog_id = null ) {
966
- global $wpdb;
967
-
968
- // Switch DB table prefixes
969
- if ( null !== $_blog_id && $_blog_id != $wpdb->blogid )
970
- switch_to_blog( pods_absint( $_blog_id ) );
971
- else
972
- $_blog_id = null;
973
-
974
- // Setup DB tables
975
- $pods_version = get_option( 'pods_framework_version' );
976
- $pods_version_last = get_option( 'pods_framework_version_last' );
977
-
978
- // Install Pods
979
- if ( empty( $pods_version ) ) {
980
- pods_upgrade()->install( $_blog_id );
981
-
982
- $old_version = get_option( 'pods_version' );
983
-
984
- if ( !empty( $old_version ) ) {
985
- if ( false === strpos( $old_version, '.' ) )
986
- $old_version = pods_version_to_point( $old_version );
987
-
988
- delete_option( 'pods_framework_version_last' );
989
- add_option( 'pods_framework_version_last', $pods_version, '', 'yes' );
990
-
991
- self::$version_last = $old_version;
992
- }
993
- }
994
- // Upgrade Wizard needed
995
- elseif ( $this->needs_upgrade( $pods_version, $pods_version_last ) ) {
996
- // Do not do anything
997
- return;
998
- }
999
- // Update Pods and run any required DB updates
1000
- elseif ( version_compare( $pods_version, PODS_VERSION, '<=' ) ) {
1001
- if ( false !== apply_filters( 'pods_update_run', null, PODS_VERSION, $pods_version, $_blog_id ) && !isset( $_GET[ 'pods_bypass_update' ] ) ) {
1002
- do_action( 'pods_update', PODS_VERSION, $pods_version, $_blog_id );
1003
-
1004
- // Update 2.0 alpha / beta sites
1005
- if ( version_compare( '2.0.0-a-1', $pods_version, '<=' ) && version_compare( $pods_version, '2.0.0-b-15', '<=' ) )
1006
- include( PODS_DIR . 'sql/update-2.0-beta.php' );
1007
-
1008
- if ( version_compare( $pods_version, PODS_DB_VERSION, '<=' ) )
1009
- include( PODS_DIR . 'sql/update.php' );
1010
-
1011
- do_action( 'pods_update_post', PODS_VERSION, $pods_version, $_blog_id );
1012
- }
1013
-
1014
- delete_option( 'pods_framework_version_last' );
1015
- add_option( 'pods_framework_version_last', $pods_version, '', 'yes' );
1016
-
1017
- self::$version_last = $pods_version;
1018
- }
1019
-
1020
- delete_option( 'pods_framework_version' );
1021
- add_option( 'pods_framework_version', PODS_VERSION, '', 'yes' );
1022
-
1023
- delete_option( 'pods_framework_db_version' );
1024
- add_option( 'pods_framework_db_version', PODS_DB_VERSION, '', 'yes' );
1025
-
1026
- pods_api()->cache_flush_pods();
1027
-
1028
- // Restore DB table prefix (if switched)
1029
- if ( null !== $_blog_id )
1030
- restore_current_blog();
1031
- else {
1032
- $this->run();
1033
- }
1034
- }
1035
-
1036
- /**
1037
- * @param null $_blog_id
1038
- */
1039
- public function reset ( $_blog_id = null ) {
1040
- global $wpdb;
1041
-
1042
- // Switch DB table prefixes
1043
- if ( null !== $_blog_id && $_blog_id != $wpdb->blogid )
1044
- switch_to_blog( pods_absint( $_blog_id ) );
1045
- else
1046
- $_blog_id = null;
1047
-
1048
- $api = pods_api();
1049
-
1050
- $pods = $api->load_pods( array( 'names_ids' => true ) );
1051
-
1052
- foreach ( $pods as $pod_id => $pod_label ) {
1053
- $api->delete_pod( array( 'id' => $pod_id ) );
1054
- }
1055
-
1056
- $templates = $api->load_templates();
1057
-
1058
- foreach ( $templates as $template ) {
1059
- $api->delete_template( array( 'id' => $template[ 'id' ] ) );
1060
- }
1061
-
1062
- $pages = $api->load_pages();
1063
-
1064
- foreach ( $pages as $page ) {
1065
- $api->delete_page( array( 'id' => $page[ 'id' ] ) );
1066
- }
1067
-
1068
- $helpers = $api->load_helpers();
1069
-
1070
- foreach ( $helpers as $helper ) {
1071
- $api->delete_helper( array( 'id' => $helper[ 'id' ] ) );
1072
- }
1073
-
1074
- $tables = $wpdb->get_results( "SHOW TABLES LIKE '{$wpdb->prefix}pods%'", ARRAY_N );
1075
-
1076
- if ( !empty( $tables ) ) {
1077
- foreach ( $tables as $table ) {
1078
- $table = $table[ 0 ];
1079
-
1080
- pods_query( "DROP TABLE `{$table}`", false );
1081
- }
1082
- }
1083
-
1084
- // Remove any orphans
1085
- $wpdb->query( "
1086
- DELETE `p`, `pm`
1087
- FROM `{$wpdb->posts}` AS `p`
1088
- LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1089
- ON `pm`.`post_id` = `p`.`ID`
1090
- WHERE
1091
- `p`.`post_type` LIKE '_pods_%'
1092
- " );
1093
-
1094
- delete_option( 'pods_framework_version' );
1095
- delete_option( 'pods_framework_db_version' );
1096
- delete_option( 'pods_framework_upgrade_2_0' );
1097
- delete_option( 'pods_framework_upgraded_1_x' );
1098
-
1099
- // @todo Make sure all entries are being cleaned and do something about the pods_framework_upgrade_{version} dynamic entries created by PodsUpgrade
1100
- delete_option( 'pods_framework_upgrade_2_0_0' );
1101
- delete_option( 'pods_framework_upgrade_2_0_sister_ids' );
1102
- delete_option( 'pods_framework_version_last' );
1103
-
1104
- delete_option( 'pods_component_settings' );
1105
-
1106
- $api->cache_flush_pods();
1107
-
1108
- pods_transient_clear( 'pods_flush_rewrites' );
1109
-
1110
- self::$version = '';
1111
-
1112
- // Restore DB table prefix (if switched)
1113
- if ( null !== $_blog_id )
1114
- restore_current_blog();
1115
- }
1116
-
1117
- public function run () {
1118
-
1119
- if ( ! did_action( 'plugins_loaded' ) ) {
1120
- add_action( 'plugins_loaded', array( $this, 'load_components' ), 11 );
1121
- }
1122
- else {
1123
- $this->load_components();
1124
- }
1125
-
1126
- if ( ! did_action( 'setup_theme' ) ) {
1127
- add_action( 'setup_theme', array( $this, 'load_meta' ), 14 );
1128
- }
1129
- else {
1130
- $this->load_meta();
1131
- }
1132
-
1133
- if ( ! did_action( 'init' ) ) {
1134
- add_action( 'init', array( $this, 'core' ), 11 );
1135
- add_action( 'init', array( $this, 'setup_content_types' ), 11 );
1136
-
1137
- if ( is_admin() ) {
1138
- add_action( 'init', array( $this, 'admin_init' ), 12 );
1139
- }
1140
- }
1141
- else {
1142
- $this->core();
1143
- $this->setup_content_types();
1144
-
1145
- if ( is_admin() ) {
1146
- $this->admin_init();
1147
- }
1148
- }
1149
-
1150
- add_action( 'wp_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1151
- add_action( 'admin_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1152
- add_action( 'login_enqueue_scripts', array( $this, 'register_assets' ), 15 );
1153
-
1154
- add_filter( 'post_updated_messages', array( $this, 'setup_updated_messages' ), 10, 1 );
1155
- add_action( 'delete_attachment', array( $this, 'delete_attachment' ) );
1156
-
1157
- // Register widgets
1158
- add_action( 'widgets_init', array( $this, 'register_widgets' ) );
1159
-
1160
- // Show admin bar links
1161
- add_action( 'admin_bar_menu', array( $this, 'admin_bar_links' ), 81 );
1162
-
1163
- }
1164
-
1165
- /**
1166
- * Delete Attachments from relationships
1167
- *
1168
- * @param int $_ID
1169
- */
1170
- public function delete_attachment ( $_ID ) {
1171
- global $wpdb;
1172
-
1173
- $_ID = (int) $_ID;
1174
-
1175
- do_action( 'pods_delete_attachment', $_ID );
1176
-
1177
- $file_types = "'" . implode( "', '", PodsForm::file_field_types() ) . "'";
1178
-
1179
- if ( !pods_tableless() ) {
1180
- $sql = "
1181
- DELETE `rel`
1182
- FROM `@wp_podsrel` AS `rel`
1183
- LEFT JOIN `{$wpdb->posts}` AS `p`
1184
- ON
1185
- `p`.`post_type` = '_pods_field'
1186
- AND ( `p`.`ID` = `rel`.`field_id` OR `p`.`ID` = `rel`.`related_field_id` )
1187
- LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1188
- ON
1189
- `pm`.`post_id` = `p`.`ID`
1190
- AND `pm`.`meta_key` = 'type'
1191
- AND `pm`.`meta_value` IN ( {$file_types} )
1192
- WHERE
1193
- `p`.`ID` IS NOT NULL
1194
- AND `pm`.`meta_id` IS NOT NULL
1195
- AND `rel`.`item_id` = {$_ID}";
1196
-
1197
- pods_query( $sql, false );
1198
- }
1199
-
1200
- // Post Meta
1201
- if ( !empty( PodsMeta::$post_types ) ) {
1202
- $sql = "
1203
- DELETE `rel`
1204
- FROM `@wp_postmeta` AS `rel`
1205
- LEFT JOIN `{$wpdb->posts}` AS `p`
1206
- ON
1207
- `p`.`post_type` = '_pods_field'
1208
- LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1209
- ON
1210
- `pm`.`post_id` = `p`.`ID`
1211
- AND `pm`.`meta_key` = 'type'
1212
- AND `pm`.`meta_value` IN ( {$file_types} )
1213
- WHERE
1214
- `p`.`ID` IS NOT NULL
1215
- AND `pm`.`meta_id` IS NOT NULL
1216
- AND `rel`.`meta_key` = `p`.`post_name`
1217
- AND `rel`.`meta_value` = '{$_ID}'";
1218
-
1219
- pods_query( $sql, false );
1220
- }
1221
-
1222
- // User Meta
1223
- if ( !empty( PodsMeta::$user ) ) {
1224
- $sql = "
1225
- DELETE `rel`
1226
- FROM `@wp_usermeta` AS `rel`
1227
- LEFT JOIN `{$wpdb->posts}` AS `p`
1228
- ON
1229
- `p`.`post_type` = '_pods_field'
1230
- LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1231
- ON
1232
- `pm`.`post_id` = `p`.`ID`
1233
- AND `pm`.`meta_key` = 'type'
1234
- AND `pm`.`meta_value` IN ( {$file_types} )
1235
- WHERE
1236
- `p`.`ID` IS NOT NULL
1237
- AND `pm`.`meta_id` IS NOT NULL
1238
- AND `rel`.`meta_key` = `p`.`post_name`
1239
- AND `rel`.`meta_value` = '{$_ID}'";
1240
-
1241
- pods_query( $sql, false );
1242
- }
1243
-
1244
- // Comment Meta
1245
- if ( !empty( PodsMeta::$comment ) ) {
1246
- $sql = "
1247
- DELETE `rel`
1248
- FROM `@wp_commentmeta` AS `rel`
1249
- LEFT JOIN `{$wpdb->posts}` AS `p`
1250
- ON
1251
- `p`.`post_type` = '_pods_field'
1252
- LEFT JOIN `{$wpdb->postmeta}` AS `pm`
1253
- ON
1254
- `pm`.`post_id` = `p`.`ID`
1255
- AND `pm`.`meta_key` = 'type'
1256
- AND `pm`.`meta_value` IN ( {$file_types} )
1257
- WHERE
1258
- `p`.`ID` IS NOT NULL
1259
- AND `pm`.`meta_id` IS NOT NULL
1260
- AND `rel`.`meta_key` = `p`.`post_name`
1261
- AND `rel`.`meta_value` = '{$_ID}'";
1262
-
1263
- pods_query( $sql, false );
1264
- }
1265
- }
1266
-
1267
- /**
1268
- * Register widgets for Pods
1269
- */
1270
- public function register_widgets () {
1271
- $widgets = array(
1272
- 'PodsWidgetSingle',
1273
- 'PodsWidgetList',
1274
- 'PodsWidgetField',
1275
- 'PodsWidgetForm',
1276
- 'PodsWidgetView'
1277
- );
1278
-
1279
- foreach ( $widgets as $widget ) {
1280
- if ( !file_exists( PODS_DIR . 'classes/widgets/' . $widget . '.php' ) )
1281
- continue;
1282
-
1283
- require_once PODS_DIR . 'classes/widgets/' . $widget . '.php';
1284
-
1285
- register_widget( $widget );
1286
- }
1287
- }
1288
-
1289
- /**
1290
- * Add Admin Bar links
1291
- */
1292
- public function admin_bar_links () {
1293
- global $wp_admin_bar, $pods;
1294
-
1295
- if ( !is_user_logged_in() || !is_admin_bar_showing() )
1296
- return;
1297
-
1298
- $all_pods = pods_api()->load_pods( array( 'type' => 'pod', 'fields' => false ) );
1299
-
1300
- // Add New item links for all pods
1301
- foreach ( $all_pods as $pod ) {
1302
- if ( 0 == $pod[ 'options' ][ 'show_in_menu' ] )
1303
- continue;
1304
-
1305
- if ( !pods_is_admin( array( 'pods', 'pods_content', 'pods_add_' . $pod[ 'name' ] ) ) )
1306
- continue;
1307
-
1308
- $singular_label = pods_var_raw( 'label_singular', $pod[ 'options' ], pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod[ 'name' ] ) ), null, true ), null, true );
1309
-
1310
- $wp_admin_bar->add_node( array(
1311
- 'id' => 'new-pod-' . $pod[ 'name' ],
1312
- 'title' => $singular_label,
1313
- 'parent' => 'new-content',
1314
- 'href' => admin_url( 'admin.php?page=pods-manage-' . $pod[ 'name' ] . '&action=add' )
1315
- ) );
1316
- }
1317
-
1318
- // Add edit link if we're on a pods page
1319
- if ( is_object( $pods ) && !is_wp_error( $pods ) && !empty( $pods->id ) && isset( $pods->pod_data ) && !empty( $pods->pod_data ) && 'pod' == $pods->pod_data[ 'type' ] ) {
1320
- $pod = $pods->pod_data;
1321
-
1322
- if ( pods_is_admin( array( 'pods', 'pods_content', 'pods_edit_' . $pod[ 'name' ] ) ) ) {
1323
- $singular_label = pods_var_raw( 'label_singular', $pod[ 'options' ], pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod[ 'name' ] ) ), null, true ), null, true );
1324
-
1325
- $wp_admin_bar->add_node( array(
1326
- 'title' => sprintf( __( 'Edit %s', 'pods' ), $singular_label ),
1327
- 'id' => 'edit-pod',
1328
- 'href' => admin_url( 'admin.php?page=pods-manage-' . $pod[ 'name' ] . '&action=edit&id=' . $pods->id() )
1329
- ) );
1330
- }
1331
- }
1332
-
1333
- }
1334
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
classes/PodsMeta.php DELETED
@@ -1,2867 +0,0 @@
1
- <?php
2
- /**
3
- * @package Pods
4
- */
5
- class PodsMeta {
6
-
7
- /**
8
- * @var PodsMeta
9
- */
10
- static $instance = null;
11
-
12
- /**
13
- * @var PodsAPI
14
- */
15
- private $api;
16
-
17
- /**
18
- * @var Pods
19
- */
20
- private static $current_pod;
21
-
22
- /**
23
- * @var array
24
- */
25
- private static $current_pod_data;
26
-
27
- /**
28
- * @var Pods
29
- */
30
- private static $current_field_pod;
31
-
32
- /**
33
- * @var int
34
- */
35
- public static $object_identifier = -1;
36
-
37
- /**
38
- * @var array
39
- */
40
- public static $advanced_content_types = array();
41
-
42
- /**
43
- * @var array
44
- */
45
- public static $post_types = array();
46
-
47
- /**
48
- * @var array
49
- */
50
- public static $taxonomies = array();
51
-
52
- /**
53
- * @var array
54
- */
55
- public static $media = array();
56
-
57
- /**
58
- * @var array
59
- */
60
- public static $user = array();
61
-
62
- /**
63
- * @var array
64
- */
65
- public static $comment = array();
66
-
67
- /**
68
- * @var array
69
- */
70
- public static $settings = array();
71
-
72
- /**
73
- * @var array
74
- */
75
- public static $queue = array();
76
-
77
- /**
78
- * @var array
79
- */
80
- public static $groups = array();
81
-
82
- /**
83
- * @var string
84
- */
85
- public static $old_post_status = '';
86
-
87
- /**
88
- * Singleton handling for a basic pods_meta() request
89
- *
90
- * @return \PodsMeta
91
- *
92
- * @since 2.3.5
93
- */
94
- public static function init () {
95
- if ( !is_object( self::$instance ) )
96
- self::$instance = new PodsMeta();
97
-
98
- return self::$instance;
99
- }
100
-
101
- /**
102
- * @return \PodsMeta
103
- *
104
- * @since 2.0
105
- */
106
- function __construct () {
107
-
108
- }
109
-
110
- /**
111
- * @return \PodsMeta
112
- */
113
- public function core () {
114
- self::$advanced_content_types = pods_api()->load_pods( array( 'type' => 'pod' ) );
115
- self::$post_types = pods_api()->load_pods( array( 'type' => 'post_type' ) );
116
- self::$taxonomies = pods_api()->load_pods( array( 'type' => 'taxonomy' ) );
117
- self::$media = pods_api()->load_pods( array( 'type' => 'media' ) );
118
- self::$user = pods_api()->load_pods( array( 'type' => 'user' ) );
119
- self::$comment = pods_api()->load_pods( array( 'type' => 'comment' ) );
120
- self::$settings = pods_api()->load_pods( array( 'type' => 'settings' ) );
121
-
122
- // Handle Post Type Editor (needed for Pods core)
123
-
124
- // Loop through and add meta boxes for individual types (can't use this, Tabify doesn't pick it up)
125
- /*
126
- foreach ( self::$post_types as $post_type ) {
127
- $post_type_name = $post_type[ 'name' ];
128
-
129
- if ( !empty( $post_type[ 'object' ] ) )
130
- $post_type_name = $post_type[ 'object' ];
131
-
132
- add_action( 'add_meta_boxes_' . $post_type_name, array( $this, 'meta_post_add' ) );
133
- }
134
- */
135
-
136
- add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
137
- add_action( 'transition_post_status', array( $this, 'save_post_detect_new' ), 10, 3 );
138
- add_action( 'save_post', array( $this, 'save_post' ), 10, 3 );
139
-
140
- if ( apply_filters( 'pods_meta_handler', true, 'post' ) ) {
141
- // Handle *_post_meta
142
- if ( apply_filters( 'pods_meta_handler_get', true, 'post' ) ) {
143
- add_filter( 'get_post_metadata', array( $this, 'get_post_meta' ), 10, 4 );
144
- }
145
-
146
- if ( !pods_tableless() ) {
147
- add_filter( 'add_post_metadata', array( $this, 'add_post_meta' ), 10, 5 );
148
- add_filter( 'update_post_metadata', array( $this, 'update_post_meta' ), 10, 5 );
149
- add_filter( 'delete_post_metadata', array( $this, 'delete_post_meta' ), 10, 5 );
150
- }
151
- }
152
-
153
- add_action( 'delete_post', array( $this, 'delete_post' ), 10, 1 );
154
-
155
- if ( !empty( self::$taxonomies ) ) {
156
- $has_fields = false;
157
-
158
- // Handle Taxonomy Editor
159
- foreach ( self::$taxonomies as $taxonomy ) {
160
- if ( empty( $taxonomy[ 'fields' ] ) ) {
161
- continue;
162
- }
163
-
164
- $has_fields = true;
165
-
166
- $taxonomy_name = $taxonomy[ 'name' ];
167
- if ( !empty( $taxonomy[ 'object' ] ) )
168
- $taxonomy_name = $taxonomy[ 'object' ];
169
-
170
- add_action( $taxonomy_name . '_edit_form_fields', array( $this, 'meta_taxonomy' ), 10, 2 );
171
- add_action( $taxonomy_name . '_add_form_fields', array( $this, 'meta_taxonomy' ), 10, 1 );
172
- }
173
-
174
- if ( $has_fields ) {
175
- // Handle Term Editor
176
- add_action( 'edited_term', array( $this, 'save_taxonomy' ), 10, 3 );
177
- add_action( 'create_term', array( $this, 'save_taxonomy' ), 10, 3 );
178
-
179
- if ( apply_filters( 'pods_meta_handler', true, 'term' ) ) {
180
- // Handle *_term_meta
181
- if ( apply_filters( 'pods_meta_handler_get', true, 'term' ) ) {
182
- add_filter( 'get_term_metadata', array( $this, 'get_term_meta' ), 10, 4 );
183
- }
184
-
185
- if ( !pods_tableless() ) {
186
- add_filter( 'add_term_metadata', array( $this, 'add_term_meta' ), 10, 5 );
187
- add_filter( 'update_term_metadata', array( $this, 'update_term_meta' ), 10, 5 );
188
- add_filter( 'delete_term_metadata', array( $this, 'delete_term_meta' ), 10, 5 );
189
- }
190
- }
191
- }
192
- }
193
-
194
- /**
195
- * Fires after a previously shared taxonomy term is split into two separate terms.
196
- *
197
- * @since 4.2.0
198
- *
199
- * @param int $term_id ID of the formerly shared term.
200
- * @param int $new_term_id ID of the new term created for the $term_taxonomy_id.
201
- * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
202
- * @param string $taxonomy Taxonomy for the split term.
203
- */
204
- add_action( 'split_shared_term', array( $this, 'split_shared_term' ), 10, 4 );
205
-
206
- // Handle Delete
207
- add_action( 'delete_term_taxonomy', array( $this, 'delete_taxonomy' ), 10, 1 );
208
-
209
- if ( !empty( self::$media ) ) {
210
- if ( pods_version_check( 'wp', '3.5' ) ) {
211
- add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
212
- add_action( 'wp_ajax_save-attachment-compat', array( $this, 'save_media_ajax' ), 0 );
213
- }
214
-
215
- add_filter( 'attachment_fields_to_edit', array( $this, 'meta_media' ), 10, 2 );
216
-
217
- add_filter( 'attachment_fields_to_save', array( $this, 'save_media' ), 10, 2 );
218
- add_filter( 'wp_update_attachment_metadata', array( $this, 'save_media' ), 10, 2 );
219
-
220
- if ( apply_filters( 'pods_meta_handler', true, 'post' ) ) {
221
- // Handle *_post_meta
222
- if ( !has_filter( 'get_post_metadata', array( $this, 'get_post_meta' ) ) ) {
223
- if ( apply_filters( 'pods_meta_handler_get', true, 'post' ) ) {
224
- add_filter( 'get_post_metadata', array( $this, 'get_post_meta' ), 10, 4 );
225
- }
226
-
227
- if ( !pods_tableless() ) {
228
- add_filter( 'add_post_metadata', array( $this, 'add_post_meta' ), 10, 5 );
229
- add_filter( 'update_post_metadata', array( $this, 'update_post_meta' ), 10, 5 );
230
- add_filter( 'delete_post_metadata', array( $this, 'delete_post_meta' ), 10, 5 );
231
- }
232
- }
233
- }
234
- }
235
-
236
- // Handle Delete
237
- add_action( 'delete_attachment', array( $this, 'delete_media' ), 10, 1 );
238
-
239
- if ( !empty( self::$user ) ) {
240
- // Handle User Editor
241
- add_action( 'show_user_profile', array( $this, 'meta_user' ) );
242
- add_action( 'edit_user_profile', array( $this, 'meta_user' ) );
243
- //add_action( 'user_register', array( $this, 'save_user' ) );
244
- add_action( 'profile_update', array( $this, 'save_user' ) );
245
-
246
- if ( apply_filters( 'pods_meta_handler', true, 'user' ) ) {
247
- // Handle *_user_meta
248
- if ( apply_filters( 'pods_meta_handler_get', true, 'user' ) ) {
249
- add_filter( 'get_user_metadata', array( $this, 'get_user_meta' ), 10, 4 );
250
- }
251
-
252
- if ( !pods_tableless() ) {
253
- add_filter( 'add_user_metadata', array( $this, 'add_user_meta' ), 10, 5 );
254
- add_filter( 'update_user_metadata', array( $this, 'update_user_meta' ), 10, 5 );
255
- add_filter( 'delete_user_metadata', array( $this, 'delete_user_meta' ), 10, 5 );
256
- }
257
- }
258
- }
259
-
260
- // Handle Delete
261
- add_action( 'delete_user', array( $this, 'delete_user' ), 10, 1 );
262
-
263
- if ( !empty( self::$comment ) ) {
264
- // Handle Comment Form / Editor
265
- add_action( 'comment_form_logged_in_after', array( $this, 'meta_comment_new_logged_in' ), 10, 2 );
266
- add_filter( 'comment_form_default_fields', array( $this, 'meta_comment_new' ) );
267
- add_action( 'add_meta_boxes_comment', array( $this, 'meta_comment_add' ) );
268
- add_filter( 'pre_comment_approved', array( $this, 'validate_comment' ), 10, 2 );
269
- add_action( 'comment_post', array( $this, 'save_comment' ) );
270
- add_action( 'edit_comment', array( $this, 'save_comment' ) );
271
-
272
- if ( apply_filters( 'pods_meta_handler', true, 'comment' ) ) {
273
- // Handle *_comment_meta
274
- add_filter( 'get_comment_metadata', array( $this, 'get_comment_meta' ), 10, 4 );
275
-
276
- if ( !pods_tableless() ) {
277
- add_filter( 'add_comment_metadata', array( $this, 'add_comment_meta' ), 10, 5 );
278
- add_filter( 'update_comment_metadata', array( $this, 'update_comment_meta' ), 10, 5 );
279
- add_filter( 'delete_comment_metadata', array( $this, 'delete_comment_meta' ), 10, 5 );
280
- }
281
- }
282
- }
283
-
284
- // Handle Delete
285
- add_action( 'delete_comment', array( $this, 'delete_comment' ), 10, 1 );
286
-
287
- // @todo Patch core to provide $option back in filters, patch core to add filter pre_add_option to add_option
288
- /*if ( !empty( self::$settings ) ) {
289
- foreach ( self::$settings as $setting_pod ) {
290
- foreach ( $setting_pod[ 'fields' ] as $option ) {
291
- add_filter( 'pre_option_' . $setting_pod[ 'name' ] . '_' . $option[ 'name' ], array( $this, 'get_option' ), 10, 1 );
292
- add_action( 'add_option_' . $setting_pod[ 'name' ] . '_' . $option[ 'name' ], array( $this, 'add_option' ), 10, 2 );
293
- add_filter( 'pre_update_option_' . $setting_pod[ 'name' ] . '_' . $option[ 'name' ], array( $this, 'update_option' ), 10, 2 );
294
- }
295
- }
296
- }*/
297
-
298
- if ( is_admin() )
299
- $this->integrations();
300
-
301
- add_action( 'init', array( $this, 'enqueue' ), 9 );
302
-
303
- if ( function_exists( 'pll_current_language' ) )
304
- add_action( 'init', array( $this, 'cache_pods' ), 101 );
305
-
306
- do_action( 'pods_meta_init' );
307
-
308
- return $this;
309
- }
310
-
311
- public static function enqueue () {
312
- foreach ( self::$queue as $type => $objects ) {
313
- foreach ( $objects as $pod_name => $pod ) {
314
- pods_transient_set( 'pods_pod_' . $pod_name, $pod );
315
- }
316
-
317
- self::$$type = array_merge( self::$$type, $objects );
318
- }
319
- }
320
-
321
- /**
322
- * Go back through and cache the Pods now that Polylang has loaded
323
- */
324
- public function cache_pods () {
325
- self::$advanced_content_types = pods_api()->load_pods( array( 'type' => 'pod', 'refresh' => true ) );
326
- self::$post_types = pods_api()->load_pods( array( 'type' => 'post_type', 'refresh' => true ) );
327
- self::$taxonomies = pods_api()->load_pods( array( 'type' => 'taxonomy', 'refresh' => true ) );
328
- self::$media = pods_api()->load_pods( array( 'type' => 'media', 'refresh' => true ) );
329
- self::$user = pods_api()->load_pods( array( 'type' => 'user', 'refresh' => true ) );
330
- self::$comment = pods_api()->load_pods( array( 'type' => 'comment', 'refresh' => true ) );
331
- self::$settings = pods_api()->load_pods( array( 'type' => 'settings', 'refresh' => true ) );
332
- }
333
-
334
- public function register ( $type, $pod ) {
335
- $pod_type = $type;
336
-
337
- if ( 'post_type' == $type )
338
- $type = 'post_types';
339
- elseif ( 'taxonomy' == $type )
340
- $type = 'taxonomies';
341
- elseif ( 'pod' == $type )
342
- $type = 'advanced_content_types';
343
-
344
- if ( !isset( self::$queue[ $type ] ) )
345
- self::$queue[ $type ] = array();
346
-
347
- if ( is_array( $pod ) && !empty( $pod ) && !isset( $pod[ 'name' ] ) ) {
348
- $data = array();
349
-
350
- foreach ( $pod as $p ) {
351
- $data[] = $this->register( $type, $p );
352
- }
353
-
354
- return $data;
355
- }
356
-
357
- $pod[ 'type' ] = $pod_type;
358
- $pod = pods_api()->save_pod( $pod, false, false );
359
-
360
- if ( !empty( $pod ) ) {
361
- self::$object_identifier--;
362
-
363
- self::$queue[ $type ][ $pod[ 'name' ] ] = $pod;
364
-
365
- return $pod;
366
- }
367
-
368
- return false;
369
- }
370
-
371
- public function register_field ( $pod, $field ) {
372
- if ( is_array( $pod ) && !empty( $pod ) && !isset( $pod[ 'name' ] ) ) {
373
- $data = array();
374
-
375
- foreach ( $pod as $p ) {
376
- $data[] = $this->register_field( $p, $field );
377
- }
378
-
379
- return $data;
380
- }
381
-
382
- if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $pod )
383
- self::$current_pod_data = pods_api()->load_pod( array( 'name' => $pod ), false );
384
-
385
- $pod = self::$current_pod_data;
386
-
387
- if ( !empty( $pod ) ) {
388
- $type = $pod[ 'type' ];
389
-
390
- if ( 'post_type' == $pod[ 'type' ] )
391
- $type = 'post_types';
392
- elseif ( 'taxonomy' == $pod[ 'type' ] )
393
- $type = 'taxonomies';
394
- elseif ( 'pod' == $pod[ 'type' ] )
395
- $type = 'advanced_content_types';
396
-
397
- if ( !isset( self::$queue[ $pod[ 'type' ] ] ) )
398
- self::$queue[ $type ] = array();
399
-
400
- $field = pods_api()->save_field( $field, false, false, $pod[ 'id' ] );
401
-
402
- if ( !empty( $field ) ) {
403
- $pod[ 'fields' ][ $field[ 'name' ] ] = $field;
404
-
405
- self::$queue[ $type ][ $pod[ 'name' ] ] = $pod;
406
-
407
- return $field;
408
- }
409
- }
410
-
411
- return false;
412
- }
413
-
414
- public function integrations () {
415
- // Codepress Admin Columns 2.x
416
- add_filter( 'cac/storage_model/meta_keys', array( $this, 'cpac_meta_keys' ), 10, 2 );
417
- add_filter( 'cac/post_types', array( $this, 'cpac_post_types' ), 10, 1 );
418
- add_filter( 'cac/column/meta/value', array( $this, 'cpac_meta_value' ), 10, 3 );
419
- }
420
-
421
-
422
- public function cpac_meta_keys ( $meta_fields, $storage_model ) {
423
- $object_type = 'post_type';
424
- $object = $storage_model->key;
425
-
426
- if ( in_array( $storage_model->key, array( 'wp-links', 'link' ) ) ) {
427
- $object_type = $object = 'link';
428
- }
429
- elseif ( in_array( $storage_model->key, array( 'wp-media', 'media' ) ) ) {
430
- $object_type = $object = 'media';
431
- }
432
- elseif ( in_array( $storage_model->key, array( 'wp-users', 'user' ) ) ) {
433
- $object_type = $object = 'user';
434
- }
435
- elseif ( in_array( $storage_model->key, array( 'wp-comments', 'comment' ) ) ) {
436
- $object_type = $object = 'comment';
437
- }
438
- elseif ( 'taxonomy' === $storage_model->type ) {
439
- $object_type = 'taxonomy';
440
- $object = $storage_model->taxonomy;
441
- }
442
-
443
- if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $object )
444
- self::$current_pod_data = pods_api()->load_pod( array( 'name' => $object ), false );
445
-
446
- $pod = self::$current_pod_data;
447
-
448
- // Add Pods fields
449
- if ( !empty( $pod ) && $object_type == $pod[ 'type' ] ) {
450
- foreach ( $pod[ 'fields' ] as $field => $field_data ) {
451
- if ( !is_array( $meta_fields ) )
452
- $meta_fields = array();
453
-
454
- if ( !in_array( $field, $meta_fields ) )
455
- $meta_fields[] = $field;
456
- }
457
- }
458
-
459
- // Remove internal Pods fields
460
- if ( is_array( $meta_fields ) ) {
461
- foreach ( $meta_fields as $k => $meta_field ) {
462
- if ( 0 === strpos( $meta_field, '_pods_' ) )
463
- unset( $meta_fields[ $k ] );
464
- }
465
- }
466
-
467
- return $meta_fields;
468
- }
469
-
470
- public function cpac_post_types ( $post_types ) {
471
- // Remove internal Pods post types
472
- foreach ( $post_types as $post_type => $post_type_name ) {
473
- if ( 0 === strpos( $post_type, '_pods_' ) || 0 === strpos( $post_type_name, '_pods_' ) )
474
- unset( $post_types[ $post_type ] );
475
- }
476
-
477
- return $post_types;
478
- }
479
-
480
- public function cpac_meta_value ( $meta, $id, $obj ) {
481
- $tableless_field_types = PodsForm::tableless_field_types();
482
-
483
- $object_type = 'post_type';
484
- $object = $obj->storage_model->key;
485
-
486
- if ( in_array( $obj->storage_model->type, array( 'wp-links', 'link' ) ) ) {
487
- $object_type = $object = 'link';
488
- }
489
- elseif ( in_array( $obj->storage_model->type, array( 'wp-media', 'media' ) ) ) {
490
- $object_type = $object = 'media';
491
- }
492
- elseif ( in_array( $obj->storage_model->type, array( 'wp-users', 'user' ) ) ) {
493
- $object_type = $object = 'user';
494
- }
495
- elseif ( in_array( $obj->storage_model->type, array( 'wp-comments', 'comment' ) ) ) {
496
- $object_type = $object = 'comment';
497
- }
498
- elseif ( 'taxonomy' === $obj->storage_model->type ) {
499
- $object_type = 'taxonomy';
500
- $object = $obj->storage_model->taxonomy;
501
- }
502
-
503
- $field = substr( $obj->options->field, 0, 10 ) == "cpachidden" ? str_replace( 'cpachidden', '', $obj->options->field ) : $obj->options->field;
504
- $field_type = $obj->options->field_type;
505
-
506
- if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $object )
507
- self::$current_pod_data = pods_api()->load_pod( array( 'name' => $object ), false );
508
-
509
- $pod = self::$current_pod_data;
510
-
511
- // Add Pods fields
512
- if ( !empty( $pod ) && isset( $pod[ 'fields' ][ $field ] ) ) {
513
-
514
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'user', 'comment', 'media' ) ) && ( !empty( $field_type ) || in_array( $pod[ 'fields' ][ $field ][ 'type' ], $tableless_field_types ) ) ) {
515
- $meta = get_metadata( ( 'post_type' == $pod[ 'type' ] ? 'post' : $pod[ 'type' ] ), $id, $field, true );
516
- }
517
-
518
- elseif ( 'taxonomy' === $pod['type'] ) {
519
- $podterms = pods( $pod['name'] );
520
- $podterms->fetch( $id );
521
- $meta = $podterms->field( $field );
522
- }
523
-
524
- $meta = PodsForm::field_method( $pod[ 'fields' ][ $field ][ 'type' ], 'ui', $id, $meta, $field, array_merge( $pod[ 'fields' ][ $field ], $pod[ 'fields' ][ $field ][ 'options' ] ), $pod[ 'fields' ], $pod );
525
- }
526
-
527
- return $meta;
528
- }
529
-
530
- public function cpac_meta_values ( $meta, $field_type, $field, $type, $id ) {
531
- $tableless_field_types = PodsForm::tableless_field_types();
532
-
533
- $object = $type;
534
-
535
- if ( 'wp-media' == $type )
536
- $object = 'media';
537
- elseif ( 'wp-users' == $type )
538
- $object = 'user';
539
- elseif ( 'wp-comments' == $type )
540
- $object = 'comment';
541
-
542
- if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $object )
543
- self::$current_pod_data = pods_api()->load_pod( array( 'name' => $object ), false );
544
-
545
- $pod = self::$current_pod_data;
546
-
547
- // Add Pods fields
548
- if ( !empty( $pod ) && isset( $pod[ 'fields' ][ $field ] ) ) {
549
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'user', 'comment', 'media' ) ) && ( !empty( $field_type ) || in_array( $pod[ 'fields' ][ $field ][ 'type' ], $tableless_field_types ) ) )
550
- $meta = get_metadata( ( 'post_type' == $pod[ 'type' ] ? 'post' : $pod[ 'type' ] ), $id, $field, true );
551
-
552
- $meta = PodsForm::field_method( $pod[ 'fields' ][ $field ][ 'type' ], 'ui', $id, $meta, $field, array_merge( $pod[ 'fields' ][ $field ], $pod[ 'fields' ][ $field ][ 'options' ] ), $pod[ 'fields' ], $pod );
553
- }
554
-
555
- return $meta;
556
- }
557
-
558
- /**
559
- * Add a meta group of fields to add/edit forms
560
- *
561
- * @param string|array $pod The pod or type of element to attach the group to.
562
- * @param string $label Title of the edit screen section, visible to user.
563
- * @param string|array $fields Either a comma separated list of text fields or an associative array containing field infomration.
564
- * @param string $context (optional) The part of the page where the edit screen section should be shown ('normal', 'advanced', or 'side').
565
- * @param string $priority (optional) The priority within the context where the boxes should show ('high', 'core', 'default' or 'low').
566
- *
567
- * @since 2.0
568
- *
569
- * @return mixed|void
570
- */
571
- public function group_add ( $pod, $label, $fields, $context = 'normal', $priority = 'default' ) {
572
- if ( is_array( $pod ) && !empty( $pod ) && !isset( $pod[ 'name' ] ) ) {
573
- foreach ( $pod as $p ) {
574
- $this->group_add( $pod, $label, $fields, $context, $priority );
575
- }
576
-
577
- return true;
578
- }
579
-
580
- if ( !is_array( $pod ) ) {
581
- if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $pod )
582
- self::$current_pod_data = pods_api()->load_pod( array( 'name' => $pod ), false );
583
-
584
- if ( !empty( self::$current_pod_data ) )
585
- $pod = self::$current_pod_data;
586
- else {
587
- $type = 'post_type';
588
-
589
- if ( in_array( $pod, array( 'media', 'user', 'comment' ) ) )
590
- $type = $pod;
591
-
592
- $pod = array(
593
- 'name' => $pod,
594
- 'type' => $type
595
- );
596
- }
597
- }
598
-
599
- if ( is_array( $pod ) && !isset( $pod[ 'id' ] ) ) {
600
- $defaults = array(
601
- 'name' => '',
602
- 'type' => 'post_type'
603
- );
604
-
605
- $pod = array_merge( $defaults, $pod );
606
- }
607
-
608
- if ( 'post' == $pod[ 'type' ] )
609
- $pod[ 'type' ] = 'post_type';
610
-
611
- if ( empty( $pod[ 'name' ] ) && isset( $pod[ 'object' ] ) && !empty( $pod[ 'object' ] ) )
612
- $pod[ 'name' ] = $pod[ 'object' ];
613
- elseif ( !isset( $pod[ 'object' ] ) || empty( $pod[ 'object' ] ) )
614
- $pod[ 'object' ] = $pod[ 'name' ];
615
-
616
- if ( empty( $pod[ 'object' ] ) )
617
- return pods_error( __( 'Object required to add a Pods meta group', 'pods' ) );
618
-
619
- $object_name = $pod[ 'object' ];
620
-
621
- if ( 'pod' == $pod[ 'type' ] )
622
- $object_name = $pod[ 'name' ];
623
-
624
- if ( !isset( self::$groups[ $pod[ 'type' ] ] ) )
625
- self::$groups[ $pod[ 'type' ] ] = array();
626
-
627
- if ( !isset( self::$groups[ $pod[ 'type' ] ][ $object_name ] ) )
628
- self::$groups[ $pod[ 'type' ] ][ $object_name ] = array();
629
-
630
- $_fields = array();
631
-
632
- if ( !is_array( $fields ) )
633
- $fields = explode( ',', $fields );
634
-
635
- foreach ( $fields as $k => $field ) {
636
- $name = $k;
637
-
638
- $defaults = array(
639
- 'name' => $name,
640
- 'label' => $name,
641
- 'type' => 'text'
642
- );
643
-
644
- if ( !is_array( $field ) ) {
645
- $name = trim( $field );
646
-
647
- $field = array(
648
- 'name' => $name,
649
- 'label' => $name
650
- );
651
- }
652
-
653
- $field = array_merge( $defaults, $field );
654
-
655
- $field[ 'name' ] = trim( $field[ 'name' ] );
656
-
657
- if ( isset( $pod[ 'fields' ] ) && isset( $pod[ 'fields' ][ $field[ 'name' ] ] ) )
658
- $field = array_merge( $field, $pod[ 'fields' ][ $field[ 'name' ] ] );
659
-
660
- $_fields[ $k ] = $field;
661
- }
662
-
663
- // Setup field options
664
- $fields = PodsForm::fields_setup( $_fields );
665
-
666
- $group = array(
667
- 'pod' => $pod,
668
- 'label' => $label,
669
- 'fields' => $fields,
670
- 'context' => $context,
671
- 'priority' => $priority
672
- );
673
-
674
- // Filter group data, pass vars separately for reference down the line (in case array changed by other filter)
675
- $group = apply_filters( 'pods_meta_group_add_' . $pod[ 'type' ] . '_' . $object_name, $group, $pod, $label, $fields );
676
- $group = apply_filters( 'pods_meta_group_add_' . $pod[ 'type' ], $group, $pod, $label, $fields );
677
- $group = apply_filters( 'pods_meta_group_add', $group, $pod, $label, $fields );
678
-
679
- self::$groups[ $pod[ 'type' ] ][ $object_name ][] = $group;
680
-
681
- // Hook it up!
682
- if ( 'post_type' == $pod[ 'type' ] ) {
683
- if ( !has_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) ) )
684
- add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
685
-
686
- /*if ( !has_action( 'save_post', array( $this, 'save_post' ), 10, 3 ) )
687
- add_action( 'save_post', array( $this, 'save_post' ), 10, 3 );*/
688
- }
689
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
690
- if ( !has_action( $pod[ 'object' ] . '_edit_form_fields', array( $this, 'meta_taxonomy' ), 10, 2 ) ) {
691
- add_action( $pod[ 'object' ] . '_edit_form_fields', array( $this, 'meta_taxonomy' ), 10, 2 );
692
- add_action( $pod[ 'object' ] . '_add_form_fields', array( $this, 'meta_taxonomy' ), 10, 1 );
693
- }
694
-
695
- if ( !has_action( 'edited_term', array( $this, 'save_taxonomy' ), 10, 3 ) ) {
696
- add_action( 'edited_term', array( $this, 'save_taxonomy' ), 10, 3 );
697
- add_action( 'create_term', array( $this, 'save_taxonomy' ), 10, 3 );
698
- }
699
- }
700
- elseif ( 'media' == $pod[ 'type' ] ) {
701
- if ( !has_filter( 'wp_update_attachment_metadata', array( $this, 'save_media' ), 10, 2 ) ) {
702
- if ( pods_version_check( 'wp', '3.5' ) ) {
703
- add_action( 'add_meta_boxes', array( $this, 'meta_post_add' ) );
704
- add_action( 'wp_ajax_save-attachment-compat', array( $this, 'save_media_ajax' ), 0 );
705
- }
706
-
707
- add_filter( 'attachment_fields_to_edit', array( $this, 'meta_media' ), 10, 2 );
708
-
709
- add_filter( 'attachment_fields_to_save', array( $this, 'save_media' ), 10, 2 );
710
- add_filter( 'wp_update_attachment_metadata', array( $this, 'save_media' ), 10, 2 );
711
- }
712
- }
713
- elseif ( 'user' == $pod[ 'type' ] ) {
714
- if ( !has_action( 'show_user_profile', array( $this, 'meta_user' ) ) ) {
715
- add_action( 'show_user_profile', array( $this, 'meta_user' ) );
716
- add_action( 'edit_user_profile', array( $this, 'meta_user' ) );
717
- //add_action( 'user_register', array( $this, 'save_user' ) );
718
- add_action( 'profile_update', array( $this, 'save_user' ) );
719
- }
720
- }
721
- elseif ( 'comment' == $pod[ 'type' ] ) {
722
- if ( !has_action( 'comment_form_logged_in_after', array( $this, 'meta_comment_new_logged_in' ), 10, 2 ) ) {
723
- add_action( 'comment_form_logged_in_after', array( $this, 'meta_comment_new_logged_in' ), 10, 2 );
724
- add_filter( 'comment_form_default_fields', array( $this, 'meta_comment_new' ) );
725
- add_action( 'add_meta_boxes_comment', array( $this, 'meta_comment_add' ) );
726
- add_action( 'wp_insert_comment', array( $this, 'save_comment' ) );
727
- add_action( 'edit_comment', array( $this, 'save_comment' ) );
728
- }
729
- }
730
- }
731
-
732
- public function object_get ( $type, $name ) {
733
- $object = self::$post_types;
734
-
735
- if ( 'taxonomy' == $type )
736
- $object = self::$taxonomies;
737
- elseif ( 'media' == $type )
738
- $object = self::$media;
739
- elseif ( 'user' == $type )
740
- $object = self::$user;
741
- elseif ( 'comment' == $type )
742
- $object = self::$comment;
743
-
744
- if ( 'pod' != $type && !empty( $object ) && is_array( $object ) && isset( $object[ $name ] ) )
745
- $pod = $object[ $name ];
746
- else {
747
- if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $name )
748
- self::$current_pod_data = pods_api()->load_pod( array( 'name' => $name ), false );
749
-
750
- $pod = self::$current_pod_data;
751
- }
752
-
753
- if ( empty( $pod ) )
754
- return array();
755
-
756
- $defaults = array(
757
- 'name' => 'post',
758
- 'object' => 'post',
759
- 'type' => 'post_type'
760
- );
761
-
762
- $pod = array_merge( $defaults, (array) $pod );
763
-
764
- if ( empty( $pod[ 'name' ] ) )
765
- $pod[ 'name' ] = $pod[ 'object' ];
766
- elseif ( empty( $pod[ 'object' ] ) )
767
- $pod[ 'object' ] = $pod[ 'name' ];
768
-
769
- if ( $pod[ 'type' ] != $type )
770
- return array();
771
-
772
- return $pod;
773
- }
774
-
775
- /**
776
- * @param $type
777
- * @param $name
778
- * @param $default_fields
779
- *
780
- * @return array
781
- */
782
- public function groups_get ( $type, $name, $default_fields = null ) {
783
- if ( 'post_type' == $type && 'attachment' == $name ) {
784
- $type = 'media';
785
- $name = 'media';
786
- }
787
-
788
- do_action( 'pods_meta_groups', $type, $name );
789
-
790
- $pod = array();
791
- $fields = array();
792
-
793
- $object = self::$post_types;
794
-
795
- if ( 'taxonomy' == $type )
796
- $object = self::$taxonomies;
797
- elseif ( 'media' == $type )
798
- $object = self::$media;
799
- elseif ( 'user' == $type )
800
- $object = self::$user;
801
- elseif ( 'comment' == $type )
802
- $object = self::$comment;
803
- elseif ( 'pod' == $type )
804
- $object = self::$advanced_content_types;
805
-
806
- if ( !empty( $object ) && is_array( $object ) && isset( $object[ $name ] ) )
807
- $fields = $object[ $name ][ 'fields' ];
808
- else {
809
- if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod_data ) || self::$current_pod_data[ 'name' ] != $name )
810
- self::$current_pod_data = pods_api()->load_pod( array( 'name' => $name ), false );
811
-
812
- $pod = self::$current_pod_data;
813
-
814
- if ( !empty( $pod ) )
815
- $fields = $pod[ 'fields' ];
816
- }
817
-
818
- if ( null !== $default_fields ) {
819
- $fields = $default_fields;
820
- }
821
-
822
- $defaults = array(
823
- 'name' => 'post',
824
- 'object' => 'post',
825
- 'type' => 'post_type'
826
- );
827
-
828
- $pod = array_merge( $defaults, (array) $pod );
829
-
830
- if ( empty( $pod[ 'name' ] ) )
831
- $pod[ 'name' ] = $pod[ 'object' ];
832
- elseif ( empty( $pod[ 'object' ] ) )
833
- $pod[ 'object' ] = $pod[ 'name' ];
834
-
835
- if ( $pod[ 'type' ] != $type )
836
- return array();
837
-
838
- $groups = array(
839
- array(
840
- 'pod' => $pod,
841
- /**
842
- * Filter the title of the Pods Metabox In The Post Editor
843
- *
844
- * @param string $title The title to use, default is 'More Fields'
845
- * @param obj|Pod $pod Current Pods Object
846
- * @param array $fields Array of fields that will go in the metabox
847
- * @param string $type The type of Pod
848
- * @params string $name Name of the Pod
849
- *
850
- * @returns string The title for the metabox.
851
- *
852
- * @since unknown
853
- */
854
- 'label' => apply_filters( 'pods_meta_default_box_title', __( 'More Fields', 'pods' ), $pod, $fields, $type, $name ),
855
- 'fields' => $fields,
856
- 'context' => 'normal',
857
- 'priority' => 'default'
858
- )
859
- );
860
-
861
- if ( isset( self::$groups[ $type ] ) && isset( self::$groups[ $type ][ $name ] ) )
862
- $groups = self::$groups[ $type ][ $name ];
863
-
864
- return $groups;
865
- }
866
-
867
- /**
868
- * @param $post_type
869
- * @param null $post
870
- */
871
- public function meta_post_add ( $post_type, $post = null ) {
872
- if ( 'comment' == $post_type )
873
- return;
874
-
875
- if ( is_object( $post ) )
876
- $post_type = $post->post_type;
877
-
878
- $groups = $this->groups_get( 'post_type', $post_type );
879
- $pods_field_found = false;
880
-
881
- foreach ( $groups as $group ) {
882
- if ( empty( $group[ 'fields' ] ) )
883
- continue;
884
-
885
- $field_found = false;
886
-
887
- foreach ( $group[ 'fields' ] as $field ) {
888
- if ( false !== PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ] ) ) {
889
- $field_found = true;
890
- break;
891
- }
892
- }
893
-
894
- if ( empty( $group[ 'label' ] ) )
895
- $group[ 'label' ] = get_post_type_object( $post_type )->labels->label;
896
-
897
- if ( $field_found ) {
898
- $pods_field_found = true;
899
- add_meta_box(
900
- 'pods-meta-' . sanitize_title( $group[ 'label' ] ),
901
- $group[ 'label' ],
902
- array( $this, 'meta_post' ),
903
- $post_type,
904
- $group[ 'context' ],
905
- $group[ 'priority' ],
906
- array( 'group' => $group )
907
- );
908
-
909
- }
910
- }
911
-
912
- if ( $pods_field_found ) {
913
- // Only add the classes to forms that actually have pods fields
914
- add_action( 'post_edit_form_tag', array( $this, 'add_class_submittable' ) );
915
- }
916
- }
917
-
918
- /**
919
- *
920
- * Called by 'post_edit_form_tag' action to include the classes in the <form> tag
921
- *
922
- */
923
- public function add_class_submittable () {
924
- echo ' class="pods-submittable pods-form"';
925
- }
926
-
927
- /**
928
- * @param $post
929
- * @param $metabox
930
- */
931
- public function meta_post ( $post, $metabox ) {
932
- wp_enqueue_style( 'pods-form' );
933
- wp_enqueue_script( 'pods' );
934
-
935
- $pod_type = 'post';
936
-
937
- if ( 'attachment' == $post->post_type ) {
938
- $pod_type = 'media';
939
- }
940
-
941
- do_action( 'pods_meta_' . __FUNCTION__, $post );
942
-
943
- $hidden_fields = array();
944
- ?>
945
- <table class="form-table pods-metabox pods-admin pods-dependency">
946
- <?php echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_' . $pod_type ), 'hidden' ); ?>
947
-
948
- <?php
949
- $id = null;
950
-
951
- if ( is_object( $post ) && false === strpos( $_SERVER[ 'REQUEST_URI' ], '/post-new.php' ) )
952
- $id = $post->ID;
953
-
954
- if ( empty( self::$current_pod_data ) || !is_object( self::$current_pod ) || self::$current_pod->pod != $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ] )
955
- self::$current_pod = pods( $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ], $id, true );
956
- elseif ( self::$current_pod->id() != $id )
957
- self::$current_pod->fetch( $id );
958
-
959
- $pod = self::$current_pod;
960
-
961
- foreach ( $metabox[ 'args' ][ 'group' ][ 'fields' ] as $field ) {
962
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field[ 'options' ], $metabox[ 'args' ][ 'group' ][ 'fields' ], $pod, $id ) ) {
963
- if ( pods_var( 'hidden', $field[ 'options' ], false ) )
964
- $field[ 'type' ] = 'hidden';
965
- else
966
- continue;
967
- }
968
- elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
969
- $field[ 'type' ] = 'hidden';
970
-
971
- $value = '';
972
-
973
- if ( !empty( $pod ) ) {
974
- pods_no_conflict_on( 'post' );
975
-
976
- $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
977
-
978
- pods_no_conflict_off( 'post' );
979
- }
980
- elseif ( !empty( $id ) )
981
- $value = get_post_meta( $id, $field[ 'name' ], true );
982
-
983
- if ( 'hidden' == $field[ 'type' ] ) {
984
- $hidden_fields[] = array(
985
- 'field' => $field,
986
- 'value' => $value
987
- );
988
- }
989
- else {
990
- $depends = PodsForm::dependencies( $field, 'pods-meta-' );
991
-
992
- do_action( 'pods_meta_' . __FUNCTION__ . '_' . $field[ 'name' ], $post, $field, $pod );
993
- ?>
994
- <tr class="form-field pods-field pods-field-input <?php echo esc_attr( 'pods-form-ui-row-type-' . $field[ 'type' ] . ' pods-form-ui-row-name-' . PodsForm::clean( $field[ 'name' ], true ) ); ?> <?php echo esc_attr( $depends ); ?>">
995
- <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); ?></th>
996
- <td>
997
- <?php
998
- // Remove any extra ? help icons
999
- if ( isset( $field[ 'help' ] ) )
1000
- unset( $field[ 'help' ] );
1001
- ?>
1002
- <div class="pods-submittable-fields">
1003
- <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?>
1004
- <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?>
1005
- </div>
1006
- </td>
1007
- </tr>
1008
- <?php
1009
- do_action( 'pods_meta_' . __FUNCTION__ . '_' . $field[ 'name' ] . '_post', $post, $field, $pod );
1010
- }
1011
- }
1012
- ?>
1013
- </table>
1014
-
1015
- <?php
1016
- do_action( 'pods_meta_' . __FUNCTION__ . '_post', $post );
1017
-
1018
- foreach ( $hidden_fields as $hidden_field ) {
1019
- $field = $hidden_field[ 'field' ];
1020
-
1021
- echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $hidden_field[ 'value' ], 'hidden' );
1022
- }
1023
- ?>
1024
-
1025
- <script type="text/javascript">
1026
- jQuery( function ( $ ) {
1027
- $( document ).Pods( 'validate' );
1028
- $( document ).Pods( 'submit_meta' );
1029
- $( document ).Pods( 'dependency', true );
1030
- } );
1031
- </script>
1032
- <?php
1033
- }
1034
-
1035
- /**
1036
- * @param $new_status
1037
- * @param $old_status
1038
- * @param $post
1039
- */
1040
- public function save_post_detect_new ( $new_status, $old_status, $post ) {
1041
- self::$old_post_status = $old_status;
1042
- }
1043
-
1044
- /**
1045
- * @param $post_id
1046
- * @param $post
1047
- * @param $update
1048
- *
1049
- * @return int Post ID
1050
- */
1051
- public function save_post ( $post_id, $post, $update = null ) {
1052
- $is_new_item = false;
1053
-
1054
- if ( is_bool( $update ) )
1055
- $is_new_item = !$update; // false is new item
1056
- elseif ( 'new' == self::$old_post_status ) {
1057
- $is_new_item = true;
1058
- }
1059
-
1060
- if ( empty( $_POST ) ) {
1061
- return $post_id;
1062
- }
1063
- elseif ( !$is_new_item && !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_post' ) ) {
1064
- return $post_id;
1065
- }
1066
-
1067
- // Reset to avoid manual new post issues
1068
- self::$old_post_status = '';
1069
-
1070
- $blacklisted_types = array(
1071
- 'revision',
1072
- '_pods_pod',
1073
- '_pods_field'
1074
- );
1075
-
1076
- $blacklisted_types = apply_filters( 'pods_meta_save_post_blacklist_types', $blacklisted_types, $post_id, $post );
1077
-
1078
- // @todo Figure out how to hook into autosave for saving meta
1079
-
1080
- // Block Autosave and Revisions
1081
- if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || in_array( $post->post_type, $blacklisted_types ) )
1082
- return $post_id;
1083
-
1084
- // Block Quick Edits / Bulk Edits
1085
- if ( 'edit.php' == pods_var( 'pagenow', 'global' ) && ( 'inline-save' == pods_var( 'action', 'post' ) || null != pods_var( 'bulk_edit', 'get' ) || is_array( pods_var( 'post', 'get' ) ) ) )
1086
- return $post_id;
1087
-
1088
- // Block Trash
1089
- if ( in_array( pods_var( 'action', 'get' ), array( 'untrash', 'trash' ) ) )
1090
- return $post_id;
1091
-
1092
- // Block Auto-drafting and Trash (not via Admin action)
1093
- $blacklisted_status = array(
1094
- 'auto-draft',
1095
- 'trash'
1096
- );
1097
-
1098
- $blacklisted_status = apply_filters( 'pods_meta_save_post_blacklist_status', $blacklisted_status, $post_id, $post );
1099
-
1100
- if ( in_array( $post->post_status, $blacklisted_status ) )
1101
- return $post_id;
1102
-
1103
- $groups = $this->groups_get( 'post_type', $post->post_type );
1104
-
1105
- if ( empty( $groups ) )
1106
- return $post_id;
1107
-
1108
- $data = array();
1109
-
1110
- $id = $post_id;
1111
- $pod = null;
1112
-
1113
- foreach ( $groups as $group ) {
1114
- if ( empty( $group[ 'fields' ] ) )
1115
- continue;
1116
-
1117
- if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1118
- if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1119
- self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1120
- elseif ( self::$current_pod->id() != $id )
1121
- self::$current_pod->fetch( $id );
1122
-
1123
- $pod = self::$current_pod;
1124
- }
1125
-
1126
- foreach ( $group[ 'fields' ] as $field ) {
1127
-
1128
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1129
- if ( !pods_var( 'hidden', $field[ 'options' ], false ) )
1130
- continue;
1131
- }
1132
-
1133
- $data[ $field[ 'name' ] ] = '';
1134
-
1135
- if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
1136
- $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
1137
- }
1138
- }
1139
-
1140
- if ( $is_new_item ) {
1141
- do_action( 'pods_meta_create_pre_post', $data, $pod, $id, $groups, $post, $post->post_type );
1142
- do_action( "pods_meta_create_pre_post_{$post->post_type}", $data, $pod, $id, $groups, $post );
1143
- }
1144
-
1145
- do_action( 'pods_meta_save_pre_post', $data, $pod, $id, $groups, $post, $post->post_type, $is_new_item );
1146
- do_action( "pods_meta_save_pre_post_{$post->post_type}", $data, $pod, $id, $groups, $post, $is_new_item );
1147
-
1148
- pods_no_conflict_on( 'post' );
1149
-
1150
- if ( !empty( $pod ) ) {
1151
- // Fix for Pods doing it's own sanitization
1152
- $data = pods_unslash( (array) $data );
1153
-
1154
- $pod->save( $data, null, null, array( 'is_new_item' => $is_new_item ) );
1155
- }
1156
- elseif ( !empty( $id ) ) {
1157
- foreach ( $data as $field => $value ) {
1158
- update_post_meta( $id, $field, $value );
1159
- }
1160
- }
1161
-
1162
- pods_no_conflict_off( 'post' );
1163
-
1164
- if ( $is_new_item ) {
1165
- do_action( 'pods_meta_create_post', $data, $pod, $id, $groups, $post, $post->post_type );
1166
- do_action( "pods_meta_create_post_{$post->post_type}", $data, $pod, $id, $groups, $post );
1167
- }
1168
-
1169
- do_action( 'pods_meta_save_post', $data, $pod, $id, $groups, $post, $post->post_type, $is_new_item );
1170
- do_action( "pods_meta_save_post_{$post->post_type}", $data, $pod, $id, $groups, $post, $is_new_item );
1171
-
1172
- return $post_id;
1173
-
1174
- }
1175
-
1176
- /**
1177
- * @param $form_fields
1178
- * @param $post
1179
- *
1180
- * @return array
1181
- */
1182
- public function meta_media ( $form_fields, $post ) {
1183
- $groups = $this->groups_get( 'media', 'media' );
1184
-
1185
- if ( empty( $groups ) || 'attachment' == pods_var( 'typenow', 'global' ) )
1186
- return $form_fields;
1187
-
1188
- wp_enqueue_style( 'pods-form' );
1189
-
1190
- $id = null;
1191
-
1192
- if ( is_object( $post ) )
1193
- $id = $post->ID;
1194
-
1195
- $pod = null;
1196
-
1197
- $meta_nonce = PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_media' ), 'hidden' );
1198
-
1199
- foreach ( $groups as $group ) {
1200
- if ( empty( $group[ 'fields' ] ) )
1201
- continue;
1202
-
1203
- if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1204
- if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1205
- self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1206
- elseif ( self::$current_pod->id() != $id )
1207
- self::$current_pod->fetch( $id );
1208
-
1209
- $pod = self::$current_pod;
1210
- }
1211
-
1212
- foreach ( $group[ 'fields' ] as $field ) {
1213
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1214
- if ( !pods_var( 'hidden', $field[ 'options' ], false ) )
1215
- continue;
1216
- }
1217
-
1218
- $value = '';
1219
-
1220
- if ( !empty( $pod ) )
1221
- $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1222
- elseif ( !empty( $id ) ) {
1223
- pods_no_conflict_on( 'post' );
1224
-
1225
- $value = get_post_meta( $id, $field[ 'name' ], true );
1226
-
1227
- pods_no_conflict_off( 'post' );
1228
- }
1229
-
1230
- $form_fields[ 'pods_meta_' . $field[ 'name' ] ] = array(
1231
- 'label' => $field[ 'label' ],
1232
- 'input' => 'html',
1233
- 'html' => PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ) . $meta_nonce,
1234
- 'helps' => PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field )
1235
- );
1236
- }
1237
- }
1238
-
1239
- $form_fields = apply_filters( 'pods_meta_' . __FUNCTION__, $form_fields );
1240
-
1241
- return $form_fields;
1242
- }
1243
-
1244
- /**
1245
- * @param $post
1246
- * @param $attachment
1247
- *
1248
- * @return mixed
1249
- */
1250
- public function save_media ( $post, $attachment ) {
1251
- $groups = $this->groups_get( 'media', 'media' );
1252
-
1253
- if ( empty( $groups ) )
1254
- return $post;
1255
-
1256
- $post_id = $attachment;
1257
-
1258
- if ( empty( $_POST ) || !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_media' ) ) {
1259
- return $post;
1260
- }
1261
-
1262
- if ( is_array( $post ) && !empty( $post ) && isset( $post[ 'ID' ] ) && 'attachment' == $post[ 'post_type' ] )
1263
- $post_id = $post[ 'ID' ];
1264
-
1265
- if ( is_array( $post_id ) || empty( $post_id ) )
1266
- return $post;
1267
-
1268
- $data = array();
1269
-
1270
- $id = $post_id;
1271
- $pod = null;
1272
-
1273
- foreach ( $groups as $group ) {
1274
- if ( empty( $group[ 'fields' ] ) )
1275
- continue;
1276
-
1277
- if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1278
- if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1279
- self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1280
- elseif ( self::$current_pod->id() != $id )
1281
- self::$current_pod->fetch( $id );
1282
-
1283
- $pod = self::$current_pod;
1284
- }
1285
-
1286
- foreach ( $group[ 'fields' ] as $field ) {
1287
-
1288
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1289
- if ( !pods_var( 'hidden', $field[ 'options' ], false ) )
1290
- continue;
1291
- }
1292
-
1293
- $data[ $field[ 'name' ] ] = '';
1294
-
1295
- if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
1296
- $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
1297
- }
1298
- }
1299
-
1300
- do_action( 'pods_meta_save_pre_media', $data, $pod, $id, $groups, $post, $attachment );
1301
-
1302
- if ( !empty( $pod ) ) {
1303
- // Fix for Pods doing it's own sanitization
1304
- $data = pods_unslash( (array) $data );
1305
-
1306
- $pod->save( $data );
1307
- }
1308
- elseif ( !empty( $id ) ) {
1309
- pods_no_conflict_on( 'post' );
1310
-
1311
- foreach ( $data as $field => $value ) {
1312
- update_post_meta( $id, $field, $value );
1313
- }
1314
-
1315
- pods_no_conflict_off( 'post' );
1316
- }
1317
-
1318
- do_action( 'pods_meta_save_media', $data, $pod, $id, $groups, $post, $attachment );
1319
-
1320
- return $post;
1321
- }
1322
-
1323
- public function save_media_ajax () {
1324
- if ( !isset( $_POST[ 'id' ] ) || empty( $_POST[ 'id' ] ) || absint( $_POST[ 'id' ] ) < 1 )
1325
- return;
1326
-
1327
- $id = absint( $_POST[ 'id' ] );
1328
-
1329
- if ( !isset( $_POST[ 'nonce' ] ) || empty( $_POST[ 'nonce' ] ) )
1330
- return;
1331
-
1332
- check_ajax_referer( 'update-post_' . $id, 'nonce' );
1333
-
1334
- if ( !current_user_can( 'edit_post', $id ) )
1335
- return;
1336
-
1337
- $post = get_post( $id, ARRAY_A );
1338
-
1339
- if ( 'attachment' != $post[ 'post_type' ] )
1340
- return;
1341
-
1342
- // fix ALL THE THINGS
1343
-
1344
- if ( !isset( $_REQUEST[ 'attachments' ] ) )
1345
- $_REQUEST[ 'attachments' ] = array();
1346
-
1347
- if ( !isset( $_REQUEST[ 'attachments' ][ $id ] ) )
1348
- $_REQUEST[ 'attachments' ][ $id ] = array();
1349
-
1350
- if ( empty( $_REQUEST[ 'attachments' ][ $id ] ) )
1351
- $_REQUEST[ 'attachments' ][ $id ][ '_fix_wp' ] = 1;
1352
- }
1353
-
1354
- /**
1355
- * @param $tag
1356
- * @param null $taxonomy
1357
- */
1358
- public function meta_taxonomy ( $tag, $taxonomy = null ) {
1359
- wp_enqueue_style( 'pods-form' );
1360
-
1361
- do_action( 'pods_meta_' . __FUNCTION__, $tag, $taxonomy );
1362
-
1363
- $taxonomy_name = $taxonomy;
1364
-
1365
- if ( !is_object( $tag ) )
1366
- $taxonomy_name = $tag;
1367
-
1368
- $groups = $this->groups_get( 'taxonomy', $taxonomy_name );
1369
-
1370
- $id = null;
1371
-
1372
- if ( is_object( $tag ) )
1373
- $id = $tag->term_id;
1374
-
1375
- $pod = null;
1376
-
1377
- echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_taxonomy' ), 'hidden' );
1378
-
1379
- foreach ( $groups as $group ) {
1380
- if ( empty( $group[ 'fields' ] ) )
1381
- continue;
1382
-
1383
- if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1384
- if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1385
- self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1386
- elseif ( self::$current_pod->id() != $id )
1387
- self::$current_pod->fetch( $id );
1388
-
1389
- $pod = self::$current_pod;
1390
- }
1391
-
1392
- foreach ( $group[ 'fields' ] as $field ) {
1393
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1394
- if ( pods_var( 'hidden', $field[ 'options' ], false ) )
1395
- $field[ 'type' ] = 'hidden';
1396
- else
1397
- continue;
1398
- }
1399
- elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
1400
- $field[ 'type' ] = 'hidden';
1401
-
1402
- $value = '';
1403
-
1404
- if ( !empty( $pod ) )
1405
- $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1406
-
1407
- if ( !is_object( $tag ) ) {
1408
- ?>
1409
- <div class="form-field pods-field" style="<?php echo esc_attr( 'hidden' == $field[ 'type' ] ? 'display:none;' : '' ); ?>">
1410
- <?php
1411
- echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field );
1412
- echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
1413
- echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
1414
- ?>
1415
- </div>
1416
- <?php
1417
- }
1418
- else {
1419
- ?>
1420
- <tr class="form-field pods-field <?php echo esc_attr( 'pods-form-ui-row-type-' . $field[ 'type' ] . ' pods-form-ui-row-name-' . PodsForm::clean( $field[ 'name' ], true ) ); ?>" style="<?php echo esc_attr( 'hidden' == $field[ 'type' ] ? 'display:none;' : '' ); ?>">
1421
- <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); ?></th>
1422
- <td>
1423
- <?php
1424
- echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
1425
- echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
1426
- ?>
1427
- </td>
1428
- </tr>
1429
- <?php
1430
- }
1431
- }
1432
- }
1433
-
1434
- do_action( 'pods_meta_' . __FUNCTION__ . '_post', $tag, $taxonomy );
1435
- }
1436
-
1437
- /**
1438
- * @param $term_id
1439
- * @param $term_taxonomy_id
1440
- * @param $taxonomy
1441
- */
1442
- public function save_taxonomy ( $term_id, $term_taxonomy_id, $taxonomy ) {
1443
- $is_new_item = false;
1444
-
1445
- if ( 'create_term' == current_filter() )
1446
- $is_new_item = true;
1447
-
1448
- if ( empty( $_POST ) || !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_taxonomy' ) ) {
1449
- return $term_id;
1450
- }
1451
-
1452
- // Block Quick Edits / Bulk Edits
1453
- if ( 'inline-save-tax' == pods_var( 'action', 'post' ) || null != pods_var( 'delete_tags', 'post' ) ) {
1454
- return $term_id;
1455
- }
1456
-
1457
- $groups = $this->groups_get( 'taxonomy', $taxonomy );
1458
-
1459
- if ( empty( $groups ) )
1460
- return $term_id;
1461
-
1462
- $term = null;
1463
-
1464
- $id = $term_id;
1465
- $pod = null;
1466
-
1467
- $has_fields = false;
1468
-
1469
- foreach ( $groups as $group ) {
1470
- if ( empty( $group[ 'fields' ] ) )
1471
- continue;
1472
-
1473
- if ( null === $term ) {
1474
- $term = get_term( $term_id, $taxonomy );
1475
-
1476
- $data = array(
1477
- 'name' => $term->name
1478
- );
1479
- }
1480
-
1481
- $has_fields = true;
1482
-
1483
- if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1484
- if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1485
- self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1486
- elseif ( self::$current_pod->id() != $id )
1487
- self::$current_pod->fetch( $id );
1488
-
1489
- $pod = self::$current_pod;
1490
- }
1491
-
1492
- foreach ( $group[ 'fields' ] as $field ) {
1493
-
1494
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1495
- if ( !pods_var( 'hidden', $field[ 'options' ], false ) )
1496
- continue;
1497
- }
1498
-
1499
- $data[ $field[ 'name' ] ] = '';
1500
-
1501
- if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
1502
- $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
1503
- }
1504
- }
1505
-
1506
- if ( !$has_fields ) {
1507
- return $term_id;
1508
- }
1509
-
1510
- if ( $is_new_item ) {
1511
- do_action( 'pods_meta_create_pre_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
1512
- do_action( "pods_meta_create_pre_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
1513
- }
1514
-
1515
- do_action( 'pods_meta_save_pre_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item );
1516
- do_action( "pods_meta_save_pre_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item );
1517
-
1518
- pods_no_conflict_on( 'taxonomy' );
1519
-
1520
- if ( !empty( $pod ) ) {
1521
- // Fix for Pods doing it's own sanitization
1522
- $data = pods_unslash( (array) $data );
1523
-
1524
- $pod->save( $data, null, null, array( 'is_new_item' => $is_new_item ) );
1525
- }
1526
-
1527
- pods_no_conflict_off( 'taxonomy' );
1528
-
1529
- if ( $is_new_item ) {
1530
- do_action( 'pods_meta_create_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
1531
- do_action( "pods_meta_create_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy );
1532
- }
1533
-
1534
- do_action( 'pods_meta_save_taxonomy', $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item );
1535
- do_action( "pods_meta_save_taxonomy_{$taxonomy}", $data, $pod, $id, $groups, $term_id, $term_taxonomy_id, $taxonomy, $is_new_item );
1536
-
1537
- return $term_id;
1538
- }
1539
-
1540
- /**
1541
- * @param $user_id
1542
- */
1543
- public function meta_user ( $user_id ) {
1544
- wp_enqueue_style( 'pods-form' );
1545
-
1546
- do_action( 'pods_meta_' . __FUNCTION__, $user_id );
1547
-
1548
- $groups = $this->groups_get( 'user', 'user' );
1549
-
1550
- if ( is_object( $user_id ) )
1551
- $user_id = $user_id->ID;
1552
-
1553
- $id = $user_id;
1554
- $pod = null;
1555
-
1556
- foreach ( $groups as $group ) {
1557
- if ( empty( $group[ 'fields' ] ) )
1558
- continue;
1559
-
1560
- if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1561
- if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1562
- self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1563
- elseif ( self::$current_pod->id() != $id )
1564
- self::$current_pod->fetch( $id );
1565
-
1566
- $pod = self::$current_pod;
1567
- }
1568
-
1569
- $hidden_fields = array();
1570
- ?>
1571
- <h3><?php echo $group[ 'label' ]; ?></h3>
1572
-
1573
- <?php echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_user' ), 'hidden' ); ?>
1574
-
1575
- <table class="form-table pods-meta">
1576
- <tbody>
1577
- <?php
1578
- foreach ( $group[ 'fields' ] as $field ) {
1579
-
1580
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1581
- if ( pods_var( 'hidden', $field[ 'options' ], false ) )
1582
- $field[ 'type' ] = 'hidden';
1583
- else
1584
- continue;
1585
- }
1586
- elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
1587
- $field[ 'type' ] = 'hidden';
1588
-
1589
- $value = '';
1590
-
1591
- if ( !empty( $pod ) )
1592
- $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1593
- elseif ( !empty( $id ) ) {
1594
- pods_no_conflict_on( 'user' );
1595
-
1596
- $value = get_user_meta( $id, $field[ 'name' ], true );
1597
-
1598
- pods_no_conflict_off( 'user' );
1599
- }
1600
-
1601
- if ( 'hidden' == $field[ 'type' ] ) {
1602
- $hidden_fields[] = array(
1603
- 'field' => $field,
1604
- 'value' => $value
1605
- );
1606
- }
1607
- else {
1608
- ?>
1609
- <tr class="form-field pods-field <?php echo esc_attr( 'pods-form-ui-row-type-' . $field[ 'type' ] . ' pods-form-ui-row-name-' . PodsForm::clean( $field[ 'name' ], true ) ); ?>">
1610
- <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); ?></th>
1611
- <td>
1612
- <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?>
1613
- <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?>
1614
- </td>
1615
- </tr>
1616
- <?php
1617
- }
1618
- }
1619
- ?>
1620
- </tbody>
1621
- </table>
1622
- <?php
1623
- foreach ( $hidden_fields as $hidden_field ) {
1624
- $field = $hidden_field[ 'field' ];
1625
-
1626
- echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $hidden_field[ 'value' ], 'hidden' );
1627
- }
1628
- }
1629
-
1630
- do_action( 'pods_meta_' . __FUNCTION__ . '_post', $user_id );
1631
- }
1632
-
1633
- /**
1634
- * @param $user_id
1635
- */
1636
- public function save_user ( $user_id ) {
1637
-
1638
- $is_new_item = false;
1639
-
1640
- if ( 'user_register' == current_filter() ) {
1641
- $is_new_item = true;
1642
- }
1643
-
1644
- if ( empty( $_POST ) ) {
1645
- return $user_id;
1646
- }
1647
- elseif ( $is_new_item || !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_user' ) ) {
1648
- return $user_id;
1649
- }
1650
-
1651
- $groups = $this->groups_get( 'user', 'user' );
1652
-
1653
- if ( empty( $groups ) ) {
1654
- return $user_id;
1655
- }
1656
-
1657
- if ( is_object( $user_id ) ) {
1658
- $user_id = $user_id->ID;
1659
- }
1660
-
1661
- $data = array();
1662
-
1663
- $id = $user_id;
1664
- $pod = null;
1665
-
1666
- foreach ( $groups as $group ) {
1667
- if ( empty( $group[ 'fields' ] ) ) {
1668
- continue;
1669
- }
1670
-
1671
- if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1672
- if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] ) {
1673
- self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1674
- }
1675
- elseif ( self::$current_pod->id() != $id ) {
1676
- self::$current_pod->fetch( $id );
1677
- }
1678
-
1679
- $pod = self::$current_pod;
1680
- }
1681
-
1682
- foreach ( $group[ 'fields' ] as $field ) {
1683
-
1684
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1685
- if ( !pods_var( 'hidden', $field[ 'options' ], false ) ) {
1686
- continue;
1687
- }
1688
- }
1689
-
1690
- $data[ $field[ 'name' ] ] = '';
1691
-
1692
- if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) ) {
1693
- $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
1694
- }
1695
- }
1696
- }
1697
-
1698
- if ( $is_new_item ) {
1699
- do_action( 'pods_meta_create_pre_user', $data, $pod, $id, $groups );
1700
- }
1701
-
1702
- do_action( 'pods_meta_save_pre_user', $data, $pod, $id, $groups, $is_new_item );
1703
-
1704
- pods_no_conflict_on( 'user' );
1705
-
1706
- if ( !empty( $pod ) ) {
1707
- // Fix for Pods doing it's own sanitization
1708
- $data = pods_unslash( (array) $data );
1709
-
1710
- $pod->save( $data, null, null, array( 'is_new_item' => $is_new_item ) );
1711
- }
1712
- elseif ( !empty( $id ) ) {
1713
- foreach ( $data as $field => $value ) {
1714
- update_user_meta( $id, $field, $value );
1715
- }
1716
- }
1717
-
1718
- pods_no_conflict_off( 'user' );
1719
-
1720
- if ( $is_new_item ) {
1721
- do_action( 'pods_meta_create_user', $data, $pod, $id, $groups );
1722
- }
1723
-
1724
- do_action( 'pods_meta_save_user', $data, $pod, $id, $groups, $is_new_item );
1725
-
1726
- return $user_id;
1727
-
1728
- }
1729
-
1730
- /**
1731
- * @param $commenter
1732
- * @param $user_identity
1733
- */
1734
- public function meta_comment_new_logged_in ( $commenter, $user_identity ) {
1735
- wp_enqueue_style( 'pods-form' );
1736
-
1737
- do_action( 'pods_meta_' . __FUNCTION__, $commenter, $user_identity );
1738
-
1739
- $groups = $this->groups_get( 'comment', 'comment' );
1740
-
1741
- $id = null;
1742
- $pod = null;
1743
-
1744
- echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_comment' ), 'hidden' );
1745
-
1746
- foreach ( $groups as $group ) {
1747
- if ( empty( $group[ 'fields' ] ) )
1748
- continue;
1749
-
1750
- if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1751
- if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1752
- self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1753
- elseif ( self::$current_pod->id() != $id )
1754
- self::$current_pod->fetch( $id );
1755
-
1756
- $pod = self::$current_pod;
1757
- }
1758
-
1759
- foreach ( $group[ 'fields' ] as $field ) {
1760
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1761
- if ( pods_var( 'hidden', $field[ 'options' ], false ) )
1762
- $field[ 'type' ] = 'hidden';
1763
- else
1764
- continue;
1765
- }
1766
- elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
1767
- $field[ 'type' ] = 'hidden';
1768
-
1769
- $value = '';
1770
-
1771
- if ( !empty( $pod ) )
1772
- $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1773
- elseif ( !empty( $id ) ) {
1774
- pods_no_conflict_on( 'comment' );
1775
-
1776
- $value = get_comment_meta( $id, $field[ 'name' ], true );
1777
-
1778
- pods_no_conflict_off( 'comment' );
1779
- }
1780
- ?>
1781
- <p class="comment-form-author comment-form-pods-meta-<?php echo esc_attr( $field[ 'name' ] ); ?> pods-field" style="<?php echo esc_attr( 'hidden' == $field[ 'type' ] ? 'display:none;' : '' ); ?>">
1782
- <?php
1783
- echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field );
1784
- echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
1785
- echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
1786
- ?>
1787
- </p>
1788
- <?php
1789
- }
1790
- }
1791
-
1792
- do_action( 'pods_meta_' . __FUNCTION__ . '_post', $commenter, $user_identity );
1793
- }
1794
-
1795
- /**
1796
- * @param $form_fields
1797
- *
1798
- * @return array
1799
- */
1800
- public function meta_comment_new ( $form_fields ) {
1801
- wp_enqueue_style( 'pods-form' );
1802
-
1803
- $groups = $this->groups_get( 'comment', 'comment' );
1804
-
1805
- $id = null;
1806
- $pod = null;
1807
-
1808
- $form_fields[ 'pods_meta' ] = PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_comment' ), 'hidden' );
1809
-
1810
- foreach ( $groups as $group ) {
1811
- if ( empty( $group[ 'fields' ] ) )
1812
- continue;
1813
-
1814
- if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
1815
- if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
1816
- self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
1817
- elseif ( self::$current_pod->id() != $id )
1818
- self::$current_pod->fetch( $id );
1819
-
1820
- $pod = self::$current_pod;
1821
- }
1822
-
1823
- foreach ( $group[ 'fields' ] as $field ) {
1824
-
1825
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
1826
- if ( pods_var( 'hidden', $field[ 'options' ], false ) )
1827
- $field[ 'type' ] = 'hidden';
1828
- else
1829
- continue;
1830
- }
1831
- elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
1832
- $field[ 'type' ] = 'hidden';
1833
-
1834
- $value = '';
1835
-
1836
- if ( !empty( $pod ) )
1837
- $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1838
- elseif ( !empty( $id ) ) {
1839
- pods_no_conflict_on( 'comment' );
1840
-
1841
- $value = get_comment_meta( $id, $field[ 'name' ], true );
1842
-
1843
- pods_no_conflict_off( 'comment' );
1844
- }
1845
-
1846
- ob_start();
1847
- ?>
1848
- <p class="comment-form-author comment-form-pods-meta-<?php echo esc_attr( $field[ 'name' ] ); ?> pods-field" style="<?php echo esc_attr( 'hidden' == $field[ 'type' ] ? 'display:none;' : '' ); ?>">
1849
- <?php
1850
- echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field );
1851
- echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id );
1852
- echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field );
1853
- ?>
1854
- </p>
1855
- <?php
1856
- $form_fields[ 'pods_meta_' . $field[ 'name' ] ] = ob_get_clean();
1857
- }
1858
- }
1859
-
1860
- $form_fields = apply_filters( 'pods_meta_' . __FUNCTION__, $form_fields );
1861
-
1862
- return $form_fields;
1863
- }
1864
-
1865
- /**
1866
- * @param $comment_type
1867
- * @param null $comment
1868
- */
1869
- public function meta_comment_add ( $comment_type, $comment = null ) {
1870
- if ( is_object( $comment ) && isset( $comment_type->comment_type ) )
1871
- $comment_type = $comment->comment_type;
1872
-
1873
- if ( is_object( $comment_type ) && isset( $comment_type->comment_type ) ) {
1874
- $comment = $comment_type;
1875
- $comment_type = $comment_type->comment_type;
1876
- }
1877
-
1878
- if ( is_object( $comment_type ) )
1879
- return;
1880
- elseif ( empty( $comment_type ) )
1881
- $comment_type = 'comment';
1882
-
1883
- $groups = $this->groups_get( 'comment', $comment_type );
1884
-
1885
- foreach ( $groups as $group ) {
1886
- if ( empty( $group[ 'fields' ] ) )
1887
- continue;
1888
-
1889
- $field_found = false;
1890
-
1891
- foreach ( $group[ 'fields' ] as $field ) {
1892
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], null, null ) ) {
1893
- if ( pods_var( 'hidden', $field[ 'options' ], false ) ) {
1894
- $field_found = true;
1895
- break;
1896
- }
1897
- else {
1898
- continue;
1899
- }
1900
- }
1901
- else {
1902
- $field_found = true;
1903
- break;
1904
- }
1905
- }
1906
-
1907
- if ( $field_found ) {
1908
- add_meta_box(
1909
- 'pods-meta-' . sanitize_title( $group[ 'label' ] ),
1910
- $group[ 'label' ],
1911
- array( $this, 'meta_comment' ),
1912
- $comment_type,
1913
- $group[ 'context' ],
1914
- $group[ 'priority' ],
1915
- array( 'group' => $group )
1916
- );
1917
- }
1918
- }
1919
- }
1920
-
1921
- /**
1922
- * @param $comment
1923
- * @param $metabox
1924
- */
1925
- public function meta_comment ( $comment, $metabox ) {
1926
- wp_enqueue_style( 'pods-form' );
1927
-
1928
- do_action( 'pods_meta_' . __FUNCTION__, $comment, $metabox );
1929
-
1930
- $hidden_fields = array();
1931
-
1932
- echo PodsForm::field( 'pods_meta', wp_create_nonce( 'pods_meta_comment' ), 'hidden' );
1933
- ?>
1934
- <table class="form-table editcomment pods-metabox">
1935
- <?php
1936
- $id = null;
1937
-
1938
- if ( is_object( $comment ) )
1939
- $id = $comment->comment_ID;
1940
-
1941
- if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ] )
1942
- self::$current_pod = pods( $metabox[ 'args' ][ 'group' ][ 'pod' ][ 'name' ], $id, true );
1943
- elseif ( self::$current_pod->id() != $id )
1944
- self::$current_pod->fetch( $id );
1945
-
1946
- $pod = self::$current_pod;
1947
-
1948
- foreach ( $metabox[ 'args' ][ 'group' ][ 'fields' ] as $field ) {
1949
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $metabox[ 'args' ][ 'group' ][ 'fields' ], $pod, $id ) ) {
1950
- if ( pods_var( 'hidden', $field[ 'options' ], false ) )
1951
- $field[ 'type' ] = 'hidden';
1952
- else
1953
- continue;
1954
- }
1955
- elseif ( !pods_has_permissions( $field[ 'options' ] ) && pods_var( 'hidden', $field[ 'options' ], false ) )
1956
- $field[ 'type' ] = 'hidden';
1957
-
1958
- $value = '';
1959
-
1960
- if ( !empty( $pod ) )
1961
- $value = $pod->field( array( 'name' => $field[ 'name' ], 'in_form' => true ) );
1962
-
1963
- if ( 'hidden' == $field[ 'type' ] ) {
1964
- $hidden_fields[] = array(
1965
- 'field' => $field,
1966
- 'value' => $value
1967
- );
1968
- }
1969
- else {
1970
- ?>
1971
- <tr class="form-field pods-field <?php echo esc_attr( 'pods-form-ui-row-type-' . $field[ 'type' ] . ' pods-form-ui-row-name-' . PodsForm::clean( $field[ 'name' ], true ) ); ?>">
1972
- <th scope="row" valign="top"><?php echo PodsForm::label( 'pods_meta_' . $field[ 'name' ], $field[ 'label' ], $field[ 'help' ], $field ); ?></th>
1973
- <td>
1974
- <?php echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $value, $field[ 'type' ], $field, $pod, $id ); ?>
1975
- <?php echo PodsForm::comment( 'pods_meta_' . $field[ 'name' ], $field[ 'description' ], $field ); ?>
1976
- </td>
1977
- </tr>
1978
- <?php
1979
- }
1980
- }
1981
- ?>
1982
- </table>
1983
- <?php
1984
- foreach ( $hidden_fields as $hidden_field ) {
1985
- $field = $hidden_field[ 'field' ];
1986
-
1987
- echo PodsForm::field( 'pods_meta_' . $field[ 'name' ], $hidden_field[ 'value' ], 'hidden' );
1988
- }
1989
-
1990
- do_action( 'pods_meta_' . __FUNCTION__ . '_post', $comment, $metabox );
1991
- }
1992
-
1993
- /**
1994
- * @param $approved
1995
- * @param $commentdata
1996
- */
1997
- public function validate_comment ( $approved, $commentdata ) {
1998
- $groups = $this->groups_get( 'comment', 'comment' );
1999
-
2000
- if ( empty( $groups ) )
2001
- return $approved;
2002
-
2003
- $data = array();
2004
-
2005
- $pod = null;
2006
- $id = null;
2007
-
2008
- foreach ( $groups as $group ) {
2009
- if ( empty( $group[ 'fields' ] ) )
2010
- continue;
2011
-
2012
- if ( null === $pod || ( is_object( $pod ) && $pod->id() != $id ) ) {
2013
- if ( !is_object( self::$current_pod ) || self::$current_pod->pod != $group[ 'pod' ][ 'name' ] )
2014
- self::$current_pod = pods( $group[ 'pod' ][ 'name' ], $id, true );
2015
- elseif ( self::$current_pod->id() != $id )
2016
- self::$current_pod->fetch( $id );
2017
-
2018
- $pod = self::$current_pod;
2019
- }
2020
-
2021
- foreach ( $group[ 'fields' ] as $field ) {
2022
-
2023
- if ( false === PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field, $group[ 'fields' ], $pod, $id ) ) {
2024
- if ( !pods_var( 'hidden', $field[ 'options' ], false ) )
2025
- continue;
2026
- }
2027
-
2028
- $data[ $field[ 'name' ] ] = '';
2029
-
2030
- if ( isset( $_POST[ 'pods_meta_' . $field[ 'name' ] ] ) )
2031
- $data[ $field[ 'name' ] ] = $_POST[ 'pods_meta_' . $field[ 'name' ] ];
2032
-
2033
- $validate = pods_api()->handle_field_validation( $data[ $field[ 'name' ] ], $field[ 'name' ], pods_api()->get_wp_object_fields( 'comment' ), $pod->fields(), $pod, array() );
2034
-
2035
- if ( false === $validate )
2036
- $validate = sprintf( __( 'There was an issue validating the field %s', 'pods' ), $field[ 'label' ] );
2037
-
2038
- if ( !is_bool( $validate ) && !empty( $validate ) )
2039
- return pods_error( $validate, $this );
2040
- }
2041
- }
2042
-
2043
- return $approved;
2044
- }
2045
-
2046
- /**
2047
- * @param $comment_id
2048
- */
2049
- public function save_comment ( $comment_id ) {
2050
- $groups = $this->groups_get( 'comment', 'comment' );
2051
-
2052
- if ( empty( $groups ) ) {
2053
- return $comment_id;
2054
- }
2055
- elseif ( empty( $_POST ) ) {
2056
- return $comment_id;
2057
- }
2058
- elseif ( !wp_verify_nonce( pods_v( 'pods_meta', 'post' ), 'pods_meta_comment' ) ) {
2059
- return $comment_id;
2060
- }
2061
-
2062
- $data = array();
2063
-
2064
- $id = $comment_id;
2065
- $pod = null;
2066
-
2067
- foreach ( $groups as $group ) {
2068
- if ( empty( $group[ 'fields' ] ) )
2069
- continue;
2070
-
2071
- if ( nu