From 921d1fb17f38801c48e1dff0b732845ae578b12a Mon Sep 17 00:00:00 2001 From: JayWood Date: Tue, 25 Jun 2019 11:32:43 -0400 Subject: [PATCH 01/16] Key services in the array so that they may be pulled and queried later. --- includes/Classifai/Plugin.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/includes/Classifai/Plugin.php b/includes/Classifai/Plugin.php index 20dbc9df3..ac067552f 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 ) { From 3640579d6fdca4435398d0a442a7d5ee49ca8af5 Mon Sep 17 00:00:00 2001 From: JayWood Date: Tue, 25 Jun 2019 11:38:56 -0400 Subject: [PATCH 02/16] Make get_settings public, return all settings for get_plugin_settings. --- includes/Classifai/Helpers.php | 13 +++++++++++-- includes/Classifai/Services/ServicesManager.php | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/includes/Classifai/Helpers.php b/includes/Classifai/Helpers.php index 73acfa643..1fb332957 100644 --- a/includes/Classifai/Helpers.php +++ b/includes/Classifai/Helpers.php @@ -2,6 +2,8 @@ namespace Classifai; +use Classifai\Services\ServicesManager; + /** * Miscellaneous Helper functions to access different parts of the * ClassifAI plugin. @@ -17,10 +19,17 @@ 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. + * + * @return array The array of ClassifAI settings. */ function get_plugin_settings() { - return get_option( 'classifai_watson_nlu' ); + $service_manager = Plugin::$instance->services[ 'service_manager' ]; + if ( $service_manager instanceof ServicesManager ) { + return $service_manager->get_settings(); + } + + return []; } /** diff --git a/includes/Classifai/Services/ServicesManager.php b/includes/Classifai/Services/ServicesManager.php index efb172c84..82e643b13 100644 --- a/includes/Classifai/Services/ServicesManager.php +++ b/includes/Classifai/Services/ServicesManager.php @@ -71,7 +71,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 From 39bb2a570903f240861a38625261a392e9ae5734 Mon Sep 17 00:00:00 2001 From: JayWood Date: Tue, 25 Jun 2019 11:55:36 -0400 Subject: [PATCH 03/16] Make settings methods public, update get_plugin_settings This updates the get_plugin_settings() function in helpers to allow for users to pass in a service to get the settings for that service. This also keys services in the filter to make them easily queryable when traversing the objects since numerical indicies can be unpredictable. --- includes/Classifai/Helpers.php | 20 ++++++++++++++++--- includes/Classifai/Providers/Provider.php | 2 +- includes/Classifai/Providers/Watson/NLU.php | 2 +- .../Classifai/Services/ServicesManager.php | 3 +-- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/includes/Classifai/Helpers.php b/includes/Classifai/Helpers.php index 1fb332957..52ed760b3 100644 --- a/includes/Classifai/Helpers.php +++ b/includes/Classifai/Helpers.php @@ -2,6 +2,7 @@ namespace Classifai; +use Classifai\Providers\Provider; use Classifai\Services\ServicesManager; /** @@ -21,15 +22,28 @@ function get_plugin() { /** * Returns the ClassifAI plugin's stored settings in the WP options. * + * @param string $service The service to get settings from. + * * @return array The array of ClassifAI settings. */ -function get_plugin_settings() { +function get_plugin_settings( $service = '' ) { $service_manager = Plugin::$instance->services[ 'service_manager' ]; - if ( $service_manager instanceof ServicesManager ) { + if ( ! $service_manager instanceof ServicesManager ) { + return []; + } + + if ( empty( $service ) ) { return $service_manager->get_settings(); } - return []; + if ( ! isset( $service_manager->services[ $service ] ) || ! $service_manager->services[ $service ] instanceof Provider ) { + return []; + } + + /** @var Provider $provider An instance or extension of the provider abstract class. */ + $provider = $service_manager->services[ $service ]; + return $provider->get_settings(); + } /** diff --git a/includes/Classifai/Providers/Provider.php b/includes/Classifai/Providers/Provider.php index 5ef2c9970..750141d54 100644 --- a/includes/Classifai/Providers/Provider.php +++ b/includes/Classifai/Providers/Provider.php @@ -97,7 +97,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..3de45472e 100644 --- a/includes/Classifai/Providers/Watson/NLU.php +++ b/includes/Classifai/Providers/Watson/NLU.php @@ -100,7 +100,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/ServicesManager.php b/includes/Classifai/Services/ServicesManager.php index 82e643b13..ae04a1ad2 100644 --- a/includes/Classifai/Services/ServicesManager.php +++ b/includes/Classifai/Services/ServicesManager.php @@ -10,8 +10,7 @@ class ServicesManager { /** * @var array List of registered services */ - protected $services = []; - + public $services = []; /** * @var array List of class instances being managed. From 3bdd4658017e12980ff74f4af51646178cd806b5 Mon Sep 17 00:00:00 2001 From: JayWood Date: Tue, 25 Jun 2019 13:44:47 -0400 Subject: [PATCH 04/16] Working replacements in helpers.php for plugin settings. --- includes/Classifai/Helpers.php | 32 ++++++++++--------- includes/Classifai/Services/Service.php | 2 +- .../Classifai/Services/ServicesManager.php | 6 ++-- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/includes/Classifai/Helpers.php b/includes/Classifai/Helpers.php index 52ed760b3..34ae274dd 100644 --- a/includes/Classifai/Helpers.php +++ b/includes/Classifai/Helpers.php @@ -3,6 +3,7 @@ namespace Classifai; use Classifai\Providers\Provider; +use Classifai\Services\Service; use Classifai\Services\ServicesManager; /** @@ -22,28 +23,29 @@ function get_plugin() { /** * Returns the ClassifAI plugin's stored settings in the WP options. * - * @param string $service The service to get settings from. + * @param string $service The service to get settings from, defaults to the ServiceManager class. * - * @return array The array of ClassifAI settings. + * @return array The array of ClassifAi settings. */ function get_plugin_settings( $service = '' ) { - $service_manager = Plugin::$instance->services[ 'service_manager' ]; - if ( ! $service_manager instanceof ServicesManager ) { + $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->services[ $service ] ) || ! $service_manager->services[ $service ] instanceof Provider ) { + 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->services[ $service ]; + $provider = $service_manager->service_classes[ $service ]->provider_classes[0]; return $provider->get_settings(); - } /** @@ -108,7 +110,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'] ) ) { @@ -130,7 +132,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'] ) ) { @@ -151,7 +153,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'] ) ) { @@ -170,7 +172,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 = []; @@ -199,7 +201,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 ] ) ) { @@ -226,7 +228,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'] ) ) { @@ -267,8 +269,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/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 ae04a1ad2..3adf315c0 100644 --- a/includes/Classifai/Services/ServicesManager.php +++ b/includes/Classifai/Services/ServicesManager.php @@ -15,7 +15,7 @@ class ServicesManager { /** * @var array List of class instances being managed. */ - protected $service_classes; + public $service_classes; /** * @var string Page title for the admin page @@ -52,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(); } } From 07b161630176a13c1296d786916f8f9e49d6c263 Mon Sep 17 00:00:00 2001 From: JayWood Date: Wed, 26 Jun 2019 11:04:34 -0400 Subject: [PATCH 05/16] Update the update checker code for settings pull. --- classifai.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/classifai.php b/classifai.php index b74e600ec..f8065c295 100644 --- a/classifai.php +++ b/classifai.php @@ -140,7 +140,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 From c84c898374ae30a160a2fb86d29c6568a5b8fa2a Mon Sep 17 00:00:00 2001 From: JayWood Date: Wed, 26 Jun 2019 11:32:01 -0400 Subject: [PATCH 06/16] Register classifai CLI command set - fixes #91 --- classifai.php | 28 +++++++++++++++++-- .../Classifai/Command/ClassifaiCommand.php | 6 ++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/classifai.php b/classifai.php index f8065c295..c6bac61fc 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() ); + error_log( get_error_install_message() ); } diff --git a/includes/Classifai/Command/ClassifaiCommand.php b/includes/Classifai/Command/ClassifaiCommand.php index 729b908d3..6719527e9 100644 --- a/includes/Classifai/Command/ClassifaiCommand.php +++ b/includes/Classifai/Command/ClassifaiCommand.php @@ -309,3 +309,9 @@ private function print( $output, $post_id ) { } } + +try { + \WP_CLI::add_command( 'classifai', __NAMESPACE__ . '\\ClassifaiCommand' ); +} catch ( \Exception $e ) { + error_log( $e->getMessage() ); +} From fe494ad65132c5f6e29398fa388cdc8479ae082e Mon Sep 17 00:00:00 2001 From: JayWood Date: Wed, 26 Jun 2019 11:52:16 -0400 Subject: [PATCH 07/16] Allow each provider class to have it's own reset settings method. --- includes/Classifai/Helpers.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/includes/Classifai/Helpers.php b/includes/Classifai/Helpers.php index 34ae274dd..b6c091ec1 100644 --- a/includes/Classifai/Helpers.php +++ b/includes/Classifai/Helpers.php @@ -97,7 +97,23 @@ function reset_plugin_settings() { ], ]; - update_option( 'classifai_settings', $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; + } + } + } } From bce2a9c1cd72e1fcbc5c94c1405c12a9bf685da3 Mon Sep 17 00:00:00 2001 From: JayWood Date: Wed, 26 Jun 2019 11:53:08 -0400 Subject: [PATCH 08/16] Actually run the reset method if possible. --- includes/Classifai/Helpers.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/Classifai/Helpers.php b/includes/Classifai/Helpers.php index b6c091ec1..2101f65ce 100644 --- a/includes/Classifai/Helpers.php +++ b/includes/Classifai/Helpers.php @@ -112,6 +112,8 @@ function reset_plugin_settings() { if ( ! $provider_class instanceof Provider || method_exists( $provider_class, 'reset_settings' ) ) { continue; } + + $provider_class->register_settings(); } } } From 08dc40e4bae30801bfa9d05d69dafbca993c155b Mon Sep 17 00:00:00 2001 From: JayWood Date: Wed, 26 Jun 2019 11:53:29 -0400 Subject: [PATCH 09/16] Fix logic. --- includes/Classifai/Helpers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Classifai/Helpers.php b/includes/Classifai/Helpers.php index 2101f65ce..dec19e4fa 100644 --- a/includes/Classifai/Helpers.php +++ b/includes/Classifai/Helpers.php @@ -109,7 +109,7 @@ function reset_plugin_settings() { } foreach ( $service_class->provider_classes as $provider_class ) { - if ( ! $provider_class instanceof Provider || method_exists( $provider_class, 'reset_settings' ) ) { + if ( ! $provider_class instanceof Provider || ! method_exists( $provider_class, 'reset_settings' ) ) { continue; } From e98942a45c56f55b6d5f40988bfe106197a20df5 Mon Sep 17 00:00:00 2001 From: JayWood Date: Wed, 26 Jun 2019 11:56:06 -0400 Subject: [PATCH 10/16] Implement methods and require the new reset_settings() method for all providers. --- includes/Classifai/Providers/AWS/Comprehend.php | 7 +++++++ includes/Classifai/Providers/Azure/ComputerVision.php | 7 +++++++ includes/Classifai/Providers/Provider.php | 5 +++++ includes/Classifai/Providers/Watson/NLU.php | 7 +++++++ 4 files changed, 26 insertions(+) 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 750141d54..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 */ diff --git a/includes/Classifai/Providers/Watson/NLU.php b/includes/Classifai/Providers/Watson/NLU.php index 3de45472e..095ba12a4 100644 --- a/includes/Classifai/Providers/Watson/NLU.php +++ b/includes/Classifai/Providers/Watson/NLU.php @@ -66,6 +66,13 @@ public function __construct( $service ) { ]; } + /** + * Resets the settings for the NLU provider. + */ + public function reset_settings() { + // TODO: Implement reset_settings() method. + } + /** * Can the functionality be initialized? * From 32de487525d376e7b9511e81bf73714585bb65f1 Mon Sep 17 00:00:00 2001 From: JayWood Date: Wed, 26 Jun 2019 11:56:58 -0400 Subject: [PATCH 11/16] Update CLI comment to be plugin-wide. --- includes/Classifai/Command/ClassifaiCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/Classifai/Command/ClassifaiCommand.php b/includes/Classifai/Command/ClassifaiCommand.php index 6719527e9..5e12a4af7 100644 --- a/includes/Classifai/Command/ClassifaiCommand.php +++ b/includes/Classifai/Command/ClassifaiCommand.php @@ -234,8 +234,8 @@ 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. From 1e180e7d4d3159c85b91ad797ca9c486284731b9 Mon Sep 17 00:00:00 2001 From: JayWood Date: Wed, 26 Jun 2019 11:57:28 -0400 Subject: [PATCH 12/16] One-line, for CLI specifically. --- includes/Classifai/Command/ClassifaiCommand.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/includes/Classifai/Command/ClassifaiCommand.php b/includes/Classifai/Command/ClassifaiCommand.php index 5e12a4af7..afb632cc6 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. Any API credentials will - * need to be re-entered after this is ran. + * 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. From 92854a9c423e5ca7c15b3a05d51842d931e24c12 Mon Sep 17 00:00:00 2001 From: JayWood Date: Wed, 26 Jun 2019 12:01:03 -0400 Subject: [PATCH 13/16] Use correct method, mis-spelling on my IDE's part.... --- includes/Classifai/Helpers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Classifai/Helpers.php b/includes/Classifai/Helpers.php index dec19e4fa..c9db91305 100644 --- a/includes/Classifai/Helpers.php +++ b/includes/Classifai/Helpers.php @@ -113,7 +113,7 @@ function reset_plugin_settings() { continue; } - $provider_class->register_settings(); + $provider_class->reset_settings(); } } } From 1365cc8526fd04e6a87a1b383f8b785e8d000bfd Mon Sep 17 00:00:00 2001 From: JayWood Date: Wed, 26 Jun 2019 12:19:56 -0400 Subject: [PATCH 14/16] Actually reset NLU settings. Update classifai option set to new format. --- includes/Classifai/Helpers.php | 35 +++++++-------------- includes/Classifai/Providers/Watson/NLU.php | 26 ++++++++++++++- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/includes/Classifai/Helpers.php b/includes/Classifai/Helpers.php index c9db91305..7f89d3677 100644 --- a/includes/Classifai/Helpers.php +++ b/includes/Classifai/Helpers.php @@ -70,32 +70,19 @@ 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, - ], - ]; + + $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. + update_option( 'classifai_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'] : '', + ] ); + } $services = get_plugin()->services; if ( ! isset( $services['service_manager'] ) || ! $services['service_manager']->service_classes ) { diff --git a/includes/Classifai/Providers/Watson/NLU.php b/includes/Classifai/Providers/Watson/NLU.php index 095ba12a4..968bd5975 100644 --- a/includes/Classifai/Providers/Watson/NLU.php +++ b/includes/Classifai/Providers/Watson/NLU.php @@ -70,7 +70,31 @@ public function __construct( $service ) { * Resets the settings for the NLU provider. */ public function reset_settings() { - // TODO: Implement reset_settings() method. + $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 ); } /** From 9007082a390c46885cfcf496c2656b8fc01edb7e Mon Sep 17 00:00:00 2001 From: JayWood Date: Wed, 26 Jun 2019 12:20:43 -0400 Subject: [PATCH 15/16] Update confirmation message to be globally accurate, not just Watson. --- includes/Classifai/Command/ClassifaiCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Classifai/Command/ClassifaiCommand.php b/includes/Classifai/Command/ClassifaiCommand.php index afb632cc6..198108a02 100644 --- a/includes/Classifai/Command/ClassifaiCommand.php +++ b/includes/Classifai/Command/ClassifaiCommand.php @@ -249,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.' ); } From 4962ed8e713ccd03e902d42ca27c976304e315eb Mon Sep 17 00:00:00 2001 From: JayWood Date: Wed, 26 Jun 2019 12:27:51 -0400 Subject: [PATCH 16/16] Resolve PHPcs build issues. --- classifai.php | 2 +- includes/Classifai/Helpers.php | 7 ++++--- includes/Classifai/Plugin.php | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/classifai.php b/classifai.php index c6bac61fc..66ecc55d1 100644 --- a/classifai.php +++ b/classifai.php @@ -142,7 +142,7 @@ function classifai_autorun() { * Generate a notice if autoload fails. */ function classifai_autoload_notice() { - printf( '

%2$s

', 'notice notice-error', get_error_install_message() ); + printf( '

%2$s

', 'notice notice-error', get_error_install_message() ); // @codingStandardsIgnoreLine Text is escaped in calling function already. error_log( get_error_install_message() ); } diff --git a/includes/Classifai/Helpers.php b/includes/Classifai/Helpers.php index 7f89d3677..9d3ad68c2 100644 --- a/includes/Classifai/Helpers.php +++ b/includes/Classifai/Helpers.php @@ -29,7 +29,7 @@ function get_plugin() { */ function get_plugin_settings( $service = '' ) { $services = Plugin::$instance->services; - if ( empty( $services ) || empty( $services['service_manager'] ) || ! $services['service_manager'] instanceof ServicesManager ) { + if ( empty( $services ) || empty( $services['service_manager'] ) || ! $services['service_manager'] instanceof ServicesManager ) { return []; } @@ -77,11 +77,12 @@ function reset_plugin_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. - update_option( 'classifai_settings', [ + $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; diff --git a/includes/Classifai/Plugin.php b/includes/Classifai/Plugin.php index ac067552f..9191003e2 100644 --- a/includes/Classifai/Plugin.php +++ b/includes/Classifai/Plugin.php @@ -70,7 +70,7 @@ public function init_services() { 'classifai_services', [ 'language_processing' => 'Classifai\Services\LanguageProcessing', - 'image_processing' => 'Classifai\Services\ImageProcessing' + 'image_processing' => 'Classifai\Services\ImageProcessing', ] );