1818final class CPT {
1919 use \J7 \WpUtils \Traits \SingletonTrait;
2020
21- /**
22- * Post metas
23- *
24- * @var array
25- */
26- public $ post_meta_array = [];
27- /**
28- * Rewrite
29- *
30- * @var array
31- */
32- public $ rewrite = [];
33-
3421 /**
3522 * Constructor
3623 */
3724 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- $ this ->post_meta_array = $ args ['post_meta_array ' ];
47- $ this ->rewrite = $ args ['rewrite ' ] ?? [];
48-
49- \add_action ( 'init ' , [ $ this , 'init ' ] );
50-
51- if ( ! empty ( $ args ['post_meta_array ' ] ) ) {
52- \add_action ( 'rest_api_init ' , [ $ this , 'add_post_meta ' ] );
53- }
54-
55- \add_action ( 'load-post.php ' , [ $ this , 'init_metabox ' ] );
56- \add_action ( 'load-post-new.php ' , [ $ this , 'init_metabox ' ] );
57-
58- if ( ! empty ( $ args ['rewrite ' ] ) ) {
59- \add_filter ( 'query_vars ' , [ $ this , 'add_query_var ' ] );
60- \add_filter ( 'template_include ' , [ $ this , 'load_custom_template ' ], 99 );
61- }
62- }
63-
64- /**
65- * Initialize
66- */
67- public function init (): void {
68- $ this ->register_cpt ();
69-
70- // add {$this->post_type}/{slug}/test rewrite rule
71- if ( ! empty ( $ this ->rewrite ) ) {
72- \add_rewrite_rule ( '^my-app/([^/]+)/ ' . $ this ->rewrite ['slug ' ] . '/?$ ' , 'index.php?post_type=my-app&name=$matches[1]& ' . $ this ->rewrite ['var ' ] . '=1 ' , 'top ' );
73- \flush_rewrite_rules ();
74- }
25+ \add_action ( 'init ' , [ __CLASS__ , 'register_cpt ' ] );
26+ \add_action ( 'load-post.php ' , [ __CLASS__ , 'init_metabox ' ] );
27+ \add_action ( 'load-post-new.php ' , [ __CLASS__ , 'init_metabox ' ] );
7528 }
7629
7730 /**
7831 * Register my-app custom post type
7932 */
80- public function register_cpt (): void {
33+ public static function register_cpt (): void {
8134
8235 $ labels = [
8336 'name ' => \esc_html__ ( 'my-app ' , 'wp_react_plugin ' ),
@@ -144,43 +97,25 @@ public function register_cpt(): void {
14497 \register_post_type ( 'my-app ' , $ args );
14598 }
14699
147- /**
148- * Register meta fields for post type to show in rest api
149- */
150- public function add_post_meta (): void {
151- foreach ( $ this ->post_meta_array as $ meta_key ) {
152- \register_meta (
153- 'post ' ,
154- Plugin::$ snake . '_ ' . $ meta_key ,
155- [
156- 'type ' => 'string ' ,
157- 'show_in_rest ' => true ,
158- 'single ' => true ,
159- ]
160- );
161- }
162- }
163100
164101 /**
165102 * Meta box initialization.
166103 */
167- public function init_metabox (): void {
168- \add_action ( 'add_meta_boxes ' , [ $ this , 'add_metabox ' ] );
169- \add_action ( 'save_post ' , [ $ this , 'save_metabox ' ], 10 , 2 );
170- \add_filter ( 'rewrite_rules_array ' , [ $ this , 'custom_post_type_rewrite_rules ' ] );
104+ public static function init_metabox (): void {
105+ \add_action ( 'add_meta_boxes ' , [ __CLASS__ , 'add_metabox ' ] );
171106 }
172107
173108 /**
174109 * Adds the meta box.
175110 *
176111 * @param string $post_type Post type.
177112 */
178- public function add_metabox ( string $ post_type ): void {
179- if ( in_array ( $ post_type , [ Plugin::$ kebab ] ) ) {
113+ public static function add_metabox ( string $ post_type ): void {
114+ if ( in_array ( $ post_type , [ Plugin::$ kebab ], true ) ) {
180115 \add_meta_box (
181116 Plugin::$ kebab . '-metabox ' ,
182117 __ ( 'My App ' , 'wp_react_plugin ' ),
183- [ $ this , 'render_meta_box ' ],
118+ [ __CLASS__ , 'render_meta_box ' ],
184119 $ post_type ,
185120 'advanced ' ,
186121 'high '
@@ -191,97 +126,7 @@ public function add_metabox( string $post_type ): void {
191126 /**
192127 * Render meta box.
193128 */
194- public function render_meta_box (): void {
195- // phpcs:ignore
129+ public static function render_meta_box (): void {
196130 echo '<div id="my_app_metabox"></div> ' ;
197131 }
198-
199-
200- /**
201- * Add query var
202- *
203- * @param array $vars Vars.
204- * @return array
205- */
206- public function add_query_var ( $ vars ) {
207- $ vars [] = $ this ->rewrite ['var ' ];
208- return $ vars ;
209- }
210-
211- /**
212- * Custom post type rewrite rules
213- *
214- * @param array $rules Rules.
215- * @return array
216- */
217- public function custom_post_type_rewrite_rules ( $ rules ) {
218- global $ wp_rewrite ;
219- $ wp_rewrite ->flush_rules ();
220- return $ rules ;
221- }
222-
223- /**
224- * Save the meta when the post is saved.
225- *
226- * @param int $post_id Post ID.
227- * @param WP_Post $post Post object.
228- */
229- public function save_metabox ( $ post_id , $ post ) { // phpcs:ignore
230- // phpcs:disable
231- /*
232- * We need to verify this came from the our screen and with proper authorization,
233- * because save_post can be triggered at other times.
234- */
235-
236- // Check if our nonce is set.
237- if ( ! isset ( $ _POST ['_wpnonce ' ] ) ) {
238- return $ post_id ;
239- }
240-
241- $ nonce = $ _POST ['_wpnonce ' ];
242-
243- /*
244- * If this is an autosave, our form has not been submitted,
245- * so we don't want to do anything.
246- */
247- if ( defined ( 'DOING_AUTOSAVE ' ) && DOING_AUTOSAVE ) {
248- return $ post_id ;
249- }
250-
251- $ post_type = \sanitize_text_field ( $ _POST ['post_type ' ] ?? '' );
252-
253- // Check the user's permissions.
254- if ( 'my-app ' !== $ post_type ) {
255- return $ post_id ;
256- }
257-
258- if ( ! \current_user_can ( 'edit_post ' , $ post_id ) ) {
259- return $ post_id ;
260- }
261-
262- /* OK, it's safe for us to save the data now. */
263-
264- // Sanitize the user input.
265- $ meta_data = \sanitize_text_field ( $ _POST [ Plugin::$ snake . '_meta ' ] );
266-
267- // Update the meta field.
268- \update_post_meta ( $ post_id , Plugin::$ snake . '_meta ' , $ meta_data );
269- }
270-
271- /**
272- * Load custom template
273- * Set {Plugin::$kebab}/{slug}/report php template
274- *
275- * @param string $template Template.
276- */
277- public function load_custom_template ( $ template ):string {
278- $ repor_template_path = Plugin::$ dir . '/inc/templates/ ' . $ this ->rewrite ['template_path ' ];
279-
280- if ( \get_query_var ( $ this ->rewrite ['var ' ] ) ) {
281- if ( file_exists ( $ repor_template_path ) ) {
282- return $ repor_template_path ;
283- }
284- }
285- return $ template ;
286- }
287132}
0 commit comments