Skip to content

Commit

Permalink
Refactor for modularity
Browse files Browse the repository at this point in the history
  • Loading branch information
justinh-rahb committed Mar 22, 2023
1 parent 44e483f commit e705b77
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 90 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ wp-config.php
#
# Note: If you wish to whitelist themes,
# uncomment the next line
#/wp-content/themes
#/wp-content/themes

.DS_Store
50 changes: 50 additions & 0 deletions openai-moderation/README.md
Original file line number Diff line number Diff line change
@@ -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.
70 changes: 70 additions & 0 deletions openai-moderation/includes/openai-moderation-moderate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
class OpenAIModeration_Moderate
{
public function __construct()
{
add_filter('preprocess_comment', [$this, 'moderate_comment']);
}

public function moderate_content($content)
{
$api_key = get_option('openai_api_key');
if (!$api_key || !get_option('openai_plugin_enabled')) {
return false;
}

$url = 'https://api.openai.com/v1/moderations';
$headers = array(
'Content-Type' => '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;
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
<?php
/*
Plugin Name: OpenAI Moderation
Plugin URI: https://github.com/RAHB-REALTORS-Association/OpenAI-Moderation-WP
Description: A simple plugin that filters input fields in text areas using the <a href="https://platform.openai.com/docs/guides/moderation/overview" target="_blank">OpenAI Moderation API</a>.
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()
Expand All @@ -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'),
Expand Down Expand Up @@ -105,68 +84,6 @@ public function settings_page() {
<?php
}

public function moderate_content($content)
{
$api_key = get_option('openai_api_key');
if (!$api_key || !get_option('openai_plugin_enabled')) {
return false;
}

$url = 'https://api.openai.com/v1/moderations';
$headers = array(
'Content-Type' => '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)) {
Expand All @@ -181,6 +98,4 @@ public function sanitize_classifications($classifications)
return in_array($classification, $allowed_classifications, true);
}));
}
}

new OpenAIModeration();
}
8 changes: 8 additions & 0 deletions openai-moderation/includes/openai-moderation-utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
class OpenAIModeration_Utils
{
public static function load_text_domain()
{
load_plugin_textdomain('openai-moderation', false, basename(dirname(__FILE__)) . '/languages');
}
}
31 changes: 31 additions & 0 deletions openai-moderation/openai-moderation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*
Plugin Name: OpenAI Moderation
Plugin URI: https://github.com/RAHB-REALTORS-Association/OpenAI-Moderation-WP
Description: A simple plugin that filters input fields in text areas using the <a href="https://platform.openai.com/docs/guides/moderation/overview" target="_blank">OpenAI Moderation API</a>.
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();

0 comments on commit e705b77

Please sign in to comment.