';
protected $postfields = array();
protected $shortcodes = array();
protected $htmltags = array();
public $limit = AIAUTOTOOL_FREE;
private $plan_limit_aiautotool ;
public $name_plan ;
public $config = array();
public $notice ;
public function __construct() {
$this->name_plan = __('Canonical & delete post','ai-auto-tool');
$this->plan_limit_aiautotool = 'plan_limit_aiautotool_'.$this->active_option_name;
$this->notice = new aiautotool_Warning_Notice();
$this->active = get_option($this->active_option_name, true);
if ($this->active=='true') {
$this->init();
}
add_action('init', array($this,'redirect'), 1);
add_action('wp_ajax_update_active_option_canonical_'.$this->active_option_name, array($this, 'update_active_option_callback'));
}
public function update_active_option_callback() {
check_ajax_referer('aiautotool_nonce', 'security');
if (!current_user_can('manage_options')) {
wp_send_json_error(array('message' => __('Permission denied', 'ai-auto-tool')), 403);
}
if (isset($_POST['active'])) {
$active = sanitize_text_field($_POST['active']);
update_option($this->active_option_name, $active,null, 'no');
wp_send_json_success(array('active' => $active));
}
wp_die();
}
public function init(){
add_action('admin_menu', array($this, 'add_menu'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
add_action('wp_ajax_aiautotool_search_posts', array($this, 'search_posts'));
add_action('wp_ajax_aiautotool_save_canonical', array($this, 'save_canonical'));
add_action('wp_ajax_aiautotool_del_canonical', array($this, 'del_canonical'));
// add_action('init', array($this, 'schedule_cron_job'));
add_filter('cron_schedules', array($this, 'aiautotool_canonical_intervals'));
if (!wp_next_scheduled('aiautotool_canonical_event')) {
wp_schedule_event(time(), 'aiautotool_canonical_intervals', 'aiautotool_canonical_event');
}
add_action('aiautotool_canonical_event', array($this, 'aiautotool_canonical_cron'));
}
public function aiautotool_canonical_intervals($schedules) {
$translate_interval = get_option('aiautotool_canonical_public_time', 5);
$schedules['aiautotool_canonical_intervals'] = array(
'interval' => $translate_interval * 60,
'display' => 'aiautotool canonical Interval'
);
return $schedules;
}
public function add_menu() {
add_submenu_page(
MENUSUBPARRENT,
' Canonical List',
' Canonical List',
'manage_options',
'aiautotool_canonical',
array($this, 'aiautotool_canonical')
);
add_submenu_page(
MENUSUBPARRENT,
' 301 Redirect',
' 301 Redirect',
'manage_options',
'aiautotool_301_edirect',
array($this, 'canonical_301_redirect')
);
}
public function del_canonical(){
check_ajax_referer('aiautotool_nonce', 'security');
$ids = sanitize_text_field($_POST['ids']);
if ($ids<0) {
wp_send_json_error(array('message' => 'Error id'));
}
$canonical_data = get_option('aiautotool_canonical_data');
if(isset($canonical_data[$ids])){
unset($canonical_data[$ids]);
update_option('aiautotool_canonical_data',$canonical_data,null, 'no');
wp_send_json_success($canonical_data);
}
wp_send_json_success(array('msg'=>'No action'));
}
public function aiautotool_canonical(){
$canonical_data = get_option('aiautotool_canonical_data',array());
?>
admin_url( 'admin-ajax.php') , 'postID' => $post->ID, 'postTitle' => $post->post_title,'security' => wp_create_nonce('aiautotool_nonce'),'languageCodes'=>$setting->languageCodes,
'aiautotool_canonical_data'=> $aiautotool_canonical_data ));
}else{
wp_localize_script( 'kct_cr_scriptx1', 'aiautotool_data',
array( 'ajax_url' => admin_url( 'admin-ajax.php') , 'security' => wp_create_nonce('aiautotool_nonce'),'languageCodes'=>$setting->languageCodes,
'aiautotool_canonical_data'=> $aiautotool_canonical_data ));
}
wp_enqueue_script('kct_cr_scriptx1');
}
public function canonical_301_redirect(){
$canonical_redirects_data = get_option('canonical_redirects_data',array());
if(isset($_POST['addnew'])){
// print_r($_POST);
$source = sanitize_text_field($_POST['urlRequest']);
$target = esc_url($_POST['urlDestination']);
$canonical_redirects_data[$source] = $target;
update_option('canonical_redirects_data', $canonical_redirects_data,null, 'no');
}
if(isset($_POST['update'])){
// print_r($_POST);
$source = sanitize_text_field($_POST['urlRequest']);
$target = esc_url($_POST['urlDestination']);
$canonical_redirects_data[$source] = $target;
update_option('canonical_redirects_data', $canonical_redirects_data,null, 'no');
}
if(isset($_POST['del'])){
// print_r($_POST);
$source = sanitize_text_field($_POST['urlRequest']);
$target = esc_url($_POST['urlDestination']);
// $canonical_redirects_data[$source] = $target;
unset($canonical_redirects_data[$source]);
update_option('canonical_redirects_data', $canonical_redirects_data,null, 'no');
}
$canonical_redirects_data = get_option('canonical_redirects_data', array());
?>
301 Control Link
'Vui lòng nhập từ khóa tìm kiếm.'));
}
$args = array(
'post_type' => 'post',
'posts_per_page' => 100,
's' => $search_query,
);
$posts = new WP_Query($args);
$results = array();
if ($posts->have_posts()) {
while ($posts->have_posts()) {
$posts->the_post();
$post_id = get_the_ID();
$post_title = get_the_title();
$post_url = get_permalink();
$results[] = array(
'id' => $post_id,
'title' => $post_title,
'url' => $post_url,
);
}
wp_reset_postdata();
wp_send_json_success($results);
} else {
wp_send_json_error(array('message' => 'Không tìm thấy bài viết nào.'));
}
}
public function save_canonical() {
check_ajax_referer('aiautotool_nonce', 'security');
$selected_posts = isset($_POST['selected_posts']) ? $_POST['selected_posts'] : array();
$canonical_url = esc_url($_POST['canonical_url']);
$autoDelete = sanitize_text_field($_POST['autoDelete']);
$deleteSchedule = sanitize_text_field($_POST['deleteSchedule']);
if (empty($selected_posts) || empty($canonical_url)) {
wp_send_json_error(array('message' => 'Vui lòng chọn bài viết và điền URL Canonical.'));
}
$canonical_data = get_option('aiautotool_canonical_data', array());
$kq = array();
foreach($selected_posts as $postid){
$kqnew = array(
'post_id'=>$postid,
'post_title'=>get_the_title($postid),
'post_url'=>get_permalink($postid),
'post_canonical'=>$canonical_url,
'canonical_time_execute'=>'',
'del_post'=>false,
'post_301'=>false,
'time_del_post'=>''
);
$kq[$postid]=$kqnew;
}
$canonical_data[] = array('post_id' => $selected_posts, 'canonical_url' => $canonical_url,'autoDelete'=>$autoDelete,'deleteSchedule'=>$deleteSchedule,'kq'=>$kq);
update_option('aiautotool_canonical_data', $canonical_data,null, 'no');
wp_send_json_success(array('message' => 'Save Success.'));
}
public function schedule_cron_job() {
if (!wp_next_scheduled('aiautotool_canonical_cron')) {
wp_schedule_event(time(), 'hourly', 'aiautotool_canonical_cron');
}
}
public function aiautotool_canonical_cron() {
// Lấy danh sách bài viết cần được cập nhật canonical URL
$canonical_data = get_option('aiautotool_canonical_data');
if (empty($canonical_data)) {
return;
}
$number_post = get_option('aiautotool_canonical_number_post', 3);
// Lấy số lượng bài viết cần cập nhật (ví dụ: 3 bài viết)
foreach($canonical_data as $key=>&$item){
$post_ids = $item['post_id'];
$canonical_url = $item['canonical_url'];
$autoDelete = $item['autoDelete'];
$deleteSchedule = $item['deleteSchedule'];
if(isset($item['kq'])){
$kq = $item['kq'];
}else{
$kq = array();
}
$post_ids = array_slice($post_ids, 0, $number_post);
foreach($post_ids as $postid){
update_post_meta($postid, '_yoast_wpseo_canonical', $canonical_url);
update_post_meta($postid, '_yoast_wpseo_canonical_time_execute', date('Y-m-d H:i:s'));
$canonical_time = strtotime(date('Y-m-d H:i:s'));
$end_time = strtotime("+".$deleteSchedule." days", $canonical_time);
$end_time_formatted = date('Y-m-d H:i:s', $end_time);
$kqnew = array(
'post_id'=>$postid,
'post_title'=>get_the_title($postid),
'post_url'=>get_permalink($postid),
'post_canonical'=>$canonical_url,
'canonical_time_execute'=>date('Y-m-d H:i:s'),
'del_post'=>false,
'post_301'=>false,
'time_del_post'=>$end_time_formatted
);
$kq[$postid]=$kqnew;
}
$item['kq'] = $kq;
$post_ids = array_slice($item['post_id'], $number_post);
$item['post_id'] = $post_ids;
}
update_option('aiautotool_canonical_data', $canonical_data,null, 'no');
self::checkAndDeletePosts();
}
public function checkAndDeletePosts() {
$canonical_data = get_option('aiautotool_canonical_data');
if (empty($canonical_data)) {
return;
}
foreach ($canonical_data as $key => &$canonical) {
if (isset($canonical['kq'])) {
$kq = $canonical['kq'];
$autoDelete = $canonical['autoDelete'];
$canonical_url = $canonical['canonical_url'];
if ($autoDelete) {
$deleteSchedule = $canonical['deleteSchedule'];
$current_time = strtotime(date('Y-m-d H:i:s'));
foreach ($kq as &$item) {
if($item['canonical_time_execute']!==''){
$canonical_time = strtotime($item['canonical_time_execute']);
$end_time = strtotime("+".$deleteSchedule." days", $canonical_time);
if ($end_time < $current_time && $item['del_post'] === false) {
$post_id = $item['post_id'];
$post_url = $item['post_url'];
wp_delete_post($post_id, true);
$item['del_post'] = true;
// update 301_canonical_redirects option
$canonical_redirects_data = get_option('canonical_redirects_data');
$canonical_redirects_data[$post_url] = $canonical_url;
update_option('canonical_redirects_data', $canonical_redirects_data,null, 'no');
$item['post_301'] = true;
}
}
}
$canonical['kq'] = $kq ;
}else{
}
}
}
update_option('aiautotool_canonical_data', $canonical_data,null, 'no');
}
public function redirect() {
// this is what the user asked for (strip out home portion, case insensitive)
$redirects = get_option('canonical_redirects_data');
if (!empty($redirects)) {
$userrequest = self::str_ireplace(str_replace('https','http',get_option('home')),'',str_replace('https','http',self::get_address()));
$userrequest = ltrim($userrequest);
$param = explode('?', $userrequest, 2);
$userrequest = current($param);
$wildcard = false;
$do_redirect = '';
// compare user request to each 301 stored in the db
foreach ($redirects as $storedrequest => $destination) {
// check if we should use regex search
$a = $storedrequest;
$storedrequest1 = $storedrequest;
$storedrequest = self::str_ireplace(str_replace('https','http',get_option('home')),'',str_replace('https','http',$storedrequest));
//self::extractPathFromURL($storedrequest);//str_replace(str_replace('https','http',get_option('home')),'',$storedrequest );
if(urldecode(trim($userrequest, '/')) == trim($storedrequest,'/')){
// simple comparison redirect
if($userrequest!='/'||$userrequest!=''){
$do_redirect = $destination;
}
}
if($userrequest==$storedrequest){
if($userrequest!='/'||$userrequest!=''){
$do_redirect = $destination;
}
}
// redirect. the second condition here prevents redirect loops as a result of wildcards.
if ($do_redirect !== '' && trim($do_redirect,'/') !== trim($userrequest,'/')) {
// check if destination needs the domain prepended
if (strpos($do_redirect,'/') === 0){
$do_redirect = home_url().$do_redirect;
}
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: ' . $do_redirect);
exit();
}
else { unset($redirects); }
}
}
} // end funcion redirect
public function extractPathFromURL($url) {
$parsedUrl = parse_url($url);
if (isset($parsedUrl['path'])) {
return $parsedUrl['path'];
} else {
// Trường hợp không có path trong URL
return "/";
}
}
public static function str_ireplace($search, $replace, $subject)
{
$token = chr(1);
$haystack = strtolower($subject);
$needle = strtolower($search);
while (($pos=strpos($haystack, $needle))!==false) {
$subject = substr_replace($subject, $token, $pos, strlen($search));
$haystack = substr_replace($haystack, $token, $pos, strlen($search));
}
$subject = str_replace($token, $replace, $subject);
return $subject;
}
public function get_address() {
if( !( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) ) return;
// return the full address
return self::get_protocol().'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
public function get_protocol() {
// Set the base protocol to http
$protocol = 'http';
// check for https
if ( isset( $_SERVER["HTTPS"] ) && strtolower( $_SERVER["HTTPS"] ) == "on" ) {
$protocol .= "s";
}
return $protocol;
} // end function get_protocol
public function render_setting() {
// Cài đặt cho lớp auto_ex_link ở đây
?>
Canonical Setting
Settings saved.
';
}
$current_interval = get_option('aiautotool_canonical_public_time', 5);
$number_post = get_option('aiautotool_canonical_number_post', 1);
?>
active=='true') {
// Cài đặt cho lớp auto_ex_link ở đây
echo ' Canonical ';
}
}
public function render_feature(){
$autoToolBox = new AutoToolBox($this->icon.' '.$this->name_plan, "Canonical multiple posts intelligently, schedule deletion, and 301 old posts after canonicalizing for a certain period, supporting the retention of old link values ", "https://doc.aiautotool.com/", $this->active_option_name, $this->active,plugins_url('../images/logo.svg', __FILE__));
echo ($autoToolBox->generateHTML());
}
}
}