Skip to content

Commit bdaf18b

Browse files
committed
feat: upgrade j7-dev/wp-utils and setting
1 parent 7e5050f commit bdaf18b

File tree

19 files changed

+692
-751
lines changed

19 files changed

+692
-751
lines changed

.eslintrc.json

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"prettier"
2828
],
2929
"rules": {
30+
"quote-props": "off",
3031
"@typescript-eslint/no-explicit-any": "warn",
3132
"@wordpress/no-unused-vars-before-return": "off",
3233
"semi": [
@@ -52,7 +53,7 @@
5253
"error",
5354
{
5455
"endOfLine": "auto",
55-
"useTabs": false,
56+
"useTabs": true,
5657
"tabWidth": 2,
5758
"prettier-multiline-arrays-set-threshold": 1
5859
}
@@ -69,10 +70,6 @@
6970
"error"
7071
],
7172
"camelcase": "off",
72-
"quote-props": [
73-
"error",
74-
"consistent"
75-
],
7673
"jsdoc/valid-types": "off",
7774
"@typescript-eslint/no-unused-vars": [
7875
"warn",
@@ -85,9 +82,9 @@
8582
"error",
8683
{
8784
"beforeBlockComment": true,
88-
"afterBlockComment": true,
85+
"afterBlockComment": false,
8986
"beforeLineComment": true,
90-
"afterLineComment": true,
87+
"afterLineComment": false,
9188
"allowBlockStart": true,
9289
"allowBlockEnd": true,
9390
"allowObjectStart": true,

.prettierrc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"singleQuote": true,
88
"tabWidth": 2,
99
"trailingComma": "all",
10-
"useTabs": false,
10+
"useTabs": true,
1111
"endOfLine": "auto",
1212
"multilineArraysWrapThreshold": 4,
1313
"plugins": [
@@ -16,7 +16,9 @@
1616
],
1717
"overrides": [
1818
{
19-
"files": ["*.json"],
19+
"files": [
20+
"*.json"
21+
],
2022
"options": {
2123
"singleQuote": false
2224
}

composer.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,23 @@
1919
],
2020
"config": {
2121
"allow-plugins": {
22-
"dealerdirect/phpcodesniffer-composer-installer": true
22+
"dealerdirect/phpcodesniffer-composer-installer": true,
23+
"phpstan/extension-installer": true
2324
}
2425
},
2526
"require": {
2627
"kucrut/vite-for-wp": "^0.8.0",
27-
"yahnis-elsts/plugin-update-checker": "^5.3",
28-
"j7-dev/tgm-plugin-activation-forked": "1.2.3",
29-
"micropackage/singleton": "^1.1"
28+
"j7-dev/wp-utils": "0.2.9"
3029
},
3130
"require-dev": {
3231
"squizlabs/php_codesniffer": "@stable",
3332
"wp-coding-standards/wpcs": "@stable",
34-
"dealerdirect/phpcodesniffer-composer-installer": "@stable"
33+
"dealerdirect/phpcodesniffer-composer-installer": "@stable",
34+
"phpcompatibility/php-compatibility": "@stable",
35+
"phpstan/phpstan": "^1.11",
36+
"php-stubs/woocommerce-stubs": "^9.0",
37+
"php-stubs/wordpress-stubs": "^6.5",
38+
"phpstan/extension-installer": "^1.4"
3539
},
3640
"scripts": {
3741
"lint": "phpcs"

inc/class/admin/class-cpt.php

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,58 @@
77

88
namespace J7\WpRefinePlugin\Admin;
99

10-
use Micropackage\Singleton\Singleton;
1110
use J7\WpRefinePlugin\Utils\Base;
1211
use 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();

inc/class/class-bootstrap.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
namespace J7\WpRefinePlugin;
99

10-
use Micropackage\Singleton\Singleton;
1110
use J7\WpRefinePlugin\Utils\Base;
1211
use Kucrut\Vite;
1312

1413
/**
1514
* Class Bootstrap
1615
*/
17-
final class Bootstrap extends Singleton {
16+
final class Bootstrap {
17+
use \J7\WpUtils\Traits\SingletonTrait;
1818

1919

2020
/**
@@ -25,8 +25,8 @@ public function __construct() {
2525
require_once __DIR__ . '/admin/index.php';
2626
require_once __DIR__ . '/front-end/index.php';
2727

28-
\add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_script' ), 99 );
29-
\add_action( 'wp_enqueue_scripts', array( $this, 'frontend_enqueue_script' ), 99 );
28+
\add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_script' ], 99 );
29+
\add_action( 'wp_enqueue_scripts', [ $this, 'frontend_enqueue_script' ], 99 );
3030
}
3131

3232
/**
@@ -63,44 +63,44 @@ public function enqueue_script(): void {
6363
Vite\enqueue_asset(
6464
Plugin::$dir . '/js/dist',
6565
'js/src/main.tsx',
66-
array(
67-
'handle' => Plugin::KEBAB,
66+
[
67+
'handle' => Plugin::$kebab,
6868
'in-footer' => true,
69-
)
69+
]
7070
);
7171

7272
$post_id = \get_the_ID();
7373
$permalink = \get_permalink( $post_id );
7474

7575
\wp_localize_script(
76-
Plugin::KEBAB,
77-
Plugin::SNAKE . '_data',
78-
array(
79-
'env' => array(
76+
Plugin::$kebab,
77+
Plugin::$snake . '_data',
78+
[
79+
'env' => [
8080
'siteUrl' => \untrailingslashit( \site_url() ),
8181
'ajaxUrl' => \untrailingslashit( \admin_url( 'admin-ajax.php' ) ),
8282
'userId' => \wp_get_current_user()->data->ID ?? null,
8383
'postId' => $post_id,
8484
'permalink' => \untrailingslashit( $permalink ),
85-
'APP_NAME' => Plugin::APP_NAME,
86-
'KEBAB' => Plugin::KEBAB,
87-
'SNAKE' => Plugin::SNAKE,
85+
'APP_NAME' => Plugin::$app_name,
86+
'KEBAB' => Plugin::$kebab,
87+
'SNAKE' => Plugin::$snake,
8888
'BASE_URL' => Base::BASE_URL,
8989
'APP1_SELECTOR' => Base::APP1_SELECTOR,
9090
'APP2_SELECTOR' => Base::APP2_SELECTOR,
9191
'API_TIMEOUT' => Base::API_TIMEOUT,
92-
'nonce' => \wp_create_nonce( Plugin::KEBAB ),
93-
),
94-
)
92+
'nonce' => \wp_create_nonce( Plugin::$kebab ),
93+
],
94+
]
9595
);
9696

9797
\wp_localize_script(
98-
Plugin::KEBAB,
98+
Plugin::$kebab,
9999
'wpApiSettings',
100-
array(
100+
[
101101
'root' => \untrailingslashit( \esc_url_raw( rest_url() ) ),
102102
'nonce' => \wp_create_nonce( 'wp_rest' ),
103-
)
103+
]
104104
);
105105
}
106106
}

0 commit comments

Comments
 (0)