PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / trunk
Post Views Counter vtrunk
1.7.13 1.7.12 1.7.11 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.2 1.3.2.1 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9
post-views-counter / includes / class-emails-period.php
post-views-counter / includes Last commit date
class-admin.php 4 months ago class-columns-modal.php 1 month ago class-columns.php 3 weeks ago class-counter.php 2 months ago class-crawler-detect.php 7 months ago class-cron.php 1 month ago class-dashboard.php 1 month ago class-emails-mailer.php 1 month ago class-emails-period.php 1 month ago class-emails-query.php 1 month ago class-emails-scheduler.php 1 month ago class-emails-template.php 1 month ago class-emails.php 1 month ago class-frontend.php 3 weeks ago class-functions.php 1 year ago class-import.php 2 months ago class-integration-gutenberg.php 6 months ago class-integrations.php 2 months ago class-query.php 2 months ago class-settings-api.php 1 month ago class-settings-display.php 4 months ago class-settings-emails.php 1 month ago class-settings-general.php 1 month ago class-settings-integrations.php 5 months ago class-settings-other.php 1 month ago class-settings-reports.php 1 month ago class-settings.php 1 month ago class-toolbar.php 4 months ago class-traffic-signals.php 2 months ago class-update.php 1 month ago class-widgets.php 1 month ago functions.php 1 month ago
class-emails-period.php
202 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * Post_Views_Counter_Emails_Period class.
8 *
9 * @class Post_Views_Counter_Emails_Period
10 */
11 class Post_Views_Counter_Emails_Period {
12
13 /**
14 * @var Post_Views_Counter
15 */
16 private $pvc;
17
18 /**
19 * Class constructor.
20 *
21 * @return void
22 */
23 public function __construct() {
24 $this->pvc = Post_Views_Counter();
25 }
26
27 /**
28 * Get a summary period.
29 *
30 * @param string $summary_type
31 * @param mixed $now
32 * @return array
33 */
34 public function get_period( $summary_type = 'weekly', $now = null ) {
35 $summary_type = self::normalize_summary_type_key( $summary_type );
36 $period = $this->build_weekly_period( $now );
37
38 if ( $summary_type === 'weekly' )
39 return apply_filters( 'pvc_email_summary_period', $period, 'weekly', $now, $this );
40
41 $filtered_period = apply_filters( 'pvc_email_summary_period', $period, $summary_type, $now, $this );
42
43 return is_array( $filtered_period ) ? array_merge( $period, $filtered_period ) : $period;
44 }
45
46 /**
47 * Get the previous complete weekly period.
48 *
49 * @param mixed $now
50 * @return array
51 */
52 public function get_weekly_period( $now = null ) {
53 $period = $this->build_weekly_period( $now );
54
55 return apply_filters( 'pvc_email_summary_period', $period, 'weekly', $now, $this );
56 }
57
58 /**
59 * Build the previous complete weekly period.
60 *
61 * @param mixed $now
62 * @return array
63 */
64 private function build_weekly_period( $now = null ) {
65 $time_basis = $this->get_time_basis();
66 $timezone = $this->get_timezone();
67 $reference = $this->normalize_datetime( $now, $timezone )->setTime( 12, 0, 0 );
68
69 // Free MVP intentionally uses previous complete ISO Monday-Sunday weeks, not start_of_week.
70 $current_week_start = $reference->modify( 'monday this week' )->setTime( 0, 0, 0 );
71 $period_start = $current_week_start->modify( '-7 days' );
72 $period_end = $period_start->modify( '+6 days' );
73 $comparison_start = $period_start->modify( '-7 days' );
74 $comparison_end = $period_start->modify( '-1 day' );
75
76 $period = [
77 'cadence' => 'weekly',
78 'time_basis' => $time_basis,
79 'timezone' => $timezone->getName(),
80 'start_date' => $period_start->format( 'Y-m-d' ),
81 'end_date' => $period_end->format( 'Y-m-d' ),
82 'start_period' => $period_start->format( 'Ymd' ),
83 'end_period' => $period_end->format( 'Ymd' ),
84 'label' => $this->build_period_label( $period_start, $period_end, $timezone ),
85 'comparison_start_date' => $comparison_start->format( 'Y-m-d' ),
86 'comparison_end_date' => $comparison_end->format( 'Y-m-d' ),
87 'comparison_start_period' => $comparison_start->format( 'Ymd' ),
88 'comparison_end_period' => $comparison_end->format( 'Ymd' ),
89 'week_period' => $period_start->format( 'oW' ),
90 'comparison_week_period' => $comparison_start->format( 'oW' )
91 ];
92
93 return $period;
94 }
95
96 /**
97 * Get the supported summary cadence keys.
98 *
99 * @return array
100 */
101 public static function get_supported_summary_types() {
102 $supported = apply_filters( 'pvc_email_summary_supported_types', [ 'weekly' ] );
103
104 if ( ! is_array( $supported ) )
105 $supported = [ 'weekly' ];
106
107 $normalized = [];
108
109 foreach ( $supported as $summary_type ) {
110 $summary_type = sanitize_key( (string) $summary_type );
111
112 if ( $summary_type !== '' && ! in_array( $summary_type, $normalized, true ) )
113 $normalized[] = $summary_type;
114 }
115
116 if ( ! in_array( 'weekly', $normalized, true ) )
117 array_unshift( $normalized, 'weekly' );
118
119 return $normalized;
120 }
121
122 /**
123 * Normalize a summary cadence key.
124 *
125 * @param string $summary_type
126 * @return string
127 */
128 public static function normalize_summary_type_key( $summary_type ) {
129 $summary_type = sanitize_key( (string) $summary_type );
130
131 if ( in_array( $summary_type, self::get_supported_summary_types(), true ) )
132 return $summary_type;
133
134 return 'weekly';
135 }
136
137 /**
138 * Get the current count time basis.
139 *
140 * @return string
141 */
142 private function get_time_basis() {
143 return $this->pvc->options['general']['count_time'] === 'gmt' ? 'gmt' : 'local';
144 }
145
146 /**
147 * Get the timezone used for period calculations.
148 *
149 * @return DateTimeZone
150 */
151 private function get_timezone() {
152 return $this->get_time_basis() === 'gmt' ? new DateTimeZone( 'UTC' ) : wp_timezone();
153 }
154
155 /**
156 * Normalize a date input into the selected timezone.
157 *
158 * @param mixed $now
159 * @param DateTimeZone $timezone
160 * @return DateTimeImmutable
161 */
162 private function normalize_datetime( $now, $timezone ) {
163 if ( $now instanceof DateTimeInterface )
164 return ( new DateTimeImmutable( '@' . $now->getTimestamp() ) )->setTimezone( $timezone );
165
166 if ( is_numeric( $now ) )
167 return ( new DateTimeImmutable( '@' . (int) $now ) )->setTimezone( $timezone );
168
169 if ( is_string( $now ) && $now !== '' ) {
170 try {
171 return ( new DateTimeImmutable( $now, $timezone ) )->setTimezone( $timezone );
172 } catch ( Exception $e ) {
173 return new DateTimeImmutable( 'now', $timezone );
174 }
175 }
176
177 return new DateTimeImmutable( 'now', $timezone );
178 }
179
180 /**
181 * Build a human readable period label.
182 *
183 * @param DateTimeImmutable $period_start
184 * @param DateTimeImmutable $period_end
185 * @param DateTimeZone $timezone
186 * @return string
187 */
188 private function build_period_label( $period_start, $period_end, $timezone ) {
189 $start_timestamp = $period_start->getTimestamp();
190 $end_timestamp = $period_end->getTimestamp();
191
192 if ( $period_start->format( 'Y-m-d' ) === $period_end->format( 'Y-m-d' ) )
193 return wp_date( 'M j, Y', $start_timestamp, $timezone );
194
195 if ( $period_start->format( 'Y' ) === $period_end->format( 'Y' ) )
196 return wp_date( 'M j', $start_timestamp, $timezone ) . ' - ' . wp_date( 'M j, Y', $end_timestamp, $timezone );
197
198 return wp_date( 'M j, Y', $start_timestamp, $timezone ) . ' - ' . wp_date( 'M j, Y', $end_timestamp, $timezone );
199 }
200
201 }
202