Skip to content

Commit

Permalink
Deploying version 0.9.8
Browse files Browse the repository at this point in the history
  • Loading branch information
A5hleyRich committed Nov 2, 2015
1 parent 411d17f commit a149d13
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 72 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
**Tags:** uploads, amazon, s3, mirror, admin, media, cdn, cloudfront
**Requires at least:** 3.7
**Tested up to:** 4.3
**Stable tag:** 0.9.7
**Stable tag:** 0.9.8
**License:** GPLv3

Copies files to Amazon S3 as they are uploaded to the Media Library. Optionally configure Amazon CloudFront for faster delivery.
Expand Down Expand Up @@ -67,6 +67,9 @@ This version requires PHP 5.3.3+ and the Amazon Web Services plugin

## Changelog ##

### 0.9.8 - 2015-11-02 ###
* Bug fix: Attachment URLs containing query string parameters incorrectly encoded

### 0.9.7 - 2015-10-26 ###
* Improvement: Improve compatibility with third party plugins when the _Remove Files From Server_ option is enabled
* Improvement: Fix inconsistent spacing on the WP Offload S3 settings screen
Expand Down
41 changes: 27 additions & 14 deletions classes/amazon-s3-and-cloudfront.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ function init( $plugin_file_path ) {
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( 'get_image_tag', array( $this, 'maybe_encode_get_image_tag' ), 10, 6 );
add_filter( 'wp_get_attachment_image_src', array( $this, 'maybe_encode_wp_get_attachment_image_src' ), 10, 4 );
add_filter( 'wp_prepare_attachment_for_js', array( $this, 'maybe_encode_wp_prepare_attachment_for_js' ), 10, 3 );
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( '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 );
Expand Down Expand Up @@ -1444,37 +1444,50 @@ function encode_filename_in_path( $file ) {
$url = parse_url( $file );

if ( ! isset( $url['path'] ) ) {
// Can't determine path, return original
return $file;
}

if ( in_array( $this->leading_slash_it( $url['path'] ), $this->encode_files ) ) {
// Already encoded return original file
if ( in_array( $this->normalize_file_path( $url['path'] ), $this->encode_files ) ) {
// Already encoded, return original
return $file;
}

$file_path = dirname( $file );
$file_path = ( '.' !== $file_path ) ? trailingslashit( $file_path ) : '';
$file_name = basename( $file );
$file_name = basename( $url['path'] );
$encoded_file_name = rawurlencode( $file_name );
$encoded_file_path = $file_path . $encoded_file_name;

if ( $file_name !== $encoded_file_name ) {
$encoded_url = parse_url( $encoded_file_path );
$this->encode_files[] = $this->leading_slash_it( $encoded_url['path'] );
if ( $file_name === $encoded_file_name ) {
// File name doesn't need encoding, return original
return $file;
}

$normalized_file_path = $this->normalize_file_path( $encoded_file_path );

if ( ! in_array( $normalized_file_path, $this->encode_files ) ) {
$this->encode_files[] = $normalized_file_path;
}

return $file_path . $encoded_file_name;
return str_replace( $file_name, $encoded_file_name, $file );
}

/**
* Leading slash it
* Normalize file path
*
* @param string $path
*
* @return string
* @return string mixed
*/
function leading_slash_it( $path ) {
return '/' . ltrim( $path, '/\\' );
function normalize_file_path( $path ) {
$url = parse_url( $path );

if ( isset( $url['scheme'] ) ) {
$path = str_replace( $url['scheme'] . '://', '', $path );
}

return '/' . ltrim( $path, '/' );
}

/**
Expand Down
127 changes: 103 additions & 24 deletions classes/as3cf-notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ public function add_notice( $message, $args = array() ) {
'dismissible' => true,
'inline' => false,
'flash' => true,
'only_show_to_user' => true,
'only_show_to_user' => true, // The user who has initiated an action resulting in notice. Otherwise show to all users.
'only_show_in_settings' => false,
'custom_id' => '',
'auto_p' => true, // Automatically wrap the message in a <p>
'class' => '', // Extra classes for the notice
);

$notice = array_intersect_key( array_merge( $defaults, $args ), $defaults );
Expand Down Expand Up @@ -127,7 +129,7 @@ protected function save_notice( $notice ) {
*
* @param array $notice
*/
protected function remove_notice( $notice ) {
public function remove_notice( $notice ) {
$user_id = get_current_user_id();

if ( $notice['only_show_to_user'] ) {
Expand All @@ -144,17 +146,9 @@ protected function remove_notice( $notice ) {
unset( $notices[ $notice['id'] ] );

if ( $notice['only_show_to_user'] ) {
if ( ! empty( $notices ) ) {
update_user_meta( $user_id, 'as3cf_notices', $notices );
} else {
delete_user_meta( $user_id, 'as3cf_notices' );
}
$this->update_user_meta( $user_id, 'as3cf_notices', $notices );
} else {
if ( ! empty( $notices ) ) {
set_site_transient( 'as3cf_notices', $notices );
} else {
delete_site_transient( 'as3cf_notices' );
}
$this->set_site_transient( 'as3cf_notices', $notices );
}
}
}
Expand Down Expand Up @@ -182,17 +176,12 @@ protected function dismiss_notice( $notice_id ) {
$notice = $this->find_notice_by_id( $notice_id );
if ( $notice ) {
if ( $notice['only_show_to_user'] ) {
if ( ! empty( $notices ) ) {
unset( $notices[ $notice['id'] ] );
update_user_meta( $user_id, 'as3cf_notices', $notices );
} else {
delete_user_meta( $user_id, 'as3cf_notices' );
}
$notices = get_user_meta( $user_id, 'as3cf_notices' );
unset( $notices[ $notice['id'] ] );

$this->update_user_meta( $user_id, 'as3cf_notices', $notices );
} else {
$dismissed_notices = get_user_meta( $user_id, 'as3cf_dismissed_notices', true );
if ( ! is_array( $dismissed_notices ) ) {
$dismissed_notices = array();
}
$dismissed_notices = $this->get_dismissed_notices( $user_id );

if ( ! in_array( $notice['id'], $dismissed_notices ) ) {
$dismissed_notices[] = $notice['id'];
Expand All @@ -202,14 +191,75 @@ protected function dismiss_notice( $notice_id ) {
}
}

/**
* Check if a notice has been dismissed for the current user
*
* @param null|int $user_id
*
* @return array
*/
public function get_dismissed_notices( $user_id = null ) {
if ( is_null( $user_id ) ) {
$user_id = get_current_user_id();
}

$dismissed_notices = get_user_meta( $user_id, 'as3cf_dismissed_notices', true );
if ( ! is_array( $dismissed_notices ) ) {
$dismissed_notices = array();
}

return $dismissed_notices;
}

/**
* Un-dismiss a notice for a user
*
* @param string $notice_id
* @param null|int $user_id
* @param null|array $dismissed_notices
*/
public function undismiss_notice_for_user( $notice_id, $user_id = null, $dismissed_notices = null ) {
if ( is_null( $user_id ) ) {
$user_id = get_current_user_id();
}

if ( is_null( $dismissed_notices ) ) {
$dismissed_notices = $this->get_dismissed_notices( $user_id );
}

$key = array_search( $notice_id, $dismissed_notices );
unset( $dismissed_notices[ $key ] );

$this->update_user_meta( $user_id, 'as3cf_dismissed_notices', $dismissed_notices );
}

/**
* Un-dismiss a notice for all users that have dismissed it
*
* @param string $notice_id
*/
public function undismiss_notice_for_all( $notice_id ) {
$args = array(
'meta_key' => 'as3cf_dismissed_notices',
'meta_value' => $notice_id,
'meta_compare' => 'LIKE',
);

$users = get_users( $args );

foreach( $users as $user ) {
$this->undismiss_notice_for_user( $notice_id, $user->ID );
}
}

/**
* Find a notice by it's ID
*
* @param string $notice_id
*
* @return mixed
* @return array|null
*/
protected function find_notice_by_id( $notice_id ) {
public function find_notice_by_id( $notice_id ) {
$user_id = get_current_user_id();

$user_notices = get_user_meta( $user_id, 'as3cf_notices', true );
Expand Down Expand Up @@ -321,4 +371,33 @@ public function ajax_dismiss_notice() {
$this->as3cf->end_ajax( $out );
}

/**
* Helper to update/delete user meta
*
* @param int $user_id
* @param string $key
* @param array $value
*/
protected function update_user_meta( $user_id, $key, $value ) {
if ( empty( $value ) ) {
delete_user_meta( $user_id, $key);
} else {
update_user_meta( $user_id, $key, $value );
}
}

/**
* Helper to update/delete site transient
*
* @param string $key
* @param array $value
*/
protected function set_site_transient( $key, $value ) {
if ( empty( $value ) ) {
delete_site_transient( $key );
} else {
set_site_transient( $key, $value );
}
}

}
Loading

0 comments on commit a149d13

Please sign in to comment.