| 1 |
<?php |
| 2 |
/** |
| 3 |
* Aggregate numbers about opt-ins, without personal data. |
| 4 |
* |
| 5 |
* @package Forge12\DoubleOptIn\Repository |
| 6 |
* @since 5.8.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare( strict_types=1 ); |
| 10 |
|
| 11 |
namespace Forge12\DoubleOptIn\Repository; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
class OptInStatsRepository { |
| 18 |
|
| 19 |
/** @var \wpdb|object */ |
| 20 |
private $wpdb; |
| 21 |
|
| 22 |
/** @var string */ |
| 23 |
private $table; |
| 24 |
|
| 25 |
/** |
| 26 |
* @param \wpdb|object $wpdb WordPress database. |
| 27 |
*/ |
| 28 |
public function __construct( $wpdb ) { |
| 29 |
$this->wpdb = $wpdb; |
| 30 |
$this->table = $wpdb->prefix . 'f12_cf7_doubleoptin'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Opt-ins created in [$from, $to) and how many of them are unconfirmed. |
| 35 |
* |
| 36 |
* @return array{total: int, unconfirmed: int} |
| 37 |
*/ |
| 38 |
public function confirmationWindow( int $from, int $to ): array { |
| 39 |
$since = gmdate( 'Y-m-d H:i:s', $from ); |
| 40 |
$until = gmdate( 'Y-m-d H:i:s', $to ); |
| 41 |
|
| 42 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- WPCS does not recognise $this->wpdb->prepare(); the table name comes from $wpdb->prefix. |
| 43 |
$row = $this->wpdb->get_row( |
| 44 |
$this->wpdb->prepare( |
| 45 |
"SELECT COUNT(*) AS total, SUM(CASE WHEN doubleoptin = 1 THEN 0 ELSE 1 END) AS unconfirmed FROM {$this->table} WHERE createtime >= %s AND createtime < %s", |
| 46 |
$since, |
| 47 |
$until |
| 48 |
), |
| 49 |
ARRAY_A |
| 50 |
); |
| 51 |
// phpcs:enable |
| 52 |
$row = is_array( $row ) ? $row : array(); |
| 53 |
|
| 54 |
return array( |
| 55 |
'total' => (int) ( $row['total'] ?? 0 ), |
| 56 |
'unconfirmed' => (int) ( $row['unconfirmed'] ?? 0 ), |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* What happened in [$from, $to): opt-ins created, how many of those |
| 62 |
* were confirmed (also when withdrawn later — a confirmation leaves its |
| 63 |
* IP, an opt-out only resets the flag), and consents withdrawn in that |
| 64 |
* time. |
| 65 |
* |
| 66 |
* @since 5.8.0 |
| 67 |
* |
| 68 |
* @return array{created: int, confirmed: int, opted_out: int} |
| 69 |
*/ |
| 70 |
public function activity( int $from, int $to ): array { |
| 71 |
$since = gmdate( 'Y-m-d H:i:s', $from ); |
| 72 |
$until = gmdate( 'Y-m-d H:i:s', $to ); |
| 73 |
|
| 74 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- WPCS does not recognise $this->wpdb->prepare(); the table name comes from $wpdb->prefix. |
| 75 |
$row = $this->wpdb->get_row( |
| 76 |
$this->wpdb->prepare( |
| 77 |
"SELECT |
| 78 |
SUM(CASE WHEN createtime >= %s AND createtime < %s THEN 1 ELSE 0 END) AS created, |
| 79 |
SUM(CASE WHEN createtime >= %s AND createtime < %s AND (doubleoptin = 1 OR ipaddr_confirmation <> '') THEN 1 ELSE 0 END) AS confirmed, |
| 80 |
SUM(CASE WHEN optouttime >= %s AND optouttime < %s THEN 1 ELSE 0 END) AS opted_out |
| 81 |
FROM {$this->table} |
| 82 |
WHERE (createtime >= %s AND createtime < %s) OR (optouttime >= %s AND optouttime < %s)", |
| 83 |
$since, |
| 84 |
$until, |
| 85 |
$since, |
| 86 |
$until, |
| 87 |
$since, |
| 88 |
$until, |
| 89 |
$since, |
| 90 |
$until, |
| 91 |
$since, |
| 92 |
$until |
| 93 |
), |
| 94 |
ARRAY_A |
| 95 |
); |
| 96 |
// phpcs:enable |
| 97 |
$row = is_array( $row ) ? $row : array(); |
| 98 |
|
| 99 |
return array( |
| 100 |
'created' => (int) ( $row['created'] ?? 0 ), |
| 101 |
'confirmed' => (int) ( $row['confirmed'] ?? 0 ), |
| 102 |
'opted_out' => (int) ( $row['opted_out'] ?? 0 ), |
| 103 |
); |
| 104 |
} |
| 105 |
} |
| 106 |
|