'15096',
'slug' => 'ai-auto-tool',
'premium_slug' => 'ai-auto-tool-pay',
'type' => 'plugin',
'public_key' => 'pk_a1bb06b76f38f9ff1132089d99b21',
'is_premium' => false,
'has_premium_version' => false,
'has_addons' => false,
'has_paid_plans' => true,
'has_affiliation' => 'all',
'menu' => array(
'slug' => 'ai_auto_tool',
'contact' => true,
'support' => false,
'network' => true,
),
) );
}
return $aiautotool_premium;
}
aiautotool_premium();
do_action( 'aiautotool_premium_loaded' );
}
aiautotool_premium()->add_filter( 'hide_freemius_powered_by', '__return_true' );
/* translators: %s là đường dẫn tới trang nâng cấp lên bản Pro của AI Auto Tool.*/
$Upgrade_notifice = sprintf('AI Auto Tool Limit Quota. Please Upgrade Pro',
aiautotool_premium()->get_upgrade_url());
define('AIAUTOTOOL_TITLE_UPGRADE', $Upgrade_notifice);
function aiautotool_activate()
{
require_once esc_html( plugin_dir_path( __FILE__ ) ) . 'vendor/aiautotool/class-ai-auto-tool-activator.php';
Ai_auto_tool_Activator::activate();
}
function aiautotool_deactivate()
{
require_once esc_html( plugin_dir_path( __FILE__ ) ) . 'vendor/aiautotool/class-ai-auto-tool-deactivator.php';
Ai_auto_tool_Deactivator::deactivate();
}
register_activation_hook( __FILE__, 'aiautotool_activate' );
register_deactivation_hook( __FILE__, 'aiautotool_deactivate' );
if( ! class_exists( 'AI_Auto_Tool' ) ) {
class AI_Auto_Tool {
protected $subclasses = [];
private $aiautotool_config_settings;
public function __construct() {
add_action('init', array($this, 'load_textdomain'));
add_action( 'admin_menu', array( $this, 'add_menu_page' ) , 10, 2);
add_action( 'admin_init', array( $this, 'register_settings' ) );
add_action( 'rest_api_init', array($this,'create_post_route'));
add_action( 'wp_ajax_aiautotool_check', array( $this, 'aiautotool_check' ));
add_action( 'wp_ajax_nopriv_aiautotool_check', array( $this, 'aiautotool_check' ) );
add_action('wp_ajax_aiautotool_insert_post', array( $this, 'aiautotool_insert_post_ajax' ));
add_action('wp_ajax_nopriv_aiautotool_insert_post', array( $this, 'aiautotool_insert_post_ajax' ));
add_action( 'wp_ajax_aiautotool_get_cate', array( $this, 'aiautotool_get_cate' ));
add_action( 'wp_ajax_nopriv_aiautotool_get_cate', array( $this, 'aiautotool_get_cate' ));
add_action( 'wp_ajax_get_all_media', array( $this, 'get_all_media_callback' ));
add_action( 'wp_ajax_nopriv_get_all_media', array( $this, 'get_all_media_callback' ));
add_action( 'wp_ajax_aiautotool_save_image', array( $this, 'aiautotool_save_image' ));
add_action('wp_ajax_aiautotool_analyze_image_positions', array($this, 'aiautotool_analyze_image_positions'));
add_action('wp_ajax_aiautotool_generate_english_prompt', array($this, 'aiautotool_generate_english_prompt'));
add_action('wp_ajax_nopriv_aiautotool_generate_english_prompt', array($this, 'aiautotool_generate_english_prompt'));
self::load_lib();
self::load_inc();
self::aiautotool_new_object();
add_action( 'admin_enqueue_scripts', array( $this, 'add_aiautotool_button_js' ) );
add_filter('plugin_action_links', array( $this, 'add_ai_auto_tool_settings_link' ), 10, 2);
add_action('wp_ajax_ai_auto_tool_process_posts', array($this, 'process_posts_callback'));
add_action('wp_ajax_nopriv_ai_auto_tool_process_posts', array($this, 'process_posts_callback'));
add_action('wp_ajax_ai_auto_tool_process_img_404_posts', array($this, 'process_img_404_posts_callback'));
add_action('wp_ajax_nopriv_ai_auto_tool_process_img_404_posts', array($this, 'process_img_404_posts_callback'));
// Add new AJAX handlers for featured image functionality
add_action('wp_ajax_aiautotool_save_draft', array($this, 'aiautotool_save_draft_callback'));
add_action('wp_ajax_nopriv_aiautotool_save_draft', array($this, 'aiautotool_save_draft_callback'));
add_action('wp_ajax_aiautotool_set_featured_image', array($this, 'aiautotool_set_featured_image_callback'));
add_action('wp_ajax_nopriv_aiautotool_set_featured_image', array($this, 'aiautotool_set_featured_image_callback'));
register_activation_hook(__FILE__, array($this, 'activate'));
register_deactivation_hook(__FILE__, array($this, 'deactivate'));
$aiautotool_Warning_Notice = new aiautotool_Warning_Notice(array(
'icon_url' => plugins_url('/images/logo.svg', __FILE__),
'title' => 'Aiautotool Plugin ',
'content' => 'The plugin is available for free so if it benefits you. Join the plugin support group here.',
'button_text' => 'Join group support Ai auto Tool Plugin',
'button_url' => 'https://t.me/aiautotoolchat',
));
}
public function init_settings() {
}
private function aiautotool_getdata() {
return rendersetting::aiautotool_getdata();
}
public function load_textdomain() {
load_plugin_textdomain('ai-auto-tool', false, dirname(plugin_basename(__FILE__)) . '/languages/');
}
public function activate() {
if (class_exists('activeCallBack')) {
$activeCallBack = new activeCallBack();
$activeCallBack->activate();
}
}
public function deactivate(){
$siteurl = get_option('siteurl');
$siteurl = str_replace('https://', 'http://', $siteurl);
update_option('siteurl', $siteurl);
// }
$home = get_option('home');
$home = str_replace('https://', 'http://', $home);
update_option('home', $home);
if (class_exists('activeCallBack')) {
$activeCallBack = new activeCallBack();
$activeCallBack->deactivate();
}
global $wpdb;
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'aiautotool_%'" );
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'fs_%'" );
}
function add_ai_auto_tool_settings_link($links, $file) {
if (false !== strpos($file, 'ai-auto-tool/Ai-Auto-Tool.php')) {
$settings_link = 'Settings';
array_unshift($links, $settings_link);
}
return $links;
}
public function addSubclass($subclass) {
$this->subclasses[] = $subclass;
}
public function aiautotool_new_object(){
if (class_exists('URL_Scanner_Plugin')) {
$url_scanner_plugin = new URL_Scanner_Plugin();
$this->addSubclass($url_scanner_plugin);
}
if (class_exists('AIAutoToolsinglepost')) {
$AIAutoToolsinglepost = new AIAutoToolsinglepost();
$this->addSubclass($AIAutoToolsinglepost);
}
if (class_exists('AIautotool_autocomment')) {
$AIautotool_autocomment = new AIautotool_autocomment();
$this->addSubclass($AIautotool_autocomment);
}
if (class_exists('AIautotool_autocreatetags')) {
$AIautotool_autocreatetags = new AIautotool_autocreatetags();
$this->addSubclass($AIautotool_autocreatetags);
}
if (class_exists('AI_AutoTool_cloudflareAI')) {
$AI_AutoTool_cloudflareAI = new AI_AutoTool_cloudflareAI ();
$this->addSubclass($AI_AutoTool_cloudflareAI);
}
if (class_exists('AI_AutoTool_suggetauto')) {
$AI_AutoTool_suggetauto = new AI_AutoTool_suggetauto ();
$this->addSubclass($AI_AutoTool_suggetauto);
}
if (class_exists('AIautotool_Prompt_CPT')) {
$AIautotool_Prompt_CPT = new AIautotool_Prompt_CPT ();
$this->addSubclass($AIautotool_Prompt_CPT);
}
if (class_exists('AIAutoTool_SubmitIndex')) {
$AIAutoTool_SubmitIndex = new AIAutoTool_SubmitIndex();
$this->addSubclass($AIAutoTool_SubmitIndex);
}
if (class_exists('Aiautotool_exLink_backlink')) {
$Aiautotool_exLink_backlink = new Aiautotool_exLink_backlink();
$this->addSubclass($Aiautotool_exLink_backlink);
}
if (class_exists('Aiautotool_Canonical')) {
$Aiautotool_Canonical = new Aiautotool_Canonical();
$this->addSubclass($Aiautotool_Canonical);
}
if (class_exists('Aiautotool_tool_extra_config')) {
$Aiautotool_tool_extra_config = new Aiautotool_tool_extra_config();
$this->addSubclass($Aiautotool_tool_extra_config);
}
// if (class_exists('AIautotool_Web_Stories')) {
// $AIautotool_Web_Stories = new AIautotool_Web_Stories();
// $this->addSubclass($AIautotool_Web_Stories);
// }
if (class_exists('AIAutoToolSecurityBot')) {
$AIAutoToolSecurityBot = new AIAutoToolSecurityBot();
$this->addSubclass($AIAutoToolSecurityBot);
}
// if (class_exists('AIAutotool_ThumbPlusSettings')) {
// $AIAutotool_ThumbPlusSettings = new AIAutotool_ThumbPlusSettings();
// $this->addSubclass($AIAutotool_ThumbPlusSettings);
// }
if (class_exists('aiautotool_AutoGenerateUsername')) {
$aiautotool_AutoGenerateUsername = new aiautotool_AutoGenerateUsername();
$this->addSubclass($aiautotool_AutoGenerateUsername);
}
if (class_exists('Post_Exporter_Plugin')) {
$Post_Exporter_Plugin = new Post_Exporter_Plugin();
$this->addSubclass($Post_Exporter_Plugin);
}
if (class_exists('Auto_Install_Plugins')) {
$Auto_Install_Plugins = new Auto_Install_Plugins();
$this->addSubclass($Auto_Install_Plugins);
}
// if (class_exists('Aiautotool_Footer_Base')) {
// $Aiautotool_Footer_Base = new Aiautotool_Footer_Base();
// $this->addSubclass($Aiautotool_Footer_Base);
// }
// if (class_exists('aiautotool_SSL_active')) {
// $aiautotool_SSL_active = new aiautotool_SSL_active();
// $this->addSubclass($aiautotool_SSL_active);
// }
if (class_exists('activeCallBack')) {
$activeCallBack = new activeCallBack();
}
if (class_exists('AI_AutoTool_Prompt_Customer')) {
$AI_AutoTool_Prompt_Customer = new AI_AutoTool_Prompt_Customer();
}
}
public function show_render_setting() {
foreach ($this->subclasses as $subclass) {
$subclass->render_setting();
}
}
public function show_render_tab_setting() {
foreach ($this->subclasses as $subclass) {
$subclass->render_tab_setting();
}
}
public function render_list_feature(){
foreach ($this->subclasses as $subclass) {
$subclass->render_feature();
}
}
public function render_plan(){
foreach ($this->subclasses as $subclass) {
$subclass->render_plan();
}
}
public function load_inc() {
// Đường dẫn đến thư mục "inc"
$inc_directory = plugin_dir_path(__FILE__) . 'inc';
$inc_files = glob($inc_directory . '/*.php');
if ($inc_files) {
foreach ($inc_files as $file) {
if (is_file($file)) {
include $file;
}
}
}
}
public function load_lib() {
$inc_directory = plugin_dir_path(__FILE__) . 'lib';
$inc_files = glob($inc_directory . '/*.php');
if ($inc_files) {
foreach ($inc_files as $file) {
if (is_file($file)) {
include $file;
}
}
}
}
function replace_links_in_content($links,$content){
if (count($links) > 3) {
shuffle($links);
$links = array_slice($links, 0, 3);
}
$nofollow = " ";
$newwindow = " target='_blank' ";
$before = "([\s,\(\);:]*?[A-Za-z0-9\x80-\xFF+]*?[\s,\)\(;:]){0,0}";
$after = "([\s,\(\);:]*?[A-Za-z0-9\x80-\xFF+]*?[\s,\)\(;:]){0,0}";
$insensitive = 'i';
$contentbefor = $content;
foreach ($links as $link) {
$content = ai_internal_link_building_blocks::findtags($content);
$anchor_text = $link->archortext;
$url = $link->url;
$name = $anchor_text;
$replace1 = '';
$replace2 = '';
$escapes = array('.','$','^','[',']','?','+','(',')','*','|','\\');
foreach($escapes as $s){
$r = '\\\\'.$s;
$name = str_replace($s, stripslashes($r), $name);
}
$needle ='@()('.$before.$name.'\b'.$after.')()@';
$content = ai_internal_link_building_blocks::findtags($content,false); blocks.
$content = ai_internal_link_building::replace($content, $needle, $replace1, $replace2, 1,$insensitive);
$content = ai_internal_link_building_blocks::findblocks($content);
$content = ai_internal_link_building_blocks::findblocks($content);
$content = ai_internal_link_building_blocks::findblocks($content);
$protectblocks = array();
$content = trim($content, ' ');
if (md5($content)!=md5($contentbefor)) {
}else{
$pos = strpos($content, "
");
if ($pos !== false) {
$p_tags = explode('', $content);
$p_tag_count = count($p_tags);
if ($p_tag_count > 1) {
$random_pos = rand(1, $p_tag_count - 1);
$pos = strpos($content, '' . $p_tags[$random_pos]);
}
$replacement = "" . $anchor_text . "";
$content = substr($content, 0, $pos + strlen("")) . $replacement . substr($content, $pos + strlen(""));
} else {
$content .= "" . $anchor_text . "";
}
}
}
return $content;
}
public function add_aiautotool_button_js() {
$setting = new rendersetting();
wp_enqueue_style( 'font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css' ,array(), '1.2'.rand());
wp_enqueue_style('aiautotool', plugin_dir_url(__FILE__) . 'css/aiautotool.css', array(), '1.2'.rand());
wp_enqueue_script('jquery');
wp_enqueue_media();
wp_enqueue_script('socket-io', 'https://cdn.socket.io/4.6.0/socket.io.min.js', array(), '4.6.0');
wp_enqueue_script('sweetalert2', 'https://cdn.jsdelivr.net/npm/sweetalert2@11"', array(), '4.6.0');
$langcodedefault = explode('-',get_bloginfo("language"));
$langcodedefault = $langcodedefault[0];
$langcodedefault = get_option('aiautotool_default_language', 'vi');
wp_register_script('kct_cr_scriptx', plugin_dir_url( __FILE__ ) .'js/aiautotool.js', array(), '1.2'.rand(), true);
global $post;
//$settingseditornumberline = wp_enqueue_code_editor( array( 'type' => 'text/html' ) );
$settingseditornumberline = '';
// wp_localize_script('jquery', 'cm_settings', $settings);
// wp_enqueue_style('wp-codemirror');
if(isset($post->ID)){
wp_localize_script( 'kct_cr_scriptx', 'aiautotool_js_setting',array( 'ajax_url' => admin_url( 'admin-ajax.php') , 'postID' => $post->ID, 'postTitle' => $post->post_title,'security' => wp_create_nonce('aiautotool_nonce'),'languageCodes'=>$setting->languageCodes,
'langcodedefault'=>$langcodedefault ,
'fsdata'=>$this->aiautotool_getdata(),
'code_mirrror'=>$settingseditornumberline,
'gemini_api'=>AIAUTOTOOL_API_GEM,
'gpt_api'=>AIAUTOTOOL_API_GTP,
'prompt'=>get_option('aiautotool_prompt_options')
));
}else{
wp_localize_script( 'kct_cr_scriptx', 'aiautotool_js_setting',array(
'ajax_url' => admin_url( 'admin-ajax.php') ,
'security' => wp_create_nonce('aiautotool_nonce'),
'languageCodes'=>$setting->languageCodes,
'langcodedefault'=>$langcodedefault,
'fsdata'=>$this->aiautotool_getdata(),
'code_mirrror'=>$settingseditornumberline,
'gemini_api'=>AIAUTOTOOL_API_GEM,
'gpt_api'=>AIAUTOTOOL_API_GTP,
'prompt'=>get_option('aiautotool_prompt_options')
));
}
wp_enqueue_script('kct_cr_scriptx');
}
public function aiautotool_hook_script(){
wp_enqueue_script( 'codemirror-js', plugin_dir_url(__FILE__) .'vendor/codemirror/codemirror.min.js', array(), false, true );
wp_enqueue_style( 'codemirror-css', plugin_dir_url(__FILE__) .'vendor/codemirror/codemirror.min.css' );
wp_enqueue_script( 'codemirror-mode-perl', plugin_dir_url(__FILE__) .'vendor/codemirror/perl.min.js', array('codemirror-js'), false, true );
wp_enqueue_style( 'codemirror-theme', plugin_dir_url(__FILE__) .'vendor/codemirror/cobalt.css' );
}
public function add_menu_page() {
add_menu_page(
' Ai Auto Tool',
' Ai Auto Tool',
'manage_options',
MENUSUBPARRENT,
array( $this, 'menu_page' ),
'data:image/svg+xml;base64,' . base64_encode(file_get_contents(plugin_dir_path(__FILE__) . '/images/logo.svg')),
3
);
add_submenu_page(
MENUSUBPARRENT,
' Integrations',
' Integrations',
'manage_options',
'aiautotool_integrations',
array($this, 'aiautotool_integrations')
);
}
public function aiautotool_integrations(){
?>
config_page();
}
function is_free_plan() {
if ( ! aiautotool_premium()->is_registered() ) {
return true;
}
if ( ! aiautotool_premium()->has_paid_plan() ) {
return true;
}
return (
'free' === aiautotool_premium()->get_plan_name() ||
! aiautotool_premium()->has_features_enabled_license()
);
}
public function config_page() {
if (!current_user_can('manage_options')) {
wp_die(esc_html__('You do not have sufficient permissions to access this page.','ai-auto-tool'));
}
$api_key_list = get_option('aiautotool_api_key_list', array());
$config_img = get_option('aiautotool_config_img', false);
$aiautotool_images_list = get_option('aiautotool_images_list', []);
$users = get_users();
if (isset($_POST['aiautotool_add_key'])) {
$new_key = 'KCT-'.uniqid();
$user_id = sanitize_text_field($_POST['aiautotool_user']);
$api_key_list = get_option('aiautotool_api_key_list', array());
$api_key_list[] = array(
'key' => $new_key,
'user_id' => $user_id,
);
update_option('aiautotool_api_key_list', $api_key_list,null, 'no');
echo esc_html('');
}
if (isset($_POST['aiautotool_save_config'])) {
if(isset($_POST['aiautotool_config_img'])){
$config_img = sanitize_text_field($_POST['aiautotool_config_img']);
update_option('aiautotool_config_img', $config_img,null, 'no');
}
}
if (isset($_POST['aiautotool_remove_api_key'])) {
$remove_key = sanitize_text_field($_POST['aiautotool_remove_api_key']);
unset($api_key_list[$remove_key]);
update_option('aiautotool_api_key_list', $api_key_list,null, 'no');
}
if (isset($_POST['aiautotool_remove_log_img'])) {
update_option('aiautotool_images_list', [],null, 'no');
$aiautotool_images_list = get_option('aiautotool_images_list', []);
echo esc_html('Reset log Images Success!!!!.
');
}
if (isset($_POST['aiautotool_remove_img_log_item'])) {
$remove_key = sanitize_text_field($_POST['aiautotool_remove_img_log_item']);
unset($aiautotool_images_list[$remove_key]);
update_option('aiautotool_images_list', $aiautotool_images_list,null, 'no');
echo esc_html('Remove log item image Success!!!!.
');
}
$this->aiautotool_config_settings = get_option('aiautotool_code_settings',array());
if(isset($_POST['aiautotool_code_settings'])){
foreach($_POST['aiautotool_code_settings'] as $key => $value){
if($key!='btnall'){
$this->aiautotool_config_settings[$key] = $value;
}
}
$result = update_option('aiautotool_code_settings',(array )$this->aiautotool_config_settings,null, 'no');
$this->aiautotool_config_settings = get_option('aiautotool_code_settings',array());
}
?>
';
}
public function create_post_route() {
register_rest_route( 'aiautotool', '/createpost', array(
'methods' => 'POST',
'callback' => array($this,'aiautotool_insert_post_ajax'),
) );
register_rest_route( 'aiautotool', '/get_cate', array(
'methods' => 'GET',
'callback' => array($this,'aiautotool_get_cate'),
));
register_rest_route( 'aiautotool', '/media', array(
'methods' => 'GET',
'callback' => array($this,'get_all_media_callback'),
));
register_rest_route( 'aiautotool', '/check', array(
'methods' => 'GET',
'callback' => array($this,'aiautotool_check'),
));
}
public function register_settings() {
register_setting( 'ai_auto_tool_api_keys', 'ai_auto_tool_api_keys' );
register_setting( 'ai_auto_tool_log', 'ai_auto_tool_log' );
}
public function aiautotool_insert_post_ajax() {
// Lấy dữ liệu POST
$name = sanitize_text_field($_POST['name']);
$detail = wp_kses_post($_POST['detail']);
$category_name = sanitize_text_field($_POST['category']);
$tags = sanitize_text_field($_POST['tags']);
$date = sanitize_text_field($_POST['date']);
$key = sanitize_text_field($_POST['key']);
$postname = $this->kct_aiautotool_sanitize($name);
$user_id = '';
$user_ids = get_users( array(
'fields' => 'ID',
) );
$random_user_id = $user_ids[array_rand($user_ids)];
$api_key_list = get_option('aiautotool_api_key_list', array());
if (count($api_key_list)>0) {
foreach ($api_key_list as $k=>$api_key){
if ($key==$api_key['key']) {
$user_id = $random_user_id;//rand(1, 11);//$api_key['user_id'];
$config_img = get_option('aiautotool_config_img', false);
if($config_img){
$aiautotool_images_list = get_option('aiautotool_images_list', []);
$html = str_replace('\"', '"', $detail);
$arrayImageUpload = $this->kct_aiautotool_get_all_img($html);
$attachThumnail = 0;
if (!empty($arrayImageUpload)){
foreach ($arrayImageUpload as $post_image_url){
$image_item = null;
foreach ($aiautotool_images_list as $item) {
if ($item['url_remote'] === $post_image_url) {
$image_item = $item;
break;
}
}
if ($image_item !== null) {
$image_url_new = $image_item['url_image_new']['url_out'];
$detail = str_replace($post_image_url, $image_url_new, $detail);
if ($attachThumbnail == 0) {
$attachThumbnail = $image_item['url_image_new']['attach_id'];
}
}else{
try {
$image_url_new = $this->kct_aiautotool_save_image($post_image_url,$postname);
if (!empty($image_url_new)){
$imgUploaded[] = $image_url_new;
if ($attachThumbnail == 0) {
$attachThumbnail = $image_url_new['attach_id'];
}
$aiautotool_images_list[] = [
'url_remote' => $post_image_url,
'url_image_new' => $image_url_new,
];
update_option('aiautotool_images_list', $aiautotool_images_list,null, 'no');
}
}catch (Exception $e) {
}
}
}
}
foreach ($imgUploaded as $img){
$detail = str_replace($img['url'],$img['baseurl'],$detail);
}
}
$category = get_category_by_slug($category_name);
$category_id = $category->term_id;
// Insert the post
$post_id = wp_insert_post(array(
'post_title' => $name,
'post_content' => $detail,
'post_category' => array($category_id),
'tags_input' => $tags,
'post_status' => 'publish',
'post_author'=>$user_id,
'post_date' => $date,
'meta_input' => array(
'key' => $key,
),
'post_type' => 'post',
));
// Return the URL of the new post
if ($post_id) {
if ($attachThumbnail != 0) {
set_post_thumbnail($post_id, $attachThumbnail);
}
$post_url = get_permalink($post_id);
echo json_encode(array('url' => $post_url));
} else {
echo json_encode(array('error' => 'Failed to insert post.'));
}
}
}
}
wp_die();
}
public function kct_aiautotool_url_encode($string)
{
return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
}
function kct_aiautotool_save_image($imgURL,$post_title){
$image_name = basename( $imgURL );
$filetype = wp_check_filetype($image_name);
$upload_dir = wp_upload_dir();
//$unique_file_name = aiktp_sanitize($post_title)."-".wp_unique_filename( $upload_dir['path'], $image_name );
$extension = $filetype['ext']?$filetype['ext']:"jpg";
if (empty($extension)) $extension = "jpg";
$unique_file_name = $this->kct_aiautotool_sanitize($post_title)."-".uniqid().".".$extension;
$filename = $upload_dir['path'] . '/' . $unique_file_name;
$baseurl = $upload_dir['baseurl'] .$upload_dir['subdir']. '/' . $unique_file_name;
$ch = curl_init($imgURL);
$fp = fopen($filename, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_exec($ch);
$rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fclose($fp);
if ($rescode == 200 && filesize($filename) > 100){
$wp_filetype = wp_check_filetype(basename($filename), null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($post_title),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $filename);
$imagenew = get_post($attach_id);
$fullsizepath = get_attached_file($imagenew->ID);
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $fullsizepath);
wp_update_attachment_metadata($attach_id, $attach_data);
$output['url'] = $imgURL;
$output['file_name'] = $unique_file_name;
$output['path'] = $filename;
$output['baseurl'] = $baseurl;
$output['attach_id'] = $attach_id;
$output['url_out'] = $baseurl;
return $output;
}else{
return null;
}
}
public function kct_aiautotool_sanitize($title) {
$replacement = '-';
$map = array();
$quotedReplacement = preg_quote($replacement, '/');
$default = array(
'/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ|À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ|å/' => 'a',
'/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ|È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ|ë/' => 'e',
'/ì|í|ị|ỉ|ĩ|Ì|Í|Ị|Ỉ|Ĩ|î/' => 'i',
'/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ|Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ|ø/' => 'o',
'/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ|Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ|ů|û/' => 'u',
'/ỳ|ý|ỵ|ỷ|ỹ|Ỳ|Ý|Ỵ|Ỷ|Ỹ/' => 'y',
'/đ|Đ/' => 'd',
'/ç/' => 'c',
'/ñ/' => 'n',
'/ä|æ/' => 'ae',
'/ö/' => 'oe',
'/ü/' => 'ue',
'/Ä/' => 'Ae',
'/Ü/' => 'Ue',
'/Ö/' => 'Oe',
'/ß/' => 'ss',
'/[^\s\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
'/\\s+/' => $replacement,
sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
);
//Some URL was encode, decode first
$title = urldecode($title);
$map = array_merge($map, $default);
return strtolower(preg_replace(array_keys($map), array_values($map), $title));
}
public function kct_aiautotool_get_all_img($html)
{
$html = stripslashes($html);
preg_match_all('/
]+src\s*=\s*["\']([^"\']+)["\']/i', $html, $matches);
return $matches[1];
}
public function kct_aiautotool_startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
public function kct_aiautotool_endsWith($haystack, $needle)
{
$length = strlen($needle);
return $length === 0 || (substr($haystack, -$length) === $needle);
}
public function aiautotool_get_cate() {
header('Content-Type: application/json; charset=utf-8');
if ( isset( $_GET['key'] ) ) {
$key = sanitize_text_field($_GET['key']);
$api_key_list = get_option('aiautotool_api_key_list', array());
if (count($api_key_list)>0) {
foreach ($api_key_list as $k=>$api_key){
if ($key==$api_key['key']) {
$categories = get_categories();
$categories_array = array();
foreach ( $categories as $category ) {
$categories_array[] = array(
'id' => $category->cat_ID,
'name' => $category->name,
'slug' => $category->slug
);
}
echo json_encode( $categories_array );
die();
}
}
}
} else {
wp_send_json_error( ' key not in AJAX.' );
}
die();
}
public function get_all_media_callback() {
$key = sanitize_text_field($_GET['key']);
$api_key_list = get_option('aiautotool_api_key_list', array());
if (count($api_key_list)>0) {
foreach ($api_key_list as $k=>$api_key){
if ($key==$api_key['key']) {
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'posts_per_page' => - 1,
);
$media_query = new WP_Query( $args );
$media = array();
if ( $media_query->have_posts() ) {
while ( $media_query->have_posts() ) {
$media_query->the_post();
$media[] = array(
'id' => get_the_ID(),
'title'=> get_the_title(),
'url' => wp_get_attachment_url(),
'type' => get_post_mime_type(),
);
}
wp_reset_postdata();
wp_send_json( $media );
die();
}else{
$arr = array('ok'=>'notfindimg');
wp_send_json( $arr );
die();
}
}
}
}
die();
}
function aiautotool_check() {
header('Content-Type: application/json; charset=utf-8');
if ( isset( $_GET['key'] ) ) {
$key = sanitize_text_field($_GET['key']);
$api_key_list = get_option('aiautotool_api_key_list', array());
if (count($api_key_list)>0) {
foreach ($api_key_list as $k=>$api_key){
if ($key==$api_key['key']) {
$arr = array(
'results'=>'ok',
'API_KEY'=>$api_key['key'],
'domain'=>sanitize_text_field($_SERVER['HTTP_HOST'])
);
echo json_encode( $arr );
die(0);
}
}
$arr = array(
'results'=>'notok',
'API_KEY'=>$api_key['key'],
'domain'=>sanitize_text_field($_SERVER['HTTP_HOST'])
);
echo json_encode( $arr );
die(0);
}
} else {
$arr = array(
'results'=>'notok',
'API_KEY'=>$api_key['key'],
'domain'=>sanitize_text_field($_SERVER['HTTP_HOST'])
);
echo json_encode( $arr );
die(0);
}
}
public function aiautotool_save_image(){
$reponse = array();
if(!empty($_POST['link_img'])){
$link_img = sanitize_text_field($_POST['link_img']);
$postId = sanitize_text_field($_POST['postId']);
$postTitle = sanitize_text_field($_POST['postTitle']);
$i = sanitize_text_field($_POST['i']);
if(strpos( $link_img,$_SERVER['HTTP_HOST']) !== false){
//echo $link_img;
$av = array(
'stt'=>'ok',
'linkedit'=>$link_img
);
}else{
// $imagenew = upload_image($postTitle,$link_img,$postId,$i);
$imagenews = $this->kct_aiautotool_save_image($link_img,$postTitle);
$imagenew = $imagenews['url_out'];
$av = array(
'stt'=>'ok',
'linkedit'=>$imagenew
);
}
}else{
$av = array(
'stt'=>'notok',
'linkedit'=>''
);
}
header( "Content-Type: application/json" );
echo json_encode($av);
exit();
}
public function aiautotool_analyze_image_positions() {
check_ajax_referer('aiautotool_nonce', 'security');
$content = sanitize_textarea_field($_POST['content']);
$prompt = sanitize_textarea_field($_POST['prompt']);
if (empty($content)) {
wp_send_json_error('Content is required');
return;
}
try {
// Gọi AI API để phân tích
$ai_response = $this->call_ai_api_for_image_analysis($prompt);
if ($ai_response) {
wp_send_json_success($ai_response);
} else {
wp_send_json_error('Failed to analyze content with AI');
}
} catch (Exception $e) {
wp_send_json_error('Error: ' . $e->getMessage());
}
}
public function aiautotool_generate_english_prompt() {
check_ajax_referer('aiautotool_nonce', 'security');
$title = sanitize_text_field($_POST['title']);
$paragraph = sanitize_textarea_field($_POST['paragraph']);
$ai_analysis = json_decode(stripslashes($_POST['ai_analysis']), true);
$prompt = sanitize_textarea_field($_POST['prompt']);
if (empty($title) || empty($paragraph)) {
wp_send_json_error('Title and paragraph are required');
return;
}
try {
// Gọi AI API để tạo prompt tiếng Anh
$english_prompt = $this->call_ai_api_for_english_prompt($prompt);
if ($english_prompt) {
wp_send_json_success($english_prompt);
} else {
wp_send_json_error('Failed to generate English prompt with AI');
}
} catch (Exception $e) {
wp_send_json_error('Error: ' . $e->getMessage());
}
}
private function call_ai_api_for_english_prompt($prompt) {
// Sử dụng API tương tự như các function khác trong plugin
$api_url = AIAUTOTOOL_API_GEM . '/bard';
$data = array(
'text' => $prompt,
'language' => 'en', // Sử dụng tiếng Anh
'option' => 'writebard'
);
$response = wp_remote_post($api_url, array(
'body' => json_encode($data),
'headers' => array('Content-Type' => 'application/json'),
'timeout' => 60
));
if (is_wp_error($response)) {
error_log('AI API Error for English prompt: ' . $response->get_error_message());
return false;
}
$body = wp_remote_retrieve_body($response);
$result = json_decode($body, true);
if ($result && isset($result['response'])) {
return $result['response'];
}
return false;
}
private function call_ai_api_for_image_analysis($prompt) {
// Sử dụng API tương tự như các function khác trong plugin
$api_url = AIAUTOTOOL_API_GEM . '/bard';
$data = array(
'text' => $prompt,
'language' => 'vi',
'option' => 'writebard'
);
$response = wp_remote_post($api_url, array(
'body' => json_encode($data),
'headers' => array('Content-Type' => 'application/json'),
'timeout' => 60
));
if (is_wp_error($response)) {
error_log('AI API Error: ' . $response->get_error_message());
return false;
}
$body = wp_remote_retrieve_body($response);
$result = json_decode($body, true);
if ($result && isset($result['response'])) {
return $result['response'];
}
return false;
}
// AJAX callback để save draft và lấy post ID
public function aiautotool_save_draft_callback() {
check_ajax_referer('aiautotool_nonce', 'security');
$title = sanitize_text_field($_POST['title']);
$content = wp_kses_post($_POST['content']);
if (empty($title) || empty($content)) {
wp_send_json_error('Title and content are required');
}
$post_data = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'draft',
'post_type' => 'post',
'post_author' => get_current_user_id()
);
$post_id = wp_insert_post($post_data);
if ($post_id && !is_wp_error($post_id)) {
wp_send_json_success(array(
'post_id' => $post_id,
'message' => 'Draft saved successfully'
));
} else {
wp_send_json_error('Failed to save draft');
}
}
// AJAX callback để set featured image
public function aiautotool_set_featured_image_callback() {
check_ajax_referer('aiautotool_nonce', 'security');
$image_url = esc_url_raw($_POST['image_url']);
$post_id = intval($_POST['post_id']);
if (empty($image_url) || empty($post_id)) {
wp_send_json_error('Image URL and post ID are required');
}
// Kiểm tra post có tồn tại không
$post = get_post($post_id);
if (!$post) {
wp_send_json_error('Post not found');
}
// Kiểm tra quyền edit post
if (!current_user_can('edit_post', $post_id)) {
wp_send_json_error('Permission denied');
}
try {
// Download và upload image vào media library
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
if ($image_data === false) {
wp_send_json_error('Failed to download image');
}
$filename = 'ai-generated-' . time() . '.jpg';
$file_path = $upload_dir['path'] . '/' . $filename;
if (file_put_contents($file_path, $image_data) === false) {
wp_send_json_error('Failed to save image to server');
}
// Tạo attachment
$wp_filetype = wp_check_filetype($filename, null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $file_path, $post_id);
if (is_wp_error($attach_id)) {
wp_send_json_error('Failed to create attachment');
}
// Generate attachment metadata
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $file_path);
wp_update_attachment_metadata($attach_id, $attach_data);
// Set as featured image
$result = set_post_thumbnail($post_id, $attach_id);
if ($result) {
wp_send_json_success(array(
'success' => true,
'attachment_id' => $attach_id,
'message' => 'Featured image set successfully'
));
} else {
wp_send_json_error('Failed to set featured image');
}
} catch (Exception $e) {
wp_send_json_error('Error: ' . $e->getMessage());
}
}
}
new AI_Auto_Tool();
function aiautotool_load_func() {
$inc_directory = plugin_dir_path(__FILE__) . 'func';
$inc_files = glob($inc_directory . '/*.php');
// print_r($inc_files);
if ($inc_files) {
foreach ($inc_files as $file) {
if (is_file($file)) {
include $file;
}
}
}
}
aiautotool_load_func();
}
add_filter('auto_update_plugin_ai-auto-tool', '__return_true');