Conditions | 13 |
Paths | 1537 |
Total Lines | 129 |
Lines | 47 |
Ratio | 36.43 % |
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 |
||
26 | function jetpack_mailchimp_block_load_assets( $attr ) { |
||
27 | |||
28 | if ( ! jetpack_mailchimp_verify_connection() ) { |
||
29 | return null; |
||
30 | } |
||
31 | |||
32 | $values = array(); |
||
33 | $blog_id = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) |
||
34 | ? get_current_blog_id() |
||
35 | : Jetpack_Options::get_option( 'id' ); |
||
36 | Jetpack_Gutenberg::load_assets_as_required( 'mailchimp' ); |
||
37 | $defaults = array( |
||
38 | 'emailPlaceholder' => esc_html__( 'Enter your email', 'jetpack' ), |
||
39 | 'submitButtonText' => esc_html__( 'Join my email list', 'jetpack' ), |
||
40 | 'consentText' => esc_html__( 'By clicking submit, you agree to share your email address with the site owner and Mailchimp to receive marketing, updates, and other emails from the site owner. Use the unsubscribe link in those emails to opt out at any time.', 'jetpack' ), |
||
41 | 'processingLabel' => esc_html__( 'Processing…', 'jetpack' ), |
||
42 | 'successLabel' => esc_html__( 'Success! You\'re on the list.', 'jetpack' ), |
||
43 | 'errorLabel' => esc_html__( 'Whoops! There was an error and we couldn\'t process your subscription. Please reload the page and try again.', 'jetpack' ), |
||
44 | ); |
||
45 | foreach ( $defaults as $id => $default ) { |
||
46 | $values[ $id ] = isset( $attr[ $id ] ) ? $attr[ $id ] : $default; |
||
47 | } |
||
48 | |||
49 | $values['submitButtonText'] = empty( $values['submitButtonText'] ) ? $defaults['submitButtonText'] : $values['submitButtonText']; |
||
50 | |||
51 | $classes = Jetpack_Gutenberg::block_classes( 'mailchimp', $attr ); |
||
52 | |||
53 | $button_styles = array(); |
||
54 | if ( ! empty( $attr['customBackgroundButtonColor'] ) ) { |
||
55 | array_push( |
||
56 | $button_styles, |
||
57 | sprintf( |
||
58 | 'background-color: %s', |
||
59 | sanitize_hex_color( $attr['customBackgroundButtonColor'] ) |
||
60 | ) |
||
61 | ); |
||
62 | } |
||
63 | View Code Duplication | if ( ! empty( $attr['customTextButtonColor'] ) ) { |
|
64 | array_push( |
||
65 | $button_styles, |
||
66 | sprintf( |
||
67 | 'color: %s', |
||
68 | sanitize_hex_color( $attr['customTextButtonColor'] ) |
||
69 | ) |
||
70 | ); |
||
71 | } |
||
72 | $button_styles = implode( ';', $button_styles ); |
||
73 | $amp_form_action = sprintf( 'https://public-api.wordpress.com/rest/v1.1/sites/%s/email_follow/amp/subscribe/', $blog_id ); |
||
74 | $is_amp_request = class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request(); |
||
75 | |||
76 | ob_start(); |
||
77 | ?> |
||
78 | |||
79 | <div class="<?php echo esc_attr( $classes ); ?>" data-blog-id="<?php echo esc_attr( $blog_id ); ?>"> |
||
80 | <div class="components-placeholder"> |
||
81 | <form |
||
82 | aria-describedby="wp-block-jetpack-mailchimp_consent-text" |
||
83 | <?php if ( $is_amp_request ) : ?> |
||
84 | action-xhr="<?php echo esc_url( $amp_form_action ); ?>" |
||
85 | method="post" |
||
86 | id="mailchimp_form" |
||
87 | target="_top" |
||
88 | <?php endif; ?> |
||
89 | > |
||
90 | <p> |
||
91 | <input |
||
92 | aria-label="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>" |
||
93 | placeholder="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>" |
||
94 | required |
||
95 | title="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>" |
||
96 | type="email" |
||
97 | name="email" |
||
98 | /> |
||
99 | </p> |
||
100 | <p> |
||
101 | <button type="submit" class="components-button is-button is-primary" style="<?php echo esc_attr( $button_styles ); ?>"> |
||
102 | <?php echo wp_kses_post( $values['submitButtonText'] ); ?> |
||
103 | </button> |
||
104 | </p> |
||
105 | <p id="wp-block-jetpack-mailchimp_consent-text"> |
||
106 | <?php echo wp_kses_post( $values['consentText'] ); ?> |
||
107 | </p> |
||
108 | |||
109 | View Code Duplication | <?php if ( $is_amp_request ) : ?> |
|
110 | |||
111 | <div submit-success> |
||
112 | <template type="amp-mustache"> |
||
113 | <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success wp-block-jetpack-mailchimp__is-amp"> |
||
114 | <?php echo esc_html( $values['successLabel'] ); ?> |
||
115 | </div> |
||
116 | </template> |
||
117 | </div> |
||
118 | <div submit-error> |
||
119 | <template type="amp-mustache"> |
||
120 | <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error wp-block-jetpack-mailchimp__is-amp"> |
||
121 | <?php echo esc_html( $values['errorLabel'] ); ?> |
||
122 | </div> |
||
123 | </template> |
||
124 | </div> |
||
125 | <div submitting> |
||
126 | <template type="amp-mustache"> |
||
127 | <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing wp-block-jetpack-mailchimp__is-amp" role="status"> |
||
128 | <?php echo esc_html( $values['processingLabel'] ); ?> |
||
129 | </div> |
||
130 | </template> |
||
131 | </div> |
||
132 | |||
133 | <?php endif; ?> |
||
134 | |||
135 | </form> |
||
136 | View Code Duplication | <?php if ( ! $is_amp_request ) : ?> |
|
137 | |||
138 | <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing" role="status"> |
||
139 | <?php echo esc_html( $values['processingLabel'] ); ?> |
||
140 | </div> |
||
141 | <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success" role="status"> |
||
142 | <?php echo esc_html( $values['successLabel'] ); ?> |
||
143 | </div> |
||
144 | <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error" role="alert"> |
||
145 | <?php echo esc_html( $values['errorLabel'] ); ?> |
||
146 | </div> |
||
147 | |||
148 | <?php endif; ?> |
||
149 | </div> |
||
150 | </div> |
||
151 | <?php |
||
152 | $html = ob_get_clean(); |
||
153 | return $html; |
||
154 | } |
||
155 | |||
172 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.