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 '

'; return; } /** * proceder */ $is_simple = 'simple' == $this->get_option( 'configuration', 'index', 'advance' ); $content = ''; $hidden = ''; $top = ''; $use_tabs = isset( $options['use_tabs'] ) && $options['use_tabs']; /** * add last_used_tab field */ if ( $use_tabs ) { $field = array( 'type' => 'hidden', 'name' => 'last_used_tab', 'id' => 'last_used_tab', 'value' => $this->get_option( 'last_used_tab' ), ); array_unshift( $options['options'], $field ); } /** * produce options */ if ( $use_tabs ) { $top .= sprintf( '
', esc_attr( $this->option_prefix ) ); } $i = 0; $label_index = 0; $last_tab = null; $related_to = array(); $configuration = 'all'; foreach ( $options['options'] as $option ) { if ( isset( $option['capability'] ) ) { if ( ! current_user_can( $option['capability'] ) ) { continue; } } /** * add default type */ if ( ! array_key_exists( 'type', $option ) ) { $option['type'] = 'text'; } /** * check show option */ $show_option = true; if ( isset( $option['check_supports'] ) && is_array( $option['check_supports'] ) && count( $option['check_supports'] ) ) { foreach ( $option['check_supports'] as $support_to_check ) { if ( ! current_theme_supports( $support_to_check ) ) { $show_option = false; } } } if ( ! $show_option ) { continue; } /** * dismiss on special type */ if ( 'special' == $option['type'] ) { continue; } /** * get option name */ $option_name = false; if ( array_key_exists( 'name', $option ) && $option['name'] ) { $option_name = $option['name']; if ( 'taxonomy' == $options['type'] ) { $option_name = sprintf( '%s_%s_%s', $option_group, $term_id, $option_name ); } } /** * dismiss if have "callback_to_show" and return false */ if ( ! preg_match( '/^(heading|info)$/', $option['type'] ) && isset( $option['callback_to_show'] ) && is_callable( $option['callback_to_show'] ) ) { if ( false === $option['callback_to_show']( $this->get_option( $option_name, $option_group ) ) ) { continue; } } /** * heading */ if ( preg_match( '/^(heading|page)$/', $option['type'] ) ) { if ( isset( $option['configuration'] ) ) { $configuration = $option['configuration']; } else { $configuration = 'all'; } } if ( ( $is_simple && $configuration == 'advance' ) || ( ! $is_simple && $configuration == 'simple' ) ) { if ( isset( $option['configuration'] ) && 'both' == $option['configuration'] ) { continue; } if ( in_array( $option['type'], array( 'checkbox', 'email', 'image', 'number', 'radio', 'text', 'textarea', 'url', ) ) ) { $html_element_name = $option_name ? $this->option_prefix . $option_name : ''; $content .= sprintf( ' %s', esc_attr( $html_element_name ), esc_attr( $this->get_option( $option_name, $option_group ) ), PHP_EOL ); } continue; } $tr_classes = array( 'iworks-options-row', sprintf( 'iworks-options-type-%s', esc_attr( strtolower( $option['type'] ) ) ), ); if ( $option['type'] == 'heading' ) { if ( $use_tabs ) { if ( $last_tab != $option['label'] ) { $last_tab = $option['label']; if ( $options['add_table'] ) { $content .= ''; } $content .= ''; } $content .= sprintf( '
', esc_attr( crc32( $option['label'] ) ), sprintf( ( isset( $option['class'] ) && $option['class'] ) ? sprintf( ' rel="%s"', esc_attr( $option['class'] ) ) : '' ) ); if ( ! $use_tabs ) { $content .= sprintf( '

%s

