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(), $this->get_menu_position() ); } 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'] : ""; $details = isset( $option['details'] ) ? $option['details'] : ""; if( empty( $id ) ) return; try{ if( isset($option['type']) && $option['type'] === 'select' ) $this->pick_settings_generate_select( $option ); elseif( isset($option['type']) && $option['type'] === 'checkbox') $this->pick_settings_generate_checkbox( $option ); elseif( isset($option['type']) && $option['type'] === 'radio') $this->pick_settings_generate_radio( $option ); elseif( isset($option['type']) && $option['type'] === 'textarea') $this->pick_settings_generate_textarea( $option ); elseif( isset($option['type']) && $option['type'] === 'number' ) $this->pick_settings_generate_number( $option ); elseif( isset($option['type']) && $option['type'] === 'text' ) $this->pick_settings_generate_text( $option ); elseif( isset($option['type']) && $option['type'] === 'colorpicker')$this->pick_settings_generate_colorpicker( $option ); elseif( isset($option['type']) && $option['type'] === 'datepicker') $this->pick_settings_generate_datepicker( $option ); elseif( isset($option['type']) && $option['type'] === 'select2') $this->pick_settings_generate_select2( $option ); elseif( isset($option['type']) && $option['type'] === 'range') $this->pick_settings_generate_range( $option ); elseif( isset($option['type']) && $option['type'] === 'media') $this->pick_settings_generate_media( $option ); do_action( "pick_settings_$id", $option ); if( !empty( $details ) ) echo "

$details

"; } catch(Pick_error $e) { echo $e->get_error_message(); } } public function pick_settings_generate_media( $option ){ $id = isset( $option['id'] ) ? $option['id'] : ""; $value = get_option( $id ); $media_url = wp_get_attachment_url( $value ); $media_type = get_post_mime_type( $value ); $media_title= get_the_title( $value ); wp_enqueue_media(); echo "
"; if( "audio/mpeg" == $media_type ){ echo "
"; echo "
$media_title
"; } else { echo ""; } echo "
"; echo ""; echo "
Upload
"; echo ""; } public function pick_settings_generate_range( $option ){ $id = isset( $option['id'] ) ? $option['id'] : ""; $min = isset( $option['min'] ) ? $option['min'] : 1; $max = isset( $option['max'] ) ? $option['max'] : 100; $value = get_option( $id ); $value = empty( $value ) ? 0 : $value; echo ""; echo "$value"; echo " "; } public function pick_settings_generate_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', USER_PROFILE_PLUGIN_URL.'assets/admin/css/select2.min.css' ); wp_enqueue_script('select2', USER_PROFILE_PLUGIN_URL.'assets/admin/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 pick_settings_generate_datepicker( $option ){ $id = isset( $option['id'] ) ? $option['id'] : ""; $placeholder = isset( $option['placeholder'] ) ? $option['placeholder'] : ""; $value = get_option( $id ); wp_enqueue_style( 'jquery-ui', USER_PROFILE_PLUGIN_URL.'assets/admin/css/jquery-ui.css' ); echo ""; echo ""; } public function pick_settings_generate_colorpicker( $option ){ $id = isset( $option['id'] ) ? $option['id'] : ""; $placeholder = isset( $option['placeholder'] ) ? $option['placeholder'] : ""; $value = get_option( $id ); echo ""; echo ""; } public function pick_settings_generate_text( $option ){ $id = isset( $option['id'] ) ? $option['id'] : ""; $placeholder = isset( $option['placeholder'] ) ? $option['placeholder'] : ""; $value = get_option( $id ); echo ""; } public function pick_settings_generate_number( $option ){ $id = isset( $option['id'] ) ? $option['id'] : ""; $placeholder = isset( $option['placeholder'] ) ? $option['placeholder'] : ""; $value = get_option( $id ); echo ""; } public function pick_settings_generate_textarea( $option ){ $id = isset( $option['id'] ) ? $option['id'] : ""; $placeholder = isset( $option['placeholder'] ) ? $option['placeholder'] : ""; $value = get_option( $id ); echo ""; } public function pick_settings_generate_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, $option ); $value = get_option( $id ); echo ""; } public function pick_settings_generate_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 ); $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_generate_radio( $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 ); $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 ) : $page_settings = isset( $page['page_settings'] ) ? $page['page_settings'] : array(); foreach( $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 ); $nav_menu_url_args = user_profile_recursive_sanitize_arr($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() ); do_action( $this->get_current_page() ); $show_submit_button = $this->show_submit_button(); if( $show_submit_button ) submit_button(); echo "
"; if( $this->show_sidebar() ) { echo "
"; echo "
"; echo ""; echo apply_filters( 'pick_settings_filter_sidebar_title', "Pick settings sidebar" ); echo "
"; do_action( 'pick_settings_sidebar_content_before' ); echo "
"; do_action( 'pick_settings_sidebar_content' ); echo "
"; do_action( 'pick_settings_sidebar_content_after' ); echo "
"; echo "
"; echo " "; } echo "
"; } // Default Functions public function generate_args_from_string( $string, $option ){ if( strpos( $string, 'PICK_PAGES_ARRAY' ) !== false ) return $this->get_pages_array(); if( strpos( $string, 'PICK_TAX_' ) !== false ) return $this->get_taxonomies_array( $string, $option ); if( strpos( $string, 'PICK_POSTS_' ) !== false ) return $this->get_posts_array( $string, $option ); return array(); } public function get_posts_array( $string, $option ){ $arr_posts = array(); preg_match_all( "/\%([^\]]*)\%/", $string, $matches ); if( isset( $matches[1][0] ) ) $post_type = $matches[1][0]; else throw new Pick_error('Invalid post type declaration!'); if( ! post_type_exists( $post_type ) ) throw new Pick_error("Post type $post_type doesn't exists!"); $wp_query = isset( $option['wp_query'] ) ? $option['wp_query'] : array(); $ppp = isset( $wp_query['posts_per_page'] ) ? $option['posts_per_page'] : -1; $wp_query = array_merge( $wp_query, array( 'post_type' => $post_type, 'posts_per_page' => $ppp ) ); $posts = get_posts( $wp_query ); foreach( $posts as $post ) $arr_posts[ $post->ID ] = $post->post_title; return $arr_posts; } public function get_taxonomies_array( $string, $option ){ $taxonomies = array(); preg_match_all( "/\%([^\]]*)\%/", $string, $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; } private function show_sidebar(){ return isset( $this->data['show_sidebar'] ) ? $this->data['show_sidebar'] : false; } private function show_submit_button(){ return isset( $this->get_pages()[$this->get_current_page()]['show_submit'] ) ? $this->get_pages()[$this->get_current_page()]['show_submit'] : true; } 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'] ) ) $pages = $this->data['pages']; else return array(); $pages_sorted = array(); foreach ($pages as $page_key => $page) $pages_sorted[$page_key] = isset( $page['priority'] ) ? $page['priority'] : 0; array_multisort($pages_sorted, SORT_ASC, $pages); return $pages; } 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_position(){ if( isset( $this->data['position'] ) ) return $this->data['position']; else return null; } private function get_menu_icon(){ if( isset( $this->data['menu_icon'] ) ) return $this->data['menu_icon']; else return null; } 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()}

"; } } }