<?php
class WpFastestCacheImageOptimisation{
public $uploadDir;
private $id = false;
private $metadata = array();
private $name = "";
private $path = "";
private $url = "";
private $images = array();
private $images_clone = array();
private $location = "";
public function __construct(){
if(isset($_GET["id"]) && $_GET["id"]){
$this->id = intval($_GET["id"]);
}
$this->create_api_key();
$this->uploadDir = wp_upload_dir();
}
public function create_api_key(){
if(!get_option("WpFc_api_key")){
update_option("WpFc_api_key", md5(microtime(true)));
}
}
public function get_template_path($file_name){
return WPFC_WP_PLUGIN_DIR."/wp-fastest-cache-premium/pro/templates/".$file_name;
}
public function get_total_reduction_image_number(){
global $wpdb;
$query = "SELECT sum(`meta_value`) as total FROM `".$wpdb->prefix."postmeta` WHERE `meta_key`= 'wpfc_optimisation_reduction'";
$result = $wpdb->get_row( $query );
if($result->total){
return ($result->total && $result->total > 0) ? $result->total : 0;
}else{
return 0;
}
}
public function get_total_optimized_image_number(){
$query_images_args = array();
$query_images_args['post_type'] = 'attachment';
$query_images_args['post_mime_type'] = array('image/jpeg', 'image/png');
$query_images_args['post_status'] = 'inherit';
$query_images_args['posts_per_page'] = -1;
$query_images_args['meta_query'] = array(
array(
'key' => 'wpfc_optimisation',
'compare' => 'EXISTS'
),
array(
'key' => 'wpfc_optimisation',
'value' => base64_encode('"destination_path"'),
'compare' => 'LIKE'
)
);
return $this->get_count_only($query_images_args);
}
public function get_total_image_error_number(){
$query_images_args = array();
$query_images_args['post_type'] = 'attachment';
$query_images_args['post_mime_type'] = array('image/jpeg', 'image/png');
$query_images_args['post_status'] = 'inherit';
$query_images_args['posts_per_page'] = -1;
$query_images_args['meta_query'] = array(
array(
'key' => 'wpfc_optimisation',
'compare' => 'EXISTS'
),
array(
'key' => 'wpfc_optimisation',
'value' => base64_encode('"error_code"'),
'compare' => 'LIKE'
)
);
return $this->get_count_only($query_images_args);
}
public function null_posts_groupby(){
return "";
}
public function count_posts_fields(){
return "COUNT(*) as post_count_wpfc";
}
public function get_count_only($query_images_args){
add_filter( 'posts_fields', array($this,'count_posts_fields'));
add_filter( 'posts_groupby', array($this, 'null_posts_groupby'));
unset($query_images_args["offset"]);
unset($query_images_args["order"]);
unset($query_images_args["orderby"]);
$query_images_args['posts_per_page'] = -1;
$query_image = new WP_Query( $query_images_args );
return $query_image->posts[0]->post_count_wpfc;
}
public function get_total_image_number(){
$query_images_args = array();
$query_images_args['post_type'] = 'attachment';
$query_images_args['post_mime_type'] = array('image/jpeg', 'image/png');
$query_images_args['post_status'] = 'inherit';
$query_images_args['posts_per_page'] = -1;
$query_images_args['meta_query'] = array(
array(
'key' => '_wp_attachment_metadata',
'compare' => 'EXISTS'
)
);
return $this->get_count_only($query_images_args);
}
public function get_total_pending_image_number(){
$query_images_args = array();
$query_images_args['post_type'] = 'attachment';
$query_images_args['post_mime_type'] = array('image/jpeg', 'image/png');
$query_images_args['post_status'] = 'inherit';
$query_images_args['posts_per_page'] = -1;
$query_images_args['meta_query'] = array(
array(
'key' => '_wp_attachment_metadata',
'compare' => 'EXISTS'
),
array(
'key' => 'wpfc_optimisation',
'compare' => 'NOT EXISTS'
)
);
return $this->get_count_only($query_images_args);
}
public function hook(){
add_action('wp_ajax_wpfc_revert_image_ajax_request', array($this, "wpfc_revert_image_ajax_request"));
add_action('wp_ajax_wpfc_statics_ajax_request', array($this, "wpfc_statics_ajax_request"));
add_action('wp_ajax_wpfc_optimize_image_ajax_request', array($this, "wpfc_optimize_image_ajax_request"));
add_action('wp_ajax_wpfc_update_image_list_ajax_request', array($this, "wpfc_update_image_list_ajax_request"));
}
public function get_not_optimized_before(){
$tmp_image = array();
$valueJson = get_post_meta($this->id, 'wpfc_optimisation', true);
$tmpvalueJson = base64_decode($valueJson);
if($tmpvalueJson){
$std = json_decode($tmpvalueJson);
$metaOptimized = $this->object_to_array($std);
$percentage = count($metaOptimized)*100/count($this->images);
}else{
$percentage = 100/count($this->images);
return array("metaOptimized" => array(), "images" => array_slice($this->images, 0, 1), "total_reduction" => 0, "percentage" => $percentage);
}
foreach ($this->images as $key => $value) {
$exist = false;
foreach ($metaOptimized as $meta_key => $meta_value) {
if($value["file"] == $meta_value["file"]){
$exist = true;
//break;
}
}
if(!$exist){
array_push($tmp_image, $value);
}
}
//START: total reduction
$total_reduction = 0;
foreach ($metaOptimized as $m_key => $m_value) {
$m_value["reduction"] = isset($m_value["reduction"]) ? $m_value["reduction"] : 0;
$total_reduction += $m_value["reduction"];
}
//END: total reduction
if(count($tmp_image) > 0){
if(isset($_GET["last"])){
if(preg_match("/last-(\d+)/", $_GET["last"], $last_number)){
if(count($tmp_image) > 5){
$tmp_image = array_slice($tmp_image, $last_number[1]*-1, 1);
}
}
}
return array("metaOptimized" => $metaOptimized, "images" => array_slice($tmp_image, 0, 1), "total_reduction" => $total_reduction, "percentage" => $percentage);
}else{
return array("metaOptimized" => array(), "images" => array(), "total_reduction" => 0);
}
}
public function object_to_array($obj) {
if(is_object($obj)) $obj = (array) $obj;
if(is_array($obj)) {
$new = array();
foreach($obj as $key => $val) {
$new[$key] = $this->object_to_array($val);
}
}
else $new = $obj;
return $new;
}
public function re_order_by_dimensions(){
$tmp = $this->images;
foreach ($tmp as $key => $value) {
$width_list[$key] = $value['width'];
}
array_multisort($width_list, SORT_DESC, $tmp);
return $tmp;
}
public function optimizeFirstImage(){
$this->setId();
$this->setMetaData();
$this->set_server_location();
if(!$this->id){
return array("finish", "success");
}else if(!isset($this->metadata["file"]) && $this->id){
$metaOptimized = array();
$metaOptimized[0]["time"] = time();
$metaOptimized[0]["id"] = $this->id;
$metaOptimized[0]["error_code"] = 17;
update_post_meta($this->id, 'wpfc_optimisation_reduction', 0);
update_post_meta($this->id, 'wpfc_optimisation', base64_encode(json_encode($metaOptimized)));
return array("Image has been optimizedxx", "success", $this->id, 100);
}else{
$this->setName();
$this->setPath();
$this->setUrl();
$this->setImages();
}
if($this->id && count($this->images) == 0){
$metaOptimized = array();
$metaOptimized[0]["time"] = time();
$metaOptimized[0]["id"] = $this->id;
$metaOptimized[0]["error_code"] = 18;
update_post_meta($this->id, 'wpfc_optimisation_reduction', 0);
update_post_meta($this->id, 'wpfc_optimisatio
【WordPress插件】2022年最新版完整功能demo+插件v1.6.2.zip
版权申诉
126 浏览量
2022-04-08
14:40:51
上传
评论
收藏 73KB ZIP 举报
Lee达森
- 粉丝: 968
- 资源: 1万+
最新资源
- 基于Javascript的影视动画设计源码 - cad
- 基于Java和深度学习的瓦斯浓度预测系统后端设计源码 - 瓦斯浓度预测后端
- Screenshot_20240528_103010.jpg
- 基于Python的新能源承载力计算及界面设计源码 - HAINING-DG
- 基于Java的本科探索学习项目设计源码 - 本科探索
- 基于Javascript和Python的微商城项目设计源码 - MicroMall
- 基于Java的网上订餐系统设计源码 - online ordering system
- 基于Javascript的超级美眉网络资源管理应用模块设计源码
- 基于Typescript和PHP的编程知识储备库设计源码 - study-php
- Screenshot_2024-05-28-11-40-58-177_com.tencent.mm.jpg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