Skip to content

Commit

Permalink
Committing built version of ee877af
Browse files Browse the repository at this point in the history
  • Loading branch information
10upbot on GitHub committed Jul 24, 2019
1 parent 282e3f0 commit 12c3307
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 69 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ![ClassifAI](https://classifaiplugin.com/wp-content/themes/classifai-theme/assets/img/logo.svg "ClassifAI")
> Enhance your WordPress content with Artificial Intelligence and Machine Learning services.
[![Build Status](https://travis-ci.com/10up/classifai.svg?token=Jy6DFK4YVZbgtyNHcjm5&branch=develop)](https://travis-ci.com/10up/classifai) [![Release Version](https://img.shields.io/github/release/10up/classifai.svg)](https://github.com/10up/classifai/releases/latest) ![WordPress tested up to version](https://img.shields.io/badge/WordPress-v5.2%20tested-success.svg) [![MIT License](https://img.shields.io/github/license/10up/classifai.svg)](https://github.com/10up/classifai/blob/develop/LICENSE.md)
[![Support Level](https://img.shields.io/badge/support-active-green.svg)](#support-level) [![Build Status](https://travis-ci.com/10up/classifai.svg?token=Jy6DFK4YVZbgtyNHcjm5&branch=develop)](https://travis-ci.com/10up/classifai) [![Release Version](https://img.shields.io/github/release/10up/classifai.svg)](https://github.com/10up/classifai/releases/latest) ![WordPress tested up to version](https://img.shields.io/badge/WordPress-v5.2%20tested-success.svg) [![MIT License](https://img.shields.io/github/license/10up/classifai.svg)](https://github.com/10up/classifai/blob/develop/LICENSE.md)

## Table of Contents
* [Features](#features)
Expand All @@ -10,6 +10,8 @@
* [Set Up Content Tagging](#set-up-content-tagging-via-ibm-watson)
* [Set Up Image Processing](#set-up-image-processing-via-microsoft-azure)
* [WP CLI Usage Instructions](#wp-cli-usage-instructions)
* [Data Gathering](#data-gathering)
* [Support](#support-level)
* [Changelog](#changelog)
* [Contributing](#contributing)

Expand Down Expand Up @@ -164,6 +166,14 @@ Prints the normalized text that will be sent to the NLU API

default: `false`

## Data Gathering

ClassifAI connects your WordPress site directly to your account with specific service provider(s) (e.g. Microsoft Azure AI, IBM Watson), so no data is gathered by 10up. The data gathered in our [registration form](https://classifaiplugin.com/#cta) is used simply to stay in touch with users so we can provide product updates and news. More information is available in the [Privacy Policy on ClassifAIplugin.com](https://drive.google.com/open?id=1Hn4XEWmNGqeMzLqnS7Uru2Hl2vJeLc7cI7225ztThgQ).

## Support Level

**Active:** 10up is actively working on this, and we expect to continue work for the foreseeable future including keeping tested up to the most recent version of WordPress. Bug reports, feature requests, questions, and pull requests are welcome.

## Changelog

A complete listing of all notable changes to Distributor are documented in [CHANGELOG.md](https://github.com/10up/classifai/blob/develop/CHANGELOG.md).
Expand Down
33 changes: 29 additions & 4 deletions classifai.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: ClassifAI
* Plugin URI: https://github.com/10up/classifai
* Description: Enhance your WordPress content with Artificial Intelligence and Machine Learning services.
* Version: 1.3.1
* Version: 1.3.2
* Author: 10up
* Author URI: https://10up.com
* License: MIT
Expand Down 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
4 changes: 2 additions & 2 deletions config.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
/**
* Global Constants for the Classifai Support Plugin. Constants should be
* Global Constants for the ClassifAI Support Plugin. Constants should be
* declared here instead of a Class.
*/

$plugin_version = '1.2.0';
$plugin_version = '1.3.2';

if ( file_exists( __DIR__ . '/.commit' ) ) {
$plugin_version .= '-' . file_get_contents( __DIR__ . '/.commit' );
Expand Down
10 changes: 7 additions & 3 deletions includes/Classifai/Admin/SavePostHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ public function register() {
* Save Post handler only runs on admin or REST requests
*/
public function can_register() {
if ( is_admin() ) {
if ( ! get_option( 'classifai_configured', false ) ) {
return false;
} elseif ( empty( get_option( 'classifai_watson_nlu' ) ) ) {
return false;
} elseif ( empty( get_option( 'classifai_watson_nlu' )['credentials']['watson_url'] ) ) {
return false;
} elseif ( is_admin() ) {
return true;
} elseif ( $this->is_rest_route() ) {
return true;
} elseif ( defined( 'PHPUNIT_RUNNER' ) && PHPUNIT_RUNNER ) {
return false;
} elseif ( defined( 'WP_CLI' ) && WP_CLI ) {
return false;
} elseif ( ! get_option( 'classifai_configured', false ) ) {
return false;
} else {
return false;
}
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
20 changes: 12 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 @@ -48,6 +48,7 @@ public function init() {
'_classifai_error',
[
'show_in_rest' => true,
'single' => true,
]
);
}
Expand All @@ -66,14 +67,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
Loading

0 comments on commit 12c3307

Please sign in to comment.