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

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