| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || exit; |
| 4 |
|
| 5 |
/** |
| 6 |
* Dynamic Custom Post Types |
| 7 |
* |
| 8 |
* A custom built plugin that helps user generate dinamic custom post types. |
| 9 |
* |
| 10 |
* @since 1.1.8 |
| 11 |
*/ |
| 12 |
|
| 13 |
|
| 14 |
function borderless_cpt_init() { |
| 15 |
$labels = array( |
| 16 |
'name' => _x( 'Borderless Post Types', 'Post Type General Name', 'borderless' ), |
| 17 |
'singular_name' => _x( 'Post Type', 'Post Type Singular Name', 'borderless' ), |
| 18 |
'menu_name' => __( 'Post Types', 'borderless' ), |
| 19 |
'name_admin_bar' => __( 'Post Type', 'borderless' ), |
| 20 |
'archives' => __( 'Item Archives', 'borderless' ), |
| 21 |
'attributes' => __( 'Item Attributes', 'borderless' ), |
| 22 |
'parent_item_colon' => __( 'Parent Item:', 'borderless' ), |
| 23 |
'all_items' => __( 'All Items', 'borderless' ), |
| 24 |
'add_new_item' => __( 'Add New Item', 'borderless' ), |
| 25 |
'add_new' => __( 'Add New', 'borderless' ), |
| 26 |
'new_item' => __( 'New Item', 'borderless' ), |
| 27 |
'edit_item' => __( 'Edit Item', 'borderless' ), |
| 28 |
'update_item' => __( 'Update Item', 'borderless' ), |
| 29 |
'view_item' => __( 'View Item', 'borderless' ), |
| 30 |
'view_items' => __( 'View Items', 'borderless' ), |
| 31 |
'search_items' => __( 'Search Item', 'borderless' ), |
| 32 |
'not_found' => __( 'Not found', 'borderless' ), |
| 33 |
'not_found_in_trash' => __( 'Not found in Trash', 'borderless' ), |
| 34 |
'featured_image' => __( 'Featured Image', 'borderless' ), |
| 35 |
'set_featured_image' => __( 'Set featured image', 'borderless' ), |
| 36 |
'remove_featured_image' => __( 'Remove featured image', 'borderless' ), |
| 37 |
'use_featured_image' => __( 'Use as featured image', 'borderless' ), |
| 38 |
'insert_into_item' => __( 'Insert into item', 'borderless' ), |
| 39 |
'uploaded_to_this_item' => __( 'Uploaded to this item', 'borderless' ), |
| 40 |
'items_list' => __( 'Items list', 'borderless' ), |
| 41 |
'items_list_navigation' => __( 'Items list navigation', 'borderless' ), |
| 42 |
'filter_items_list' => __( 'Filter items list', 'borderless' ), |
| 43 |
); |
| 44 |
$args = array( |
| 45 |
'label' => __( 'Post Type', 'borderless' ), |
| 46 |
'description' => __( 'Borderless Custom Post Types', 'borderless' ), |
| 47 |
'labels' => $labels, |
| 48 |
'supports' => array( 'title' ), |
| 49 |
'hierarchical' => false, |
| 50 |
'public' => true, |
| 51 |
'show_ui' => true, |
| 52 |
'show_in_menu' => 'edit.php?post_type=borderless_cpt', |
| 53 |
'menu_position' => 5, |
| 54 |
'show_in_admin_bar' => true, |
| 55 |
'show_in_nav_menus' => true, |
| 56 |
'can_export' => true, |
| 57 |
'has_archive' => true, |
| 58 |
'exclude_from_search' => true, |
| 59 |
'publicly_queryable' => true, |
| 60 |
'capability_type' => 'post', |
| 61 |
'query_var' => true, |
| 62 |
'rewrite' => true, |
| 63 |
); |
| 64 |
register_post_type( 'borderless_cpt', $args ); |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
$the_query = new WP_Query(array('post_type' => array('borderless_cpt'))); |
| 69 |
while ($the_query->have_posts()) : $the_query->the_post(); |
| 70 |
$taxonomies[] = ''; |
| 71 |
$supports[] = ''; |
| 72 |
|
| 73 |
/** |
| 74 |
* General |
| 75 |
*/ |
| 76 |
|
| 77 |
$singular_name = get_post_meta( get_the_ID(), 'borderless-cpt-general-singular-name', true ); |
| 78 |
$plural_name = get_post_meta( get_the_ID(), 'borderless-cpt-general-plural-name', true ); |
| 79 |
$slug = strtolower( get_post_meta( get_the_ID(), 'borderless-cpt-general-slug', true ) ); |
| 80 |
$hierarchical = get_post_meta( get_the_ID(), 'borderless-cpt-general-hierarchical', true ) == '2' ? true : false; |
| 81 |
$categories = get_post_meta( get_the_ID(), 'borderless-cpt-general-categories', true ) == '2' ? $taxonomies[] = 'category' : ''; |
| 82 |
$tags = get_post_meta( get_the_ID(), 'borderless-cpt-general-tags', true ) == '2' ? $taxonomies[] = 'post_tag' : ''; |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Labels |
| 87 |
*/ |
| 88 |
|
| 89 |
$menu_name = get_post_meta( get_the_ID(), 'borderless-cpt-labels-menu-name', true ); |
| 90 |
$admin_bar_name = get_post_meta( get_the_ID(), 'borderless-cpt-labels-admin-bar-name', true ); |
| 91 |
$archives = get_post_meta( get_the_ID(), 'borderless-cpt-labels-archives', true ); |
| 92 |
$attributes = get_post_meta( get_the_ID(), 'borderless-cpt-labels-attributes', true ); |
| 93 |
$parent_item = get_post_meta( get_the_ID(), 'borderless-cpt-labels-parent-item', true ); |
| 94 |
$all_items = get_post_meta( get_the_ID(), 'borderless-cpt-labels-all-items', true ); |
| 95 |
$add_new_item = get_post_meta( get_the_ID(), 'borderless-cpt-labels-add-new-item', true ); |
| 96 |
$add_new = get_post_meta( get_the_ID(), 'borderless-cpt-labels-add-new', true ); |
| 97 |
$new_item = get_post_meta( get_the_ID(), 'borderless-cpt-labels-new-item', true ); |
| 98 |
$edit_item = get_post_meta( get_the_ID(), 'borderless-cpt-labels-edit-item', true ); |
| 99 |
$update_item = get_post_meta( get_the_ID(), 'borderless-cpt-labels-update-item', true ); |
| 100 |
$view_item = get_post_meta( get_the_ID(), 'borderless-cpt-labels-view-item', true ); |
| 101 |
$view_items = get_post_meta( get_the_ID(), 'borderless-cpt-labels-view-items', true ); |
| 102 |
$search_item = get_post_meta( get_the_ID(), 'borderless-cpt-labels-search-item', true ); |
| 103 |
$not_found = get_post_meta( get_the_ID(), 'borderless-cpt-labels-not-found', true ); |
| 104 |
$not_found_in_trash = get_post_meta( get_the_ID(), 'borderless-cpt-labels-not-found-in-trash', true ); |
| 105 |
$featured_image = get_post_meta( get_the_ID(), 'borderless-cpt-labels-featured-image', true ); |
| 106 |
$set_featured_image = get_post_meta( get_the_ID(), 'borderless-cpt-labels-set-featured-image', true ); |
| 107 |
$remove_featured_image = get_post_meta( get_the_ID(), 'borderless-cpt-labels-remove-featured-image', true ); |
| 108 |
$use_as_featured_image = get_post_meta( get_the_ID(), 'borderless-cpt-labels-use-as-featured-image', true ); |
| 109 |
$insert_into_item = get_post_meta( get_the_ID(), 'borderless-cpt-labels-insert-into-item', true ); |
| 110 |
$uploaded_to_this_item = get_post_meta( get_the_ID(), 'borderless-cpt-labels-uploaded-to-this-item', true ); |
| 111 |
$items_list = get_post_meta( get_the_ID(), 'borderless-cpt-labels-items-list', true ); |
| 112 |
$items_list_navigation = get_post_meta( get_the_ID(), 'borderless-cpt-labels-items-list-navigation', true ); |
| 113 |
$filter_items_list = get_post_meta( get_the_ID(), 'borderless-cpt-labels-filter-items-list', true ); |
| 114 |
|
| 115 |
|
| 116 |
/** |
| 117 |
* Options |
| 118 |
*/ |
| 119 |
|
| 120 |
$title = get_post_meta( get_the_ID(), 'borderless-cpt-options-title', true ) == '2' ? $supports[] = 'title' : ''; |
| 121 |
$editor = get_post_meta( get_the_ID(), 'borderless-cpt-options-editor', true ) == '2' ? $supports[] = 'editor' : ''; |
| 122 |
$excerpt = get_post_meta( get_the_ID(), 'borderless-cpt-options-excerpt', true ) == '2' ? $supports[] = 'excerpt' : ''; |
| 123 |
$author = get_post_meta( get_the_ID(), 'borderless-cpt-options-author', true ) == '2' ? $supports[] = 'author' : ''; |
| 124 |
$featured_image_support = get_post_meta( get_the_ID(), 'borderless-cpt-options-featured-image', true ) == '2' ? $supports[] = 'thumbnail' : ''; |
| 125 |
$comments = get_post_meta( get_the_ID(), 'borderless-cpt-options-comments', true ) == '2' ? $supports[] = 'comments' : ''; |
| 126 |
$trackbacks = get_post_meta( get_the_ID(), 'borderless-cpt-options-trackbacks', true ) == '2' ? $supports[] = 'trackbacks' : ''; |
| 127 |
$revisions = get_post_meta( get_the_ID(), 'borderless-cpt-options-revisions', true ) == '2' ? $supports[] = 'revisions' : ''; |
| 128 |
$custom_fields = get_post_meta( get_the_ID(), 'borderless-cpt-options-custom-fields', true ) == '2' ? $supports[] = 'custom-fields' : ''; |
| 129 |
$page_attributes = get_post_meta( get_the_ID(), 'borderless-cpt-options-page-attributes', true ) == '2' ? $supports[] = 'page-attributes' : ''; |
| 130 |
$post_formats = get_post_meta( get_the_ID(), 'borderless-cpt-options-post-formats', true ) == '2' ? $supports[] = 'post-formats' : ''; |
| 131 |
$exclude_from_search = get_post_meta( get_the_ID(), 'borderless-cpt-options-exclude-from-search', true ) == '2' ? true : false; |
| 132 |
$enable_export = get_post_meta( get_the_ID(), 'borderless-cpt-options-enable-export', true ) == '2' ? true : false; |
| 133 |
$enable_archives = get_post_meta( get_the_ID(), 'borderless-cpt-options-enable-archives', true ); |
| 134 |
$custom_archive_slug = get_post_meta( get_the_ID(), 'borderless-cpt-options-custom-archive-slug', true ); |
| 135 |
|
| 136 |
if ( $enable_archives == 1 ) { |
| 137 |
$has_archive = false; |
| 138 |
} else if ( $enable_archives == 2 ) { |
| 139 |
$has_archive = true; |
| 140 |
} else if ( $enable_archives == 3 ) { |
| 141 |
if ( $custom_archive_slug == '' ) { $custom_archive_slug = true; } |
| 142 |
$has_archive = $custom_archive_slug; |
| 143 |
} else { |
| 144 |
$has_archive = true; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
/** |
| 149 |
* Presentation |
| 150 |
*/ |
| 151 |
|
| 152 |
$public = get_post_meta( get_the_ID(), 'borderless-cpt-presentation-public', true ) == '2' ? true : false; |
| 153 |
$show_ui = get_post_meta( get_the_ID(), 'borderless-cpt-presentation-show-ui', true ) == '2' ? true : false; |
| 154 |
$show_in_admin_sidebar = get_post_meta( get_the_ID(), 'borderless-cpt-presentation-show-in-admin-sidebar', true ); |
| 155 |
$show_in_admin_bar = get_post_meta( get_the_ID(), 'borderless-cpt-presentation-show-in-admin-bar', true ) == '2' ? true : false; |
| 156 |
$show_in_navigation_menus = get_post_meta( get_the_ID(), 'borderless-cpt-presentation-show-in-navigation-menus', true ) == '2' ? true : false; |
| 157 |
$admin_sidebar_icon = get_post_meta( get_the_ID(), 'borderless-cpt-presentation-admin-sidebar-icon', true ); |
| 158 |
|
| 159 |
|
| 160 |
/** |
| 161 |
* Query |
| 162 |
*/ |
| 163 |
|
| 164 |
$query = get_post_meta( get_the_ID(), 'borderless-cpt-query-query', true ); |
| 165 |
$publicly_queryable = get_post_meta( get_the_ID(), 'borderless-cpt-query-publicly-queryable', true ) == '2' ? true : false; |
| 166 |
$custom_query = get_post_meta( get_the_ID(), 'borderless-cpt-query-custom-query', true ); |
| 167 |
if ( $query == '1' ) { |
| 168 |
$query = $slug; |
| 169 |
} else { |
| 170 |
if ($custom_query == '') { $custom_query = $slug; } |
| 171 |
$query = $custom_query; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
/** |
| 176 |
* Permalinks |
| 177 |
*/ |
| 178 |
|
| 179 |
$permalink_rewrite = get_post_meta( get_the_ID(), 'borderless-cpt-permalinks-permalink-rewrite', true ); |
| 180 |
$use_url_slug = get_post_meta( get_the_ID(), 'borderless-cpt-permalinks-use-url-slug', true ) == '2' ? true : false; |
| 181 |
$pagination = get_post_meta( get_the_ID(), 'borderless-cpt-permalinks-pagination', true ) == '2' ? true : false; |
| 182 |
$feeds = get_post_meta( get_the_ID(), 'borderless-cpt-permalinks-feeds', true ) == '2' ? true : false; |
| 183 |
$url_slug = strtolower( get_post_meta( get_the_ID(), 'borderless-cpt-permalinks-url-slug', true ) ); |
| 184 |
|
| 185 |
if ( $permalink_rewrite == '1' ) { |
| 186 |
$rewrite = $slug; |
| 187 |
} else if ( $permalink_rewrite == '2' ) { |
| 188 |
$rewrite = false; |
| 189 |
} else if ( $permalink_rewrite == '3' ) { |
| 190 |
if ($url_slug == '') { $url_slug = $slug; } |
| 191 |
|
| 192 |
$rewrite = array( |
| 193 |
'slug' => $url_slug, |
| 194 |
'with_front' => $use_url_slug, |
| 195 |
'pages' => $pagination, |
| 196 |
'feeds' => $feeds, |
| 197 |
); |
| 198 |
} else { |
| 199 |
$rewrite = $slug; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
/** |
| 204 |
* Capabilities |
| 205 |
*/ |
| 206 |
|
| 207 |
$base_capabilities = get_post_meta( get_the_ID(), 'borderless-cpt-capabilities-base-capabilities', true ); |
| 208 |
$base_capability_type = get_post_meta( get_the_ID(), 'borderless-cpt-capabilities-base-capability-type', true ); |
| 209 |
$capability_type = get_post_meta( get_the_ID(), 'borderless-cpt-capabilities-capability-type', true ); |
| 210 |
|
| 211 |
if ( $base_capabilities == '2' ) { |
| 212 |
if ( !empty( $capability_type ) ) { |
| 213 |
if ( false !== strpos( $capability_type, ',' ) ) { |
| 214 |
$caps = array_map( 'trim', explode( ',', $capability_type ) ); |
| 215 |
if ( count( $caps ) > 2 ) { |
| 216 |
$caps = array_slice( $caps, 0, 2 ); |
| 217 |
} |
| 218 |
$capability_type = $caps; |
| 219 |
} |
| 220 |
$capabilities = array( |
| 221 |
'capabilities' => $capability_type, |
| 222 |
); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
/** |
| 228 |
* Rest API |
| 229 |
*/ |
| 230 |
|
| 231 |
$show_in_rest = get_post_meta( get_the_ID(), 'borderless-cpt-rest-api-show-in-rest', true ) == '2' ? true : false; |
| 232 |
$rest_base = get_post_meta( get_the_ID(), 'borderless-cpt-rest-api-rest-base', true ); |
| 233 |
$rest_controller_class = get_post_meta( get_the_ID(), 'borderless-cpt-rest-api-show-in-rest', true ); |
| 234 |
|
| 235 |
if ($rest_base == '') {$rest_base = $slug;} |
| 236 |
|
| 237 |
$labels = array( |
| 238 |
'name' => _x( $plural_name, 'Post Type General Name', 'borderless' ), |
| 239 |
'singular_name' => _x( $singular_name, 'Post Type Singular Name', 'borderless' ), |
| 240 |
'menu_name' => __( $menu_name, 'borderless' ), |
| 241 |
'name_admin_bar' => __( $admin_bar_name, 'borderless' ), |
| 242 |
'archives' => __( $archives, 'borderless' ), |
| 243 |
'attributes' => __( $attributes, 'borderless' ), |
| 244 |
'parent_item_colon' => __( $parent_item, 'borderless' ), |
| 245 |
'all_items' => __( $all_items, 'borderless' ), |
| 246 |
'add_new_item' => __( $add_new_item, 'borderless' ), |
| 247 |
'add_new' => __( $add_new, 'borderless' ), |
| 248 |
'new_item' => __( $new_item, 'borderless' ), |
| 249 |
'edit_item' => __( $edit_item, 'borderless' ), |
| 250 |
'update_item' => __( $update_item, 'borderless' ), |
| 251 |
'view_item' => __( $view_item, 'borderless' ), |
| 252 |
'view_items' => __( $view_items, 'borderless' ), |
| 253 |
'search_items' => __( $search_item, 'borderless' ), |
| 254 |
'not_found' => __( $not_found, 'borderless' ), |
| 255 |
'not_found_in_trash' => __( $not_found_in_trash, 'borderless' ), |
| 256 |
'featured_image' => __( $featured_image, 'borderless' ), |
| 257 |
'set_featured_image' => __( $set_featured_image, 'borderless' ), |
| 258 |
'remove_featured_image' => __( $remove_featured_image, 'borderless' ), |
| 259 |
'use_featured_image' => __( $use_as_featured_image, 'borderless' ), |
| 260 |
'insert_into_item' => __( $insert_into_item, 'borderless' ), |
| 261 |
'uploaded_to_this_item' => __( $uploaded_to_this_item, 'borderless' ), |
| 262 |
'items_list' => __( $items_list, 'borderless' ), |
| 263 |
'items_list_navigation' => __( $items_list_navigation, 'borderless' ), |
| 264 |
'filter_items_list' => __( $filter_items_list, 'borderless' ), |
| 265 |
); |
| 266 |
$args = array( |
| 267 |
'label' => __( $singular_name, 'borderless' ), |
| 268 |
'description' => __( 'Post Type Description', 'borderless' ), |
| 269 |
'labels' => $labels, |
| 270 |
'supports' => $supports, |
| 271 |
'taxonomies' => $taxonomies, |
| 272 |
'hierarchical' => $hierarchical, |
| 273 |
'public' => $public, |
| 274 |
'show_ui' => $show_ui, |
| 275 |
'show_in_menu' => true, |
| 276 |
'menu_position' => $show_in_admin_sidebar, |
| 277 |
'menu_icon' => $admin_sidebar_icon, |
| 278 |
'show_in_admin_bar' => $show_in_admin_bar, |
| 279 |
'show_in_nav_menus' => $show_in_navigation_menus, |
| 280 |
'can_export' => $enable_export, |
| 281 |
'has_archive' => $has_archive, |
| 282 |
'exclude_from_search' => $exclude_from_search, |
| 283 |
'publicly_queryable' => $publicly_queryable , |
| 284 |
'query_var' => $query, |
| 285 |
'rewrite' => $rewrite, |
| 286 |
'capability_type' => $base_capability_type, |
| 287 |
'show_in_rest' => $show_in_rest, |
| 288 |
'rest_base' => $rest_base, |
| 289 |
'rest_controller_class' => $rest_controller_class, |
| 290 |
); |
| 291 |
if ( $base_capabilities == '2' && !empty( $capability_type ) ) { $args = array_merge($args, $capabilities); } |
| 292 |
register_post_type($slug , $args); |
| 293 |
|
| 294 |
endwhile; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
/** |
| 299 |
* General |
| 300 |
*/ |
| 301 |
|
| 302 |
class borderless_cpt_general { |
| 303 |
private $config = '{"title":"General","prefix":"borderless-cpt-general-","domain":"borderless","class_name":"borderless_cpt_general","context":"normal","priority":"default","cpt":"borderless_cpt","fields":[{"type":"text","label":"Singular Name","default":"Post Type","id":"borderless-cpt-general-singular-name"},{"type":"text","label":"Plural Name","default":"Post Types","id":"borderless-cpt-general-plural-name"},{"type":"text","label":"Slug","id":"borderless-cpt-general-slug"},{"type":"select","label":"Categories","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-general-categories"},{"type":"select","label":"Tags","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-general-tags"}]}'; |
| 304 |
|
| 305 |
public function __construct() { |
| 306 |
$this->config = json_decode( $this->config, true ); |
| 307 |
$this->process_cpts(); |
| 308 |
add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] ); |
| 309 |
add_action( 'save_post', [ $this, 'save_post' ] ); |
| 310 |
} |
| 311 |
|
| 312 |
public function process_cpts() { |
| 313 |
if ( !empty( $this->config['cpt'] ) ) { |
| 314 |
if ( empty( $this->config['post-type'] ) ) { |
| 315 |
$this->config['post-type'] = []; |
| 316 |
} |
| 317 |
$parts = explode( ',', $this->config['cpt'] ); |
| 318 |
$parts = array_map( 'trim', $parts ); |
| 319 |
$this->config['post-type'] = array_merge( $this->config['post-type'], $parts ); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
public function add_meta_boxes() { |
| 324 |
foreach ( $this->config['post-type'] as $screen ) { |
| 325 |
add_meta_box( |
| 326 |
sanitize_title( $this->config['title'] ), |
| 327 |
$this->config['title'], |
| 328 |
[ $this, 'add_meta_box_callback' ], |
| 329 |
$screen, |
| 330 |
$this->config['context'], |
| 331 |
$this->config['priority'] |
| 332 |
); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
public function save_post( $post_id ) { |
| 337 |
foreach ( $this->config['fields'] as $field ) { |
| 338 |
switch ( $field['type'] ) { |
| 339 |
default: |
| 340 |
if ( isset( $_POST[ $field['id'] ] ) ) { |
| 341 |
$sanitized = sanitize_text_field( htmlspecialchars( $_POST[ $field['id'] ] ) ); |
| 342 |
update_post_meta( $post_id, $field['id'], $sanitized ); |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
public function add_meta_box_callback() { |
| 349 |
$this->fields_table(); |
| 350 |
} |
| 351 |
|
| 352 |
private function fields_table() { |
| 353 |
?><table class="form-table" role="presentation"> |
| 354 |
<tbody><?php |
| 355 |
foreach ( $this->config['fields'] as $field ) { |
| 356 |
?><tr> |
| 357 |
<th scope="row"><?php $this->label( $field ); ?></th> |
| 358 |
<td><?php $this->field( $field ); ?></td> |
| 359 |
</tr><?php |
| 360 |
} |
| 361 |
?></tbody> |
| 362 |
</table><?php |
| 363 |
} |
| 364 |
|
| 365 |
private function label( $field ) { |
| 366 |
switch ( $field['type'] ) { |
| 367 |
default: |
| 368 |
printf( |
| 369 |
'<label class="" for="%s">%s</label>', |
| 370 |
$field['id'], $field['label'] |
| 371 |
); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
private function field( $field ) { |
| 376 |
switch ( $field['type'] ) { |
| 377 |
case 'select': |
| 378 |
$this->select( $field ); |
| 379 |
break; |
| 380 |
default: |
| 381 |
$this->input( $field ); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
private function input( $field ) { |
| 386 |
printf( |
| 387 |
'<input class="regular-text %s" id="%s" name="%s" %s type="%s" value="%s">', |
| 388 |
isset( $field['class'] ) ? $field['class'] : '', |
| 389 |
$field['id'], $field['id'], |
| 390 |
isset( $field['pattern'] ) ? "pattern='{$field['pattern']}'" : '', |
| 391 |
$field['type'], |
| 392 |
$this->value( $field ) |
| 393 |
); |
| 394 |
} |
| 395 |
|
| 396 |
private function select( $field ) { |
| 397 |
printf( |
| 398 |
'<select id="%s" name="%s">%s</select>', |
| 399 |
$field['id'], $field['id'], |
| 400 |
$this->select_options( $field ) |
| 401 |
); |
| 402 |
} |
| 403 |
|
| 404 |
private function select_selected( $field, $current ) { |
| 405 |
$value = $this->value( $field ); |
| 406 |
if ( $value === $current ) { |
| 407 |
return 'selected'; |
| 408 |
} |
| 409 |
return ''; |
| 410 |
} |
| 411 |
|
| 412 |
private function select_options( $field ) { |
| 413 |
$output = []; |
| 414 |
$options = explode( "\r\n", $field['options'] ); |
| 415 |
$i = 0; |
| 416 |
foreach ( $options as $option ) { |
| 417 |
$pair = explode( ':', $option ); |
| 418 |
$pair = array_map( 'trim', $pair ); |
| 419 |
$output[] = sprintf( |
| 420 |
'<option %s value="%s"> %s</option>', |
| 421 |
$this->select_selected( $field, $pair[0] ), |
| 422 |
$pair[0], $pair[1] |
| 423 |
); |
| 424 |
$i++; |
| 425 |
} |
| 426 |
return implode( '<br>', $output ); |
| 427 |
} |
| 428 |
|
| 429 |
private function value( $field ) { |
| 430 |
global $post; |
| 431 |
if ( metadata_exists( 'post', $post->ID, $field['id'] ) ) { |
| 432 |
$value = get_post_meta( $post->ID, $field['id'], true ); |
| 433 |
} else if ( isset( $field['default'] ) ) { |
| 434 |
$value = $field['default']; |
| 435 |
} else { |
| 436 |
return ''; |
| 437 |
} |
| 438 |
return str_replace( '\u0027', "'", $value ); |
| 439 |
} |
| 440 |
|
| 441 |
} |
| 442 |
new borderless_cpt_general; |
| 443 |
|
| 444 |
|
| 445 |
/** |
| 446 |
* Labels |
| 447 |
*/ |
| 448 |
|
| 449 |
class borderless_cpt_labels { |
| 450 |
private $config = '{"title":"Labels","prefix":"borderless-cpt-labels-","domain":"borderless","class_name":"borderless_cpt_labels","context":"normal","priority":"default","cpt":"borderless_cpt","fields":[{"type":"text","label":"Menu Name","default":"Post Types","id":"borderless-cpt-labels-menu-name"},{"type":"text","label":"Admin Bar Name","default":"Post Type","id":"borderless-cpt-labels-admin-bar-name"},{"type":"text","label":"Archives","default":"Item Archives","id":"borderless-cpt-labels-archives"},{"type":"text","label":"Attributes","default":"Item Attributes","id":"borderless-cpt-labels-attributes"},{"type":"text","label":"Parent Item","default":"Parent Item:","id":"borderless-cpt-labels-parent-item"},{"type":"text","label":"All Items","default":"All Items","id":"borderless-cpt-labels-all-items"},{"type":"text","label":"Add New Item","default":"Add New Item","id":"borderless-cpt-labels-add-new-item"},{"type":"text","label":"Add New","default":"Add New","id":"borderless-cpt-labels-add-new"},{"type":"text","label":"New Item","default":"New Item","id":"borderless-cpt-labels-new-item"},{"type":"text","label":"Edit Item","default":"Edit Item","id":"borderless-cpt-labels-edit-item"},{"type":"text","label":"Update Item","default":"Update Item","id":"borderless-cpt-labels-update-item"},{"type":"text","label":"View Item","default":"View Item","id":"borderless-cpt-labels-view-item"},{"type":"text","label":"View Items","default":"View Items","id":"borderless-cpt-labels-view-items"},{"type":"text","label":"Search Item","default":"Search Item","id":"borderless-cpt-labels-search-item"},{"type":"text","label":"Not Found","default":"Not found","id":"borderless-cpt-labels-not-found"},{"type":"text","label":"Not Found in Trash","default":"Not found in Trash","id":"borderless-cpt-labels-not-found-in-trash"},{"type":"text","label":"Featured Image","default":"Featured Image","id":"borderless-cpt-labels-featured-image"},{"type":"text","label":"Set featured image","default":"Set featured image","id":"borderless-cpt-labels-set-featured-image"},{"type":"text","label":"Remove featured image","default":"Remove featured image","id":"borderless-cpt-labels-remove-featured-image"},{"type":"text","label":"Use as featured image","default":"Use as featured image","id":"borderless-cpt-labels-use-as-featured-image"},{"type":"text","label":"Insert into item","default":"Insert into item","id":"borderless-cpt-labels-insert-into-item"},{"type":"text","label":"Uploaded to this item","default":"Uploaded to this item","id":"borderless-cpt-labels-uploaded-to-this-item"},{"type":"text","label":"Items list","default":"Items list","id":"borderless-cpt-labels-items-list"},{"type":"text","label":"Items list navigation","default":"Items list navigation","id":"borderless-cpt-labels-items-list-navigation"},{"type":"text","label":"Filter items list","default":"Filter items list","id":"borderless-cpt-labels-filter-items-list"}]}'; |
| 451 |
|
| 452 |
public function __construct() { |
| 453 |
$this->config = json_decode( $this->config, true ); |
| 454 |
$this->process_cpts(); |
| 455 |
add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] ); |
| 456 |
add_action( 'save_post', [ $this, 'save_post' ] ); |
| 457 |
} |
| 458 |
|
| 459 |
public function process_cpts() { |
| 460 |
if ( !empty( $this->config['cpt'] ) ) { |
| 461 |
if ( empty( $this->config['post-type'] ) ) { |
| 462 |
$this->config['post-type'] = []; |
| 463 |
} |
| 464 |
$parts = explode( ',', $this->config['cpt'] ); |
| 465 |
$parts = array_map( 'trim', $parts ); |
| 466 |
$this->config['post-type'] = array_merge( $this->config['post-type'], $parts ); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
public function add_meta_boxes() { |
| 471 |
foreach ( $this->config['post-type'] as $screen ) { |
| 472 |
add_meta_box( |
| 473 |
sanitize_title( $this->config['title'] ), |
| 474 |
$this->config['title'], |
| 475 |
[ $this, 'add_meta_box_callback' ], |
| 476 |
$screen, |
| 477 |
$this->config['context'], |
| 478 |
$this->config['priority'] |
| 479 |
); |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
public function save_post( $post_id ) { |
| 484 |
foreach ( $this->config['fields'] as $field ) { |
| 485 |
switch ( $field['type'] ) { |
| 486 |
default: |
| 487 |
if ( isset( $_POST[ $field['id'] ] ) ) { |
| 488 |
$sanitized = sanitize_text_field( htmlspecialchars( $_POST[ $field['id'] ] ) ); |
| 489 |
update_post_meta( $post_id, $field['id'], $sanitized ); |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
public function add_meta_box_callback() { |
| 496 |
$this->fields_table(); |
| 497 |
} |
| 498 |
|
| 499 |
private function fields_table() { |
| 500 |
?><table class="form-table" role="presentation"> |
| 501 |
<tbody><?php |
| 502 |
foreach ( $this->config['fields'] as $field ) { |
| 503 |
?><tr> |
| 504 |
<th scope="row"><?php $this->label( $field ); ?></th> |
| 505 |
<td><?php $this->field( $field ); ?></td> |
| 506 |
</tr><?php |
| 507 |
} |
| 508 |
?></tbody> |
| 509 |
</table><?php |
| 510 |
} |
| 511 |
|
| 512 |
private function label( $field ) { |
| 513 |
switch ( $field['type'] ) { |
| 514 |
default: |
| 515 |
printf( |
| 516 |
'<label class="" for="%s">%s</label>', |
| 517 |
$field['id'], $field['label'] |
| 518 |
); |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
private function field( $field ) { |
| 523 |
switch ( $field['type'] ) { |
| 524 |
default: |
| 525 |
$this->input( $field ); |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
private function input( $field ) { |
| 530 |
printf( |
| 531 |
'<input class="regular-text %s" id="%s" name="%s" %s type="%s" value="%s">', |
| 532 |
isset( $field['class'] ) ? $field['class'] : '', |
| 533 |
$field['id'], $field['id'], |
| 534 |
isset( $field['pattern'] ) ? "pattern='{$field['pattern']}'" : '', |
| 535 |
$field['type'], |
| 536 |
$this->value( $field ) |
| 537 |
); |
| 538 |
} |
| 539 |
|
| 540 |
private function value( $field ) { |
| 541 |
global $post; |
| 542 |
if ( metadata_exists( 'post', $post->ID, $field['id'] ) ) { |
| 543 |
$value = get_post_meta( $post->ID, $field['id'], true ); |
| 544 |
} else if ( isset( $field['default'] ) ) { |
| 545 |
$value = $field['default']; |
| 546 |
} else { |
| 547 |
return ''; |
| 548 |
} |
| 549 |
return str_replace( '\u0027', "'", $value ); |
| 550 |
} |
| 551 |
|
| 552 |
} |
| 553 |
new borderless_cpt_labels; |
| 554 |
|
| 555 |
|
| 556 |
/** |
| 557 |
* Options |
| 558 |
*/ |
| 559 |
|
| 560 |
class borderless_cpt_options { |
| 561 |
private $config = '{"title":"Options","prefix":"borderless-cpt-options-","domain":"borderless","class_name":"borderless_cpt_options","context":"normal","priority":"default","cpt":"borderless_cpt","fields":[{"type":"select","label":"Title","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-options-title"},{"type":"select","label":"Editor","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-options-editor"},{"type":"select","label":"Excerpt","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-options-excerpt"},{"type":"select","label":"Author","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-options-author"},{"type":"select","label":"Featured Image","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-options-featured-image"},{"type":"select","label":"Comments","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-options-comments"},{"type":"select","label":"Trackbacks","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-options-trackbacks"},{"type":"select","label":"Revisions","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-options-revisions"},{"type":"select","label":"Custom Fields","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-options-custom-fields"},{"type":"select","label":"Page Attributes","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-options-page-attributes"},{"type":"select","label":"Post Formats","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-options-post-formats"},{"type":"select","label":"Exclude From Search","default":"1","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-options-exclude-from-search"},{"type":"select","label":"Enable Export","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-options-enable-export"},{"type":"select","label":"Enable Archives","default":"2","options":"1 : Prevent archive pages\r\n2 : Use default slug\r\n3 : Set custom archive slug","id":"borderless-cpt-options-enable-archives"},{"type":"text","label":"Custom Archive Slug","id":"borderless-cpt-options-custom-archive-slug"}]}'; |
| 562 |
|
| 563 |
public function __construct() { |
| 564 |
$this->config = json_decode( $this->config, true ); |
| 565 |
$this->process_cpts(); |
| 566 |
add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] ); |
| 567 |
add_action( 'save_post', [ $this, 'save_post' ] ); |
| 568 |
} |
| 569 |
|
| 570 |
public function process_cpts() { |
| 571 |
if ( !empty( $this->config['cpt'] ) ) { |
| 572 |
if ( empty( $this->config['post-type'] ) ) { |
| 573 |
$this->config['post-type'] = []; |
| 574 |
} |
| 575 |
$parts = explode( ',', $this->config['cpt'] ); |
| 576 |
$parts = array_map( 'trim', $parts ); |
| 577 |
$this->config['post-type'] = array_merge( $this->config['post-type'], $parts ); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
public function add_meta_boxes() { |
| 582 |
foreach ( $this->config['post-type'] as $screen ) { |
| 583 |
add_meta_box( |
| 584 |
sanitize_title( $this->config['title'] ), |
| 585 |
$this->config['title'], |
| 586 |
[ $this, 'add_meta_box_callback' ], |
| 587 |
$screen, |
| 588 |
$this->config['context'], |
| 589 |
$this->config['priority'] |
| 590 |
); |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
public function save_post( $post_id ) { |
| 595 |
foreach ( $this->config['fields'] as $field ) { |
| 596 |
switch ( $field['type'] ) { |
| 597 |
default: |
| 598 |
if ( isset( $_POST[ $field['id'] ] ) ) { |
| 599 |
$sanitized = sanitize_text_field( htmlspecialchars( $_POST[ $field['id'] ] ) ); |
| 600 |
update_post_meta( $post_id, $field['id'], $sanitized ); |
| 601 |
} |
| 602 |
} |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
public function add_meta_box_callback() { |
| 607 |
$this->fields_table(); |
| 608 |
} |
| 609 |
|
| 610 |
private function fields_table() { |
| 611 |
?><table class="form-table" role="presentation"> |
| 612 |
<tbody><?php |
| 613 |
foreach ( $this->config['fields'] as $field ) { |
| 614 |
?><tr> |
| 615 |
<th scope="row"><?php $this->label( $field ); ?></th> |
| 616 |
<td><?php $this->field( $field ); ?></td> |
| 617 |
</tr><?php |
| 618 |
} |
| 619 |
?></tbody> |
| 620 |
</table><?php |
| 621 |
} |
| 622 |
|
| 623 |
private function label( $field ) { |
| 624 |
switch ( $field['type'] ) { |
| 625 |
default: |
| 626 |
printf( |
| 627 |
'<label class="" for="%s">%s</label>', |
| 628 |
$field['id'], $field['label'] |
| 629 |
); |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
private function field( $field ) { |
| 634 |
switch ( $field['type'] ) { |
| 635 |
case 'select': |
| 636 |
$this->select( $field ); |
| 637 |
break; |
| 638 |
default: |
| 639 |
$this->input( $field ); |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
private function input( $field ) { |
| 644 |
printf( |
| 645 |
'<input class="regular-text %s" id="%s" name="%s" %s type="%s" value="%s">', |
| 646 |
isset( $field['class'] ) ? $field['class'] : '', |
| 647 |
$field['id'], $field['id'], |
| 648 |
isset( $field['pattern'] ) ? "pattern='{$field['pattern']}'" : '', |
| 649 |
$field['type'], |
| 650 |
$this->value( $field ) |
| 651 |
); |
| 652 |
} |
| 653 |
|
| 654 |
private function select( $field ) { |
| 655 |
printf( |
| 656 |
'<select id="%s" name="%s">%s</select>', |
| 657 |
$field['id'], $field['id'], |
| 658 |
$this->select_options( $field ) |
| 659 |
); |
| 660 |
} |
| 661 |
|
| 662 |
private function select_selected( $field, $current ) { |
| 663 |
$value = $this->value( $field ); |
| 664 |
if ( $value === $current ) { |
| 665 |
return 'selected'; |
| 666 |
} |
| 667 |
return ''; |
| 668 |
} |
| 669 |
|
| 670 |
private function select_options( $field ) { |
| 671 |
$output = []; |
| 672 |
$options = explode( "\r\n", $field['options'] ); |
| 673 |
$i = 0; |
| 674 |
foreach ( $options as $option ) { |
| 675 |
$pair = explode( ':', $option ); |
| 676 |
$pair = array_map( 'trim', $pair ); |
| 677 |
$output[] = sprintf( |
| 678 |
'<option %s value="%s"> %s</option>', |
| 679 |
$this->select_selected( $field, $pair[0] ), |
| 680 |
$pair[0], $pair[1] |
| 681 |
); |
| 682 |
$i++; |
| 683 |
} |
| 684 |
return implode( '<br>', $output ); |
| 685 |
} |
| 686 |
|
| 687 |
private function value( $field ) { |
| 688 |
global $post; |
| 689 |
if ( metadata_exists( 'post', $post->ID, $field['id'] ) ) { |
| 690 |
$value = get_post_meta( $post->ID, $field['id'], true ); |
| 691 |
} else if ( isset( $field['default'] ) ) { |
| 692 |
$value = $field['default']; |
| 693 |
} else { |
| 694 |
return ''; |
| 695 |
} |
| 696 |
return str_replace( '\u0027', "'", $value ); |
| 697 |
} |
| 698 |
|
| 699 |
} |
| 700 |
new borderless_cpt_options; |
| 701 |
|
| 702 |
|
| 703 |
/** |
| 704 |
* Presentation |
| 705 |
*/ |
| 706 |
|
| 707 |
class borderless_cpt_presentation { |
| 708 |
private $config = '{"title":"Presentation","prefix":"borderless-cpt-presentation-","domain":"borderless","class_name":"borderless_cpt_presentation","context":"normal","priority":"default","cpt":"borderless_cpt","fields":[{"type":"select","label":"Public","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-presentation-public"},{"type":"select","label":"Show in Admin Bar","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-presentation-show-in-admin-bar"},{"type":"select","label":"Show in Navigation Menus","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-presentation-show-in-navigation-menus"},{"type":"select","label":"Show UI","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-presentation-show-ui"},{"type":"select","label":"Show in Admin Sidebar","default":"5","options":"2 : Dashboard\r\n4 : Separator\r\n5 : Posts\r\n10 : Media\r\n15 : Links\r\n20 : Pages\r\n25 : Comments\r\n59 : Separator\r\n60 : Appearance\r\n65 : Plugins\r\n70 : Users\r\n75 : Tools\r\n80 : Settings\r\n99 : Separator","id":"borderless-cpt-presentation-show-in-admin-sidebar"},{"type":"text","label":"Admin Sidebar Icon","default":"dashicons-admin-generic","id":"borderless-cpt-presentation-admin-sidebar-icon"}]}'; |
| 709 |
|
| 710 |
public function __construct() { |
| 711 |
$this->config = json_decode( $this->config, true ); |
| 712 |
$this->process_cpts(); |
| 713 |
add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] ); |
| 714 |
add_action( 'save_post', [ $this, 'save_post' ] ); |
| 715 |
} |
| 716 |
|
| 717 |
public function process_cpts() { |
| 718 |
if ( !empty( $this->config['cpt'] ) ) { |
| 719 |
if ( empty( $this->config['post-type'] ) ) { |
| 720 |
$this->config['post-type'] = []; |
| 721 |
} |
| 722 |
$parts = explode( ',', $this->config['cpt'] ); |
| 723 |
$parts = array_map( 'trim', $parts ); |
| 724 |
$this->config['post-type'] = array_merge( $this->config['post-type'], $parts ); |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
public function add_meta_boxes() { |
| 729 |
foreach ( $this->config['post-type'] as $screen ) { |
| 730 |
add_meta_box( |
| 731 |
sanitize_title( $this->config['title'] ), |
| 732 |
$this->config['title'], |
| 733 |
[ $this, 'add_meta_box_callback' ], |
| 734 |
$screen, |
| 735 |
$this->config['context'], |
| 736 |
$this->config['priority'] |
| 737 |
); |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
public function save_post( $post_id ) { |
| 742 |
foreach ( $this->config['fields'] as $field ) { |
| 743 |
switch ( $field['type'] ) { |
| 744 |
default: |
| 745 |
if ( isset( $_POST[ $field['id'] ] ) ) { |
| 746 |
$sanitized = sanitize_text_field( htmlspecialchars( $_POST[ $field['id'] ] ) ); |
| 747 |
update_post_meta( $post_id, $field['id'], $sanitized ); |
| 748 |
} |
| 749 |
} |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
public function add_meta_box_callback() { |
| 754 |
$this->fields_table(); |
| 755 |
} |
| 756 |
|
| 757 |
private function fields_table() { |
| 758 |
?><table class="form-table" role="presentation"> |
| 759 |
<tbody><?php |
| 760 |
foreach ( $this->config['fields'] as $field ) { |
| 761 |
?><tr> |
| 762 |
<th scope="row"><?php $this->label( $field ); ?></th> |
| 763 |
<td><?php $this->field( $field ); ?></td> |
| 764 |
</tr><?php |
| 765 |
} |
| 766 |
?></tbody> |
| 767 |
</table><?php |
| 768 |
} |
| 769 |
|
| 770 |
private function label( $field ) { |
| 771 |
switch ( $field['type'] ) { |
| 772 |
default: |
| 773 |
printf( |
| 774 |
'<label class="" for="%s">%s</label>', |
| 775 |
$field['id'], $field['label'] |
| 776 |
); |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
private function field( $field ) { |
| 781 |
switch ( $field['type'] ) { |
| 782 |
case 'select': |
| 783 |
$this->select( $field ); |
| 784 |
break; |
| 785 |
default: |
| 786 |
$this->input( $field ); |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
private function input( $field ) { |
| 791 |
printf( |
| 792 |
'<input class="regular-text %s" id="%s" name="%s" %s type="%s" value="%s">', |
| 793 |
isset( $field['class'] ) ? $field['class'] : '', |
| 794 |
$field['id'], $field['id'], |
| 795 |
isset( $field['pattern'] ) ? "pattern='{$field['pattern']}'" : '', |
| 796 |
$field['type'], |
| 797 |
$this->value( $field ) |
| 798 |
); |
| 799 |
} |
| 800 |
|
| 801 |
private function select( $field ) { |
| 802 |
printf( |
| 803 |
'<select id="%s" name="%s">%s</select>', |
| 804 |
$field['id'], $field['id'], |
| 805 |
$this->select_options( $field ) |
| 806 |
); |
| 807 |
} |
| 808 |
|
| 809 |
private function select_selected( $field, $current ) { |
| 810 |
$value = $this->value( $field ); |
| 811 |
if ( $value === $current ) { |
| 812 |
return 'selected'; |
| 813 |
} |
| 814 |
return ''; |
| 815 |
} |
| 816 |
|
| 817 |
private function select_options( $field ) { |
| 818 |
$output = []; |
| 819 |
$options = explode( "\r\n", $field['options'] ); |
| 820 |
$i = 0; |
| 821 |
foreach ( $options as $option ) { |
| 822 |
$pair = explode( ':', $option ); |
| 823 |
$pair = array_map( 'trim', $pair ); |
| 824 |
$output[] = sprintf( |
| 825 |
'<option %s value="%s"> %s</option>', |
| 826 |
$this->select_selected( $field, $pair[0] ), |
| 827 |
$pair[0], $pair[1] |
| 828 |
); |
| 829 |
$i++; |
| 830 |
} |
| 831 |
return implode( '<br>', $output ); |
| 832 |
} |
| 833 |
|
| 834 |
private function value( $field ) { |
| 835 |
global $post; |
| 836 |
if ( metadata_exists( 'post', $post->ID, $field['id'] ) ) { |
| 837 |
$value = get_post_meta( $post->ID, $field['id'], true ); |
| 838 |
} else if ( isset( $field['default'] ) ) { |
| 839 |
$value = $field['default']; |
| 840 |
} else { |
| 841 |
return ''; |
| 842 |
} |
| 843 |
return str_replace( '\u0027', "'", $value ); |
| 844 |
} |
| 845 |
|
| 846 |
} |
| 847 |
new borderless_cpt_presentation; |
| 848 |
|
| 849 |
|
| 850 |
/** |
| 851 |
* Query |
| 852 |
*/ |
| 853 |
|
| 854 |
class borderless_cpt_query { |
| 855 |
private $config = '{"title":"Query","prefix":"borderless-cpt-query-","domain":"borderless","class_name":"borderless_cpt_query","context":"normal","priority":"default","cpt":"borderless_cpt","fields":[{"type":"select","label":"Query","default":"1","options":"1 : Default post type key\r\n2 : Custom query variable","id":"borderless-cpt-query-query"},{"type":"select","label":"Publicly Queryable","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-query-publicly-queryable"},{"type":"text","label":"Custom Query","id":"borderless-cpt-query-custom-query"}]}'; |
| 856 |
|
| 857 |
public function __construct() { |
| 858 |
$this->config = json_decode( $this->config, true ); |
| 859 |
$this->process_cpts(); |
| 860 |
add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] ); |
| 861 |
add_action( 'save_post', [ $this, 'save_post' ] ); |
| 862 |
} |
| 863 |
|
| 864 |
public function process_cpts() { |
| 865 |
if ( !empty( $this->config['cpt'] ) ) { |
| 866 |
if ( empty( $this->config['post-type'] ) ) { |
| 867 |
$this->config['post-type'] = []; |
| 868 |
} |
| 869 |
$parts = explode( ',', $this->config['cpt'] ); |
| 870 |
$parts = array_map( 'trim', $parts ); |
| 871 |
$this->config['post-type'] = array_merge( $this->config['post-type'], $parts ); |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
public function add_meta_boxes() { |
| 876 |
foreach ( $this->config['post-type'] as $screen ) { |
| 877 |
add_meta_box( |
| 878 |
sanitize_title( $this->config['title'] ), |
| 879 |
$this->config['title'], |
| 880 |
[ $this, 'add_meta_box_callback' ], |
| 881 |
$screen, |
| 882 |
$this->config['context'], |
| 883 |
$this->config['priority'] |
| 884 |
); |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
public function save_post( $post_id ) { |
| 889 |
foreach ( $this->config['fields'] as $field ) { |
| 890 |
switch ( $field['type'] ) { |
| 891 |
default: |
| 892 |
if ( isset( $_POST[ $field['id'] ] ) ) { |
| 893 |
$sanitized = sanitize_text_field( htmlspecialchars( $_POST[ $field['id'] ] ) ); |
| 894 |
update_post_meta( $post_id, $field['id'], $sanitized ); |
| 895 |
} |
| 896 |
} |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
public function add_meta_box_callback() { |
| 901 |
$this->fields_table(); |
| 902 |
} |
| 903 |
|
| 904 |
private function fields_table() { |
| 905 |
?><table class="form-table" role="presentation"> |
| 906 |
<tbody><?php |
| 907 |
foreach ( $this->config['fields'] as $field ) { |
| 908 |
?><tr> |
| 909 |
<th scope="row"><?php $this->label( $field ); ?></th> |
| 910 |
<td><?php $this->field( $field ); ?></td> |
| 911 |
</tr><?php |
| 912 |
} |
| 913 |
?></tbody> |
| 914 |
</table><?php |
| 915 |
} |
| 916 |
|
| 917 |
private function label( $field ) { |
| 918 |
switch ( $field['type'] ) { |
| 919 |
default: |
| 920 |
printf( |
| 921 |
'<label class="" for="%s">%s</label>', |
| 922 |
$field['id'], $field['label'] |
| 923 |
); |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
private function field( $field ) { |
| 928 |
switch ( $field['type'] ) { |
| 929 |
case 'select': |
| 930 |
$this->select( $field ); |
| 931 |
break; |
| 932 |
default: |
| 933 |
$this->input( $field ); |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
private function input( $field ) { |
| 938 |
printf( |
| 939 |
'<input class="regular-text %s" id="%s" name="%s" %s type="%s" value="%s">', |
| 940 |
isset( $field['class'] ) ? $field['class'] : '', |
| 941 |
$field['id'], $field['id'], |
| 942 |
isset( $field['pattern'] ) ? "pattern='{$field['pattern']}'" : '', |
| 943 |
$field['type'], |
| 944 |
$this->value( $field ) |
| 945 |
); |
| 946 |
} |
| 947 |
|
| 948 |
private function select( $field ) { |
| 949 |
printf( |
| 950 |
'<select id="%s" name="%s">%s</select>', |
| 951 |
$field['id'], $field['id'], |
| 952 |
$this->select_options( $field ) |
| 953 |
); |
| 954 |
} |
| 955 |
|
| 956 |
private function select_selected( $field, $current ) { |
| 957 |
$value = $this->value( $field ); |
| 958 |
if ( $value === $current ) { |
| 959 |
return 'selected'; |
| 960 |
} |
| 961 |
return ''; |
| 962 |
} |
| 963 |
|
| 964 |
private function select_options( $field ) { |
| 965 |
$output = []; |
| 966 |
$options = explode( "\r\n", $field['options'] ); |
| 967 |
$i = 0; |
| 968 |
foreach ( $options as $option ) { |
| 969 |
$pair = explode( ':', $option ); |
| 970 |
$pair = array_map( 'trim', $pair ); |
| 971 |
$output[] = sprintf( |
| 972 |
'<option %s value="%s"> %s</option>', |
| 973 |
$this->select_selected( $field, $pair[0] ), |
| 974 |
$pair[0], $pair[1] |
| 975 |
); |
| 976 |
$i++; |
| 977 |
} |
| 978 |
return implode( '<br>', $output ); |
| 979 |
} |
| 980 |
|
| 981 |
private function value( $field ) { |
| 982 |
global $post; |
| 983 |
if ( metadata_exists( 'post', $post->ID, $field['id'] ) ) { |
| 984 |
$value = get_post_meta( $post->ID, $field['id'], true ); |
| 985 |
} else if ( isset( $field['default'] ) ) { |
| 986 |
$value = $field['default']; |
| 987 |
} else { |
| 988 |
return ''; |
| 989 |
} |
| 990 |
return str_replace( '\u0027', "'", $value ); |
| 991 |
} |
| 992 |
|
| 993 |
} |
| 994 |
new borderless_cpt_query; |
| 995 |
|
| 996 |
|
| 997 |
/** |
| 998 |
* Permalinks |
| 999 |
*/ |
| 1000 |
|
| 1001 |
class borderless_cpt_permalinks { |
| 1002 |
private $config = '{"title":"Permalinks","prefix":"borderless-cpt-permalinks-","domain":"borderless","class_name":"borderless_cpt_permalinks","context":"normal","priority":"default","cpt":"borderless_cpt","fields":[{"type":"select","label":"Permalink Rewrite","default":"1","options":"1 : Default permalink\r\n2 : No permalink\r\n3 : Custom permalink","id":"borderless-cpt-permalinks-permalink-rewrite"},{"type":"select","label":"Use URL Slug","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-permalinks-use-url-slug"},{"type":"select","label":"Pagination","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-permalinks-pagination"},{"type":"select","label":"Feeds","default":"2","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-permalinks-feeds"},{"type":"text","label":"URL Slug","id":"borderless-cpt-permalinks-url-slug"}]}'; |
| 1003 |
|
| 1004 |
public function __construct() { |
| 1005 |
$this->config = json_decode( $this->config, true ); |
| 1006 |
$this->process_cpts(); |
| 1007 |
add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] ); |
| 1008 |
add_action( 'save_post', [ $this, 'save_post' ] ); |
| 1009 |
} |
| 1010 |
|
| 1011 |
public function process_cpts() { |
| 1012 |
if ( !empty( $this->config['cpt'] ) ) { |
| 1013 |
if ( empty( $this->config['post-type'] ) ) { |
| 1014 |
$this->config['post-type'] = []; |
| 1015 |
} |
| 1016 |
$parts = explode( ',', $this->config['cpt'] ); |
| 1017 |
$parts = array_map( 'trim', $parts ); |
| 1018 |
$this->config['post-type'] = array_merge( $this->config['post-type'], $parts ); |
| 1019 |
} |
| 1020 |
} |
| 1021 |
|
| 1022 |
public function add_meta_boxes() { |
| 1023 |
foreach ( $this->config['post-type'] as $screen ) { |
| 1024 |
add_meta_box( |
| 1025 |
sanitize_title( $this->config['title'] ), |
| 1026 |
$this->config['title'], |
| 1027 |
[ $this, 'add_meta_box_callback' ], |
| 1028 |
$screen, |
| 1029 |
$this->config['context'], |
| 1030 |
$this->config['priority'] |
| 1031 |
); |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
public function save_post( $post_id ) { |
| 1036 |
foreach ( $this->config['fields'] as $field ) { |
| 1037 |
switch ( $field['type'] ) { |
| 1038 |
default: |
| 1039 |
if ( isset( $_POST[ $field['id'] ] ) ) { |
| 1040 |
$sanitized = sanitize_text_field( htmlspecialchars( $_POST[ $field['id'] ] ) ); |
| 1041 |
update_post_meta( $post_id, $field['id'], $sanitized ); |
| 1042 |
} |
| 1043 |
} |
| 1044 |
} |
| 1045 |
} |
| 1046 |
|
| 1047 |
public function add_meta_box_callback() { |
| 1048 |
$this->fields_table(); |
| 1049 |
} |
| 1050 |
|
| 1051 |
private function fields_table() { |
| 1052 |
?><table class="form-table" role="presentation"> |
| 1053 |
<tbody><?php |
| 1054 |
foreach ( $this->config['fields'] as $field ) { |
| 1055 |
?><tr> |
| 1056 |
<th scope="row"><?php $this->label( $field ); ?></th> |
| 1057 |
<td><?php $this->field( $field ); ?></td> |
| 1058 |
</tr><?php |
| 1059 |
} |
| 1060 |
?></tbody> |
| 1061 |
</table><?php |
| 1062 |
} |
| 1063 |
|
| 1064 |
private function label( $field ) { |
| 1065 |
switch ( $field['type'] ) { |
| 1066 |
default: |
| 1067 |
printf( |
| 1068 |
'<label class="" for="%s">%s</label>', |
| 1069 |
$field['id'], $field['label'] |
| 1070 |
); |
| 1071 |
} |
| 1072 |
} |
| 1073 |
|
| 1074 |
private function field( $field ) { |
| 1075 |
switch ( $field['type'] ) { |
| 1076 |
case 'select': |
| 1077 |
$this->select( $field ); |
| 1078 |
break; |
| 1079 |
default: |
| 1080 |
$this->input( $field ); |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
private function input( $field ) { |
| 1085 |
printf( |
| 1086 |
'<input class="regular-text %s" id="%s" name="%s" %s type="%s" value="%s">', |
| 1087 |
isset( $field['class'] ) ? $field['class'] : '', |
| 1088 |
$field['id'], $field['id'], |
| 1089 |
isset( $field['pattern'] ) ? "pattern='{$field['pattern']}'" : '', |
| 1090 |
$field['type'], |
| 1091 |
$this->value( $field ) |
| 1092 |
); |
| 1093 |
} |
| 1094 |
|
| 1095 |
private function select( $field ) { |
| 1096 |
printf( |
| 1097 |
'<select id="%s" name="%s">%s</select>', |
| 1098 |
$field['id'], $field['id'], |
| 1099 |
$this->select_options( $field ) |
| 1100 |
); |
| 1101 |
} |
| 1102 |
|
| 1103 |
private function select_selected( $field, $current ) { |
| 1104 |
$value = $this->value( $field ); |
| 1105 |
if ( $value === $current ) { |
| 1106 |
return 'selected'; |
| 1107 |
} |
| 1108 |
return ''; |
| 1109 |
} |
| 1110 |
|
| 1111 |
private function select_options( $field ) { |
| 1112 |
$output = []; |
| 1113 |
$options = explode( "\r\n", $field['options'] ); |
| 1114 |
$i = 0; |
| 1115 |
foreach ( $options as $option ) { |
| 1116 |
$pair = explode( ':', $option ); |
| 1117 |
$pair = array_map( 'trim', $pair ); |
| 1118 |
$output[] = sprintf( |
| 1119 |
'<option %s value="%s"> %s</option>', |
| 1120 |
$this->select_selected( $field, $pair[0] ), |
| 1121 |
$pair[0], $pair[1] |
| 1122 |
); |
| 1123 |
$i++; |
| 1124 |
} |
| 1125 |
return implode( '<br>', $output ); |
| 1126 |
} |
| 1127 |
|
| 1128 |
private function value( $field ) { |
| 1129 |
global $post; |
| 1130 |
if ( metadata_exists( 'post', $post->ID, $field['id'] ) ) { |
| 1131 |
$value = get_post_meta( $post->ID, $field['id'], true ); |
| 1132 |
} else if ( isset( $field['default'] ) ) { |
| 1133 |
$value = $field['default']; |
| 1134 |
} else { |
| 1135 |
return ''; |
| 1136 |
} |
| 1137 |
return str_replace( '\u0027', "'", $value ); |
| 1138 |
} |
| 1139 |
|
| 1140 |
} |
| 1141 |
new borderless_cpt_permalinks; |
| 1142 |
|
| 1143 |
|
| 1144 |
/** |
| 1145 |
* Capabilities |
| 1146 |
*/ |
| 1147 |
|
| 1148 |
class borderless_cpt_capabilities { |
| 1149 |
private $config = '{"title":"Capabilities","prefix":"borderless-cpt-capabilities-","domain":"borderless","class_name":"borderless_cpt_capabilities","context":"normal","priority":"default","cpt":"borderless_cpt","fields":[{"type":"select","label":"Base Capabilities","default":"1","options":"1 : Base capabilities\r\n2 : Custom capabilities","id":"borderless-cpt-capabilities-base-capabilities"},{"type":"select","label":"Base Capability Type","default":"page","options":"page : Pages\r\npost : Posts","id":"borderless-cpt-capabilities-base-capability-type"},{"type":"text","label":"Capability Type","default":"story, stories","id":"borderless-cpt-capabilities-capability-type"}]}'; |
| 1150 |
|
| 1151 |
public function __construct() { |
| 1152 |
$this->config = json_decode( $this->config, true ); |
| 1153 |
$this->process_cpts(); |
| 1154 |
add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] ); |
| 1155 |
add_action( 'save_post', [ $this, 'save_post' ] ); |
| 1156 |
} |
| 1157 |
|
| 1158 |
public function process_cpts() { |
| 1159 |
if ( !empty( $this->config['cpt'] ) ) { |
| 1160 |
if ( empty( $this->config['post-type'] ) ) { |
| 1161 |
$this->config['post-type'] = []; |
| 1162 |
} |
| 1163 |
$parts = explode( ',', $this->config['cpt'] ); |
| 1164 |
$parts = array_map( 'trim', $parts ); |
| 1165 |
$this->config['post-type'] = array_merge( $this->config['post-type'], $parts ); |
| 1166 |
} |
| 1167 |
} |
| 1168 |
|
| 1169 |
public function add_meta_boxes() { |
| 1170 |
foreach ( $this->config['post-type'] as $screen ) { |
| 1171 |
add_meta_box( |
| 1172 |
sanitize_title( $this->config['title'] ), |
| 1173 |
$this->config['title'], |
| 1174 |
[ $this, 'add_meta_box_callback' ], |
| 1175 |
$screen, |
| 1176 |
$this->config['context'], |
| 1177 |
$this->config['priority'] |
| 1178 |
); |
| 1179 |
} |
| 1180 |
} |
| 1181 |
|
| 1182 |
public function save_post( $post_id ) { |
| 1183 |
foreach ( $this->config['fields'] as $field ) { |
| 1184 |
switch ( $field['type'] ) { |
| 1185 |
default: |
| 1186 |
if ( isset( $_POST[ $field['id'] ] ) ) { |
| 1187 |
$sanitized = sanitize_text_field( htmlspecialchars( $_POST[ $field['id'] ] ) ); |
| 1188 |
update_post_meta( $post_id, $field['id'], $sanitized ); |
| 1189 |
} |
| 1190 |
} |
| 1191 |
} |
| 1192 |
} |
| 1193 |
|
| 1194 |
public function add_meta_box_callback() { |
| 1195 |
$this->fields_table(); |
| 1196 |
} |
| 1197 |
|
| 1198 |
private function fields_table() { |
| 1199 |
?><table class="form-table" role="presentation"> |
| 1200 |
<tbody><?php |
| 1201 |
foreach ( $this->config['fields'] as $field ) { |
| 1202 |
?><tr> |
| 1203 |
<th scope="row"><?php $this->label( $field ); ?></th> |
| 1204 |
<td><?php $this->field( $field ); ?></td> |
| 1205 |
</tr><?php |
| 1206 |
} |
| 1207 |
?></tbody> |
| 1208 |
</table><?php |
| 1209 |
} |
| 1210 |
|
| 1211 |
private function label( $field ) { |
| 1212 |
switch ( $field['type'] ) { |
| 1213 |
default: |
| 1214 |
printf( |
| 1215 |
'<label class="" for="%s">%s</label>', |
| 1216 |
$field['id'], $field['label'] |
| 1217 |
); |
| 1218 |
} |
| 1219 |
} |
| 1220 |
|
| 1221 |
private function field( $field ) { |
| 1222 |
switch ( $field['type'] ) { |
| 1223 |
case 'select': |
| 1224 |
$this->select( $field ); |
| 1225 |
break; |
| 1226 |
default: |
| 1227 |
$this->input( $field ); |
| 1228 |
} |
| 1229 |
} |
| 1230 |
|
| 1231 |
private function input( $field ) { |
| 1232 |
printf( |
| 1233 |
'<input class="regular-text %s" id="%s" name="%s" %s type="%s" value="%s">', |
| 1234 |
isset( $field['class'] ) ? $field['class'] : '', |
| 1235 |
$field['id'], $field['id'], |
| 1236 |
isset( $field['pattern'] ) ? "pattern='{$field['pattern']}'" : '', |
| 1237 |
$field['type'], |
| 1238 |
$this->value( $field ) |
| 1239 |
); |
| 1240 |
} |
| 1241 |
|
| 1242 |
private function select( $field ) { |
| 1243 |
printf( |
| 1244 |
'<select id="%s" name="%s">%s</select>', |
| 1245 |
$field['id'], $field['id'], |
| 1246 |
$this->select_options( $field ) |
| 1247 |
); |
| 1248 |
} |
| 1249 |
|
| 1250 |
private function select_selected( $field, $current ) { |
| 1251 |
$value = $this->value( $field ); |
| 1252 |
if ( $value === $current ) { |
| 1253 |
return 'selected'; |
| 1254 |
} |
| 1255 |
return ''; |
| 1256 |
} |
| 1257 |
|
| 1258 |
private function select_options( $field ) { |
| 1259 |
$output = []; |
| 1260 |
$options = explode( "\r\n", $field['options'] ); |
| 1261 |
$i = 0; |
| 1262 |
foreach ( $options as $option ) { |
| 1263 |
$pair = explode( ':', $option ); |
| 1264 |
$pair = array_map( 'trim', $pair ); |
| 1265 |
$output[] = sprintf( |
| 1266 |
'<option %s value="%s"> %s</option>', |
| 1267 |
$this->select_selected( $field, $pair[0] ), |
| 1268 |
$pair[0], $pair[1] |
| 1269 |
); |
| 1270 |
$i++; |
| 1271 |
} |
| 1272 |
return implode( '<br>', $output ); |
| 1273 |
} |
| 1274 |
|
| 1275 |
private function value( $field ) { |
| 1276 |
global $post; |
| 1277 |
if ( metadata_exists( 'post', $post->ID, $field['id'] ) ) { |
| 1278 |
$value = get_post_meta( $post->ID, $field['id'], true ); |
| 1279 |
} else if ( isset( $field['default'] ) ) { |
| 1280 |
$value = $field['default']; |
| 1281 |
} else { |
| 1282 |
return ''; |
| 1283 |
} |
| 1284 |
return str_replace( '\u0027', "'", $value ); |
| 1285 |
} |
| 1286 |
|
| 1287 |
} |
| 1288 |
new borderless_cpt_capabilities; |
| 1289 |
|
| 1290 |
|
| 1291 |
/** |
| 1292 |
* Rest API |
| 1293 |
*/ |
| 1294 |
|
| 1295 |
class borderless_cpt_rest_api { |
| 1296 |
private $config = '{"title":"Rest API","prefix":"borderless-cpt-rest-api-","domain":"borderless","class_name":"borderless_cpt_rest_api","context":"normal","priority":"default","cpt":"borderless_cpt","fields":[{"type":"select","label":"Show in Rest","default":"1","options":"1 : No\r\n2 : Yes","id":"borderless-cpt-rest-api-show-in-rest"},{"type":"text","label":"Rest Base","id":"borderless-cpt-rest-api-rest-base"},{"type":"text","label":"Rest Controller Class","default":"WP_REST_Posts_Controller","id":"borderless-cpt-rest-api-rest-controller-class"}]}'; |
| 1297 |
|
| 1298 |
public function __construct() { |
| 1299 |
$this->config = json_decode( $this->config, true ); |
| 1300 |
$this->process_cpts(); |
| 1301 |
add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] ); |
| 1302 |
add_action( 'save_post', [ $this, 'save_post' ] ); |
| 1303 |
} |
| 1304 |
|
| 1305 |
public function process_cpts() { |
| 1306 |
if ( !empty( $this->config['cpt'] ) ) { |
| 1307 |
if ( empty( $this->config['post-type'] ) ) { |
| 1308 |
$this->config['post-type'] = []; |
| 1309 |
} |
| 1310 |
$parts = explode( ',', $this->config['cpt'] ); |
| 1311 |
$parts = array_map( 'trim', $parts ); |
| 1312 |
$this->config['post-type'] = array_merge( $this->config['post-type'], $parts ); |
| 1313 |
} |
| 1314 |
} |
| 1315 |
|
| 1316 |
public function add_meta_boxes() { |
| 1317 |
foreach ( $this->config['post-type'] as $screen ) { |
| 1318 |
add_meta_box( |
| 1319 |
sanitize_title( $this->config['title'] ), |
| 1320 |
$this->config['title'], |
| 1321 |
[ $this, 'add_meta_box_callback' ], |
| 1322 |
$screen, |
| 1323 |
$this->config['context'], |
| 1324 |
$this->config['priority'] |
| 1325 |
); |
| 1326 |
} |
| 1327 |
} |
| 1328 |
|
| 1329 |
public function save_post( $post_id ) { |
| 1330 |
foreach ( $this->config['fields'] as $field ) { |
| 1331 |
switch ( $field['type'] ) { |
| 1332 |
default: |
| 1333 |
if ( isset( $_POST[ $field['id'] ] ) ) { |
| 1334 |
$sanitized = sanitize_text_field( htmlspecialchars( $_POST[ $field['id'] ] ) ); |
| 1335 |
update_post_meta( $post_id, $field['id'], $sanitized ); |
| 1336 |
} |
| 1337 |
} |
| 1338 |
} |
| 1339 |
} |
| 1340 |
|
| 1341 |
public function add_meta_box_callback() { |
| 1342 |
$this->fields_table(); |
| 1343 |
} |
| 1344 |
|
| 1345 |
private function fields_table() { |
| 1346 |
?><table class="form-table" role="presentation"> |
| 1347 |
<tbody><?php |
| 1348 |
foreach ( $this->config['fields'] as $field ) { |
| 1349 |
?><tr> |
| 1350 |
<th scope="row"><?php $this->label( $field ); ?></th> |
| 1351 |
<td><?php $this->field( $field ); ?></td> |
| 1352 |
</tr><?php |
| 1353 |
} |
| 1354 |
?></tbody> |
| 1355 |
</table><?php |
| 1356 |
} |
| 1357 |
|
| 1358 |
private function label( $field ) { |
| 1359 |
switch ( $field['type'] ) { |
| 1360 |
default: |
| 1361 |
printf( |
| 1362 |
'<label class="" for="%s">%s</label>', |
| 1363 |
$field['id'], $field['label'] |
| 1364 |
); |
| 1365 |
} |
| 1366 |
} |
| 1367 |
|
| 1368 |
private function field( $field ) { |
| 1369 |
switch ( $field['type'] ) { |
| 1370 |
case 'select': |
| 1371 |
$this->select( $field ); |
| 1372 |
break; |
| 1373 |
default: |
| 1374 |
$this->input( $field ); |
| 1375 |
} |
| 1376 |
} |
| 1377 |
|
| 1378 |
private function input( $field ) { |
| 1379 |
printf( |
| 1380 |
'<input class="regular-text %s" id="%s" name="%s" %s type="%s" value="%s">', |
| 1381 |
isset( $field['class'] ) ? $field['class'] : '', |
| 1382 |
$field['id'], $field['id'], |
| 1383 |
isset( $field['pattern'] ) ? "pattern='{$field['pattern']}'" : '', |
| 1384 |
$field['type'], |
| 1385 |
$this->value( $field ) |
| 1386 |
); |
| 1387 |
} |
| 1388 |
|
| 1389 |
private function select( $field ) { |
| 1390 |
printf( |
| 1391 |
'<select id="%s" name="%s">%s</select>', |
| 1392 |
$field['id'], $field['id'], |
| 1393 |
$this->select_options( $field ) |
| 1394 |
); |
| 1395 |
} |
| 1396 |
|
| 1397 |
private function select_selected( $field, $current ) { |
| 1398 |
$value = $this->value( $field ); |
| 1399 |
if ( $value === $current ) { |
| 1400 |
return 'selected'; |
| 1401 |
} |
| 1402 |
return ''; |
| 1403 |
} |
| 1404 |
|
| 1405 |
private function select_options( $field ) { |
| 1406 |
$output = []; |
| 1407 |
$options = explode( "\r\n", $field['options'] ); |
| 1408 |
$i = 0; |
| 1409 |
foreach ( $options as $option ) { |
| 1410 |
$pair = explode( ':', $option ); |
| 1411 |
$pair = array_map( 'trim', $pair ); |
| 1412 |
$output[] = sprintf( |
| 1413 |
'<option %s value="%s"> %s</option>', |
| 1414 |
$this->select_selected( $field, $pair[0] ), |
| 1415 |
$pair[0], $pair[1] |
| 1416 |
); |
| 1417 |
$i++; |
| 1418 |
} |
| 1419 |
return implode( '<br>', $output ); |
| 1420 |
} |
| 1421 |
|
| 1422 |
private function value( $field ) { |
| 1423 |
global $post; |
| 1424 |
if ( metadata_exists( 'post', $post->ID, $field['id'] ) ) { |
| 1425 |
$value = get_post_meta( $post->ID, $field['id'], true ); |
| 1426 |
} else if ( isset( $field['default'] ) ) { |
| 1427 |
$value = $field['default']; |
| 1428 |
} else { |
| 1429 |
return ''; |
| 1430 |
} |
| 1431 |
return str_replace( '\u0027', "'", $value ); |
| 1432 |
} |
| 1433 |
|
| 1434 |
} |
| 1435 |
new borderless_cpt_rest_api; |
| 1436 |
|
| 1437 |
add_action('init', 'borderless_cpt_init'); |
| 1438 |
?> |