data = &$args; if( $this->add_in_menu() ) { add_action( 'admin_menu', array( $this, 'add_menu_in_admin_menu' ), 12 ); } add_action( 'admin_init', array( $this, 'pick_settings_display_fields' ), 12 ); add_filter( 'whitelist_options', array( $this, 'pick_settings_whitelist_options' ), 99, 1 ); add_action( 'admin_enqueue_scripts', array( $this, 'pick_enqueue_color_picker' ) ); } public function add_menu_in_admin_menu() { if( "main" == $this->get_menu_type() ) { add_menu_page( $this->get_menu_name(), $this->get_menu_title(), $this->get_capability(), $this->get_menu_slug(), array( $this, 'pick_settings_display_function' ), $this->get_menu_icon() ); } if( "submenu" == $this->get_menu_type() ) { add_submenu_page( $this->get_parent_slug(), $this->get_page_title(), $this->get_menu_title(), $this->get_capability(), $this->get_menu_slug(), array( $this, 'pick_settings_display_function' ) ); } } public function pick_enqueue_color_picker(){ wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_script( 'wp-color-picker' ); wp_enqueue_script( 'jquery-ui-datepicker' ); } public function pick_settings_display_fields() { foreach( $this->get_settings_fields() as $key => $setting ): add_settings_section( $key, isset( $setting['title'] ) ? $setting['title'] : "", array( $this, 'pick_settings_section_callback' ), $this->get_current_page() ); foreach( $setting['options'] as $option ) : add_settings_field( $option['id'], $option['title'], array($this,'pick_settings_field_generator'), $this->get_current_page(), $key, $option ); endforeach; endforeach; } public function pick_settings_field_generator( $option ) { $id = isset( $option['id'] ) ? $option['id'] : ""; $type = isset( $option['type'] ) ? $option['type'] : ""; $details = isset( $option['details'] ) ? $option['details'] : ""; if( empty( $id ) ) return; try{ //var_dump($type); if( isset($option['type']) && $option['type'] === 'select' ) $this->generate_field_select( $option ); elseif( isset($option['type']) && $option['type'] === 'select_multi') $this->generate_field_select_multi( $option ); elseif( isset($option['type']) && $option['type'] === 'select2') $this->generate_field_select2( $option ); elseif( isset($option['type']) && $option['type'] === 'checkbox') $this->generate_field_checkbox( $option ); elseif( isset($option['type']) && $option['type'] === 'radio') $this->generate_field_radio( $option ); elseif( isset($option['type']) && $option['type'] === 'textarea') $this->generate_field_textarea( $option ); elseif( isset($option['type']) && $option['type'] === 'number' ) $this->generate_field_number( $option ); elseif( isset($option['type']) && $option['type'] === 'text' ) $this->generate_field_text( $option ); elseif( isset($option['type']) && $option['type'] === 'text_multi' ) $this->generate_field_text_multi( $option ); elseif( isset($option['type']) && $option['type'] === 'colorpicker') $this->generate_field_colorpicker( $option ); elseif( isset($option['type']) && $option['type'] === 'datepicker') $this->generate_field_datepicker( $option ); elseif( isset($option['type']) && $option['type'] === 'repeater') $this->generate_field_repeater( $option ); elseif( isset($option['type']) && $option['type'] === 'faq') $this->generate_field_faq( $option ); elseif( isset($option['type']) && $option['type'] === 'addons_grid') $this->generate_field_addons_grid( $option ); elseif( isset($option['type']) && $option['type'] === 'custom_html') $this->generate_field_custom_html( $option ); elseif( isset($option['type']) && $option['type'] === $type ) do_action( "wp_admin_settings_custom_field_$type", $option ); if( !empty( $details ) ) echo "

$details

"; } catch(Pick_error $e) { echo $e->get_error_message(); } } public function generate_field_datepicker( $option ){ $id = isset( $option['id'] ) ? $option['id'] : ""; $placeholder = isset( $option['placeholder'] ) ? $option['placeholder'] : ""; $value = get_option( $id ); wp_register_style( 'jquery-ui', 'http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css' ); wp_enqueue_style( 'jquery-ui' ); echo ""; echo ""; } public function generate_field_colorpicker( $option ){ $id = isset( $option['id'] ) ? $option['id'] : ""; $placeholder = isset( $option['placeholder'] ) ? $option['placeholder'] : ""; $value = get_option( $id ); echo ""; echo ""; } public function generate_field_text( $option ){ $id = isset( $option['id'] ) ? $option['id'] : ""; $placeholder = isset( $option['placeholder'] ) ? $option['placeholder'] : ""; $value = get_option( $id ); echo ""; } public function generate_field_text_multi( $option ){ $id = isset( $option['id'] ) ? $option['id'] : ""; $placeholder = isset( $option['placeholder'] ) ? $option['placeholder'] : ""; $values = get_option( $id ); //var_dump($values); ?> Add
X
X
"; } public function generate_field_textarea( $option ){ $id = isset( $option['id'] ) ? $option['id'] : ""; $placeholder = isset( $option['placeholder'] ) ? $option['placeholder'] : ""; $value = get_option( $id ); echo ""; } public function generate_field_select( $option ){ $id = isset( $option['id'] ) ? $option['id'] : ""; $args = isset( $option['args'] ) ? $option['args'] : array(); $args = is_array( $args ) ? $args : $this->generate_args_from_string( $args ); $value = get_option( $id ); echo ""; } public function generate_field_select_multi( $option ){ $id = isset( $option['id'] ) ? $option['id'] : ""; $args = isset( $option['args'] ) ? $option['args'] : array(); $args = is_array( $args ) ? $args : $this->generate_args_from_string( $args ); $option_value = get_option( $id ); echo ""; } public function generate_field_select2( $option ){ $id = isset( $option['id'] ) ? $option['id'] : ""; $args = isset( $option['args'] ) ? $option['args'] : array(); $args = is_array( $args ) ? $args : $this->generate_args_from_string( $args, $option ); $value = get_option( $id ); $multiple = isset( $option['multiple'] ) ? $option['multiple'] : ''; wp_enqueue_style('select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css' ); wp_enqueue_script('select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js', array('jquery') ); echo $multiple ? ""; foreach( $args as $key => $name ): if( $multiple ) $selected = in_array( $key, $value ) ? "selected" : ""; else $selected = $value == $key ? "selected" : ""; echo ""; endforeach; echo ""; echo ""; } public function generate_field_checkbox( $option ){ $id = isset( $option['id'] ) ? $option['id'] : ""; $args = isset( $option['args'] ) ? $option['args'] : array(); $args = is_array( $args ) ? $args : $this->generate_args_from_string( $args ); $option_value = get_option( $id ); //var_dump($option_value); echo "
"; foreach( $args as $key => $value ): $checked = is_array( $option_value ) && in_array( $key, $option_value ) ? "checked" : ""; echo "
"; endforeach; echo "
"; } public function generate_field_faq( $option ){ $id = isset( $option['id'] ) ? $option['id'] : ""; $args = isset( $option['args'] ) ? $option['args'] : array(); $args = is_array( $args ) ? $args : $this->generate_args_from_string( $args ); $option_value = get_option( $id ); //var_dump($option_value); ?>
$value ): $title = $value['title']; $link = $value['link']; $content = $value['content']; ?>
$grid_item){ $title = isset($grid_item['title']) ? $grid_item['title'] : ''; $link = isset($grid_item['link']) ? $grid_item['link'] : ''; $thumb = isset($grid_item['thumb']) ? $grid_item['thumb'] : ''; ?>
$field ): $type = isset($field['type']) ? $field['type'] : ''; $title = isset($field['title']) ? $field['title'] : ''; ?>
generate_args_from_string( $args ); $option_value = get_option( $id ); echo "
"; foreach( $args as $key => $value ): $checked = is_array( $option_value ) && in_array( $key, $option_value ) ? "checked" : ""; echo "
"; endforeach; echo "
"; } public function pick_settings_section_callback( $section ) { $data = isset( $section['callback'][0]->data ) ? $section['callback'][0]->data : array(); $description = isset( $data['pages'][$this->get_current_page()]['page_settings'][$section['id']]['description'] ) ? $data['pages'][$this->get_current_page()]['page_settings'][$section['id']]['description'] : ""; echo $description; } public function pick_settings_whitelist_options( $whitelist_options ){ foreach( $this->get_pages() as $page_id => $page ): foreach( $page['page_settings'] as $section ): foreach( $section['options'] as $option ): $whitelist_options[$page_id][] = $option['id']; endforeach; endforeach; endforeach; return $whitelist_options; } public function pick_settings_display_function(){ echo "
"; echo "

