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 | if ( $this->is_enabled_jetpack_simple_payments() ) { |
||
62 | jetpack_register_block( 'simple-payments' ); |
||
63 | } else { |
||
64 | jetpack_register_block( 'simple-payments', array(), array( 'available' => false, 'unavailable_reason' => 'missing_plan' ) ); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | function remove_auto_paragraph_from_product_description( $content ) { |
||
69 | if ( get_post_type() === self::$post_type_product ) { |
||
70 | remove_filter( 'the_content', 'wpautop' ); |
||
71 | } |
||
72 | |||
73 | return $content; |
||
74 | } |
||
75 | |||
76 | function get_blog_id() { |
||
77 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
78 | return get_current_blog_id(); |
||
79 | } |
||
80 | |||
81 | return Jetpack_Options::get_option( 'id' ); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Used to check whether Simple Payments are enabled for given site. |
||
86 | * |
||
87 | * @return bool True if Simple Payments are enabled, false otherwise. |
||
88 | */ |
||
89 | function is_enabled_jetpack_simple_payments() { |
||
90 | /** |
||
91 | * Can be used by plugin authors to disable the conflicting output of Simple Payments. |
||
92 | * |
||
93 | * @since 6.3.0 |
||
94 | * |
||
95 | * @param bool True if Simple Payments should be disabled, false otherwise. |
||
96 | */ |
||
97 | if ( apply_filters( 'jetpack_disable_simple_payments', false ) ) { |
||
98 | return false; |
||
99 | } |
||
100 | |||
101 | // For WPCOM sites |
||
102 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'has_any_blog_stickers' ) ) { |
||
103 | $site_id = $this->get_blog_id(); |
||
104 | return has_any_blog_stickers( array( 'premium-plan', 'business-plan', 'ecommerce-plan' ), $site_id ); |
||
105 | } |
||
106 | |||
107 | // For all Jetpack sites |
||
108 | return Jetpack::is_active() && Jetpack::active_plan_supports( 'simple-payments'); |
||
109 | } |
||
110 | |||
111 | function parse_shortcode( $attrs, $content = false ) { |
||
112 | if ( empty( $attrs['id'] ) ) { |
||
113 | return; |
||
114 | } |
||
115 | $product = get_post( $attrs['id'] ); |
||
116 | if ( ! $product || is_wp_error( $product ) ) { |
||
117 | return; |
||
118 | } |
||
119 | if ( $product->post_type !== self::$post_type_product || 'publish' !== $product->post_status ) { |
||
120 | return; |
||
121 | } |
||
122 | |||
123 | // We allow for overriding the presentation labels |
||
124 | $data = shortcode_atts( array( |
||
125 | 'blog_id' => $this->get_blog_id(), |
||
126 | 'dom_id' => uniqid( self::$css_classname_prefix . '-' . $product->ID . '_', true ), |
||
127 | 'class' => self::$css_classname_prefix . '-' . $product->ID, |
||
128 | 'title' => get_the_title( $product ), |
||
129 | 'description' => $product->post_content, |
||
130 | 'cta' => get_post_meta( $product->ID, 'spay_cta', true ), |
||
131 | 'multiple' => get_post_meta( $product->ID, 'spay_multiple', true ) || '0' |
||
132 | ), $attrs ); |
||
133 | |||
134 | $data['price'] = $this->format_price( |
||
135 | get_post_meta( $product->ID, 'spay_price', true ), |
||
136 | get_post_meta( $product->ID, 'spay_currency', true ) |
||
137 | ); |
||
138 | |||
139 | $data['id'] = $attrs['id']; |
||
140 | |||
141 | if( ! wp_style_is( 'jetpack-simple-payments', 'enqueue' ) ) { |
||
142 | wp_enqueue_style( 'jetpack-simple-payments' ); |
||
143 | } |
||
144 | |||
145 | if ( ! $this->is_enabled_jetpack_simple_payments() ) { |
||
146 | return $this->output_admin_warning( $data ); |
||
147 | } |
||
148 | |||
149 | if ( ! wp_script_is( 'paypal-express-checkout', 'enqueued' ) ) { |
||
150 | wp_enqueue_script( 'paypal-express-checkout' ); |
||
151 | } |
||
152 | |||
153 | wp_add_inline_script( 'paypal-express-checkout', sprintf( |
||
154 | "try{PaypalExpressCheckout.renderButton( '%d', '%d', '%s', '%d' );}catch(e){}", |
||
155 | esc_js( $data['blog_id'] ), |
||
156 | esc_js( $attrs['id'] ), |
||
157 | esc_js( $data['dom_id'] ), |
||
158 | esc_js( $data['multiple'] ) |
||
159 | ) ); |
||
160 | |||
161 | return $this->output_shortcode( $data ); |
||
162 | } |
||
163 | |||
164 | function output_admin_warning( $data ) { |
||
165 | if ( ! current_user_can( 'manage_options' ) ) { |
||
166 | return; |
||
167 | } |
||
168 | $css_prefix = self::$css_classname_prefix; |
||
169 | |||
170 | $support_url = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) |
||
171 | ? 'https://support.wordpress.com/simple-payments/' |
||
172 | : 'https://jetpack.com/support/simple-payment-button/'; |
||
173 | |||
174 | return sprintf( ' |
||
175 | <div class="%1$s"> |
||
176 | <div class="%2$s"> |
||
177 | <div class="%3$s"> |
||
178 | <div class="%4$s" id="%5$s"> |
||
179 | <p>%6$s</p> |
||
180 | <p>%7$s</p> |
||
181 | </div> |
||
182 | </div> |
||
183 | </div> |
||
184 | </div> |
||
185 | ', |
||
186 | esc_attr( "{$data['class']} ${css_prefix}-wrapper" ), |
||
187 | esc_attr( "${css_prefix}-product" ), |
||
188 | esc_attr( "${css_prefix}-details" ), |
||
189 | esc_attr( "${css_prefix}-purchase-message show error" ), |
||
190 | esc_attr( "{$data['dom_id']}-message-container" ), |
||
191 | sprintf( |
||
192 | wp_kses( |
||
193 | __( 'Your plan doesn\'t include Simple Payments. <a href="%s" rel="noopener noreferrer" target="_blank">Learn more and upgrade</a>.', 'jetpack' ), |
||
194 | array( 'a' => array( 'href' => array(), 'rel' => array(), 'target' => array() ) ) |
||
195 | ), |
||
196 | esc_url( $support_url ) |
||
197 | ), |
||
198 | esc_html__( '(Only administrators will see this message.)', 'jetpack' ) |
||
199 | ); |
||
200 | } |
||
201 | |||
202 | function output_shortcode( $data ) { |
||
203 | $items = ''; |
||
204 | $css_prefix = self::$css_classname_prefix; |
||
205 | |||
206 | if ( $data['multiple'] ) { |
||
207 | $items = sprintf( ' |
||
208 | <div class="%1$s"> |
||
209 | <input class="%2$s" type="number" value="1" min="1" id="%3$s" /> |
||
210 | </div> |
||
211 | ', |
||
212 | esc_attr( "${css_prefix}-items" ), |
||
213 | esc_attr( "${css_prefix}-items-number" ), |
||
214 | esc_attr( "{$data['dom_id']}_number" ) |
||
215 | ); |
||
216 | } |
||
217 | $image = ""; |
||
218 | if( has_post_thumbnail( $data['id'] ) ) { |
||
219 | $image = sprintf( '<div class="%1$s"><div class="%2$s">%3$s</div></div>', |
||
220 | esc_attr( "${css_prefix}-product-image" ), |
||
221 | esc_attr( "${css_prefix}-image" ), |
||
222 | get_the_post_thumbnail( $data['id'], 'full' ) |
||
223 | ); |
||
224 | } |
||
225 | return sprintf( ' |
||
226 | <div class="%1$s"> |
||
227 | <div class="%2$s"> |
||
228 | %3$s |
||
229 | <div class="%4$s"> |
||
230 | <div class="%5$s"><p>%6$s</p></div> |
||
231 | <div class="%7$s"><p>%8$s</p></div> |
||
232 | <div class="%9$s"><p>%10$s</p></div> |
||
233 | <div class="%11$s" id="%12$s"></div> |
||
234 | <div class="%13$s"> |
||
235 | %14$s |
||
236 | <div class="%15$s" id="%16$s"></div> |
||
237 | </div> |
||
238 | </div> |
||
239 | </div> |
||
240 | </div> |
||
241 | ', |
||
242 | esc_attr( "{$data['class']} ${css_prefix}-wrapper" ), |
||
243 | esc_attr( "${css_prefix}-product" ), |
||
244 | $image, |
||
245 | esc_attr( "${css_prefix}-details" ), |
||
246 | esc_attr( "${css_prefix}-title" ), |
||
247 | $data['title'], |
||
248 | esc_attr( "${css_prefix}-description" ), |
||
249 | $data['description'], |
||
250 | esc_attr( "${css_prefix}-price" ), |
||
251 | esc_html( $data['price'] ), |
||
252 | esc_attr( "${css_prefix}-purchase-message" ), |
||
253 | esc_attr( "{$data['dom_id']}-message-container" ), |
||
254 | esc_attr( "${css_prefix}-purchase-box" ), |
||
255 | $items, |
||
256 | esc_attr( "${css_prefix}-button" ), |
||
257 | esc_attr( "{$data['dom_id']}_button" ) |
||
258 | ); |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Format a price with currency |
||
263 | * |
||
264 | * Uses currency-aware formatting to output a formatted price with a simple fallback. |
||
265 | * |
||
266 | * Largely inspired by WordPress.com's Store_Price::display_currency |
||
267 | * |
||
268 | * @param string $price Price. |
||
269 | * @param string $currency Currency. |
||
270 | * @return string Formatted price. |
||
271 | */ |
||
272 | private function format_price( $price, $currency ) { |
||
273 | $currency_details = self::get_currency( $currency ); |
||
274 | |||
275 | if ( $currency_details ) { |
||
276 | // Ensure USD displays as 1234.56 even in non-US locales. |
||
277 | $amount = 'USD' === $currency |
||
278 | ? number_format( $price, $currency_details['decimal'], '.', ',' ) |
||
279 | : number_format_i18n( $price, $currency_details['decimal'] ); |
||
280 | |||
281 | return sprintf( |
||
282 | $currency_details['format'], |
||
283 | $currency_details['symbol'], |
||
284 | $amount |
||
285 | ); |
||
286 | } |
||
287 | |||
288 | return "$price $currency"; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Allows custom post types to be used by REST API. |
||
293 | * @param $post_types |
||
294 | * @see hook 'rest_api_allowed_post_types' |
||
295 | * @return array |
||
296 | */ |
||
297 | function allow_rest_api_types( $post_types ) { |
||
302 | |||
303 | function allow_sync_post_meta( $post_meta ) { |
||
318 | |||
319 | /** |
||
320 | * Enable Simple payments custom meta values for access through the REST API. |
||
321 | * Field’s value will be exposed on a .meta key in the endpoint response, |
||
322 | * and WordPress will handle setting up the callbacks for reading and writing |
||
323 | * to that meta key. |
||
324 | * |
||
325 | * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/ |
||
326 | */ |
||
327 | public function register_meta_fields_in_rest_api() { |
||
381 | |||
382 | /** |
||
383 | * Sanitize three-character ISO-4217 Simple payments currency |
||
384 | * |
||
385 | * List has to be in sync with list at the client side: |
||
386 | * @link https://github.com/Automattic/wp-calypso/blob/6d02ffe73cc073dea7270a22dc30881bff17d8fb/client/lib/simple-payments/constants.js |
||
387 | * |
||
388 | * Currencies should be supported by PayPal: |
||
389 | * @link https://developer.paypal.com/docs/integration/direct/rest/currency-codes/ |
||
390 | */ |
||
391 | public static function sanitize_currency( $currency ) { |
||
421 | |||
422 | /** |
||
423 | * Sanitize price: |
||
424 | * |
||
425 | * Positive integers and floats |
||
426 | * Supports two decimal places. |
||
427 | * Maximum length: 10. |
||
428 | * |
||
429 | * See `price` from PayPal docs: |
||
430 | * @link https://developer.paypal.com/docs/api/orders/v1/#definition-item |
||
431 | * |
||
432 | * @param $value |
||
433 | * @return null|string |
||
434 | */ |
||
435 | public static function sanitize_price( $price ) { |
||
438 | |||
439 | /** |
||
440 | * Sets up the custom post types for the module. |
||
441 | */ |
||
442 | function setup_cpts() { |
||
529 | |||
530 | /** |
||
531 | * Format a price for display |
||
532 | * |
||
533 | * Largely taken from WordPress.com Store_Price class |
||
534 | * |
||
535 | * The currency array will have the shape: |
||
536 | * format => string sprintf format with placeholders `%1$s`: Symbol `%2$s`: Price. |
||
537 | * symbol => string Symbol string |
||
538 | * desc => string Text description of currency |
||
539 | * decimal => int Number of decimal places |
||
540 | * |
||
541 | * @param string $the_currency The desired currency, e.g. 'USD'. |
||
542 | * @return ?array Currency object or null if not found. |
||
|
|||
543 | */ |
||
544 | private static function get_currency( $the_currency ) { |
||
673 | } |
||
674 | Jetpack_Simple_Payments::getInstance(); |
||
675 |
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.