*/ /** * Menu Icons Settings module */ final class Menu_Icons_Settings { const UPDATE_KEY = 'menu-icons-settings-update'; const RESET_KEY = 'menu-icons-settings-reset'; const TRANSIENT_KEY = 'menu_icons_message'; /** * Default setting values * * @since 0.3.0 * @var array * @access protected */ protected static $defaults = array( 'global' => array( 'icon_types' => array( 'dashicons' ), ), ); /** * Setting values * * @since 0.3.0 * @var array * @access protected */ protected static $settings = array(); /** * Script dependencies * * @since 0.9.0 * @access protected * @var array */ protected static $script_deps = array( 'jquery' ); /** * Settings init * * @since 0.3.0 */ public static function init() { /** * Allow themes/plugins to override the default settings * * @since 0.9.0 * * @param array $default_settings Default settings. */ self::$defaults = apply_filters( 'menu_icons_settings_defaults', self::$defaults ); self::$settings = get_option( 'menu-icons', self::$defaults ); foreach ( self::$settings as $key => &$value ) { if ( 'global' === $key ) { // Remove unregistered icon types. $value['icon_types'] = array_values( array_intersect( array_keys( Menu_Icons::get( 'types' ) ), array_filter( (array) $value['icon_types'] ) ) ); } else { // Backward-compatibility. if ( isset( $value['width'] ) && ! isset( $value['svg_width'] ) ) { $value['svg_width'] = $value['width']; } unset( $value['width'] ); } } unset( $value ); /** * Allow themes/plugins to override the settings * * @since 0.9.0 * * @param array $settings Menu Icons settings. */ self::$settings = apply_filters( 'menu_icons_settings', self::$settings ); if ( self::is_menu_icons_disabled_for_menu() ) { return; } if ( ! empty( self::$settings['global']['icon_types'] ) ) { require_once Menu_Icons::get( 'dir' ) . 'includes/picker.php'; Menu_Icons_Picker::init(); self::$script_deps[] = 'icon-picker'; } add_action( 'load-nav-menus.php', array( __CLASS__, '_load_nav_menus' ), 1 ); add_action( 'wp_ajax_menu_icons_update_settings', array( __CLASS__, '_ajax_menu_icons_update_settings' ) ); } /** * Check if menu icons is disabled for a menu * * @since 0.8.0 * * @param int $menu_id Menu ID. Defaults to current menu being edited. * * @return bool */ public static function is_menu_icons_disabled_for_menu( $menu_id = 0 ) { if ( empty( $menu_id ) ) { $menu_id = self::get_current_menu_id(); } // When we're creating a new menu or the recently edited menu // could not be found. if ( empty( $menu_id ) ) { return true; } $menu_settings = self::get_menu_settings( $menu_id ); $is_disabled = ! empty( $menu_settings['disabled'] ); return $is_disabled; } /** * Get ID of menu being edited * * @since 0.7.0 * @since 0.8.0 Get the recently edited menu from user option. * * @return int */ public static function get_current_menu_id() { global $nav_menu_selected_id; if ( ! empty( $nav_menu_selected_id ) ) { return $nav_menu_selected_id; } if ( is_admin() && isset( $_REQUEST['menu'] ) ) { $menu_id = absint( $_REQUEST['menu'] ); } else { $menu_id = absint( get_user_option( 'nav_menu_recently_edited' ) ); } return $menu_id; } /** * Get menu settings * * @since 0.3.0 * * @param int $menu_id * * @return array */ public static function get_menu_settings( $menu_id ) { $menu_settings = self::get( sprintf( 'menu_%d', $menu_id ) ); $menu_settings = apply_filters( 'menu_icons_menu_settings', $menu_settings, $menu_id ); if ( ! is_array( $menu_settings ) ) { $menu_settings = array(); } return $menu_settings; } /** * Get setting value * * @since 0.3.0 * @return mixed */ public static function get() { $args = func_get_args(); return kucrut_get_array_value_deep( self::$settings, $args ); } /** * Prepare wp-admin/nav-menus.php page * * @since 0.3.0 * @wp_hook action load-nav-menus.php */ public static function _load_nav_menus() { add_action( 'admin_enqueue_scripts', array( __CLASS__, '_enqueue_assets' ), 99 ); /** * Allow settings meta box to be disabled. * * @since 0.4.0 * * @param bool $disabled Defaults to FALSE. */ $settings_disabled = apply_filters( 'menu_icons_disable_settings', false ); if ( true === $settings_disabled ) { return; } self::_maybe_update_settings(); self::_add_settings_meta_box(); add_action( 'admin_notices', array( __CLASS__, '_admin_notices' ) ); } /** * Update settings * * @since 0.3.0 */ public static function _maybe_update_settings() { if ( ! empty( $_POST['menu-icons']['settings'] ) ) { check_admin_referer( self::UPDATE_KEY, self::UPDATE_KEY ); $redirect_url = self::_update_settings( $_POST['menu-icons']['settings'] ); // Input var okay. wp_redirect( $redirect_url ); } elseif ( ! empty( $_REQUEST[ self::RESET_KEY ] ) ) { check_admin_referer( self::RESET_KEY, self::RESET_KEY ); wp_redirect( self::_reset_settings() ); } } /** * Update settings * * @since 0.7.0 * @access protected * * @param array $values Settings values. * * @return string Redirect URL. */ protected static function _update_settings( $values ) { update_option( 'menu-icons', wp_parse_args( kucrut_validate( $values ), self::$settings ) ); set_transient( self::TRANSIENT_KEY, 'updated', 30 ); $redirect_url = remove_query_arg( array( 'menu-icons-reset' ), wp_get_referer() ); return $redirect_url; } /** * Reset settings * * @since 0.7.0 * @access protected * @return string Redirect URL. */ protected static function _reset_settings() { delete_option( 'menu-icons' ); set_transient( self::TRANSIENT_KEY, 'reset', 30 ); $redirect_url = remove_query_arg( array( self::RESET_KEY, 'menu-icons-updated' ), wp_get_referer() ); return $redirect_url; } /** * Settings meta box * * @since 0.3.0 * @access private */ private static function _add_settings_meta_box() { add_meta_box( 'menu-icons-settings', __( 'Menu Icons Settings', 'menu-icons' ), array( __CLASS__, '_meta_box' ), 'nav-menus', 'side', 'low', array() ); } /** * Update settings via ajax * * @since 0.7.0 * @wp_hook action wp_ajax_menu_icons_update_settings */ public static function _ajax_menu_icons_update_settings() { check_ajax_referer( self::UPDATE_KEY, self::UPDATE_KEY ); if ( empty( $_POST['menu-icons']['settings'] ) ) { wp_send_json_error(); } $redirect_url = self::_update_settings( $_POST['menu-icons']['settings'] ); // Input var okay. wp_send_json_success( array( 'redirectUrl' => $redirect_url ) ); } /** * Print admin notices * * @since 0.3.0 * @wp_hook action admin_notices */ public static function _admin_notices() { $messages = array( 'updated' => __( 'Menu Icons Settings have been successfully updated.', 'menu-icons' ), 'reset' => __( 'Menu Icons Settings have been successfully reset.', 'menu-icons' ), ); $message_type = get_transient( self::TRANSIENT_KEY ); if ( ! empty( $message_type ) && ! empty( $messages[ $message_type ] ) ) { printf( '
%s