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

Commit 9f52246

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

File tree

3 files changed

+118
-2
lines changed

3 files changed

+118
-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: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ function flagpole_admin_imports( $hook ) {
9999
require plugin_dir_path( __FILE__ ) . 'includes/admin/settings-page.php';
100100
require plugin_dir_path( __FILE__ ) . 'includes/api/api.general.php';
101101
require plugin_dir_path( __FILE__ ) . 'includes/api/api.shortcode.php';
102+
require plugin_dir_path( __FILE__ ) . 'includes/acf/class-acf-filter.php';
102103

103104
/**
104105
* AJAX Action toggling features from the WP admin area.
@@ -176,8 +177,12 @@ function flagpole_create_group() {
176177
$validation = array_filter( $validation );
177178

178179
if ( $validation ) {
179-
$result = Flagpole::init()->create_group( $validation['group-key'], $validation['group-name'],
180-
$validation['group-desc'], $validation['group-private'] );
180+
$result = Flagpole::init()->create_group(
181+
$validation['group-key'],
182+
$validation['group-name'],
183+
$validation['group-desc'],
184+
$validation['group-private']
185+
);
181186

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

includes/acf/class-acf-filter.php

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

0 commit comments

Comments
 (0)