Skip to content

Commit

Permalink
Merge pull request #709 from 10up/feature/671
Browse files Browse the repository at this point in the history
Fully merge the Watson and Embeddings classification features
  • Loading branch information
dkotter authored Feb 19, 2024
2 parents fc64f71 + 420d2f0 commit 4d02df0
Show file tree
Hide file tree
Showing 23 changed files with 1,602 additions and 2,067 deletions.
37 changes: 35 additions & 2 deletions includes/Classifai/Admin/BulkActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function register_language_processing_hooks() {
continue;
}

foreach ( $settings['post_types'] as $key => $post_type ) {
foreach ( $settings['post_types'] as $post_type ) {
add_filter( "bulk_actions-edit-$post_type", [ $this, 'register_language_processing_actions' ] );
add_filter( "handle_bulk_actions-edit-$post_type", [ $this, 'language_processing_actions_handler' ], 10, 3 );

Expand All @@ -107,6 +107,10 @@ public function register_language_processing_actions( array $bulk_actions ): arr
continue;
}

if ( ! in_array( get_post_type(), $feature->get_supported_post_types(), true ) ) {
continue;
}

$bulk_actions[ $feature::ID ] = $feature->get_label();

switch ( $feature::ID ) {
Expand Down Expand Up @@ -153,7 +157,25 @@ function ( $feature ) {
foreach ( $post_ids as $post_id ) {
switch ( $doaction ) {
case Classification::ID:
( new Classification() )->run( $post_id );
// Check to see if processing is disabled and overwrite that.
// Since we are manually classifying, we want to force this.
$classification_enabled = get_post_meta( $post_id, '_classifai_process_content', true );
if ( 'yes' !== $classification_enabled ) {
update_post_meta( $post_id, '_classifai_process_content', 'yes' );
}

$classification = new Classification();
$classify_results = $classification->run( $post_id, 'classify' );

// Ensure the processing value is changed back to what it was.
if ( 'yes' !== $classification_enabled ) {
update_post_meta( $post_id, '_classifai_process_content', 'no' );
}

if ( ! empty( $classify_results ) && ! is_wp_error( $classify_results ) ) {
$classification->save( $post_id, $classify_results );
}

$action = $doaction;
break;

Expand Down Expand Up @@ -283,6 +305,17 @@ public function register_language_processing_row_action( array $actions, \WP_Pos
continue;
}

if ( ! in_array( get_post_type(), $feature->get_supported_post_types(), true ) ) {
continue;
}

if (
Classification::ID === $feature::ID &&
! in_array( get_post_status(), $feature->get_supported_post_statuses(), true )
) {
continue;
}

switch ( $feature::ID ) {
case Classification::ID:
$actions[ Classification::ID ] = sprintf(
Expand Down
53 changes: 35 additions & 18 deletions includes/Classifai/Command/ClassifaiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,17 @@ public function post( $args = [], $opts = [] ) {
$post_ids = $this->get_posts_to_classify( $opts );
}

$total = count( $post_ids );
$classifier = new PostClassifier();
$limit = $opts['limit'];
$link = $opts['link'];
$link = filter_var( $link, FILTER_VALIDATE_BOOLEAN );
$feature = new Classification();
$provider = $feature->get_feature_provider_instance();

if ( Embeddings::ID !== $provider::ID ) {
\WP_CLI::error( 'This command is only available for the IBM Watson Provider' );
}

$total = count( $post_ids );
$limit = $opts['limit'];
$link = $opts['link'];
$link = filter_var( $link, FILTER_VALIDATE_BOOLEAN );

if ( ! empty( $total ) ) {
if ( ! empty( $limit ) ) {
Expand All @@ -83,15 +89,18 @@ public function post( $args = [], $opts = [] ) {

$progress_bar->tick();

if ( $link ) {
$output = $classifier->classify_and_link( $post_id, $opts );
$results = $feature->run( $post_id, 'classify' );

if ( is_wp_error( $results ) ) {
$errors[ $post_id ] = $results;
}

if ( is_wp_error( $output ) ) {
$errors[ $post_id ] = $output;
if ( ! empty( $results ) && ! is_wp_error( $results ) ) {
if ( $link ) {
$feature->save( $post_id, $results );
} else {
$this->print( $results, $post_id );
}
} else {
$output = $classifier->classify( $post_id, $opts );
$this->print( $output, $post_id );
}
}

Expand Down Expand Up @@ -963,8 +972,8 @@ public function embeddings( $args = [], $opts = [] ) {
$embeddings = new Embeddings( false );
$opts = wp_parse_args( $opts, $defaults );
$opts['per_page'] = (int) $opts['per_page'] > 0 ? $opts['per_page'] : 100;
$allowed_post_types = $embeddings->supported_post_types();
$allowed_post_status = $embeddings->supported_post_statuses();
$allowed_post_types = $feature->get_supported_post_types();
$allowed_post_status = $feature->get_supported_post_statuses();

$count = 0;
$errors = 0;
Expand Down Expand Up @@ -1015,9 +1024,13 @@ public function embeddings( $args = [], $opts = [] ) {

foreach ( $posts as $post_id ) {
if ( ! $dry_run ) {
$result = $feature->run( $post_id );
$results = $feature->run( $post_id, 'classify' );

if ( is_wp_error( $result ) ) {
if ( ! empty( $results ) && ! is_wp_error( $results ) ) {
$feature->save( $post_id, $results );
}

if ( is_wp_error( $results ) ) {
\WP_CLI::error( sprintf( 'Error while processing item ID %s', $post_id ), false );
++$errors;
}
Expand Down Expand Up @@ -1063,9 +1076,13 @@ public function embeddings( $args = [], $opts = [] ) {
}

if ( ! $dry_run ) {
$result = $feature->run( $post_id );
$results = $feature->run( $post_id, 'classify' );

if ( is_wp_error( $result ) ) {
if ( ! empty( $results ) && ! is_wp_error( $results ) ) {
$feature->save( $post_id, $results );
}

if ( is_wp_error( $results ) ) {
\WP_CLI::error( sprintf( 'Error while processing item ID %s', $post_id ), false );
++$errors;
}
Expand Down
Loading

0 comments on commit 4d02df0

Please sign in to comment.