PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 2.2.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v2.2.0
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / class-betterdocs.php

class-betterdocs.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 2.2.0, at includes/class-betterdocs.php

461 lines 14.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The file that defines the core plugin class
5 *
6 * A class definition that includes attributes and functions used across both the
7 * public-facing side of the site and the admin area.
8 *
9 * @link https://wpdeveloper.com
10 * @since 1.0.0
11 *
12 * @package BetterDocs
13 * @subpackage BetterDocs/includes
14 */
15
16 /**
17 * The core plugin class.
18 *
19 * This is used to define internationalization, admin-specific hooks, and
20 * public-facing site hooks.
21 *
22 * Also maintains the unique identifier of this plugin as well as the current
23 * version of the plugin.
24 *
25 * @since 1.0.0
26 * @package BetterDocs
27 * @subpackage BetterDocs/includes
28 * @author WPDeveloper <support@wpdeveloper.com>
29 */
30 class BetterDocs
31 {
32
33 /**
34 * The loader that's responsible for maintaining and registering all hooks that power
35 * the plugin.
36 *
37 * @since 1.0.0
38 * @access protected
39 * @var BetterDocs_Loader $loader Maintains and registers all hooks for the plugin.
40 */
41 protected $loader;
42
43 /**
44 * The unique identifier of this plugin.
45 *
46 * @since 1.0.0
47 * @access protected
48 * @var string $plugin_name The string used to uniquely identify this plugin.
49 */
50 protected $plugin_name;
51
52 /**
53 * The current version of the plugin.
54 *
55 * @since 1.0.0
56 * @access protected
57 * @var string $version The current version of the plugin.
58 */
59 protected $version;
60
61 /**
62 * Define the core functionality of the plugin.
63 *
64 * Set the plugin name and the plugin version that can be used throughout the plugin.
65 * Load the dependencies, define the locale, and set the hooks for the admin area and
66 * the public-facing side of the site.
67 *
68 * @since 1.0.0
69 */
70 public function __construct()
71 {
72 if (defined('BETTERDOCS_VERSION')) {
73 $this->version = BETTERDOCS_VERSION;
74 } else {
75 $this->version = '1.0.0';
76 }
77 $this->plugin_name = 'betterdocs';
78 $this->db();
79 add_action('init', array($this, 'search_migration'));
80 $this->load_dependencies();
81 $this->set_locale();
82 $this->start_plugin_tracking();
83 $this->define_admin_hooks();
84 $this->define_public_hooks();
85 if (is_admin()) {
86 new Betterdocs_Role_Management_Lite();
87 }
88 add_action('admin_init', array($this, 'redirect'));
89 add_action('wp_ajax_optin_wizard_action_betterdocs', array($this, 'wizard_action'));
90 }
91
92 /**
93 * Load the required dependencies for this plugin.
94 *
95 * Include the following files that make up the plugin:
96 *
97 * - BetterDocs_Loader. Orchestrates the hooks of the plugin.
98 * - BetterDocs_i18n. Defines internationalization functionality.
99 * - BetterDocs_Admin. Defines all hooks for the admin area.
100 * - BetterDocs_Public. Defines all hooks for the public side of the site.
101 *
102 * Create an instance of the loader which will be used to register the hooks
103 * with WordPress.
104 *
105 * @since 1.0.0
106 * @access private
107 */
108 private function load_dependencies()
109 {
110 /**
111 * Quick Setup Wizard
112 */
113 require_once plugin_dir_path(dirname(__FILE__)) . 'admin/setup-wizard/betterdocs-setup-wizard-config.php';
114 /**
115 * The class responsible for orchestrating the actions and filters of the
116 * core plugin.
117 */
118 require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-betterdocs-loader.php';
119
120 /**
121 * The class responsible for defining internationalization functionality
122 * of the plugin.
123 */
124 require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-betterdocs-i18n.php';
125 require_once BETTERDOCS_DIR_PATH . 'includes/class-betterdocs-usage-tracker.php';
126 /**
127 * The class responsible for defining all actions that occur in the admin area.
128 */
129 require_once BETTERDOCS_ADMIN_DIR_PATH . 'class-betterdocs-admin.php';
130 require_once BETTERDOCS_ADMIN_DIR_PATH . 'class-betterdocs-admin-screen.php';
131 require_once BETTERDOCS_ADMIN_DIR_PATH . 'partials/class-betterdocs-list-table.php';
132 require_once BETTERDOCS_ADMIN_DIR_PATH . 'includes/class-betterdocs-db.php';
133 require_once BETTERDOCS_ADMIN_DIR_PATH . 'includes/class-betterdocs-metabox.php';
134 require_once BETTERDOCS_ADMIN_DIR_PATH . 'includes/class-betterdocs-settings.php';
135 require_once BETTERDOCS_ADMIN_DIR_PATH . 'includes/class-betterdocs-role-management-lite.php';
136 require_once BETTERDOCS_ADMIN_DIR_PATH . 'reports/class-betterdocs-email-template.php';
137 require_once BETTERDOCS_ADMIN_DIR_PATH . 'reports/class-betterdocs-report-email.php';
138
139 /**
140 * Notice Messages
141 */
142 require_once BETTERDOCS_ADMIN_DIR_PATH . 'includes/class-betterdocs-notice.php';
143
144 /**
145 * The class responsible for defining all actions that occur in the public-facing
146 * side of the site.
147 */
148 require_once plugin_dir_path(dirname(__FILE__)) . 'public/class-betterdocs-public.php';
149
150 /**
151 * The functions responsible for betterdocs helpers
152 */
153 require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-betterdocs-helpers.php';
154
155 /**
156 * The class responsible for registering docs post type and it's category and tags taxonomy
157 */
158 require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-betterdocs-docs-post-type.php';
159
160 /**
161 * The functions responsible for betterdocs shortcodes
162 */
163 require_once plugin_dir_path(dirname(__FILE__)) . 'public/betterdocs-shortcodes.php';
164
165 /**
166 * The functions responsible for betterdocs breadcrumbs
167 */
168 require_once plugin_dir_path(dirname(__FILE__)) . 'public/betterdocs-breadcrumbs.php';
169
170 /**
171 * The functions responsible for betterdocs customizer
172 */
173 require_once plugin_dir_path(dirname(__FILE__)) . 'admin/customizer/customizer.php';
174 require_once plugin_dir_path(dirname(__FILE__)) . 'admin/customizer/defaults.php';
175
176 /**
177 * The class responsible for registering widget in elementor and extend single page functionality
178 */
179 require_once plugin_dir_path(dirname(__FILE__)) . 'includes/elementor/class-betterdocs-elementor.php';
180
181 /**
182 * The class responsible for registering widget in elementor and extend single page functionality
183 */
184 require_once plugin_dir_path(dirname(__FILE__)) . 'includes/gutenberg/class-betterdocs-gutenberg.php';
185 require_once plugin_dir_path(dirname(__FILE__)) . 'includes/gutenberg/class-style-handler.php';
186
187 $this->loader = new BetterDocs_Loader();
188 }
189
190 public static function db() {
191 global $wpdb;
192 $installed_ver = get_site_option( "betterdocs_db_version" );
193 if ( $installed_ver != BETTERDOCS_DB_VERSION ) {
194 $search_keyword_table = $wpdb->prefix . 'betterdocs_search_keyword';
195 $search_keyword = "CREATE TABLE $search_keyword_table (
196 id bigint NOT NULL AUTO_INCREMENT,
197 keyword text NOT NULL,
198 PRIMARY KEY (id)
199 )";
200
201 $search_log_table = $wpdb->prefix . 'betterdocs_search_log';
202 $search_log = "CREATE TABLE $search_log_table (
203 id bigint NOT NULL AUTO_INCREMENT,
204 keyword_id bigint NOT NULL,
205 count mediumint(9) NULL,
206 not_found_count mediumint(9) NULL,
207 created_at date DEFAULT '0000-00-00' NOT NULL,
208 PRIMARY KEY (id),
209 KEY keyword_id (keyword_id),
210 KEY created_at (created_at)
211 )";
212
213 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
214
215 dbDelta( $search_keyword );
216 dbDelta( $search_log );
217
218 update_option( "betterdocs_db_version", BETTERDOCS_DB_VERSION );
219 }
220 }
221
222 public static function search_migration() {
223 global $wpdb;
224 if (get_site_option( "betterdocs_search_data_migration" ) == false) {
225 $search_data = get_site_option( 'betterdocs_search_data' );
226 if (!empty($search_data)) {
227 $search_data_arr = unserialize($search_data);
228 foreach ($search_data_arr as $key=>$value) {
229 $args = array(
230 'post_type' => 'docs',
231 'post_status' => 'publish',
232 'posts_per_page' => -1,
233 'suppress_filters' => true,
234 's' => $key
235 );
236
237 $loop = new WP_Query($args);
238 if ($loop->have_posts()) {
239 $count = $value;
240 $not_found_count = 0;
241 } else {
242 $count = 0;
243 $not_found_count = $value;
244 }
245
246 $keyword = $wpdb->get_var(
247 $wpdb->prepare( "
248 SELECT keyword
249 FROM {$wpdb->prefix}betterdocs_search_keyword
250 WHERE keyword = %s",
251 $key
252 )
253 );
254
255 if ( $keyword == NUll ) {
256 $insert = $wpdb->query(
257 $wpdb->prepare(
258 "INSERT INTO {$wpdb->prefix}betterdocs_search_keyword
259 ( keyword )
260 VALUES ( %s )",
261 array(
262 $key
263 )
264 )
265 );
266
267 if ($insert) {
268 $wpdb->query(
269 $wpdb->prepare(
270 "INSERT INTO {$wpdb->prefix}betterdocs_search_log
271 (keyword_id, count, not_found_count, created_at)
272 VALUES (%d, %d, %d, %s)",
273 array(
274 $wpdb->insert_id,
275 $count,
276 $not_found_count,
277 date('Y-m-d')
278 )
279 )
280 );
281 }
282 }
283 }
284 update_option( "betterdocs_search_data_migration", '1.0' );
285 }
286 }
287 }
288
289 /**
290 * Optional usage tracker
291 *
292 * @since v1.0.0
293 */
294 public function start_plugin_tracking()
295 {
296 $tracker = BetterDocs_Plugin_Usage_Tracker::get_instance(BETTERDOCS_FILE, [
297 'opt_in' => true,
298 'goodbye_form' => true,
299 'item_id' => 'c7b16777b4f1b83f6083'
300 ]);
301 $tracker->set_notice_options(array(
302 'notice' => __('Want to help make <strong>BetterDocs</strong> even more awesome? You can get a <strong>10% discount coupon</strong> for Premium extensions if you allow us to track the usage.', 'betterdocs'),
303 'extra_notice' => __('We collect non-sensitive diagnostic data and plugin usage information.
304 Your site URL, WordPress & PHP version, plugins & themes and email address to send you the
305 discount coupon. This data lets us make sure this plugin always stays compatible with the most
306 popular plugins and themes. No spam, I promise.', 'betterdocs'),
307 ));
308 $tracker->init();
309 }
310
311 public function wizard_action()
312 {
313 if (!check_ajax_referer('betterdocsqswnonce', 'nonce')) {
314 return;
315 }
316 if ($this->do_wizard_tracking(true, $_POST)) {
317 wp_send_json_success('done');
318 }
319 wp_send_json_error('Something went wrong.');
320 }
321
322 public function do_wizard_tracking($force = false, $data = [])
323 {
324 if (!class_exists('BetterDocs_Plugin_Usage_Tracker')) {
325 require_once BETTERDOCS_DIR_PATH . 'includes/class-betterdocs-usage-tracker.php';
326 }
327 $tracker = BetterDocs_Plugin_Usage_Tracker::get_instance(BETTERDOCS_FILE, [
328 'opt_in' => true,
329 'goodbye_form' => true,
330 'item_id' => 'c7b16777b4f1b83f6083'
331 ]);
332 // If the home site hasn't been defined, we just drop out. Nothing much we can do.
333 if (empty($tracker::API_URL)) {
334 return false;
335 }
336 // Get our data
337 $body = $tracker->get_data();
338 if (isset($data['admin_email']) && !empty($data['admin_email'])) {
339 $body['email'] = $data['admin_email'];
340 }
341 // Send the data
342 return $tracker->send_data($body);
343 }
344
345 /**
346 * Define the locale for this plugin for internationalization.
347 *
348 * Uses the BetterDocs_i18n class in order to set the domain and to register the hook
349 * with WordPress.
350 *
351 * @since 1.0.0
352 * @access private
353 */
354 private function set_locale()
355 {
356 $plugin_i18n = new BetterDocs_i18n();
357 $this->loader->add_action('plugins_loaded', $plugin_i18n, 'load_plugin_textdomain');
358 }
359
360 /**
361 * Register all of the hooks related to the admin area functionality
362 * of the plugin.
363 *
364 * @since 1.0.0
365 * @access private
366 */
367 private function define_admin_hooks()
368 {
369 $plugin_admin = new BetterDocs_Admin($this->get_plugin_name(), $this->get_version());
370 add_action('admin_enqueue_scripts', array($plugin_admin, 'enqueue_styles'));
371 add_action('admin_enqueue_scripts', array($plugin_admin, 'enqueue_scripts'));
372 add_action('admin_bar_menu', array($plugin_admin, 'toolbar_menu'), 32);
373
374 $this->loader->add_filter('admin_body_class', $plugin_admin, 'body_classes');
375 $admin_screen = new Betterdocs_Admin_Screen();
376 $this->loader->add_action('betterdocs_listing_header', $admin_screen, 'header_template');
377
378 BetterDocs_Settings::init();
379 }
380
381 /**
382 * Register all of the hooks related to the public-facing functionality
383 * of the plugin.
384 *
385 * @since 1.0.0
386 * @access private
387 */
388 private function define_public_hooks()
389 {
390 $plugin_public = new BetterDocs_Public($this->get_plugin_name(), $this->get_version());
391 $this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'load_assets');
392 // $this->loader->add_action('enqueue_block_assets', $plugin_public, 'load_assets');
393 }
394
395 /**
396 * Run the loader to execute all of the hooks with WordPress.
397 *
398 * @since 1.0.0
399 */
400 public function run()
401 {
402 $this->loader->run();
403 }
404
405 /**
406 * The name of the plugin used to uniquely identify it within the context of
407 * WordPress and to define internationalization functionality.
408 *
409 * @since 1.0.0
410 * @return string The name of the plugin.
411 */
412 public function get_plugin_name()
413 {
414 return $this->plugin_name;
415 }
416
417 /**
418 * The reference to the class that orchestrates the hooks with the plugin.
419 *
420 * @since 1.0.0
421 * @return BetterDocs_Loader Orchestrates the hooks of the plugin.
422 */
423 public function get_loader()
424 {
425 return $this->loader;
426 }
427
428 /**
429 * Retrieve the version number of the plugin.
430 *
431 * @since 1.0.0
432 * @return string The version number of the plugin.
433 */
434 public function get_version()
435 {
436 return $this->version;
437 }
438
439 public function is_pro_active()
440 {
441 return defined('BETTERDOCS_PRO_VERSION');
442 }
443
444 public function redirect()
445 {
446 // Bail if no activation transient is set.
447 if (!get_transient('_betterdocs_meta_activation_notice')) {
448 return;
449 }
450 // Delete the activation transient.
451 delete_transient('_betterdocs_meta_activation_notice');
452
453 if (!is_multisite()) {
454 // Redirect to the welcome page.
455 wp_safe_redirect(add_query_arg(array(
456 'page' => 'betterdocs-setup'
457 ), admin_url('admin.php?page=betterdocs-setup')));
458 }
459 }
460 }
461