Conditions | 10 |
Paths | 19 |
Total Lines | 67 |
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 |
||
182 | public function render_button( $attrs ) { |
||
183 | Jetpack_Gutenberg::load_assets_as_required( self::$button_block_name, array( 'thickbox', 'wp-polyfill' ) ); |
||
184 | |||
185 | if ( empty( $attrs['planId'] ) ) { |
||
186 | return; |
||
187 | } |
||
188 | $id = intval( $attrs['planId'] ); |
||
189 | $product = get_post( $id ); |
||
190 | if ( ! $product || is_wp_error( $product ) ) { |
||
191 | return; |
||
192 | } |
||
193 | if ( $product->post_type !== self::$post_type_plan || 'publish' !== $product->post_status ) { |
||
194 | return; |
||
195 | } |
||
196 | |||
197 | $data = array( |
||
198 | 'blog_id' => self::get_blog_id(), |
||
199 | 'id' => $id, |
||
200 | 'button_label' => __( 'Your contribution', 'jetpack' ), |
||
201 | 'powered_text' => __( 'Powered by WordPress.com', 'jetpack' ), |
||
202 | ); |
||
203 | |||
204 | $classes = array( |
||
205 | 'components-button', |
||
206 | 'is-primary', |
||
207 | 'is-button', |
||
208 | 'wp-block-jetpack-' . self::$button_block_name, |
||
209 | self::$css_classname_prefix . '-' . $data['id'], |
||
210 | ); |
||
211 | if ( isset( $attrs['className'] ) ) { |
||
212 | array_push( $classes, $attrs['className'] ); |
||
213 | } |
||
214 | if ( isset( $attrs['submitButtonText'] ) ) { |
||
215 | $data['button_label'] = $attrs['submitButtonText']; |
||
216 | } |
||
217 | $button_styles = array(); |
||
218 | if ( ! empty( $attrs['customBackgroundButtonColor'] ) ) { |
||
219 | array_push( |
||
220 | $button_styles, |
||
221 | sprintf( |
||
222 | 'background-color: %s', |
||
223 | sanitize_hex_color( $attrs['customBackgroundButtonColor'] ) |
||
224 | ) |
||
225 | ); |
||
226 | } |
||
227 | if ( ! empty( $attrs['customTextButtonColor'] ) ) { |
||
228 | array_push( |
||
229 | $button_styles, |
||
230 | sprintf( |
||
231 | 'color: %s', |
||
232 | sanitize_hex_color( $attrs['customTextButtonColor'] ) |
||
233 | ) |
||
234 | ); |
||
235 | } |
||
236 | $button_styles = implode( $button_styles, ';' ); |
||
237 | add_thickbox(); |
||
238 | return sprintf( |
||
239 | '<button data-blog-id="%d" data-powered-text="%s" data-plan-id="%d" data-lang="%s" class="%s" style="%s">%s</button>', |
||
240 | esc_attr( $data['blog_id'] ), |
||
241 | esc_attr( $data['powered_text'] ), |
||
242 | esc_attr( $data['id'] ), |
||
243 | esc_attr( get_locale() ), |
||
244 | esc_attr( implode( $classes, ' ' ) ), |
||
245 | esc_attr( $button_styles ), |
||
246 | esc_html( $data['button_label'] ) |
||
247 | ); |
||
248 | } |
||
249 | |||
273 |