Completed
Branch FET-9414-allow-prices-to-be-mo... (2c9812)
by
unknown
654:02 queued 637:10
created

EE_PUE   D

Complexity

Total Complexity 58

Size/Duplication

Total Lines 354
Duplicated Lines 1.98 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 0
Metric Value
dl 7
loc 354
rs 4.8387
c 0
b 0
f 0
wmc 58
lcom 0
cbo 9

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 4
B is_update_available() 0 22 5
A _uxip_hooks() 0 6 2
A track_active_theme() 0 8 2
D track_event_info() 0 53 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
 * Event Espresso
4
 *
5
 * Event Registration and Management Plugin for WordPress
6
 *
7
 * @ package			Event Espresso
8
 * @ author			Seth Shoultes
9
 * @ copyright		(c) 2008-2011 Event Espresso  All Rights Reserved.
10
 * @ license			http://eventespresso.com/support/terms-conditions/   * see Plugin Licensing *
11
 * @ link					http://www.eventespresso.com
12
 * @ version		 	4.0
13
 *
14
 * ------------------------------------------------------------------------
15
 *
16
 * EE_PUE
17
 *
18
 * @package			Event Espresso
19
 * @subpackage	includes/core/
20
 * @author				Darren Ethier
21
 *
22
 * ------------------------------------------------------------------------
23
 */
