Completed
Push — update/changelog-5.2 ( 09baa5...b8cf0c )
by
unknown
182:03 queued 172:51
created

Jetpack_Simple_Payments::parse_shortcode()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 45
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 32
nc 5
nop 2
dl 0
loc 45
rs 6.7272
c 0
b 0
f 0
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
	// Increase this number each time there's a change in CSS or JS to bust cache.
17
	static $version = '0.23';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $version.

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...
18
19
	// Classic singleton pattern:
20
	private static $instance;
21
	private function __construct() {}
22
	static function getInstance() {
23
		if ( ! self::$instance ) {
24
			self::$instance = new self();
25
			self::$instance->register_init_hook();
26
		}
27
		return self::$instance;
28
	}
29
30
	private function register_scripts() {
31
		/**
32
		 * Paypal heavily discourages putting that script in your own server:
33
		 * @see https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/add-paypal-button/
34
		 */
35
		wp_register_script( 'paypal-checkout-js', 'https://www.paypalobjects.com/api/checkout.js', array(), null, true );
36
		wp_register_script( 'paypal-express-checkout', plugins_url( '/paypal-express-checkout.js', __FILE__ ),
37
			array( 'jquery', 'paypal-checkout-js' ), self::$version );
38
		wp_enqueue_style( 'simple-payments', plugins_url( '/simple-payments.css', __FILE__ ) );
39
	}
40
	private function register_init_hook() {
41
		add_action( 'init', array( $this, 'init_hook_action' ) );
42
	}
43
	private function register_shortcode() {
44
		add_shortcode( self::$shortcode, array( $this, 'parse_shortcode' ) );
45
	}
46
47
	public function init_hook_action() {
48
		add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) );
49
		add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'allow_sync_post_meta' ) );
50
		$this->register_scripts();
51
		$this->register_shortcode();
52
		$this->setup_cpts();
53
54
		add_filter( 'the_content', array( $this, 'remove_auto_paragraph_from_product_description' ), 0 );
55
	}
56
57
	function remove_auto_paragraph_from_product_description( $content ) {
58
		if ( get_post_type() === self::$post_type_product ) {
59
			remove_filter( 'the_content', 'wpautop' );
60
		}
61
62
		return $content;
63
	}
64
65
	function get_blog_id() {
66
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
67
			return get_current_blog_id();
68
		}
69
70
		return Jetpack_Options::get_option( 'id' );
71
	}
72
73
	function parse_shortcode( $attrs, $content = false ) {
74
		if ( empty( $attrs['id'] ) ) {
75
			return;
76
		}
77
		$product = get_post( $attrs['id'] );
78
		if ( ! $product || is_wp_error( $product ) ) {
79
			return;
80
		}
81
		if ( $product->post_type !== self::$post_type_product ) {
82
			return;
83
		}
84
85
		// We allow for overriding the presentation labels
86
		$data = shortcode_atts( array(
87
			'blog_id'     => $this->get_blog_id(),
88
			'dom_id'      => uniqid( self::$css_classname_prefix . '-' . $product->ID . '_', true ),
89
			'class'       => self::$css_classname_prefix . '-' . $product->ID,
90
			'title'       => get_the_title( $product ),
91
			'description' => $product->post_content,
92
			'cta'         => get_post_meta( $product->ID, 'spay_cta', true ),
93
			'multiple'    => get_post_meta( $product->ID, 'spay_multiple', true ) || '0'
94
		), $attrs );
95
96
		$data['price'] = $this->format_price(
97
			get_post_meta( $product->ID, 'spay_formatted_price', true ),
98
			get_post_meta( $product->ID, 'spay_price', true ),
99
			get_post_meta( $product->ID, 'spay_currency', true ),
100
			$data
101
		);
102
103
		$data['id'] = $attrs['id'];
104
		if ( ! wp_script_is( 'paypal-express-checkout', 'enqueued' ) ) {
105
			wp_enqueue_script( 'paypal-express-checkout' );
106
		}
107
108
		wp_add_inline_script( 'paypal-express-checkout', sprintf(
109
			"try{PaypalExpressCheckout.renderButton( '%d', '%d', '%s', '%d' );}catch(e){}",
110
			esc_js( $data['blog_id'] ),
111
			esc_js( $attrs['id'] ),
112
			esc_js( $data['dom_id'] ),
113
			esc_js( $data['multiple'] )
114
		) );
115
116
		return $this->output_shortcode( $data );
117
	}
118
119
	function output_shortcode( $data ) {
120
		$items = '';
121
		$css_prefix = self::$css_classname_prefix;
122
123
		if ( $data['multiple'] ) {
124
			$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...
125
				<input class='${css_prefix}-items-number' type='number' value='1' min='1' id='{$data['dom_id']}_number' />
126
			</div>";
127
		}
128
		$image = "";
129
		if( has_post_thumbnail( $data['id'] ) ) {
130
			$image = "<div class='${css_prefix}-product-image'><div class='${css_prefix}-image'>" . get_the_post_thumbnail( $data['id'], 'full' ) . "</div></div>";
131
		}
132
		return "
133
<div class='{$data['class']} ${css_prefix}-wrapper'>
134
	<div class='${css_prefix}-purchase-message' id='{$data['dom_id']}-message-container'></div>
135
	<div class='${css_prefix}-product'>
136
		{$image}
137
		<div class='${css_prefix}-details'>
138
			<div class='${css_prefix}-title'><p>{$data['title']}</p></div>
139
			<div class='${css_prefix}-description'><p>{$data['description']}</p></div>
140
			<div class='${css_prefix}-price'><p>{$data['price']}</p></div>
141
			<div class='${css_prefix}-purchase-box'>
142
				{$items}
143
				<div class='${css_prefix}-button' id='{$data['dom_id']}_button'></div>
144
			</div>
145
		</div>
146
	</div>
147
</div>
148
		";
149
	}
