module-post-type-features.php
3 months ago
module-post-type-fields.php
3 years ago
module-post-type-upgrades.php
3 years ago
module-post-type.php
3 months ago
module-post-type.php
426 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_module_post_type')): |
| 8 | |
| 9 | class acfe_module_post_type extends acfe_module{ |
| 10 | |
| 11 | /** |
| 12 | * initialize |
| 13 | */ |
| 14 | function initialize(){ |
| 15 | |
| 16 | $this->name = 'post_type'; |
| 17 | $this->plural = 'post_types'; |
| 18 | $this->setting = 'modules/post_types'; |
| 19 | $this->settings = 'modules.post_types'; |
| 20 | $this->view = 'edit.php?post_type=%s'; |
| 21 | $this->register = 'init'; |
| 22 | |
| 23 | $this->post_type = 'acfe-dpt'; |
| 24 | $this->args = array( |
| 25 | 'label' => __('Post Types', 'acfe'), |
| 26 | 'show_in_menu' => 'tools.php', |
| 27 | 'labels' => array( |
| 28 | 'name' => __('Post Types', 'acfe'), |
| 29 | 'singular_name' => __('Post Type', 'acfe'), |
| 30 | 'menu_name' => __('Post Types', 'acfe'), |
| 31 | 'edit_item' => __('Edit Post Type', 'acfe'), |
| 32 | 'add_new_item' => __('New Post Type', 'acfe'), |
| 33 | 'enter_title' => __('Post Type Label', 'acfe'), |
| 34 | ), |
| 35 | ); |
| 36 | |
| 37 | $this->messages = array( |
| 38 | 'export_title' => __('Export Post Types', 'acfe'), |
| 39 | 'export_description' => __('Export Post Types', 'acfe'), |
| 40 | 'export_select' => __('Select Post Types', 'acfe'), |
| 41 | 'export_not_found' => __('No post type available.', 'acfe'), |
| 42 | 'export_not_selected' => __('No post types selected', 'acfe'), |
| 43 | 'export_success_single' => __('1 post type exported', 'acfe'), |
| 44 | 'export_success_multiple' => __('%s post types exported', 'acfe'), |
| 45 | 'export_instructions' => sprintf(__('It is recommended to include this code within the <code>init</code> hook (<a href="%s" target="blank">see documentation</a>).', 'acfe'), esc_url('https://developer.wordpress.org/reference/functions/register_post_type/')), |
| 46 | 'import_title' => __('Import Post Types', 'acfe'), |
| 47 | 'import_description' => __('Import Post Types', 'acfe'), |
| 48 | 'import_success_single' => __('1 post type imported', 'acfe'), |
| 49 | 'import_success_multiple' => __('%s post types imported', 'acfe'), |
| 50 | ); |
| 51 | |
| 52 | $this->export_files = array( |
| 53 | 'single' => 'post-type', |
| 54 | 'multiple' => 'post-types', |
| 55 | ); |
| 56 | |
| 57 | $this->validate = array('name'); |
| 58 | |
| 59 | $this->columns = array( |
| 60 | 'acfe-name' => __('Name', 'acfe'), |
| 61 | 'acfe-taxonomies' => __('Taxonomies', 'acfe'), |
| 62 | 'acfe-posts' => __('Posts', 'acfe'), |
| 63 | ); |
| 64 | |
| 65 | $this->item = array( |
| 66 | 'name' => '', |
| 67 | 'label' => '', |
| 68 | 'active' => true, |
| 69 | 'description' => '', |
| 70 | 'hierarchical' => false, |
| 71 | 'supports' => array('title', 'editor'), |
| 72 | 'taxonomies' => array(), |
| 73 | 'public' => true, |
| 74 | 'exclude_from_search' => false, |
| 75 | 'publicly_queryable' => true, |
| 76 | 'can_export' => true, |
| 77 | 'delete_with_user' => null, |
| 78 | 'labels' => array(), |
| 79 | 'menu_position' => null, |
| 80 | 'menu_icon' => 'dashicons-admin-post', |
| 81 | 'show_ui' => true, |
| 82 | 'show_in_menu' => true, |
| 83 | 'show_in_nav_menus' => true, |
| 84 | 'show_in_admin_bar' => true, |
| 85 | 'has_archive' => true, |
| 86 | 'rewrite' => true, |
| 87 | 'capability_type' => 'post', |
| 88 | 'capabilities' => array(), |
| 89 | 'map_meta_cap' => null, |
| 90 | 'show_in_rest' => false, |
| 91 | 'rest_base' => null, |
| 92 | 'rest_controller_class' => 'WP_REST_Posts_Controller', |
| 93 | 'acfe_archive_template' => null, |
| 94 | 'acfe_archive_ppp' => 10, |
| 95 | 'acfe_archive_orderby' => 'date', |
| 96 | 'acfe_archive_order' => 'DESC', |
| 97 | 'acfe_archive_meta_key' => '', |
| 98 | 'acfe_archive_meta_type'=> '', |
| 99 | 'acfe_single_template' => null, |
| 100 | 'acfe_admin_archive' => false, |
| 101 | 'acfe_admin_ppp' => 10, |
| 102 | 'acfe_admin_orderby' => 'date', |
| 103 | 'acfe_admin_order' => 'DESC', |
| 104 | 'acfe_admin_meta_key' => '', |
| 105 | 'acfe_admin_meta_type' => '', |
| 106 | ); |
| 107 | |
| 108 | $this->l10n = array('label', 'description', 'labels'); |
| 109 | |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * register_item |
| 115 | * |
| 116 | * acfe/module/register_item |
| 117 | * |
| 118 | * @param $item |
| 119 | */ |
| 120 | function register_item($item){ |
| 121 | |
| 122 | // validate |
| 123 | if(!empty($item['name']) && !post_type_exists($item['name'])){ |
| 124 | register_post_type($item['name'], $item); |
| 125 | } |
| 126 | |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /** |
| 131 | * load_post |
| 132 | * |
| 133 | * acfe/module/load_post |
| 134 | */ |
| 135 | function load_post(){ |
| 136 | flush_rewrite_rules(false); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | /** |
| 141 | * imported_item |
| 142 | * |
| 143 | * acfe/module/imported_item |
| 144 | * |
| 145 | * @param $item |
| 146 | */ |
| 147 | function imported_item($item){ |
| 148 | flush_rewrite_rules(false); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /** |
| 153 | * trashed_item |
| 154 | * |
| 155 | * acfe/module/trashed_item |
| 156 | * |
| 157 | * @param $id |
| 158 | */ |
| 159 | function trashed_item($id){ |
| 160 | flush_rewrite_rules(false); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /** |
| 165 | * untrashed_item |
| 166 | * |
| 167 | * acfe/module/untrashed_item |
| 168 | * |
| 169 | * @param $id |
| 170 | */ |
| 171 | function untrashed_item($id){ |
| 172 | flush_rewrite_rules(false); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /** |
| 177 | * validate_name |
| 178 | * |
| 179 | * @param $value |
| 180 | * @param $item |
| 181 | * |
| 182 | * @return false|string |
| 183 | */ |
| 184 | function validate_name($value, $item){ |
| 185 | |
| 186 | // editing current post type |
| 187 | if($item['name'] === $value){ |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | // check sibiling post types (could be disabled) |
| 192 | $sibiling_item = $this->get_item($value); |
| 193 | |
| 194 | if($sibiling_item && $sibiling_item['ID'] !== $item['ID']){ |
| 195 | return __('This post type already exists', 'acfe'); |
| 196 | } |
| 197 | |
| 198 | // reserved wp post types |
| 199 | // see: https://codex.wordpress.org/Function_Reference/register_post_type#Reserved_Post_Types |
| 200 | $exclude = array('post', 'posts', 'page', 'attachment', 'revision', 'nav_menu_item', 'custom_css', 'customize_changeset', 'oembed_cache', 'user_request', 'wp_block', 'action', 'author', 'order', 'theme'); |
| 201 | $exclude = array_merge($exclude, acfe_get_setting('reserved_post_types', array())); |
| 202 | |
| 203 | // check if reserved name |
| 204 | if(in_array($value, $exclude)){ |
| 205 | return __('This post type is reserved', 'acfe'); |
| 206 | } |
| 207 | |
| 208 | // check existing post types |
| 209 | global $wp_post_types; |
| 210 | |
| 211 | foreach((array) $wp_post_types as $post_type){ |
| 212 | |
| 213 | // post type already exists |
| 214 | if($value === $post_type->name){ |
| 215 | return __('This post type already exists', 'acfe'); |
| 216 | } |
| 217 | |
| 218 | } |
| 219 | |
| 220 | return true; |
| 221 | |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /** |
| 226 | * prepare_load_item |
| 227 | * |
| 228 | * acfe/module/prepare_load_item |
| 229 | * |
| 230 | * @param $item |
| 231 | * |
| 232 | * @return mixed |
| 233 | */ |
| 234 | function prepare_load_item($item){ |
| 235 | |
| 236 | // menu: show in menu |
| 237 | if($item['show_in_menu'] && is_string($item['show_in_menu'])){ |
| 238 | |
| 239 | $item['show_in_menu_text'] = $item['show_in_menu']; |
| 240 | $item['show_in_menu'] = true; |
| 241 | |
| 242 | } |
| 243 | |
| 244 | // archive: has archive |
| 245 | if($item['has_archive'] && is_string($item['has_archive'])){ |
| 246 | |
| 247 | $item['has_archive_slug'] = $item['has_archive']; |
| 248 | $item['has_archive'] = true; |
| 249 | |
| 250 | } |
| 251 | |
| 252 | // single: rewrite |
| 253 | if($item['rewrite'] && is_array($item['rewrite'])){ |
| 254 | |
| 255 | $item['rewrite_args'] = $item['rewrite']; |
| 256 | $item['rewrite'] = true; |
| 257 | $item['rewrite_args_select'] = true; |
| 258 | |
| 259 | } |
| 260 | |
| 261 | return $item; |
| 262 | |
| 263 | } |
| 264 | |
| 265 | |
| 266 | /** |
| 267 | * prepare_save_item |
| 268 | * |
| 269 | * acfe/module/prepare_save_item |
| 270 | * |
| 271 | * @param $item |
| 272 | * |
| 273 | * @return mixed |
| 274 | */ |
| 275 | function prepare_save_item($item){ |
| 276 | |
| 277 | // general: taxonomies |
| 278 | $item['taxonomies'] = acfe_as_array($item['taxonomies']); |
| 279 | |
| 280 | // general: supports |
| 281 | if(empty($item['supports'])){ |
| 282 | $item['supports'] = false; |
| 283 | } |
| 284 | |
| 285 | // menu: menu position |
| 286 | if(!acf_is_empty($item['menu_position'])){ |
| 287 | $item['menu_position'] = (int) $item['menu_position']; |
| 288 | } |
| 289 | |
| 290 | // menu: show in menu |
| 291 | if($item['show_in_menu'] && !empty($item['show_in_menu_text'])){ |
| 292 | $item['show_in_menu'] = $item['show_in_menu_text']; |
| 293 | } |
| 294 | |
| 295 | // archive: has archive |
| 296 | if($item['has_archive'] && $item['has_archive_slug']){ |
| 297 | $item['has_archive'] = $item['has_archive_slug']; |
| 298 | } |
| 299 | |
| 300 | // single: rewrite |
| 301 | if($item['rewrite'] && $item['rewrite_args_select']){ |
| 302 | $item['rewrite'] = $item['rewrite_args']; |
| 303 | } |
| 304 | |
| 305 | if(is_array($item['capability_type']) && count($item['capability_type']) === 1){ |
| 306 | $item['capability_type'] = current($item['capability_type']); |
| 307 | } |
| 308 | |
| 309 | // return |
| 310 | return $item; |
| 311 | |
| 312 | } |
| 313 | |
| 314 | |
| 315 | /** |
| 316 | * edit_column_acfe_name |
| 317 | * |
| 318 | * @param $item |
| 319 | */ |
| 320 | function edit_column_acfe_name($item){ |
| 321 | echo '<code style="font-size: 12px;">' . $item['name'] . '</code>'; |
| 322 | } |
| 323 | |
| 324 | |
| 325 | /** |
| 326 | * edit_column_acfe_taxonomies |
| 327 | * |
| 328 | * @param $item |
| 329 | */ |
| 330 | function edit_column_acfe_taxonomies($item){ |
| 331 | |
| 332 | $text = '—'; |
| 333 | |
| 334 | if(empty($item['taxonomies'])){ |
| 335 | echo $text; |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | $taxonomies = array(); |
| 340 | |
| 341 | foreach($item['taxonomies'] as $taxonomy){ |
| 342 | if(taxonomy_exists($taxonomy)){ |
| 343 | $taxonomies[] = $taxonomy; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | if($taxonomies){ |
| 348 | |
| 349 | $labels = acf_get_taxonomy_labels($taxonomies); |
| 350 | |
| 351 | if(!empty($labels)){ |
| 352 | $text = implode(', ', $labels); |
| 353 | } |
| 354 | |
| 355 | } |
| 356 | |
| 357 | echo $text; |
| 358 | |
| 359 | } |
| 360 | |
| 361 | |
| 362 | /** |
| 363 | * edit_column_acfe_posts |
| 364 | * |
| 365 | * @param $item |
| 366 | */ |
| 367 | function edit_column_acfe_posts($item){ |
| 368 | |
| 369 | // vars |
| 370 | $text = '—'; |
| 371 | |
| 372 | if(!post_type_exists($item['name'])){ |
| 373 | echo $text; |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | $count = wp_count_posts($item['name']); |
| 378 | |
| 379 | if(!empty($count) && isset($count->publish)){ |
| 380 | |
| 381 | $count_publish = $count->publish; |
| 382 | $text = '<a href="' . admin_url('edit.php?post_type=' . $item['name']) . '">' . $count_publish . '</a>'; |
| 383 | |
| 384 | } |
| 385 | |
| 386 | echo $text; |
| 387 | |
| 388 | } |
| 389 | |
| 390 | |
| 391 | /** |
| 392 | * export_code |
| 393 | * |
| 394 | * @param $return |
| 395 | * @param $code |
| 396 | * @param $args |
| 397 | * |
| 398 | * @return string |
| 399 | */ |
| 400 | function export_code($code, $args){ |
| 401 | return "register_post_type('{$args['name']}', {$code});"; |
| 402 | } |
| 403 | |
| 404 | |
| 405 | /** |
| 406 | * export_local_code |
| 407 | * |
| 408 | * @param $return |
| 409 | * @param $code |
| 410 | * @param $args |
| 411 | * |
| 412 | * @return string |
| 413 | */ |
| 414 | function export_local_code($code, $args){ |
| 415 | return "acfe_register_post_type({$code});"; |
| 416 | } |
| 417 | |
| 418 | } |
| 419 | |
| 420 | acfe_register_module('acfe_module_post_type'); |
| 421 | |
| 422 | endif; |
| 423 | |
| 424 | function acfe_register_post_type($item){ |
| 425 | acfe_get_module('post_type')->add_local_item($item); |
| 426 | } |