'; private $client = null; public $notices = []; public $limit = AIAUTOTOOL_FREE; private $plan_limit_aiautotool ; public $name_plan ; public $config = array(); public $notice ; public function __construct() { $this->name_plan = __('Index GOOGLE API','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, false); if ($this->active=='true') { $this->init(); } 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_die(__('Permission denied', 'ai-auto-tool'), '', array('response' => 403)); } if (isset($_POST['active'])) { $active = sanitize_text_field($_POST['active']); update_option($this->active_option_name, $active,null, 'no'); print_r($active); } wp_die(); } function init(){ add_action('admin_init', array($this, 'init_settings')); add_action( 'wp_ajax_push_link', [ $this, 'ajax_pushurltoapi' ] ); add_action( 'admin_notices', [ $this, 'display_notices' ], 10, 1 ); // print_r($this->get_setting( 'json_key' )); if ( $this->get_setting( 'json_key' ) ) { add_action('admin_menu', array($this, 'add_menu')); $post_types = $this->get_setting( 'post_type', [] ); foreach ( $post_types as $key => $post_type ) { if ( empty( $post_type ) ) { continue; } add_action( 'save_post_' . $post_type, [ $this, 'publish_post' ], 10, 2 ); // add_filter( 'bulk_actions-edit-' . $post_type, [ $this, 'register_bulk_actions' ] ); // add_filter( 'handle_bulk_actions-edit-' . $post_type, [ $this, 'bulk_action_handler' ], 10, 3 ); } add_action( 'wp_trash_post', [ $this, 'delete_post' ], 10, 1 ); } add_action('wp_ajax_clear_log_submitindex', array($this, 'clear_log_submitindex_callback')); } public function clear_log_submitindex_callback() { // Xóa log bằng cách cập nhật giá trị option thành chuỗi rỗng update_option('aiautotool_auto_submitindex_submissions', array(),null, 'no'); // Trả về một phản hồi để cho biết đã xóa thành công wp_send_json_success('Log cleared successfully.'); wp_die(); } public function display_notices() { $screen = get_current_screen(); $stored = get_option( 'aiautotool_submitindex_notices', [] ); $this->notices = array_merge( $stored, $this->notices ); delete_option( 'aiautotool_submitindex_notices' ); foreach ( $this->notices as $notice ) { if ( ! empty( $notice['show_on'] ) && is_array( $notice['show_on'] ) && ! in_array( $screen->id, $notice['show_on'], true ) ) { return; } $class = 'notice instant-indexing-notice ' . $notice['class']; printf( '

%2$s

', esc_attr( $class ), wp_kses_post( $notice['message'] ) ); } } public function add_menu() { add_submenu_page( MENUSUBPARRENT, ' Submit index ', ' Submit index', 'manage_options', 'submit-index', array($this, 'ai_autotool_console') ); } public function ajax_pushurltoapi() { // check_ajax_referer( 'aiautotool-console' ); // if ( ! current_user_can( apply_filters( 'rank_math/indexing_api/capability', 'manage_options' ) ) ) { // die( '0' ); // } $url_input = $this->get_input_urls(); $action = sanitize_title( wp_unslash( $_POST['ajax_action'] ) ); header( 'Content-type: application/json' ); $result = $this->send_to_api( $url_input, $action, true ); wp_send_json( array('success'=>true,'data'=>$result )); exit(); } public function ai_autotool_console(){ ?>

Ai auto Tool

Limit counter

get_limits(); $json = $this->get_settings( ); ?>

= /
= /
= /

'array', 'sanitize_callback' => array($this, 'sanitize_submitindex_setting'), )); add_settings_section('aiautotool-section', __('AI Auto Tool Settings', 'ai-auto-tool' ), array($this, 'section_callback'), 'aiautotool-settings'); add_settings_field('submitindex_field', __('Information:', 'ai-auto-tool' ), array($this, 'submitindex_field_callback'), 'aiautotool-settings', 'aiautotool-section'); add_settings_field('post_types_field', __('Post Types List:', 'ai-auto-tool' ), array($this, 'post_types_field_callback'), 'aiautotool-settings', 'aiautotool-section'); } /** * Sanitize submitindex setting * * @param mixed $value Input value * @return array Sanitized array */ public function sanitize_submitindex_setting($value) { if (!is_array($value)) { return array(); } return map_deep($value, 'sanitize_text_field'); } // Gọi hàm này khi vào trang cài đặt public function render_setting() { if($this->active!="true"){ return ''; } $setting = get_option('aiautotool_setting_submitindex'); ?> active=="true"){ echo ''; } } public function render_feature() { // Load feature descriptions require_once plugin_dir_path(dirname(__FILE__)) . 'lib/feature-descriptions.php'; $autoToolBox = new AutoToolBox( $this->icon.' '.__('Google Indexing API','ai-auto-tool'), AiAutoTool_Feature_Descriptions::get('google_index'), "https://doc.aiautotool.com/features/google-indexing", $this->active_option_name, $this->active, plugins_url('../../images/logo.svg', __FILE__) ); echo ($autoToolBox->generateHTML()); } // Callback cho section public function section_callback() { echo '

