Skip to content

Commit 6fd68bd

Browse files
committed
Added initial Plugin::get_plugin_option(), with object caching (if available)
1 parent 3d1a6a7 commit 6fd68bd

4 files changed

Lines changed: 38 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ It may also be used as the means of [separating custom code](http://www.billeric
3030
* Add hidden `password` field with encrypted `hidden` field
3131
* Possibly add hooks
3232
* Possibly add TGMPA example
33+
* Allow loading Carbon Fields via [plugin](https://github.com/dmhendricks/carbon-fields-loader) rather than Composer dependency
3334
* Test compatibility with WordPress 4.0 and higher
3435

3536
## Change Log
3637

3738
#### 0.2.0
3839

39-
* Added experimental Cache class
40+
* Added Object Cache class
4041
* Added example of loading Font Awesome if enabled in plugin settings
4142
* Removed `./vendor` from repo
4243
* Renamed Helpers Class to Utils

app/Core.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function __construct() {
99
add_filter( 'body_class', array(&$this, 'add_body_classes') );
1010

1111
// Remove Emoji code from header
12-
if( carbon_get_theme_option( self::$prefix.'remove_header_emojicons') ) {
12+
if( $this->get_plugin_option( 'remove_header_emojicons' ) ) {
1313
if(!$this->is_ajax()) add_filter( 'init', array( $this, 'disable_wp_emojicons' ) );
1414
}
1515

app/Plugin.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22
namespace Nimbium\MyPlugin;
3+
use Carbon_Fields\Container;
4+
use Carbon_Fields\Field;
35

46
class Plugin {
57

@@ -67,6 +69,25 @@ private function verify_dependencies() {
6769

6870
}
6971

72+
/**
73+
* Get Carbon Fields option, with object caching (if available)
74+
*
75+
* @return bool
76+
*/
77+
public function get_plugin_option( $key, $cache = true, $source = null ) {
78+
79+
if( $cache ) {
80+
// Attempt to get value from cache, else return value from database
81+
return Cache::get_object( self::$prefix . $key, function() use (&$key, &$source) {
82+
return carbon_get_theme_option( self::$prefix.$key );
83+
});
84+
} else {
85+
// Return uncached value
86+
return carbon_get_theme_option( self::$prefix.$key );
87+
}
88+
89+
}
90+
7091
/**
7192
* Returns true if WP_ENV is anything other than 'development' or 'staging'.
7293
* Useful for determining whether or not to enqueue a minified or non-

app/Settings.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ class Settings extends Plugin {
99
* Create a options/settings page in WP Admin
1010
*/
1111
function __construct() {
12-
// Carbon Fields Docs: https://carbonfields.net/docs/containers-theme-options/
1312

13+
// Clear the cache when settings are saved
14+
add_action('carbon_fields_theme_options_container_saved', array( $this, 'options_saved_hook' ) );
15+
16+
// Carbon Fields Docs: https://carbonfields.net/docs/containers-theme-options/
1417
Container::make('theme_options', self::$settings['data']['Name'])
1518
->set_page_parent('options-general.php')
1619
->add_tab(__('General'), array(
@@ -83,4 +86,14 @@ function __construct() {
8386

8487
}
8588

89+
/**
90+
* Logic that is run when settings are saved.
91+
*/
92+
public function options_saved_hook() {
93+
94+
// Clear the cache so that new settings are loaded
95+
Cache::flush();
96+
97+
}
98+
8699
}

0 commit comments

Comments
 (0)