From 425d614446bf21c93a5e3705c8daa75125f48c32 Mon Sep 17 00:00:00 2001 From: Kim Date: Sat, 14 Sep 2024 15:57:00 +0200 Subject: [PATCH] Create ai_content_restrictor_1.3.php 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 --- ai_content_restrictor_1.3.php | 175 ++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 ai_content_restrictor_1.3.php diff --git a/ai_content_restrictor_1.3.php b/ai_content_restrictor_1.3.php new file mode 100644 index 0000000..ef1ab74 --- /dev/null +++ b/ai_content_restrictor_1.3.php @@ -0,0 +1,175 @@ + +
+

AI Content Restrictor Settings

+
+ +
+
+ "; +} + +// 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 ""; + echo ""; + echo ""; +} + +// Settings field callback for excluded tags (tags as a comma-separated list) +function excluded_tags_callback() { + $tags = get_option('excluded_tags'); + echo ""; + echo "

Enter the tags to exclude, separated by commas (e.g., 'tag1, tag2, tag3').

"; +} + +// 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');