SHARE
TWEET

Thrive image optimization

a guest Oct 19th, 2018 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. <?php
  3. //thrive image optimization
  4.  
  5.  
  6. add_filter( 'wp_generate_attachment_metadata', 'thrive_update_attachment', 10, 2 );
  7.  
  8. add_filter( 'manage_media_columns', 'thrive_media_columns' );
  9. add_action( 'manage_media_custom_column', 'thrive_media_custom_column', 10, 2 );
  10.  
  11. add_action( 'admin_action_wp_kraken_manual', 'thrive_process_single_kraken_image' );
  12.  
  13. add_action( 'rest_api_init', 'thrive_kraken_callback_route' );
  14.  
  15. function thrive_kraken_callback_route() {
  16.     register_rest_route( 'thrive', '/kraken', array(
  17.         array(
  18.             'methods'  => WP_REST_Server::CREATABLE,
  19.             'callback' => 'thrive_kraken_callback',
  20.         ),
  21.     ) );
  22. }
  23.  
  24. function thrive_update_attachment( $meta, $ID ) {
  25.  
  26.     if ( ! isset( $meta['sizes'] ) ) {
  27.         return $meta;
  28.     }
  29.    
  30.     $optimize_image_type = thrive_get_theme_options( 'image_optimization_type' );
  31.     if ( $optimize_image_type === 'off' ) {
  32.         return $meta;
  33.     }
  34.     $lossy = $optimize_image_type === 'lossy';
  35.  
  36.     $attachment_file_path = get_attached_file( $ID );
  37.     $attachment_file_url  = wp_get_attachment_url( $ID );
  38.     $upload_dir           = trailingslashit( dirname( $attachment_file_path ) );
  39.  
  40.     $sizes = array(
  41.         array(
  42.             'id'       => $ID,
  43.             'strategy' => 'none',
  44.             'file'     => $attachment_file_path,
  45.         )
  46.     );
  47.     foreach ( $meta['sizes'] as $size_key => $size_data ) {
  48.         $sizes[] = array(
  49.             'id'       => $size_key,
  50.             'strategy' => 'auto',
  51.             'width'    => $size_data['width'],
  52.             'height'   => $size_data['height'],
  53.             'file'     => $upload_dir . $size_data['file']
  54.         );
  55.     }
  56.  
  57.     do {
  58.         $reduced        = array_slice( $sizes, 0, 10 );
  59.         $process_result = thrive_process_kraken_image( $attachment_file_url, $reduced, $lossy );
  60.         $sizes          = array_slice( $sizes, 9 );
  61.     } while ( count( $sizes ) > 10 );
  62.     $meta['wp_kraken'] = $process_result;
  63.  
  64.     return $meta;
  65. }
  66.  
  67. function thrive_media_columns( $defaults ) {
  68.     $optimize_image_type = thrive_get_theme_options( 'image_optimization_type' );
  69.  
  70.     if ( $optimize_image_type == "off" ) {
  71.         return $defaults;
  72.     }
  73.     $defaults['kraken'] = 'Optimize';
  74.  
  75.     return $defaults;
  76. }
  77.  
  78. function thrive_media_custom_column( $column_name, $id ) {
  79.     $optimize_image_type = thrive_get_theme_options( 'image_optimization_type' );
  80.     if ( $optimize_image_type == "off" ) {
  81.         return $defaults;
  82.     }
  83.     if ( 'kraken' == $column_name ) {
  84.         $data = wp_get_attachment_metadata( $id );
  85.         if ( isset( $data['wp_kraken'] ) && ! empty( $data['wp_kraken'] ) ) {
  86.             print $data['wp_kraken'];
  87.             printf( "<br><a href=\"admin.php?action=wp_kraken_manual&amp;attachment_ID=%d\">%s</a>", $id, __( 'Re-compress', 'thrive' ) );
  88.         } else {
  89.             if ( wp_attachment_is_image( $id ) ) {
  90.                 print __( 'Not processed', 'thrive' );
  91.                 printf( "<br><a href=\"admin.php?action=wp_kraken_manual&amp;attachment_ID=%d\">%s</a>", $id, __( 'Compress', 'thrive' ) );
  92.             }
  93.         }
  94.     }
  95. }
  96.  
  97. function thrive_process_single_kraken_image() {
  98.     if ( ! current_user_can( 'upload_files' ) ) {
  99.         wp_die( __( "You don't have permission to work with uploaded files.", 'thrive' ) );
  100.     }
  101.  
  102.     if ( ! isset( $_GET['attachment_ID'] ) ) {
  103.         wp_die( __( 'No attachment ID was provided.', 'thrive' ) );
  104.     }
  105.  
  106.     $attachment_ID = intval( $_GET['attachment_ID'] );
  107.  
  108.     $original_meta = wp_get_attachment_metadata( $attachment_ID );
  109.  
  110.     $new_meta = thrive_update_attachment( $original_meta, $attachment_ID );
  111.     wp_update_attachment_metadata( $attachment_ID, $new_meta );
  112.  
  113.     wp_redirect( preg_replace( '|[^a-z0-9-~+_.?#=&;,/:]|i', '', wp_get_referer() ) );
  114.     exit();
  115. }
  116.  
  117. /**
  118.  * Send request to kraken for optimizing an image
  119.  *
  120.  * @param $file_url
  121.  * @param array $sizes
  122.  * @param int $lossy
  123.  *
  124.  * @return string
  125.  */
  126. function thrive_process_kraken_image( $file_url, $sizes = array(), $lossy = 1 ) {
  127.     require_once 'libs/ThriveOptimize.php';
  128.  
  129.     $thriveOptimize = new ThriveOptimize();
  130.  
  131.     $kraken_callback_url = get_rest_url() . 'thrive/kraken';
  132.  
  133.     $params = array(
  134.         'file_url'      => $file_url,
  135.         'callback_url'  => $kraken_callback_url,
  136.         'lossy'         => $lossy,
  137.         'resize'        => array_values( $sizes ),
  138.         'preserve_meta' => array(
  139.             'profile',
  140.             'copyright',
  141.         ),
  142.     );
  143.  
  144.     $data = $thriveOptimize->url( $params );
  145.  
  146.     if ( ! isset( $data['id'] ) && isset( $data['message'] ) ) {
  147.         return $data['message'];
  148.     }
  149.  
  150.     if ( ! isset( $data['id'] ) ) {
  151.         return 'Compress failed';
  152.     }
  153.  
  154.     $paths = array();
  155.  
  156.     foreach ( $sizes as $size ) {
  157.         $paths[ $size['id'] ] = $size['file'];
  158.     }
  159.  
  160.     add_option( $data['id'], json_encode( $paths ), '', 'no' );
  161.  
  162.     return 'Compress in progress (refresh to see the result)';
  163. }
  164.  
  165. /**
  166.  * Kraken callback for optimizing images
  167.  */
  168. function thrive_kraken_callback() {
  169.     $response = json_decode( file_get_contents( 'php://input' ), true );
  170.  
  171.     if ( ! isset( $response['id'] ) ) {
  172.         die;
  173.     }
  174.  
  175.     $results   = $response['results'];
  176.     $option_id = $response['id'];
  177.  
  178.     $option_str = get_option( $option_id );
  179.  
  180.     if ( ! $option_str ) {
  181.         die;
  182.     }
  183.  
  184.     $files = json_decode( $option_str );
  185.     foreach ( $files as $aid => $file_path ) {
  186.  
  187.         $meta = wp_get_attachment_metadata( $aid );
  188.  
  189.         if ( ! empty( $response['file_already_compressed'] ) ) {
  190.             $meta_info_msg     = 'File is already compressed';
  191.             $meta['wp_kraken'] = $meta_info_msg;
  192.             wp_update_attachment_metadata( $aid, $meta );
  193.             die;
  194.         }
  195.  
  196.         /*
  197.          * On success overwrite the current file
  198.          */
  199.         if ( ! empty( $results[ $aid ] ) ) {
  200.             $original_size = filesize( $file_path );
  201.             $file_response = wp_remote_get( $results[ $aid ]['kraked_url'] );
  202.             $image_string  = wp_remote_retrieve_body( $file_response );
  203.  
  204.             if ( empty( $image_string ) || is_wp_error( $file_response ) ) {
  205.                 $meta_info_msg     = 'Compress failed';
  206.                 $meta['wp_kraken'] = $meta_info_msg;
  207.                 wp_update_attachment_metadata( $aid, $meta );
  208.                 die;
  209.             }
  210.             $new_size = file_put_contents( $file_path, $image_string );
  211.  
  212.             $meta_info_msg     = 'Compressed (saved ' . ( $original_size - $new_size ) . ' bytes)';
  213.             $meta['wp_kraken'] = $meta_info_msg;
  214.             wp_update_attachment_metadata( $aid, $meta );
  215.         }
  216.     }
  217.  
  218.     /* we should delete the option each time */
  219.     delete_option( $option_id );
  220. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top