Skip to content

Commit

Permalink
Merge pull request #653 from 10up/feature-first-e2e
Browse files Browse the repository at this point in the history
[WIP]: Updated E2E tests for feature-first refactor
  • Loading branch information
dkotter authored Jan 30, 2024
2 parents b42700f + ebbbcc5 commit ff5b712
Show file tree
Hide file tree
Showing 27 changed files with 1,044 additions and 928 deletions.
1 change: 0 additions & 1 deletion .wp-env.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{

Check warning on line 1 in .wp-env.json

View workflow job for this annotation

GitHub Actions / eslint

File ignored by default.
"core": "WordPress/WordPress#6.1",
"plugins": [".", "./tests/test-plugin", "https://downloads.wordpress.org/plugin/classic-editor.zip"],
"env": {
"tests": {
Expand Down
10 changes: 6 additions & 4 deletions includes/Classifai/Features/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,8 @@ public function render_auto_caption_fields( array $args ) {
* @param array $args The args passed to add_settings_field
*/
public function render_radio_group( array $args = array() ) {
$setting_index = $this->get_settings();
$option_index = isset( $args['option_index'] ) ? $args['option_index'] : false;
$setting_index = $this->get_settings( $option_index );
$value = $setting_index[ $args['label_for'] ] ?? '';
$options = $args['options'] ?? [];

Expand All @@ -865,12 +866,13 @@ public function render_radio_group( array $args = array() ) {
// Render radio button.
printf(
'<p>
<label for="%1$s_%2$s_%3$s">
<input type="radio" id="%1$s_%2$s_%3$s" name="%1$s[%2$s]" value="%3$s" %4$s />
%5$s
<label for="%1$s_%3$s_%4$s">
<input type="radio" id="%1$s_%3$s_%4$s" name="%1$s%2$s[%3$s]" value="%4$s" %5$s />
%6$s
</label>
</p>',
esc_attr( $this->get_option_name() ),
$option_index ? '[' . esc_attr( $option_index ) . ']' : '',
esc_attr( $args['label_for'] ),
esc_attr( $option_value ),
checked( $value, $option_value, false ),
Expand Down
4 changes: 1 addition & 3 deletions includes/Classifai/Features/ImageCropping.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,8 @@ public function attachment_data_meta_box( \WP_Post $post ) {

/**
* Display meta data.
*
* @param \WP_Post $post The post object.
*/
public function attachment_data_meta_box_content( \WP_Post $post ) {
public function attachment_data_meta_box_content() {
$smart_crop = get_transient( 'classifai_azure_computer_vision_image_cropping_latest_response' ) ? __( 'Regenerate smart thumbnail', 'classifai' ) : __( 'Create smart thumbnail', 'classifai' );
?>

Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Features/ImageTextExtraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ImageTextExtraction extends Feature {
*
* @var string
*/
const ID = 'feature_image_to_text_generation';
const ID = 'feature_image_to_text_generator';

/**
* Constructor.
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Features/TitleGeneration.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public function sanitize_default_feature_settings( array $new_settings ): array
$settings = $this->get_settings();

$new_settings['number_of_titles'] = sanitize_number_of_responses_field( 'number_of_titles', $new_settings, $settings );
$new_settings['generate_title_prompt'] = sanitize_prompts( 'generate_excerpt_prompt', $new_settings );
$new_settings['generate_title_prompt'] = sanitize_prompts( 'generate_title_prompt', $new_settings );

return $new_settings;
}
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ function get_default_prompt( array $prompts ): ?string {
$prompt_data = array_filter(
$prompts,
function ( $prompt ) {
return isset( $prompt['default'] ) && ! $prompt['original'];
return isset( $prompt['default'] ) && $prompt['default'] && ! $prompt['original'];
}
);

Expand Down
20 changes: 20 additions & 0 deletions includes/Classifai/Providers/Azure/ComputerVision.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ public function do_read_cron( string $operation_url, int $attachment_id ) {
* @return array|WP_Error
*/
public function smart_crop_image( array $metadata, int $attachment_id ) {
if ( ! wp_attachment_is_image( $attachment_id ) ) {
return new WP_Error( 'invalid', esc_html__( 'This attachment can\'t be processed.', 'classifai' ) );
}

$feature = new ImageCropping();
$settings = $feature->get_settings( static::ID );

Expand Down Expand Up @@ -379,6 +383,10 @@ public function smart_crop_image( array $metadata, int $attachment_id ) {
* @return string|WP_Error
*/
public function ocr_processing( array $metadata = [], int $attachment_id = 0 ) {
if ( ! wp_attachment_is_image( $attachment_id ) ) {
return new WP_Error( 'invalid', esc_html__( 'This attachment can\'t be processed.', 'classifai' ) );
}

$feature = new ImageTextExtraction();
$settings = $feature->get_settings( static::ID );

Expand Down Expand Up @@ -423,6 +431,10 @@ public function ocr_processing( array $metadata = [], int $attachment_id = 0 ) {
* @return string|WP_Error
*/
public function generate_alt_tags( string $image_url, int $attachment_id ) {
if ( ! wp_attachment_is_image( $attachment_id ) ) {
return new WP_Error( 'invalid', esc_html__( 'This attachment can\'t be processed.', 'classifai' ) );
}

$feature = new DescriptiveTextGenerator();
$rtn = '';

Expand Down Expand Up @@ -515,6 +527,10 @@ public function read_pdf( int $attachment_id ) {
* @return array|WP_Error
*/
public function generate_image_tags( string $image_url, int $attachment_id ) {
if ( ! wp_attachment_is_image( $attachment_id ) ) {
return new WP_Error( 'invalid', esc_html__( 'This attachment can\'t be processed.', 'classifai' ) );
}

$rtn = [];
$feature = new ImageTagsGenerator();
$settings = $feature->get_settings( static::ID );
Expand Down Expand Up @@ -707,6 +723,10 @@ public function rest_endpoint_callback( $attachment_id, string $route_to_call =

$metadata = wp_get_attachment_metadata( $attachment_id );

if ( ! $metadata ) {
return new WP_Error( 'invalid', esc_html__( 'No valid metadata found.', 'classifai' ) );
}

switch ( $route_to_call ) {
case 'ocr':
return $this->ocr_processing( $metadata, $attachment_id );
Expand Down
21 changes: 10 additions & 11 deletions includes/Classifai/Providers/Watson/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,15 @@ function get_feature_enabled( string $classify_by ): bool {
* @return float
*/
function get_feature_threshold( string $feature ): float {
$settings = get_plugin_settings( 'language_processing', 'Natural Language Understanding' );
$threshold = 0;

if ( ! empty( $settings ) && ! empty( $settings['features'] ) ) {
if ( ! empty( $settings['features'][ $feature . '_threshold' ] ) ) {
$threshold = filter_var(
$settings['features'][ $feature . '_threshold' ],
FILTER_VALIDATE_INT
);
}
$classification_feature = new Classification();
$settings = $classification_feature->get_settings( NLU::ID );
$threshold = 0;

if ( ! empty( $settings ) && ! empty( $settings[ $feature . '_threshold' ] ) ) {
$threshold = filter_var(
$settings[ $feature . '_threshold' ],
FILTER_VALIDATE_INT
);
}

if ( empty( $threshold ) ) {
Expand All @@ -237,7 +236,7 @@ function get_feature_threshold( string $feature ): float {
* @hook classifai_feature_threshold
*
* @param {float} $threshold The threshold to use, expressed as a decimal between 0 and 1 inclusive.
* @param {string} $feature The feature in question.
* @param {string} $feature The feature in question.
*
* @return {float} The filtered threshold.
*/
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Providers/Watson/NLU.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function render_provider_fields() {
'default_value' => $settings['username'],
'input_type' => 'text',
'large' => true,
'class' => 'classifai-provider-field ' . ( $this->use_username_password() ? 'hidden' : '' ) . ' provider-scope-' . static::ID, // Important to add this.
'class' => 'classifai-provider-field ' . ( $this->use_username_password() ? 'hide-username' : '' ) . ' provider-scope-' . static::ID, // Important to add this.
]
);

Expand Down
Loading

0 comments on commit ff5b712

Please sign in to comment.