Completed
Branch BUG-10738-inconsistency-in-ses... (860590)
by
unknown
57:39 queued 45:30
created

EE_PUE   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 341
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 0
Metric Value
wmc 57
lcom 0
cbo 10
dl 0
loc 341
rs 5.1724
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A _uxip_hooks() 0 6 2
A track_active_theme() 0 8 2
F __construct() 0 139 29
A espresso_data_collection_optin_text() 0 10 3
A espresso_data_collection_optin_notice() 0 18 2
A espresso_data_optin_ajax_handler() 0 13 3
B is_update_available() 0 22 5
C track_event_info() 0 54 10
A espresso_data_collection_enqueue_scripts() 0 7 1

How to fix   Complexity   

Complex Class

Complex classes like EE_PUE often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use EE_PUE, and based on these observations, apply Extract Interface, too.

1
<?php use EventEspresso\core\interfaces\InterminableInterface;
2
3
if ( ! defined( 'EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed');
4
/**
5
 * EE_PUE
6
 *
7
 * @package		Event Espresso
8
 * @subpackage	includes/core/
9
 * @author		Darren Ethier
10
 */
11
class EE_PUE implements InterminableInterface {
12
13
14
	/**
15
	 *	class constructor
16
	 *
17
	 *	@access public
18
	 */
19
	public function __construct() {
20
//		throw new EE_Error('error');
21
22
		do_action( 'AHEE_log', __CLASS__, __FUNCTION__ );
23
24
		//wp have no MONTH_IN_SECONDS constant.  So we approximate our own assuming all months are 4 weeks long.
25
		if ( !defined('MONTH_IN_SECONDS' ) )
26
			define( 'MONTH_IN_SECONDS', WEEK_IN_SECONDS * 4 );
27
28
		if(EE_Maintenance_Mode::instance()->level() != EE_Maintenance_Mode::level_2_complete_maintenance){
29
			$this->_uxip_hooks();
30
		}
31
32
33
		$ueip_optin = EE_Registry::instance()->CFG->core->ee_ueip_optin;
34
		$ueip_has_notified = EE_Registry::instance()->CFG->core->ee_ueip_has_notified;
35
36
		//has optin been selected for data collection?
37
		$espresso_data_optin = !empty($ueip_optin) ? $ueip_optin : NULL;
38
39
		if ( empty($ueip_has_notified) && EE_Maintenance_Mode::instance()->level() != EE_Maintenance_Mode::level_2_complete_maintenance ) {
40
			add_action('admin_notices', array( $this, 'espresso_data_collection_optin_notice' ), 10 );
41
			add_action('admin_enqueue_scripts', array( $this, 'espresso_data_collection_enqueue_scripts' ), 10 );
42
			add_action('wp_ajax_espresso_data_optin', array( $this, 'espresso_data_optin_ajax_handler' ), 10 );
43
			update_option('ee_ueip_optin', 'yes');
44
			$espresso_data_optin = 'yes';
45
		}
46
47
		//let's prepare extra stats
48
		$extra_stats = array();
49
50
		//only collect extra stats if the plugin user has opted in.
51
		if ( !empty($espresso_data_optin) && $espresso_data_optin == 'yes' ) {
52
			//let's only setup extra data if transient has expired
53
			if ( false === ( $transient = get_transient('ee_extra_data') ) && EE_Maintenance_Mode::instance()->level() != EE_Maintenance_Mode::level_2_complete_maintenance ) {
54
55
				$current_site = is_multisite() ? get_current_site() : NULL;
56
				$site_pre = ! is_main_site() && ! empty($current_site) ? trim( preg_replace('/\b\w\S\w\b/', '', $current_site->domain ), '.' ) . '_' : '';
57
58
59
				//active gateways
60
				$active_gateways = get_option('event_espresso_active_gateways');
61
				if ( !empty($active_gateways ) ) {
62
					foreach ( (array) $active_gateways as $gateway => $ignore ) {
63
						$extra_stats[$site_pre . $gateway . '_gateway_active'] = 1;
64
					}
65
				}
66
67
				if ( is_multisite() && is_main_site() ) {
68
					$extra_stats['is_multisite'] = true;
69
				}
70
71
				//what is the current active theme?
72
				$active_theme = get_option('uxip_ee_active_theme');
73
				if ( !empty( $active_theme ) )
74
					$extra_stats[$site_pre . 'active_theme'] = $active_theme;
75
76
				//event info regarding an all event count and all "active" event count
77
				$all_events_count = get_option('uxip_ee4_all_events_count');
78
				if ( !empty( $all_events_count ) )
79
					$extra_stats[$site_pre . 'ee4_all_events_count'] = $all_events_count;
80
				$active_events_count = get_option('uxip_ee4_active_events_count');
81
				if ( !empty( $active_events_count ) )
82
					$extra_stats[$site_pre . 'ee4_active_events_count'] = $active_events_count;
83
84
				//datetime stuff
85
				$dtt_count = get_option('uxip_ee_all_dtts_count');
86
				if ( !empty( $dtt_count ) )
87
					$extra_stats[$site_pre . 'all_dtts_count'] = $dtt_count;
88
89
				$dtt_sold = get_option('uxip_ee_dtt_sold');
90
				if ( !empty( $dtt_sold ) )
91
					$extra_stats[$site_pre . 'dtt_sold'] = $dtt_sold;
92
93
				//ticket stuff
94
				$all_tkt_count = get_option('uxip_ee_all_tkt_count');
95
				if ( !empty( $all_tkt_count ) )
96
					$extra_stats[$site_pre . 'all_tkt_count'] = $all_tkt_count;
97
98
				$free_tkt_count = get_option('uxip_ee_free_tkt_count');
99
				if ( !empty( $free_tkt_count ) )
100
					$extra_stats[$site_pre . 'free_tkt_count'] = $free_tkt_count;
101
102
				$paid_tkt_count = get_option('uxip_ee_paid_tkt_count');
103
				if ( !empty( $paid_tkt_count ) )
104
					$extra_stats[$site_pre . 'paid_tkt_count'] = $paid_tkt_count;
105
106
				$tkt_sold = get_option('uxip_ee_tkt_sold' );
107
				if ( !empty($tkt_sold) )
108
					$extra_stats[$site_pre . 'tkt_sold'] = $tkt_sold;
109
110
				//phpversion checking
111
				$extra_stats['phpversion'] = function_exists('phpversion') ? phpversion() : 'unknown';
112
113
				//set transient
114
				set_transient( 'ee_extra_data', $extra_stats, WEEK_IN_SECONDS );
115
			}
116
		}
117
118
119
120
		// PUE Auto Upgrades stuff
121
		if (is_readable(EE_THIRD_PARTY . 'pue/pue-client.php')) { //include the file
122
			require_once(EE_THIRD_PARTY . 'pue/pue-client.php' );
123
124
			$api_key = isset( EE_Registry::instance()->NET_CFG->core->site_license_key ) ? EE_Registry::instance()->NET_CFG->core->site_license_key : '';
125
			$host_server_url = 'https://eventespresso.com'; //this needs to be the host server where plugin update engine is installed. Note, if you leave this blank then it is assumed the WordPress repo will be used and we'll just check there.
126
127
			//Note: PUE uses a simple preg_match to determine what type is currently installed based on version number.  So it's important that you use a key for the version type that is unique and not found in another key.
128
			//For example:
129
			//$plugin_slug['premium']['p'] = 'some-premium-slug';
130
			//$plugin_slug['prerelease']['pr'] = 'some-pre-release-slug';
131
			//The above would not work because "p" is found in both keys for the version type. ( i.e 1.0.p vs 1.0.pr ) so doing something like:
132
			//$plugin_slug['premium']['p'] = 'some-premium-slug';
133
			//$plugin_slug['prerelease']['b'] = 'some-pre-release-slug';
134
			//..WOULD work!
135
			$plugin_slug = array(
136
				'free' => array( 'decaf' => 'event-espresso-core-decaf' ),
137
				'premium' => array( 'p' => 'event-espresso-core-reg' ),
138
				'prerelease' => array( 'beta' => 'event-espresso-core-pr' )
139
				);
140
141
142
			//$options needs to be an array with the included keys as listed.
143
			$options = array(
144
			//	'optionName' => '', //(optional) - used as the reference for saving update information in the clients options table.  Will be automatically set if left blank.
145
				'apikey' => $api_key, //(required), you will need to obtain the apikey that the client gets from your site and then saves in their sites options table (see 'getting an api-key' below)
146
				'lang_domain' => 'event_espresso', //(optional) - put here whatever reference you are using for the localization of your plugin (if it's localized).  That way strings in this file will be included in the translation for your plugin.
147
				'checkPeriod' => '24', //(optional) - use this parameter to indicate how often you want the client's install to ping your server for update checks.  The integer indicates hours.  If you don't include this parameter it will default to 12 hours.
148
				'option_key' => 'site_license_key', //this is what is used to reference the api_key in your plugin options.  PUE uses this to trigger updating your information message whenever this option_key is modified.
149
				'options_page_slug' => 'espresso_general_settings',
150
				'plugin_basename' => EE_PLUGIN_BASENAME,
151
				'use_wp_update' => true, //if TRUE then you want FREE versions of the plugin to be updated from WP
152
				'extra_stats' => $extra_stats,
153
				'turn_on_notices_saved' => true
154
			);
155
			new PluginUpdateEngineChecker($host_server_url, $plugin_slug, $options); //initiate the class and start the plugin update engine!
156
		}
157
	}
158
159
160
161
	/**
162
	 * The purpose of this function is to display information about Event Espresso data collection
163
	 * and a optin selection for extra data collecting by users.
164
	 *
165
	 * @param bool $extra
166
	 * @return string html.
167
	 */
168
	 public static function espresso_data_collection_optin_text( $extra = true ) {
169
	 	if ( ! $extra ) {
170
			 echo '<h2 class="ee-admin-settings-hdr" '. (!$extra ? 'id="UXIP_settings"' : '').'>'.__('User eXperience Improvement Program (UXIP)', 'event_espresso').EEH_Template::get_help_tab_link('organization_logo_info').'</h2>';
171
			 echo sprintf( __('%sPlease help us make Event Espresso better and vote for your favorite features.%s The %sUser eXperience Improvement Program (UXIP)%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 but it is enabled 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. %sPlease see our %sPrivacy Policy%s for more information.', 'event_espresso'), '<p><em>', '</em></p>','<a href="http://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">','</a>','<br><br>','<a href="http://eventespresso.com/about/privacy-policy/" target="_blank">','</a>' );
172
		} else {
173
			$settings_url = EE_Admin_Page::add_query_args_and_nonce( array( 'action' => 'default'), admin_url( 'admin.php?page=espresso_general_settings') );
174
			$settings_url .= '#UXIP_settings';
175
			echo sprintf( __( 'The Event Espresso UXIP feature is active on your site. For %smore info%s and to opt-out %sclick here%s.', 'event_espresso' ), '<a href="http://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">', '</a>', '<a href="' . $settings_url . '" target="_blank">', '</a>' );
176
		}
177
	}
178
179
180
181
182
	function espresso_data_collection_optin_notice() {
183
		$ueip_has_notified = EE_Registry::instance()->CFG->core->ee_ueip_has_notified;
184
		if ( $ueip_has_notified ) return;
185
		// $settings_url = EE_Admin_Page::add_query_args_and_nonce( array( 'action' => 'default'), admin_url( 'admin.php?page=espresso_general_settings') );
186
		// $settings_url = $settings_url . '#UXIP_settings';
187
		?>
188
		<div class="updated data-collect-optin" id="espresso-data-collect-optin-container">
189
			<div id="data-collect-optin-options-container">
190
				<span class="dashicons dashicons-admin-site"></span>
191
				<span class="data-optin-text"><?php echo EE_PUE::espresso_data_collection_optin_text(); ?></span>
192
				<span style="display: none" id="data-optin-nonce"><?php echo wp_create_nonce('ee-data-optin'); ?></span>
193
				<button class="button-secondary data-optin-button" value="no"><?php _e('Dismiss', 'event_espresso'); ?></button>
194
				<!--<button class="button-primary data-optin-button" value="yes"><?php _e('Yes! I\'m In', 'event_espresso'); ?></button>-->
195
				<div style="clear:both"></div>
196
			</div>
197
		</div>
198
		<?php
199
	}
200
201
202
203
	/**
204
	 * enqueue scripts/styles needed for data collection optin
205
	 * @return void
206
	 */
207
	function espresso_data_collection_enqueue_scripts() {
208
		wp_register_script( 'ee-data-optin-js', EE_GLOBAL_ASSETS_URL . 'scripts/ee-data-optin.js', array('jquery'), EVENT_ESPRESSO_VERSION, TRUE );
209
		wp_register_style( 'ee-data-optin-css', EE_GLOBAL_ASSETS_URL . 'css/ee-data-optin.css', array(), EVENT_ESPRESSO_VERSION );
210
211
		wp_enqueue_script('ee-data-optin-js');
212
		wp_enqueue_style('ee-data-optin-css');
213
	}
214
215
216
217
	/**
218
	 * This just handles the setting of the selected option for data optin via ajax
219
	 * @return void
220
	 */
221
	function espresso_data_optin_ajax_handler() {
222
223
		//verify nonce
224
		if ( isset($_POST['nonce']) && !wp_verify_nonce($_POST['nonce'], 'ee-data-optin') ) exit();
225
226
		//made it here so let's save the selection
227
		// $ueip_optin = isset( $_POST['selection'] ) ? $_POST['selection'] : 'no';
228
229
		//update_option('ee_ueip_optin', $ueip_optin);
230
		EE_Registry::instance()->CFG->core->ee_ueip_has_notified = 1;
231
		EE_Registry::instance()->CFG->update_espresso_config( FALSE, FALSE );
232
		exit();
233
	}
234
235
236
237
	/**
238
	 * This is a handy helper method for retrieving whether there is an update available for the given plugin.
239
	 * @param  string  $basename Use the equivalent result from plugin_basename() for this param as WP uses that to identify plugins. Defaults to core update
240
	 * @return boolean           True if update available, false if not.
241
	 */
242
	public static function is_update_available($basename = '') {
243
244
		$basename = ! empty( $basename ) ? $basename : EE_PLUGIN_BASENAME;
245
246
		$update = false;
247
248
		$folder = DS . dirname($basename); // should take "event-espresso-core/espresso.php" and change to "/event-espresso-core"
249
250
		$plugins = get_plugins($folder);
251
		$current = get_site_transient( 'update_plugins' );
252
253
		foreach ( (array) $plugins as $plugin_file => $plugin_data ) {
254
			if ( isset( $current->response['plugin_file'] ) )
255
				$update = true;
256
		}
257
258
		//it's possible that there is an update but an invalid site-license-key is in use
259
		if ( get_site_option('pue_json_error_' . $basename ) )
260
			$update = true;
261
262
		return $update;
263
	}
264
265
266
	/**
267
	 * UXIP TRACKING *******
268
	 */
269
270
271
	/**
272
	 * This method contains all the hooks into EE for gathering stats that will be reported with the PUE uxip system
273
	 * @public
274
	 * @return void
275
	 */
276
	public function _uxip_hooks() {
277
		if ( EE_Maintenance_Mode::instance()->level() != EE_Maintenance_Mode::level_2_complete_maintenance ) {
278
			add_action('admin_init', array( $this, 'track_active_theme' ) );
279
			add_action('admin_init', array( $this, 'track_event_info' ) );
280
		}
281
	}
282
283
284
285
286
	public function track_active_theme() {
287
		//we only check this once a month.
288
		if ( false === ( $transient = get_transient( 'ee_active_theme_check' ) ) ) {
289
			$theme = wp_get_theme();
290
			update_option('uxip_ee_active_theme', $theme->get('Name') );
291
			set_transient('ee_active_theme_check', 1, MONTH_IN_SECONDS );
292
		}
293
	}
294
295
296
	public function track_event_info() {
297
		//we only check this once every couple weeks.
298
		if ( false === ( $transient = get_transient( 'ee4_event_info_check') ) ) {
299
			//first let's get the number for ALL events
300
			/** @var EEM_Event $EVT */
301
			$EVT = EE_Registry::instance()->load_model( 'Event');
302
			$DTT = EE_Registry::instance()->load_model('Datetime');
303
			$TKT = EE_Registry::instance()->load_model('Ticket');
304
			$count = $EVT->count();
305
			if ( $count > 0 )
306
				update_option('uxip_ee4_all_events_count', $count);
307
308
			//next let's just get the number of ACTIVE events
309
			$count_active = $EVT->get_active_events(array(), TRUE);
310
			if ( $count_active > 0 )
311
				update_option('uxip_ee4_active_events_count', $count_active);
312
313
			//datetimes!
314
			$dtt_count = $DTT->count();
315
			if ( $dtt_count > 0 )
316
				update_option( 'uxip_ee_all_dtts_count', $dtt_count );
317
318
319
			//dttsold
320
			$dtt_sold = $DTT->sum(array(), 'DTT_sold');
321
			if ( $dtt_sold > 0 )
322
				update_option( 'uxip_ee_dtt_sold', $dtt_sold );
323
324
			//allticketcount
325
			$all_tkt_count = $TKT->count();
326
			if ( $all_tkt_count > 0 )
327
				update_option( 'uxip_ee_all_tkt_count', $all_tkt_count );
328
329
			//freetktcount
330
			$_where = array( 'TKT_price' => 0 );
331
			$free_tkt_count = $TKT->count(array($_where));
332
			if ( $free_tkt_count > 0 )
333
				update_option( 'uxip_ee_free_tkt_count', $free_tkt_count );
334
335
			//paidtktcount
336
			$_where = array( 'TKT_price' => array('>', 0) );
337
			$paid_tkt_count = $TKT->count( array( $_where ) );
338
			if ( $paid_tkt_count > 0 )
339
				update_option( 'uxip_ee_paid_tkt_count', $paid_tkt_count );
340
341
			//tktsold
342
			$tkt_sold = $TKT->sum( array(), 'TKT_sold' );
343
			if( $tkt_sold > 0 )
344
				update_option( 'uxip_ee_tkt_sold', $tkt_sold );
345
346
347
			set_transient( 'ee4_event_info_check', 1, WEEK_IN_SECONDS * 2 );
348
		}
349
	}
350
351
}
352
// End of file EE_PUE.core.php
353
// Location: ./core/EE_PUE.core.php
354