/** * Should filter content. * * @return bool */ protected function should_filter_content() { if ($this->as3cf->is_plugin_setup() && $this->as3cf->get_setting('serve-from-s3')) { return true; } return false; }
/** * Abstract class constructor * * @param string $plugin_file_path */ function init($plugin_file_path) { self::$plugin_page = $this->plugin_slug; $this->plugin_title = __('Offload S3', 'amazon-s3-and-cloudfront'); $this->plugin_menu_title = __('S3 and CloudFront', 'amazon-s3-and-cloudfront'); new AS3CF_Upgrade_Region_Meta($this); new AS3CF_Upgrade_File_Sizes($this); new AS3CF_Upgrade_Meta_WP_Error($this); // Plugin setup add_action('aws_admin_menu', array($this, 'admin_menu')); add_filter('plugin_action_links', array($this, 'plugin_actions_settings_link'), 10, 2); add_filter('pre_get_space_used', array($this, 'multisite_get_spaced_used')); // UI AJAX add_action('wp_ajax_as3cf-get-buckets', array($this, 'ajax_get_buckets')); add_action('wp_ajax_as3cf-save-bucket', array($this, 'ajax_save_bucket')); add_action('wp_ajax_as3cf-create-bucket', array($this, 'ajax_create_bucket')); add_action('wp_ajax_as3cf-manual-save-bucket', array($this, 'ajax_save_bucket')); add_action('wp_ajax_as3cf-get-url-preview', array($this, 'ajax_get_url_preview')); // Rewriting URLs, doesn't depend on plugin being setup add_filter('wp_get_attachment_url', array($this, 'wp_get_attachment_url'), 99, 2); add_filter('get_image_tag', array($this, 'maybe_encode_get_image_tag'), 99, 6); add_filter('wp_get_attachment_image_src', array($this, 'maybe_encode_wp_get_attachment_image_src'), 99, 4); add_filter('wp_prepare_attachment_for_js', array($this, 'maybe_encode_wp_prepare_attachment_for_js'), 99, 3); add_filter('get_attached_file', array($this, 'get_attached_file'), 10, 2); // Communication with S3, plugin needs to be setup add_filter('wp_handle_upload_prefilter', array($this, 'wp_handle_upload_prefilter'), 1); add_filter('wp_update_attachment_metadata', array($this, 'wp_update_attachment_metadata'), 110, 2); add_filter('delete_attachment', array($this, 'delete_attachment'), 20); add_filter('update_attached_file', array($this, 'update_attached_file'), 100, 2); // include compatibility code for other plugins $this->plugin_compat = new AS3CF_Plugin_Compatibility($this); load_plugin_textdomain('amazon-s3-and-cloudfront', false, dirname(plugin_basename($plugin_file_path)) . '/languages/'); // Register modal scripts and styles $this->register_modal_assets(); }
/** * Abstract class constructor * * @param string $plugin_file_path */ function init($plugin_file_path) { self::$plugin_page = $this->plugin_slug; $this->plugin_title = __('Offload S3', 'amazon-s3-and-cloudfront'); $this->plugin_menu_title = __('S3 and CloudFront', 'amazon-s3-and-cloudfront'); new AS3CF_Upgrade_Region_Meta($this); new AS3CF_Upgrade_File_Sizes($this); new AS3CF_Upgrade_Meta_WP_Error($this); add_action('aws_admin_menu', array($this, 'admin_menu')); add_action('wp_ajax_as3cf-get-buckets', array($this, 'ajax_get_buckets')); add_action('wp_ajax_as3cf-save-bucket', array($this, 'ajax_save_bucket')); add_action('wp_ajax_as3cf-create-bucket', array($this, 'ajax_create_bucket')); add_action('wp_ajax_as3cf-manual-save-bucket', array($this, 'ajax_save_bucket')); add_action('wp_ajax_as3cf-get-url-preview', array($this, 'ajax_get_url_preview')); // Admin notices add_action('admin_notices', array($this, 'maybe_show_admin_notices')); add_action('network_admin_notices', array($this, 'maybe_show_admin_notices')); add_action('shutdown', array($this, 'save_admin_notices')); add_filter('wp_get_attachment_url', array($this, 'wp_get_attachment_url'), 99, 2); add_filter('wp_handle_upload_prefilter', array($this, 'wp_handle_upload_prefilter'), 1); add_filter('wp_update_attachment_metadata', array($this, 'wp_update_attachment_metadata'), 110, 2); add_filter('wp_get_attachment_metadata', array($this, 'wp_get_attachment_metadata'), 10, 2); add_filter('delete_attachment', array($this, 'delete_attachment'), 20); add_filter('update_attached_file', array($this, 'update_attached_file'), 100, 2); add_filter('get_attached_file', array($this, 'get_attached_file'), 10, 2); add_filter('plugin_action_links', array($this, 'plugin_actions_settings_link'), 10, 2); add_filter('pre_get_space_used', array($this, 'multisite_get_spaced_used')); // include compatibility code for other plugins new AS3CF_Plugin_Compatibility($this); load_plugin_textdomain('amazon-s3-and-cloudfront', false, dirname(plugin_basename($plugin_file_path)) . '/languages/'); // Register modal scripts and styles $this->register_modal_assets(); }
/** * Helper for the above action requests * * @param int $status */ function change_status_request($status) { $session = $this->get_session(); $session['status'] = $status; $this->save_session($session); $url = $this->as3cf->get_plugin_page_url(array(), 'self'); wp_redirect($url); exit; }
/** * Download a file from S3 if the file does not exist locally and places it where * the attachment's file should be. * * @param array $s3_object * @param string $file * * @return string|bool File if downloaded, false on failure */ protected function copy_s3_file_to_server($s3_object, $file) { try { $this->as3cf->get_s3client($s3_object['region'], true)->getObject(array('Bucket' => $s3_object['bucket'], 'Key' => $s3_object['key'], 'SaveAs' => $file)); } catch (Exception $e) { error_log(sprintf(__('There was an error attempting to download the file %s from S3: %s', 'as3cf'), $s3_object['key'], $e->getMessage())); return false; } return $file; }
/** * Handler for AJAX request to dismiss a notice */ public function ajax_dismiss_notice() { $this->as3cf->verify_ajax_request(); if (!isset($_POST['notice_id']) || !($notice_id = sanitize_text_field($_POST['notice_id']))) { $out = array('error' => __('Invalid notice ID.', 'amazon-s3-and-cloudfront')); $this->as3cf->end_ajax($out); } $this->dismiss_notice($notice_id); $out = array('success' => '1'); $this->as3cf->end_ajax($out); }
/** * Register stream wrappers per region * * @param string $region * * @return mixed */ protected function register_stream_wrapper($region) { $stored_region = '' === $region ? Amazon_S3_And_CloudFront::DEFAULT_REGION : $region; if (in_array($stored_region, self::$stream_wrappers)) { return; } $client = $this->as3cf->get_s3client($region, true); $protocol = $this->get_stream_wrapper_protocol($region); // Register the region specific S3 stream wrapper to be used by plugins AS3CF_Stream_Wrapper::register($client, $protocol); self::$stream_wrappers[] = $stored_region; }
/** * Replace local URLs with S3 ones for srcset image sources * * @param array $sources * @param array $size_array * @param string $image_src * @param array $image_meta * @param int $attachment_id * * @return array */ public function wp_calculate_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id) { if (!($s3object = $this->as3cf->get_attachment_s3_info($attachment_id))) { // Attachment not uploaded to S3, abort return $sources; } foreach ($sources as $width => $source) { $filename = basename($source['url']); $size = $this->find_image_size_from_width($image_meta['sizes'], $width, $filename); $s3_url = $this->as3cf->get_attachment_s3_url($attachment_id, $s3object, null, $size, $image_meta); if (false === $s3_url || is_wp_error($s3_url)) { continue; } $sources[$width]['url'] = $s3_url; } return $sources; }
/** * Update the saved upgrade ID */ protected function update_saved_upgrade_id() { $this->as3cf->set_setting($this->settings_key, $this->upgrade_id); $this->as3cf->save_settings(); }
/** * Abstract class constructor * * @param string $plugin_file_path */ function init($plugin_file_path) { self::$plugin_page = $this->plugin_slug; $this->plugin_title = __('Offload S3 Lite', 'amazon-s3-and-cloudfront'); $this->plugin_menu_title = __('S3 and CloudFront', 'amazon-s3-and-cloudfront'); new AS3CF_Upgrade_Region_Meta($this); new AS3CF_Upgrade_File_Sizes($this); new AS3CF_Upgrade_Meta_WP_Error($this); new AS3CF_Upgrade_Content_Replace_URLs($this); new AS3CF_Upgrade_EDD_Replace_URLs($this); new AS3CF_Upgrade_Filter_Post_Excerpt($this); // Plugin setup add_action('aws_admin_menu', array($this, 'admin_menu')); add_filter('plugin_action_links', array($this, 'plugin_actions_settings_link'), 10, 2); add_filter('pre_get_space_used', array($this, 'multisite_get_spaced_used')); // display a notice when either lite or pro is automatically deactivated add_action('pre_current_active_plugins', array($this, 'plugin_deactivated_notice')); // Attachment screens/modals add_action('load-upload.php', array($this, 'load_media_assets'), 11); add_action('admin_enqueue_scripts', array($this, 'load_attachment_assets'), 11); add_action('add_meta_boxes', array($this, 'attachment_s3_meta_box')); // UI AJAX add_action('wp_ajax_as3cf-get-buckets', array($this, 'ajax_get_buckets')); add_action('wp_ajax_as3cf-save-bucket', array($this, 'ajax_save_bucket')); add_action('wp_ajax_as3cf-create-bucket', array($this, 'ajax_create_bucket')); add_action('wp_ajax_as3cf-manual-save-bucket', array($this, 'ajax_save_bucket')); add_action('wp_ajax_as3cf-get-url-preview', array($this, 'ajax_get_url_preview')); add_action('wp_ajax_as3cf_get_attachment_s3_details', array($this, 'ajax_get_attachment_s3_details')); add_action('wp_ajax_as3cf-get-diagnostic-info', array($this, 'ajax_get_diagnostic_info')); // Rewriting URLs, doesn't depend on plugin being setup add_filter('wp_get_attachment_url', array($this, 'wp_get_attachment_url'), 99, 2); add_filter('get_image_tag', array($this, 'maybe_encode_get_image_tag'), 99, 6); add_filter('wp_get_attachment_image_src', array($this, 'maybe_encode_wp_get_attachment_image_src'), 99, 4); add_filter('wp_prepare_attachment_for_js', array($this, 'maybe_encode_wp_prepare_attachment_for_js'), 99, 3); add_filter('image_get_intermediate_size', array($this, 'maybe_encode_image_get_intermediate_size'), 99, 3); add_filter('get_attached_file', array($this, 'get_attached_file'), 10, 2); // Communication with S3, plugin needs to be setup add_filter('wp_handle_upload_prefilter', array($this, 'wp_handle_upload_prefilter'), 1); add_filter('wp_update_attachment_metadata', array($this, 'wp_update_attachment_metadata'), 110, 2); add_filter('delete_attachment', array($this, 'delete_attachment'), 20); add_filter('update_attached_file', array($this, 'update_attached_file'), 100, 2); // Content filtering $this->filter_local = new AS3CF_Local_To_S3($this); $this->filter_s3 = new AS3CF_S3_To_Local($this); // include compatibility code for other plugins $this->plugin_compat = new AS3CF_Plugin_Compatibility($this); load_plugin_textdomain('amazon-s3-and-cloudfront', false, dirname(plugin_basename($plugin_file_path)) . '/languages/'); // Register modal scripts and styles $this->register_modal_assets(); }
/** * Check to see if the plugin is setup * * @return bool */ function is_plugin_setup() { if (isset($this->licence)) { if (!$this->licence->is_valid_licence()) { // Empty, invalid or expired license return false; } if ($this->licence->is_licence_over_media_limit()) { // License key over the media library total license limit return false; } } return parent::is_plugin_setup(); }
/** * */ public function rooftop_add_s3_bucket(&$error = null) { $bucket = 'rooftop.site.' . get_current_blog_id(); $bucket = apply_filters('as3cf_setting_bucket', $bucket); $amazon_webservices_path = ABSPATH . '../app/mu-plugins/amazon-web-services/amazon-web-services.php'; $wp_s3_cloudfront_path = ABSPATH . '../app/mu-plugins/amazon-s3-and-cloudfront/wordpress-s3.php'; $webservice = new Amazon_Web_Services($amazon_webservices_path); $aws = new Amazon_S3_And_CloudFront($wp_s3_cloudfront_path, $webservice); try { $aws->create_bucket($bucket, 'eu-west-1'); return true; } catch (Exception $e) { error_log('Error creating bucket S3: ' . $e->getMessage()); $error = $e; return; } }