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
|
|
|
|
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() { |
44
|
|
|
add_action( 'init', array( $this, 'init_hook_action' ) ); |
45
|
|
|
} |
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
|
|
|
|
58
|
|
|
add_filter( 'the_content', array( $this, 'remove_auto_paragraph_from_product_description' ), 0 ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function register_gutenberg_block() { |
62
|
|
|
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); |
63
|
|
|
add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_assets' ) ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function enqueue_block_editor_assets() { |
67
|
|
|
wp_enqueue_script( |
68
|
|
|
'gutenberg-simple-payments-button', |
69
|
|
|
plugins_url( 'simple-payments-block.js', __FILE__ ), |
70
|
|
|
array( 'wp-blocks', 'wp-element' ) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function enqueue_block_assets() { |
75
|
|
|
wp_enqueue_style( |
76
|
|
|
'gutenberg-simple-payments-button-styles', |
77
|
|
|
plugins_url( 'simple-payments-block.css', __FILE__ ), |
78
|
|
|
array(), |
79
|
|
|
JETPACK__VERSION |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
function remove_auto_paragraph_from_product_description( $content ) { |
84
|
|
|
if ( get_post_type() === self::$post_type_product ) { |
85
|
|
|
remove_filter( 'the_content', 'wpautop' ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $content; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
function get_blog_id() { |
92
|
|
|
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
93
|
|
|
return get_current_blog_id(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return Jetpack_Options::get_option( 'id' ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
function parse_shortcode( $attrs, $content = false ) { |
100
|
|
|
if ( empty( $attrs['id'] ) ) { |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
$product = get_post( $attrs['id'] ); |
104
|
|
|
if ( ! $product || is_wp_error( $product ) ) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
if ( $product->post_type !== self::$post_type_product || 'trash' === $product->post_status ) { |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// We allow for overriding the presentation labels |
112
|
|
|
$data = shortcode_atts( array( |
113
|
|
|
'blog_id' => $this->get_blog_id(), |
114
|
|
|
'dom_id' => uniqid( self::$css_classname_prefix . '-' . $product->ID . '_', true ), |
115
|
|
|
'class' => self::$css_classname_prefix . '-' . $product->ID, |
116
|
|
|
'title' => get_the_title( $product ), |
117
|
|
|
'description' => $product->post_content, |
118
|
|
|
'cta' => get_post_meta( $product->ID, 'spay_cta', true ), |
119
|
|
|
'multiple' => get_post_meta( $product->ID, 'spay_multiple', true ) || '0' |
120
|
|
|
), $attrs ); |
121
|
|
|
|
122
|
|
|
$data['price'] = $this->format_price( |
123
|
|
|
get_post_meta( $product->ID, 'spay_formatted_price', true ), |
124
|
|
|
get_post_meta( $product->ID, 'spay_price', true ), |
125
|
|
|
get_post_meta( $product->ID, 'spay_currency', true ), |
126
|
|
|
$data |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$data['id'] = $attrs['id']; |
130
|
|
|
if ( ! wp_script_is( 'paypal-express-checkout', 'enqueued' ) ) { |
131
|
|
|
wp_enqueue_script( 'paypal-express-checkout' ); |
132
|
|
|
} |
133
|
|
|
if ( ! wp_style_is( 'simple-payments', 'enqueued' ) ) { |
134
|
|
|
wp_enqueue_style( 'simple-payments', plugins_url( 'simple-payments.css', __FILE__ ), array( 'dashicons' ) ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
wp_add_inline_script( 'paypal-express-checkout', sprintf( |
138
|
|
|
"try{PaypalExpressCheckout.renderButton( '%d', '%d', '%s', '%d' );}catch(e){}", |
139
|
|
|
esc_js( $data['blog_id'] ), |
140
|
|
|
esc_js( $attrs['id'] ), |
141
|
|
|
esc_js( $data['dom_id'] ), |
142
|
|
|
esc_js( $data['multiple'] ) |
143
|
|
|
) ); |
144
|
|
|
|
145
|
|
|
return $this->output_shortcode( $data ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
function output_shortcode( $data ) { |
149
|
|
|
$items = ''; |
150
|
|
|
$css_prefix = self::$css_classname_prefix; |
151
|
|
|
|
152
|
|
|
if ( $data['multiple'] ) { |
153
|
|
|
$items="<div class='${css_prefix}-items'> |
|
|
|
|
154
|
|
|
<input class='${css_prefix}-items-number' type='number' value='1' min='1' id='{$data['dom_id']}_number' /> |
155
|
|
|
</div>"; |
156
|
|
|
} |
157
|
|
|
$image = ""; |
158
|
|
|
if( has_post_thumbnail( $data['id'] ) ) { |
159
|
|
|
$image = "<div class='${css_prefix}-product-image'><div class='${css_prefix}-image'>" . get_the_post_thumbnail( $data['id'], 'full' ) . "</div></div>"; |
160
|
|
|
} |
161
|
|
|
return " |
162
|
|
|
<div class='{$data['class']} ${css_prefix}-wrapper'> |
163
|
|
|
<div class='${css_prefix}-product'> |
164
|
|
|
{$image} |
165
|
|
|
<div class='${css_prefix}-details'> |
166
|
|
|
<div class='${css_prefix}-title'><p>{$data['title']}</p></div> |
167
|
|
|
<div class='${css_prefix}-description'><p>{$data['description']}</p></div> |
168
|
|
|
<div class='${css_prefix}-price'><p>{$data['price']}</p></div> |
169
|
|
|
<div class='${css_prefix}-purchase-message' id='{$data['dom_id']}-message-container'></div> |
170
|
|
|
<div class='${css_prefix}-purchase-box'> |
171
|
|
|
{$items} |
172
|
|
|
<div class='${css_prefix}-button' id='{$data['dom_id']}_button'></div> |
173
|
|
|
</div> |
174
|
|
|
</div> |
175
|
|
|
</div> |
176
|
|
|
</div> |
177
|
|
|
"; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
function format_price( $formatted_price, $price, $currency, $all_data ) { |
181
|
|
|
if ( $formatted_price ) { |
182
|
|
|
return $formatted_price; |
183
|
|
|
} |
184
|
|
|
return "$price $currency"; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Allows custom post types to be used by REST API. |
189
|
|
|
* @param $post_types |
190
|
|
|
* @see hook 'rest_api_allowed_post_types' |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
|
|
function allow_rest_api_types( $post_types ) { |
194
|
|
|
$post_types[] = self::$post_type_order; |
195
|
|
|
$post_types[] = self::$post_type_product; |
196
|
|
|
return $post_types; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
function allow_sync_post_meta( $post_meta ) { |
200
|
|
|
return array_merge( $post_meta, array( |
201
|
|
|
'spay_paypal_id', |
202
|
|
|
'spay_status', |
203
|
|
|
'spay_product_id', |
204
|
|
|
'spay_quantity', |
205
|
|
|
'spay_price', |
206
|
|
|
'spay_customer_email', |
207
|
|
|
'spay_currency', |
208
|
|
|
'spay_cta', |
209
|
|
|
'spay_email', |
210
|
|
|
'spay_multiple', |
211
|
|
|
'spay_formatted_price', |
212
|
|
|
) ); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Sets up the custom post types for the module. |
217
|
|
|
*/ |
218
|
|
|
function setup_cpts() { |
219
|
|
|
|
220
|
|
|
/* |
221
|
|
|
* ORDER data structure. holds: |
222
|
|
|
* title = customer_name | 4xproduct_name |
223
|
|
|
* excerpt = customer_name + customer contact info + customer notes from paypal form |
224
|
|
|
* metadata: |
225
|
|
|
* spay_paypal_id - paypal id of transaction |
226
|
|
|
* spay_status |
227
|
|
|
* spay_product_id - post_id of bought product |
228
|
|
|
* spay_quantity - quantity of product |
229
|
|
|
* spay_price - item price at the time of purchase |
230
|
|
|
* spay_customer_email - customer email |
231
|
|
|
* ... (WIP) |
232
|
|
|
*/ |
233
|
|
|
$order_capabilities = array( |
234
|
|
|
'edit_post' => 'edit_posts', |
235
|
|
|
'read_post' => 'read_private_posts', |
236
|
|
|
'delete_post' => 'delete_posts', |
237
|
|
|
'edit_posts' => 'edit_posts', |
238
|
|
|
'edit_others_posts' => 'edit_others_posts', |
239
|
|
|
'publish_posts' => 'publish_posts', |
240
|
|
|
'read_private_posts' => 'read_private_posts', |
241
|
|
|
); |
242
|
|
|
$order_args = array( |
243
|
|
|
'label' => esc_html__( 'Order', 'jetpack' ), |
244
|
|
|
'description' => esc_html__( 'Simple Payments orders', 'jetpack' ), |
245
|
|
|
'supports' => array( 'custom-fields', 'excerpt' ), |
246
|
|
|
'hierarchical' => false, |
247
|
|
|
'public' => false, |
248
|
|
|
'show_ui' => false, |
249
|
|
|
'show_in_menu' => false, |
250
|
|
|
'show_in_admin_bar' => false, |
251
|
|
|
'show_in_nav_menus' => false, |
252
|
|
|
'can_export' => true, |
253
|
|
|
'has_archive' => false, |
254
|
|
|
'exclude_from_search' => true, |
255
|
|
|
'publicly_queryable' => false, |
256
|
|
|
'rewrite' => false, |
257
|
|
|
'capabilities' => $order_capabilities, |
258
|
|
|
'show_in_rest' => true, |
259
|
|
|
); |
260
|
|
|
register_post_type( self::$post_type_order, $order_args ); |
261
|
|
|
|
262
|
|
|
/* |
263
|
|
|
* PRODUCT data structure. Holds: |
264
|
|
|
* title - title |
265
|
|
|
* content - description |
266
|
|
|
* thumbnail - image |
267
|
|
|
* metadata: |
268
|
|
|
* spay_price - price |
269
|
|
|
* spay_formatted_price |
270
|
|
|
* spay_currency - currency code |
271
|
|
|
* spay_cta - text with "Buy" or other CTA |
272
|
|
|
* spay_email - paypal email |
273
|
|
|
* spay_multiple - allow for multiple items |
274
|
|
|
* spay_status - status. { enabled | disabled } |
275
|
|
|
*/ |
276
|
|
|
$product_capabilities = array( |
277
|
|
|
'edit_post' => 'edit_posts', |
278
|
|
|
'read_post' => 'read_private_posts', |
279
|
|
|
'delete_post' => 'delete_posts', |
280
|
|
|
'edit_posts' => 'edit_posts', |
281
|
|
|
'edit_others_posts' => 'edit_others_posts', |
282
|
|
|
'publish_posts' => 'publish_posts', |
283
|
|
|
'read_private_posts' => 'read_private_posts', |
284
|
|
|
); |
285
|
|
|
$product_args = array( |
286
|
|
|
'label' => esc_html__( 'Product', 'jetpack' ), |
287
|
|
|
'description' => esc_html__( 'Simple Payments products', 'jetpack' ), |
288
|
|
|
'supports' => array( 'title', 'editor','thumbnail', 'custom-fields' ), |
289
|
|
|
'hierarchical' => false, |
290
|
|
|
'public' => false, |
291
|
|
|
'show_ui' => false, |
292
|
|
|
'show_in_menu' => false, |
293
|
|
|
'show_in_admin_bar' => false, |
294
|
|
|
'show_in_nav_menus' => false, |
295
|
|
|
'can_export' => true, |
296
|
|
|
'has_archive' => false, |
297
|
|
|
'exclude_from_search' => true, |
298
|
|
|
'publicly_queryable' => false, |
299
|
|
|
'rewrite' => false, |
300
|
|
|
'capabilities' => $product_capabilities, |
301
|
|
|
'show_in_rest' => true, |
302
|
|
|
); |
303
|
|
|
register_post_type( self::$post_type_product, $product_args ); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
} |
307
|
|
|
Jetpack_Simple_Payments::getInstance(); |
308
|
|
|
|
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.