This commit is contained in:
2026-07-02 15:54:39 -06:00
commit 9883323161
17470 changed files with 4470592 additions and 0 deletions
@@ -0,0 +1,48 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Logging\Endpoints;
use Gravity_Forms\Gravity_SMTP\Models\Debug_Log_Model;
use Gravity_Forms\Gravity_SMTP\Users\Roles;
use Gravity_Forms\Gravity_Tools\Endpoints\Endpoint;
class Delete_Debug_Logs_Endpoint extends Endpoint {
const PARAM_ALL_LOGS = 'all_logs';
const ACTION_NAME = 'delete_debug_logs';
protected $minimum_cap = Roles::DELETE_DEBUG_LOG;
/**
* @var Debug_Log_Model
*/
protected $logs;
protected $required_params = array(
self::PARAM_ALL_LOGS,
);
public function __construct( Debug_Log_Model $logs ) {
$this->logs = $logs;
}
protected function get_nonce_name() {
return self::ACTION_NAME;
}
public function handle() {
if ( ! $this->validate() ) {
wp_send_json_error( __( 'Missing required parameters.', 'gravitysmtp' ), 400 );
}
$delete_all_logs = filter_input( INPUT_POST, self::PARAM_ALL_LOGS );
$delete_all_logs = htmlspecialchars( $delete_all_logs );
if ( $delete_all_logs == '1' ) {
$this->logs->clear();
wp_send_json_success( array( 'message' => __( 'All logs deleted successfully', 'gravitysmtp' ) ), 200 );
}
}
}
@@ -0,0 +1,54 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Logging\Endpoints;
use Gravity_Forms\Gravity_SMTP\Models\Event_Model;
use Gravity_Forms\Gravity_SMTP\Users\Roles;
use Gravity_Forms\Gravity_Tools\Endpoints\Endpoint;
class Delete_Email_Endpoint extends Endpoint {
const PARAM_EVENT_ID = 'event_id';
const ACTION_NAME = 'delete_email';
protected $minimum_cap = Roles::DELETE_EMAIL_LOG;
/**
* @var Event_Model
*/
protected $emails;
public function __construct( Event_Model $emails ) {
$this->emails = $emails;
}
protected function get_nonce_name() {
return self::ACTION_NAME;
}
public function handle() {
if ( ! $this->validate() ) {
wp_send_json_error( __( 'Missing required parameters.', 'gravitysmtp' ), 400 );
}
$event_id = filter_input( INPUT_POST, self::PARAM_EVENT_ID, FILTER_SANITIZE_NUMBER_INT );
$this->emails->delete( $event_id );
wp_send_json_success( array( 'message' => __( 'Event deleted successfully', 'gravitysmtp' ) ), 200 );
}
protected function validate() {
if ( ! parent::validate() ) {
return false;
}
if ( empty( $_REQUEST[ self::PARAM_EVENT_ID ] ) ) {
return false;
}
return true;
}
}
@@ -0,0 +1,70 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Logging\Endpoints;
use Gravity_Forms\Gravity_SMTP\Models\Event_Model;
use Gravity_Forms\Gravity_SMTP\Users\Roles;
use Gravity_Forms\Gravity_Tools\Endpoints\Endpoint;
class Delete_Events_Endpoint extends Endpoint {
const PARAM_EVENT_IDS = 'event_ids';
const PARAM_ALL_EVENTS = 'all_events';
const PARAM_MAX_DATE = 'max_date';
const ACTION_NAME = 'delete_events';
protected $minimum_cap = Roles::DELETE_EMAIL_LOG;
/**
* @var Event_Model
*/
protected $events;
protected $required_params = array(
self::PARAM_EVENT_IDS,
self::PARAM_ALL_EVENTS,
);
public function __construct( Event_Model $events ) {
$this->events = $events;
}
protected function get_nonce_name() {
return self::ACTION_NAME;
}
public function handle() {
if ( ! $this->validate() ) {
wp_send_json_error( __( 'Missing required parameters.', 'gravitysmtp' ), 400 );
}
$delete_all_events = filter_input( INPUT_POST, self::PARAM_ALL_EVENTS );
$delete_all_events = htmlspecialchars( $delete_all_events );
$max_date = filter_input( INPUT_POST, self::PARAM_MAX_DATE );
if ( ! empty( $max_date ) ) {
$max_date = htmlspecialchars( $max_date );
}
if ( ! empty( $max_date ) && $delete_all_events == '1' ) {
$this->events->delete_before( $max_date );
wp_send_json_success( array( 'message' => __( 'Events deleted successfully', 'gravitysmtp' ) ), 200 );
}
if ( $delete_all_events == '1' ) {
$this->events->delete_all();
wp_send_json_success( array( 'message' => __( 'All events deleted successfully', 'gravitysmtp' ) ), 200 );
}
$event_ids = filter_input( INPUT_POST, self::PARAM_EVENT_IDS, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
foreach( $event_ids as $id ) {
$id_to_delete = filter_var( $id, FILTER_SANITIZE_NUMBER_INT );
$this->events->delete( $id_to_delete );
}
wp_send_json_success( array( 'message' => $event_ids ), 200 );
}
}
@@ -0,0 +1,181 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Logging\Endpoints;
use Gravity_Forms\Gravity_SMTP\Models\Event_Model;
use Gravity_Forms\Gravity_SMTP\Users\Roles;
use Gravity_Forms\Gravity_Tools\Endpoints\Endpoint;
class Get_Email_Message_Endpoint extends Endpoint {
const PARAM_EVENT_ID = 'event_id';
const ACTION_NAME = 'get_email_message';
protected $minimum_cap = Roles::VIEW_EMAIL_LOG_DETAILS;
/**
* @var Event_Model
*/
protected $emails;
public function __construct( Event_Model $emails ) {
$this->emails = $emails;
}
protected function get_nonce_name() {
return self::ACTION_NAME;
}
public function handle() {
if ( ! $this->validate() ) {
wp_send_json_error( __( 'Missing required parameters.', 'gravitysmtp' ), 400 );
}
$event_id = filter_input( INPUT_GET, self::PARAM_EVENT_ID, FILTER_SANITIZE_NUMBER_INT );
$email = $this->emails->find( array( array( 'id', '=', $event_id ) ) );
if ( empty( $email[0] ) ) {
header( 'Content-Type: text/html' );
/* translators: %d: email ID */
echo sprintf( __( 'Could not get content for email ID: %d.', 'gravitysmtp' ), $email );
wp_die();
}
header( 'Content-Type: text/html' );
echo $this->format_email_content( $email[0]['message'] );
wp_die();
}
protected function format_email_content( $content ) {
if ( $content !== strip_tags( $content ) ) {
// Open links in a new tab (target) and harden with rel=noopener noreferrer where needed.
$content = preg_replace_callback(
'/<a\s([^>]*?)>/i',
function ( $matches ) {
$attrs = $matches[1];
$trimmed = trim( $attrs );
if ( '' === $trimmed ) {
return $matches[0];
}
$attrs = $this->ensure_anchor_target_blank( $attrs );
$attrs = $this->ensure_anchor_rel_noopener_noreferrer( $attrs );
return '<a ' . trim( $attrs ) . '>';
},
$content
);
return $content;
} else {
return '<pre style="white-space: pre-wrap; word-break: break-all; color: #242748; padding: 20px 25px; font-size: 13px; font-family: inter, -apple-system, blinkmacsystemfont, \'Segoe UI\', roboto, oxygen-sans, ubuntu, cantarell, \'Helvetica Neue\';">' . htmlspecialchars( $content ) . '</pre><style>body { margin: 0; background: #fff; }</style>';
}
}
/**
* Ensures anchor tags open in a new tab when attributes are present.
*
* @param string $attrs Raw attribute string from the opening &lt;a&gt; tag.
*
* @return string
*/
protected function ensure_anchor_target_blank( $attrs ) {
if ( preg_match( '/\btarget\s*=\s*(["\']?)_blank\1/i', $attrs ) ) {
return $attrs;
}
if ( preg_match( '/\btarget\s*=\s*("|\')(.*?)\1/is', $attrs ) ) {
return preg_replace( '/\btarget\s*=\s*("|\')(.*?)\1/is', 'target="_blank"', $attrs, 1 );
}
if ( preg_match( '/\btarget\s*=\s*[^\s>]+/i', $attrs ) ) {
return preg_replace( '/\btarget\s*=\s*[^\s>]+/i', 'target="_blank"', $attrs, 1 );
}
return rtrim( $attrs ) . ' target="_blank"';
}
/**
* Ensures rel includes noopener and noreferrer (safe external links with target _blank).
*
* @param string $attrs Raw attribute string from the opening &lt;a&gt; tag.
*
* @return string
*/
protected function ensure_anchor_rel_noopener_noreferrer( $attrs ) {
$rel_quoted = '/\brel\s*=\s*(["\'])(.*?)\1/is';
if ( preg_match( $rel_quoted, $attrs, $rel_match ) ) {
$tokens = preg_split( '/\s+/', trim( $rel_match[2] ), -1, PREG_SPLIT_NO_EMPTY );
if ( $this->rel_has_noopener_and_noreferrer( $tokens ) ) {
return $attrs;
}
$tokens = $this->merge_rel_noopener_noreferrer_tokens( $tokens );
$new_rel = implode( ' ', $tokens );
return preg_replace( $rel_quoted, 'rel="' . $new_rel . '"', $attrs, 1 );
}
if ( preg_match( '/\brel\s*=\s*([^\s>]+)/i', $attrs, $rel_match ) ) {
$tokens = preg_split( '/\s+/', trim( $rel_match[1] ), -1, PREG_SPLIT_NO_EMPTY );
if ( $this->rel_has_noopener_and_noreferrer( $tokens ) ) {
return $attrs;
}
$tokens = $this->merge_rel_noopener_noreferrer_tokens( $tokens );
$new_rel = implode( ' ', $tokens );
return preg_replace( '/\brel\s*=\s*[^\s>]+/i', 'rel="' . $new_rel . '"', $attrs, 1 );
}
return rtrim( $attrs ) . ' rel="noopener noreferrer"';
}
/**
* @param array $tokens rel attribute tokens.
*
* @return bool
*/
protected function rel_has_noopener_and_noreferrer( array $tokens ) {
$lower = array_map( 'strtolower', $tokens );
return in_array( 'noopener', $lower, true ) && in_array( 'noreferrer', $lower, true );
}
/**
* @param array $tokens rel attribute tokens.
*
* @return array
*/
protected function merge_rel_noopener_noreferrer_tokens( array $tokens ) {
$lower = array_map( 'strtolower', $tokens );
if ( ! in_array( 'noopener', $lower, true ) ) {
$tokens[] = 'noopener';
}
if ( ! in_array( 'noreferrer', $lower, true ) ) {
$tokens[] = 'noreferrer';
}
return $tokens;
}
protected function validate() {
if ( ! parent::validate() ) {
return false;
}
if ( empty( $_REQUEST[ self::PARAM_EVENT_ID ] ) ) {
return false;
}
return true;
}
}
@@ -0,0 +1,156 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Logging\Endpoints;
use Gravity_Forms\Gravity_SMTP\Models\Debug_Log_Model;
use Gravity_Forms\Gravity_SMTP\Users\Roles;
use Gravity_Forms\Gravity_Tools\Endpoints\Endpoint;
class Get_Paginated_Debug_Log_Items_Endpoint extends Endpoint {
const PARAM_PER_PAGE = 'per_page';
const PARAM_REQUESTED_PAGE = 'requested_page';
const PARAM_MAX_DATE = 'max_date';
const PARAM_SEARCH_TERM = 'search_term';
const PARAM_SEARCH_TYPE = 'search_type';
const PARAM_PRIORITY = 'priority';
const ACTION_NAME = 'get_paginated_debug_log_items';
protected $minimum_cap = Roles::VIEW_DEBUG_LOG;
/**
* @var Debug_Log_Model
*/
protected $events;
public function __construct( Debug_Log_Model $event_model ) {
$this->events = $event_model;
}
protected function get_nonce_name() {
return self::ACTION_NAME;
}
public function handle() {
if ( ! $this->validate() ) {
wp_send_json_error( __( 'Missing required parameters.', 'gravitysmtp' ), 400 );
}
$per_page = filter_input( INPUT_POST, 'per_page', FILTER_SANITIZE_NUMBER_INT );
$requested_page = filter_input( INPUT_POST, 'requested_page', FILTER_SANITIZE_NUMBER_INT );
$max_date = filter_input( INPUT_POST, 'max_date' );
$search_term = filter_input( INPUT_POST, 'search_term' );
$search_type = filter_input( INPUT_POST, 'search_type' );
$priority = filter_input( INPUT_POST, 'priority' );
if ( ! empty( $max_date ) ) {
$max_date = htmlspecialchars( $max_date );
}
if ( ! empty( $search_term ) ) {
$search_term = htmlspecialchars( $search_term );
}
if ( ! empty( $search_type ) ) {
$search_type = htmlspecialchars( $search_type );
}
if ( ! empty( $priority ) ) {
$priority = htmlspecialchars( $priority );
}
$requested_page = intval( $requested_page );
$offset = ( $requested_page - 1 ) * $per_page;
if ( ! $max_date ) {
$max_date = date( 'Y-m-d H:i:s', time() );
}
if ( empty( $per_page ) ) {
$per_page = 20;
}
$rows = $this->events->paginate( $requested_page, $per_page, $max_date, $search_term, $search_type, $priority );
$count = $this->events->count( $search_term, $search_type, $priority );
$data = array(
'rows' => $this->get_formatted_data_rows( $rows ),
'total' => $count,
'row_count' => count( $rows ),
);
wp_send_json_success( $data );
}
private function get_formatted_data_rows( $data ) {
return $this->events->lines_as_data_grid( $data );
}
private function get_grid_actions( $event_id ) {
$actions = array(
'component' => 'Box',
'components' => array(
array(
'component' => 'Button',
'props' => array(
'action' => 'view',
'customAttributes' => array(
'title' => esc_html__( 'View email log', 'gravitysmtp' ),
),
'customClasses' => array( 'gravitysmtp-data-grid__action' ),
'icon' => 'information-circle',
'iconPrefix' => 'gravity-component-icon',
'spacing' => [ 0, 2, 0, 0 ],
'size' => 'size-height-s',
'type' => 'icon-white',
'data' => array(
'event_id' => $event_id,
),
'disabled' => ! current_user_can( Roles::VIEW_EMAIL_LOG_DETAILS ),
),
),
array(
'component' => 'Button',
'props' => array(
'action' => 'preview',
'customAttributes' => array(
'title' => esc_html__( 'View email', 'gravitysmtp' ),
),
'customClasses' => array( 'gravitysmtp-data-grid__action' ),
'icon' => 'eye',
'iconPrefix' => 'gravity-admin-icon',
'spacing' => [ 0, 2, 0, 0 ],
'size' => 'size-height-s',
'type' => 'icon-white',
'data' => array(
'event_id' => $event_id,
),
'disabled' => ! current_user_can( Roles::VIEW_EMAIL_LOG_PREVIEW ),
),
),
array(
'component' => 'Button',
'props' => array(
'action' => 'delete',
'customAttributes' => array(
'title' => esc_html__( 'Delete email log', 'gravitysmtp' ),
),
'customClasses' => array( 'gravitysmtp-data-grid__action' ),
'icon' => 'trash',
'iconPrefix' => 'gravity-admin-icon',
'size' => 'size-height-s',
'type' => 'icon-white',
'data' => array(
'event_id' => $event_id,
),
'disabled' => ! current_user_can( Roles::DELETE_EMAIL_LOG ),
),
),
),
);
return apply_filters( 'gravitysmtp_debug_log_actions', $actions );
}
}
@@ -0,0 +1,309 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Logging\Endpoints;
use Gravity_Forms\Gravity_SMTP\Enums\Integration_Enum;
use Gravity_Forms\Gravity_SMTP\Enums\Status_Enum;
use Gravity_Forms\Gravity_SMTP\Models\Event_Model;
use Gravity_Forms\Gravity_SMTP\Users\Roles;
use Gravity_Forms\Gravity_SMTP\Utils\Recipient_Parser;
use Gravity_Forms\Gravity_Tools\Endpoints\Endpoint;
class Get_Paginated_Items_Endpoint extends Endpoint {
const PARAM_PER_PAGE = 'per_page';
const PARAM_REQUESTED_PAGE = 'requested_page';
const PARAM_MAX_DATE = 'max_date';
const PARAM_SEARCH_TERM = 'search_term';
const PARAM_SEARCH_TYPE = 'search_type';
const PARAM_SORT_BY = 'sort_by';
const PARAM_SORT_ORDER = 'sort_order';
const PARAM_FILTERS = 'filters';
const ACTION_NAME = 'get_paginated_items';
protected $minimum_cap = Roles::VIEW_EMAIL_LOG;
/**
* @var Event_Model
*/
protected $events;
/**
* @var Recipient_Parser
*/
protected $parser;
public function __construct( Event_Model $event_model, Recipient_Parser $parser ) {
$this->events = $event_model;
$this->parser = $parser;
}
protected function get_nonce_name() {
return self::ACTION_NAME;
}
public function handle() {
if ( ! $this->validate() ) {
wp_send_json_error( __( 'Missing required parameters.', 'gravitysmtp' ), 400 );
}
$per_page = filter_input( INPUT_POST, self::PARAM_PER_PAGE, FILTER_SANITIZE_NUMBER_INT );
$requested_page = filter_input( INPUT_POST, self::PARAM_REQUESTED_PAGE, FILTER_SANITIZE_NUMBER_INT );
$max_date = filter_input( INPUT_POST, self::PARAM_MAX_DATE );
$search_term = filter_input( INPUT_POST, self::PARAM_SEARCH_TERM );
$search_type = filter_input( INPUT_POST, self::PARAM_SEARCH_TYPE );
$sort_by = filter_input( INPUT_POST, self::PARAM_SORT_BY );
$sort_order = filter_input( INPUT_POST, self::PARAM_SORT_ORDER );
$filters = filter_input( INPUT_POST, self::PARAM_FILTERS, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
if ( ! empty( $max_date ) ) {
$max_date = htmlspecialchars( $max_date );
}
if ( ! empty( $search_term ) ) {
$search_term = htmlspecialchars( $search_term );
}
if ( ! empty( $search_type ) ) {
$search_type = htmlspecialchars( $search_type );
}
if ( ! empty( $sort_by ) ) {
$sort_by = htmlspecialchars( $sort_by );
}
if ( ! empty( $sort_order ) ) {
$sort_order = htmlspecialchars( $sort_order );
}
$requested_page = intval( $requested_page );
$offset = ( $requested_page - 1 ) * $per_page;
if ( ! $max_date ) {
$max_date = date( 'Y-m-d H:i:s', time() );
}
if ( empty( $per_page ) ) {
$per_page = 20;
}
if ( ! is_array( $filters ) || empty( $filters ) ) {
$filters = array();
}
$rows = $this->events->paginate( $requested_page, $per_page, $max_date, $search_term, $search_type, $sort_by, $sort_order, $filters );
$count = $this->events->count( $search_term, $search_type, $filters );
$data = array(
'rows' => $this->get_formatted_data_rows( $rows ),
'total' => $count,
'row_count' => count( $rows ),
);
wp_send_json_success( $data );
}
private function get_formatted_data_rows( $data ) {
$rows = array();
foreach ( $data as $row ) {
$grid_actions = $this->get_grid_actions( $row );
$extra = strpos( $row['extra'], '{' ) === 0 ? json_decode( $row['extra'], true ) : unserialize( $row['extra'] );
$to = isset( $extra['to'] ) ? $extra['to'] : '';
$to_address = $this->parser->parse( $to )->first()->email();
$more_count = max( 0, $row['email_counts'] - 1 );
$rows[] = array(
'id' => $row['id'],
'subject' => array(
'component' => 'Button',
'props' => array(
'action' => 'view',
'customClasses' => array( 'gravitysmtp-data-grid__subject' ),
'label' => $row['subject'],
'type' => 'unstyled',
'data' => array(
'event_id' => $row['id'],
),
),
),
'status' => array(
'component' => 'StatusIndicator',
'props' => array(
'label' => Status_Enum::label( $row['status'] ),
'status' => Status_Enum::indicator( $row['status'] ),
'hasDot' => false,
),
),
'to' => array(
'component' => 'Box',
'props' => array(
'customClasses' => array( 'gravitysmtp-activity-log-app__activity-log-table-recipient' ),
'display' => 'flex',
),
'components' => array(
array(
'component' => 'Box',
'props' => array(
'customClasses' => array( 'gravitysmtp-activity-log-app__activity-log-table-recipient-meta' ),
'display' => 'flex',
),
'components' => array(
array(
'component' => 'Gravatar',
'props' => array(
'circular' => true,
'customClasses' => array( 'gravitysmtp-activity-log-app__activity-log-table-recipient-gravatar' ),
'defaultImage' => 'mp',
'emailHash' => hash( 'sha256', $to_address ),
'height' => 24,
'width' => 24,
),
),
array(
'component' => $more_count > 0 ? 'Text' : null,
'props' => array(
'content' => '+' . (string) $more_count,
'customClasses' => array( 'gravitysmtp-activity-log-app__activity-log-table-recipient-more' ),
'size' => 'text-xxs',
),
),
),
),
array(
'component' => 'Text',
'props' => array(
'content' => $to_address,
'customClasses' => array( 'gravitysmtp-activity-log-app__activity-log-table-recipient-email' ),
'size' => 'text-sm',
),
),
),
),
'source' => array(
'component' => 'Text',
'props' => array(
'content' => $row['source'],
'size' => 'text-sm',
),
),
'integration' => array(
'external' => true,
'key' => $row['service'] . '_logo',
'props' => array(
'height' => 24,
'title' => Integration_Enum::svg_title( $row['service'] ),
'width' => 24,
),
),
'date' => array(
'component' => 'Text',
'props' => array(
'content' => $this->convert_dates_to_timezone( $row['date_updated'] ),
'size' => 'text-sm',
),
),
'actions' => $grid_actions,
);
}
return $rows;
}
private function convert_dates_to_timezone( $date ) {
$gmt_time = new \DateTimeZone( 'UTC' );
$local_time = new \DateTimeZone( wp_timezone_string() );
$datetime = new \DateTime( $date, $gmt_time );
$datetime->setTimezone( $local_time );
return $datetime->format( 'F d, Y \a\t h:ia' );
}
private function get_grid_actions( $row ) {
$actions = array(
'component' => 'Box',
'components' => array(
array(
'component' => 'Button',
'props' => array(
'action' => 'view',
'customAttributes' => array(
'title' => esc_html__( 'View email log', 'gravitysmtp' ),
),
'customClasses' => array( 'gravitysmtp-data-grid__action' ),
'icon' => 'information-circle',
'iconPrefix' => 'gravity-component-icon',
'spacing' => [ 0, 2, 0, 0 ],
'size' => 'size-height-s',
'type' => 'icon-white',
'data' => array(
'event_id' => $row['id'],
),
'disabled' => ! current_user_can( Roles::VIEW_EMAIL_LOG_DETAILS ),
),
),
array(
'component' => 'Button',
'props' => array(
'action' => 'preview',
'customAttributes' => array(
'title' => esc_html__( 'View email', 'gravitysmtp' ),
),
'customClasses' => array( 'gravitysmtp-data-grid__action' ),
'icon' => 'eye',
'iconPrefix' => 'gravity-admin-icon',
'spacing' => [ 0, 2, 0, 0 ],
'size' => 'size-height-s',
'type' => 'icon-white',
'data' => array(
'event_id' => $row['id'],
),
'disabled' => ! current_user_can( Roles::VIEW_EMAIL_LOG_PREVIEW ),
),
),
array(
'component' => 'Button',
'props' => array(
'action' => 'resend',
'customAttributes' => array(
'title' => esc_html__( 'Resend email', 'gravitysmtp' ),
),
'customClasses' => array( 'gravitysmtp-data-grid__action' ),
'icon' => 'paper-plane',
'iconPrefix' => 'gravity-admin-icon',
'spacing' => [ 0, 2, 0, 0 ],
'size' => 'size-height-s',
'type' => 'icon-white',
'data' => array(
'event_id' => $row['id'],
),
'disabled' => ! current_user_can( Roles::VIEW_EMAIL_LOG_PREVIEW ) || ! $row['can_resend'],
// @todo: Add resend permission?
),
),
array(
'component' => 'Button',
'props' => array(
'action' => 'delete',
'customAttributes' => array(
'title' => esc_html__( 'Delete email log', 'gravitysmtp' ),
),
'customClasses' => array( 'gravitysmtp-data-grid__action' ),
'icon' => 'trash',
'iconPrefix' => 'gravity-admin-icon',
'size' => 'size-height-s',
'type' => 'icon-white',
'data' => array(
'event_id' => $row['id'],
),
'disabled' => ! current_user_can( Roles::DELETE_EMAIL_LOG ),
),
),
),
);
return apply_filters( 'gravitysmtp_email_log_actions', $actions );
}
}
@@ -0,0 +1,60 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Logging\Endpoints;
use Gravity_Forms\Gravity_SMTP\Logging\Debug\Debug_Logger;
use Gravity_Forms\Gravity_SMTP\Users\Roles;
use Gravity_Forms\Gravity_Tools\Endpoints\Endpoint;
class Log_Item_Endpoint extends Endpoint {
const PARAM_LOG_MESSAGE = 'message';
const PARAM_PRIORITY = 'priority';
const ACTION_NAME = 'log_debug_item';
protected $minimum_cap = Roles::EDIT_EMAIL_LOG;
/**
* @var Debug_Logger
*/
protected $logger;
public function __construct( Debug_Logger $logger ) {
$this->logger = $logger;
}
protected function get_nonce_name() {
return self::ACTION_NAME;
}
public function handle() {
if ( ! $this->validate() ) {
wp_send_json_error( __( 'Missing required parameters.', 'gravitysmtp' ), 400 );
}
$message = filter_input( INPUT_POST, self::PARAM_LOG_MESSAGE );
$priority = filter_input( INPUT_POST, self::PARAM_PRIORITY );
if ( ! empty( $message ) ) {
$message = htmlspecialchars( $message );
}
$this->logger->log( $message, $priority );
wp_send_json_success( array( 'message' => __( 'Message logged', 'gravitysmtp' ) ), 200 );
}
protected function validate() {
if ( ! parent::validate() ) {
return false;
}
if ( empty( $_REQUEST[ self::PARAM_LOG_MESSAGE ] ) ) {
return false;
}
return true;
}
}
@@ -0,0 +1,94 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Logging\Endpoints;
use Gravity_Forms\Gravity_SMTP\Apps\Config\Tools_Config;
use Gravity_Forms\Gravity_SMTP\Data_Store\Data_Store_Router;
use Gravity_Forms\Gravity_SMTP\Logging\Debug\Debug_Logger;
use Gravity_Forms\Gravity_SMTP\Models\Debug_Log_Model;
use Gravity_Forms\Gravity_SMTP\Users\Roles;
use Gravity_Forms\Gravity_Tools\Endpoints\Endpoint;
class View_Log_Endpoint extends Endpoint {
const PARAM_LOG_KEY = 'key';
const OPTION_VERIFICATION_KEY = 'gravitysmtp_log_verification_key';
const ACTION_NAME = 'view_debug_log';
protected $minimum_cap = Roles::VIEW_DEBUG_LOG;
/**
* @var Data_Store_Router
*/
protected $data;
/**
* @var Debug_Logger $debug_logger
*/
protected $debug_logger;
/**
* @var Debug_Log_Model $model
*/
protected $model;
public function __construct( Data_Store_Router $data, $debug_logger, $debug_model ) {
$this->data = $data;
$this->debug_logger = $debug_logger;
$this->model = $debug_model;
}
protected function get_nonce_name() {
return self::ACTION_NAME;
}
public function handle() {
if ( ! $this->validate() ) {
echo __( 'Unauthorized', 'gravitysmtp' );
die();
}
$lines = $this->debug_logger->get_log_items();
echo '<pre style="white-space: pre-wrap; word-wrap: break-word; overflow-wrap: break-word;">';
foreach ( $lines as $index => $item ) {
printf( "%s %s - %s --> %s", str_pad( '[' . $item->id() . ']', 8, ' ' ), $item->timestamp(), strtoupper( str_pad( $item->priority(), 7, ' ' ) ), esc_html( $item->line() ) );
if ( $index < count( $lines ) - 1 ) {
echo '</pre><hr><pre style="white-space: pre-wrap; word-wrap: break-word; overflow-wrap: break-word;">';
}
}
echo '</pre>';
die();
}
protected function validate() {
// Ensure logging is enabled.
$enabled = $this->data->get_plugin_setting( Tools_Config::SETTING_DEBUG_LOG_ENABLED, false );
if ( ! $enabled ) {
return false;
}
if ( is_user_logged_in() ) {
return current_user_can( Roles::VIEW_DEBUG_LOG );
}
// Get the passed verficiation key; bail if empty.
$key = filter_input( INPUT_GET, self::PARAM_LOG_KEY );
if ( empty( $key ) ) {
return false;
}
// Get the stored verification key; if it is empty or does not match the provided key, bail.
$key = htmlspecialchars( $key );
$check = get_option( self::OPTION_VERIFICATION_KEY );
if ( empty( $check ) || $check !== $key ) {
return false;
}
return true;
}
}