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 all commits
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
15 changes: 15 additions & 0 deletions tests/Classifai/PostClassifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ function test_it_can_link_post() {
}

function test_it_can_classify_post() {
$this->test_can_have_empty_assertion();

if ( defined( 'WATSON_USERNAME' ) && ! empty( WATSON_USERNAME ) && defined( 'WATSON_PASSWORD' ) && ! empty( WATSON_PASSWORD ) ) {
$text = 'The quick brown fox jumps over the lazy dog.';
$post_id = $this->factory->post->create( [
Expand All @@ -139,6 +141,8 @@ function test_it_can_classify_post() {
}

function test_it_can_classify_and_link_post() {
$this->test_can_have_empty_assertion();

if ( defined( 'WATSON_USERNAME' ) && ! empty( WATSON_USERNAME ) && defined( 'WATSON_PASSWORD' ) && ! empty( WATSON_PASSWORD ) ) {
$text = <<<TEXT
Albert Einstein (/ˈaɪnstaɪn/; German: [ˈalbɛɐ̯t
Expand Down Expand Up @@ -175,6 +179,8 @@ function test_it_can_classify_and_link_post() {
}

function test_it_only_classifies_configured_features() {
$this->test_can_have_empty_assertion();

if ( defined( 'WATSON_USERNAME' ) && ! empty( WATSON_USERNAME ) && defined( 'WATSON_PASSWORD' ) && ! empty( WATSON_PASSWORD ) ) {
$text = <<<TEXT
Albert Einstein (/ˈaɪnstaɪn/; German: [ˈalbɛɐ̯t
Expand Down Expand Up @@ -218,4 +224,13 @@ function test_it_only_classifies_configured_features() {
$this->assertEmpty( $actual );
}
}

/**
* Set test to not perform assertion to fix risky tests.
*/
public function test_can_have_empty_assertion() {
if ( ! defined( 'WATSON_USERNAME' ) && ! defined( 'WATSON_PASSWORD' ) ) {
$this->expectNotToPerformAssertions();
}
}
}
5 changes: 2 additions & 3 deletions tests/Classifai/Providers/Azure/ComputerVisionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ public function test_set_image_meta_data() {
// Instantiate the hooks
$this->get_computer_vision()->register();

$attachment = $this->factory->attachment->create_and_get();
wp_generate_attachment_metadata( $attachment->ID, DIR_TESTDATA .'/images/33772.jpg' );
$meta = wp_get_attachment_metadata( $attachment->ID );
$attachment = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/33772.jpg' );
$meta = wp_get_attachment_metadata( $attachment );
$this->assertNotFalse( $meta );
}
}
15 changes: 15 additions & 0 deletions tests/Classifai/Watson/APIRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function test_it_uses_local_username_if_present() {
}

function test_it_uses_constant_username_if_present() {
$this->test_can_have_empty_assertion();

if ( defined( 'WATSON_USERNAME' ) ) {
$actual = $this->request->get_username();
$this->assertEquals( WATSON_USERNAME, $actual );
Expand All @@ -38,6 +40,8 @@ function test_it_uses_local_password_if_present() {
}

function test_it_constant_password_if_present() {
$this->test_can_have_empty_assertion();

if ( defined( 'WATSON_PASSWORD' ) ) {
$actual = $this->request->get_password();
$this->assertEquals( WATSON_PASSWORD, $actual );
Expand Down Expand Up @@ -81,6 +85,8 @@ function test_it_can_add_auth_header() {
}

function test_it_can_make_an_api_request() {
$this->test_can_have_empty_assertion();

if ( defined( 'WATSON_USERNAME' ) && defined( 'WATSON_PASSWORD' ) ) {
$url = 'https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze?version=2017-02-27';
$options = [
Expand All @@ -101,4 +107,13 @@ function test_it_can_make_an_api_request() {
}
}

/**
* Set test to not perform assertion to fix risky tests.
*/
public function test_can_have_empty_assertion() {
if ( ! defined( 'WATSON_USERNAME' ) && ! defined( 'WATSON_PASSWORD' ) ) {
$this->expectNotToPerformAssertions();
}
}

}
4 changes: 4 additions & 0 deletions tests/Classifai/Watson/ClassifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ function test_it_has_a_default_request_body() {
}

function test_it_can_classify_text() {
if ( ! defined( 'WATSON_USERNAME' ) && ! defined( 'WATSON_PASSWORD' ) ) {
$this->expectNotToPerformAssertions();
}

if ( defined( 'WATSON_USERNAME' ) && ! empty( WATSON_USERNAME ) && defined( 'WATSON_PASSWORD' ) && ! empty( WATSON_PASSWORD ) ) {
$text = 'The quick brown fox jumps over the lazy dog.';
$actual = $this->classifier->classify( $text );
Expand Down