*/ class Admin_Bug_Report_Admin { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Options * * @since 1.0.0 * @access protected * @var string $options Array of plugin options stored in the database */ protected $options = array(); /** * Settings Fields * * @since 1.0.0 * @access private * @var string $fields Settings Fields */ protected $fields = array(); /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of this plugin. * @param string $version The version of this plugin. * @param array $options Plugins options. */ public function __construct( $plugin_name, $version, $options = array() ) { $this->plugin_name = $plugin_name; $this->version = $version; $this->options = $options; } /** * Register the stylesheets for the admin area. * * @since 1.0.0 */ public function enqueue_styles() { if ( wp_style_is( 'fontawesome', 'enqueued' ) ) { wp_register_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/admin-bug-report-admin.min.css', array( 'fontawesome' ), $this->version, 'all' ); } else if ( wp_style_is( 'font-awesome', 'enqueued' ) ) { wp_register_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/admin-bug-report-admin.min.css', array( 'font-awesome' ), $this->version, 'all' ); } else { wp_register_style( $this->plugin_name.'-font-awesome', '//use.fontawesome.com/releases/v5.5.0/css/all.css', array(), '5.5.0', 'all' ); wp_register_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/admin-bug-report-admin.min.css', array( $this->plugin_name.'-font-awesome' ), $this->version, 'all' ); wp_enqueue_style( $this->plugin_name.'-font-awesome' ); } wp_enqueue_style( $this->plugin_name ); } /** * Register the JavaScript for the admin area. * * @since 1.0.0 */ public function enqueue_scripts() { wp_register_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/dist/admin-bug-report-admin.min.js', array(), $this->version, true ); // Localization/JS Vars $globals = array(); if ( isset( $this->options['email-include-debugging'] ) && $this->options['email-include-debugging'] ) { global $post; $globals = array(); if ( is_admin() ) { $globals['SCREEN'] = get_current_screen(); } if ( is_a( $post, 'WP_Post' ) ) { $globals['POST'] = $post; } } $data = array( 'AJAX_URL' => admin_url( 'admin-ajax.php' ), 'GLOBALS' => $globals, 'NONCES' => array( 'SUBMIT_REPORT' => wp_create_nonce( 'SUBMIT_REPORT' ), ), ); wp_localize_script( $this->plugin_name, 'abr', $data ); wp_enqueue_script( $this->plugin_name ); } /** * Add plugin actions links * * @param array $links * * @return array * * @since 1.0.0 * @access public */ public function plugin_action_links( $links ) { $links[] = '' . __( 'Settings', $this->plugin_name ) . ''; return $links; } /** * Add the options page * * @since 1.0.0 * @access public */ public function admin_page() { $parent_slug = 'options-general.php'; $page_title = __( 'Admin Bug Report Settings', $this->plugin_name ); $menu_title = __( 'Admin Bug Report', $this->plugin_name ); $capability = 'manage_options'; $slug = 'admin_bug_report'; $callback = array( $this, 'admin_page_display' ); add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $slug, $callback ); } /** * Setup admin settings * * @since 1.0.0 * @access public */ public function admin_settings() { $this->setup_settings_fields(); $this->setup_settings_sections(); } /** * Include the admin settings page template * * @since 1.0.0 * @access public */ public function admin_page_display() { include_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/admin-bug-report-admin-settings.php'; } /** * Setup the sections for the settigns page * * @since 1.0.0 * @access public */ public function setup_settings_sections() { add_settings_section( 'abr_general', __( 'General', $this->plugin_name ), array( $this, 'settings_section_callback' ), 'admin_bug_report' ); } /** * Settings sections callback * * @param array $arguments Array of arguments for the section * * @since 1.0.0 * @access public */ public function settings_section_callback( $arguments ) { switch ( $arguments['id'] ){ case 'abr_general': esc_html_e( 'General settings for the admin issue/bug reporting feature.', $this->plugin_name ); break; } } /** * Setup the settings fields * * @since 1.0.0 * @access public */ public function setup_settings_fields() { $this->fields = array( 'abr_general' => array( array( 'uid' => 'email-recipient', 'label' => __( 'Email Recipient', $this->plugin_name ), 'section' => 'abr_general', 'type' => 'email', 'args' => array( 'placeholder' => 'email@domain.com', ), ), array( 'uid' => 'email-subject', 'label' => __( 'Email Subject', $this->plugin_name ), 'section' => 'abr_general', 'type' => 'text', ), array( 'uid' => 'email-include-debugging', 'label' => __( 'Include Debugging Info', $this->plugin_name ), 'section' => 'abr_general', 'type' => 'checkbox', 'default' => '1', 'args' => array( 'value' => 1, ), ), ), ); foreach ( $this->fields as $section => $fields ) { foreach ( $fields as $field ) { add_settings_field( $field['uid'], $field['label'], array( $this, 'settings_field_callback' ), 'admin_bug_report', $field['section'], $field ); } } register_setting( 'admin_bug_report', $this->plugin_name, array( $this, 'validate_settings_callback' ) ); } /** * Copy product to Squadron Posters using the REST API when created * * @param array $arguments Array of field arguments * * @since 1.0.0 * @access public */ public function settings_field_callback( $arguments ) { if ( is_array( $arguments ) && ! empty( $arguments ) ) { $html = ''; $classes = array(); $field_attrs = ''; $value = ''; $field_name = $this->plugin_name.'['.$arguments['uid'].']'; if ( isset( $this->options[$arguments['uid']] ) ) { $value = $this->options[$arguments['uid']]; } else if ( isset( $arguments['default'] ) ) { $value = $arguments['default']; } if ( isset( $arguments['args'] ) && is_array( $arguments['args'] ) && ! empty( $arguments['args'] ) ) { $field_params = $arguments['args']; if ( isset( $field_params['attrs']) && is_array( $field_params['attrs'] ) && ! empty( $field_params['attrs'] ) ) { foreach ( $field_params['attrs'] as $attr_key => $attr_val ) { $field_attrs .= sprintf( ' %1$s="%2$s"', esc_attr( $attr_key ), esc_attr( $attr_val ) ); } } if ( isset( $field_params['toggle'] ) && ! empty( $field_params['toggle'] ) ) { $field_attrs .= sprintf( ' data-toggle="%1$s" data-toggle-value="%2$s"', esc_attr( $field_params['toggle'] ), esc_attr( $field_params['toggle_value'] ) ); } if ( isset( $field_params['toggled_by'] ) && ! empty( $field_params['toggled_by'] ) ) { $field_attrs .= sprintf( ' data-toggled-by="%1$s"', esc_attr( $field_params['toggled_by'] ) ); $toggler_value = $this->options[$field_params['toggled_by']]; if ( $toggler_value != $this->options[$this->fields[$field_params['toggled_by']]['args']['toggle_value']] ) { $classes[] = ' conditional-hidden'; } } if ( isset( $field_params['class'] ) && ! empty( $field_params['class'] ) ) { $classes[] = $field_params['class']; } if ( isset( $field_params['class'] ) && ! empty( $field_params['class'] ) ) { $classes[] = $field_params['class']; } if ( is_array( $classes ) && ! empty( $classes ) ) { $field_attrs .= sprintf( ' class="%1$s"', esc_attr( implode( ' ', $classes ) ) ); } if ( isset( $field_params['placeholder'] ) && ! empty( $field_params['placeholder'] ) ) { $field_attrs .= sprintf( ' placeholder="%1$s"', esc_attr( $field_params['placeholder'] ) ); } } switch ( $arguments['type'] ) { case 'text': case 'hidden': case 'password': case 'number': case 'email': case 'tel': case 'url': case 'search': case 'range': case 'date': case 'datetime': case 'datetime-local': case 'month': case 'week': case 'time': $html .= sprintf( '', esc_attr( $arguments['type'] ), esc_attr( $field_name ), esc_attr( $value ), $field_attrs ); break; case 'checkbox': $checkbox_value = $arguments['args']['value']; $checked = isset( $this->options[$arguments['uid']] ); $input = ''; if ( isset( $arguments['args']['wrap_label'] ) && false !== $arguments['args']['wrap_label'] ) { $input = ' '; } $html .= $input; break; case 'checkbox_multi': if ( is_array( $arguments['args'] ) && isset( $arguments['args']['options'] ) && ! empty( $arguments['args']['options'] ) ) { foreach ( $arguments['args']['options'] as $value => $label ) { $checked = false; $checked = is_array( $field_value ) && in_array( $value, $field_value ); $input = ''; if ( $arguments['args']['wrap_label'] ) { $input = ' '; } $html .= $input; } } break; case 'radio': if ( is_array( $arguments['args'] ) && isset( $arguments['args']['options'] ) && ! empty( $arguments['args']['options'] ) ) { foreach ( $arguments['args']['options'] as $value => $label ) { $checked = false; $checked = $value == $field_value; $input = ''; if ( $arguments['args']['wrap_label'] ) { $input = ' '; } $html .= $input; } } break; case 'select': $is_multiple = false; $select_name = esc_attr( $field_name ); if ( is_array( $arguments['args'] ) && isset( $arguments['args']['multiple'] ) && $arguments['args']['multiple'] !== false ) { $is_multiple = true; $select_name .= '[]'; } $html .= ''; break; case 'textarea': $html .= ''; break; case 'image': $image_thumb = ''; if ( $value ) { $image_thumb = wp_get_attachment_thumb_url( $value ); } $html .= '
'; $html .= ''; $html .= ''; $html .= ''; break; case 'button': $html .= sprintf( '', esc_attr( $arguments['type'] ), esc_attr( $field_name ), esc_attr( $field_params['button_text'] ) ); break; } if ( isset( $arguments['helper'] ) && $helper = $arguments['helper'] ) { $html .= sprintf( '%s', $helper ); } if ( isset( $arguments['description'] ) && $description = $arguments['description'] ) { $html .= sprintf( '

%2$s

', esc_attr( $field_name ), $description ); } } echo $html; } /** * Helper to create select options from an array of values * * @param array $options Array of options * @param mixed $values Current value(s) for the select * * @since 1.0.0 * @access protected */ protected function select_options( array $options, $values ) { $options_html = ''; if ( empty( $options ) ) return $options_html; foreach ( $options as $id => $value ) { if ( is_array ($value ) ) { sprintf( '%2$s', esc_attr( $id ), $this->select_options( $value, $values ) ); continue; } $option_value = html_entity_decode( $id ); if ( is_array( $values ) ) { $is_selected = array_search( $option_value, $values ); } else { $is_selected = $option_value == $values; } $options_html .= '