Skip to content

Commit 9985afa

Browse files
committed
Refactored configuration management
1 parent 3e058fd commit 9985afa

8 files changed

Lines changed: 53 additions & 43 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,15 @@ Release changes are noted on the [Releases](https://github.com/dmhendricks/wordp
6666
#### Branch: `master`
6767

6868
* Bumped minimum PHP version check to 5.6
69+
* Drastically refactored configuration management
6970
* Split out settings pages, shortcodes, CPT & widgets into separate files/classes (thanks [obstschale](https://github.com/obstschale/wordpress-base-plugin))
7071
* Added `wp-pot-cli` to `package.json` to create `.pot` translation file
71-
* Added `Utils::is_json()` function to test for valid syntax
7272
* Added `register_uninstall_hook` to delete Carbon Fields settings when plugin uninstalled
7373
* Added [wordpress-settings-api-class](https://github.com/tareq1988/wordpress-settings-api-class) settings page example
7474
* Added `WPTK_DISABLE_CACHE` constant
7575
* Added `VERSION` constant ([info](https://github.com/dmhendricks/wordpress-base-plugin/wiki/Configuration-&-Constants#defined-by-plugin))
7676
* Added [wordpress-toolkit](https://github.com/dmhendricks/wordpress-toolkit) as dependency
77+
* Renamed `Utils` class to `Helpers`
7778

7879
## Credits
7980

app/Core.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ function __construct() {
2626
* @since 0.1.0
2727
*/
2828
public function add_body_classes($classes) {
29-
$parent_slug = Utils::get_parent_slug(true);
30-
$categories = is_single() ? Utils::get_post_categories(true) : array();
29+
$parent_slug = Helpers::get_parent_slug(true);
30+
$categories = is_single() ? Helpers::get_post_categories(true) : array();
3131

3232
// Add page, parent and post-type classes, if available
33-
$classes[] = 'page-' . Utils::get_page_slug();
33+
$classes[] = 'page-' . Helpers::get_page_slug();
3434
if($parent_slug) $classes[] = 'parent-' . $parent_slug;
3535
$classes[] = 'post-type-' . get_post_type();
3636

app/EnqueueScripts.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function enqueue_font_awesome() {
6767
* @since 0.1.0
6868
*/
6969
public function get_script_version($script, $return_minified = false, $script_version = null) {
70-
$version = $script_version ?: self::$settings['data']['Version'];
70+
$version = $script_version ?: self::$config->get( 'plugin/meta/Version' );
7171
if($this->is_production()) return $version;
7272

7373
$script = $this->get_script_path($script, $return_minified);
@@ -102,7 +102,7 @@ public function get_script_path($script, $return_minified = false, $return_url =
102102
$script = implode('.', $script_parts) . '.min.' . $script_extension;
103103
}
104104

105-
return self::$settings[$return_url ? 'url' : 'path'] . $script;
105+
return self::$config->get( $return_url ? 'plugin/url' : 'plugin/path' ) . $script;
106106
}
107107

108108
/**

app/Utils.php renamed to app/Helpers.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Carbon_Fields\Container;
44
use Carbon_Fields\Field;
55

6-
class Utils extends Plugin {
6+
class Helpers extends Plugin {
77

88
/**
99
* Display a notice/message in WP Admin
@@ -177,7 +177,7 @@ public static function get_post_categories($as_slugs = false, $post_id = null) {
177177
*/
178178
public static function encrypt( $str ) {
179179
$salt = defined( 'WP_ENCRYPT_KEY' ) && WP_ENCRYPT_KEY ? WP_ENCRYPT_KEY : SECURE_AUTH_KEY;
180-
return openssl_encrypt($str, self::$settings['encrypt_method'], $salt);
180+
return openssl_encrypt($str, self::$config->get( 'encrypt_method' ), $salt);
181181
}
182182

183183
/**
@@ -186,11 +186,11 @@ public static function encrypt( $str ) {
186186
* @param string $str String to decrypt
187187
* @return string Decrypted string
188188
* @since 0.2.1
189-
* @see Utils::encrypt()
189+
* @see Helpers::encrypt()
190190
*/
191191
public static function decrypt( $str ) {
192192
$salt = defined( 'WP_ENCRYPT_KEY' ) && WP_ENCRYPT_KEY ? WP_ENCRYPT_KEY : SECURE_AUTH_KEY;
193-
return openssl_decrypt($str, self::$settings['encrypt_method'], $salt);
193+
return openssl_decrypt($str, self::$config->get( 'encrypt_method' ), $salt);
194194
}
195195

196196
}

app/Plugin.php

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,45 @@
11
<?php
22
namespace VendorName\PluginName;
33
use WordPress_ToolKit\ObjectCache;
4+
use WordPress_ToolKit\ConfigRegistry;
45
use Carbon_Fields\Container;
56
use Carbon_Fields\Field;
67
use Config;
78

89
class Plugin {
910

10-
public static $settings;
1111
public static $textdomain;
1212
protected static $cache;
13+
protected static $config;
1314

14-
function __construct( $_settings ) {
15+
function __construct() {
1516

1617
// Initialize plugin settings
17-
$plugin_config = new Config\Config( $_settings['path'] . 'plugin.json' );
18-
self::$textdomain = $_settings['data']['TextDomain'];
19-
self::$settings = array_merge( $_settings, $plugin_config->get() );
18+
19+
// Get plugin path, URL, identifier and slug
20+
$plugin_data['path'] = plugin_dir_path( __DIR__ );
21+
$plugin_data['slug'] = end( explode( '/', trim( $plugin_data['path'], '/' ) ) );
22+
$plugin_data['file'] = end( explode( '/', debug_backtrace()[0]['file'] ) );
23+
$plugin_data = array( 'plugin' => array(
24+
'identifier' => $plugin_data['slug'] . DIRECTORY_SEPARATOR . $plugin_data['file'],
25+
'slug' => $plugin_data['slug'],
26+
'path' => $plugin_data['path'],
27+
'url' => plugin_dir_url( __DIR__ ),
28+
'meta' => get_plugin_data( $plugin_data['path'] . $plugin_data['file'] )
29+
));
30+
31+
self::$config = new ConfigRegistry( $plugin_data['plugin']['path'] . 'plugin.json' );
32+
self::$config = self::$config->merge( new ConfigRegistry( $plugin_data ) );
33+
self::$textdomain = self::$config->get( 'plugin/meta/Name' ) ?: self::$config->get( 'plugin/slug' );
2034

2135
// Define plugin version constant
22-
if ( !defined( __NAMESPACE__ . '\VERSION' ) ) define( __NAMESPACE__ . '\VERSION', $_settings['data']['Version'] );
36+
if ( !defined( __NAMESPACE__ . '\VERSION' ) ) define( __NAMESPACE__ . '\VERSION', self::$config->get( 'plugin/meta/Version' ) );
2337

2438
// Initialize ObjectCache
25-
self::$cache = new ObjectCache( self::$settings );
39+
self::$cache = new ObjectCache( self::$config );
2640

2741
// Verify dependecies and load plugin logic
28-
register_activation_hook( self::$settings['plugin_file'], array( $this, 'activate' ) );
42+
register_activation_hook( self::$config->get( 'plugin/identifier' ), array( $this, 'activate' ) );
2943
add_action( 'plugins_loaded', array( $this, 'init' ) );
3044

3145
}
@@ -105,10 +119,10 @@ public function load_plugin() {
105119
*/
106120
private function verify_dependencies( $deps = true, $args = array() ) {
107121

108-
if( is_bool( $deps ) && $deps ) $deps = self::$settings['dependencies'];
109-
if( !is_array( $deps ) ) $deps = array( $deps => self::$settings['dependencies'][$deps] );
122+
if( is_bool( $deps ) && $deps ) $deps = self::$config->get( 'dependencies' );
123+
if( !is_array( $deps ) ) $deps = array( $deps => self::$config->get( 'dependencies/' . $deps ) );
110124

111-
$args = Utils::set_default_atts( array(
125+
$args = Helpers::set_default_atts( array(
112126
'echo' => true,
113127
'activate' => true
114128
), $args);
@@ -122,7 +136,7 @@ private function verify_dependencies( $deps = true, $args = array() ) {
122136
case 'php':
123137

124138
if( version_compare( phpversion(), $version, '<' ) ) {
125-
$notices[] = __( 'This plugin is not supported on versions of PHP below', self::$textdomain ) . ' ' . self::$settings['dependencies']['php'] . '.' ;
139+
$notices[] = __( 'This plugin is not supported on versions of PHP below', self::$textdomain ) . ' ' . self::$config->get( 'dependencies/php' ) . '.' ;
126140
}
127141
break;
128142

@@ -132,7 +146,7 @@ private function verify_dependencies( $deps = true, $args = array() ) {
132146
if( !$args['activate'] && !defined('\\Carbon_Fields\\VERSION') ) {
133147
$notices[] = __( 'An unknown error occurred while trying to load the Carbon Fields framework.', self::$textdomain );
134148
} else if ( defined('\\Carbon_Fields\\VERSION') && version_compare( \Carbon_Fields\VERSION, $version, '<' ) ) {
135-
$notices[] = __( 'An outdated version of Carbon Fields has been detected:', self::$textdomain ) . ' ' . \Carbon_Fields\VERSION . ' (&gt;= '.self::$settings['dependencies']['carbon_fields'] . ' ' . __( 'required', self::$textdomain ) . ').' . ' <strong>' . self::$settings['data']['Name'] . '</strong> ' . __( 'deactivated.', self::$textdomain ) ;
149+
$notices[] = __( 'An outdated version of Carbon Fields has been detected:', self::$textdomain ) . ' ' . \Carbon_Fields\VERSION . ' (&gt;= ' . self::$config->get( 'dependencies/carbon_fields' ) . ' ' . __( 'required', self::$textdomain ) . ').' . ' <strong>' . self::$config->get( 'plugin/meta/Name' ) . '</strong> ' . __( 'deactivated.', self::$textdomain ) ;
136150
}
137151
break;
138152

@@ -142,12 +156,12 @@ private function verify_dependencies( $deps = true, $args = array() ) {
142156

143157
if( $notices ) {
144158

145-
deactivate_plugins( self::$settings['plugin_file'] );
159+
deactivate_plugins( self::$config->get( 'plugin/identifier' ) );
146160

147161
$notices = '<ul><li>' . implode( "</li>\n<li>", $notices ) . '</li></ul>';
148162

149163
if( $args['echo'] ) {
150-
Utils::show_notice($notices, 'error', false);
164+
Helpers::show_notice($notices, 'error', false);
151165
return false;
152166
} else {
153167
return $notices;
@@ -211,14 +225,14 @@ public static function get_const( $const, $filter_validate = null ) {
211225
}
212226

213227
/**
214-
* A wrapper for the plugin's data fiala prefix as defined in $settings
228+
* A wrapper for the plugin's data fiala prefix as defined in $config
215229
*
216230
* @param string|null $str The string/field to prefix
217231
* @return string Prefixed string/field value
218232
* @since 0.2.0
219233
*/
220234
public function prefix( $field_name = null ) {
221-
return $field_name !== null ? self::$settings['prefix'] . $field_name : self::$settings['prefix'];
235+
return $field_name !== null ? self::$config->get( 'prefix' ) . $field_name : self::$config->get( 'prefix' );
222236
}
223237

224238
/**

app/Settings/Carbon_Page.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct() {
2727

2828
// Register uninstall hook to delete settings
2929
if( $this->get_plugin_option( 'uninstall_remove_settings' ) ) {
30-
register_uninstall_hook( self::$settings['plugin_file'], array( $this, 'plugin_settings_uninstall' ) );
30+
register_uninstall_hook( self::$config->get( 'plugin/identifier' ), array( $this, 'plugin_settings_uninstall' ) );
3131
}
3232

3333
}
@@ -40,7 +40,7 @@ public function __construct() {
4040
private function create_tabbed_options_page() {
4141

4242
// Carbon Fields Docs: https://carbonfields.net/docs/containers-theme-options/
43-
$container = Container::make( 'theme_options', self::$settings['data']['Name'] )
43+
$container = Container::make( 'theme_options', self::$config->get( 'plugin/meta/Name' ) )
4444
->set_page_parent('options-general.php')
4545
->add_tab( __('General', self::$textdomain), array(
4646
Field::make('checkbox', $this->prefix('uninstall_remove_settings'), __('Delete Plugin Settings On Uninstall', self::$textdomain) ),
@@ -72,7 +72,7 @@ private function create_tabbed_options_page() {
7272
'left' => __('Left', self::$textdomain)
7373
))
7474
->set_default_value('top'),
75-
Field::make('complex', $this->prefix('slides'), self::$settings['data']['Name'] . ' ' . __('Slides', self::$textdomain))->add_fields(array(
75+
Field::make('complex', $this->prefix('slides'), self::$config->get( 'plugin/meta/Name' ) . ' ' . __('Slides', self::$textdomain))->add_fields(array(
7676
Field::make('text', 'title'),
7777
Field::make('image', 'photo'),
7878
)),
@@ -107,11 +107,11 @@ private function create_tabbed_options_page() {
107107
*/
108108
private function create_single_options_page() {
109109

110-
Container::make('theme_options', self::$settings['data']['Name'])
110+
Container::make( 'theme_options', self::$config->get( 'plugin/meta/Name' ) )
111111
->set_page_parent('options-general.php')
112-
->add_fields(array(
113-
Field::make('text', $this->prefix('your_name'), __('Your Name', self::$textdomain) ),
114-
Field::make('image', $this->prefix('profile_pic'), __('Profile Pic', self::$textdomain) )
112+
->add_fields( array(
113+
Field::make( 'text', $this->prefix('your_name'), __( 'Your Name', self::$textdomain) ),
114+
Field::make( 'image', $this->prefix('profile_pic'), __('Profile Pic', self::$textdomain) )
115115
)
116116
);
117117

app/Settings/WPSAC_Page.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function wpsac_admin_init() {
4242
* @since 0.3.0
4343
*/
4444
public function wpsac_admin_menu() {
45-
add_options_page( self::$settings['short_name'], self::$settings['short_name'] . ' ' . __( 'Settings', self::$textdomain ) . ' (WPSAC)', 'manage_options', sanitize_title( self::$settings['data']['Name'] ), array( $this, 'create_wpsac_settings_page' ) );
45+
add_options_page( self::$config->get( 'short_name' ), self::$config->get( 'short_name' ) . ' ' . __( 'Settings', self::$textdomain ) . ' (WPSAC)', 'manage_options', sanitize_title( self::$config->get( 'plugin/meta/Name' ) ), array( $this, 'create_wpsac_settings_page' ) );
4646
}
4747

4848
/**
@@ -56,7 +56,7 @@ public function get_settings_sections() {
5656

5757
array(
5858
'id' => $this->prefix( 'general' ),
59-
'title' => self::$settings['short_name'] . ' ' . __( 'Settings', self::$textdomain ) . ' (WPSAC)'
59+
'title' => self::$config->get( 'short_name' ) . ' ' . __( 'Settings', self::$textdomain ) . ' (WPSAC)'
6060
)
6161
);
6262

wordpress-base-plugin.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,5 @@
3636
require( __DIR__ . '/vendor/autoload.php' );
3737
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
3838

39-
// Initialize plugin - Change to use your own namespace
40-
new \VendorName\PluginName\Plugin( array(
41-
'data' => get_plugin_data( __FILE__ ),
42-
'path' => trailingslashit( realpath( plugin_dir_path( __FILE__ ) ) ),
43-
'url' => plugin_dir_url( __FILE__ ),
44-
'plugin_file' => plugin_basename( __FILE__ )
45-
) );
39+
// Initialize plugin
40+
new \VendorName\PluginName\Plugin();

0 commit comments

Comments
 (0)