* @license GPL-2.0+ * @link http://tovolt.com * @copyright 2014 ToVolt */ /** * anspress_admin class. This class should ideally be used to work with the * administrative side of the WordPress site. * * @package AnsPress * @author Rahul Aryan */ class anspress_admin { /** * Instance of this class. * @var object */ protected static $instance = null; /** * Slug of the plugin screen. * @var string */ protected $plugin_screen_hook_suffix = null; // Name of the array protected $option_name = 'anspress_opt'; /** * Initialize the plugin by loading admin scripts & styles and adding a * settings page and menu. * */ private function __construct() { /* * Call $plugin_slug from public plugin class. * */ $plugin = anspress::get_instance(); $this->plugin_slug = $plugin->get_plugin_slug(); // Load admin style sheet and JavaScript. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); // Add the options page and menu item. add_action( 'admin_menu', array( $this, 'add_plugin_admin_menu' ) ); // Add an action link pointing to the options page. $plugin_basename = plugin_basename( plugin_dir_path( __DIR__ ) . $this->plugin_slug . '.php' ); add_filter( 'plugin_action_links_' . $plugin_basename, array( $this, 'add_action_links' ) ); add_action('admin_init', array($this, 'register_setting')); // flush rewrite rule if option updated add_action('admin_init', array($this, 'flush_rules')); } /** * Return an instance of this class. * @return object A single instance of this class. */ public static function get_instance() { // If the single instance hasn't been set, set it now. if ( null == self::$instance ) { self::$instance = new self; } return self::$instance; } /** * Register and enqueue admin-specific style sheet. * @return null Return early if no settings page is registered. */ public function enqueue_admin_styles() { $screen = get_current_screen(); if ( $this->plugin_screen_hook_suffix == $screen->id || 'question' == get_post_type() || 'answer' == get_post_type() || is_tax( 'question_category' ) || is_tax( 'question_tags' )) { wp_enqueue_style( 'ap-admin-css', ANSPRESS_URL.'assets/ap-admin.css'); wp_enqueue_style( 'ap-bootstrap-css', ANSPRESS_URL.'assets/bootstrap.css'); } } /** * Register and enqueue admin-specific JavaScript. * @return null Return early if no settings page is registered. */ public function enqueue_admin_scripts() { $screen = get_current_screen(); if ( $this->plugin_screen_hook_suffix == $screen->id || 'question' == get_post_type() || 'answer' == get_post_type() || is_tax( 'question_category' ) || is_tax( 'question_tags' )) { wp_enqueue_script( 'jquery-ui-autocomplete' ); wp_enqueue_script( 'ap-bootstrap-js', ANSPRESS_URL.'assets/bootstrap.js'); wp_enqueue_script( 'ap-admin-js', ANSPRESS_URL.'assets/ap-admin.js'); } } /** * Register the administration menu for this plugin into the WordPress Dashboard menu. */ public function add_plugin_admin_menu() { /* * Add a settings page for this plugin to the Settings menu. * * NOTE: Alternative menu locations are available via WordPress administration menu functions. * * Administration Menus: http://codex.wordpress.org/Administration_Menus */ $this->plugin_screen_hook_suffix = add_options_page( __( 'AnsPress Options', $this->plugin_slug ), __( 'AnsPress Options', $this->plugin_slug ), 'manage_options', 'anspress_settings', array( $this, 'display_plugin_admin_page' ) ); } /** * Render the settings page for this plugin. */ public function display_plugin_admin_page() { include_once( 'views/admin.php' ); } /** * Add settings action link to the plugins page. */ public function add_action_links( $links ) { return array_merge( array( 'settings' => '' . __( 'Settings', $this->plugin_slug ) . '' ), $links ); } //register settings public function register_setting(){ // Register settings and call sanitation functions register_setting( 'anspress_options', 'anspress_opt', array($this, 'validate_options') ); } public function validate_options( $input ) { global $sa_options, $sa_categories, $sa_layouts; $settings = get_option( 'sa_options', $sa_options ); // We strip all tags from the text field, to avoid vulnerablilties like XSS $input['footer_copyright'] = wp_filter_nohtml_kses( $input['footer_copyright'] ); // We strip all tags from the text field, to avoid vulnerablilties like XSS $input['intro_text'] = wp_filter_post_kses( $input['intro_text'] ); // We select the previous value of the field, to restore it in case an invalid entry has been given $prev = $settings['featured_cat']; // We verify if the given value exists in the categories array if ( !array_key_exists( $input['featured_cat'], $sa_categories ) ) $input['featured_cat'] = $prev; // We select the previous value of the field, to restore it in case an invalid entry has been given $prev = $settings['layout_view']; // We verify if the given value exists in the layouts array if ( !array_key_exists( $input['layout_view'], $sa_layouts ) ) $input['layout_view'] = $prev; // If the checkbox has not been checked, we void it if ( ! isset( $input['author_credits'] ) ) $input['author_credits'] = null; // We verify if the input is a boolean value $input['author_credits'] = ( $input['author_credits'] == 1 ? 1 : 0 ); return $input; } public function flush_rules(){ // flush_rules if option updated if(isset($_GET['page']) && ('anspress_settings' == $_GET['page']) && isset($_GET['settings-updated']) && $_GET['settings-updated']){ flush_rewrite_rules(); } } }