Skip to content

Commit

Permalink
feat: 新增單例模式,重新調整檔案結構
Browse files Browse the repository at this point in the history
  • Loading branch information
j7-dev committed Apr 14, 2024
1 parent 98da45f commit e6c2f71
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 90 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"usefulteam/jwt-auth": "^2.1",
"lodash-php/lodash-php": "^0.0.7",
"yahnis-elsts/plugin-update-checker": "^5.3",
"j7-dev/tgm-plugin-activation-forked": "^1.0"
"j7-dev/tgm-plugin-activation-forked": "^1.0",
"micropackage/singleton": "^1.1"
},
"require-dev": {
"squizlabs/php_codesniffer": "@stable",
Expand Down
49 changes: 47 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

namespace J7\WpReactPlugin\Admin;

use J7\WpReactPlugin\Utils;
use Micropackage\Singleton\Singleton;
use J7\WpReactPlugin\Utils\Base;
use J7\WpReactPlugin\Plugin;

/**
* Class CPT
*/
final class CPT {
final class CPT extends Singleton {

/**
* Post metas
Expand Down Expand Up @@ -184,7 +185,7 @@ public function add_metabox( string $post_type ): void {
*/
public function render_meta_box(): void {
// phpcs:ignore
echo '<div id="' . Utils::APP2_SELECTOR . '"></div>';
echo '<div id="' . Base::APP2_SELECTOR . '"></div>';
}


Expand Down Expand Up @@ -277,7 +278,7 @@ public function load_custom_template( $template ) {
}
}

new CPT(
CPT::get(
array(
'post_metas' => array( 'meta', 'settings' ),
'rewrite' => array(
Expand Down
2 changes: 1 addition & 1 deletion inc/class/admin/index.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?php // phpcs:ignore
require_once __DIR__ . '/cpt/index.php';
require_once __DIR__ . '/class-cpt.php';
13 changes: 8 additions & 5 deletions inc/class/class-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@

namespace J7\WpReactPlugin;

use Micropackage\Singleton\Singleton;
use J7\WpReactPlugin\Utils\Base;
use Kucrut\Vite;

/**
* Class Bootstrap
*/
final class Bootstrap {
final class Bootstrap extends Singleton {


/**
* Constructor
*/
public function __construct() {
require_once __DIR__ . '/utils/index.php';
require_once __DIR__ . '/admin/index.php';
require_once __DIR__ . '/front-end/index.php';

Expand Down Expand Up @@ -82,10 +85,10 @@ public function enqueue_script(): void {
'APP_NAME' => Plugin::APP_NAME,
'KEBAB' => Plugin::KEBAB,
'SNAKE' => Plugin::SNAKE,
'BASE_URL' => Utils::BASE_URL,
'APP1_SELECTOR' => '#' . Utils::APP1_SELECTOR,
'APP2_SELECTOR' => '#' . Utils::APP2_SELECTOR,
'API_TIMEOUT' => Utils::API_TIMEOUT,
'BASE_URL' => Base::BASE_URL,
'APP1_SELECTOR' => '#' . Base::APP1_SELECTOR,
'APP2_SELECTOR' => '#' . Base::APP2_SELECTOR,
'API_TIMEOUT' => Base::API_TIMEOUT,
'nonce' => \wp_create_nonce( Plugin::KEBAB ),
),
)
Expand Down
33 changes: 33 additions & 0 deletions inc/class/front-end/class-entry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Front-end Entry
*/

declare(strict_types=1);

namespace J7\WpReactPlugin\FrontEnd;

use Micropackage\Singleton\Singleton;
use J7\WpReactPlugin\Utils\Base;
/**
* Class Entry
*/
final class Entry extends Singleton {

/**
* Constructor
*/
public function __construct() {
\add_action( 'wp_footer', array( $this, 'render_app' ) );
}

/**
* Render application's markup
*/
public function render_app(): void {
// phpcs:ignore
echo '<div id="' . Base::APP1_SELECTOR . '"></div>';
}
}

Entry::get();
33 changes: 2 additions & 31 deletions inc/class/front-end/index.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,2 @@
<?php
/**
* Front-end
*/

declare(strict_types=1);

namespace J7\WpReactPlugin;

/**
* Class FrontEnd
*/
final class FrontEnd {

/**
* Constructor
*/
public function __construct() {
\add_action( 'wp_footer', array( $this, 'render_app' ) );
}

/**
* Render application's markup
*/
public function render_app(): void {
// phpcs:ignore
echo '<div id="' . Utils::APP1_SELECTOR . '"></div>';
}
}

new FrontEnd();
<?php // phpcs:ignore
require_once __DIR__ . '/class-entry.php';
28 changes: 28 additions & 0 deletions inc/class/utils/class-base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Base
*/

declare (strict_types = 1);

namespace J7\WpReactPlugin\Utils;

/**
* Class Base
*/
abstract class Base {
const BASE_URL = '/';
const APP1_SELECTOR = 'my_app';
const APP2_SELECTOR = 'my_app_metabox';
const API_TIMEOUT = '30000';
const DEFAULT_IMAGE = 'http://1.gravatar.com/avatar/1c39955b5fe5ae1bf51a77642f052848?s=96&d=mm&r=g';

/**
* Is HPOS enabled
*
* @return bool
*/
public static function is_hpos_enabled(): bool {
return class_exists( \Automattic\WooCommerce\Utilities\OrderUtil::class ) && \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled();
}
}
2 changes: 2 additions & 0 deletions inc/class/utils/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php // phpcs:ignore
require_once __DIR__ . '/class-base.php';
19 changes: 0 additions & 19 deletions inc/utils/class-utils.php

This file was deleted.

2 changes: 0 additions & 2 deletions inc/utils/index.php

This file was deleted.

30 changes: 5 additions & 25 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@
namespace J7\WpReactPlugin;

use YahnisElsts\PluginUpdateChecker\v5\PucFactory;
use Micropackage\Singleton\Singleton;
use TGM_Plugin_Activation;

if ( ! \class_exists( 'J7\WpReactPlugin\Plugin' ) ) {
require_once __DIR__ . '/vendor/autoload.php';

/**
* Class Plugin
*/
final class Plugin {

final class Plugin extends Singleton {
const APP_NAME = 'My App';
const KEBAB = 'my-app';
const SNAKE = 'my_app';
Expand Down Expand Up @@ -62,13 +63,6 @@ final class Plugin {
*/
public static $version;

/**
* Instance
*
* @var Plugin
*/
private static $instance;

/**
* Required plugins
*
Expand All @@ -93,8 +87,6 @@ final class Plugin {
* Constructor
*/
public function __construct() {
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/inc/utils/index.php';
require_once __DIR__ . '/inc/class/class-bootstrap.php';

\register_activation_hook( __FILE__, array( $this, 'activate' ) );
Expand All @@ -121,20 +113,8 @@ public function check_required_plugins() {
$plugin_data = \get_plugin_data( __FILE__ );
self::$version = $plugin_data['Version'];

new Bootstrap();
}
}

/**
* Instance
*
* @return Plugin
*/
public static function instance() {
if ( empty( self::$instance ) ) {
self::$instance = new self();
Bootstrap::get();
}
return self::$instance;
}

/**
Expand Down Expand Up @@ -288,5 +268,5 @@ public function deactivate(): void {
}
}

Plugin::instance();
Plugin::get();
}

0 comments on commit e6c2f71

Please sign in to comment.