Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save wordpressvn/b26c9ab5dfe905dd82dd85ff9d9b611a to your computer and use it in GitHub Desktop.

Select an option

Save wordpressvn/b26c9ab5dfe905dd82dd85ff9d9b611a to your computer and use it in GitHub Desktop.

Revisions

  1. @wpmudev-sls wpmudev-sls revised this gist Oct 26, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion smush-original-images.php
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ class WPMUDEV_Original_Images {

    private static $_instance = null;
    private static $smush_core = null;
    private static $per_page = 2;
    private static $per_page = 20;

    public static function get_instance() {

  2. @wpmudev-sls wpmudev-sls revised this gist Oct 26, 2019. 1 changed file with 22 additions and 19 deletions.
    41 changes: 22 additions & 19 deletions smush-original-images.php
    Original file line number Diff line number Diff line change
    @@ -10,15 +10,15 @@


    if ( ! defined( 'ABSPATH' ) ) {
    exit;
    exit;
    }


    if ( ! class_exists( 'WPMUDEV_Original_Images' ) ) {

    class WPMUDEV_Original_Images {

    private static $_instance = null;
    private static $_instance = null;
    private static $smush_core = null;
    private static $per_page = 2;

    @@ -33,7 +33,7 @@ public static function get_instance() {

    private function __construct() {

    if ( ! class_exists( 'WP_Smush' ) ) {
    if ( ! class_exists( 'WP_Smush' ) && ! class_exists( 'Smush\\WP_Smush' ) ) {
    return;
    }

    @@ -47,11 +47,11 @@ private function __construct() {
    public function ajax_delete_attachment() {
    check_ajax_referer( 'wpmudev_delete_bak_image', 'security' );

    $bak_path = filter_input( INPUT_POST, 'bak_path', FILTER_DEFAULT );
    $attachment_id = filter_input( INPUT_POST, 'attachment_id', FILTER_VALIDATE_INT );
    $bak_path = filter_input( INPUT_POST, 'bak_path', FILTER_DEFAULT );
    $attachment_id = filter_input( INPUT_POST, 'attachment_id', FILTER_VALIDATE_INT );

    if ( unlink( $bak_path ) ) {
    delete_post_meta( $attachment_id, '_wp_attachment_backup_sizes' );
    delete_post_meta( $attachment_id, '_wp_attachment_backup_sizes' );
    }

    return wp_send_json(
    @@ -64,20 +64,21 @@ public function ajax_delete_attachment() {
    public function fetch_images ( $offset = 0 ) {

    $args = array(
    'post_type' => 'attachment',
    'offset' => $offset,
    'posts_per_page' => self::$per_page,
    'post_status' => null
    'post_type' => 'attachment',
    'offset' => $offset,
    'posts_per_page' => self::$per_page,
    'post_status' => null
    );
    $attachments = get_posts( $args );
    $images = array();

    if( ! empty( $attachments ) ) {
    foreach( $attachments as $attachment ){

    $fullsize_path = get_attached_file( $attachment->ID );
    $back_src = $this->add_bak_substension( $attachment->guid );
    $back_path = $this->add_bak_substension( $fullsize_path );
    $fullsize_path = get_attached_file( $attachment->ID );
    $back_src = $this->add_bak_substension( $attachment->guid );
    $back_path = $this->add_bak_substension( $fullsize_path );
    list( $src, $width, $height ) = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' );

    if ( ! file_exists( $back_path ) ) {
    continue;
    @@ -86,7 +87,8 @@ public function fetch_images ( $offset = 0 ) {
    $images[] = array(
    'attachment_id' => $attachment->ID,
    'src' => $back_src,
    'bak_path' => $back_path
    'bak_path' => $back_path,
    'thumb' => $src
    );

    }
    @@ -151,8 +153,9 @@ private function list_images( $images ) {
    foreach ( $images as $image ) {

    $attachment_id = $image[ 'attachment_id' ];
    $src = $image[ 'src' ];
    //$src = $image[ 'src' ];
    $bak_path = $image[ 'bak_path' ];
    $src = $image[ 'thumb' ];
    $out .= "
    <div style=\"width: 100%; vertical-align: top; margin-bottom: 20px;\" id=\"back-item-wrap-{$attachment_id}\">
    <label>
    @@ -321,11 +324,11 @@ public function original_images() {

    if ( ! function_exists( 'wpmudev_original_images' ) ) {

    function wpmudev_original_images(){
    return WPMUDEV_Original_Images::get_instance();
    };
    function wpmudev_original_images(){
    return WPMUDEV_Original_Images::get_instance();
    };

    add_action( 'plugins_loaded', 'wpmudev_original_images', 10 );
    add_action( 'plugins_loaded', 'wpmudev_original_images', 10 );
    }

    }
  3. @wpmudev-sls wpmudev-sls revised this gist Oct 4, 2019. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions smush-original-images.php
    Original file line number Diff line number Diff line change
    @@ -47,8 +47,12 @@ private function __construct() {
    public function ajax_delete_attachment() {
    check_ajax_referer( 'wpmudev_delete_bak_image', 'security' );

    $bak_path = filter_input( INPUT_POST, 'bak_path', FILTER_DEFAULT );
    unlink( $bak_path );
    $bak_path = filter_input( INPUT_POST, 'bak_path', FILTER_DEFAULT );
    $attachment_id = filter_input( INPUT_POST, 'attachment_id', FILTER_VALIDATE_INT );

    if ( unlink( $bak_path ) ) {
    delete_post_meta( $attachment_id, '_wp_attachment_backup_sizes' );
    }

    return wp_send_json(
    array(
  4. @wpmudev-sls wpmudev-sls created this gist Mar 19, 2019.
    327 changes: 327 additions & 0 deletions smush-original-images.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,327 @@
    <?php
    /**
    * Plugin Name: [Smush] - Original Images
    * Plugin URI: https://premium.wpmudev.org/
    * Description: Displays all original images so they can be deleted. Requires Smush
    * Author: Panos Lyrakis @ WPMUDEV
    * Author URI: https://premium.wpmudev.org/
    * License: GPLv2 or later
    */


    if ( ! defined( 'ABSPATH' ) ) {
    exit;
    }


    if ( ! class_exists( 'WPMUDEV_Original_Images' ) ) {

    class WPMUDEV_Original_Images {

    private static $_instance = null;
    private static $smush_core = null;
    private static $per_page = 2;

    public static function get_instance() {

    if( is_null( self::$_instance ) ){
    self::$_instance = new WPMUDEV_Original_Images();
    }
    return self::$_instance;

    }

    private function __construct() {

    if ( ! class_exists( 'WP_Smush' ) ) {
    return;
    }

    add_action( 'admin_menu', array( $this, 'admin_menu' ), 20 );
    add_action( 'wp_ajax_wpmudev_fetch_bak_images', array( $this, 'ajax_fetch_images' ) ) ;
    add_action( 'wp_ajax_wpmudev_delete_bak_image', array( $this, 'ajax_delete_attachment' ) );

    }


    public function ajax_delete_attachment() {
    check_ajax_referer( 'wpmudev_delete_bak_image', 'security' );

    $bak_path = filter_input( INPUT_POST, 'bak_path', FILTER_DEFAULT );
    unlink( $bak_path );

    return wp_send_json(
    array(
    'success' => true,
    )
    );
    }

    public function fetch_images ( $offset = 0 ) {

    $args = array(
    'post_type' => 'attachment',
    'offset' => $offset,
    'posts_per_page' => self::$per_page,
    'post_status' => null
    );
    $attachments = get_posts( $args );
    $images = array();

    if( ! empty( $attachments ) ) {
    foreach( $attachments as $attachment ){

    $fullsize_path = get_attached_file( $attachment->ID );
    $back_src = $this->add_bak_substension( $attachment->guid );
    $back_path = $this->add_bak_substension( $fullsize_path );

    if ( ! file_exists( $back_path ) ) {
    continue;
    }

    $images[] = array(
    'attachment_id' => $attachment->ID,
    'src' => $back_src,
    'bak_path' => $back_path
    );

    }

    if ( empty( $images ) ) {
    return true;
    }

    return $images;
    }
    else {
    return false;
    }

    }

    public function ajax_fetch_images() {

    check_ajax_referer( 'wpmudev_fetch_bak_images', 'security' );

    $offset = (int) $_POST['offset'];
    $images = $this->fetch_images( $offset );

    if ( ! $images ) {
    $return = array(
    'success' => true,
    'list' => '',
    'offset' => false
    );
    wp_send_json($return);
    }

    $images_list = $this->list_images( $images );
    $offset += self::$per_page;

    $return = array(
    'success' => true,
    'list' => $images_list,
    'offset' => $offset
    );
    wp_send_json($return);

    }


    private function add_bak_substension( $path ) {
    $pathinfo = pathinfo( $path );
    $back_path = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '.bak.' . $pathinfo['extension'];

    return $back_path;
    }


    private function list_images( $images ) {

    if ( ! is_array( $images ) ) {
    return;
    }

    $out = '';

    foreach ( $images as $image ) {

    $attachment_id = $image[ 'attachment_id' ];
    $src = $image[ 'src' ];
    $bak_path = $image[ 'bak_path' ];
    $out .= "
    <div style=\"width: 100%; vertical-align: top; margin-bottom: 20px;\" id=\"back-item-wrap-{$attachment_id}\">
    <label>
    <span style=\"display:inline-block;vertical-align: top;\">
    <input type=\"checkbox\" class=\"single-back-chkbx\" id=\"back-item-{$attachment_id}\" value=\"{$attachment_id}\" data-bak-path=\"{$bak_path}\" />
    </span>
    <span style=\"display:inline-block; width:150px;\">
    <img src=\"{$src}\" width=\"150\" />
    </span>
    <span style=\"display:inline-block;vertical-align: top;\">
    {$bak_path}
    </span>
    </label>
    </div>";
    }

    return $out;

    }


    public function admin_menu() {

    add_submenu_page(
    'smush' ,
    'Original Images',
    'Original Images',
    'manage_options',
    'original-images',
    array( $this, 'original_images' )
    );

    }

    public function original_images() {
    ?>
    <h1>Original Images</h1>

    <div id="smush-back-images-list"></div>
    <div id="smush-back-images-del-button-wrap" style="display: none;" >

    <label>
    <input type="checkbox" id="smush-back-check-all" /> Check all
    </label>

    <button id="smush-back-deleter">Delete Selected</button>
    </div>
    <div id="smush-spinner"> <img src='https://svn.automattic.com/wordpress/trunk/wp-admin/images/spinner.gif' /> </div>

    <script type="text/javascript">
    ( $ => {

    // List all bak images with Ajax
    Smush_Orig_Images = {

    list_container : $( '#smush-back-images-list' ),

    init : function(){
    this.fetch( 0 );
    $( '#smush-back-deleter' ).on( 'click', this.delete_images );
    },

    fetch : function( offset ){

    var data = {
    action: 'wpmudev_fetch_bak_images',
    security: '<?php echo wp_create_nonce( "wpmudev_fetch_bak_images" ); ?>',
    offset: offset
    };

    $.post( ajaxurl, data, function(response) {

    if( response.success ){
    Smush_Orig_Images.list( response.list );

    if ( response.offset ) {
    Smush_Orig_Images.fetch( response.offset );
    }
    else {
    Smush_Orig_Images.fetch_done();
    }

    }

    });

    },

    fetch_done : function() {
    $( '#smush-spinner' ).hide( 300 );
    $( '#smush-back-images-del-button-wrap' ).show( 300 );
    $( '#smush-back-check-all' ).on( 'click', this.toggle_checkboxes );
    },

    toggle_checkboxes : function() {
    let checkboxes = $( '#smush-back-images-list .single-back-chkbx' );

    if ( $( this ).prop('checked') ) {
    checkboxes.each(function(){
    $(this).prop('checked', true);
    });
    } else {
    checkboxes.each(function(){
    $(this).prop('checked', false);
    });
    }

    },

    list : function( list ){
    this.list_container.append( list );
    },

    delete_images : function() {

    let chosen_images = $( 'input.single-back-chkbx:checked' ),
    images_holder = [];

    chosen_images.each(function(){
    images_holder.push( $( this ).val() );
    });

    Smush_Orig_Images.process_deletion( images_holder );

    },

    process_deletion : function( images ) {

    if (typeof images !== 'undefined' && images.length > 0) {
    let attachment_id = images[0],
    bak_path = $( '#back-item-' + attachment_id ).data( 'bak-path' );

    var data = {
    action: 'wpmudev_delete_bak_image',
    security: '<?php echo wp_create_nonce( "wpmudev_delete_bak_image" ); ?>',
    attachment_id: attachment_id,
    bak_path : bak_path
    };

    $.post( ajaxurl, data, function( response ) {

    if( response.success ){
    images.shift();
    Smush_Orig_Images.process_deletion( images );
    }

    });
    } else {
    alert( 'Back images removed' );
    location.reload();
    }

    }

    }

    $( document ).ready( Smush_Orig_Images.init() );
    })(jQuery);
    </script>
    <?php

    }

    }


    if ( ! function_exists( 'wpmudev_original_images' ) ) {

    function wpmudev_original_images(){
    return WPMUDEV_Original_Images::get_instance();
    };

    add_action( 'plugins_loaded', 'wpmudev_original_images', 10 );
    }

    }