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 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, and based on these observations, apply Extract Interface, too.
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
| 13 | class Jetpack_Simple_Payments { |
||
| 14 | // These have to be under 20 chars because that is CPT limit. |
||
| 15 | static $post_type_order = 'jp_pay_order'; |
||
| 16 | static $post_type_product = 'jp_pay_product'; |
||
| 17 | |||
| 18 | static $shortcode = 'simple-payment'; |
||
| 19 | |||
| 20 | static $css_classname_prefix = 'jetpack-simple-payments'; |
||
| 21 | |||
| 22 | static $required_plan; |
||
| 23 | |||
| 24 | // Increase this number each time there's a change in CSS or JS to bust cache. |
||
| 25 | static $version = '0.25'; |
||
| 26 | |||
| 27 | // Classic singleton pattern: |
||
| 28 | private static $instance; |
||
| 29 | private function __construct() {} |
||
| 38 | |||
| 39 | private function register_scripts_and_styles() { |
||
| 49 | |||
| 50 | private function register_init_hooks() { |
||
| 54 | |||
| 55 | private function register_shortcode() { |
||
| 58 | |||
| 59 | public function init_hook_action() { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Enqueue the static assets needed in the frontend. |
||
| 73 | */ |
||
| 74 | public function enqueue_frontend_assets() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Add an inline script for setting up the PayPal checkout button. |
||
| 86 | * |
||
| 87 | * @param int $id Product ID. |
||
| 88 | * @param int $dom_id ID of the DOM element with the purchase message. |
||
| 89 | * @param boolean $is_multiple Whether multiple items of the same product can be purchased. |
||
| 90 | */ |
||
| 91 | public function setup_paypal_checkout_button( $id, $dom_id, $is_multiple ) { |
||
| 103 | |||
| 104 | function remove_auto_paragraph_from_product_description( $content ) { |
||
| 111 | |||
| 112 | function get_blog_id() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Used to check whether Simple Payments are enabled for given site. |
||
| 122 | * |
||
| 123 | * @return bool True if Simple Payments are enabled, false otherwise. |
||
| 124 | */ |
||
| 125 | function is_enabled_jetpack_simple_payments() { |
||
| 126 | /** |
||
| 127 | * Can be used by plugin authors to disable the conflicting output of Simple Payments. |
||
| 128 | * |
||
| 129 | * @since 6.3.0 |
||
| 130 | * |
||
| 131 | * @param bool True if Simple Payments should be disabled, false otherwise. |
||
| 132 | */ |
||
| 133 | if ( apply_filters( 'jetpack_disable_simple_payments', false ) ) { |
||
| 134 | return false; |
||
| 135 | } |
||
| 136 | |||
| 137 | // For WPCOM sites |
||
| 138 | View Code Duplication | if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'has_any_blog_stickers' ) ) { |
|
| 139 | $site_id = $this->get_blog_id(); |
||
| 140 | return has_any_blog_stickers( array( 'premium-plan', 'business-plan', 'ecommerce-plan' ), $site_id ); |
||
| 141 | } |
||
| 142 | |||
| 143 | // For all Jetpack sites |
||
| 144 | return Jetpack::is_active() && Jetpack_Plan::supports( 'simple-payments'); |
||
| 145 | } |
||
| 146 | |||
| 147 | function parse_shortcode( $attrs, $content = false ) { |
||
| 189 | |||
| 190 | function output_admin_warning( $data ) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get the HTML output to use as PayPal purchase box. |
||
| 203 | * |
||
| 204 | * @param string $dom_id ID of the DOM element with the purchase message. |
||
| 205 | * @param boolean $is_multiple Whether multiple items of the same product can be purchased. |
||
| 206 | * |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | public function output_purchase_box( $dom_id, $is_multiple ) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Get the HTML output to replace the `simple-payments` shortcode. |
||
| 238 | * |
||
| 239 | * @param array $data Product data. |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | public function output_shortcode( $data ) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Format a price with currency |
||
| 283 | * |
||
| 284 | * Uses currency-aware formatting to output a formatted price with a simple fallback. |
||
| 285 | * |
||
| 286 | * Largely inspired by WordPress.com's Store_Price::display_currency |
||
| 287 | * |
||
| 288 | * @param string $price Price. |
||
| 289 | * @param string $currency Currency. |
||
| 290 | * @return string Formatted price. |
||
| 291 | */ |
||
| 292 | private function format_price( $price, $currency ) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Allows custom post types to be used by REST API. |
||
| 299 | * @param $post_types |
||
| 300 | * @see hook 'rest_api_allowed_post_types' |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | function allow_rest_api_types( $post_types ) { |
||
| 308 | |||
| 309 | function allow_sync_post_meta( $post_meta ) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Enable Simple payments custom meta values for access through the REST API. |
||
| 327 | * Field’s value will be exposed on a .meta key in the endpoint response, |
||
| 328 | * and WordPress will handle setting up the callbacks for reading and writing |
||
| 329 | * to that meta key. |
||
| 330 | * |
||
| 331 | * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/ |
||
| 332 | */ |
||
| 333 | public function register_meta_fields_in_rest_api() { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Sanitize three-character ISO-4217 Simple payments currency |
||
| 390 | * |
||
| 391 | * List has to be in sync with list at the block's client side and widget's backend side: |
||
| 392 | * @link https://github.com/Automattic/jetpack/blob/31efa189ad223c0eb7ad085ac0650a23facf9ef5/extensions/blocks/simple-payments/constants.js#L9-L39 |
||
| 393 | * @link https://github.com/Automattic/jetpack/blob/31efa189ad223c0eb7ad085ac0650a23facf9ef5/modules/widgets/simple-payments.php#L19-L44 |
||
| 394 | * |
||
| 395 | * Currencies should be supported by PayPal: |
||
| 396 | * @link https://developer.paypal.com/docs/api/reference/currency-codes/ |
||
| 397 | * |
||
| 398 | * Indian Rupee (INR) not supported because at the time of the creation of this file |
||
| 399 | * because it's limited to in-country PayPal India accounts only. |
||
| 400 | * Discussion: https://github.com/Automattic/wp-calypso/pull/28236 |
||
| 401 | */ |
||
| 402 | public static function sanitize_currency( $currency ) { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Sanitize price: |
||
| 435 | * |
||
| 436 | * Positive integers and floats |
||
| 437 | * Supports two decimal places. |
||
| 438 | * Maximum length: 10. |
||
| 439 | * |
||
| 440 | * See `price` from PayPal docs: |
||
| 441 | * @link https://developer.paypal.com/docs/api/orders/v1/#definition-item |
||
| 442 | * |
||
| 443 | * @param $value |
||
| 444 | * @return null|string |
||
| 445 | */ |
||
| 446 | public static function sanitize_price( $price ) { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Sets up the custom post types for the module. |
||
| 452 | */ |
||
| 453 | function setup_cpts() { |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Format a price for display |
||
| 543 | * |
||
| 544 | * Largely taken from WordPress.com Store_Price class |
||
| 545 | * |
||
| 546 | * The currency array will have the shape: |
||
| 547 | * format => string sprintf format with placeholders `%1$s`: Symbol `%2$s`: Price. |
||
| 548 | * symbol => string Symbol string |
||
| 549 | * desc => string Text description of currency |
||
| 550 | * decimal => int Number of decimal places |
||
| 551 | * |
||
| 552 | * @param string $the_currency The desired currency, e.g. 'USD'. |
||
| 553 | * @return ?array Currency object or null if not found. |
||
|
|
|||
| 554 | */ |
||
| 555 | private static function get_currency( $the_currency ) { |
||
| 564 | } |
||
| 565 | Jetpack_Simple_Payments::getInstance(); |
||
| 566 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.