| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The admin-specific functionality of the plugin. |
| 5 |
* |
| 6 |
* @link https://wpdeveloper.com |
| 7 |
* @since 1.0.0 |
| 8 |
* |
| 9 |
* @package BetterDocs |
| 10 |
* @subpackage BetterDocs/admin |
| 11 |
* @author WPDeveloper <support@wpdeveloper.com> |
| 12 |
*/ |
| 13 |
|
| 14 |
class BetterDocs_Admin |
| 15 |
{ |
| 16 |
|
| 17 |
/** |
| 18 |
* The ID of this plugin. |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
* @access private |
| 22 |
* @var string $plugin_name The ID of this plugin. |
| 23 |
*/ |
| 24 |
private $plugin_name; |
| 25 |
private $menu_slug; |
| 26 |
/** |
| 27 |
* All builder args |
| 28 |
* |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
private $builder_args; |
| 32 |
/** |
| 33 |
* Builder Metabox ID |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
private $metabox_id; |
| 38 |
|
| 39 |
/** |
| 40 |
* The version of this plugin. |
| 41 |
* |
| 42 |
* @since 1.0.0 |
| 43 |
* @access private |
| 44 |
* @var string $version The current version of this plugin. |
| 45 |
*/ |
| 46 |
private $version; |
| 47 |
|
| 48 |
/** |
| 49 |
* The type. |
| 50 |
* |
| 51 |
* @since 1.0.0 |
| 52 |
* @access public |
| 53 |
* @var string the post type of betterdocs. |
| 54 |
*/ |
| 55 |
public $type = 'docs'; |
| 56 |
|
| 57 |
public $metabox; |
| 58 |
|
| 59 |
public static $prefix = 'betterdocs_meta_'; |
| 60 |
|
| 61 |
public static $settings; |
| 62 |
|
| 63 |
/** |
| 64 |
* Initialize the class and set its properties. |
| 65 |
* |
| 66 |
* @since 1.0.0 |
| 67 |
* @param string $plugin_name The name of this plugin. |
| 68 |
* @param string $version The version of this plugin. |
| 69 |
*/ |
| 70 |
public static $counts; |
| 71 |
|
| 72 |
public static $enabled_types = []; |
| 73 |
public static $active_items = []; |
| 74 |
|
| 75 |
public function __construct($plugin_name, $version) |
| 76 |
{ |
| 77 |
|
| 78 |
$this->plugin_name = $plugin_name; |
| 79 |
$this->version = $version; |
| 80 |
$this->metabox = new BetterDocs_MetaBox(); |
| 81 |
self::$settings = BetterDocs_DB::get_settings(); |
| 82 |
add_action('wp_ajax_betterdocs_dark_mode', array($this, 'dark_mode')); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Register the stylesheets for the admin area. |
| 87 |
* |
| 88 |
* @since 1.0.0 |
| 89 |
*/ |
| 90 |
public function enqueue_styles($hook) |
| 91 |
{ |
| 92 |
wp_enqueue_style( |
| 93 |
$this->plugin_name . '-admin-global', |
| 94 |
BETTERDOCS_ADMIN_URL . 'assets/css/betterdocs-global.css', |
| 95 |
array(), |
| 96 |
$this->version, |
| 97 |
'all' |
| 98 |
); |
| 99 |
|
| 100 |
$tax = function_exists('get_current_screen') ? get_current_screen() : ''; |
| 101 |
if (!in_array($hook, array('toplevel_page_betterdocs-admin', 'betterdocs_page_betterdocs-settings', 'betterdocs_page_betterdocs-analytics'))) { |
| 102 |
if ($tax->taxonomy !== 'doc_category') { |
| 103 |
return; |
| 104 |
} else { |
| 105 |
return; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
wp_enqueue_style( |
| 110 |
$this->plugin_name . '-select2', |
| 111 |
BETTERDOCS_ADMIN_URL . 'assets/css/select2.min.css', |
| 112 |
array(), |
| 113 |
$this->version, |
| 114 |
'all' |
| 115 |
); |
| 116 |
|
| 117 |
wp_enqueue_style( |
| 118 |
'daterangepicker', |
| 119 |
BETTERDOCS_ADMIN_URL . 'assets/css/daterangepicker.css', |
| 120 |
array(), |
| 121 |
$this->version, |
| 122 |
'all' |
| 123 |
); |
| 124 |
|
| 125 |
wp_enqueue_style( |
| 126 |
$this->plugin_name, |
| 127 |
BETTERDOCS_ADMIN_URL . 'assets/css/betterdocs-admin.css', |
| 128 |
array(), |
| 129 |
$this->version, |
| 130 |
'all' |
| 131 |
); |
| 132 |
} |
| 133 |
/** |
| 134 |
* Register the JavaScript for the admin area. |
| 135 |
* |
| 136 |
* @since 1.0.0 |
| 137 |
*/ |
| 138 |
public function enqueue_scripts($hook) |
| 139 |
{ |
| 140 |
wp_enqueue_script( |
| 141 |
'betterdocs-admin-global', |
| 142 |
BETTERDOCS_ADMIN_URL . 'assets/js/admin-global.js', |
| 143 |
array('jquery'), |
| 144 |
$this->version, |
| 145 |
true |
| 146 |
); |
| 147 |
|
| 148 |
wp_localize_script( |
| 149 |
'betterdocs-admin-global', |
| 150 |
'betterdocs_admin', |
| 151 |
array( |
| 152 |
'menu_title' => __('Switch to BetterDocs UI', 'betterdocs'), |
| 153 |
'site_address' => get_bloginfo('url'), |
| 154 |
'betterdocs_pro_plugin' => in_array('betterdocs-pro/betterdocs-pro.php', apply_filters('active_plugins', get_option('active_plugins'))) ? 'true' : 'false' |
| 155 |
) |
| 156 |
); |
| 157 |
|
| 158 |
$tax = function_exists('get_current_screen') ? get_current_screen() : ''; |
| 159 |
if (!in_array($hook, array('toplevel_page_betterdocs-admin', 'betterdocs_page_betterdocs-settings', 'betterdocs_page_betterdocs-analytics', 'betterdocs_page_betterdocs-setup'))) { |
| 160 |
if ($tax->taxonomy !== 'doc_category') { |
| 161 |
return; |
| 162 |
} else { |
| 163 |
return; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
wp_enqueue_script('wp-color-picker'); |
| 168 |
|
| 169 |
wp_enqueue_script( |
| 170 |
$this->plugin_name . '-select2', |
| 171 |
BETTERDOCS_ADMIN_URL . 'assets/js/select2.min.js', |
| 172 |
array('jquery'), |
| 173 |
$this->version, |
| 174 |
true |
| 175 |
); |
| 176 |
|
| 177 |
wp_enqueue_script( |
| 178 |
$this->plugin_name . '-sweetalert', |
| 179 |
BETTERDOCS_ADMIN_URL . 'assets/js/sweetalert.min.js', |
| 180 |
array('jquery'), |
| 181 |
$this->version, |
| 182 |
true |
| 183 |
); |
| 184 |
|
| 185 |
wp_enqueue_script( |
| 186 |
'moment', |
| 187 |
BETTERDOCS_ADMIN_URL . 'assets/js/moment.min.js', |
| 188 |
array('jquery'), |
| 189 |
$this->version, |
| 190 |
true |
| 191 |
); |
| 192 |
|
| 193 |
wp_enqueue_script( |
| 194 |
'daterangepicker', |
| 195 |
BETTERDOCS_ADMIN_URL . 'assets/js/daterangepicker.min.js', |
| 196 |
array('jquery'), |
| 197 |
$this->version, |
| 198 |
true |
| 199 |
); |
| 200 |
|
| 201 |
|
| 202 |
wp_enqueue_script( |
| 203 |
$this->plugin_name, |
| 204 |
BETTERDOCS_ADMIN_URL . 'assets/js/betterdocs-admin.js', |
| 205 |
array('jquery'), |
| 206 |
$this->version, |
| 207 |
true |
| 208 |
); |
| 209 |
|
| 210 |
$dark_mode = false; |
| 211 |
|
| 212 |
if (class_exists('BetterDocs_DB')) { |
| 213 |
$dark_mode = BetterDocs_DB::get_settings('dark_mode'); |
| 214 |
} |
| 215 |
|
| 216 |
wp_localize_script( |
| 217 |
$this->plugin_name, |
| 218 |
'betterdocs_admin', |
| 219 |
array( |
| 220 |
'ajaxurl' => admin_url('admin-ajax.php'), |
| 221 |
'doc_cat_order_nonce' => wp_create_nonce('doc_cat_order_nonce'), |
| 222 |
'knowledge_base_order_nonce' => wp_create_nonce('knowledge_base_order_nonce'), |
| 223 |
'paged' => isset($_GET['paged']) ? absint(wp_unslash($_GET['paged'])) : 0, |
| 224 |
'per_page_id' => "edit_{$tax->taxonomy}_per_page", |
| 225 |
'menu_title' => __('Switch to BetterDocs UI', 'betterdocs'), |
| 226 |
'dark_mode' => !empty($dark_mode) ? boolval($dark_mode) : false, |
| 227 |
'text' => esc_html__('Copied!', 'betterdocs'), |
| 228 |
) |
| 229 |
); |
| 230 |
|
| 231 |
wp_localize_script($this->plugin_name, 'betterdocsAdminConfig', self::toggleFields()); |
| 232 |
} |
| 233 |
|
| 234 |
public function body_classes($classes) |
| 235 |
{ |
| 236 |
$saved_settings = get_option('betterdocs_settings', false); |
| 237 |
$dark_mode = isset($saved_settings['dark_mode']) ? $saved_settings['dark_mode'] : false; |
| 238 |
$dark_mode = !empty($dark_mode) ? boolval($dark_mode) : false; |
| 239 |
if ($dark_mode === true) { |
| 240 |
$classes .= ' betterdocs-dark-mode '; |
| 241 |
} |
| 242 |
return $classes; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* AJAX Handler to update terms' tax position. |
| 247 |
*/ |
| 248 |
public function dark_mode() |
| 249 |
{ |
| 250 |
if (!check_ajax_referer('doc_cat_order_nonce', 'nonce', false)) { |
| 251 |
wp_send_json_error(); |
| 252 |
} |
| 253 |
|
| 254 |
if (isset($_POST['mode'])) { |
| 255 |
$saved_settings = BetterDocs_DB::get_settings(); |
| 256 |
$saved_settings['dark_mode'] = $_POST['mode']; |
| 257 |
|
| 258 |
if (BetterDocs_DB::update_settings($saved_settings)) { |
| 259 |
wp_send_json_success(); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
wp_send_json_error(); |
| 264 |
} |
| 265 |
|
| 266 |
public function toggleFields($builder = false) |
| 267 |
{ |
| 268 |
|
| 269 |
$args = BetterDocs_Settings::settings_args(); |
| 270 |
|
| 271 |
$toggleFields = $hideFields = $conditions = array(); |
| 272 |
|
| 273 |
$tabs = $args; |
| 274 |
if (!empty($tabs)) { |
| 275 |
foreach ($tabs as $tab_id => $tab) { |
| 276 |
$sections = isset($tab['sections']) ? $tab['sections'] : []; |
| 277 |
if (!empty($sections)) { |
| 278 |
foreach ($sections as $section_id => $section) { |
| 279 |
$fields = isset($section['fields']) ? $section['fields'] : []; |
| 280 |
if (isset($section['tabs']) && !empty($section['tabs'])) { |
| 281 |
foreach ($section['tabs'] as $inner_field_tab_key => $inner_field_tab) { |
| 282 |
if (isset($inner_field_tab['fields'])) { |
| 283 |
foreach ($inner_field_tab['fields'] as $inner_tab_field_key => $inner_tab_field_tab) { |
| 284 |
if (isset($inner_tab_field_tab['hide']) && !empty($inner_tab_field_tab['hide']) && is_array($inner_tab_field_tab['hide'])) { |
| 285 |
$hideFields = $this->a_walk($inner_tab_field_tab['hide'], $inner_tab_field_key, $hideFields); |
| 286 |
} |
| 287 |
if (isset($inner_tab_field_tab['dependency']) && !empty($inner_tab_field_tab['dependency']) && is_array($inner_tab_field_tab['dependency'])) { |
| 288 |
$conditions = $this->a_walk($inner_tab_field_tab['dependency'], $inner_tab_field_key, $conditions); |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
if (!empty($fields)) { |
| 295 |
foreach ($fields as $field_key => $field) { |
| 296 |
if (isset($field['fields'])) { |
| 297 |
$iFields = $field['fields']; |
| 298 |
foreach ($iFields as $inner_field_key => $inner_field) { |
| 299 |
if (isset($inner_field['hide']) && !empty($inner_field['hide']) && is_array($inner_field['hide'])) { |
| 300 |
$hideFields = $this->a_walk($inner_field['hide'], $inner_field_key, $hideFields); |
| 301 |
} |
| 302 |
if (isset($inner_field['dependency']) && !empty($inner_field['dependency']) && is_array($inner_field['dependency'])) { |
| 303 |
$conditions = $this->a_walk($inner_field['dependency'], $inner_field_key, $conditions); |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
if (isset($field['hide']) && !empty($field['hide']) && is_array($field['hide'])) { |
| 308 |
$hideFields = $this->a_walk($field['hide'], $field_key, $hideFields); |
| 309 |
} |
| 310 |
if (isset($field['dependency']) && !empty($field['dependency']) && is_array($field['dependency'])) { |
| 311 |
$conditions = $this->a_walk($field['dependency'], $field_key, $conditions); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
return array( |
| 321 |
'toggleFields' => $conditions, // TODO: toggling system has to be more optimized! |
| 322 |
'hideFields' => $hideFields, |
| 323 |
); |
| 324 |
} |
| 325 |
|
| 326 |
public function a_walk($array, $field_key, &$returned_array = []) |
| 327 |
{ |
| 328 |
array_walk($array, function ($value, $key) use ($field_key, &$returned_array) { |
| 329 |
$returned_array[$field_key][$key] = $value; |
| 330 |
}); |
| 331 |
|
| 332 |
return $returned_array; |
| 333 |
} |
| 334 |
|
| 335 |
public function quick_builder() |
| 336 |
{ |
| 337 |
$builder_args = $this->builder_args; |
| 338 |
$tabs = $this->builder_args['tabs']; |
| 339 |
$prefix = self::$prefix; |
| 340 |
$metabox_id = $this->metabox_id; |
| 341 |
/** |
| 342 |
* This lines of code is for editing a notification in simple|quick builder |
| 343 |
* |
| 344 |
* @var [type] |
| 345 |
*/ |
| 346 |
$idd = null; |
| 347 |
if (isset($_GET['post_id']) && !empty($_GET['post_id'])) { |
| 348 |
$idd = intval($_GET['post_id']); |
| 349 |
} |
| 350 |
include_once BETTERDOCS_ADMIN_DIR_PATH . 'partials/betterdocs-quick-builder-display.php'; |
| 351 |
} |
| 352 |
/** |
| 353 |
* Generate the builder data acording to default meta data |
| 354 |
* |
| 355 |
* @param array $data |
| 356 |
* @return array |
| 357 |
*/ |
| 358 |
protected function builder_data($data) |
| 359 |
{ |
| 360 |
$post_data = []; |
| 361 |
$prefix = self::$prefix; |
| 362 |
$meta_fields = BetterDocs_MetaBox::get_metabox_fields($prefix); |
| 363 |
foreach ($meta_fields as $meta_key => $meta_field) { |
| 364 |
if (in_array($meta_key, array_keys($data))) { |
| 365 |
$post_data[$meta_key] = $data[$meta_key]; |
| 366 |
} else { |
| 367 |
$post_data[$meta_key] = ''; |
| 368 |
|
| 369 |
if (isset($meta_field['defaults'])) { |
| 370 |
$post_data[$meta_key] = $meta_field['defaults']; |
| 371 |
} |
| 372 |
if (isset($meta_field['default'])) { |
| 373 |
$post_data[$meta_key] = $meta_field['default']; |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
return array_merge($post_data, $data); |
| 379 |
} |
| 380 |
|
| 381 |
public static function get_form_action($query_var = '', $builder_form = false) |
| 382 |
{ |
| 383 |
$page = '/edit.php?post_type=docs&page=betterdocs-settings'; |
| 384 |
|
| 385 |
if (is_network_admin()) { |
| 386 |
return network_admin_url($page . $query_var); |
| 387 |
} else { |
| 388 |
return admin_url($page . $query_var); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
/** |
| 394 |
* Admin Init For User Interactions |
| 395 |
* @return void |
| 396 |
*/ |
| 397 |
public function admin_init($hook) |
| 398 |
{ |
| 399 |
/** |
| 400 |
* BetterDocs Admin URL |
| 401 |
*/ |
| 402 |
$current_url = admin_url('edit.php?post_type=docs&page=betterdocs-settings'); |
| 403 |
} |
| 404 |
public function toolbar_menu($admin_bar) |
| 405 |
{ |
| 406 |
if (!is_admin() || !is_admin_bar_showing()) { |
| 407 |
return; |
| 408 |
} |
| 409 |
|
| 410 |
// Show only when the user is a member of this site, or they're a super admin. |
| 411 |
if (!is_user_member_of_blog() && !is_super_admin()) { |
| 412 |
return; |
| 413 |
} |
| 414 |
|
| 415 |
$saved_settings = BetterDocs_DB::get_settings(); |
| 416 |
$docs_url = ''; |
| 417 |
if (isset($saved_settings['builtin_doc_page']) && intval($saved_settings['builtin_doc_page'])) { |
| 418 |
$docs_url = get_post_type_archive_link(BetterDocs_Docs_Post_Type::$post_type); |
| 419 |
} elseif (isset($saved_settings['docs_page']) && intval($saved_settings['docs_page'])) { |
| 420 |
$docs_url = !empty($saved_settings['docs_page']) ? get_page_link($saved_settings['docs_page']) : false; |
| 421 |
} |
| 422 |
|
| 423 |
if ($docs_url) { |
| 424 |
$admin_bar->add_node( |
| 425 |
array( |
| 426 |
'parent' => 'site-name', |
| 427 |
'id' => 'view-docs', |
| 428 |
'title' => __('Visit Documentation', 'betterdocs'), |
| 429 |
'href' => $docs_url, |
| 430 |
) |
| 431 |
); |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
|