解决方案盒/WordPress 设置框架

1.3.0 2024-06-07 17:36 UTC

This package is auto-updated.

Last update: 2024-09-07 18:18:56 UTC


README

Total Downloads Latest Stable Version License

WordPress 设置框架旨在通过有效地创建围绕 WordPress 设置 API 的包装,简化创建 WordPress 插件设置页面的过程。

实际上,这个仓库是一个演示如何在你的插件中实现 SBSA 的有效插件。有关详细信息,请参阅 src/sbsa-test.php

如果你在插件中使用自动加载,则可以使用此框架与 composer。

安装

composer require solutionbox/wordpress-settings-framework

设置您的插件

  1. 通过 composer 安装此包。
  2. 在您的插件根目录中创建一个 "settings" 文件夹。
  3. 在您的新 "settings" 文件夹中创建一个设置文件(例如 settings-general.php

现在,您可以像这样设置您的插件

use  Solution_Box_Settings;

class SBSATest {
	/**
	 * @var string
	 */
	private $plugin_path;

	/**
	 * @var WordPressSettingsFramework
	 */
	private $sbsa;

	/**
	 * SBSATest constructor.
	 */
	function __construct() {
		$this->plugin_path = plugin_dir_path( __FILE__ );

		$this->sbsa = new Solution_Box_Settings\SettingsAPI( $this->plugin_path . 'src/settings/example-settings.php', 'my_example_settings' );

		// Add admin menu
		add_action( 'admin_menu', array( $this, 'add_settings_page' ), 20 );
		
		// Add an optional settings validation filter (recommended)
		add_filter( $this->sbsa->get_option_group() . '_settings_validate', array( &$this, 'validate_settings' ) );
	}

	/**
	 * Add WooCommerce sub settings page.
	 */
	function add_settings_page() {
		$this->sbsa->add_settings_page( array(
			'parent_slug' => 'woocommerce',
			'page_title'  => __( 'Page Title', 'text-domain' ),
			'menu_title'  => __( 'menu Title', 'text-domain' ),
			'capability'  => 'manage_woocommerce',
		) );
	}

	/**
	 * Validate settings.
	 * 
	 * @param $input
	 *
	 * @return mixed
	 */
	function validate_settings( $input ) {
		// Do your settings validation here
		// Same as $sanitize_callback from http://codex.wordpress.org/Function_Reference/register_setting
		return $input;
	}

	// ...
}

您可以像这样访问您的设置值

// Get settings
$this->sbsa->get_settings();

这将获取保存的设置值或您在设置文件中设置的默认值。

或通过获取单个设置

// Get individual setting
$setting = Solution_Box_Settings\SettingsAPI::get_setting( 'prefix_settings_general', 'general', 'text' );

设置文件

设置文件通过以下格式向全局 $sbsa_settings 数组填充数据来工作

$sbsa_settings[] = array(
    'section_id' => 'general', // The section ID (required)
    'section_title' => 'General Settings', // The section title (required)
    'section_description' => 'Some intro description about this section.', // The section description (optional)
    'section_order' => 5, // The order of the section (required)
    'fields' => array(
        array(
            'id' => 'text',
            'title' => 'Text',
            'desc' => 'This is a description.',
            'placeholder' => 'This is a placeholder.',
            'type' => 'text',
            'default' => 'This is the default value'
        ),
        array(
            'id' => 'select',
            'title' => 'Select',
            'desc' => 'This is a description.',
            'type' => 'select',
            'default' => 'green',
            'choices' => array(
                'red' => 'Red',
                'green' => 'Green',
                'blue' => 'Blue'
            )
        ),

        // add as many fields as you need...

    )
);

有效的 fields 值包括

  • id - 字段 ID
  • title - 字段标题
  • desc - 字段描述
  • placeholder - 字段占位符
  • type - 字段类型(文本/密码/文本区域/选择/单选按钮/复选框/复选框组/颜色/文件/编辑器/代码编辑器)
  • default - 默认值(或选中的选项)
  • choices - 选项数组(用于选择/单选按钮/复选框组)
  • mimetype - Code Mirror 接受的任何有效 MIME 类型(用于代码编辑器)

请参阅 settings/example-settings.php 了解可能的值示例。

API 详细信息

new Solution_Box_Settings\SettingsAPI( string $settings_file [, string $option_group = ''] )

基于设置文件创建新的设置 option_group

  • $settings_file - 设置文件的路径
  • $option_group - 可选的 "option_group" 覆盖(默认情况下,这将设置为设置文件的 basename)
 Solution_Box_Settings\SettingsAPI::get_setting( $option_group, $section_id, $field_id )

从选项组获取设置

  • $option_group - 选项组 ID。
  • $section_id - 部分ID(当使用选项卡时,请将其更改为 [{$tab_id}_{$section_id}]
  • $field_id - 字段 ID。
Solution_Box_Settings\SettingsAPI::delete_settings( $option_group )

从选项组删除所有保存的设置

  • $option_group - 选项组 ID

动作 & 过滤器

过滤器

  • sbsa_register_settings_[option_group] - 用于注册您的设置的过滤器。请参阅 settings/example-settings.php 以获取示例。
  • [option_group]_settings_validate - 基本上等同于 $sanitize_callbackregister_setting。使用 $sbsa->get_option_group() 获取选项组 ID。
  • sbsa_defaults_[option_group] - 设置字段的默认参数

动作

  • sbsa_before_settings_page_[option_group] - 在输出设置页面 HTML 之前
  • sbsa_after_settings_page_[option_group] - 在输出设置页面 HTML 之后
  • sbsa_before_settings_page_header_[option_group] - 在输出设置页面标题 HTML 之前
  • sbsa_after_settings_page_header_[option_group] - 在输出设置页面标题 HTML 之后
  • sbsa_settings_sections_args_[option_group] - 部分的额外参数,用于用 HTML 和额外类包装部分 更多信息
  • sbsa_before_field_[option_group] - 在输出字段 HTML 之前
  • sbsa_before_field_[option_group]_[field_id] - 在输出字段HTML之前
  • sbsa_after_field_[option_group] - 在输出字段HTML之后
  • sbsa_after_field_[option_group]_[field_id] - 在输出字段HTML之后
  • sbsa_before_settings_[option_group] - 在输出设置表单HTML之前
  • sbsa_after_settings_[option_group] - 在输出设置表单HTML之后
  • sbsa_before_tabless_settings_[option_group] - 在输出设置部分HTML之前
  • sbsa_after_tabless_settings_[option_group] - 在输出设置部分HTML之后
  • sbsa_before_settings_fields_[option_group] - 在输出设置表单字段HTML之前(在 <form> 内部)
  • sbsa_do_settings_sections_[option_group] - 设置表单字段HTML输出(在 <form> 内部)
  • sbsa_before_tab_links_[option_group] - 在输出标签HTML之前
  • sbsa_after_tab_links_[option_group] - 在输出标签HTML之后

示例

示例1:无表格设置 image

示例2:标签式设置 image

致谢

WordPress设置框架是从 iconicwp 克隆的,然后将其转换为具有更多功能的php包。

请通过 报告错误 和提交 拉取请求 来贡献。

想要表达感谢吗? 考虑给我打赏