Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update hooks priority for wp_generate_attachment_metadata #271

Merged
merged 2 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions includes/Classifai/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,29 @@ function get_largest_size_and_dimensions_image_url( $full_image, $full_url, $met

return null;
}

/**
* Allows returning modified image URL for a given attachment.
*
* @param int $post_id Post ID.
*
* @return mixed
*/
function get_modified_image_source_url( $post_id ) {
/**
* Filter to modify image source URL in order to allow scanning images,
* stored on third party storages that cannot be used by
* helper function `get_largest_acceptable_image_url()` to determine `filesize()` locally.
*
* Default is null, return filtered string to allow classifying image on external source.
*
* @since 1.6.0
* @hook classifai_generate_image_alt_tags_source_url
*
* @param {mixed} $image_url New image path for given attachment ID.
* @param {int} $post_id The ID of the attachment to be used in classification.
*
* @return {mixed} NULL or filtered URl for given attachment id.
*/
return apply_filters( 'classifai_generate_image_alt_tags_source_url', null, $post_id );
}
47 changes: 30 additions & 17 deletions includes/Classifai/Providers/Azure/ComputerVision.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use function Classifai\computer_vision_max_filesize;
use function Classifai\get_largest_acceptable_image_url;
use function Classifai\get_modified_image_source_url;

class ComputerVision extends Provider {

Expand Down Expand Up @@ -64,8 +65,8 @@ public function register() {
add_action( 'add_meta_boxes_attachment', [ $this, 'setup_attachment_meta_box' ] );
add_action( 'edit_attachment', [ $this, 'maybe_rescan_image' ] );
add_filter( 'posts_clauses', [ $this, 'filter_attachment_query_keywords' ], 10, 1 );
add_filter( 'wp_generate_attachment_metadata', [ $this, 'smart_crop_image' ], 8, 2 );
add_filter( 'wp_generate_attachment_metadata', [ $this, 'generate_image_alt_tags' ], 10, 2 );
add_filter( 'wp_generate_attachment_metadata', [ $this, 'smart_crop_image' ], 7, 2 );
add_filter( 'wp_generate_attachment_metadata', [ $this, 'generate_image_alt_tags' ], 8, 2 );
add_filter( 'posts_clauses', [ $this, 'filter_attachment_query_keywords' ], 10, 1 );

$settings = $this->get_settings();
Expand Down Expand Up @@ -233,12 +234,18 @@ public function attachment_data_meta_box( $post ) {
public function maybe_rescan_image( $attachment_id ) {
$routes = [];
$metadata = wp_get_attachment_metadata( $attachment_id );
$image_url = get_largest_acceptable_image_url(
get_attached_file( $attachment_id ),
wp_get_attachment_url( $attachment_id ),
$metadata['sizes'],
computer_vision_max_filesize()
);

// Allow rescanning image that are not stored in local storage.
$image_url = get_modified_image_source_url( $attachment_id );

if ( empty( $image_url ) || ! filter_var( $image_url, FILTER_VALIDATE_URL ) ) {
$image_url = get_largest_acceptable_image_url(
get_attached_file( $attachment_id ),
wp_get_attachment_url( $attachment_id ),
$metadata['sizes'],
computer_vision_max_filesize()
);
}

if ( filter_input( INPUT_POST, 'rescan-captions' ) ) {
$routes[] = 'alt-tags';
Expand Down Expand Up @@ -341,18 +348,19 @@ public function generate_image_alt_tags( $metadata, $attachment_id ) {
'no' !== $settings['enable_image_captions']
) {

$image_url = apply_filters( 'classifai_generate_image_alt_tags_source_url', null, $attachment_id );
// Allow scanning image that are not stored in local storage.
$image_url = get_modified_image_source_url( $attachment_id );

if ( empty( $image_url ) || ! filter_var( $image_url, FILTER_VALIDATE_URL ) ) {
if ( isset( $metadata['sizes'] ) && is_array( $metadata['sizes'] ) ) {
$image_url = get_largest_acceptable_image_url(
get_attached_file( $attachment_id ),
wp_get_attachment_url( $attachment_id, 'full' ),
wp_get_attachment_url( $attachment_id ),
$metadata['sizes'],
computer_vision_max_filesize()
);
} else {
$image_url = wp_get_attachment_url( $attachment_id, 'full' );
$image_url = wp_get_attachment_url( $attachment_id );
}
}

Expand Down Expand Up @@ -932,12 +940,17 @@ public function rest_endpoint_callback( $post_id, $route_to_call ) {
return $this->ocr_processing( $metadata, $post_id, true );
}

$image_url = get_largest_acceptable_image_url(
get_attached_file( $post_id ),
wp_get_attachment_url( $post_id ),
$metadata['sizes'],
computer_vision_max_filesize()
);
// Allow rescanning image that are not stored in local storage.
$image_url = get_modified_image_source_url( $post_id );

if ( empty( $image_url ) || ! filter_var( $image_url, FILTER_VALIDATE_URL ) ) {
$image_url = get_largest_acceptable_image_url(
get_attached_file( $post_id ),
wp_get_attachment_url( $post_id ),
$metadata['sizes'],
computer_vision_max_filesize()
);
}

if ( empty( $image_url ) ) {
return new WP_Error( 'error', esc_html__( 'Valid image size not found. Make sure the image is less than 4MB.' ) );
Expand Down