notices = array(); $this->version = '2.8.7'; $this->option_group = 'index'; $this->option_function_name = null; $this->option_prefix = null; /** * afer basic setup */ $this->files = $this->get_files(); /** * hooks */ add_action( 'admin_enqueue_scripts', array( $this, 'register_styles' ), 0 ); add_action( 'admin_head', array( $this, 'admin_head' ) ); add_action( 'admin_menu', array( $this, 'admin_menu' ) ); add_action( 'admin_notices', array( &$this, 'admin_notices' ) ); add_filter( 'screen_layout_columns', array( $this, 'screen_layout_columns' ), 10, 2 ); } public function init() { $this->get_option_array(); } /** * Set option class mode. * * @since 2.6.5 * * @param string $mode Working mode, possible values "plugin", "theme". */ public function set_mode( $mode ) { if ( preg_match( '/^(plugin|theme)$/', $mode ) ) { $this->mode = $mode; } } /** * Get group * * @since 2.6.7 * * @param string $option_group Name of config group. */ public function get_group( $option_group = null ) { if ( null === $option_group ) { $option_group = $this->option_group; } return $this->get_option_array( $option_group ); } public function admin_menu() { $data = $this->get_option_array(); if ( ! isset( $this->options ) ) { return; } $pages = array(); $pages['index'] = $data; if ( isset( $data['pages'] ) ) { $pages = $data['pages'] + $pages; } foreach ( $pages as $key => $data ) { $keys_to_sanitize = array( 'menu', 'parent' ); foreach ( $keys_to_sanitize as $key_to_sanitize ) { if ( ! array_key_exists( $key_to_sanitize, $data ) ) { $data[ $key_to_sanitize ] = ''; } } if ( 'submenu' == $data['menu'] ) { if ( ! empty( $data['parent'] ) ) { /** * Check callback */ $callback = array( $this, 'show_page' ); if ( isset( $data['show_page_callback'] ) && is_callable( $data['show_page_callback'] ) ) { $callback = $data['show_page_callback']; } if ( isset( $data['set_callback_to_null'] ) && $data['set_callback_to_null'] ) { $callback = null; } /** * add submenu */ $this->pagehooks[ $key ] = add_submenu_page( $data['parent'], $data['page_title'], isset( $data['menu_title'] ) ? $data['menu_title'] : $data['page_title'], apply_filters( 'iworks_options_capability', 'manage_options', 'settings' ), isset( $data['menu_slug'] ) ? $data['menu_slug'] : $this->get_option_name( $key ), $callback ); add_action( 'load-' . $this->pagehooks[ $key ], array( $this, 'load_page' ) ); } } else { switch ( $data['menu'] ) { case 'comments': case 'dashboard': case 'links': case 'management': case 'media': case 'options': case 'pages': case 'plugins': case 'posts': case 'posts': case 'theme': case 'users': $function = sprintf( 'add_%s_page', $data['menu'] ); break; default: $function = 'add_menu_page'; break; } if ( isset( $data['page_title'] ) ) { $this->pagehooks[ $key ] = $function( $data['page_title'], isset( $data['menu_title'] ) ? $data['menu_title'] : $data['page_title'], 'manage_options', $this->get_option_name( $key ), array( $this, 'show_page' ) ); add_action( 'load-' . $this->pagehooks[ $key ], array( $this, 'load_page' ) ); } } } } public function get_version() { return $this->version; } public function set_option_function_name( $option_function_name ) { $this->option_function_name = $option_function_name; } public function set_option_prefix( $option_prefix ) { $this->option_prefix = $option_prefix; } private function get_option_array( $option_group = null ) { if ( null === $option_group ) { $option_group = $this->option_group; } $options = array(); if ( array_key_exists( $option_group, $options ) && ! empty( $options[ $option_group ] ) ) { $options = apply_filters( $this->option_function_name, $this->options ); return $options[ $option_group ]; } if ( is_callable( $this->option_function_name ) ) { $options = apply_filters( $this->option_function_name, call_user_func( $this->option_function_name ) ); } if ( array_key_exists( $option_group, $options ) && ! empty( $options[ $option_group ] ) ) { $this->options[ $option_group ] = $options[ $option_group ]; return apply_filters( $this->option_function_name, $this->options[ $option_group ] ); } return apply_filters( $this->option_function_name, array() ); } public function build_options( $option_group = 'index', $echo = true, $term_id = false ) { $this->option_group = $option_group; $options = $this->get_option_array(); /** * add some defaults */ $options['show_submit_button'] = true; $options['add_table'] = true; if ( ! array_key_exists( 'type', $options ) ) { $options['type'] = 'option'; } /** * add defaults for taxonomies */ if ( 'taxonomy' == $options['type'] ) { $options['show_submit_button'] = false; $options['add_table'] = false; } /** * check options exists? */ if ( ! is_array( $options['options'] ) ) { echo '
'; esc_html_e( 'An error occurred while getting the configuration.', 'sierotki' ); echo '
%s
%s
', esc_html( $args['value'] ) ); } private function money( $name, $value = '', $args = array() ) { if ( empty( $value ) || ! is_array( $value ) ) { $value = array(); } $value = wp_parse_args( $value, array( 'integer' => 0, 'fractional' => 0, 'currency' => false, ) ); $args = wp_parse_args( $args, array( 'kind' => 'complex', 'currency' => false, 'currency_default' => false, ) ); $content = ''; /** * Integer */ $n = sprintf( '%s[integer]', $name ); $content .= $this->input( $n, $value['integer'], array( 'min' => 0 ), 'number' ); if ( 'complex' === $args['kind'] ) { /** * fractional */ $n = sprintf( '%s[fractional]', $name ); $content .= $this->input( $n, $value['fractional'], array( 'min' => 0, 'max' => 99, ), 'number' ); } if ( is_array( $args['currency'] ) && ! empty( $args['currency'] ) ) { $n = sprintf( '%s[currency]', $name ); $atts = array( 'default' => $args['currency_default'], 'options' => $args['currency'], ); $content .= $this->select( $n, $value['currency'], $atts ); } return $content; } private function location( $name, $value = '', $args = array() ) { if ( empty( $value ) || ! is_array( $value ) ) { $value = array(); } $defaults = array( 'country' => '', 'city' => '', 'street' => '', 'zip' => '', ); $i18n = array( 'country' => esc_html__( 'Country', 'sierotki' ), 'city' => esc_html__( 'City', 'sierotki' ), 'street' => esc_html__( 'Street', 'sierotki' ), 'zip' => esc_html__( 'ZIP code', 'sierotki' ), ); $value = wp_parse_args( $value, $defaults ); /** * Content */ $content = ''; foreach ( array_keys( $defaults ) as $key ) { $content .= sprintf( ''; } return $content; } public function admin_head() { if ( false === $this->check_hooks_to_load_asses() ) { return; } $files = $this->get_files(); foreach ( $files as $data ) { if ( $data['style'] ) { wp_enqueue_style( $data['handle'] ); } else { wp_enqueue_script( $data['handle'] ); } } } /** * Convert color to rgb * * @since 2.4.1 * * @param string $hex Hex value of color * @return array RGB array. */ public function hex2rgb( $hex ) { $hex = str_replace( '#', '', $hex ); if ( strlen( $hex ) == 3 ) { $r = hexdec( substr( $hex, 0, 1 ) . substr( $hex, 0, 1 ) ); $g = hexdec( substr( $hex, 1, 1 ) . substr( $hex, 1, 1 ) ); $b = hexdec( substr( $hex, 2, 1 ) . substr( $hex, 2, 1 ) ); } else { $r = hexdec( substr( $hex, 0, 2 ) ); $g = hexdec( substr( $hex, 2, 2 ) ); $b = hexdec( substr( $hex, 4, 2 ) ); } $rgb = array( $r, $g, $b ); return $rgb; // returns an array with the rgb values } public function register_styles() { if ( false === $this->check_hooks_to_load_asses() ) { return; } $files = $this->get_files(); foreach ( $files as $data ) { $file = sprintf( 'assets/%s/%s', $data['style'] ? 'styles' : 'scripts', $data['file'] ); if ( 'theme' == $this->mode ) { $url = str_replace( get_template_directory(), '', dirname( __FILE__ ) ); $file = get_template_directory_uri() . $url . '/' . $file; } else { $file = plugins_url( $file, __FILE__ ); } $version = isset( $data['version'] ) ? $data['version'] : $this->version; $deps = isset( $data['deps'] ) ? $data['deps'] : array(); $in_footer = isset( $data['in_footer'] ) ? $data['in_footer'] : true; if ( $data['style'] ) { wp_register_style( $data['handle'], $file, $deps, $version ); } else { wp_register_script( $data['handle'], $file, $deps, $version, $in_footer ); if ( isset( $data['wp_localize_script'] ) ) { wp_localize_script( $data['handle'], $data['handle'], $data['wp_localize_script'] ); } } } } public function get_files() { $f = array( /** * iworks_options core files */ array( 'handle' => __CLASS__, 'file' => 'jquery-ui.min.css', ), array( 'handle' => __CLASS__, 'file' => 'common.js', 'deps' => array( 'jquery', 'switch_button', 'jquery-ui-tabs' ), 'wp_localize_script' => array( 'buttons' => array( 'select_media' => __( 'Select Image', 'sierotki' ), ), ), ), /** * switch checkbox */ array( 'handle' => 'switch_button', 'file' => 'jquery.switch_button.css', 'version' => '1.0', ), array( 'handle' => 'switch_button', 'file' => 'jquery.switch_button.js', 'version' => '1.0', 'deps' => array( 'jquery', 'jquery-effects-core', 'jquery-ui-widget' ), 'wp_localize_script' => $this->get_switch_button_data(), ), /** * select2 */ array( 'handle' => 'select2', 'file' => 'select2.min.css', 'version' => '4.0.3', ), array( 'handle' => 'select2', 'file' => 'select2.min.js', 'version' => '4.0.3', 'deps' => array( 'jquery' ), ), /** * options */ array( 'handle' => 'iworks-options', 'file' => 'options-admin.css', 'version' => $this->version, ), ); $files = array(); foreach ( $f as $data ) { $data['style'] = preg_match( '/css$/', $data['file'] ); $files[] = $data; } return $files; } public function get_switch_button_data() { $data = array( 'labels' => array( 'off_label' => esc_html__( 'OFF', 'sierotki' ), 'on_label' => esc_html__( 'ON', 'sierotki' ), ), ); return $data; } /** * Get option page * * @since 2.6.0 */ public function get_pagehook() { return $this->option_prefix . $this->option_group; } /** * Flush rewrite roles when it is configured * * @since 2.6.7 */ public function flush_rewrite_rules() { flush_rewrite_rules(); } /** * check to register or load assets * * check to register or load assets to avoid loading when it is not needed * * @since 2.8.0 */ private function check_hooks_to_load_asses() { if ( ! function_exists( 'get_current_screen' ) ) { return false; } $screen = get_current_screen(); if ( ! is_object( $screen ) ) { return false; } return in_array( $screen->id, $this->pagehooks ); } /** * Set plugin value * * @since 2.7.3 * * @param string $plugin Plugin file. */ public function set_plugin( $plugin ) { $this->plugin = $plugin; } /** * get nonce value * * @since 2.8.6 */ private function get_nonce_value() { $nonce_names = array( $this->get_nonce_name(), '_wpnonce' ); foreach ( $nonce_names as $nonce_name ) { if ( isset( $_REQUEST[ $nonce_name ] ) ) { return $_REQUEST[ $nonce_value ]; } } return new WP_Error( 'security', esc_html__( 'Failed Security Check', 'sierotki' ) ); } /** * get nonce name * * @since 2.8.6 */ private function get_nonce_name() { return apply_filters( 'iworks_options_nonce_name', 'iworks_options' ); } }