PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 2.3.7
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v2.3.7
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.3.7, at includes/class-betterdocs.php

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