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