24
class EE_PUE {
25
26
	/**
27
	 * 	EE_Registry Object
28
	 *	@var 	object
29
	 * 	@access 	protected
30
	 */
31
	protected $EE = NULL;
32
33
	/**
34
	 * This property is used to hold an array of EE_default_term objects assigned to a custom post type when the post for that post type is published with no terms set for the taxonomy.
35
	  *
36
	 * @var array of EE_Default_Term objects
37
	 */
38
	protected $_default_terms = array();
39
40
41
42
43
44
	/**
45
	 *	class constructor
46
	 *
47
	 *	@access public
48
	 */
49
	public function __construct() {
50
//		throw new EE_Error('error');
51
52
		do_action( 'AHEE_log', __CLASS__, __FUNCTION__ );
53
54
		//wp have no MONTH_IN_SECONDS constant.  So we approximate our own assuming all months are 4 weeks long.
55
		if ( !defined('MONTH_IN_SECONDS' ) )
56
			define( 'MONTH_IN_SECONDS', WEEK_IN_SECONDS * 4 );
57
58
		if(EE_Maintenance_Mode::instance()->level() != EE_Maintenance_Mode::level_2_complete_maintenance){
59
			$this->_uxip_hooks();
60
		}
61
62
63
		$ueip_optin = EE_Registry::instance()->CFG->core->ee_ueip_optin;
64
		$ueip_has_notified = EE_Registry::instance()->CFG->core->ee_ueip_has_notified;
65
66
		//has optin been selected for data collection?
67
		$espresso_data_optin = !empty($ueip_optin) ? $ueip_optin : NULL;
68
69
		if ( empty($ueip_has_notified) && EE_Maintenance_Mode::instance()->level() != EE_Maintenance_mode::level_2_complete_maintenance ) {
70
			add_action('admin_notices', array( $this, 'espresso_data_collection_optin_notice' ), 10 );
71
			add_action('admin_enqueue_scripts', array( $this, 'espresso_data_collection_enqueue_scripts' ), 10 );
72
			add_action('wp_ajax_espresso_data_optin', array( $this, 'espresso_data_optin_ajax_handler' ), 10 );
73
			update_option('ee_ueip_optin', 'yes');
74
			$espresso_data_optin = 'yes';
75
		}
76
77
		//let's prepare extra stats
78
		$extra_stats = array();
79
80
		//only collect extra stats if the plugin user has opted in.
81
		if ( !empty($espresso_data_optin) && $espresso_data_optin == 'yes' ) {
82
			//let's only setup extra data if transient has expired
83
			if ( false === ( $transient = get_transient('ee_extra_data') ) && EE_Maintenance_Mode::instance()->level() != EE_Maintenance_Mode::level_2_complete_maintenance ) {
84
85
				$current_site = is_multisite() ? get_current_site() : NULL;
86
				$site_pre = ! is_main_site() && ! empty($current_site) ? trim( preg_replace('/\b\w\S\w\b/', '', $current_site->domain ), '.' ) . '_' : '';
87
88
89
				//active gateways
90
				$active_gateways = get_option('event_espresso_active_gateways');
91
				if ( !empty($active_gateways ) ) {
92
					foreach ( (array) $active_gateways as $gateway => $ignore ) {
93
						$extra_stats[$site_pre . $gateway . '_gateway_active'] = 1;
94
					}
95
				}
96
97
				if ( is_multisite() && is_main_site() ) {
98
					$extra_stats['is_multisite'] = true;
99
				}
100
101
				//what is the current active theme?
102
				$active_theme = get_option('uxip_ee_active_theme');
103
				if ( !empty( $active_theme ) )
104
					$extra_stats[$site_pre . 'active_theme'] = $active_theme;
105
106
				//event info regarding an all event count and all "active" event count
107
				$all_events_count = get_option('uxip_ee4_all_events_count');
108
				if ( !empty( $all_events_count ) )
109
					$extra_stats[$site_pre . 'ee4_all_events_count'] = $all_events_count;
110
				$active_events_count = get_option('uxip_ee4_active_events_count');
111
				if ( !empty( $active_events_count ) )
112
					$extra_stats[$site_pre . 'ee4_active_events_count'] = $active_events_count;
113
114
				//datetime stuff
115
				$dtt_count = get_option('uxip_ee_all_dtts_count');
116
				if ( !empty( $dtt_count ) )
117
					$extra_stats[$site_pre . 'all_dtts_count'] = $dtt_count;
118
119
				$dtt_sold = get_option('uxip_ee_dtt_sold');
120
				if ( !empty( $dtt_sold ) )
121
					$extra_stats[$site_pre . 'dtt_sold'] = $dtt_sold;
122
123
				//ticket stuff
124
				$all_tkt_count = get_option('uxip_ee_all_tkt_count');
125
				if ( !empty( $all_tkt_count ) )
126
					$extra_stats[$site_pre . 'all_tkt_count'] = $all_tkt_count;
127
128
				$free_tkt_count = get_option('uxip_ee_free_tkt_count');
129
				if ( !empty( $free_tkt_count ) )
130
					$extra_stats[$site_pre . 'free_tkt_count'] = $free_tkt_count;
131
132
				$paid_tkt_count = get_option('uxip_ee_paid_tkt_count');
133
				if ( !empty( $paid_tkt_count ) )
134
					$extra_stats[$site_pre . 'paid_tkt_count'] = $paid_tkt_count;
135
136
				$tkt_sold = get_option('uxip_ee_tkt_sold' );
137
				if ( !empty($tkt_sold) )
138
					$extra_stats[$site_pre . 'tkt_sold'] = $tkt_sold;
139
140
				//phpversion checking
141
				$extra_stats['phpversion'] = function_exists('phpversion') ? phpversion() : 'unknown';
142
143
				//set transient
144
				set_transient( 'ee_extra_data', $extra_stats, WEEK_IN_SECONDS );
145
			}
146
		}
147
148
149
150
		// PUE Auto Upgrades stuff
151
		if (is_readable(EE_THIRD_PARTY . 'pue/pue-client.php')) { //include the file
152
			require_once(EE_THIRD_PARTY . 'pue/pue-client.php' );
153
154
			$api_key = isset( EE_Registry::instance()->NET_CFG->core->site_license_key ) ? EE_Registry::instance()->NET_CFG->core->site_license_key : '';
155
			$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.
156
157
			//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.
158
			//For example:
159
			//$plugin_slug['premium']['p'] = 'some-premium-slug';
160
			//$plugin_slug['prerelease']['pr'] = 'some-pre-release-slug';
161
			//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:
162
			//$plugin_slug['premium']['p'] = 'some-premium-slug';
163
			//$plugin_slug['prerelease']['b'] = 'some-pre-release-slug';
164
			//..WOULD work!
165
			$plugin_slug = array(
166
				'free' => array( 'decaf' => 'event-espresso-core-decaf' ),
167
				'premium' => array( 'p' => 'event-espresso-core-reg' ),
168
				'prerelease' => array( 'beta' => 'event-espresso-core-pr' )
169
				);
170
171
172
			//$options needs to be an array with the included keys as listed.
173
			$options = array(
174
			//	'optionName' => '', //(optional) - used as the reference for saving update information in the clients options table.  Will be automatically set if left blank.
175
				'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)
176
				'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.
177
				'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.
178
				'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.
179
				'options_page_slug' => 'espresso_general_settings',
180
				'plugin_basename' => EE_PLUGIN_BASENAME,
181
				'use_wp_update' => true, //if TRUE then you want FREE versions of the plugin to be updated from WP
182
				'extra_stats' => $extra_stats,
183
				'turn_on_notices_saved' => true
184
			);
185
			$check_for_updates = 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...
Unused Code introduced by
$check_for_updates is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
186
		}
187
	}
188
189
190
191
	/**
192
	 * The purpose of this function is to display information about Event Espresso data collection and a optin selection for extra data collecting by users.
193
	 * @return string html.
194
	 */
195
	 public static function espresso_data_collection_optin_text( $extra = TRUE ) {
196
	 	if ( ! $extra ) {
197
			 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>';
198
			 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>' );
199
		} else {
200
			$settings_url = EE_Admin_Page::add_query_args_and_nonce( array( 'action' => 'default'), admin_url( 'admin.php?page=espresso_general_settings') );
201
			$settings_url .= '#UXIP_settings';
202
			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/" traget="_blank">', '</a>', '<a href="' . $settings_url . '" target="_blank">', '</a>' );
203
		}
204
	}
205
206
207
208
209
	function espresso_data_collection_optin_notice() {
210
		$ueip_has_notified = EE_Registry::instance()->CFG->core->ee_ueip_has_notified;
211
		if ( $ueip_has_notified ) return;
212
		$settings_url = EE_Admin_Page::add_query_args_and_nonce( array( 'action' => 'default'), admin_url( 'admin.php?page=espresso_general_settings') );
213
		$settings_url = $settings_url . '#UXIP_settings';
0 ignored issues
show
Unused Code introduced by
$settings_url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
214
		?>
215
		<div class="updated data-collect-optin" id="espresso-data-collect-optin-container">
216
			<div id="data-collect-optin-options-container">
217
				<span class="dashicons dashicons-admin-site"></span>
218
				<span class="data-optin-text"><?php echo EE_PUE::espresso_data_collection_optin_text(); ?></span>
219
				<span style="display: none" id="data-optin-nonce"><?php echo wp_create_nonce('ee-data-optin'); ?></span>
220
				<button class="button-secondary data-optin-button" value="no"><?php _e('Dismiss', 'event_espresso'); ?></button>
221
				<!--<button class="button-primary data-optin-button" value="yes"><?php _e('Yes! I\'m In', 'event_espresso'); ?></button>-->
222
				<div style="clear:both"></div>
223
			</div>
224
		</div>
225
		<?php
226
	}
227
228
229
230
	/**
231
	 * enqueue scripts/styles needed for data collection optin
232
	 * @return void
233
	 */
234 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...
235
		wp_register_script( 'ee-data-optin-js', EE_GLOBAL_ASSETS_URL . 'scripts/ee-data-optin.js', array('jquery'), EVENT_ESPRESSO_VERSION, TRUE );
236
		wp_register_style( 'ee-data-optin-css', EE_GLOBAL_ASSETS_URL . 'css/ee-data-optin.css', array(), EVENT_ESPRESSO_VERSION );
237
238
		wp_enqueue_script('ee-data-optin-js');
239
		wp_enqueue_style('ee-data-optin-css');
240
	}
