Skip to content

Commit

Permalink
Merge pull request #77 from szepeviktor/wpcs
Browse files Browse the repository at this point in the history
Make WPCS happy and the code more readable, props @szepeviktor
  • Loading branch information
kasparsd authored Aug 28, 2016
2 parents d847702 + 87cd989 commit 6e1e7de
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 56 deletions.
11 changes: 6 additions & 5 deletions include/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public function init() {
function plugin_action_link_cache_bump( $links ) {

$links[] = sprintf(
'<a href="%s">%s</a>',
wp_nonce_url( add_query_arg( 'purge_minit', true ), 'purge_minit' ),
__( 'Purge cache', 'minit' )
);
'<a href="%s">%s</a>',
wp_nonce_url( add_query_arg( 'purge_minit', true ), 'purge_minit' ),
__( 'Purge cache', 'minit' )
);

return $links;

Expand All @@ -39,8 +39,9 @@ function plugin_action_link_cache_bump( $links ) {

function cache_bump() {

if ( ! isset( $_GET['purge_minit'] ) || ! check_admin_referer( 'purge_minit' ) )
if ( ! isset( $_GET['purge_minit'] ) || ! check_admin_referer( 'purge_minit' ) ) {
return;
}

$this->plugin->cache_bump();

Expand Down
18 changes: 11 additions & 7 deletions include/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

function minit_comment_combined( $content, $object, $handle ) {

if ( ! $content )
if ( ! $content ) {
return $content;
}

return sprintf(
"\n\n/* Minit: %s */\n",
$object->registered[ $handle ]->src
) . $content;
"\n\n/* Minit: %s */\n",
$object->registered[ $handle ]->src
) . $content;

}

Expand All @@ -23,13 +24,15 @@ function minit_comment_combined( $content, $object, $handle ) {

function minit_add_toc( $content, $items ) {

if ( ! $content || empty( $items ) )
if ( ! $content || empty( $items ) ) {
return $content;
}

$toc = array();

foreach ( $items as $handle => $item_content )
foreach ( $items as $handle => $item_content ) {
$toc[] = sprintf( ' - %s', $handle );
}

return sprintf( "/* Contents:\n%s\n*/", implode( "\n", $toc ) ) . $content;

Expand All @@ -42,8 +45,9 @@ function minit_add_toc( $content, $items ) {

function minit_maybe_ssl_url( $url ) {

if ( is_ssl() )
if ( is_ssl() ) {
return str_replace( 'http://', 'https://', $url );
}

return $url;

Expand Down
52 changes: 34 additions & 18 deletions include/minit-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ function __construct( $handler, $extension = null, $revision = null ) {

$this->handler = $handler;

if ( empty( $extension ) )
if ( empty( $extension ) ) {
$extension = get_class( $handler );
}

$this->extension = $extension;
$this->revision = $revision;
Expand All @@ -36,8 +37,9 @@ abstract function init();
*/
function register( $todo ) {

if ( empty( $todo ) )
if ( empty( $todo ) ) {
return $todo;
}

// Queue all of them for Minit
$this->queue = array_merge( $this->queue, $todo );
Expand All @@ -56,8 +58,9 @@ function minit() {

$done = array();

if ( empty( $this->queue ) )
if ( empty( $this->queue ) ) {
return false;
}

// Allow others to exclude handles from Minit
$exclude = (array) apply_filters( 'minit-exclude-' . $this->extension, array() );
Expand All @@ -70,8 +73,9 @@ function minit() {
);

// Include individual scripts versions in the cache key
foreach ( $this->queue as $handle )
foreach ( $this->queue as $handle ) {
$ver[] = sprintf( '%s-%s', $handle, $this->handler->registered[ $handle ]->ver );
}

$cache_ver = md5( 'minit-' . implode( '-', $ver ) );

Expand All @@ -86,19 +90,22 @@ function minit() {

foreach ( $this->queue as $handle ) {

if ( in_array( $handle, $exclude ) )
if ( in_array( $handle, $exclude ) ) {
continue;
}

// Ignore pseudo packages such as jquery which return src as empty string
if ( empty( $this->handler->registered[ $handle ]->src ) )
if ( empty( $this->handler->registered[ $handle ]->src ) ) {
$done[ $handle ] = null;
}

// Get the relative URL of the asset
$src = $this->get_asset_relative_path( $handle );

// Skip if the file is not hosted locally
if ( empty( $src ) || ! file_exists( ABSPATH . $src ) )
if ( empty( $src ) || ! file_exists( ABSPATH . $src ) ) {
continue;
}

$item = $this->minit_item( file_get_contents( ABSPATH . $src ), $handle, $src );

Expand All @@ -109,22 +116,25 @@ function minit() {
$handle
);

if ( false !== $item )
if ( false !== $item ) {
$done[ $handle ] = $item;

}
}

if ( empty( $done ) )
if ( empty( $done ) ) {
return false;
}

$this->mark_done( array_keys( $done ) );

$wp_upload_dir = wp_upload_dir();

// Try to create the folder for cache
if ( ! is_dir( $wp_upload_dir['basedir'] . '/minit' ) )
if ( ! mkdir( $wp_upload_dir['basedir'] . '/minit' ) )
if ( ! is_dir( $wp_upload_dir['basedir'] . '/minit' ) ) {
if ( ! mkdir( $wp_upload_dir['basedir'] . '/minit' ) ) {
return false;
}
}

$combined_file_path = sprintf( '%s/minit/%s.%s', $wp_upload_dir['basedir'], $cache_ver, $this->extension );
$combined_file_url = sprintf( '%s/minit/%s.%s', $wp_upload_dir['baseurl'], $cache_ver, $this->extension );
Expand All @@ -136,9 +146,11 @@ function minit() {
$done_imploded = apply_filters( 'minit-content-' . $this->extension, implode( "\n\n", $done ), $done );

// Store the combined file on the filesystem
if ( ! file_exists( $combined_file_path ) )
if ( ! file_put_contents( $combined_file_path, $done_imploded ) )
if ( ! file_exists( $combined_file_path ) ) {
if ( ! file_put_contents( $combined_file_path, $done_imploded ) ) {
return false;
}
}

// Cache this set of scripts, by default for 24 hours
$cache_ttl = apply_filters( 'minit-cache-expiration', 24 * 60 * 60 );
Expand Down Expand Up @@ -207,28 +219,32 @@ protected function mark_done( $handles ) {
*/
protected function get_asset_relative_path( $handle ) {

if ( ! isset( $this->handler->registered[ $handle ] ) )
if ( ! isset( $this->handler->registered[ $handle ] ) ) {
return false;
}

$item_url = $this->handler->registered[ $handle ]->src;

if ( empty( $item_url ) )
if ( empty( $item_url ) ) {
return false;
}

// Remove protocol reference from the local base URL
$base_url = preg_replace( '/^(https?:)/i', '', $this->handler->base_url );

// Check if this is a local asset which we can include
$src_parts = explode( $base_url, $item_url );

if ( empty( $src_parts ) )
if ( empty( $src_parts ) ) {
return false;
}

// Get the trailing part of the local URL
$maybe_relative = array_pop( $src_parts );

if ( file_exists( ABSPATH . $maybe_relative ) )
if ( file_exists( ABSPATH . $maybe_relative ) ) {
return $maybe_relative;
}

return false;

Expand Down
35 changes: 20 additions & 15 deletions include/minit-css.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ function process( $todo ) {
// Can this return an array instead?
$inline_styles = $this->handler->get_data( $script, 'after' );

if ( ! empty( $inline_styles ) )
if ( ! empty( $inline_styles ) ) {
$this->handler->add_inline_style( $handle, implode( "\n", $inline_styles ) );

}
}

return $todo;
Expand All @@ -59,8 +59,9 @@ function process( $todo ) {

function minit_item( $content, $handle, $src ) {

if ( empty( $content ) )
if ( empty( $content ) ) {
return $content;
}

// Exclude styles with media queries from being included in Minit
$content = $this->exclude_with_media_query( $content, $handle, $src );
Expand All @@ -78,15 +79,16 @@ function minit_item( $content, $handle, $src ) {

private function resolve_urls( $content, $handle, $src ) {

if ( ! $content )
if ( ! $content ) {
return $content;
}

// Make all local asset URLs absolute
$content = preg_replace(
'/url\(["\' ]?+(?!data:|https?:|\/\/)(.*?)["\' ]?\)/i',
sprintf( "url('%s/$1')", $this->handler->base_url . dirname( $src ) ),
$content
);
'/url\(["\' ]?+(?!data:|https?:|\/\/)(.*?)["\' ]?\)/i',
sprintf( "url('%s/$1')", $this->handler->base_url . dirname( $src ) ),
$content
);

return $content;

Expand All @@ -95,15 +97,16 @@ private function resolve_urls( $content, $handle, $src ) {

private function resolve_imports( $content, $handle, $src ) {

if ( ! $content )
if ( ! $content ) {
return $content;
}

// Make all import asset URLs absolute
$content = preg_replace(
'/@import\s+(url\()?["\'](?!https?:|\/\/)(.*?)["\'](\)?)/i',
sprintf( "@import url('%s/$2')", $this->handler->base_url . dirname( $src ) ),
$content
);
'/@import\s+(url\()?["\'](?!https?:|\/\/)(.*?)["\'](\)?)/i',
sprintf( "@import url('%s/$2')", $this->handler->base_url . dirname( $src ) ),
$content
);

return $content;

Expand All @@ -112,14 +115,16 @@ private function resolve_imports( $content, $handle, $src ) {

private function exclude_with_media_query( $content, $handle, $src ) {

if ( ! $content )
if ( ! $content ) {
return $content;
}

$whitelist = array( '', 'all', 'screen' );

// Exclude from Minit if media query specified
if ( ! in_array( $this->handler->registered[ $handle ]->args, $whitelist ) )
if ( ! in_array( $this->handler->registered[ $handle ]->args, $whitelist ) ) {
return false;
}

return $content;

Expand Down
23 changes: 14 additions & 9 deletions include/minit-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public function init() {
function process( $todo ) {

// Run this only in the footer
if ( ! did_action( 'wp_print_footer_scripts' ) )
if ( ! did_action( 'wp_print_footer_scripts' ) ) {
return $todo;
}

// Put back handlers that were excluded from Minit
$todo = array_merge( $todo, $this->queue );
Expand All @@ -59,13 +60,14 @@ function process( $todo ) {

$extra = $this->handler->get_data( $script, 'data' );

if ( ! empty( $extra ) )
if ( ! empty( $extra ) ) {
$inline_js[] = $extra;

}
}

if ( ! empty( $inline_js ) )
if ( ! empty( $inline_js ) ) {
$this->handler->add_data( $handle, 'data', implode( "\n", $inline_js ) );
}

return $todo;

Expand All @@ -90,11 +92,11 @@ public function print_async_scripts() {
// Add this script to our async queue
$async_queue[] = $handle;
}

}

if ( empty( $async_queue ) )
if ( empty( $async_queue ) ) {
return;
}

?>
<!-- Asynchronous scripts by Minit -->
Expand Down Expand Up @@ -128,16 +130,19 @@ public function print_async_scripts() {
public function script_tag_async( $tag, $handle, $src ) {

// Allow others to disable this feature
if ( ! apply_filters( 'minit-script-tag-async', true ) )
if ( ! apply_filters( 'minit-script-tag-async', true ) ) {
return $tag;
}

// Do this for minit scripts only
if ( false === stripos( $handle, 'minit-' ) )
if ( false === stripos( $handle, 'minit-' ) ) {
return $tag;
}

// Bail if async is already set
if ( false !== stripos( $tag, ' async' ) )
if ( false !== stripos( $tag, ' async' ) ) {
return $tag;
}

return str_ireplace( '<script ', '<script async ', $tag );

Expand Down
6 changes: 4 additions & 2 deletions minit.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public static function instance() {

static $instance;

if ( ! $instance )
if ( ! $instance ) {
$instance = new self();
}

return $instance;

Expand All @@ -44,8 +45,9 @@ protected function __construct() {

public function init() {

if ( is_admin() || is_customize_preview() )
if ( is_admin() || is_customize_preview() ) {
return;
}

include dirname( __FILE__ ) . '/include/minit-assets.php';
include dirname( __FILE__ ) . '/include/minit-js.php';
Expand Down

0 comments on commit 6e1e7de

Please sign in to comment.