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 |
||
| 7 | class Jetpack_Simple_Payments { |
||
| 8 | // These have to be under 20 chars because that is CPT limit. |
||
| 9 | static $post_type_order = 'jp_pay_order'; |
||
| 10 | static $post_type_product = 'jp_pay_product'; |
||
| 11 | |||
| 12 | static $shortcode = 'simple-payment'; |
||
| 13 | |||
| 14 | static $css_classname_prefix = 'jetpack-simple-payments'; |
||
| 15 | |||
| 16 | // Increase this number each time there's a change in CSS or JS to bust cache. |
||
| 17 | static $version = '0.25'; |
||
| 18 | |||
| 19 | // Classic singleton pattern: |
||
| 20 | private static $instance; |
||
| 21 | private function __construct() {} |
||
| 29 | |||
| 30 | private function register_scripts_and_styles() { |
||
| 40 | |||
| 41 | private function register_init_hooks() { |
||
| 42 | add_action( 'init', array( $this, 'init_hook_action' ) ); |
||
| 43 | add_action( 'rest_api_init', array( $this, 'register_meta_fields_in_rest_api' ) ); |
||
| 44 | } |
||
| 45 | |||
| 46 | private function register_shortcode() { |
||
| 49 | |||
| 50 | public function init_hook_action() { |
||
| 51 | add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) ); |
||
| 52 | add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'allow_sync_post_meta' ) ); |
||
| 53 | if ( ! is_admin() ) { |
||
| 54 | $this->register_scripts_and_styles(); |
||
| 55 | } |
||
| 56 | $this->register_shortcode(); |
||
| 57 | $this->setup_cpts(); |
||
| 58 | |||
| 59 | add_filter( 'the_content', array( $this, 'remove_auto_paragraph_from_product_description' ), 0 ); |
||
| 60 | } |
||
| 61 | |||
| 62 | function remove_auto_paragraph_from_product_description( $content ) { |
||
| 63 | if ( get_post_type() === self::$post_type_product ) { |
||
| 64 | remove_filter( 'the_content', 'wpautop' ); |
||
| 65 | } |
||
| 66 | |||
| 67 | return $content; |
||
| 68 | } |
||
| 69 | |||
| 70 | function get_blog_id() { |
||
| 71 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 72 | return get_current_blog_id(); |
||
| 73 | } |
||
| 74 | |||
| 75 | return Jetpack_Options::get_option( 'id' ); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Used to check whether Simple Payments are enabled for given site. |
||
| 80 | * |
||
| 81 | * @return bool True if Simple Payments are enabled, false otherwise. |
||
| 82 | */ |
||
| 83 | function is_enabled_jetpack_simple_payments() { |
||
| 84 | /** |
||
| 85 | * Can be used by plugin authors to disable the conflicting output of Simple Payments. |
||
| 86 | * |
||
| 87 | * @since 6.3.0 |
||
| 88 | * |
||
| 89 | * @param bool True if Simple Payments should be disabled, false otherwise. |
||
| 90 | */ |
||
| 91 | if ( apply_filters( 'jetpack_disable_simple_payments', false ) ) { |
||
| 92 | return false; |
||
| 93 | } |
||
| 94 | |||
| 95 | // For WPCOM sites |
||
| 96 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'has_blog_sticker' ) ) { |
||
| 97 | $site_id = $this->get_blog_id(); |
||
| 98 | return has_blog_sticker( 'premium-plan', $site_id ) || has_blog_sticker( 'business-plan', $site_id ); |
||
| 99 | } |
||
| 100 | |||
| 101 | // For all Jetpack sites |
||
| 102 | return Jetpack::is_active() && Jetpack::active_plan_supports( 'simple-payments'); |
||
| 103 | } |
||
| 104 | |||
| 105 | function parse_shortcode( $attrs, $content = false ) { |
||
| 106 | if ( empty( $attrs['id'] ) ) { |
||
| 107 | return; |
||
| 108 | } |
||
| 109 | $product = get_post( $attrs['id'] ); |
||
| 110 | if ( ! $product || is_wp_error( $product ) ) { |
||
| 111 | return; |
||
| 112 | } |
||
| 113 | if ( $product->post_type !== self::$post_type_product || 'trash' === $product->post_status ) { |
||
| 114 | return; |
||
| 115 | } |
||
| 116 | |||
| 117 | // We allow for overriding the presentation labels |
||
| 118 | $data = shortcode_atts( array( |
||
| 119 | 'blog_id' => $this->get_blog_id(), |
||
| 120 | 'dom_id' => uniqid( self::$css_classname_prefix . '-' . $product->ID . '_', true ), |
||
| 121 | 'class' => self::$css_classname_prefix . '-' . $product->ID, |
||
| 122 | 'title' => get_the_title( $product ), |
||
| 123 | 'description' => $product->post_content, |
||
| 124 | 'cta' => get_post_meta( $product->ID, 'spay_cta', true ), |
||
| 125 | 'multiple' => get_post_meta( $product->ID, 'spay_multiple', true ) || '0' |
||
| 126 | ), $attrs ); |
||
| 127 | |||
| 128 | $data['price'] = $this->format_price( |
||
| 129 | get_post_meta( $product->ID, 'spay_price', true ), |
||
| 130 | get_post_meta( $product->ID, 'spay_currency', true ) |
||
| 131 | ); |
||
| 132 | |||
| 133 | $data['id'] = $attrs['id']; |
||
| 134 | |||
| 135 | if( ! wp_style_is( 'jetpack-simple-payments', 'enqueue' ) ) { |
||
| 136 | wp_enqueue_style( 'jetpack-simple-payments' ); |
||
| 137 | } |
||
| 138 | |||
| 139 | if ( ! $this->is_enabled_jetpack_simple_payments() ) { |
||
| 140 | return $this->output_admin_warning( $data ); |
||
| 141 | } |
||
| 142 | |||
| 143 | if ( ! wp_script_is( 'paypal-express-checkout', 'enqueued' ) ) { |
||
| 144 | wp_enqueue_script( 'paypal-express-checkout' ); |
||
| 145 | } |
||
| 146 | |||
| 147 | wp_add_inline_script( 'paypal-express-checkout', sprintf( |
||
| 148 | "try{PaypalExpressCheckout.renderButton( '%d', '%d', '%s', '%d' );}catch(e){}", |
||
| 149 | esc_js( $data['blog_id'] ), |
||
| 150 | esc_js( $attrs['id'] ), |
||
| 151 | esc_js( $data['dom_id'] ), |
||
| 152 | esc_js( $data['multiple'] ) |
||
| 153 | ) ); |
||
| 154 | |||
| 155 | return $this->output_shortcode( $data ); |
||
| 156 | } |
||
| 157 | |||
| 158 | function output_admin_warning( $data ) { |
||
| 195 | |||
| 196 | function output_shortcode( $data ) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Format a price with currency |
||
| 257 | * |
||
| 258 | * Uses currency-aware formatting to output a formatted price with a simple fallback. |
||
| 259 | * |
||
| 260 | * Largely inspired by WordPress.com's Store_Price::display_currency |
||
| 261 | * |
||
| 262 | * @param string $price Price. |
||
| 263 | * @param string $currency Currency. |
||
| 264 | * @return string Formatted price. |
||
| 265 | */ |
||
| 266 | private function format_price( $price, $currency ) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Allows custom post types to be used by REST API. |
||
| 287 | * @param $post_types |
||
| 288 | * @see hook 'rest_api_allowed_post_types' |
||
| 289 | * @return array |
||
| 290 | */ |
||
| 291 | function allow_rest_api_types( $post_types ) { |
||
| 296 | |||
| 297 | function allow_sync_post_meta( $post_meta ) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Enable Simple payments custom meta values for access through the REST API. |
||
| 315 | * Field’s value will be exposed on a .meta key in the endpoint response, |
||
| 316 | * and WordPress will handle setting up the callbacks for reading and writing |
||
| 317 | * to that meta key. |
||
| 318 | * |
||
| 319 | * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/ |
||
| 320 | */ |
||
| 321 | public function register_meta_fields_in_rest_api() { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Sanitize three-character ISO-4217 Simple payments currency |
||
| 378 | * |
||
| 379 | * List has to be in sync with list at the client side: |
||
| 380 | * @link https://github.com/Automattic/wp-calypso/blob/6d02ffe73cc073dea7270a22dc30881bff17d8fb/client/lib/simple-payments/constants.js |
||
| 381 | * |
||
| 382 | * Currencies should be supported by PayPal: |
||
| 383 | * @link https://developer.paypal.com/docs/integration/direct/rest/currency-codes/ |
||
| 384 | */ |
||
| 385 | public static function sanitize_currency( $currency ) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Sanitize price: |
||
| 418 | * |
||
| 419 | * Positive integers and floats |
||
| 420 | * Supports two decimal places. |
||
| 421 | * Maximum length: 10. |
||
| 422 | * |
||
| 423 | * See `price` from PayPal docs: |
||
| 424 | * @link https://developer.paypal.com/docs/api/orders/v1/#definition-item |
||
| 425 | * |
||
| 426 | * @param $value |
||
| 427 | * @return null|string |
||
| 428 | */ |
||
| 429 | public static function sanitize_price( $price ) { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Sets up the custom post types for the module. |
||
| 435 | */ |
||
| 436 | function setup_cpts() { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Format a price for display |
||
| 526 | * |
||
| 527 | * Largely taken from WordPress.com Store_Price class |
||
| 528 | * |
||
| 529 | * The currency array will have the shape: |
||
| 530 | * format => string sprintf format with placeholders `%1$s`: Symbol `%2$s`: Price. |
||
| 531 | * symbol => string Symbol string |
||
| 532 | * desc => string Text description of currency |
||
| 533 | * decimal => int Number of decimal places |
||
| 534 | * |
||
| 535 | * @param string $the_currency The desired currency, e.g. 'USD'. |
||
| 536 | * @return ?array Currency object or null if not found. |
||
|
|
|||
| 537 | */ |
||
| 538 | private static function get_currency( $the_currency ) { |
||
| 667 | } |
||
| 668 | Jetpack_Simple_Payments::getInstance(); |
||
| 669 |
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.