-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-knowledgebase.php
More file actions
506 lines (371 loc) · 12.2 KB
/
wp-knowledgebase.php
File metadata and controls
506 lines (371 loc) · 12.2 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
<?php
/**
* Plugin Name: WP Knowledgebase
* Plugin URI: http://wordpress.org/plugins/wp-knowledgebase
* Description: Simple and flexible knowledgebase plugin for WordPress
* Version: 2.0.1
* Author: SurfHost
* Author URI: https://surfhost.nl
* Text Domain: wp-knowledgebase
* License: GPL2
*
* == Copyright ==
* Copyright 2020 Mihai Iova, 2026 SurfHost
*
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Main plugin class
*
*/
Class WP_Knowledgebase {
/**
* Whether to run the update procedure or not
*
* @access private
* @var bool
*
*/
private $do_update = false;
/**
* The current object instance
*
* @access private
* @var WP_Knowledgebase
*
*/
private static $instance;
/**
* A list with the objects that handle database requests
*
* @access public
* @var array
*
*/
public $db = array();
/**
* Constructor
*
*/
public function __construct() {
// Defining new constants
define( 'KBE_PLUGIN_VERSION', '2.0.1' );
define( 'KBE_BASENAME', plugin_basename( __FILE__ ) );
define( 'KBE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'KBE_PLUGIN_DIR_URL', plugin_dir_url( __FILE__ ) );
$this->include_files();
$this->load_db_layer();
// Defining legacy constants
define( 'WP_KNOWLEDGEBASE', plugin_dir_url( __FILE__ ) );
define( 'KBE_ARTICLE_QTY', get_option( 'kbe_article_qty' ) );
define( 'KBE_PLUGIN_SLUG', get_option( 'kbe_plugin_slug' ) );
define( 'KBE_CATEGORY_SLUG', get_option( 'kbe_category_slug', 'knowledgebase_category' ) );
define( 'KBE_SEARCH_SETTING', get_option( 'kbe_search_setting' ) );
define( 'KBE_BREADCRUMBS_SETTING', get_option( 'kbe_breadcrumbs_setting' ) );
define( 'KBE_SIDEBAR_HOME', get_option( 'kbe_sidebar_home' ) );
define( 'KBE_SIDEBAR_INNER', get_option( 'kbe_sidebar_inner' ) );
define( 'KBE_COMMENT_SETTING', get_option( 'kbe_comments_setting' ) );
define( 'KBE_BG_COLOR', get_option( 'kbe_bgcolor' ) );
define( 'KBE_LINK_STRUCTURE', get_option( 'permalink_structure' ) );
define( 'KBE_POST_TYPE', 'kbe_knowledgebase' );
define( 'KBE_POST_TAXONOMY', 'kbe_taxonomy' );
define( 'KBE_POST_TAGS', 'kbe_tags' );
define( 'KBE_PAGE_TITLE', kbe_get_knowledgebase_page_id() );
// Add submenu pages
add_action( 'admin_menu', array( $this, 'load_admin_submenu_pages' ), 9 );
// Load plugin textdomain
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ), 15 );
// Verifies if an update is needed
add_action( 'plugins_loaded', array( $this, 'set_do_update' ), 15 );
// Check if just updated
add_action( 'plugins_loaded', array( $this, 'update_check' ), 20 );
// Update the database tables
add_action( 'kbe_update_check', array( $this, 'update_database_tables' ) );
// Remove plugin query args from the URL
add_filter( 'removable_query_args', array( $this, 'removable_query_args' ) );
// Add extra action links to the plugin in Plugins list table
add_filter( 'plugin_action_links_' . KBE_BASENAME, array( $this, 'add_plugin_action_links' ) );
// Set and unset cron jobs
register_activation_hook( __FILE__, array( $this, 'set_cron_jobs' ) );
register_deactivation_hook( __FILE__, array( $this, 'unset_cron_jobs' ) );
// Set fresh install transient
register_activation_hook( __FILE__, array( $this, 'set_activation_transient' ) );
/**
* Plugin initialized
*
*/
do_action( 'kbe_initialized' );
}
/**
* Returns an instance of the plugin object
*
* @return WP_Knowledgebase
*
*/
public static function instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WP_Knowledgebase ) )
self::$instance = new WP_Knowledgebase;
return self::$instance;
}
/**
* Loads plugin text domain
*
*/
public function load_textdomain() {
$locale = apply_filters( 'plugin_locale', get_locale(), 'wp-knowledgebase' );
// Load textdomain
load_textdomain( 'wp-knowledgebase', WP_LANG_DIR . '/wp-knowledgebase/wp-knowledgebase-' . $locale . '.mo' );
$current_theme = wp_get_theme();
if( ! empty( $current_theme->stylesheet ) && file_exists( get_theme_root() . '/' . $current_theme->stylesheet . '/wp_knowledgebase/languages/' ) )
load_plugin_textdomain( 'wp-knowledgebase', false, plugin_basename( dirname( __FILE__ ) ) . '/../../themes/' . $current_theme->stylesheet . '/wp_knowledgebase/languages/' );
else
load_plugin_textdomain( 'wp-knowledgebase', false, plugin_basename( dirname( __FILE__ ) ) . '/languages/' );
}
/**
* Include files
*
* @return void
*
*/
public function include_files() {
/**
* Include abstract classes
*
*/
$abstract_files = glob( KBE_PLUGIN_DIR . 'includes/abstracts/abstract-*.php' );
foreach( $abstract_files as $abstract_file ) {
include $abstract_file;
}
/**
* Include all functions.php files from all plugin folders
*
*/
$this->_recursively_include_files( KBE_PLUGIN_DIR . 'includes' );
// Require File kbe_articles.php
require 'includes/kbe-articles.php';
require 'includes/kbe-core-functions.php';
// Require templates
require 'includes/kbe-template-functions.php';
require 'includes/kbe-template-hooks.php';
// Require AJAX actions
require 'includes/functions-actions-ajax.php';
// Require deprecated functions
require 'includes/kbe-functions-deprecated.php';
// Require Category Widget file
require 'includes/widgets/kbe-widget-category.php';
// Require Articles Widget file
require 'includes/widgets/kbe-widget-article.php';
// Require Search Articles Widget file
require 'includes/widgets/kbe-widget-search.php';
// Require Tags Widget file
require 'includes/widgets/kbe-widget-tags.php';
// Include update checker
require 'includes/class-update-checker.php';
// Include admin file(s)
if( is_admin() ) {
require 'includes/admin/kbe-admin-functions.php';
require 'includes/admin/class-plugins-screen-updates.php';
require 'includes/admin/functions-actions-ajax.php';
}
// Include migrations
require 'includes/migrations/class-abstract-migration.php';
require 'includes/migrations/migration-install.php';
// Include the deactivation class
require 'includes/class-deactivation.php';
/**
* Include theme compatibility file
*
*/
$active_theme = sanitize_file_name( get_option( 'template' ) );
if( file_exists( KBE_PLUGIN_DIR . 'includes/theme-support/' . $active_theme . '.php' ) )
require KBE_PLUGIN_DIR . 'includes/theme-support/' . $active_theme . '.php';
/**
* Helper hook to include files early
*
*/
do_action( 'kbe_include_files' );
}
/**
* Recursively includes all functions.php files from the given directory path
*
* @param string $dir_path
*
*/
protected function _recursively_include_files( $dir_path ) {
$folders = array_filter( glob( $dir_path . '/*' ), 'is_dir' );
foreach( $folders as $folder_path ) {
if( file_exists( $folder_path . '/functions.php' ) )
include $folder_path . '/functions.php';
$this->_recursively_include_files( $folder_path );
}
}
/**
* Sets up all objects that handle database related requests and adds them to the
* $db property of the app
*
*/
public function load_db_layer() {
/**
* Hook to register db class handlers
* The array element should be 'class_slug' => 'class_name'
*
* @param array
*
*/
$db_classes = apply_filters( 'kbe_register_database_classes', array() );
if( empty( $db_classes ) )
return;
foreach( $db_classes as $db_class_slug => $db_class_name ) {
$this->db[$db_class_slug] = new $db_class_name;
}
}
/**
* Sets up all objects that handle submenu pages and adds them to the
* $submenu_pages property of the app
*
*/
public function load_admin_submenu_pages() {
/**
* Hook to register submenu_pages class handlers
* The array element should be 'submenu_page_slug' => array( 'class_name' => array(), 'data' => array() )
*
* @param array
*
*/
$submenu_pages = apply_filters( 'kbe_register_submenu_page', array() );
if( empty( $submenu_pages ) )
return;
foreach( $submenu_pages as $submenu_page_slug => $submenu_page ) {
if( empty( $submenu_page['data'] ) )
continue;
if( empty( $submenu_page['data']['page_title'] ) || empty( $submenu_page['data']['menu_title'] ) || empty( $submenu_page['data']['capability'] ) || empty( $submenu_page['data']['menu_slug'] ) )
continue;
$this->submenu_pages[$submenu_page['data']['menu_slug']] = new $submenu_page['class_name']( $submenu_page['data']['page_title'], $submenu_page['data']['menu_title'], $submenu_page['data']['capability'], $submenu_page['data']['menu_slug'], ( isset( $submenu_page['data']['position'] ) ? $submenu_page['data']['position'] : null ) );
}
}
/**
* Verifies whether the update procedure should run or not.
*
*/
public function set_do_update() {
$db_version = get_option( 'kbe_version', '' );
// If current version number differs from saved version number
if( $db_version != KBE_PLUGIN_VERSION ) {
$this->do_update = true;
}
/**
* Filter to change do_update if needed
*
* @param bool
*
*/
$this->do_update = apply_filters( 'kbe_update_check_do_update', $this->do_update );
}
/**
* Checks to see if the current version of the plugin matches the version
* saved in the database
*
* @return void
*
*/
public function update_check() {
if( ! $this->do_update )
return;
$db_version = get_option( 'kbe_version', '' );
// Update the version number in the DB
update_option( 'kbe_version', KBE_PLUGIN_VERSION );
// Update the previous version number in the DB
if( ! empty( $db_version ) ) {
update_option( 'kbe_previous_version', $db_version );
}
// Add first activation time
if( get_option( 'kbe_first_activation', '' ) == '' ) {
update_option( 'kbe_first_activation', time() );
/**
* Hook for first time activation
*
*/
do_action( 'kbe_first_activation', $db_version );
}
/**
* Hook for fresh update
*
*/
do_action( 'kbe_update_check', $db_version );
// Trigger set cron jobs
$this->set_cron_jobs();
}
/**
* Creates and updates the database tables
*
* @return void
*
*/
public function update_database_tables() {
foreach( $this->db as $db_class ) {
$db_class->create_table();
}
}
/**
* Sets an action hook for modules to add custom schedules
*
*/
public function set_cron_jobs() {
do_action( 'kbe_set_cron_jobs' );
}
/**
* Sets an action hook for modules to remove custom schedules
*
*/
public function unset_cron_jobs() {
do_action( 'kbe_unset_cron_jobs' );
}
/**
* Sets a transient right at activation time
*
*/
public function set_activation_transient() {
set_transient( '_kbe_activated', 1, 60 );
}
/**
* Add extra action links in the plugins page
*
* @param array $links
*
* @return array
*
*/
public function add_plugin_action_links( $links ) {
array_unshift( $links, '<a href="' . esc_url( add_query_arg( array( 'post_type' => KBE_POST_TYPE, 'page' => 'kbe-settings' ), admin_url( 'edit.php' ) ) ) . '">' . __( 'Settings', 'wp-knowledgebase' ) . '</a>' );
return $links;
}
/**
* Removes the query variables from the URL upon page load
*
*/
public function removable_query_args( $args = array() ) {
$args[] = 'kbe_message';
return $args;
}
}
/**
* Main plugin function
*
*/
function wp_knowledgebase() {
return WP_Knowledgebase::instance();
}
// Let's get this party started
wp_knowledgebase();