* @version 2017-02-22
* @since 2.0.0
*/
if ( ! function_exists( 'add_action' ) ) {
echo "Hi there! I'm just a part of plugin, not much I can do when called directly.";
exit;
}
/**
* Class Add_Quicktag_Settings
*/
class Add_Quicktag_Settings extends Add_Quicktag {
/**
* string for translation
*
* @var string
*/
static public $textdomain;
/**
* string for options in table options
*
* @var string
*/
static private $option_string;
/**
* string for plugin file
*
* @var string
*/
static private $plugin;
/**
* post types for the settings
*
* @var array
*/
static private $post_types_for_js;
/**
* string for nonce fields
*
* @var string
*/
static public $nonce_string;
/**
* @var
*/
protected $page_hook;
/**
* Handler for the action 'init'. Instantiates this class.
*
* @access public
* @since 2.0.0
* @return \Add_Quicktag|\Add_Quicktag_Settings $instance
*/
public static function get_object() {
static $instance;
if ( NULL === $instance ) {
$instance = new self();
}
return $instance;
}
/**
* Constructor, init on defined hooks of WP and include second class
*
* @access public
* @since 0.0.2
* @uses register_activation_hook, register_uninstall_hook, add_action
*/
private function __construct() {
if ( ! is_admin() ) {
return;
}
self::$option_string = parent::get_option_string();
self::$plugin = parent::get_plugin_string();
self::$post_types_for_js = parent::get_post_types_for_js();
self::$nonce_string = 'addquicktag_nonce';
// Makes sure the plugin is defined before trying to use it
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
}
register_uninstall_hook( __FILE__, array( 'Add_Quicktag_Settings', 'unregister_settings' ) );
// settings for an active multisite
if ( is_multisite() && is_plugin_active_for_network( self::$plugin ) ) {
add_action( 'network_admin_menu', array( $this, 'add_settings_page' ) );
// add settings link
add_filter(
'network_admin_plugin_action_links', array(
$this,
'network_admin_plugin_action_links'
), 10, 2
);
// save settings on network
add_action( 'network_admin_edit_' . self::$option_string, array( $this, 'save_network_settings_page' ) );
// return message for update settings
add_action( 'network_admin_notices', array( $this, 'get_network_admin_notices' ) );
// add script on settings page
} else {
add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
// add settings link
add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 2 );
// use settings API
add_action( 'admin_init', array( $this, 'register_settings' ) );
}
// include js
add_action(
'admin_print_scripts-settings_page_' . str_replace( '.php', '', plugin_basename( __FILE__ ) ),
array( $this, 'print_scripts' )
);
// add meta boxes on settings pages
add_action( 'addquicktag_settings_page_sidebar', array( $this, 'get_plugin_infos' ) );
add_action( 'addquicktag_settings_page_sidebar', array( $this, 'get_about_plugin' ) );
// include class for remove core quicktags
require_once dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'class-remove-quicktags.php';
// include class for add enhanced code quicktags
// @TODO Solution for special code tags in quicktags
require_once dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'class-code-quicktags.php';
// include class for im/export
require_once dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'class-imexport.php';
}
/**
* Return allowed post types for include scripts
*
* @since 2.1.1
* @access public
* @return array
*/
public function get_post_types_for_js() {
return self::$post_types_for_js;
}
/**
* Add settings link on plugins.php in backend
*
* @uses
* @access public
*
* @param array $links , string $file
* @param string $file
*
* @since 2.0.0
* @return string $links
*/
public function plugin_action_links( $links, $file ) {
if ( parent::get_plugin_string() === $file ) {
$links[] = '' . esc_html__(
'Settings'
) . '';
}
return $links;
}
/**
* Add settings link on plugins.php on network admin in backend
*
* @uses
* @access public
* @since 2.0.0
*
* @param array $links , string $file
* @param $file
*
* @return string $links
*/
public function network_admin_plugin_action_links( $links, $file ) {
if ( parent::get_plugin_string() === $file ) {
$links[] = '' . esc_html__(
'Settings'
) . '';
}
return $links;
}
/**
* Add settings page in WP backend
*
* @uses add_options_page
* @access public
* @since 2.0.0
*/
public function add_settings_page() {
if ( is_multisite() && is_plugin_active_for_network( self::$plugin ) ) {
add_submenu_page(
'settings.php',
parent::get_plugin_data( 'Name' ) . ' ' . esc_html__( 'Settings', 'addquicktag' ),
parent::get_plugin_data( 'Name' ),
'manage_options',
plugin_basename( __FILE__ ),
array( $this, 'get_settings_page' )
);
} else {
add_options_page(
parent::get_plugin_data( 'Name' ) . ' ' . esc_html__( 'Settings', 'addquicktag' ),
parent::get_plugin_data( 'Name' ),
'manage_options',
plugin_basename( __FILE__ ),
array( $this, 'get_settings_page' )
);
}
}
/**
* Return form and markup on settings page
*
* @uses settings_fields, normalize_whitespace, is_plugin_active_for_network, get_site_option, get_option
* @access public
* @since 0.0.2
*/
public function get_settings_page() {
?>
validate_settings( $_POST[ self::$option_string ] );
// update options
update_site_option( self::$option_string, $value );
// redirect to settings page in network
wp_redirect(
add_query_arg(
array( 'page' => plugin_basename( __FILE__ ), 'updated' => 'true' ),
network_admin_url( 'settings.php' )
)
);
exit();
}
/*
* Retrun string vor update message
*
* @uses
* @access public
* @since 2.0.0
* @return string $notice
*/
public function get_network_admin_notices() {
// if updated and the right page
if ( array_key_exists(
'updated', $_GET
)
&& 'settings_page_addquicktag/inc/class-settings-network' === $GLOBALS[ 'current_screen' ]->id
) {
$message = esc_html__( 'Options saved.', 'addquicktag' );
$notice = '';
echo $notice;
}
}
/**
* Validate settings for options
*
* @uses normalize_whitespace
* @access public
*
* @param array $value
*
* @since 2.0.0
* @return string $value
*/
public function validate_settings( $value ) {
// Save core buttons changes
if ( array_key_exists( 'core_buttons', $value ) ) {
$core_buttons = $value[ 'core_buttons' ];
}
// Save Code buttons
if ( array_key_exists( 'code_buttons', $value ) ) {
$code_buttons = $value[ 'code_buttons' ];
}
// set allowed values for import, only the defaults of plugin and custom post types
$allowed_settings = (array) array_merge(
$this->get_post_types_for_js(),
array( 'text', 'dashicon', 'title', 'start', 'end', 'access', 'order', 'visual' )
);
$buttons = array();
// filter for allowed values
foreach ( (array) $value[ 'buttons' ] as $key => $button ) {
foreach ( (array) $button as $label => $val ) {
if ( ! in_array( $label, $allowed_settings, TRUE ) ) {
unset( $button[ $label ] );
}
}
$buttons[] = $button;
}
// return filtered array
$filtered_values[ 'buttons' ] = $buttons;
$value = $filtered_values;
$buttons = array();
$c_buttons = count( $value[ 'buttons' ] );
for ( $i = 0; $i < $c_buttons; $i ++ ) {
$b = $value[ 'buttons' ][ $i ];
if ( ! empty( $b[ 'start' ] ) ) {
//preg_replace( '~[^\p{L}]~u', '', $string );
$b[ 'text' ] = sanitize_text_field( $b[ 'text' ] );
$b[ 'dashicon' ] = sanitize_text_field( $b[ 'dashicon' ] );
$b[ 'title' ] = sanitize_text_field( $b[ 'title' ] );
$b[ 'start' ] = wp_kses_stripslashes( $b[ 'start' ] );
$b[ 'end' ] = wp_kses_stripslashes( $b[ 'end' ] );
if ( array_key_exists( 'access', $b ) ) {
$b[ 'access' ] = esc_html( $b[ 'access' ] );
}
if ( array_key_exists( 'order', $b ) ) {
$b[ 'order' ] = (int) $b[ 'order' ];
}
// visual settings
if ( array_key_exists( 'visual', $b ) ) {
$b[ 'visual' ] = (int) $b[ 'visual' ];
} else {
$b[ 'visual' ] = 0;
}
// post types
foreach ( $this->get_post_types_for_js() as $post_type ) {
if ( array_key_exists( $post_type, $b ) ) {
$b[ $post_type ] = (int) $b[ $post_type ];
} else {
$b[ $post_type ] = 0;
}
}
$buttons[] = $b;
}
}
$value[ 'buttons' ] = $buttons;
// Check for wrong empty values and kill
foreach ( $value[ 'buttons' ] as $key => $b ) {
if ( empty( $b[ 'text' ] ) && empty( $b[ 'start' ] ) ) {
unset( $value[ 'buttons' ][ $key ] );
}
}
// reorder the array
$value[ 'buttons' ] = array_values( $value[ 'buttons' ] );
// Filter core button values, strings and convert to integer
if ( ! empty( $core_buttons ) ) {
/**
* $key is core-string
* 'core_buttons' =>
* array (size=1)
* 'strong' =>
* array (size=2)
* 'post' => string '1' (length=1)
* 'page' => string '1' (length=1)
*/
$filtered_core_buttons = array();
foreach ( (array) $core_buttons as $key => $var ) {
$core_button = array();
foreach ( (array) $var as $post_type => $val ) {
$core_button[ $post_type ] = (int) $val;
}
$filtered_core_buttons[ $key ] = $core_button;
}
$value[ 'core_buttons' ] = $filtered_core_buttons;
}
// Filter code button values, strings and convert to integer
if ( ! empty( $code_buttons ) ) {
$filtered_code_buttons = array();
foreach ( (array) $code_buttons as $key => $var ) {
$code_button = array();
foreach ( (array) $var as $post_type => $val ) {
$code_button[ $post_type ] = (int) $val;
}
$filtered_code_buttons[ $key ] = $code_button;
}
$value[ 'code_buttons' ] = $filtered_code_buttons;
}
return $value;
}
/**
* Register settings for options
*
* @uses register_setting
* @access public
* @since 2.0.0
* @return void
*/
public function register_settings() {
register_setting( self::$option_string . '_group', self::$option_string, array( $this, 'validate_settings' ) );
}
/**
* Unregister and delete settings; clean database
*
* @uses unregister_setting, delete_option
* @access public
* @since 0.0.2
* @return void
*/
public function unregister_settings() {
unregister_setting( self::$option_string . '_group', self::$option_string );
delete_option( self::$option_string );
}
/**
* Enqueue scripts and stylesheets
*
* @since 0.0.2
*
* @internal param $where
*/
public function print_scripts() {
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.dev' : '';
wp_register_script(
self::$option_string . '_dashicon_picker',
plugins_url( '/js/dashicons-picker' . $suffix . '.js', parent::get_plugin_string() ),
array( 'jquery' ),
'',
TRUE
);
wp_register_script(
self::$option_string . '_admin_script',
plugins_url( '/js/settings' . $suffix . '.js', parent::get_plugin_string() ),
array( 'jquery', 'quicktags' ),
'',
TRUE
);
wp_enqueue_script( self::$option_string . '_dashicon_picker' );
wp_enqueue_script( self::$option_string . '_admin_script' );
wp_register_style(
self::$option_string . '_dashicon_picker',
plugins_url( '/css/dashicons-picker' . $suffix . '.css', parent::get_plugin_string() ),
array( 'dashicons' ),
FALSE,
'screen'
);
wp_register_style(
self::$option_string . '_admin_style',
plugins_url( '/css/settings' . $suffix . '.css', parent::get_plugin_string() ),
array(),
FALSE,
'screen'
);
wp_enqueue_style( self::$option_string . '_dashicon_picker' );
wp_enqueue_style( self::$option_string . '_admin_style' );
}
}
$add_quicktag_settings = Add_Quicktag_Settings::get_object();