Skip to content

Commit dbd8805

Browse files
committed
Added Serialized Datastore example
1 parent 687cdcf commit dbd8805

5 files changed

Lines changed: 95 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Release changes are noted on the [Releases](https://github.com/dmhendricks/wordp
7777

7878
#### Branch: `master`
7979

80-
* None since release
80+
* Added Carbon Fields [Serialized Datastore](https://github.com/dmhendricks/wordpress-base-plugin/wiki/Features-&-Customization#serialized-data-store) example
8181

8282
## Credits
8383

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
namespace Carbon_Fields\Datastore\Datastore;
3+
use Carbon_Fields\Field\Field;
4+
use Carbon_Fields\Datastore\Datastore;
5+
6+
/**
7+
* Stores serialized values in the database
8+
*/
9+
class Serialized_Theme_Options_Datastore extends Datastore {
10+
11+
/**
12+
* Initialization tasks for concrete datastores.
13+
**/
14+
public function init() {
15+
16+
}
17+
18+
protected function get_key_for_field( Field $field ) {
19+
$key = '_' . $field->get_base_name();
20+
return $key;
21+
}
22+
23+
/**
24+
* Save a single key-value pair to the database with autoload
25+
*
26+
* @param string $key
27+
* @param string $value
28+
* @param bool $autoload
29+
*/
30+
protected function save_key_value_pair_with_autoload( $key, $value, $autoload ) {
31+
$notoptions = wp_cache_get( 'notoptions', 'options' );
32+
$notoptions[ $key ] = '';
33+
wp_cache_set( 'notoptions', $notoptions, 'options' );
34+
$autoload = $autoload ? 'yes' : 'no';
35+
36+
if ( ! add_option( $key, $value, null, $autoload ) ) {
37+
update_option( $key, $value, $autoload );
38+
}
39+
}
40+
41+
/**
42+
* Load the field value(s)
43+
*
44+
* @param Field $field The field to load value(s) in.
45+
* @return array
46+
*/
47+
public function load( Field $field ) {
48+
$key = $this->get_key_for_field( $field );
49+
$value = get_option( $key, null );
50+
return $value;
51+
}
52+
53+
/**
54+
* Save the field value(s)
55+
*
56+
* @param Field $field The field to save.
57+
*/
58+
public function save( Field $field ) {
59+
if ( ! empty( $field->get_hierarchy() ) ) {
60+
return; // only applicable to root fields
61+
}
62+
$key = $this->get_key_for_field( $field );
63+
$value = $field->get_full_value();
64+
if ( is_a( $field, '\\Carbon_Fields\\Field\\Complex_Field' ) ) {
65+
$value = $field->get_value_tree();
66+
}
67+
$this->save_key_value_pair_with_autoload( $key, $value, $field->get_autoload() );
68+
}
69+
70+
/**
71+
* Delete the field value(s)
72+
*
73+
* @param Field $field The field to delete.
74+
*/
75+
public function delete( Field $field ) {
76+
if ( ! empty( $field->get_hierarchy() ) ) {
77+
return; // only applicable to root fields
78+
}
79+
$key = $this->get_key_for_field( $field );
80+
delete_option( $key );
81+
}
82+
}

app/PostTypes/Clients.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class Clients extends Plugin {
1010
public function __construct() {
1111

1212
// Sample Custom Post Type - Client
13-
add_action( 'carbon_fields_loaded', array( $this, 'add_post_type_client' ) );
14-
//$this->add_post_type_client();
13+
//add_action( 'carbon_fields_loaded', array( $this, 'add_post_type_client' ) );
14+
$this->add_post_type_client();
1515

1616
// Hide unnecessary publishing options like Draft, visibility, etc.
1717
add_action( 'admin_head-post.php', array( $this, 'hide_publishing_actions' ) );

app/Settings/Carbon_Page.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace VendorName\PluginName\Settings;
33
use VendorName\PluginName\Plugin;
4+
use Carbon_Fields\Datastore\Datastore\Serialized_Theme_Options_Datastore;
45
use Carbon_Fields\Container;
56
use Carbon_Fields\Field;
67

@@ -73,10 +74,13 @@ private function create_tabbed_options_page() {
7374
'left' => __('Left', self::$textdomain)
7475
))
7576
->set_default_value('top'),
76-
Field::make('complex', $this->prefix('slides'), self::$config->get( 'plugin/meta/Name' ) . ' ' . __('Slides', self::$textdomain))->add_fields(array(
77-
Field::make('text', 'title'),
78-
Field::make('image', 'photo'),
79-
)),
77+
Field::make('complex', $this->prefix('slides'), self::$config->get( 'plugin/meta/Name' ) . ' ' . __('Slides', self::$textdomain))
78+
->set_datastore( new Serialized_Theme_Options_Datastore() )
79+
->add_fields(array(
80+
Field::make('text', 'title'),
81+
Field::make('image', 'photo'),
82+
)
83+
),
8084
Field::make('select', $this->prefix('select_dropdown'), __('Favorite Continent', self::$textdomain) )
8185
->add_options(array(
8286
'aria' => __('Asia', self::$textdomain),

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"minimum-stability": "dev",
2929
"autoload": {
3030
"psr-4": {
31-
"VendorName\\PluginName\\": "app/"
31+
"VendorName\\PluginName\\": "app/",
32+
"Carbon_Fields\\Datastore\\Datastore\\": "app/Datastores/"
3233
}
3334
}
3435
}

0 commit comments

Comments
 (0)