BP_Admin_Tab
描述
源
文件:bp-core /类/ class-bp-admin-tab.php
Abstract类BP_Admin_Tab {/ ** *全局变量名称存储标签实例* * @since buddyboss 1.0.0 * @var字符串* / public $ global_tabs_var ='';/ ** *管理员菜单页面名称* * @since buddyboss 1.0.0 * @var字符串* / public $ menu_page ='';/ ** *选项卡标签名称* * @since buddyboss 1.0.0 * @var字符串* / public $ tab_label ='';/ ** *选项卡url slug * * @since buddyboss 1.0.0 * @var字符串* / public $ tab_name ='';/ ** *选项卡订单* * @since buddyboss 1.0.0 * @var整数* / public $ tab_order = 50;公共功能__construct(){$ this-> initialize();$ this-> register_tab();$ this-> register_hook();if($ this-> is_active()){$ this-> register_fields();do_action('bp_admin_tab_fields_registered',$ this-> tab_name,$以下); add_action( 'bp_admin_init', [$this, 'maybe_save_admin_settings'], 100 ); } } /** * Cutom class initialization * * @since BuddyBoss 1.0.0 */ public function initialize() { // nothing } /** * Determine whether this tab is active * * @since BuddyBoss 1.0.0 */ public function is_active() { return true; } /** * Register the tab to global variable * * @since BuddyBoss 1.0.0 */ public function register_tab() { global ${$this->global_tabs_var}; ${$this->global_tabs_var}[$this->tab_name] = $this; } public function register_hook() { add_action('admin_head', [$this, 'register_admin_script']); } public function register_admin_script() { wp_enqueue_script( 'bp-admin', buddypress()->plugin_url . 'bp-core/admin/js/settings-page.js', [ 'jquery' ], buddypress()->version, true ); wp_localize_script( 'bp-admin', 'BP_ADMIN', [ 'ajax_url' => admin_url( 'admin-ajax.php' ), 'tools' => array( 'default_data' => array( 'submit_button_message' => esc_js( __( 'Are you sure you want to import data? This action is going to alter your database. If this is a live website you may want to create a backup of your database first.', 'buddyboss' ) ), 'clear_button_message' => esc_js( __( "Are you sure you want to delete all Default Data content? Content that was created by you and others, and not by this default data installer, will not be deleted.", 'buddyboss' ) ), ) ), ] ); } /** * Register setting fields belong to this group * * @since BuddyBoss 1.0.0 */ public function register_fields() { // nothing } /** * Save the fields if it's form post request * * @since BuddyBoss 1.0.0 */ public function maybe_save_admin_settings() { if ( ! $this->is_saving() ) { return false; } check_admin_referer( $this->tab_name . '-options' ); $this->settings_save(); do_action( 'bp_admin_tab_setting_save', $this->tab_name, $this ); $this->settings_saved(); do_action( 'bp_admin_tab_setting_saved', $this->tab_name, $this ); } /** * Determine whether current request is saving on the current tab * * @since BuddyBoss 1.0.0 */ public function is_saving() { if ( ! isset( $_GET['page'] ) || ! isset( $_POST['submit'] ) ) { return false; } if ( $this->menu_page != $_GET['page'] ) { return false; } if ( $this->tab_name != $this->get_active_tab() ) { return false; } return true; } /** * Method to save the fields * * By default it'll loop throught the setting group's fields, but allow * extended classes to have their own logic if needed * * @since BuddyBoss 1.0.0 */ public function settings_save() { global $wp_settings_fields; $fields = isset( $wp_settings_fields[ $this->tab_name ] )? (array) $wp_settings_fields[ $this->tab_name ] : []; foreach( $fields as $section => $settings ) { foreach( $settings as $setting_name => $setting ) { $value = isset( $_POST[$setting_name] ) ? $_POST[$setting_name] : ''; bp_update_option( $setting_name, $value ); } } } /** * Method trigger after data are saved * * @since BuddyBoss 1.0.0 */ abstract public function settings_saved(); /** * Method that should return the current active tab * * @since BuddyBoss 1.0.0 */ abstract public function get_active_tab(); /** * Return if the tab should be visible. Default to if there's any setting fields * * @since BuddyBoss 1.0.0 */ public function is_tab_visible() { return $this->has_fields(); } /** * Return if this tab has setting fields * * @since BuddyBoss 1.0.0 */ public function has_fields() { global $wp_settings_fields; return ! empty( $wp_settings_fields[ $this->tab_name ] ); } /** * Output the form html on the setting page (not including tab and page title) * * @since BuddyBoss 1.0.0 */ public function form_html() { settings_fields( $this->tab_name ); $this->bp_custom_do_settings_sections( $this->tab_name ); printf( '', esc_attr__( 'Save Settings', 'buddyboss' ) ); } /** * Add a wp setting section into current tab. Chainable * * @since BuddyBoss 1.0.0 */ public function add_section( $id, $title, $callback = '__return_null' ) { add_settings_section( $id, $title, $callback, $this->tab_name ); $this->active_section = $id; return $this; } /** * Add a wp setting field to a wp setting section. Chainable * * @since BuddyBoss 1.0.0 */ public function add_field( $name, $label, $callback, $field_args = [], $callback_args = [], $id = null ) { if ( !$id ) { $id = $this->active_section; } add_settings_field( $name, $label, $callback, $this->tab_name, $id, $callback_args ); register_setting( $this->tab_name, $name, $field_args ); return $this; } /** * Alias to add input text box field * * @since BuddyBoss 1.0.0 */ public function add_input_field( $name, $label, $callback_args = [], $field_args = 'sanitize_text_field', $id = null ) { $callback = [$this, 'render_input_field_html']; $callback_args = wp_parse_args( $callback_args, [ 'input_type' => 'text', 'input_name' => $this->get_input_name( $name ), 'input_id' => $this->get_input_id( $name ), 'input_description' => '', 'input_value' => $this->get_input_value( $name ), 'input_placeholder' => '' ] ); return $this->add_field( $name, $label, $callback, $field_args, $callback_args, $id ); } /** * Alias to add input check box field * * @since BuddyBoss 1.0.0 */ public function add_checkbox_field( $name, $label, $callback_args = [], $field_args = 'intval', $id = null ) { $callback = [$this, 'render_checkbox_field_html']; $callback_args = wp_parse_args( $callback_args, [ 'input_name' => $this->get_input_name( $name ), 'input_id' => $this->get_input_id( $name ), 'input_text' => '', 'input_description' => '', 'input_value' => $this->get_input_value( $name, null ), 'input_default' => 0, 'input_run_js' => false, ] ); return $this->add_field( $name, $label, $callback, $field_args, $callback_args, $id ); } /** * Alias to add input select field * * @since BuddyBoss 1.0.0 */ public function add_select_field( $name, $label, $callback_args = [], $field_args = 'sanitize_text_field', $id = null ) { $callback = [$this, 'render_select_field_html']; $callback_args = wp_parse_args( $callback_args, [ 'input_name' => $this->get_input_name( $name ), 'input_id' => $this->get_input_id( $name ), 'input_options' => [], 'input_description' => '', 'input_value' => $this->get_input_value( $name, null ), 'input_default' => 0, ] ); return $this->add_field( $name, $label, $callback, $field_args, $callback_args, $id ); } /** * Output the input field html based on the arguments * * @since BuddyBoss 1.0.0 */ public function render_input_field_html( $args ) { printf( ' %s', $args['input_name'], $args['input_type'], $args['input_id'], $args['input_value'], $args['input_placeholder'], $args['input_description']? $this->render_input_description( $args['input_description'] ) : '' ); } /** * Output the checkbox field html based on the arguments * * @since BuddyBoss 1.0.0 */ public function render_checkbox_field_html( $args ) { $input_value = is_null( $args['input_value'] )? $args['input_default'] : $args['input_value']; printf( ' %6$s ', $args['input_id'], $args['input_name'], checked( (bool) $input_value, true, false ), $args['input_run_js']? "data-run-js-condition=\"{$args['input_run_js']}\"" : '', $args['input_text'], $args['input_description']? $this->render_input_description( $args['input_description'] ) : '' ); } /** * Output the select field html based on the arguments * * @since BuddyBoss 1.0.0 */ public function render_select_field_html( $args ) { $input_value = is_null( $args['input_value'] )? $args['input_default'] : $args['input_value']; $input_name = $args['input_name']; printf( ''; if ( $args['input_description'] ) { echo $this->render_input_description($args['input_description']); } } protected function render_input_description( $text ) { return "
{$text}
"; } protected function get_input_name( $name ) { return $name; } protected function get_input_id( $id ) { return sanitize_title( $id ); } protected function get_input_value( $key, $default = '' ) { return bp_get_option( $key, $default ); } /** * Prints out all settings sections added to a particular settings page * * Part of the Settings API. Use this in a settings page callback function * to output all the sections and fields that were added to that $page with * add_settings_section() and add_settings_field() * * @global $wp_settings_sections Storage array of all settings sections added to admin pages * @global $wp_settings_fields Storage array of settings fields and info about their pages/sections * @since BuddyPress 2.7.0 * * @param string $page The slug name of the page whose settings sections you want to output */ public function bp_custom_do_settings_sections( $page ) { global $wp_settings_sections, $wp_settings_fields; if ( ! isset( $wp_settings_sections[$page] ) ) return; foreach ( (array) $wp_settings_sections[$page] as $section ) { echo ""; if ( $section['title'] ) { echo "'; } } /** * Print out the settings fields for a particular settings section * * Part of the Settings API. Use this in a settings page to output * a specific section. Should normally be called by do_settings_sections() * rather than directly. * * @global $wp_settings_fields Storage array of settings fields and their pages/sections * * @since BuddyPress 2.7.0 * * @param string $page Slug title of the admin page who's settings fields you want to show. * @param string $section Slug title of the settings section who's fields you want to show. */ public function bp_custom_do_settings_fields($page, $section) { global $wp_settings_fields; if ( ! isset( $wp_settings_fields[$page][$section] ) ) return; foreach ( (array) $wp_settings_fields[$page][$section] as $field ) { $class = ''; if ( ! empty( $field['args']['class'] ) ) { $class = ' class="' . esc_attr( $field['args']['class'] ) . '"'; } echo "{$section['title']}
\n"; } if ( $section['callback'] ) { call_user_func( $section['callback'], $section ); } if ( ! isset( $wp_settings_fields ) || !isset( $wp_settings_fields[$page] ) || !isset( $wp_settings_fields[$page][$section['id']] ) ) { continue; } echo ''; $this->bp_custom_do_settings_fields( $page, $section['id'] ); echo '
"; if ( ! empty( $field['args']['label_for'] ) ) { echo ' '; } } }'; } else { echo ' ' . $field['title'] . ' '; } echo ''; call_user_func($field['callback'], $field['args']); echo ' '; echo '
方法
- __construct
- add_checkbox_field-别名添加输入复选框字段
- add_field-添加wp设置字段到wp设置部分。证明
- add_input_field-添加输入文本框字段的别名
- add_section-添加wp设置部分到当前选项卡。证明
- add_select_field-添加输入选择字段的别名
- bp_custom_do_settings_fields-打印特定设置部分的设置字段
- bp_custom_do_settings_sections打印出所有添加到特定设置页面的设置部分
- form_html-在设置页面上输出表单html(不包括制表符和页面标题)
- get_active_tab返回当前活动选项卡的方法
- get_input_id
- get_input_name
- get_input_value
- has_fields-如果这个选项卡有设置字段返回
- 初始化-自定义类初始化
- is_active—确认该选项卡是否激活
- is_saving-确定当前请求是否保存在当前选项卡上
- is_tab_visible-如果选项卡应该是可见的返回。如果有任何设置字段,默认为
- maybe_save_admin_settings-保存字段,如果它是表单post请求
- register_admin_script
- register_fields—注册设置字段属于该组
- register_hook
- register_tab—将标签注册为全局变量
- render_checkbox_field_html-根据参数输出复选框字段html
- render_input_description
- render_input_field_html-根据参数输出输入字段html
- render_select_field_html-根据参数输出select字段html
- settings_save—保存字段的方法
- settings_saved—数据保存后的方法触发