Conditions | 5 |
Paths | 3 |
Total Lines | 86 |
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 |
||
25 | function jetpack_render_eventbrite_block( $attr, $content ) { |
||
26 | if ( is_admin() || empty( $attr['eventId'] ) || empty( $attr['url'] ) ) { |
||
27 | return ''; |
||
28 | } |
||
29 | |||
30 | $attr['url'] = Jetpack_Gutenberg::validate_block_embed_url( |
||
31 | $attr['url'], |
||
32 | array( '#^https?:\/\/(?:[0-9a-z]+\.)?eventbrite\.(?:com|co\.uk|com\.ar|com\.au|be|com\.br|ca|cl|co|dk|de|es|fi|fr|hk|ie|it|com\.mx|nl|co\.nz|at|com\.pe|pt|ch|sg|se)\/e\/[^\/]*?(?:\d+)\/?(?:\?[^\/]*)?$#' ), |
||
33 | true |
||
34 | ); |
||
35 | |||
36 | $widget_id = wp_unique_id( 'eventbrite-widget-' ); |
||
37 | |||
38 | wp_enqueue_script( 'eventbrite-widget', 'https://www.eventbrite.com/static/widgets/eb_widgets.js', array(), JETPACK__VERSION, true ); |
||
39 | |||
40 | // Add CSS to hide direct link. |
||
41 | Jetpack_Gutenberg::load_assets_as_required( 'eventbrite' ); |
||
42 | |||
43 | // Show the embedded version. |
||
44 | if ( empty( $attr['useModal'] ) ) { |
||
45 | wp_add_inline_script( |
||
46 | 'eventbrite-widget', |
||
47 | "window.EBWidgets.createWidget( { |
||
48 | widgetType: 'checkout', |
||
49 | eventId: " . absint( $attr['eventId'] ) . ", |
||
50 | iframeContainerId: '" . esc_js( $widget_id ) . "', |
||
51 | } );" |
||
52 | ); |
||
53 | |||
54 | // $content contains a fallback link to the event that's saved in the post_content. |
||
55 | // Append a div that will hold the iframe embed created by the Eventbrite widget.js. |
||
56 | $classes = \Jetpack_Gutenberg::block_classes( 'eventbrite', $attr ); |
||
57 | |||
58 | $content .= sprintf( |
||
59 | '<div id="%1$s" class="%2$s"></div>', |
||
60 | esc_attr( $widget_id ), |
||
61 | esc_attr( $classes ) |
||
62 | ); |
||
63 | |||
64 | return sprintf( |
||
65 | '%s<noscript><a href="%s" rel="noopener noreferrer" target="_blank">%s</a></noscript>', |
||
66 | $content, |
||
67 | esc_url( $attr['url'] ), |
||
68 | esc_html__( 'Register on Eventbrite', 'jetpack' ) |
||
69 | ); |
||
70 | } |
||
71 | |||
72 | // Show the modal version. |
||
73 | wp_add_inline_script( |
||
74 | 'eventbrite-widget', |
||
75 | "window.EBWidgets.createWidget( { |
||
76 | widgetType: 'checkout', |
||
77 | eventId: " . absint( $attr['eventId'] ) . ", |
||
78 | modal: true, |
||
79 | modalTriggerElementId: '" . esc_js( $widget_id ) . "', |
||
80 | } );" |
||
81 | ); |
||
82 | |||
83 | // Modal button is saved as an `<a>` element with `role="button"` because `<button>` is not allowed |
||
84 | // by WordPress.com wp_kses. This javascript adds the necessary event handling for button-like behavior. |
||
85 | // @link https://www.w3.org/TR/wai-aria-practices/examples/button/button.html. |
||
86 | wp_add_inline_script( |
||
87 | 'eventbrite-widget', |
||
88 | "( function() { |
||
89 | var widget = document.getElementById( '" . esc_js( $widget_id ) . "' ); |
||
90 | if ( widget ) { |
||
91 | widget.addEventListener( 'click', function( event ) { |
||
92 | event.preventDefault(); |
||
93 | } ); |
||
94 | |||
95 | widget.addEventListener( 'keydown', function( event ) { |
||
96 | // Enter and space keys. |
||
97 | if ( event.keyCode === 13 || event.keyCode === 32 ) { |
||
98 | event.preventDefault(); |
||
99 | event.target && event.target.click(); |
||
100 | } |
||
101 | } ); |
||
102 | } |
||
103 | } )();" |
||
104 | ); |
||
105 | |||
106 | // Replace the placeholder id saved in the post_content with a unique id used by widget.js. |
||
107 | $content = preg_replace( '/eventbrite-widget-\d+/', $widget_id, $content ); |
||
108 | |||
109 | return $content; |
||
110 | } |
||
111 |