/**
  * @return bool
  * @throws \Exception
  */
 private function sync_blog_uploads()
 {
     $option = Plugin::get_aws_option();
     $client = Plugin::get_client($option);
     $upload_paths = wp_upload_dir();
     $blog_local_dir = $upload_paths['basedir'];
     if (is_multisite()) {
         switch_to_blog(BLOG_ID_CURRENT_SITE);
         $upload_paths = wp_upload_dir();
         $main_local_dir = $upload_paths['basedir'];
         restore_current_blog();
         $key_prefix = trim(str_replace($main_local_dir, '', $blog_local_dir), '/');
         if (empty($key_prefix)) {
             $key_prefix = null;
         }
     } else {
         $key_prefix = null;
     }
     try {
         $client->uploadDirectory($blog_local_dir, $option['bucket'], $key_prefix, array('params' => array('ACL' => 'public-read'), 'concurrency' => 20));
         return true;
     } catch (\Exception $e) {
         throw $e;
     }
 }
Exemple #2
0
 public function action_init()
 {
     if (!is_admin()) {
         return;
     }
     if (!isset($_GET['page']) || Plugin::SITE_OPTION_AWS !== $_GET['page']) {
         return;
     }
     if (!WPUtils::is_submitting()) {
         return;
     }
     if (is_multisite()) {
         $cap = 'manage_network';
     } else {
         $cap = 'administrator';
     }
     if (!current_user_can($cap)) {
         return;
     }
     if (!wp_verify_nonce($_POST[Plugin::SITE_OPTION_AWS . '_nonce'], Plugin::SITE_OPTION_AWS)) {
         return;
     }
     $option = WPUtils::trim_stripslashes_deep($_POST[Plugin::SITE_OPTION_AWS]);
     $validated = Plugin::validate_aws_option($error, $option);
     Plugin::set_aws_option($option, $validated);
     $this->message = $validated ? 'AWS S3 options updated!' : $error;
     $this->error = !$validated;
 }
Exemple #3
0
 /**
  * @param $post_id
  */
 public function action_delete_attachment($post_id)
 {
     $option = Plugin::get_aws_option();
     $client = Plugin::get_client($option);
     $attachment_meta = wp_get_attachment_metadata($post_id);
     $backup_sizes = get_post_meta($post_id, '_wp_attachment_backup_sizes', true);
     $attached = get_post_meta($post_id, '_wp_attached_file', true);
     if ($attachment_meta) {
         $full_local_path = $this->attached_file_meta_to_full_local_path($attached);
         $key_prefix = $this->full_local_path_to_key_prefix($full_local_path);
         $dir = explode('/', $attachment_meta['file']);
         $file = array_pop($dir);
         $this->delete_one($client, $option['bucket'], $key_prefix, $file);
         if (isset($attachment_meta['sizes']) && is_array($attachment_meta['sizes'])) {
             foreach ($attachment_meta['sizes'] as $size) {
                 $this->delete_one($client, $option['bucket'], $key_prefix, $size['file']);
             }
         }
         if ($backup_sizes && is_array($backup_sizes)) {
             foreach ($backup_sizes as $size) {
                 $this->delete_one($client, $option['bucket'], $key_prefix, $size['file']);
             }
         }
     }
 }