Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from shinsenter/develop
Browse files Browse the repository at this point in the history
Optimized scriptloader, polyfill, scrset
  • Loading branch information
shinsenter authored Mar 21, 2019
2 parents f211832 + 98263b3 commit 5099340
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 35 deletions.
4 changes: 4 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ Lazy loading content on web page can help reduce resource contention and improve

== Changelog ==

1.0.7 Optimized scriptloader, polyfill, scrset

1.0.6 Small bug fixes (color placeholders, css)

1.0.5 Migrate with defer.js library

1.0.0 ~ 1.0.4 The first implement
Expand Down
2 changes: 1 addition & 1 deletion _dist_/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.6
1.0.7
1 change: 0 additions & 1 deletion _dist_/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ rsync -aHxW --delete --exclude-from=$base_dir/_dist_/blacklist.txt $base_dir/ $p

cd $plugin_dir
mv trunk/defer-wordpress.php $plugin_dir/trunk/$plugin_name.php
sed -nE 's|A performant|Magic|g' $plugin_dir/trunk/$plugin_name.php
svn stat

svn add trunk/* --force
Expand Down
4 changes: 2 additions & 2 deletions admin/class-defer-js-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public function enqueue_scripts()
// wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/defer-js-admin.js', ['jquery'], $this->version, false);
}

public function enable_defer_js()
public function enable_defer_wordpress()
{
ob_start('ob_defer_js');
ob_start('ob_defer_wordpress');
}
}
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 30 additions & 17 deletions defer-wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@
die;
}

/*
* Currently plugin version.
* Rename this for your plugin and update it as you release new versions.
*/
define('DEFER_WORDPRESS_PLUGIN_VERSION', '1.0.7');
define('DEFER_JS_PREFIX', 'shinsenter_deferjs_');

/*
* @wordpress-plugin
* Plugin Name: A performant lazy loader (defer.js)
* Plugin URI: https://github.com/shinsenter/defer-wordpress
* Description: 🔌 A Wordpress plugin integrating my beloved "defer.js" library into your websites. Hope you guys like it.
* Version: 1.0.6
* Version: 1.0.7
* Author: MAI NHUT TAN
* Author URI: https://code.shin.company/
* License: GPL-2.0+
Expand All @@ -31,15 +38,21 @@
*/

/*
* Currently plugin version.
* Start at version 1.0.0 and use SemVer - https://semver.org
* Rename this for your plugin and update it as you release new versions.
* defer.js library version
*/
define('DEFER_JS_VERSION', '1.0.6');
define('DEFER_JS_PREFIX', 'shinsenter_deferjs_');
if (!defined('DEFER_JS_VERSION')) {
define('DEFER_JS_VERSION', 'latest');
}

if (!defined('DEFER_JS_CACHE_SUFFIX')) {
define('DEFER_JS_CACHE_SUFFIX', '_' . DEFER_WORDPRESS_PLUGIN_VERSION);
}

