PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.14
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.14
1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 1.3.45 All 177 releases
disco / app / Analytics / Services / BaseService.php

BaseService.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.4.14, at app/Analytics/Services/BaseService.php

94 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php //phpcs:ignore
2
3 /**
4 * BaseService — shared helpers for analytics service classes.
5 *
6 * @package Disco
7 * @subpackage Disco\App\Analytics\Services
8 * @since 1.3.37
9 */
10
11 namespace Disco\App\Analytics\Services;
12
13 /**
14 * Shared, side-effect-free helpers used by every analytics service.
15 *
16 * Holds period math and raw-string parsing only — no SQL, no WP queries.
17 */
18 abstract class BaseService {
19
20 /**
21 * Returns the comparison period (same duration, immediately before the current period).
22 *
23 * @param string $date_from Y-m-d start of current period.
24 * @param string $date_to Y-m-d end of current period.
25 * @return array { from: string, to: string }
26 */
27 protected static function resolve_compare_period( string $date_from, string $date_to ): array {
28 if ( ! $date_from || ! $date_to ) {
29 return array( 'from' => '', 'to' => '' );
30 }
31
32 $ts_from = strtotime( $date_from );
33 $ts_to = strtotime( $date_to );
34
35 if ( ! $ts_from || ! $ts_to ) {
36 return array( 'from' => '', 'to' => '' );
37 }
38
39 $duration = (int) round( ( $ts_to - $ts_from ) / DAY_IN_SECONDS );
40 $compare_to = gmdate( 'Y-m-d', $ts_from - DAY_IN_SECONDS );
41 $compare_from = gmdate( 'Y-m-d', strtotime( $compare_to ) - $duration * DAY_IN_SECONDS );
42
43 return array( 'from' => $compare_from, 'to' => $compare_to );
44 }
45
46 /**
47 * Parses an "id:name||id2:name2" raw string into [{ id, name }] pairs.
48 *
49 * Used for the categories_raw / campaigns_raw columns produced by the
50 * query layer. Entries without a ":" separator are skipped.
51 *
52 * @param string $raw Raw concatenated string from the query layer.
53 * @return array<int, array{id: int, name: string}>
54 */
55 protected static function parse_id_name_pairs( string $raw ): array {
56 if ( '' === $raw ) {
57 return array();
58 }
59
60 $pairs = array();
61
62 foreach ( explode( '||', $raw ) as $entry ) {
63 $parts = explode( ':', $entry, 2 );
64
65 if ( count( $parts ) < 2 ) {
66 continue;
67 }
68
69 [ $id_str, $name ] = $parts;
70 $pairs[] = array( 'id' => (int) $id_str, 'name' => $name );
71 }
72
73 return $pairs;
74 }
75
76 /**
77 * Builds the standard pagination block for table responses.
78 *
79 * @param array $result Query result containing total + pages.
80 * @param int $page Current page number.
81 * @param int $limit Per-page limit.
82 * @return array { total, pages, page, limit }
83 */
84 protected static function build_pagination( array $result, int $page, int $limit ): array {
85 return array(
86 'total' => $result['total'],
87 'pages' => $result['pages'],
88 'page' => $page,
89 'limit' => $limit,
90 );
91 }
92
93 }
94