| 1 |
<?php |
| 2 |
/** |
| 3 |
* The admin-specific functionality of the plugin. |
| 4 |
* |
| 5 |
* @link https://wpdeveloper.net |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @package BetterDocs |
| 9 |
* @subpackage BetterDocs/admin |
| 10 |
* @author WPDeveloper <support@wpdeveloper.net> |
| 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 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Register the stylesheets for the admin area. |
| 83 |
* |
| 84 |
* @since 1.0.0 |
| 85 |
*/ |
| 86 |
public function enqueue_styles( $hook ) { |
| 87 |
$page_status = false; |
| 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 |
if( $hook == 'betterdocs_page_betterdocs-settings' || $hook == 'toplevel_page_betterdocs-admin' ) { |
| 94 |
$page_status = true; |
| 95 |
} |
| 96 |
|
| 97 |
if( ! $page_status ) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
wp_enqueue_style( |
| 102 |
$this->plugin_name . '-select2', |
| 103 |
BETTERDOCS_ADMIN_URL . 'assets/css/select2.min.css', |
| 104 |
array(), $this->version, 'all' |
| 105 |
); |
| 106 |
wp_enqueue_style( |
| 107 |
$this->plugin_name, |
| 108 |
BETTERDOCS_ADMIN_URL . 'assets/css/betterdocs-admin.css', |
| 109 |
array(), $this->version, 'all' |
| 110 |
); |
| 111 |
} |
| 112 |
/** |
| 113 |
* Register the JavaScript for the admin area. |
| 114 |
* |
| 115 |
* @since 1.0.0 |
| 116 |
*/ |
| 117 |
public function enqueue_scripts( $hook ) { |
| 118 |
wp_enqueue_script( 'wp-color-picker' ); |
| 119 |
// wp_enqueue_script( 'jquery-ui-datepicker' ); |
| 120 |
// wp_enqueue_media(); |
| 121 |
wp_enqueue_script( |
| 122 |
$this->plugin_name . '-select2', |
| 123 |
BETTERDOCS_ADMIN_URL . 'assets/js/select2.min.js', |
| 124 |
array( 'jquery' ), $this->version, true |
| 125 |
); |
| 126 |
wp_enqueue_script( |
| 127 |
$this->plugin_name . '-swal', |
| 128 |
BETTERDOCS_ADMIN_URL . 'assets/js/sweetalert.min.js', |
| 129 |
array( 'jquery' ), $this->version, true |
| 130 |
); |
| 131 |
wp_enqueue_script( |
| 132 |
$this->plugin_name, |
| 133 |
BETTERDOCS_ADMIN_URL . 'assets/js/betterdocs-admin.js', |
| 134 |
array( 'jquery' ), $this->version, true |
| 135 |
); |
| 136 |
wp_localize_script( $this->plugin_name, 'betterdocsAdminConfig', self::toggleFields() ); |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
public function toggleFields( $builder = false ){ |
| 141 |
$args = BetterDocs_Settings::settings_args(); |
| 142 |
|
| 143 |
$toggleFields = $hideFields = $conditions = array(); |
| 144 |
|
| 145 |
$tabs = $args; |
| 146 |
if( ! empty( $tabs ) ) { |
| 147 |
foreach( $tabs as $tab_id => $tab ) { |
| 148 |
$sections = isset( $tab['sections'] ) ? $tab[ 'sections' ] : []; |
| 149 |
if( ! empty( $sections ) ) { |
| 150 |
foreach( $sections as $section_id => $section ) { |
| 151 |
$fields = isset( $section['fields'] ) ? $section[ 'fields' ] : []; |
| 152 |
if( isset( $section['tabs'] ) && ! empty( $section['tabs'] ) ) { |
| 153 |
foreach( $section['tabs'] as $inner_field_tab_key => $inner_field_tab ) { |
| 154 |
if( isset( $inner_field_tab['fields'] ) ) { |
| 155 |
foreach( $inner_field_tab['fields'] as $inner_tab_field_key => $inner_tab_field_tab ) { |
| 156 |
if( isset( $inner_tab_field_tab['hide'] ) && ! empty( $inner_tab_field_tab['hide'] ) && is_array( $inner_tab_field_tab['hide'] ) ) { |
| 157 |
$hideFields = $this->a_walk( $inner_tab_field_tab['hide'], $inner_tab_field_key, $hideFields ); |
| 158 |
} |
| 159 |
if( isset( $inner_tab_field_tab['dependency'] ) && ! empty( $inner_tab_field_tab['dependency'] ) && is_array( $inner_tab_field_tab['dependency'] ) ) { |
| 160 |
$conditions = $this->a_walk( $inner_tab_field_tab['dependency'], $inner_tab_field_key, $conditions ); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
if( ! empty( $fields ) ) { |
| 167 |
foreach( $fields as $field_key => $field ) { |
| 168 |
if( isset( $field['fields'] ) ) { |
| 169 |
$iFields = $field['fields']; |
| 170 |
foreach( $iFields as $inner_field_key => $inner_field ) { |
| 171 |
if( isset( $inner_field['hide'] ) && ! empty( $inner_field['hide'] ) && is_array( $inner_field['hide'] ) ) { |
| 172 |
$hideFields = $this->a_walk( $inner_field['hide'], $inner_field_key, $hideFields ); |
| 173 |
} |
| 174 |
if( isset( $inner_field['dependency'] ) && ! empty( $inner_field['dependency'] ) && is_array( $inner_field['dependency'] ) ) { |
| 175 |
$conditions = $this->a_walk( $inner_field['dependency'], $inner_field_key, $conditions ); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
if( isset( $field['hide'] ) && ! empty( $field['hide'] ) && is_array( $field['hide'] ) ) { |
| 180 |
$hideFields = $this->a_walk( $field['hide'], $field_key, $hideFields ); |
| 181 |
} |
| 182 |
if( isset( $field['dependency'] ) && ! empty( $field['dependency'] ) && is_array( $field['dependency'] ) ) { |
| 183 |
$conditions = $this->a_walk( $field['dependency'], $field_key, $conditions ); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
return array( |
| 193 |
'toggleFields' => $conditions, // TODO: toggling system has to be more optimized! |
| 194 |
'hideFields' => $hideFields, |
| 195 |
); |
| 196 |
} |
| 197 |
public function a_walk( $array, $field_key, &$returned_array = [] ){ |
| 198 |
array_walk( $array, function( $value, $key ) use ( $field_key, &$returned_array ) { |
| 199 |
$returned_array[ $field_key ][ $key ] = $value; |
| 200 |
} ); |
| 201 |
|
| 202 |
return $returned_array; |
| 203 |
} |
| 204 |
/** |
| 205 |
* Admin Menu Page |
| 206 |
* |
| 207 |
* @return void |
| 208 |
*/ |
| 209 |
public function menu_page(){ |
| 210 |
$settings_class = new betterdocs_settings(); |
| 211 |
|
| 212 |
$betterdocs_articles_caps = apply_filters( 'betterdocs_articles_caps', 'edit_posts', 'article_roles' ); |
| 213 |
$betterdocs_terms_caps = apply_filters( 'betterdocs_terms_caps', 'delete_others_posts', 'article_roles' ); |
| 214 |
$betterdocs_settings_caps = apply_filters( 'betterdocs_settings_caps', 'administrator', 'settings_roles' ); |
| 215 |
|
| 216 |
$settings = apply_filters( 'betterdocs_admin_menu', array( |
| 217 |
'betterdocs-setup' => array( |
| 218 |
'title' => __('Quick Setup', 'betterdocs'), |
| 219 |
'capability' => 'delete_users', |
| 220 |
'callback' => 'BetterDocsSetupWizard::plugin_setting_page' |
| 221 |
), |
| 222 |
'betterdocs-settings' => array( |
| 223 |
'title' => __('Settings', 'betterdocs'), |
| 224 |
'capability' => $betterdocs_settings_caps, |
| 225 |
'callback' => array( $settings_class, 'settings_page' ) |
| 226 |
), |
| 227 |
) ); |
| 228 |
|
| 229 |
$this->menu_slug = apply_filters( 'betterdocs_menu_slug', 'edit.php?post_type=docs' ); |
| 230 |
$betterdocs_admin_output = apply_filters( 'betterdocs_admin_output', array() ); |
| 231 |
|
| 232 |
add_menu_page( |
| 233 |
'BetterDocs', 'BetterDocs', |
| 234 |
$betterdocs_articles_caps, $this->menu_slug, $betterdocs_admin_output, |
| 235 |
BETTERDOCS_ADMIN_URL.'/assets/img/betterdocs-icon-white.svg', 5 |
| 236 |
); |
| 237 |
add_submenu_page( |
| 238 |
$this->menu_slug, '', |
| 239 |
__( 'All Articles', 'betterdocs' ), |
| 240 |
$betterdocs_articles_caps, $this->menu_slug |
| 241 |
); |
| 242 |
add_submenu_page( $this->menu_slug, __('Add New', 'betterdocs'), __('Add New', 'betterdocs'), $betterdocs_articles_caps, 'post-new.php?post_type=docs'); |
| 243 |
add_submenu_page( |
| 244 |
$this->menu_slug, __('Categories', 'betterdocs'), __('Categories', 'betterdocs'), |
| 245 |
$betterdocs_terms_caps, 'edit-tags.php?taxonomy=doc_category&post_type=docs' |
| 246 |
); |
| 247 |
add_submenu_page( |
| 248 |
$this->menu_slug, __('Tags', 'betterdocs'), __('Tags', 'betterdocs'), |
| 249 |
$betterdocs_terms_caps, 'edit-tags.php?taxonomy=doc_tag&post_type=docs' |
| 250 |
); |
| 251 |
foreach( $settings as $slug => $setting ) { |
| 252 |
$cap = isset( $setting['capability'] ) ? $setting['capability'] : 'delete_users'; |
| 253 |
$hook = add_submenu_page( $this->menu_slug, $setting['title'], $setting['title'], $cap, $slug, $setting['callback'] ); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
public function highlight_admin_menu( $parent_file ){ |
| 258 |
global $current_screen; |
| 259 |
if( $this->menu_slug === 'betterdocs-admin' && in_array( $current_screen->id, array( 'edit-doc_tag', 'edit-doc_category' ) ) ) { |
| 260 |
$parent_file = 'betterdocs-admin'; |
| 261 |
} else { |
| 262 |
if( in_array( $current_screen->id, array( 'edit-doc_tag', 'edit-doc_category' ) ) ) { |
| 263 |
$parent_file = 'edit.php?post_type=docs'; |
| 264 |
} |
| 265 |
} |
| 266 |
return $parent_file; |
| 267 |
} |
| 268 |
|
| 269 |
public function highlight_admin_submenu( $parent_file, $submenu_file ){ |
| 270 |
global $current_screen, $pagenow; |
| 271 |
if ( $current_screen->post_type == 'docs' ) { |
| 272 |
if ( $pagenow == 'post.php' ) { |
| 273 |
$submenu_file = 'edit.php?post_type=docs'; |
| 274 |
} |
| 275 |
if ( $pagenow == 'post-new.php' ) { |
| 276 |
$submenu_file = 'post-new.php?post_type=docs'; |
| 277 |
} |
| 278 |
if( $current_screen->id === 'edit-doc_category' ) { |
| 279 |
$submenu_file = 'edit-tags.php?taxonomy=doc_category&post_type=docs'; |
| 280 |
} |
| 281 |
if( $current_screen->id === 'edit-doc_tag' ) { |
| 282 |
$submenu_file = 'edit-tags.php?taxonomy=doc_tag&post_type=docs'; |
| 283 |
} |
| 284 |
} |
| 285 |
if( 'betterdocs_page_betterdocs-settings' == $current_screen->id ) { |
| 286 |
$submenu_file = 'betterdocs-settings'; |
| 287 |
} |
| 288 |
if( 'betterdocs_page_betterdocs-setup' == $current_screen->id ) { |
| 289 |
$submenu_file = 'betterdocs-setup'; |
| 290 |
} |
| 291 |
|
| 292 |
return $submenu_file; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
public function quick_builder() { |
| 297 |
$builder_args = $this->builder_args; |
| 298 |
$tabs = $this->builder_args['tabs']; |
| 299 |
$prefix = self::$prefix; |
| 300 |
$metabox_id = $this->metabox_id; |
| 301 |
/** |
| 302 |
* This lines of code is for editing a notification in simple|quick builder |
| 303 |
* |
| 304 |
* @var [type] |
| 305 |
*/ |
| 306 |
$idd = null; |
| 307 |
if( isset( $_GET['post_id'] ) && ! empty( $_GET['post_id'] )) { |
| 308 |
$idd = intval( $_GET['post_id'] ); |
| 309 |
} |
| 310 |
include_once BETTERDOCS_ADMIN_DIR_PATH . 'partials/betterdocs-quick-builder-display.php'; |
| 311 |
} |
| 312 |
/** |
| 313 |
* Generate the builder data acording to default meta data |
| 314 |
* |
| 315 |
* @param array $data |
| 316 |
* @return array |
| 317 |
*/ |
| 318 |
protected function builder_data( $data ) { |
| 319 |
$post_data = []; |
| 320 |
$prefix = self::$prefix; |
| 321 |
$meta_fields = BetterDocs_MetaBox::get_metabox_fields( $prefix ); |
| 322 |
foreach( $meta_fields as $meta_key => $meta_field ) { |
| 323 |
if( in_array( $meta_key, array_keys($data) ) ) { |
| 324 |
$post_data[ $meta_key ] = $data[ $meta_key ]; |
| 325 |
} else { |
| 326 |
$post_data[ $meta_key ] = ''; |
| 327 |
|
| 328 |
if( isset( $meta_field['defaults'] ) ) { |
| 329 |
$post_data[ $meta_key ] = $meta_field['defaults']; |
| 330 |
} |
| 331 |
if( isset( $meta_field['default'] ) ) { |
| 332 |
$post_data[ $meta_key ] = $meta_field['default']; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
return array_merge( $post_data, $data ); |
| 338 |
} |
| 339 |
|
| 340 |
public static function get_form_action( $query_var = '', $builder_form = false ) { |
| 341 |
$page = '/edit.php?post_type=docs&page=betterdocs-settings'; |
| 342 |
|
| 343 |
if ( is_network_admin() ) { |
| 344 |
return network_admin_url( $page . $query_var ); |
| 345 |
} else { |
| 346 |
return admin_url( $page . $query_var ); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
/** |
| 352 |
* Admin Init For User Interactions |
| 353 |
* @return void |
| 354 |
*/ |
| 355 |
public function admin_init( $hook ){ |
| 356 |
/** |
| 357 |
* BetterDocs Admin URL |
| 358 |
*/ |
| 359 |
$current_url = admin_url('edit.php?post_type=docs&page=betterdocs-settings'); |
| 360 |
} |
| 361 |
public function toolbar_menu( $admin_bar ){ |
| 362 |
if ( ! is_admin() || ! is_admin_bar_showing() ) { |
| 363 |
return; |
| 364 |
} |
| 365 |
|
| 366 |
// Show only when the user is a member of this site, or they're a super admin. |
| 367 |
if ( ! is_user_member_of_blog() && ! is_super_admin() ) { |
| 368 |
return; |
| 369 |
} |
| 370 |
|
| 371 |
$saved_settings = BetterDocs_DB::get_settings(); |
| 372 |
if( isset( $saved_settings['builtin_doc_page'] ) && intval( $saved_settings['builtin_doc_page'] ) ) { |
| 373 |
$docs_url = isset( $saved_settings['docs_slug'] ) && ! empty( $saved_settings['docs_slug'] ) ? home_url( $saved_settings['docs_slug'] ) : false; |
| 374 |
if( $docs_url ) { |
| 375 |
// Add an option to visit the store. |
| 376 |
$admin_bar->add_node( |
| 377 |
array( |
| 378 |
'parent' => 'site-name', |
| 379 |
'id' => 'view-docs', |
| 380 |
'title' => __( 'Visit Documentation', 'betterdocs' ), |
| 381 |
'href' => $docs_url, |
| 382 |
) |
| 383 |
); |
| 384 |
} |
| 385 |
} elseif (isset( $saved_settings['docs_page'] ) && intval( $saved_settings['docs_page'] )) { |
| 386 |
$docs_page_url = isset( $saved_settings['docs_page'] ) && ! empty( $saved_settings['docs_page'] ) ? get_page_link($saved_settings['docs_page']) : false; |
| 387 |
$admin_bar->add_node( |
| 388 |
array( |
| 389 |
'parent' => 'site-name', |
| 390 |
'id' => 'view-docs', |
| 391 |
'title' => __( 'Visit Documentation', 'betterdocs' ), |
| 392 |
'href' => $docs_page_url, |
| 393 |
) |
| 394 |
); |
| 395 |
} |
| 396 |
|
| 397 |
} |
| 398 |
} |
| 399 |
|