-
Notifications
You must be signed in to change notification settings - Fork 53
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
Addressing TODO from https://github.com/10up/classifai/issues/260 ComputerVision::reset_settings() #264
Changes from 4 commits
9357cf5
d469a6d
3205866
f8c5641
38340ac
11f64cb
975b089
2ecfa98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,27 @@ public function __construct( $service ) { | |
* Resets settings for the ComputerVision provider. | ||
*/ | ||
public function reset_settings() { | ||
// TODO: Implement reset_settings() method. | ||
update_option( $this->get_option_name(), $this->get_default_settings() ); | ||
} | ||
|
||
/** | ||
* Default settings for ComputerVision | ||
* | ||
* @return array | ||
*/ | ||
private function get_default_settings() { | ||
return [ | ||
'valid' => false, | ||
'url' => '', | ||
'api_key' => '', | ||
'enable_image_captions' => true, | ||
'enable_image_tagging' => true, | ||
'enable_smart_cropping' => false, | ||
'enable_ocr' => false, | ||
'caption_threshold' => 75, | ||
'tag_threshold' => 70, | ||
'image_tag_taxonomy' => 'classifai-image-tags', | ||
]; | ||
} | ||
|
||
/** | ||
|
@@ -603,16 +623,18 @@ protected function generate_image_tags( $tags, $attachment_id ) { | |
*/ | ||
public function setup_fields_sections() { | ||
add_settings_section( $this->get_option_name(), $this->provider_service_name, '', $this->get_option_name() ); | ||
$settings = $this->get_default_settings(); | ||
add_settings_field( | ||
'url', | ||
esc_html__( 'Endpoint URL', 'classifai' ), | ||
[ $this, 'render_input' ], | ||
$this->get_option_name(), | ||
$this->get_option_name(), | ||
[ | ||
'label_for' => 'url', | ||
'input_type' => 'text', | ||
'description' => __( 'e.g. <code>https://REGION.api.cognitive.microsoft.com/</code>', 'classifai' ), | ||
'label_for' => 'url', | ||
'input_type' => 'text', | ||
'default_value' => $settings['url'], | ||
'description' => __( 'e.g. <code>https://REGION.api.cognitive.microsoft.com/</code>', 'classifai' ), | ||
] | ||
); | ||
add_settings_field( | ||
|
@@ -622,8 +644,9 @@ public function setup_fields_sections() { | |
$this->get_option_name(), | ||
$this->get_option_name(), | ||
[ | ||
'label_for' => 'api_key', | ||
'input_type' => 'password', | ||
'label_for' => 'api_key', | ||
'input_type' => 'password', | ||
'default_value' => $settings['api_key'], | ||
] | ||
); | ||
add_settings_field( | ||
|
@@ -635,23 +658,10 @@ public function setup_fields_sections() { | |
[ | ||
'label_for' => 'enable_image_captions', | ||
'input_type' => 'checkbox', | ||
'default_value' => true, | ||
'default_value' => $settings['enable_image_captions'], | ||
'description' => __( 'Images will be captioned with alt text upon upload', 'classifai' ), | ||
] | ||
); | ||
add_settings_field( | ||
'caption-threshold', | ||
esc_html__( 'Caption Confidence Threshold', 'classifai' ), | ||
[ $this, 'render_input' ], | ||
$this->get_option_name(), | ||
$this->get_option_name(), | ||
[ | ||
'label_for' => 'caption_threshold', | ||
'input_type' => 'number', | ||
'default_value' => 75, | ||
'description' => __( 'Minimum confidence score for automatically applied image captions, numeric value from 0-100. Recommended to be set to at least 75.', 'classifai' ), | ||
] | ||
); | ||
add_settings_field( | ||
'enable-image-tagging', | ||
esc_html__( 'Automatically Tag Images', 'classifai' ), | ||
|
@@ -661,42 +671,10 @@ public function setup_fields_sections() { | |
[ | ||
'label_for' => 'enable_image_tagging', | ||
'input_type' => 'checkbox', | ||
'default_value' => true, | ||
'default_value' => $settings['enable_image_tagging'], | ||
'description' => __( 'Images will be tagged upon upload', 'classifai' ), | ||
] | ||
); | ||
add_settings_field( | ||
'image-tag-threshold', | ||
esc_html__( 'Tag Confidence Threshold', 'classifai' ), | ||
[ $this, 'render_input' ], | ||
$this->get_option_name(), | ||
$this->get_option_name(), | ||
[ | ||
'label_for' => 'tag_threshold', | ||
'input_type' => 'number', | ||
'default_value' => 70, | ||
'description' => __( 'Minimum confidence score for automatically applied image tags, numeric value from 0-100. Recommended to be set to at least 70.', 'classifai' ), | ||
] | ||
); | ||
|
||
// What taxonomy should we tag images with? | ||
$attachment_taxonomies = get_object_taxonomies( 'attachment', 'objects' ); | ||
$options = []; | ||
foreach ( $attachment_taxonomies as $name => $taxonomy ) { | ||
$options[ $name ] = $taxonomy->label; | ||
} | ||
add_settings_field( | ||
'image-tag-taxonomy', | ||
esc_html__( 'Tag Taxonomy', 'classifai' ), | ||
[ $this, 'render_select' ], | ||
$this->get_option_name(), | ||
$this->get_option_name(), | ||
[ | ||
'label_for' => 'image_tag_taxonomy', | ||
'options' => $options, | ||
] | ||
); | ||
|
||
add_settings_field( | ||
'enable-smart-cropping', | ||
esc_html__( 'Enable smart cropping', 'classifai' ), | ||
|
@@ -706,14 +684,13 @@ public function setup_fields_sections() { | |
[ | ||
'label_for' => 'enable_smart_cropping', | ||
'input_type' => 'checkbox', | ||
'default_value' => false, | ||
'default_value' => $settings['enable_smart_cropping'], | ||
'description' => __( | ||
'Crop images around a region of interest identified by ComputerVision', | ||
'classifai' | ||
), | ||
] | ||
); | ||
|
||
add_settings_field( | ||
'enable-ocr', | ||
esc_html__( 'Enable OCR', 'classifai' ), | ||
|
@@ -723,13 +700,57 @@ public function setup_fields_sections() { | |
[ | ||
'label_for' => 'enable_ocr', | ||
'input_type' => 'checkbox', | ||
'default_value' => false, | ||
'default_value' => $settings['enable_ocr'], | ||
'description' => __( | ||
'Detect text in an image and store that as post content', | ||
'classifai' | ||
), | ||
] | ||
); | ||
add_settings_field( | ||
'caption-threshold', | ||
esc_html__( 'Caption Confidence Threshold', 'classifai' ), | ||
[ $this, 'render_input' ], | ||
$this->get_option_name(), | ||
$this->get_option_name(), | ||
[ | ||
'label_for' => 'caption_threshold', | ||
'input_type' => 'number', | ||
'default_value' => $settings['caption_threshold'], | ||
'description' => __( 'Minimum confidence score for automatically applied image captions, numeric value from 0-100. Recommended to be set to at least 75.', 'classifai' ), | ||
] | ||
); | ||
add_settings_field( | ||
'image-tag-threshold', | ||
esc_html__( 'Tag Confidence Threshold', 'classifai' ), | ||
[ $this, 'render_input' ], | ||
$this->get_option_name(), | ||
$this->get_option_name(), | ||
[ | ||
'label_for' => 'tag_threshold', | ||
'input_type' => 'number', | ||
'default_value' => $settings['tag_threshold'], | ||
'description' => __( 'Minimum confidence score for automatically applied image tags, numeric value from 0-100. Recommended to be set to at least 70.', 'classifai' ), | ||
] | ||
); | ||
// What taxonomy should we tag images with? | ||
$attachment_taxonomies = get_object_taxonomies( 'attachment', 'objects' ); | ||
$options = []; | ||
foreach ( $attachment_taxonomies as $name => $taxonomy ) { | ||
$options[ $name ] = $taxonomy->label; | ||
} | ||
add_settings_field( | ||
'image-tag-taxonomy', | ||
esc_html__( 'Tag Taxonomy', 'classifai' ), | ||
[ $this, 'render_select' ], | ||
$this->get_option_name(), | ||
$this->get_option_name(), | ||
[ | ||
'label_for' => 'image_tag_taxonomy', | ||
'options' => $options, | ||
'default_value' => $settings['image_tag_taxonomy'], | ||
] | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you explain why we should move this code here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to reorder the settings fields in the function to match the order that appears in the default settings array. I ordered the default settings array based on the order that was returned in the database when I created a fresh install of the plugin. I think it's best if the settings field order matches the order of the default settings field array, but I am not married to the current order of things (as I said, I just copied the order based on the serialized array that was returned in the database). In my opinion we have two routes to choose from:
Let me know what you would prefer, I am fine with going either route. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @ActuallyConnor, I'm sorry for this very late reply. I think we should keep the order of fields as they were before. The order of fields in an associated array doesn't affect the way we're using it. And by keeping the order of settings fields, our users won't be surprised after updating the plugin. Do you want to continue working on this one (and updating the default setting variable name)? If not, please let me know, I can handle the rest. |
||
} | ||
|
||
/** | ||
|
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.
We can be more explicit here,
$default_settings
sounds better to me.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.
Sounds good, I can implement this request.