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

Commit

Permalink
refactor(Features/Acf/OptionPages): use single public function and fi…
Browse files Browse the repository at this point in the history
…x CPT subpage name (#167)

* docs(Features/Acf/OptionPages): add inline docs for public API

* refactor(Features/Acf/OptionPages): use one function to get any number of options

BREAKING CHANGE: Removed OptionPages::getOption and OptionPages::getOptions. Using OptionPages::get instead.

* fix(Features/Acf/OptionPages): convert custom post type option sub page name to camelCase
  • Loading branch information
Doğa Gürdal committed May 11, 2017
1 parent c8e7087 commit 99aa561
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 36 deletions.
6 changes: 4 additions & 2 deletions Components/ComponentLoaderFlexible/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
array_key_exists('optionCategory', $optionPage) &&
array_key_exists('subPageName', $optionPage)
) {
$options = OptionPages::getOptions(
$options = OptionPages::get(
$optionPage['optionType'],
$optionPage['optionCategory'],
$optionPage['subPageName']
);
$data = array_merge($data, $options);
if ($options) {
$data = array_merge($data, $options);
}
}
return $data;
});
Expand Down
6 changes: 4 additions & 2 deletions Components/ComponentLoaderStatic/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
array_key_exists('optionCategory', $optionPage) &&
array_key_exists('subPageName', $optionPage)
) {
$options = OptionPages::getOptions(
$options = OptionPages::get(
$optionPage['optionType'],
$optionPage['optionCategory'],
$optionPage['subPageName']
);
$data = array_merge($data, $options);
if ($options) {
$data = array_merge($data, $options);
}
} else if (isset($parentData['post'])) {
$data = array_merge($data, $parentData['post']->fields);
}
Expand Down
2 changes: 1 addition & 1 deletion Features/Acf/GoogleMaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class GoogleMaps
public static function init()
{
add_filter('acf/fields/google_map/api', function ($api) {
$apiKey = OptionPages::getOption('globalOptions', 'feature', 'Acf', 'googleMapsApiKey');
$apiKey = OptionPages::get('globalOptions', 'feature', 'Acf', 'googleMapsApiKey');
if ($apiKey) {
$api['key'] = $apiKey;
}
Expand Down
58 changes: 28 additions & 30 deletions Features/Acf/OptionPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,35 @@ public static function init()
// PUBLIC API
// ============

// usage: OptionPages::getOptions('globalOptions', 'customPostType', 'myCustomPostTypeName');
// usage: OptionPages::getOptions('globalOptions', 'feature', 'myFeatureName');
// usage: OptionPages::getOptions('translatableOptions', 'component', 'myComponentName');
// all params expected to be camelCase
public static function getOptions($optionType, $optionCategory, $subPageName)
/**
* Get option(s) from a sub page.
*
* 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.
*
* @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.
* @param string $fieldName (optional) Name of the field to get.
* @return mixed The value of the option or array of options. False if subpage doesn't exist or no option was found.
**/
public static function get($optionType, $optionCategory, $subPageName, $fieldName = null)
{
$optionType = lcfirst($optionType);

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

$prefix = implode('', [$optionType, ucfirst($optionCategory), ucfirst($subPageName), '_']);
$optionCategory = ucfirst($optionCategory);
$subPageName = ucfirst($subPageName);

$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) : [];
return array_reduce($optionKeys, function ($carry, $key) use ($options, $prefix) {
$options = array_reduce($optionKeys, function ($carry, $key) use ($options, $prefix) {
$count = 0;
$option = $options[$key];
$key = str_replace($prefix, '', $key, $count);
Expand All @@ -111,16 +124,12 @@ public static function getOptions($optionType, $optionCategory, $subPageName)
}
return $carry;
}, []);
}

// usage: OptionPages::getOption('globalOptions', 'customPostType', 'myCustomPostTypeName', 'myFieldName');
// usage: OptionPages::getOption('globalOptions', 'feature', 'myFeatureName', 'myFieldName');
// usage: OptionPages::getOption('translatableOptions', 'component', 'myComponentName', 'myFieldName');
// all params expected to be camelCase
public static function getOption($optionType, $optionCategory, $subPageName, $fieldName)
{
$options = self::getOptions($optionType, $optionCategory, $subPageName);
return array_key_exists($fieldName, $options) ? $options[$fieldName] : false;
if (isset($fieldName)) {
$fieldName = lcfirst($fieldName);
return array_key_exists($fieldName, $options) ? $options[$fieldName] : false;
}
return $options;
}

// ============
Expand Down Expand Up @@ -180,19 +189,8 @@ public static function addCustomPostTypeSubPage($name, $customPostType)
$filePath = $customPostType['dir'] . '/fields.json';

if (is_file($filePath)) {
// TODO refactor
// $cptName = ucfirst($cptDir->getFilename());
// if (isset($cptConfig['label'])) {
// $label = $cptConfig['label'];
// }
// if (isset($cptConfig['labels'])) {
// if (isset($cptConfig['labels']['menu_name'])) {
// $label = $cptConfig['labels']['menu_name'];
// } else if (isset($cptConfig['labels']['singular_name'])) {
// $label = $cptConfig['labels']['singular_name'];
// }
// }
self::createSubPageFromConfig($filePath, 'customPostType', ucfirst($name));
$name = StringHelpers::kebapCaseToCamelCase($name);
self::createSubPageFromConfig($filePath, 'customPostType', $name);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Features/GoogleAnalytics/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

function init()
{
$googleAnalyticsOptions = OptionPages::getOptions('globalOptions', 'feature', 'GoogleAnalytics');
$googleAnalyticsOptions = OptionPages::get('globalOptions', 'feature', 'GoogleAnalytics');
if ($googleAnalyticsOptions) {
new GoogleAnalytics($googleAnalyticsOptions);
}
Expand Down

0 comments on commit 99aa561

Please sign in to comment.