ensure_widget_active(); add_action('wp_ajax_get_cron_status', array($this, 'get_cron_status_callback')); add_action('wp_ajax_get_notifications', array($this, 'get_notifications_callback')); add_action('admin_footer', array($this, 'add_cron_status_widget')); // Load text domain for translations at 'init' action to comply with WordPress 6.7.0+ requirements add_action('init', array($this, 'load_textdomain')); } /** * Ensure widget is always active - automatically enable if not already set * This widget should always run and cannot be disabled */ private function ensure_widget_active() { $active_option = 'AIAutoTool_Cron_Status_Widget_active'; $current_value = get_option($active_option); // If option doesn't exist or is not 'true', set it to 'true' if ($current_value === false || $current_value !== 'true') { update_option($active_option, 'true'); } } public function load_textdomain() { load_plugin_textdomain('aiautotool-cron-widget', false, dirname(plugin_basename(__FILE__)) . '/languages'); } public function get_cron_status_callback() { // Set proper headers for JSON response if (!headers_sent()) { header('Content-Type: application/json; charset=UTF-8'); } // Check nonce without dying - handle error gracefully $nonce_check = check_ajax_referer('aiautotool_nonce', 'security', false); if (!$nonce_check) { wp_send_json_error(array( 'message' => __('Security check failed. Please refresh the page and try again.', 'aiautotool-cron-widget'), 'code' => 'nonce_failed' )); wp_die(); return; } // Check if user is logged in if (!is_user_logged_in()) { wp_send_json_error(array( 'message' => __('You must be logged in.', 'aiautotool-cron-widget'), 'code' => 'not_logged_in' )); wp_die(); return; } // Chỉ cho phép admin có quyền cao nhất if (!current_user_can('manage_options')) { wp_send_json_error(array( 'message' => __('Unauthorized access', 'aiautotool-cron-widget'), 'code' => 'insufficient_permissions' )); wp_die(); return; } $user = wp_get_current_user(); if (!$user || !$user->exists()) { wp_send_json_error(array( 'message' => __('Invalid user', 'aiautotool-cron-widget'), 'code' => 'invalid_user' )); wp_die(); return; } $user_roles = $user->roles; if (!is_array($user_roles)) { $user_roles = array(); } if (!in_array('administrator', $user_roles) && !is_super_admin()) { wp_send_json_error(array( 'message' => __('Insufficient permissions', 'aiautotool-cron-widget'), 'code' => 'insufficient_permissions' )); wp_die(); return; } try { $cron_status = get_transient('aiautotool_cron_status'); if (!$cron_status || !is_array($cron_status)) { $cron_status = array( 'is_running' => false, 'current_task' => '', 'current_post_title' => '', 'progress' => 0, 'last_update' => '', 'next_schedule' => '', 'cron_type' => '' ); } // Calculate next schedule time for all cron types $this->update_next_schedule_times($cron_status); wp_send_json_success($cron_status); } catch (Exception $e) { wp_send_json_error(array( 'message' => __('Error retrieving cron status: ', 'aiautotool-cron-widget') . $e->getMessage(), 'code' => 'exception' )); } wp_die(); // Always die after AJAX response } private function update_next_schedule_times(&$cron_status) { $next_schedules = array(); // Auto Content cron $content_interval = get_option('aiautotool_draft_public_time', 5); $next_content_run = time() + ($content_interval * 60); $next_schedules[] = __('Auto Content', 'aiautotool-cron-widget') . ': ' . date('H:i:s', $next_content_run); // Auto Comment cron $comment_interval = get_option('aiautotool_autocomment_time', 10); $next_comment_run = time() + ($comment_interval * 60); $next_schedules[] = __('Auto Comment', 'aiautotool-cron-widget') . ': ' . date('H:i:s', $next_comment_run); // Auto Create Tag cron $tag_interval = get_option('aiautotool_autocreatetag_time', 15); $next_tag_run = time() + ($tag_interval * 60); $next_schedules[] = __('Auto Create Tag', 'aiautotool-cron-widget') . ': ' . date('H:i:s', $next_tag_run); // Auto Suggest cron $suggest_interval = get_option('aiautotool_suggestauto_time', 20); $next_suggest_run = time() + ($suggest_interval * 60); $next_schedules[] = __('Auto Suggest', 'aiautotool-cron-widget') . ': ' . date('H:i:s', $next_suggest_run); $cron_status['next_schedule'] = implode(' | ', $next_schedules); } public function get_notifications_callback() { // Set proper headers for JSON response if (!headers_sent()) { header('Content-Type: application/json; charset=UTF-8'); } // Check nonce without dying - handle error gracefully $nonce_check = check_ajax_referer('aiautotool_nonce', 'security', false); if (!$nonce_check) { wp_send_json_error(array( 'message' => __('Security check failed. Please refresh the page and try again.', 'aiautotool-cron-widget'), 'code' => 'nonce_failed' )); wp_die(); return; } // Check if user is logged in if (!is_user_logged_in()) { wp_send_json_error(array( 'message' => __('You must be logged in.', 'aiautotool-cron-widget'), 'code' => 'not_logged_in' )); wp_die(); return; } // Chỉ cho phép admin có quyền cao nhất if (!current_user_can('manage_options')) { wp_send_json_error(array( 'message' => __('Unauthorized access', 'aiautotool-cron-widget'), 'code' => 'insufficient_permissions' )); wp_die(); return; } $user = wp_get_current_user(); if (!$user || !$user->exists()) { wp_send_json_error(array( 'message' => __('Invalid user', 'aiautotool-cron-widget'), 'code' => 'invalid_user' )); wp_die(); return; } $user_roles = $user->roles; if (!is_array($user_roles)) { $user_roles = array(); } if (!in_array('administrator', $user_roles) && !is_super_admin()) { wp_send_json_error(array( 'message' => __('Insufficient permissions', 'aiautotool-cron-widget'), 'code' => 'insufficient_permissions' )); wp_die(); return; } try { $notifications = get_transient('aiautotool_notifications'); if ($notifications === false) { $notifications = array(); } if (!is_array($notifications)) { $notifications = array(); } // Clear after sending delete_transient('aiautotool_notifications'); wp_send_json_success($notifications); } catch (Exception $e) { wp_send_json_error(array( 'message' => __('Error retrieving notifications: ', 'aiautotool-cron-widget') . $e->getMessage(), 'code' => 'exception' )); } wp_die(); // Always die after AJAX response } public function update_cron_status($status_data) { $status_data['last_update'] = current_time('Y-m-d H:i:s'); set_transient('aiautotool_cron_status', $status_data, 300); // 5 minutes // Send notification for important events if (isset($status_data['current_task'])) { if (strpos($status_data['current_task'], __('Post published successfully', 'aiautotool-cron-widget')) !== false) { $this->send_notification(__('Post published:', 'aiautotool-cron-widget') . ' ' . $status_data['current_post_title'], 'success'); } elseif (strpos($status_data['current_task'], __('Content creation completed', 'aiautotool-cron-widget')) !== false) { $this->send_notification(__('Content created for post:', 'aiautotool-cron-widget') . ' ' . $status_data['current_post_title'], 'success'); } elseif (strpos($status_data['current_task'], __('Comment creation completed', 'aiautotool-cron-widget')) !== false) { $this->send_notification(__('Comment created for post:', 'aiautotool-cron-widget') . ' ' . $status_data['current_post_title'], 'success'); } elseif (strpos($status_data['current_task'], __('Tag creation completed', 'aiautotool-cron-widget')) !== false) { $this->send_notification(__('Tag created for post:', 'aiautotool-cron-widget') . ' ' . $status_data['current_post_title'], 'success'); } elseif (strpos($status_data['current_task'], __('Suggestion creation completed', 'aiautotool-cron-widget')) !== false) { $this->send_notification(__('Draft post created:', 'aiautotool-cron-widget') . ' ' . $status_data['current_post_title'], 'success'); } elseif (strpos($status_data['current_task'], __('Error:', 'aiautotool-cron-widget')) !== false) { $this->send_notification($status_data['current_task'], 'error'); } } } public function send_notification($message, $type = 'info') { // Store notification for JavaScript to display $notifications = get_transient('aiautotool_notifications') ?: array(); $notifications[] = array( 'message' => $message, 'type' => $type, 'time' => current_time('Y-m-d H:i:s') ); set_transient('aiautotool_notifications', $notifications, 60); // 1 minute } public function add_cron_status_widget() { // Chỉ hiển thị cho admin có quyền cao nhất if (!current_user_can('manage_options')) { return; } // Kiểm tra thêm quyền super admin hoặc administrator $user = wp_get_current_user(); $user_roles = $user->roles; // Chỉ hiển thị cho administrator hoặc super admin if (!in_array('administrator', $user_roles) && !is_super_admin()) { return; } ?>