Completed
Push — simple-payments/improve-gettin... ( 511640 )
by Rastislav
28:43 queued 16:50
created

Jetpack_Simple_Payments   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 267
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 267
rs 10
c 0
b 0
f 0
wmc 27
lcom 2
cbo 1

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A getInstance() 0 7 2
A register_scripts() 0 9 1
A register_init_hook() 0 3 1
A register_shortcode() 0 3 1
A init_hook_action() 0 9 1
A remove_auto_paragraph_from_product_description() 0 7 2
A get_blog_id() 0 7 3
C parse_shortcode() 0 45 7
B output_shortcode() 0 31 3
A format_price() 0 6 2
A allow_rest_api_types() 0 5 1
A allow_sync_post_meta() 0 15 1
B setup_cpts() 0 87 1
1
<?php
2
/*
3
 * Simple Payments lets users embed a PayPal button fully integrated with wpcom to sell products on the site.
4
 * This is not a proper module yet, because not all the pieces are in place. Until everything is shipped, it can be turned
5
 * into module that can be enabled/disabled.
6
*/
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';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $post_type_order.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
10
	static $post_type_product = 'jp_pay_product';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $post_type_product.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
11
12
	static $shortcode = 'simple-payment';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $shortcode.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
13
14
	static $css_classname_prefix = 'jetpack-simple-payments';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $css_classname_prefix.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
15
16
	// Classic singleton pattern:
17
	private static $instance;
18
	private function __construct() {}
19
	static function getInstance() {
20
		if ( ! self::$instance ) {
21
			self::$instance = new self();
22
			self::$instance->register_init_hook();
23
		}
24
		return self::$instance;
25
	}
26
27
	private function register_scripts() {
28
		/**
29
		 * Paypal heavily discourages putting that script in your own server:
30
		 * @see https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/add-paypal-button/
31
		 */
32
		wp_register_script( 'paypal-checkout-js', 'https://www.paypalobjects.com/api/checkout.js', array(), null, true );
33
		wp_register_script( 'paypal-express-checkout', plugins_url( '/paypal-express-checkout.js', __FILE__ ) , array( 'jquery', 'paypal-checkout-js' ), '0.21' );
34
		wp_enqueue_style( 'simple-payments', plugins_url( '/simple-payments.css', __FILE__ ) );
35
	}
36
	private function register_init_hook() {
37
		add_action( 'init', array( $this, 'init_hook_action' ) );
38
	}
39
	private function register_shortcode() {
40
		add_shortcode( self::$shortcode, array( $this, 'parse_shortcode' ) );
41
	}
42
43
	public function init_hook_action() {
44
		add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) );
45
		add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'allow_sync_post_meta' ) );
46
		$this->register_scripts();
47
		$this->register_shortcode();
48
		$this->setup_cpts();
49
50
		add_filter( 'the_content', array( $this, 'remove_auto_paragraph_from_product_description' ), 0 );
51
	}
52
53
	function remove_auto_paragraph_from_product_description( $content ) {
54
		if ( get_post_type() === self::$post_type_product ) {
55
			remove_filter( 'the_content', 'wpautop' );
56
		}
57
58
		return $content;
59
	}
60
61
	function get_blog_id() {
62
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
63
			return get_current_blog_id();
64
		}
65
66
		return Jetpack_Options::get_option( 'id' );
67
	}
