Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_Simple_Payments_Widget often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Simple_Payments_Widget, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Jetpack_Simple_Payments_Widget extends WP_Widget { |
||
| 16 | // https://developer.paypal.com/docs/integration/direct/rest/currency-codes/ |
||
| 17 | private static $supported_currency_list = array( |
||
| 18 | 'USD' => '$', |
||
| 19 | 'GBP' => '£', |
||
| 20 | 'JPY' => '¥', |
||
| 21 | 'BRL' => 'R$', |
||
| 22 | 'EUR' => '€', |
||
| 23 | 'NZD' => 'NZ$', |
||
| 24 | 'AUD' => 'A$', |
||
| 25 | 'CAD' => 'C$', |
||
| 26 | 'INR' => '₹', |
||
| 27 | 'ILS' => '₪', |
||
| 28 | 'RUB' => '₽', |
||
| 29 | 'MXN' => 'MX$', |
||
| 30 | 'SEK' => 'Skr', |
||
| 31 | 'HUF' => 'Ft', |
||
| 32 | 'CHF' => 'CHF', |
||
| 33 | 'CZK' => 'Kč', |
||
| 34 | 'DKK' => 'Dkr', |
||
| 35 | 'HKD' => 'HK$', |
||
| 36 | 'NOK' => 'Kr', |
||
| 37 | 'PHP' => '₱', |
||
| 38 | 'PLN' => 'PLN', |
||
| 39 | 'SGD' => 'S$', |
||
| 40 | 'TWD' => 'NT$', |
||
| 41 | 'THB' => '฿', |
||
| 42 | ); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Constructor. |
||
| 46 | */ |
||
| 47 | function __construct() { |
||
| 48 | parent::__construct( |
||
| 49 | 'jetpack_simple_payments_widget', |
||
| 50 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
||
| 51 | apply_filters( 'jetpack_widget_name', __( 'Simple Payments', 'jetpack' ) ), |
||
| 52 | array( |
||
| 53 | 'classname' => 'jetpack-simple-payments', |
||
| 54 | 'description' => __( 'Add a Simple Payment Button as a Widget.', 'jetpack' ), |
||
| 55 | 'customize_selective_refresh' => true, |
||
| 56 | ) |
||
| 57 | ); |
||
| 58 | |||
| 59 | if ( is_customize_preview() ) { |
||
| 60 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_styles_and_scripts' ) ); |
||
| 61 | |||
| 62 | add_filter( 'customize_refresh_nonces', array( $this, 'filter_nonces' ) ); |
||
| 63 | add_action( 'wp_ajax_customize-jetpack-simple-payments-button-save', array( $this, 'ajax_save_payment_button' ) ); |
||
| 64 | add_action( 'wp_ajax_customize-jetpack-simple-payments-button-delete', array( $this, 'ajax_delete_payment_button' ) ); |
||
| 65 | } |
||
| 66 | |||
| 67 | if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { |
||
| 68 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) ); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Return an associative array of default values. |
||
| 74 | * |
||
| 75 | * These values are used in new widgets. |
||
| 76 | * |
||
| 77 | * @return array Default values for the widget options. |
||
| 78 | */ |
||
| 79 | private function defaults() { |
||
| 80 | $current_user = wp_get_current_user(); |
||
| 81 | |||
| 82 | return array( |
||
| 83 | 'title' => '', |
||
| 84 | 'product_post_id' => 0, |
||
| 85 | 'form_action' => '', |
||
| 86 | 'form_product_id' => 0, |
||
| 87 | 'form_product_title' => '', |
||
| 88 | 'form_product_description' => '', |
||
| 89 | 'form_product_image_id' => 0, |
||
| 90 | 'form_product_image_src' => '', |
||
| 91 | 'form_product_currency' => '', |
||
| 92 | 'form_product_price' => '', |
||
| 93 | 'form_product_multiple' => '', |
||
| 94 | 'form_product_email' => $current_user->user_email, |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Adds a nonce for customizing menus. |
||
| 100 | * |
||
| 101 | * @param array $nonces Array of nonces. |
||
| 102 | * @return array $nonces Modified array of nonces. |
||
| 103 | */ |
||
| 104 | function filter_nonces( $nonces ) { |
||
| 105 | $nonces['customize-jetpack-simple-payments'] = wp_create_nonce( 'customize-jetpack-simple-payments' ); |
||
| 106 | return $nonces; |
||
| 107 | } |
||
| 108 | |||
| 109 | function enqueue_style() { |
||
| 110 | wp_enqueue_style( 'jetpack-simple-payments-widget-style', plugins_url( 'simple-payments/style.css', __FILE__ ), array(), '20180518' ); |
||
| 111 | } |
||
| 112 | |||
| 113 | function admin_enqueue_styles_and_scripts(){ |
||
| 114 | wp_enqueue_style( 'jetpack-simple-payments-widget-customizer', plugins_url( 'simple-payments/customizer.css', __FILE__ ) ); |
||
| 115 | |||
| 116 | wp_enqueue_media(); |
||
| 117 | wp_enqueue_script( 'jetpack-simple-payments-widget-customizer', plugins_url( '/simple-payments/customizer.js', __FILE__ ), array( 'jquery' ), false, true ); |
||
| 118 | wp_localize_script( 'jetpack-simple-payments-widget-customizer', 'jpSimplePaymentsStrings', array( |
||
| 119 | 'deleteConfirmation' => __( 'Are you sure you want to delete this item? It will be disabled and removed from all locations where it currently appears.', 'jetpack' ) |
||
| 120 | ) ); |
||
| 121 | } |
||
| 122 | |||
| 123 | public function ajax_save_payment_button() { |
||
| 124 | if ( ! check_ajax_referer( 'customize-jetpack-simple-payments', 'customize-jetpack-simple-payments-nonce', false ) ) { |
||
| 125 | wp_send_json_error( 'bad_nonce', 400 ); |
||
| 126 | } |
||
| 127 | |||
| 128 | if ( ! current_user_can( 'customize' ) ) { |
||
| 129 | wp_send_json_error( 'customize_not_allowed', 403 ); |
||
| 130 | } |
||
| 131 | |||
| 132 | $post_type_object = get_post_type_object( Jetpack_Simple_Payments::$post_type_product ); |
||
| 133 | if ( ! current_user_can( $post_type_object->cap->create_posts ) || ! current_user_can( $post_type_object->cap->publish_posts ) ) { |
||
| 134 | wp_send_json_error( 'insufficient_post_permissions', 403 ); |
||
| 135 | } |
||
| 136 | |||
| 137 | if ( empty( $_POST['params'] ) || ! is_array( $_POST['params'] ) ) { |
||
| 138 | wp_send_json_error( 'missing_params', 400 ); |
||
| 139 | } |
||
| 140 | |||
| 141 | $params = wp_unslash( $_POST['params'] ); |
||
| 142 | $illegal_params = array_diff( array_keys( $params ), array( 'product_post_id', 'post_title', 'post_content', 'image_id', 'currency', 'price', 'multiple', 'email' ) ); |
||
| 143 | if ( ! empty( $illegal_params ) ) { |
||
| 144 | wp_send_json_error( 'illegal_params', 400 ); |
||
| 145 | } |
||
| 146 | |||
| 147 | $errors = $this->validate_ajax_params( $params ); |
||
| 148 | if( is_wp_error( $errors ) ){ |
||
| 149 | wp_send_json_error( $errors ); |
||
| 150 | } |
||
| 151 | |||
| 152 | $product_post_id = isset( $params['product_post_id'] ) ? intval( $params['product_post_id'] ) : 0; |
||
| 153 | |||
| 154 | $product_post = array( |
||
| 155 | 'ID' => $product_post_id, |
||
| 156 | 'post_type' => Jetpack_Simple_Payments::$post_type_product, |
||
| 157 | 'post_status' => 'publish', |
||
| 158 | 'post_title' => $params['post_title'], |
||
| 159 | 'post_content' => $params['post_content'], |
||
| 160 | '_thumbnail_id' => ! empty( $params['image_id'] ) ? $params['image_id'] : -1, |
||
| 161 | 'meta_input' => array( |
||
| 162 | 'spay_currency' => $params['currency'], |
||
| 163 | 'spay_price' => $params['price'], |
||
| 164 | 'spay_multiple' => isset( $params['multiple'] ) ? intval( $params['multiple'] ) : 0, |
||
| 165 | 'spay_email' => is_email( $params['email'] ), |
||
| 166 | ), |
||
| 167 | ); |
||
| 168 | |||
| 169 | if ( empty( $product_post_id ) ) { |
||
| 170 | $product_post_id = wp_insert_post( $product_post ); |
||
| 171 | } else { |
||
| 172 | $product_post_id = wp_update_post( $product_post ); |
||
| 173 | } |
||
| 174 | |||
| 175 | if ( ! $product_post_id || is_wp_error( $product_post_id ) ) { |
||
| 176 | wp_send_json_error( $product_post_id ); |
||
| 177 | } |
||
| 178 | |||
| 179 | wp_send_json_success( array( |
||
| 180 | 'product_post_id' => $product_post_id, |
||
| 181 | 'product_post_title' => $params['post_title'], |
||
| 182 | ) ); |
||
| 183 | } |
||
| 184 | |||
| 185 | public function ajax_delete_payment_button() { |
||
| 186 | if ( ! check_ajax_referer( 'customize-jetpack-simple-payments', 'customize-jetpack-simple-payments-nonce', false ) ) { |
||
| 187 | wp_send_json_error( 'bad_nonce', 400 ); |
||
| 188 | } |
||
| 189 | |||
| 190 | if ( ! current_user_can( 'customize' ) ) { |
||
| 191 | wp_send_json_error( 'customize_not_allowed', 403 ); |
||
| 192 | } |
||
| 193 | |||
| 194 | if ( empty( $_POST['params'] ) || ! is_array( $_POST['params'] ) ) { |
||
| 195 | wp_send_json_error( 'missing_params', 400 ); |
||
| 196 | } |
||
| 197 | |||
| 198 | $params = wp_unslash( $_POST['params'] ); |
||
| 199 | $illegal_params = array_diff( array_keys( $params ), array( 'product_post_id' ) ); |
||
| 200 | if ( ! empty( $illegal_params ) ) { |
||
| 201 | wp_send_json_error( 'illegal_params', 400 ); |
||
| 202 | } |
||
| 203 | |||
| 204 | $product_id = ( int ) $params['product_post_id']; |
||
| 205 | $product_post = get_post( $product_id ); |
||
| 206 | |||
| 207 | $return = array( 'status' => $product_post->post_status ); |
||
| 208 | |||
| 209 | wp_delete_post( $product_id, true ); |
||
| 210 | $status = get_post_status( $product_id ); |
||
| 211 | if ( false === $status ) { |
||
| 212 | $return['status'] = 'deleted'; |
||
| 213 | } |
||
| 214 | |||
| 215 | wp_send_json_success( $return ); |
||
| 216 | } |
||
| 217 | |||
| 218 | public function ajax_save_payment_button() { |
||
| 219 | if ( ! check_ajax_referer( 'customize-jetpack-simple-payments', 'customize-jetpack-simple-payments-nonce', false ) ) { |
||
| 220 | wp_send_json_error( 'bad_nonce', 400 ); |
||
| 221 | } |
||
| 222 | |||
| 223 | if ( ! current_user_can( 'customize' ) ) { |
||
| 224 | wp_send_json_error( 'customize_not_allowed', 403 ); |
||
| 225 | } |
||
| 226 | |||
| 227 | $post_type_object = get_post_type_object( Jetpack_Simple_Payments::$post_type_product ); |
||
| 228 | if ( ! current_user_can( $post_type_object->cap->create_posts ) || ! current_user_can( $post_type_object->cap->publish_posts ) ) { |
||
| 229 | wp_send_json_error( 'insufficient_post_permissions', 403 ); |
||
| 230 | } |
||
| 231 | |||
| 232 | if ( empty( $_POST['params'] ) || ! is_array( $_POST['params'] ) ) { |
||
| 233 | wp_send_json_error( 'missing_params', 400 ); |
||
| 234 | } |
||
| 235 | |||
| 236 | $params = wp_unslash( $_POST['params'] ); |
||
| 237 | $illegal_params = array_diff( array_keys( $params ), array( 'product_post_id', 'post_title', 'post_content', 'image_id', 'currency', 'price', 'multiple', 'email' ) ); |
||
| 238 | if ( ! empty( $illegal_params ) ) { |
||
| 239 | wp_send_json_error( 'illegal_params', 400 ); |
||
| 240 | } |
||
| 241 | |||
| 242 | $product_post_id = isset( $params['product_post_id'] ) ? intval( $params['product_post_id'] ) : 0; |
||
| 243 | |||
| 244 | $product_post = array( |
||
| 245 | 'ID' => $product_post_id, |
||
| 246 | 'post_type' => Jetpack_Simple_Payments::$post_type_product, |
||
| 247 | 'post_status' => 'publish', |
||
| 248 | 'post_title' => $params['post_title'], |
||
| 249 | 'post_content' => $params['post_content'], |
||
| 250 | '_thumbnail_id' => ! empty( $params['image_id'] ) ? $params['image_id'] : -1, |
||
| 251 | 'meta_input' => array( |
||
| 252 | 'spay_currency' => $params['currency'], |
||
| 253 | 'spay_price' => $params['price'], |
||
| 254 | 'spay_multiple' => isset( $params['multiple'] ) ? intval( $params['multiple'] ) : 0, |
||
| 255 | 'spay_email' => is_email( $params['email'] ), |
||
| 256 | ), |
||
| 257 | ); |
||
| 258 | |||
| 259 | if ( empty( $product_post_id ) ) { |
||
| 260 | $product_post_id = wp_insert_post( $product_post ); |
||
| 261 | } else { |
||
| 262 | $product_post_id = wp_update_post( $product_post ); |
||
| 263 | } |
||
| 264 | |||
| 265 | if ( ! $product_post_id || is_wp_error( $product_post_id ) ) { |
||
| 266 | wp_send_json_error( $product_post_id ); |
||
| 267 | } |
||
| 268 | |||
| 269 | wp_send_json_success( [ |
||
| 270 | 'product_post_id' => $product_post_id, |
||
| 271 | 'product_post_title' => $params['post_title'], |
||
| 272 | ] ); |
||
| 273 | } |
||
| 274 | |||
| 275 | public function ajax_delete_payment_button() { |
||
| 276 | if ( ! check_ajax_referer( 'customize-jetpack-simple-payments', 'customize-jetpack-simple-payments-nonce', false ) ) { |
||
| 277 | wp_send_json_error( 'bad_nonce', 400 ); |
||
| 278 | } |
||
| 279 | |||
| 280 | if ( ! current_user_can( 'customize' ) ) { |
||
| 281 | wp_send_json_error( 'customize_not_allowed', 403 ); |
||
| 282 | } |
||
| 283 | |||
| 284 | if ( empty( $_POST['params'] ) || ! is_array( $_POST['params'] ) ) { |
||
| 285 | wp_send_json_error( 'missing_params', 400 ); |
||
| 286 | } |
||
| 287 | |||
| 288 | $params = wp_unslash( $_POST['params'] ); |
||
| 289 | $illegal_params = array_diff( array_keys( $params ), array( 'product_post_id' ) ); |
||
| 290 | if ( ! empty( $illegal_params ) ) { |
||
| 291 | wp_send_json_error( 'illegal_params', 400 ); |
||
| 292 | } |
||
| 293 | |||
| 294 | $product_id = ( int ) $params['product_post_id']; |
||
| 295 | $product_post = get_post( $product_id ); |
||
| 296 | |||
| 297 | $return = array( 'status' => $product_post->post_status ); |
||
| 298 | |||
| 299 | wp_delete_post( $product_id, true ); |
||
| 300 | $status = get_post_status( $product_id ); |
||
| 301 | if ( false === $status ) { |
||
| 302 | $return['status'] = 'deleted'; |
||
| 303 | } |
||
| 304 | |||
| 305 | wp_send_json_success( $return ); |
||
| 306 | } |
||
| 307 | |||
| 308 | public function validate_ajax_params( $params ) { |
||
| 309 | $errors = new WP_Error(); |
||
| 310 | |||
| 311 | $illegal_params = array_diff( array_keys( $params ), array( 'product_post_id', 'post_title', 'post_content', 'image_id', 'currency', 'price', 'multiple', 'email' ) ); |
||
| 312 | if ( ! empty( $illegal_params ) ) { |
||
| 313 | $errors.add( 'illegal_params' ); |
||
| 314 | } |
||
| 315 | |||
| 316 | if ( empty( $params['post_title'] ) ) { |
||
| 317 | $errors->add( 'post_title', __( 'People need to know what they\'re paying for! Please add a brief title.' ) ); |
||
| 318 | } |
||
| 319 | |||
| 320 | if ( empty( $params['price'] ) || intval( $params['price'] ) > 0 ) { |
||
| 321 | $errors->add( 'price', __( 'Everything comes with a price tag these days. Please add a your product price.' ) ); |
||
| 322 | } |
||
| 323 | |||
| 324 | if ( empty( $params['email'] ) || ! is_email( $params['email'] ) ) { |
||
| 325 | $errors->add( 'email', __( 'We want to make sure payments reach you, so please add an email address.' ) ); |
||
| 326 | } |
||
| 327 | |||
| 328 | return $errors; |
||
| 329 | } |
||
| 330 | /** |
||
| 331 | * Front-end display of widget. |
||
| 332 | * |
||
| 333 | * @see WP_Widget::widget() |
||
| 334 | * |
||
| 335 | * @param array $args Widget arguments. |
||
| 336 | * @param array $instance Saved values from database. |
||
| 337 | */ |
||
| 338 | function widget( $args, $instance ) { |
||
| 339 | $instance = wp_parse_args( $instance, $this->defaults() ); |
||
| 340 | |||
| 341 | echo $args['before_widget']; |
||
| 342 | |||
| 343 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
||
| 344 | $title = apply_filters( 'widget_title', $instance['title'] ); |
||
| 345 | if ( ! empty( $title ) ) { |
||
| 346 | echo $args['before_title'] . $title . $args['after_title']; |
||
| 347 | } |
||
| 348 | |||
| 349 | echo '<div class="jetpack-simple-payments-content">'; |
||
| 350 | |||
| 351 | if ( ! empty( $instance['form_action'] ) && in_array( $instance['form_action'], array( 'add', 'edit' ) ) && is_customize_preview() ) { |
||
| 352 | require( dirname( __FILE__ ) . '/simple-payments/widget.php' ); |
||
| 353 | } else { |
||
| 354 | if ( ! empty( $instance['product_post_id'] ) ) { |
||
| 355 | $attrs = array( 'id' => $instance['product_post_id'] ); |
||
| 356 | } else { |
||
| 357 | $product_posts = get_posts( array( |
||
| 358 | 'numberposts' => 1, |
||
| 359 | 'orderby' => 'date', |
||
| 360 | 'post_type' => Jetpack_Simple_Payments::$post_type_product, |
||
| 361 | 'post_status' => 'publish', |
||
| 362 | ) ); |
||
| 363 | |||
| 364 | $attrs = array( 'id' => $product_posts[0]->ID ); |
||
| 365 | } |
||
| 366 | |||
| 367 | $jsp = Jetpack_Simple_Payments::getInstance(); |
||
| 368 | $simple_payments_button = $jsp->parse_shortcode( $attrs ); |
||
| 369 | if ( is_null( $simple_payments_button ) && ! is_customize_preview() ) { |
||
| 370 | return; |
||
| 371 | } |
||
| 372 | |||
| 373 | echo $simple_payments_button; |
||
| 374 | } |
||
| 375 | |||
| 376 | echo '</div><!--simple-payments-->'; |
||
| 377 | |||
| 378 | echo $args['after_widget']; |
||
| 379 | |||
| 380 | /** This action is already documented in modules/widgets/gravatar-profile.php */ |
||
| 381 | do_action( 'jetpack_stats_extra', 'widget_view', 'simple_payments' ); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Gets the latests field value from either the old instance or the new instance. |
||
| 386 | * |
||
| 387 | * @param array $mixed Array of values for the new form instance. |
||
| 388 | * @param array $mixed Array of values for the old form instance. |
||
| 389 | * @return mixed $mixed Field value. |
||
| 390 | */ |
||
| 391 | private function get_latest_field_value( $new_instance, $old_instance, $field) { |
||
| 392 | return ! empty( $new_instance[ $field ] ) |
||
| 393 | ? sanitize_text_field( $new_instance[ $field ] ) |
||
| 394 | : $old_instance[ $field ]; |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Gets the product fields from the product post. If no post found |
||
| 505 |