-
Notifications
You must be signed in to change notification settings - Fork 3
/
wp-react-starter-plugin.php
176 lines (162 loc) · 3.57 KB
/
wp-react-starter-plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* WP React Starter Plugin
*
* @package WPReactStarterPlugin
* @author Shazahanul Islam Shohag
* @copyright 2020 Shazahanul Islam Shohag
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: WP React Starter Plugin
* Plugin URI: https://example.com/plugin-name
* Description: Demonstration of React.js usage in WP plugin.
* Version: 1.0.0
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: Shazahanul Islam Shohag
* Author URI: https://wpdev.icu
* Text Domain: wprsp
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
/**
* Main Plugin Class.
*
* @since 1.0.0
* Class WPReactStarterPlugin
*/
final class WPReactStarterPlugin {
/**
* Plugin version.
*
* @var string
*/
const VERSION = '1.0.0';
/**
* Plugin Instance.
*
* @var WPReactStarterPlugin
*/
private static $instance;
/**
* WPReactStarterPlugin constructor.
*
* @since 1.0.0
* @return void
*/
private function __construct() {
$this->define_constants();
register_activation_hook( __FILE__, array( $this, 'activation' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivation' ) );
add_action( 'plugin_loaded', array( $this, 'plugin_functionality' ) );
}
/**
* Initialize the plugin.
*
* @since 1.0.0
* @return WPReactStarterPlugin
*/
public static function init() {
if ( empty( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Plugin Activation logic.
*
* @since 1.0.0
* @return void
*/
public function activation() {
// Plugin Activation logic goes here.
}
/**
* Plugin Deactivation logic.
*
* @since 1.0.0
* @return void
*/
public function deactivation() {
// Plugin Deactivation logic goes here.
}
/**
* Defined all the required constants.
*
* @since 1.0.0
* @return void
*/
private function define_constants() {
define( 'WPRSP_VERSION', self::VERSION );
define( 'WPRSP_FILE', __FILE__ );
define( 'WPRSP_PATH', __DIR__ );
define( 'WPRSP_ASSETS', plugins_url( '', WPRSP_FILE ) . '/assets' );
define( 'WPRSP_ASSETS_REACT', plugins_url( '', WPRSP_FILE ) . '/build' );
}
/**
* All the plugin functionality
*
* @since 1.0.0
* @return void
*/
public function plugin_functionality() {
add_action( 'wp_enqueue_scripts', array( $this, 'register_assets' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'register_assets' ) );
add_action( 'admin_menu', array( $this, 'add_menu' ) );
}
/**
* Register all the plugin assets
*
* @since 1.0.0
* @return void
*/
public function register_assets() {
$index_asset = require WPRSP_PATH . '/build/index.asset.php';
wp_register_script(
'wprsp-index',
WPRSP_ASSETS_REACT . '/index.js',
$index_asset['dependencies'],
$index_asset['version'],
false
);
}
/**
* Display option menu.
*
* @since 1.0.0
* @return void
*/
public function add_menu() {
add_options_page(
__( 'WP React Starter Plugin', 'wprsp' ),
__( 'WP React Starter', 'wprsp' ),
'manage_options',
'wprsp_react_demo',
array(
$this,
'settings_page',
)
);
}
/**
* Settings Page.
*
* @return void
*/
public function settings_page() {
wp_enqueue_script( 'wprsp-index' );
echo '<div class="wrap"><div id="wprsp"></div></div>';
}
}
/**
* Main function for calling the class
* from other plugins.
*
* @since 1.0.0
* @return WPReactStarterPlugin
*/
function wprsp() {
return WPReactStarterPlugin::init();
}
wprsp();