-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-base-plugin.php
155 lines (132 loc) · 4.33 KB
/
wp-base-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
<?php
use App\Common\PluginActivator;
/**
* Plugin Name: Wp Base Plugin
* Plugin URI: http://github.com/rahat1994
* Description: A sample WordPress plugin to implement Vue with tailwind.
* Author: Rahat Baksh
* Author URI: http://github.com/rahat1994
* Version: 1.0.0
* Text Domain: wp-base-plugin
* Domain Path: /languages
* License: GPL2
*/
define('PLUGIN_CONST_URL', plugin_dir_url(__FILE__));
define('PLUGIN_CONST_DIR', plugin_dir_path(__FILE__));
define('PLUGIN_CONST_VERSION', '1.0.0');
define('PLUGIN_CONST_DEVELOPMENT', true);
require_once(PLUGIN_CONST_DIR . 'vendor/autoload.php');
use App\Common\AjaxHandler;
use App\Common\LoadAssets;
use App\Interfaces\Commons\ApiHandlerInterface;
use App\Interfaces\Commons\AssetsLoaderInterface;
use DI\Container;
class WpBasePlugin
{
public AjaxHandler $apiHandler;
public LoadAssets $assetsLoader;
public array $CPTS = [];
public array $shortCodes = [];
public array $settingsPages = [];
public PluginActivator $pluginActivator;
public function __construct(AjaxHandler $apiHandler, LoadAssets $assetsLoader, PluginActivator $pluginActivator)
{
$this->apiHandler = $apiHandler;
$this->assetsLoader = $assetsLoader;
$this->pluginActivator = $pluginActivator;
}
public function boot()
{
$this->activatePlugin();
$this->registerHooks();
$this->registerShortCodes();
$this->registerSettingsPages();
$this->registerApiRoutes();
$this->registerCPT();
$this->renderMenu();
}
public function registerCPT(){
foreach($this->CPTS as $cpt){
$cpt->boot();
}
}
public function registerShortCodes() {
foreach($this->shortCodes as $shortCode){
$shortCode->boot();
}
}
public function setShortCodes($shortCodes){
$this->shortCodes = $shortCodes;
}
public function registerSettingsPages() {
foreach($this->settingsPages as $page){
$page->boot();
}
}
public function registerHooks()
{
add_action('init', array($this, 'loadTextDomain'));
}
public function activatePlugin()
{
register_activation_hook(__FILE__, function ($netWorkWide) {
$this->pluginActivator->migrateDatabases($netWorkWide);
});
}
public function registerApiRoutes()
{
$this->apiHandler->boot();
}
public function renderMenu()
{
add_action('admin_menu', function () {
if (!current_user_can('manage_options')) {
return;
}
global $submenu;
add_menu_page(
'WPBasePlugin',
'WPBasePlugin',
'manage_options',
'wp-base-plugin',
array($this, 'renderAdminPage'),
'dashicons-editor-code',
25
);
$submenu['pluginslug.php']['dashboard'] = array(
'Dashboard',
'manage_options',
'admin.php?page=wp-base-plugin#/',
);
$submenu['pluginslug.php']['contact'] = array(
'Contact',
'manage_options',
'admin.php?page=wp-base-plugin#/contact',
);
});
}
public function renderAdminPage()
{
$this->assetsLoader->admin();
$translatable = apply_filters('wp-base-plugin/frontend_translatable_strings', array(
'hello' => __('Hello', 'wp-base-plugin'),
));
$pluginlowercase = apply_filters('pluginlowercase/admin_app_vars', array(
'url' => PLUGIN_CONST_URL,
'assets_url' => PLUGIN_CONST_URL . 'assets/',
'ajaxurl' => admin_url('admin-ajax.php'),
'action' => 'wp_base_plugin',
'nonce' => wp_create_nonce('wp-base-plugin-nonce'),
'i18n' => $translatable
));
wp_localize_script('wp-base-plugin-script', 'wpbaseplugin', $pluginlowercase);
echo '<div id="app"></div>';
}
public function loadTextDomain()
{
load_plugin_textdomain('wp-base-plugin', false, basename(dirname(__FILE__)) . '/languages');
}
}
$container = ServiceContainer::getInstance()->getContainer();
$wpBasePlugin = $container->get(WpBasePlugin::class);
$wpBasePlugin->boot();