150
151
	function format_price( $formatted_price, $price, $currency, $all_data ) {
152
		if ( $formatted_price ) {
153
			return $formatted_price;
154
		}
155
		return "$price $currency";
156
	}
157
158
	/**
159
	 * Allows custom post types to be used by REST API.
160
	 * @param $post_types
161
	 * @see hook 'rest_api_allowed_post_types'
162
	 * @return array
163
	 */
164
	function allow_rest_api_types( $post_types ) {
165
		$post_types[] = self::$post_type_order;
166
		$post_types[] = self::$post_type_product;
167
		return $post_types;
168
	}
169
170
	function allow_sync_post_meta( $post_meta ) {
171
		return array_merge( $post_meta, array(
172
			'spay_paypal_id',
173
			'spay_status',
174
			'spay_product_id',
175
			'spay_quantity',
176
			'spay_price',
177
			'spay_customer_email',
178
			'spay_currency',
179
			'spay_cta',
180
			'spay_email',
181
			'spay_multiple',
182
			'spay_formatted_price',
183
		) );
184
	}
185
186
	/**
187
	 * Sets up the custom post types for the module.
188
	 */
189
	function setup_cpts() {
190
191
		/*
192
		 * ORDER data structure. holds:
193
		 * title = customer_name | 4xproduct_name
194
		 * excerpt = customer_name + customer contact info + customer notes from paypal form
195
		 * metadata:
196
		 * spay_paypal_id - paypal id of transaction
197
		 * spay_status
198
		 * spay_product_id - post_id of bought product
199
		 * spay_quantity - quantity of product
200
		 * spay_price - item price at the time of purchase
201
		 * spay_customer_email - customer email
202
		 * ... (WIP)
203
		 */
204
		$order_capabilities = array(
205
			'edit_post'             => 'edit_posts',
206
			'read_post'             => 'read_private_posts',
207
			'delete_post'           => 'delete_posts',
208
			'edit_posts'            => 'edit_posts',
209
			'edit_others_posts'     => 'edit_others_posts',
210
			'publish_posts'         => 'publish_posts',
211
			'read_private_posts'    => 'read_private_posts',
212
		);
213
		$order_args = array(
214
			'label'                 => esc_html__( 'Order', 'jetpack' ),
215
			'description'           => esc_html__( 'Simple Payments orders', 'jetpack' ),
216
			'supports'              => array( 'custom-fields', 'excerpt' ),
217
			'hierarchical'          => false,
218
			'public'                => false,
219
			'show_ui'               => false,
220
			'show_in_menu'          => false,
221
			'show_in_admin_bar'     => false,
222
			'show_in_nav_menus'     => false,
223
			'can_export'            => true,
224
			'has_archive'           => false,
225
			'exclude_from_search'   => true,
226
			'publicly_queryable'    => false,
227
			'rewrite'               => false,
228
			'capabilities'          => $order_capabilities,
229
			'show_in_rest'          => true,
230
		);
231
		register_post_type( self::$post_type_order, $order_args );
232
233
		/*
234
		 * PRODUCT data structure. Holds:
235
		 * title - title
236
		 * content - description
237
		 * thumbnail - image
238
		 * metadata:
239
		 * spay_price - price
240
		 * spay_formatted_price
241
		 * spay_currency - currency code
242
		 * spay_cta - text with "Buy" or other CTA
243
		 * spay_email - paypal email
244
		 * spay_multiple - allow for multiple items
245
		 * spay_status - status. { enabled | disabled }
246
		 */
247
		$product_capabilities = array(
248
			'edit_post'             => 'edit_posts',
249
			'read_post'             => 'read_private_posts',
250
			'delete_post'           => 'delete_posts',
251
			'edit_posts'            => 'edit_posts',
252
			'edit_others_posts'     => 'edit_others_posts',
253
			'publish_posts'         => 'publish_posts',
254
			'read_private_posts'    => 'read_private_posts',
255
		);
256
		$product_args = array(
257
			'label'                 => esc_html__( 'Product', 'jetpack' ),
258
			'description'           => esc_html__( 'Simple Payments products', 'jetpack' ),
259
			'supports'              => array( 'title', 'editor','thumbnail', 'custom-fields' ),
260
			'hierarchical'          => false,
261
			'public'                => false,
262
			'show_ui'               => false,
263
			'show_in_menu'          => false,
264
			'show_in_admin_bar'     => false,
265
			'show_in_nav_menus'     => false,
266
			'can_export'            => true,
267
			'has_archive'           => false,
268
			'exclude_from_search'   => true,
269
			'publicly_queryable'    => false,
270
			'rewrite'               => false,
271
			'capabilities'          => $product_capabilities,
272
			'show_in_rest'          => true,
273
		);
274
		register_post_type( self::$post_type_product, $product_args );
275
	}
276
277
}
278
Jetpack_Simple_Payments::getInstance();
279