Conditions | 7 |
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 |
||
38 | function render_block( $attr, $content ) { |
||
39 | if ( is_admin() || empty( $attr['eventId'] ) || empty( $attr['url'] ) ) { |
||
40 | return ''; |
||
41 | } |
||
42 | |||
43 | $attr['url'] = Jetpack_Gutenberg::validate_block_embed_url( |
||
44 | $attr['url'], |
||
45 | 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+)\/?(?:\?[^\/]*)?$#' ), |
||
46 | true |
||
47 | ); |
||
48 | |||
49 | $widget_id = wp_unique_id( 'eventbrite-widget-' ); |
||
50 | |||
51 | wp_enqueue_script( 'eventbrite-widget', 'https://www.eventbrite.com/static/widgets/eb_widgets.js', array(), JETPACK__VERSION, true ); |
||
52 | |||
53 | // Add CSS to hide direct link. |
||
54 | Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); |
||
55 | |||
56 | // Show the embedded version. |
||
57 | if ( empty( $attr['useModal'] ) && ( empty( $attr['style'] ) || 'modal' !== $attr['style'] ) ) { |
||
58 | wp_add_inline_script( |
||
59 | 'eventbrite-widget', |
||
60 | "window.EBWidgets.createWidget( { |
||
61 | widgetType: 'checkout', |
||
62 | eventId: " . absint( $attr['eventId'] ) . ", |
||
63 | iframeContainerId: '" . esc_js( $widget_id ) . "', |
||
64 | } );" |
||
65 | ); |
||
66 | |||
67 | // $content contains a fallback link to the event that's saved in the post_content. |
||
68 | // Append a div that will hold the iframe embed created by the Eventbrite widget.js. |
||
69 | $classes = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attr ); |
||
70 | |||
71 | $content .= sprintf( |
||
72 | '<div id="%1$s" class="%2$s"></div>', |
||
73 | esc_attr( $widget_id ), |
||
74 | esc_attr( $classes ) |
||
75 | ); |
||
76 | |||
77 | return sprintf( |
||
78 | '%s<noscript><a href="%s" rel="noopener noreferrer" target="_blank">%s</a></noscript>', |
||
79 | $content, |
||
80 | esc_url( $attr['url'] ), |
||
81 | esc_html__( 'Register on Eventbrite', 'jetpack' ) |
||
82 | ); |
||
83 | } |
||
84 | |||
85 | // Show the modal version. |
||
86 | wp_add_inline_script( |
||
87 | 'eventbrite-widget', |
||
88 | "window.EBWidgets.createWidget( { |
||
89 | widgetType: 'checkout', |
||
90 | eventId: " . absint( $attr['eventId'] ) . ", |
||
91 | modal: true, |
||
92 | modalTriggerElementId: '" . esc_js( $widget_id ) . "', |
||
93 | } );" |
||
94 | ); |
||
95 | |||
96 | // Modal button is saved as an `<a>` element with `role="button"` because `<button>` is not allowed |
||
97 | // by WordPress.com wp_kses. This javascript adds the necessary event handling for button-like behavior. |
||
98 | // @link https://www.w3.org/TR/wai-aria-practices/examples/button/button.html. |
||
99 | wp_add_inline_script( |
||
100 | 'eventbrite-widget', |
||
101 | "( function() { |
||
102 | var widget = document.getElementById( '" . esc_js( $widget_id ) . "' ); |
||
103 | if ( widget ) { |
||
104 | widget.addEventListener( 'click', function( event ) { |
||
105 | event.preventDefault(); |
||
106 | } ); |
||
107 | |||
108 | widget.addEventListener( 'keydown', function( event ) { |
||
109 | // Enter and space keys. |
||
110 | if ( event.keyCode === 13 || event.keyCode === 32 ) { |
||
111 | event.preventDefault(); |
||
112 | event.target && event.target.click(); |
||
113 | } |
||
114 | } ); |
||
115 | } |
||
116 | } )();" |
||
117 | ); |
||
118 | |||
119 | // Replace the placeholder id saved in the post_content with a unique id used by widget.js. |
||
120 | $content = preg_replace( '/eventbrite-widget-\d+/', $widget_id, $content ); |
||
121 | |||
122 | return $content; |
||
123 | } |
||
124 |