JFIF $ d d 7 * Z qn )ZP 0 !1 A "#a$2Qr D8 a Ri [f \m I ykI w0 c u F c Rı?lO7 к _f˓ [C $殷WF< _W ԣ s Kc ëI zyQy /_LK ℂ ;C",p F A : /] =H ~ , l s / 9 ć: [=/#f ; ) x { ٛE Q )~ = 𘙲r*2~ a _V = ' ku m F D } KYYC )( { * g& f `툪 r y ` = ^ c J. I ]( * `wq 1dđ #̩ ͑ 0 ; H ] u 搂 @:~ ו K L N s h } O I R* 8 :2 ! lD JV o ( 3 = M ( z Ȱ+i *N A r6K n S l ) ! J J Ӂ* % ݉?|D } d5: eP0 R; {$X' x F @ .ÊB {,WJ uQ ɲRI ;9 Q E琯 6 2fT .DU J ;*c P A\IL Nj! J ۱+ O\ ͔ ] ޒ S J ȧ c%A N ol ՎprU L Zԛe r E2= X D Xg VQeӓ k y P7 U* o m Q I s ,K `) 6\G3t?pgjr m ۛجw luGt f h 9uyP 0D;Uڽ"OXl i f $ ) &|ML 0 Z rm1 [ H XP l P R0 'G=i 2N +0 e 2 ]] 9 VT P O 7 h(F * 癈 ' = Q VZD F,d ߬ ~ T X G[ `le 69C R(!S2! P < 0 x < !1AQ "Raq 02Br #S CTb ? Ζ "]mH5WR 7 k.ۛ ! } Q~ + yԏ z |@ T20 S~ K e k *zF f^2X *( @ 8 r ? C I uI|֓>^Ex LgNU Y+{.R Ѫ τV YT D I 6 2 '8Y2 7'\T P.6 d &˦ @V q i |8 -O Ε ] ʔ U= T L 8= ;6c | !qfF3 a ů& ~$ l} 'NW U s$U k^S V : U#6w + +s &r+n ڐ { @ 29gL u " T Ù M=6 (^ "7 r}=6Yݾ lC u hquymp Ǧ G jhs ǜNl ɻ} o 7 #S6 a w 4 ! OS r D 5 7 % | ?x> L | / nD 6?/8 w # [ ) L 7 +6〼T A T g ! %5 Mm Z /c -{1_Je "| ^$ 'O & ޱմ < H vNqϡQ$ < 6 &Nݛ ֨tǻN (9 n ru~'όN sMG& ,uH Kw *Ī 5@ u6 BԒ i YN * f 0 _ GhKW u lT <nj- 0{ M L~ | /a v\ E = ?1 \ɟuq Ђ Y O a K ޡP q $ H8o Q˼EV ~ k 0 h' L Pp M -M" m^ !6l nմ 쮝C U T . ! :vMǨb h ډ Vc r @~p # !1QAa q ? AL0L x LU 4 d ; x " 16ad0 7.\ߋ 0 = P l v/ " c A Ay A|2pO8"d MV #1] = \ " ˡ ] k v I9 d ۓ & q ) ǐq E ꞅ v "P )q X &Q З non]я u ג./ ; gZ$ M QР 2M ]h ni 0 P $ێ 3CɏGjώy : m c # S_ l@ '_ \m|C 2`K _ 1 cIvGc $? PCQ S+D ? K aײ k Fr b 8 ύ NEدg ĝ U 3 6 #Ik+ v { v i ( ވ ? a e) X xy/*@Q Ǽ Ծ ) $ IWO l c G; !i k{ gOŀ L ('x T rb $ w) R $ & N1E td U3Uȉ1p M"N* (DNy d96.(jQ) X 5 c QɎMy W ? Q *!R > 6=7)Xj 5`J ] e 8%t ! + ' ! 1Q 5 !1 AQaq ё #2 "0BRb ? G t^ ## .l lQ T $ v , ,m 㵜5u bV =sY+ @d { N ! dn O < .- B; _w J t 6;Q Jd. Q c%p{ 1 ,sND dF H I0 Гo X ш e黅X ۢ F :)[ F G X ƹ / w _c MeD ,ʡc c . W D tA $ j@ : ) - #u c1< @ۗ9F ) KJ- hpP] _ x [ q B lb p ʖ w q " LFGd ƶ * s +ډ_Zc " ? % t[I P 6 J ]# =ɺ V v v CGs Gh1 > )6 |e y ? L ӣ m,4 G W Ui ` ] uJVo VDG< SB 6ϏQ @ TiU l y O U 0 kfV ~ ~}S Z @ *WU Ui ## ; s /[ = ! 7}" W N ] ' (L ! ~ y5 g9T̅J kbM' +s :S +B) v @ M j e C f j E 0 Y\Qn z G 1 д~ W o{T 9?` Rm yhsy3 !HAD ] m c1 ~ 2 L S u7x T ;j$ `} 4-> L# vzŏ I L S ֭ T { r jGK C ; b pU= - ` Bs K. S F w4 M q] Z dH S 0 ) tLg
<?php
namespace WPForms\Emails;
use Exception;
use WPForms\Emails\Tasks\FetchInfoBlocksTask;
/**
* Email Summaries main class.
*
* @since 1.5.4
*/
class Summaries {
/**
* Constructor.
*
* @since 1.5.4
*/
public function __construct() {
$this->hooks();
$summaries_disabled = $this->is_disabled();
if ( $summaries_disabled && wp_next_scheduled( 'wpforms_email_summaries_cron' ) ) {
wp_clear_scheduled_hook( 'wpforms_email_summaries_cron' );
}
if ( ! $summaries_disabled && ! wp_next_scheduled( 'wpforms_email_summaries_cron' ) ) {
// Since v1.9.1 we use a single event and manually reoccur it
// because a recurring event cannot guarantee
// its firing at the same time during WP_CLI execution.
wp_schedule_single_event( $this->get_next_launch_time(), 'wpforms_email_summaries_cron' );
}
}
/**
* Get the instance of a class and store it in itself.
*
* @since 1.5.4
*/
public static function get_instance() {
static $instance;
if ( ! $instance ) {
$instance = new self();
}
return $instance;
}
/**
* Email Summaries hooks.
*
* @since 1.5.4
*/
public function hooks() {
add_filter( 'wpforms_settings_defaults', [ $this, 'disable_summaries_setting' ] );
add_action( 'wpforms_settings_updated', [ $this, 'deregister_fetch_info_blocks_task' ] );
// Leave early if Email Summaries are disabled in settings.
if ( $this->is_disabled() ) {
return;
}
add_action( 'init', [ $this, 'preview' ] );
add_action( 'wpforms_email_summaries_cron', [ $this, 'cron' ] );
add_filter( 'wpforms_tasks_get_tasks', [ $this, 'register_fetch_info_blocks_task' ] );
}
/**
* Check if Email Summaries are disabled in settings.
*
* @since 1.5.4
*
* @return bool
*/
protected function is_disabled(): bool {
/**
* Allows to modify whether Email Summaries are disabled in settings.
*
* @since 1.5.4
*
* @param bool $is_disabled True if Email Summaries are disabled in settings. False by default.
*/
return (bool) apply_filters( 'wpforms_emails_summaries_is_disabled', (bool) wpforms_setting( 'email-summaries-disable', false ) );
}
/**
* Add "Disable Email Summaries" to WPForms settings.
*
* @since 1.5.4
*
* @param array $settings WPForms settings.
*
* @return mixed
*/
public function disable_summaries_setting( $settings ) {
/** This filter is documented in wpforms/src/Emails/Summaries.php */
if ( (bool) apply_filters( 'wpforms_emails_summaries_is_disabled', false ) ) {
return $settings;
}
$url = wp_nonce_url(
add_query_arg(
[
'wpforms_email_template' => 'summary',
'wpforms_email_preview' => '1',
],
admin_url()
),
Preview::PREVIEW_NONCE_NAME
);
$desc = esc_html__( 'Disable Email Summaries weekly delivery.', 'wpforms-lite' );
if ( ! $this->is_disabled() ) {
$desc .= ' <a href="' . $url . '" target="_blank">' . esc_html__( 'View Email Summary Example', 'wpforms-lite' ) . '</a>.';
}
// Get the uninstall data setting.
$uninstall_data = $settings['misc']['uninstall-data'];
// Remove the uninstall data setting.
unset( $settings['misc']['uninstall-data'] );
// Add the email summaries setting.
$settings['misc']['email-summaries-disable'] = [
'id' => 'email-summaries-disable',
'name' => esc_html__( 'Disable Email Summaries', 'wpforms-lite' ),
'desc' => $desc,
'type' => 'toggle',
'status' => true,
];
// Add the uninstall data setting to the end.
$settings['misc']['uninstall-data'] = $uninstall_data;
return $settings;
}
/**
* Preview Email Summary.
*
* @since 1.5.4
*/
public function preview() {
// Leave early if the current request is not a preview for the summaries email template.
if ( ! $this->is_preview() ) {
return;
}
// Get form entries.
$entries = $this->get_entries();
$args = [
'body' => [
'overview' => $this->get_calculation_overview( $entries ),
'entries' => $this->format_trends_for_display( $entries ),
'has_trends' => $this->entries_has_trends( $entries ),
'notification_block' => ( new NotificationBlocks() )->get_block(),
'info_block' => ( new InfoBlocks() )->get_next(),
'icons' => $this->get_icons_url(),
],
];
$template = ( new Templates\Summary() )->set_args( $args );
/**
* Filters the summaries email template.
*
* @since 1.5.4
*
* @param Templates\Summary $template Default summaries email template.
*/
$template = apply_filters( 'wpforms_emails_summaries_template', $template );
$content = $template->get();
if ( Helpers::is_plain_text_template() ) {
$content = wpautop( $content );
}
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $content;
exit;
}
/**
* Get next cron occurrence date.
*
* @since 1.5.4
* @deprecated 1.9.1
*
* @return int
*/
protected function get_first_cron_date_gmt(): int {
_deprecated_function( __METHOD__, '1.9.1 of the WPForms plugin', __CLASS__ . '::get_next_launch_time()' );
return $this->get_next_launch_time();
}
/**
* Get next Monday 2p.m with WordPress offset.
*
* @since 1.9.1
*
* @return int
*/
protected function get_next_launch_time(): int {
$datetime = date_create( 'now', wp_timezone() );
$now_plus_week = time() + constant( 'WEEK_IN_SECONDS' );
if ( ! $datetime ) {
return $now_plus_week;
}
$hours = 14;
// If today is Monday and the current time is less than 2 p.m.,
// we can launch the cron for today.
if (
(int) $datetime->format( 'N' ) !== 1 ||
(int) $datetime->format( 'H' ) >= $hours
) {
try {
$datetime->modify( 'next monday' );
} catch ( Exception $e ) {
return $now_plus_week;
}
}
$datetime->setTime( $hours, 0 );
$timestamp = $datetime->getTimestamp();
return $timestamp > 0 ? $timestamp : $now_plus_week;
}
/**
* Add custom Email Summaries cron schedule.
*
* @since 1.5.4
* @deprecated 1.9.1
*
* @param array $schedules WP cron schedules.
*
* @return array
*/
public function add_weekly_cron_schedule( $schedules ) {
_deprecated_function( __METHOD__, '1.9.1 of the WPForms plugin' );
$schedules['wpforms_email_summaries_weekly'] = [
'interval' => $this->get_next_launch_time() - time(),
'display' => esc_html__( 'Weekly WPForms Email Summaries', 'wpforms-lite' ),
];
return $schedules;
}
/**
* Email Summaries cron callback.
*
* @since 1.5.4
*/
public function cron() {
$entries = $this->get_entries();
/**
* Allows skipping the cron early return when there are no entries.
*
* Pro features (e.g. re-engagement alerts) need the weekly email to send
* even when the site has zero entries.
*
* @since 1.10.1.1
*
* @param bool $skip_empty Whether to skip sending when entries are empty. Default true.
* @param array $entries Form entries.
*/
$skip_empty = (bool) apply_filters( 'wpforms_emails_summaries_cron_skip_empty', true, $entries );
// Email won't be sent if there are no form entries.
if ( empty( $entries ) && $skip_empty ) {
return;
}
$notification = new NotificationBlocks();
$notification_block = $notification->get_block();
$info_blocks = new InfoBlocks();
$next_block = $info_blocks->get_next();
$args = [
'body' => [
'overview' => $this->get_calculation_overview( $entries ),
'entries' => $this->format_trends_for_display( $entries ),
'has_trends' => $this->entries_has_trends( $entries ),
'notification_block' => $notification_block,
'info_block' => $next_block,
'icons' => $this->get_icons_url(),
],
];
$template = ( new Templates\Summary() )->set_args( $args );
/** This filter is documented in preview() method above. */
$template = apply_filters( 'wpforms_emails_summaries_template', $template );
$content = $template->get();
if ( ! $content ) {
return;
}
$parsed_home_url = wp_parse_url( home_url() );
$site_domain = $parsed_home_url['host'];
if ( is_multisite() && isset( $parsed_home_url['path'] ) ) {
$site_domain .= $parsed_home_url['path'];
}
$subject = sprintf(
/* translators: %s - site domain. */
esc_html__( 'Your Weekly WPForms Summary for %s', 'wpforms-lite' ),
$site_domain
);
/**
* Filters the summaries email subject.
*
* @since 1.5.4
*
* @param string $subject Default summaries email subject.
*/
$subject = apply_filters( 'wpforms_emails_summaries_cron_subject', $subject );
/**
* Filters the summaries recipient email address.
*
* @since 1.5.4
*
* @param string $option Default summaries recipient email address.
*/
$to_email = apply_filters( 'wpforms_emails_summaries_cron_to_email', get_option( 'admin_email' ) );
$sent = ( new Mailer() )
->template( $template )
->subject( $subject )
->to_email( $to_email )
->send();
if ( $sent === true ) {
$info_blocks->register_sent( $next_block );
// Cache the notification block shown to avoid showing it again in the future.
$notification->maybe_remember_shown_block( $notification_block );
}
}
/**
* Get form entries.
*
* @since 1.5.4
*
* @return array
*/
protected function get_entries(): array {
// The return value is intentionally left empty, as each email summary
// depending on the plugin edition Lite/Pro will have different implementation.
return [];
}
/**
* Get calculation overview.
*
* @since 1.8.8
*
* @param array $entries Form entries.
*
* @return array
*/
private function get_calculation_overview( $entries ): array {
// Check if the entries array is empty.
if ( empty( $entries ) ) {
return [];
}
// Get the sum of 'count' index in all entries.
$sum_current = array_sum( array_column( $entries, 'count' ) );
// Choose a specific 'form_id' to check if 'count_previous_week' index exists.
$sample_form_id = key( $entries );
// Check if 'count_previous_week' index doesn't exist and return early.
if ( ! isset( $entries[ $sample_form_id ]['count_previous_week'] ) ) {
return [];
}
// Get the sum of 'count_previous_week' index in all entries.
$sum_previous_week = array_sum( array_column( $entries, 'count_previous_week' ) );
// Check if the sum of counts from the previous week is 0.
// If so, return the sum of counts from the current week and trends as "+100%".
if ( $sum_previous_week === 0 ) {
return [
'total' => $sum_current,
'trends' => $this->format_trends_for_display( $sum_current === 0 ? 0 : 100 ),
];
}
// Calculate trends based on the sum of counts from the current week and the previous week.
$trends = round( ( $sum_current - $sum_previous_week ) / $sum_previous_week * 100 );
// Return an array with the total and trends.
return [
'total' => $sum_current,
'trends' => $this->format_trends_for_display( $trends ),
];
}
/**
* Register Action Scheduler task to fetch and cache Info Blocks.
*
* @since 1.6.4
*
* @param \WPForms\Tasks\Task[] $tasks List of task classes.
*
* @return array
*/
public static function register_fetch_info_blocks_task( $tasks ): array {
$tasks[] = FetchInfoBlocksTask::class;
return $tasks;
}
/**
* Deregister Action Scheduler task to fetch and cache Info Blocks.
*
* @since 1.6.4
*/
public function deregister_fetch_info_blocks_task() {
if ( ! $this->is_disabled() ) {
return;
}
// Deregister the task.
( new FetchInfoBlocksTask() )->cancel();
// Delete last run time record.
delete_option( FetchInfoBlocksTask::LAST_RUN );
// Remove the cache file if it exists.
$file_name = ( new InfoBlocks() )->get_cache_file_path();
if ( file_exists( $file_name ) ) {
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.unlink_unlink
@unlink( $file_name );
}
}
/**
* Check if the current request is a preview for the summaries email template.
*
* @since 1.8.8
*
* @return bool
*/
private function is_preview(): bool {
// Leave if the current user can't access.
if ( ! wpforms_current_user_can() ) {
return false;
}
// phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
// Leave early if nonce verification failed.
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), Preview::PREVIEW_NONCE_NAME ) ) {
return false;
}
// Leave early if preview is not requested.
if ( ! isset( $_GET['wpforms_email_preview'], $_GET['wpforms_email_template'] ) ) {
return false;
}
// Leave early if preview is not requested for the summaries template.
if ( $_GET['wpforms_email_template'] !== 'summary' ) {
return false;
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
return true;
}
/**
* Format entries trends for display.
*
* This function takes an array of entries and formats the 'trends' value for display.
*
* @since 1.8.8
*
* @param array|int $input Input data to format.
*
* @return array|string
*/
private function format_trends_for_display( $input ) {
// If input is a numeric value, format and return it.
if ( is_numeric( $input ) ) {
return sprintf( '%s%s%%', $input >= 0 ? '+' : '', $input );
}
// Loop through entries and format 'trends' values.
foreach ( $input as &$form ) {
// Leave early if 'trends' index doesn't exist.
if ( ! isset( $form['trends'] ) ) {
continue;
}
// Add percent sign to trends and + sign if value greater than zero.
$form['trends'] = sprintf( '%s%s%%', $form['trends'] >= 0 ? '+' : '', $form['trends'] );
}
return $input;
}
/**
* Check if trends can be displayed for the given entries.
*
* @since 1.8.8
*
* @param array $entries The entries data.
*
* @return bool
*/
private function entries_has_trends( array $entries ): bool {
// Return false if entries array is empty.
if ( empty( $entries ) ) {
return false;
}
// Check if at least one array item has the 'trends' key.
foreach ( $entries as $entry ) {
if ( isset( $entry['trends'] ) ) {
return true;
}
}
return false;
}
/**
* Get icons URL.
* Primarily used in the HTML version of the email template.
*
* @since 1.8.8
*
* @return array
*/
private function get_icons_url(): array {
$base_url = WPFORMS_PLUGIN_URL . 'assets/images/email/';
return [
'overview' => $base_url . 'icon-overview.png',
'upward' => $base_url . 'icon-upward.png',
'downward' => $base_url . 'icon-downward.png',
'notification_block' => $base_url . 'notification-block-icon.png',
'info_block' => $base_url . 'info-block-icon.png',
];
}
}