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 support controlling feature availability #614

Merged
merged 7 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function register() {
'recommended-content-block-editor-script',
sprintf(
'var hasRecommendedContentAccess = %d;',
$personalizer->has_access( 'recommended_content' )
$personalizer->is_feature_enabled( 'recommended_content' )
),
'before'
);
Expand Down
39 changes: 12 additions & 27 deletions includes/Classifai/Providers/Azure/ComputerVision.php
Original file line number Diff line number Diff line change
Expand Up @@ -1431,39 +1431,24 @@ public function rest_endpoint_callback( $post_id, $route_to_call, $args = [] ) {
}

/**
* Determine if the current user can access the feature
* Determine if the feature is turned on.
* Note: This function does not check if the user has access to the feature.
*
* @since 2.5.0
*
* @param string $feature Feature to check.
* @return bool
*/
public function is_feature_enabled( string $feature ) {
if ( 'image_captions' !== $feature ) {
return parent::is_feature_enabled( $feature );
}

$access = false;
$settings = $this->get_settings();
public function is_enabled( string $feature ) {
$settings = $this->get_settings();
$enable_key = 'enable_' . $feature;

// Check if provider is configured and user has access to the feature and the feature is turned on.
if (
$this->is_configured() &&
$this->has_access( $feature ) &&
! empty( $this->get_alt_text_settings() )
) {
$access = true;
$is_enabled = ( isset( $settings[ $enable_key ] ) && 1 === (int) $settings[ $enable_key ] );
if ( 'image_captions' === $feature ) {
$is_enabled = ( ! empty( $this->get_alt_text_settings() ) );
}

/**
* Filter to override permission to a specific classifai feature.
*
* @since 2.4.0
* @hook classifai_{$this->option_name}_enable_{$feature}
*
* @param {bool} $access Current access value.
* @param {array} $settings Current feature settings.
*
* @return {bool} Should the user have access?
*/
return apply_filters( "classifai_{$this->option_name}_enable_{$feature}", $access, $settings );
/** This filter is documented in includes/Classifai/Providers/Provider.php */
return apply_filters( "classifai_is_{$feature}_enabled", $is_enabled, $settings );
}
}
61 changes: 55 additions & 6 deletions includes/Classifai/Providers/Azure/Personalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct( $service ) {
'title' => __( 'Microsoft Azure AI Personalizer', 'classifai' ),
'fields' => array( 'url', 'api-key' ),
'features' => array(
'authenticated' => __( 'Recommended content block', 'classifai' ),
'enable_recommended_content' => __( 'Recommended content block', 'classifai' ),
),
);
}
Expand All @@ -73,19 +73,47 @@ public function get_default_settings() {
return array_merge(
$default_settings,
[
'authenticated' => false,
'url' => '',
'api_key' => '',
'enable_recommended_content' => false,
'authenticated' => false,
'url' => '',
'api_key' => '',
]
);
}

/**
* Get the settings and allow for settings default values.
*
* @param string|bool|mixed $index Optional. Name of the settings option index.
*
* @return string|array|mixed
*/
public function get_settings( $index = false ) {
$defaults = $this->get_default_settings();
$settings = get_option( $this->get_option_name(), [] );

// Backward compatibility for enable feature setting.
if ( ! empty( $settings ) && ! isset( $settings['enable_recommended_content'] ) ) {
$settings['enable_recommended_content'] = $settings['authenticated'] ?? $defaults['enable_recommended_content'];
}

$settings = wp_parse_args( $settings, $defaults );

if ( $index && isset( $settings[ $index ] ) ) {
return $settings[ $index ];
}

return $settings;
}

/**
* Register the functionality.
*/
public function register() {
// Setup Blocks
Blocks\setup();
if ( $this->is_enabled( 'recommended_content' ) ) {
// Setup Blocks
Blocks\setup();
}
}