if (!function_exists('ob_defer_js')) {
function ob_defer_js($buffer)
/*
* The main code
*/
if (!function_exists('ob_defer_wordpress')) {
function ob_defer_wordpress($buffer)
{
$optimized = null;

Expand All @@ -62,10 +75,8 @@ function ob_defer_js($buffer)
$defer->enable_defer_scripts = get_option(DEFER_JS_PREFIX . 'enable_defer_scripts', false);
$defer->enable_defer_images = get_option(DEFER_JS_PREFIX . 'enable_defer_images', true);
$defer->enable_defer_iframes = get_option(DEFER_JS_PREFIX . 'enable_defer_iframes', true);
$defer->defer_web_fonts = get_option(DEFER_JS_PREFIX . 'defer_web_fonts', true);

$defer->empty_gif = get_option(DEFER_JS_PREFIX . 'empty_gif', 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');
$defer->empty_src = get_option(DEFER_JS_PREFIX . 'empty_src', 'about:blank');
$defer->defer_web_fonts = get_option(DEFER_JS_PREFIX . 'defer_web_fonts', true);
$defer->use_color_placeholder = get_option(DEFER_JS_PREFIX . 'use_color_placeholder', true);

$optimized = $defer->fromHtml($buffer)->toHtml();
Expand All @@ -82,8 +93,9 @@ function ob_defer_js($buffer)
* The code that runs during plugin activation.
* This action is documented in includes/class-defer-js-activator.php
*/
function activate_defer_js()
function activate_defer_wordpress()
{
require_once __DIR__ . '/vendor/autoload.php';
require_once plugin_dir_path(__FILE__) . 'includes/class-defer-js-activator.php';
Defer_Js_Activator::activate();
}
Expand All @@ -92,14 +104,15 @@ function activate_defer_js()
* The code that runs during plugin deactivation.
* This action is documented in includes/class-defer-js-deactivator.php
*/
function deactivate_defer_js()
function deactivate_defer_wordpress()
{
require_once __DIR__ . '/vendor/autoload.php';
require_once plugin_dir_path(__FILE__) . 'includes/class-defer-js-deactivator.php';
Defer_Js_Deactivator::deactivate();
}

register_activation_hook(__FILE__, 'activate_defer_js');
register_deactivation_hook(__FILE__, 'deactivate_defer_js');
register_activation_hook(__FILE__, 'activate_defer_wordpress');
register_deactivation_hook(__FILE__, 'deactivate_defer_wordpress');

/**
* The core plugin class that is used to define internationalization,
Expand All @@ -116,12 +129,12 @@ function deactivate_defer_js()
*
* @since 1.0.0
*/
function run_defer_js()
function run_defer_wordpress()
{
require_once __DIR__ . '/vendor/autoload.php';

$plugin = new Defer_Js();
$plugin->run();
}

run_defer_js();
run_defer_wordpress();
28 changes: 28 additions & 0 deletions includes/class-defer-js-activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,33 @@ class Defer_Js_Activator
*/
public static function activate()
{
if (class_exists('shinsenter\Defer')) {
// Reset all options when activate the plugin
$defer = new \shinsenter\Defer();

$defer->append_defer_js = false;
$defer->default_defer_time = 50;

$defer->enable_preloading = true;
$defer->enable_dns_prefetch = true;
$defer->fix_render_blocking = true;
$defer->minify_output_html = true;

$defer->enable_defer_css = true;
$defer->enable_defer_scripts = false;
$defer->enable_defer_images = true;
$defer->enable_defer_iframes = true;

$defer->defer_web_fonts = true;
$defer->use_color_placeholder = true;

foreach ($defer->options as $key => $value) {
update_option(DEFER_JS_PREFIX . $key, $value);
}

// Create library cache
$dummy = '<!DOCTYPE html><html><head></head><body></body></html>';
$defer->fromHtml($dummy)->toHtml();
}
}
}
6 changes: 3 additions & 3 deletions includes/class-defer-js-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ public function run()
private function add($hooks, $hook, $component, $callback, $priority, $accepted_args)
{
$hooks[] = [
'hook' => $hook,
'hook' => (string) $hook,
'component' => $component,
'callback' => $callback,
'priority' => $priority,
'accepted_args' => $accepted_args,
'priority' => (int) $priority,
'accepted_args' => (int) $accepted_args,
];

return $hooks;
Expand Down
10 changes: 5 additions & 5 deletions includes/class-defer-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ class Defer_Js
*/
public function __construct()
{
if (defined('DEFER_JS_VERSION')) {
$this->version = DEFER_JS_VERSION;
if (defined('DEFER_WORDPRESS_PLUGIN_VERSION')) {
$this->version = DEFER_WORDPRESS_PLUGIN_VERSION;
} else {
$this->version = '1.0.0';
}

$this->plugin_name = 'defer-js';
$this->plugin_name = 'defer-wordpress';

$this->load_dependencies();
$this->set_locale();
Expand Down Expand Up @@ -191,7 +191,7 @@ private function define_admin_hooks()
// $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts');

// if (!(defined('WP_CLI') && WP_CLI)) {
// $this->loader->add_action('init', $plugin_admin, 'enable_defer_js', 2);
// $this->loader->add_action('init', $plugin_admin, 'enable_defer_wordpress', pi());
// }
}

Expand All @@ -209,7 +209,7 @@ private function define_public_hooks()
// $this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_scripts');

if (!(defined('WP_CLI') && WP_CLI) && !is_admin()) {
$this->loader->add_action('init', $plugin_public, 'enable_defer_js', 2);
$this->loader->add_action('init', $plugin_public, 'enable_defer_wordpress', pi());
}
}
}
4 changes: 2 additions & 2 deletions public/class-defer-js-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public function enqueue_scripts()
// wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/defer-js-public.js', ['jquery'], $this->version, false);
}

public function enable_defer_js()
public function enable_defer_wordpress()
{
ob_start('ob_defer_js');
ob_start('ob_defer_wordpress');
}
}

0 comments on commit 5099340

Please sign in to comment.