From 87cd98938057375b4c474be3b8e98e9eba67d0a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Sun, 28 Aug 2016 18:52:20 +0000 Subject: [PATCH] Make WPCS happy and the code more readable --- include/admin.php | 11 +++++---- include/helpers.php | 18 ++++++++------ include/minit-assets.php | 52 ++++++++++++++++++++++++++-------------- include/minit-css.php | 35 +++++++++++++++------------ include/minit-js.php | 23 +++++++++++------- minit.php | 6 +++-- 6 files changed, 89 insertions(+), 56 deletions(-) diff --git a/include/admin.php b/include/admin.php index 25fbcf1..518a479 100644 --- a/include/admin.php +++ b/include/admin.php @@ -27,10 +27,10 @@ public function init() { function plugin_action_link_cache_bump( $links ) { $links[] = sprintf( - '%s', - wp_nonce_url( add_query_arg( 'purge_minit', true ), 'purge_minit' ), - __( 'Purge cache', 'minit' ) - ); + '%s', + wp_nonce_url( add_query_arg( 'purge_minit', true ), 'purge_minit' ), + __( 'Purge cache', 'minit' ) + ); return $links; @@ -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(); diff --git a/include/helpers.php b/include/helpers.php index 8932eea..04c8e44 100644 --- a/include/helpers.php +++ b/include/helpers.php @@ -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; } @@ -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; @@ -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; diff --git a/include/minit-assets.php b/include/minit-assets.php index a300c49..44f8354 100644 --- a/include/minit-assets.php +++ b/include/minit-assets.php @@ -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; @@ -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 ); @@ -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() ); @@ -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 ) ); @@ -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 ); @@ -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 ); @@ -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 ); @@ -207,13 +219,15 @@ 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 ); @@ -221,14 +235,16 @@ protected function get_asset_relative_path( $handle ) { // 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; diff --git a/include/minit-css.php b/include/minit-css.php index 55a7264..d1f7080 100644 --- a/include/minit-css.php +++ b/include/minit-css.php @@ -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; @@ -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 ); @@ -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; @@ -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; @@ -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; diff --git a/include/minit-js.php b/include/minit-js.php index 842e72a..88b4971 100644 --- a/include/minit-js.php +++ b/include/minit-js.php @@ -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 ); @@ -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; @@ -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; + } ?> @@ -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( '