|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class Jetpack_Simple_Payments { |
|
4
|
|
|
// These have to be under 20 chars because that is CPT limit. |
|
5
|
|
|
static $post_type_order = 'jp_pay_order'; |
|
|
|
|
|
|
6
|
|
|
static $post_type_product = 'jp_pay_product'; |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
static $shortcode = 'simple-payment'; |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
// Classic singleton pattern: |
|
11
|
|
|
private static $instance; |
|
12
|
|
|
private function __construct() {} |
|
13
|
|
|
static function getInstance() { |
|
14
|
|
|
if ( ! self::$instance ) { |
|
15
|
|
|
self::$instance = new self(); |
|
16
|
|
|
self::$instance->register_init_hook(); |
|
17
|
|
|
} |
|
18
|
|
|
return self::$instance; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
private function register_scripts() { |
|
22
|
|
|
wp_register_script( 'paypal-checkout-js', 'https://www.paypalobjects.com/api/checkout.js' ); |
|
23
|
|
|
wp_register_script( 'paypal-express-checkout', plugins_url( '/paypal-express-checkout.js', __FILE__ ) , array( 'paypal-checkout-js' ) ); |
|
24
|
|
|
} |
|
25
|
|
|
private function register_init_hook() { |
|
26
|
|
|
add_action( 'init', array( $this, 'init_hook_action' ) ); |
|
27
|
|
|
} |
|
28
|
|
|
private function register_shortcode() { |
|
29
|
|
|
add_shortcode( static::$shortcode, array( $this, 'parse_shortcode' ) ); |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function init_hook_action() { |
|
33
|
|
|
add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) ); |
|
34
|
|
|
$this->register_scripts(); |
|
35
|
|
|
$this->register_shortcode(); |
|
36
|
|
|
$this->setup_cpts(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
function parse_shortcode( $attrs, $content = false ) { |
|
40
|
|
|
if( empty( $attrs[ 'id' ] ) ) { |
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
$product = get_post( $attrs[ 'id' ] ); |
|
44
|
|
|
if( is_wp_error( $product ) ) { |
|
45
|
|
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
if( $product->post_type !== self::$post_type_product ) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// We allow for overriding the presentation labels |
|
52
|
|
|
$data = shortcode_atts( array( |
|
53
|
|
|
'dom_id' => uniqid( 'jp_simple_payments__button_' . $product->ID . '_' ), |
|
54
|
|
|
'class' => 'jp_simple_payments__' . $product->ID, |
|
55
|
|
|
'title' => get_the_title( $product ), |
|
56
|
|
|
'description' => $product->post_content, |
|
57
|
|
|
'cta' => get_post_meta( $product->ID, 'spay_cta', true ), |
|
58
|
|
|
), $attrs ); |
|
59
|
|
|
$data[ 'price' ] = $this->format_price( |
|
60
|
|
|
get_post_meta( $product->ID, 'spay_price', true ), |
|
61
|
|
|
get_post_meta( $product->ID, 'spay_currency', true ), |
|
62
|
|
|
$data |
|
|
|
|
|
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
wp_enqueue_script( 'paypal-express-checkout' ); |
|
66
|
|
|
wp_add_inline_script( 'paypal-express-checkout', "try{PaypalExpressCheckout.renderButton( {$data['dom_id']} );}catch(e){}" ); |
|
67
|
|
|
|
|
68
|
|
|
return $this->output_shortcode( $data ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
function output_shortcode( $data ) { |
|
72
|
|
|
$output = <<<TEMPLATE |
|
73
|
|
|
<div class="{$data[ 'class' ]} jp_simple_payments__wrapper"> |
|
74
|
|
|
<h2 class="jp_simple_payments__title">{$data['title']}</h2> |
|
75
|
|
|
<div class="jp_simple_payments__description">{$data['description']}</div> |
|
76
|
|
|
<div class="jp_simple_payments__price">{$data['price']}</div> |
|
77
|
|
|
<div class="jp_simple_payments__button" id="{$data['dom_id']}"></div> |
|
78
|
|
|
</div> |
|
79
|
|
|
TEMPLATE; |
|
80
|
|
|
return $output; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
function format_price( $price, $currency ) { |
|
84
|
|
|
// TODO: better logic here. |
|
|
|
|
|
|
85
|
|
|
return $price. " " . $currency; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Allows custom post types to be used by REST API. |
|
90
|
|
|
* @param $post_types |
|
91
|
|
|
* @see hook 'rest_api_allowed_post_types' |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
function allow_rest_api_types( $post_types ) { |
|
95
|
|
|
$post_types[] = self::$post_type_order; |
|
96
|
|
|
$post_types[] = self::$post_type_product; |
|
97
|
|
|
return $post_types; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Sets up the custom post types for the module. |
|
102
|
|
|
*/ |
|
103
|
|
|
function setup_cpts() { |
|
104
|
|
|
|
|
105
|
|
|
/* |
|
106
|
|
|
* ORDER data structure. holds: |
|
107
|
|
|
* title = customer_name | 4xproduct_name |
|
108
|
|
|
* excerpt = customer_name + customer contact info + customer notes from paypal form |
|
109
|
|
|
* metadata: |
|
110
|
|
|
* spay_paypal_id - paypal id of transaction |
|
111
|
|
|
* spay_status |
|
112
|
|
|
* spay_product_id - post_id of bought product |
|
113
|
|
|
* spay_quantity - quantity of product |
|
114
|
|
|
* spay_price - item price at the time of purchase |
|
115
|
|
|
* spay_customer_email - customer email |
|
116
|
|
|
* ... (WIP) |
|
117
|
|
|
*/ |
|
118
|
|
|
$order_capabilities = array( |
|
119
|
|
|
'edit_post' => 'edit_posts', |
|
120
|
|
|
'read_post' => 'read_private_posts', |
|
121
|
|
|
'delete_post' => 'delete_posts', |
|
122
|
|
|
'edit_posts' => 'edit_posts', |
|
123
|
|
|
'edit_others_posts' => 'edit_others_posts', |
|
124
|
|
|
'publish_posts' => 'publish_posts', |
|
125
|
|
|
'read_private_posts' => 'read_private_posts', |
|
126
|
|
|
); |
|
127
|
|
|
$order_args = array( |
|
128
|
|
|
'label' => __( 'Order', 'jetpack' ), |
|
129
|
|
|
'description' => __( 'Simple Payments orders', 'jetpack' ), |
|
130
|
|
|
'supports' => array( 'custom-fields', 'excerpt' ), |
|
131
|
|
|
'hierarchical' => false, |
|
132
|
|
|
'public' => false, |
|
133
|
|
|
'show_ui' => false, |
|
134
|
|
|
'show_in_menu' => false, |
|
135
|
|
|
'show_in_admin_bar' => false, |
|
136
|
|
|
'show_in_nav_menus' => false, |
|
137
|
|
|
'can_export' => true, |
|
138
|
|
|
'has_archive' => false, |
|
139
|
|
|
'exclude_from_search' => true, |
|
140
|
|
|
'publicly_queryable' => false, |
|
141
|
|
|
'rewrite' => false, |
|
142
|
|
|
'capabilities' => $order_capabilities, |
|
143
|
|
|
'show_in_rest' => true, |
|
144
|
|
|
); |
|
145
|
|
|
register_post_type( self::$post_type_order, $order_args ); |
|
146
|
|
|
|
|
147
|
|
|
/* |
|
148
|
|
|
* PRODUCT data structure. Holds: |
|
149
|
|
|
* title - title |
|
150
|
|
|
* content - description |
|
151
|
|
|
* thumbnail - image |
|
152
|
|
|
* metadata: |
|
153
|
|
|
* spay_price - price |
|
154
|
|
|
* spay_currency - currency code |
|
155
|
|
|
* spay_cta - text with "Buy" or other CTA |
|
156
|
|
|
* spay_email - paypal email |
|
157
|
|
|
* spay_multiple - allow for multiple items |
|
158
|
|
|
* spay_status - status. { enabled | disabled } |
|
159
|
|
|
*/ |
|
160
|
|
|
$product_capabilities = array( |
|
161
|
|
|
'edit_post' => 'edit_posts', |
|
162
|
|
|
'read_post' => 'read_private_posts', |
|
163
|
|
|
'delete_post' => 'delete_posts', |
|
164
|
|
|
'edit_posts' => 'edit_posts', |
|
165
|
|
|
'edit_others_posts' => 'edit_others_posts', |
|
166
|
|
|
'publish_posts' => 'publish_posts', |
|
167
|
|
|
'read_private_posts' => 'read_private_posts', |
|
168
|
|
|
); |
|
169
|
|
|
$product_args = array( |
|
170
|
|
|
'label' => __( 'Product', 'jetpack' ), |
|
171
|
|
|
'description' => __( 'Simple Payments products', 'jetpack' ), |
|
172
|
|
|
'supports' => array( 'title', 'editor','thumbnail', 'custom-fields' ), |
|
173
|
|
|
'hierarchical' => false, |
|
174
|
|
|
'public' => false, |
|
175
|
|
|
'show_ui' => false, |
|
176
|
|
|
'show_in_menu' => false, |
|
177
|
|
|
'show_in_admin_bar' => false, |
|
178
|
|
|
'show_in_nav_menus' => false, |
|
179
|
|
|
'can_export' => true, |
|
180
|
|
|
'has_archive' => false, |
|
181
|
|
|
'exclude_from_search' => true, |
|
182
|
|
|
'publicly_queryable' => false, |
|
183
|
|
|
'rewrite' => false, |
|
184
|
|
|
'capabilities' => $product_capabilities, |
|
185
|
|
|
'show_in_rest' => true, |
|
186
|
|
|
); |
|
187
|
|
|
register_post_type( self::$post_type_product, $product_args ); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
Jetpack_Simple_Payments::getInstance(); |
|
192
|
|
|
|
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.