Skip to content

Commit

Permalink
✨ updates and align custom post type and custom taxonomies
Browse files Browse the repository at this point in the history
  • Loading branch information
gfazioli committed Jul 10, 2024
1 parent f0fe5ba commit b3bf041
Show file tree
Hide file tree
Showing 2 changed files with 448 additions and 124 deletions.
218 changes: 157 additions & 61 deletions src/Foundation/WordPressCustomPostTypeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
abstract class WordPressCustomPostTypeServiceProvider extends ServiceProvider
{
/**
* Post type key, must not exceed 20 characters.
* Post type key. Must not exceed 20 characters and may only contain
* lowercase alphanumeric characters, dashes, and underscores. See sanitize_key().
*
* `register_post_type( $post_type, $args = array() )`
*
* @var string
*/
protected $id = '';

/**
* Name of the post type shown in the menu.
* Name of the post type shown in the menu. Usually plural.
*
* @var string
*/
Expand Down Expand Up @@ -59,19 +62,20 @@ abstract class WordPressCustomPostTypeServiceProvider extends ServiceProvider
protected $public = false;

/**
* Whether the post type is hierarchical (e.g. page). Defaults to false.
* Whether the post type is hierarchical (e.g. page).
* Defaults to false.
*
* @var bool
*/
protected $hierarchical = false;
protected $hierarchical;

/**
* Whether to exclude posts with this post type from front end search results.
* If not set, the opposite of public's current value is used.
*
* @var bool
*/
protected $excludeFromSearch = true;
protected $excludeFromSearch;

/**
* Whether queries can be performed on the front end for the post type as part of parse_request().
Expand All @@ -84,15 +88,15 @@ abstract class WordPressCustomPostTypeServiceProvider extends ServiceProvider
*
* @var bool
*/
protected $publiclyQueryable = false;
protected $publiclyQueryable;

/**
* Whether to generate a default UI for managing this post type in the admin.
* If not set, the default is inherited from public.
*
* @var bool
*/
protected $showUI = true;
protected $showUI;

/**
* Where to show the post type in the admin menu.
Expand All @@ -103,23 +107,79 @@ abstract class WordPressCustomPostTypeServiceProvider extends ServiceProvider
*
* @var bool
*/
protected $showInMenu = true;
protected $showInMenu;

/**
* Makes this post type available for selection in navigation menus.
* If not set, the default is inherited from public.
*
* @var bool
*/
protected $showInNavMenus = true;
protected $showInNavMenus;

/**
* Makes this post type available via the admin bar.
* If not set, the default is inherited from show_in_menu
* If not set, the default is inherited from showInMenu
*
* @var bool
*/
protected $showInAdminBar;

/**
* Whether to include the post type in the REST API.
* Set this to true for the post type to be available in the block editor.
*
* @var bool
*/
protected $showInAdminBar = true;
protected $showInRest;

/**
* To change the base url of REST API route.
* Default is the post type key.
*
* @var string
*/
protected $restBase;

/**
* To change the namespace of REST API route.
* Default is 'wp/v2'.
*
* @var string
*/
protected $restNamespace;

/**
* To change the controller class of REST API route.
* Default is 'WP_REST_Posts_Controller'.
*
* @var string
*/
protected $restControllerClass;

/**
* To change the controller class of REST API route for autosaves.
* Default is 'WP_REST_Autosaves_Controller'.
*
* @var string
*/
protected $autoSaveRestControllerClass;

/**
* To change the controller class of REST API route for revisions.
* Default is 'WP_REST_Revisions_Controller'.
*
* @var string
*/
protected $revisionsRestControllerClass;

/**
* A flag to direct the REST API controllers for autosave / revisions
* should be registered before/after the post type controller.
*
* @var string
*/
protected $lateRouteRegistration;

/**
* The position in the menu order the post type should appear.
Expand All @@ -128,7 +188,7 @@ abstract class WordPressCustomPostTypeServiceProvider extends ServiceProvider
*
* @var null
*/
protected $menuPosition = null;
protected $menuPosition;

/**
* The url to the icon to be used for this menu. Defaults to use the posts icon.
Expand All @@ -148,7 +208,7 @@ abstract class WordPressCustomPostTypeServiceProvider extends ServiceProvider
*
* @var string
*/
protected $capabilityType = 'post';
protected $capabilityType;

/**
* Array of capabilities for this post type.
Expand All @@ -164,7 +224,7 @@ abstract class WordPressCustomPostTypeServiceProvider extends ServiceProvider
*
* @var bool
*/
protected $mapMetaCap = false;
protected $mapMetaCap;

/**
* An alias for calling add_post_type_support() directly. Defaults to title and editor.
Expand All @@ -180,7 +240,7 @@ abstract class WordPressCustomPostTypeServiceProvider extends ServiceProvider
*
* @var null
*/
protected $registerMetaBoxCallback = null;
protected $registerMetaBoxCallback;

/**
* An array of taxonomy identifiers that will be registered for the post type.
Expand All @@ -197,7 +257,7 @@ abstract class WordPressCustomPostTypeServiceProvider extends ServiceProvider
*
* @var bool
*/
protected $hasArchive = false;
protected $hasArchive;

/**
* Triggers the handling of rewrites for this post type. Defaults to true, using $post_type as slug.
Expand All @@ -220,28 +280,28 @@ abstract class WordPressCustomPostTypeServiceProvider extends ServiceProvider
*
* @var string
*/
protected $slug = 'wp_kirk_slug';
protected $slug;

/**
* Should the permastruct be prepended with WP_Rewrite::$front. Defaults to true.
*
* @var bool
*/
protected $withFront = true;
protected $withFront;

/**
* Should a feed permastruct be built for this post type. Inherits default from has_archive.
*
* @var bool
*/
protected $feeds = false;
protected $feeds;

/**
* Should the permastruct provide for pagination. Defaults to true.
*
* @var bool
*/
protected $pages = true;
protected $pages;

/**
* Assign an endpoint mask.
Expand All @@ -259,26 +319,47 @@ abstract class WordPressCustomPostTypeServiceProvider extends ServiceProvider
*
* @var string
*/
protected $queryVar = 'wp_kirk_vars';
protected $queryVar;

/**
* Allows this post type to be exported. Defaults to true.
*
* @var bool
*/
protected $canExport = true;
protected $canExport;

/**
* Whether to delete posts of this type when deleting a user.
*
* If true, posts of this type belonging to the user will be moved to trash when then user is deleted.
* If false, posts of this type belonging to the user will *not* be trashed or deleted.
* If not set (the default), posts are trashed if post_type_supports('author'). Otherwise posts are not trashed or
* deleted.
* If not set (the default), posts are trashed if post_type_supports('author').
* Otherwise posts are not trashed or deleted.
*
* @var bool
*/
protected $deleteWithUser = false;
protected $deleteWithUser;

/**
* Array of blocks to use as the default initial state for an editor
* session. Each item should be an array containing block name and
* optional attributes. Default empty array.
*
* @var array
*/
protected $template;

/**
* Whether the block template should be locked if $template is set.
* - If set to 'all', the user is unable to insert new blocks,
* move existing blocks and delete blocks.
* - If set to 'insert', the user is able to move existing blocks
* but is unable to insert new blocks and delete blocks.
* Default false.
*
* @var string|bool
*/
protected $templateLock;

/**
* true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY!
Expand All @@ -300,7 +381,8 @@ public function register()
$this->boot();

// Register custom post type
register_post_type($this->id, $this->args());
register_post_type($this->id, $this->optionalArgs());

$this->initHooks();
}

Expand All @@ -313,35 +395,45 @@ public function boot()
// You may override this method
}

protected function args(): array
protected function optionalArgs(): array
{
return [
'labels' => $this->labels(),
'description' => $this->description,
'public' => $this->public,
'hierarchical' => $this->hierarchical,
'exclude_from_search' => $this->excludeFromSearch,
'publicly_queryable' => $this->publiclyQueryable,
'show_ui' => $this->showUI,
'show_in_menu' => $this->showInMenu,
'show_in_nav_menus' => $this->showInNavMenus,
'show_in_admin_bar' => $this->showInAdminBar,
'menu_position' => $this->menuPosition,
'menu_icon' => $this->menuIcon,
'capability_type' => $this->capabilityType,
'capabilities' => $this->capabilities,
'map_meta_cap' => $this->mapMetaCap,
'supports' => $this->supports(),
'register_meta_box_cb' => $this->registerMetaBoxCallback,
'taxonomies' => $this->taxonomies,
'has_archive' => $this->hasArchive,
'rewrite' => $this->rewrite(),
'query_var' => $this->queryVar,
'can_export' => $this->canExport,
'delete_with_user' => $this->deleteWithUser,
// '_builtin' => $this->_builtin,
// '_edit_link' => $this->_editLink,
$mapProperties = [
'label' => 'name',
'labels' => 'labels',
'description' => 'description',
'public' => 'public',
'hierarchical' => 'hierarchical',
'exclude_from_search' => 'excludeFromSearch',
'publicly_queryable' => 'publiclyQueryable',
'show_ui' => 'showUI',
'show_in_menu' => 'showInMenu',
'show_in_nav_menus' => 'showInNavMenus',
'show_in_admin_bar' => 'showInAdminBar',
'show_in_rest' => 'showInRest',
'rest_base' => 'restBase',
'rest_namespace' => 'restNamespace',
'rest_controller_class' => 'restControllerClass',
'autosave_rest_controller_class' => 'autoSaveRestControllerClass',
'revisions_rest_controller_class' => 'revisionsRestControllerClass',
'late_route_registration' => 'lateRouteRegistration',
'menu_position' => 'menuPosition',
'menu_icon' => 'menuIcon',
'capability_type' => 'capabilityType',
'capabilities' => 'capabilities',
'map_meta_cap' => 'mapMetaCap',
'supports' => 'supports',
'register_meta_box_cb' => 'registerMetaBoxCallback',
'taxonomies' => 'taxonomies',
'has_archive' => 'hasArchive',
'rewrite' => 'rewrite',
'query_var' => 'queryVar',
'can_export' => 'canExport',
'delete_with_user' => 'deleteWithUser',
'template' => 'template',
'template_lock' => 'templateLock',
];

return $this->mapPropertiesToArray($mapProperties);
}

/**
Expand All @@ -368,6 +460,7 @@ protected function labels(): array
'archive_title' => $this->name,
'parent_item_colon' => '',
];

if (empty($this->labels)) {
return $defaults;
}
Expand Down Expand Up @@ -416,16 +509,19 @@ protected function supports(): array
*/
protected function rewrite(): array
{
if (empty($this->rewrite)) {
return [
'slug' => $this->slug,
'with_front' => $this->withFront,
'pages' => $this->pages,
'ep_mask' => $this->epMask,
];
if (!empty($this->rewrite)) {
return $this->rewrite;
}

return $this->rewrite;
$mapProperties = [
'slug' => 'slug',
'with_front' => 'withFront',
'pages' => 'pages',
'ep_mask' => 'epMask',
];


return $this->mapPropertiesToArray($mapProperties);
}

protected function initHooks()
Expand Down
Loading

0 comments on commit b3bf041

Please sign in to comment.