Skip to content

Commit 3190885

Browse files
committed
WPSAC: Added get_wpsac_plugin_option() example; flush cache on submit
1 parent 754159d commit 3190885

3 files changed

Lines changed: 59 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ Release changes are noted on the [Releases](https://github.com/dmhendricks/wordp
8888

8989
* Fixed non-static deprecation notice
9090
* Added support for before/after strings to prefix()
91+
* Added version check for wordpress-toolkit
92+
* Added get_wpsac_plugin_option() example to Plugin class
9193

9294
## Credits
9395

app/Plugin.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,14 @@ private function verify_dependencies( $deps = true, $args = array() ) {
188188
* cache flushed appropriately.
189189
*
190190
* @param string $key The name of the option key
191+
* @param bool $cache Whether or not to attempt to get cached value
191192
* @return mixed The value of specified Carbon Fields option key
192193
* @link https://carbonfields.net/docs/containers-usage/ Carbon Fields containers
193194
* @since 0.2.0
194195
*
195196
*/
196197
public static function get_plugin_option( $key, $cache = true ) {
198+
197199
$key = self::prefix( $key );
198200

199201
if( $cache ) {
@@ -208,6 +210,38 @@ public static function get_plugin_option( $key, $cache = true ) {
208210

209211
}
210212

213+
/**
214+
* Get plugin option from WordPress Settings API Class, with object caching
215+
* (if available).
216+
*
217+
* @param string $key The name of the option key
218+
* @param string $group_id The group_id of the settings page, as specified
219+
* in WPSAC_Page class
220+
* @param bool $cache Whether or not to attempt to get cached value
221+
* @return string The value of specified option key
222+
* @link https://github.com/tareq1988/wordpress-settings-api-class WPSAC options
223+
* @since 0.3.2
224+
*
225+
*/
226+
public static function get_wpsac_plugin_option( $key, $group_id = 'options', $cache = true ) {
227+
228+
$key = self::prefix( $key );
229+
$options = array();
230+
231+
if( $cache ) {
232+
// Attempt to get value from cache, else fetch value from database
233+
$options = self::$cache->get_object( $key, function() use ( &$key, &$group_id ) {
234+
return get_option( self::prefix( $group_id ) );
235+
});
236+
} else {
237+
// Return uncached value
238+
$options = get_option( self::prefix( $group_id ) );
239+
}
240+
241+
return isset( $options[ $key ] ) ? $options[ $key ] : null;
242+
243+
}
244+
211245
/**
212246
* A wrapper for the plugin's data fiala prefix as defined in $config
213247
*

app/Settings/WPSAC_Page.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
class WPSAC_Page extends Plugin {
1414

1515
private $settings_api;
16+
private $section_id = 'options';
1617

1718
function __construct() {
1819

1920
$this->settings_api = new \WeDevs_Settings_API();
2021
$this->settings_fields = array();
2122

23+
// Process saved options on submit
24+
if( isset( $_POST['option_page'] ) && $_POST['option_page'] == $this->prefix( $this->section_id ) ) $this->options_container_saved();
25+
2226
// Create a settings page using wordpress-settings-api-class (Settings > Settings API)
2327
add_action( 'admin_init', array( $this, 'wpsac_admin_init' ) );
2428
add_action( 'admin_menu', array( $this, 'wpsac_admin_menu' ) );
@@ -31,6 +35,10 @@ function __construct() {
3135
* @since 0.3.0
3236
*/
3337
public function wpsac_admin_init() {
38+
39+
// Flush cache when settings saved
40+
if( isset( $_POST['option_page'] ) && $_POST['option_page'] == $this->prefix( $this->section_id ) ) self::$cache->flush();
41+
3442
$this->settings_api->set_sections( $this->get_settings_sections() );
3543
$this->settings_api->set_fields( $this->get_settings_fields() );
3644
$this->settings_api->admin_init();
@@ -55,7 +63,7 @@ public function get_settings_sections() {
5563
$sections = array(
5664

5765
array(
58-
'id' => $this->prefix( 'general' ),
66+
'id' => $this->prefix( $this->section_id ),
5967
'title' => self::$config->get( 'short_name' ) . ' ' . __( 'Settings', self::$textdomain ) . ' (WPSAC)'
6068
)
6169
);
@@ -72,7 +80,7 @@ public function get_settings_sections() {
7280
public function get_settings_fields() {
7381

7482
$settings_fields = array(
75-
$this->prefix( 'general' ) => array(
83+
$this->prefix( $this->section_id ) => array(
7684
array(
7785
'name' => $this->prefix( 'blog_name' ),
7886
'label' => __( 'Blog Name', self::$textdomain ),
@@ -125,4 +133,17 @@ public function create_wpsac_settings_page() {
125133

126134
}
127135

136+
/**
137+
* When settings saved, run optional code to process. This is not elegant,
138+
* but WPSAC doesn't have an options saved hook.
139+
*
140+
* @since 0.3.2
141+
*/
142+
private function options_container_saved() {
143+
144+
// Example - If we wanted to saved the "blog_name" field as an MD5 hash:
145+
//$_POST[ $this->prefix( $this->section_id ) ][ $this->prefix( 'blog_name' ) ] = md5( $this->prefix( 'blog_name' ) );
146+
147+
}
148+
128149
}

0 commit comments

Comments
 (0)