Conditions | 4 |
Paths | 3 |
Total Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
27 | function jetpack_render_eventbrite_block( $attr, $content ) { |
||
28 | if ( empty( $attr['eventId'] ) || empty( $attr['url'] ) ) { |
||
29 | return ''; |
||
30 | } |
||
31 | |||
32 | $widget_id = JETPACK_EVENTBRITE_WIDGET_SLUG . '-' . $attr['eventId']; |
||
33 | |||
34 | wp_enqueue_script( 'eventbrite-widget', 'https://www.eventbrite.com/static/widgets/eb_widgets.js', array(), JETPACK__VERSION, true ); |
||
35 | |||
36 | // Add CSS to hide direct link. |
||
37 | Jetpack_Gutenberg::load_assets_as_required( 'eventbrite' ); |
||
38 | |||
39 | // Show the embedded version. |
||
40 | if ( empty( $attr['useModal'] ) ) { |
||
41 | wp_add_inline_script( |
||
42 | 'eventbrite-widget', |
||
43 | "window.EBWidgets.createWidget( { |
||
44 | widgetType: 'checkout', |
||
45 | eventId: " . absint( $attr['eventId'] ) . ", |
||
46 | iframeContainerId: '" . esc_js( $widget_id ) . "', |
||
47 | } );" |
||
48 | ); |
||
49 | |||
50 | return sprintf( |
||
51 | '%s<noscript><a href="%s" rel="noopener noreferrer" target="_blank">%s</a></noscript>', |
||
52 | $content, |
||
53 | esc_url( $attr['url'] ), |
||
54 | esc_html__( 'Register on Eventbrite', 'jetpack' ) |
||
55 | ); |
||
56 | } |
||
57 | |||
58 | // Show the modal version. |
||
59 | wp_add_inline_script( |
||
60 | 'eventbrite-widget', |
||
61 | "window.EBWidgets.createWidget( { |
||
62 | widgetType: 'checkout', |
||
63 | eventId: " . absint( $attr['eventId'] ) . ", |
||
64 | modal: true, |
||
65 | modalTriggerElementId: '" . esc_js( $widget_id ) . "', |
||
66 | } );" |
||
67 | ); |
||
68 | |||
69 | // Modal button is saved as an `<a>` element with `role="button"` because `<button>` is not allowed |
||
70 | // by WordPress.com wp_kses. This javascript adds the necessary event handling for button-like behavior. |
||
71 | // @link https://www.w3.org/TR/wai-aria-practices/examples/button/button.html. |
||
72 | wp_add_inline_script( |
||
73 | 'eventbrite-widget', |
||
74 | "( function() { |
||
75 | var widget = document.getElementById( '" . esc_js( $widget_id ) . "' ); |
||
76 | if ( widget ) { |
||
77 | widget.addEventListener( 'click', function( event ) { |
||
78 | event.preventDefault(); |
||
79 | } ); |
||
80 | |||
81 | widget.addEventListener( 'keydown', function( event ) { |
||
82 | // Enter and space keys. |
||
83 | if ( event.keyCode === 13 || event.keyCode === 32 ) { |
||
84 | event.preventDefault(); |
||
85 | event.target && event.target.click(); |
||
86 | } |
||
87 | } ); |
||
88 | } |
||
89 | } )();" |
||
90 | ); |
||
91 | |||
92 | return $content; |
||
93 | } |
||
94 | |||
111 |