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:  * Papi type that handle all psot types except attachment,
  5:  * option data and rendering. All page types should extend
  6:  * this class.
  7:  */
  8: class Papi_Page_Type extends Papi_Page_Type_Meta {
  9: 
 10:     /**
 11:      * The array of meta boxes to register.
 12:      *
 13:      * @var array
 14:      */
 15:     protected $boxes = [];
 16: 
 17:     /**
 18:      * Load all boxes even if we aren't on a post type.
 19:      *
 20:      * @var bool
 21:      */
 22:     private $load_boxes = false;
 23: 
 24:     /**
 25:      * Array of post type supports to remove.
 26:      * By default remove `postcustom` which is the Custom fields metabox.
 27:      *
 28:      * @var array
 29:      */
 30:     private $post_type_supports = ['custom-fields'];
 31: 
 32:     /**
 33:      * Remove meta boxes.
 34:      *
 35:      * @var array
 36:      */
 37:     private $remove_meta_boxes = [];
 38: 
 39:     /**
 40:      * Add new meta box with properties.
 41:      *
 42:      * @param mixed $file_or_options
 43:      * @param array $properties
 44:      */
 45:     protected function box( $file_or_options = [], $properties = [] ) {
 46:         if ( ! is_string( $file_or_options ) && ! is_array( $file_or_options ) && ! is_object( $file_or_options ) ) {
 47:             return;
 48:         }
 49: 
 50:         list( $options, $properties ) = papi_get_options_and_properties(
 51:             $file_or_options,
 52:             $properties,
 53:             true
 54:         );
 55: 
 56:         $post_type = $this->get_post_type();
 57: 
 58:         // Check so we have a post the to add the box to.
 59:         // @codeCoverageIgnoreStart
 60:         if ( ! $this->load_boxes && ( empty( $post_type ) || ! $this->has_post_type( $post_type ) ) ) {
 61:             return;
 62:         }
 63:         // @codeCoverageIgnoreEnd
 64: 
 65:         // Add post type to the options array.
 66:         $options['_post_type'] = $post_type;
 67: 
 68:         if ( isset( $options['sort_order'] ) ) {
 69:             $sort_order = intval( $options['sort_order'] );
 70:         } else {
 71:             $sort_order = papi_filter_settings_sort_order();
 72:         }
 73: 
 74:         if ( is_callable( $properties ) ) {
 75:             $properties = call_user_func( $properties );
 76:         }
 77: 
 78:         // Check and convert all non properties objects to properties objects.
 79:         $properties = $this->convert_properties( $properties );
 80: 
 81:         $options['title'] = papi_esc_html(
 82:             isset( $options['title'] ) ? $options['title'] : ''
 83:         );
 84: 
 85:         array_push( $this->boxes, [
 86:             $options,
 87:             $properties,
 88:             'sort_order' => $sort_order,
 89:             'title'      => $options['title']
 90:         ] );
 91:     }
 92: 
 93:     /**
 94:      * Convert properties to properties objects.
 95:      *
 96:      * @param  array|object $properties
 97:      *
 98:      * @return array
 99:      */
100:     private function convert_properties( $properties ) {
101:         if ( is_array( $properties ) ) {
102:             if ( isset( $properties['type'] ) ) {
103:                 $properties = [$properties];
104:             } else if ( isset( $properties[0]->tab ) && $properties[0]->tab ) {
105:                 foreach ( $properties as $index => $items ) {
106:                     $items->properties = array_map(
107:                         'papi_get_property_type',
108:                         $items->properties
109:                     );
110:                 }
111: 
112:                 return $properties;
113:             }
114:         }
115: 
116:         if ( is_object( $properties ) ) {
117:             $properties = papi_get_property_type( $properties );
118:         }
119: 
120:         if ( papi_is_property( $properties ) ) {
121:             $properties = [$properties];
122:         }
123: 
124:         $properties = is_array( $properties ) ? $properties : [];
125:         $properties = array_map( 'papi_get_property_type', $properties );
126: 
127:         return array_filter( $properties, 'papi_is_property' );
128:     }
129: 
130:     /**
131:      * Should the Page Type be displayed in WordPress admin or not?
132:      *
133:      * @param  string $post_type
134:      *
135:      * @return bool
136:      */
137:     public function display( $post_type ) {
138:         return true;
139:     }
140: 
141:     /**
142:      * Get boxes from the page type.
143:      *
144:      * @return array
145:      */
146:     public function get_boxes() {
147:         if ( empty( $this->boxes ) && $this->load_boxes === false ) {
148:             if ( ! method_exists( $this, 'register' ) ) {
149:                 return [];
150:             }
151: 
152:             $this->load_boxes = true;
153: 
154:             $this->register();
155:         }
156: 
157:         return $this->boxes;
158:     }
159: 
160:     /**
161:      * Get post type.
162:      *
163:      * @return string
164:      */
165:     public function get_post_type() {
166:         return papi_get_post_type();
167:     }
168: 
169:     /**
170:      * Get child property.
171:      *
172:      * @param  array  $items
173:      * @param  string $slug
174:      *
175:      * @return object
176:      */
177:     protected function get_child_property( $items, $slug ) {
178:         $result = null;
179: 
180:         foreach ( $items as $property ) {
181:             if ( is_array( $property ) ) {
182:                 $result = $this->get_child_property( $property, $slug );
183: 
184:                 if ( is_object( $result ) ) {
185:                     return papi_get_property_type( $result );
186:                 }
187:             }
188: 
189:             $property = papi_get_property_type( $property );
190: 
191:             if ( papi_is_property( $property ) && $property->match_slug( $slug ) ) {
192:                 return papi_get_property_type( $property );
193:             }
194:         }
195: 
196:         return $result;
197:     }
198: 
199:     /**
200:      * Get property from page type.
201:      *
202:      * @param  string $slug
203:      * @param  string $child_slug
204:      *
205:      * @return null|Papi_Property
206:      */
207:     public function get_property( $slug, $child_slug = '' ) {
208:         $boxes = $this->get_boxes();
209:         $parts = preg_split( '/\[\d+\]/', $slug );
210:         $parts = array_map( function ( $part ) {
211:             return preg_replace( '/(\[|\])/', '', $part );
212:         }, $parts );
213: 
214:         if ( count( $parts ) > 1 ) {
215:             $property = null;
216: 
217:             for ( $i = 0, $l = count( $parts ); $i < $l; $i++ ) {
218:                 $child    = isset( $parts[$i + 1] ) ? $parts[$i + 1] : '';
219:                 $property = $this->get_property( $parts[$i], $child );
220: 
221:                 if ( isset( $parts[$i + 1] ) ) {
222:                     $i++;
223:                 }
224:             }
225: 
226:             return $property;
227:         }
228: 
229:         if ( empty( $boxes ) ) {
230:             return;
231:         }
232: 
233:         foreach ( $boxes as $box ) {
234:             $properties = isset( $box[1][0]->properties ) ?
235:                 $box[1][0]->properties : $box[1];
236: 
237:             foreach ( $properties as $property ) {
238:                 $property = papi_get_property_type( $property );
239: 
240:                 if ( papi_is_property( $property ) && $property->match_slug( $slug ) ) {
241:                     if ( empty( $child_slug ) ) {
242:                         return $property;
243:                     }
244: 
245:                     $result = $this->get_child_property(
246:                         $property->get_child_properties(),
247:                         $child_slug
248:                     );
249: 
250:                     if ( is_object( $result ) ) {
251:                         return papi_get_property_type( $result );
252:                     }
253:                 }
254:             }
255:         }
256:     }
257: 
258:     /**
259:      * This function will setup all meta boxes.
260:      */
261:     public function setup() {
262:         if ( ! method_exists( $this, 'register' ) ) {
263:             return;
264:         }
265: 
266:         // 1. Run the register method.
267:         $this->register();
268: 
269:         // 2. Remove post type support
270:         $this->remove_post_type_support();
271: 
272:         // 3. Load all boxes.
273:         $this->boxes = papi_sort_order( array_reverse( $this->boxes ) );
274: 
275:         foreach ( $this->boxes as $box ) {
276:             new Papi_Admin_Meta_Box( $box[0], $box[1] );
277:         }
278:     }
279: 
280:     /**
281:      * Add new property to the page using array or rendering property template file.
282:      *
283:      * @param  array|string $file_or_options
284:      * @param  array $values
285:      *
286:      * @return null|Papi_Property
287:      */
288:     protected function property( $file_or_options = [], $values = [] ) {
289:         return papi_property( $file_or_options, $values );
290:     }
291: 
292:     /**
293:      * Remove post type support. Runs once, on page load.
294:      *
295:      * @param array $post_type_supports
296:      */
297:     protected function remove( $post_type_supports = [] ) {
298:         $this->post_type_supports = array_merge( $this->post_type_supports, papi_to_array( $post_type_supports ) );
299:     }
300: 
301:     /**
302:      * Remove post type support action.
303:      */
304:     public function remove_post_type_support() {
305:         global $_wp_post_type_features;
306: 
307:         $post_type = $this->get_post_type();
308: 
309:         if ( empty( $post_type ) ) {
310:             return;
311:         }
312: 
313:         foreach ( $this->post_type_supports as $key => $value ) {
314:             if ( is_numeric( $key ) ) {
315:                 $key = $value;
316:                 $value = '';
317:             }
318: 
319:             if ( isset( $_wp_post_type_features[$post_type] ) && isset( $_wp_post_type_features[$post_type][$key] ) ) {
320:                 unset( $_wp_post_type_features[$post_type][$key] );
321:                 continue;
322:             }
323: 
324:             // Add non post type support to remove meta boxes array.
325:             if ( empty( $value ) ) {
326:                 $value = 'normal';
327:             }
328: 
329:             $this->remove_meta_boxes[] = [$key, $value];
330:         }
331: 
332:         add_action( 'add_meta_boxes', [$this, 'remove_meta_boxes'], 999 );
333:     }
334: 
335:     /**
336:      * Remove meta boxes.
337:      */
338:     public function remove_meta_boxes() {
339:         $post_type = $this->get_post_type();
340: 
341:         if ( empty( $post_type ) ) {
342:             return;
343:         }
344: 
345:         foreach ( $this->remove_meta_boxes as $item ) {
346:             remove_meta_box( $item[0], $post_type, $item[1] );
347:         }
348:     }
349: 
350:     /**
351:      * Add a new tab.
352:      *
353:      * @param  mixed $file_or_options
354:      * @param  array $properties
355:      *
356:      * @return array
357:      */
358:     protected function tab( $file_or_options = [], $properties = [] ) {
359:         if ( ! is_string( $file_or_options ) && ! is_array( $file_or_options ) ) {
360:             return;
361:         }
362: 
363:         $tab = papi_tab( $file_or_options, $properties );
364: 
365:         // Tabs sometimes will be in $tab->options['options'] when you use a tab template in this method
366:         // and using the return value of papi_tab function is used.
367:         //
368:         // This should be fixed later, not a priority for now since this works.
369:         if ( is_object( $tab ) && isset( $tab->options ) && isset( $tab->options['options'] ) ) {
370:             $tab = (object) $tab->options;
371:         }
372: 
373:         if ( isset( $tab->options ) ) {
374:             $tab->options = papi_esc_html( $tab->options );
375:         }
376: 
377:         return $tab;
378:     }
379: 
380:     /**
381:      * Load template file.
382:      *
383:      * @param  string $file
384:      * @param  array  $values
385:      *
386:      * @return array
387:      */
388:     protected function template( $file, $values = [] ) {
389:         return papi_template( $file, $values );
390:     }
391: }
392: 
API documentation generated by ApiGen