241
242
243
244
	/**
245
	 * This just handles the setting of the selected option for data optin via ajax
246
	 * @return void
247
	 */
248
	function espresso_data_optin_ajax_handler() {
249
250
		//verify nonce
251
		if ( isset($_POST['nonce']) && !wp_verify_nonce($_POST['nonce'], 'ee-data-optin') ) exit();
252
253
		//made it here so let's save the selection
254
		$ueip_optin = isset( $_POST['selection'] ) ? $_POST['selection'] : 'no';
0 ignored issues
show
Unused Code introduced by
$ueip_optin is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
255
256
		//update_option('ee_ueip_optin', $ueip_optin);
257
		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...
258
		EE_Registry::instance()->CFG->update_espresso_config( FALSE, FALSE );
259
		exit();
260
	}
261
262
263
264
	/**
265
	 * This is a handy helper method for retrieving whether there is an update available for the given plugin.
266
	 * @param  string  $basename Use the equivalent reulst from plugin_basename() for this param as WP uses that to identify plugins. Defaults to core update
267
	 * @return boolean           True if update available, false if not.
268
	 */
269
	public static function is_update_available($basename = '') {
270
271
		$basename = ! empty( $basename ) ? $basename : EE_PLUGIN_BASENAME;
272
273
		$update = false;
274
275
		$folder = DS . dirname($basename); // should take "event-espresso-core/espresso.php" and change to "/event-espresso-core"
276
277
		$plugins = get_plugins($folder);
278
		$current = get_site_transient( 'update_plugins' );
279
280
		foreach ( (array) $plugins as $plugin_file => $plugin_data ) {
281
			if ( isset( $current->response['plugin_file'] ) )
282
				$update = true;
283
		}
284
285
		//it's possible that there is an update but an invalid site-license-key is in use
286
		if ( get_site_option('pue_json_error_' . $basename ) )
287
			$update = true;
288
289
		return $update;
290
	}
