Skip to content

Commit 7ca15d1

Browse files
committed
Refactored base class
1 parent 30a41f7 commit 7ca15d1

3 files changed

Lines changed: 234 additions & 136 deletions

File tree

README.md

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ It may also be used as the means of [separating custom code](http://www.billeric
99
## Features
1010

1111
* Namespaces, PSR-4, dependency autoloading
12-
* Object caching helper class [Usage examples](https://github.com/dmhendricks/wordpress-base-plugin/wiki#caching)
12+
* PHP and dependency version checking
13+
* Object caching helper class - [Usage Examples](https://github.com/dmhendricks/wordpress-base-plugin/wiki#caching)
1314
* [Many more to come...](#planned-features)
1415

1516
## Requirements
1617

1718
* WordPress 4.0 or higher
1819
* PHP 5.4 or higher
1920

20-
*PHP 5.3 will work IF all of your dependencies are compatible.*
21-
2221
## Installation
2322

2423
### Clone Repository
@@ -33,35 +32,33 @@ It may also be used as the means of [separating custom code](http://www.billeric
3332

3433
## TODO / Planned Features
3534

36-
* Refactor code
37-
* Add/test/document object caching class
35+
* Fix i18n issues and create means to easily generate `.pot` language file
3836
* Add activation/deactivation/uninstall hooks
39-
* Update to comply with new Carbon Fields standards
4037
* **Add task runner**, related documentation, update .gitignore and rearrange `./assets`
41-
* Fix i18n issues and create `.pot` language file
42-
* Create means to autogenerate language file
4338
* Add Ajax call example
44-
* Add encrypt/decrypt example
45-
* Add hidden `password` field with encrypted `hidden` field
46-
* Possibly add hooks (maybe to assist in consolidation of multiple settings pages?)
39+
* Add encrypt/decrypt example. Allow specifying custom salt, else default to WordPress.
40+
* Add `password` field with encrypted `hidden` field
41+
* Add wordpress-settings-api-class example
4742
* Possibly add TGMPA example
48-
* Possible add Customizer example
49-
* Add wordpress-settings-api-class example(s)
43+
* Added GitHub update checker example
44+
* Possibly add Customizer example
45+
* Ability to create dynamic CSS/JS files based on settings
5046
* Allow loading Carbon Fields via [plugin](https://github.com/dmhendricks/carbon-fields-loader) rather than Composer dependency
51-
* Test compatibility with WordPress 4.0 and higher
5247

5348
## Change Log
5449

5550
#### 0.2.0
5651

52+
This is the last version that is compatible with PHP 5.3 (_If_ all of your dependencies are compatible). Future releases will require PHP 5.4 or higher.
53+
54+
* Significantly refactored dependency checking
55+
* Properly hooked admin notices
5756
* Added object cache helper class
58-
* Added example of loading Font Awesome if enabled in plugin settings
5957
* Removed closing ?> tags ([obstschale](https://github.com/dmhendricks/wordpress-base-plugin/issues/1))
6058
* Removed `./vendor` from repo
6159
* Renamed Helpers class to Utils
62-
* Tested PHP 5.4 - 7.1 compatibility
6360
* Localized many strings
64-
* Fixed various PHP 5.3 issues; bumped minimum suggested version to 5.4.0
61+
* Fixed various PHP 5.3 issues
6562
* Added minimum PHP version check
6663
* Renamed namespace to `VendorName\MyPlugin`
6764
* Added screenshot

app/EnqueueScripts.php

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ class EnqueueScripts extends Plugin {
55

66
function __construct() {
77

8+
// TODO: Put back font awesome
9+
810
// Enqueue frontend and backend scripts
911
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts') );
1012
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts') );
1113

12-
// Load Font Awesome from CDN, if enabled
13-
$enqueue_font_awesome = carbon_get_theme_option( self::$prefix.'enqueue_font_awesome' );
14+
// Load Font Awesome from CDN, if enabled in Settings Page
15+
$enqueue_font_awesome = $this->get_plugin_option( 'enqueue_font_awesome' );
1416
if( $enqueue_font_awesome ) {
1517
if( in_array( 'frontend', $enqueue_font_awesome) )
1618
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_font_awesome') );
@@ -25,8 +27,7 @@ function __construct() {
2527
*/
2628
public function enqueue_frontend_scripts() {
2729

28-
// Enqueuing a script added from ZIP file via composer (http://hgoebl.github.io/mobile-detect.js/),
29-
// purely as an example.
30+
// Purely as an example, enqueuing a script added from ZIP file via composer (http://hgoebl.github.io/mobile-detect.js/)
3031
wp_enqueue_script( 'modile-detect', $this->get_script_url('vendor/hgoebl/mobile-detect/mobile-detect.min.js'), null, '1.3.6' );
3132

3233
// Enqueuing custom CSS for child theme (Twentysixteen was used for testing)
@@ -52,4 +53,70 @@ public function enqueue_font_awesome() {
5253

5354
}
5455

56+
/**
57+
* Returns script ?ver= version based on environment (WP_ENV)
58+
*
59+
* If WP_ENV is not defined or equals anything other than 'development' or 'staging'
60+
* returns $script_version (if defined) else plugin verson. If WP_ENV is defined
61+
* as 'development' or 'staging', returns string representing file last modification
62+
* date (to discourage browser during development).
63+
*
64+
* @param string $script The filesystem path (relative to the script location of
65+
* calling script) to return the version for.
66+
* @param string $script_version (optional) The version that will be returned if
67+
* WP_ENV is defined as anything other than 'development' or 'staging'.
68+
*
69+
* @return string
70+
* @since 0.1.0
71+
*/
72+
public function get_script_version($script, $return_minified = false, $script_version = null) {
73+
$version = $script_version ?: self::$settings['data']['Version'];
74+
if($this->is_production()) return $version;
75+
76+
$script = $this->get_script_path($script, $return_minified);
77+
if(file_exists($script)) {
78+
$version = date("ymd-Gis", filemtime( $script ) );
79+
}
80+
81+
return $version;
82+
}
83+
84+
/**
85+
* Returns script path or URL, either regular or minified (if exists).
86+
*
87+
* If in production mode or if @param $force_minify == true, inserts '.min' to the filename
88+
* (if exists), else return script name without (example: style.css vs style.min.css).
89+
*
90+
* @param string $script The relative (to the plugin folder) path to the script.
91+
* @param bool $return_minified If true and is_production() === true then will prefix the
92+
* extension with .min. NB! Due to performance reasons, I did not include logic to check
93+
* to see if the script_name.min.ext exists, so use only when you know it exists.
94+
* @param bool $return_url If true, returns full-qualified URL rather than filesystem path.
95+
*
96+
* @return string The URL or path to minified or regular $script.
97+
* @since 0.1.0
98+
*/
99+
public function get_script_path($script, $return_minified = false, $return_url = false) {
100+
$script = trim($script, '/');
101+
if($return_minified && strpos($script, '.') && $this->is_production()) {
102+
$script_parts = explode('.', $script);
103+
$script_extension = end($script_parts);
104+
array_pop($script_parts);
105+
$script = implode('.', $script_parts) . '.min.' . $script_extension;
106+
}
107+
108+
return self::$settings[$return_url ? 'url' : 'path'] . $script;
109+
}
110+
111+
/**
112+
* Returns absolute URL of $script.
113+
*
114+
* @param string $script The relative (to the plugin folder) path to the script.
115+
* @param bool
116+
* @since 0.1.0
117+
*/
118+
public function get_script_url($script, $return_minified = false) {
119+
return $this->get_script_path($script, $return_minified, true);
120+
}
121+
55122
}

0 commit comments

Comments
 (0)