Conditions | 12 |
Paths | 129 |
Total Lines | 142 |
Lines | 18 |
Ratio | 12.68 % |
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 |
||
96 | function render_block( $attr, $content ) { |
||
97 | // Keep content as-is if rendered in other contexts than frontend (i.e. feed, emails, API, etc.). |
||
98 | if ( ! jetpack_is_frontend() ) { |
||
99 | return $content; |
||
100 | } |
||
101 | |||
102 | Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME, array( 'thickbox' ) ); |
||
103 | add_thickbox(); |
||
104 | |||
105 | require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php'; |
||
106 | jetpack_require_lib( 'class-jetpack-currencies' ); |
||
107 | |||
108 | $donations = array( |
||
109 | 'one-time' => array_merge( |
||
110 | array( |
||
111 | 'title' => __( 'One-Time', 'jetpack' ), |
||
112 | 'class' => 'donations__one-time-item', |
||
113 | ), |
||
114 | $attr['oneTimeDonation'] |
||
115 | ), |
||
116 | ); |
||
117 | View Code Duplication | if ( $attr['monthlyDonation']['show'] ) { |
|
118 | $donations['1 month'] = array_merge( |
||
119 | array( |
||
120 | 'title' => __( 'Monthly', 'jetpack' ), |
||
121 | 'class' => 'donations__monthly-item', |
||
122 | ), |
||
123 | $attr['monthlyDonation'] |
||
124 | ); |
||
125 | } |
||
126 | View Code Duplication | if ( $attr['annualDonation']['show'] ) { |
|
127 | $donations['1 year'] = array_merge( |
||
128 | array( |
||
129 | 'title' => __( 'Yearly', 'jetpack' ), |
||
130 | 'class' => 'donations__annual-item', |
||
131 | ), |
||
132 | $attr['annualDonation'] |
||
133 | ); |
||
134 | } |
||
135 | |||
136 | $currency = $attr['currency']; |
||
137 | $nav = ''; |
||
138 | $headings = ''; |
||
139 | $amounts = ''; |
||
140 | $extra_text = ''; |
||
141 | $buttons = ''; |
||
142 | foreach ( $donations as $interval => $donation ) { |
||
143 | $plan_id = (int) $donation['planId']; |
||
144 | $plan = get_post( $plan_id ); |
||
145 | if ( ! $plan || is_wp_error( $plan ) ) { |
||
146 | continue; |
||
147 | } |
||
148 | |||
149 | if ( count( $donations ) > 1 ) { |
||
150 | if ( ! $nav ) { |
||
151 | $nav .= '<div class="donations__nav">'; |
||
152 | } |
||
153 | $nav .= sprintf( |
||
154 | '<div role="button" tabindex="0" class="donations__nav-item" data-interval="%1$s">%2$s</div>', |
||
155 | esc_attr( $interval ), |
||
156 | esc_html( $donation['title'] ) |
||
157 | ); |
||
158 | } |
||
159 | $headings .= sprintf( |
||
160 | '<h4 class="%1$s">%2$s</h4>', |
||
161 | esc_attr( $donation['class'] ), |
||
162 | wp_kses_post( $donation['heading'] ) |
||
163 | ); |
||
164 | $amounts .= sprintf( |
||
165 | '<div class="donations__amounts %s">', |
||
166 | esc_attr( $donation['class'] ) |
||
167 | ); |
||
168 | foreach ( $donation['amounts'] as $amount ) { |
||
169 | $amounts .= sprintf( |
||
170 | '<div class="donations__amount" data-amount="%1$s">%2$s</div>', |
||
171 | esc_attr( $amount ), |
||
172 | esc_html( \Jetpack_Currencies::format_price( $amount, $currency ) ) |
||
173 | ); |
||
174 | } |
||
175 | $amounts .= '</div>'; |
||
176 | $extra_text .= sprintf( |
||
177 | '<p class="%1$s">%2$s</p>', |
||
178 | esc_attr( $donation['class'] ), |
||
179 | wp_kses_post( $donation['extraText'] ) |
||
180 | ); |
||
181 | $buttons .= sprintf( |
||
182 | '<a class="wp-block-button__link donations__donate-button %1$s" href="%2$s">%3$s</a>', |
||
183 | esc_attr( $donation['class'] ), |
||
184 | esc_url( \Jetpack_Memberships::get_instance()->get_subscription_url( $plan_id ) ), |
||
185 | wp_kses_post( $donation['buttonText'] ) |
||
186 | ); |
||
187 | } |
||
188 | if ( $nav ) { |
||
189 | $nav .= '</div>'; |
||
190 | } |
||
191 | |||
192 | $custom_amount = ''; |
||
193 | if ( $attr['showCustomAmount'] ) { |
||
194 | $custom_amount .= sprintf( |
||
195 | '<p>%s</p>', |
||
196 | wp_kses_post( $attr['customAmountText'] ) |
||
197 | ); |
||
198 | $default_custom_amount = \Jetpack_Memberships::SUPPORTED_CURRENCIES[ $currency ] * 100; |
||
199 | $custom_amount .= sprintf( |
||
200 | '<div class="donations__amount donations__custom-amount"> |
||
201 | %1$s |
||
202 | <div class="donations__amount-value" data-currency="%2$s" data-empty-text="%3$s"></div> |
||
203 | </div>', |
||
204 | esc_html( \Jetpack_Currencies::CURRENCIES[ $attr['currency'] ]['symbol'] ), |
||
205 | esc_attr( $attr['currency'] ), |
||
206 | esc_attr( \Jetpack_Currencies::format_price( $default_custom_amount, $currency, false ) ) |
||
207 | ); |
||
208 | } |
||
209 | |||
210 | return sprintf( |
||
211 | ' |
||
212 | <div class="%1$s"> |
||
213 | <div class="donations__container"> |
||
214 | %2$s |
||
215 | <div class="donations__content"> |
||
216 | <div class="donations__tab"> |
||
217 | %3$s |
||
218 | <p>%4$s</p> |
||
219 | %5$s |
||
220 | %6$s |
||
221 | <hr class="donations__separator"> |
||
222 | %7$s |
||
223 | %8$s |
||
224 | </div> |
||
225 | </div> |
||
226 | </div> |
||
227 | ', |
||
228 | esc_attr( Blocks::classes( FEATURE_NAME, $attr ) ), |
||
229 | $nav, |
||
230 | $headings, |
||
231 | $attr['chooseAmountText'], |
||
232 | $amounts, |
||
233 | $custom_amount, |
||
234 | $extra_text, |
||
235 | $buttons |
||
236 | ); |
||
237 | } |
||
238 | |||
257 |
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.