@@ -174,42 +174,83 @@ public static function get_stats( $request ) {
174
174
* @since 2.0.0
175
175
*/
176
176
public static function get_extended ( WP_REST_Request $ request ): WP_REST_Response {
177
+ // Verify scope.
177
178
$ scope = $ request ->get_param ( 'scope ' );
179
+ if ( ! in_array ( $ scope , array ( 'year ' , 'month ' , 'day ' ) ) ) {
180
+ return new WP_REST_Response (
181
+ array ( 'error ' => 'invalid scope (allowed: year, month, day) ' ),
182
+ 400
183
+ );
184
+ }
185
+
186
+ // Retrieve from cache, if data is not post-specific.
178
187
$ post = $ request ->get_param ( 'post ' );
188
+ $ stats = false ;
189
+ if ( ! $ post ) {
190
+ $ stats = self ::from_cache ( $ scope );
191
+ }
179
192
180
- $ status = 200 ;
181
- if ( 'year ' === $ scope ) {
182
- $ stats = Statify_Evaluation::get_views_for_all_years ( $ post );
183
- } elseif ( 'month ' === $ scope ) {
184
- $ visits = Statify_Evaluation::get_views_for_all_months ( $ post );
185
- $ stats = array ( 'visits ' => array () );
186
- $ last_ym = 0 ;
187
- foreach ( $ visits as $ ym => $ v ) {
188
- $ ym = explode ( '- ' , $ ym );
189
- $ year = intval ( $ ym [0 ] );
190
- $ month = intval ( $ ym [1 ] );
191
- $ year_month = $ year * 12 + $ month ;
192
- for ( $ ym = $ last_ym + 1 ; $ last_ym > 0 && $ ym < $ year_month ; $ ym ++ ) {
193
- // Fill gaps.
194
- $ y = intval ( $ ym / 12 );
195
- if ( ! isset ( $ stats ['visits ' ][ $ y ] ) ) {
196
- $ stats ['visits ' ][ $ y ] = array ();
193
+ if ( ! $ stats ) {
194
+ if ( 'year ' === $ scope ) {
195
+ $ stats = Statify_Evaluation::get_views_for_all_years ( $ post );
196
+ } elseif ( 'month ' === $ scope ) {
197
+ $ visits = Statify_Evaluation::get_views_for_all_months ( $ post );
198
+ $ stats = array ( 'visits ' => array () );
199
+ $ last_ym = 0 ;
200
+ foreach ( $ visits as $ ym => $ v ) {
201
+ $ ym = explode ( '- ' , $ ym );
202
+ $ year = intval ( $ ym [0 ] );
203
+ $ month = intval ( $ ym [1 ] );
204
+ $ year_month = $ year * 12 + $ month ;
205
+ for ( $ ym = $ last_ym + 1 ; $ last_ym > 0 && $ ym < $ year_month ; $ ym ++ ) {
206
+ // Fill gaps.
207
+ $ y = intval ( $ ym / 12 );
208
+ if ( ! isset ( $ stats ['visits ' ][ $ y ] ) ) {
209
+ $ stats ['visits ' ][ $ y ] = array ();
210
+ }
211
+ $ stats ['visits ' ][ $ y ][ $ ym % 12 ] = 0 ;
197
212
}
198
- $ stats ['visits ' ][ $ y ][ $ ym % 12 ] = 0 ;
199
- }
200
- if ( ! isset ( $ stats ['visits ' ][ $ year ] ) ) {
201
- $ stats ['visits ' ][ $ year ] = array ();
213
+ if ( ! isset ( $ stats ['visits ' ][ $ year ] ) ) {
214
+ $ stats ['visits ' ][ $ year ] = array ();
215
+ }
216
+ $ stats ['visits ' ][ $ year ][ $ month ] = $ v ;
217
+ $ last_ym = $ year_month ;
202
218
}
203
- $ stats ['visits ' ][ $ year ][ $ month ] = $ v ;
204
- $ last_ym = $ year_month ;
219
+ } elseif ( 'day ' === $ scope ) {
220
+ $ stats = Statify_Evaluation::get_views_for_all_days ( $ post );
221
+ }
222
+
223
+ // Update cache, if data is not post-specific.
224
+ if ( ! $ post ) {
225
+ self ::update_cache ( $ scope , $ stats );
205
226
}
206
- } elseif ( 'day ' === $ scope ) {
207
- $ stats = Statify_Evaluation::get_views_for_all_days ( $ post );
208
- } else {
209
- $ stats = array ( 'error ' => 'invalid scope (allowed: year, month, day) ' );
210
- $ status = 400 ;
211
227
}
212
228
213
- return new WP_REST_Response ( $ stats , $ status );
229
+ return new WP_REST_Response ( $ stats );
230
+ }
231
+
232
+ /**
233
+ * Retrieve data from cache.
234
+ *
235
+ * @param string $scope Scope (year, month, day).
236
+ *
237
+ * @return array|false Transient data or FALSE.
238
+ */
239
+ private static function from_cache ( string $ scope ) {
240
+ return get_transient ( 'statify_data_ ' . $ scope );
241
+ }
242
+
243
+ /**
244
+ * Update data cache.
245
+ *
246
+ * @param string $scope Scope (year, month, day).
247
+ * @param array $data Data.
248
+ */
249
+ private static function update_cache ( string $ scope , array $ data ): void {
250
+ set_transient (
251
+ 'statify_data_ ' . $ scope ,
252
+ $ data ,
253
+ 30 * MINUTE_IN_SECONDS
254
+ );
214
255
}
215
256
}
0 commit comments