-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
463 lines (392 loc) · 9.21 KB
/
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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<?php
/**
* Post Comments plugin
*
* @package Post Comments
* @category Plugins
* @version 1.0.0
* @since 1.0.0
*/
// Stop if accessed directly.
if ( ! defined( 'BLUDIT' ) ) {
die( 'You are not allowed direct access to this file.' );
}
// Access namespaced functions.
use function Post_Comments\{
plugin,
can_manage,
debug_mode,
enqueue_assets,
comments_log_path,
post_comments_full,
disable_comments
};
/**
* Core plugin class
*
* Extends the Bludit class for plugin functionality,
* options form, and template hooks.
*
* @since 1.0.0
* @version 1.0.0
*/
class Post_Comments extends Plugin {
/**
* Directory name
*
* Used for the plugin directory and
* for content directories.
*
* @since 1.0.0
* @access public
* @var string
*/
public $file_dir = 'post-comments';
/**
* Prepare plugin for installation
*
* @since 1.0.0
* @access public
* @return void
*/
public function prepare() {
$this->get_files();
}
/**
* Constructor method
*
* @since 1.0.0
* @access public
* @return self
*/
public function __construct() {
// Run parent constructor.
parent :: __construct();
// Include functionality.
if ( $this->installed() ) {
$this->get_files();
}
}
/**
* Include functionality
*
* @since 1.0.0
* @access public
* @return void
*/
public function get_files() {
// Plugin path.
$path = PATH_PLUGINS . $this->directoryName . DS;
// Get plugin functions.
foreach ( glob( $path . 'includes/*.php' ) as $filename ) {
require_once $filename;
}
}
/**
* Initialize plugin
*
* @since 1.0.0
* @access public
* @global object $L The Language class.
* @return void
*/
public function init() {
// Access global variables.
global $L;
// Plugin options for database.
$this->dbFields = [
'user_level' => 'admin',
'post_types' => 'post',
'logged_form' => true,
'logged_comments' => false,
'form_location' => 'before',
'form_heading' => $L->get( 'Leave a Comment' ),
'form_heading_el' => 'h2',
'loop_heading' => $L->get( 'User Discussion' ),
'loop_heading_el' => 'h2',
'admin_email' => '',
'terms_page' => '',
'accept_terms' => false,
'dashboard_log' => true,
];
// Array of custom hooks.
$this->customHooks = [
'comments_full',
'comments_list',
'comments_form'
];
if ( ! $this->installed() ) {
$Tmp = new dbJSON( $this->filenameDb );
$this->db = $Tmp->db;
$this->prepare();
}
}
/**
* Install plugin
*
* @since 1.0.0
* @access public
* @param integer $position
* @return boolean Return true if the installation is successful.
*/
public function install( $position = 1 ) {
if ( $this->installed() ) {
return;
}
// Create workspace.
$workspace = $this->workspace();
mkdir( $workspace, DIR_PERMISSIONS, true );
// Create plugin directory for the database
mkdir( PATH_PLUGINS_DATABASES . $this->directoryName, DIR_PERMISSIONS, true );
$this->dbFields['position'] = $position;
// Sanitize default values to store in the file.
foreach ( $this->dbFields as $key => $value ) {
if ( is_array( $value ) ) {
$value = $value;
} else {
$value = Sanitize :: html( $value );
}
settype( $value, gettype( $this->dbFields[$key] ) );
$this->db[$key] = $value;
}
$storage = PATH_CONTENT . $this->file_dir . DS;
if ( ! file_exists( $storage ) ) {
Filesystem :: mkdir( $storage, 0755 );
}
file_put_contents( $storage . '.htaccess', 'Deny from all' );
// Create the database.
return $this->save();
}
/**
* Load frontend scripts & styles
*
* @since 1.0.0
* @access public
* @return string
*/
public function siteHead() {
// Maybe get non-minified assets.
$suffix = '';
if ( ! debug_mode() ) {
$suffix = '.min';
}
$assets = '';
if ( enqueue_assets() ) {
$assets .= '<link rel="stylesheet" type="text/css" href="' . $this->domainPath() . "assets/css/frontend{$suffix}.css?version=" . $this->getMetadata( 'version' ) . '" />' . PHP_EOL;
}
return "\n" . $assets;
}
/**
* Admin page controller
*
* @since 1.0.0
* @access public
* @global object $L The Language class.
* @global array $layout
* @global object $site The Site class.
* @return void
*/
public function adminController() {
// Access global variables.
global $L, $layout, $pages, $site, $url;
checkRole( [ 'admin', 'editor', 'author' ] );
$layout['title'] = $L->get( 'Manage Post Comments' ) . ' | ' . $site->title();
}
/**
* Admin settings form
*
* @since 1.0.0
* @access public
* @global object $L Language class.
* @global object $plugin Plugin class.
* @global object $site Site class.
* @return string Returns the markup of the form.
*/
public function form() {
// Access global variables.
global $L, $plugin, $site;
$html = '';
ob_start();
include( $this->phpPath() . '/views/page-form.php' );
$html .= ob_get_clean();
return $html;
}
/**
* Load backend scripts & styles
*
* @since 1.0.0
* @access public
* @return string
*/
public function adminHead() {
// Maybe get non-minified assets.
$suffix = '';
if ( ! debug_mode() ) {
$suffix = '.min';
}
$assets = '<link rel="stylesheet" type="text/css" href="' . $this->domainPath() . "assets/css/admin{$suffix}.css?version=" . $this->getMetadata( 'version' ) . '" />' . PHP_EOL;
return "\n" . $assets;
}
/**
* Admin page(s)
*
* @since 1.0.0
* @access public
* @global object $L Language class.
* @global object $security Security class.
* @return string Returns page markup.
*/
public function adminView() {
// Access global variables.
global $L, $security;
$html = '';
ob_start();
if ( isset( $_GET['page'] ) && 'guide' == $_GET['page'] ) {
include( $this->phpPath() . '/views/page-info.php' );
} else {
include( $this->phpPath() . '/views/page-admin.php' );
}
$html .= ob_get_clean();
return $html;
}
/**
* Body begin admin hook
*
* Used to begin user session if not already.
*
* @since 1.0.0
* @access public
* @return void
*/
public function adminBodyBegin() {
if ( ! isset( $_SESSION ) ) {
session_start();
};
}
/**
* Admin sidebar hook
*
* @since 1.0.0
* @access public
* @global object $L Language class.
* @return string Returns link markup.
*/
public function adminSidebar() {
// Access global variables.
global $L;
$url = DOMAIN_ADMIN . 'plugin/' . $this->className();
$html = sprintf(
'<a class="nav-link" href="%s"><span class="fa fa-comments"></span>%s</a>',
$url,
$L->get( 'Comments' )
);
return $html;
}
/**
* Dashboard admin hook
*
* @since 1.0.0
* @access public
* @global object $L Language class.
* @global object $security Security class.
* @return string Returns the log widget markup.
*/
public function dashboard() {
// Access global variables.
global $L, $security;
if ( $this->dashboard_log() && ! can_manage() ) {
return false;
} elseif ( ! $this->dashboard_log() ) {
return false;
}
$html = '';
ob_start();
include( $this->phpPath() . '/views/dashboard-log.php' );
$html .= ob_get_clean();
return $html;
}
/**
* Full comments theme hook
*
* Prints the comments list and the form
*
* @since 1.0.0
* @access public
* @global object $L Language class.
* @global object $page The Page class.
* @global object $url The Url class.
* @return mixed
*/
public function comments_full() {
// Access global variables.
global $L, $page, $url;
if ( 'page' !== $url->whereAmI() ) {
return false;
}
if ( disable_comments() ) {
printf(
'<p class="comments-closed-notice">%s</p>',
$L->get( 'Comments are closed.' )
);
return false;
}
if ( $page->isStatic() && ( 'page' == $this->post_types() || 'both' == $this->post_types() ) ) {
return post_comments_full();
} elseif ( ! $page->isStatic() && 'page' !== $this->post_types() ) {
return post_comments_full();
}
return false;
}
// @return string
public function user_level() {
return $this->getValue( 'user_level' );
}
// @return string
public function post_types() {
return $this->getValue( 'post_types' );
}
// @return boolean
public function logged_form() {
return $this->getValue( 'logged_form' );
}
// @return boolean
public function logged_comments() {
return $this->getValue( 'logged_comments' );
}
// @return string
public function form_location() {
return $this->getValue( 'form_location' );
}
// @return string
public function form_heading() {
return $this->getValue( 'form_heading' );
}
// @return string
public function form_heading_el() {
return $this->getValue( 'form_heading_el' );
}
// @return string
public function loop_heading() {
return $this->getValue( 'loop_heading' );
}
// @return string
public function loop_heading_el() {
return $this->getValue( 'loop_heading_el' );
}
// @return string
public function admin_email() {
return $this->getValue( 'admin_email' );
}
// @return boolean
public function accept_terms() {
return $this->getValue( 'accept_terms' );
}
// @return string
public function terms_page() {
return $this->getValue( 'terms_page' );
}
// @return boolean
public function dashboard_log() {
return $this->getValue( 'dashboard_log' );
}
}