', esc_html( $option['label'] ) ); } if ( $options['add_table'] ) { $content .= sprintf( '', esc_attr( isset( $options['widefat'] ) ? ' widefat' : '' ), esc_attr( isset( $options['style'] ) ? $options['style'] : '' ) ); $content .= ''; } } $content .= sprintf( '', esc_attr( $option_name ? $option_name : '' ), esc_attr( implode( ' ', $tr_classes ) ) ); $content .= sprintf( '', isset( $option['dashicon'] ) && $option['dashicon'] ? sprintf( ' ', esc_attr( $option['dashicon'] ) ) : '', isset( $option['th'] ) && $option['th'] ? $option['th'] : ' ' ); $content .= ''; $content .= ''; } } /** * filter */ if ( isset( $option['filter'] ) ) { $content .= apply_filters( $option['filter'], '' ); } /** * content */ if ( $content ) { if ( isset( $options['label'] ) && $options['label'] && ! $use_tabs ) { $top .= sprintf( '

%s

', esc_html( $options['label'] ) ); } $top .= $hidden; if ( $use_tabs ) { if ( $options['add_table'] ) { $content .= ''; } $content .= '
'; $content = $top . $content; } else { if ( $options['add_table'] ) { $top .= sprintf( '', esc_attr( isset( $options['widefat'] ) ? ' widefat' : '' ), esc_attr( isset( $options['style'] ) ? $options['style'] : '' ) ); if ( isset( $options['thead'] ) ) { $top .= sprintf( '', esc_attr( implode( ' ', $tr_classes ) ) ); foreach ( $options['thead'] as $text => $colspan ) { $top .= sprintf( '%s', $colspan > 1 ? ' colspan="' . $colspan . '"' : '', esc_html( $text ) ); } $top .= ''; } $top .= ''; } $content = $top . $content; if ( $options['add_table'] ) { $content .= ''; } } } if ( $use_tabs ) { $content .= '
'; } /** * submit button */ if ( $options['show_submit_button'] ) { $content .= get_submit_button( esc_html__( 'Save Changes', 'sierotki' ), 'primary', 'submit_button' ); } /** * iworks-options wrapper */ $content = sprintf( '
%s
', $content ); /* print ? */ if ( $echo ) { echo $content; return; } return $content; } private function register_setting( $options, $option_group ) { foreach ( $options as $option ) { /** * don't register setting without type and name */ if ( ! is_array( $option ) || ! array_key_exists( 'type', $option ) || ! array_key_exists( 'name', $option ) ) { continue; } /** * don't register certain type setting or with empty name */ if ( preg_match( '/^(sub)?heading$/', $option['type'] ) || empty( $option['name'] ) ) { continue; } /** * register setting */ $args = array(); if ( isset( $option['sanitize_callback'] ) ) { $args['sanitize_callback'] = $option['sanitize_callback']; } if ( isset( $option['description'] ) ) { $args['description'] = $option['description']; } if ( isset( $option['flush_rewrite_rules'] ) ) { $action = sprintf( 'update_option_%s%s', $this->option_prefix, $option['name'] ); add_action( $action, array( $this, 'flush_rewrite_rules' ) ); } /** * register */ register_setting( $this->option_prefix . $option_group, $this->option_prefix . $option['name'], $args ); } } public function options_init() { $options = $this->get_option_array(); /** * register_setting last_used_tab field */ if ( isset( $options['use_tabs'] ) && $options['use_tabs'] ) { register_setting( $this->option_prefix . 'index', $this->option_prefix . 'last_used_tab' ); } /** * filter it */ $options = apply_filters( $this->option_function_name, $options ); /** * register_setting */ foreach ( $options as $key => $data ) { if ( isset( $data['options'] ) && is_array( $data['options'] ) ) { $this->register_setting( $data['options'], $key ); } elseif ( 'options' == $key ) { $key = $this->mode; if ( ! empty( $this->option_group ) ) { $key = $this->option_group; } $this->register_setting( $data, $key ); } } } public function get_values( $option_name, $option_group = 'index' ) { $this->option_group = $option_group; $data = $this->get_option_array(); $data = $data['options']; foreach ( $data as $one ) { if ( isset( $one['name'] ) && $one['name'] != $option_name ) { continue; } switch ( $one['type'] ) { case 'checkbox_group': return $one['options']; case 'radio': return $one['radio']; } } return; } public function get_default_value( $option_name, $option_group = 'index' ) { $this->option_group = $option_group; $options = $this->get_option_array(); /** * check options exists? */ if ( ! array_key_exists( 'options', $options ) or ! is_array( $options['options'] ) ) { return null; } /** * default key name */ $default_option_name = $option_name; /** * default name for taxonomies */ if ( array_key_exists( 'type', $options ) && 'taxonomy' == $options['type'] ) { $re = sprintf( '/^%s_\d+_/', $option_group ); $default_option_name = preg_replace( $re, '', $default_option_name ); } foreach ( $options['options'] as $option ) { if ( isset( $option['name'] ) && $option['name'] == $default_option_name ) { return isset( $option['default'] ) ? $option['default'] : null; } } return null; } public function activate() { $options = apply_filters( $this->option_function_name, call_user_func( $this->option_function_name ) ); foreach ( $options as $key => $data ) { foreach ( $data['options'] as $option ) { if ( ( isset( $option['type'] ) && $option['type'] == 'heading' ) or ! isset( $option['name'] ) or ! $option['name'] or ! isset( $option['default'] ) ) { continue; } add_option( $this->option_prefix . $option['name'], $option['default'], '', isset( $option['autoload'] ) ? $option['autoload'] : 'yes' ); } } add_option( $this->option_prefix . 'cache_stamp', date( 'c' ) ); } public function deactivate() { $options = apply_filters( $this->option_function_name, call_user_func( $this->option_function_name ) ); foreach ( $options as $key => $data ) { foreach ( $data['options'] as $option ) { if ( ( isset( $option['type'] ) && 'heading' == $option['type'] ) or ! isset( $option['name'] ) or ! $option['name'] ) { continue; } /** * prevent special options */ if ( isset( $option['dont_deactivate'] ) && $option['dont_deactivate'] ) { continue; } delete_option( $this->option_prefix . $option['name'] ); } } delete_option( $this->option_prefix . 'cache_stamp' ); delete_option( $this->option_prefix . 'version' ); delete_option( $this->option_prefix . 'flush_rules' ); } public function settings_fields( $option_name, $use_prefix = true ) { if ( $use_prefix ) { settings_fields( $this->option_prefix . $option_name ); } else { settings_fields( $option_name ); } } /** * admin_notices */ public function admin_notices() { if ( empty( $this->notices ) ) { return; } foreach ( $this->notices as $notice ) { printf( '

%s

', esc_html( $notice ) ); } } /** * options: add, get, update */ public function add_option( $option_name, $option_value, $autoload = true ) { $autoload = $autoload ? 'yes' : 'no'; add_option( $this->option_prefix . $option_name, $option_value, null, $autoload ); } public function get_option( $option_name, $option_group = 'index', $default_value = null, $forece_default = false ) { $option_value = get_option( $this->option_prefix . $option_name, null ); $default_value = $this->get_default_value( $option_name, $option_group ); if ( ( $default_value || $forece_default ) && is_null( $option_value ) ) { $option_value = $default_value; } return $option_value; } public function get_all_options() { $data = array(); $options = $this->get_option_array(); foreach ( $options['options'] as $option ) { if ( ! array_key_exists( 'name', $option ) || ! $option['name'] ) { continue; } $value = $this->get_option( $option['name'] ); if ( array_key_exists( 'sanitize_callback', $option ) && is_callable( $option['sanitize_callback'] ) ) { $value = call_user_func( $option['sanitize_callback'], $value ); } $data[ $option['name'] ] = $value; } return $data; } /** * Get option name by adding prefix. * * @since 1.0 * @since 2.6.4 Added `hidden` argument. * * @param string $name Name of option. * @param boolean $hidden If this hidden value. * * @return string $option_name */ public function get_option_name( $name, $hidden = false ) { $option_name = sprintf( '%s%s', $this->option_prefix, $name ); if ( $hidden ) { $option_name = sprintf( '_%s', $option_name ); } return $option_name; } public function update_option( $option_name, $option_value ) { /** * delete if option have a default value */ $default_value = $this->get_default_value( $this->option_prefix . $option_name ); if ( $option_value === $default_value ) { delete_option( $this->option_prefix . $option_name ); return; } update_option( $this->option_prefix . $option_name, $option_value ); } /** * update taxonomy options */ public function update_taxonomy_options( $option_group, $term_id ) { /** * check for nonce */ $nonce_value = $this->get_nonce_value(); if ( is_wp_error( $nonce_value ) || ! wp_verify_nonce( $nonce, $this->get_nonce_name() ) ) { return; } /** * groups */ $this->option_group = $option_group; $options = $this->get_option_array(); /** * only for taxonomies */ if ( ! array_key_exists( 'type', $options ) ) { return; } if ( 'taxonomy' != $options['type'] ) { return; } foreach ( $options['options'] as $option ) { if ( ! array_key_exists( 'name', $option ) || ! $option['name'] ) { continue; } $option_name = sprintf( '%s_%s_%s', $option_group, $term_id, $option['name'] ); /** * get & sanitize value * * @since 2.8.6 - added `sanitize_text_field`. */ $value = false; if ( array_key_exists( $this->get_option_name( $option_name ), $_POST ) ) { $value = sanitize_text_field( $_POST[ $this->get_option_name( $option_name ) ] ); } /** * add custom sanitization * * @since 2.8.6 */ if ( array_key_exists( 'sanitize_callback', $option ) && is_callable( $option['sanitize_callback'] ) ) { $value = call_user_func( $option['sanitize_callback'], $value ); } if ( $value ) { $this->update_option( $option_name, $value ); } else { delete_option( $option_name ); } } } /** * helpers */ public function select_page_helper( $name, $show_option_none = false, $post_type = 'page' ) { $args = array( 'echo' => false, 'name' => $this->get_option_name( $name ), 'selected' => $this->get_option( $name ), 'show_option_none' => $show_option_none, 'post_type' => $post_type, ); return wp_dropdown_pages( $args ); } public function select_category_helper( $name, $hide_empty = null, $show_option_none = false ) { $args = array( 'echo' => false, 'name' => $this->get_option_name( $name ), 'selected' => $this->get_option( $name ), 'hierarchical' => true, 'hide_empty' => $hide_empty, ); if ( $show_option_none ) { $args['show_option_none'] = true; } return wp_dropdown_categories( $args ); } public function get_option_group() { return $this->option_group; } private function get_option_index_from_screen() { $screen = get_current_screen(); $key = explode( $this->option_prefix, $screen->id ); if ( 2 != count( $key ) ) { return false; } return $key[1]; } public function show_page( $check_option_name = true, $url = 'options.php' ) { $options = array(); $option_name = 'index'; if ( $check_option_name ) { $option_name = $this->get_option_index_from_screen(); if ( ! $option_name ) { return; } $options = $this->options[ $option_name ]; } else { $options = $this->get_option_array(); } global $screen_layout_columns; $data = array(); ?>

options[ $option_name ] ) ) { ?>
pagehooks[ $option_name ], 'side', $this ); ?>
settings_fields( $option_name ); $this->build_options( $option_name ); ?>

options[ $option_name ] ) ) { include_once ABSPATH . '/wp-admin/includes/meta-boxes.php'; } } public function load_page() { $option_name = $this->get_option_index_from_screen(); if ( ! $option_name ) { return; } /** * check options for key */ if ( ! array_key_exists( $option_name, $this->options ) ) { return; } /** * check metaboxes for key */ if ( ! array_key_exists( 'metaboxes', $this->options[ $option_name ] ) ) { return; } if ( ! count( $this->options[ $option_name ]['metaboxes'] ) ) { return; } /** * ensure, that the needed javascripts been loaded to allow drag/drop, * expand/collapse and hide/show of boxes */ wp_enqueue_script( 'common' ); wp_enqueue_script( 'wp-lists' ); wp_enqueue_script( 'postbox' ); foreach ( $this->options[ $option_name ]['metaboxes'] as $id => $data ) { add_meta_box( $id, $data['title'], $data['callback'], $this->pagehooks[ $option_name ], $data['context'], $data['priority'] ); } /** * wp_enqueue_script */ if ( array_key_exists( 'enqueue_scripts', $this->options[ $option_name ] ) ) { $scripts = array(); if ( is_admin() && isset( $this->options[ $option_name ]['enqueue_scripts']['admin'] ) ) { $scripts = $this->options[ $option_name ]['enqueue_scripts']['admin']; } elseif ( ! is_admin() && isset( $this->options[ $option_name ]['enqueue_scripts']['frontend'] ) ) { $scripts = $this->options[ $option_name ]['enqueue_scripts']['frontend']; } else { $scripts = $this->options[ $option_name ]['enqueue_scripts']; } foreach ( $scripts as $script ) { wp_enqueue_script( $script ); } } /** * wp_enqueue_style */ if ( array_key_exists( 'enqueue_styles', $this->options[ $option_name ] ) ) { $styles = array(); if ( is_admin() && isset( $this->options[ $option_name ]['enqueue_styles']['admin'] ) ) { $styles = $this->options[ $option_name ]['enqueue_styles']['admin']; } elseif ( ! is_admin() && isset( $this->options[ $option_name ]['enqueue_styles']['frontend'] ) ) { $styles = $this->options[ $option_name ]['enqueue_styles']['frontend']; } else { $styles = $this->options[ $option_name ]['enqueue_styles']; } foreach ( $styles as $style ) { wp_enqueue_style( $style ); } } } public function screen_layout_columns( $columns, $screen ) { foreach ( $this->pagehooks as $option_name => $pagehook ) { if ( $screen == $pagehook ) { $columns[ $pagehook ] = 2; } } return $columns; } public function get_options_by_group( $group ) { $opts = array(); $options = $this->get_option_array(); if ( ! isset( $options['options'] ) || empty( $options['options'] ) ) { return $options; } foreach ( $options['options'] as $one ) { if ( ! isset( $one['name'] ) || ! isset( $one['type'] ) ) { continue; } if ( ! isset( $one['group'] ) || $group != $one['group'] ) { continue; } $opts[] = $one; } return $opts; } /** * input types */ public function get_field_by_type( $type, $name, $value = '', $args = array() ) { if ( method_exists( $this, $type ) ) { wp_enqueue_style( __CLASS__ ); if ( ! isset( $args['class'] ) ) { switch ( $type ) { case 'switch_button': wp_enqueue_script( __CLASS__ ); wp_enqueue_style( 'switch_button' ); break; case 'checkbox': case 'radio': break; default: $args['class'] = array( 'large-text' ); break; } } $args['class'][] = sprintf( 'iworks-options-%s', preg_replace( '/_/', '-', esc_attr( $type ) ) ); return $this->$type( $name, $value, $args ); } return sprintf( 'wrong type: %s', esc_html( $type ) ); } private function build_field_attributes( $args ) { $atts = ''; foreach ( $args as $key => $value ) { if ( is_array( $value ) ) { $value = implode( ' ', $value ); } $atts .= sprintf( ' %s="%s"', esc_html( $key ), esc_attr( trim( $value ) ) ); } return $atts; } /** * Print HTML select */ private function select( $name, $value = '', $args = array(), $type = 'text' ) { /** * default value */ if ( isset( $args['default'] ) ) { if ( empty( $value ) ) { $value = $args['default']; } unset( $args['default'] ); } /** * options */ $options = array(); if ( isset( $args['options'] ) ) { $options = $args['options']; unset( $args['options'] ); } if ( empty( $options ) && ! empty( $value ) ) { $options[ $value['value'] ] = $value['label']; } $value_to_check = is_array( $value ) && isset( $value['value'] ) ? $value['value'] : $value; $content = sprintf( ''; return $content; } private function input( $name, $value = '', $args = array(), $type = 'text' ) { /** * default value */ if ( isset( $args['default'] ) ) { if ( empty( $value ) ) { $value = $args['default']; } unset( $args['default'] ); } /** * turn off autocomplete */ if ( 'text' == $type ) { if ( ! isset( $args['autocomplete'] ) ) { $args['autocomplete'] = 'off'; } } /** * before & after */ $keys = array( 'before', 'after' ); foreach ( $keys as $key ) { $$key = ''; if ( isset( $args[ $key ] ) ) { $$key = $args[ $key ]; unset( $args[ $key ] ); } } /** * produce */ return sprintf( '%s%s', $before, esc_attr( $type ), esc_attr( $name ), esc_attr( $value ), $this->build_field_attributes( $args ), $after ); } /** * Checkbox HTML element * * @since 2.6.4 */ private function checkbox( $name, $value = '', $args = array() ) { if ( ! empty( $value ) ) { $args['checked'] = 'checked'; } return $this->input( $name, $value, $args, __FUNCTION__ ); } /** * Switch button element (based on checkbox field). * * @since 2.6.4 */ private function switch_button( $name, $value = '', $args = array() ) { return $this->checkbox( $name, $value, $args ); } private function text( $name, $value = '', $args = array() ) { return $this->input( $name, $value, $args, __FUNCTION__ ); } private function number( $name, $value = '', $args = array() ) { return $this->input( $name, $value, $args, __FUNCTION__ ); } private function button( $name, $value = '', $args = array() ) { return $this->input( $name, $value, $args, __FUNCTION__ ); } private function submit( $name, $value = '', $args = array() ) { return $this->input( $name, $value, $args, __FUNCTION__ ); } private function hidden( $name, $value = '', $args = array() ) { return $this->input( $name, $value, $args, __FUNCTION__ ); } private function date( $name, $value = '', $args = array() ) { if ( ! isset( $args['class'] ) ) { $args['class'] = array(); } $args['class'][] = 'datepicker'; return $this->input( $name, $value, $args ); } private function select2( $name, $value = '', $args = array() ) { if ( isset( $args['data-nonce-action'] ) ) { $args['data-nonce'] = wp_create_nonce( $args['data-nonce-action'] ); unset( $args['data-nonce-action'] ); } if ( ! isset( $args['class'] ) ) { $args['class'] = array(); } $args['class'][] = 'select2'; return $this->select( $name, $value, $args ); } private function textarea( $name, $value = '', $args = array() ) { if ( ! isset( $args['rows'] ) ) { $args['rows'] = 3; } return sprintf( '', esc_attr( $name ), $this->build_field_attributes( $args ), $value ); } private function radio( $name, $value = '', $args = array() ) { $radio = ''; $options = $args['options']; unset( $args['options'] ); /** * default value */ if ( isset( $args['default'] ) && '' == $value ) { $value = $args['default']; } $i = 0; foreach ( $options as $option_value => $input ) { $id = sprintf( '%s%d', $name, $i++ ); $radio .= sprintf( '
  • ', esc_attr( sanitize_title( $value ) ), esc_attr( $id ), esc_attr( $name ), esc_attr( $option_value ), checked( $option_value, $value, false ), esc_attr( $id ), esc_html( is_string( $input ) ? $input : $input['label'] ) ); if ( isset( $input['description'] ) ) { $radio .= '
    '; $radio .= $this->description( '', '', array( 'description' => $input['description'] ) ); } $radio .= '
  • '; } if ( $radio ) { $radio = sprintf( '', $radio ); } return $radio; } private function description( $name, $value = '', $args = array() ) { if ( ! isset( $args['value'] ) || empty( $args['value'] ) ) { return ''; } return sprintf( '

    %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( '
    ', esc_attr( $key ) ); $content .= '
    '; } 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' ); } }