Completed
Branch BUG/11475/decode-site-title-fo... (bbd86e)
by
unknown
13:39 queued 25s
created

EE_Admin::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
use EventEspresso\core\domain\entities\notifications\PersistentAdminNotice;
4
use EventEspresso\core\domain\services\admin\ExitModal;
5
use EventEspresso\core\exceptions\InvalidDataTypeException;
6
use EventEspresso\core\exceptions\InvalidInterfaceException;
7
use EventEspresso\core\interfaces\InterminableInterface;
8
use EventEspresso\core\services\container\exceptions\ServiceNotFoundException;
9
use EventEspresso\core\services\loaders\LoaderFactory;
10
use EventEspresso\core\services\notifications\PersistentAdminNoticeManager;
11
12
defined('EVENT_ESPRESSO_VERSION') || exit('No direct script access allowed');
13
14
15
16
/**
17
 * EE_Admin
18
 *
19
 * @package               Event Espresso
20
 * @subpackage            /core/admin/
21
 * @author                Brent Christensen
22
 */
23
final class EE_Admin implements InterminableInterface
24
{
25
26
    /**
27
     * @var EE_Admin $_instance
28
     */
29
    private static $_instance;
30
31
    /**
32
     * @var PersistentAdminNoticeManager $persistent_admin_notice_manager
33
     */
34
    private $persistent_admin_notice_manager;
35
36
    /**
37
     * @singleton method used to instantiate class object
38
     * @return EE_Admin
39
     * @throws EE_Error
40
     */
41
    public static function instance()
42
    {
43
        // check if class object is instantiated
44
        if (! self::$_instance instanceof EE_Admin) {
45
            self::$_instance = new self();
46
        }
47
        return self::$_instance;
48
    }
49
50
51
    /**
52
     * @return EE_Admin
53
     * @throws EE_Error
54
     */
55
    public static function reset()
56
    {
57
        self::$_instance = null;
58
        return self::instance();
59
    }
60
61
62
    /**
63
     * class constructor
64
     *
65
     * @throws EE_Error
66
     * @throws InvalidDataTypeException
67
     * @throws InvalidInterfaceException
68
     * @throws InvalidArgumentException
69
     */
70
    protected function __construct()
71
    {
72
        // define global EE_Admin constants
73
        $this->_define_all_constants();
74
        // set autoloaders for our admin page classes based on included path information
75
        EEH_Autoloader::instance()->register_autoloaders_for_each_file_in_folder(EE_ADMIN);
76
        // admin hooks
77
        add_filter('plugin_action_links', array($this, 'filter_plugin_actions'), 10, 2);
78
        // load EE_Request_Handler early
79
        add_action('AHEE__EE_System__core_loaded_and_ready', array($this, 'get_request'));
80
        add_action('AHEE__EE_System__initialize_last', array($this, 'init'));
81
        add_action('AHEE__EE_Admin_Page__route_admin_request', array($this, 'route_admin_request'), 100, 2);
82
        add_action('wp_loaded', array($this, 'wp_loaded'), 100);
83
        add_action('admin_init', array($this, 'admin_init'), 100);
84
        add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'), 20);
85
        add_action('admin_notices', array($this, 'display_admin_notices'), 10);
86
        add_action('network_admin_notices', array($this, 'display_admin_notices'), 10);
87
        add_filter('pre_update_option', array($this, 'check_for_invalid_datetime_formats'), 100, 2);
88
        add_filter('admin_footer_text', array($this, 'espresso_admin_footer'));
89
        add_action('load-plugins.php', array($this, 'hookIntoWpPluginsPage'));
90
        //reset Environment config (we only do this on admin page loads);
91
        EE_Registry::instance()->CFG->environment->recheck_values();
92
        do_action('AHEE__EE_Admin__loaded');
93
    }
94
95
96
97
    /**
98
     * _define_all_constants
99
     * define constants that are set globally for all admin pages
100
     *
101
     * @return void
102
     */
103 View Code Duplication
    private function _define_all_constants()
104
    {
105
        if (! defined('EE_ADMIN_URL')) {
106
            define('EE_ADMIN_URL', EE_PLUGIN_DIR_URL . 'core/admin/');
107
            define('EE_ADMIN_PAGES_URL', EE_PLUGIN_DIR_URL . 'admin_pages/');
108
            define('EE_ADMIN_TEMPLATE', EE_ADMIN . 'templates' . DS);
109
            define('WP_ADMIN_PATH', ABSPATH . 'wp-admin/');
110
            define('WP_AJAX_URL', admin_url('admin-ajax.php'));
111
        }
112
    }
113
114
115
    /**
116
     * filter_plugin_actions - adds links to the Plugins page listing
117
     *
118
     * @param    array  $links
119
     * @param    string $plugin
120
     * @return    array
121
     */
122
    public function filter_plugin_actions($links, $plugin)
123
    {
124
        // set $main_file in stone
125
        static $main_file;
126
        // if $main_file is not set yet
127
        if (! $main_file) {
128
            $main_file = plugin_basename(EVENT_ESPRESSO_MAIN_FILE);
129
        }
130
        if ($plugin === $main_file) {
131
            // compare current plugin to this one
132
            if (EE_Maintenance_Mode::instance()->level() === EE_Maintenance_Mode::level_2_complete_maintenance) {
133
                $maintenance_link = '<a href="admin.php?page=espresso_maintenance_settings"'
134
                                    . ' title="Event Espresso is in maintenance mode.  Click this link to learn why.">'
135
                                    . esc_html__('Maintenance Mode Active', 'event_espresso')
136
                                    . '</a>';
137
                array_unshift($links, $maintenance_link);
138
            } else {
139
                $org_settings_link = '<a href="admin.php?page=espresso_general_settings">'
140
                                     . esc_html__('Settings', 'event_espresso')
141
                                     . '</a>';
142
                $events_link       = '<a href="admin.php?page=espresso_events">'
143
                                     . esc_html__('Events', 'event_espresso')
144
                                     . '</a>';
145
                // add before other links
146
                array_unshift($links, $org_settings_link, $events_link);
147
            }
148
        }
149
        return $links;
150
    }
151
152
153
    /**
154
     * _get_request
155
     *
156
     * @return void
157
     * @throws EE_Error
158
     * @throws InvalidArgumentException
159
     * @throws InvalidDataTypeException
160
     * @throws InvalidInterfaceException
161
     * @throws ReflectionException
162
     */
163
    public function get_request()
164
    {
165
        EE_Registry::instance()->load_core('Request_Handler');
166
        EE_Registry::instance()->load_core('CPT_Strategy');
167
    }
168
169
170
171
    /**
172
     * hide_admin_pages_except_maintenance_mode
173
     *
174
     * @param array $admin_page_folder_names
175
     * @return array
176
     */
177
    public function hide_admin_pages_except_maintenance_mode($admin_page_folder_names = array())
178
    {
179
        return array(
180
            'maintenance' => EE_ADMIN_PAGES . 'maintenance' . DS,
181
            'about'       => EE_ADMIN_PAGES . 'about' . DS,
182
            'support'     => EE_ADMIN_PAGES . 'support' . DS,
183
        );
184
    }
185
186
187
188
    /**
189
     * init- should fire after shortcode, module,  addon, other plugin (default priority), and even
190
     * EE_Front_Controller's init phases have run
191
     *
192
     * @return void
193
     * @throws EE_Error
194
     * @throws InvalidArgumentException
195
     * @throws InvalidDataTypeException
196
     * @throws InvalidInterfaceException
197
     * @throws ReflectionException
198
     * @throws ServiceNotFoundException
199
     */
200
    public function init()
201
    {
202
        //only enable most of the EE_Admin IF we're not in full maintenance mode
203
        if (EE_Maintenance_Mode::instance()->models_can_query()) {
204
            //ok so we want to enable the entire admin
205
            $this->persistent_admin_notice_manager = LoaderFactory::getLoader()->getShared(
206
                'EventEspresso\core\services\notifications\PersistentAdminNoticeManager'
207
            );
208
            $this->persistent_admin_notice_manager->setReturnUrl(
209
                EE_Admin_Page::add_query_args_and_nonce(
210
                    array(
211
                        'page'   => EE_Registry::instance()->REQ->get('page', ''),
212
                        'action' => EE_Registry::instance()->REQ->get('action', ''),
213
                    ),
214
                    EE_ADMIN_URL
215
                )
216
            );
217
            $this->maybeSetDatetimeWarningNotice();
218
            //at a glance dashboard widget
219
            add_filter('dashboard_glance_items', array($this, 'dashboard_glance_items'), 10);
220
            //filter for get_edit_post_link used on comments for custom post types
221
            add_filter('get_edit_post_link', array($this, 'modify_edit_post_link'), 10, 2);
222
        }
223
        // run the admin page factory but ONLY if we are doing an ee admin ajax request
224
        if (! defined('DOING_AJAX') || EE_ADMIN_AJAX) {
225
            try {
226
                //this loads the controller for the admin pages which will setup routing etc
227
                EE_Registry::instance()->load_core('Admin_Page_Loader');
228
            } catch (EE_Error $e) {
229
                $e->get_error();
230
            }
231
        }
232
        add_filter('content_save_pre', array($this, 'its_eSpresso'), 10, 1);
233
        //make sure our CPTs and custom taxonomy metaboxes get shown for first time users
234
        add_action('admin_head', array($this, 'enable_hidden_ee_nav_menu_metaboxes'), 10);
235
        add_action('admin_head', array($this, 'register_custom_nav_menu_boxes'), 10);
236
        //exclude EE critical pages from all nav menus and wp_list_pages
237
        add_filter('nav_menu_meta_box_object', array($this, 'remove_pages_from_nav_menu'), 10);
238
    }
239
240
241
    /**
242
     *    get_persistent_admin_notices
243
     *
244
     * @access    public
245
     * @return void
246
     * @throws EE_Error
247
     * @throws InvalidArgumentException
248
     * @throws InvalidDataTypeException
249
     * @throws InvalidInterfaceException
250
     */
251
    public function maybeSetDatetimeWarningNotice()
252
    {
253
        //add dismissable notice for datetime changes.  Only valid if site does not have a timezone_string set.
254
        //@todo This needs to stay in core for a bit to catch anyone upgrading from a version without this to a version
255
        //with this.  But after enough time (indeterminate at this point) we can just remove this notice.
256
        //this was added with https://events.codebasehq.com/projects/event-espresso/tickets/10626
257
        if (apply_filters('FHEE__EE_Admin__maybeSetDatetimeWarningNotice', true)
258
            && ! get_option('timezone_string')
259
            && EEM_Event::instance()->count() > 0
260
        ) {
261
            new PersistentAdminNotice(
262
                'datetime_fix_notice',
263
                sprintf(
264
                    esc_html__(
265
                        '%1$sImportant announcement related to your install of Event Espresso%2$s: There are some changes made to your site that could affect how dates display for your events and other related items with dates and times.  Read more about it %3$shere%4$s. If your dates and times are displaying incorrectly (incorrect offset), you can fix it using the tool on %5$sthis page%4$s.',
266
                        'event_espresso'
267
                    ),
268
                    '<strong>',
269
                    '</strong>',
270
                    '<a href="https://eventespresso.com/2017/08/important-upcoming-changes-dates-times">',
271
                    '</a>',
272
                    '<a href="' . EE_Admin_Page::add_query_args_and_nonce(
273
                        array(
274
                            'page' => 'espresso_maintenance_settings',
275
                            'action' => 'datetime_tools'
276
                        ),
277
                        admin_url('admin.php')
278
                    ) . '">'
279
                ),
280
                false,
281
                'manage_options',
282
                'datetime_fix_persistent_notice'
283
            );
284
        }
285
    }
286
287
288
289
    /**
290
     * this simply hooks into the nav menu setup of pages metabox and makes sure that we remove EE critical pages from
291
     * the list of options. the wp function "wp_nav_menu_item_post_type_meta_box" found in
292
     * wp-admin/includes/nav-menu.php looks for the "_default_query" property on the post_type object and it uses that
293
     * to override any queries found in the existing query for the given post type.  Note that _default_query is not a
294
     * normal property on the post_type object.  It's found ONLY in this particular context.
295
     *
296
     * @param WP_Post $post_type WP post type object
297
     * @return WP_Post
298
     * @throws InvalidArgumentException
299
     * @throws InvalidDataTypeException
300
     * @throws InvalidInterfaceException
301
     */
302
    public function remove_pages_from_nav_menu($post_type)
303
    {
304
        //if this isn't the "pages" post type let's get out
305
        if ($post_type->name !== 'page') {
306
            return $post_type;
307
        }
308
        $critical_pages = EE_Registry::instance()->CFG->core->get_critical_pages_array();
309
        $post_type->_default_query = array(
310
            'post__not_in' => $critical_pages,
311
        );
312
        return $post_type;
313
    }
314
315
316
317
    /**
318
     * WP by default only shows three metaboxes in "nav-menus.php" for first times users.  We want to make sure our
319
     * metaboxes get shown as well
320
     *
321
     * @return void
322
     */
323
    public function enable_hidden_ee_nav_menu_metaboxes()
324
    {
325
        global $wp_meta_boxes, $pagenow;
326
        if (! is_array($wp_meta_boxes) || $pagenow !== 'nav-menus.php') {
327
            return;
328
        }
329
        $user = wp_get_current_user();
330
        //has this been done yet?
331
        if (get_user_option('ee_nav_menu_initialized', $user->ID)) {
332
            return;
333
        }
334
335
        $hidden_meta_boxes  = get_user_option('metaboxhidden_nav-menus', $user->ID);
336
        $initial_meta_boxes = apply_filters(
337
            'FHEE__EE_Admin__enable_hidden_ee_nav_menu_boxes__initial_meta_boxes',
338
            array(
339
                'nav-menu-theme-locations',
340
                'add-page',
341
                'add-custom-links',
342
                'add-category',
343
                'add-espresso_events',
344
                'add-espresso_venues',
345
                'add-espresso_event_categories',
346
                'add-espresso_venue_categories',
347
                'add-post-type-post',
348
                'add-post-type-page',
349
            )
350
        );
351
352
        if (is_array($hidden_meta_boxes)) {
353
            foreach ($hidden_meta_boxes as $key => $meta_box_id) {
354
                if (in_array($meta_box_id, $initial_meta_boxes, true)) {
355
                    unset($hidden_meta_boxes[$key]);
356
                }
357
            }
358
        }
359
        update_user_option($user->ID, 'metaboxhidden_nav-menus', $hidden_meta_boxes, true);
360
        update_user_option($user->ID, 'ee_nav_menu_initialized', 1, true);
361
    }
362
363
364
365
    /**
366
     * This method simply registers custom nav menu boxes for "nav_menus.php route"
367
     * Currently EE is using this to make sure there are menu options for our CPT archive page routes.
368
     *
369
     * @todo   modify this so its more dynamic and automatic for all ee CPTs and setups and can also be hooked into by
370
     *         addons etc.
371
     * @return void
372
     */
373
    public function register_custom_nav_menu_boxes()
374
    {
375
        add_meta_box(
376
            'add-extra-nav-menu-pages',
377
            esc_html__('Event Espresso Pages', 'event_espresso'),
378
            array($this, 'ee_cpt_archive_pages'),
379
            'nav-menus',
380
            'side',
381
            'core'
382
        );
383
    }
384
385
386
387
    /**
388
     * Use this to edit the post link for our cpts so that the edit link points to the correct page.
389
     *
390
     * @since   4.3.0
391
     * @param string $link the original link generated by wp
392
     * @param int    $id   post id
393
     * @return string  the (maybe) modified link
394
     */
395
    public function modify_edit_post_link($link, $id)
396
    {
397
        if (! $post = get_post($id)) {
398
            return $link;
399
        }
400
        if ($post->post_type === 'espresso_attendees') {
401
            $query_args = array(
402
                'action' => 'edit_attendee',
403
                'post'   => $id,
404
            );
405
            return EEH_URL::add_query_args_and_nonce(
406
                $query_args,
407
                admin_url('admin.php?page=espresso_registrations')
408
            );
409
        }
410
        return $link;
411
    }
412
413
414
415
    public function ee_cpt_archive_pages()
416
    {
417
        global $nav_menu_selected_id;
418
        $db_fields   = false;
419
        $walker      = new Walker_Nav_Menu_Checklist($db_fields);
420
        $current_tab = 'event-archives';
421
        $removed_args = array(
422
            'action',
423
            'customlink-tab',
424
            'edit-menu-item',
425
            'menu-item',
426
            'page-tab',
427
            '_wpnonce',
428
        );
429
        ?>
430
        <div id="posttype-extra-nav-menu-pages" class="posttypediv">
431
            <ul id="posttype-extra-nav-menu-pages-tabs" class="posttype-tabs add-menu-item-tabs">
432
                <li <?php echo('event-archives' === $current_tab ? ' class="tabs"' : ''); ?>>
433
                    <a class="nav-tab-link" data-type="tabs-panel-posttype-extra-nav-menu-pages-event-archives"
434
                       href="<?php if ($nav_menu_selected_id) {
435
                            echo esc_url(
436
                                add_query_arg(
437
                                    'extra-nav-menu-pages-tab',
438
                                    'event-archives',
439
                                    remove_query_arg($removed_args)
440
                                )
441
                            );
442
                       } ?>#tabs-panel-posttype-extra-nav-menu-pages-event-archives">
443
                        <?php _e('Event Archive Pages', 'event_espresso'); ?>
444
                    </a>
445
                </li>
446
            </ul><!-- .posttype-tabs -->
447
448
            <div id="tabs-panel-posttype-extra-nav-menu-pages-event-archives" class="tabs-panel <?php
449
                echo('event-archives' === $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive');
450
                ?>">
451
                    <ul id="extra-nav-menu-pageschecklist-event-archives" class="categorychecklist form-no-clear">
452
                        <?php
453
                        $pages          = $this->_get_extra_nav_menu_pages_items();
454
                        $args['walker'] = $walker;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$args was never initialized. Although not strictly required by PHP, it is generally a good practice to add $args = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
455
                        echo walk_nav_menu_tree(
456
                            array_map(
457
                                array($this, '_setup_extra_nav_menu_pages_items'),
458
                                $pages
459
                            ),
460
                            0,
461
                            (object) $args
462
                        );
463
                        ?>
464
                    </ul>
465
                </div><!-- /.tabs-panel -->
466
467
                <p class="button-controls">
468
                <span class="list-controls">
469
                    <a href="<?php
470
                    echo esc_url(add_query_arg(
471
                        array(
472
                            'extra-nav-menu-pages-tab' => 'event-archives',
473
                            'selectall'                => 1,
474
                        ),
475
                        remove_query_arg($removed_args)
476
                    ));
477
                    ?>#posttype-extra-nav-menu-pages>" class="select-all"><?php _e('Select All'); ?></a>
478
                </span>
479
                <span class="add-to-menu">
480
                    <input type="submit"<?php wp_nav_menu_disabled_check($nav_menu_selected_id); ?>
481
                       class="button-secondary submit-add-to-menu right"
482
                       value="<?php esc_attr_e(__('Add to Menu')); ?>" name="add-post-type-menu-item"
483
                       id="<?php esc_attr_e('submit-posttype-extra-nav-menu-pages'); ?>"/>
484
                    <span class="spinner"></span>
485
                </span>
486
                </p>
487
488
        </div><!-- /.posttypediv -->
489
        <?php
490
    }
491
492
493
    /**
494
     * Returns an array of event archive nav items.
495
     *
496
     * @todo  for now this method is just in place so when it gets abstracted further we can substitute in whatever
497
     *        method we use for getting the extra nav menu items
498
     * @return array
499
     */
500
    private function _get_extra_nav_menu_pages_items()
501
    {
502
        $menuitems[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$menuitems was never initialized. Although not strictly required by PHP, it is generally a good practice to add $menuitems = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
503
            'title'       => esc_html__('Event List', 'event_espresso'),
504
            'url'         => get_post_type_archive_link('espresso_events'),
505
            'description' => esc_html__('Archive page for all events.', 'event_espresso'),
506
        );
507
        return apply_filters('FHEE__EE_Admin__get_extra_nav_menu_pages_items', $menuitems);
508
    }
509
510
511
    /**
512
     * Setup nav menu walker item for usage in the event archive nav menu metabox.  It receives a menu_item array with
513
     * the properties and converts it to the menu item object.
514
     *
515
     * @see wp_setup_nav_menu_item() in wp-includes/nav-menu.php
516
     * @param $menu_item_values
517
     * @return stdClass
518
     */
519
    private function _setup_extra_nav_menu_pages_items($menu_item_values)
520
    {
521
        $menu_item = new stdClass();
522
        $keys      = array(
523
            'ID'               => 0,
524
            'db_id'            => 0,
525
            'menu_item_parent' => 0,
526
            'object_id'        => -1,
527
            'post_parent'      => 0,
528
            'type'             => 'custom',
529
            'object'           => '',
530
            'type_label'       => esc_html__('Extra Nav Menu Item', 'event_espresso'),
531
            'title'            => '',
532
            'url'              => '',
533
            'target'           => '',
534
            'attr_title'       => '',
535
            'description'      => '',
536
            'classes'          => array(),
537
            'xfn'              => '',
538
        );
539
540
        foreach ($keys as $key => $value) {
541
            $menu_item->{$key} = isset($menu_item_values[$key]) ? $menu_item_values[$key] : $value;
542
        }
543
        return $menu_item;
544
    }
545
546
547
    /**
548
     * This is the action hook for the AHEE__EE_Admin_Page__route_admin_request hook that fires off right before an
549
     * EE_Admin_Page route is called.
550
     *
551
     * @return void
552
     */
553
    public function route_admin_request()
554
    {
555
    }
556
557
558
    /**
559
     * wp_loaded should fire on the WordPress wp_loaded hook.  This fires on a VERY late priority.
560
     *
561
     * @return void
562
     */
563
    public function wp_loaded()
564
    {
565
    }
566
567
568
    /**
569
     * admin_init
570
     *
571
     * @return void
572
     * @throws EE_Error
573
     * @throws InvalidArgumentException
574
     * @throws InvalidDataTypeException
575
     * @throws InvalidInterfaceException
576
     * @throws ReflectionException
577
     */
578
    public function admin_init()
579
    {
580
581
        /**
582
         * our cpt models must be instantiated on WordPress post processing routes (wp-admin/post.php),
583
         * so any hooking into core WP routes is taken care of.  So in this next few lines of code:
584
         * - check if doing post processing.
585
         * - check if doing post processing of one of EE CPTs
586
         * - instantiate the corresponding EE CPT model for the post_type being processed.
587
         */
588
        if (isset($_POST['action'], $_POST['post_type']) && $_POST['action'] === 'editpost') {
589
            EE_Registry::instance()->load_core('Register_CPTs');
590
            EE_Register_CPTs::instantiate_cpt_models($_POST['post_type']);
591
        }
592
593
594
        /**
595
         * This code excludes EE critical pages anywhere `wp_dropdown_pages` is used to create a dropdown for selecting
596
         * critical pages.  The only place critical pages need included in a generated dropdown is on the "Critical
597
         * Pages" tab in the EE General Settings Admin page.
598
         * This is for user-proofing.
599
         */
600
        add_filter('wp_dropdown_pages', array($this, 'modify_dropdown_pages'));
601
    }
602
603
604
    /**
605
     * Callback for wp_dropdown_pages hook to remove ee critical pages from the dropdown selection.
606
     *
607
     * @param string $output Current output.
608
     * @return string
609
     * @throws InvalidArgumentException
610
     * @throws InvalidDataTypeException
611
     * @throws InvalidInterfaceException
612
     */
613
    public function modify_dropdown_pages($output)
614
    {
615
        //get critical pages
616
        $critical_pages = EE_Registry::instance()->CFG->core->get_critical_pages_array();
617
618
        //split current output by line break for easier parsing.
619
        $split_output = explode("\n", $output);
620
621
        //loop through to remove any critical pages from the array.
622
        foreach ($critical_pages as $page_id) {
623
            $needle = 'value="' . $page_id . '"';
624
            foreach ($split_output as $key => $haystack) {
625
                if (strpos($haystack, $needle) !== false) {
626
                    unset($split_output[$key]);
627
                }
628
            }
629
        }
630
        //replace output with the new contents
631
        return implode("\n", $split_output);
632
    }
633
634
635
    /**
636
     * enqueue all admin scripts that need loaded for admin pages
637
     *
638
     * @return void
639
     */
640
    public function enqueue_admin_scripts()
641
    {
642
        // this javascript is loaded on every admin page to catch any injections ee needs to add to wp run js.
643
        // Note: the intention of this script is to only do TARGETED injections.  I.E, only injecting on certain script
644
        // calls.
645
        wp_enqueue_script(
646
            'ee-inject-wp',
647
            EE_ADMIN_URL . 'assets/ee-cpt-wp-injects.js',
648
            array('jquery'),
649
            EVENT_ESPRESSO_VERSION,
650
            true
651
        );
652
        // register cookie script for future dependencies
653
        wp_register_script(
654
            'jquery-cookie',
655
            EE_THIRD_PARTY_URL . 'joyride/jquery.cookie.js',
656
            array('jquery'),
657
            '2.1',
658
            true
659
        );
660
        //joyride is turned OFF by default, but prior to the admin_enqueue_scripts hook, can be turned back on again
661
        // via: add_filter('FHEE_load_joyride', '__return_true' );
662
        if (apply_filters('FHEE_load_joyride', false)) {
663
            //joyride style
664
            wp_register_style('joyride-css', EE_THIRD_PARTY_URL . 'joyride/joyride-2.1.css', array(), '2.1');
665
            wp_register_style(
666
                'ee-joyride-css',
667
                EE_GLOBAL_ASSETS_URL . 'css/ee-joyride-styles.css',
668
                array('joyride-css'),
669
                EVENT_ESPRESSO_VERSION
670
            );
671
            wp_register_script(
672
                'joyride-modernizr',
673
                EE_THIRD_PARTY_URL . 'joyride/modernizr.mq.js',
674
                array(),
675
                '2.1',
676
                true
677
            );
678
            //joyride JS
679
            wp_register_script(
680
                'jquery-joyride',
681
                EE_THIRD_PARTY_URL . 'joyride/jquery.joyride-2.1.js',
682
                array('jquery-cookie', 'joyride-modernizr'),
683
                '2.1',
684
                true
685
            );
686
            // wanna go for a joyride?
687
            wp_enqueue_style('ee-joyride-css');
688
            wp_enqueue_script('jquery-joyride');
689
        }
690
    }
691
692
693
    /**
694
     * display_admin_notices
695
     *
696
     * @return void
697
     */
698
    public function display_admin_notices()
699
    {
700
        echo EE_Error::get_notices();
701
    }
702
703
704
705
    /**
706
     * @param array $elements
707
     * @return array
708
     * @throws EE_Error
709
     * @throws InvalidArgumentException
710
     * @throws InvalidDataTypeException
711
     * @throws InvalidInterfaceException
712
     */
713
    public function dashboard_glance_items($elements)
714
    {
715
        $elements                        = is_array($elements) ? $elements : array($elements);
716
        $events                          = EEM_Event::instance()->count();
717
        $items['events']['url']          = EE_Admin_Page::add_query_args_and_nonce(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$items was never initialized. Although not strictly required by PHP, it is generally a good practice to add $items = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
718
            array('page' => 'espresso_events'),
719
            admin_url('admin.php')
720
        );
721
        $items['events']['text']         = sprintf(_n('%s Event', '%s Events', $events), number_format_i18n($events));
722
        $items['events']['title']        = esc_html__('Click to view all Events', 'event_espresso');
723
        $registrations                   = EEM_Registration::instance()->count(
724
            array(
725
                array(
726
                    'STS_ID' => array('!=', EEM_Registration::status_id_incomplete),
727
                ),
728
            )
729
        );
730
        $items['registrations']['url']   = EE_Admin_Page::add_query_args_and_nonce(
731
            array('page' => 'espresso_registrations'),
732
            admin_url('admin.php')
733
        );
734
        $items['registrations']['text']  = sprintf(
735
            _n('%s Registration', '%s Registrations', $registrations),
736
            number_format_i18n($registrations)
737
        );
738
        $items['registrations']['title'] = esc_html__('Click to view all registrations', 'event_espresso');
739
740
        $items = (array)apply_filters('FHEE__EE_Admin__dashboard_glance_items__items', $items);
741
742
        foreach ($items as $type => $item_properties) {
743
            $elements[] = sprintf(
744
                '<a class="ee-dashboard-link-' . $type . '" href="%s" title="%s">%s</a>',
745
                $item_properties['url'],
746
                $item_properties['title'],
747
                $item_properties['text']
748
            );
749
        }
750
        return $elements;
751
    }
752
753
754
    /**
755
     * check_for_invalid_datetime_formats
756
     * if an admin changes their date or time format settings on the WP General Settings admin page, verify that
757
     * their selected format can be parsed by PHP
758
     *
759
     * @param    $value
760
     * @param    $option
761
     * @throws EE_Error
762
     * @return    string
763
     */
764
    public function check_for_invalid_datetime_formats($value, $option)
765
    {
766
        // check for date_format or time_format
767
        switch ($option) {
768
            case 'date_format':
769
                $date_time_format = $value . ' ' . get_option('time_format');
770
                break;
771
            case 'time_format':
772
                $date_time_format = get_option('date_format') . ' ' . $value;
773
                break;
774
            default:
775
                $date_time_format = false;
776
        }
777
        // do we have a date_time format to check ?
778
        if ($date_time_format) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $date_time_format of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
779
            $error_msg = EEH_DTT_Helper::validate_format_string($date_time_format);
780
781
            if (is_array($error_msg)) {
782
                $msg = '<p>'
783
                       . sprintf(
784
                           esc_html__(
785
                               'The following date time "%s" ( %s ) is difficult to be properly parsed by PHP for the following reasons:',
786
                               'event_espresso'
787
                           ),
788
                           date($date_time_format),
789
                           $date_time_format
790
                       )
791
                       . '</p><p><ul>';
792
793
794
                foreach ($error_msg as $error) {
795
                    $msg .= '<li>' . $error . '</li>';
796
                }
797
798
                $msg .= '</ul></p><p>'
799
                        . sprintf(
800
                            esc_html__(
801
                                '%sPlease note that your date and time formats have been reset to "F j, Y" and "g:i a" respectively.%s',
802
                                'event_espresso'
803
                            ),
804
                            '<span style="color:#D54E21;">',
805
                            '</span>'
806
                        )
807
                        . '</p>';
808
809
                // trigger WP settings error
810
                add_settings_error(
811
                    'date_format',
812
                    'date_format',
813
                    $msg
814
                );
815
816
                // set format to something valid
817
                switch ($option) {
818
                    case 'date_format':
819
                        $value = 'F j, Y';
820
                        break;
821
                    case 'time_format':
822
                        $value = 'g:i a';
823
                        break;
824
                }
825
            }
826
        }
827
        return $value;
828
    }
