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

feat/278: Add settings fields to enable storing image captions in non-alt text image fields. #374

Merged
merged 26 commits into from
Feb 20, 2023

Conversation

Sidsector9
Copy link
Member

@Sidsector9 Sidsector9 commented Sep 30, 2022

Description of the Change

This PR adds settings to add image captions to the following places:

Screenshot 2022-11-27 at 11 58 27 PM

Closes #278

How to test the Change

  1. Test for a new installation.
  2. Test for old installation with previous setup.

Changelog Entry

Added - Ability to set alt text as image caption and image description.

Credits

Props @jeffpaul @Sidsector9

Checklist:

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests pass.

@Sidsector9 Sidsector9 self-assigned this Oct 30, 2022
@jeffpaul jeffpaul added this to the 1.9.0 milestone Nov 10, 2022
@jeffpaul
Copy link
Member

@Sidsector9 is there more work needed here or can we assign OSP to get this into review?

@Sidsector9
Copy link
Member Author

@jeffpaul few minor things I need to take care of. I'll prepare the final PR next week.

@Sidsector9 Sidsector9 requested review from a team and peterwilsoncc and removed request for a team November 27, 2022 18:32
Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a couple of notes inline, there's a bug for sites with data saved in the old format.

It would be good to add a test for the old format option of enable_image_captions in computer vision phpunit tests. I think it would start with the following:

	public function test_option_reformatting() {
		add_filter(
			'pre_get_option_classifai_computer_vision',
			function() {
				return array (
					'valid' => false,
					'url' => '',
					'api_key' => '',
					'enable_image_captions' => '1',
					'enable_image_tagging' => '1',
					'enable_smart_cropping' => 'no',
					'enable_ocr' => 'no',
					'enable_read_pdf' => 'no',
					'caption_threshold' => 75,
					'tag_threshold' => 70,
					'image_tag_taxonomy' => 'classifai-image-tags',
				);
			}
		);

		// Get the alt text formatting.
		$settings = $this->get_computer_vision()->get_alt_text_settings();

		// Some assertion to confirm ALT text is enabled.
		// Some assertion to confirm the data is an array.
		// Some assertion to confirm the defaults are as expected for the two new settings.
	}

Comment on lines 43 to 49
wp_localize_script(
'classifai-media-script',
'classifaiMediaVars',
array(
'enabledAltTextFields' => $this->provider_classes[0]->get_alt_text_settings() ? $this->provider_classes[0]->get_alt_text_settings() : array(),
)
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For settings, it's recommended to use wp_add_inline_script() instead.

The method wp_localize_script() uses does some type casting so it's not guaranteed that the data will always be in the form expected.

@peterwilsoncc
Copy link
Contributor

Sorry, noticed some additional errors:

I was sometimes getting a warning too:

Invalid argument supplied for foreach()
    content/plugins/classifai/includes/Classifai/Providers/Azure/ComputerVision.php:84
    Classifai\P\A\ComputerVision->get_alt_text_settings()
    content/plugins/classifai/includes/Classifai/Services/ImageProcessing.php:47
    Classifai\Services\ImageProcessing->enqueue_media_scripts()
    wp-includes/class-wp-hook.php:308
    do_action('admin_enqueue_scripts')
    wp-admin/admin-header.php:118

Generate alt text no longer seems appropriate here. "Generate descriptive text" could be better. This applies to the threshold option too.

Screen Shot 2022-11-28 at 3 14 54 pm

Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few notes below. I'm still seeing notices and warnings under certain circumstances that it would be good to figure out.

I think a few additional isset checks are needed, or always using the new method when it's possible. Using the new method may not be possible if you need to account for different ai providers though.

'image_tag_taxonomy' => 'classifai-image-tags',
);

/** Test with `enable_image_captions` set to `1` */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** Test with `enable_image_captions` set to `1` */
// Test with `enable_image_captions` set to `1`

A couple of things here:

  • /** indicated a docblock rather than a comment, however....
  • the WPCS for single line comments is to use // rather than /*

)
);

/** Test with `enable_image_captions` set to `no` */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** Test with `enable_image_captions` set to `no` */
// Test with `enable_image_captions` set to `no`

$enabled_fields = array();

if ( ! isset( $settings['enable_image_captions'] ) ) {
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return false;
return array();

I noticed that when no fields are set, this method sometimes returns false and sometimes an empty array. It would be good to be consistent.

This might allow you to simplify generate_alt_tags() a little as the method could assume an array and lose the repeated is_array() check.

To reproduce:

  • delete the option from the database
  • var_dump shows false
  • save the image processing options page without enabling the descriptive text for any option
  • var_dump of this method shows array()

Comment on lines 224 to 225
if ( ! is_array( $setting_index['enable_image_captions'] ) ) {
$default_value = '1' === $setting_index['enable_image_captions'] ? 'alt' : '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two lines throw a notice if you visit the page before the options are saved. To reproduce:

  • Delete the classifai_computer_vision option from the database
  • Visit the options page
  • A notice will be thrown

Saving the option then caused different errors, continue from above:

  • save the options without making any changes
  • notices are thrown on line 233 for each item in the loop:
    • Undefined index: alt
    • Undefined index: caption
    • Undefined index: description

Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple more notes inline.

PHP notices thrown in previous testing have been resolved, thank you! It would be good to add a test for the deleted option as that was causing problems earlier.

@@ -770,8 +827,8 @@ public function setup_fields_sections() {
);
add_settings_field(
'enable-image-captions',
esc_html__( 'Generate alt text', 'classifai' ),
[ $this, 'render_input' ],
esc_html__( 'Generate descriptive text', 'classifai' ),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alt text confidence threshold will need to become Descriptive text confidence threshold in the next option.

Comment on lines -13 to +14
"FormData": "readonly"
"FormData": "readonly",
"classifaiMediaVars": "readonly"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

classifaiMediaVars was removed in 1bfe51c

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peterwilsoncc the PR wasn't up for re-review. I re-added classifaiMediaVars in ba3162a

@Sidsector9 Sidsector9 marked this pull request as ready for review January 24, 2023 05:35
@Sidsector9 Sidsector9 requested review from a team, dkotter and jeffpaul as code owners January 24, 2023 05:35
Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code all looks good but one thing I noticed is that the feature branch no longer automatically adds the alt text when an image is uploaded via the block editor

I tested this after saving the image processing page using the new settings.

Develop branch

develop-branch

Feature branch

feature-branch

@Sidsector9
Copy link
Member Author

@peterwilsoncc The descriptive texts are generating for me in the block editor.

I tested drag and drop as well as uploading via the image block using the upload button.

Please watch this screencast:

classifai-278.mov

@jeffpaul jeffpaul requested review from peterwilsoncc and removed request for jeffpaul January 30, 2023 14:20
Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sidsector9 Sorry, not sure what went wrong last time. It seems to be working now.

Possibly I confused WP / the bot by using ngrok -- setting the ngrok tunnel's domain as the site's primary domain worked. I'll keep that in mind for next time.

@jeffpaul jeffpaul merged commit 117b49c into develop Feb 20, 2023
@jeffpaul jeffpaul deleted the feat/278 branch February 20, 2023 21:08
dkotter added a commit that referenced this pull request May 1, 2023
…as appropriate. While fixing that, found a few bugs that were introduced as part of #374, so fixed those up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Option to store Image Captions in alternate fields
3 participants