Skip to content
This repository has been archived by the owner on Apr 30, 2020. It is now read-only.

Commit

Permalink
fix(Features/Acf): add required hooks check to OptionPages::get() (#229)
Browse files Browse the repository at this point in the history
* fix(Features/Acf): add required hooks check to OptionPages::get()

This makes sure that acf/init has already been run and therefore all option pages are available to
be queried. It fixes issues that arise with the object cache when used incorrectly.

* refactor(Features/GoogleAnalytics): remove unnecessary priority

not needed anymore as this priority was a hotfix for the previous ACF OptionPages feature version

* refactor(Features/Acf): split OptionPages::get() functionality into separate functions

to reduce complexity and improve scanability / readability

* refactor(Features/Acf): move protected functions to correct section further below
  • Loading branch information
Doğa Gürdal committed Jun 28, 2017
1 parent 43cc404 commit 0f62245
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
52 changes: 37 additions & 15 deletions Features/Acf/OptionPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ public static function init()
* Returns an option of a sub page. If no field name is provided it will get all option of that sub page.
* Parameters are expected to be camelCase.
*
* @since 0.2.0 introduced as a replacement for OptionPages::getOption and OptionPages::getOptions
* @since %%NEXT_VERSION%% added check for required hooks to have run to alert of timing issues when used incorrectly
*
* @param string $optionType Type of option page. Either globalOptions or translatableOptions.
* @param string $optionCategory Category of option page. One of these three values: component, feature, customPostType.
* @param string $subPageName Name of the sub page.
Expand All @@ -115,29 +118,22 @@ public static function init()
**/
public static function get($optionType, $optionCategory, $subPageName, $fieldName = null)
{
$optionType = lcfirst($optionType);

if (!isset(self::$optionTypes[$optionType])) {
if (!self::checkRequiredHooks($optionType, $optionCategory, $subPageName, $fieldName)) {
return false;
}

// convert parameters
$optionType = lcfirst($optionType);
$optionCategory = ucfirst($optionCategory);
$subPageName = ucfirst($subPageName);

if (!isset(self::$optionTypes[$optionType])) {
return false;
}

$prefix = implode('', [$optionType, $optionCategory, $subPageName, '_']);
$options = self::getOptionFields(self::$optionTypes[$optionType]['translatable']);

// find and replace relevant keys, then return an array of all options for this Sub-Page
$optionKeys = is_array($options) ? array_keys($options) : [];
$options = array_reduce($optionKeys, function ($carry, $key) use ($options, $prefix) {
$count = 0;
$option = $options[$key];
$key = str_replace($prefix, '', $key, $count);
if ($count > 0) {
$carry[$key] = $option;
}
return $carry;
}, []);
$options = self::collectOptionsWithPrefix($options, $prefix);

if (isset($fieldName)) {
$fieldName = lcfirst($fieldName);
Expand Down Expand Up @@ -338,6 +334,17 @@ protected static function prefixFields($fields, $prefix)
}, $fields);
}

protected static function checkRequiredHooks($optionType, $optionCategory, $subPageName, $fieldName)
{
if (did_action('acf/init') < 1) {
$parameters = "${optionType}, ${optionCategory}, ${subPageName}, ";
$parameters .= isset($fieldName) ? $fieldName : 'NULL';
trigger_error("Could not get option/s for [${parameters}]. Required hooks have not yet been executed! Please make sure to run `OptionPages::get()` after the `acf/init` action is finished.", E_USER_WARNING);
return false;
}
return true;
}

protected static function getOptionFields($translatable)
{
global $sitepress;
Expand Down Expand Up @@ -381,6 +388,21 @@ protected static function getCachedOptionFields($namespace = '')
return $options;
}
// find and replace relevant keys, then return an array of all options for this Sub-Page
protected static function collectOptionsWithPrefix($options, $prefix)
{
$optionKeys = is_array($options) ? array_keys($options) : [];
return array_reduce($optionKeys, function ($carry, $key) use ($options, $prefix) {
$count = 0;
$option = $options[$key];
$key = str_replace($prefix, '', $key, $count);
if ($count > 0) {
$carry[$key] = $option;
}
return $carry;
}, []);
}
protected static function combineArrayDefaults(array $array, array $defaults)
{
return array_map(function ($value) use ($defaults) {
Expand Down
2 changes: 1 addition & 1 deletion Features/GoogleAnalytics/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Flynt\Utils\Feature;
use Flynt\Features\Acf\OptionPages;

add_action('init', 'Flynt\Features\GoogleAnalytics\init', 100);
add_action('init', 'Flynt\Features\GoogleAnalytics\init');

function init()
{
Expand Down

0 comments on commit 0f62245

Please sign in to comment.