From e705b775a92b49c6c916633a28405f2db3427c25 Mon Sep 17 00:00:00 2001 From: Justin Hayes Date: Wed, 22 Mar 2023 10:53:01 -0400 Subject: [PATCH] Refactor for modularity --- .gitignore | 4 +- openai-moderation/README.md | 50 ++++++++++ .../includes/openai-moderation-moderate.php | 70 ++++++++++++++ .../includes/openai-moderation-settings.php | 93 +------------------ .../includes/openai-moderation-utils.php | 8 ++ openai-moderation/openai-moderation.php | 31 +++++++ 6 files changed, 166 insertions(+), 90 deletions(-) create mode 100644 openai-moderation/README.md create mode 100644 openai-moderation/includes/openai-moderation-moderate.php rename openai-moderation.php => openai-moderation/includes/openai-moderation-settings.php (61%) create mode 100644 openai-moderation/includes/openai-moderation-utils.php create mode 100644 openai-moderation/openai-moderation.php diff --git a/.gitignore b/.gitignore index 5469669..fb55eb3 100644 --- a/.gitignore +++ b/.gitignore @@ -45,4 +45,6 @@ wp-config.php # # Note: If you wish to whitelist themes, # uncomment the next line -#/wp-content/themes \ No newline at end of file +#/wp-content/themes + +.DS_Store \ No newline at end of file diff --git a/openai-moderation/README.md b/openai-moderation/README.md new file mode 100644 index 0000000..ccab920 --- /dev/null +++ b/openai-moderation/README.md @@ -0,0 +1,50 @@ +# OpenAI Moderation WordPress Plugin + +This WordPress plugin filters input fields in text areas using the [OpenAI Moderation API](https://platform.openai.com/docs/guides/moderation/overview). It helps ensure that user-submitted content is respectful and allows you to control which content classifications are allowed on your website. + +### Forever Free, No Pro Version + +We’re proud to announce that this plugin will always be 100% free, with no plans for a “Pro” or paid version. Our goal is to provide a reliable and accessible solution for the community, and we’re committed to keeping it that way. + +## Features + +- Configure the OpenAI API key and allowed classifications. +- Enable or disable the plugin easily from the settings page. +- Moderate comments before they are saved in the database. +- Show an error message to users when their comment contains content that violates the allowed classifications. + +## Installation + +1. Download the plugin's ZIP file and extract it to the `/wp-content/plugins/` directory, or install the plugin through the WordPress plugins screen directly. +2. Activate the plugin through the 'Plugins' screen in WordPress. +3. Navigate to the 'Settings' > 'OpenAI Moderation' screen to configure the plugin. + +## Configuration + +1. Obtain an OpenAI API key by signing up at https://beta.openai.com/signup/ and going to this page https://platform.openai.com/account/api-keys. +2. Go to the 'Settings' > 'OpenAI Moderation' screen in your WordPress admin area. +3. Enter your OpenAI API key in the 'OpenAI API Key' field. +4. Select the content categories you want to allow in the 'Allowed Classifications' field. +5. Check the 'Enable OpenAI Moderation' checkbox to enable the plugin. +6. Click 'Save Changes' to save your settings. + +## Usage + +Once the plugin is enabled and configured, it will automatically moderate comments on your website. If a user tries to post a comment that contains content that violates the allowed +classifications, they will see an error message and the comment will not be posted. + +You can extend the plugin to moderate other types of content, such as user-submitted posts, by using the appropriate hooks and filters in WordPress. + +## Troubleshooting + +If the plugin is not working as expected, make sure that your OpenAI API key is valid and that the 'Enable OpenAI Moderation' checkbox is checked in the plugin settings. + +## Support and Contributions + +While we are thrilled to receive your pull requests and issue reports, please note that we cannot guarantee support or address all feedback. Our team will prioritize security-related reports, as outlined in our security policy. We appreciate your understanding, and we encourage you to contribute to the ongoing improvement of this plugin. + +Thank you for using and supporting our plugin! + +## License + +This plugin is licensed under the GPLv2 license. See the LICENSE file for more information. diff --git a/openai-moderation/includes/openai-moderation-moderate.php b/openai-moderation/includes/openai-moderation-moderate.php new file mode 100644 index 0000000..6a518c1 --- /dev/null +++ b/openai-moderation/includes/openai-moderation-moderate.php @@ -0,0 +1,70 @@ + 'application/json', + 'Authorization' => 'Bearer ' . $api_key + ); + $body = array( + 'input' => $content + ); + + $response = wp_remote_post($url, array( + 'headers' => $headers, + 'body' => json_encode($body), + 'timeout' => 30 + )); + + if (is_wp_error($response)) { + return false; + } + + $response_body = json_decode(wp_remote_retrieve_body($response), true); + if (!$response_body || !isset($response_body['results']) || !isset($response_body['results'][0])) { + return false; + } + + $moderation_result = $response_body['results'][0]; + return $moderation_result; + } + + public function moderate_comment($comment_data) + { + $content = $comment_data['comment_content']; + $moderation_result = $this->moderate_content($content); + + if (!$moderation_result || !$moderation_result['flagged']) { + return $comment_data; + } + + $allowed_classifications = get_option('openai_classifications'); + $allowed_classifications = array_map('trim', $allowed_classifications); + + $violates_policies = false; + foreach ($moderation_result['categories'] as $category => $flagged) { + if ($flagged && in_array($category, $allowed_classifications)) { + $violates_policies = true; + break; + } + } + + if ($violates_policies) { + wp_die(__('Your comment could not be posted as it contains content that violates our policies.', 'openai-moderation')); + } + + return $comment_data; + } +} \ No newline at end of file diff --git a/openai-moderation.php b/openai-moderation/includes/openai-moderation-settings.php similarity index 61% rename from openai-moderation.php rename to openai-moderation/includes/openai-moderation-settings.php index e11a4d5..0189a0e 100644 --- a/openai-moderation.php +++ b/openai-moderation/includes/openai-moderation-settings.php @@ -1,32 +1,10 @@ OpenAI Moderation API. -Version: 1.0.2 -Author: RAHB -Author URI: https://github.com/RAHB-REALTORS-Association -License: GPLv2 -Text Domain: openai-moderation -*/ - -if (!defined('ABSPATH')) { - exit; -} - -class OpenAIModeration +class OpenAIModeration_Settings { public function __construct() { add_action('admin_menu', [$this, 'register_settings_submenu']); add_action('admin_init', [$this, 'register_settings']); - add_action('plugins_loaded', [$this, 'load_text_domain']); - add_filter('preprocess_comment', [$this, 'moderate_comment']); - } - - public function load_text_domain() - { - load_plugin_textdomain('openai-moderation', false, basename(dirname(__FILE__)) . '/languages'); } public function register_settings_submenu() @@ -50,7 +28,8 @@ public function register_settings() register_setting('openai-moderation', 'openai_plugin_enabled'); } - public function settings_page() { + public function settings_page() + { $allowed_classifications_options = array( 'hate' => __('Hate', 'openai-moderation'), 'hate/threatening' => __('Hate/Threatening', 'openai-moderation'), @@ -105,68 +84,6 @@ public function settings_page() { 'application/json', - 'Authorization' => 'Bearer ' . $api_key - ); - $body = array( - 'input' => $content - ); - - $response = wp_remote_post($url, array( - 'headers' => $headers, - 'body' => json_encode($body), - 'timeout' => 30 - )); - - if (is_wp_error($response)) { - return false; - } - - $response_body = json_decode(wp_remote_retrieve_body($response), true); - if (!$response_body || !isset($response_body['results']) || !isset($response_body['results'][0])) { - return false; - } - - $moderation_result = $response_body['results'][0]; - return $moderation_result; - } - - public function moderate_comment($comment_data) - { - $content = $comment_data['comment_content']; - $moderation_result = $this->moderate_content($content); - - if (!$moderation_result || !$moderation_result['flagged']) { - return $comment_data; - } - - $allowed_classifications = get_option('openai_classifications'); - $allowed_classifications = array_map('trim', $allowed_classifications); - - $violates_policies = false; - foreach ($moderation_result['categories'] as $category => $flagged) { - if ($flagged && in_array($category, $allowed_classifications)) { - $violates_policies = true; - break; - } - } - - if ($violates_policies) { - wp_die(__('Your comment could not be posted as it contains content that violates our policies.', 'openai-moderation')); - } - - return $comment_data; - } - public function sanitize_classifications($classifications) { if (!is_array($classifications)) { @@ -181,6 +98,4 @@ public function sanitize_classifications($classifications) return in_array($classification, $allowed_classifications, true); })); } -} - -new OpenAIModeration(); +} \ No newline at end of file diff --git a/openai-moderation/includes/openai-moderation-utils.php b/openai-moderation/includes/openai-moderation-utils.php new file mode 100644 index 0000000..763c365 --- /dev/null +++ b/openai-moderation/includes/openai-moderation-utils.php @@ -0,0 +1,8 @@ +OpenAI Moderation API. +Version: 1.1 +Author: RAHB +Author URI: https://github.com/RAHB-REALTORS-Association +License: GPLv2 +Text Domain: openai-moderation +*/ + +if (!defined('ABSPATH')) { + exit; +} + +require_once plugin_dir_path(__FILE__) . 'includes/openai-moderation-settings.php'; +require_once plugin_dir_path(__FILE__) . 'includes/openai-moderation-moderate.php'; +require_once plugin_dir_path(__FILE__) . 'includes/openai-moderation-utils.php'; + +class OpenAIModeration +{ + public function __construct() + { + add_action('plugins_loaded', [OpenAIModeration_Utils::class, 'load_text_domain']); + new OpenAIModeration_Settings(); + new OpenAIModeration_Moderate(); + } +} + +new OpenAIModeration(); \ No newline at end of file