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

Addressing TODO from https://github.com/10up/classifai/issues/260 ComputerVision::reset_settings() #264

Merged
merged 8 commits into from
May 7, 2021
133 changes: 77 additions & 56 deletions includes/Classifai/Providers/Azure/ComputerVision.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
}

/**
Expand Down Expand Up @@ -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();
Copy link
Contributor

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.

Copy link
Contributor Author

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.

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(
Expand All @@ -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(
Expand All @@ -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' ),
Expand All @@ -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' ),
Expand All @@ -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' ),
Expand All @@ -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'],
]
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you explain why we should move this code here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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:

  1. Keep it as is since the settings fields order matches the default settings array order which matches the order of the serialized array.
  2. Revert the settings fields order back to the way it was, then match the default settings array order to the original settings fields order (this would just mean that we would ignore the database serialized array settings order which is fine as well since no one will really be looking at the database serialized array settings order).

Let me know what you would prefer, I am fine with going either route.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

}

/**
Expand Down