Overview

Classes

  • Papi_Admin
  • Papi_Admin_Ajax
  • Papi_Admin_Assets
  • Papi_Admin_Management_Pages
  • Papi_Admin_Menu
  • Papi_Admin_Meta_Box
  • Papi_Admin_Meta_Box_Tabs
  • Papi_Admin_Option_Handler
  • Papi_Admin_Post_Handler
  • Papi_Admin_View
  • Papi_Attachment_Type
  • Papi_Conditional_Rules
  • Papi_Container
  • Papi_Core_Autoload
  • Papi_Core_Conditional
  • Papi_Core_Conditional_Rule
  • Papi_Core_Data_Handler
  • Papi_Core_Page
  • Papi_Core_Property
  • Papi_Core_Type
  • Papi_Loader
  • Papi_Option_Page
  • Papi_Option_Type
  • Papi_Page_Type
  • Papi_Page_Type_Meta
  • Papi_Porter
  • Papi_Porter_Driver
  • Papi_Porter_Driver_Core
  • Papi_Post_Page
  • Papi_Property
  • Papi_Property_Bool
  • Papi_Property_Checkbox
  • Papi_Property_Color
  • Papi_Property_Datetime
  • Papi_Property_Divider
  • Papi_Property_Dropdown
  • Papi_Property_Editor
  • Papi_Property_Email
  • Papi_Property_File
  • Papi_Property_Flexible
  • Papi_Property_Gallery
  • Papi_Property_Hidden
  • Papi_Property_Html
  • Papi_Property_Image
  • Papi_Property_Link
  • Papi_Property_Number
  • Papi_Property_Post
  • Papi_Property_Radio
  • Papi_Property_Reference
  • Papi_Property_Relationship
  • Papi_Property_Repeater
  • Papi_Property_String
  • Papi_Property_Term
  • Papi_Property_Text
  • Papi_Property_Url
  • Papi_Property_User

