diff --git a/classifai.php b/classifai.php index b74e600ec..66ecc55d1 100644 --- a/classifai.php +++ b/classifai.php @@ -97,6 +97,18 @@ function classifai_autoloader() { } } +/** + * Gets the installation message error. + * + * This was put in a function specifically because it's used both in WP-CLI and within an admin notice if not using + * WP-CLI. + * + * @return string + */ +function get_error_install_message() { + return esc_html__( 'Error: Please run $ composer install in the classifai plugin directory.', 'classifai' ); +} + /** * Plugin code entry point. Singleton instance is used to maintain a common single * instance of the plugin throughout the current request's lifecycle. @@ -108,7 +120,19 @@ function classifai_autorun() { if ( classifai_autoload() ) { $plugin = \Classifai\Plugin::get_instance(); $plugin->enable(); + + if ( defined( 'WP_CLI' ) && WP_CLI ) { + require_once CLASSIFAI_PLUGIN_DIR . '/includes/Classifai/Command/ClassifaiCommand.php'; + } } else { + if ( defined( 'WP_CLI' ) && WP_CLI ) { + try { + \WP_CLI::error( get_error_install_message() ); + } catch ( \WP_CLI\ExitException $e ) { + error_log( $e->getMessage() ); + } + } + add_action( 'admin_notices', 'classifai_autoload_notice' ); } } @@ -118,8 +142,8 @@ function classifai_autorun() { * Generate a notice if autoload fails. */ function classifai_autoload_notice() { - printf( '

%2$s

', 'notice notice-error', esc_html__( 'Error: Please run $ composer install in the classifai plugin directory.', 'classifai' ) ); - error_log( esc_html__( 'Error: Please run $ composer install in the classifai plugin directory.', 'classifai' ) ); + printf( '

%2$s

', 'notice notice-error', get_error_install_message() ); // @codingStandardsIgnoreLine Text is escaped in calling function already. + error_log( get_error_install_message() ); } @@ -140,7 +164,8 @@ classifai_autorun(); /* * Enable updates if we have a valid license */ - $settings = \Classifai\get_plugin_settings(); + $service_manager = new \Classifai\Services\ServicesManager(); + $settings = $service_manager->get_settings(); if ( isset( $settings['valid_license'] ) && $settings['valid_license'] ) { // @codingStandardsIgnoreStart diff --git a/includes/Classifai/Command/ClassifaiCommand.php b/includes/Classifai/Command/ClassifaiCommand.php index 729b908d3..198108a02 100644 --- a/includes/Classifai/Command/ClassifaiCommand.php +++ b/includes/Classifai/Command/ClassifaiCommand.php @@ -234,8 +234,7 @@ public function auth( $args = [], $opts = [] ) { } /** - * Restores the plugin configuration to factory defaults. IBM Watson - * credentials must be reentered after this command. + * Restores the plugin configuration to factory defaults. Any API credentials will need to be re-entered after this is ran. * * @param array $args Arguments. * @param array $opts Options. @@ -250,7 +249,7 @@ public function reset( $args = [], $opts = [] ) { \Classifai\reset_plugin_settings(); \WP_CLI::success( - 'Defaults restored successfully. Please update the IBM Watson credentials.' + 'Defaults restored successfully. Please update all your API credentials.' ); } @@ -309,3 +308,9 @@ private function print( $output, $post_id ) { } } + +try { + \WP_CLI::add_command( 'classifai', __NAMESPACE__ . '\\ClassifaiCommand' ); +} catch ( \Exception $e ) { + error_log( $e->getMessage() ); +} diff --git a/includes/Classifai/Helpers.php b/includes/Classifai/Helpers.php index 73acfa643..9d3ad68c2 100644 --- a/includes/Classifai/Helpers.php +++ b/includes/Classifai/Helpers.php @@ -2,6 +2,10 @@ namespace Classifai; +use Classifai\Providers\Provider; +use Classifai\Services\Service; +use Classifai\Services\ServicesManager; + /** * Miscellaneous Helper functions to access different parts of the * ClassifAI plugin. @@ -17,10 +21,31 @@ function get_plugin() { } /** - * Returns the ClassifAI plugin's stored settings in the WP options + * Returns the ClassifAI plugin's stored settings in the WP options. + * + * @param string $service The service to get settings from, defaults to the ServiceManager class. + * + * @return array The array of ClassifAi settings. */ -function get_plugin_settings() { - return get_option( 'classifai_watson_nlu' ); +function get_plugin_settings( $service = '' ) { + $services = Plugin::$instance->services; + if ( empty( $services ) || empty( $services['service_manager'] ) || ! $services['service_manager'] instanceof ServicesManager ) { + return []; + } + + /** @var ServicesManager $service_manager Instance of the services manager class. */ + $service_manager = $services['service_manager']; + if ( empty( $service ) ) { + return $service_manager->get_settings(); + } + + if ( ! isset( $service_manager->service_classes[ $service ] ) || ! $service_manager->service_classes[ $service ] instanceof Service ) { + return []; + } + + /** @var Provider $provider An instance or extension of the provider abstract class. */ + $provider = $service_manager->service_classes[ $service ]->provider_classes[0]; + return $provider->get_settings(); } /** @@ -45,34 +70,40 @@ function set_plugin_settings( $settings ) { } /** - * Resets the plugin to factory defaults. + * Resets the plugin to factory defaults, keeping licensing information only. */ function reset_plugin_settings() { - $settings = [ - 'post_types' => [ - 'post', - 'page', - ], - 'features' => [ - 'category' => true, - 'category_threshold' => WATSON_CATEGORY_THRESHOLD, - 'category_taxonomy' => WATSON_CATEGORY_TAXONOMY, - - 'keyword' => true, - 'keyword_threshold' => WATSON_KEYWORD_THRESHOLD, - 'keyword_taxonomy' => WATSON_KEYWORD_TAXONOMY, - - 'concept' => false, - 'concept_threshold' => WATSON_CONCEPT_THRESHOLD, - 'concept_taxonomy' => WATSON_CONCEPT_TAXONOMY, - - 'entity' => false, - 'entity_threshold' => WATSON_ENTITY_THRESHOLD, - 'entity_taxonomy' => WATSON_ENTITY_TAXONOMY, - ], - ]; - update_option( 'classifai_settings', $settings ); + $options = get_option( 'classifai_settings' ); + if ( $options && isset( $options['registration'] ) ) { + // This is a legacy option set, so let's update it to the new format. + $new_settings = [ + 'valid_license' => $options['valid_license'], + 'email' => isset( $options['registration']['email'] ) ? $options['registration']['email'] : '', + 'license_key' => isset( $options['registration']['license_key'] ) ? $options['registration']['license_key'] : '', + ]; + update_option( 'classifai_settings', $new_settings ); + } + + $services = get_plugin()->services; + if ( ! isset( $services['service_manager'] ) || ! $services['service_manager']->service_classes ) { + return; + } + + $service_classes = $services['service_manager']->service_classes; + foreach ( $service_classes as $service_class ) { + if ( ! $service_class instanceof Service || empty( $service_class->provider_classes ) ) { + continue; + } + + foreach ( $service_class->provider_classes as $provider_class ) { + if ( ! $provider_class instanceof Provider || ! method_exists( $provider_class, 'reset_settings' ) ) { + continue; + } + + $provider_class->reset_settings(); + } + } } @@ -85,7 +116,7 @@ function reset_plugin_settings() { * @return string */ function get_watson_api_url() { - $settings = get_plugin_settings(); + $settings = get_plugin_settings( 'language_processing' ); $creds = ! empty( $settings['credentials'] ) ? $settings['credentials'] : []; if ( ! empty( $creds['watson_url'] ) ) { @@ -107,7 +138,7 @@ function get_watson_api_url() { * @return string */ function get_watson_username() { - $settings = get_plugin_settings(); + $settings = get_plugin_settings( 'language_processing' ); $creds = ! empty( $settings['credentials'] ) ? $settings['credentials'] : []; if ( ! empty( $creds['watson_username'] ) ) { @@ -128,7 +159,7 @@ function get_watson_username() { * @return string */ function get_watson_password() { - $settings = get_plugin_settings(); + $settings = get_plugin_settings( 'language_processing' ); $creds = ! empty( $settings['credentials'] ) ? $settings['credentials'] : []; if ( ! empty( $creds['watson_password'] ) ) { @@ -147,7 +178,7 @@ function get_watson_password() { * return array */ function get_supported_post_types() { - $classifai_settings = get_plugin_settings(); + $classifai_settings = get_plugin_settings( 'language_processing' ); if ( empty( $classifai_settings ) ) { $post_types = []; @@ -176,7 +207,7 @@ function get_supported_post_types() { * @return bool */ function get_feature_enabled( $feature ) { - $settings = get_plugin_settings(); + $settings = get_plugin_settings( 'language_processing' ); if ( ! empty( $settings ) && ! empty( $settings['features'] ) ) { if ( ! empty( $settings['features'][ $feature ] ) ) { @@ -203,7 +234,7 @@ function get_feature_enabled( $feature ) { * @return int */ function get_feature_threshold( $feature ) { - $settings = get_plugin_settings(); + $settings = get_plugin_settings( 'language_processing' ); $threshold = 0; if ( ! empty( $settings ) && ! empty( $settings['features'] ) ) { @@ -244,8 +275,8 @@ function get_feature_threshold( $feature ) { * @return string Taxonomy mapped to the feature */ function get_feature_taxonomy( $feature ) { - $settings = get_plugin_settings(); - $taxonomy = 0; + $settings = get_plugin_settings( 'language_processing' ); + $taxonomy = 0; if ( ! empty( $settings ) && ! empty( $settings['features'] ) ) { if ( ! empty( $settings['features'][ $feature . '_taxonomy' ] ) ) { diff --git a/includes/Classifai/Plugin.php b/includes/Classifai/Plugin.php index 20dbc9df3..9191003e2 100644 --- a/includes/Classifai/Plugin.php +++ b/includes/Classifai/Plugin.php @@ -11,7 +11,7 @@ class Plugin { /** * @var array $services The known list of services. */ - protected $services = []; + public $services = []; /** * Lazy initialize the plugin @@ -66,14 +66,17 @@ public function i18n() { * Initialize the Services. */ public function init_services() { + $classifai_services = apply_filters( + 'classifai_services', + [ + 'language_processing' => 'Classifai\Services\LanguageProcessing', + 'image_processing' => 'Classifai\Services\ImageProcessing', + ] + ); + $this->services = [ - new Services\ServicesManager( - apply_filters( - 'classifai_services', - [ 'Classifai\Services\LanguageProcessing', 'Classifai\Services\ImageProcessing' ] - ) - ), - new Admin\Notifications(), + 'service_manager' => new Services\ServicesManager( $classifai_services ), + 'admin_notifications' => new Admin\Notifications(), ]; foreach ( $this->services as $service ) { diff --git a/includes/Classifai/Providers/AWS/Comprehend.php b/includes/Classifai/Providers/AWS/Comprehend.php index 9f5e4abcd..71d884ddd 100644 --- a/includes/Classifai/Providers/AWS/Comprehend.php +++ b/includes/Classifai/Providers/AWS/Comprehend.php @@ -23,6 +23,13 @@ public function __construct( $service ) { ); } + /** + * Resets the settings for the Comprehend provider. + */ + public function reset_settings() { + // TODO: Implement reset_settings() method. + } + /** * Can the functionality be initialized? * diff --git a/includes/Classifai/Providers/Azure/ComputerVision.php b/includes/Classifai/Providers/Azure/ComputerVision.php index 714ac9a09..965133901 100644 --- a/includes/Classifai/Providers/Azure/ComputerVision.php +++ b/includes/Classifai/Providers/Azure/ComputerVision.php @@ -27,6 +27,13 @@ public function __construct( $service ) { ); } + /** + * Resets settings for the ComputerVision provider. + */ + public function reset_settings() { + // TODO: Implement reset_settings() method. + } + /** * Can the functionality be initialized? * diff --git a/includes/Classifai/Providers/Provider.php b/includes/Classifai/Providers/Provider.php index 5ef2c9970..493229cb0 100644 --- a/includes/Classifai/Providers/Provider.php +++ b/includes/Classifai/Providers/Provider.php @@ -73,6 +73,11 @@ abstract public function can_register(); */ abstract public function register(); + /** + * Resets the settings for this provider. + */ + abstract public function reset_settings(); + /** * Initialization routine */ @@ -97,7 +102,7 @@ public function register_settings() { * * @return array */ - protected function get_settings( $index = false ) { + public function get_settings( $index = false ) { $defaults = []; $settings = get_option( $this->get_option_name(), [] ); $settings = wp_parse_args( $settings, $defaults ); diff --git a/includes/Classifai/Providers/Watson/NLU.php b/includes/Classifai/Providers/Watson/NLU.php index 29605b59d..968bd5975 100644 --- a/includes/Classifai/Providers/Watson/NLU.php +++ b/includes/Classifai/Providers/Watson/NLU.php @@ -66,6 +66,37 @@ public function __construct( $service ) { ]; } + /** + * Resets the settings for the NLU provider. + */ + public function reset_settings() { + $settings = [ + 'post_types' => [ + 'post', + 'page', + ], + 'features' => [ + 'category' => true, + 'category_threshold' => WATSON_CATEGORY_THRESHOLD, + 'category_taxonomy' => WATSON_CATEGORY_TAXONOMY, + + 'keyword' => true, + 'keyword_threshold' => WATSON_KEYWORD_THRESHOLD, + 'keyword_taxonomy' => WATSON_KEYWORD_TAXONOMY, + + 'concept' => false, + 'concept_threshold' => WATSON_CONCEPT_THRESHOLD, + 'concept_taxonomy' => WATSON_CONCEPT_TAXONOMY, + + 'entity' => false, + 'entity_threshold' => WATSON_ENTITY_THRESHOLD, + 'entity_taxonomy' => WATSON_ENTITY_TAXONOMY, + ], + ]; + + update_option( $this->get_option_name(), $settings ); + } + /** * Can the functionality be initialized? * @@ -100,7 +131,7 @@ public function register() { * * @return array */ - protected function get_settings( $index = false ) { + public function get_settings( $index = false ) { $defaults = []; $settings = get_option( $this->get_option_name(), [] ); diff --git a/includes/Classifai/Services/Service.php b/includes/Classifai/Services/Service.php index e348e4e13..627c0c560 100644 --- a/includes/Classifai/Services/Service.php +++ b/includes/Classifai/Services/Service.php @@ -25,7 +25,7 @@ abstract class Service { /** * @var array Array of class instances. */ - protected $provider_classes; + public $provider_classes; /** * Service constructor. diff --git a/includes/Classifai/Services/ServicesManager.php b/includes/Classifai/Services/ServicesManager.php index efb172c84..3adf315c0 100644 --- a/includes/Classifai/Services/ServicesManager.php +++ b/includes/Classifai/Services/ServicesManager.php @@ -10,13 +10,12 @@ class ServicesManager { /** * @var array List of registered services */ - protected $services = []; - + public $services = []; /** * @var array List of class instances being managed. */ - protected $service_classes; + public $service_classes; /** * @var string Page title for the admin page @@ -53,9 +52,9 @@ public function can_register() { * Register the actions required for the settings page. */ public function register() { - foreach ( $this->services as $service ) { + foreach ( $this->services as $key => $service ) { if ( class_exists( $service ) ) { - $this->service_classes[] = new $service(); + $this->service_classes[ $key ] = new $service(); } } @@ -71,7 +70,7 @@ public function register() { * * @param string $index Optional specific setting to be retrieved. */ - protected function get_settings( $index = false ) { + public function get_settings( $index = false ) { $settings = get_option( 'classifai_settings' ); // Special handling polyfill for pre-1.3 settings which were nested