Skip to content

Commit

Permalink
Merge branch 'release/1.0.10' into legacy_master
Browse files Browse the repository at this point in the history
  • Loading branch information
BracketSpaceWorker committed Feb 23, 2021
2 parents 330d86a + d43398c commit 521a4a8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 30 deletions.
2 changes: 1 addition & 1 deletion easy-watermark.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Author URI: https://bracketspace.com/
* Text Domain: easy-watermark
* Domain Path: /languages
* Version: 1.0.9
* Version: 1.0.10
* License: GPLv3 or later
*
* @package easy-watermark
Expand Down
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tags: watermark, image, picture, photo, media, gallery, signature, transparent,
Requires at least: 4.6
Requires PHP: 5.6
Tested up to: 5.6
Stable tag: 1.0.9
Stable tag: 1.0.10
License: GPLv3 or later
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Text Domain: easy-watermark
Expand Down Expand Up @@ -118,6 +118,10 @@ All available placeholders are listed in a box titled 'Placeholders' displayed w

== Changelog ==

= 1.0.10 =
* [Added] Role existence checks during plugin activation.
* [Added] Filter to prevent applying certain watermark.

= 1.0.9 =
* [Fixed] Translated view file names causing `missing view` errors.
* [Fixed] Errors occuring on the first plugin activation.
Expand Down
43 changes: 24 additions & 19 deletions src/classes/Core/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,34 @@ public static function activate() {

$admin = get_role( 'administrator' );

$admin->add_cap( 'edit_watermark' );
$admin->add_cap( 'edit_watermarks' );
$admin->add_cap( 'edit_others_watermarks' );
$admin->add_cap( 'delete_watermarks' );
$admin->add_cap( 'delete_others_watermarks' );
$admin->add_cap( 'apply_watermark' );
if ( $admin ) {
$admin->add_cap( 'edit_watermark' );
$admin->add_cap( 'edit_watermarks' );
$admin->add_cap( 'edit_others_watermarks' );
$admin->add_cap( 'delete_watermarks' );
$admin->add_cap( 'delete_others_watermarks' );
$admin->add_cap( 'apply_watermark' );
}

$editor = get_role( 'editor' );

$editor->add_cap( 'edit_watermark' );
$editor->add_cap( 'edit_watermarks' );
$editor->add_cap( 'edit_others_watermarks' );
$editor->add_cap( 'delete_watermarks' );
$editor->add_cap( 'delete_others_watermarks' );
$editor->add_cap( 'apply_watermark' );
if ( $editor ) {
$editor->add_cap( 'edit_watermark' );
$editor->add_cap( 'edit_watermarks' );
$editor->add_cap( 'edit_others_watermarks' );
$editor->add_cap( 'delete_watermarks' );
$editor->add_cap( 'delete_others_watermarks' );
$editor->add_cap( 'apply_watermark' );
}

$author = get_role( 'author' );

$author->add_cap( 'edit_watermark' );
$author->add_cap( 'edit_watermarks' );
$author->add_cap( 'delete_watermarks' );
$author->add_cap( 'apply_watermark' );

if ( $author ) {
$author->add_cap( 'edit_watermark' );
$author->add_cap( 'edit_watermarks' );
$author->add_cap( 'delete_watermarks' );
$author->add_cap( 'apply_watermark' );
}
}

/**
Expand Down Expand Up @@ -121,6 +126,8 @@ public static function uninstall() {
*/
public static function update( $from, $defaults ) {

update_option( Plugin::get()->get_slug() . '-version', Plugin::get()->get_version() );

flush_rewrite_rules();

if ( version_compare( $from, '1.0.0', '>=' ) ) {
Expand Down Expand Up @@ -228,8 +235,6 @@ public static function update( $from, $defaults ) {

self::update_backup_info();

update_option( Plugin::get()->get_slug() . '-version', Plugin::get()->get_version() );

}

/**
Expand Down
33 changes: 24 additions & 9 deletions src/classes/Watermark/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,31 +223,43 @@ public function apply_watermarks( $attachment_id, $watermarks, $meta = [] ) {
return false;
}

$error = new WP_Error();

$this->resolver->reset();
$this->resolver->set_attachment( $attachment );

$applied_watermarks = get_post_meta( $attachment_id, '_ew_applied_watermarks', true );

if ( ! is_array( $applied_watermarks ) ) {
$applied_watermarks = [];
}

$watermarks = array_filter( $watermarks, function( $watermark ) use ( $applied_watermarks ) {
return ! array_key_exists( $watermark->ID, $applied_watermarks );
} );

if ( empty( $meta ) ) {
$meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
}

$error = new WP_Error();

if ( ! $meta ) {
$error->add( 'empty_metadata', __( 'You try to watermark an item that doesn\'t exist. Please refresh the page and try again.', 'easy-watermark' ) );

return $error;
}

$watermarks = array_filter( $watermarks, function( $watermark ) use ( $attachment_id, $attachment, $meta, $applied_watermarks ) {
if ( array_key_exists( $watermark->ID, $applied_watermarks ) ) {
return false;
}

/**
* This filter allows to programatically prevent watermarking certain
* attachment if `false` returned from attached function.
*
* @param boolean $should_apply Whether the image should be watermarked. Return false to prevent watermarking.
* @param Watermark $watermark Watermark object.
* @param integer $attachment_id Attachment ID.
* @param WP_Post $attachment Attachment post.
* @param array $meta Attachment metadata.
* @param array $applied_watermarks Array containing all watermarks applied to this attaachment.
*/
return apply_filters( 'easy-watermark/should-apply-watermark', true, $watermark, $attachment_id, $attachment, $meta, $applied_watermarks );
} );

$filepath = get_attached_file( $attachment_id );
$sizes = $meta['sizes'];
$baename = wp_basename( $meta['file'] );
Expand All @@ -257,6 +269,9 @@ public function apply_watermarks( $attachment_id, $watermarks, $meta = [] ) {
'mime-type' => $attachment->post_mime_type,
];

$this->resolver->reset();
$this->resolver->set_attachment( $attachment );

$this->do_backup( $attachment_id );

foreach ( $sizes as $size => $image ) {
Expand Down

0 comments on commit 521a4a8

Please sign in to comment.