7
7
8
8
namespace J7 \WpReactPlugin \Admin ;
9
9
10
- use Micropackage \Singleton \Singleton ;
11
10
use J7 \WpReactPlugin \Utils \Base ;
12
11
use J7 \WpReactPlugin \Plugin ;
13
12
14
13
/**
15
14
* Class CPT
16
15
*/
17
- final class CPT extends Singleton {
16
+ final class CPT {
17
+ use \J7 \WpUtils \Traits \SingletonTrait;
18
18
19
19
/**
20
20
* Post metas
21
21
*
22
22
* @var array
23
23
*/
24
- public $ post_metas = array ();
24
+ public $ post_meta_array = array ();
25
25
/**
26
26
* Rewrite
27
27
*
@@ -31,16 +31,22 @@ final class CPT extends Singleton {
31
31
32
32
/**
33
33
* Constructor
34
- *
35
- * @param array $args Arguments.
36
34
*/
37
- public function __construct ( $ args ) {
38
- $ this ->post_metas = $ args ['post_metas ' ];
39
- $ this ->rewrite = $ args ['rewrite ' ] ?? array ();
35
+ public function __construct () {
36
+ $ args = array (
37
+ 'post_meta_array ' => array ( 'meta ' , 'settings ' ),
38
+ 'rewrite ' => array (
39
+ 'template_path ' => 'test.php ' ,
40
+ 'slug ' => 'test ' ,
41
+ 'var ' => Plugin::$ snake . '_test ' ,
42
+ ),
43
+ );
44
+ $ this ->post_meta_array = $ args ['post_meta_array ' ];
45
+ $ this ->rewrite = $ args ['rewrite ' ] ?? array ();
40
46
41
47
\add_action ( 'init ' , array ( $ this , 'init ' ) );
42
48
43
- if ( ! empty ( $ args ['post_metas ' ] ) ) {
49
+ if ( ! empty ( $ args ['post_meta_array ' ] ) ) {
44
50
\add_action ( 'rest_api_init ' , array ( $ this , 'add_post_meta ' ) );
45
51
}
46
52
@@ -140,10 +146,10 @@ public static function register_cpt(): void {
140
146
* Register meta fields for post type to show in rest api
141
147
*/
142
148
public function add_post_meta (): void {
143
- foreach ( $ this ->post_metas as $ meta_key ) {
149
+ foreach ( $ this ->post_meta_array as $ meta_key ) {
144
150
\register_meta (
145
151
'post ' ,
146
- Plugin::SNAKE . '_ ' . $ meta_key ,
152
+ Plugin::$ snake . '_ ' . $ meta_key ,
147
153
array (
148
154
'type ' => 'string ' ,
149
155
'show_in_rest ' => true ,
@@ -168,9 +174,9 @@ public function init_metabox(): void {
168
174
* @param string $post_type Post type.
169
175
*/
170
176
public function add_metabox ( string $ post_type ): void {
171
- if ( in_array ( $ post_type , array ( Plugin::KEBAB ) ) ) {
177
+ if ( in_array ( $ post_type , array ( Plugin::$ kebab ) ) ) {
172
178
\add_meta_box (
173
- Plugin::KEBAB . '-metabox ' ,
179
+ Plugin::$ kebab . '-metabox ' ,
174
180
__ ( 'My App ' , 'wp_react_plugin ' ),
175
181
array ( $ this , 'render_meta_box ' ),
176
182
$ post_type ,
@@ -254,15 +260,15 @@ public function save_metabox( $post_id, $post ) { // phpcs:ignore
254
260
/* OK, it's safe for us to save the data now. */
255
261
256
262
// Sanitize the user input.
257
- $ meta_data = \sanitize_text_field ( $ _POST [ Plugin::SNAKE . '_meta ' ] );
263
+ $ meta_data = \sanitize_text_field ( $ _POST [ Plugin::$ snake . '_meta ' ] );
258
264
259
265
// Update the meta field.
260
- \update_post_meta ( $ post_id , Plugin::SNAKE . '_meta ' , $ meta_data );
266
+ \update_post_meta ( $ post_id , Plugin::$ snake . '_meta ' , $ meta_data );
261
267
}
262
268
263
269
/**
264
270
* Load custom template
265
- * Set {Plugin::KEBAB }/{slug}/report php template
271
+ * Set {Plugin::$kebab }/{slug}/report php template
266
272
*
267
273
* @param string $template Template.
268
274
*/
@@ -278,13 +284,4 @@ public function load_custom_template( $template ) {
278
284
}
279
285
}
280
286
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
- );
287
+ CPT ::instance ();
0 commit comments