| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plus WebP |
| 4 |
* |
| 5 |
* @package Plus WebP |
| 6 |
* @subpackage PlusWebp Main function |
| 7 |
/* Copyright (c) 2019- Katsushi Kawamori (email : dodesyoswift312@gmail.com) |
| 8 |
This program is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; version 2 of the License. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with this program; if not, write to the Free Software |
| 19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
$pluswebp = new PlusWebp(); |
| 23 |
|
| 24 |
/** ================================================== |
| 25 |
* Class Main function |
| 26 |
* |
| 27 |
* @since 1.00 |
| 28 |
*/ |
| 29 |
class PlusWebp { |
| 30 |
|
| 31 |
/** ================================================== |
| 32 |
* Dir |
| 33 |
* |
| 34 |
* @var $upload_dir DIR. |
| 35 |
*/ |
| 36 |
private $upload_dir; |
| 37 |
|
| 38 |
/** ================================================== |
| 39 |
* URL |
| 40 |
* |
| 41 |
* @var $upload_url URL. |
| 42 |
*/ |
| 43 |
private $upload_url; |
| 44 |
|
| 45 |
/** ================================================== |
| 46 |
* Path |
| 47 |
* |
| 48 |
* @var $upload_path PATH. |
| 49 |
*/ |
| 50 |
private $upload_path; |
| 51 |
|
| 52 |
/** ================================================== |
| 53 |
* Construct |
| 54 |
* |
| 55 |
* @since 1.00 |
| 56 |
*/ |
| 57 |
public function __construct() { |
| 58 |
|
| 59 |
list( $this->upload_dir, $this->upload_url, $this->upload_path ) = $this->upload_dir_url_path(); |
| 60 |
|
| 61 |
add_filter( 'wp_generate_attachment_metadata', array( $this, 'generate_webp' ), 10, 2 ); |
| 62 |
add_action( 'pluswebp_mail_send', array( $this, 'mail_send' ), 10, 1 ); |
| 63 |
|
| 64 |
/* Ajax */ |
| 65 |
$action1 = 'bulkgeneratewebp-ajax-action'; |
| 66 |
$action2 = 'bulkgeneratewebp_message'; |
| 67 |
add_action( 'wp_ajax_' . $action1, array( $this, 'bulkgeneratewebp_update_callback' ) ); |
| 68 |
add_action( 'wp_ajax_' . $action2, array( $this, 'bulkgeneratewebp_message_callback' ) ); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
/** ================================================== |
| 73 |
* Webp generate |
| 74 |
* |
| 75 |
* @param array $metadata metadata. |
| 76 |
* @param int $attachment_id ID. |
| 77 |
* @return array $metadata metadata. |
| 78 |
* @since 1.00 |
| 79 |
*/ |
| 80 |
public function generate_webp( $metadata, $attachment_id ) { |
| 81 |
|
| 82 |
$pluswebp_settings = get_option( 'pluswebp' ); |
| 83 |
$replace = $pluswebp_settings['replace']; |
| 84 |
|
| 85 |
$mime_type = get_post_mime_type( $attachment_id ); |
| 86 |
if ( in_array( $mime_type, $pluswebp_settings['types'] ) ) { |
| 87 |
$metadata_webp = $metadata; |
| 88 |
$file_webp = $this->change_ext( $metadata['file'], 'webp', $pluswebp_settings['addext'] ); |
| 89 |
$metadata_webp['file'] = $file_webp; |
| 90 |
foreach ( (array) $metadata['sizes'] as $key => $value ) { |
| 91 |
$file_thumb = $value['file']; |
| 92 |
$file_thumb_webp = $this->change_ext( $file_thumb, 'webp', $pluswebp_settings['addext'] ); |
| 93 |
if ( '.' === dirname( $file_webp ) ) { |
| 94 |
$dir_name_url = '/'; |
| 95 |
$dir_name_path = wp_normalize_path( '/' ); |
| 96 |
} else { |
| 97 |
$dir_name_url = '/' . dirname( $file_webp ) . '/'; |
| 98 |
$dir_name_path = wp_normalize_path( $dir_name_url ); |
| 99 |
} |
| 100 |
$url = $this->upload_url . $dir_name_url; |
| 101 |
$path = $this->upload_dir . $dir_name_path; |
| 102 |
$ret = $this->create_webp( $path . $file_thumb, $mime_type, $path . $file_thumb_webp ); |
| 103 |
if ( $ret ) { |
| 104 |
$metadata_webp['sizes'][ $key ]['file'] = $file_thumb_webp; |
| 105 |
$metadata_webp['sizes'][ $key ]['mime-type'] = 'image/webp'; |
| 106 |
if ( $replace ) { |
| 107 |
unlink( $path . $file_thumb ); |
| 108 |
$this->change_db( $url . $file_thumb, $url . $file_thumb_webp ); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
if ( ! empty( $metadata['original_image'] ) ) { |
| 113 |
$org_img_file = wp_get_original_image_path( $attachment_id, false ); |
| 114 |
$org_webp_file = $this->change_ext( $org_img_file, 'webp', $pluswebp_settings['addext'] ); |
| 115 |
$ret = $this->create_webp( $org_img_file, $mime_type, $org_webp_file ); |
| 116 |
if ( $ret ) { |
| 117 |
$metadata_webp['original_image'] = wp_basename( $org_webp_file ); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
$ret = $this->create_webp( $this->upload_dir . '/' . $metadata['file'], $mime_type, $this->upload_dir . '/' . $file_webp ); |
| 122 |
if ( $ret ) { |
| 123 |
if ( $replace ) { |
| 124 |
$up_post = array( |
| 125 |
'ID' => $attachment_id, |
| 126 |
'guid' => $this->upload_url . '/' . $file_webp, |
| 127 |
'post_mime_type' => 'image/webp', |
| 128 |
); |
| 129 |
wp_update_post( $up_post ); |
| 130 |
update_post_meta( $attachment_id, '_wp_attached_file', $file_webp ); |
| 131 |
/* for bulk generate */ |
| 132 |
update_post_meta( $attachment_id, '_wp_attachment_metadata', $metadata_webp ); |
| 133 |
/* delete org file */ |
| 134 |
unlink( $this->upload_dir . '/' . $metadata['file'] ); |
| 135 |
/* Replace */ |
| 136 |
$this->change_db( $this->upload_url . '/' . $metadata['file'], $this->upload_url . '/' . $file_webp ); |
| 137 |
/* for hook */ |
| 138 |
$metadata = $metadata_webp; |
| 139 |
/* for mail */ |
| 140 |
$attach_id = $attachment_id; |
| 141 |
} else { |
| 142 |
$post = get_post( $attachment_id ); |
| 143 |
$title = get_the_title( $post ); |
| 144 |
$attachment = array( |
| 145 |
'guid' => $this->upload_url . '/' . $file_webp, |
| 146 |
'post_mime_type' => 'image/webp', |
| 147 |
'post_title' => $title, |
| 148 |
'post_content' => '', |
| 149 |
'post_status' => 'inherit', |
| 150 |
); |
| 151 |
$attach_id = wp_insert_attachment( $attachment, $this->upload_dir . '/' . $file_webp ); |
| 152 |
wp_update_attachment_metadata( $attach_id, $metadata_webp ); |
| 153 |
|
| 154 |
$author = get_userdata( $post->post_author ); |
| 155 |
$userid = $author->ID; |
| 156 |
$postdate = get_the_date( 'Y-m-d H:i:s', $attachment_id ); |
| 157 |
$postdategmt = get_gmt_from_date( $postdate ); |
| 158 |
$up_post = array( |
| 159 |
'ID' => $attach_id, |
| 160 |
'post_author' => $userid, |
| 161 |
'post_date' => $postdate, |
| 162 |
'post_date_gmt' => $postdategmt, |
| 163 |
'post_modified' => $postdate, |
| 164 |
'post_modified_gmt' => $postdategmt, |
| 165 |
); |
| 166 |
wp_update_post( $up_post ); |
| 167 |
} |
| 168 |
|
| 169 |
$ids = get_option( 'pluswebp_generate' ); |
| 170 |
$ids[] = $attach_id; |
| 171 |
update_option( 'pluswebp_generate', $ids ); |
| 172 |
|
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
return $metadata; |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
/** ================================================== |
| 181 |
* IDs Callback |
| 182 |
* |
| 183 |
* @since 2.00 |
| 184 |
*/ |
| 185 |
public function bulkgeneratewebp_update_callback() { |
| 186 |
|
| 187 |
$action1 = 'bulkgeneratewebp-ajax-action'; |
| 188 |
if ( check_ajax_referer( $action1, 'nonce', false ) ) { |
| 189 |
if ( current_user_can( 'manage_options' ) ) { |
| 190 |
if ( ! empty( $_POST['id'] ) ) { |
| 191 |
$id = absint( $_POST['id'] ); |
| 192 |
} else { |
| 193 |
return; |
| 194 |
} |
| 195 |
if ( ! wp_get_attachment_url( $id ) ) { |
| 196 |
$error_string = __( 'No media!', 'plus-webp' ); |
| 197 |
$output_html = '<div>ID: ' . $id . ' <span style="color: red;">' . $error_string . '</span></div>'; |
| 198 |
} else { |
| 199 |
$output_html = $this->output_html( $id ); |
| 200 |
} |
| 201 |
if ( ! empty( $output_html ) ) { |
| 202 |
header( 'Content-type: text/html; charset=UTF-8' ); |
| 203 |
$allowed_output_html = array( |
| 204 |
'a' => array( |
| 205 |
'href' => array(), |
| 206 |
'target' => array(), |
| 207 |
'rel' => array(), |
| 208 |
'style' => array(), |
| 209 |
), |
| 210 |
'img' => array( |
| 211 |
'src' => array(), |
| 212 |
'width' => array(), |
| 213 |
'height' => array(), |
| 214 |
'style' => array(), |
| 215 |
), |
| 216 |
'div' => array( |
| 217 |
'style' => array(), |
| 218 |
'class' => array(), |
| 219 |
), |
| 220 |
'font' => array( |
| 221 |
'color' => array(), |
| 222 |
), |
| 223 |
'ul' => array(), |
| 224 |
'li' => array(), |
| 225 |
'span' => array( |
| 226 |
'class' => array(), |
| 227 |
'style' => array(), |
| 228 |
), |
| 229 |
); |
| 230 |
echo wp_kses( $output_html, $allowed_output_html ); |
| 231 |
} |
| 232 |
} |
| 233 |
} else { |
| 234 |
status_header( '403' ); |
| 235 |
echo 'Forbidden'; |
| 236 |
} |
| 237 |
|
| 238 |
wp_die(); |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
/** ================================================== |
| 243 |
* Output html for Ajax |
| 244 |
* |
| 245 |
* @param array $attach_id attach_id. |
| 246 |
* @since 2.00 |
| 247 |
*/ |
| 248 |
private function output_html( $attach_id ) { |
| 249 |
|
| 250 |
include_once ABSPATH . 'wp-admin/includes/image.php'; |
| 251 |
|
| 252 |
/* Generate thumbnails */ |
| 253 |
$metadata_org = wp_get_attachment_metadata( $attach_id ); |
| 254 |
do_action( 'wp_generate_attachment_metadata', $metadata_org, $attach_id ); |
| 255 |
if ( get_option( 'pluswebp_generate' ) ) { |
| 256 |
$ids = get_option( 'pluswebp_generate' ); |
| 257 |
delete_option( 'pluswebp_generate' ); |
| 258 |
$webp_id = $ids[0]; |
| 259 |
$metadata = wp_get_attachment_metadata( $webp_id ); |
| 260 |
|
| 261 |
/* Thumbnail urls */ |
| 262 |
list( $image_thumbnail, $imagethumburls ) = $this->thumbnail_urls( $webp_id, $metadata, $this->upload_url ); |
| 263 |
/* Output datas*/ |
| 264 |
list( $attachment_link, $attachment_url, $original_image_url, $mime_type, $stamptime, $file_size ) = $this->output_datas( $webp_id ); |
| 265 |
|
| 266 |
$output_html = '<div style="border-bottom: 1px solid; padding-top: 5px; padding-bottom: 5px;">'; |
| 267 |
$output_html .= '<img width="40" height="40" src="' . $image_thumbnail . '" style="float: left; margin: 5px;">'; |
| 268 |
$output_html .= '<div style="overflow: hidden;">'; |
| 269 |
$output_html .= '<div>ID: ' . $webp_id . '</div>'; |
| 270 |
$output_html .= '<div>' . __( 'Title' ) . ': ' . get_the_title( $webp_id ) . '</div>'; |
| 271 |
$output_html .= '<div>' . __( 'Permalink:' ) . ' <a href="' . $attachment_link . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $attachment_link . '</a></div>'; |
| 272 |
$output_html .= '<div>URL: <a href="' . $attachment_url . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $attachment_url . '</a></div>'; |
| 273 |
$output_html .= '<div>' . __( 'File name:' ) . ' ' . wp_basename( $attachment_url ) . '</div>'; |
| 274 |
if ( ! empty( $metadata['original_image'] ) ) { |
| 275 |
$output_html .= '<div>' . __( 'Original URL', 'plus-webp' ) . ': <a href="' . $original_image_url . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $original_image_url . '</a></div>'; |
| 276 |
$output_html .= '<div>' . __( 'Original File name', 'plus-webp' ) . ': ' . wp_basename( $original_image_url ) . '</div>'; |
| 277 |
} |
| 278 |
$output_html .= '<div>' . __( 'Date/Time' ) . ': ' . $stamptime . '</div>'; |
| 279 |
$output_html .= '<div>' . __( 'File type:' ) . ' ' . $mime_type . '</div>'; |
| 280 |
$output_html .= '<div>' . __( 'File size:' ) . ' ' . $file_size . '</div>'; |
| 281 |
if ( ! empty( $imagethumburls ) ) { |
| 282 |
$output_html .= '<div>' . __( 'Images' ) . ': '; |
| 283 |
foreach ( $imagethumburls as $thumbsize => $imagethumburl ) { |
| 284 |
$output_html .= '[<a href="' . $imagethumburl . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $thumbsize . '</a>]'; |
| 285 |
} |
| 286 |
$output_html .= '</div>'; |
| 287 |
} |
| 288 |
$output_html .= '</div></div>'; |
| 289 |
} else { |
| 290 |
$output_html = null; |
| 291 |
} |
| 292 |
|
| 293 |
return $output_html; |
| 294 |
|
| 295 |
} |
| 296 |
|
| 297 |
/** ================================================== |
| 298 |
* Thumbnail urls |
| 299 |
* |
| 300 |
* @param int $attach_id attach_id. |
| 301 |
* @param array $metadata metadata. |
| 302 |
* @param string $upload_url upload_url. |
| 303 |
* @return array $image_thumbnail(string), $imagethumburls(array) |
| 304 |
* @since 2.00 |
| 305 |
*/ |
| 306 |
private function thumbnail_urls( $attach_id, $metadata, $upload_url ) { |
| 307 |
|
| 308 |
$image_attr_thumbnail = wp_get_attachment_image_src( $attach_id, 'thumbnail', true ); |
| 309 |
$image_thumbnail = $image_attr_thumbnail[0]; |
| 310 |
|
| 311 |
$imagethumburls = array(); |
| 312 |
$media_path = get_post_meta( $attach_id, '_wp_attached_file', true ); |
| 313 |
$media_paths = explode( DIRECTORY_SEPARATOR, $media_path ); |
| 314 |
$str_length = strlen( end( $media_paths ) ); |
| 315 |
$media_path = substr( $media_path, 0, -$str_length ); |
| 316 |
$media_url = $upload_url . '/' . $media_path; |
| 317 |
if ( ! empty( $metadata ) ) { |
| 318 |
$thumbnails = $metadata['sizes']; |
| 319 |
foreach ( $thumbnails as $key => $key2 ) { |
| 320 |
$imagethumburls[ $key ] = $media_url . $key2['file']; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
return array( $image_thumbnail, $imagethumburls ); |
| 325 |
|
| 326 |
} |
| 327 |
|
| 328 |
/** ================================================== |
| 329 |
* Output datas |
| 330 |
* |
| 331 |
* @param int $attach_id attach_id. |
| 332 |
* @return array (string) $attachment_link, $attachment_url, $original_image_url, $mime_type(string), $stamptime, $file_size |
| 333 |
* @since 2.00 |
| 334 |
*/ |
| 335 |
private function output_datas( $attach_id ) { |
| 336 |
|
| 337 |
$attachment_link = get_attachment_link( $attach_id ); |
| 338 |
$attachment_url = wp_get_attachment_url( $attach_id ); |
| 339 |
$original_image_url = wp_get_original_image_url( $attach_id ); |
| 340 |
$filetype = wp_check_filetype( get_attached_file( $attach_id ) ); |
| 341 |
$mime_type = $filetype['type']; |
| 342 |
|
| 343 |
$stamptime = get_the_time( 'Y-n-j ', $attach_id ) . get_the_time( 'G:i:s', $attach_id ); |
| 344 |
if ( isset( $metadata['filesize'] ) ) { |
| 345 |
$file_size = $metadata['filesize']; |
| 346 |
} else { |
| 347 |
$file_size = @filesize( get_attached_file( $attach_id ) ); |
| 348 |
} |
| 349 |
if ( ! $file_size ) { |
| 350 |
$file_size = '<font color="red">' . __( 'Could not retrieve.', 'plus-webp' ) . '</font>'; |
| 351 |
} else { |
| 352 |
$file_size = size_format( $file_size ); |
| 353 |
} |
| 354 |
|
| 355 |
return array( $attachment_link, $attachment_url, $original_image_url, $mime_type, $stamptime, $file_size ); |
| 356 |
|
| 357 |
} |
| 358 |
|
| 359 |
/** ================================================== |
| 360 |
* Messages Callback |
| 361 |
* |
| 362 |
* @since 2.00 |
| 363 |
*/ |
| 364 |
public function bulkgeneratewebp_message_callback() { |
| 365 |
|
| 366 |
$action2 = 'bulkgeneratewebp_message'; |
| 367 |
if ( check_ajax_referer( $action2, 'nonce', false ) ) { |
| 368 |
$error_count = 0; |
| 369 |
$error_update = null; |
| 370 |
$success_count = 0; |
| 371 |
if ( ! empty( $_POST['error_count'] ) ) { |
| 372 |
$error_count = absint( $_POST['error_count'] ); |
| 373 |
} |
| 374 |
if ( ! empty( $_POST['error_update'] ) ) { |
| 375 |
$error_update = sanitize_text_field( wp_unslash( $_POST['error_update'] ) ); |
| 376 |
} |
| 377 |
if ( ! empty( $_POST['success_count'] ) ) { |
| 378 |
$success_count = absint( $_POST['success_count'] ); |
| 379 |
} |
| 380 |
|
| 381 |
$back_html = '<strong><a style="text-decoration: none;" href="' . admin_url( 'tools.php?page=pluswebp' ) . '">' . __( 'Back' ) . '</a></strong>'; |
| 382 |
|
| 383 |
$output_html = null; |
| 384 |
if ( $error_count > 0 ) { |
| 385 |
/* translators: error message %1$d: media count %2$s: back link */ |
| 386 |
$error_message = sprintf( __( 'Errored to the generation of %1$d medias.', 'plus-webp' ), $error_count, $back_html ); |
| 387 |
$output_html .= '<div class="notice notice-error is-dismissible"><ul><li><div>' . $error_message . '</div>' . $error_update . '</li></ul></div>'; |
| 388 |
} |
| 389 |
if ( $success_count > 0 ) { |
| 390 |
/* translators: success message %1$d: media count %2$s: back link */ |
| 391 |
$success_message = sprintf( __( 'Succeeded to the generation webp of %1$d medias for Media Library. %2$s', 'plus-webp' ), $success_count, $back_html ); |
| 392 |
$output_html .= '<div class="notice notice-success is-dismissible"><ul><li><div>' . $success_message . '</li></ul></div>'; |
| 393 |
} |
| 394 |
|
| 395 |
header( 'Content-type: text/html; charset=UTF-8' ); |
| 396 |
$allowed_output_html = array( |
| 397 |
'div' => array( |
| 398 |
'class' => array(), |
| 399 |
), |
| 400 |
'ul' => array(), |
| 401 |
'li' => array(), |
| 402 |
'strong' => array(), |
| 403 |
'a' => array( |
| 404 |
'style' => array(), |
| 405 |
'href' => array(), |
| 406 |
), |
| 407 |
); |
| 408 |
echo wp_kses( $output_html, $allowed_output_html ); |
| 409 |
} |
| 410 |
|
| 411 |
wp_die(); |
| 412 |
|
| 413 |
} |
| 414 |
|
| 415 |
/** ================================================== |
| 416 |
* Mail send hook |
| 417 |
* |
| 418 |
* @param string $to to. |
| 419 |
* @since 1.09 |
| 420 |
*/ |
| 421 |
public function mail_send( $to ) { |
| 422 |
|
| 423 |
$post_ids = get_option( 'pluswebp_generate' ); |
| 424 |
delete_option( 'pluswebp_generate' ); |
| 425 |
|
| 426 |
if ( function_exists( 'wp_date' ) ) { |
| 427 |
$now_date_time = wp_date( 'Y-m-d H:i:s' ); |
| 428 |
} else { |
| 429 |
$now_date_time = date_i18n( 'Y-m-d H:i:s' ); |
| 430 |
} |
| 431 |
$pluswebp_mail_send = array(); |
| 432 |
$pluswebp_mail_send['datetime'] = $now_date_time; |
| 433 |
|
| 434 |
/* translators: Date and Time */ |
| 435 |
$message = sprintf( __( 'Plus WebP : %s', 'plus-webp' ), $now_date_time ) . "\r\n\r\n"; |
| 436 |
|
| 437 |
if ( ! empty( $post_ids ) ) { |
| 438 |
|
| 439 |
$message .= __( 'Generation of WebP is complete.', 'plus-webp' ) . "\r\n\r\n"; |
| 440 |
$pluswebp_mail_send['count'] = count( $post_ids ); |
| 441 |
|
| 442 |
$count = 0; |
| 443 |
foreach ( $post_ids as $attach_id ) { |
| 444 |
$filename = get_attached_file( $attach_id ); |
| 445 |
$metadata = wp_get_attachment_metadata( $attach_id ); |
| 446 |
/* OutputMetaData */ |
| 447 |
list( $imagethumburls, $stamptime, $file_size ) = $this->output_metadata( $attach_id, $metadata, $this->upload_url ); |
| 448 |
$count++; |
| 449 |
$message .= __( 'Count' ) . ': ' . $count . "\n"; |
| 450 |
$message .= 'ID: ' . $attach_id . "\n"; |
| 451 |
$message .= __( 'Title' ) . ': ' . get_the_title( $attach_id ) . "\n"; |
| 452 |
$message .= __( 'Permalink:' ) . ' ' . get_attachment_link( $attach_id ) . "\n"; |
| 453 |
$message .= 'URL: ' . wp_get_attachment_url( $attach_id ) . "\n"; |
| 454 |
$message .= __( 'File name:' ) . ' ' . wp_basename( wp_get_attachment_url( $attach_id ) ) . "\n"; |
| 455 |
if ( ! empty( $metadata['original_image'] ) ) { |
| 456 |
$message .= __( 'Original URL:', 'plus-webp' ) . ' ' . wp_get_original_image_url( $attach_id ) . "\n"; |
| 457 |
$message .= __( 'Original File name:', 'plus-webp' ) . ' ' . wp_basename( wp_get_original_image_url( $attach_id ) ) . "\n"; |
| 458 |
} |
| 459 |
$message .= __( 'Date/Time' ) . ': ' . $stamptime . "\n"; |
| 460 |
if ( ! $file_size ) { |
| 461 |
$file_size = __( 'Could not retrieve.', 'plus-webp' ); |
| 462 |
} else { |
| 463 |
$file_size = size_format( $file_size ); |
| 464 |
} |
| 465 |
$message .= __( 'File size:' ) . ' ' . $file_size . "\n"; |
| 466 |
if ( ! empty( $imagethumburls ) ) { |
| 467 |
foreach ( $imagethumburls as $thumbsize => $imagethumburl ) { |
| 468 |
$message .= $thumbsize . ': ' . $imagethumburl . "\n"; |
| 469 |
} |
| 470 |
} |
| 471 |
$message .= "\n"; |
| 472 |
} |
| 473 |
} else { |
| 474 |
$message .= __( 'WebP was not generated. A file with the same name already exists.', 'plus-webp' ) . "\r\n\r\n"; |
| 475 |
$pluswebp_mail_send['count'] = 0; |
| 476 |
} |
| 477 |
|
| 478 |
update_option( 'pluswebp_generate_mail', $pluswebp_mail_send ); |
| 479 |
|
| 480 |
/* translators: blogname for subject */ |
| 481 |
$subject = sprintf( __( '[%s] WebP generate', 'plus-webp' ), get_option( 'blogname' ) ); |
| 482 |
wp_mail( $to, $subject, $message ); |
| 483 |
|
| 484 |
} |
| 485 |
|
| 486 |
/** ================================================== |
| 487 |
* Output metadata |
| 488 |
* |
| 489 |
* @param int $attach_id attach_id. |
| 490 |
* @param array $metadata metadata. |
| 491 |
* @param string $upload_url upload_url. |
| 492 |
* @return array $imagethumburls(string), $stamptime(string), $file_size(string) |
| 493 |
* @since 1.09 |
| 494 |
*/ |
| 495 |
private function output_metadata( $attach_id, $metadata, $upload_url ) { |
| 496 |
|
| 497 |
$imagethumburls = array(); |
| 498 |
$wp_attached_file = get_post_meta( $attach_id, '_wp_attached_file', true ); |
| 499 |
$imagethumburl_base = $upload_url . '/' . rtrim( $wp_attached_file, wp_basename( $wp_attached_file ) ); |
| 500 |
if ( ! empty( $metadata ) ) { |
| 501 |
foreach ( $metadata as $key1 => $key2 ) { |
| 502 |
if ( 'sizes' === $key1 ) { |
| 503 |
foreach ( $metadata[ $key1 ] as $key2 => $key3 ) { |
| 504 |
$imagethumburls[ $key2 ] = $imagethumburl_base . $metadata['sizes'][ $key2 ]['file']; |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
$stamptime = get_the_time( 'Y-n-j ', $attach_id ) . get_the_time( 'G:i:s', $attach_id ); |
| 511 |
$file_size = @filesize( get_attached_file( $attach_id, false ) ); |
| 512 |
|
| 513 |
return array( $imagethumburls, $stamptime, $file_size ); |
| 514 |
|
| 515 |
} |
| 516 |
|
| 517 |
/** ================================================== |
| 518 |
* Webp create |
| 519 |
* |
| 520 |
* @param string $filename input filename for pictures. |
| 521 |
* @param string $mime_type mimetype. |
| 522 |
* @param string $filename_webp output filename for webp. |
| 523 |
* @return bool $ret create bool. |
| 524 |
* @since 1.00 |
| 525 |
*/ |
| 526 |
private function create_webp( $filename, $mime_type, $filename_webp ) { |
| 527 |
|
| 528 |
if ( ! file_exists( $filename ) ) { |
| 529 |
return false; |
| 530 |
} |
| 531 |
if ( file_exists( $filename_webp ) ) { |
| 532 |
return false; |
| 533 |
} |
| 534 |
|
| 535 |
$pluswebp_settings = get_option( 'pluswebp' ); |
| 536 |
@set_time_limit( 60 ); |
| 537 |
|
| 538 |
$ret = false; |
| 539 |
switch ( $mime_type ) { |
| 540 |
case 'image/jpeg': |
| 541 |
$src = imagecreatefromjpeg( $filename ); |
| 542 |
$img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); |
| 543 |
imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) ); |
| 544 |
imagealphablending( $img, true ); |
| 545 |
break; |
| 546 |
case 'image/png': |
| 547 |
$src = imagecreatefrompng( $filename ); |
| 548 |
$img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); |
| 549 |
imagealphablending( $img, false ); |
| 550 |
imagesavealpha( $img, true ); |
| 551 |
break; |
| 552 |
case 'image/bmp': |
| 553 |
$src = imagecreatefrombmp( $filename ); |
| 554 |
$img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); |
| 555 |
break; |
| 556 |
case 'image/gif': |
| 557 |
$src = imagecreatefromgif( $filename ); |
| 558 |
$img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); |
| 559 |
$bgcolor = imagecolorallocatealpha( $img, 0, 0, 0, 127 ); |
| 560 |
imagefill( $img, 0, 0, $bgcolor ); |
| 561 |
imagecolortransparent( $img, $bgcolor ); |
| 562 |
break; |
| 563 |
} |
| 564 |
|
| 565 |
imagecopy( $img, $src, 0, 0, 0, 0, imagesx( $src ), imagesy( $src ) ); |
| 566 |
imagedestroy( $src ); |
| 567 |
$ret = imagewebp( $img, $filename_webp, $pluswebp_settings['quality'] ); |
| 568 |
imagedestroy( $img ); |
| 569 |
|
| 570 |
return $ret; |
| 571 |
|
| 572 |
} |
| 573 |
|
| 574 |
/** ================================================== |
| 575 |
* Change ext |
| 576 |
* |
| 577 |
* @param string $before_file_name before_file_name. |
| 578 |
* @param string $ext ext. |
| 579 |
* @param bool $addext addext. |
| 580 |
* @return array $after_file_name after_file_name. |
| 581 |
* @since 1.00 |
| 582 |
*/ |
| 583 |
private function change_ext( $before_file_name, $ext, $addext ) { |
| 584 |
|
| 585 |
if ( $addext ) { |
| 586 |
$after_file_name = $before_file_name . '.' . $ext; |
| 587 |
} else { |
| 588 |
$exts = explode( '.', $before_file_name ); |
| 589 |
$before_ext = '.' . end( $exts ); |
| 590 |
$after_ext = '.' . $ext; |
| 591 |
$after_file_name = str_replace( $before_ext, $after_ext, $before_file_name ); |
| 592 |
} |
| 593 |
|
| 594 |
return $after_file_name; |
| 595 |
|
| 596 |
} |
| 597 |
|
| 598 |
/** ================================================== |
| 599 |
* Change DB |
| 600 |
* |
| 601 |
* @param string $before_url before_url. |
| 602 |
* @param string $after_url after_url. |
| 603 |
* @since 1.00 |
| 604 |
*/ |
| 605 |
private function change_db( $before_url, $after_url ) { |
| 606 |
|
| 607 |
global $wpdb; |
| 608 |
|
| 609 |
/* Replace */ |
| 610 |
$wpdb->query( |
| 611 |
$wpdb->prepare( |
| 612 |
"UPDATE `$wpdb->posts` SET post_content = replace(post_content, %s, %s)", |
| 613 |
$before_url, |
| 614 |
$after_url |
| 615 |
) |
| 616 |
); |
| 617 |
|
| 618 |
} |
| 619 |
|
| 620 |
/** ================================================== |
| 621 |
* Upload Path |
| 622 |
* |
| 623 |
* @return array $upload_dir,$upload_url,$upload_path uploadpath. |
| 624 |
* @since 1.00 |
| 625 |
*/ |
| 626 |
public function upload_dir_url_path() { |
| 627 |
|
| 628 |
$wp_uploads = wp_upload_dir(); |
| 629 |
|
| 630 |
$relation_path_true = strpos( $wp_uploads['baseurl'], '../' ); |
| 631 |
if ( $relation_path_true > 0 ) { |
| 632 |
$relationalpath = substr( $wp_uploads['baseurl'], $relation_path_true ); |
| 633 |
$basepath = substr( $wp_uploads['baseurl'], 0, $relation_path_true ); |
| 634 |
$upload_url = $this->realurl( $basepath, $relationalpath ); |
| 635 |
$upload_dir = wp_normalize_path( realpath( $wp_uploads['basedir'] ) ); |
| 636 |
} else { |
| 637 |
$upload_url = $wp_uploads['baseurl']; |
| 638 |
$upload_dir = wp_normalize_path( $wp_uploads['basedir'] ); |
| 639 |
} |
| 640 |
|
| 641 |
if ( is_ssl() ) { |
| 642 |
$upload_url = str_replace( 'http:', 'https:', $upload_url ); |
| 643 |
} |
| 644 |
|
| 645 |
if ( $relation_path_true > 0 ) { |
| 646 |
$upload_path = $relationalpath; |
| 647 |
} else { |
| 648 |
$upload_path = str_replace( site_url( '/' ), '', $upload_url ); |
| 649 |
} |
| 650 |
|
| 651 |
$upload_dir = untrailingslashit( $upload_dir ); |
| 652 |
$upload_url = untrailingslashit( $upload_url ); |
| 653 |
$upload_path = untrailingslashit( $upload_path ); |
| 654 |
|
| 655 |
return array( $upload_dir, $upload_url, $upload_path ); |
| 656 |
|
| 657 |
} |
| 658 |
|
| 659 |
/** ================================================== |
| 660 |
* Real Url |
| 661 |
* |
| 662 |
* @param string $base base. |
| 663 |
* @param string $relationalpath relationalpath. |
| 664 |
* @return string $realurl realurl. |
| 665 |
* @since 1.00 |
| 666 |
*/ |
| 667 |
private function realurl( $base, $relationalpath ) { |
| 668 |
|
| 669 |
$parse = array( |
| 670 |
'scheme' => null, |
| 671 |
'user' => null, |
| 672 |
'pass' => null, |
| 673 |
'host' => null, |
| 674 |
'port' => null, |
| 675 |
'query' => null, |
| 676 |
'fragment' => null, |
| 677 |
); |
| 678 |
$parse = wp_parse_url( $base ); |
| 679 |
|
| 680 |
if ( strpos( $parse['path'], '/', ( strlen( $parse['path'] ) - 1 ) ) !== false ) { |
| 681 |
$parse['path'] .= '.'; |
| 682 |
} |
| 683 |
|
| 684 |
if ( preg_match( '#^https?://#', $relationalpath ) ) { |
| 685 |
return $relationalpath; |
| 686 |
} elseif ( preg_match( '#^/.*$#', $relationalpath ) ) { |
| 687 |
return $parse['scheme'] . '://' . $parse['host'] . $relationalpath; |
| 688 |
} else { |
| 689 |
$base_path = explode( '/', dirname( $parse['path'] ) ); |
| 690 |
$rel_path = explode( '/', $relationalpath ); |
| 691 |
foreach ( $rel_path as $rel_dir_name ) { |
| 692 |
if ( '.' === $rel_dir_name ) { |
| 693 |
array_shift( $base_path ); |
| 694 |
array_unshift( $base_path, '' ); |
| 695 |
} elseif ( '..' === $rel_dir_name ) { |
| 696 |
array_pop( $base_path ); |
| 697 |
if ( count( $base_path ) === 0 ) { |
| 698 |
$base_path = array( '' ); |
| 699 |
} |
| 700 |
} else { |
| 701 |
array_push( $base_path, $rel_dir_name ); |
| 702 |
} |
| 703 |
} |
| 704 |
$path = implode( '/', $base_path ); |
| 705 |
return $parse['scheme'] . '://' . $parse['host'] . $path; |
| 706 |
} |
| 707 |
|
| 708 |
} |
| 709 |
|
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
|