77
88namespace J7 \WpRefinePlugin \Admin ;
99
10- use Micropackage \Singleton \Singleton ;
1110use J7 \WpRefinePlugin \Utils \Base ;
1211use J7 \WpRefinePlugin \Plugin ;
1312
1413/**
1514 * Class CPT
1615 */
17- final class CPT extends Singleton {
16+ final class CPT {
17+ use \J7 \WpUtils \Traits \SingletonTrait;
1818
1919 /**
2020 * Post metas
2121 *
2222 * @var array
2323 */
24- public $ post_metas = array () ;
24+ public $ post_meta_array = [] ;
2525 /**
2626 * Rewrite
2727 *
2828 * @var array
2929 */
30- public $ rewrite = array () ;
30+ public $ rewrite = [] ;
3131
3232 /**
3333 * Constructor
3434 *
3535 * @param array $args Arguments.
3636 */
37- public function __construct ( $ args ) {
38- $ this ->post_metas = $ args ['post_metas ' ];
39- $ this ->rewrite = $ args ['rewrite ' ] ?? array ();
40-
41- \add_action ( 'init ' , array ( $ this , 'init ' ) );
42-
43- if ( ! empty ( $ args ['post_metas ' ] ) ) {
44- \add_action ( 'rest_api_init ' , array ( $ this , 'add_post_meta ' ) );
37+ public function __construct () {
38+ $ args = [
39+ 'post_meta_array ' => [ 'meta ' , 'settings ' ],
40+ 'rewrite ' => [
41+ 'template_path ' => 'test.php ' ,
42+ 'slug ' => 'test ' ,
43+ 'var ' => Plugin::$ snake . '_test ' ,
44+ ],
45+ ];
46+
47+ $ this ->post_meta_array = $ args ['post_meta_array ' ];
48+ $ this ->rewrite = $ args ['rewrite ' ] ?? [];
49+
50+ \add_action ( 'init ' , [ $ this , 'init ' ] );
51+
52+ if ( ! empty ( $ args ['post_meta_array ' ] ) ) {
53+ \add_action ( 'rest_api_init ' , [ $ this , 'add_post_meta ' ] );
4554 }
4655
47- \add_action ( 'load-post.php ' , array ( $ this , 'init_metabox ' ) );
48- \add_action ( 'load-post-new.php ' , array ( $ this , 'init_metabox ' ) );
56+ \add_action ( 'load-post.php ' , [ $ this , 'init_metabox ' ] );
57+ \add_action ( 'load-post-new.php ' , [ $ this , 'init_metabox ' ] );
4958
5059 if ( ! empty ( $ args ['rewrite ' ] ) ) {
51- \add_filter ( 'query_vars ' , array ( $ this , 'add_query_var ' ) );
52- \add_filter ( 'template_include ' , array ( $ this , 'load_custom_template ' ) , 99 );
60+ \add_filter ( 'query_vars ' , [ $ this , 'add_query_var ' ] );
61+ \add_filter ( 'template_include ' , [ $ this , 'load_custom_template ' ] , 99 );
5362 }
5463 }
5564
@@ -71,7 +80,7 @@ public function init(): void {
7180 */
7281 public static function register_cpt (): void {
7382
74- $ labels = array (
83+ $ labels = [
7584 'name ' => \esc_html__ ( 'my-refine-app ' , 'wp_refine_plugin ' ),
7685 'singular_name ' => \esc_html__ ( 'my-refine-app ' , 'wp_refine_plugin ' ),
7786 'add_new ' => \esc_html__ ( 'Add new ' , 'wp_refine_plugin ' ),
@@ -103,8 +112,8 @@ public static function register_cpt(): void {
103112 'item_reverted_to_draft ' => \esc_html__ ( 'my-refine-app reverted to draft ' , 'wp_refine_plugin ' ),
104113 'item_scheduled ' => \esc_html__ ( 'my-refine-app scheduled ' , 'wp_refine_plugin ' ),
105114 'item_updated ' => \esc_html__ ( 'my-refine-app updated ' , 'wp_refine_plugin ' ),
106- ) ;
107- $ args = array (
115+ ] ;
116+ $ args = [
108117 'label ' => \esc_html__ ( 'my-refine-app ' , 'wp_refine_plugin ' ),
109118 'labels ' => $ labels ,
110119 'description ' => '' ,
@@ -125,13 +134,13 @@ public static function register_cpt(): void {
125134 'menu_position ' => 6 ,
126135 'menu_icon ' => 'dashicons-store ' ,
127136 'capability_type ' => 'post ' ,
128- 'supports ' => array ( 'title ' , 'editor ' , 'thumbnail ' , 'custom-fields ' , 'author ' ) ,
129- 'taxonomies ' => array () ,
137+ 'supports ' => [ 'title ' , 'editor ' , 'thumbnail ' , 'custom-fields ' , 'author ' ] ,
138+ 'taxonomies ' => [] ,
130139 'rest_controller_class ' => 'WP_REST_Posts_Controller ' ,
131- 'rewrite ' => array (
140+ 'rewrite ' => [
132141 'with_front ' => true ,
133- ) ,
134- ) ;
142+ ] ,
143+ ] ;
135144
136145 \register_post_type ( 'my-refine-app ' , $ args );
137146 }
@@ -140,15 +149,15 @@ public static function register_cpt(): void {
140149 * Register meta fields for post type to show in rest api
141150 */
142151 public function add_post_meta (): void {
143- foreach ( $ this ->post_metas as $ meta_key ) {
152+ foreach ( $ this ->post_meta_array as $ meta_key ) {
144153 \register_meta (
145154 'post ' ,
146- Plugin::SNAKE . '_ ' . $ meta_key ,
147- array (
155+ Plugin::$ snake . '_ ' . $ meta_key ,
156+ [
148157 'type ' => 'string ' ,
149158 'show_in_rest ' => true ,
150159 'single ' => true ,
151- )
160+ ]
152161 );
153162 }
154163 }
@@ -157,9 +166,9 @@ public function add_post_meta(): void {
157166 * Meta box initialization.
158167 */
159168 public function init_metabox (): void {
160- \add_action ( 'add_meta_boxes ' , array ( $ this , 'add_metabox ' ) );
161- \add_action ( 'save_post ' , array ( $ this , 'save_metabox ' ) , 10 , 2 );
162- \add_filter ( 'rewrite_rules_array ' , array ( $ this , 'custom_post_type_rewrite_rules ' ) );
169+ \add_action ( 'add_meta_boxes ' , [ $ this , 'add_metabox ' ] );
170+ \add_action ( 'save_post ' , [ $ this , 'save_metabox ' ] , 10 , 2 );
171+ \add_filter ( 'rewrite_rules_array ' , [ $ this , 'custom_post_type_rewrite_rules ' ] );
163172 }
164173
165174 /**
@@ -168,11 +177,11 @@ public function init_metabox(): void {
168177 * @param string $post_type Post type.
169178 */
170179 public function add_metabox ( string $ post_type ): void {
171- if ( in_array ( $ post_type , array ( Plugin::KEBAB ) ) ) {
180+ if ( in_array ( $ post_type , [ Plugin::$ kebab ] ) ) {
172181 \add_meta_box (
173- Plugin::KEBAB . '-metabox ' ,
182+ Plugin::$ kebab . '-metabox ' ,
174183 __ ( 'My Refine App ' , 'wp_refine_plugin ' ),
175- array ( $ this , 'render_meta_box ' ) ,
184+ [ $ this , 'render_meta_box ' ] ,
176185 $ post_type ,
177186 'advanced ' ,
178187 'high '
@@ -185,7 +194,7 @@ public function add_metabox( string $post_type ): void {
185194 */
186195 public function render_meta_box (): void {
187196 // phpcs:ignore
188- echo '<div id=" ' . Base::APP2_SELECTOR . '" class="relative"></div> ' ;
197+ echo '<div id=" ' . substr ( Base::APP2_SELECTOR , 1 ) . '" class="relative"></div> ' ;
189198 }
190199
191200
@@ -254,15 +263,15 @@ public function save_metabox( $post_id, $post ) { // phpcs:ignore
254263 /* OK, it's safe for us to save the data now. */
255264
256265 // Sanitize the user input.
257- $ meta_data = \sanitize_text_field ( $ _POST [ Plugin::SNAKE . '_meta ' ] );
266+ $ meta_data = \sanitize_text_field ( $ _POST [ Plugin::$ snake . '_meta ' ] );
258267
259268 // Update the meta field.
260- \update_post_meta ( $ post_id , Plugin::SNAKE . '_meta ' , $ meta_data );
269+ \update_post_meta ( $ post_id , Plugin::$ snake . '_meta ' , $ meta_data );
261270 }
262271
263272 /**
264273 * Load custom template
265- * Set {Plugin::KEBAB }/{slug}/report php template
274+ * Set {Plugin::$kebab }/{slug}/report php template
266275 *
267276 * @param string $template Template.
268277 */
@@ -278,13 +287,4 @@ public function load_custom_template( $template ) {
278287 }
279288}
280289
281- CPT ::get (
282- array (
283- 'post_metas ' => array ( 'meta ' , 'settings ' ),
284- 'rewrite ' => array (
285- 'template_path ' => 'test.php ' ,
286- 'slug ' => 'test ' ,
287- 'var ' => Plugin::SNAKE . '_test ' ,
288- ),
289- )
290- );
290+ CPT ::instance ();
0 commit comments