hungluu/simwp

此包已被放弃,不再维护。没有推荐替代包。

帮助更容易地与WordPress管理仪表板交互

dev-master 2019-04-05 08:19 UTC

This package is auto-updated.

Last update: 2021-04-05 13:21:29 UTC


README

这个轻量级模块旨在放入您的任何WordPress主题或插件中,以帮助更容易地与WordPress管理仪表板交互。

注意:对于非composer用户,您可以在simwp-compiled下载编译版本。

composer require simwp=dev-master

目录

  1. 构建WordPress设置页面
  2. 处理WordPress选项
  3. 几秒钟内创建WordPress设置字段
  4. 处理WordPress通知
  5. 需要帮助
  6. 常见问题解答
  7. 许可

构建WordPress设置页面

注册您自己的第一个自定义菜单,其中将包含所有设置页面

// create an admin holder to hold custom menus
// and provide features like auto translating
Simwp::admin('custom admin')
	->menu('custom menu'); // Create your new menu item
	->page('custom page'); // A menu contains on or many pages
	->append(Section_Simple::class) // Register a Section to be display (1)
	->set('render', 'custom_render_callback') // Or use a custom rendering function
	->link('https://github.com/hungluu/simwp'); // Or simply redirect to another url

(1) 部分(sections)由Simwp管理,自动将页面内容渲染到设置页面中

wordpress sections are auto-rendered by Simwp

处理WordPress选项

而不是编写这些代码和更多内容

if(wp_verify_nonce($_POST['_nonce'], 'nonce-name') && current_user_can('manage_options')){
	if(isset($_POST[$key]) {
		if(is_string($_POST[$key])){
			...

您可以让Simwp为您处理所有WordPress option

// Tell Simwp to auto sanitize and update the option for you when user hit enter
// and show error messages when the submitted value is not valid
Simwp::manage('opt-key');

// Check if user submit option value, if so, sanitize it
Simwp::updated('opt-key', $opt_callback);

// Check if user submit option value, and if that value
// is valid with Symfony\Validator, or accessible for current
// section / page
Simwp::handled('opt-key', $opt_callback);

Simwp::is('csrf'); // check if csrf attack presents in admin dashboard, good for ajax options
Simwp::is('dashboard'); // check if current view is admin dashboard
Simwp::is('user'); // check if user logged in

// Some simple option handling
Simwp::option('opt-key')
	->appendTo(Simple_Section::class) // the option updating only available in a specified section
	->appendTo($custom_menu) // or shared with all the pages inside menu
	->appendTo('themes.php') // or shared with an admin-dashboard slug
	->validate(new Assert\NotBlank()) // force option not to be blank
	->validate(new Assert\Email()) // and force it to be an email
	->updated($fn); // Or push a callback to determine when the option is submited, data sanitized before
					// being passed in as an argument

Wordpress options are validated by Symfony\Validator

获取选项值

Simwp::get('opt-key') // get option value

Simwp::option('opt-key')
	->default('def-value') // default value to return when option not found
	->type('array') // submited value will be parsed as array
	->type('boolean'); // submited value weill be parsed as boolean

几秒钟内创建WordPress设置字段

section的示例代码

class SimpleSection extends Simwp\Section {
	function is_registered(){
		// create a checkbox for 'registered' option
		// short version of $this->view('toggle')->render('registered')
		$this->toggle('registered');
	}

	function user_name(){
		// create an input to fill 'followed-users'
		// short version of $this->view('tags')->render('followed-users')
		$this->tags('followed-users');
	}
}

Some section components

更多组件正在创建中,当前列表是默认组件(带有它们自己的JavaScript事件)

  • 编辑器(TinyMCE)
  • 复选框
  • 颜色选择器
  • 日期选择器
  • 日期时间选择器
  • 图片(simwp_image_selected, simwp_image_removed)
  • 输入
  • 线条(simwp_line_added, simwp_line_removed)
  • 选项
  • 单选按钮
  • 标签
  • 文本区域
  • 切换

处理WordPress通知

创建一个新的通知并给它一个名字

Simwp::notice('simple-notice')
	->append('example text');

通知有4种类型

  • no-control通知是交互式的
  • dismissible通知可以被用户隐藏,但在下一次请求时可以显示
  • removable通知可以在任何请求时被用户避免
  • force通知永远不会被用户避免

通知有5个标志:primary info success warning error

Notices have 4 types and 4 flags

需要帮助

问题中找到已解决的问题或创建一个新的,如果您找不到与您的问题相关的问题。

常见问题解答

我如何创建自己的部分组件和过滤器?

您可以在任何时候声明您自己的组件和过滤器,组件应位于命名空间Simwp\Form中,过滤器应位于命名空间Simwp\Form\Filter中,以便默认的viewfilter方法可以轻松识别它们。

例如,您声明一个新的组件Simwp\Form\Custom,在section中您可以调用此行来渲染您的新自定义组件

$this->custom('opt-name');

许可

本模块完全免费,适用于任何项目,包括商业项目,并基于 MIT 许可证。您可以随意使用它。项目目前处于非常早期阶段,需要更多的贡献性测试、组件、过滤器或翻译……所以如果您能加入我们,我将非常感激。谢谢。

敬请关注。