291
292
293
	/**
294
	 * UXIP TRACKING *******
295
	 */
296
297
298
	/**
299
	 * This method contains all the hooks into EE for gathering stats that will be reported with the PUE uxip system
300
	 * @public
301
	 * @return void
302
	 */
303
	public function _uxip_hooks() {
304
		if ( EE_Maintenance_Mode::instance()->level() != EE_Maintenance_Mode::level_2_complete_maintenance ) {
305
			add_action('admin_init', array( $this, 'track_active_theme' ) );
306
			add_action('admin_init', array( $this, 'track_event_info' ) );
307
		}
308
	}
309
310
311
312
313
	public function track_active_theme() {
314
		//we only check this once a month.
315
		if ( false === ( $transient = get_transient( 'ee_active_theme_check' ) ) ) {
316
			$theme = wp_get_theme();
317
			update_option('uxip_ee_active_theme', $theme->get('Name') );
318
			set_transient('ee_active_theme_check', 1, MONTH_IN_SECONDS );
319
		}
320
	}
321
322
323
	public function track_event_info() {
324
		//we only check this once every couple weeks.
325
		if ( false === ( $transient = get_transient( 'ee4_event_info_check') ) ) {
326
			//first let's get the number for ALL events
327
			$EVT = EE_Registry::instance()->load_model('Event');
328
			$DTT = EE_Registry::instance()->load_model('Datetime');
329
			$TKT = EE_Registry::instance()->load_model('Ticket');
330
			$count = $EVT->count();
331
			if ( $count > 0 )
332
				update_option('uxip_ee4_all_events_count', $count);
333
334
			//next let's just get the number of ACTIVE events
335
			$count_active = $EVT->get_active_events(array(), TRUE);
0 ignored issues
show
Documentation Bug introduced by
The method get_active_events does not exist on object<EEM_Base>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
336
			if ( $count_active > 0 )
337
				update_option('uxip_ee4_active_events_count', $count_active);
338
339
			//datetimes!
340
			$dtt_count = $DTT->count();
341
			if ( $dtt_count > 0 )
342
				update_option( 'uxip_ee_all_dtts_count', $dtt_count );
343
344
345
			//dttsold
346
			$dtt_sold = $DTT->sum(array(), 'DTT_sold');
347
			if ( $dtt_sold > 0 )
348
				update_option( 'uxip_ee_dtt_sold', $dtt_sold );
349
350
			//allticketcount
351
			$all_tkt_count = $TKT->count();
352
			if ( $all_tkt_count > 0 )
353
				update_option( 'uxip_ee_all_tkt_count', $all_tkt_count );
354
355
			//freetktcount
356
			$_where = array( 'TKT_price' => 0 );
357
			$free_tkt_count = $TKT->count(array($_where));
358
			if ( $free_tkt_count > 0 )
359
				update_option( 'uxip_ee_free_tkt_count', $free_tkt_count );
360
361
			//paidtktcount
362
			$_where = array( 'TKT_price' => array('>', 0) );
363
			$paid_tkt_count = $TKT->count( array( $_where ) );
364
			if ( $paid_tkt_count > 0 )
365
				update_option( 'uxip_ee_paid_tkt_count', $paid_tkt_count );
366
367
			//tktsold
368
			$tkt_sold = $TKT->sum( array(), 'TKT_sold' );
369
			if( $tkt_sold > 0 )
370
				update_option( 'uxip_ee_tkt_sold', $tkt_sold );
371
372
373
			set_transient( 'ee4_event_info_check', 1, WEEK_IN_SECONDS * 2 );
374
		}
375
	}
376
377
}
378
// End of file EE_PUE.core.php
379
// Location: ./core/EE_PUE.core.php
380