829
830
831
    /**
832
     * its_eSpresso - converts the less commonly used spelling of "Expresso" to "Espresso"
833
     *
834
     * @param $content
835
     * @return    string
836
     */
837
    public function its_eSpresso($content)
838
    {
839
        return str_replace('[EXPRESSO_', '[ESPRESSO_', $content);
840
    }
841
842
843
    /**
844
     * espresso_admin_footer
845
     *
846
     * @return    string
847
     */
848
    public function espresso_admin_footer()
849
    {
850
        return \EEH_Template::powered_by_event_espresso('aln-cntr', '', array('utm_content' => 'admin_footer'));
851
    }
852
853
854
    /**
855
     * static method for registering ee admin page.
856
     * This method is deprecated in favor of the new location in EE_Register_Admin_Page::register.
857
     *
858
     * @since      4.3.0
859
     * @deprecated 4.3.0    Use EE_Register_Admin_Page::register() instead
860
     * @see        EE_Register_Admin_Page::register()
861
     * @param       $page_basename
862
     * @param       $page_path
863
     * @param array $config
864
     * @return void
865
     * @throws EE_Error
866
     */
867
    public static function register_ee_admin_page($page_basename, $page_path, $config = array())
868
    {
869
        EE_Error::doing_it_wrong(
870
            __METHOD__,
871
            sprintf(
872
                esc_html__(
873
                    'Usage is deprecated.  Use EE_Register_Admin_Page::register() for registering the %s admin page.',
874
                    'event_espresso'
875
                ),
876
                $page_basename
877
            ),
878
            '4.3'
879
        );
880
        if (class_exists('EE_Register_Admin_Page')) {
881
            $config['page_path'] = $page_path;
882
        }
883
        EE_Register_Admin_Page::register($page_basename, $config);
884
    }