{$this->get_menu_page_title()}


"; parse_str( $_SERVER['QUERY_STRING'], $nav_menu_url_args ); global $pagenow; settings_errors(); $tab_count = 0; echo ""; echo "
"; settings_fields( $this->get_current_page() ); do_settings_sections( $this->get_current_page() ); submit_button(); echo "
"; echo "
"; } // Default Functions public function generate_args_from_string( $string ){ if( strpos( $string, 'PICK_PAGES_ARRAY' ) !== false ) return $this->get_pages_array(); if( strpos( $string, 'PICK_TAX_' ) !== false ) return $this->get_taxonomies_array( $string ); return array(); } public function get_taxonomies_array( $string ){ $taxonomies = array(); preg_match_all( "/\%([^\]]*)\%/", $string, $matches ); var_dump($matches); if( isset( $matches[1][0] ) ) $taxonomy = $matches[1][0]; else throw new Pick_error('Invalid taxonomy declaration !'); if( ! taxonomy_exists( $taxonomy ) ) throw new Pick_error("Taxonomy $taxonomy doesn't exists !"); $terms = get_terms( $taxonomy, array( 'hide_empty' => false, ) ); foreach( $terms as $term ) $taxonomies[ $term->term_id ] = $term->name; return $taxonomies; } public function get_pages_array(){ $pages_array = array(); foreach( get_pages() as $page ) $pages_array[ $page->ID ] = $page->post_title; return apply_filters( 'FILTER_PICK_PAGES_ARRAY', $pages_array ); } // Get Data from Dataset // public function get_option_ids(){ $option_ids = array(); foreach( $this->get_pages() as $page ): $setting_sections = isset( $page['page_settings'] ) ? $page['page_settings'] : array(); foreach( $setting_sections as $setting_section ): $options = isset( $setting_section['options'] ) ? $setting_section['options'] : array(); foreach( $options as $option ) $option_ids[] = isset( $option['id'] ) ? $option['id'] : ''; endforeach; endforeach; return $option_ids; } public function get_current_page(){ $all_pages = $this->get_pages(); $page_keys = array_keys($all_pages); $default_tab = ! empty( $all_pages ) ? reset( $page_keys ) : ""; return isset( $_GET['tab'] ) ? sanitize_text_field($_GET['tab']) : $default_tab; } private function get_menu_type(){ if( isset( $this->data['menu_type'] ) ) return $this->data['menu_type']; else return "main"; } private function get_pages(){ if( isset( $this->data['pages'] ) ) return $this->data['pages']; else return array(); } private function get_settings_fields(){ if( isset( $this->get_pages()[$this->get_current_page()]['page_settings'] ) ) return $this->get_pages()[$this->get_current_page()]['page_settings']; else return array(); } private function get_settings_name(){ if( isset( $this->data['settings_name'] ) ) return $this->data['settings_name']; else return "my_custom_settings"; } private function get_menu_icon(){ if( isset( $this->data['menu_icon'] ) ) return $this->data['menu_icon']; else return ""; } private function get_menu_slug(){ if( isset( $this->data['menu_slug'] ) ) return $this->data['menu_slug']; else return "my-custom-settings"; } private function get_capability(){ if( isset( $this->data['capability'] ) ) return $this->data['capability']; else return "manage_options"; } private function get_menu_page_title(){ if( isset( $this->data['menu_page_title'] ) ) return $this->data['menu_page_title']; else return "My Custom Menu"; } private function get_menu_name(){ if( isset( $this->data['menu_name'] ) ) return $this->data['menu_name']; else return "Menu Name"; } private function get_menu_title(){ if( isset( $this->data['menu_title'] ) ) return $this->data['menu_title']; else return "Menu Title"; } private function get_page_title(){ if( isset( $this->data['page_title'] ) ) return $this->data['page_title']; else return "Page Title"; } private function add_in_menu(){ if( isset( $this->data['add_in_menu'] ) && $this->data['add_in_menu'] ) return true; else return false; } private function get_parent_slug(){ if( isset( $this->data['parent_slug'] ) && $this->data['parent_slug'] ) return $this->data['parent_slug']; else return ""; } } } if( ! class_exists( 'Pick_error' ) ) { class Pick_error extends Exception { public function __construct($message, $code = 0, Exception $previous = null) { parent::__construct($message, $code, $previous); } public function get_error_message(){ return "

{$this->getMessage()}

"; } } }