-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44e483f
commit e705b77
Showing
6 changed files
with
166 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |