| 1 |
<?php |
| 2 |
/** |
| 3 |
* Data-collection consent notice. |
| 4 |
* |
| 5 |
* @package ULTP\Notice |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace ULTP\Includes\Notice; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
use ULTP\Includes\Durbin\Xpo; |
| 13 |
use ULTP\Includes\Durbin\DurbinClient; |
| 14 |
|
| 15 |
/** |
| 16 |
* Asks the site owner to share diagnostic data. Shows until they accept or close it. |
| 17 |
* |
| 18 |
* Only users who can manage the site see it or answer it: the answer applies to the whole site. |
| 19 |
* Not a campaign: it has no dates and ignores the shared notice priority. |
| 20 |
*/ |
| 21 |
class Consent_Notice implements Notice_Type { |
| 22 |
|
| 23 |
/** |
| 24 |
* Notice key. Don't change it: the dismissal transient is named after it. |
| 25 |
*/ |
| 26 |
const KEY = 'ultp_durbin_dc1'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Transient prefix that marks the notice as answered. |
| 30 |
*/ |
| 31 |
const DISMISS_PREFIX = 'ultp_durbin_notice_'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Capability needed to see and answer the notice. |
| 35 |
*/ |
| 36 |
const CAPABILITY = 'manage_options'; |
| 37 |
|
| 38 |
/** |
| 39 |
* Nonce for the two links, created once per page. |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
private $nonce = ''; |
| 44 |
|
| 45 |
/** |
| 46 |
* Notice key. |
| 47 |
* |
| 48 |
* @return string |
| 49 |
*/ |
| 50 |
public function get_key() { |
| 51 |
return self::KEY; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Show to site managers until answered, and not on the page load right after clicking one of its links. |
| 56 |
* |
| 57 |
* @return bool |
| 58 |
*/ |
| 59 |
public function should_display() { |
| 60 |
return current_user_can( self::CAPABILITY ) |
| 61 |
&& ! isset( $_GET['ultp_durbin_key'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only; hides the notice on the page load after it is dismissed. |
| 62 |
&& 'off' !== Xpo::get_transient_without_cache( self::DISMISS_PREFIX . self::KEY ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Queue the notice stylesheet. |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function enqueue_assets() { |
| 71 |
Notice_Assets::enqueue_stylesheet(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Print the notice. The template views/consent.php reads this object as `$notice`. |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function render() { |
| 80 |
$view = dirname( __DIR__ ) . '/views/consent.php'; |
| 81 |
if ( ! is_readable( $view ) ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
$this->enqueue_assets(); |
| 85 |
$notice = $this; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Used by the template. |
| 86 |
include $view; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Link that closes the notice without sharing data. |
| 91 |
* |
| 92 |
* @return string Unescaped URL. |
| 93 |
*/ |
| 94 |
public function get_close_url() { |
| 95 |
return add_query_arg( |
| 96 |
array( |
| 97 |
'ultp_durbin_key' => self::KEY, |
| 98 |
'wpnonce' => $this->get_nonce(), |
| 99 |
) |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Link that accepts data sharing and closes the notice. |
| 105 |
* |
| 106 |
* @return string Unescaped URL. |
| 107 |
*/ |
| 108 |
public function get_accept_url() { |
| 109 |
return add_query_arg( |
| 110 |
array( |
| 111 |
'ultp_durbin_key' => self::KEY, |
| 112 |
'ultp_get_durbin' => 'get', |
| 113 |
'wpnonce' => $this->get_nonce(), |
| 114 |
) |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Act on a click of either link. Notice::set_dismiss_notice_callback() checks the nonce first. |
| 120 |
* |
| 121 |
* @return void |
| 122 |
*/ |
| 123 |
public static function handle_dismiss_request() { |
| 124 |
$durbin_key = sanitize_text_field( wp_unslash( $_GET['ultp_durbin_key'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verified by the caller. |
| 125 |
if ( empty( $durbin_key ) || ! current_user_can( self::CAPABILITY ) ) { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
Xpo::set_transient_without_cache( self::DISMISS_PREFIX . $durbin_key, 'off' ); |
| 130 |
|
| 131 |
if ( 'get' === sanitize_text_field( wp_unslash( $_GET['ultp_get_durbin'] ?? '' ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verified by the caller. |
| 132 |
DurbinClient::send( DurbinClient::ACTIVATE_ACTION ); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Nonce for the two links, so both carry the same value. |
| 138 |
* |
| 139 |
* @return string |
| 140 |
*/ |
| 141 |
private function get_nonce() { |
| 142 |
if ( '' === $this->nonce ) { |
| 143 |
$this->nonce = wp_create_nonce( 'ultp-nonce' ); |
| 144 |
} |
| 145 |
return $this->nonce; |
| 146 |
} |
| 147 |
} |
| 148 |
|