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:  * Admin class that handle loading of Papi types,
  5:  * columns and loading of posts.
  6:  */
  7: final class Papi_Admin {
  8: 
  9:     /**
 10:      * The page type.
 11:      *
 12:      * @var Papi_Page_Type
 13:      */
 14:     private $page_type;
 15: 
 16:     /**
 17:      * The page type id.
 18:      *
 19:      * @var string
 20:      */
 21:     private $page_type_id;
 22: 
 23:     /**
 24:      * The post type.
 25:      *
 26:      * @var string
 27:      */
 28:     private $post_type;
 29: 
 30:     /**
 31:      * The constructor.
 32:      */
 33:     public function __construct() {
 34:         $this->load_files();
 35:         $this->setup_globals();
 36:         $this->setup_actions();
 37:         $this->setup_filters();
 38:     }
 39: 
 40:     /**
 41:      * Cloning is forbidden.
 42:      *
 43:      * @codeCoverageIgnore
 44:      */
 45:     public function __clone() {
 46:         _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'papi' ), '2.4.2' );
 47:     }
 48: 
 49:     /**
 50:      * Unserializing instances of this class is forbidden.
 51:      *
 52:      * @codeCoverageIgnore
 53:      */
 54:     public function __wakeup() {
 55:         _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'papi' ), '2.4.2' );
 56:     }
 57: 
 58:     /**
 59:      * Admin init.
 60:      *
 61:      * Setup the page type.
 62:      */
 63:     public function admin_init() {
 64:         // Preload all page types.
 65:         foreach ( papi_get_post_types() as $post_type ) {
 66:             papi_get_all_page_types( false, $post_type );
 67:         }
 68: 
 69:         if ( ! $this->setup_papi() ) {
 70:             return;
 71:         }
 72: 
 73:         $this->page_type->setup();
 74:     }
 75: 
 76:     /**
 77:      * Add custom body class when it's a page type.
 78:      *
 79:      * @param  string $classes
 80:      *
 81:      * @return string
 82:      */
 83:     public function admin_body_class( $classes ) {
 84:         if ( ! in_array( $this->post_type, papi_get_post_types() ) ) {
 85:             return $classes;
 86:         }
 87: 
 88:         if ( count( get_page_templates() ) ) {
 89:             $classes .= 'papi-hide-cpt';
 90:         }
 91: 
 92:         return $classes;
 93:     }
 94: 
 95:     /**
 96:      * Output Papi page type hidden field.
 97:      *
 98:      * This will only output on a post type page.
 99:      */
