Completed
Pull Request — master (#1427)
by Darren
09:10
created
core/domain/services/pue/Stats.php 1 patch
Indentation   +201 added lines, -201 removed lines patch added patch discarded remove patch
@@ -19,86 +19,86 @@  discard block
 block discarded – undo
19 19
 class Stats
20 20
 {
21 21
 
22
-    const OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS = 'ee_uxip_stats_expiry';
23
-
24
-    /**
25
-     * @var Config
26
-     */
27
-    private $config;
28
-
29
-
30
-    /**
31
-     * @var StatsGatherer
32
-     */
33
-    private $stats_gatherer;
34
-
35
-
36
-    /**
37
-     * @var EE_Maintenance_Mode
38
-     */
39
-    private $maintenance_mode;
40
-
41
-    public function __construct(
42
-        Config $config,
43
-        EE_Maintenance_Mode $maintenance_mode,
44
-        StatsGatherer $stats_gatherer
45
-    ) {
46
-        $this->config = $config;
47
-        $this->maintenance_mode = $maintenance_mode;
48
-        $this->stats_gatherer = $stats_gatherer;
49
-        $this->setUxipNotices();
50
-    }
51
-
52
-
53
-    /**
54
-     * Displays uxip opt-in notice if necessary.
55
-     */
56
-    private function setUxipNotices()
57
-    {
58
-        if ($this->canDisplayNotices()) {
59
-            add_action('admin_notices', array($this, 'optinNotice'));
60
-            add_action('admin_enqueue_scripts', array($this, 'enqueueScripts'));
61
-            add_action('wp_ajax_espresso_data_optin', array($this, 'ajaxHandler'));
62
-        }
63
-    }
64
-
65
-
66
-    /**
67
-     * This returns the callback that PluginUpdateEngineChecker will use for getting any extra stats to send.
68
-     *
69
-     * @return Closure
70
-     */
71
-    public function statsCallback()
72
-    {
73
-        // returns a callback that can is used to retrieve the stats to send along to the pue server.
74
-        return function () {
75
-            // we only send stats one a week, so let's see if our stat timestamp has expired.
76
-            if (! $this->sendStats()) {
77
-                return array();
78
-            }
79
-            return $this->stats_gatherer->stats();
80
-        };
81
-    }
82
-
83
-
84
-    /**
85
-     * Return whether notices can be displayed or not
86
-     *
87
-     * @return bool
88
-     */
89
-    private function canDisplayNotices()
90
-    {
91
-        return ! $this->config->hasNotifiedForUxip()
92
-               && $this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance;
93
-    }
94
-
95
-
96
-    /**
97
-     * Callback for the admin_notices hook that outputs the UXIP optin-in notice.
98
-     */
99
-    public function optinNotice()
100
-    {
101
-        ?>
22
+	const OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS = 'ee_uxip_stats_expiry';
23
+
24
+	/**
25
+	 * @var Config
26
+	 */
27
+	private $config;
28
+
29
+
30
+	/**
31
+	 * @var StatsGatherer
32
+	 */
33
+	private $stats_gatherer;
34
+
35
+
36
+	/**
37
+	 * @var EE_Maintenance_Mode
38
+	 */
39
+	private $maintenance_mode;
40
+
41
+	public function __construct(
42
+		Config $config,
43
+		EE_Maintenance_Mode $maintenance_mode,
44
+		StatsGatherer $stats_gatherer
45
+	) {
46
+		$this->config = $config;
47
+		$this->maintenance_mode = $maintenance_mode;
48
+		$this->stats_gatherer = $stats_gatherer;
49
+		$this->setUxipNotices();
50
+	}
51
+
52
+
53
+	/**
54
+	 * Displays uxip opt-in notice if necessary.
55
+	 */
56
+	private function setUxipNotices()
57
+	{
58
+		if ($this->canDisplayNotices()) {
59
+			add_action('admin_notices', array($this, 'optinNotice'));
60
+			add_action('admin_enqueue_scripts', array($this, 'enqueueScripts'));
61
+			add_action('wp_ajax_espresso_data_optin', array($this, 'ajaxHandler'));
62
+		}
63
+	}
64
+
65
+
66
+	/**
67
+	 * This returns the callback that PluginUpdateEngineChecker will use for getting any extra stats to send.
68
+	 *
69
+	 * @return Closure
70
+	 */
71
+	public function statsCallback()
72
+	{
73
+		// returns a callback that can is used to retrieve the stats to send along to the pue server.
74
+		return function () {
75
+			// we only send stats one a week, so let's see if our stat timestamp has expired.
76
+			if (! $this->sendStats()) {
77
+				return array();
78
+			}
79
+			return $this->stats_gatherer->stats();
80
+		};
81
+	}
82
+
83
+
84
+	/**
85
+	 * Return whether notices can be displayed or not
86
+	 *
87
+	 * @return bool
88
+	 */
89
+	private function canDisplayNotices()
90
+	{
91
+		return ! $this->config->hasNotifiedForUxip()
92
+			   && $this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance;
93
+	}
94
+
95
+
96
+	/**
97
+	 * Callback for the admin_notices hook that outputs the UXIP optin-in notice.
98
+	 */
99
+	public function optinNotice()
100
+	{
101
+		?>
102 102
         <div class="updated data-collect-optin" id="espresso-data-collect-optin-container">
103 103
             <div id="data-collect-optin-options-container">
104 104
                 <span class="dashicons dashicons-admin-site"></span>
@@ -111,125 +111,125 @@  discard block
 block discarded – undo
111 111
             </div>
112 112
         </div>
113 113
         <?php
114
-    }
115
-
116
-
117
-    /**
118
-     * Retrieves the optin text (static so it can be used in multiple places as necessary).
119
-     *
120
-     * @param bool $extra
121
-     */
122
-    public static function optinText($extra = true)
123
-    {
124
-        if (! $extra) {
125
-            echo '<h2 class="ee-admin-settings-hdr" '
126
-                 . (! $extra ? 'id="UXIP_settings"' : '')
127
-                 . '>'
128
-                 . esc_html__('User eXperience Improvement Program (UXIP)', 'event_espresso')
129
-                 . EEH_Template::get_help_tab_link('organization_logo_info')
130
-                 . '</h2>';
131
-            printf(
132
-                esc_html__(
133
-                    '%1$sPlease help us make Event Espresso better and vote for your favorite features.%2$s The %3$sUser eXperience Improvement Program (UXIP)%4$s, has been created so when you use Event Espresso you are voting for the features and settings that are important to you. The UXIP helps us understand how you use our products and services, track problems and in what context. If you opt-out of the UXIP you essentially elect for us to disregard how you use Event Espresso as we build new features and make changes. Participation in the program is completely voluntary and it is disabled by default. The end results of the UXIP are software improvements to better meet your needs. The data we collect will never be sold, traded, or misused in any way. %5$sPlease see our %6$sPrivacy Policy%7$s for more information.',
134
-                    'event_espresso'
135
-                ),
136
-                '<p><em>',
137
-                '</em></p>',
138
-                '<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">',
139
-                '</a>',
140
-                '<br><br>',
141
-                '<a href="https://eventespresso.com/about/privacy-policy/" target="_blank">',
142
-                '</a>'
143
-            );
144
-        } else {
145
-            $settings_url = EEH_URL::add_query_args_and_nonce(
146
-                array('action' => 'default'),
147
-                admin_url('admin.php?page=espresso_general_settings')
148
-            );
149
-            $settings_url .= '#UXIP_settings';
150
-            printf(
151
-                esc_html__(
152
-                    'The Event Espresso UXIP feature is not yet active on your site. For %1$smore info%2$s and to opt-in %3$sclick here%4$s.',
153
-                    'event_espresso'
154
-                ),
155
-                '<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">',
156
-                '</a>',
157
-                '<a href="' . $settings_url . '" target="_blank">',
158
-                '</a>'
159
-            );
160
-        }
161
-    }
162
-
163
-
164
-    /**
165
-     * Callback for admin_enqueue_scripts that sets up the scripts and styles for the uxip notice
166
-     */
167
-    public function enqueueScripts()
168
-    {
169
-        wp_register_script(
170
-            'ee-data-optin-js',
171
-            EE_GLOBAL_ASSETS_URL . 'scripts/ee-data-optin.js',
172
-            array('jquery'),
173
-            EVENT_ESPRESSO_VERSION,
174
-            true
175
-        );
176
-        wp_register_style(
177
-            'ee-data-optin-css',
178
-            EE_GLOBAL_ASSETS_URL . 'css/ee-data-optin.css',
179
-            array(),
180
-            EVENT_ESPRESSO_VERSION
181
-        );
182
-
183
-        wp_enqueue_script('ee-data-optin-js');
184
-        wp_enqueue_style('ee-data-optin-css');
185
-    }
186
-
187
-
188
-    /**
189
-     * Callback for wp_ajax_espresso_data_optin that handles the ajax request
190
-     */
191
-    public function ajaxHandler()
192
-    {
193
-        // verify nonce
194
-        if (isset($_POST['nonce']) && ! wp_verify_nonce($_POST['nonce'], 'ee-data-optin')) {
195
-            exit();
196
-        }
197
-
198
-        // update has notified option
199
-        $this->config->setHasNotifiedAboutUxip();
200
-        exit();
201
-    }
202
-
203
-
204
-    /**
205
-     * Used to determine whether additional stats are sent.
206
-     */
207
-    private function sendStats()
208
-    {
209
-        return $this->config->isOptedInForUxip()
210
-               && $this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance
211
-               && $this->statSendTimestampExpired();
212
-    }
213
-
214
-
215
-    /**
216
-     * Returns true when the timestamp used to track whether stats get sent (currently a weekly interval) is expired.
217
-     * Returns false otherwise.
218
-     *
219
-     * @return bool
220
-     */
221
-    private function statSendTimestampExpired()
222
-    {
223
-        $current_expiry = get_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, null);
224
-        if ($current_expiry === null) {
225
-            add_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, time() + WEEK_IN_SECONDS, '', 'no');
226
-            return true;
227
-        }
228
-
229
-        if (time() > (int) $current_expiry) {
230
-            update_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, time() + WEEK_IN_SECONDS);
231
-            return true;
232
-        }
233
-        return false;
234
-    }
114
+	}
115
+
116
+
117
+	/**
118
+	 * Retrieves the optin text (static so it can be used in multiple places as necessary).
119
+	 *
120
+	 * @param bool $extra
121
+	 */
122
+	public static function optinText($extra = true)
123
+	{
124
+		if (! $extra) {
125
+			echo '<h2 class="ee-admin-settings-hdr" '
126
+				 . (! $extra ? 'id="UXIP_settings"' : '')
127
+				 . '>'
128
+				 . esc_html__('User eXperience Improvement Program (UXIP)', 'event_espresso')
129
+				 . EEH_Template::get_help_tab_link('organization_logo_info')
130
+				 . '</h2>';
131
+			printf(
132
+				esc_html__(
133
+					'%1$sPlease help us make Event Espresso better and vote for your favorite features.%2$s The %3$sUser eXperience Improvement Program (UXIP)%4$s, has been created so when you use Event Espresso you are voting for the features and settings that are important to you. The UXIP helps us understand how you use our products and services, track problems and in what context. If you opt-out of the UXIP you essentially elect for us to disregard how you use Event Espresso as we build new features and make changes. Participation in the program is completely voluntary and it is disabled by default. The end results of the UXIP are software improvements to better meet your needs. The data we collect will never be sold, traded, or misused in any way. %5$sPlease see our %6$sPrivacy Policy%7$s for more information.',
134
+					'event_espresso'
135
+				),
136
+				'<p><em>',
137
+				'</em></p>',
138
+				'<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">',
139
+				'</a>',
140
+				'<br><br>',
141
+				'<a href="https://eventespresso.com/about/privacy-policy/" target="_blank">',
142
+				'</a>'
143
+			);
144
+		} else {
145
+			$settings_url = EEH_URL::add_query_args_and_nonce(
146
+				array('action' => 'default'),
147
+				admin_url('admin.php?page=espresso_general_settings')
148
+			);
149
+			$settings_url .= '#UXIP_settings';
150
+			printf(
151
+				esc_html__(
152
+					'The Event Espresso UXIP feature is not yet active on your site. For %1$smore info%2$s and to opt-in %3$sclick here%4$s.',
153
+					'event_espresso'
154
+				),
155
+				'<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">',
156
+				'</a>',
157
+				'<a href="' . $settings_url . '" target="_blank">',
158
+				'</a>'
159
+			);
160
+		}
161
+	}
162
+
163
+
164
+	/**
165
+	 * Callback for admin_enqueue_scripts that sets up the scripts and styles for the uxip notice
166
+	 */
167
+	public function enqueueScripts()
168
+	{
169
+		wp_register_script(
170
+			'ee-data-optin-js',
171
+			EE_GLOBAL_ASSETS_URL . 'scripts/ee-data-optin.js',
172
+			array('jquery'),
173
+			EVENT_ESPRESSO_VERSION,
174
+			true
175
+		);
176
+		wp_register_style(
177
+			'ee-data-optin-css',
178
+			EE_GLOBAL_ASSETS_URL . 'css/ee-data-optin.css',
179
+			array(),
180
+			EVENT_ESPRESSO_VERSION
181
+		);
182
+
183
+		wp_enqueue_script('ee-data-optin-js');
184
+		wp_enqueue_style('ee-data-optin-css');
185
+	}
186
+
187
+
188
+	/**
189
+	 * Callback for wp_ajax_espresso_data_optin that handles the ajax request
190
+	 */
191
+	public function ajaxHandler()
192
+	{
193
+		// verify nonce
194
+		if (isset($_POST['nonce']) && ! wp_verify_nonce($_POST['nonce'], 'ee-data-optin')) {
195
+			exit();
196
+		}
197
+
198
+		// update has notified option
199
+		$this->config->setHasNotifiedAboutUxip();
200
+		exit();
201
+	}
202
+
203
+
204
+	/**
205
+	 * Used to determine whether additional stats are sent.
206
+	 */
207
+	private function sendStats()
208
+	{
209
+		return $this->config->isOptedInForUxip()
210
+			   && $this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance
211
+			   && $this->statSendTimestampExpired();
212
+	}
213
+
214
+
215
+	/**
216
+	 * Returns true when the timestamp used to track whether stats get sent (currently a weekly interval) is expired.
217
+	 * Returns false otherwise.
218
+	 *
219
+	 * @return bool
220
+	 */
221
+	private function statSendTimestampExpired()
222
+	{
223
+		$current_expiry = get_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, null);
224
+		if ($current_expiry === null) {
225
+			add_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, time() + WEEK_IN_SECONDS, '', 'no');
226
+			return true;
227
+		}
228
+
229
+		if (time() > (int) $current_expiry) {
230
+			update_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, time() + WEEK_IN_SECONDS);
231
+			return true;
232
+		}
233
+		return false;
234
+	}
235 235
 }
Please login to merge, or discard this patch.