* * @copyright (c) 2016, Incsub (http://incsub.com) */ if ( ! defined( 'WPINC' ) ) { die; } /** * Class WP_Smush_Nextgen_Admin */ class WP_Smush_Nextgen_Admin extends WP_Smush_Nextgen { /** * Total image count. * * @var int $total_count */ public $total_count = 0; /** * Count of images ( Attachments ), Does not includes additional sizes that might have been created. * * @var int $smushed_count */ public $smushed_count = 0; /** * Includes the count of different sizes an image might have * * @var int $image_count */ public $image_count = 0; /** * Remaining count. * * @var int $remaining_count */ public $remaining_count = 0; /** * Super Smushed. * * @var int $super_smushed */ public $super_smushed = 0; /** * Smushed images. * * @var array $smushed */ public $smushed = array(); /** * Stores all lossless smushed IDs. * * @var array $resmush_ids */ public $resmush_ids = array(); /** * WP_Smush_Nextgen_Stats class object. * * @var WP_Smush_Nextgen_Stats */ public $ng_stats; /** * WP_Smush_Nextgen_Admin constructor. * * @param WP_Smush_Nextgen_Stats $stats Class object. */ public function __construct( WP_Smush_Nextgen_Stats $stats ) { $this->ng_stats = $stats; // Update the number of columns. add_filter( 'ngg_manage_images_number_of_columns', array( $this, 'wp_smush_manage_images_number_of_columns' ) ); // Update resmush list, if a NextGen image is deleted. add_action( 'ngg_delete_picture', array( $this, 'update_resmush_list' ) ); // Update Stats, if a NextGen image is deleted. add_action( 'ngg_delete_picture', array( $this, 'update_nextgen_stats' ) ); // Update Stats, Lists - if a NextGen Gallery is deleted. add_action( 'ngg_delete_gallery', array( $this->ng_stats, 'update_stats_cache' ) ); // Update the Super Smush count, after the smushing. add_action( 'wp_smush_image_optimised_nextgen', array( $this, 'update_lists' ), '', 2 ); } /** * Returns a column name for WP Smush. * * @param array $columns Current columns. * * @return array|string */ public function wp_smush_image_column_name( $columns ) { // Latest next gen takes string, while the earlier WP Smush plugin shows there use to be a array. if ( is_array( $columns ) ) { $columns['wp_smush_image'] = esc_html__( 'Smush', 'wp-smushit' ); } else { $columns = esc_html__( 'Smush', 'wp-smushit' ); } return $columns; } /** * Returns Smush option / Stats, depending if image is already smushed or not. * * @param string $column_name Column name. * @param object|int $id Image object or ID. * @param bool $echo Echo or return. * * @return array|bool|string */ public function wp_smush_column_options( $column_name, $id, $echo = false ) { // NExtGen Doesn't returns Column name, weird? yeah, right, it is proper because hook is called for the particular column. if ( 'wp_smush_image' === $column_name || '' === $column_name ) { // We're not using our in-house function WP_Smush_Nextgen::get_nextgen_image_from_id() // as we're already instializing the nextgen gallery object, we need $storage instance later. // Registry Object for NextGen Gallery. $registry = C_Component_Registry::get_instance(); // Gallery Storage Object. $storage = $registry->get_utility( 'I_Gallery_Storage' ); // We'll get the image object in $id itself, else fetch it using Gallery Storage. if ( is_object( $id ) ) { $image = $id; } else { // get an image object. $image = $storage->object->_image_mapper->find( $id ); } // Check if it is supported image format, get image type to do that get the absolute path. $file_path = $storage->get_image_abspath( $image, 'full' ); // Get image type from file path. $image_type = $this->get_file_type( $file_path ); // If image type not supported. if ( ! $image_type || ! in_array( $image_type, WP_Smush_Core::$mime_types, true ) ) { return; } $image->meta_data = $this->get_combined_stats( $image->meta_data ); // Check Image metadata, if smushed, print the stats or super smush button. if ( ! empty( $image->meta_data['wp_smush'] ) ) { // Echo the smush stats. return $this->ng_stats->show_stats( $image->pid, $image->meta_data['wp_smush'], $image_type, false, $echo ); } // Print the status of image, if Not smushed. return $this->set_status( $image->pid, $echo, false ); } } /** * Localize Translations And Stats */ public function localize() { $handle = 'smush-admin'; $wp_smush_msgs = array( 'resmush' => esc_html__( 'Super-Smush', 'wp-smushit' ), 'smush_now' => esc_html__( 'Smush Now', 'wp-smushit' ), 'error_in_bulk' => esc_html__( '{{smushed}}/{{total}} images were successfully compressed, {{errors}} encountered issues.', 'wp-smushit' ), 'all_resmushed' => esc_html__( 'All images are fully optimized.', 'wp-smushit' ), 'restore' => esc_html__( 'Restoring image..', 'wp-smushit' ), 'smushing' => esc_html__( 'Smushing image..', 'wp-smushit' ), 'checking' => esc_html__( 'Checking images..', 'wp-smushit' ), // Button text. 'resmush_check' => esc_html__( 'RE-CHECK IMAGES', 'wp-smushit' ), 'resmush_complete' => esc_html__( 'CHECK COMPLETE', 'wp-smushit' ), ); wp_localize_script( $handle, 'wp_smush_msgs', $wp_smush_msgs ); // If premium, Super smush allowed, all images are smushed, localize lossless smushed ids for bulk compression. if ( $resmush_ids = get_option( 'wp-smush-nextgen-resmush-list', array() ) ) { $this->resmush_ids = $resmush_ids; } // Setup image counts ( Total, Smushed, Super-smushed, Remaining ). $this->setup_image_counts(); // Get the Latest Stats. $this->stats = $this->ng_stats->get_smush_stats(); // Get the unsmushed ids, used for localized stats as well as normal localization. $unsmushed = $this->ng_stats->get_ngg_images( 'unsmushed' ); $unsmushed = ( ! empty( $unsmushed ) && is_array( $unsmushed ) ) ? array_keys( $unsmushed ) : ''; $smushed = $this->ng_stats->get_ngg_images(); $smushed = ( ! empty( $smushed ) && is_array( $smushed ) ) ? array_keys( $smushed ) : ''; $this->smushed = $smushed; if ( ! empty( $_REQUEST['ids'] ) ) { // Sanitize the ids and assign it to a variable. $this->ids = array_map( 'intval', explode( ',', $_REQUEST['ids'] ) ); } else { $this->ids = $unsmushed; } $this->super_smushed = get_option( 'wp-smush-super_smushed_nextgen', array() ); $this->super_smushed = ! empty( $this->super_smushed['ids'] ) ? $this->super_smushed['ids'] : array(); // If we have images to be resmushed, Update supersmush list. if ( ! empty( $this->resmush_ids ) && ! empty( $this->super_smushed ) ) { $this->super_smushed = array_diff( $this->super_smushed, $this->resmush_ids ); } // If supersmushedimages are more than total, clean it up. if ( count( $this->super_smushed ) > $this->total_count ) { $this->super_smushed = $this->cleanup_super_smush_data(); } // Array of all smushed, unsmushed and lossless ids. $data = array( 'count_smushed' => $this->smushed_count, 'count_supersmushed' => count( $this->super_smushed ), 'count_total' => $this->total_count, 'count_images' => $this->image_count, 'smushed' => $smushed, 'unsmushed' => $unsmushed, 'resmush' => $this->resmush_ids, ); // Add the stats to arrray. if ( ! empty( $this->stats ) && is_array( $this->stats ) ) { $data = array_merge( $data, $this->stats ); } wp_localize_script( $handle, 'wp_smushit_data', $data ); } /** * Increase the count of columns for Nextgen Gallery Manage page. * * @param int $count Current columns count. * * @return int */ public function wp_smush_manage_images_number_of_columns( $count ) { $count ++; // Add column Heading. add_filter( "ngg_manage_images_column_{$count}_header", array( $this, 'wp_smush_image_column_name' ) ); // Add Column data. add_filter( "ngg_manage_images_column_{$count}_content", array( $this, 'wp_smush_column_options' ), 10, 2 ); return $count; } /** * Set send button status * * @param $pid * @param bool $echo * @param bool $text_only * * @return string */ function set_status( $pid, $echo = true, $text_only = false ) { // the status. $status_txt = __( 'Not processed', 'wp-smushit' ); // we need to show the smush button. $show_button = true; // the button text. $button_txt = __( 'Smush', 'wp-smushit' ); if ( $text_only ) { return $status_txt; } // If we are not showing smush button, append progree bar, else it is already there. if ( ! $show_button ) { $status_txt .= WP_Smush::get_instance()->core()->mod->smush->progress_bar(); } $text = $this->column_html( $pid, $status_txt, $button_txt, $show_button, false, $echo ); if ( ! $echo ) { return $text; } } /** * Print the column html * * @param string $pid Media id * @param string $status_txt Status text * @param string $button_txt Button label * @param boolean $show_button Whether to shoe the button * * @return null */ function column_html( $pid, $status_txt = '', $button_txt = '', $show_button = true, $smushed = false, $echo = true, $wrapper = false ) { $class = $smushed ? '' : ' sui-hidden'; $html = '
' . $status_txt . '
'; $html .= wp_nonce_field( 'wp_smush_nextgen', '_wp_smush_nonce', '', false ); // if we aren't showing the button. if ( ! $show_button ) { if ( $echo ) { echo $html . WP_Smush::get_instance()->core()->mod->smush->progress_bar(); return; } else { if ( ! $smushed ) { $class = ' currently-smushing'; } else { $class = ' smushed'; } return $wrapper ? '