PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.0
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.0
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / classes / Tracking / Notices.php

Notices.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.0, at classes/Tracking/Notices.php

207 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare(strict_types=1);
3
4 namespace Imagify\Tracking;
5
6 use Imagify\Dependencies\WPMedia\Mixpanel\Optin;
7 use Imagify\EventManagement\SubscriberInterface;
8
9 /**
10 * Handles the Imagify Analytics opt-in UI and AJAX toggle.
11 *
12 * @since 2.3.0
13 */
14 class Notices implements SubscriberInterface {
15
16 const THANKYOU_TRANSIENT = 'imagify_analytics_optin_thanks';
17
18 const NOTICE_DISPLAYED_OPTION = 'imagify_analytics_notice_displayed';
19
20 /**
21 * The Mixpanel opt-in service.
22 *
23 * @var Optin
24 */
25 private $optin;
26
27 /**
28 * Constructor.
29 *
30 * @param Optin $optin The Mixpanel opt-in service.
31 */
32 public function __construct( Optin $optin ) {
33 $this->optin = $optin;
34 }
35
36 /**
37 * Returns the list of events this subscriber wants to listen to.
38 *
39 * @return array<string, string|array<int, array<int, int|string>>>
40 */
41 public static function get_subscribed_events(): array {
42 return [
43 // @action imagify_settings_after_lossless
44 'imagify_settings_after_lossless' => 'render_optin_section',
45 // @action wp_ajax_imagify_toggle_tracking_optin
46 'wp_ajax_imagify_toggle_tracking_optin' => 'ajax_toggle_optin',
47 // @action admin_notices
48 'admin_notices' => [
49 [ 'render_optin_notice', 9 ],
50 [ 'render_thankyou_notice', 10 ],
51 ],
52 // @action admin_post_imagify_analytics_optin
53 'admin_post_imagify_analytics_optin' => 'handle_optin_action',
54 ];
55 }
56
57 /**
58 * Render the opt-in section on the settings page.
59 *
60 * @return void
61 */
62 public function render_optin_section(): void {
63 $data = $this->collect_data();
64 $data['is_enabled'] = $this->optin->is_enabled();
65 // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
66 extract( $data, EXTR_SKIP );
67
68 include IMAGIFY_PATH . 'views/part-settings-analytics.php';
69 }
70
71 /**
72 * Handle the AJAX toggle for the analytics opt-in.
73 *
74 * @return void
75 */
76 public function ajax_toggle_optin(): void {
77 check_ajax_referer( 'imagify_tracking_optin', 'nonce' );
78
79 if ( ! current_user_can( 'manage_options' ) ) {
80 wp_send_json_error( __( 'Unauthorized.', 'imagify' ), 403 );
81 }
82
83 $value = isset( $_POST['value'] ) ? (int) $_POST['value'] : 0;
84
85 if ( 1 === $value ) {
86 $this->optin->enable();
87 set_transient( self::THANKYOU_TRANSIENT, 1, 60 );
88 } else {
89 $this->optin->disable();
90 }
91
92 wp_send_json_success();
93 }
94
95 /**
96 * Render the analytics opt-in ask banner.
97 *
98 * Shown once on the Imagify settings screen until the user answers Yes or No.
99 *
100 * @return void
101 */
102 public function render_optin_notice(): void {
103 if ( ! current_user_can( 'manage_options' ) ) {
104 return;
105 }
106
107 if ( ! imagify_is_screen( 'imagify-settings' ) ) {
108 return;
109 }
110
111 if ( get_option( self::NOTICE_DISPLAYED_OPTION ) ) {
112 return;
113 }
114
115 if ( $this->optin->is_enabled() ) {
116 return;
117 }
118
119 // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
120 extract( $this->collect_data(), EXTR_SKIP );
121
122 include IMAGIFY_PATH . 'views/notice-analytics-optin.php';
123 }
124
125 /**
126 * Handle the accept/decline action from the opt-in ask banner.
127 *
128 * @return void
129 */
130 public function handle_optin_action(): void {
131 check_admin_referer( 'imagify_analytics_optin' );
132
133 if ( ! current_user_can( 'manage_options' ) ) {
134 wp_safe_redirect( wp_get_referer() );
135 exit;
136 }
137
138 if ( isset( $_GET['value'] ) && 'yes' === $_GET['value'] ) {
139 $this->optin->enable();
140 set_transient( self::THANKYOU_TRANSIENT, 1, 60 );
141 }
142
143 update_option( self::NOTICE_DISPLAYED_OPTION, 1 );
144
145 wp_safe_redirect( wp_get_referer() );
146 exit;
147 }
148
149 /**
150 * Render the "Thank you" admin notice after opt-in is first enabled.
151 *
152 * Triggered on the next page load via transient.
153 *
154 * @return void
155 */
156 public function render_thankyou_notice(): void {
157 if ( ! current_user_can( 'manage_options' ) ) {
158 return;
159 }
160
161 if ( ! imagify_is_screen( 'imagify-settings' ) ) {
162 return;
163 }
164
165 if ( ! get_transient( self::THANKYOU_TRANSIENT ) ) {
166 return;
167 }
168
169 delete_transient( self::THANKYOU_TRANSIENT );
170
171 // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
172 extract( $this->collect_data(), EXTR_SKIP );
173
174 include IMAGIFY_PATH . 'views/notice-analytics-thankyou.php';
175 }
176
177 /**
178 * Collect the anonymous tracking data to display in the analytics UI.
179 *
180 * @return array<string, string>
181 */
182 private function collect_data(): array {
183 $wp_version = get_bloginfo( 'version' );
184 $php_version = PHP_VERSION;
185 $plugin_version = IMAGIFY_VERSION;
186 $opt_level = imagify_get_optimization_level_label( (int) get_imagify_option( 'optimization_level' ) );
187
188 $convert_webp = (bool) get_imagify_option( 'convert_to_webp' );
189 $convert_avif = (bool) get_imagify_option( 'convert_to_avif' );
190 $next_gen = __( 'None', 'imagify' );
191 if ( $convert_webp && $convert_avif ) {
192 $next_gen = __( 'WebP and AVIF', 'imagify' );
193 } elseif ( $convert_avif ) {
194 $next_gen = __( 'AVIF', 'imagify' );
195 } elseif ( $convert_webp ) {
196 $next_gen = __( 'WebP', 'imagify' );
197 }
198
199 $imagify_user = get_imagify_user();
200 $license_type = ! is_wp_error( $imagify_user ) && ! empty( $imagify_user->plan_label )
201 ? ucfirst( $imagify_user->plan_label )
202 : __( 'N/A', 'imagify' );
203
204 return compact( 'wp_version', 'php_version', 'plugin_version', 'opt_level', 'next_gen', 'license_type' );
205 }
206 }
207