rebase on oct-10-2023

This commit is contained in:
Rachit Bhargava
2023-10-10 17:23:21 -04:00
parent d37566ffb6
commit d096058d7d
4789 changed files with 254611 additions and 307223 deletions

View File

@@ -2,6 +2,7 @@
namespace WPMailSMTP\Admin\DebugEvents;
use WP_Error;
use WPMailSMTP\Admin\Area;
use WPMailSMTP\Options;
use WPMailSMTP\Tasks\DebugEventsCleanupTask;
@@ -14,6 +15,15 @@ use WPMailSMTP\WP;
*/
class DebugEvents {
/**
* Transient name for the error debug events.
*
* @since 3.9.0
*
* @var string
*/
const ERROR_DEBUG_EVENTS_TRANSIENT = 'wp_mail_smtp_error_debug_events_transient';
/**
* Register hooks.
*
@@ -248,6 +258,51 @@ class DebugEvents {
);
}
/**
* Returns the number of error debug events in a given time span.
*
* By default it returns the number of error debug events in the last 30 days.
*
* @since 3.9.0
*
* @param string $span_of_time The time span to count the events for. Default '-30 days'.
*
* @return int|WP_Error The number of error debug events or WP_Error on failure.
*/
public static function get_error_debug_events_count( $span_of_time = '-30 days' ) {
$timestamp = strtotime( $span_of_time );
if ( ! $timestamp || $timestamp > time() ) {
return new WP_Error( 'wp_mail_smtp_admin_debug_events_get_error_debug_events_count_invalid_time', 'Invalid time span.' );
}
$transient_key = self::ERROR_DEBUG_EVENTS_TRANSIENT . '_' . sanitize_title_with_dashes( $span_of_time );
$cached_error_events_count = get_transient( $transient_key );
if ( $cached_error_events_count !== false ) {
return (int) $cached_error_events_count;
}
global $wpdb;
// phpcs:disable WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder
$sql = $wpdb->prepare(
'SELECT COUNT(*) FROM `%1$s` WHERE event_type = %2$d AND created_at >= "%3$s"',
self::get_table_name(),
Event::TYPE_ERROR,
gmdate( WP::datetime_mysql_format(), $timestamp )
);
// phpcs:enable WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
$error_events_count = (int) $wpdb->get_var( $sql );
set_transient( $transient_key, $error_events_count, HOUR_IN_SECONDS );
return $error_events_count;
}
/**
* Register the screen options for the debug events page.
*
@@ -359,7 +414,8 @@ class DebugEvents {
$table = self::get_table_name();
$is_valid = (bool) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s;', $table ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
$is_valid = (bool) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s;', $table ) );
return $is_valid;
}