From 7c28fafb39f7f9d83f94b35436fe31fab2e4b77f Mon Sep 17 00:00:00 2001 From: berchj Date: Thu, 18 Jul 2024 01:10:17 -0300 Subject: [PATCH 1/4] tests clases --- ai-entries/includes/class-ai-entries-cron.php | 26 +- .../includes/class-ai-entries-settings.php | 1 + ai-entries/includes/class-ai-entries.php | 27 +- ai-entries/includes/settings-page.php | 6 +- ai-entries/tests/BaseTest.php | 339 ------------------ ai-entries/tests/bootstrap.php | 11 + ai-entries/tests/classes/AIEntriesAPITest.php | 115 ++++++ ...ingsTest.php => AIEntriesSettingsTest.php} | 32 +- ai-entries/tests/classes/AIEntriesTest.php | 23 +- makefile | 3 +- readme.md | 19 +- 11 files changed, 227 insertions(+), 375 deletions(-) delete mode 100644 ai-entries/tests/BaseTest.php create mode 100644 ai-entries/tests/bootstrap.php create mode 100644 ai-entries/tests/classes/AIEntriesAPITest.php rename ai-entries/tests/classes/{AiEntriesSettingsTest.php => AIEntriesSettingsTest.php} (63%) diff --git a/ai-entries/includes/class-ai-entries-cron.php b/ai-entries/includes/class-ai-entries-cron.php index ae8d4e7..ddd22e7 100644 --- a/ai-entries/includes/class-ai-entries-cron.php +++ b/ai-entries/includes/class-ai-entries-cron.php @@ -2,11 +2,11 @@ class AIEntries_Cron { - public static function daily_task() { + public function daily_task() { $question = get_option('AIEntries_question', ''); $num_calls = get_option('AIEntries_num_calls', 1); $api_key = get_option('AIEntries_api_key', ''); - $category_name = get_option('AIEntries_category', ''); + $category_name = get_option('AIEntries_category', ''); if (!empty($question) && $num_calls > 0) { for ($i = 0; $i < $num_calls; $i++) { @@ -14,4 +14,26 @@ public static function daily_task() { } } } + public static function show_next_scheduled_cron() { + // Specify the hook name of the cron job you are interested in + $hook = 'AIEntries_daily_cron_job'; + + // Get the next scheduled occurrence of the specified cron job + $next_scheduled = wp_next_scheduled( $hook ); + + if ( $next_scheduled !== false ) { + // Convert Unix timestamp to human-readable format + $next_scheduled_date = date( 'Y-m-d H:i:s', $next_scheduled ); + + // Display the admin notice + echo '
'; + echo '

Next scheduled occurrence of the cron job "' . $hook . '" is: ' . $next_scheduled_date . '

'; + echo '
'; + } else { + // If the event is not scheduled + echo '
'; + echo '

The cron job "' . $hook . '" is not currently scheduled.

'; + echo '
'; + } + } } diff --git a/ai-entries/includes/class-ai-entries-settings.php b/ai-entries/includes/class-ai-entries-settings.php index d8c317e..1560214 100644 --- a/ai-entries/includes/class-ai-entries-settings.php +++ b/ai-entries/includes/class-ai-entries-settings.php @@ -12,6 +12,7 @@ public static function add_menu_page() 'AIEntries-settings', [self::class, 'settings_page'] ); + return true; } public static function settings_page() diff --git a/ai-entries/includes/class-ai-entries.php b/ai-entries/includes/class-ai-entries.php index 3d27526..59f6ab3 100644 --- a/ai-entries/includes/class-ai-entries.php +++ b/ai-entries/includes/class-ai-entries.php @@ -1,43 +1,52 @@ includes(); $this->init_hooks(); } - public static function createInstance() { + public static function createInstance() + { return new self(); } - public static function instance() { + public static function instance() + { if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } - private function includes() { + private function includes() + { require_once plugin_dir_path(__FILE__) . 'class-ai-entries-settings.php'; require_once plugin_dir_path(__FILE__) . 'class-ai-entries-api.php'; require_once plugin_dir_path(__FILE__) . 'class-ai-entries-cron.php'; } - private function init_hooks() { + private function init_hooks() + { add_action('admin_menu', ['AIEntries_Settings', 'add_menu_page']); add_action('AIEntries_daily_cron_job', ['AIEntries_Cron', 'daily_task']); + add_action('admin_notices', ['AIEntries_Cron', 'show_next_scheduled_cron']); } - public static function activate() { + public static function activate() + { if (!wp_next_scheduled('AIEntries_daily_cron_job')) { - wp_schedule_event(time(), 'daily', 'AIEntries_daily_cron_job'); + wp_schedule_event(time(), 'hourly', 'AIEntries_daily_cron_job'); } } - public static function deactivate() { + public static function deactivate() + { $timestamp = wp_next_scheduled('AIEntries_daily_cron_job'); wp_unschedule_event($timestamp, 'AIEntries_daily_cron_job'); } diff --git a/ai-entries/includes/settings-page.php b/ai-entries/includes/settings-page.php index 6ed4ae7..d979914 100644 --- a/ai-entries/includes/settings-page.php +++ b/ai-entries/includes/settings-page.php @@ -1,7 +1,9 @@ +

AIEntries Settings

-

This plugin runs once a day according to the following parameters:

- +

This plugin runs once a day according to the following parameters: