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:
| 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 | |||
| 22 | private function __construct() {} |
||
| 23 | |||
| 24 | static function getInstance() { |
||
| 25 | if ( ! self::$instance ) { |
||
| 26 | self::$instance = new self(); |
||
| 27 | self::$instance->register_init_hook(); |
||
| 28 | self::$instance->register_gutenberg_block(); |
||
| 29 | } |
||
| 30 | return self::$instance; |
||
| 31 | } |
||
| 32 | |||
| 33 | private function register_scripts() { |
||
| 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 | } |
||
| 42 | |||
| 43 | private function register_init_hook() { |
||
| 46 | |||
| 47 | private function register_shortcode() { |
||
| 48 | add_shortcode( self::$shortcode, array( $this, 'parse_shortcode' ) ); |
||
| 49 | } |
||
| 50 | |||
| 51 | public function init_hook_action() { |
||
| 52 | add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) ); |
||
| 53 | add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'allow_sync_post_meta' ) ); |
||
| 54 | $this->register_scripts(); |
||
| 55 | $this->register_shortcode(); |
||
| 56 | $this->setup_cpts(); |
||
| 57 | $this->setup_meta_fields_for_rest(); |
||
| 58 | |||
| 59 | add_filter( 'the_content', array( $this, 'remove_auto_paragraph_from_product_description' ), 0 ); |
||
| 60 | } |
||
| 61 | |||
| 62 | private function register_gutenberg_block() { |
||
| 63 | add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); |
||
| 64 | add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_assets' ) ); |
||
| 65 | |||
| 66 | register_block_type( 'jetpack/simple-payments-button', array( |
||
| 67 | 'render_callback' => array( $this, 'render_gutenberg_block' ), |
||
| 68 | ) ); |
||
| 69 | } |
||
| 70 | |||
| 71 | public function enqueue_block_editor_assets() { |
||
| 72 | wp_enqueue_script( |
||
| 73 | 'gutenberg-simple-payments-button', |
||
| 74 | plugins_url( 'simple-payments-block.js', __FILE__ ), |
||
| 75 | array( 'wp-blocks', 'wp-element' ) |
||
| 76 | ); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function enqueue_block_assets() { |
||
| 80 | if ( is_admin() ) { |
||
| 81 | // Load the styles just in admin area and rely on custom PayPal |
||
| 82 | // styles on front end |
||
| 83 | wp_enqueue_style( |
||
| 84 | 'gutenberg-simple-payments-button-styles', |
||
| 85 | plugins_url( 'simple-payments-block.css', __FILE__ ), |
||
| 86 | array(), |
||
| 87 | JETPACK__VERSION |
||
| 88 | ); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | public function render_gutenberg_block( $attributes, $content ) { |
||
| 93 | $blog_id = $this->get_blog_id(); |
||
| 94 | $dom_id = 'paypal-express-checkout'; |
||
| 95 | $multiple = $attributes[ 'multipleItems' ]; |
||
| 96 | // mock id for now |
||
| 97 | // $id = $attributes['id']; |
||
| 98 | $id = 357; |
||
| 99 | |||
| 100 | if ( ! wp_script_is( 'paypal-express-checkout', 'enqueued' ) ) { |
||
| 101 | wp_enqueue_script( 'paypal-express-checkout' ); |
||
| 102 | } |
||
| 103 | View Code Duplication | if ( ! wp_style_is( 'simple-payments', 'enqueued' ) ) { |
|
| 104 | wp_enqueue_style( 'simple-payments', plugins_url( 'simple-payments.css', __FILE__ ), array( 'dashicons' ) ); |
||
| 105 | } |
||
| 106 | |||
| 107 | wp_add_inline_script( 'paypal-express-checkout', sprintf( |
||
| 108 | "try{PaypalExpressCheckout.renderButton( '%d', '%d', '%s', '%d' );}catch(e){}", |
||
| 109 | esc_js( $blog_id ), |
||
| 110 | esc_js( $id ), |
||
| 111 | esc_js( $dom_id ), |
||
| 112 | esc_js( $multiple ) |
||
| 113 | ) ); |
||
| 114 | |||
| 115 | return $content; |
||
| 116 | } |
||
| 117 | |||
| 118 | function remove_auto_paragraph_from_product_description( $content ) { |
||
| 125 | |||
| 126 | function get_blog_id() { |
||
| 133 | |||
| 134 | function parse_shortcode( $attrs, $content = false ) { |
||
| 182 | |||
| 183 | function output_shortcode( $data ) { |
||
| 214 | |||
| 215 | function format_price( $formatted_price, $price, $currency, $all_data ) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Allows custom post types to be used by REST API. |
||
| 224 | * @param $post_types |
||
| 225 | * @see hook 'rest_api_allowed_post_types' |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | function allow_rest_api_types( $post_types ) { |
||
| 233 | |||
| 234 | function allow_sync_post_meta( $post_meta ) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Sets up the custom post types for the module. |
||
| 252 | */ |
||
| 253 | function setup_cpts() { |
||
| 340 | |||
| 341 | public function update_post_meta_for_api( $meta_arr, $object ) { |
||
| 342 | //get the id of the post object array |
||
| 343 | $post_id = $object->ID; |
||
| 344 | |||
| 345 | foreach( $meta_arr as $meta_key => $meta_val ) { |
||
| 351 | |||
| 352 | function setup_meta_fields_for_rest() { |
||
| 359 | |||
| 360 | } |
||
| 361 | Jetpack_Simple_Payments::getInstance(); |
||
| 362 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.