Skip to content

Commit

Permalink
Support sending debug notifications to multiple emails
Browse files Browse the repository at this point in the history
  • Loading branch information
renatonascalves committed Oct 11, 2024
1 parent e4dcc3d commit ed59dff
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ public function __construct( $page ) {
'type' => [ 'no', 'yes' ],
],
'apple_news_admin_email' => [
'label' => __( 'Administrator Email', 'apple-news' ),
'label' => __( 'Email(s)', 'apple-news' ),
'required' => false,
'type' => 'string',
'type' => 'email',
'size' => 40,
'multiple' => true,
],
];

Expand All @@ -70,7 +71,7 @@ public function __construct( $page ) {
*/
public function get_section_info() {
return __(
'If debugging is enabled, emails will be sent to an administrator for every publish, update or delete action with a detailed API response.',
'If debugging is enabled (and valid emails are provided), emails will be sent for every publish, update or delete action with a detailed API response.',
'apple-news'
);
}
Expand Down
13 changes: 11 additions & 2 deletions admin/settings/class-admin-apple-settings-section.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class Admin_Apple_Settings_Section extends Apple_News {
'max' => [],
'step' => [],
'type' => [],
'multiple' => [],
'required' => [],
'size' => [],
'id' => [],
Expand Down Expand Up @@ -324,6 +325,14 @@ public function render_field( $args ) {
$field = '<textarea id="%s" name="%s">%s</textarea>';
} elseif ( 'number' === $type ) {
$field = '<input type="number" id="%s" name="%s" value="%s" size="%s" min="%s" max="%s" step="%s" %s>';
} elseif ( 'email' === $type ) {
$field = '<input type="email" id="%s" name="%s" value="%s" size="%s"';

if ( $this->is_multiple( $name ) ) {
$field .= ' multiple %s>';
} else {
$field .= ' %s>';
}
} else {
// If nothing else matches, it's a string.
$field = '<input type="text" id="%s" name="%s" value="%s" size="%s" %s>';
Expand Down Expand Up @@ -403,9 +412,9 @@ public function render_field( $args ) {
protected function get_type_for( $name ) {
if ( $this->hidden ) {
return 'hidden';
} else {
return empty( $this->settings[ $name ]['type'] ) ? 'string' : $this->settings[ $name ]['type'];
}

return empty( $this->settings[ $name ]['type'] ) ? 'string' : $this->settings[ $name ]['type'];
}

/**
Expand Down
16 changes: 12 additions & 4 deletions includes/apple-push-api/request/class-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,17 @@ private function parse_response( $response, $json = true, $type = 'post', $meta
&& 'yes' === $settings['apple_news_enable_debugging']
&& 'get' !== $type ) {

// Get the admin email.
$admin_email = filter_var( $settings['apple_news_admin_email'], FILTER_VALIDATE_EMAIL );
if ( empty( $admin_email ) ) {
$emails = $settings['apple_news_admin_email'] ?? '';

if ( str_contains( $emails, ',' ) ) {
$emails = array_map( 'trim', explode( ',', $emails ) );
} else {
$emails = [ $emails ];
}

$to = array_filter( $emails, 'is_email' );

if ( empty( $to ) ) {
return; // TODO Fix inconsistent return value.
}

Expand Down Expand Up @@ -217,7 +225,7 @@ private function parse_response( $response, $json = true, $type = 'post', $meta
// Send the email.
if ( ! empty( $body ) ) {
wp_mail( // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_mail_wp_mail
$admin_email,
$to,
esc_html__( 'Apple News Notification', 'apple-news' ),
$body,
$headers
Expand Down

0 comments on commit ed59dff

Please sign in to comment.