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 |
||
| 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 | static $required_plan; |
||
| 17 | |||
| 18 | // Increase this number each time there's a change in CSS or JS to bust cache. |
||
| 19 | static $version = '0.25'; |
||
| 20 | |||
| 21 | // Classic singleton pattern: |
||
| 22 | private static $instance; |
||
| 23 | private function __construct() {} |
||
| 24 | View Code Duplication | static function getInstance() { |
|
| 25 | if ( ! self::$instance ) { |
||
| 26 | self::$instance = new self(); |
||
| 27 | self::$instance->register_init_hooks(); |
||
| 28 | self::$required_plan = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? 'value_bundle' : 'jetpack_premium'; |
||
| 29 | } |
||
| 30 | return self::$instance; |
||
| 31 | } |
||
| 32 | |||
| 33 | private function register_scripts_and_styles() { |
||
| 34 | /** |
||
| 35 | * Paypal heavily discourages putting that script in your own server: |
||
| 36 | * @see https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/add-paypal-button/ |
||
| 37 | */ |
||
| 38 | wp_register_script( 'paypal-checkout-js', 'https://www.paypalobjects.com/api/checkout.js', array(), null, true ); |
||
| 39 | wp_register_script( 'paypal-express-checkout', plugins_url( '/paypal-express-checkout.js', __FILE__ ), |
||
| 40 | array( 'jquery', 'paypal-checkout-js' ), self::$version ); |
||
| 41 | wp_register_style( 'jetpack-simple-payments', plugins_url( '/simple-payments.css', __FILE__ ), array( 'dashicons' ) ); |
||
| 42 | } |
||
| 43 | |||
| 44 | private function register_init_hooks() { |
||
| 45 | add_action( 'init', array( $this, 'init_hook_action' ) ); |
||
| 46 | add_action( 'jetpack_register_gutenberg_extensions', array( $this, 'register_gutenberg_block' ) ); |
||
| 47 | add_action( 'rest_api_init', array( $this, 'register_meta_fields_in_rest_api' ) ); |
||
| 48 | } |
||
| 49 | |||
| 50 | private function register_shortcode() { |
||
| 51 | add_shortcode( self::$shortcode, array( $this, 'parse_shortcode' ) ); |
||
| 52 | } |
||
| 53 | |||
| 54 | public function init_hook_action() { |
||
| 55 | add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) ); |
||
| 56 | add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'allow_sync_post_meta' ) ); |
||
| 57 | if ( ! is_admin() ) { |
||
| 58 | $this->register_scripts_and_styles(); |
||
| 59 | } |
||
| 60 | $this->register_shortcode(); |
||
| 61 | $this->setup_cpts(); |
||
| 62 | |||
| 63 | add_filter( 'the_content', array( $this, 'remove_auto_paragraph_from_product_description' ), 0 ); |
||
| 64 | } |
||
| 65 | |||
| 66 | function register_gutenberg_block() { |
||
| 67 | if ( $this->is_enabled_jetpack_simple_payments() ) { |
||
| 68 | jetpack_register_block( 'jetpack/simple-payments' ); |
||
| 69 | } else { |
||
| 70 | Jetpack_Gutenberg::set_extension_unavailable( |
||
| 71 | 'jetpack/simple-payments', |
||
| 72 | 'missing_plan', |
||
| 73 | array( |
||
| 74 | 'required_feature' => 'simple-payments', |
||
| 75 | 'required_plan' => self::$required_plan, |
||
| 76 | ) |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | function remove_auto_paragraph_from_product_description( $content ) { |
||
| 82 | if ( get_post_type() === self::$post_type_product ) { |
||
| 83 | remove_filter( 'the_content', 'wpautop' ); |
||
| 84 | } |
||
| 85 | |||
| 86 | return $content; |
||
| 87 | } |
||
| 88 | |||
| 89 | function get_blog_id() { |
||
| 90 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 91 | return get_current_blog_id(); |
||
| 92 | } |
||
| 93 | |||
| 94 | return Jetpack_Options::get_option( 'id' ); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Used to check whether Simple Payments are enabled for given site. |
||
| 99 | * |
||
| 100 | * @return bool True if Simple Payments are enabled, false otherwise. |
||
| 101 | */ |
||
| 102 | function is_enabled_jetpack_simple_payments() { |
||
| 103 | /** |
||
| 104 | * Can be used by plugin authors to disable the conflicting output of Simple Payments. |
||
| 105 | * |
||
| 106 | * @since 6.3.0 |
||
| 107 | * |
||
| 108 | * @param bool True if Simple Payments should be disabled, false otherwise. |
||
| 109 | */ |
||
| 110 | if ( apply_filters( 'jetpack_disable_simple_payments', false ) ) { |
||
| 111 | return false; |
||
| 112 | } |
||
| 113 | |||
| 114 | // For WPCOM sites |
||
| 115 | View Code Duplication | if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'has_any_blog_stickers' ) ) { |
|
| 116 | $site_id = $this->get_blog_id(); |
||
| 117 | return has_any_blog_stickers( array( 'premium-plan', 'business-plan', 'ecommerce-plan' ), $site_id ); |
||
| 118 | } |
||
| 119 | |||
| 120 | // For all Jetpack sites |
||
| 121 | error_log( print_r(Jetpack::is_active() ? 'Jetpack::is_active true' : 'Jetpack::is_active false ', 1) ); |
||
| 122 | error_log( print_r(Jetpack_Plan::supports( 'simple-payments') ? 'Jetpack_Plan::supports true' : 'Jetpack_Plan::supports false ', 1) ); |
||
| 123 | return Jetpack::is_active() && Jetpack_Plan::supports( 'simple-payments'); |
||
| 124 | } |
||
| 125 | |||
| 126 | function parse_shortcode( $attrs, $content = false ) { |
||
| 127 | if ( empty( $attrs['id'] ) ) { |
||
| 128 | return; |
||
| 129 | } |
||
| 130 | $product = get_post( $attrs['id'] ); |
||
| 131 | if ( ! $product || is_wp_error( $product ) ) { |
||
| 132 | return; |
||
| 133 | } |
||
| 134 | if ( $product->post_type !== self::$post_type_product || 'publish' !== $product->post_status ) { |
||
| 135 | return; |
||
| 136 | } |
||
| 137 | |||
| 138 | // We allow for overriding the presentation labels |
||
| 139 | $data = shortcode_atts( array( |
||
| 140 | 'blog_id' => $this->get_blog_id(), |
||
| 141 | 'dom_id' => uniqid( self::$css_classname_prefix . '-' . $product->ID . '_', true ), |
||
| 142 | 'class' => self::$css_classname_prefix . '-' . $product->ID, |
||
| 143 | 'title' => get_the_title( $product ), |
||
| 144 | 'description' => $product->post_content, |
||
| 145 | 'cta' => get_post_meta( $product->ID, 'spay_cta', true ), |
||
| 146 | 'multiple' => get_post_meta( $product->ID, 'spay_multiple', true ) || '0' |
||
| 147 | ), $attrs ); |
||
| 148 | |||
| 149 | $data['price'] = $this->format_price( |
||
| 150 | get_post_meta( $product->ID, 'spay_price', true ), |
||
| 151 | get_post_meta( $product->ID, 'spay_currency', true ) |
||
| 152 | ); |
||
| 153 | |||
| 154 | $data['id'] = $attrs['id']; |
||
| 155 | |||
| 156 | if( ! wp_style_is( 'jetpack-simple-payments', 'enqueued' ) ) { |
||
| 157 | wp_enqueue_style( 'jetpack-simple-payments' ); |
||
| 158 | } |
||
| 159 | |||
| 160 | if ( ! $this->is_enabled_jetpack_simple_payments() ) { |
||
| 161 | return $this->output_admin_warning( $data ); |
||
| 162 | } |
||
| 163 | |||
| 164 | if ( ! wp_script_is( 'paypal-express-checkout', 'enqueued' ) ) { |
||
| 165 | wp_enqueue_script( 'paypal-express-checkout' ); |
||
| 166 | } |
||
| 167 | |||
| 168 | wp_add_inline_script( 'paypal-express-checkout', sprintf( |
||
| 169 | "try{PaypalExpressCheckout.renderButton( '%d', '%d', '%s', '%d' );}catch(e){}", |
||
| 170 | esc_js( $data['blog_id'] ), |
||
| 171 | esc_js( $attrs['id'] ), |
||
| 172 | esc_js( $data['dom_id'] ), |
||
| 173 | esc_js( $data['multiple'] ) |
||
| 174 | ) ); |
||
| 175 | |||
| 176 | return $this->output_shortcode( $data ); |
||
| 177 | } |
||
| 178 | |||
| 179 | function output_admin_warning( $data ) { |
||
| 180 | if ( ! current_user_can( 'manage_options' ) ) { |
||
| 181 | return; |
||
| 182 | } |
||
| 183 | |||
| 184 | jetpack_require_lib( 'components' ); |
||
| 185 | return Jetpack_Components::render_upgrade_nudge( array( |
||
| 186 | 'plan' => self::$required_plan |
||
| 187 | ) ); |
||
| 188 | } |
||
| 189 | |||
| 190 | function output_shortcode( $data ) { |
||
| 191 | $items = ''; |
||
| 192 | $css_prefix = self::$css_classname_prefix; |
||
| 193 | |||
| 194 | if ( $data['multiple'] ) { |
||
| 195 | $items = sprintf( ' |
||
| 196 | <div class="%1$s"> |
||
| 197 | <input class="%2$s" type="number" value="1" min="1" id="%3$s" /> |
||
| 198 | </div> |
||
| 199 | ', |
||
| 200 | esc_attr( "${css_prefix}-items" ), |
||
| 201 | esc_attr( "${css_prefix}-items-number" ), |
||
| 202 | esc_attr( "{$data['dom_id']}_number" ) |
||
| 203 | ); |
||
| 204 | } |
||
| 205 | $image = ""; |
||
| 206 | if( has_post_thumbnail( $data['id'] ) ) { |
||
| 207 | $image = sprintf( '<div class="%1$s"><div class="%2$s">%3$s</div></div>', |
||
| 208 | esc_attr( "${css_prefix}-product-image" ), |
||
| 209 | esc_attr( "${css_prefix}-image" ), |
||
| 210 | get_the_post_thumbnail( $data['id'], 'full' ) |
||
| 211 | ); |
||
| 212 | } |
||
| 213 | return sprintf( ' |
||
| 214 | <div class="%1$s"> |
||
| 215 | <div class="%2$s"> |
||
| 216 | %3$s |
||
| 217 | <div class="%4$s"> |
||
| 218 | <div class="%5$s"><p>%6$s</p></div> |
||
| 219 | <div class="%7$s"><p>%8$s</p></div> |
||
| 220 | <div class="%9$s"><p>%10$s</p></div> |
||
| 221 | <div class="%11$s" id="%12$s"></div> |
||
| 222 | <div class="%13$s"> |
||
| 223 | %14$s |
||
| 224 | <div class="%15$s" id="%16$s"></div> |
||
| 225 | </div> |
||
| 226 | </div> |
||
| 227 | </div> |
||
| 228 | </div> |
||
| 229 | ', |
||
| 230 | esc_attr( "{$data['class']} ${css_prefix}-wrapper" ), |
||
| 231 | esc_attr( "${css_prefix}-product" ), |
||
| 232 | $image, |
||
| 233 | esc_attr( "${css_prefix}-details" ), |
||
| 234 | esc_attr( "${css_prefix}-title" ), |
||
| 235 | esc_html( $data['title'] ), |
||
| 236 | esc_attr( "${css_prefix}-description" ), |
||
| 237 | wp_kses( $data['description'], wp_kses_allowed_html( 'post' ) ), |
||
| 238 | esc_attr( "${css_prefix}-price" ), |
||
| 239 | esc_html( $data['price'] ), |
||
| 240 | esc_attr( "${css_prefix}-purchase-message" ), |
||
| 241 | esc_attr( "{$data['dom_id']}-message-container" ), |
||
| 242 | esc_attr( "${css_prefix}-purchase-box" ), |
||
| 243 | $items, |
||
| 244 | esc_attr( "${css_prefix}-button" ), |
||
| 245 | esc_attr( "{$data['dom_id']}_button" ) |
||
| 246 | ); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Format a price with currency |
||
| 251 | * |
||
| 252 | * Uses currency-aware formatting to output a formatted price with a simple fallback. |
||
| 253 | * |
||
| 254 | * Largely inspired by WordPress.com's Store_Price::display_currency |
||
| 255 | * |
||
| 256 | * @param string $price Price. |
||
| 257 | * @param string $currency Currency. |
||
| 258 | * @return string Formatted price. |
||
| 259 | */ |
||
| 260 | private function format_price( $price, $currency ) { |
||
| 261 | $currency_details = self::get_currency( $currency ); |
||
| 262 | |||
| 263 | if ( $currency_details ) { |
||
| 264 | // Ensure USD displays as 1234.56 even in non-US locales. |
||
| 265 | $amount = 'USD' === $currency |
||
| 266 | ? number_format( $price, $currency_details['decimal'], '.', ',' ) |
||
| 267 | : number_format_i18n( $price, $currency_details['decimal'] ); |
||
| 268 | |||
| 269 | return sprintf( |
||
| 270 | $currency_details['format'], |
||
| 271 | $currency_details['symbol'], |
||
| 272 | $amount |
||
| 273 | ); |
||
| 274 | } |
||
| 275 | |||
| 276 | // Fall back to unspecified currency symbol like `¤1,234.05`. |
||
| 277 | // @link https://en.wikipedia.org/wiki/Currency_sign_(typography). |
||
| 278 | if ( ! $currency ) { |
||
| 279 | return '¤' . number_format_i18n( $price, 2 ); |
||
| 280 | } |
||
| 281 | |||
| 282 | return number_format_i18n( $price, 2 ) . ' ' . $currency; |
||
| 283 | } |
||
| 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.