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() {} |
||
| 30 | View Code Duplication | static function getInstance() { |
|
| 31 | if ( ! self::$instance ) { |
||
| 32 | self::$instance = new self(); |
||
| 33 | self::$instance->register_init_hooks(); |
||
| 34 | self::$required_plan = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? 'value_bundle' : 'jetpack_premium'; |
||
| 35 | } |
||
| 36 | return self::$instance; |
||
| 37 | } |
||
| 38 | |||
| 39 | private function register_scripts_and_styles() { |
||
| 40 | /** |
||
| 41 | * Paypal heavily discourages putting that script in your own server: |
||
| 42 | * @see https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/add-paypal-button/ |
||
| 43 | */ |
||
| 44 | wp_register_script( 'paypal-checkout-js', 'https://www.paypalobjects.com/api/checkout.js', array(), null, true ); |
||
| 45 | wp_register_script( 'paypal-express-checkout', plugins_url( '/paypal-express-checkout.js', __FILE__ ), |
||
| 46 | array( 'jquery', 'paypal-checkout-js' ), self::$version ); |
||
| 47 | wp_register_style( 'jetpack-simple-payments', plugins_url( '/simple-payments.css', __FILE__ ), array( 'dashicons' ) ); |
||
| 48 | } |
||
| 49 | |||
| 50 | private function register_init_hooks() { |
||
| 51 | add_action( 'init', array( $this, 'init_hook_action' ) ); |
||
| 52 | add_action( 'rest_api_init', array( $this, 'register_meta_fields_in_rest_api' ) ); |
||
| 53 | } |
||
| 54 | |||
| 55 | private function register_shortcode() { |
||
| 56 | add_shortcode( self::$shortcode, array( $this, 'parse_shortcode' ) ); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function init_hook_action() { |
||
| 60 | add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) ); |
||
| 61 | add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'allow_sync_post_meta' ) ); |
||
| 62 | if ( ! is_admin() ) { |
||
| 63 | $this->register_scripts_and_styles(); |
||
| 64 | } |
||
| 65 | $this->register_shortcode(); |
||
| 66 | $this->setup_cpts(); |
||
| 67 | |||
| 68 | add_filter( 'the_content', array( $this, 'remove_auto_paragraph_from_product_description' ), 0 ); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Enqueue the static assets needed in the frontend. |
||
| 73 | */ |
||
| 74 | public function enqueue_frontend_assets() { |
||
| 75 | if ( ! wp_style_is( 'jetpack-simple-payments', 'enqueued' ) ) { |
||
| 76 | wp_enqueue_style( 'jetpack-simple-payments' ); |
||
| 77 | } |
||
| 78 | |||
| 79 | if ( ! wp_script_is( 'paypal-express-checkout', 'enqueued' ) ) { |
||
| 80 | wp_enqueue_script( 'paypal-express-checkout' ); |
||
| 81 | } |
||
| 82 | } |
||
| 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 ) { |
||
| 92 | wp_add_inline_script( |
||
| 93 | 'paypal-express-checkout', |
||
| 94 | sprintf( |
||
| 95 | "try{PaypalExpressCheckout.renderButton( '%d', '%d', '%s', '%d' );}catch(e){}", |
||
| 96 | esc_js( $this->get_blog_id() ), |
||
| 97 | esc_js( $id ), |
||
| 98 | esc_js( $dom_id ), |
||
| 99 | esc_js( $is_multiple ) |
||
| 100 | ) |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | |||
| 104 | function remove_auto_paragraph_from_product_description( $content ) { |
||
| 105 | if ( get_post_type() === self::$post_type_product ) { |
||
| 106 | remove_filter( 'the_content', 'wpautop' ); |
||
| 107 | } |
||
| 108 | |||
| 109 | return $content; |
||
| 110 | } |
||
| 111 | |||
| 112 | function get_blog_id() { |
||
| 113 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 114 | return get_current_blog_id(); |
||
| 115 | } |
||
| 116 | |||
| 117 | return Jetpack_Options::get_option( 'id' ); |
||
| 118 | } |
||
| 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 ) { |
||
| 148 | if ( empty( $attrs['id'] ) ) { |
||
| 149 | return; |
||
| 150 | } |
||
| 151 | $product = get_post( $attrs['id'] ); |
||
| 152 | if ( ! $product || is_wp_error( $product ) ) { |
||
| 153 | return; |
||
| 154 | } |
||
| 155 | if ( $product->post_type !== self::$post_type_product || 'publish' !== $product->post_status ) { |
||
| 156 | return; |
||
| 157 | } |
||
| 158 | |||
| 159 | // We allow for overriding the presentation labels |
||
| 160 | $data = shortcode_atts( array( |
||
| 161 | 'blog_id' => $this->get_blog_id(), |
||
| 162 | 'dom_id' => uniqid( self::$css_classname_prefix . '-' . $product->ID . '_', true ), |
||
| 163 | 'class' => self::$css_classname_prefix . '-' . $product->ID, |
||
| 164 | 'title' => get_the_title( $product ), |
||
| 165 | 'description' => $product->post_content, |
||
| 166 | 'cta' => get_post_meta( $product->ID, 'spay_cta', true ), |
||
| 167 | 'multiple' => get_post_meta( $product->ID, 'spay_multiple', true ) || '0' |
||
| 168 | ), $attrs ); |
||
| 169 | |||
| 170 | $data['price'] = $this->format_price( |
||
| 171 | get_post_meta( $product->ID, 'spay_price', true ), |
||
| 172 | get_post_meta( $product->ID, 'spay_currency', true ) |
||
| 173 | ); |
||
| 174 | |||
| 175 | $data['id'] = $attrs['id']; |
||
| 176 | |||
| 177 | if ( ! $this->is_enabled_jetpack_simple_payments() ) { |
||
| 178 | if ( jetpack_is_frontend() ) { |
||
| 179 | return $this->output_admin_warning( $data ); |
||
| 180 | } |
||
| 181 | return; |
||
| 182 | } |
||
| 183 | |||
| 184 | $this->enqueue_frontend_assets(); |
||
| 185 | $this->setup_paypal_checkout_button( $attrs['id'], $data['dom_id'], $data['multiple'] ); |
||
| 186 | |||
| 187 | return $this->output_shortcode( $data ); |
||
| 188 | } |
||
| 189 | |||
| 190 | function output_admin_warning( $data ) { |
||
| 191 | if ( ! current_user_can( 'manage_options' ) ) { |
||
| 192 | return; |
||
| 193 | } |
||
| 194 | |||
| 195 | jetpack_require_lib( 'components' ); |
||
| 196 | return Jetpack_Components::render_upgrade_nudge( array( |
||
| 197 | 'plan' => self::$required_plan |
||
| 198 | ) ); |
||
| 199 | } |
||
| 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 ) { |
||
| 210 | $items = ''; |
||
| 211 | $css_prefix = self::$css_classname_prefix; |
||
| 212 | |||
| 213 | if ( $is_multiple ) { |
||
| 214 | $items = sprintf( ' |
||
| 215 | <div class="%1$s"> |
||
| 216 | <input class="%2$s" type="number" value="1" min="1" id="%3$s" /> |
||
| 217 | </div> |
||
| 218 | ', |
||
| 219 | esc_attr( "${css_prefix}-items" ), |
||
| 220 | esc_attr( "${css_prefix}-items-number" ), |
||
| 221 | esc_attr( "{$dom_id}_number" ) |
||
| 222 | ); |
||
| 223 | } |
||
| 224 | |||
| 225 | return sprintf( |
||
| 226 | '<div class="%1$s" id="%2$s"></div><div class="%3$s">%4$s<div class="%5$s" id="%6$s"></div></div>', |
||
| 227 | esc_attr( "${css_prefix}-purchase-message" ), |
||
| 228 | esc_attr( "{$dom_id}-message-container" ), |
||
| 229 | esc_attr( "${css_prefix}-purchase-box" ), |
||
| 230 | $items, |
||
| 231 | esc_attr( "${css_prefix}-button" ), |
||
| 232 | esc_attr( "{$dom_id}_button" ) |
||
| 233 | ); |
||
| 234 | } |
||
| 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 ) { |
||
| 243 | $css_prefix = self::$css_classname_prefix; |
||
| 244 | |||
| 245 | $image = ""; |
||
| 246 | if( has_post_thumbnail( $data['id'] ) ) { |
||
| 247 | $image = sprintf( '<div class="%1$s"><div class="%2$s">%3$s</div></div>', |
||
| 248 | esc_attr( "${css_prefix}-product-image" ), |
||
| 249 | esc_attr( "${css_prefix}-image" ), |
||
| 250 | get_the_post_thumbnail( $data['id'], 'full' ) |
||
| 251 | ); |
||
| 252 | } |
||
| 253 | |||
| 254 | return sprintf( ' |
||
| 255 | <div class="%1$s"> |
||
| 256 | <div class="%2$s"> |
||
| 257 | %3$s |
||
| 258 | <div class="%4$s"> |
||
| 259 | <div class="%5$s"><p>%6$s</p></div> |
||
| 260 | <div class="%7$s"><p>%8$s</p></div> |
||
| 261 | <div class="%9$s"><p>%10$s</p></div> |
||
| 262 | %11$s |
||
| 263 | </div> |
||
| 264 | </div> |
||
| 265 | </div> |
||
| 266 | ', |
||
| 267 | esc_attr( "{$data['class']} ${css_prefix}-wrapper" ), |
||
| 268 | esc_attr( "${css_prefix}-product" ), |
||
| 269 | $image, |
||
| 270 | esc_attr( "${css_prefix}-details" ), |
||
| 271 | esc_attr( "${css_prefix}-title" ), |
||
| 272 | esc_html( $data['title'] ), |
||
| 273 | esc_attr( "${css_prefix}-description" ), |
||
| 274 | wp_kses( $data['description'], wp_kses_allowed_html( 'post' ) ), |
||
| 275 | esc_attr( "${css_prefix}-price" ), |
||
| 276 | esc_html( $data['price'] ), |
||
| 277 | $this->output_purchase_box( $data['dom_id'], $data['multiple'] ) |
||
| 278 | ); |
||
| 279 | } |
||
| 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 ) { |
||
| 293 | jetpack_require_lib( 'class-jetpack-currencies' ); |
||
| 294 | return Jetpack_Currencies::format_price( $price, $currency ); |
||
| 295 | } |
||
| 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 ) { |
||
| 403 | $valid_currencies = array( |
||
| 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.