<?php
/**
* CPT data controller class
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( ! class_exists( 'Jet_Engine_CPT_Data' ) ) {
/**
* Define Jet_Engine_CPT_Data class
*/
class Jet_Engine_CPT_Data extends Jet_Engine_Base_Data {
/**
* Table name
*
* @var string
*/
public $table = 'post_types';
/**
* Query arguments
*
* @var array
*/
public $query_args = array(
'status' => 'publish',
);
/**
* Table format
*
* @var string
*/
public $table_format = array( '%s', '%s', '%s', '%s', '%s' );
/**
* Returns blacklisted post types slugs
*
* @return array
*/
public function items_blacklist() {
return array(
'post',
'page',
'attachment',
'revision',
'nav_menu_item',
'custom_css',
'customize_changeset',
'action',
'author',
'order',
'theme',
);
}
/**
* Returns blacklisted post types slugs
*
* @return array
*/
public function meta_blacklist() {
return array(
'_wpnonce',
'_wp_http_referer',
'user_ID',
'action',
'originalaction',
'post_author',
'post_type',
'original_post_status',
'referredby',
'_wp_original_http_referer',
'post_ID',
'meta-box-order-nonce',
'closedpostboxesnonce',
'post_title',
'samplepermalinknonce',
'content',
'wp-preview',
'hidden_post_status',
'post_status',
'hidden_post_password',
'hidden_post_visibility',
'visibility',
'post_password',
'mm',
'jj',
'aa',
'hh',
'mn',
'ss',
'hidden_mm',
'cur_mm',
'hidden_jj',
'cur_jj',
'hidden_aa',
'cur_aa',
'hidden_hh',
'cur_hh',
'hidden_mn',
'cur_mn',
'original_publish',
'save',
'post_format',
'tax_input',
'parent_id',
'menu_order',
'_thumbnail_id',
'meta',
'excerpt',
'trackback_url',
'_ajax_nonce',
'metakeyselect',
'metakeyinput',
'metavalue',
'advanced_view',
'comment_status',
'ping_status',
'post_name',
'post_author_override',
'post_mime_type',
'ID',
'post_content',
'post_excerpt',
'post_parent',
'to_ping',
'screen',
'taxonomy',
'action',
'tag-name',
'slug',
'description',
);
}
/**
* Prepare post data from request to write into database
*
* @return array
*/
public function sanitize_item_from_request( $is_built_in = false ) {
$request = $this->request;
if ( $is_built_in ) {
$status = 'built-in';;
} else {
$status = 'publish';
}
$result = array(
'slug' => '',
'status' => $status,
'labels' => array(),
'args' => array(),
'meta_fields' => array(),
);
if ( $is_built_in && ! empty( $request['id'] ) ) {
$result['id'] = absint( $request['id'] );
}
if ( $is_built_in ) {
$slug = ! empty( $request['slug'] ) ? $request['slug'] : false;
} else {
$slug = ! empty( $request['slug'] ) ? $this->sanitize_slug( $request['slug'] ) : false;
}
$name = ! empty( $request['name'] ) ? sanitize_text_field( $request['name'] ) : false;
if ( ! $slug ) {
return false;
}
$labels = array();
if ( $is_built_in ) {
if ( $name ) {
$labels = array(
'name' => $name,
);
} else {
$name = array();
}
} else {
$labels = array(
'name' => $name,
);
}
$labels_list = array(
'singular_name',
'menu_name',
'name_admin_bar',
'add_new',
'add_new_item',
'new_item',
'edit_item',
'view_item',
'all_items',
'search_items',
'parent_item_colon',
'not_found',
'not_found_in_trash',
'featured_image',
'set_featured_image',
'remove_featured_image',
'use_featured_image',
'archives',
'insert_into_item',
'uploaded_to_this_item',
);
foreach ( $labels_list as $label_key ) {
if ( ! empty( $request[ $label_key ] ) ) {
$labels[ $label_key ] = $request[ $label_key ];
}
}
$args = array();
$ensure_bool = array(
'public',
'publicly_queryable',
'show_ui',
'show_in_menu',
'show_in_nav_menus',
'show_in_rest',
'query_var',
'rewrite',
'has_archive',
'hierarchical',
'exclude_from_search',
'with_front',
'show_edit_link',
);
foreach ( $ensure_bool as $key ) {
if ( $is_built_in ) {
if ( isset( $request[ $key ] ) ) {
$args[ $key ] = filter_var( $request[ $key ], FILTER_VALIDATE_BOOLEAN );
}
} else {
$args[ $key ] = ! empty( $request[ $key ] )
? filter_var( $request[ $key ], FILTER_VALIDATE_BOOLEAN )
: false;
}
}
$regular_args = array(
'rewrite_slug' => $slug,
'capability_type' => 'post',
'menu_position' => null,
'menu_icon' => '',
'supports' => array(),
'admin_columns' => array(),
);
foreach ( $regular_args as $key => $default ) {
if ( $is_built_in ) {
if ( isset( $request[ $key ] ) ) {
$args[ $key ] = $request[ $key ];
}
} else {
$args[ $key ] = ! empty( $request[ $key ] ) ? $request[ $key ] : $default;
}
}
if ( ! isset( $args['admin_columns'] ) ) {
$args['admin_columns'] = array();
}
// Remove collapsed trigger from admin columns
if ( ! empty( $args['admin_columns'] ) ) {
for ( $i = 0; $i < count( $args['admin_columns'] ); $i++ ) {
if ( isset( $args['admin_columns'][ $i ]['collapsed'] ) ) {
unset( $args['admin_columns'][ $i ]['collapsed'] );
}
}
}
/**
* @todo Validate meta fields before saving - ensure that used correct types and all names was set.
*/
$meta_fields = ! empty( $request['meta_fields'] ) ? $request['meta_fields'] : array();
$result['slug'] = $slug;
$result['labels'] = $labels;
$result['args'] = $args;
$result['meta_fields'] = $this->sanitize_meta_fields( $meta_fields );
return $result;
}
/**
* Sanitize meta fields
*
* @param [type] $meta_fields [description]
* @return [type] [description]
*/
public function sanitize_meta_fields( $meta_fields ) {
foreach ( $meta_fields as $key => $field ) {
// If name is empty - create it from title, else - santize it
if ( empty( $field['name'] ) ) {
$field['name'] = $this->sanitize_slug( $field['title'] );
} else {
$field['name'] = $this->sanitize_slug( $field['name'] );
}
// If still empty - create random name
if ( empty( $field['name'] ) ) {
$field['name'] = '_field_' . rand( 10000, 99999 );
}
// If name in blak list - add underscore at start
if ( in_array( $field['name'], $this->meta_blacklist() ) ) {
$meta_fields[ $key ]['name'] = '_' . $field['name'];
} else {
$meta_fields[ $key ]['name'] = $field['name'];
}
}
return $meta_fields;
}
/**
* Filter post type for register
*
* @return array
*/
public function filter_item_for_register( $item ) {
$result = array();
Loading ...