' . esc_html__('Your settings have been successfully saved!', 'ai-comment-creator') . '
';
} else {
echo '
' . esc_html__('Sorry, there was an error saving your settings. Please try again later.', 'ai-comment-creator') . '
';
}
}
}
}
function aicommentcreator_get_wpcron_time($time){
switch($time){
case 1:
return 'hourly';
case 2:
return 'twicedaily';
case 3:
return 'daily';
case 4:
return 'weekly';
default:
return 'hourly';
}
}
function aicommentcreator_remove_element($array, $element){
$index = array_search($element, $array);
if($index !== false){
array_splice($array, $index, 1);
}
return $array;
}
function aicommentcreator_get_last_post_by_category($category_ids,$post_count = 30,$order_by = 'date',$order = 'DESC') {
$args = array(
'category__in' => $category_ids,
'orderby' => $order_by,
'order' => $order,
'numberposts' => $post_count
);
$posts = get_posts($args);
return $posts;
}
function aicommentcreator_get_user_info($json_data) {
$data = json_decode($json_data, true);
$user = $data["results"][0];
$name = $user["name"]["first"];
$last_name = $user["name"]["last"];
$email = $user["email"];
$user_avatar = $user["picture"]["large"];
$user_avatar_thumbnail = $user["picture"]["thumbnail"];
return array("name" => $name, "last_name" => $last_name, "email" => $email, "user_avatar" => $user_avatar, "user_avatar_thumbnail" => $user_avatar_thumbnail);
}
function aicommentcreator_parseHTML($string) {
$string = preg_replace('/<[^>]*>/', '', $string);
return $string;
}
function aicommentcreator_custom_avatar($avatar, $id_or_email, $size, $default, $alt) {
$comment = get_comment($id_or_email);
$comment_id = $comment->comment_ID;
$avatar_url = get_comment_meta($comment_id, 'avatar_url', true);
if (!empty($avatar_url)) {
$custom_avatar = $avatar_url;
} else {
$custom_avatar = get_avatar_url($id_or_email);
}
return "";
}
add_filter('get_avatar', 'aicommentcreator_custom_avatar', 10, 5);
function aicommentcreator_generate_comment_with_aicontentgenerator($domain_name,$prompt,$blog_post,$max_token, $selected_commment_text_language){
$url = 'https://www.aicontentgenerator.app/api/v3/generateComment';
$admin_email = get_option('admin_email', false);
if ($admin_email !== false) {
// seçenek varsa işlemlerinizi yapabilirsiniz
} else {
// seçenek yoksa, bir hata mesajı görüntüleyebilir veya varsayılan bir değer kullanabilirsiniz
$admin_email = 'email@example.com';
}
$data = array(
'userDomain' => $domain_name,
'userEmail' => sanitize_email($admin_email),
'maxToken' => $max_token,
'prompt' => $prompt,
'post' => $blog_post,
'comment_text_language' => $selected_commment_text_language
);
$response = wp_remote_post(
$url,
array(
'timeout' => 2400, // Timeout set to 2 minutes.
'headers' => array(
'Content-Type' => 'application/json'
),
'body' => json_encode($data)
)
);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
return "";
} else {
$response_data = wp_remote_retrieve_body($response);
$responseData = json_decode($response_data, true);
$text = $responseData['choices'][0]['text'];
return $text;
}
}
//Check subscription with WP-CRON - START
// Deactivation hook
register_deactivation_hook(__FILE__, 'aicommentcreator_subscription_checker_deactivate');
// Deactivation callback function
function aicommentcreator_subscription_checker_deactivate() {
// Check if the cron job is scheduled and unschedule it
if (wp_next_scheduled('aicommentcreator_check_subscription_event')) {
wp_clear_scheduled_hook('aicommentcreator_check_subscription_event');
}
}
// Uninstall hook
register_uninstall_hook(__FILE__, 'aicommentcreator_subscription_checker_uninstall');
// Uninstall callback function
function aicommentcreator_subscription_checker_uninstall() {
// Check if the cron job is scheduled and unschedule it
if (wp_next_scheduled('aicomentcreator_check_subscription_event')) {
wp_clear_scheduled_hook('aicomentcreator_check_subscription_event');
}
// Additional uninstall actions can be performed here if needed
}
// Activation hook
register_activation_hook( __FILE__, 'aicommentcreator_subscription_checker_activate' );
// Activation callback function
function aicommentcreator_subscription_checker_activate() {
// Check if the cron job is already scheduled
if ( ! wp_next_scheduled( 'aicommentcreator_check_subscription_event' ) ) {
// Schedule the cron job to run every 30 minutes
wp_schedule_event( time(), 'halfhourly', 'aicommentcreator_check_subscription_event' );
}
}
// Cron callback function
function aicommentcreator_check_subscription() {
// Get the subscription ID from plugin options
$subscription_id = get_user_from_aicontentgenerator_api()['subscription_id'];
// Check if the subscription ID is empty
if (empty($subscription_id)) {
return; // No subscription ID found, exit the function
}
// API request URL
$api_url = 'https://aicontentgenerator.app/api/v3/checkSubscription';
// API request parameters
$params = array(
'sub_id' => $subscription_id
);
// Perform the API request using WordPress HTTP API
$response = wp_remote_post($api_url, array('body' => $params));
// Check if the API request was successful
if (is_wp_error($response)) {
error_log('API request failed: ' . $response->get_error_message());
return;
}
// Get the response body
$body = wp_remote_retrieve_body($response);
// Parse the JSON response
$data = json_decode($body);
}
// Hook into the cron event
add_action( 'aicommentcreator_check_subscription_event', 'aicommentcreator_check_subscription' );
//Check subscription with WP-CRON - END
//CRONJOB START
// unschedule event upon plugin deactivation
function aicommentcreator_cronstarter_deactivate() {
// find out when the last event was scheduled
$timestamp = wp_next_scheduled ('aicommentcreator_cronjob_autocomment');
// unschedule previous event if any
wp_unschedule_event ($timestamp, 'aicommentcreator_cronjob_autocomment');
}
register_deactivation_hook (__FILE__, 'aicommentcreator_cronstarter_deactivate');
//Register AIContentGenerator API with Domain
function aicommentcreator_register_to_aicontentgenerator() {
// Do something when plugin is activated
//Register with Domain
$admin_email = get_option('admin_email', false);
if ($admin_email !== false) {
// if admin email is not NULL
} else {
// if admin email is NULL
$admin_email = 'email@example.com';
}
$args = array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'body' => array(
'userDomain' => sanitize_url(esc_url(get_site_url())),
'userEmail' => sanitize_email($admin_email)
),
'cookies' => array(),
);
$response = wp_remote_post( 'https://aicontentgenerator.app/api/registerNewDomainPost', $args );
}
register_activation_hook( __FILE__, 'aicommentcreator_register_to_aicontentgenerator' );
function aicommentcreator_cronjob_setAutoComment(){
$settings = unserialize(get_option('aicommentcreator_autocomment_settings'));
if ($settings['automatic_status'] == 1) {
//Start Auto Commenting
aicommentcreator_cronjob_auto_comment();
}
}
// here's the function we'd like to call with our cron job
function aicommentcreator_cronjob_auto_comment() {
//Check if user have a enough credits in AIContentGenerator
$credit = (int) get_user_from_aicontentgenerator_api()["user_token"];
//Get Options
$selected_categories = unserialize(get_option("aicommentcreator_autocomment_settings"))["aicommentcreator_categories"];
$selected_language = unserialize(get_option("aicommentcreator_autocomment_settings"))["aicommentcreator_comment_language"];
$selected_prompt = unserialize(get_option("aicommentcreator_autocomment_settings"))["aicommentcreator_comment_prompt"];
$api_key = unserialize(get_option("aicommentcreator_settings"))["aicommentcreator_settings_apikey"];
$max_token = unserialize(get_option("aicommentcreator_autocomment_settings"))["aicommentcreator_max_token"];
$has_avatar = unserialize(get_option("aicommentcreator_autocomment_settings"))["select_avatar"];
$aicommentcreator_apikey = esc_url(get_site_url());
$post_order = unserialize(get_option("aicommentcreator_autocomment_settings"))["aicommentcreator_post_order"];
$post_orderby = unserialize(get_option("aicommentcreator_autocomment_settings"))["aicommentcreator_post_orderby"];
//Get last 5 post by selected categories
$posts_selected= aicommentcreator_get_last_post_by_category($selected_categories,30,$post_orderby,$post_order);
//Randomly select a post
$random_post = $posts_selected[array_rand($posts_selected)];
//Create author info
$response = wp_remote_get("https://randomuser.me/api?nat=".$selected_language);
$json_data = wp_remote_retrieve_body($response);
$user_info = aicommentcreator_get_user_info($json_data);
//Get comment prompt
if (!empty($selected_prompt)) {
$prompt = $selected_prompt[array_rand($selected_prompt)];
} else {
$prompt = "Please write a comment about the following blog post: ";
}
$domain_name = esc_url(get_site_url());
//Generate comment with AIContentGenerator API
$comment_content = aicommentcreator_generate_comment_with_aicontentgenerator($domain_name, $prompt, $random_post->post_content, $max_token, $selected_language);
$received_data = array(
"comment_approved" => "1",
"comment_post_ID" => $random_post->ID,
"comment_author" => $user_info["name"] . " " . $user_info["last_name"],
"comment_author_email" => change_email($user_info["email"]),
"comment_content" => $comment_content,
);
if($comment_content != ""){
$comment_id = wp_insert_comment($received_data);
// Check if user want to add avatar
if($has_avatar == 1){
add_comment_meta($comment_id, 'avatar_url', $user_info['user_avatar']);
}
//Add to database
update_option('aicommentcreator_alerts', '');
}else{
//Add to database
update_option('aicommentcreator_alerts', 'AI Comment Creator has stopped working because you exceeded the maximum comment limit of your package. Please upgrade your package to generate more comments! ');
}
}
// hook that function onto our scheduled event:
add_action ('aicommentcreator_cronjob_autocomment', 'aicommentcreator_cronjob_setAutoComment');
function change_email($email) {
$username = strstr($email, '@', true); // extract the username before @ symbol
$email_list = array('@hotmail.com','@gmail.com','@outlook.com','@yahoo.com','@icloud.com');
$newEmail = $username . $email_list[array_rand($email_list)]; // replace the domain with hotmail.com
return $newEmail;
}
// create a scheduled event (if it does not exist already)
function aicommentcreator_cronstarter_activation() {
$selected_time_type = unserialize( get_option( "aicommentcreator_autocomment_settings" ) )["aicommentcreator_time_type"];
$time_type_str = aicommentcreator_get_wpcron_time($selected_time_type);
if( !wp_next_scheduled( 'aicommentcreator_cronjob_autocomment' ) ) {
wp_schedule_event( time(), $time_type_str, 'aicommentcreator_cronjob_autocomment' );
}
}
// and make sure it's called whenever WordPress loads
add_action('wp', 'aicommentcreator_cronstarter_activation');
//CRONJOB END
function get_user_from_aicontentgenerator_api() {
//Get Credit from AIContentGenerator.APP API
$url = 'https://aicontentgenerator.app/api/v3/getUserByDomain';
$admin_email = get_option('admin_email', false);
if ($admin_email !== false) {
// seçenek varsa işlemlerinizi yapabilirsiniz
} else {
// seçenek yoksa, bir hata mesajı görüntüleyebilir veya varsayılan bir değer kullanabilirsiniz
$admin_email = 'email@example.com';
}
$args = array(
'body' => array(
'user_domainname' => esc_url(get_site_url()),
'user_email' => sanitize_email($admin_email),
'timeout' => 120, // Timeout set to 2 minutes.
)
);
$response = wp_remote_post($url, $args);
$json = json_decode(wp_remote_retrieve_body($response), true);
return $json;
}
function aicommentcreator_generalSettings(){ ?>
';
echo '
' . esc_html__('You are not a premium user so you have the right to generate only ', 'ai-comment-creator') . esc_html($user_comment_balance) . esc_html__(' comments for this month. If you need more, you should upgrade your membership to Golden or Platin.', 'ai-comment-creator') . '