Completed
Branch FET-10896-improvements-to-floa... (000e49)
by
unknown
163:03 queued 150:10
created

EED_Ticket_Selector   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 436
Duplicated Lines 4.13 %

Coupling/Cohesion

Components 3
Dependencies 10

Importance

Changes 0
Metric Value
dl 18
loc 436
rs 8.8
c 0
b 0
f 0
wmc 36
lcom 3
cbo 10

27 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 3 1
A set_config() 0 5 1
A set_hooks() 0 11 1
A set_hooks_admin() 0 19 1
A set_definitions() 4 16 3
A ticketSelector() 0 7 2
A run() 0 1 1
A getIframeEmbedButton() 0 6 2
A ticket_selector_iframe_embed_button() 0 4 1
A ticket_selector_iframe() 0 4 1
A display_ticket_selector() 0 3 1
A process_ticket_selections() 0 4 1
A cancel_ticket_selections() 0 5 1
A translate_js_strings() 0 5 1
A load_tckt_slctr_assets() 0 23 2
A loadIframeAssets() 0 21 1
A iframeCss() 0 5 1
A iframeJs() 0 5 1
A display_view_details_btn() 0 5 1
A display_ticket_selector_submit() 0 5 1
A iframe_code_button() 7 12 3
A ticket_selector_form_open() 0 5 1
A ticket_selector_form_close() 0 5 1
A no_tkt_slctr_end_dv() 0 5 1
A tkt_slctr_end_dv() 0 4 1
A clear_tkt_slctr() 0 4 1
A load_tckt_slctr_assets_admin() 7 11 3

How to fix   Duplicated Code   

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:

1
<?php use EventEspresso\modules\ticket_selector\DisplayTicketSelector;
2
use EventEspresso\modules\ticket_selector\ProcessTicketSelector;
3
use EventEspresso\modules\ticket_selector\TicketSelectorIframe;
4
use EventEspresso\modules\ticket_selector\TicketSelectorIframeEmbedButton;
5
6
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) {
7
	exit( 'No direct script access allowed' );
8
}
9
10
11
12
/**
13
 * ------------------------------------------------------------------------
14
 *
15
 * Ticket Selector  class
16
 *
17
 * @package		Event Espresso
18
 * @subpackage	includes/classes/EE_Ticket_Selector.class.php
19
 * @author		Brent Christensen
20
 *
21
 * ------------------------------------------------------------------------
22
 */
