Skip to content

Commit 9829dc3

Browse files
committed
feat: add WpUtils
1 parent 1948f9a commit 9829dc3

File tree

7 files changed

+56
-303
lines changed

7 files changed

+56
-303
lines changed

composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
},
2525
"require": {
2626
"kucrut/vite-for-wp": "^0.8.0",
27-
"yahnis-elsts/plugin-update-checker": "^5.3",
28-
"j7-dev/tgm-plugin-activation-forked": "1.2.2",
29-
"micropackage/singleton": "^1.1"
27+
"j7-dev/wp-utils": "0.0.8"
3028
},
3129
"require-dev": {
3230
"squizlabs/php_codesniffer": "@stable",

inc/class/admin/class-cpt.php

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77

88
namespace J7\WpReactPlugin\Admin;
99

10-
use Micropackage\Singleton\Singleton;
1110
use J7\WpReactPlugin\Utils\Base;
1211
use J7\WpReactPlugin\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 = array();
2525
/**
2626
* Rewrite
2727
*
@@ -31,16 +31,22 @@ final class CPT extends Singleton {
3131

3232
/**
3333
* Constructor
34-
*
35-
* @param array $args Arguments.
3634
*/
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();
4046

4147
\add_action( 'init', array( $this, 'init' ) );
4248

43-
if ( ! empty( $args['post_metas'] ) ) {
49+
if ( ! empty( $args['post_meta_array'] ) ) {
4450
\add_action( 'rest_api_init', array( $this, 'add_post_meta' ) );
4551
}
4652

@@ -140,10 +146,10 @@ public static function register_cpt(): void {
140146
* Register meta fields for post type to show in rest api
141147
*/
142148
public function add_post_meta(): void {
143-
foreach ( $this->post_metas as $meta_key ) {
149+
foreach ( $this->post_meta_array as $meta_key ) {
144150
\register_meta(
145151
'post',
146-
Plugin::SNAKE . '_' . $meta_key,
152+
Plugin::$snake . '_' . $meta_key,
147153
array(
148154
'type' => 'string',
149155
'show_in_rest' => true,
@@ -168,9 +174,9 @@ public function init_metabox(): void {
168174
* @param string $post_type Post type.
169175
*/
170176
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 ) ) ) {
172178
\add_meta_box(
173-
Plugin::KEBAB . '-metabox',
179+
Plugin::$kebab . '-metabox',
174180
__( 'My App', 'wp_react_plugin' ),
175181
array( $this, 'render_meta_box' ),
176182
$post_type,
@@ -254,15 +260,15 @@ public function save_metabox( $post_id, $post ) { // phpcs:ignore
254260
/* OK, it's safe for us to save the data now. */
255261

256262
// Sanitize the user input.
257-
$meta_data = \sanitize_text_field( $_POST[ Plugin::SNAKE . '_meta' ] );
263+
$meta_data = \sanitize_text_field( $_POST[ Plugin::$snake . '_meta' ] );
258264

259265
// 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 );
261267
}
262268

263269
/**
264270
* Load custom template
265-
* Set {Plugin::KEBAB}/{slug}/report php template
271+
* Set {Plugin::$kebab}/{slug}/report php template
266272
*
267273
* @param string $template Template.
268274
*/
@@ -278,13 +284,4 @@ public function load_custom_template( $template ) {
278284
}
279285
}
280286

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();

inc/class/class-bootstrap.php

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

88
namespace J7\WpReactPlugin;
99

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

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

2019
/**
2120
* Constructor
@@ -64,7 +63,7 @@ public function enqueue_script(): void {
6463
Plugin::$dir . '/js/dist',
6564
'js/src/main.tsx',
6665
array(
67-
'handle' => Plugin::KEBAB,
66+
'handle' => Plugin::$kebab,
6867
'in-footer' => true,
6968
)
7069
);
@@ -73,29 +72,29 @@ public function enqueue_script(): void {
7372
$permalink = \get_permalink( $post_id );
7473

7574
\wp_localize_script(
76-
Plugin::KEBAB,
77-
Plugin::SNAKE . '_data',
75+
Plugin::$kebab,
76+
Plugin::$snake . '_data',
7877
array(
7978
'env' => array(
8079
'siteUrl' => \untrailingslashit( \site_url() ),
8180
'ajaxUrl' => \untrailingslashit( \admin_url( 'admin-ajax.php' ) ),
8281
'userId' => \wp_get_current_user()->data->ID ?? null,
8382
'postId' => $post_id,
8483
'permalink' => \untrailingslashit( $permalink ),
85-
'APP_NAME' => Plugin::APP_NAME,
86-
'KEBAB' => Plugin::KEBAB,
87-
'SNAKE' => Plugin::SNAKE,
84+
'APP_NAME' => Plugin::$app_name,
85+
'KEBAB' => Plugin::$kebab,
86+
'SNAKE' => Plugin::$snake,
8887
'BASE_URL' => Base::BASE_URL,
8988
'APP1_SELECTOR' => Base::APP1_SELECTOR,
9089
'APP2_SELECTOR' => Base::APP2_SELECTOR,
9190
'API_TIMEOUT' => Base::API_TIMEOUT,
92-
'nonce' => \wp_create_nonce( Plugin::KEBAB ),
91+
'nonce' => \wp_create_nonce( Plugin::$kebab ),
9392
),
9493
)
9594
);
9695

9796
\wp_localize_script(
98-
Plugin::KEBAB,
97+
Plugin::$kebab,
9998
'wpApiSettings',
10099
array(
101100
'root' => \untrailingslashit( \esc_url_raw( rest_url() ) ),

inc/class/front-end/class-entry.php

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

88
namespace J7\WpReactPlugin\FrontEnd;
99

10-
use Micropackage\Singleton\Singleton;
1110
use J7\WpReactPlugin\Utils\Base;
1211
/**
1312
* Class Entry
1413
*/
15-
final class Entry extends Singleton {
14+
final class Entry {
15+
use \J7\WpUtils\Traits\SingletonTrait;
1616

1717
/**
1818
* Constructor
@@ -30,4 +30,4 @@ public function render_app(): void {
3030
}
3131
}
3232

33-
Entry::get();
33+
Entry::instance();

inc/class/utils/class-base.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,4 @@ abstract class Base {
1616
const APP2_SELECTOR = '#my_app_metabox';
1717
const API_TIMEOUT = '30000';
1818
const DEFAULT_IMAGE = 'http://1.gravatar.com/avatar/1c39955b5fe5ae1bf51a77642f052848?s=96&d=mm&r=g';
19-
20-
/**
21-
* Is HPOS enabled
22-
*
23-
* @return bool
24-
*/
25-
public static function is_hpos_enabled(): bool {
26-
return class_exists( \Automattic\WooCommerce\Utilities\OrderUtil::class ) && \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled();
27-
}
28-
29-
/**
30-
* Delete post meta by meta id
31-
*
32-
* @param int $mid - meta id
33-
* @return string
34-
*/
35-
public static function delete_post_meta_by_mid( $mid ) {
36-
global $wpdb;
37-
38-
// 执行删除查询
39-
$deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}postmeta WHERE meta_id = %d", $mid ) );
40-
41-
$delete_success = $deleted !== false;
42-
43-
return $delete_success;
44-
}
4519
}

0 commit comments

Comments
 (0)