Functions

  • current_page
  • papi
  • papi_action_delete_value
  • papi_action_include
  • papi_append_post_type_query
  • papi_body_class
  • papi_cache_delete
  • papi_cache_get
  • papi_cache_key
  • papi_cache_set
  • papi_camel_case
  • papi_cast_string_value
  • papi_convert_to_string
  • papi_current_user_is_allowed
  • papi_dashify
  • papi_delete_field
  • papi_delete_option
  • papi_delete_property_meta_value
  • papi_display_page_type
  • papi_doing_ajax
  • papi_esc_html
  • papi_f
  • papi_field
  • papi_field_shortcode
  • papi_field_value
  • papi_fields
  • papi_filter_conditional_rule_allowed
  • papi_filter_core_load_one_type_on
  • papi_filter_format_value
  • papi_filter_load_value
  • papi_filter_settings_directories
  • papi_filter_settings_only_page_type
  • papi_filter_settings_page_type_column_title
  • papi_filter_settings_show_page_type
  • papi_filter_settings_show_standard_page_type
  • papi_filter_settings_show_standard_page_type_in_filter
  • papi_filter_settings_sort_order
  • papi_filter_settings_standard_page_description
  • papi_filter_settings_standard_page_name
  • papi_filter_settings_standard_page_thumbnail
  • papi_filter_update_value
  • papi_from_property_array_slugs
  • papi_get_all_files_in_directory
  • papi_get_all_page_type_files
  • papi_get_all_page_types
  • papi_get_box_property
  • papi_get_class_name
  • papi_get_field
  • papi_get_file_path
  • papi_get_number_of_pages
  • papi_get_only_objects
  • papi_get_option
  • papi_get_options_and_properties
  • papi_get_or_post
  • papi_get_page
  • papi_get_page_new_url
  • papi_get_page_query_strings
  • papi_get_page_type
  • papi_get_page_type_base_path
  • papi_get_page_type_by_id
  • papi_get_page_type_by_post_id
  • papi_get_page_type_id
  • papi_get_page_type_key
  • papi_get_page_type_meta_value
  • papi_get_page_type_name
  • papi_get_page_type_template
  • papi_get_parent_post_id
  • papi_get_post_id
  • papi_get_post_type
  • papi_get_post_type_label
  • papi_get_post_types
  • papi_get_property_class_name
  • papi_get_property_meta_value
  • papi_get_property_options
  • papi_get_property_type
  • papi_get_property_type_key
  • papi_get_property_type_key_f
  • papi_get_qs
  • papi_get_sanitized_post
  • papi_get_slugs
  • papi_get_tab_options
  • papi_html_name
  • papi_html_tag
  • papi_include_template
  • papi_is_empty
  • papi_is_json
  • papi_is_method
  • papi_is_option_page
  • papi_is_option_type
  • papi_is_page_type
  • papi_is_property
  • papi_is_property_type_key
  • papi_is_rule
  • papi_management_page_type_render_box
  • papi_maybe_convert_to_array
  • papi_maybe_convert_to_object
  • papi_maybe_get_callable_value
  • papi_maybe_json_decode
  • papi_maybe_json_encode
  • papi_nl2br
  • papi_option_shortcode
  • papi_option_type_exists
  • papi_page_type_exists
  • papi_populate_properties
  • papi_property
  • papi_remove_papi
  • papi_remove_trailing_quotes
  • papi_render_html_tag
  • papi_render_properties
  • papi_render_property
  • papi_require_text
  • papi_required_html
  • papi_rule
  • papi_santize_data
  • papi_set_page_type_id
  • papi_setup_tabs
  • papi_slugify
  • papi_sort_order
  • papi_tab
  • papi_template
  • papi_template_include
  • papi_to_array
  • papi_to_property_array_slugs
  • papi_translate_keys
  • papi_underscorify
  • papi_update_field
  • papi_update_option
  • papi_update_property_meta_value
  • papi_with
  • papify
  • the_papi_field
  • the_papi_option
  • the_papi_page_type_name
  • Overview
  • Class
  1: <?php
  2: 
  3: /**
  4:  * Core class that implements a Papi property.
  5:  */
  6: class Papi_Core_Property {
  7: 
  8:     /**
  9:      * The conditional class.
 10:      *
 11:      * @var Papi_Core_Conditional
 12:      */
 13:     protected $conditional;
 14: 
 15:     /**
 16:      * The convert type.
 17:      *
 18:      * @var string
 19:      */
 20:     public $convert_type = 'string';
 21: 
 22:     /**
 23:      * Default import settings.
 24:      *
 25:      * @var array
 26:      */
 27:     private $default_import_settings = [
 28:         'property_array_slugs' => false
 29:     ];
 30: 
 31:     /**
 32:      * Default options.
 33:      *
 34:      * @var array
 35:      */
 36:     protected $default_options = [
 37:         'after_class'  => '',
 38:         'after_html'   => '',
 39:         'before_class' => '',
 40:         'before_html'  => '',
 41:         'capabilities' => [],
 42:         'default'      => '',
 43:         'description'  => '',
 44:         'disabled'     => false,
 45:         'display'      => true,
 46:         'lang'         => false,
 47:         'overwrite'    => false,
 48:         'post_type'    => '',
 49:         'raw'          => false,
 50:         'required'     => false,
 51:         'rules'        => [],
 52:         'settings'     => [],
 53:         'sidebar'      => true,
 54:         'slug'         => '',
 55:         'sort_order'   => -1,
 56:         'title'        => '',
 57:         'type'         => '',
 58:         'value'        => ''
 59:     ];
 60: 
 61:     /**
 62:      * Default value.
 63:      *
 64:      * @var null
 65:      */
 66:     public $default_value;
 67: 
 68:     /**
 69:      * Display the property in WordPress admin.
 70:      *
 71:      * @var bool
 72:      */
 73:     protected $display = true;
 74: 
 75:     /**
 76:      * Current property options object.
 77:      *
 78:      * @var stdClass
 79:      */
 80:     private $options;
 81: 
 82:     /**
 83:      * The page that the property exists on.
 84:      *
 85:      * @var Papi_Core_Page
 86:      */
 87:     private $page;
 88: 
 89:     /**
 90:      * The post id.
 91:      *
 92:      * @var int
 93:      */
 94:     private $post_id;
 95: 
 96:     /**
 97:      * The constructor.
 98:      */
 99:     public function __construct() {
100:         $this->setup_actions();
101:         $this->setup_conditional();
102:         $this->setup_default_options();
103:         $this->setup_filters();
104:     }
105: 
106:     /**
107:      * Get option value dynamic.
108:      *
109:      * @param  string $key
110:      *
111:      * @return mixed
112:      */
113:     public function __get( $key ) {
114:         return $this->get_option( $key );
115:     }
116: 
117:     /**
118:      * Check if options value exists or not.
119:      *
120:      * @param  string $key
121:      *
122:      * @return bool
123:      */
124:     public function __isset( $key ) {
125:         return $this->get_option( $key ) !== null;
126:     }
127: 
128:     /**
129:      * Set options value dynamic.
130:      *
131:      * @param string $key
132:      * @param mixed  $value
133:      */
134:     public function __set( $key, $value ) {
135:         $this->set_option( $key, $value );
136:     }
137: 
138:     /**
139:      * Create a property from options.
140:      *
141:      * @param  array|object $options
142:      *
143:      * @return Papi_Property
144:      */
145:     public static function create( $options = [] ) {
146:         $property = new static;
147:         $property->set_options( $options );
148:         return $property;
149:     }
150: 
151:     /**
152:      * Convert settings items to properties if they are a property.
153:      *
154:      * @param  array $settings
155:      *
156:      * @return array
157:      */
158:     private function convert_settings( $settings ) {
159:         foreach ( $settings as $key => $value ) {
160:             if ( ! is_array( $value ) ) {
161:                 continue;
162:             }
163: 
164:             $settings[$key] = $this->convert_items_array( $value );
165:         }
166: 
167:         return $settings;
168:     }
169: 
170:     /**
171:      * Convert all arrays that has a valid property type.
172:      *
173:      * @param  array $items
174:      *
175:      * @return array
176:      */
177:     private function convert_items_array( $items ) {
178:         foreach ( $items as $index => $item ) {
179:             if ( is_array( $item ) && ! isset( $item['type'] ) ) {
180:                 foreach ( $item as $key => $value ) {
181:                     if ( is_array( $value ) ) {
182:                         $items[$index][$key] = $this->convert_items_array( $value );
183:                         $items[$index][$key] = array_filter( $items[$index][$key] );
184:                         $items[$index][$key] = array_values( $items[$index][$key] );
185:                     }
186:                 }
187: 
188:                 continue;
189:             }
190: 
191:             if ( papi_is_property( $item ) ) {
192:                 $child_items = $item->get_setting( 'items' );
193: 
194:                 if ( is_array( $child_items ) ) {
195:                     $items[$index]->set_setting( 'items', $this->convert_items_array( $child_items ) );
196:                 }
197: 
198:                 continue;
199:             }
200: 
201:             if ( ( is_array( $item ) && isset( $item['type'] ) ) || ( is_object( $item ) && isset( $item->type ) ) ) {
202:                 $items[$index] = papi_get_property_type( $item );
203: 
204:                 if ( is_null( $items[$index] ) ) {
205:                     unset( $items[$index] );
206:                     continue;
207:                 }
208: 
209:                 if ( is_object( $items[$index] ) ) {
210:                     $child_items = $items[$index]->get_setting( 'items' );
211: 
212:                     if ( is_array( $child_items ) ) {
213:                         $items[$index]->set_setting( 'items', $this->convert_items_array( $child_items ) );
214:                     }
215:                 }
216:             }
217:         }
218: 
219:         return $items;
220:     }
221: 
222:     /**
223:      * Delete value from the database.
224:      *
225:      * @param  string $slug
226:      * @param  int    $post_id
227:      * @param  string $type
228:      *
229:      * @return bool
230:      */
231:     public function delete_value( $slug, $post_id, $type ) {
232:         if ( $type === Papi_Core_Page::TYPE_OPTION || $this->is_option_page() ) {
233:             return delete_option( $slug );
234:         }
235: 
236:         return delete_post_meta( $post_id, $slug );
237:     }
238: 
239:     /**
240:      * Determine if the property is disabled or not.
241:      *
242:      * @return bool
243:      */
244:     public function disabled() {
245:         // If the post type don't match the current one
246:         // the property should not be rendered.
247:         if ( ! empty( $this->post_type ) && $this->post_type !== papi_get_post_type() ) {
248:             return true;
249:         }
250: 
251:         return $this->disabled;
252:     }
253: 
254:     /**
255:      * Determine if the property should be displayed.
256:      *
257:      * @return bool
258:      */
259:     public function display() {
260:         // If the property display is true it can be changed with
261:         // the display option.
262:         return $this->display ? $this->options->display : false;
263:     }
264: 
265:     /**
266:      * Create a new instance of the given type.
267:      *
268:      * @param  mixed $type
269:      *
270:      * @return object
271:      */
272:     public static function factory( $type ) {
273:         if ( is_array( $type ) ) {
274:             $prop = self::create( $type );
275:             $type = $prop->get_options();
276:         }
277: 
278:         if ( ! is_string( $type ) && ! is_object( $type ) ) {
279:             return;
280:         }
281: 
282:         if ( is_subclass_of( $type, __CLASS__ ) ) {
283:             return $type;
284:         }
285: 
286:         $options = null;
287: 
288:         if ( is_object( $type ) ) {
289:             if ( ! isset( $type->type ) || ! is_string( $type->type ) ) {
290:                 return;
291:             }
292: 
293:             $options = $type;
294:             $type = $type->type;
295:         }
296: 
297:         $type = preg_replace( '/^Property/', '', $type );
298: 
299:         if ( empty( $type ) ) {
300:             return;
301:         }
302: 
303:         $class_name = papi_get_property_class_name( $type );
304: 
305:         if ( ! class_exists( $class_name ) || ! is_subclass_of( $class_name, __CLASS__ ) ) {
306:             return;
307:         }
308: 
309:         if ( ! papi()->exists( $class_name ) ) {
310:             papi()->bind( $class_name, new $class_name() );
311:         }
312: 
313:         $class = papi()->make( $class_name );
314: 
315:         if ( ! is_object( $class ) || $class instanceof Papi_Core_Property === false ) {
316:             $class = new $class_name();
317:             papi()->bind( $class_name, $class );
318:         }
319: 
320:         $property = clone $class;
321: 
322:         if ( is_object( $options ) ) {
323:             $property->set_options( $options );
324:         }
325: 
326:         return $property;
327:     }
328: 
329:     /**
330:      * Format the value of the property before it's returned
331:      * to WordPress admin or the site.
332:      *
333:      * @param  mixed  $value
334:      * @param  string $slug
335:      * @param  int    $post_id
336:      *
337:      * @return mixed
338:      */
339:     public function format_value( $value, $slug, $post_id ) {
340:         return papi_maybe_json_decode(
341:             maybe_unserialize( $value ),
342:             $this->convert_type === 'array'
343:         );
344:     }
345: 
346:     /**
347:      * Get child properties from `items` in the settings array.
348:      *
349:      * @return mixed
350:      */
351:     public function get_child_properties() {
352:         return $this->get_setting( 'items', [] );
353:     }
354: 
355:     /**
356:      * Get default settings.
357:      *
358:      * @return array
359:      */
360:     public function get_default_settings() {
361:         return [];
362:     }
363: 
364:     /**
365:      * Get import settings.
366:      *
367:      * @return array
368:      */
369:     public function get_import_settings() {
370:         return [];
371:     }
372: 
373:     /**
374:      * Get option value.
375:      *
376:      * @param  string $key
377:      *
378:      * @return mixed
379:      */
380:     public function get_option( $key ) {
381:         if ( isset( $this->options->$key ) ) {
382:             return $this->options->$key;
383:         }
384: 
385:         if ( isset( $this->default_options[$key] ) ) {
386:             $option = $this->default_options[$key];
387: 
388:             if ( $key === 'settings' ) {
389:                 $option = (object) $option;
390:             }
391: 
392:             return $option;
393:         }
394:     }
395: 
396:     /**
397:      * Get the current property options object.
398:      *
399:      * @return stdClass
400:      */
401:     public function get_options() {
402:         return $this->options;
403:     }
404: 
405:     /**
406:      * Get the page that the property is on.
407:      *
408:      * @return Papi_Core_Page|null
409:      */
410:     public function get_page() {
411:         if ( $this->page instanceof Papi_Core_Page ) {
412:             return $this->page;
413:         }
414: 
415:         return papi_get_page( $this->get_post_id() );
416:     }
417: 
418:     /**
419:      * Get post id.
420:      *
421:      * @return int
422:      */
423:     public function get_post_id() {
424:         if ( ! papi_is_empty( $this->post_id ) ) {
425:             return $this->post_id;
426:         }
427: 
428:         if ( $this->page instanceof Papi_Core_Page ) {
429:             return $this->page->id;
430:         }
431: 
432:         return papi_get_post_id();
433:     }
434: 
435:     /**
436:      * Get conditional rules.
437:      *
438:      * @return array
439:      */
440:     public function get_rules() {
441:         return $this->get_option( 'rules' );
442:     }
443: 
444:     /**
445:      * Get setting value.
446:      *
447:      * @param  string $key
448:      * @param  mixed  $default
449:      *
450:      * @return stdClass
451:      */
452:     public function get_setting( $key, $default = null ) {
453:         if ( ! is_string( $key ) ) {
454:             return $default;
455:         }
456: 
457:         $settings = $this->get_settings();
458: 
459:         if ( isset( $settings->$key ) ) {
460:             return $settings->$key;
461:         }
462: 
463:         return $default;
464:     }
465: 
466:     /**
467:      * Get custom property settings.
468:      *
469:      * @return stdClass
470:      */
471:     public function get_settings() {
472:         $settings = wp_parse_args(
473:             $this->get_option( 'settings' ),
474:             $this->get_default_settings()
475:         );
476: 
477:         return (object) $this->convert_settings( $settings );
478:     }
479: 
480:     /**
481:      * Get property slug.
482:      *
483:      * @param  bool $remove_prefix
484:      *
485:      * @return string
486:      */
487:     public function get_slug( $remove_prefix = false ) {
488:         if ( $remove_prefix ) {
489:             return papi_remove_papi( $this->get_option( 'slug' ) );
490:         }
491: 
492:         return $this->get_option( 'slug' );
493:     }
494: 
495:     /**
496:      * Get value.
497:      *
498:      * @return mixed
499:      */
500:     public function get_value() {
501:         $value = $this->get_option( 'value' );
502: 
503:         if ( papi_is_empty( $value ) ) {
504:             $slug = $this->get_slug( true );
505: 
506:             if ( $this->is_option_page() ) {
507:                 $value = papi_get_option( $slug );
508:             } else {
509:                 $value = papi_get_field( $this->get_post_id(), $slug );
510:             }
511: 
512:             $post_status = get_post_status( $this->get_post_id() );
513: 
514:             if ( papi_is_empty( $value ) && ( $post_status === false || $post_status === 'auto-draft' ) ) {
515:                 $value = $this->get_option( 'default' );
516:             }
517:         }
518: 
519:         return $this->prepare_value( $value );
520:     }
521: 
522:     /**
523:      * Get the html id attribute value.
524:      *
525:      * @param  object|string $suffix
526:      * @param  int $row
527:      *
528:      * @return string
529:      */
530:     public function html_id( $suffix = '', $row = null ) {
531:         if ( is_array( $suffix ) || is_object( $suffix ) ) {
532:             return '_' . $this->html_name( $suffix, $row );
533:         } else {
534:             $suffix = empty( $suffix ) ||
535:                 ! is_string( $suffix ) ? '' : '_' . $suffix;
536:             $suffix = papi_underscorify( papi_slugify( $suffix ) );
537:         }
538: 
539:         $name = $this->html_name();
540: 
541:         if ( $name[strlen( $name ) - 1] === ']' ) {
542:             return sprintf( '_%s%s]', substr( $name, 0, strlen( $name ) - 1 ), $suffix );
543:         }
544: 
545:         return sprintf( '_%s%s', $this->html_name(), $suffix );
546:     }
547: 
548:     /**
549:      * Get html name for property with or without sub property and row number.
550:      *
551:      * @param  array|object $sub_property
552:      * @param  int $row
553:      *
554:      * @return string
555:      */
556:     public function html_name( $sub_property = null, $row = null ) {
557:         $base_slug = $this->get_option( 'slug' );
558: 
559:         if ( is_null( $sub_property ) ) {
560:             return $base_slug;
561:         }
562: 
563:         if ( is_numeric( $row ) ) {
564:             $base_slug = sprintf( '%s[%d]', $base_slug, intval( $row ) );
565:         }
566: 
567:         if ( ! papi_is_property( $sub_property ) ) {
568:             if ( is_array( $sub_property ) || is_object( $sub_property ) ) {
569:                 $sub_property = self::factory( $sub_property );
570:             } else {
571:                 return $base_slug;
572:             }
573:         }
574: 
575:         return sprintf(
576:             '%s[%s]',
577:             $base_slug,
578:             papi_remove_papi( $sub_property->get_slug() )
579:         );
580:     }
581: 
582:     /**
583:      * Get the import settings.
584:      *
585:      * @param  string $key
586:      * @param  mixed  $default
587:      *
588:      * @return mixed
589:      */
590:     public function import_setting( $key, $default = null ) {
591:         if ( ! is_string( $key ) ) {
592:             return $default;
593:         }
594: 
595:         $settings = $this->import_settings();
596: 
597:         return isset( $settings->$key ) ? $settings->$key : $default;
598:     }
599: 
600:     /**
601:      * Get the import settings.
602:      *
603:      * @return object
604:      */
605:     public function import_settings() {
606:         $settings = $this->get_import_settings();
607: 
608:         $settings = is_array( $settings ) || is_object( $settings ) ?
609:             $settings : [];
610: 
611:         return (object) array_merge(
612:             $this->default_import_settings,
613:             (array) $settings
614:         );
615:     }
616: 
617:     /**
618:      * Import value to the property.
619:      *
620:      * @param  mixed  $value
621:      * @param  string $slug
622:      * @param  int    $post_id
623:      *
624:      * @return mixed
625:      */
626:     public function import_value( $value, $slug, $post_id ) {
627:         return papi_maybe_json_decode(
628:             maybe_unserialize( $value ),
629:             $this->convert_type === 'array'
630:         );
631:     }
632: 
633:     /**
634:      * Check if it's a option page or not.
635:      *
636:      * @return bool
637:      */
638:     public function is_option_page() {
639:         if ( $this->page === null ) {
640:             return papi_is_option_page();
641:         }
642: 
643:         return $this->page->is( Papi_Core_Page::TYPE_OPTION );
644:     }
645: 
646:     /**
647:      * Change value after it's loaded from the database.
648:      *
649:      * @param  mixed  $value
650:      * @param  string $slug
651:      * @param  int    $post_id
652:      *
653:      * @return mixed
654:      */
655:     public function load_value( $value, $slug, $post_id ) {
656:         return papi_maybe_json_decode(
657:             maybe_unserialize( $value ),
658:             $this->convert_type === 'array'
659:         );
660:     }
661: 
662:     /**
663:      * Match property slug with given slug value.
664:      *
665:      * @param  string $slug
666:      *
667:      * @return bool
668:      */
669:     public function match_slug( $slug ) {
670:         if ( ! is_string( $slug ) ) {
671:             $slug = '';
672:         }
673: 
674:         return $this->get_slug( ! preg_match( '/^papi\_/', $slug ) ) === $slug;
675:     }
676: 
677:     /**
678:      * Prepare property value.
679:      *
680:      * @param  mixed $value
681:      *
682:      * @return mixed
683:      */
684:     protected function prepare_value( $value ) {
685:         if ( papi_is_empty( $value ) ) {
686:             return $this->default_value;
687:         }
688: 
689:         if ( $this->convert_type === 'string' ) {
690:             $value = papi_convert_to_string( $value );
691:         }
692: 
693:         return papi_santize_data( $value );
694:     }
695: 
696:     /**
697:      * Render AJAX request.
698:      */
699:     public function render_ajax_request() {
700:         papi_render_property( $this );
701:     }
702: 
703:     /**
704:      * Check if the property is allowed
705:      * to render by the conditional rules.
706:      *
707:      * @param  array $rules
708:      *
709:      * @return bool
710:      */
711:     public function render_is_allowed_by_rules( array $rules = [] ) {
712:         if ( empty( $rules ) ) {
713:             $rules = $this->get_rules();
714:         }
715: 
716:         return $this->conditional->display( $rules, $this );
717:     }
718: 
719:     /**
720:      * Set the page that the property is on.
721:      *
722:      * @param Papi_Core_Page $page
723:      */
724:     public function set_page( Papi_Core_Page $page ) {
725:         $this->page = $page;
726:     }
727: 
728:     /**
729:      * Set post id.
730:      *
731:      * @param int $post_id
732:      */
733:     public function set_post_id( $post_id ) {
734:         if ( ! is_numeric( $post_id ) ) {
735:             return;
736:         }
737: 
738:         $this->post_id = (int) $post_id;
739:     }
740: 
741:     /**
742:      * Set the current property options object.
743:      *
744:      * @param array|object $options
745:      */
746:     public function set_options( $options ) {
747:         $this->options = $this->setup_options( $options );
748:     }
749: 
750:     /**
751:      * Set property option value.
752:      *
753:      * @param string $key
754:      * @param mixed  $value
755:      */
756:     public function set_option( $key, $value ) {
757:         if ( ! is_object( $this->options ) ) {
758:             $this->options = (object) $this->default_options;
759:         }
760: 
761:         if ( isset( $this->options->$key ) ) {
762:             $this->options->$key = $value;
763:         }
764:     }
765: 
766:     /**
767:      * Set property setting value.
768:      *
769:      * @param string $key
770:      * @param mixed  $value
771:      */
772:     public function set_setting( $key, $value ) {
773:         if ( isset( $this->options->settings ) && isset( $this->options->settings->$key ) ) {
774:             $this->options->settings->$key = $value;
775:         }
776:     }
777: 
778:     /**
779:      * Setup actions.
780:      */
781:     protected function setup_actions() {
782:     }
783: 
784:     /**
785:      * Setup conditional class.
786:      */
787:     protected function setup_conditional() {
788:         $this->conditional = new Papi_Core_Conditional();
789:     }
790: 
791:     /**
792:      * Setup default options values.
793:      * All default values can't be set in the `$default_options` array.
794:      */
795:     private function setup_default_options() {
796:         if ( $this->default_options['sort_order'] === -1 ) {
797:             $this->default_options['sort_order'] = papi_filter_settings_sort_order();
798:         }
799: 
800:         if ( empty( $this->default_options['post_type'] ) ) {
801:             $this->default_options['post_type'] = papi_get_post_type();
802:         }
803: 
804:         if ( papi_is_empty( $this->default_options['default'] ) ) {
805:             $this->default_options['default'] = $this->default_value;
806:         }
807:     }
808: 
809:     /**
810:      * Setup filters.
811:      */
812:     protected function setup_filters() {
813:     }
814: 
815:     /**
816:      * Setup property options.
817:      *
818:      * @param  mixed $options
819:      *
820:      * @return mixed
821:      */
822:     private function setup_options( $options ) {
823:         // When a object is sent in, just return it.
824:         if ( is_object( $options ) ) {
825:             return $options;
826:         }
827: 
828:         // Only arrays can be handled.
829:         if ( ! is_array( $options ) ) {
830:             $options = [];
831:         }
832: 
833:         // Merge default options with the given options array.
834:         $options = array_merge( $this->default_options, $options );
835:         $options = (object) $options;
836: 
837:         // Capabilities should be a array.
838:         $options->capabilities = papi_to_array( $options->capabilities );
839: 
840:         // Setup property slug.
841:         $options->slug = $this->setup_options_slug( $options );
842: 
843:         // Setup property settings.
844:         $options->settings = $this->setup_options_settings( $options );
845: 
846:         // Escape all options except those that are send it as second argument.
847:         return papi_esc_html( $options, ['before_html', 'html', 'after_html'] );
848:     }
849: 
850:     /**
851:      * Setup options slug.
852:      *
853:      * @param  stdClass $options
854:      *
855:      * @return string
856:      */
857:     private function setup_options_slug( $options ) {
858:         $slug = $options->slug;
859: 
860:         if ( empty( $slug ) ) {
861:             if ( empty( $options->title ) ) {
862:                 $slug = papi_slugify( $options->type );
863:             } else {
864:                 $slug = papi_slugify( $options->title );
865:             }
866:         }
867: 
868:         return $slug === 'papi_' ? '' : papi_html_name( $slug );
869:     }
870: 
871:     /**
872:      * Setup options settings.
873:      *
874:      * @param  stdClass $options
875:      *
876:      * @return stdClass
877:      */
878:     private function setup_options_settings( $options ) {
879:         $property_class = self::factory( $options->type );
880: 
881:         if ( papi_is_property( $property_class ) ) {
882:             $options->settings = array_merge(
883:                 (array) $property_class->get_default_settings(),
884:                 (array) $options->settings
885:             );
886:         }
887: 
888:         return (object) $this->convert_settings( $options->settings );
889:     }
890: 
891:     /**
892:      * Update value before it's saved to the database.
893:      *
894:      * @param mixed  $value
895:      * @param string $slug
896:      * @param int    $post_id
897:      *
898:      * @return mixed
899:      */
900:     public function update_value( $value, $slug, $post_id ) {
901:         return papi_maybe_json_encode( $value );
902:     }
903: }
904: 
API documentation generated by ApiGen