-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
605 lines (524 loc) · 11.7 KB
/
plugin.php
File metadata and controls
605 lines (524 loc) · 11.7 KB
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
<?php
/**
* Search Forms
*
* Plugin core class, do not namespace.
*
* Improved search form plugin for Bludit CMS.
*
* @package Search Forms
* @subpackage Core
* @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 SearchForms\sidebar_search;
/**
* Core plugin class
*
* Extends the Plugin class to inherit its methods.
*/
class Search_Forms extends Plugin {
/**
* Source
*
* @since 1.0.0
* @access private
* @var array An array of associative arrays.
*/
private $pagesFound = [];
/**
* Search mode
*
* @since 1.0.0
* @access private
* @var integer
*/
private $numberOfItems = 0;
/**
* Constructor method
*
* @since 1.0.0
* @access public
* @return self
*/
public function __construct() {
// Run parent constructor.
parent :: __construct();
// Include functionality.
if ( $this->installed() ) {
$this->autoload();
$this->get_files();
}
}
/**
* Prepare plugin for installation
*
* @since 1.0.0
* @access public
* @return void
*/
public function prepare() {
$this->autoload();
$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;
}
}
/**
* Autoload classes
*
* @since 1.0.0
* @access public
* @return void
*/
public function autoload() {
// Path to class files.
$path = PATH_PLUGINS . $this->directoryName() . DS . 'includes/classes' . DS;
// Array of namespaced classes & filenames.
$classes = [
'SearchForms\Search_Results' => $path . 'class-search-results.php'
];
spl_autoload_register(
function ( string $class ) use ( $classes ) {
if ( isset( $classes[ $class ] ) ) {
require $classes[ $class ];
}
}
);
}
/**
* Initiate plugin
*
* @since 1.0.0
* @access public
* @global object $L Language class.
* @return void
*/
public function init() {
// Access global variables.
global $L;
// Database fields array.
$this->dbFields = [
'min_chars' => 3,
'cache_words' => 800,
'in_sidebar' => true,
'wrap' => true,
'wrap_class' => 'form-wrap search-form-wrap',
'form_class' => 'form search-form',
'label' => $L->get( 'Search' ),
'label_wrap' => 'h2',
'placeholder' => $L->get( "Enter at least 3 characters." ),
'button' => true,
'button_text' => $L->get( 'Submit' ),
'button_class' => 'button btn btn-md search-submit-button'
];
// Array of custom hooks.
$this->customHooks = [
'search_form'
];
if ( ! $this->installed() ) {
$Tmp = new dbJSON( $this->filenameDb );
$this->db = $Tmp->db;
$this->prepare();
}
}
/**
* Admin head
*
* Used to insert a style block into the
* admin head section.
*
* @since 1.0.0
* @access public
* @global object $site Site class.
* @global object $site Url class.
* @return void
*/
public function adminHead() {
// Access global variables.
global $site, $url;
if ( 'configureight' == $site->theme() ) {
return null;
}
// Load only for this plugin's pages.
if ( ! str_contains( $url->slug(), $this->className() ) ) {
return null;
}
?>
<style>
.form-range-row {
padding: 0 30px;
}
.form-range-controls {
display: flex;
align-items: center;
flex-wrap: nowrap;
gap: 1em;
width: 100%;
max-width: 640px;
margin: 0;
padding: 0;
}
.form-range-value {
display: inline-block;
min-width: 6ch;
padding: 0.25em 0.5em;
border: solid 1px #dee2e6;
text-align: center;
}
</style>
<?php
}
/**
* 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/form-page.php' );
$html .= ob_get_clean();
return $html;
}
/**
* Admin controller
*
* Change the text of the `<title>` tag.
*
* @since 1.0.0
* @access public
* @global object $L The Language class.
* @global array $layout
* @return string Returns the head content.
*/
public function adminController() {
global $L, $layout, $site;
$layout['title'] = $L->get( 'Search Forms Guide' ) . ' | ' . $site->title();
}
/**
* Admin user guide
*
* @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 page.
*/
public function adminView() {
// Access global variables.
global $L, $plugin, $site;
$html = '';
ob_start();
include( $this->phpPath() . '/views/guide-page.php' );
$html .= ob_get_clean();
return $html;
}
/**
* Sidebar search form
*
* @since 1.0.0
* @access public
* @return string Returns the form markup.
*/
public function siteSidebar() {
if ( $this->in_sidebar() ) {
return sidebar_search();
}
}
/**
* Install the plugin
*
* Sets sidebar position and creates
* the cache file.
*
* @since 1.0.0
* @access public
* @param integer $position
* @return void
*/
public function install( $position = 0 ) {
parent :: install( $position );
return $this->create_cache();
}
/**
* Form save
*
* @since 1.0.0
* @access public
* @return void
*/
public function post() {
parent :: post();
return $this->create_cache();
}
/**
* After page create hook
*
* @since 1.0.0
* @access public
* @return void
*/
public function afterPageCreate() {
$this->create_cache();
}
/**
* After page modify hook
*
* @since 1.0.0
* @access public
* @return void
*/
public function afterPageModify() {
$this->create_cache();
}
/**
* After page delete hook
*
* @since 1.0.0
* @access public
* @return void
*/
public function afterPageDelete() {
$this->create_cache();
}
/**
* Before all hook
*
* @since 1.0.0
* @access public
* @return void
*/
public function beforeAll() {
// Access global variables.
global $site, $url;
// Check if the URL matches the webhook.
$webhook = 'search';
if ( $this->webhook( $webhook, false, false ) ) {
// Change the whereAmI to avoid load pages in the rule 69.pages.
// This is only for performance purposes.
$url->setWhereAmI( 'search' );
// Get the string to search from the URL
$stringToSearch = $this->webhook( $webhook, true, false );
$stringToSearch = trim( $stringToSearch, '/' );
// Search the string in the cache and get all pages with matches
$list = $this->search_results( $stringToSearch );
$this->numberOfItems = count( $list );
// Split the content in pages
// The first page number is 1, so the real is 0
$realPageNumber = $url->pageNumber() - 1;
$itemsPerPage = $site->itemsPerPage();
if ( $itemsPerPage <= 0 ) {
if ( $realPageNumber === 0 ) {
$this->pagesFound = $list;
}
} else {
$chunks = array_chunk( $list, $itemsPerPage );
if ( isset( $chunks[$realPageNumber] ) ) {
$this->pagesFound = $chunks[$realPageNumber];
}
}
}
}
/**
* Paginator
*
* Paginates search results according to per-page setting.
*
* @since 1.0.0
* @access public
* @return void
*/
public function paginator() {
// Access global variables.
global $numberOfItems;
$webhook = 'search';
if ( $this->webhook( $webhook, false, false ) ) {
/**
* Get the pre-defined variable from
* bl-kernal/boot/rules/99.paginator.php`.
*
* Is necessary to change this variable to fit the
* paginator with the result from the search.
*/
$numberOfItems = $this->numberOfItems;
}
}
/**
* Before site load
*
* @since 1.0.0
* @access public
* @return void
*/
public function beforeSiteLoad() {
// Access global variables.
global $content, $url, $WHERE_AM_I;
$webhook = 'search';
if ( $this->webhook( $webhook, false, false ) ) {
$WHERE_AM_I = 'search';
$content = [];
foreach ( $this->pagesFound as $key ) {
try {
$page = new Page( $key );
array_push( $content, $page );
} catch ( Exception $e ) {
// Continue.
}
}
}
}
/**
* Generate cache file
*
* Necessary to call it when you create, edit or remove content.
*
* @since 1.0.0
* @access private
* @global object $pages The Pages class.
* @return void
*/
private function create_cache() {
// Access global variables.
global $pages;
// Get list of published pages.
$list = $pages->getList(
$pageNumber = 1,
$numberOfItems = -1,
$published = true,
$static = true,
$sticky = true,
$draft = false,
$scheduled = false
);
$cache = [];
// Get page object for each published.
foreach ( $list as $key ) {
$page = buildPage( $key );
/**
* Process content
*
* Assuming average characters per word is 5.
*/
$words = $this->cache_words() * 5;
$content = $page->content();
$content = Text :: removeHTMLTags( $content );
$content = Text :: truncate( $content, $words, '' );
// Include the page to the cache.
$cache[$key]['title'] = $page->title();
$cache[$key]['description'] = $page->description();
$cache[$key]['content'] = $content;
}
// Generate JSON file with the cache.
$json = json_encode( $cache );
return file_put_contents( $this->cache_file(), $json, LOCK_EX );
}
/**
* Cache file
*
* Returns the absolute path of the cache file.
*
* @since 1.0.0
* @access private
* @return void
*/
private function cache_file() {
return $this->workspace() . 'cache.json';
}
/**
* Get search results
*
* Searches text inside the cache, sorted by score.
*
* @since 1.0.0
* @access private
* @param string $text
* @return array Returns an array with the pages keys
* related to the text.
*/
private function search_results( $text ) {
// Read the cache file.
$json = file_get_contents( $this->cache_file() );
$cache = json_decode( $json, true );
$search = new SearchForms\Search_Results( $cache, 10, 1, true );
$results = $search->search( $text, $this->min_chars() );
return array_keys( $results );
}
// @return integer
public function min_chars() {
return $this->getValue( 'min_chars' );
}
// @return integer
public function cache_words() {
return $this->getValue( 'cache_words' );
}
// @return boolean
public function in_sidebar() {
return $this->getValue( 'in_sidebar' );
}
// @return boolean
public function wrap() {
return $this->getValue( 'wrap' );
}
// @return string
public function label() {
return $this->getValue( 'label' );
}
// @return string
public function label_wrap() {
return strtolower( $this->getValue( 'label_wrap' ) );
}
// @return string
public function placeholder() {
return $this->getValue( 'placeholder' );
}
// @return boolean
public function button() {
return $this->getValue( 'button' );
}
// @return string
public function button_text() {
return $this->getValue( 'button_text' );
}
// @return string
public function button_class() {
return $this->getValue( 'button_class' );
}
/**
* Custom hook
*
* Prints the sidebar search form by
* calling the `search_form' hook.
*
* @since 1.0.0
* @access private
* @return string Returns the form markup.
*/
public function search_form() {
return sidebar_search();
}
}