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