initial commit
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
if (!defined('WPO_VERSION')) die('No direct access allowed');
|
||||
|
||||
require_once WPO_PLUGIN_MAIN_PATH . 'includes/class-wp-optimize-htaccess.php';
|
||||
|
||||
if (!class_exists('WP_Optimize_WebP')) :
|
||||
|
||||
class WP_Optimize_WebP {
|
||||
|
||||
private $_htaccess = null;
|
||||
|
||||
private $_server_info_instance;
|
||||
|
||||
private $_rewrite_status = false;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->set_converter_status();
|
||||
$this->set_server_info_instance();
|
||||
$this->set_rewrite_status();
|
||||
$this->set_webp_serve_method();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns singleton instance
|
||||
*
|
||||
* @return WP_Optimize_WebP
|
||||
*/
|
||||
public static function get_instance() {
|
||||
static $instance = null;
|
||||
if (null === $instance) {
|
||||
$instance = new WP_Optimize_WebP();
|
||||
}
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Run and find converter status
|
||||
*/
|
||||
public function set_converter_status() {
|
||||
include_once WPO_PLUGIN_MAIN_PATH . 'webp/class-wpo-webp-test-run.php';
|
||||
$converters = WP_Optimize()->get_options()->get_option('webp_converters', false);
|
||||
|
||||
if (empty($converters)) {
|
||||
$converter_status = WPO_WebP_Test_Run::get_converter_status();
|
||||
WP_Optimize()->get_options()->update_option('webp_converters', $converter_status['working_converters']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets server info instance variable
|
||||
*/
|
||||
private function set_server_info_instance() {
|
||||
if (!class_exists('WPO_Server_Info')) {
|
||||
require_once(WPO_PLUGIN_MAIN_PATH . 'webp/class-wpo-server-info.php');
|
||||
}
|
||||
$this->_server_info_instance = WPO_Server_Info::get_instance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets server's rewrite status
|
||||
*/
|
||||
private function set_rewrite_status() {
|
||||
$this->_rewrite_status = $this->_server_info_instance->get_rewrite_status();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets how webp images are served
|
||||
*/
|
||||
private function set_webp_serve_method() {
|
||||
if (true === $this->_rewrite_status) {
|
||||
$this->use_htaccess();
|
||||
} else {
|
||||
$this->use_alter_html();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup .htaccess method
|
||||
*/
|
||||
private function use_htaccess() {
|
||||
$options = Updraft_Smush_Manager()->get_smush_options();
|
||||
$webp_conversion_enabled = isset($options['webp_conversion']) ? $options['webp_conversion'] : false;
|
||||
$this->save_htaccess_rules($webp_conversion_enabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup alter html method
|
||||
*/
|
||||
private function use_alter_html() {
|
||||
// To be continued in another part
|
||||
// add_action('template_direct', array(''));
|
||||
}
|
||||
|
||||
/**
|
||||
* Save .htaccess rules
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function save_htaccess_rules($webp_conversion_enabled = false) {
|
||||
$wp_uploads = wp_get_upload_dir();
|
||||
$htaccess_file = $wp_uploads['basedir'] . '/.htaccess';
|
||||
if (!file_exists($htaccess_file)) {
|
||||
file_put_contents($htaccess_file, '');
|
||||
}
|
||||
$htaccess_comment_section = 'WP-Optimize WebP Rules';
|
||||
$this->_htaccess = new WP_Optimize_Htaccess($htaccess_file);
|
||||
|
||||
if ($this->_htaccess->is_exists() && !$webp_conversion_enabled) {
|
||||
$this->_htaccess->remove_commented_section($htaccess_comment_section);
|
||||
$this->_htaccess->write_file();
|
||||
return true;
|
||||
}
|
||||
if ($this->_htaccess->is_commented_section_exists($htaccess_comment_section)) return false;
|
||||
$this->_htaccess->update_commented_section($this->prepare_webp_htaccess_rules(), $htaccess_comment_section);
|
||||
$this->_htaccess->write_file();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare array of htaccess rules to use webp images.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function prepare_webp_htaccess_rules() {
|
||||
return array(
|
||||
array(
|
||||
'<IfModule mod_rewrite.c>',
|
||||
'RewriteEngine On',
|
||||
'',
|
||||
'# Redirect to existing converted image in same dir (if browser supports webp)',
|
||||
'RewriteCond %{HTTP_ACCEPT} image/webp',
|
||||
'RewriteCond %{REQUEST_FILENAME} (?i)(.*)(\.jpe?g|\.png)$',
|
||||
'RewriteCond %1%2\.webp -f',
|
||||
'RewriteRule (?i)(.*)(\.jpe?g|\.png)$ %1%2\.webp [T=image/webp,E=EXISTING:1,E=ADDVARY:1,L]',
|
||||
'',
|
||||
'# Make sure that browsers which does not support webp also gets the Vary:Accept header',
|
||||
'# when requesting images that would be redirected to webp on browsers that does.',
|
||||
array(
|
||||
'<IfModule mod_headers.c>',
|
||||
array(
|
||||
'<FilesMatch "(?i)\.(jpe?g|png)$">',
|
||||
'Header append "Vary" "Accept"',
|
||||
'</FilesMatch>',
|
||||
),
|
||||
'</IfModule>',
|
||||
),
|
||||
'',
|
||||
'</IfModule>',
|
||||
'',
|
||||
),
|
||||
array(
|
||||
'# Rules for handling requests for webp images',
|
||||
'# ---------------------------------------------',
|
||||
'',
|
||||
'# Set Vary:Accept header if we came here by way of our redirect, which set the ADDVARY environment variable',
|
||||
'# The purpose is to make proxies and CDNs aware that the response varies with the Accept header',
|
||||
'<IfModule mod_headers.c>',
|
||||
array(
|
||||
'<IfModule mod_setenvif.c>',
|
||||
'# Apache appends "REDIRECT_" in front of the environment variables defined in mod_rewrite, but LiteSpeed does not',
|
||||
'# So, the next lines are for Apache, in order to set environment variables without "REDIRECT_"',
|
||||
'SetEnvIf REDIRECT_EXISTING 1 EXISTING=1',
|
||||
'SetEnvIf REDIRECT_ADDVARY 1 ADDVARY=1',
|
||||
'',
|
||||
'Header append "Vary" "Accept" env=ADDVARY',
|
||||
'',
|
||||
'# Set X-WPO-WebP header for diagnose purposes',
|
||||
'Header set "X-WPO-WebP" "Redirected directly to existing webp" env=EXISTING',
|
||||
'</IfModule>',
|
||||
),
|
||||
'</IfModule>',
|
||||
),
|
||||
array(
|
||||
'# Register webp mime type',
|
||||
'<IfModule mod_mime.c>',
|
||||
'AddType image/webp .webp',
|
||||
'</IfModule>',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
endif;
|
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
if (!defined('WPO_PLUGIN_MAIN_PATH')) die('No direct access allowed');
|
||||
|
||||
require_once(WPO_PLUGIN_MAIN_PATH . 'vendor/autoload.php');
|
||||
use HtaccessCapabilityTester\HtaccessCapabilityTester;
|
||||
|
||||
class WPO_Htaccess_Capabilities {
|
||||
|
||||
private static $_instance = null;
|
||||
|
||||
/**
|
||||
* Tests and sets up server's htacess capabilities as properties
|
||||
*/
|
||||
public function __construct() {
|
||||
$uploads = wp_upload_dir();
|
||||
$this->hct = new HtaccessCapabilityTester($uploads['basedir'] . '/wpo/', $uploads['baseurl'] . '/wpo/');
|
||||
$this->htaccess_enabled = $this->hct->htaccessEnabled();
|
||||
$this->mod_headers = $this->get_mod_header_status();
|
||||
$this->mod_mime = $this->get_mod_mime_status();
|
||||
$this->mod_rewrite = $this->get_mod_rewrite_status();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets singleton instance
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public static function get_instance() {
|
||||
if (null === self::$_instance) {
|
||||
self::$_instance = new WPO_Htaccess_Capabilities();
|
||||
}
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets `mod_headers` status by checking if it is loaded and working
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function get_mod_header_status() {
|
||||
return (true === $this->hct->moduleLoaded('headers') && true === $this->hct->headerSetWorks());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets `mod_mime` status by checking if it is loaded and working
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function get_mod_mime_status() {
|
||||
return (true === $this->hct->moduleLoaded('mime') && true === $this->hct->addTypeWorks());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets `mod_rewrite` status by checking if it is loaded and working
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function get_mod_rewrite_status() {
|
||||
return (true === $this->hct->moduleLoaded('rewrite') && true == $this->hct->rewriteWorks());
|
||||
}
|
||||
}
|
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
if (!defined('WPO_PLUGIN_MAIN_PATH')) die('No direct access allowed');
|
||||
|
||||
class WPO_Server_Info {
|
||||
|
||||
private static $_instance = null;
|
||||
|
||||
/**
|
||||
* @var $_server_name Web server engine name
|
||||
*/
|
||||
private $_server_name;
|
||||
|
||||
/**
|
||||
* @var $_rewrite_status Web server's URL rewrite ability
|
||||
*/
|
||||
private $_rewrite_status;
|
||||
|
||||
/**
|
||||
* Setup server information
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->set_server_name();
|
||||
$this->set_rewrite_status();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets singleton instance
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public static function get_instance() {
|
||||
if (null === self::$_instance) {
|
||||
self::$_instance = new self();
|
||||
}
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect and set web server names
|
||||
*/
|
||||
private function set_server_name() {
|
||||
global $is_apache, $is_nginx, $is_iis7, $is_IIS;
|
||||
|
||||
if ($is_apache) {
|
||||
$this->_server_name = 'apache';
|
||||
}
|
||||
|
||||
if ($is_iis7) {
|
||||
$this->_server_name = 'iis7';
|
||||
}
|
||||
|
||||
if ($is_IIS) {
|
||||
$this->_server_name = 'iis';
|
||||
}
|
||||
|
||||
if ($is_nginx) {
|
||||
$this->_server_name = 'nginx';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets web server's rewrite ability
|
||||
*/
|
||||
private function set_rewrite_status() {
|
||||
if ('apache' === $this->_server_name) {
|
||||
$this->test_htaccess_capabilities();
|
||||
} else {
|
||||
$this->_rewrite_status = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Apache/LiteSpeed server's htaccess capabilities, and sets status and info
|
||||
*/
|
||||
private function test_htaccess_capabilities() {
|
||||
if (!class_exists('WPO_Htaccess_Capabilities')) {
|
||||
include_once WPO_PLUGIN_MAIN_PATH . 'webp/class-wpo-htaccess-capabilities.php';
|
||||
}
|
||||
$htc = WPO_Htaccess_Capabilities::get_instance();
|
||||
if ($htc->htaccess_enabled && $htc->mod_rewrite && $htc->mod_headers && $htc->mod_mime) {
|
||||
$this->_rewrite_status = true;
|
||||
} elseif ($htc->htaccess_enabled) {
|
||||
$this->_rewrite_status = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for web server name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_server_name() {
|
||||
return $this->_server_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for web server's rewrite capability
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function get_rewrite_status() {
|
||||
return $this->_rewrite_status;
|
||||
}
|
||||
}
|
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
if (!defined('WPO_VERSION')) die('No direct access allowed');
|
||||
use \WebPConvert\Convert\ConverterFactory;
|
||||
|
||||
require_once(WPO_PLUGIN_MAIN_PATH . 'vendor/autoload.php');
|
||||
require_once(WPO_PLUGIN_MAIN_PATH . 'webp/class-wpo-webp-test-run.php');
|
||||
|
||||
if (!class_exists('WPO_WebP_Convert')) :
|
||||
|
||||
class WPO_WebP_Convert {
|
||||
|
||||
public $converters = null;
|
||||
|
||||
public function __construct() {
|
||||
$this->converters = WP_Optimize()->get_options()->get_option('webp_converters');
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts uploaded image to webp format
|
||||
*
|
||||
* @param string $source - path of the source file
|
||||
*/
|
||||
public function convert($source) {
|
||||
if (count($this->converters) < 1) return false;
|
||||
|
||||
$destination = $this->get_destination_path($source);
|
||||
$this->check_converters_and_do_conversion($source, $destination);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the destination full path
|
||||
*
|
||||
* @param string $source - path of the source file
|
||||
*
|
||||
* @return string $destination - path of destination file
|
||||
*/
|
||||
protected function get_destination_path($source) {
|
||||
$path_parts = pathinfo($source);
|
||||
$destination = $path_parts['dirname'] . '/'. basename($source) . '.webp';
|
||||
return $destination;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loop through available converters and do the conversion
|
||||
*
|
||||
* @param string $source - path of source file
|
||||
* @param string $destination - path of destination file
|
||||
*/
|
||||
protected function check_converters_and_do_conversion($source, $destination) {
|
||||
foreach ($this->converters as $converter) {
|
||||
$converter_instance = ConverterFactory::makeConverter(
|
||||
$converter,
|
||||
$source,
|
||||
$destination
|
||||
);
|
||||
$converter_instance->doConvert();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
endif;
|
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
if (!defined('WPO_VERSION')) die('No direct access allowed');
|
||||
|
||||
use \WebPConvert\Convert\ConverterFactory;
|
||||
|
||||
require_once WPO_PLUGIN_MAIN_PATH . 'vendor/autoload.php';
|
||||
|
||||
if (!class_exists('WPO_WebP_Test_Run')) :
|
||||
/**
|
||||
* Test run
|
||||
*/
|
||||
class WPO_WebP_Test_Run {
|
||||
|
||||
/**
|
||||
* Get a test result object OR false, if tests cannot be made.
|
||||
*
|
||||
* @return object|false
|
||||
*/
|
||||
public static function get_converter_status() {
|
||||
$source = WPO_PLUGIN_MAIN_PATH . 'images/logo/wpo_logo_small.png';
|
||||
$upload_dir = wp_upload_dir();
|
||||
$destination = $upload_dir['basedir']. '/wpo/images/wpo_logo_small.webp';
|
||||
|
||||
$converters = array(
|
||||
// 'cwebp',
|
||||
'vips',
|
||||
'imagemagick',
|
||||
'graphicsmagick',
|
||||
'ffmpeg',
|
||||
'wpc',
|
||||
'ewww',
|
||||
'imagick',
|
||||
'gmagick',
|
||||
'gd',
|
||||
);
|
||||
$working_converters = array();
|
||||
$errors = array();
|
||||
|
||||
foreach ($converters as $converter) {
|
||||
$converter_id = $converter;
|
||||
try {
|
||||
$converter_instance = ConverterFactory::makeConverter(
|
||||
$converter_id,
|
||||
$source,
|
||||
$destination
|
||||
);
|
||||
$converter_instance->doConvert();
|
||||
$working_converters[] = $converter_id;
|
||||
} catch (\Exception $e) {
|
||||
$errors[$converter_id] = $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'working_converters' => $working_converters,
|
||||
'errors' => $errors,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
endif;
|
Reference in New Issue
Block a user