diff --git a/.gitignore b/.gitignore index 32979eb..7fca32c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -.DS_Store -/svn +.DS_Store +/svn diff --git a/admin.css b/admin.css deleted file mode 100644 index 5199a48..0000000 --- a/admin.css +++ /dev/null @@ -1,16 +0,0 @@ -.fixed .column-count, -.fixed .column-type { - width: 10%; -} - -.fixed .column-compression { - width: 15%; -} -.column-compression meter { - width: 100%; -} - -.fixed .column-last-modified, -.fixed .column-expires { - width: 12%; -} diff --git a/admin.js b/admin.js deleted file mode 100644 index e69de29..0000000 diff --git a/dependency-minification.php b/dependency-minification.php index 1cc347b..23ba076 100644 --- a/dependency-minification.php +++ b/dependency-minification.php @@ -8,58 +8,122 @@ Text Domain: depmin */ - class Dependency_Minification { - static $options = array(); - protected static $minified_count = 0; - static $admin_page_hook; - const DEFAULT_ENDPOINT = '_minify'; + /*** Constants ********************************************************/ + + /** + * The plugin version constant. + * + * @since X + * @var float + */ + const VERSION = '0.9.4'; + const CRON_MINIFY_ACTION = 'minify_dependencies'; const CACHE_KEY_PREFIX = 'depmin_cache_'; const FILENAME_PATTERN = '([^/]+?)\.([0-9a-f]+)(?:\.([0-9a-f]+))?\.(css|js)'; const AJAX_ACTION = 'dependency_minification'; + const AJAX_OPTIONS_ACTION = 'dependency_minification_options'; const ADMIN_PAGE_SLUG = 'dependency-minification'; const ADMIN_PARENT_PAGE = 'tools.php'; - static $query_vars = array( + // @deprecated... + const DEFAULT_ENDPOINT = '_minify'; + + + /*** Properties *******************************************************/ + + /** + * The plugin admin page hook_suffix. + * + * @var string + * @access public + */ + public static $admin_page_hook; + + /** + * The plugin run-time options list. + * + * @var array + * @access public + */ + public static $options = array(); + + /** + * The plugin query vars keys. + * + * @var array + * @access public + */ + public static $query_vars = array( 'depmin_handles', 'depmin_src_hash', 'depmin_ver_hash', 'depmin_file_ext', ); - static function setup() { - self::$options = apply_filters( 'dependency_minification_options', array_merge( - array( - 'endpoint' => self::DEFAULT_ENDPOINT, - 'default_exclude_remote_dependencies' => true, - 'cache_control_max_age_cache' => 2629743, // 1 month in seconds - 'cache_control_max_age_error' => 60 * 60, // 1 hour, to try minifying again - 'allow_not_modified_responses' => true, // only needs to be true if not Akamaized and max-age is short - 'admin_page_capability' => 'edit_theme_options', - 'show_error_messages' => ( defined( 'WP_DEBUG' ) && WP_DEBUG ), - 'disable_if_wp_debug' => true, - ), - self::$options - ) ); + protected static $minified_count = 0; + + protected static $is_footer = array( + 'scripts' => false, + 'styles' => false, + ); + - $is_frontend = ! ( - is_admin() - || - in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) ) - ); - if ( $is_frontend ) { + /*** Functions *******************************************************/ + + /** + * Define the plugin global constants. + * + * @return void + * @since X + */ + public static function define_constants() { + + // Define the plugin directory path constant. + define( 'DEPMIN_DIR', plugin_dir_path( __FILE__ ) ); + + // Define the plugin directory URL constant. + define( 'DEPMIN_URL', plugin_dir_url( __FILE__ ) ); + + } // end define_constants() + + /** + * Load the plugin components files. + * + * @return void + * @since X + */ + public static function load_components() { + + require DEPMIN_DIR . '/inc/common/functions.php'; + + if ( is_admin() ) + require DEPMIN_DIR . '/inc/admin/admin.php'; + + } // end load_functions() + + /** + * Load the plugin files and setup the needed hooks. + * + * @return void + * @since X + */ + public static function setup() { + + // Load the plugin! + self::define_constants(); + self::load_components(); + + add_action( 'init', array( __CLASS__, 'hook_rewrites' ) ); + add_action( self::CRON_MINIFY_ACTION, array( __CLASS__, 'minify' ), 10, 4 ); + + if ( depmin_is_frontend() ) { add_filter( 'print_scripts_array', array( __CLASS__, 'filter_print_scripts_array' ) ); add_filter( 'print_styles_array', array( __CLASS__, 'filter_print_styles_array' ) ); } - add_action( 'init', array( __CLASS__, 'hook_rewrites' ) ); - add_action( self::CRON_MINIFY_ACTION, array( __CLASS__, 'minify' ), 10, 4 ); - add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) ); - add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) ); - add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) ); - add_action( 'wp_ajax_' . self::AJAX_ACTION, array( __CLASS__, 'admin_ajax_handler' ) ); - add_filter( 'plugin_action_links', array( __CLASS__, 'admin_plugin_action_links' ), 10, 2 ); + } static function hook_rewrites() { @@ -69,7 +133,7 @@ static function hook_rewrites() { } static function add_rewrite_rule() { - $regex = sprintf( '^%s/%s', self::$options['endpoint'], self::FILENAME_PATTERN ); + $regex = sprintf( '^%s/%s', depmin_get_option( 'endpoint' ), self::FILENAME_PATTERN ); $redirect = 'index.php?'; for ( $i = 0; $i < count( self::$query_vars ); $i += 1 ) { $redirect .= sprintf( '%s=$matches[%d]&', self::$query_vars[$i], $i + 1 ); @@ -77,11 +141,6 @@ static function add_rewrite_rule() { add_rewrite_rule( $regex, $redirect, 'top' ); } - protected static $is_footer = array( - 'scripts' => false, - 'styles' => false, - ); - /** * register_activation_hook */ @@ -107,362 +166,28 @@ static function filter_query_vars( $query_vars ) { * @filter print_styles_array */ static function filter_print_styles_array( $handles ) { - $handles = self::filter_print_dependency_array( $handles, 'styles' ); - return $handles; + return self::filter_print_dependency_array( $handles, 'styles' ); } /** * @filter print_scripts_array */ static function filter_print_scripts_array( $handles ) { - $handles = self::filter_print_dependency_array( $handles, 'scripts' ); - return $handles; - } - - /** - * @action admin_enqueue_scripts - */ - static function admin_enqueue_scripts( $hook ) { - if ( $hook !== self::$admin_page_hook ) { - return; - } - $meta = get_plugin_data( __FILE__ ); - wp_enqueue_script( 'depmin-admin', plugin_dir_url( __FILE__ ) . 'admin.js', array( 'jquery' ), $meta['Version'] ); - wp_enqueue_style( 'depmin-admin', plugin_dir_url( __FILE__ ) . 'admin.css', array(), $meta['Version'] ); - } - - /** - * @action admin_menu - */ - static function admin_menu() { - self::$admin_page_hook = add_submenu_page( - self::ADMIN_PARENT_PAGE, - __( 'Dependency Minification', 'depmin' ), - __( 'Dep. Minification', 'depmin' ), - self::$options['admin_page_capability'], - self::ADMIN_PAGE_SLUG, - array( __CLASS__, 'admin_page' ) - ); + return self::filter_print_dependency_array( $handles, 'scripts' ); } - /** - * @action admin_notices - */ - static function admin_notices() { - // Show a notice to notify user that pretty urls is disabled, hence the plugin won't work - if ( empty( $GLOBALS['wp_rewrite']->permalink_structure ) ) { - ?> -
-

%1$s: %2$s', - __( 'Dependency Minification', 'depmin' ), - sprintf( - __( 'Pretty permalinks are not enabled in your %1$s, which is required for this plugin to operate. Select something other than Default (e.g. ?p=123)', 'depmin' ), - sprintf( - '%2$s', - admin_url( 'options-permalink.php' ), - __( 'Permalinks Settings', 'depmin' ) - ) - ) - ); - ?>

-
- id !== self::$admin_page_hook ) { - return; - } - if ( empty( $_GET['updated-count'] ) ) { - return; - } - if ( empty( $_GET['updated-action'] ) ) { - return; - } - - $updated_count = intval( $_REQUEST['updated-count'] ); - $updated_task = sanitize_title( $_REQUEST['updated-action'] ); - ?> -
- -

- -

- -

- -
- %s', esc_url( $admin_page_url ), esc_html__( 'Settings', 'depmin' ) ); - array_push( $links, $admin_page_link ); - } - return $links; - } - - static function admin_page() { - if ( ! current_user_can( self::$options['admin_page_capability'] ) ) { - wp_die( __( 'You cannot access this page.', 'depmin' ) ); - } - $nonce = wp_create_nonce( self::AJAX_ACTION ); - ?> -
-

-

- - -
- - - - prepare( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s", self::CACHE_KEY_PREFIX . '%' ); - $option_names = $wpdb->get_col( $sql ); - $minified_dependencies = array(); - foreach ( $option_names as $option_name ) { - $minified_dependencies[$option_name] = get_option($option_name); - } - $minified_dependencies = array_filter( $minified_dependencies ); - - $minify_crons = array(); - foreach ( _get_cron_array() as $timestamp => $cron ) { - if ( isset( $cron[self::CRON_MINIFY_ACTION] ) ) { - foreach ( $cron[self::CRON_MINIFY_ACTION] as $key => $min_cron ) { - $cached = $min_cron['args'][0]; - $src_hash = self::hash_array( wp_list_pluck( $cached['deps'], 'src' ) ); - $cache_option_name = self::get_cache_option_name( $src_hash ); - if ( array_key_exists( $cache_option_name, $minified_dependencies ) ) { - $minified_dependencies[$cache_option_name] = array_merge( - $minified_dependencies[$cache_option_name], - $cached - ); - } else { - $minified_dependencies[$cache_option_name] = $cached; - } - } - } - } - ?> - - -
-

-
- - - -

- -

- -
-
- - -
-
-
- - - $tcontainer ) : ?> - <> - - - - - - - - - - > - - - - $minified_dependency ) : ?> - $deps, $type, $pending, $scheduled, $error - $handles = wp_list_pluck( $deps, 'handle' ); - $minified_src = self::get_dependency_minified_url( $deps, $type ); - $link_params = array( - '_wpnonce' => $nonce, - 'action' => self::AJAX_ACTION, - 'depmin_option_name[]' => $option_name, - ); - ?> - - - - - - - $last_modified, - 'expires' => $expires, - ); - ?> - $time ) : ?> - - - - - -
- -
- - > - - - - - - - -
-

- - - - - - - -

-
- - -
- - - - | - - - - | - - - - - - - - - - -
- -
', $percentage, esc_attr( sprintf( __( '(%1$d / %2$d)', 'depmin' ), $min, $max ) ) ); - print esc_html( sprintf( __( '%1$d%%', 'depmin' ), $percentage ) ); - print ''; - } - - ?> - - - - - -
- -
-
- permalink_structure ); if ( $disabled ) { return $handles; } @@ -582,7 +305,7 @@ static function filter_print_dependency_array( array $handles, $type ) { ); if ( $is_error ) { - if ( self::$options['show_error_messages'] ) { + if ( depmin_get_option( 'show_error_messages' ) ) { print "\n