Commit c696b8ad authored by Joe Hoyle's avatar Joe Hoyle
Browse files

Merge pull request #33 from humanmade/feature/enabler

Enable the plugin via CLI
parents d3bd79d5 c8114e95
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -96,3 +96,19 @@ set the Expires header way off in the future. For example:
define( 'S3_UPLOADS_HTTP_EXPIRES', gmdate( 'D, d M Y H:i:s', time() + (10 * 365 * 24 * 60 * 60) ) .' GMT' );
	// will expire in 10 years time
```

Default Behaviour
==========

As S3 Uploads is a plug and play plugin, activating it will start rewriting image URLs to S3, and also put
new uploads on S3. Sometimes this isn't required behaviour as a site owner may want to upload a large
amount of media to S3 using the `wp-cli` commands before enabling S3 Uploads to direct all uploads requests
to S3. In this case one can define the `S3_UPLOADS_AUTOENABLE` to `false`. For example, place the following
in your `wp-config.php`:

```PHP
define( 'S3_UPLOADS_AUTOENABLE', false );
```

To then enabled S3 Uploads rewriting, use the wp-cli command: `wp s3-uploads enable` / `wp s3-uploads disable`
to toggle the behaviour.
+33 −2
Original line number Diff line number Diff line
@@ -71,7 +71,14 @@ class S3_Uploads_WP_CLI_Command extends WP_CLI_Command {
		WP_CLI::success( 'Moved all attachment to S3. If you wish to update references in your database run: ' );
		WP_CLI::line( '' );

		$old_upload_dir = S3_Uploads::get_instance()->get_original_upload_dir();
		// Ensure things are active
		$instance = S3_Uploads::get_instance();

		if ( ! s3_uploads_enabled() ) {
			$instance->setup();
		}

		$old_upload_dir = $instance->get_original_upload_dir();
		$upload_dir = wp_upload_dir();

		WP_CLI::Line( sprintf( 'wp search-replace "%s" "%s"', $old_upload_dir['baseurl'], $upload_dir['baseurl'] ) );
@@ -85,7 +92,13 @@ class S3_Uploads_WP_CLI_Command extends WP_CLI_Command {
	 */
	public function migrate_attachment_to_s3( $args, $args_assoc ) {

		$old_upload_dir = S3_Uploads::get_instance()->get_original_upload_dir();
		// Ensure things are active
		$instance = S3_Uploads::get_instance();
		if ( ! s3_uploads_enabled() ) {
			$instance->setup();
		}

		$old_upload_dir = $instance->get_original_upload_dir();
		$upload_dir = wp_upload_dir();

		$files = array( get_post_meta( $args[0], '_wp_attached_file', true ) );
@@ -361,6 +374,24 @@ class S3_Uploads_WP_CLI_Command extends WP_CLI_Command {
		WP_CLI::success( sprintf( 'Successfully deleted %s', $prefix ) );
	}

	/**
	 * Ensable the auto-rewriting of media links to S3
	 */
	public function enable( $args, $assoc_args ) {
		update_option( 's3_uploads_enabled', 'enabled' );

		WP_CLI::success( 'Media URL rewriting enabled.' );
	}

	/**
	 * Disable the auto-rewriting of media links to S3
	 */
	public function disable( $args, $assoc_args ) {
		delete_option( 's3_uploads_enabled' );

		WP_CLI::success( 'Media URL rewriting disabled.' );
	}

	private function recurse_copy($src,$dst) {
		$dir = opendir($src);
		@mkdir($dst);
+25 −1
Original line number Diff line number Diff line
@@ -32,6 +32,30 @@ class S3_Uploads {
		$this->region = $region;
	}

	/**
	 * Setup the hooks, urls filtering etc for S3 Uploads
	 */
	public function setup() {
		$this->register_stream_wrapper();

		add_filter( 'upload_dir', array( $this, 'filter_upload_dir' ) );
		add_filter( 'wp_image_editors', array( $this, 'filter_editors' ), 9 );
		remove_filter( 'admin_notices', 'wpthumb_errors' );

		add_action( 'wp_handle_sideload_prefilter', array( $this, 'filter_sideload_move_temp_file_to_s3' ) );
	}

	/**
	 * Tear down the hooks, url filtering etc for S3 Uploads
	 */
	public function tear_down() {

		stream_wrapper_unregister( 's3' );
		remove_filter( 'upload_dir', array( $this, 'filter_upload_dir' ) );
		remove_filter( 'wp_image_editors', array( $this, 'filter_editors' ), 9 );
		remove_filter( 'wp_handle_sideload_prefilter', array( $this, 'filter_sideload_move_temp_file_to_s3' ) );
	}

	/**
	 * Register the stream wrapper for s3
	 */
+1 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
			<file>./s3-uploads.php</file>
			<exclude>
				<directory suffix=".php">./lib</directory>
				<file>./inc/class-s3-uploads-wp-cli-command.php</file>
			</exclude>
		</whitelist>
	</filter>
+23 −6
Original line number Diff line number Diff line
@@ -21,12 +21,29 @@ function s3_uploads_init() {
		return;
	}

	if ( ! s3_uploads_enabled() ) {
		return;
	}

	$instance = S3_Uploads::get_instance();
	$instance->register_stream_wrapper();
	$instance->setup();
}

	add_filter( 'upload_dir', array( $instance, 'filter_upload_dir' ) );
	add_filter( 'wp_image_editors', array( $instance, 'filter_editors' ), 9 );
	remove_filter( 'admin_notices', 'wpthumb_errors' );
/**
 * Check if URL rewriting is enabled.
 *
 * Define S3_UPLOADS_AUTOENABLE to false in your wp-config to disable, or use the
 * s3_uploads_enabled option.
 *
 * @return bool
 */
function s3_uploads_enabled() {
	// Make sure the plugin is enabled when autoenable is on
	$constant_autoenable_off = ( defined( 'S3_UPLOADS_AUTOENABLE' ) && false === S3_UPLOADS_AUTOENABLE );

	if ( $constant_autoenable_off && 'enabled' !== get_option( 's3_uploads_enabled' ) ) {                         // If the plugin is not enabled, skip
		return false;
	}

	add_action( 'wp_handle_sideload_prefilter', array( $instance, 'filter_sideload_move_temp_file_to_s3' ) );
	return true;
}
Loading