Completed
Branch BUG-10911-php-7.2 (bb4630)
by
unknown
44:45 queued 33:11
created

EE_PUE::track_event_info()   D

Complexity

Conditions 10
Paths 257

Size

Total Lines 63
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 32
nc 257
nop 0
dl 0
loc 63
rs 4.6153
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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