68
69
	function parse_shortcode( $attrs, $content = false ) {
70
		if ( empty( $attrs['id'] ) ) {
71
			return;
72
		}
73
		$product = get_post( $attrs['id'] );
74
		if ( ! $product || is_wp_error( $product ) ) {
75
			return;
76
		}
77
		if ( $product->post_type !== self::$post_type_product ) {
78
			return;
79
		}
80
81
		// We allow for overriding the presentation labels
82
		$data = shortcode_atts( array(
83
			'blog_id'     => $this->get_blog_id(),
84
			'dom_id'      => uniqid( self::$css_classname_prefix . '-' . $product->ID . '_', true ),
85
			'class'       => self::$css_classname_prefix . '-' . $product->ID,
86
			'title'       => get_the_title( $product ),
87
			'description' => $product->post_content,
88
			'cta'         => get_post_meta( $product->ID, 'spay_cta', true ),
89
			'multiple'    => get_post_meta( $product->ID, 'spay_multiple', true ) || '0'
90
		), $attrs );
91
92
		$data['price'] = $this->format_price(
93
			get_post_meta( $product->ID, 'spay_formatted_price', true ),
94
			get_post_meta( $product->ID, 'spay_price', true ),
95
			get_post_meta( $product->ID, 'spay_currency', true ),
96
			$data
97
		);
98
99
		$data['id'] = $attrs['id'];
100
		if ( ! wp_script_is( 'paypal-express-checkout', 'enqueued' ) ) {
101
			wp_enqueue_script( 'paypal-express-checkout' );
102
		}
103
104
		wp_add_inline_script( 'paypal-express-checkout', sprintf(
105
			"try{PaypalExpressCheckout.renderButton( '%d', '%d', '%s', '%d' );}catch(e){}",
106
			esc_js( $data['blog_id'] ),
107
			esc_js( $attrs['id'] ),
108
			esc_js( $data['dom_id'] ),
109
			esc_js( $data['multiple'] )
110
		) );
111
112
		return $this->output_shortcode( $data );
113
	}
114
115
	function output_shortcode( $data ) {
116
		$items = '';
117
		$css_prefix = self::$css_classname_prefix;
118
119
		if ( $data['multiple'] ) {
120
			$items="<div class='${css_prefix}-items'>
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
121
				<input class='${css_prefix}-items-number' type='number' value='1' min='1' id='{$data['dom_id']}_number' />
122
			</div>";
123
		}
124
		$image = "";
125
		if( has_post_thumbnail( $data['id'] ) ) {
126
			$image = "<div class='${css_prefix}-product-image'><div class='${css_prefix}-image'>" . get_the_post_thumbnail( $data['id'], 'full' ) . "</div></div>";
127
		}
128
		return "
129
<div class='{$data['class']} ${css_prefix}-wrapper'>
130
	<div class='${css_prefix}-purchase-message' id='{$data['dom_id']}-message-container'></div>
131
	<div class='${css_prefix}-product'>
132
		{$image}
133
		<div class='${css_prefix}-details'>
134
			<div class='${css_prefix}-title'><p>{$data['title']}</p></div>
135
			<div class='${css_prefix}-description'><p>{$data['description']}</p></div>
136
			<div class='${css_prefix}-price'><p>{$data['price']}</p></div>
137
			<div class='${css_prefix}-purchase-box'>
138
				{$items}
139
				<div class='${css_prefix}-button' id='{$data['dom_id']}_button'></div>
140
			</div>
141
		</div>
142
	</div>
143
</div>
144
		";
145
	}
146
147
	function format_price( $formatted_price, $price, $currency, $all_data ) {
148
		if ( $formatted_price ) {
149
			return $formatted_price;
150
		}
151
		return "$price $currency";
152
	}
153
154
	/**
155
	 * Allows custom post types to be used by REST API.
156
	 * @param $post_types
157
	 * @see hook 'rest_api_allowed_post_types'
158
	 * @return array
159
	 */
160
	function allow_rest_api_types( $post_types ) {
161
		$post_types[] = self::$post_type_order;
162
		$post_types[] = self::$post_type_product;
163
		return $post_types;
164
	}
165
166
	function allow_sync_post_meta( $post_meta ) {
167
		return array_merge( $post_meta, array(
168
			'spay_paypal_id',
169
			'spay_status',
170
			'spay_product_id',
171
			'spay_quantity',
172
			'spay_price',
173
			'spay_customer_email',
174
			'spay_currency',
175
			'spay_cta',
176
			'spay_email',
177
			'spay_multiple',
178
			'spay_formatted_price',
179
		) );
180
	}
