-
Notifications
You must be signed in to change notification settings - Fork 52
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
Conversation
@Sidsector9 is there more work needed here or can we assign OSP to get this into review? |
@jeffpaul few minor things I need to take care of. I'll prepare the final PR next week. |
There was a problem hiding this 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.
}
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(), | ||
) | ||
); |
There was a problem hiding this comment.
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.
Sorry, noticed some additional errors: I was sometimes getting a warning too:
Generate alt text no longer seems appropriate here. "Generate descriptive text" could be better. This applies to the threshold option too. |
There was a problem hiding this 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` */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/** 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` */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/** 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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()
if ( ! is_array( $setting_index['enable_image_captions'] ) ) { | ||
$default_value = '1' === $setting_index['enable_image_captions'] ? 'alt' : ''; |
There was a problem hiding this comment.
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
There was a problem hiding this 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' ), |
There was a problem hiding this comment.
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.
"FormData": "readonly" | ||
"FormData": "readonly", | ||
"classifaiMediaVars": "readonly" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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 |
There was a problem hiding this 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.
…as appropriate. While fixing that, found a few bugs that were introduced as part of #374, so fixed those up
Description of the Change
This PR adds settings to add image captions to the following places:
Closes #278
How to test the Change
Changelog Entry
Credits
Props @jeffpaul @Sidsector9
Checklist: