) ); } } return FALSE; } /** * Get list of image handlers class name. * * @since 1.0.0 * @return array */ public function get_handler_classes() { if ( ! class_exists( 'CPP_Image_Watermark_Handler' ) ) { require Content_Protector_Pack::dir_path( 'includes/watermark/class-cpp-image-watermark-handler.php' ); } if ( ! class_exists( 'CPP_Image_Watermark_GD' ) ) { require Content_Protector_Pack::dir_path( 'includes/watermark/class-cpp-image-watermark-gd.php' ); } if ( ! class_exists( 'CPP_Image_Watermark_Imagick' ) ) { require Content_Protector_Pack::dir_path( 'includes/watermark/class-cpp-image-watermark-imagick.php' ); } return array( 'CPP_Image_Watermark_Imagick', 'CPP_Image_Watermark_GD', ); } /** * Get an instance of WP FileSystem Object * * @global WP_Filesystem_Direct $wp_filesystem WordPress Filesystem Class * * @since 1.0.0 * @return WP_Filesystem_Direct */ public function file_system_instance() { global $wp_filesystem; if ( ! function_exists( 'WP_Filesystem' ) ) { require_once ABSPATH . '/wp-admin/includes/file.php'; } WP_Filesystem( TRUE, WP_CONTENT_DIR, FALSE ); return $wp_filesystem; } /** * @param string $file relative path to the file. * @param int $attachment_id optional * * @return bool true on success. * @since 1.0.0 */ protected function add_to_delete_queue( $file, $attachment_id ) { $files = get_option( 'cpp-watermark-backups', array() ); $files[ $file ] = $attachment_id; return (bool) update_option( 'cpp-watermark-backups', array_filter( $files ) ); } /** * @hooked content-protector-pack/watermark/clear-backups * * @since 1.0.0 * @return bool true on success. */ public function clear_backup_files() { if ( ! $files = get_option( 'cpp-watermark-backups', array() ) ) { return FALSE; } $file_system = $this->file_system_instance(); $basedir = $this->upload_base_dir(); foreach ( $files as $file => $attachment_id ) { if ( $file_system->delete( $basedir . $file ) ) { unset( $files[ $file ] ); $this->backup_image_unset( $attachment_id ); } } return (bool) update_option( 'cpp-watermark-backups', array_filter( $files ) ); } /** * @hooked wp_generate_attachment_metadata * * @param array $metadata An array of attachment meta data. * @param int $attachment_id Current attachment ID. * * @since 1.0.0 * @return array */ public function save_backup_file( $metadata, $attachment_id ) { if ( $this->image_file !== $metadata['file'] ) { return $metadata; } if ( ! cpp_option( 'watermark_backup_original' ) ) { $this->add_to_delete_queue( $this->image_backup_file, $attachment_id ); } $metadata['without_watermark'] = $this->image_backup_file; $metadata['cpp_watermark'] = TRUE; return $metadata; } /** * @param int $attachment_id Current attachment ID. * * @since 1.0.0 * @return bool true on success. */ protected function backup_image_unset( $attachment_id ) { if ( ! $metadata = wp_get_attachment_metadata( $attachment_id ) ) { return FALSE; } if ( ! isset( $metadata['without_watermark'] ) ) { return FALSE; } unset( $metadata['without_watermark'] ); return (bool) wp_update_attachment_metadata( $attachment_id, $metadata ); } /** * @param array $form_fields An array of attachment form fields. * @param WP_Post $attachment The WP_Post attachment object. * * @since 1.0.0 * @return array */ public function add_manage_links( $form_fields, $attachment ) { $have_watermark = $this->has_post_thumbnail_attachment( $attachment->ID ); $form_fields['cpp_watermark_control'] = array( 'show_in_edit' => FALSE, 'tr' => '
', ); return $form_fields; } /** * @param int $attachment_id * * @return bool true if exists. */ protected function has_post_thumbnail_attachment( $attachment_id ) { if ( ! $metadata = wp_get_attachment_metadata( $attachment_id ) ) { return FALSE; } return ! empty( $metadata['cpp_watermark'] ); } /** * @hooked wp_ajax_cpp-watermark * @since 1.0.0 */ public function handle_ajax_actions() { if ( empty( $_REQUEST['status'] ) || empty( $_REQUEST['post_id'] ) ) { return; } $status = $_REQUEST['status']; $attachment_id = intval( $_REQUEST['post_id'] ); check_ajax_referer( $this->get_nonce_key( $attachment_id ), 'nonce' ); if ( ! $metadata = wp_get_attachment_metadata( $attachment_id ) ) { return FALSE; } $basedir = $this->upload_base_dir(); // Apply watermark if ( 'add' === $status ) { if ( $this->watermark( $basedir . $metadata['file'] ) ) { wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $basedir . $metadata['file'] ) ); wp_send_json_success(); } } // Remove watermark if ( 'remove' === $status ) { if ( empty( $metadata['without_watermark'] ) ) { wp_send_json_error(); } $file_system = $this->file_system_instance(); $file_system->delete( $basedir . $metadata['file'] ); if ( $file_system->move( $basedir . $metadata['without_watermark'], $basedir . $metadata['file'] ) ) { wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $basedir . $metadata['file'] ) ); wp_send_json_success(); } } wp_send_json_error(); } /** * Get security nonce for given post * * @param $post_id the post id * * @since 1.0.0 * @return string */ protected function get_nonce( $post_id ) { return wp_create_nonce( $this->get_nonce_key( $post_id ) ); } /** * Get nonce key. * * @param int $post_id * * @since 1.0.0 * @return string */ protected function get_nonce_key( $post_id ) { return 'cpp-watermark-' . $post_id; } /** * @since 1.0.0 * @return array */ protected function upload_base_dir() { $basedir = wp_get_upload_dir(); $basedir = $basedir['basedir']; return trailingslashit( $basedir ); } /** * @since 1.0.0 * @return string */ protected function watermark_file() { return get_attached_file( cpp_option( 'watermark_file' ), 'full' ); } /** * @param int $attachment_id * * @since 1.0.0 */ public function delete_backup_file( $attachment_id ) { $metadata = wp_get_attachment_metadata( $attachment_id ); if ( empty( $metadata['without_watermark'] ) ) { return; } $basedir = $this->upload_base_dir(); $this->file_system_instance()->delete( $basedir . $metadata['without_watermark'] ); } }