Skip to content

Commit

Permalink
Merge pull request #609 from 10up/enhancement/566
Browse files Browse the repository at this point in the history
Enhancement/566 Confirm Classification before Save
  • Loading branch information
dkotter authored Nov 21, 2023
2 parents 65cdddf + fa8b86e commit e9198e5
Show file tree
Hide file tree
Showing 20 changed files with 1,034 additions and 297 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
matrix:
core:
- {name: 'WP latest', version: 'latest'}
- {name: 'WP minimum', version: 'WordPress/WordPress#5.8'}
- {name: 'WP minimum', version: 'WordPress/WordPress#6.1'}
- {name: 'WP trunk', version: 'WordPress/WordPress#master'}
steps:
- name: Checkout
Expand Down
1 change: 1 addition & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"core": "WordPress/WordPress#6.1",
"plugins": [".", "./tests/test-plugin", "https://downloads.wordpress.org/plugin/classic-editor.zip"],
"env": {
"tests": {
Expand Down
2 changes: 1 addition & 1 deletion classifai.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Update URI: https://classifaiplugin.com
* Description: Enhance your WordPress content with Artificial Intelligence and Machine Learning services.
* Version: 2.5.0-dev
* Requires at least: 5.8
* Requires at least: 6.1
* Requires PHP: 7.4
* Author: 10up
* Author URI: https://10up.com
Expand Down
55 changes: 41 additions & 14 deletions includes/Classifai/Admin/SavePostHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use \Classifai\Providers\Azure\TextToSpeech;
use \Classifai\Watson\Normalizer;
use function Classifai\get_classification_mode;

/**
* Classifies Posts based on the current ClassifAI configuration.
Expand All @@ -20,6 +21,7 @@ class SavePostHandler {
*/
public function register() {
add_filter( 'removable_query_args', [ $this, 'classifai_removable_query_args' ] );
add_filter( 'default_post_metadata', [ $this, 'default_post_metadata' ], 10, 3 );
add_action( 'save_post', [ $this, 'did_save_post' ] );
add_action( 'admin_notices', [ $this, 'show_error_if' ] );
add_action( 'admin_post_classifai_classify_post', array( $this, 'classifai_classify_post' ) );
Expand Down Expand Up @@ -58,6 +60,28 @@ public function is_configured() {
return ! empty( get_option( 'classifai_configured' ) ) && ! empty( get_option( 'classifai_watson_nlu' )['credentials']['watson_url'] );
}

/**
* Sets the default value for the _classifai_process_content meta key.
*
* @param mixed $value The value get_metadata() should return - a single metadata value,
* or an array of values.
* @param int $object_id Object ID.
* @param string $meta_key Meta key.
*
* @return mixed
*/
public function default_post_metadata( $value, $object_id, $meta_key ) {
if ( '_classifai_process_content' === $meta_key ) {
if ( 'automatic_classification' === get_classification_mode() ) {
return 'yes';
} else {
return 'no';
}
}

return $value;
}

/**
* If current post type support is enabled in ClassifAI settings, it
* is tagged using the IBM Watson classification result.
Expand Down Expand Up @@ -107,11 +131,12 @@ public function did_save_post( $post_id ) {
* Classifies the post specified with the PostClassifier object.
* Existing terms relationships are removed before classification.
*
* @param int $post_id the post to classify & link
* @param int $post_id the post to classify & link.
* @param bool $link_terms Whether to link the terms to the post.
*
* @return array
*/
public function classify( $post_id ) {
public function classify( $post_id, $link_terms = true ) {
/**
* Filter whether ClassifAI should classify a post.
*
Expand All @@ -133,23 +158,25 @@ public function classify( $post_id ) {

$classifier = $this->get_classifier();

if ( \Classifai\get_feature_enabled( 'category' ) ) {
wp_delete_object_term_relationships( $post_id, \Classifai\get_feature_taxonomy( 'category' ) );
}
if ( $link_terms ) {
if ( \Classifai\get_feature_enabled( 'category' ) ) {
wp_delete_object_term_relationships( $post_id, \Classifai\get_feature_taxonomy( 'category' ) );
}

if ( \Classifai\get_feature_enabled( 'keyword' ) ) {
wp_delete_object_term_relationships( $post_id, \Classifai\get_feature_taxonomy( 'keyword' ) );
}
if ( \Classifai\get_feature_enabled( 'keyword' ) ) {
wp_delete_object_term_relationships( $post_id, \Classifai\get_feature_taxonomy( 'keyword' ) );
}

if ( \Classifai\get_feature_enabled( 'concept' ) ) {
wp_delete_object_term_relationships( $post_id, \Classifai\get_feature_taxonomy( 'concept' ) );
}
if ( \Classifai\get_feature_enabled( 'concept' ) ) {
wp_delete_object_term_relationships( $post_id, \Classifai\get_feature_taxonomy( 'concept' ) );
}

if ( \Classifai\get_feature_enabled( 'entity' ) ) {
wp_delete_object_term_relationships( $post_id, \Classifai\get_feature_taxonomy( 'entity' ) );
if ( \Classifai\get_feature_enabled( 'entity' ) ) {
wp_delete_object_term_relationships( $post_id, \Classifai\get_feature_taxonomy( 'entity' ) );
}
}

$output = $classifier->classify_and_link( $post_id );
$output = $classifier->classify_and_link( $post_id, [], $link_terms );

if ( is_wp_error( $output ) ) {
update_post_meta(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { list, grid } from '@wordpress/icons';
import { useSelect } from '@wordpress/data';
import ServerSideRender from '@wordpress/server-side-render';
import TaxonomyControls from './inspector-controls/taxonomy-controls';
import TaxonomyControls from '../../../../src/js/taxonomy-controls';
import { usePostTypes } from './utils';

/**
Expand Down

This file was deleted.

29 changes: 28 additions & 1 deletion includes/Classifai/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Classifai\Admin\UserProfile;
use Classifai\Providers\Provider;
use Classifai\Providers\Azure;
use Classifai\Providers\Watson\NLU;
use Classifai\Services\Service;
use Classifai\Services\ServicesManager;
use WP_Error;
Expand Down Expand Up @@ -127,7 +128,6 @@ function reset_plugin_settings() {
}
}


/**
* Returns the currently configured Watson API URL. Lookup order is,
*
Expand Down Expand Up @@ -171,6 +171,33 @@ function get_watson_username() {
}
}

/**
* Get Classification mode.
*
* @since 2.5.0
*
* @return string
*/
function get_classification_mode() {
$provider = new NLU( 'Natural Language Understanding' );
$settings = get_plugin_settings( 'language_processing', 'Natural Language Understanding' );
$value = isset( $settings['classification_mode'] ) ? $settings['classification_mode'] : '';

if ( $provider->is_configured() ) {
if ( empty( $value ) ) {
// existing users
// default: automatic_classification
return 'automatic_classification';
}
} else {
// new users
// default: manual_review
return 'manual_review';
}

return $value;
}

/**
* Returns the currently configured Watson username. Lookup order is,
*
Expand Down
17 changes: 12 additions & 5 deletions includes/Classifai/PostClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,21 @@ public function classify( $post_id, $opts = [] ) {
*
* @param int $post_id The post to classify
* @param array $opts The classification options
* @param bool $link_terms Whether to link the terms or not.
*
* @return array
*/
public function classify_and_link( $post_id, $opts = [] ) {
public function classify_and_link( $post_id, $opts = [], $link_terms = true ) {
$output = $this->classify( $post_id, $opts );

if ( is_wp_error( $output ) ) {
return $output;
} elseif ( empty( $output ) ) {
return false;
} else {
$this->link( $post_id, $output, $opts );
return $output;
$link_output = $this->link( $post_id, $output, $opts, $link_terms );

return $link_terms ? $output : $link_output;
}
}

Expand All @@ -89,10 +92,14 @@ public function classify_and_link( $post_id, $opts = [] ) {
* @param int $post_id The post id.
* @param array $output The classification results from Watson NLU.
* @param array $opts Link options.
* @param bool $link_terms Whether to link the terms or not.
*
* @return array The linked output.
*/
public function link( $post_id, $output, $opts = [] ) {
public function link( $post_id, $output, $opts = [], $link_terms = true ) {
$linker = $this->get_linker();
$linker->link( $post_id, $output, $opts );

return $linker->link( $post_id, $output, $opts, $link_terms );
}

/* helpers */
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Providers/OpenAI/Embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ public function render_metabox( $post ) {
<p>
<label for="classifai-process-content" class="classifai-preview-toggle">
<input type="checkbox" value="yes" name="_classifai_process_content" id="classifai-process-content" <?php echo esc_html( $checked ); ?> />
<strong><?php esc_html_e( 'Process content on update', 'classifai' ); ?></strong>
<strong><?php esc_html_e( 'Automatically tag content on update', 'classifai' ); ?></strong>
</label>
</p>
</div>
Expand Down
Loading

0 comments on commit e9198e5

Please sign in to comment.