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

Add ability to only allow manual processing of our various image Features #862

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
20 changes: 19 additions & 1 deletion includes/Classifai/Features/DescriptiveTextGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ public function rest_endpoint_callback( WP_REST_Request $request ) {
* @return array
*/
public function generate_image_alt_tags( array $metadata, int $attachment_id ): array {
if ( ! $this->is_feature_enabled() ) {
if (
! $this->is_feature_enabled() ||
'automatic' !== $this->get_processing_mode()
) {
return $metadata;
}

Expand Down Expand Up @@ -343,6 +346,18 @@ public function get_alt_text_settings(): array {
return $enabled_fields;
}

/**
* Return the processing mode for the feature.
*
* @return string
*/
public function get_processing_mode(): string {
$settings = $this->get_settings();
$value = $settings['processing_mode'] ?? 'automatic';

return $value;
}

/**
* Get the description for the enable field.
*
Expand Down Expand Up @@ -390,6 +405,7 @@ public function get_feature_default_settings(): array {
'caption' => 0,
'description' => 0,
],
'processing_mode' => 'automatic',
'provider' => ComputerVision::ID,
];
}
Expand Down Expand Up @@ -445,6 +461,8 @@ public function sanitize_default_feature_settings( array $new_settings ): array

$new_settings['descriptive_text_fields'] = array_map( 'sanitize_text_field', $new_settings['descriptive_text_fields'] ?? $settings['descriptive_text_fields'] );

$new_settings['processing_mode'] = sanitize_text_field( $new_settings['processing_mode'] ?? $settings['processing_mode'] );

return $new_settings;
}

Expand Down
34 changes: 32 additions & 2 deletions includes/Classifai/Features/ImageCropping.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ public function rest_endpoint_callback( WP_REST_Request $request ) {
* @return array
*/
public function generate_smart_crops( array $metadata, int $attachment_id ): array {
if ( ! $this->is_feature_enabled() ) {
if (
! $this->is_feature_enabled() ||
'automatic' !== $this->get_processing_mode()
) {
return $metadata;
}

Expand Down Expand Up @@ -347,10 +350,25 @@ public function get_enable_description(): string {
*/
public function get_feature_default_settings(): array {
return [
'provider' => ComputerVision::ID,
'processing_mode' => 'automatic',
'provider' => ComputerVision::ID,
];
}

/**
* Sanitizes the default feature settings.
*
* @param array $new_settings Settings being saved.
* @return array
*/
public function sanitize_default_feature_settings( array $new_settings ): array {
$settings = $this->get_settings();

$new_settings['processing_mode'] = sanitize_text_field( $new_settings['processing_mode'] ?? $settings['processing_mode'] );

return $new_settings;
}

/**
* Provides the global WP_Filesystem_Base class instance.
*
Expand Down Expand Up @@ -380,6 +398,18 @@ public function get_wp_filesystem() {
return apply_filters( 'classifai_smart_crop_wp_filesystem', $this->wp_filesystem );
}

/**
* Return the processing mode for the feature.
*
* @return string
*/
public function get_processing_mode(): string {
$settings = $this->get_settings();
$value = $settings['processing_mode'] ?? 'automatic';

return $value;
}

/**
* Generates feature setting data required for migration from
* ClassifAI < 3.0.0 to 3.0.0
Expand Down
24 changes: 21 additions & 3 deletions includes/Classifai/Features/ImageTagsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ public function rest_endpoint_callback( WP_REST_Request $request ) {
* @return array
*/
public function generate_image_tags( array $metadata, int $attachment_id ): array {
if ( ! $this->is_feature_enabled() ) {
if (
! $this->is_feature_enabled() ||
'automatic' !== $this->get_processing_mode()
) {
return $metadata;
}

Expand Down Expand Up @@ -385,8 +388,9 @@ public function get_feature_default_settings(): array {
}

return [
'tag_taxonomy' => array_key_first( $options ),
'provider' => ComputerVision::ID,
'tag_taxonomy' => array_key_first( $options ),
'processing_mode' => 'automatic',
'provider' => ComputerVision::ID,
];
}

Expand All @@ -401,6 +405,8 @@ public function sanitize_default_feature_settings( array $new_settings ): array

$new_settings['tag_taxonomy'] = $new_settings['tag_taxonomy'] ?? $settings['tag_taxonomy'];

$new_settings['processing_mode'] = sanitize_text_field( $new_settings['processing_mode'] ?? $settings['processing_mode'] );

return $new_settings;
}

Expand Down Expand Up @@ -432,6 +438,18 @@ public function get_taxonomies( array $object_types = [] ): array {
return apply_filters( 'classifai_' . static::ID . '_setting_taxonomies', $taxonomies, $this );
}

/**
* Return the processing mode for the feature.
*
* @return string
*/
public function get_processing_mode(): string {
$settings = $this->get_settings();
$value = $settings['processing_mode'] ?? 'automatic';

return $value;
}

/**
* Generates feature setting data required for migration from
* ClassifAI < 3.0.0 to 3.0.0
Expand Down
34 changes: 32 additions & 2 deletions includes/Classifai/Features/ImageTextExtraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ public function rest_endpoint_callback( WP_REST_Request $request ) {
* @return array
*/
public function generate_ocr_text( array $metadata, int $attachment_id ): array {
if ( ! $this->is_feature_enabled() ) {
if (
! $this->is_feature_enabled() ||
'automatic' !== $this->get_processing_mode()
) {
return $metadata;
}

Expand Down Expand Up @@ -446,10 +449,37 @@ public function get_enable_description(): string {
*/
public function get_feature_default_settings(): array {
return [
'provider' => ComputerVision::ID,
'processing_mode' => 'automatic',
'provider' => ComputerVision::ID,
];
}

/**
* Sanitizes the default feature settings.
*
* @param array $new_settings Settings being saved.
* @return array
*/
public function sanitize_default_feature_settings( array $new_settings ): array {
$settings = $this->get_settings();

$new_settings['processing_mode'] = sanitize_text_field( $new_settings['processing_mode'] ?? $settings['processing_mode'] );

return $new_settings;
}

/**
* Return the processing mode for the feature.
*
* @return string
*/
public function get_processing_mode(): string {
$settings = $this->get_settings();
$value = $settings['processing_mode'] ?? 'automatic';

return $value;
}

/**
* Generates feature setting data required for migration from
* ClassifAI < 3.0.0 to 3.0.0
Expand Down
36 changes: 35 additions & 1 deletion includes/Classifai/Features/PDFTextExtraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ public function attachment_data_meta_box( \WP_Post $post ) {
* @param int $attachment_id Attachment ID.
*/
public function read_pdf( int $attachment_id ) {
if (
! $this->is_feature_enabled() ||
'automatic' !== $this->get_processing_mode()
) {
return;
}

$this->run( $attachment_id, 'read_pdf' );
}

Expand Down Expand Up @@ -269,10 +276,37 @@ public function get_enable_description(): string {
*/
public function get_feature_default_settings(): array {
return [
'provider' => ComputerVision::ID,
'processing_mode' => 'automatic',
'provider' => ComputerVision::ID,
];
}

/**
* Sanitizes the default feature settings.
*
* @param array $new_settings Settings being saved.
* @return array
*/
public function sanitize_default_feature_settings( array $new_settings ): array {
$settings = $this->get_settings();

$new_settings['processing_mode'] = sanitize_text_field( $new_settings['processing_mode'] ?? $settings['processing_mode'] );

return $new_settings;
}

/**
* Return the processing mode for the feature.
*
* @return string
*/
public function get_processing_mode(): string {
$settings = $this->get_settings();
$value = $settings['processing_mode'] ?? 'automatic';

return $value;
}

/**
* Generates feature setting data required for migration from
* ClassifAI < 3.0.0 to 3.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { useSelect, useDispatch } from '@wordpress/data';
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
import { CheckboxControl } from '@wordpress/components';
import { CheckboxControl, RadioControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

/**
Expand All @@ -13,9 +13,7 @@ import { SettingsRow } from '../settings-row';
import { STORE_NAME } from '../../data/store';

/**
* Component for Descriptive Text Generator feature settings.
*
* This component is used within the FeatureSettings component to allow users to configure the Descriptive Text Generator feature.
* Component for the Descriptive Text Generator Feature settings.
*
* @return {React.ReactElement} DescriptiveTextGeneratorSettings component.
*/
Expand All @@ -30,38 +28,68 @@ export const DescriptiveTextGeneratorSettings = () => {
caption: __( 'Image caption', 'classifai' ),
description: __( 'Image description', 'classifai' ),
};

return (
<SettingsRow
label={ __( 'Descriptive text fields', 'classifai' ) }
description={ __(
'Choose image fields where the generated text should be applied.',
'classifai'
) }
className="classifai-descriptive-text-fields"
>
{ Object.keys( options ).map( ( option ) => {
return (
<CheckboxControl
id={ option }
key={ option }
checked={
featureSettings.descriptive_text_fields?.[
option
] === option
}
label={ options[ option ] }
onChange={ ( value ) => {
setFeatureSettings( {
descriptive_text_fields: {
...featureSettings.descriptive_text_fields,
[ option ]: value ? option : '0',
},
} );
} }
__nextHasNoMarginBottom
/>
);
} ) }
</SettingsRow>
<>
<SettingsRow
label={ __( 'Descriptive text fields', 'classifai' ) }
description={ __(
'Choose image fields where the generated text should be applied.',
'classifai'
) }
className="classifai-descriptive-text-fields"
>
{ Object.keys( options ).map( ( option ) => {
return (
<CheckboxControl
id={ option }
key={ option }
checked={
featureSettings.descriptive_text_fields?.[
option
] === option
}
label={ options[ option ] }
onChange={ ( value ) => {
setFeatureSettings( {
descriptive_text_fields: {
...featureSettings.descriptive_text_fields,
[ option ]: value ? option : '0',
},
} );
} }
__nextHasNoMarginBottom
/>
);
} ) }
</SettingsRow>
<SettingsRow
label={ __( 'Processing mode', 'classifai' ) }
description={ __(
'Choose how you want images to be processed. These can be processed automatically when each image is uploaded or can instead be triggered manually on each desired image. Note if set to automatic, you can still trigger the processing manually on individual images.',
'classifai'
) }
>
<RadioControl
className="processing-mode-radio-control"
onChange={ ( value ) => {
setFeatureSettings( {
processing_mode: value,
} );
} }
options={ [
{
label: __( 'Automatically on upload', 'classifai' ),
value: 'automatic',
},
{
label: __( 'Manually trigger', 'classifai' ),
value: 'manual',
},
] }
selected={ featureSettings.processing_mode }
/>
</SettingsRow>
</>
);
};
Loading