HEX
Server: nginx/1.18.0
System: Linux iZj6c1ieg2jrpk1z5tzi19Z 6.3.9-1.el7.elrepo.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jun 21 22:18:40 EDT 2023 x86_64
User: www (1001)
PHP: 8.2.4
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/www.cytocare.cn/wp-content/plugins/site-mailer/modules/logs/database/log-entry.php
<?php

namespace SiteMailer\Modules\Logs\Database;

use SiteMailer\Classes\Database\Entry;
use SiteMailer\Modules\Statuses\Database\Statuses_Table;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * Class Log_Entry
 */
class Log_Entry extends Entry {
	/**
	 * @var mixed|null
	 */
	public mixed $subject;
	/**
	 * @var mixed|null
	 */
	public mixed $message;
	/**
	 * @var mixed|null
	 */
	public mixed $to;

	public static function get_helper_class(): string {
		return Logs_Table::get_class_name();
	}

	/**
	 * @param string | array $where
	 * @param null $limit
	 * @param null $offset
	 * @param array $order_by
	 *
	 * @return array
	 */
	public static function get_logs(
		$where = '1',
		$limit = null,
		$offset = null,
		array $order_by = []
	): array {

		$logs_table = Logs_Table::table_name();
		$statuses_table = Statuses_Table::table_name();

		$fields = [
			"$logs_table.*",
			"$statuses_table.status AS single_status",
			"$statuses_table.opened AS single_opened",
			"COUNT($statuses_table.id) AS status_count",
		];

		$group_by = [
			"$logs_table.id",
		];

		$join = "LEFT JOIN $statuses_table ON $logs_table.api_id = $statuses_table.log_id";

		$query = Logs_Table::build_sql_string( $fields, $where, $limit, $offset, $join, $order_by, $group_by );

		return Logs_Table::db()->get_results( $query );
	}

	/**
	 * @param string|array $where
	 *
	 * @return array
	 */
	public static function get_logs_count( $where ): array {
		return Logs_Table::select( 'COUNT(*) as count', $where );
	}

	/**
	 * @param string|array $where
	 *
	 * @return array
	 */
	public static function get_logs_stats( $where ): array {
		return Logs_Table::select(
			"COUNT(
					CASE WHEN `status` NOT IN ('not sent', 'rate limit', 'not valid', 'unsubscribed') THEN 1 END
				) as total,
				COUNT(CASE WHEN `status` = 'delivered' THEN 1 END) as delivered,
				COUNT(CASE WHEN `status` IN ('failed', 'bounce', 'dropped') THEN 1 END) as failed,
				COUNT(CASE WHEN `opened` = 1 THEN 1 END) as opened",
			$where
		);
	}

	/**
	 * @param array $ids
	 *
	 * @return void
	 */
	public static function delete_logs( array $ids ): void {
		$escaped = implode( ',', array_map( function ( $item ) {
			return Logs_Table::db()->prepare( '%s', $item );
		}, $ids ) );
		$query = 'DELETE FROM `' . Logs_Table::table_name() . '` WHERE `' . Logs_Table::API_ID . '` IN(' . $escaped . ')';
		Logs_Table::query( $query );
	}

	/**
	 * Delete logs oldest then 30 days
	 * @return void
	 */
	public static function delete_expired_logs() {
		$query = 'DELETE FROM `' . Logs_Table::table_name() . '` WHERE `' . Logs_Table::CREATED_AT . '` < NOW() - INTERVAL 30 DAY';
		Logs_Table::query( $query );
	}
}