Completed
Branch FET-9795-new-interfaces (4c886e)
by
unknown
14:13 queued 1438:08
created

EE_PUE   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 341
Duplicated Lines 2.05 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
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_collection_enqueue_scripts() 7 7 1
A espresso_data_optin_ajax_handler() 0 13 3
B is_update_available() 0 22 5
A _uxip_hooks() 0 6 2
A track_active_theme() 0 8 2
C track_event_info() 0 54 10

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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 if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed');
2
/**
3
 * EE_PUE
4
 *
5
 * @package		Event Espresso
6
 * @subpackage	includes/core/
7
 * @author		Darren Ethier
8
 */
9
class EE_PUE implements EventEspresso\core\interfaces\UnsettableInterface {
10
11
12
	/**
13
	 *	class constructor
14
	 *
15
	 *	@access public
16
	 */
17
	public function __construct() {
18
//		throw new EE_Error('error');
19
20
		do_action( 'AHEE_log', __CLASS__, __FUNCTION__ );
21
22
		//wp have no MONTH_IN_SECONDS constant.  So we approximate our own assuming all months are 4 weeks long.
23
		if ( !defined('MONTH_IN_SECONDS' ) )
24
			define( 'MONTH_IN_SECONDS', WEEK_IN_SECONDS * 4 );
25
26
		if(EE_Maintenance_Mode::instance()->level() != EE_Maintenance_Mode::level_2_complete_maintenance){
27
			$this->_uxip_hooks();
28
		}
29
30
31
		$ueip_optin = EE_Registry::instance()->CFG->core->ee_ueip_optin;
32
		$ueip_has_notified = EE_Registry::instance()->CFG->core->ee_ueip_has_notified;
33
34
		//has optin been selected for data collection?
35
		$espresso_data_optin = !empty($ueip_optin) ? $ueip_optin : NULL;
36
37
		if ( empty($ueip_has_notified) && EE_Maintenance_Mode::instance()->level() != EE_Maintenance_Mode::level_2_complete_maintenance ) {
38
			add_action('admin_notices', array( $this, 'espresso_data_collection_optin_notice' ), 10 );
39
			add_action('admin_enqueue_scripts', array( $this, 'espresso_data_collection_enqueue_scripts' ), 10 );
40
			add_action('wp_ajax_espresso_data_optin', array( $this, 'espresso_data_optin_ajax_handler' ), 10 );
41
			update_option('ee_ueip_optin', 'yes');
42
			$espresso_data_optin = 'yes';
43
		}
44
45
		//let's prepare extra stats
46
		$extra_stats = array();
47
48
		//only collect extra stats if the plugin user has opted in.
49
		if ( !empty($espresso_data_optin) && $espresso_data_optin == 'yes' ) {
50
			//let's only setup extra data if transient has expired
51
			if ( false === ( $transient = get_transient('ee_extra_data') ) && EE_Maintenance_Mode::instance()->level() != EE_Maintenance_Mode::level_2_complete_maintenance ) {
52
53
				$current_site = is_multisite() ? get_current_site() : NULL;
54
				$site_pre = ! is_main_site() && ! empty($current_site) ? trim( preg_replace('/\b\w\S\w\b/', '', $current_site->domain ), '.' ) . '_' : '';
55
56
57
				//active gateways
58
				$active_gateways = get_option('event_espresso_active_gateways');
59
				if ( !empty($active_gateways ) ) {
60
					foreach ( (array) $active_gateways as $gateway => $ignore ) {
61
						$extra_stats[$site_pre . $gateway . '_gateway_active'] = 1;
62
					}
63
				}
64
65
				if ( is_multisite() && is_main_site() ) {
66
					$extra_stats['is_multisite'] = true;
67
				}
68
69
				//what is the current active theme?
70
				$active_theme = get_option('uxip_ee_active_theme');
71
				if ( !empty( $active_theme ) )
72
					$extra_stats[$site_pre . 'active_theme'] = $active_theme;
73
74
				//event info regarding an all event count and all "active" event count
75
				$all_events_count = get_option('uxip_ee4_all_events_count');
76
				if ( !empty( $all_events_count ) )
77
					$extra_stats[$site_pre . 'ee4_all_events_count'] = $all_events_count;
78
				$active_events_count = get_option('uxip_ee4_active_events_count');
79
				if ( !empty( $active_events_count ) )
80
					$extra_stats[$site_pre . 'ee4_active_events_count'] = $active_events_count;
81
82
				//datetime stuff
83
				$dtt_count = get_option('uxip_ee_all_dtts_count');
84
				if ( !empty( $dtt_count ) )
85
					$extra_stats[$site_pre . 'all_dtts_count'] = $dtt_count;
86
87
				$dtt_sold = get_option('uxip_ee_dtt_sold');
88
				if ( !empty( $dtt_sold ) )
89
					$extra_stats[$site_pre . 'dtt_sold'] = $dtt_sold;
90
91
				//ticket stuff
92
				$all_tkt_count = get_option('uxip_ee_all_tkt_count');
93
				if ( !empty( $all_tkt_count ) )
94
					$extra_stats[$site_pre . 'all_tkt_count'] = $all_tkt_count;
95
96
				$free_tkt_count = get_option('uxip_ee_free_tkt_count');
97
				if ( !empty( $free_tkt_count ) )
98
					$extra_stats[$site_pre . 'free_tkt_count'] = $free_tkt_count;
99
100
				$paid_tkt_count = get_option('uxip_ee_paid_tkt_count');
101
				if ( !empty( $paid_tkt_count ) )
102
					$extra_stats[$site_pre . 'paid_tkt_count'] = $paid_tkt_count;
103
104
				$tkt_sold = get_option('uxip_ee_tkt_sold' );
105
				if ( !empty($tkt_sold) )
106
					$extra_stats[$site_pre . 'tkt_sold'] = $tkt_sold;
107
108
				//phpversion checking
109
				$extra_stats['phpversion'] = function_exists('phpversion') ? phpversion() : 'unknown';
110
111
				//set transient
112
				set_transient( 'ee_extra_data', $extra_stats, WEEK_IN_SECONDS );
113
			}
114
		}
115
116
117
118
		// PUE Auto Upgrades stuff
119
		if (is_readable(EE_THIRD_PARTY . 'pue/pue-client.php')) { //include the file
120
			require_once(EE_THIRD_PARTY . 'pue/pue-client.php' );
121
122
			$api_key = isset( EE_Registry::instance()->NET_CFG->core->site_license_key ) ? EE_Registry::instance()->NET_CFG->core->site_license_key : '';
123
			$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.
124
125
			//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.
126
			//For example:
127
			//$plugin_slug['premium']['p'] = 'some-premium-slug';
128
			//$plugin_slug['prerelease']['pr'] = 'some-pre-release-slug';
129
			//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:
130
			//$plugin_slug['premium']['p'] = 'some-premium-slug';
131
			//$plugin_slug['prerelease']['b'] = 'some-pre-release-slug';
132
			//..WOULD work!
133
			$plugin_slug = array(
134
				'free' => array( 'decaf' => 'event-espresso-core-decaf' ),
135
				'premium' => array( 'p' => 'event-espresso-core-reg' ),
136
				'prerelease' => array( 'beta' => 'event-espresso-core-pr' )
137
				);
138
139
140
			//$options needs to be an array with the included keys as listed.
141
			$options = array(
142
			//	'optionName' => '', //(optional) - used as the reference for saving update information in the clients options table.  Will be automatically set if left blank.
143
				'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)
144
				'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.
145
				'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.
146
				'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.
147
				'options_page_slug' => 'espresso_general_settings',
148
				'plugin_basename' => EE_PLUGIN_BASENAME,
149
				'use_wp_update' => true, //if TRUE then you want FREE versions of the plugin to be updated from WP
150
				'extra_stats' => $extra_stats,
151
				'turn_on_notices_saved' => true
152
			);
153
			new PluginUpdateEngineChecker($host_server_url, $plugin_slug, $options); //initiate the class and start the plugin update engine!
0 ignored issues
show
Documentation introduced by
$plugin_slug is of type array<string,array,{"fre..."beta\":\"string\"}>"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
154
		}
155
	}
156
157
158
159
	/**
160
	 * The purpose of this function is to display information about Event Espresso data collection
161
	 * and a optin selection for extra data collecting by users.
162
	 *
163
	 * @param bool $extra
164
	 * @return string html.
165
	 */
166
	 public static function espresso_data_collection_optin_text( $extra = true ) {
167
	 	if ( ! $extra ) {
168
			 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>';
169
			 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>' );
170
		} else {
171
			$settings_url = EE_Admin_Page::add_query_args_and_nonce( array( 'action' => 'default'), admin_url( 'admin.php?page=espresso_general_settings') );
172
			$settings_url .= '#UXIP_settings';
173
			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>' );
174
		}
175
	}
176
177
178
179
180
	function espresso_data_collection_optin_notice() {
181
		$ueip_has_notified = EE_Registry::instance()->CFG->core->ee_ueip_has_notified;
182
		if ( $ueip_has_notified ) return;
183
		// $settings_url = EE_Admin_Page::add_query_args_and_nonce( array( 'action' => 'default'), admin_url( 'admin.php?page=espresso_general_settings') );
184
		// $settings_url = $settings_url . '#UXIP_settings';
185
		?>
186
		<div class="updated data-collect-optin" id="espresso-data-collect-optin-container">
187
			<div id="data-collect-optin-options-container">
188
				<span class="dashicons dashicons-admin-site"></span>
189
				<span class="data-optin-text"><?php echo EE_PUE::espresso_data_collection_optin_text(); ?></span>
190
				<span style="display: none" id="data-optin-nonce"><?php echo wp_create_nonce('ee-data-optin'); ?></span>
191
				<button class="button-secondary data-optin-button" value="no"><?php _e('Dismiss', 'event_espresso'); ?></button>
192
				<!--<button class="button-primary data-optin-button" value="yes"><?php _e('Yes! I\'m In', 'event_espresso'); ?></button>-->
193
				<div style="clear:both"></div>
194
			</div>
195
		</div>
196
		<?php
197
	}
198
199
200
201
	/**
202
	 * enqueue scripts/styles needed for data collection optin
203
	 * @return void
204
	 */
205 View Code Duplication
	function espresso_data_collection_enqueue_scripts() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
206
		wp_register_script( 'ee-data-optin-js', EE_GLOBAL_ASSETS_URL . 'scripts/ee-data-optin.js', array('jquery'), EVENT_ESPRESSO_VERSION, TRUE );
207
		wp_register_style( 'ee-data-optin-css', EE_GLOBAL_ASSETS_URL . 'css/ee-data-optin.css', array(), EVENT_ESPRESSO_VERSION );
208
209
		wp_enqueue_script('ee-data-optin-js');
210
		wp_enqueue_style('ee-data-optin-css');
211
	}
212
213
214
215
	/**
216
	 * This just handles the setting of the selected option for data optin via ajax
217
	 * @return void
218
	 */
219
	function espresso_data_optin_ajax_handler() {
220
221
		//verify nonce
222
		if ( isset($_POST['nonce']) && !wp_verify_nonce($_POST['nonce'], 'ee-data-optin') ) exit();
223
224
		//made it here so let's save the selection
225
		// $ueip_optin = isset( $_POST['selection'] ) ? $_POST['selection'] : 'no';
226
227
		//update_option('ee_ueip_optin', $ueip_optin);
228
		EE_Registry::instance()->CFG->core->ee_ueip_has_notified = 1;
0 ignored issues
show
Documentation Bug introduced by
The property $ee_ueip_has_notified was declared of type boolean, but 1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
229
		EE_Registry::instance()->CFG->update_espresso_config( FALSE, FALSE );
230
		exit();
231
	}
232
233
234
235
	/**
236
	 * This is a handy helper method for retrieving whether there is an update available for the given plugin.
237
	 * @param  string  $basename Use the equivalent result from plugin_basename() for this param as WP uses that to identify plugins. Defaults to core update
238
	 * @return boolean           True if update available, false if not.
239
	 */
240
	public static function is_update_available($basename = '') {
241
242
		$basename = ! empty( $basename ) ? $basename : EE_PLUGIN_BASENAME;
243
244
		$update = false;
245
246
		$folder = DS . dirname($basename); // should take "event-espresso-core/espresso.php" and change to "/event-espresso-core"
247
248
		$plugins = get_plugins($folder);
249
		$current = get_site_transient( 'update_plugins' );
250
251
		foreach ( (array) $plugins as $plugin_file => $plugin_data ) {
252
			if ( isset( $current->response['plugin_file'] ) )
253
				$update = true;
254
		}
255
256
		//it's possible that there is an update but an invalid site-license-key is in use
257
		if ( get_site_option('pue_json_error_' . $basename ) )
258
			$update = true;
259
260
		return $update;
261
	}
262
263
264
	/**
265
	 * UXIP TRACKING *******
266
	 */
267
268
269
	/**
270
	 * This method contains all the hooks into EE for gathering stats that will be reported with the PUE uxip system
271
	 * @public
272
	 * @return void
273
	 */
274
	public function _uxip_hooks() {
275
		if ( EE_Maintenance_Mode::instance()->level() != EE_Maintenance_Mode::level_2_complete_maintenance ) {
276
			add_action('admin_init', array( $this, 'track_active_theme' ) );
277
			add_action('admin_init', array( $this, 'track_event_info' ) );
278
		}
279
	}
280
281
282
283
284
	public function track_active_theme() {
285
		//we only check this once a month.
286
		if ( false === ( $transient = get_transient( 'ee_active_theme_check' ) ) ) {
287
			$theme = wp_get_theme();
288
			update_option('uxip_ee_active_theme', $theme->get('Name') );
289
			set_transient('ee_active_theme_check', 1, MONTH_IN_SECONDS );
290
		}
291
	}
292
293
294
	public function track_event_info() {
295
		//we only check this once every couple weeks.
296
		if ( false === ( $transient = get_transient( 'ee4_event_info_check') ) ) {
297
			//first let's get the number for ALL events
298
			/** @var EEM_Event $EVT */
299
			$EVT = EE_Registry::instance()->load_model( 'Event');
300
			$DTT = EE_Registry::instance()->load_model('Datetime');
301
			$TKT = EE_Registry::instance()->load_model('Ticket');
302
			$count = $EVT->count();
303
			if ( $count > 0 )
304
				update_option('uxip_ee4_all_events_count', $count);
305
306
			//next let's just get the number of ACTIVE events
307
			$count_active = $EVT->get_active_events(array(), TRUE);
308
			if ( $count_active > 0 )
309
				update_option('uxip_ee4_active_events_count', $count_active);
310
311
			//datetimes!
312
			$dtt_count = $DTT->count();
313
			if ( $dtt_count > 0 )
314
				update_option( 'uxip_ee_all_dtts_count', $dtt_count );
315
316
317
			//dttsold
318
			$dtt_sold = $DTT->sum(array(), 'DTT_sold');
319
			if ( $dtt_sold > 0 )
320
				update_option( 'uxip_ee_dtt_sold', $dtt_sold );
321
322
			//allticketcount
323
			$all_tkt_count = $TKT->count();
324
			if ( $all_tkt_count > 0 )
325
				update_option( 'uxip_ee_all_tkt_count', $all_tkt_count );
326
327
			//freetktcount
328
			$_where = array( 'TKT_price' => 0 );
329
			$free_tkt_count = $TKT->count(array($_where));
330
			if ( $free_tkt_count > 0 )
331
				update_option( 'uxip_ee_free_tkt_count', $free_tkt_count );
332
333
			//paidtktcount
334
			$_where = array( 'TKT_price' => array('>', 0) );
335
			$paid_tkt_count = $TKT->count( array( $_where ) );
336
			if ( $paid_tkt_count > 0 )
337
				update_option( 'uxip_ee_paid_tkt_count', $paid_tkt_count );
338
339
			//tktsold
340
			$tkt_sold = $TKT->sum( array(), 'TKT_sold' );
341
			if( $tkt_sold > 0 )
342
				update_option( 'uxip_ee_tkt_sold', $tkt_sold );
343
344
345
			set_transient( 'ee4_event_info_check', 1, WEEK_IN_SECONDS * 2 );
346
		}
347
	}
348
349
}
350
// End of file EE_PUE.core.php
351
// Location: ./core/EE_PUE.core.php
352