diff --git a/easy-watermark.php b/easy-watermark.php index f684ac5..e1b5aa2 100644 --- a/easy-watermark.php +++ b/easy-watermark.php @@ -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 diff --git a/readme.txt b/readme.txt index 236c707..165ed49 100644 --- a/readme.txt +++ b/readme.txt @@ -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 @@ -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. diff --git a/src/classes/Core/Installer.php b/src/classes/Core/Installer.php index 24ef5eb..b9027d0 100644 --- a/src/classes/Core/Installer.php +++ b/src/classes/Core/Installer.php @@ -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' ); + } } /** @@ -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', '>=' ) ) { @@ -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() ); - } /** diff --git a/src/classes/Watermark/Handler.php b/src/classes/Watermark/Handler.php index 19bc89d..3c47a72 100644 --- a/src/classes/Watermark/Handler.php +++ b/src/classes/Watermark/Handler.php @@ -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'] ); @@ -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 ) {