|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class to add feature flags defined with Flagpole as a filter option in ACF. |
| 4 | + * Documentation for this functionality available here: https://www.advancedcustomfields.com/resources/custom-location-rules/ |
| 5 | + * |
| 6 | + * @package Peake\Plugins |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Flagpole; |
| 10 | + |
| 11 | +use ACF_Location; |
| 12 | +use Flagpole\Flagpole; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class ACF_Filter |
| 16 | + * |
| 17 | + * @package FeatureFlags |
| 18 | + */ |
| 19 | +class ACF_Filter extends ACF_Location { |
| 20 | + // Type hints must match the original source exactly, so PHPCS checks have been disabled but docblocks are accurate. |
| 21 | + // phpcs:disable NeutronStandard.Functions.TypeHint.NoArgumentType |
| 22 | + // phpcs:disable NeutronStandard.Functions.TypeHint.NoReturnType |
| 23 | + // phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 24 | + |
| 25 | + /** |
| 26 | + * Sets the base values for the location filter. |
| 27 | + * |
| 28 | + * @return void |
| 29 | + */ |
| 30 | + public function initialize() { |
| 31 | + $this->name = 'feature-flags'; |
| 32 | + $this->label = __( 'Feature flags', 'flagpole' ); |
| 33 | + $this->category = 'forms'; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Gets the list of feature flags as an ID => printable name pair. |
| 38 | + * |
| 39 | + * @param array $rule Information on the current rule (value, parameter, operator etc.). |
| 40 | + * @return array List of all flag IDs and names. |
| 41 | + */ |
| 42 | + public function get_values( $rule ) { |
| 43 | + $flagpole_flags = Flagpole::init()->get_flags(); |
| 44 | + $flagpole_values = array(); |
| 45 | + |
| 46 | + foreach ( $flagpole_flags as $flagpole_flag ) { |
| 47 | + $flagpole_values[ $flagpole_flag->key ] = $flagpole_flag->name; |
| 48 | + } |
| 49 | + |
| 50 | + return $flagpole_values; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Returns an array of operators. |
| 55 | + * |
| 56 | + * @param array $rule A location rule. |
| 57 | + * @return array |
| 58 | + */ |
| 59 | + public static function get_operators( $rule ) { |
| 60 | + return array( |
| 61 | + '==' => __( 'is enabled', 'flagpole' ), |
| 62 | + '!=' => __( 'is not enabled', 'flagpole' ), |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns true or false depending on whether or not the feature flag is enabled and whether our operator is '==' or '!='. |
| 68 | + * |
| 69 | + * @param array $rule Parameter info, including the operator and feature flag ID value. |
| 70 | + * @param array $screen Current page info (post type, ID, language). |
| 71 | + * @param array $field_group Field group info (field group name, rules, position etc.). |
| 72 | + * @return boolean Whether our parameters have been met. |
| 73 | + */ |
| 74 | + public function match( $rule, $screen, $field_group ) { |
| 75 | + if ( '==' === $rule['operator'] ) { |
| 76 | + return flagpole_flag_enabled( $rule['value'] ); |
| 77 | + } |
| 78 | + |
| 79 | + if ( '=!' === $rule['operator'] ) { |
| 80 | + return ! flagpole_flag_enabled( $rule['value'] ); |
| 81 | + } |
| 82 | + |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + // phpcs:enable NeutronStandard.Functions.TypeHint.NoArgumentType |
| 87 | + // phpcs:enable NeutronStandard.Functions.TypeHint.NoReturnType |
| 88 | + // phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 89 | +} |
0 commit comments