/
he-ib.php
278 lines (226 loc) · 7.44 KB
/
he-ib.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/*
Plugin Name: he-ib
Plugin URI: http://halo.ib/?return=true
Description: HaloEngine Informative Boxes plugin for WordPress
Version: 1.0.0
Author: HungTran
Author URI: http://halo.ib/wordpress-plugins/
License: GPLv2 or later
*/
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
defined( 'ABSPATH' ) || exit;
require_once(dirname(__FILE__) . '/includes/lib.php');
/** If you hardcode a WP.com API key here, all key config screens will be hidden */
class HEIBApp {
private function __construct() { /* Do nothing here */ }
/**
* Main HEIBApp Instance.
*
*/
public static function getInstance() {
// Store the instance locally to avoid private static replication
static $instance = null;
// Only run these methods if they haven't been run previously
if ( null === $instance ) {
$instance = new HEIBApp;
$instance->defineConstants();
$instance->version();
$instance->setupHooks();
}
// Always return the instance
return $instance;
}
private function defineConstants(){
}
private function version(){
}
private function setupHooks(){
if(is_admin()){
$this->adminHooks();
} else {
$this->frontendHooks();
}
$this->apiHooks();
$this->commonHooks();
$this->shortcodeHooks();
$this->ajaxHooks();
//hook for 3rd plugin to add additional endpoints/shortcodes
do_action('heib_hooks_loaded');
}
private function ajaxHooks(){
add_action( 'wp_ajax_heib_ajax', 'HEIBApp::ajaxProcess' );
add_action( 'wp_ajax_nopriv_heib_ajax', 'HEIBApp::ajaxProcess' );
}
private function adminHooks(){
//admin menu
add_action('admin_menu', 'HEIBApp::admin_menu');
}
private function frontendHooks(){
HEIBApp::frontend_enqueue_scripts();
}
private function apiHooks(){
require_once(dirname(__FILE__) . '/includes/endpoints.php');
}
private function commonHooks(){
}
private function shortcodeHooks(){
require_once(dirname(__FILE__) . '/includes/shortcodes.php');
HEIBShortcode::init();
}
/****************************************** Define hook call back function ***************************************/
public static function admin_menu(){
$parent_slug = 'heib_dashboard';
$handle = 'HEIBApp::getDashboardContent';
$capability = 'publish_pages';
//menu page
$page = add_menu_page( 'Informative Boxes',
'Informative Boxes',
//'manage_options',
$capability,
$parent_slug,
$handle,
'');
//hight top menu
add_submenu_page( $parent_slug,
'Settings',
'Settings',
$capability,
$parent_slug);
$designPage = add_submenu_page( $parent_slug,
'Box Design',
'Box Design',
$capability,
'heib_design',
'HEIBApp::getDesignContent' );
add_action( 'admin_print_styles-' . $designPage, 'HEIBApp::admin_enqueue_styles' );
add_action( 'admin_print_scripts-' . $designPage, 'HEIBApp::admin_enqueue_scripts' );
//call register settings function
add_action( 'admin_init', 'HEIBAPP::register_plugin_settings' );
}
public static function admin_enqueue_styles(){
}
public static function admin_enqueue_scripts(){
wp_enqueue_script( 'heibadmin', plugins_url( '/dist/backend.bundle.js' , __FILE__ ), array( 'jquery' ));
wp_enqueue_script( 'heibadmin_config', plugins_url( '/assets/js/config.js' , __FILE__ ), array( 'heibadmin' ));
}
public static function frontend_enqueue_scripts(){
if(get_option('heib_enable', 1)){
add_action('wp_head',function(){
echo '<script type="text/javascript"> var ajaxurl = "' . admin_url('admin-ajax.php') .'"</script>';
});
wp_enqueue_script( 'heibfrontend', plugins_url( '/dist/frontend.bundle.js' , __FILE__ ), array( 'jquery' ));
wp_enqueue_script( 'heibfrontend_config', plugins_url( '/assets/js/config.js' , __FILE__ ), array( 'heibfrontend' ));
}
}
public static function admin_content(){
if(isset($_REQUEST['view']) && $_REQUEST['view'] == 'design'){
HEIBApp::getDesignContent();
} else {
HEIBApp::getDashboardContent();
}
}
public static function register_plugin_settings(){
//register our settings
register_setting( 'heib-settings-group', 'heib_blacklist_urls' );
register_setting( 'heib-settings-group', 'heib_enable' );
}
public static function getDashboardContent(){
?>
<div class="wrap">
<h2>Informative Boxes Settings</h2>
<form method="post" action="options.php">
<?php settings_fields( 'heib-settings-group' ); ?>
<?php do_settings_sections( 'heib-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Enable Informative Box</th>
<td>
<select name="heib_enable" value="<?php echo esc_attr( get_option('heib_enable') ); ?>">
<option value="1" <?php if(esc_attr( get_option('heib_enable', 1) ) == 1) echo "selected"?>>Yes</option>
<option value="0" <?php if(esc_attr( get_option('heib_enable', 1) ) == 0) echo "selected"?>>No</option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row">Do not show Informative Boxes for urls</th>
<td>
<textarea rows="5" cols="40" name="heib_blacklist_urls"><?php echo esc_attr( get_option('heib_blacklist_urls') ); ?></textarea>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
public static function getDesignContent(){
echo '<div id="heib_wrapper"></div>';
$settings = array( 'media_buttons' => false );
$editor_id = 'heib-editor';
wp_editor( '', $editor_id, $settings );
}
public static function ajaxProcess(){
$res = new HEIBResponse();
$res = HEIBApp::processEndpoint($res);
echo $res->toJson();
exit;
}
public static function processEndpoint($res){
$query = $res->query();
if(!$query['endpoint']){
$res->error = 1;
$res->errMsg = 'No endpoints are provided';
} else {
$res = apply_filters('heib_process_endpoint__' . $query['endpoint'], $res);
}
return $res;
}
/****************************************** End hook call back function ***************************************/
}
class HEIBResponse extends stdClass {
protected $query;
public function __construct($query = null){
global $heib__Response;
if(is_null($query)){
$query = $_REQUEST;
}
$this->query = $query;
//setup global response
$heib__Response = $this;
// parent::__construct();
}
public function query(){
return $this->query;
}
public function toJson(){
return json_encode($this);
}
public function toObject(){
$rtn = new stdClass();
if(isset($this->data)){
$rtn->data = $this->data;
}
if(isset($this->error)){
$rtn->error = $this->error;
}
if(isset($this->errMsg)){
$rtn->errMsg = $this->errMsg;
}
return $rtn;
}
}
$GLOBALS['heib'] = HEIBApp::getInstance();
$GLOBALS['heib__Response'] = null;