diff --git a/easy-watermark.php b/easy-watermark.php index 68f3d54..c98a943 100644 --- a/easy-watermark.php +++ b/easy-watermark.php @@ -1,12 +1,12 @@ wp_enqueue_media(); $enqueue = 'media-library'; $localize = [ diff --git a/src/classes/Core/Installer.php b/src/classes/Core/Installer.php index 5111e90..d490fef 100644 --- a/src/classes/Core/Installer.php +++ b/src/classes/Core/Installer.php @@ -107,6 +107,12 @@ public static function uninstall() { $role->remove_cap( 'apply_watermark' ); } + $watermarks = Watermark::get_all(); + + foreach ( $watermarks as $watermark ) { + $result = wp_delete_post( $watermark->ID, true ); + } + } /** @@ -198,6 +204,9 @@ public static function update( $from, $defaults ) { $defaults['general']['jpeg_quality'] = $settings['general']['jpg_quality']; } + update_option( Plugin::get()->get_slug() . '-settings', $defaults ); + update_option( Plugin::get()->get_slug() . '-version', Plugin::get()->get_version() ); + if ( isset( $settings['image']['watermark_id'] ) && ! empty( $settings['image']['watermark_id'] ) ) { self::insert_image_watermark( $watermark_defaults, $settings ); } @@ -206,9 +215,6 @@ public static function update( $from, $defaults ) { self::insert_text_watermark( $watermark_defaults, $settings ); } - update_option( Plugin::get()->get_slug() . '-settings', $defaults ); - update_option( Plugin::get()->get_slug() . '-version', Plugin::get()->get_version() ); - self::update_backup_info(); } diff --git a/src/classes/Core/Plugin.php b/src/classes/Core/Plugin.php index de63d67..1161ba8 100644 --- a/src/classes/Core/Plugin.php +++ b/src/classes/Core/Plugin.php @@ -73,7 +73,6 @@ protected function __construct() { register_activation_hook( EW_FILE_PATH, [ 'EasyWatermark\Core\Installer', 'activate' ] ); register_deactivation_hook( EW_FILE_PATH, [ 'EasyWatermark\Core\Installer', 'deactivate' ] ); - register_uninstall_hook( EW_FILE_PATH, [ 'EasyWatermark\Core\Installer', 'uninstall' ] ); if ( ! ew_dochooks_enabled() ) { add_action( 'plugins_loaded', [ $this, 'setup' ] ); @@ -82,7 +81,10 @@ protected function __construct() { $this->hook(); // Init Freemius. - ew_fs(); + $fs = ew_fs(); + + // Register uninstall hook with freemius. + $fs->add_action( 'after_uninstall', [ 'EasyWatermark\Core\Installer', 'uninstall' ] ); // Signal that SDK was initiated. do_action( 'ew_fs_loaded' ); @@ -111,13 +113,7 @@ public function setup() { $this->get_watermark_handler(); - $settings = Settings::get(); - - $last_version = get_option( $this->slug . '-version' ); - if ( $this->version !== $last_version ) { - // Version has changed. Update. - Installer::update( $last_version, $settings->get_settings() ); - } + Settings::get(); $this->setup_metaboxes(); @@ -163,6 +159,13 @@ public function init() { add_rewrite_tag( '%watermark_id%', '([0-9]+)' ); add_rewrite_tag( '%image_size%', '([^./-]+)' ); + $last_version = get_option( $this->slug . '-version' ); + if ( $this->version !== $last_version ) { + // Version has changed. Update. + $settings = Settings::get(); + Installer::update( $last_version, $settings->get_settings() ); + } + } /** diff --git a/src/classes/Dashboard/Dashboard.php b/src/classes/Dashboard/Dashboard.php index e8b12bb..129f8c4 100644 --- a/src/classes/Dashboard/Dashboard.php +++ b/src/classes/Dashboard/Dashboard.php @@ -64,7 +64,7 @@ public function add_options_page() { $this->page_hook = add_management_page( __( 'Easy Watermark', 'easy-watermark' ), __( 'Easy Watermark', 'easy-watermark' ), - 'manage_options', + 'apply_watermark', 'easy-watermark', [ $this, 'page_content' ] ); diff --git a/src/classes/Dashboard/Page.php b/src/classes/Dashboard/Page.php index 9d7ff59..54957b2 100644 --- a/src/classes/Dashboard/Page.php +++ b/src/classes/Dashboard/Page.php @@ -37,6 +37,13 @@ abstract class Page { */ protected $priority; + /** + * Permission + * + * @var string + */ + protected $permission = 'apply_watermark'; + /** * Constructor * @@ -66,10 +73,12 @@ public function __construct( $title, $slug = null, $priority = 10 ) { */ public function add_tab( $tabs ) { - $tabs[ $this->slug ] = [ - 'title' => $this->title, - 'priority' => $this->priority, - ]; + if ( current_user_can( $this->permission ) ) { + $tabs[ $this->slug ] = [ + 'title' => $this->title, + 'priority' => $this->priority, + ]; + } return $tabs; diff --git a/src/classes/Dashboard/Permissions.php b/src/classes/Dashboard/Permissions.php index f424fe6..364404a 100644 --- a/src/classes/Dashboard/Permissions.php +++ b/src/classes/Dashboard/Permissions.php @@ -18,6 +18,7 @@ class Permissions extends Page { * Constructor */ public function __construct() { + $this->permission = 'manage_options'; parent::__construct( __( 'Permissions', 'easy-watermark' ), null, 100 ); } diff --git a/src/classes/Dashboard/Settings.php b/src/classes/Dashboard/Settings.php index dc28f7c..2a32539 100644 --- a/src/classes/Dashboard/Settings.php +++ b/src/classes/Dashboard/Settings.php @@ -19,6 +19,7 @@ class Settings extends Page { * Constructor */ public function __construct() { + $this->permission = 'manage_options'; parent::__construct( __( 'Settings', 'easy-watermark' ), null, 80 ); } diff --git a/src/classes/Dashboard/Tools.php b/src/classes/Dashboard/Tools.php index fcf6b67..4b9f635 100644 --- a/src/classes/Dashboard/Tools.php +++ b/src/classes/Dashboard/Tools.php @@ -60,33 +60,26 @@ public function view_args( $args ) { return [ 'watermarks' => $handler->get_watermarks(), 'backup_count' => $backup_count, + 'attachments' => $this->get_attachments(), ]; } /** - * Prepares arguments for view - * - * @action wp_ajax_easy-watermark/tools/get-attachments + * Gets attachments available for watermarking * - * @return void + * @param string $mode Mode (watermark|restore). + * @return array */ - public function get_attachments() { - - check_ajax_referer( 'get_attachments', 'nonce' ); + private function get_attachments( $mode = 'watermark' ) { $mime_types = ImageHelper::get_available_mime_types(); - - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput - $mode = isset( $_REQUEST['mode'] ) ? $_REQUEST['mode'] : null; - - $posts = get_posts( [ + $result = []; + $posts = get_posts( [ 'post_type' => 'attachment', 'post_mime_type' => array_keys( $mime_types ), 'numberposts' => -1, ] ); - $result = []; - foreach ( $posts as $post ) { if ( get_post_meta( $post->ID, '_ew_used_as_watermark', true ) ) { // Skip images used as watermark. @@ -104,6 +97,26 @@ public function get_attachments() { ]; } + return $result; + + } + + /** + * Prepares arguments for view + * + * @action wp_ajax_easy-watermark/tools/get-attachments + * + * @return void + */ + public function ajax_get_attachments() { + + check_ajax_referer( 'get_attachments', 'nonce' ); + + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput + $mode = isset( $_REQUEST['mode'] ) ? $_REQUEST['mode'] : null; + $result = $this->get_attachments( $mode ); + wp_send_json_success( $result ); + } } diff --git a/src/classes/Features/SrcsetFilter.php b/src/classes/Features/SrcsetFilter.php index a19d72a..34fc4c7 100644 --- a/src/classes/Features/SrcsetFilter.php +++ b/src/classes/Features/SrcsetFilter.php @@ -92,6 +92,11 @@ public function register_settings( $section ) { */ public function wp_calculate_image_srcset_meta( $image_meta, $size_array, $image_src, $attachment_id ) { + if ( ! $this->switch ) { + // Don't do anything if settings have not been loaded yet. + return $image_meta; + } + if ( true === $this->switch->get_value() && isset( $image_meta['sizes'] ) && is_array( $image_meta['sizes'] ) ) { $applied_watermarks = get_post_meta( $attachment_id, '_ew_applied_watermarks', true ); diff --git a/src/classes/Settings/Settings.php b/src/classes/Settings/Settings.php index 40e85f3..511aeb7 100644 --- a/src/classes/Settings/Settings.php +++ b/src/classes/Settings/Settings.php @@ -44,7 +44,7 @@ class Settings extends Singleton { /** * Constructor */ - public function __construct() { + protected function __construct() { $this->hook(); $this->register_sections(); $this->load_settings(); diff --git a/src/classes/Watermark/PostType.php b/src/classes/Watermark/PostType.php index a24dbef..668ecb8 100644 --- a/src/classes/Watermark/PostType.php +++ b/src/classes/Watermark/PostType.php @@ -233,7 +233,7 @@ public function delete_post( $post_id ) { public function redirect( $location ) { global $post; - if ( 'watermark' === $post->post_type ) { + if ( $post && 'watermark' === $post->post_type ) { if ( false !== strpos( $location, 'untrashed=1' ) && ! $this->untrashed ) { $location = add_query_arg( [ 'ew-limited' => '1', @@ -447,7 +447,7 @@ public function wp_insert_post_data( $data, $postarr ) { if ( 'watermark' === $data['post_type'] && isset( $postarr['watermark'] ) ) { $watermark_data = Watermark::parse_params( $postarr['watermark'] ); - $data['post_content'] = wp_json_encode( $watermark_data ); + $data['post_content'] = wp_json_encode( $watermark_data, JSON_UNESCAPED_UNICODE ); $old_attachment_id = isset( $postarr['ew-previous-attachment-id'] ) ? $postarr['ew-previous-attachment-id'] : false; $new_attachment_id = $postarr['watermark']['attachment_id']; diff --git a/src/classes/Watermark/Watermark.php b/src/classes/Watermark/Watermark.php index 40b4663..1cb2bd3 100644 --- a/src/classes/Watermark/Watermark.php +++ b/src/classes/Watermark/Watermark.php @@ -102,7 +102,8 @@ public static function get( $post ) { public static function get_all() { $posts = get_posts( [ - 'post_type' => 'watermark', + 'post_type' => 'watermark', + 'numberposts' => -1, ] ); foreach ( $posts as $post ) { diff --git a/src/inc/freemius.php b/src/inc/freemius.php index 55a74da..5426991 100644 --- a/src/inc/freemius.php +++ b/src/inc/freemius.php @@ -15,7 +15,7 @@ function ew_fs() { // Include Freemius SDK. require_once EW_DIR_PATH . '/freemius/start.php'; - $ew_fs = fs_dynamic_init( array( + $ew_fs = fs_dynamic_init( [ 'id' => '2801', 'slug' => 'easy-watermark', 'type' => 'plugin', @@ -23,7 +23,16 @@ function ew_fs() { 'is_premium' => false, 'has_addons' => false, 'has_paid_plans' => false, - ) ); + 'menu' => [ + 'slug' => 'easy-watermark', + 'account' => false, + 'contact' => false, + 'support' => false, + 'parent' => [ + 'slug' => 'tools.php', + ], + ], + ] ); } return $ew_fs; diff --git a/src/inc/hooks.php b/src/inc/hooks.php index 626fc11..703f35a 100644 --- a/src/inc/hooks.php +++ b/src/inc/hooks.php @@ -49,7 +49,7 @@ add_filter( 'easy-watermark/dashboard/tabs', [ $this->objects['EasyWatermark\Dashboard\Permissions']['instance'], 'add_tab' ], 10, 1 ); add_action( 'easy-watermark/dashboard/settings/notices', [ $this->objects['EasyWatermark\Dashboard\Tools']['instance'], 'admin_notices' ], 10, 0 ); add_filter( 'easy-watermark/dashboard/tools/view-args', [ $this->objects['EasyWatermark\Dashboard\Tools']['instance'], 'view_args' ], 10, 1 ); -add_action( 'wp_ajax_easy-watermark/tools/get-attachments', [ $this->objects['EasyWatermark\Dashboard\Tools']['instance'], 'get_attachments' ], 10, 0 ); +add_action( 'wp_ajax_easy-watermark/tools/get-attachments', [ $this->objects['EasyWatermark\Dashboard\Tools']['instance'], 'ajax_get_attachments' ], 10, 0 ); add_filter( 'easy-watermark/dashboard/tabs', [ $this->objects['EasyWatermark\Dashboard\Tools']['instance'], 'add_tab' ], 10, 1 ); add_action( 'easy-watermark/settings/register/general', [ $this->objects['EasyWatermark\Features\SrcsetFilter']['instance'], 'register_settings' ], 10, 1 ); add_filter( 'wp_calculate_image_srcset_meta', [ $this->objects['EasyWatermark\Features\SrcsetFilter']['instance'], 'wp_calculate_image_srcset_meta' ], 1000, 4 ); diff --git a/src/views/dashboard/pages/tools.php b/src/views/dashboard/pages/tools.php index a4ca777..c8933f2 100644 --- a/src/views/dashboard/pages/tools.php +++ b/src/views/dashboard/pages/tools.php @@ -13,19 +13,23 @@

-

- -

-

-

+ +

+ +

+

+

+ +

+ %s', admin_url( 'post-new.php?post_type=watermark' ), esc_html_x( 'create watermark', 'link text for new watermark page', 'easy-watermark' ) ); ?> diff --git a/src/views/dashboard/pages/watermarks.php b/src/views/dashboard/pages/watermarks.php index bcdd1bf..e00984d 100644 --- a/src/views/dashboard/pages/watermarks.php +++ b/src/views/dashboard/pages/watermarks.php @@ -11,7 +11,13 @@
-

post_title ); ?>

+

+ post_author ) : ?> + post_title ); ?> + + post_title ); ?> + +

type ) : ?> attachment_id, 'full' ); ?> @@ -20,16 +26,20 @@
- - | - - + post_author ) : ?> + + | + + post_author ) : ?> + + +
- $watermarks_count ) : ?> + $watermarks_count && current_user_can( 'edit_watermarks' ) ) : ?>