Conditions | 7 |
Paths | 12 |
Total Lines | 76 |
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 | |||
59 | $style = get_attribute( $attr, 'style' ); |
||
60 | $hide_event_type_details = get_attribute( $attr, 'hideEventTypeDetails' ); |
||
61 | $background_color = get_attribute( $attr, 'backgroundColor' ); |
||
62 | $text_color = get_attribute( $attr, 'textColor' ); |
||
63 | $primary_color = get_attribute( $attr, 'primaryColor' ); |
||
64 | $classes = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attr, array( 'calendly-style-' . $style ) ); |
||
65 | $block_id = wp_unique_id( 'calendly-block-' ); |
||
66 | |||
67 | if ( ! wp_script_is( 'jetpack-calendly-external-js' ) ) { |
||
68 | enqueue_calendly_js(); |
||
69 | } |
||
70 | |||
71 | $base_url = $url; |
||
72 | $url = add_query_arg( |
||
73 | array( |
||
74 | 'hide_event_type_details' => (int) $hide_event_type_details, |
||
75 | 'background_color' => sanitize_hex_color_no_hash( $background_color ), |
||
76 | 'text_color' => sanitize_hex_color_no_hash( $text_color ), |
||
77 | 'primary_color' => sanitize_hex_color_no_hash( $primary_color ), |
||
78 | ), |
||
79 | $url |
||
80 | ); |
||
81 | |||
82 | if ( 'link' === $style ) { |
||
83 | if ( ! wp_style_is( 'jetpack-calendly-external-css' ) ) { |
||
84 | wp_enqueue_style( 'jetpack-calendly-external-css', 'https://assets.calendly.com/assets/external/widget.css', null, JETPACK__VERSION ); |
||
85 | } |
||
86 | |||
87 | // Render deprecated version of Calendly block if needed. New markup block button class before rendering here. |
||
88 | if ( false === strpos( $content, 'wp-block-jetpack-button' ) ) { |
||
89 | $content = deprecated_render_button_v1( $attr, $block_id, $classes, $url ); |
||
90 | } else { |
||
91 | $content = str_replace( 'calendly-widget-id', esc_attr( $block_id ), $content ); |
||
92 | $content = str_replace( $base_url, $url, $content ); |
||
93 | } |
||
94 | |||
95 | wp_add_inline_script( |
||
96 | 'jetpack-calendly-external-js', |
||
97 | sprintf( "calendly_attach_link_events( '%s' )", esc_js( $block_id ) ) |
||
98 | ); |
||
99 | } else { // Inline style. |
||
100 | $content = sprintf( |
||
101 | '<div class="%1$s" id="%2$s"></div>', |
||
102 | esc_attr( $classes ), |
||
103 | esc_attr( $block_id ) |
||
104 | ); |
||
105 | $script = <<<JS_END |
||
106 | Calendly.initInlineWidget({ |
||
107 | url: '%s', |
||
108 | parentElement: document.getElementById('%s'), |
||
109 | inlineStyles: false, |
||
110 | }); |
||
111 | JS_END; |
||
112 | wp_add_inline_script( 'jetpack-calendly-external-js', sprintf( $script, esc_url( $url ), esc_js( $block_id ) ) ); |
||
113 | } |
||
114 | |||
115 | return $content; |
||
116 | } |
||
117 | |||
226 |