11<?php
22namespace VendorName \PluginName ;
33use WordPress_ToolKit \ObjectCache ;
4+ use WordPress_ToolKit \ConfigRegistry ;
45use Carbon_Fields \Container ;
56use Carbon_Fields \Field ;
67use Config ;
78
89class 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 . ' (>= ' . 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 . ' (>= ' . 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 /**
0 commit comments