/**
Expand Down Expand Up @@ -133,6 +161,20 @@ public function setup_fields_sections() {
]
);

add_settings_field(
'enable_recommended_content',
esc_html__( 'Recommend content', 'classifai' ),
[ $this, 'render_input' ],
$this->get_option_name(),
$this->get_option_name(),
[
'label_for' => 'enable_recommended_content',
'input_type' => 'checkbox',
'default_value' => $default_settings['enable_recommended_content'],
'description' => __( 'Enables Recommended content block.', 'classifai' ),
]
);

$this->add_access_settings( 'recommended_content' );
}

Expand All @@ -145,6 +187,13 @@ public function setup_fields_sections() {
*/
public function sanitize_settings( $settings ) {
$new_settings = $this->sanitize_access_settings( $settings, 'recommended_content' );

if ( empty( $settings['enable_recommended_content'] ) || 1 !== (int) $settings['enable_recommended_content'] ) {
$new_settings['enable_recommended_content'] = 'no';
} else {
$new_settings['enable_recommended_content'] = '1';
}

if ( ! empty( $settings['url'] ) && ! empty( $settings['api_key'] ) ) {
$auth_check = $this->authenticate_credentials( $settings['url'], $settings['api_key'] );
if ( is_wp_error( $auth_check ) ) {
Expand Down
98 changes: 57 additions & 41 deletions includes/Classifai/Providers/Azure/TextToSpeech.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct( $service ) {
'title' => __( 'Microsoft Azure Text to Speech', 'classifai' ),
'fields' => array( 'url', 'api-key' ),
'features' => array(
'authenticated' => __( 'Generate speech for post content', 'classifai' ),
'enable_text_to_speech' => __( 'Generate speech for post content', 'classifai' ),
),
);
}
Expand Down Expand Up @@ -139,7 +139,9 @@ public function register() {
}
}

add_filter( 'the_content', [ $this, 'render_post_audio_controls' ] );
if ( $this->is_enabled( 'text_to_speech' ) ) {
add_filter( 'the_content', [ $this, 'render_post_audio_controls' ] );
}
}

/**
Expand Down Expand Up @@ -186,6 +188,20 @@ public function setup_fields_sections() {
]
);

add_settings_field(
'enable_text_to_speech',
esc_html__( 'Generate speech for post content', 'classifai' ),
[ $this, 'render_input' ],
$this->get_option_name(),
$this->get_option_name(),
[
'label_for' => 'enable_text_to_speech',
'input_type' => 'checkbox',
'default_value' => $default_settings['enable_text_to_speech'],
'description' => __( 'Enables speech generation for post content.', 'classifai' ),
]
);

// Add user/role based access settings.
$this->add_access_settings( 'text_to_speech' );

Expand Down Expand Up @@ -230,6 +246,12 @@ public function sanitize_settings( $settings ) {
$current_settings = array_merge( $current_settings, $this->sanitize_access_settings( $settings, 'text_to_speech' ) );
$is_credentials_changed = false;

if ( empty( $settings['enable_text_to_speech'] ) || 1 !== (int) $settings['enable_text_to_speech'] ) {
$current_settings['enable_text_to_speech'] = 'no';
} else {
$current_settings['enable_text_to_speech'] = '1';
}

if ( ! empty( $settings['credentials']['url'] ) && ! empty( $settings['credentials']['api_key'] ) ) {
$new_url = trailingslashit( esc_url_raw( $settings['credentials']['url'] ) );
$new_key = sanitize_text_field( $settings['credentials']['api_key'] );
Expand Down Expand Up @@ -444,18 +466,46 @@ public function get_default_settings() {
return array_merge(
$default_settings,
[
'credentials' => array(
'enable_text_to_speech' => false,
'credentials' => array(
'url' => '',
'api_key' => '',
),
'voices' => array(),
'voice' => '',
'authenticated' => false,
'post_types' => array(),
'voices' => array(),
'voice' => '',
'authenticated' => false,
'post_types' => array(
'post' => 'post',
),
]
);
}

/**
* Get the settings and allow for settings default values.
*
* @param string|bool|mixed $index Optional. Name of the settings option index.
*
* @return string|array|mixed
*/
public function get_settings( $index = false ) {
$defaults = $this->get_default_settings();
$settings = get_option( $this->get_option_name(), [] );

// Backward compatibility for enable feature setting.
if ( ! empty( $settings ) && ! isset( $settings['enable_text_to_speech'] ) ) {
$settings['enable_text_to_speech'] = $settings['authenticated'] ?? $defaults['enable_text_to_speech'];
}

$settings = wp_parse_args( $settings, $defaults );

if ( $index && isset( $settings[ $index ] ) ) {
return $settings[ $index ];
}

return $settings;
}

