Conditions | 9 |
Paths | 5 |
Total Lines | 95 |
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 |
||
41 | function load_assets( $attr, $content ) { |
||
42 | |||
43 | if ( is_admin() ) { |
||
44 | return; |
||
45 | } |
||
46 | $url = Jetpack_Gutenberg::validate_block_embed_url( |
||
47 | get_attribute( $attr, 'url' ), |
||
48 | array( 'calendly.com' ) |
||
49 | ); |
||
50 | if ( empty( $url ) ) { |
||
51 | return; |
||
52 | } |
||
53 | |||
54 | /* |
||
55 | * Enqueue necessary scripts and styles. |
||
56 | */ |
||
57 | Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); |
||
58 | wp_enqueue_script( |
||
59 | 'jetpack-calendly-external-js', |
||
60 | 'https://assets.calendly.com/assets/external/widget.js', |
||
61 | null, |
||
62 | JETPACK__VERSION, |
||
63 | true |
||
64 | ); |
||
65 | |||
66 | $style = get_attribute( $attr, 'style' ); |
||
67 | $hide_event_type_details = get_attribute( $attr, 'hideEventTypeDetails' ); |
||
68 | $background_color = get_attribute( $attr, 'backgroundColor' ); |
||
69 | $text_color = get_attribute( $attr, 'textColor' ); |
||
70 | $primary_color = get_attribute( $attr, 'primaryColor' ); |
||
71 | $submit_button_text = get_attribute( $attr, 'submitButtonText' ); |
||
72 | $submit_button_classes = get_attribute( $attr, 'submitButtonClasses' ); |
||
73 | $submit_button_text_color = get_attribute( $attr, 'customTextButtonColor' ); |
||
74 | $submit_button_background_color = get_attribute( $attr, 'customBackgroundButtonColor' ); |
||
75 | $classes = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attr, array( 'calendly-style-' . $style ) ); |
||
76 | $block_id = wp_unique_id( 'calendly-block-' ); |
||
77 | |||
78 | $url = add_query_arg( |
||
79 | array( |
||
80 | 'hide_event_type_details' => (int) $hide_event_type_details, |
||
81 | 'background_color' => sanitize_hex_color_no_hash( $background_color ), |
||
82 | 'text_color' => sanitize_hex_color_no_hash( $text_color ), |
||
83 | 'primary_color' => sanitize_hex_color_no_hash( $primary_color ), |
||
84 | ), |
||
85 | $url |
||
86 | ); |
||
87 | |||
88 | if ( 'link' === $style ) { |
||
89 | wp_enqueue_style( 'jetpack-calendly-external-css', 'https://assets.calendly.com/assets/external/widget.css', null, JETPACK__VERSION ); |
||
90 | |||
91 | /* |
||
92 | * If we have some additional styles from the editor |
||
93 | * (a custom text color, custom bg color, or both ) |
||
94 | * Let's add that CSS inline. |
||
95 | */ |
||
96 | if ( ! empty( $submit_button_text_color ) || ! empty( $submit_button_background_color ) ) { |
||
97 | $inline_styles = sprintf( |
||
98 | '#%1$s .wp-block-button__link{%2$s%3$s}', |
||
99 | esc_attr( $block_id ), |
||
100 | ! empty( $submit_button_text_color ) |
||
101 | ? 'color:#' . sanitize_hex_color_no_hash( $submit_button_text_color ) . ';' |
||
102 | : '', |
||
103 | ! empty( $submit_button_background_color ) |
||
104 | ? 'background-color:#' . sanitize_hex_color_no_hash( $submit_button_background_color ) . ';' |
||
105 | : '' |
||
106 | ); |
||
107 | wp_add_inline_style( 'jetpack-calendly-external-css', $inline_styles ); |
||
108 | } |
||
109 | |||
110 | $content = sprintf( |
||
111 | '<div class="wp-block-button %1$s" id="%2$s"><a class="%3$s" role="button" onclick="Calendly.initPopupWidget({url:\'%4$s\'});return false;">%5$s</a></div>', |
||
112 | esc_attr( $classes ), |
||
113 | esc_attr( $block_id ), |
||
114 | ! empty( $submit_button_classes ) ? esc_attr( $submit_button_classes ) : 'wp-block-button__link', |
||
115 | esc_js( $url ), |
||
116 | wp_kses_post( $submit_button_text ) |
||
117 | ); |
||
118 | } else { // Inline style. |
||
119 | $content = sprintf( |
||
120 | '<div class="%1$s" id="%2$s"></div>', |
||
121 | esc_attr( $classes ), |
||
122 | esc_attr( $block_id ) |
||
123 | ); |
||
124 | $script = <<<JS_END |
||
125 | Calendly.initInlineWidget({ |
||
126 | url: '%s', |
||
127 | parentElement: document.getElementById('%s'), |
||
128 | inlineStyles: false, |
||
129 | }); |
||
130 | JS_END; |
||
131 | wp_add_inline_script( 'jetpack-calendly-external-js', sprintf( $script, esc_url( $url ), esc_js( $block_id ) ) ); |
||
132 | } |
||
133 | |||
134 | return $content; |
||
135 | } |
||
136 | |||
163 |