getMessage() ); } } add_action( 'wp_footer', 'aimsaisc_handle_error' ); /** * Log plugin errors. * * @param string $message Error message to log. */ function aimsaisc_log_error( $message ) { global $wp_filesystem; if ( empty( $wp_filesystem ) ) { require_once ABSPATH . '/wp-admin/includes/file.php'; WP_Filesystem(); } $upload_dir = wp_upload_dir(); $plugin_dir = trailingslashit( $upload_dir['basedir'] ) . 'aims-ai-scheduler'; $log_file = $plugin_dir . '/php_error_log.txt'; if ( ! file_exists( $plugin_dir ) ) { wp_mkdir_p( $plugin_dir ); // Create log directory. } if ( ! file_exists( $log_file ) ) { $wp_filesystem->put_contents( $log_file, '', FS_CHMOD_FILE ); // Create empty file. } $formatted_message = '[' . gmdate( 'Y-m-d H:i:s' ) . '] ' . $message . PHP_EOL; $existing_content = $wp_filesystem->exists( $log_file ) ? $wp_filesystem->get_contents( $log_file ) : ''; $wp_filesystem->put_contents( $log_file, $existing_content . $formatted_message, FS_CHMOD_FILE ); } /** * Add custom cron intervals. * * @param array $schedules Existing cron schedules. * @return array Modified cron schedules. */ function aimsaisc_content_custom_intervals( $schedules ) { $schedules['2_hours'] = array( 'interval' => 7200, 'display' => __( 'Every 2 Hours', 'aims-ai-scheduler' ), ); $schedules['12_hours'] = array( 'interval' => 43200, 'display' => __( 'Every 12 Hours', 'aims-ai-scheduler' ), ); return $schedules; } add_filter( 'cron_schedules', 'aimsaisc_content_custom_intervals' ); /** * Clear cron on plugin deactivation. */ function ai_clear_cron_on_deactivate() { wp_clear_scheduled_hook( 'aimsaisc_generate_content_event' ); } register_deactivation_hook( __FILE__, 'ai_clear_cron_on_deactivate' ); /** * Schedule custom cron event on plugin activation. */ function ai_schedule_custom_cron() { wp_clear_scheduled_hook( 'aimsaisc_generate_content_event' ); $interval = get_option( 'aimsaisc_content_cron_interval', 'daily' ); $valid_intervals = array_keys( wp_get_schedules() ); if ( ! in_array( $interval, $valid_intervals, true ) ) { $interval = 'daily'; // Fallback to daily. } wp_schedule_event( time(), $interval, 'aimsaisc_generate_content_event' ); } register_activation_hook( __FILE__, 'ai_schedule_custom_cron' ); /** * Get available WP languages from JSON file. * * @return array List of languages or error message. */ function get_all_wp_languages_from_json() { $json_path = AIMSAISC_PLUGIN_DIR . 'assets/language/language.json'; if ( ! file_exists( $json_path ) ) { return array( 'error' => 'languages.json file not found.' ); } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $json = file_get_contents( $json_path ); // Note: use wp_remote_get() for remote URLs. $data = json_decode( $json, true ); if ( ! is_array( $data ) || empty( $data['translations'] ) ) { return array( 'error' => 'Invalid or empty translations data.' ); } $languages = array(); foreach ( $data['translations'] as $lang ) { $locale = $lang['language']; $name = $lang['native_name'] ?? $lang['english_name']; $languages[ $locale ] = $name; } return $languages; } /** * Delete related data from aimsaisc_post_generator_prompt_created_post when a post is deleted. * * @param int $post_id ID of the deleted post. */ function aimsaisc_delete_prompt_row_on_post_delete( $post_id ) { global $wpdb; $table = $wpdb->prefix . 'aimsaisc_post_generator_prompt_created_post'; // Only delete if the post ID exists in the table $wpdb->delete( $table, array( 'post_id' => $post_id ), array( '%d' ) ); } add_action( 'before_delete_post', 'aimsaisc_delete_prompt_row_on_post_delete' );