/**
* Initial audio generation state.
*
Expand Down Expand Up @@ -894,38 +944,4 @@ protected function get_post_types_select_options() {

return $options;
}


/**
* Determine if the current user can access the feature
* TODO: remove this method once enable/disable feature is implemented.
*
* @param string $feature Feature to check.
* @return bool
*/
public function is_feature_enabled( string $feature = '' ) {
$access = false;
$settings = $this->get_settings();

// Check if provider is configured and user has access to the feature.
if (
$this->is_configured() &&
$this->has_access( $feature )
) {
$access = true;
}

/**
* Filter to override permission to a specific classifai feature.
*
* @since 2.4.0
* @hook classifai_{$this->option_name}_enable_{$feature}
*
* @param {bool} $access Current access value.
* @param {array} $settings Current feature settings.
*
* @return {bool} Should the user have access?
*/
return apply_filters( "classifai_{$this->option_name}_enable_{$feature}", $access, $settings );
}
}
70 changes: 31 additions & 39 deletions includes/Classifai/Providers/OpenAI/ChatGPT.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,45 +95,6 @@ public function __construct( $service ) {
);
}

/**
* Determine if the current user can access the feature
*
* @param string $feature Feature to check.
* @return bool
*/
public function is_feature_enabled( string $feature = '' ) {
$access = false;
$settings = $this->get_settings();
$features = array(
'title_generation' => 'enable_titles',
'excerpt_generation' => 'enable_excerpt',
'resize_content' => 'enable_resize_content',
);

$enable_key = $features[ $feature ] ?? 'enable_' . $feature;

// Check if user has access to the feature and the feature is turned on.
if (
$this->has_access( $feature ) &&
( isset( $settings[ $enable_key ] ) && 1 === (int) $settings[ $enable_key ] )
) {
$access = true;
}

/**
* Filter to override permission to a ChatGPT generate feature.
*
* @since 2.3.0
* @hook classifai_openai_chatgpt_enable_{$feature}
*
* @param {bool} $access Current access value.
* @param {array} $settings Current feature settings.
*
* @return {bool} Should the user have access?
*/
return apply_filters( "classifai_openai_chatgpt_{$enable_key}", $access, $settings );
}

/**
* Register what we need for the plugin.
*
Expand Down Expand Up @@ -1230,4 +1191,35 @@ function ( $prompt ) {

return $default_prompt;
}

/**
* Determine if the feature is turned on.
* Note: This function does not check if the user has access to the feature.
*
* @param string $feature Feature to check.
* @return bool
*/
public function is_enabled( string $feature ) {
$settings = $this->get_settings();
$enable_key = 'enable_' . $feature;

// Handle different enable keys.
switch ( $feature ) {
case 'title_generation':
$enable_key = 'enable_titles';
break;

case 'excerpt_generation':
$enable_key = 'enable_excerpt';
break;

default:
break;
}

$is_enabled = ( isset( $settings[ $enable_key ] ) && 1 === (int) $settings[ $enable_key ] );

/** This filter is documented in includes/Classifai/Providers/Provider.php */
return apply_filters( "classifai_is_{$feature}_enabled", $is_enabled, $settings );
}
}
Loading