Skip to content

Commit 95223bb

Browse files
committed
Moved script URL functions to Helpers class
1 parent 7f258fa commit 95223bb

2 files changed

Lines changed: 92 additions & 92 deletions

File tree

app/EnqueueScripts.php

Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function __construct() {
1717
$enqueue_font_awesome = $this->get_plugin_option( 'enqueue_font_awesome' );
1818
if( $enqueue_font_awesome ) {
1919
if( in_array( 'frontend', $enqueue_font_awesome) )
20-
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_font_awesome') );
20+
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_font_awesome' ) );
2121
if( in_array( 'backend', $enqueue_font_awesome) )
2222
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_font_awesome') );
2323
}
@@ -34,10 +34,10 @@ public function enqueue_frontend_scripts() {
3434
$this->enqueue_bower_scripts();
3535

3636
// Enqueuing custom CSS for child theme (Twentysixteen was used for testing)
37-
wp_enqueue_style( 'wordpress-base-plugin', $this->get_script_url('assets/css/wordpress-base-plugin.css'), null, $this->get_script_version('assets/css/wordpress-base-plugin.css') );
37+
wp_enqueue_style( 'wordpress-base-plugin', Helpers::get_script_url( 'assets/css/wordpress-base-plugin.css' ), null, Helpers::get_script_version( 'assets/css/wordpress-base-plugin.css' ) );
3838

3939
// Enqueue frontend JavaScript
40-
wp_enqueue_script( 'wordpress-base-plugin', $this->get_script_url('assets/js/wordpress-base-plugin.js'), array('jquery', 'jquery-waituntilexists'), $this->get_script_version('assets/js/wordpress-base-plugin.js'), true );
40+
wp_enqueue_script( 'wordpress-base-plugin', Helpers::get_script_url( 'assets/js/wordpress-base-plugin.js' ), array( 'jquery', 'jquery-waituntilexists' ), Helpers::get_script_version( 'assets/js/wordpress-base-plugin.js' ), true );
4141
wp_localize_script( 'wordpress-base-plugin', 'wpbp_ajax_filter_params', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
4242

4343
}
@@ -52,10 +52,10 @@ public function enqueue_admin_scripts() {
5252
$this->enqueue_bower_scripts();
5353

5454
// Enqueuing custom CSS for child theme (Twentysixteen was used for testing)
55-
wp_enqueue_style( 'wordpress-base-plugin', $this->get_script_url('assets/css/wordpress-base-plugin-admin.css'), null, $this->get_script_version('assets/css/wordpress-base-plugin-admin.css') );
55+
wp_enqueue_style( 'wordpress-base-plugin', Helpers::get_script_url( 'assets/css/wordpress-base-plugin-admin.css' ), null, Helpers::get_script_version( 'assets/css/wordpress-base-plugin-admin.css' ) );
5656

5757
// Enqueue WP Admin JavaScript
58-
wp_enqueue_script( 'wordpress-base-plugin-admin', $this->get_script_url('assets/js/wordpress-base-plugin-admin.js'), array('jquery', 'jquery-waituntilexists'), $this->get_script_version('assets/js/wordpress-base-plugin-admin.js'), true );
58+
wp_enqueue_script( 'wordpress-base-plugin-admin', Helpers::get_script_url( 'assets/js/wordpress-base-plugin-admin.js' ), array('jquery', 'jquery-waituntilexists'), Helpers::get_script_version( 'assets/js/wordpress-base-plugin-admin.js' ), true );
5959
wp_localize_script( 'wordpress-base-plugin-admin', 'wpbp_ajax_filter_params', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
6060

6161
}
@@ -67,7 +67,7 @@ public function enqueue_admin_scripts() {
6767
private function enqueue_bower_scripts() {
6868

6969
// Enqueue common (frontend/backend) JavaScript
70-
wp_enqueue_script( 'jquery-waituntilexists', $this->get_script_url('assets/components/jq.waituntilexists/jquery.waitUntilExists.min.js', false), array('jquery'), '0.1.0' );
70+
wp_enqueue_script( 'jquery-waituntilexists', Helpers::get_script_url( 'assets/components/jq.waituntilexists/jquery.waitUntilExists.min.js', false ), array( 'jquery' ), '0.1.0' );
7171

7272
}
7373

@@ -98,70 +98,4 @@ public function inject_javascript_settings() {
9898

9999
}
100100

101-
/**
102-
* Returns script ?ver= version based on environment (WP_ENV)
103-
*
104-
* If WP_ENV is not defined or equals anything other than 'development' or 'staging'
105-
* returns $script_version (if defined) else plugin verson. If WP_ENV is defined
106-
* as 'development' or 'staging', returns string representing file last modification
107-
* date (to discourage browser during development).
108-
*
109-
* @param string $script The filesystem path (relative to the script location of
110-
* calling script) to return the version for.
111-
* @param string $script_version (optional) The version that will be returned if
112-
* WP_ENV is defined as anything other than 'development' or 'staging'.
113-
*
114-
* @return string
115-
* @since 0.1.0
116-
*/
117-
public function get_script_version($script, $return_minified = false, $script_version = null) {
118-
$version = $script_version ?: self::$config->get( 'plugin/meta/Version' );
119-
if($this->is_production()) return $version;
120-
121-
$script = $this->get_script_path($script, $return_minified);
122-
if(file_exists($script)) {
123-
$version = date("ymd-Gis", filemtime( $script ) );
124-
}
125-
126-
return $version;
127-
}
128-
129-
/**
130-
* Returns script path or URL, either regular or minified (if exists).
131-
*
132-
* If in production mode or if @param $force_minify == true, inserts '.min' to the filename
133-
* (if exists), else return script name without (example: style.css vs style.min.css).
134-
*
135-
* @param string $script The relative (to the plugin folder) path to the script.
136-
* @param bool $return_minified If true and is_production() === true then will prefix the
137-
* extension with .min. NB! Due to performance reasons, I did not include logic to check
138-
* to see if the script_name.min.ext exists, so use only when you know it exists.
139-
* @param bool $return_url If true, returns full-qualified URL rather than filesystem path.
140-
*
141-
* @return string The URL or path to minified or regular $script.
142-
* @since 0.1.0
143-
*/
144-
public function get_script_path($script, $return_minified = true, $return_url = false) {
145-
$script = trim($script, '/');
146-
if($return_minified && strpos($script, '.') && $this->is_production()) {
147-
$script_parts = explode('.', $script);
148-
$script_extension = end($script_parts);
149-
array_pop($script_parts);
150-
$script = implode('.', $script_parts) . '.min.' . $script_extension;
151-
}
152-
153-
return self::$config->get( $return_url ? 'plugin/url' : 'plugin/path' ) . $script;
154-
}
155-
156-
/**
157-
* Returns absolute URL of $script.
158-
*
159-
* @param string $script The relative (to the plugin folder) path to the script.
160-
* @param bool
161-
* @since 0.1.0
162-
*/
163-
public function get_script_url($script, $return_minified = false) {
164-
return $this->get_script_path($script, $return_minified, true);
165-
}
166-
167101
}

app/Helpers.php

Lines changed: 86 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ private function array_merge_recursive_distinct( array &$array1, array &$array2
4747
foreach ( $array2 as $key => &$value )
4848
{
4949
if ( is_array ( $value ) && isset ( $merged [$key] ) && is_array ( $merged [$key] ) ) {
50-
$merged [$key] = self::array_merge_recursive_distinct ( $merged [$key], $value );
50+
$merged[$key] = self::array_merge_recursive_distinct ( $merged[$key], $value );
5151
} else {
52-
$merged [$key] = $value;
52+
$merged[$key] = $value;
5353
}
5454
}
5555

@@ -67,18 +67,18 @@ private function array_merge_recursive_distinct( array &$array1, array &$array2
6767
* else $post global is used instead
6868
* @return string The slug of the specified/current post
6969
*/
70-
public static function get_page_slug($post_id = null) {
70+
public static function get_page_slug( $post_id = null ) {
7171
global $post;
7272

73-
$_slug = $post_id ? get_post($post_id)->post_name : $post->post_name;
73+
$_slug = $post_id ? get_post( $post_id )->post_name : $post->post_name;
7474

75-
if(is_front_page()) {
75+
if( is_front_page() ) {
7676
$_slug = 'front';
77-
} else if(is_search()) {
77+
} else if( is_search() ) {
7878
$_slug = 'search';
79-
} else if(is_archive()) {
79+
} else if( is_archive() ) {
8080
$_slug = 'archive';
81-
} else if(is_single()) {
81+
} else if( is_single() ) {
8282
$_slug = 'single';
8383
}
8484

@@ -98,18 +98,18 @@ public static function get_page_slug($post_id = null) {
9898
* else $post global is used instead
9999
* @return string The parent slug of the specified post, if availaable
100100
*/
101-
public static function get_parent_slug($include_self_as_parent_if_root = false, $post_id = null) {
101+
public static function get_parent_slug( $include_self_as_parent_if_root = false, $post_id = null ) {
102102
global $post;
103-
$post_id = $post_id ? $post_id : $post->ID;
103+
$post_id = $post_id ? $post_id : @$post->ID;
104104

105-
if (is_page()) {
106-
if(get_post($post_id)->post_parent) {
107-
@$parent = end(get_post_ancestors($post_id));
105+
if ( is_page() ) {
106+
if( get_post( $post_id )->post_parent ) {
107+
$parent = @end( get_post_ancestors( $post_id ) ) ;
108108
} else {
109109
$parent = $post->ID;
110110
}
111-
$post_data = get_post($parent, ARRAY_A);
112-
if($include_self_as_parent_if_root || $post_data['post_name'] != self::get_page_slug($post_id)) return $post_data['post_name'];
111+
$post_data = get_post( $parent, ARRAY_A );
112+
if( $include_self_as_parent_if_root || $post_data['post_name'] != self::get_page_slug( $post_id ) ) return $post_data['post_name'];
113113
}
114114
return array();
115115
}
@@ -129,17 +129,83 @@ public static function get_post_categories($as_slugs = false, $post_id = null) {
129129
global $post;
130130
$return = array();
131131

132-
@$post_id = $post_id ? $post_id : $post->ID;
133-
if(!$post_id) return $return;
132+
$post_id = $post_id ? $post_id : @$post->ID;
133+
if( !$post_id ) return $return;
134134

135-
$categories = get_the_category($post_id);
136-
if(!$categories) return $return;
135+
$categories = get_the_category( $post_id );
136+
if( !$categories ) return $return;
137137

138-
foreach($categories as $cat) {
138+
foreach( $categories as $cat ) {
139139
$return[] = $as_slugs ? $cat->slug : $cat->name;
140140
}
141141

142142
return $return;
143143
}
144144

145+
/**
146+
* Returns script ?ver= version based on environment (WP_ENV)
147+
*
148+
* If WP_ENV is not defined or equals anything other than 'development' or 'staging'
149+
* returns $script_version (if defined) else plugin verson. If WP_ENV is defined
150+
* as 'development' or 'staging', returns string representing file last modification
151+
* date (to discourage browser during development).
152+
*
153+
* @param string $script The filesystem path (relative to the script location of
154+
* calling script) to return the version for.
155+
* @param string $script_version (optional) The version that will be returned if
156+
* WP_ENV is defined as anything other than 'development' or 'staging'.
157+
*
158+
* @return string
159+
* @since 0.1.0
160+
*/
161+
public function get_script_version( $script, $return_minified = false, $script_version = null ) {
162+
$version = $script_version ?: self::$config->get( 'plugin/meta/Version' );
163+
if( self::is_production() ) return $version;
164+
165+
$script = $this->get_script_path( $script, $return_minified );
166+
if( file_exists($script) ) {
167+
$version = date( "ymd-Gis", filemtime( $script ) );
168+
}
169+
170+
return $version;
171+
}
172+
173+
/**
174+
* Returns script path or URL, either regular or minified (if exists).
175+
*
176+
* If in production mode or if @param $force_minify == true, inserts '.min' to the filename
177+
* (if exists), else return script name without (example: style.css vs style.min.css).
178+
*
179+
* @param string $script The relative (to the plugin folder) path to the script.
180+
* @param bool $return_minified If true and is_production() === true then will prefix the
181+
* extension with .min. NB! Due to performance reasons, I did not include logic to check
182+
* to see if the script_name.min.ext exists, so use only when you know it exists.
183+
* @param bool $return_url If true, returns full-qualified URL rather than filesystem path.
184+
*
185+
* @return string The URL or path to minified or regular $script.
186+
* @since 0.1.0
187+
*/
188+
public function get_script_path( $script, $return_minified = true, $return_url = false ) {
189+
$script = trim( $script, '/' );
190+
if( $return_minified && strpos( $script, '.' ) && $this->is_production() ) {
191+
$script_parts = explode( '.', $script );
192+
$script_extension = end( $script_parts );
193+
array_pop( $script_parts );
194+
$script = implode( '.', $script_parts ) . '.min.' . $script_extension;
195+
}
196+
197+
return self::$config->get( $return_url ? 'plugin/url' : 'plugin/path' ) . $script;
198+
}
199+
200+
/**
201+
* Returns absolute URL of $script.
202+
*
203+
* @param string $script The relative (to the plugin folder) path to the script.
204+
* @param bool
205+
* @since 0.1.0
206+
*/
207+
public function get_script_url( $script, $return_minified = false ) {
208+
return self::get_script_path( $script, $return_minified, true );
209+
}
210+
145211
}

0 commit comments

Comments
 (0)