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

Ensure you can properly remove Providers #456

Merged
merged 6 commits into from
May 18, 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
8 changes: 8 additions & 0 deletions includes/Classifai/Admin/Onboarding.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@ public function get_features() {
$service_slug = $service->get_menu_slug();
$features = array();

if ( empty( $service->provider_classes ) ) {
continue;
}

foreach ( $service->provider_classes as $provider_class ) {
$options = $provider_class->get_onboarding_options();
if ( ! empty( $options ) && ! empty( $options['features'] ) ) {
Expand Down Expand Up @@ -458,6 +462,10 @@ public function get_providers() {
$providers = [];

foreach ( $service_manager->service_classes as $service ) {
if ( empty( $service->provider_classes ) ) {
continue;
}

foreach ( $service->provider_classes as $provider_class ) {
$providers[ $provider_class->get_option_name() ] = $provider_class;
}
Expand Down
35 changes: 33 additions & 2 deletions includes/Classifai/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Classifai\Providers\Provider;
use Classifai\Services\Service;
use Classifai\Services\ServicesManager;
use WP_Error;

/**
* Miscellaneous Helper functions to access different parts of the
Expand Down Expand Up @@ -43,9 +44,16 @@ function get_plugin_settings( $service = '', $provider = '' ) {
return [];
}

// Ensure we have at least one provider.
$providers = $service_manager->service_classes[ $service ]->provider_classes;

if ( empty( $providers ) ) {
return [];
}

// If we want settings for a specific provider, find the proper provider service.
if ( ! empty( $provider ) ) {
foreach ( $service_manager->service_classes[ $service ]->provider_classes as $provider_class ) {
foreach ( $providers as $provider_class ) {
if ( $provider_class->provider_service_name === $provider ) {
return $provider_class->get_settings();
}
Expand All @@ -55,7 +63,7 @@ function get_plugin_settings( $service = '', $provider = '' ) {
}

/** @var Provider $provider An instance or extension of the provider abstract class. */
$provider = $service_manager->service_classes[ $service ]->provider_classes[0];
$provider = $providers[0];
return $provider->get_settings();
}

Expand Down Expand Up @@ -661,3 +669,26 @@ function clean_input( string $key = '', bool $is_get = false, string $sanitize_c

return false;
}

/**
* Find the provider class that a service belongs to.
*
* @param array $provider_classes Provider classes to look in.
* @param string $service_name Service name to look for.
* @return Provider|WP_Error
*/
function find_provider_class( array $provider_classes = [], string $service_name = '' ) {
$provider = '';

foreach ( $provider_classes as $provider_class ) {
if ( $service_name === $provider_class->provider_service_name ) {
$provider = $provider_class;
}
}

if ( ! $provider ) {
return new WP_Error( 'provider_class_required', esc_html__( 'Provider class not found.', 'classifai' ) );
}

return $provider;
}
46 changes: 19 additions & 27 deletions includes/Classifai/Services/ImageProcessing.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use WP_REST_Server;
use WP_REST_Request;
use WP_Error;
use function Classifai\attachment_is_pdf;
use function Classifai\get_asset_info;
use function Classifai\find_provider_class;

class ImageProcessing extends Service {

Expand Down Expand Up @@ -51,15 +51,18 @@ public function enqueue_media_scripts() {
true
);

wp_add_inline_script(
'classifai-media-script',
'const classifaiMediaVars = ' . wp_json_encode(
array(
'enabledAltTextFields' => $this->provider_classes[0]->get_alt_text_settings() ? $this->provider_classes[0]->get_alt_text_settings() : array(),
)
),
'before'
);
$provider = find_provider_class( $this->provider_classes ?? [], 'Computer Vision' );
if ( ! is_wp_error( $provider ) ) {
wp_add_inline_script(
'classifai-media-script',
'const classifaiMediaVars = ' . wp_json_encode(
array(
'enabledAltTextFields' => $provider->get_alt_text_settings() ? $provider->get_alt_text_settings() : array(),
)
),
'before'
);
}
}

/**
Expand Down Expand Up @@ -220,7 +223,6 @@ public function computer_vision_endpoint_callback( $request ) {
$attachment_id = $request->get_param( 'id' );
$custom_atts = $request->get_attributes();
$route_to_call = empty( $custom_atts['args']['route'] ) ? false : strtolower( $custom_atts['args']['route'][0] );
$provider = '';

// Check to be sure the post both exists and is an attachment.
if ( ! get_post( $attachment_id ) || 'attachment' !== get_post_type( $attachment_id ) ) {
Expand All @@ -234,15 +236,11 @@ public function computer_vision_endpoint_callback( $request ) {
}

// Find the right provider class.
foreach ( $this->provider_classes as $provider_class ) {
if ( 'Computer Vision' === $provider_class->provider_service_name ) {
$provider = $provider_class;
}
}
$provider = find_provider_class( $this->provider_classes ?? [], 'Computer Vision' );

// Ensure we have a provider class. Should never happen but :shrug:
if ( ! $provider ) {
return new WP_Error( 'provider_class_required', esc_html__( 'Provider class not found.', 'classifai' ) );
if ( is_wp_error( $provider ) ) {
return $provider;
}

// Call the provider endpoint function
Expand Down Expand Up @@ -305,18 +303,12 @@ public function computer_vision_endpoint_permissions_check( WP_REST_Request $req
* @return \WP_REST_Response|WP_Error
*/
public function generate_image( WP_REST_Request $request ) {
$provider = '';

// Find the right provider class.
foreach ( $this->provider_classes as $provider_class ) {
if ( 'DALL·E' === $provider_class->provider_service_name ) {
$provider = $provider_class;
}
}
$provider = find_provider_class( $this->provider_classes ?? [], 'DALL·E' );

// Ensure we have a provider class. Should never happen but :shrug:
if ( ! $provider ) {
return new WP_Error( 'provider_class_required', esc_html__( 'Provider class not found.', 'classifai' ) );
if ( is_wp_error( $provider ) ) {
return $provider;
}

return rest_ensure_response(
Expand Down
14 changes: 5 additions & 9 deletions includes/Classifai/Services/LanguageProcessing.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Classifai\Services;

use Classifai\Admin\SavePostHandler;
use function Classifai\find_provider_class;
use WP_REST_Server;
use WP_REST_Request;
use WP_Error;
Expand Down Expand Up @@ -174,19 +175,14 @@ public function generate_post_tags_permissions_check( WP_REST_Request $request )
* @return \WP_REST_Response|WP_Error
*/
public function generate_post_excerpt( WP_REST_Request $request ) {
$post_id = $request->get_param( 'id' );
$provider = '';
$post_id = $request->get_param( 'id' );

// Find the right provider class.
foreach ( $this->provider_classes as $provider_class ) {
if ( 'ChatGPT' === $provider_class->provider_service_name ) {
$provider = $provider_class;
}
}
$provider = find_provider_class( $this->provider_classes ?? [], 'ChatGPT' );

// Ensure we have a provider class. Should never happen but :shrug:
if ( ! $provider ) {
return new WP_Error( 'provider_class_required', esc_html__( 'Provider class not found.', 'classifai' ) );
if ( is_wp_error( $provider ) ) {
return $provider;
}

return rest_ensure_response( $provider->rest_endpoint_callback( $post_id, 'excerpt' ) );
Expand Down
21 changes: 11 additions & 10 deletions includes/Classifai/Services/Personalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Classifai\Services;

use function Classifai\find_provider_class;
use WP_REST_Server;
use WP_REST_Request;
use WP_Error;
Expand Down Expand Up @@ -68,8 +69,13 @@ public function ajax_render_recommended_content() {
}
}

// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $this->provider_classes[0]->render_recommended_content( $attributes );
$provider = find_provider_class( $this->provider_classes ?? [], 'Personalizer' );

if ( ! is_wp_error( $provider ) ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $provider->render_recommended_content( $attributes );
}

exit();
}

Expand Down Expand Up @@ -128,7 +134,6 @@ public function reward_endpoint_callback( WP_REST_Request $request ) {
$event_id = $request->get_param( 'eventId' );
$reward = ( '1' === $request->get_param( 'rewarded' ) ) ? 1 : 0;
$route = $request->get_param( 'route' ) ?? false;
$provider = '';

// If no args, respond 404.
if ( false === $route ) {
Expand All @@ -141,15 +146,11 @@ public function reward_endpoint_callback( WP_REST_Request $request ) {
}

// Find the right provider class.
foreach ( $this->provider_classes as $provider_class ) {
if ( 'Personalizer' === $provider_class->provider_service_name ) {
$provider = $provider_class;
}
}
$provider = find_provider_class( $this->provider_classes ?? [], 'Personalizer' );

// Ensure we have a provider class. Should never happen but :shrug:
if ( ! $provider ) {
return new WP_Error( 'provider_class_required', esc_html__( 'Provider class not found.', 'classifai' ) );
if ( is_wp_error( $provider ) ) {
return $provider;
}

// Send reward to personalizer.
Expand Down
24 changes: 20 additions & 4 deletions includes/Classifai/Services/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Classifai\Services;

use function Classifai\find_provider_class;
use WP_Error;

abstract class Service {
Expand Down Expand Up @@ -106,7 +107,8 @@ public function get_display_name() {
* Render the start of a settings page. The rest is added by the providers
*/
public function render_settings_page() {
$active_tab = isset( $_GET['provider'] ) ? sanitize_text_field( $_GET['provider'] ) : $this->provider_classes[0]->get_settings_section(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$active_tab = $this->provider_classes ? $this->provider_classes[0]->get_settings_section() : '';
$active_tab = isset( $_GET['provider'] ) ? sanitize_text_field( $_GET['provider'] ) : $active_tab; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$base_url = add_query_arg(
array(
'page' => 'classifai',
Expand All @@ -121,14 +123,23 @@ public function render_settings_page() {
?>
<div class="classifai-wrap wrap wrap--nlu">
<h2><?php echo esc_html( $this->display_name ); ?></h2>
<?php if ( ! empty( $this->provider_classes ) ) : ?>

<?php
if ( empty( $this->provider_classes ) ) {
echo '<p>' . esc_html__( 'No providers available for this service.', 'classifai' ) . '</p>';
dkotter marked this conversation as resolved.
Show resolved Hide resolved
echo '</div></div>';
return;
}
?>

<h2 class="nav-tab-wrapper">
<?php foreach ( $this->provider_classes as $provider_class ) : ?>
<a href="<?php echo esc_url( add_query_arg( 'provider', $provider_class->get_settings_section(), $base_url ) ); ?>" class="nav-tab <?php echo $provider_class->get_settings_section() === $active_tab ? 'nav-tab-active' : ''; ?>"><?php echo esc_html( $provider_class->provider_name ); ?></a>
<?php endforeach; ?>
</h2>
<?php endif; ?>

<?php settings_errors(); ?>

<div class="classifai-nlu-sections">
<form method="post" action="options.php">
<?php
Expand All @@ -137,7 +148,12 @@ public function render_settings_page() {
submit_button();
?>
</form>
<?php if ( isset( $this->provider_classes[0] ) && ! empty( $this->provider_classes[0]->can_register() ) ) : ?>
<?php
// Find the right provider class.
$provider = find_provider_class( $this->provider_classes ?? [], 'Natural Language Understanding' );

if ( ! is_wp_error( $provider ) && ! empty( $provider->can_register() ) ) :
?>
<div id="classifai-post-preview-app">
<?php
$supported_post_statuses = \Classifai\get_supported_post_statuses();
Expand Down