jamesrwilliams/wp-feature-flags 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jamesrwilliams/wp-feature-flags

最新稳定版本:0.1.4-beta

Composer 安装命令:

composer require jamesrwilliams/wp-feature-flags

包简介

Easily register and work with feature flags in your theme.

README 文档

README

Maintainability GitHub release PRs Welcome Packagist Downloads Known Vulnerabilities

This plugin is for WordPress theme developers who wish to add simple feature flags to their themes. These flags can be enabled via an admin interface, previewed on a per-user, or group basis, and even enabled via a query string for those without accounts. For planned development work please see the roadmap or issues labeled with enhancement.

Contents

  1. Installation
  2. Adding flags to themes
  3. Enabling flags
  4. Flag Groups
  5. Shortcodes
  6. Contributing

Installation

Add this project's source code to your wp-content/plugins directory and enable it like you would any other plugin. It is also available via Packagist to use with composer.

composer require jamesrwilliams/flagpole

This plugin is currently not available via the WordPress Plugin directory however we are working towards that for v1.0.0.

Required theme changes

As this plugin is closely coupled with your theme code it is a good idea to add the following block to your theme to catch any errors if the Flagpole plugin is disabled for any reason.

if ( ! function_exists( 'flagpole_flag_enabled' ) ) {
	function flagpole_flag_enabled() {
		return false;
	}
}

Register a flag

To register a flag, simply add the following to your theme's functions.php file using the flagpole_register_flag function:

if ( function_exists( 'flagpole_register_flag' ) ) {
	flagpole_register_flag([
		'title'       => 'My awesome new feature',
		'key'         => 'correct-horse-battery-staple',
		'enforced'    => false,
		'label'       => 'All',
		'description' => 'An example feature definition',
		'stable'      => false,
	]);
}

Wrapping the registration function call in a function_exists helps avoid errors if the plugin is disabled for any reason. You can also pass an array of flags to flagpole_register_flag to easily instantiate multiple flags at once.

Checking the status of a flag

In your templates you can then check the feature status using the flagpole_flag_enabled function in your PHP theme code to toggle features based on the status of your flags:

if ( flagpole_flag_enabled( 'flag_key' ) ) {
	/* flag_key is enabled! */
}

Replace flag_key with the key used in the flagpole_register_flag function to check if it is enabled.

Flag options/arguments

Parameter Type Default Description
key string - The unique key used in the template to check if a feature is enabled.
title string "" The human readable feature name.
description (optional) string "" A description displayed in the admin screen.
stable (optional) boolean false If true, allows users to publish features from the admin area.
enforced (optional) boolean false Setting this to true will override any user specific settings and will enforce the flag to be enabled for every user. Useful for deploying a flag before removing it from the codebase.
label string All Using labels lets you group together flags that are similar. Adding a label will separate the flag out in the admin UI.

Enabling flags

There are three ways to have a flag enabled with Flagpole. These are:

  • Previewing - Enable a flag only for the current logged-in user.
  • Publishing - Enable a flag for every visitor on the site.
  • Enforcing - Flags which are enabled by default in the theme.

Previewing

A flag can be previewed for the current logged-in user by enabling the preview in the Flagpole admin screen.

Navigate to the Flagpole screen in the WP admin dashboard, located under Tools > Flagpole. Find a flag you wish to enable, and click the "enable preview" button.

This flag will now be enabled for this user until it is toggled again. Users can preview any number of flags at any one time. For previewing multiple flags at the same time check out Flag Groups.

Publishing

Publishing a flag enables it for every user that visits your site, this includes logged-out users. Any user can publish a feature as long as it has been marked as stable by setting the stable property to true in the flag registration block. This acts as a safety net allowing theme developers to mark features ready for publication.

E.g.

flagpole_register_flag([
	'title'       => 'Feature ready for publication',
	'key'         => 'super-awesome-navigation-change',
	'stable'      => true,
]);

Enforcing

Enforcing a flag is where a developer can force a flag to be in a published state. This allows them to enable a flag via their source code by setting the 'enforced' option to true in the flag options. The idea behind enforced flags are a stepping stone before removing the flag logic from the theme. Enforced flags are displayed in a separate list in the admin area and are not interactive to users in the admin area.

flagpole_register_flag([
	'title'       => 'An enforced flag',
	'key'         => 'enforced-flag',
	'enforced'    => true,
]);

Flag Groups

Flag Groups are a way to manage multiple flags at a time. You can preview Flag Groups like you can a single flag and by using the group URL parameter, and the group key you wish to enable. A private group will require users to login to the site prior to activating the flags for them.

You can either preview a Flag Group by enabling it in the WP Admin as you would for a single flag, or you can use the query string method to enable a group of flags using the following query string format: ?group={groupKey}

Example:

https://www.example.com?group=battery-horse-staple

Shortcodes

This plugin adds a number of utility shortcodes to help debug the use of Flagpole flags.

debugFlagpole_flags

The shortcode by default shows all flags that are not enforced found in your theme. You can also specify which flags you're looking to debug specifically using the flag parameter like so with either a single key or a comma separated list:

Basic Usage:

// Single Key
echo do_shortcode('[debugFlagpole_flags]');

This will display a table of all the non-enforced flags currently found in the active theme, including a status and, if they are enabled, a reason why.

You can specify single or multiple flag for this to output if you don't want to show everything.

// Multiple keys
echo do_shortcode('[debugFlagpole_flags flag="key-1,key-2,key-3"]');

Passing the enforced value will display all enforced flags instead of the other flags. E.g:

echo do_shortcode('[debugFlagpole_flags enforced="true"]');

debugFlagpole_groups

Use the following shortcode to get a debug output for Flag Groups.

echo do_shortcode('[debugFlagpole_groups]');

debugFlagpole_db

Use the following shortcode to get a debug output for everything!

echo do_shortcode('[debugFlagpole_db]');

Contributing

Any PRs and suggestions are very welcome, along with ideas and discussions on issues.

jamesrwilliams/wp-feature-flags 适用场景与选型建议

jamesrwilliams/wp-feature-flags 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20.28k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2019 年 01 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「wordpress」 「feature flags」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jamesrwilliams/wp-feature-flags 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jamesrwilliams/wp-feature-flags 我们能提供哪些服务?
定制开发 / 二次开发

基于 jamesrwilliams/wp-feature-flags 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 20.28k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 14
  • Watchers: 5
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-01-17