' . esc_html__('Enter information and select Post Types.', 'ai-auto-tool' ) . '

'; } // Callback cho textarea thông tin public function submitindex_field_callback() { $value = get_option('aiautotool_setting_submitindex'); echo ''; } // Callback cho danh sách Post Types public function post_types_field_callback() { $post_types = get_post_types(array('public' => true), 'objects'); $selected_post_types = get_option('aiautotool_setting_post_types', array()); foreach ($post_types as $post_type) { $checked = in_array($post_type->name, $selected_post_types) ? 'checked="checked"' : ''; echo ' ' . esc_html($post_type->label) . '
'; } } private function get_settings() { $settings = get_option( 'aiautotool_setting_submitindex', [] ); return $settings; } public function get_setting( $setting, $default = null ) { $settings = $this->get_settings(); if ( $setting === 'json_key' ) { if(isset($settings[ 'json_key' ])){ $jsonkey = $settings[ 'json_key' ]; if(count($jsonkey)>0){ $keyrandom = $jsonkey[array_rand($jsonkey)]; return $keyrandom; }else{ return null; } }else{ return null; } } return ( isset( $settings[ $setting ] ) ? $settings[ $setting ] : $default ); } public function publish_post( $post_id ) { $post = get_post( $post_id ); if ( $post->post_status !== 'publish' ) { return; } if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) { return; } $send_url = apply_filters( 'aiautotool/submitindex/publish_url', get_permalink( $post ), $post, 'google' ); // Early exit if filter is set to false. if ( ! $send_url ) { return; } $this->send_to_api( $send_url, 'update', false ); $this->add_notice( __( 'A recently published post has been automatically submitted to the Search Index API.', 'ai-auto-tool' ), 'notice-info', null, true ); } public function add_notice( $message, $class = '', $show_on = null, $persist = false, $id = '' ) { $notice = [ 'message' => $message, 'class' => $class . ' is-dismissible', 'show_on' => $show_on, ]; if ( ! $id ) { $id = md5( serialize( $notice ) ); } if ( $persist ) { $notices = get_option( 'aiautotool_submitindex_notices', [] ); $notices[ $id ] = $notice; update_option( 'aiautotool_submitindex_notices', $notices,null, 'no' ); return; } $this->notices[ $id ] = $notice; } public function delete_post( $post_id ) { $post_types = $this->get_setting( 'post_type', [] ); $post = get_post( $post_id ); if ( ! in_array( $post->post_type, $post_types, true ) ) { return; } // Only submit delete action if post was published. if ( $post->post_status !== 'publish' ) { return; } $send_url = apply_filters( 'aiautotool/submitindex/delete_url', get_permalink( $post ), $post ); // Early exit if filter is set to false. if ( ! $send_url ) { return; } $this->send_to_api( $send_url, 'delete', false ); $this->add_notice( __( 'A deleted post has been automatically submitted to the Search Index API for deletion.', 'ai-auto-tool' ), 'notice-info', null, true ); } public function get_limits() { $current_limits = [ 'publishperday' => 0, 'permin' => 0, 'metapermin' => 0, 'bing_submitperday' => 0, ]; $settings = $this->get_settings(); $count = 200; if(isset($settings[ 'json_key' ])){ $jsonkey = $settings[ 'json_key' ]; if($jsonkey){ // $jsonkey = json_decode($jsonkey); $count = 200 * count($jsonkey); } } $limit_publishperday = apply_filters( 'aiautotool/submitindex/limit_publishperday', $count ); $limit_permin = apply_filters( 'aiautotool/submitindex/limit_perminute', 600 ); $limit_metapermin = apply_filters( 'aiautotool/submitindex/limit_metaperminute', 180 ); $limit_bingsubmitperday = apply_filters( 'aiautotool/submitindex/limit_bing_submitperday', 10 ); $requests_log = get_option( 'aiautotool_submitindex_requests', [ 'update' => [], 'delete' => [], 'getstatus' => [], 'bing_submit' => [], ] ); $timestamp_1day_ago = strtotime( '-1 day' ); $timestamp_1min_ago = strtotime( '-1 minute' ); $publish_1day = 0; $all_1min = 0; $meta_1min = 0; foreach ( $requests_log['update'] as $time ) { if ( $time > $timestamp_1day_ago ) { $publish_1day++; } if ( $time > $timestamp_1min_ago ) { $all_1min++; } } foreach ( $requests_log['delete'] as $time ) { if ( $time > $timestamp_1min_ago ) { $all_1min++; } } foreach ( $requests_log['getstatus'] as $time ) { if ( $time > $timestamp_1min_ago ) { $all_1min++; $meta_1min++; } } $bing_submit_1day = 0; if ( ! isset( $requests_log['bing_submit'] ) ) { $requests_log['bing_submit'] = []; } foreach ( $requests_log['bing_submit'] as $time ) { if ( $time > $timestamp_1day_ago ) { $bing_submit_1day++; } } $current_limits['publishperday'] = $limit_publishperday - $publish_1day; $current_limits['permin'] = $limit_permin - $all_1min; $current_limits['metapermin'] = $limit_metapermin - $meta_1min; $current_limits['bing_submitperday'] = $limit_bingsubmitperday - $bing_submit_1day; $current_limits['publishperday_max'] = $limit_publishperday; $current_limits['permin_max'] = $limit_permin; $current_limits['metapermin_max'] = $limit_metapermin; $current_limits['bing_submitperday_max'] = $limit_bingsubmitperday; return $current_limits; } public function send_to_api( $url_input, $action, $is_manual = true ) { $url_input = (array) $url_input; $urls_count = count( $url_input ); if ( strpos( $action, 'bing' ) === false ) { $url_input = array_unique( $url_input ); // if ( count( $url_input ) > 100 ) { // $url_input = array_slice( $url_input, 0, 100 ); // } $auto_submission_log = get_option( 'aiautotool_auto_submitindex_submissions', [] ); if ( ! $is_manual ) { // We keep the auto-submitted URLs in a log to prevent duplicates. $logs = array_values( array_reverse( $auto_submission_log ) ); if ( ! empty( $logs[0] ) && $logs[0]['url'] === $url_input[0] && time() - $logs[0]['time'] < self::THROTTLE_LIMIT ) { return false; } } $keyjson = $this->get_setting( 'json_key' ); // This is NOT a Bing API request, so it's Google. include_once AIAUTOTOOL_DIR . 'vendor/autoload.php'; $this->client = new Google_Client(); $this->client->setAuthConfig( (array)json_decode($keyjson,true)); $this->client->setConfig( 'base_path', 'https://indexing.googleapis.com' ); $this->client->addScope( 'https://www.googleapis.com/auth/indexing' ); // Batch request. $this->client->setUseBatch( true ); // init google batch and set root URL. $service = new Google_Service_Indexing( $this->client ); $batch = new Google_Http_Batch( $this->client, false, 'https://indexing.googleapis.com' ); foreach ( $url_input as $i => $url ) { $post_body = new Google_Service_Indexing_UrlNotification(); if ( $action === 'getstatus' ) { $request_part = $service->urlNotifications->getMetadata( [ 'url' => $url ] ); // phpcs:ignore } else { $post_body->setType( $action === 'update' ? 'URL_UPDATED' : 'URL_DELETED' ); $post_body->setUrl( $url ); $request_part = $service->urlNotifications->publish( $post_body ); // phpcs:ignore } $batch->add( $request_part, 'url-' . $i ); // Log auto-submitted URLs. } // if ( ! $is_manual ) { // } $results = $batch->execute(); $data = []; $res_count = count( $results ); foreach ( $results as $id => $response ) { // Change "response-url-1" to "url-1". $local_id = substr( $id, 9 ); if ( is_a( $response, 'Google_Service_Exception' ) ) { $data[ $local_id ] = json_decode( $response->getMessage() ); } else { $data[ $local_id ] = (array) $response->toSimpleObject(); } if ( ! $is_manual ) { $auto_submission_log[] = [ 'url' => $url, 'time' => time(), 'manual_submission'=>1, 'result'=>$data[ $local_id ] ]; }else{ $auto_submission_log[] = [ 'url' => $url, 'time' => time(), 'manual_submission'=>0, 'result'=>$data[ $local_id ] ]; } if ( $res_count === 1 ) { $data = $data[ $local_id ]; } } if ( count( $auto_submission_log ) > 100 ) { $auto_submission_log = array_slice( $auto_submission_log, -100, 100, true ); } update_option( 'aiautotool_auto_submitindex_submissions', $auto_submission_log,null, 'no' ); } else { // IndexNow submit URL. /** * Filter the URL to be submitted to IndexNow. * Returning false will prevent the URL from being submitted. * * @param bool $is_manual Whether the URL is submitted manually by the user. */ // if ( ! $is_manual ) { // $logs = array_values( array_reverse( $this->rmapi->get_log() ) ); // if ( ! empty( $logs[0] ) && $logs[0]['url'] === $url_input[0] && time() - $logs[0]['time'] < self::THROTTLE_LIMIT ) { // return false; // } // } // $request = $this->rmapi->submit( $url_input, $is_manual ); // if ( $request ) { // $data = [ // 'success' => true, // ]; // } else { // $data = [ // 'error' => [ // 'code' => $this->rmapi->get_response_code(), // 'message' => $this->rmapi->get_error(), // ], // ]; // } // $action = 'indexnow_submit'; } $this->log_request( $action, $urls_count ); return $data; } public function log_request( $type, $number = 1 ) { $requests_log = get_option( 'aiautotool_submitindex_requests', [ 'update' => [], 'delete' => [], 'getstatus' => [], 'bing_submit' => [], ] ); if ( ! isset( $requests_log[ $type ] ) ) { $requests_log[ $type ] = []; } $add = array_fill( 0, $number, time() ); $requests_log[ $type ] = array_merge( $requests_log[ $type ], $add ); if ( count( $requests_log[ $type ] ) > 600 ) { $requests_log[ $type ] = array_slice( $requests_log[ $type ], -600, 600, true ); } update_option( 'aiautotool_submitindex_requests', $requests_log,null, 'no' ); } public function displaylog($array) { if (!empty($array)) { $html = ''; $html .= ''; foreach ($array[0] as $key => $value) { $html .= ''; } $html .= ''; foreach ($array as $item) { $html .= ''; foreach ($item as $key => $value) { if ($key == 'time') { $formattedTime = date('Y-m-d H:i:s', $value); $html .= ''; } elseif ($key == 'manual_submission' && $value == 0) { $html .= ''; } elseif ($key == 'manual_submission' && $value == 1) { $html .= ''; }elseif($key=='result'){ // print_r($value); $value = (array)$value; if(isset($value['urlNotificationMetadata']->latestRemove)){ $html .= ''; }else{ $html .= ''; } }else { $html .= ''; } } $html .= ''; } $html .= '
' . htmlspecialchars($key) . '
' . htmlspecialchars($formattedTime) . 'AutoManual'.$value['urlNotificationMetadata']->latestRemove->type.''.$value['urlNotificationMetadata']->latestUpdate->type.'' . htmlspecialchars($value) . '
'; echo esc_html($html); } else { echo 'No Logs'; } } } // End of class_exists check }