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

Fix NLU Settings and Wp-CLI #96

Merged
merged 16 commits into from
Jul 24, 2019
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
31 changes: 28 additions & 3 deletions classifai.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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' );
}
}
Expand All @@ -118,8 +142,8 @@ function classifai_autorun() {
* Generate a notice if autoload fails.
*/
function classifai_autoload_notice() {
printf( '<div class="%1$s"><p>%2$s</p></div>', '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( '<div class="%1$s"><p>%2$s</p></div>', 'notice notice-error', get_error_install_message() ); // @codingStandardsIgnoreLine Text is escaped in calling function already.
error_log( get_error_install_message() );
}


Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions includes/Classifai/Command/ClassifaiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.'
);
}

Expand Down Expand Up @@ -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() );
}
103 changes: 67 additions & 36 deletions includes/Classifai/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
}

/**
Expand All @@ -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();
}
}
}


Expand All @@ -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'] ) ) {
Expand All @@ -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'] ) ) {
Expand All @@ -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'] ) ) {
Expand All @@ -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 = [];
Expand Down Expand Up @@ -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 ] ) ) {
Expand All @@ -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'] ) ) {
Expand Down Expand Up @@ -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' ] ) ) {
Expand Down
19 changes: 11 additions & 8 deletions includes/Classifai/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Plugin {
/**
* @var array $services The known list of services.
*/
protected $services = [];
public $services = [];

/**
* Lazy initialize the plugin
Expand Down Expand Up @@ -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 ) {
Expand Down
7 changes: 7 additions & 0 deletions includes/Classifai/Providers/AWS/Comprehend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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?
*
Expand Down
7 changes: 7 additions & 0 deletions includes/Classifai/Providers/Azure/ComputerVision.php
Original file line number Diff line number Diff line change
Expand Up @@ -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?
*
Expand Down
7 changes: 6 additions & 1 deletion includes/Classifai/Providers/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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 );
Expand Down
33 changes: 32 additions & 1 deletion includes/Classifai/Providers/Watson/NLU.php
Original file line number Diff line number Diff line change
Expand Up @@ -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?
*
Expand Down Expand Up @@ -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(), [] );

Expand Down
Loading