|
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'; |
|
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() {} |
|
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_and_styles() { |
|
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_register_style( 'jetpack-simple-payments', plugins_url( '/simple-payments.css', __FILE__ ), array( 'dashicons' ) ); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
private function register_init_hook() { |
|
42
|
|
|
add_action( 'init', array( $this, 'init_hook_action' ) ); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function register_shortcode() { |
|
46
|
|
|
add_shortcode( self::$shortcode, array( $this, 'parse_shortcode' ) ); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function init_hook_action() { |
|
50
|
|
|
add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) ); |
|
51
|
|
|
add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'allow_sync_post_meta' ) ); |
|
52
|
|
|
$this->register_scripts_and_styles(); |
|
53
|
|
|
$this->register_shortcode(); |
|
54
|
|
|
$this->setup_cpts(); |
|
55
|
|
|
|
|
56
|
|
|
add_filter( 'the_content', array( $this, 'remove_auto_paragraph_from_product_description' ), 0 ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
function remove_auto_paragraph_from_product_description( $content ) { |
|
60
|
|
|
if ( get_post_type() === self::$post_type_product ) { |
|
61
|
|
|
remove_filter( 'the_content', 'wpautop' ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $content; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
function get_blog_id() { |
|
68
|
|
|
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
69
|
|
|
return get_current_blog_id(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return Jetpack_Options::get_option( 'id' ); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Used to check whether Simple Payments are enabled for given site. |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool True if Simple Payments are enabled, false otherwise. |
|
79
|
|
|
*/ |
|
80
|
|
|
function is_enabled_jetpack_simple_payments() { |
|
81
|
|
|
/** |
|
82
|
|
|
* Can be used by plugin authors to disable the conflicting output of Simple Payments. |
|
83
|
|
|
* |
|
84
|
|
|
* @since 6.3.0 |
|
85
|
|
|
* |
|
86
|
|
|
* @param bool True if Simple Payments should be disabled, false otherwise. |
|
87
|
|
|
*/ |
|
88
|
|
|
if ( apply_filters( 'jetpack_disable_simple_payments', false ) ) { |
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// For WPCOM sites |
|
93
|
|
|
if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'has_blog_sticker' ) ) { |
|
94
|
|
|
$site_id = $this->get_blog_id(); |
|
95
|
|
|
return has_blog_sticker( 'premium-plan', $site_id ) || has_blog_sticker( 'business-plan', $site_id ); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// For all Jetpack sites |
|
99
|
|
|
return Jetpack::is_active() && Jetpack::active_plan_supports( 'simple-payments'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
function parse_shortcode( $attrs, $content = false ) { |
|
103
|
|
|
if ( empty( $attrs['id'] ) ) { |
|
104
|
|
|
return; |
|
105
|
|
|
} |
|
106
|
|
|
$product = get_post( $attrs['id'] ); |
|
107
|
|
|
if ( ! $product || is_wp_error( $product ) ) { |
|
108
|
|
|
return; |
|
109
|
|
|
} |
|
110
|
|
|
if ( $product->post_type !== self::$post_type_product || 'trash' === $product->post_status ) { |
|
111
|
|
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// We allow for overriding the presentation labels |
|
115
|
|
|
$data = shortcode_atts( array( |
|
116
|
|
|
'blog_id' => $this->get_blog_id(), |
|
117
|
|
|
'dom_id' => uniqid( self::$css_classname_prefix . '-' . $product->ID . '_', true ), |
|
118
|
|
|
'class' => self::$css_classname_prefix . '-' . $product->ID, |
|
119
|
|
|
'title' => get_the_title( $product ), |
|
120
|
|
|
'description' => $product->post_content, |
|
121
|
|
|
'cta' => get_post_meta( $product->ID, 'spay_cta', true ), |
|
122
|
|
|
'multiple' => get_post_meta( $product->ID, 'spay_multiple', true ) || '0' |
|
123
|
|
|
), $attrs ); |
|
124
|
|
|
|
|
125
|
|
|
$data['price'] = $this->format_price( |
|
126
|
|
|
get_post_meta( $product->ID, 'spay_formatted_price', true ), |
|
127
|
|
|
get_post_meta( $product->ID, 'spay_price', true ), |
|
128
|
|
|
get_post_meta( $product->ID, 'spay_currency', true ), |
|
129
|
|
|
$data |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
|
|
$data['id'] = $attrs['id']; |
|
133
|
|
|
|
|
134
|
|
|
if( ! wp_style_is( 'jetpack-simple-payments', 'enqueue' ) ) { |
|
135
|
|
|
wp_enqueue_style( 'jetpack-simple-payments' ); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
if ( ! $this->is_enabled_jetpack_simple_payments() ) { |
|
139
|
|
|
return $this->output_admin_warning( $data ); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
if ( ! wp_script_is( 'paypal-express-checkout', 'enqueued' ) ) { |
|
143
|
|
|
wp_enqueue_script( 'paypal-express-checkout' ); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
wp_add_inline_script( 'paypal-express-checkout', sprintf( |
|
147
|
|
|
"try{PaypalExpressCheckout.renderButton( '%d', '%d', '%s', '%d' );}catch(e){}", |
|
148
|
|
|
esc_js( $data['blog_id'] ), |
|
149
|
|
|
esc_js( $attrs['id'] ), |
|
150
|
|
|
esc_js( $data['dom_id'] ), |
|
151
|
|
|
esc_js( $data['multiple'] ) |
|
152
|
|
|
) ); |
|
153
|
|
|
|
|
154
|
|
|
return $this->output_shortcode( $data ); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
function output_admin_warning( $data ) { |
|
158
|
|
|
if ( ! current_user_can( 'manage_options' ) ) { |
|
159
|
|
|
return; |
|
160
|
|
|
} |
|
161
|
|
|
$css_prefix = self::$css_classname_prefix; |
|
162
|
|
|
|
|
163
|
|
|
$support_url = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) |
|
164
|
|
|
? 'https://support.wordpress.com/simple-payments/' |
|
165
|
|
|
: 'https://jetpack.com/support/simple-payment-button/'; |
|
166
|
|
|
|
|
167
|
|
|
return sprintf( ' |
|
168
|
|
|
<div class="%1$s"> |
|
169
|
|
|
<div class="%2$s"> |
|
170
|
|
|
<div class="%3$s"> |
|
171
|
|
|
<div class="%4$s" id="%5$s"> |
|
172
|
|
|
<p>%6$s</p> |
|
173
|
|
|
<p>%7$s</p> |
|
174
|
|
|
</div> |
|
175
|
|
|
</div> |
|
176
|
|
|
</div> |
|
177
|
|
|
</div> |
|
178
|
|
|
', |
|
179
|
|
|
esc_attr( "{$data['class']} ${css_prefix}-wrapper" ), |
|
180
|
|
|
esc_attr( "${css_prefix}-product" ), |
|
181
|
|
|
esc_attr( "${css_prefix}-details" ), |
|
182
|
|
|
esc_attr( "${css_prefix}-purchase-message show error" ), |
|
183
|
|
|
esc_attr( "{$data['dom_id']}-message-container" ), |
|
184
|
|
|
sprintf( |
|
185
|
|
|
wp_kses( |
|
186
|
|
|
__( 'Your plan doesn\'t include Simple Payments. <a href="%s" rel="noopener noreferrer" target="_blank">Learn more and upgrade</a>.', 'jetpack' ), |
|
187
|
|
|
array( 'a' => array( 'href' => array(), 'rel' => array(), 'target' => array() ) ) |
|
188
|
|
|
), |
|
189
|
|
|
esc_url( $support_url ) |
|
190
|
|
|
), |
|
191
|
|
|
esc_html__( '(Only administrators will see this message.)', 'jetpack' ) |
|
192
|
|
|
); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
function output_shortcode( $data ) { |
|
196
|
|
|
$items = ''; |
|
197
|
|
|
$css_prefix = self::$css_classname_prefix; |
|
198
|
|
|
|
|
199
|
|
|
if ( $data['multiple'] ) { |
|
200
|
|
|
$items = sprintf( ' |
|
201
|
|
|
<div class="%1$s"> |
|
202
|
|
|
<input class="%2$s" type="number" value="1" min="1" id="%3$s" /> |
|
203
|
|
|
</div> |
|
204
|
|
|
', |
|
205
|
|
|
esc_attr( "${css_prefix}-items" ), |
|
206
|
|
|
esc_attr( "${css_prefix}-items-number" ), |
|
207
|
|
|
esc_attr( "{$data['dom_id']}_number" ) |
|
208
|
|
|
); |
|
209
|
|
|
} |
|
210
|
|
|
$image = ""; |
|
211
|
|
|
if( has_post_thumbnail( $data['id'] ) ) { |
|
212
|
|
|
$image = sprintf( '<div class="%1$s"><div class="%2$s">%3$s</div></div>', |
|
213
|
|
|
esc_attr( "${css_prefix}-product-image" ), |
|
214
|
|
|
esc_attr( "${css_prefix}-image" ), |
|
215
|
|
|
get_the_post_thumbnail( $data['id'], 'full' ) |
|
216
|
|
|
); |
|
217
|
|
|
} |
|
218
|
|
|
return sprintf( ' |
|
219
|
|
|
<div class="%1$s"> |
|
220
|
|
|
<div class="%2$s"> |
|
221
|
|
|
%3$s |
|
222
|
|
|
<div class="%4$s"> |
|
223
|
|
|
<div class="%5$s"><p>%6$s</p></div> |
|
224
|
|
|
<div class="%7$s"><p>%8$s</p></div> |
|
225
|
|
|
<div class="%9$s"><p>%10$s</p></div> |
|
226
|
|
|
<div class="%11$s" id="%12$s"></div> |
|
227
|
|
|
<div class="%13$s"> |
|
228
|
|
|
%14$s |
|
229
|
|
|
<div class="%15$s" id="%16$s"></div> |
|
230
|
|
|
</div> |
|
231
|
|
|
</div> |
|
232
|
|
|
</div> |
|
233
|
|
|
</div> |
|
234
|
|
|
', |
|
235
|
|
|
esc_attr( "{$data['class']} ${css_prefix}-wrapper" ), |
|
236
|
|
|
esc_attr( "${css_prefix}-product" ), |
|
237
|
|
|
$image, |
|
238
|
|
|
esc_attr( "${css_prefix}-details" ), |
|
239
|
|
|
esc_attr( "${css_prefix}-title" ), |
|
240
|
|
|
$data['title'], |
|
241
|
|
|
esc_attr( "${css_prefix}-description" ), |
|
242
|
|
|
$data['description'], |
|
243
|
|
|
esc_attr( "${css_prefix}-price" ), |
|
244
|
|
|
esc_html( $data['price'] ), |
|
245
|
|
|
esc_attr( "${css_prefix}-purchase-message" ), |
|
246
|
|
|
esc_attr( "{$data['dom_id']}-message-container" ), |
|
247
|
|
|
esc_attr( "${css_prefix}-purchase-box" ), |
|
248
|
|
|
$items, |
|
249
|
|
|
esc_attr( "${css_prefix}-button" ), |
|
250
|
|
|
esc_attr( "{$data['dom_id']}_button" ) |
|
251
|
|
|
); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
function format_price( $formatted_price, $price, $currency, $all_data ) { |
|
255
|
|
|
if ( $formatted_price ) { |
|
256
|
|
|
return $formatted_price; |
|
257
|
|
|
} |
|
258
|
|
|
return "$price $currency"; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Allows custom post types to be used by REST API. |
|
263
|
|
|
* @param $post_types |
|
264
|
|
|
* @see hook 'rest_api_allowed_post_types' |
|
265
|
|
|
* @return array |
|
266
|
|
|
*/ |
|
267
|
|
|
function allow_rest_api_types( $post_types ) { |
|
268
|
|
|
$post_types[] = self::$post_type_order; |
|
269
|
|
|
$post_types[] = self::$post_type_product; |
|
270
|
|
|
return $post_types; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
function allow_sync_post_meta( $post_meta ) { |
|
274
|
|
|
return array_merge( $post_meta, array( |
|
275
|
|
|
'spay_paypal_id', |
|
276
|
|
|
'spay_status', |
|
277
|
|
|
'spay_product_id', |
|
278
|
|
|
'spay_quantity', |
|
279
|
|
|
'spay_price', |
|
280
|
|
|
'spay_customer_email', |
|
281
|
|
|
'spay_currency', |
|
282
|
|
|
'spay_cta', |
|
283
|
|
|
'spay_email', |
|
284
|
|
|
'spay_multiple', |
|
285
|
|
|
'spay_formatted_price', |
|
286
|
|
|
) ); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Sets up the custom post types for the module. |
|
291
|
|
|
*/ |
|
292
|
|
|
function setup_cpts() { |
|
293
|
|
|
|
|
294
|
|
|
/* |
|
295
|
|
|
* ORDER data structure. holds: |
|
296
|
|
|
* title = customer_name | 4xproduct_name |
|
297
|
|
|
* excerpt = customer_name + customer contact info + customer notes from paypal form |
|
298
|
|
|
* metadata: |
|
299
|
|
|
* spay_paypal_id - paypal id of transaction |
|
300
|
|
|
* spay_status |
|
301
|
|
|
* spay_product_id - post_id of bought product |
|
302
|
|
|
* spay_quantity - quantity of product |
|
303
|
|
|
* spay_price - item price at the time of purchase |
|
304
|
|
|
* spay_customer_email - customer email |
|
305
|
|
|
* ... (WIP) |
|
306
|
|
|
*/ |
|
307
|
|
|
$order_capabilities = array( |
|
308
|
|
|
'edit_post' => 'edit_posts', |
|
309
|
|
|
'read_post' => 'read_private_posts', |
|
310
|
|
|
'delete_post' => 'delete_posts', |
|
311
|
|
|
'edit_posts' => 'edit_posts', |
|
312
|
|
|
'edit_others_posts' => 'edit_others_posts', |
|
313
|
|
|
'publish_posts' => 'publish_posts', |
|
314
|
|
|
'read_private_posts' => 'read_private_posts', |
|
315
|
|
|
); |
|
316
|
|
|
$order_args = array( |
|
317
|
|
|
'label' => esc_html_x( 'Order', 'noun: a quantity of goods or items purchased or sold', 'jetpack' ), |
|
318
|
|
|
'description' => esc_html__( 'Simple Payments orders', 'jetpack' ), |
|
319
|
|
|
'supports' => array( 'custom-fields', 'excerpt' ), |
|
320
|
|
|
'hierarchical' => false, |
|
321
|
|
|
'public' => false, |
|
322
|
|
|
'show_ui' => false, |
|
323
|
|
|
'show_in_menu' => false, |
|
324
|
|
|
'show_in_admin_bar' => false, |
|
325
|
|
|
'show_in_nav_menus' => false, |
|
326
|
|
|
'can_export' => true, |
|
327
|
|
|
'has_archive' => false, |
|
328
|
|
|
'exclude_from_search' => true, |
|
329
|
|
|
'publicly_queryable' => false, |
|
330
|
|
|
'rewrite' => false, |
|
331
|
|
|
'capabilities' => $order_capabilities, |
|
332
|
|
|
'show_in_rest' => true, |
|
333
|
|
|
); |
|
334
|
|
|
register_post_type( self::$post_type_order, $order_args ); |
|
335
|
|
|
|
|
336
|
|
|
/* |
|
337
|
|
|
* PRODUCT data structure. Holds: |
|
338
|
|
|
* title - title |
|
339
|
|
|
* content - description |
|
340
|
|
|
* thumbnail - image |
|
341
|
|
|
* metadata: |
|
342
|
|
|
* spay_price - price |
|
343
|
|
|
* spay_formatted_price |
|
344
|
|
|
* spay_currency - currency code |
|
345
|
|
|
* spay_cta - text with "Buy" or other CTA |
|
346
|
|
|
* spay_email - paypal email |
|
347
|
|
|
* spay_multiple - allow for multiple items |
|
348
|
|
|
* spay_status - status. { enabled | disabled } |
|
349
|
|
|
*/ |
|
350
|
|
|
$product_capabilities = array( |
|
351
|
|
|
'edit_post' => 'edit_posts', |
|
352
|
|
|
'read_post' => 'read_private_posts', |
|
353
|
|
|
'delete_post' => 'delete_posts', |
|
354
|
|
|
'edit_posts' => 'publish_posts', |
|
355
|
|
|
'edit_others_posts' => 'edit_others_posts', |
|
356
|
|
|
'publish_posts' => 'publish_posts', |
|
357
|
|
|
'read_private_posts' => 'read_private_posts', |
|
358
|
|
|
); |
|
359
|
|
|
$product_args = array( |
|
360
|
|
|
'label' => esc_html__( 'Product', 'jetpack' ), |
|
361
|
|
|
'description' => esc_html__( 'Simple Payments products', 'jetpack' ), |
|
362
|
|
|
'supports' => array( 'title', 'editor','thumbnail', 'custom-fields', 'author' ), |
|
363
|
|
|
'hierarchical' => false, |
|
364
|
|
|
'public' => false, |
|
365
|
|
|
'show_ui' => false, |
|
366
|
|
|
'show_in_menu' => false, |
|
367
|
|
|
'show_in_admin_bar' => false, |
|
368
|
|
|
'show_in_nav_menus' => false, |
|
369
|
|
|
'can_export' => true, |
|
370
|
|
|
'has_archive' => false, |
|
371
|
|
|
'exclude_from_search' => true, |
|
372
|
|
|
'publicly_queryable' => false, |
|
373
|
|
|
'rewrite' => false, |
|
374
|
|
|
'capabilities' => $product_capabilities, |
|
375
|
|
|
'show_in_rest' => true, |
|
376
|
|
|
); |
|
377
|
|
|
register_post_type( self::$post_type_product, $product_args ); |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
} |
|
381
|
|
|
Jetpack_Simple_Payments::getInstance(); |
|
382
|
|
|
|