885
886
887
    /**
888
     * @deprecated 4.8.41
889
     * @param  int      $post_ID
890
     * @param  \WP_Post $post
891
     * @return void
892
     */
893
    public static function parse_post_content_on_save($post_ID, $post)
894
    {
895
        EE_Error::doing_it_wrong(
896
            __METHOD__,
897
            esc_html__('Usage is deprecated', 'event_espresso'),
898
            '4.8.41'
899
        );
900
    }
901
902
903
    /**
904
     * @deprecated 4.8.41
905
     * @param  $option
906
     * @param  $old_value
907
     * @param  $value
908
     * @return void
909
     */
910
    public function reset_page_for_posts_on_change($option, $old_value, $value)
911
    {
912
        EE_Error::doing_it_wrong(
913
            __METHOD__,
914
            esc_html__('Usage is deprecated', 'event_espresso'),
915
            '4.8.41'
916
        );
917
    }
918
919
920
921
    /**
922
     * @deprecated 4.9.27
923
     * @return void
924
     */
925
    public function get_persistent_admin_notices()
926
    {
927
        EE_Error::doing_it_wrong(
928
            __METHOD__,
929
            sprintf(
930
                __('Usage is deprecated. Use "%1$s" instead.', 'event_espresso'),
931
                '\EventEspresso\core\services\notifications\PersistentAdminNoticeManager'
932
            ),
933
            '4.9.27'
934
        );
935
    }
