Skip to content

Commit

Permalink
Added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
justinh-rahb committed Mar 25, 2023
1 parent a7d32e1 commit 9491d6a
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions openai-moderation/includes/openai-moderation-moderate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ class OpenAIModeration_Moderate
{
public function __construct()
{
// Add the moderation filter to the comment form.
add_filter('preprocess_comment', [$this, 'moderate_comment']);
}

// Moderate the content using the OpenAI Moderation API.
public function moderate_content($content)
{
$api_key = get_option('openai_api_key');
if (!$api_key || !get_option('openai_plugin_enabled')) {
return false;
}

// Make the request to the OpenAI Moderation API.
$url = 'https://api.openai.com/v1/moderations';
$headers = array(
'Content-Type' => 'application/json',
Expand All @@ -28,10 +31,12 @@ public function moderate_content($content)
'timeout' => 30
));

// If the request fails, return false.
if (is_wp_error($response)) {
return false;
}

// If the request succeeds, return the moderation result.
$response_body = json_decode(wp_remote_retrieve_body($response), true);
if (!$response_body || !isset($response_body['results']) || !isset($response_body['results'][0])) {
return false;
Expand All @@ -41,26 +46,30 @@ public function moderate_content($content)
return $moderation_result;
}

// Moderate the comment and redirect to the error page if it violates the configured policies.
public function moderate_comment($comment_data)
{
$content = $comment_data['comment_content'];
$moderation_result = $this->moderate_content($content);


// If the comment does not violate the configured policies, return the comment data.
if (!$moderation_result || !$moderation_result['flagged']) {
return $comment_data;
}

$disallowed_classifications = get_option('openai_classifications');
$disallowed_classifications = array_map('trim', $disallowed_classifications);


// Check if the comment violates the configured policies.
$violates_policies = false;
foreach ($moderation_result['categories'] as $category => $flagged) {
if ($flagged && in_array($category, $disallowed_classifications)) {
$violates_policies = true;
break;
}
}


// If the comment violates the configured policies, redirect to the error page.
if ($violates_policies) {
$error_page_id = get_option('openai_error_page');
if ($error_page_id) {
Expand All @@ -74,7 +83,7 @@ public function moderate_comment($comment_data)
wp_die(__('Your comment could not be posted as it contains content that violates our policies.', 'openai-moderation'));
}
}

return $comment_data;
}
}

0 comments on commit 9491d6a

Please sign in to comment.