100:     public function edit_form_after_title() {
101:         wp_nonce_field( 'papi_save_data', 'papi_meta_nonce' );
102: 
103:         papi_render_html_tag( 'input', [
104:             'data-papi-page-type-key' => true,
105:             'name'                    => esc_attr( papi_get_page_type_key() ),
106:             'type'                    => 'hidden',
107:             'value'                   => esc_attr( papi_get_page_type_id() )
108:         ] );
109:     }
110: 
111:     /**
112:      * Output hidden meta boxes.
113:      */
114:     public function hidden_meta_boxes() {
115:         global $_wp_post_type_features;
116:         if ( ! isset( $_wp_post_type_features[$this->post_type]['editor'] ) ) {
117:             add_meta_box(
118:                 'papi-hidden-editor',
119:                 'Papi hidden editor',
120:                 [$this, 'hidden_meta_box_editor'],
121:                 $this->post_type
122:             );
123:         }
124:     }
125: 
126:     /**
127:      * Output hidden WordPress editor.
128:      */
129:     public function hidden_meta_box_editor() {
130:         wp_editor( '', 'papiHiddenEditor' );
131:     }
132: 
133:     /**
134:      * Load admin files that are not loaded by the autoload.
135:      */
136:     private function load_files() {
137:         require_once __DIR__ . '/class-papi-admin-ajax.php';
138:         require_once __DIR__ . '/class-papi-admin-management-pages.php';
139:         require_once __DIR__ . '/class-papi-admin-post-handler.php';
140:         require_once __DIR__ . '/class-papi-admin-option-handler.php';
141:     }
142: 
143:     /**
144:      * Load post new action
145:      * Redirect to right url if no page type is set.
146:      */
147:     public function load_post_new() {
148:         $request_uri = $_SERVER['REQUEST_URI'];
149:         $post_types = papi_get_post_types();
150: 
151:         if ( in_array( $this->post_type, $post_types ) && strpos( $request_uri, 'page_type=' ) === false ) {
152:             $parsed_url = parse_url( $request_uri );
153: 
154:             $only_page_type = papi_filter_settings_only_page_type( $this->post_type );
155:             $page_types     = papi_get_all_page_types( false, $this->post_type );
156:             $show_standard  = false;
157: 
158:             if ( count( $page_types ) === 1 && empty( $only_page_type ) ) {
159:                 $show_standard  = $page_types[0]->standard_type;
160:                 $show_standard  = $show_standard ?
161:                     papi_filter_settings_show_standard_page_type( $this->post_type ) :
162:                     $show_standard;
163: 
164:                 $only_page_type = $show_standard ? '' : $page_types[0]->get_id();
165:             }
166: 
167:             // Check if we should show one post type or not and
168:             // create the right url for that.
169:             if ( ! empty( $only_page_type ) && ! $show_standard ) {
170:                 $url = papi_get_page_new_url( $only_page_type, false );
171:             } else {
172:                 $page = 'page=papi-add-new-page,' . $this->post_type;
173: 
174:                 if ( $this->post_type !== 'post' ) {
175:                     $page = '&' . $page;
176:                 }
177: 
178:                 $url = 'edit.php?' . $parsed_url['query'] . $page;
179:             }
180: 
181:             wp_safe_redirect( $url );
182:             is_admin() && exit;
183:         }
184:     }
185: 
186:     /**
187:      * Add custom table header to page type.
188:      *
189:      * @param  array $defaults
190:      *
191:      * @return array
192:      */
193:     public function manage_page_type_posts_columns( $defaults ) {
194:         if ( ! in_array( $this->post_type, papi_get_post_types() ) ) {
195:             return $defaults;
196:         }
197: 
198:         $defaults['page_type'] = papi_filter_settings_page_type_column_title(
199:             $this->post_type
200:         );
201: 
202:         return $defaults;
203:     }
204: 
205:     /**
206:      * Add custom table column to page type.
207:      *
208:      * @param string $column_name
209:      * @param int    $post_id
210:      */
211:     public function manage_page_type_posts_custom_column( $column_name, $post_id ) {
212:         if ( ! in_array( $this->post_type, papi_get_post_types() ) ) {
213:             return;
214:         }
215: 
216:         if ( $column_name === 'page_type' ) {
217:             $page_type = papi_get_page_type_by_post_id( $post_id );
218: 
219:             if ( ! is_null( $page_type ) ) {
220:                 echo esc_html( $page_type->name );
221:             } else {
222:                 echo esc_html( papi_filter_settings_standard_page_name(
223:                     papi_get_post_type()
224:                 ) );
225:             }
226:         }
227:     }
228: 
229:     /**
230:      * Filter page types in post type list.
231:      */
232:     public function restrict_page_types() {
233:         $post_types = papi_get_post_types();
234: 
235:         if ( in_array( $this->post_type, $post_types ) ) {
236:             $page_types = papi_get_all_page_types( false, $this->post_type );
237: 
238:             $page_types = array_map( function ( $page_type ) {
239:                 return [
240:                     'name' => $page_type->name,
241:                     'value' => $page_type->get_id()
242:                 ];
243:             }, $page_types );
244: 
245:             // Add the standard page that isn't a real page type.
246:             if ( papi_filter_settings_show_standard_page_type_in_filter( $this->post_type ) ) {
247:                 $page_types[] = [
248:                     'name'  => papi_filter_settings_standard_page_name( $this->post_type ),
249:                     'value' => 'papi-standard-page'
250:                 ];
251:             }
252: 
253:             usort( $page_types, function ( $a, $b ) {
254:                 return strcmp(
255:                     strtolower( $a['name'] ),
256:                     strtolower( $b['name'] )
257:                 );
258:             } );
259:             ?>
260:             <select name="page_type" class="postform">
261:                 <option value="0" selected><?php _e( 'All types', 'papi' ); ?></option>
262:                 <?php
263:                 foreach ( $page_types as $page_type ) {
264:                     printf(
265:                         '<option value="%s" %s>%s</option>',
266:                         $page_type['value'],
267:                         papi_get_qs( 'page_type' ) === $page_type['value'] ? ' selected' : '',
268:                         $page_type['name']
269:                     );
270:                 }
271:                 ?>
272:             </select>
273:             <?php
274:         }
275:     }
276: 
277:     /**
278:      * Filter posts on load if `page_type` query string is set.
279:      *
280:      * @param  WP_Query $query
281:      *
282:      * @return WP_Query
283:      */
284:     public function pre_get_posts( $query ) {
285:         global $pagenow;
286: 
287:         if ( $pagenow === 'edit.php' && ! is_null( papi_get_qs( 'page_type' ) ) ) {
288:             if ( papi_get_qs( 'page_type' ) === 'papi-standard-page' ) {
289:                 $query->set( 'meta_query', [
290:                     [
291:                         'key'     => papi_get_page_type_key(),
292:                         'compare' => 'NOT EXISTS'
293:                     ]
294:                 ] );
295:             } else {
296:                 $query->set( 'meta_key', papi_get_page_type_key() );
297:                 $query->set( 'meta_value', papi_get_qs( 'page_type' ) );
298:             }
299:         }
300: 
301:         return $query;
302:     }
303: 
304:     /**
305:      * Setup actions.
306:      */
307:     private function setup_actions() {
308:         if ( is_admin() ) {
309:             add_action( 'admin_init', [$this, 'admin_init'] );
310:             add_action( 'edit_form_after_title', [$this, 'edit_form_after_title'] );
311:             add_action( 'load-post-new.php', [$this, 'load_post_new'] );
312:             add_action( 'restrict_manage_posts', [ $this, 'restrict_page_types'] );
313:             add_action( 'add_meta_boxes', [$this, 'hidden_meta_boxes'], 10 );
314:         }
315:     }
316: 
317:     /**
318:      * Setup filters.
319:      */
320:     private function setup_filters() {
321:         if ( is_admin() ) {
322:             add_filter( 'admin_body_class', [$this, 'admin_body_class'] );
323:             add_filter( 'pre_get_posts', [$this, 'pre_get_posts'] );
324:             add_filter( 'wp_link_query', [$this, 'wp_link_query'] );
325:             add_filter( 'manage_' . $this->post_type . '_posts_columns', [
326:                 $this,
327:                 'manage_page_type_posts_columns'
328:             ] );
329:             add_action( 'manage_' . $this->post_type . '_posts_custom_column', [
330:                 $this,
331:                 'manage_page_type_posts_custom_column'
332:             ], 10, 2 );
333:         }
334:     }
335: 
336:     /**
337:      * Setup globals.
338:      */
339:     private function setup_globals() {
340:         $this->post_type = papi_get_post_type();
341: 
342:         if ( is_admin() ) {
343:             $this->page_type_id  = papi_get_page_type_id();
344:         }
345:     }
346: 
347:     /**
348:      * Load right Papi file if it exists.
349:      *
350:      * @return bool
351:      */
352:     public function setup_papi() {
353:         // If the post type isn't in the post types array we can't proceed.
354:         if ( in_array( $this->post_type, ['revision', 'nav_menu_item'] ) ) {
355:             return false;
356:         }
357: 
358:         if ( empty( $this->page_type_id ) ) {
359:             // If only page type is used, override the page type value.
360:             $this->page_type_id = papi_filter_settings_only_page_type(
361:                 $this->post_type
362:             );
363: 
364:             if ( empty( $this->page_type_id ) ) {
365:                 // Load page types that don't have any real post type.
366:                 $this->page_type_id = str_replace(
367:                     'papi/',
368:                     '',
369:                     papi_get_qs( 'page' )
370:                 );
371:             }
372: 
373:             if ( empty( $this->page_type_id ) ) {
374:                 $this->page_type_id = papi_get_page_type_id();
375:             }
376:         }
377: 
378:         if ( empty( $this->page_type_id ) ) {
379:             return false;
380:         }
381: 
382:         $this->page_type = papi_get_page_type_by_id( $this->page_type_id );
383: 
384:         // Do a last check so we can be sure that we have a page type instance.
385:         return $this->page_type instanceof Papi_Page_Type;
386:     }
387: 
388:     /**
389:      * Filter the link query results.
390:      *
391:      * @param  array $results
392:      *
393:      * @return array
394:      */
395:     public function wp_link_query( $results ) {
396:         $post_type = papi_get_post_type();
397: 
398:         foreach ( $results as $index => $value ) {
399:             $name = papi_get_page_type_name( $value['ID'] );
400: 
401:             if ( empty( $name ) ) {
402:                 $name = papi_filter_settings_standard_page_name( $post_type );
403:             }
404: 
405:             $results[$index]['info'] = esc_html( $name );
406:         }
407: 
408:         return $results;
409:     }
410: }
411: 
412: new Papi_Admin;
413: 
API documentation generated by ApiGen