936
937
938
939
    /**
940
     * @deprecated 4.9.27
941
     * @throws InvalidInterfaceException
942
     * @throws InvalidDataTypeException
943
     * @throws DomainException
944
     */
945
    public function dismiss_ee_nag_notice_callback()
946
    {
947
        EE_Error::doing_it_wrong(
948
            __METHOD__,
949
            sprintf(
950
                __('Usage is deprecated. Use "%1$s" instead.', 'event_espresso'),
951
                '\EventEspresso\core\services\notifications\PersistentAdminNoticeManager'
952
            ),
953
            '4.9.27'
954
        );
955
        $this->persistent_admin_notice_manager->dismissNotice();
956
    }
957
958
959
    /**
960
     * Callback on load-plugins.php hook for setting up anything hooking into the wp plugins page.
961
     * @throws InvalidArgumentException
962
     * @throws InvalidDataTypeException
963
     * @throws InvalidInterfaceException
964
     */
965
    public function hookIntoWpPluginsPage()
966
    {
967
        LoaderFactory::getLoader()->getShared('EventEspresso\core\domain\services\admin\ExitModal');
968
        LoaderFactory::getLoader()
969
                     ->getShared('EventEspresso\core\domain\services\admin\PluginUpsells')
970
                     ->decafUpsells();
971
    }
972
}
973