Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/wpforms-.../src/Emails
File: NotificationBlocks.php
<?php
[0] Fix | Delete
namespace WPForms\Emails;
[1] Fix | Delete
[2] Fix | Delete
use WPForms\Admin\Notifications\Notifications;
[3] Fix | Delete
[4] Fix | Delete
/**
[5] Fix | Delete
* Notification class.
[6] Fix | Delete
* This class is responsible for displaying the notification block in the email summaries.
[7] Fix | Delete
*
[8] Fix | Delete
* @since 1.8.8
[9] Fix | Delete
*/
[10] Fix | Delete
class NotificationBlocks {
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* Notifications class instance.
[14] Fix | Delete
*
[15] Fix | Delete
* @since 1.8.8
[16] Fix | Delete
*
[17] Fix | Delete
* @var Notifications
[18] Fix | Delete
*/
[19] Fix | Delete
private $notifications;
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* Class constructor.
[23] Fix | Delete
* Initializes the Notifications class instance.
[24] Fix | Delete
*
[25] Fix | Delete
* @since 1.8.8
[26] Fix | Delete
*/
[27] Fix | Delete
public function __construct() {
[28] Fix | Delete
[29] Fix | Delete
// Store the instance of the "Notifications" class.
[30] Fix | Delete
$this->notifications = wpforms()->obj( 'notifications' );
[31] Fix | Delete
}
[32] Fix | Delete
[33] Fix | Delete
/**
[34] Fix | Delete
* Retrieves the notification block from the feed, considering shown notifications and license type.
[35] Fix | Delete
*
[36] Fix | Delete
* @since 1.8.8
[37] Fix | Delete
*
[38] Fix | Delete
* @return array
[39] Fix | Delete
*/
[40] Fix | Delete
public function get_block(): array {
[41] Fix | Delete
[42] Fix | Delete
// Check if the user has access to notifications.
[43] Fix | Delete
// If the user has disabled announcements, return an empty array.
[44] Fix | Delete
if ( ! $this->notifications || ! $this->notifications->has_access() ) {
[45] Fix | Delete
return [];
[46] Fix | Delete
}
[47] Fix | Delete
[48] Fix | Delete
// Get the response array from the notifications.
[49] Fix | Delete
$notifications = $this->notifications->get_option();
[50] Fix | Delete
[51] Fix | Delete
// Check if 'feed' key is present and non-empty.
[52] Fix | Delete
if ( empty( $notifications['feed'] ) || ! is_array( $notifications['feed'] ) ) {
[53] Fix | Delete
return [];
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
// Remove items from $feed where their id index is in `shown_notifications` option value.
[57] Fix | Delete
$feed = $this->filter_feed( $notifications['feed'] );
[58] Fix | Delete
[59] Fix | Delete
// Sort the array of items using usort and the custom comparison function.
[60] Fix | Delete
$feed = $this->sort_feed( $feed );
[61] Fix | Delete
[62] Fix | Delete
// Get the very first item from the $feed.
[63] Fix | Delete
$block = reset( $feed );
[64] Fix | Delete
[65] Fix | Delete
// Check if $block is empty.
[66] Fix | Delete
if ( empty( $block ) ) {
[67] Fix | Delete
return [];
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
// Return the notification block.
[71] Fix | Delete
return $this->prepare_and_sanitize_content( $block );
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
/**
[75] Fix | Delete
* Save the shown notification block if it's not empty.
[76] Fix | Delete
*
[77] Fix | Delete
* @since 1.8.8
[78] Fix | Delete
*
[79] Fix | Delete
* @param array $notification The notification to be saved.
[80] Fix | Delete
*/
[81] Fix | Delete
public function maybe_remember_shown_block( array $notification ) {
[82] Fix | Delete
[83] Fix | Delete
// Check if the notification or its ID is empty.
[84] Fix | Delete
if ( empty( $notification ) || empty( $notification['id'] ) ) {
[85] Fix | Delete
// If the notification or its ID is empty, return early.
[86] Fix | Delete
return;
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
// Get shown notifications from options.
[90] Fix | Delete
$shown_notifications = (array) get_option( 'wpforms_email_summaries_shown_notifications', [] );
[91] Fix | Delete
[92] Fix | Delete
// Add the notification id to the $shown_notifications array.
[93] Fix | Delete
$shown_notifications[] = (int) $notification['id'];
[94] Fix | Delete
[95] Fix | Delete
// Update the shown notifications in the options.
[96] Fix | Delete
// Avoid autoloading the option, as it's not needed.
[97] Fix | Delete
update_option( 'wpforms_email_summaries_shown_notifications', $shown_notifications, false );
[98] Fix | Delete
}
[99] Fix | Delete
[100] Fix | Delete
/**
[101] Fix | Delete
* Filter the feed to remove shown notifications.
[102] Fix | Delete
*
[103] Fix | Delete
* @since 1.8.8
[104] Fix | Delete
*
[105] Fix | Delete
* @param array $feed The feed to filter.
[106] Fix | Delete
*
[107] Fix | Delete
* @return array
[108] Fix | Delete
*/
[109] Fix | Delete
private function filter_feed( array $feed ): array {
[110] Fix | Delete
[111] Fix | Delete
$shown_notifications = (array) get_option( 'wpforms_email_summaries_shown_notifications', [] );
[112] Fix | Delete
[113] Fix | Delete
return array_filter(
[114] Fix | Delete
$feed,
[115] Fix | Delete
static function ( $item ) use ( $shown_notifications ) {
[116] Fix | Delete
[117] Fix | Delete
return ! in_array( $item['id'], $shown_notifications, true );
[118] Fix | Delete
}
[119] Fix | Delete
);
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
/**
[123] Fix | Delete
* Sort the feed in descending order by start date.
[124] Fix | Delete
*
[125] Fix | Delete
* @since 1.8.8
[126] Fix | Delete
*
[127] Fix | Delete
* @param array $feed The feed to sort.
[128] Fix | Delete
*
[129] Fix | Delete
* @return array
[130] Fix | Delete
*/
[131] Fix | Delete
private function sort_feed( array $feed ): array {
[132] Fix | Delete
[133] Fix | Delete
usort(
[134] Fix | Delete
$feed,
[135] Fix | Delete
static function ( $a, $b ) {
[136] Fix | Delete
[137] Fix | Delete
return strtotime( $b['start'] ) - strtotime( $a['start'] );
[138] Fix | Delete
}
[139] Fix | Delete
);
[140] Fix | Delete
[141] Fix | Delete
return $feed;
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
/**
[145] Fix | Delete
* Prepare and sanitize content for display.
[146] Fix | Delete
*
[147] Fix | Delete
* @since 1.8.8
[148] Fix | Delete
*
[149] Fix | Delete
* @param string|array $content The content to be prepared and sanitized.
[150] Fix | Delete
*
[151] Fix | Delete
* @return string|array
[152] Fix | Delete
*/
[153] Fix | Delete
private function prepare_and_sanitize_content( $content ) {
[154] Fix | Delete
[155] Fix | Delete
// If the content is empty, return as is.
[156] Fix | Delete
if ( empty( $content ) ) {
[157] Fix | Delete
return $content;
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
// If the content is already a string, sanitize and return it.
[161] Fix | Delete
if ( is_string( $content ) ) {
[162] Fix | Delete
// Define allowed HTML tags and attributes.
[163] Fix | Delete
$content_allowed_tags = $this->notifications->get_allowed_tags();
[164] Fix | Delete
[165] Fix | Delete
// For design consistency, remove the 'p' tag from the allowed tags.
[166] Fix | Delete
unset( $content_allowed_tags['p'] );
[167] Fix | Delete
[168] Fix | Delete
// Apply wp_kses() for sanitization.
[169] Fix | Delete
return wp_kses( $content, $content_allowed_tags );
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
// If the content is an array with the 'content' index, modify and sanitize it.
[173] Fix | Delete
if ( is_array( $content ) && isset( $content['content'] ) ) {
[174] Fix | Delete
// Sanitize the content of the array.
[175] Fix | Delete
$content['content'] = $this->prepare_and_sanitize_content( $content['content'] );
[176] Fix | Delete
[177] Fix | Delete
// Return the modified array.
[178] Fix | Delete
return $content;
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
// If the content is not a string or an array with 'content' index, return the content as is.
[182] Fix | Delete
return $content;
[183] Fix | Delete
}
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function