23
class EED_Ticket_Selector extends  EED_Module {
24
25
    /**
26
     * @var EventEspresso\modules\ticket_selector\DisplayTicketSelector $ticket_selector
27
     */
28
    private static $ticket_selector;
29
30
    /**
31
     * @var EventEspresso\modules\ticket_selector\TicketSelectorIframeEmbedButton $iframe_embed_button
32
     */
33
    private static $iframe_embed_button;
34
35
36
37
	/**
38
	 * @return EED_Ticket_Selector
39
	 */
40
	public static function instance() {
41
		return parent::get_instance( __CLASS__ );
42
	}
43
44
45
46
	protected function set_config(){
47
		$this->set_config_section( 'template_settings' );
48
		$this->set_config_class( 'EE_Ticket_Selector_Config' );
49
		$this->set_config_name( 'EED_Ticket_Selector' );
50
	}
51
52
53
54
	/**
55
	 * 	set_hooks - for hooking into EE Core, other modules, etc
56
	 *
57
	 *  @access 	public
58
	 *  @return 	void
59
	 */
60
	public static function set_hooks() {
61
		// routing
62
		EE_Config::register_route( 'iframe', 'EED_Ticket_Selector', 'ticket_selector_iframe', 'ticket_selector' );
63
		EE_Config::register_route( 'process_ticket_selections', 'EED_Ticket_Selector', 'process_ticket_selections' );
64
        EE_Config::register_route('cancel_ticket_selections', 'EED_Ticket_Selector', 'cancel_ticket_selections');
65
        add_action( 'wp_loaded', array( 'EED_Ticket_Selector', 'set_definitions' ), 2 );
66
		add_action( 'AHEE_event_details_header_bottom', array( 'EED_Ticket_Selector', 'display_ticket_selector' ), 10, 1 );
67
        add_action( 'wp_enqueue_scripts', array( 'EED_Ticket_Selector', 'translate_js_strings' ), 0 );
68
        add_action( 'wp_enqueue_scripts', array( 'EED_Ticket_Selector', 'load_tckt_slctr_assets' ), 10 );
69
        EED_Ticket_Selector::loadIframeAssets();
70
    }
71
72
73
74
	/**
75
	 * 	set_hooks_admin - for hooking into EE Admin Core, other modules, etc
76
	 *
77
	 *  @access 	public
78
	 *  @return 	void
79
	 */
80
	public static function set_hooks_admin() {
81
		// hook into the end of the \EE_Admin_Page::_load_page_dependencies()
82
		// to load assets for "espresso_events" page on the "edit" route (action)
83
		add_action(
84
			'FHEE__EE_Admin_Page___load_page_dependencies__after_load__espresso_events__edit',
85
			array( 'EED_Ticket_Selector', 'ticket_selector_iframe_embed_button' ),
86
			10
87
		);
88
89
        /**
90
         * Make sure assets for the ticket selector are loaded on the espresso registrations route so  admin side
91
         * registrations work.
92
         */
93
		add_action(
94
		    'FHEE__EE_Admin_Page___load_page_dependencies__after_load__espresso_registrations__new_registration',
95
            array('EED_Ticket_Selector', 'set_definitions'),
96
            10
97
        );
98
    }
99
100
101
102
	/**
103
	 * 	set_definitions
104
	 *
105
	 *  @access 	public
106
	 *  @return 	void
107
	 */
108
	public static function set_definitions() {
109
	    // don't do this twice
110
	    if(defined('TICKET_SELECTOR_ASSETS_URL')) {
111
	        return;
112
        }
113
		define( 'TICKET_SELECTOR_ASSETS_URL', plugin_dir_url( __FILE__ ) . 'assets' . DS );
114
		define(
115
		    'TICKET_SELECTOR_TEMPLATES_PATH',
116
            str_replace( '\\', DS, plugin_dir_path( __FILE__ )) . 'templates' . DS
117
        );
118
		//if config is not set, initialize
119 View Code Duplication
		if ( ! EE_Registry::instance()->CFG->template_settings->EED_Ticket_Selector instanceof EE_Ticket_Selector_Config ) {
120
            \EED_Ticket_Selector::instance()->set_config();
121
            \EE_Registry::instance()->CFG->template_settings->EED_Ticket_Selector = \EED_Ticket_Selector::instance()->config();
122
		}
123
	}
124
125
126
127
	/**
128
     * @return \EventEspresso\modules\ticket_selector\DisplayTicketSelector
129
     */
130
    public static function ticketSelector()
131
    {
132
        if ( ! EED_Ticket_Selector::$ticket_selector instanceof DisplayTicketSelector) {
133
            EED_Ticket_Selector::$ticket_selector = new DisplayTicketSelector();
134
        }
135
        return EED_Ticket_Selector::$ticket_selector;
136
    }
137
138
139
	/**
140
	 * 	gets the ball rolling
141
	 *
142
	 *	@access public
143
	 * 	@param	WP $WP
144
	 * 	@return void
145
	 */
146
	public function run( $WP ) {}
147
148
149
150
	/**
151
	 * @return \EventEspresso\modules\ticket_selector\TicketSelectorIframeEmbedButton
152
	 */
153
	public static function getIframeEmbedButton() {
154
		if ( ! self::$iframe_embed_button instanceof TicketSelectorIframeEmbedButton ) {
155
			self::$iframe_embed_button = new TicketSelectorIframeEmbedButton();
156
		}
157
		return self::$iframe_embed_button;
158
	}
159
160
161
162
	/**
163
	 * ticket_selector_iframe_embed_button
164
	 *
165
	 * @return    void
166
	 * @throws \EE_Error
167
	 */
168
	public static function ticket_selector_iframe_embed_button() {
169
		$iframe_embed_button = \EED_Ticket_Selector::getIframeEmbedButton();
170
		$iframe_embed_button->addEventEditorIframeEmbedButton();
171
	}
172
173
174
175
	/**
176
	 * ticket_selector_iframe
177
	 *
178
	 * @return    void
179
	 * @throws \DomainException
180
	 * @throws \EE_Error
181
	 */
182
	public function ticket_selector_iframe() {
183
		$ticket_selector_iframe = new TicketSelectorIframe();
184
		$ticket_selector_iframe->display();
185
	}
186
187
188
189
    /**
190
     *    creates buttons for selecting number of attendees for an event
191
     *
192
     * @access    public
193
     * @param    WP_Post|int $event
194
     * @param    bool        $view_details
195
     * @return    string
196
     * @throws \EE_Error
197
     */
198
	public static function display_ticket_selector( $event = NULL, $view_details = FALSE ) {
199
		return EED_Ticket_Selector::ticketSelector()->display( $event, $view_details );
200
	}
201
202
203
204
	/**
205
	 * process_ticket_selections
206
	 *
207
	 * @access        public
208
	 * @return        array  or FALSE
209
	 * @throws \EE_Error
210
	 */
211
	public function process_ticket_selections() {
212
		$form = new ProcessTicketSelector();
213
		return $form->processTicketSelections();
214
	}
215
216
217
218
    /**
219
     * cancel_ticket_selections
220
     *
221
     * @access        public
222
     * @return        string
223
     */
224
    public static function cancel_ticket_selections()
225
    {
226
        $form = new ProcessTicketSelector();
227
        return $form->cancelTicketSelections();
228
    }
229
230
231
232
	/**
233
	* @return void
234
	*/
235
	public static function translate_js_strings() {
236
        EE_Registry::$i18n_js_strings['please_select_date_filter_notice'] = esc_html__(
237
            'please select a datetime', 'event_espresso'
238
        );
239
    }
240
241
242
243
	/**
244
	* 	load js
245
	*
246
	* 	@access 		public
247
	* 	@return 		void
248
	*/
249
	public static function load_tckt_slctr_assets() {
250
		if ( apply_filters( 'FHEE__EED_Ticket_Selector__load_tckt_slctr_assets', FALSE ) ) {
251
            // add some style
252
            wp_register_style(
253
                'ticket_selector',
254
                TICKET_SELECTOR_ASSETS_URL . 'ticket_selector.css',
255
                array(),
256
                EVENT_ESPRESSO_VERSION
257
            );
258
            wp_enqueue_style('ticket_selector');
259
            // make it dance
260
            wp_register_script(
261
                'ticket_selector',
262
                TICKET_SELECTOR_ASSETS_URL . 'ticket_selector.js',
263
                array('espresso_core'),
264
                EVENT_ESPRESSO_VERSION,
265
                TRUE
266
            );
267
			wp_enqueue_script('ticket_selector');
268
            require_once( EE_LIBRARIES.'form_sections/strategies/display/EE_Checkbox_Dropdown_Selector_Display_Strategy.strategy.php');
269
            \EE_Checkbox_Dropdown_Selector_Display_Strategy::enqueue_styles_and_scripts();
270
        }
271
	}
272
273
274
275
    /**
276
     * @return void
277
     */
278
    public static function loadIframeAssets()
279
    {
280
        // for event lists
281
        add_filter(
282
            'FHEE__EventEspresso_modules_events_archive_EventsArchiveIframe__display__css',
283
            array('EED_Ticket_Selector', 'iframeCss')
284
        );
285
        add_filter(
286
            'FHEE__EventEspresso_modules_events_archive_EventsArchiveIframe__display__js',
287
            array('EED_Ticket_Selector', 'iframeJs')
288
        );
289
        // for ticket selectors
290
        add_filter(
291
            'FHEE__EED_Ticket_Selector__ticket_selector_iframe__css',
292
            array('EED_Ticket_Selector', 'iframeCss')
293
        );
294
        add_filter(
295
            'FHEE__EED_Ticket_Selector__ticket_selector_iframe__js',
296
            array('EED_Ticket_Selector', 'iframeJs')
297
        );
298
    }
299
300
301
302
    /**
303
     * Informs the rest of the forms system what CSS and JS is needed to display the input
304
     *
305
     * @param array $iframe_css
306
     * @return array
307
     */
308
    public static function iframeCss(array $iframe_css)
309
    {
310
        $iframe_css['ticket_selector'] = TICKET_SELECTOR_ASSETS_URL . 'ticket_selector.css';
311
        return $iframe_css;
312
    }
313
314
315
316
    /**
317
     * Informs the rest of the forms system what CSS and JS is needed to display the input
318
     *
319
     * @param array $iframe_js
320
     * @return array
321
     */
322
    public static function iframeJs(array $iframe_js)
323
    {
324
        $iframe_js['ticket_selector'] = TICKET_SELECTOR_ASSETS_URL . 'ticket_selector.js';
325
        return $iframe_js;
326
    }
327
328
329
	/****************************** DEPRECATED ******************************/
330
331
332
333
    /**
334
     * @deprecated
335
     * @return string
336
     * @throws \EE_Error
337
     */
338
    public static function display_view_details_btn()
339
    {
340
        // todo add doing_it_wrong() notice during next major version
341
        return EED_Ticket_Selector::ticketSelector()->displayViewDetailsButton();
342
    }
343
344
345
346
    /**
347
     * @deprecated
348
     * @return string
349
     * @throws \EE_Error
350
     */
351
    public static function display_ticket_selector_submit()
352
    {
353
        // todo add doing_it_wrong() notice during next major version
354
        return EED_Ticket_Selector::ticketSelector()->displaySubmitButton();
355
    }
356
357
358
359
    /**
360
     * @deprecated
361
     * @param string $permalink_string
362
     * @param int    $id
363
     * @param string $new_title
364
     * @param string $new_slug
365
     * @return string
366
     */
367
    public static function iframe_code_button($permalink_string, $id, $new_title = '', $new_slug = '')
368
    {
369
        // todo add doing_it_wrong() notice during next major version
370 View Code Duplication
        if (
371
        	\EE_Registry::instance()->REQ->get('page') === 'espresso_events'
372
        	&& \EE_Registry::instance()->REQ->get('action') === 'edit'
373
        ) {
374
            $iframe_embed_button = \EED_Ticket_Selector::getIframeEmbedButton();
375
            $iframe_embed_button->addEventEditorIframeEmbedButton();
376
        }
377
        return '';
378
    }
379
380
381
382
    /**
383
     * @deprecated
384
     * @param int    $ID
385
     * @param string $external_url
386
     * @return string
387
     */
388
    public static function ticket_selector_form_open($ID = 0, $external_url = '')
389
    {
390
        // todo add doing_it_wrong() notice during next major version
391
        return EED_Ticket_Selector::ticketSelector()->formOpen($ID, $external_url);
392
    }
393
394
395
396
    /**
397
     * @deprecated
398
     * @return string
399
     */
400
    public static function ticket_selector_form_close()
401
    {
402
        // todo add doing_it_wrong() notice during next major version
403
        return EED_Ticket_Selector::ticketSelector()->formClose();
404
    }
405
406
407
408
    /**
409
     * @deprecated
410
     * @return string
411
     */
412
    public static function no_tkt_slctr_end_dv()
413
    {
414
        // todo add doing_it_wrong() notice during next major version
415
        return EED_Ticket_Selector::ticketSelector()->ticketSelectorEndDiv();
416
    }
417
418
419
420
    /**
421
     * @deprecated 4.9.13
422
     * @return string
423
     */
424
    public static function tkt_slctr_end_dv()
425
    {
426
        return EED_Ticket_Selector::ticketSelector()->clearTicketSelector();
427
    }
428
429
430
431
    /**
432
     * @deprecated
433
     * @return string
434
     */
435
    public static function clear_tkt_slctr()
436
    {
437
        return EED_Ticket_Selector::ticketSelector()->clearTicketSelector();
438
    }
439
440
441
442
    /**
443
     * @deprecated
444
     */
445
    public static function load_tckt_slctr_assets_admin()
446
    {
447
        // todo add doing_it_wrong() notice during next major version
448 View Code Duplication
	    if (
449
		    \EE_Registry::instance()->REQ->get( 'page' ) === 'espresso_events'
450
		    && \EE_Registry::instance()->REQ->get( 'action' ) === 'edit'
451
	    ) {
452
		    $iframe_embed_button = \EED_Ticket_Selector::getIframeEmbedButton();
453
            $iframe_embed_button->embedButtonAssets();
454
        }
455
    }
456
457
458
}
459
// End of file EED_Ticket_Selector.module.php
460
// Location: modules/ticket_selector/EED_Ticket_Selector.module.php
461