From 50778fd65e63d9a92036d24bc394e4fa582d9055 Mon Sep 17 00:00:00 2001 From: davidsherlock <43mZGKl$bjD@KfMO%ggj#V5Y5dgrF%ps> Date: Mon, 18 Mar 2024 16:00:23 +0300 Subject: [PATCH] Various improvements --- composer.json | 2 +- src/Helper.php | 5 +++-- src/Post_Type.php | 16 +++++++++++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 983bdbf..598d108 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ }, "autoload": { "psr-4": { - "ArrayPress\\WP\\CPT_Inline_List_Table": "src/" + "ArrayPress\\WP\\CPT_Inline_List_Table\\": "src/" }, "files": [ "src/Functions.php", diff --git a/src/Helper.php b/src/Helper.php index 13a7ab0..e8db098 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -79,8 +79,9 @@ function register_inline_table( string $post_type, array $column_callbacks, stri * @param string $slug The slug for the post type, used in URLs and query vars. * @param array $additional_supports An array of additional features that the post type supports. Default features include 'title' and 'page-attributes'. * @param bool $show_in_rest Whether to expose this post type in the WordPress REST API. Enables use of the Gutenberg editor and REST API queries. + * @param array $args An associative array of custom arguments to override or extend the default post type registration settings. */ - function register_inline_table_post_type( string $post_type, string $singular_name, string $plural_name, string $slug, array $additional_supports = [], bool $show_in_rest = true ) { - new Post_Type( $post_type, $singular_name, $plural_name, $slug, $additional_supports, $show_in_rest ); + function register_inline_table_post_type( string $post_type, string $singular_name, string $plural_name, string $slug, array $additional_supports = [], bool $show_in_rest = true, array $args = [] ) { + new Post_Type( $post_type, $singular_name, $plural_name, $slug, $additional_supports, $show_in_rest, $args ); } } \ No newline at end of file diff --git a/src/Post_Type.php b/src/Post_Type.php index bf87e9f..5e71d1d 100644 --- a/src/Post_Type.php +++ b/src/Post_Type.php @@ -22,7 +22,7 @@ namespace ArrayPress\WP\CPT_Inline_List_Table; /** - * Check if the class `Register_Post_Type` is defined, and if not, define it. + * Check if the class `\\Post_Type` is defined, and if not, define it. */ if ( ! class_exists( __NAMESPACE__ . '\\Post_Type' ) ) : @@ -58,6 +58,11 @@ class Post_Type { */ protected bool $show_in_rest = false; + /** + * @var array Custom arguments to override the default post type arguments. + */ + protected array $args; + /** * Constructor for the custom post type registration class. * This constructor initializes the post type with provided settings and automatically registers it with WordPress during the 'init' action. @@ -68,14 +73,16 @@ class Post_Type { * @param string $slug The slug for the post type, used in URLs and query vars. * @param array $additional_supports An array of additional features that the post type supports. Default features include 'title' and 'page-attributes'. * @param bool $show_in_rest Whether to expose this post type in the WordPress REST API. Enables use of the Gutenberg editor and REST API queries. + * @param array $args An associative array of custom arguments to override or extend the default post type registration settings. */ - public function __construct( string $post_type, string $singular_name, string $plural_name, string $slug, array $additional_supports = [], bool $show_in_rest = true ) { + public function __construct( string $post_type, string $singular_name, string $plural_name, string $slug, array $additional_supports = [], bool $show_in_rest = true, array $args = [] ) { $this->post_type = $post_type; $this->singular_name = $singular_name; $this->plural_name = $plural_name; $this->slug = $slug; $this->additional_supports = $additional_supports; $this->show_in_rest = $show_in_rest; + $this->args = $args; // Store the custom arguments add_action( 'init', [ $this, 'register' ] ); } @@ -87,7 +94,7 @@ public function register(): void { $labels = $this->generate_labels(); $supports = array_merge( [ 'title', 'page-attributes' ], $this->additional_supports ); - $args = [ + $default_args = [ 'labels' => $labels, 'public' => false, 'publicly_queryable' => false, @@ -102,6 +109,9 @@ public function register(): void { ]; + // Merge the default args with the custom args, allowing for overrides + $args = array_merge( $default_args, $this->args ); + register_post_type( $this->post_type, $args ); }