181
182
	/**
183
	 * Sets up the custom post types for the module.
184
	 */
185
	function setup_cpts() {
186
187
		/*
188
		 * ORDER data structure. holds:
189
		 * title = customer_name | 4xproduct_name
190
		 * excerpt = customer_name + customer contact info + customer notes from paypal form
191
		 * metadata:
192
		 * spay_paypal_id - paypal id of transaction
193
		 * spay_status
194
		 * spay_product_id - post_id of bought product
195
		 * spay_quantity - quantity of product
196
		 * spay_price - item price at the time of purchase
197
		 * spay_customer_email - customer email
198
		 * ... (WIP)
199
		 */
200
		$order_capabilities = array(
201
			'edit_post'             => 'edit_posts',
202
			'read_post'             => 'read_private_posts',
203
			'delete_post'           => 'delete_posts',
204
			'edit_posts'            => 'edit_posts',
205
			'edit_others_posts'     => 'edit_others_posts',
206
			'publish_posts'         => 'publish_posts',
207
			'read_private_posts'    => 'read_private_posts',
208
		);
209
		$order_args = array(
210
			'label'                 => esc_html__( 'Order', 'jetpack' ),
211
			'description'           => esc_html__( 'Simple Payments orders', 'jetpack' ),
212
			'supports'              => array( 'custom-fields', 'excerpt' ),
213
			'hierarchical'          => false,
214
			'public'                => false,
215
			'show_ui'               => false,
216
			'show_in_menu'          => false,
217
			'show_in_admin_bar'     => false,
218
			'show_in_nav_menus'     => false,
219
			'can_export'            => true,
220
			'has_archive'           => false,
221
			'exclude_from_search'   => true,
222
			'publicly_queryable'    => false,
223
			'rewrite'               => false,
224
			'capabilities'          => $order_capabilities,
225
			'show_in_rest'          => true,
226
		);
227
		register_post_type( self::$post_type_order, $order_args );
228
229
		/*
230
		 * PRODUCT data structure. Holds:
231
		 * title - title
232
		 * content - description
233
		 * thumbnail - image
234
		 * metadata:
235
		 * spay_price - price
236
		 * spay_formatted_price
237
		 * spay_currency - currency code
238
		 * spay_cta - text with "Buy" or other CTA
239
		 * spay_email - paypal email
240
		 * spay_multiple - allow for multiple items
241
		 * spay_status - status. { enabled | disabled }
242
		 */
243
		$product_capabilities = array(
244
			'edit_post'             => 'edit_posts',
245
			'read_post'             => 'read_private_posts',
246
			'delete_post'           => 'delete_posts',
247
			'edit_posts'            => 'edit_posts',
248
			'edit_others_posts'     => 'edit_others_posts',
249
			'publish_posts'         => 'publish_posts',
250
			'read_private_posts'    => 'read_private_posts',
251
		);
252
		$product_args = array(
253
			'label'                 => esc_html__( 'Product', 'jetpack' ),
254
			'description'           => esc_html__( 'Simple Payments products', 'jetpack' ),
255
			'supports'              => array( 'title', 'editor','thumbnail', 'custom-fields' ),
256
			'hierarchical'          => false,
257
			'public'                => false,
258
			'show_ui'               => false,
259
			'show_in_menu'          => false,
260
			'show_in_admin_bar'     => false,
261
			'show_in_nav_menus'     => false,
262
			'can_export'            => true,
263
			'has_archive'           => false,
264
			'exclude_from_search'   => true,
265
			'publicly_queryable'    => false,
266
			'rewrite'               => false,
267
			'capabilities'          => $product_capabilities,
268
			'show_in_rest'          => true,
269
		);
270
		register_post_type( self::$post_type_product, $product_args );
271
	}
272
273
}
274
Jetpack_Simple_Payments::getInstance();
275