Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit c41efdc

Browse files
author
Jonny Bull
committed
ACF Support
1 parent 95b8a16 commit c41efdc

File tree

3 files changed

+119
-2
lines changed

3 files changed

+119
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ Example:
133133
https://www.example.com?group=battery-horse-staple
134134
```
135135

136+
## Advanced Custom Fields (ACF) Support
137+
138+
ACF field groups can also be set to show or hide based on feature flags.
139+
140+
In the 'Location' section of a field group, 'Feature flags' will be available as an option. This allows you to show a field group depending on whether a feature flag are enabled or not. This can be combined with the and/or rules to display a field group depending on the status of multiple feature flags.
141+
136142
## Shortcodes
137143

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

flagpole.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ function flagpole_admin_imports( $hook ) {
100100
require plugin_dir_path( __FILE__ ) . 'includes/api/api.general.php';
101101
require plugin_dir_path( __FILE__ ) . 'includes/api/api.shortcode.php';
102102

103+
if ( class_exists( 'ACF' ) ) {
104+
require_once( plugin_dir_path( __FILE__ ) . 'includes/acf/class-acf-filter.php' );
105+
}
106+
103107
/**
104108
* AJAX Action toggling features from the WP admin area.
105109
*/
@@ -176,8 +180,12 @@ function flagpole_create_group() {
176180
$validation = array_filter( $validation );
177181

178182
if ( $validation ) {
179-
$result = Flagpole::init()->create_group( $validation['group-key'], $validation['group-name'],
180-
$validation['group-desc'], $validation['group-private'] );
183+
$result = Flagpole::init()->create_group(
184+
$validation['group-key'],
185+
$validation['group-name'],
186+
$validation['group-desc'],
187+
$validation['group-private']
188+
);
181189

182190
flagpole_operation_redirect( $result );
183191
}
@@ -338,3 +346,17 @@ function flagpole_operation_redirect( $error_code = false, $redirect = true ) {
338346
add_shortcode( 'debugFlagpole_flags', 'flagpole_shortcode_debug_flags' );
339347
add_shortcode( 'debugFlagpole_groups', 'flagpole_shortcode_debug_groups' );
340348
add_shortcode( 'debugFlagpole_db', 'flagpole_shortcode_debug_db' );
349+
350+
// Check ACF exists before registering our filter.
351+
if ( class_exists( 'ACF' ) ) {
352+
add_action( 'acf/init', __NAMESPACE__ . '\\flagpole_acf_location_type' );
353+
}
354+
355+
/**
356+
* Register our ACF feature flag location filter.
357+
*
358+
* @return void
359+
*/
360+
function flagpole_acf_location_type() {
361+
acf_register_location_type( 'Flagpole\ACF_Filter' );
362+
}

includes/acf/class-acf-filter.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)