Skip to content

Commit

Permalink
Merge pull request #647 from 10up/fix/646
Browse files Browse the repository at this point in the history
Ensure that the "Classify" row/bulk action is visible only to users who have access to it.
  • Loading branch information
dkotter authored Jan 9, 2024
2 parents cfed77f + 341c070 commit 7fb61a0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
57 changes: 46 additions & 11 deletions includes/Classifai/Admin/BulkActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Classifai\Providers\OpenAI\Embeddings;
use Classifai\Providers\OpenAI\Whisper;
use Classifai\Providers\OpenAI\Whisper\Transcribe;
use Classifai\Providers\Watson\NLU;

use function Classifai\get_post_types_for_language_settings;
use function Classifai\get_supported_post_types;
use function Classifai\get_tts_supported_post_types;
Expand Down Expand Up @@ -55,6 +57,11 @@ public function can_register() {
*/
private $text_to_speech;

/**
* @var \Classifai\Providers\Watson\NLU
*/
private $ibm_watson_nlu;

/**
* Register the actions needed.
*/
Expand All @@ -72,12 +79,33 @@ public function register_language_processing_hooks() {
$this->chat_gpt = new ChatGPT( false );
$this->embeddings = new Embeddings( false );
$this->text_to_speech = new TextToSpeech( false );
$this->ibm_watson_nlu = new NLU( false );

$embeddings_post_types = [];
$nlu_post_types = get_supported_post_types();
$text_to_speech_post_types = get_tts_supported_post_types();
$nlu_post_types = [];
$text_to_speech_post_types = [];
$chat_gpt_post_types = [];

// Set up the NLU post types if the feature is enabled. Otherwise clear.
if (
$this->ibm_watson_nlu &&
$this->ibm_watson_nlu->is_feature_enabled( 'content_classification' )
) {
$nlu_post_types = get_supported_post_types();
} else {
$this->ibm_watson_nlu = null;
}

// Set up the NLU post types if the feature is enabled. Otherwise clear.
if (
$this->text_to_speech &&
$this->text_to_speech->is_feature_enabled( 'content_classification' )
) {
$text_to_speech_post_types = get_tts_supported_post_types();
} else {
$this->text_to_speech = null;
}

// Set up the save post handler if we have any post types.
if ( ! empty( $nlu_post_types ) || ! empty( $text_to_speech_post_types ) ) {
$this->save_post_handler = new SavePostHandler();
Expand All @@ -100,11 +128,6 @@ public function register_language_processing_hooks() {
$this->embeddings = null;
}

// Clear our TextToSpeech handler if no post types are set up.
if ( empty( $text_to_speech_post_types ) ) {
$this->text_to_speech = null;
}

// Merge our post types together and make them unique.
$post_types = array_unique( array_merge( $chat_gpt_post_types, $embeddings_post_types, $nlu_post_types, $text_to_speech_post_types ) );

Expand Down Expand Up @@ -147,15 +170,24 @@ public function register_bulk_actions( $bulk_actions ) {
$nlu_post_types = get_supported_post_types();

if (
! empty( $nlu_post_types ) ||
( is_a( $this->embeddings, '\Classifai\Providers\OpenAI\Embeddings' ) && ! empty( $this->embeddings->supported_post_types() ) )
(
is_a( $this->ibm_watson_nlu, '\Classifai\Providers\Watson\NLU' ) &&
$this->ibm_watson_nlu->is_feature_enabled( 'content_classification' ) &&
! empty( $nlu_post_types )
) ||
(
is_a( $this->embeddings, '\Classifai\Providers\OpenAI\Embeddings' ) &&
$this->embeddings->is_feature_enabled( 'classification' ) &&
! empty( $this->embeddings->supported_post_types() )
)
) {
$bulk_actions['classify'] = __( 'Classify', 'classifai' );
}

if (
is_a( $this->chat_gpt, '\Classifai\Providers\OpenAI\ChatGPT' ) &&
in_array( get_current_screen()->post_type, array_keys( get_post_types_for_language_settings() ), true )
in_array( get_current_screen()->post_type, array_keys( get_post_types_for_language_settings() ), true ) &&
$this->chat_gpt->is_feature_enabled( 'excerpt_generation' )
) {
$bulk_actions['generate_excerpt'] = __( 'Generate excerpt', 'classifai' );
}
Expand Down Expand Up @@ -221,7 +253,10 @@ public function bulk_action_handler( $redirect_to, $doaction, $post_ids ) {
foreach ( $post_ids as $post_id ) {
if ( 'classify' === $doaction ) {
// Handle NLU classification.
if ( is_a( $this->save_post_handler, '\Classifai\Admin\SavePostHandler' ) ) {
if (
is_a( $this->ibm_watson_nlu, '\Classifai\Providers\Watson\NLU' ) &&
is_a( $this->save_post_handler, '\Classifai\Admin\SavePostHandler' )
) {
$action = 'classified';
$this->save_post_handler->classify( $post_id );
}
Expand Down
5 changes: 5 additions & 0 deletions includes/Classifai/Admin/UserProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ public function get_allowed_features( $user_id ) {
continue;
}
foreach ( $provider_features as $feature => $feature_name ) {
// Check if feature is enabled.
if ( ! $provider_class->is_enabled( $feature ) ) {
continue;
}

$access_control = new AccessControl( $provider_class, $feature );

// Check if feature has user based opt-out enabled.
Expand Down

0 comments on commit 7fb61a0

Please sign in to comment.