Last active
          February 7, 2019 23:20 
        
      - 
      
 - 
        
Save imvaskii/88c8c05b5c71e4cd9c99371eac456113 to your computer and use it in GitHub Desktop.  
    S3 file path fixer. 
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| /** | |
| * Migrate local image to S3 Bucket | |
| * | |
| * @author Bhaskar K C | |
| */ | |
| $time_start = microtime( true ); | |
| // Load WP | |
| require_once dirname( __FILE__ ) . '/wp-config.php'; | |
| if ( ! is_user_logged_in() ) { | |
| wp_redirect( '/news/', 302 ); | |
| exit; | |
| } | |
| amazon_web_services_require_files(); | |
| $aws = new Amazon_Web_Services( WP_PLUGIN_DIR . '/amazon-web-services/amazon-web-services.php' ); | |
| $aws_s3 = new Amazon_S3_And_CloudFront( WP_PLUGIN_DIR . '/amazon-s3-and-cloudfront/wordpress-s3.php', $aws ); | |
| $_config['s3_bucket'] = $aws_s3->get_setting( 'bucket' ); | |
| $_config['s3_region'] = $aws_s3->get_setting( 'region' ); | |
| $_config['s3_enable_object_prefix'] = $aws_s3->get_setting( 'enable-object-prefix' ); | |
| $_config['s3_folder_prefix'] = $aws_s3->get_setting( 'object-prefix' ); | |
| $_config['s3_domain'] = $aws_s3->get_setting( 'domain' ); // cloudfront | |
| $_config['s3_cloudfront'] = $aws_s3->get_setting( 'cloudfront' ); // static.domain.com.au | |
| $_config['s3_force_https'] = $aws_s3->get_setting( 'force-https' ); | |
| $_config['protocol'] = 'https://'; | |
| $_config['prepath'] = filter_var( $_config['s3_enable_object_prefix'], FILTER_VALIDATE_BOOLEAN ) && ( 'cloudfront' === $_config['s3_domain'] ) ? $_config['s3_cloudfront'] : "{$_config['s3_bucket']}.s3.amazonaws.com"; | |
| vprintf( | |
| ' | |
| Bucket : %1$s | |
| Region : %2$s | |
| Enabled Object Prefix : %3$s | |
| Folder Prefix : %4$s | |
| Domain : %5$s | |
| Cloudfront : %6$s | |
| Force HTTPS : %7$s | |
| Protocol : %8$s | |
| Prepath : %9$s | |
| ', $_config | |
| ); | |
| global $table_prefix, $wpdb; | |
| $limit = ( PHP_SAPI != 'cli' ) && ( ! empty( $_GET['limit'] ) && is_numeric( $_GET['limit'] ) ) ? intval( $_GET['limit'] ) : 1000; | |
| $attachments = $wpdb->get_results( | |
| $wpdb->prepare( | |
| " | |
| SELECT | |
| p.ID, | |
| m2.meta_value as file, | |
| m3.meta_value as s3_info | |
| FROM | |
| wp_posts AS p | |
| LEFT JOIN wp_postmeta AS m ON | |
| m.post_id = p.ID | |
| AND m.meta_id IS NULL | |
| LEFT JOIN wp_postmeta As m2 ON | |
| m2.post_id = p.ID | |
| AND m2.meta_key = %s | |
| LEFT JOIN wp_postmeta As m3 ON | |
| m3.post_id = p.ID | |
| AND m3.meta_key = %s | |
| WHERE | |
| p.post_type = %s | |
| AND m3.meta_value IS NULL | |
| AND p.post_mime_type IN ( 'image/bmp','image/gif','image/jpeg','image/jpeg','image/jpeg','image/png','image/tiff','image/tiff') | |
| ORDER BY ID DESC | |
| LIMIT 0, %d | |
| ", | |
| [ | |
| '_wp_attached_file', | |
| 'amazonS3_info', | |
| 'attachment', | |
| $limit, | |
| ] | |
| ) | |
| ); | |
| if ( ! empty( $attachments ) ) { | |
| $tdata = ''; | |
| foreach ( $attachments as $attachment ) { | |
| if ( ! empty( $attachment->s3_info ) ) { | |
| echo "Attachment: {$attachment->ID} is already offloaded." . PHP_EOL; | |
| continue; | |
| } | |
| $file = str_replace( 'wp-content/uploads/', '', $attachment->file ); | |
| $s3_meta = [ | |
| 'bucket' => $_config['s3_bucket'], | |
| 'key' => $_config['s3_folder_prefix'] . $file, | |
| 'region' => $_config['s3_region'], | |
| ]; | |
| $wpdb->insert( | |
| 'wp_postmeta', | |
| [ | |
| 'post_id' => $attachment->ID, | |
| 'meta_key' => 'amazonS3_info', | |
| 'meta_value' => serialize( $s3_meta ), | |
| ] | |
| ); | |
| $s3_link = "<a target='_blank' href='{$_config['protocol']}{$_config['prepath']}/{$_config['s3_folder_prefix']}/{$file}'>S3 link</a>"; | |
| $tdata .= " | |
| <tr> | |
| <td>{$attachment->ID}</td> | |
| <td>{$attachment->file}</td> | |
| <td>{$s3_link}</td> | |
| </tr> | |
| "; | |
| unset( $s3_link ); | |
| unset( $s3_meta ); | |
| unset( $file ); | |
| } | |
| printf( | |
| ' | |
| <table border="1" cellspacing="0" cellpadding="6"> | |
| <tr><th>ID</th><th>file</th><th>S3 path (not verified)</th></tr> | |
| %1$s | |
| </table> | |
| ', | |
| $tdata | |
| ); | |
| } | |
| $time_end = microtime( true ); | |
| $time = $time_end - $time_start; | |
| printf( 'Execution duration : %f second(s).', $time ); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment