1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12: function papi_delete_property_meta_value( $post_id, $slug, $type = 'post' ) {
13: if ( $type === Papi_Core_Page::TYPE_OPTION || papi_is_option_page() ) {
14: return delete_option( papi_remove_papi( $slug ) );
15: }
16:
17: return delete_post_meta( $post_id, papi_remove_papi( $slug ) );
18: }
19:
20: 21: 22: 23: 24: 25: 26: 27:
28: function papi_from_property_array_slugs( array $values, $slug ) {
29: $results = [];
30:
31: if ( empty( $values ) ) {
32: return $results;
33: }
34:
35: for ( $i = 0; $i < $values[$slug]; $i++ ) {
36: $item = [];
37: $item_slug = $slug . '_' . $i . '_';
38: $keys = preg_grep( '/' . preg_quote( $item_slug ). '/' , array_keys( $values ) );
39:
40: foreach ( $keys as $key ) {
41: $arr_key = str_replace( $item_slug, '', $key );
42: $item[$arr_key] = $values[$key];
43: }
44:
45: $results[] = $item;
46: }
47:
48: return $results;
49: }
50:
51: 52: 53: 54: 55: 56: 57:
58: function papi_is_property( $value ) {
59: return $value instanceof Papi_Property;
60: }
61:
62: 63: 64: 65: 66: 67: 68:
69: function papi_get_box_property( array $properties ) {
70: $box_property = array_filter( $properties, function ( $property ) {
71: return ! is_object( $property );
72: } );
73:
74: if ( ! empty( $box_property ) && ! isset( $box_property[0] ) && ! isset( $box_property[0]['tab'] ) ) {
75: $property = papi_property( $properties );
76:
77: if ( ! $property->disabled() ) {
78: $property->_box_property = true;
79: $properties = [$property];
80: }
81: }
82:
83: return $properties;
84: }
85:
86: 87: 88: 89: 90: 91: 92: 93: 94:
95: function papi_get_options_and_properties( $file_or_options = [], $properties = [], $is_box = true ) {
96: $options = [];
97:
98: if ( is_array( $file_or_options ) ) {
99: if ( empty( $properties ) && $is_box ) {
100:
101: if ( isset( $file_or_options['title'] ) ) {
102: $options['title'] = $file_or_options['title'];
103: } else if ( isset( $file_or_options[0]->title ) ) {
104: $options['title'] = $file_or_options[0]->title;
105: if ( $file_or_options[0]->sidebar === false && $file_or_options[0]->required ) {
106: $options['_required'] = true;
107: }
108: } else if ( isset( $file_or_options[0]->options ) && isset( $file_or_options[0]->options['title'] ) ) {
109: $options['title'] = $file_or_options[0]->options['title'];
110: } else {
111: $options['title'] = '';
112: }
113: $properties = $file_or_options;
114: } else {
115: $options = array_merge( $options, $file_or_options );
116:
117: if ( ! $is_box ) {
118:
119: foreach ( $options as $key => $value ) {
120: if ( ! is_string( $key ) ) {
121: $properties[] = $value;
122: unset( $options[$key] );
123: }
124: }
125: }
126: }
127: } else if ( is_string( $file_or_options ) ) {
128:
129:
130: if ( preg_match( '/\.php$/', $file_or_options ) === 1 ) {
131: $values = $properties;
132: $template = papi_template( $file_or_options, $values );
133:
134:
135: $properties = [];
136: $options = $template;
137:
138:
139: foreach ( $options as $key => $value ) {
140: if ( ! is_string( $key ) ) {
141: $properties[] = $value;
142: unset( $options[$key] );
143: }
144: }
145: } else {
146:
147: $options['title'] = $file_or_options;
148: }
149: }
150:
151: return [$options, $properties];
152: }
153:
154: 155: 156: 157: 158: 159: 160:
161: function papi_get_property_class_name( $type ) {
162: if ( ! is_string( $type ) || empty( $type ) ) {
163: return;
164: }
165:
166: return 'Papi_Property_' . ucfirst( preg_replace( '/^Property/', '', $type ) );
167: }
168:
169: 170: 171: 172: 173: 174: 175: 176: 177:
178: function papi_get_property_meta_value( $post_id, $slug, $type = 'post' ) {
179: if ( $type === Papi_Core_Page::TYPE_OPTION || papi_is_option_page() ) {
180: $value = get_option( $slug, null );
181: } else {
182: $value = get_post_meta( $post_id, $slug, true );
183: }
184:
185: if ( papi_is_empty( $value ) ) {
186: return;
187: }
188:
189: return $value;
190: }
191:
192: 193: 194: 195: 196: 197: 198:
199: function papi_get_property_options( $options ) {
200: if ( ! is_array( $options ) ) {
201: if ( is_object( $options ) ) {
202: return $options;
203: }
204:
205: return;
206: }
207:
208: $property = Papi_Property::create( $options );
209:
210: return $property->get_options();
211: }
212:
213: 214: 215: 216: 217: 218: 219:
220: function papi_get_property_type( $type ) {
221: if ( papi_is_empty( $type ) ) {
222: return;
223: }
224:
225: return Papi_Property::factory( $type );
226: }
227:
228: 229: 230: 231: 232: 233: 234:
235: function papi_get_property_type_key( $str = '' ) {
236: $suffix = '_property';
237:
238: if ( ! is_string( $str ) || strlen( $str ) === 0 ) {
239: return $suffix;
240: }
241:
242: $len = strlen( $str );
243:
244: if ( isset( $str[$len - 1] ) && $str[$len - 1] === ']' ) {
245: $str = substr( $str, 0, $len - 1 );
246: return papi_get_property_type_key( $str ) . ']';
247: }
248:
249: return papi_remove_papi( $str . $suffix );
250: }
251:
252: 253: 254: 255: 256: 257: 258:
259: function papi_get_property_type_key_f( $str ) {
260: return papi_f( papi_get_property_type_key( $str ) );
261: }
262:
263: 264: 265: 266: 267: 268: 269:
270: function papi_is_property_type_key( $str = '' ) {
271: $pattern = '_property';
272: $pattern = str_replace( '_', '\_', $pattern );
273: $pattern = str_replace( '-', '\-', $pattern );
274: $pattern = '/' . $pattern . '$/';
275:
276: return preg_match( $pattern, $str ) === 1;
277: }
278:
279: 280: 281: 282: 283: 284: 285: 286:
287: function papi_property( $file_or_options, $values = [] ) {
288: if ( papi_is_empty( $file_or_options ) ) {
289: return;
290: }
291:
292: if ( is_array( $file_or_options ) ) {
293: $file_or_options = papi_get_property_options( $file_or_options );
294: }
295:
296: if ( is_string( $file_or_options ) && is_array( $values ) ) {
297: $file_or_options = papi_template( $file_or_options, $values );
298: }
299:
300: if ( is_object( $file_or_options ) ) {
301: return papi_get_property_type( $file_or_options );
302: }
303: }
304:
305: 306: 307: 308: 309:
310: function papi_render_property( $property ) {
311: $property = Papi_Property::factory( $property );
312:
313: if ( is_null( $property ) ) {
314: return;
315: }
316:
317: $property->render();
318: }
319:
320: 321: 322: 323: 324:
325: function papi_render_properties( array $properties ) {
326: if ( empty( $properties ) ) {
327: return;
328: }
329:
330:
331:
332: if ( isset( $properties[0]->tab ) && $properties[0]->tab ) {
333: new Papi_Admin_Meta_Box_Tabs( $properties );
334: } else {
335: ?>
336:
337: <table class="papi-table">
338: <tbody>
339: <?php
340: foreach ( $properties as $property ) {
341: papi_render_property( $property );
342: }
343: ?>
344: </tbody>
345: </table>
346:
347: <?php
348: }
349: }
350:
351: 352: 353: 354: 355: 356: 357:
358: function papi_require_text( $property ) {
359: if ( ! is_object( $property ) || ! $property->required ) {
360: return '';
361: }
362:
363: return esc_html__( '(required field)', 'papi' );
364: }
365:
366: 367: 368: 369: 370: 371: 372: 373:
374: function papi_required_html( $property, $text = false ) {
375: if ( ! is_object( $property ) || ! $property->required ) {
376: return '';
377: }
378:
379: return ' <span class="papi-rq" data-property-name="' . $property->title . '" data-property-id="' . $property->slug . '">' . ( $text ? papi_require_text( $property ) : '*' ) . '</span>';
380: }
381:
382: 383: 384: 385: 386: 387: 388:
389: function papi_populate_properties( $properties ) {
390: if ( ! is_array( $properties ) && ! is_object( $properties ) || empty( $properties ) ) {
391: return [];
392: }
393:
394: if ( is_object( $properties ) ) {
395: return [$properties];
396: }
397:
398: $results = [];
399:
400:
401: $properties = papi_get_box_property( $properties );
402:
403:
404: $properties = array_map( function ( $property ) {
405: if ( ! is_object( $property ) && is_array( $property ) && ! isset( $property['tab'] ) ) {
406: return papi_get_property_options( $property );
407: }
408:
409: return $property;
410: }, $properties );
411:
412:
413: $properties = array_reverse( $properties );
414:
415: foreach ( $properties as $property ) {
416: if ( isset( $property->tab ) && $property->tab ) {
417: $results[] = $property;
418: continue;
419: }
420:
421: $results[] = $property;
422: }
423:
424: if ( empty( $results ) || ( isset( $results[0]->tab ) && $results[0]->tab ) ) {
425: return $results;
426: }
427:
428: return papi_sort_order( $results );
429: }
430:
431: 432: 433: 434: 435: 436: 437: 438:
439: function papi_update_property_meta_value( array $meta = [] ) {
440: $meta = (object) $meta;
441: $option = papi_is_option_page();
442: $save_value = true;
443:
444: foreach ( papi_to_array( $meta->value ) as $key => $value ) {
445: if ( is_string( $key ) ) {
446: $save_value = false;
447: break;
448: }
449: }
450:
451: if ( ! isset( $meta->post_id ) ) {
452: $meta->post_id = 0;
453: }
454:
455: if ( ! $save_value && is_array( $meta->value ) ) {
456: $meta->value = [$meta->value];
457: }
458:
459: if ( papi_is_empty( $meta->value ) ) {
460: papi_cache_delete( $meta->slug, $meta->post_id );
461:
462: if ( $option ) {
463: return delete_option( papi_remove_papi( $meta->slug ) );
464: } else {
465: return delete_post_meta( $meta->post_id, papi_remove_papi( $meta->slug ) );
466: }
467: }
468:
469: $result = true;
470:
471: foreach ( papi_to_array( $meta->value ) as $key => $value ) {
472: papi_cache_delete( $meta->slug, $meta->post_id );
473:
474: if ( ! is_array( $value ) ) {
475: if ( $save_value ) {
476: $value = $meta->value;
477: }
478:
479: if ( $option ) {
480: $out = update_option( papi_remove_papi( $meta->slug ), $value );
481: $result = $out ? $result : $out;
482: } else {
483: $out = update_post_meta( $meta->post_id, papi_remove_papi( $meta->slug ), $value );
484: $result = $out ? $result : $out;
485: }
486:
487: continue;
488: }
489:
490: foreach ( $value as $child_key => $child_value ) {
491: if ( papi_is_empty( $child_value ) ) {
492: if ( $option ) {
493: delete_option( papi_remove_papi( $child_key ) );
494: } else {
495: delete_post_meta( $meta->post_id, papi_remove_papi( $child_key ) );
496: }
497: } else {
498: if ( $option ) {
499: update_option( papi_remove_papi( $child_key ), $child_value );
500: } else {
501: update_post_meta( $meta->post_id, papi_remove_papi( $child_key ), $child_value );
502: }
503: }
504: }
505: }
506:
507: return $result;
508: }
509:
510: 511: 512: 513: 514: 515: 516: 517: 518:
519: function papi_to_property_array_slugs( array $value, $slug ) {
520: $results = [];
521: $counter = [];
522:
523: foreach ( $value as $index => $arr ) {
524:
525: if ( ! is_array( $arr ) ) {
526: continue;
527: }
528:
529: $counter[] = $arr;
530:
531: foreach ( $arr as $key => $val ) {
532:
533: if ( ! is_string( $key ) || empty( $key ) ) {
534: continue;
535: }
536:
537: if ( $key[0] !== '_' ) {
538: $key = '_' . $key;
539: }
540:
541: $item_slug = $slug . '_' . $index . $key;
542:
543: if ( papi_is_property_type_key( $item_slug ) ) {
544: $item_slug = papi_f( $item_slug );
545: }
546:
547: $results[$item_slug] = $val;
548: }
549: }
550:
551: $results[$slug] = count( $counter );
552:
553: return $results;
554: }
555: