* * @copyright (c) 2017, Incsub (http://incsub.com) */ namespace Smush\Core\Integrations; use Amazon_S3_And_CloudFront; use DeliciousBrains\WP_Offload_Media\Items\Media_Library_Item; use Smush\WP_Smush; use Smush\Core\Settings; if ( ! defined( 'WPINC' ) ) { die; } /** * Class S3 */ class S3 extends Abstract_Integration { /** * S3 constructor. */ public function __construct() { $this->module = 's3'; $this->class = 'pro'; $this->enabled = function_exists( 'as3cf_init' ) || function_exists( 'as3cf_pro_init' ); parent::__construct(); // Hook at the end of setting row to output a error div. add_action( 'smush_setting_column_right_inside', array( $this, 's3_setup_message' ), 15 ); // Do not continue if not PRO member or S3 Offload plugin is not installed. if ( ! WP_Smush::is_pro() || ! $this->enabled ) { return; } /** * FILTERS */ // Show submit button when a pro user and the S3 plugin is installed. add_filter( 'wp_smush_integration_show_submit', '__return_true' ); // Check if the backup file exists. add_filter( 'smush_backup_exists', array( $this, 'backup_exists_on_s3' ), 10, 3 ); /** * ACTIONS */ // Check if the file exists for the given path and download. add_action( 'smush_file_exists', array( $this, 'maybe_download_file' ), 10, 3 ); // Show S3 integration message, if user hasn't enabled it. add_action( 'wp_smush_header_notices', array( $this, 's3_support_required_notice' ) ); } /************************************** * * OVERWRITE PARENT CLASS FUNCTIONALITY */ /** * Filters the setting variable to add S3 setting title and description. * * @param array $settings Settings array. * * @return mixed */ public function register( $settings ) { $plugin_url = esc_url( 'https://wordpress.org/plugins/amazon-s3-and-cloudfront/' ); $settings[ $this->module ] = array( 'label' => __( 'Enable Amazon S3 support', 'wp-smushit' ), 'short_label' => __( 'Amazon S3', 'wp-smushit' ), 'desc' => sprintf( esc_html__( "Storing your image on S3 buckets using %1\$sWP Offload Media%2\$s? Smush can detect and smush those assets for you, including when you're removing files from your host server.", 'wp-smushit' ), "", '' ), ); return $settings; } /************************************** * * PUBLIC CLASSES */ /** * Check if the file is served by S3 and download the file for given path * * @param string $file_path Full file path. * @param string $attachment_id Attachment ID. * @param array $size_details Array of width and height for the image. * * @return bool|string False/ File Path */ public function maybe_download_file( $file_path = '', $attachment_id = '', $size_details = array() ) { if ( empty( $file_path ) || empty( $attachment_id ) ) { return false; } // Download if file not exists and served by S3. if ( ! file_exists( $file_path ) && $this->is_image_on_s3( $attachment_id ) ) { return $this->download_file( $attachment_id, $size_details, $file_path ); } return false; } /** * Checks if the given attachment is on S3 or not, Returns S3 URL or WP Error * * @param string $attachment_id Attachment ID. * * @return bool */ public function is_image_on_s3( $attachment_id = '' ) { /** * Amazon_S3_And_CloudFront global. * * @var Amazon_S3_And_CloudFront $as3cf */ global $as3cf; if ( empty( $attachment_id ) || ! is_object( $as3cf ) ) { return false; } $full_url = $this->is_attachment_served_by_provider( $as3cf, $attachment_id ); // If the file path contains S3, get the s3 URL for the file. return ! empty( $full_url ) ? $as3cf->get_attachment_url( $attachment_id ) : false; } /** * Checks if we've backup on S3 for the given attachment id and backup path * * @param bool $exists If backup exists on S3. * @param string $attachment_id Attachment ID. * @param string $backup_path Backup path. * * @return bool */ public function backup_exists_on_s3( $exists, $attachment_id = '', $backup_path = '' ) { // If the file is on S3, Check if backup image object exists. if ( $this->is_image_on_s3( $attachment_id ) ) { return $this->does_image_exists( $attachment_id, $backup_path ); } return $exists; } /** * Error message to show when S3 support is required. * * Show a error message to admins, if they need to enable S3 support. If "remove files from * server" option is enabled in WP Offload Media plugin, we need WP Smush Pro to enable S3 support. * * @return bool */ public function s3_support_required_notice() { // Do not display it for other users. Do not display on network screens, if network-wide option is disabled. if ( ! current_user_can( 'manage_options' ) || ! Settings::can_access( 'integrations' ) ) { return true; } // Do not display the notice on Bulk Smush Screen. global $current_screen; $allowed_pages = array( 'toplevel_page_smush', 'gallery_page_wp-smush-nextgen-bulk', 'toplevel_page_smush-network', ); if ( ! empty( $current_screen->base ) && ! in_array( $current_screen->base, $allowed_pages, true ) ) { return true; } // If already dismissed, do not show. if ( '1' === get_site_option( 'wp-smush-hide_s3support_alert' ) ) { return true; } // Return early, if support is not required. if ( ! $this->s3_support_required() ) { return true; } // Settings link. $settings_link = is_multisite() && is_network_admin() ? network_admin_url( 'admin.php?page=smush' ) : menu_page_url( 'smush', false ); if ( WP_Smush::is_pro() ) { $message = sprintf( /** * If premium user, but S3 support is not enabled. * * Translators: %1$s: opening strong tag, %2$s: closing strong tag, %s: settings link, * %3$s: opening a and strong tags, %4$s: closing a and strong tags */ __( "We can see you have WP Offload Media installed with the %1\$sRemove Files From Server%2\$s option activated. If you want to optimize your S3 images you'll need to enable the %3\$sAmazon S3 Support%4\$s feature in Smush's settings.", 'wp-smushit' ), '', '', "", '' ); } else { $message = sprintf( /** * If not a premium user. * * Translators: %1$s: opening strong tag, %2$s: closing strong tag, %s: settings link, * %3$s: opening a and strong tags, %4$s: closing a and strong tags */ __( "We can see you have WP Offload Media installed with the %1\$sRemove Files From Server%2\$s option activated. If you want to optimize your S3 images you'll need to %3\$supgrade to Smush Pro%4\$s", 'wp-smushit' ), '', '', '', '' ); } ?>
module !== $setting_key ) { return; } /** * Amazon_S3_And_CloudFront global. * * @var Amazon_S3_And_CloudFront $as3cf */ global $as3cf; $is_pro = WP_Smush::is_pro(); // If S3 integration is not enabled, return. $setting_val = $is_pro ? $this->settings->get( $this->module ) : 0; // If integration is disabled when S3 offload is active, do not continue. if ( ! $setting_val && is_object( $as3cf ) ) { return; } // If S3 offload global variable is not available, plugin is not active. if ( ! is_object( $as3cf ) ) { $class = ''; $message = __( 'To use this feature you need to install WP Offload Media and have an Amazon S3 account setup.', 'wp-smushit' ); } elseif ( ! method_exists( $as3cf, 'is_plugin_setup' ) ) { // Check if in case for some reason, we couldn't find the required function. $class = ' sui-notice-warning'; $support_url = esc_url( 'https://premium.wpmudev.org/contact' ); $message = sprintf( /* translators: %1$s: opening a tag, %2$s: closing a tag */ esc_html__( 'We are having trouble interacting with WP Offload Media, make sure the plugin is activated. Or you can %1$sreport a bug%2$s.', 'wp-smushit' ), '', '' ); } elseif ( ! $as3cf->is_plugin_setup() ) { // Plugin is not setup, or some information is missing. $class = ' sui-notice-warning'; $configure_url = $as3cf->get_plugin_page_url(); $message = sprintf( /* translators: %1$s: opening a tag, %2$s: closing a tag */ esc_html__( 'It seems you haven’t finished setting up WP Offload Media yet. %1$sConfigure it now%2$s to enable Amazon S3 support.', 'wp-smushit' ), '', '' ); } else { // S3 support is active. $class = ' sui-notice-info'; $message = __( 'Amazon S3 support is active.', 'wp-smushit' ); } // Return early if we don't need to do anything. if ( empty( $message ) || ! $is_pro ) { return; } ?>