Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../public_h.../clone/wp-conte.../plugins/wpforms-.../src/Lite/Emails
File: Summaries.php
<?php
[0] Fix | Delete
namespace WPForms\Lite\Emails;
[1] Fix | Delete
[2] Fix | Delete
use WPForms\Lite\Reports\EntriesCount;
[3] Fix | Delete
use WPForms\Emails\Summaries as BaseSummaries;
[4] Fix | Delete
[5] Fix | Delete
/**
[6] Fix | Delete
* Email Summaries.
[7] Fix | Delete
*
[8] Fix | Delete
* @since 1.8.8
[9] Fix | Delete
*/
[10] Fix | Delete
class Summaries extends BaseSummaries {
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* Whether counting entries is allowed for Lite users.
[14] Fix | Delete
*
[15] Fix | Delete
* @since 1.8.8
[16] Fix | Delete
*
[17] Fix | Delete
* @var bool
[18] Fix | Delete
*/
[19] Fix | Delete
private $allow_entries_count_lite;
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* Constructor for the class.
[23] Fix | Delete
* Initializes the object and registers the Lite weekly entries count cron schedule.
[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
// phpcs:disable WPForms.PHP.ValidateHooks.InvalidHookName
[30] Fix | Delete
[31] Fix | Delete
// Disabling this filter will prevent entries submission count from being updated.
[32] Fix | Delete
/** This filter is documented in /lite/wpforms-lite.php */
[33] Fix | Delete
$this->allow_entries_count_lite = apply_filters( 'wpforms_dash_widget_allow_entries_count_lite', true );
[34] Fix | Delete
[35] Fix | Delete
// phpcs:enable WPForms.PHP.ValidateHooks.InvalidHookName
[36] Fix | Delete
[37] Fix | Delete
parent::__construct();
[38] Fix | Delete
[39] Fix | Delete
// Register the Lite weekly entries count cron schedule.
[40] Fix | Delete
$this->register_entries_count_schedule();
[41] Fix | Delete
}
[42] Fix | Delete
[43] Fix | Delete
/**
[44] Fix | Delete
* Hooks.
[45] Fix | Delete
*
[46] Fix | Delete
* @since 1.8.8
[47] Fix | Delete
*/
[48] Fix | Delete
public function hooks() {
[49] Fix | Delete
[50] Fix | Delete
parent::hooks();
[51] Fix | Delete
[52] Fix | Delete
// The following schedule is essential for the Lite version.
[53] Fix | Delete
// Regardless of the "Weekly Summaries" feature being disabled or enabled,
[54] Fix | Delete
// it ensures that entries numbers are consistently updated.
[55] Fix | Delete
if ( ! $this->allow_entries_count_lite ) {
[56] Fix | Delete
return;
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
add_filter( 'cron_schedules', [ $this, 'weekly_entries_count' ] ); // phpcs:ignore
[60] Fix | Delete
add_action( 'wpforms_weekly_entries_count_cron', [ $this, 'entries_count_cron' ] );
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
/**
[64] Fix | Delete
* Adjusts the Lite weekly entries count cron schedule.
[65] Fix | Delete
*
[66] Fix | Delete
* This function modifies the Lite weekly entries count cron schedule by reducing the interval by 5 seconds.
[67] Fix | Delete
*
[68] Fix | Delete
* @since 1.8.8
[69] Fix | Delete
*
[70] Fix | Delete
* @param array $schedules WP cron schedules.
[71] Fix | Delete
*
[72] Fix | Delete
* @return array
[73] Fix | Delete
*/
[74] Fix | Delete
public function weekly_entries_count( $schedules ) {
[75] Fix | Delete
[76] Fix | Delete
$schedules['wpforms_weekly_entries_count'] = [
[77] Fix | Delete
'interval' => $this->get_weekly_entries_count_interval() - time(),
[78] Fix | Delete
'display' => esc_html__( 'Calculate WPForms Lite Weekly Entries Count', 'wpforms-lite' ),
[79] Fix | Delete
];
[80] Fix | Delete
[81] Fix | Delete
return $schedules;
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
/**
[85] Fix | Delete
* Run the cron job to update entries count for Lite users.
[86] Fix | Delete
*
[87] Fix | Delete
* This function retrieves the current entries count for Lite users, calculates the count for the
[88] Fix | Delete
* previous week, and updates the necessary post meta data for trend calculations.
[89] Fix | Delete
*
[90] Fix | Delete
* @since 1.8.8
[91] Fix | Delete
*/
[92] Fix | Delete
public function entries_count_cron() {
[93] Fix | Delete
[94] Fix | Delete
// Get entries count for Lite users.
[95] Fix | Delete
$entries = ( new EntriesCount() )->get_by_form();
[96] Fix | Delete
[97] Fix | Delete
// Exit if there are no form entries to update.
[98] Fix | Delete
if ( empty( $entries ) ) {
[99] Fix | Delete
return;
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
foreach ( $entries as $form_id => &$form ) {
[103] Fix | Delete
// Set total entries count to the current count.
[104] Fix | Delete
$form['total'] = $form['count'];
[105] Fix | Delete
[106] Fix | Delete
// Retrieve the previous week's count data from post meta.
[107] Fix | Delete
$previous_week_count = get_post_meta( $form_id, 'wpforms_entries_count_previous_week', true );
[108] Fix | Delete
[109] Fix | Delete
// Continue to the next form if the count data is not valid.
[110] Fix | Delete
if ( ! is_array( $previous_week_count ) || count( $previous_week_count ) !== 3 ) {
[111] Fix | Delete
$prev_count_previous_week = $form['total'];
[112] Fix | Delete
[113] Fix | Delete
// Set the previous week's count zero "0" if the form was published less than or equal to 7 days ago.
[114] Fix | Delete
if ( $this->is_form_created_in_7days( $form_id ) ) {
[115] Fix | Delete
$prev_count_previous_week = 0;
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
update_post_meta( $form_id, 'wpforms_entries_count_previous_week', [ $form['total'], $form['total'], $prev_count_previous_week ] );
[119] Fix | Delete
[120] Fix | Delete
continue;
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
list( $total_previous_week, $count_previous_week ) = $previous_week_count;
[124] Fix | Delete
[125] Fix | Delete
// Calculate count, count_previous_week, and trends.
[126] Fix | Delete
$form['count'] = $form['total'] - $total_previous_week;
[127] Fix | Delete
[128] Fix | Delete
if ( count( array_unique( $previous_week_count ) ) === 1 ) {
[129] Fix | Delete
// If the previous week's count is the same as the current count, skip trends calculation.
[130] Fix | Delete
update_post_meta( $form_id, 'wpforms_entries_count_previous_week_skip_trends', true );
[131] Fix | Delete
} else {
[132] Fix | Delete
// If the previous week's count is different from the current count, calculate trends.
[133] Fix | Delete
delete_post_meta( $form_id, 'wpforms_entries_count_previous_week_skip_trends' );
[134] Fix | Delete
}
[135] Fix | Delete
[136] Fix | Delete
// Update post meta data for trend calculations.
[137] Fix | Delete
update_post_meta( $form_id, 'wpforms_entries_count_previous_week', [ $form['total'], $form['count'], $count_previous_week ] );
[138] Fix | Delete
}
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
/**
[142] Fix | Delete
* Get form entries.
[143] Fix | Delete
*
[144] Fix | Delete
* @since 1.8.8
[145] Fix | Delete
*
[146] Fix | Delete
* @return array
[147] Fix | Delete
*/
[148] Fix | Delete
protected function get_entries(): array {
[149] Fix | Delete
[150] Fix | Delete
return ( new EntriesCount() )->get_form_trends();
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
/**
[154] Fix | Delete
* Register entries count schedule.
[155] Fix | Delete
*
[156] Fix | Delete
* @since 1.8.8
[157] Fix | Delete
*/
[158] Fix | Delete
private function register_entries_count_schedule() {
[159] Fix | Delete
[160] Fix | Delete
// Leave early if WPForms Pro is active, and clear the schedule if it exists.
[161] Fix | Delete
if ( ! $this->allow_entries_count_lite && wp_next_scheduled( 'wpforms_weekly_entries_count_cron' ) ) {
[162] Fix | Delete
wp_clear_scheduled_hook( 'wpforms_weekly_entries_count_cron' );
[163] Fix | Delete
[164] Fix | Delete
return;
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
// Register the schedule if it doesn't exist.
[168] Fix | Delete
if ( $this->allow_entries_count_lite && ! wp_next_scheduled( 'wpforms_weekly_entries_count_cron' ) ) {
[169] Fix | Delete
wp_schedule_event( $this->get_weekly_entries_count_interval(), 'wpforms_weekly_entries_count', 'wpforms_weekly_entries_count_cron' );
[170] Fix | Delete
}
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
/**
[174] Fix | Delete
* Get the interval for the Lite weekly entries count cron schedule.
[175] Fix | Delete
*
[176] Fix | Delete
* This function calculates the interval for the Lite weekly entries count cron schedule.
[177] Fix | Delete
* It goes back 14 hours from the timestamp of the next Monday at 2pm.
[178] Fix | Delete
*
[179] Fix | Delete
* @since 1.8.8
[180] Fix | Delete
*
[181] Fix | Delete
* @return int
[182] Fix | Delete
*/
[183] Fix | Delete
private function get_weekly_entries_count_interval(): int {
[184] Fix | Delete
[185] Fix | Delete
$interval = strtotime( 'next monday 2pm' ) - 14 * HOUR_IN_SECONDS;
[186] Fix | Delete
[187] Fix | Delete
return absint( $interval - ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
/**
[191] Fix | Delete
* Check if the given form_id was published less than or equal to 7 days ago.
[192] Fix | Delete
*
[193] Fix | Delete
* @since 1.8.8
[194] Fix | Delete
*
[195] Fix | Delete
* @param int $form_id The ID of the form (post).
[196] Fix | Delete
*
[197] Fix | Delete
* @return bool
[198] Fix | Delete
*/
[199] Fix | Delete
private function is_form_created_in_7days( int $form_id ): bool {
[200] Fix | Delete
[201] Fix | Delete
// Get the form (post) publish date.
[202] Fix | Delete
$date_created = get_post_field( 'post_date', $form_id, 'raw' );
[203] Fix | Delete
[204] Fix | Delete
// If the form date is not available, return false.
[205] Fix | Delete
if ( empty( $date_created ) ) {
[206] Fix | Delete
return false;
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
// Calculate the time difference between the post date and the current date.
[210] Fix | Delete
$time_difference = time() - strtotime( $date_created );
[211] Fix | Delete
[212] Fix | Delete
// Compare the time difference with 7 days in seconds.
[213] Fix | Delete
return $time_difference <= 7 * DAY_IN_SECONDS;
[214] Fix | Delete
}
[215] Fix | Delete
}
[216] Fix | Delete
[217] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function