Skip to content

Commit

Permalink
Create ai_content_restrictor_1.3.php
Browse files Browse the repository at this point in the history
Explanation:

Excluded Tags Setting:
The excluded_tags setting allows admins to define a list of tags that should be blocked. This is inputted as a comma-separated string (e.g., tag1, tag2, tag3).
Checking for Excluded Tags:

The check_for_excluded_tags() function checks whether an article contains any tags that are in the admin-defined excluded list. If it finds an excluded tag, the article is not published.
Unpublishing Posts with Excluded Tags:

The function unpublish_posts_with_excluded_tags() checks the already published posts for excluded tags and unpublishes them by changing their status to draft.
Scheduling:

A WordPress cron job is set up to periodically check for posts that contain excluded tags and unpublish them. This is scheduled daily.

How to Use:

In the Admin Panel:
Go to the "AI Content Restrictor" settings page.
Enter restricted categories and user-defined sources as before.
Add a comma-separated list of excluded tags in the new "Excluded Tags" field.

Crawling and Tag Checking:
The plugin will
  • Loading branch information
kimjac authored Sep 14, 2024
1 parent b01a314 commit 425d614
Showing 1 changed file with 175 additions and 0 deletions.
175 changes: 175 additions & 0 deletions ai_content_restrictor_1.3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php
/*
Plugin Name: AI Content Restrictor with User-Defined Sources and Tag Exclusion
Description: Crawls external sources, classifies content, and restricts publication based on admin criteria, user-defined sources, and excluded tags.
Version: 1.3
Author: Kim Jacobsen
*/

// Hook to initialize the plugin and add menu
add_action('admin_menu', 'ai_content_restrictor_menu');
function ai_content_restrictor_menu() {
add_menu_page('AI Content Restrictor Settings', 'AI Content Restrictor', 'manage_options', 'ai-content-restrictor', 'ai_content_restrictor_settings_page');
}

// Settings Page in Admin Panel
function ai_content_restrictor_settings_page() {
?>
<div class="wrap">
<h1>AI Content Restrictor Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields('ai_content_restrictor_settings');
do_settings_sections('ai-content-restrictor');
submit_button();
?>
</form>
</div>
<?php
}

// Register settings
add_action('admin_init', 'ai_content_restrictor_settings');
function ai_content_restrictor_settings() {
// Register settings for restricted categories, user-defined sources, and excluded tags
register_setting('ai_content_restrictor_settings', 'restricted_categories');
register_setting('ai_content_restrictor_settings', 'user_defined_sources');
register_setting('ai_content_restrictor_settings', 'excluded_tags');

// Add sections and fields
add_settings_section('ai_content_restrictor_main', 'Main Settings', null, 'ai-content-restrictor');
add_settings_field('restricted_categories', 'Restricted Categories', 'restricted_categories_callback', 'ai-content-restrictor', 'ai_content_restrictor_main');
add_settings_field('user_defined_sources', 'User-Defined Sources', 'user_defined_sources_callback', 'ai-content-restrictor', 'ai_content_restrictor_main');
add_settings_field('excluded_tags', 'Excluded Tags', 'excluded_tags_callback', 'ai-content_restrictor', 'ai_content_restrictor_main');
}

// Settings field callback for restricted categories
function restricted_categories_callback() {
$options = get_option('restricted_categories');
echo "<input type='text' name='restricted_categories' value='$options' />";
}

// Settings field callback for user-defined sources (multiple URLs as a list)
function user_defined_sources_callback() {
$sources = get_option('user_defined_sources');
$sources_list = explode(PHP_EOL, $sources); // Convert to array
echo "<ul id='source-list'>";
foreach ($sources_list as $source_url) {
echo "<li><input type='text' name='user_defined_sources[]' value='".esc_attr(trim($source_url))."' /> <button class='remove-source'>Remove</button></li>";
}
echo "</ul>";
echo "<button id='add-source'>Add New Source</button>";
echo "<script type='text/javascript'>
document.getElementById('add-source').addEventListener('click', function() {
var list = document.getElementById('source-list');
var newItem = document.createElement('li');
newItem.innerHTML = \"<input type='text' name='user_defined_sources[]' /> <button class='remove-source'>Remove</button>\";
list.appendChild(newItem);
});
</script>";
}

// Settings field callback for excluded tags (tags as a comma-separated list)
function excluded_tags_callback() {
$tags = get_option('excluded_tags');
echo "<input type='text' name='excluded_tags' value='$tags' />";
echo "<p>Enter the tags to exclude, separated by commas (e.g., 'tag1, tag2, tag3').</p>";
}

// Save the list of sources as a single string
add_action('update_option_user_defined_sources', 'save_user_defined_sources', 10, 2);
function save_user_defined_sources($old_value, $value) {
if (is_array($value)) {
$value = implode(PHP_EOL, array_map('trim', $value));
}
update_option('user_defined_sources', $value);
}

// Function to Crawl Content from User-Defined Sources
function crawl_user_defined_sources() {
$sources = get_option('user_defined_sources');
$sources_array = explode(PHP_EOL, $sources); // Split by new lines

foreach ($sources_array as $source_url) {
$source_url = trim($source_url);
if (!empty($source_url)) {
crawl_external_content($source_url); // Call existing function to crawl content
}
}
}

// Modify the Crawl External Content Function to Include AI Classification and Tag Checking
function crawl_external_content($source_url) {
$response = wp_remote_get($source_url);
if (is_wp_error($response)) {
return;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true); // Assuming the content is in JSON format

// Example logic for crawling and restricting content
foreach ($data['articles'] as $article) {
$category = classify_content($article['text']); // AI classification
$tags = $article['tags']; // Assuming tags are provided in the crawled content

// Get restricted categories and excluded tags
$restricted_categories = explode(',', get_option('restricted_categories'));
$excluded_tags = explode(',', get_option('excluded_tags'));

// Check if the article contains any excluded tags
if (!check_for_excluded_tags($tags, $excluded_tags) && !in_array(trim($category), $restricted_categories)) {
publish_article($article); // Publish the article if not restricted
}
}
}

// Function to Check for Excluded Tags
function check_for_excluded_tags($article_tags, $excluded_tags) {
foreach ($excluded_tags as $excluded_tag) {
$excluded_tag = trim($excluded_tag);
if (in_array($excluded_tag, $article_tags)) {
return true; // Found an excluded tag, block the article
}
}
return false; // No excluded tags found
}

// Example AI Classification Function (Modify with a real AI API)
function classify_content($content) {
$api_url = 'https://mock-ai-api.com/classify'; // Replace with a real AI API URL
$response = wp_remote_post($api_url, array(
'body' => json_encode(array('text' => $content)),
'headers' => array('Content-Type' => 'application/json')
));

if (is_wp_error($response)) {
return 'unknown'; // Default if AI fails
}

$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);

return $data['category']; // Return the category classified by the AI
}

// Function to Publish Article
function publish_article($article) {
$post_data = array(
'post_title' => wp_strip_all_tags($article['title']),
'post_content' => $article['content'],
'post_status' => 'publish',
'post_author' => 1,
'tags_input' => $article['tags'], // Add the tags to the post
);
wp_insert_post($post_data);
}

// Schedule the crawling process
add_action('wp', 'setup_crawl_schedule');
function setup_crawl_schedule() {
if (!wp_next_scheduled('crawl_user_defined_sources_event')) {
wp_schedule_event(time(), 'hourly', 'crawl_user_defined_sources_event');
}
}

add_action('crawl_user_defined_sources_event', 'crawl_user_defined_sources');

0 comments on commit 425d614

Please sign in to comment.