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-button.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
|
|
|
$post = get_post( $attrs[ 'id' ] ); |
44
|
|
|
if( is_wp_error( $post ) ) { |
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
if( $post->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_' . $post->ID . '_' ), |
54
|
|
|
'style' => 'jp_simple_payments__' . $post->ID, |
55
|
|
|
'title' => get_the_title( $post ), |
56
|
|
|
'description' => get_the_content( $post ), |
57
|
|
|
'cta' => get_post_meta( $post->ID, 'spay_cta', true ), |
58
|
|
|
), $attrs ); |
59
|
|
|
|
60
|
|
|
wp_enqueue_script( 'paypal-express-checkout' ); |
61
|
|
|
wp_add_inline_script( 'paypal-express-checkout', "try{PaypalExpressCheckoutButton( {$data['dom_id']} );}catch(e){}" ); |
62
|
|
|
|
63
|
|
|
return output_shortcode( $data ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
function output_shortcode( $data ) { |
67
|
|
|
$output = <<<TEMPLATE |
68
|
|
|
<div class="{$data[ 'class' ]} jp_simple_payments__wrapper"> |
69
|
|
|
<h2 class="jp_simple_payments__title">{$data['title']}</h2> |
70
|
|
|
<div class="jp_simple_payments__description">{$data['description']}</div> |
71
|
|
|
<div class="jp_simple_payments__button" id="{$data['dom_id']}"></div> |
72
|
|
|
</div> |
73
|
|
|
TEMPLATE; |
74
|
|
|
return $output; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Allows custom post types to be used by REST API. |
79
|
|
|
* @param $post_types |
80
|
|
|
* @see hook 'rest_api_allowed_post_types' |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
function allow_rest_api_types( $post_types ) { |
84
|
|
|
$post_types[] = self::$post_type_order; |
85
|
|
|
$post_types[] = self::$post_type_product; |
86
|
|
|
return $post_types; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sets up the custom post types for the module. |
91
|
|
|
*/ |
92
|
|
|
function setup_cpts() { |
93
|
|
|
|
94
|
|
|
/* |
95
|
|
|
* ORDER data structure. holds: |
96
|
|
|
* title = customer_name | 4xproduct_name |
97
|
|
|
* excerpt = customer_name + customer contact info + customer notes from paypal form |
98
|
|
|
* metadata: |
99
|
|
|
* spay_paypal_id - paypal id of transaction |
100
|
|
|
* spay_status |
101
|
|
|
* spay_product_id - post_id of bought product |
102
|
|
|
* spay_quantity - quantity of product |
103
|
|
|
* spay_price - item price at the time of purchase |
104
|
|
|
* spay_customer_email - customer email |
105
|
|
|
* ... (WIP) |
106
|
|
|
*/ |
107
|
|
|
$order_capabilities = array( |
108
|
|
|
'edit_post' => 'edit_posts', |
109
|
|
|
'read_post' => 'read_private_posts', |
110
|
|
|
'delete_post' => 'delete_posts', |
111
|
|
|
'edit_posts' => 'edit_posts', |
112
|
|
|
'edit_others_posts' => 'edit_others_posts', |
113
|
|
|
'publish_posts' => 'publish_posts', |
114
|
|
|
'read_private_posts' => 'read_private_posts', |
115
|
|
|
); |
116
|
|
|
$order_args = array( |
117
|
|
|
'label' => __( 'Order', 'jetpack' ), |
118
|
|
|
'description' => __( 'Simple Payments orders', 'jetpack' ), |
119
|
|
|
'supports' => array( 'custom-fields', 'excerpt' ), |
120
|
|
|
'hierarchical' => false, |
121
|
|
|
'public' => false, |
122
|
|
|
'show_ui' => false, |
123
|
|
|
'show_in_menu' => false, |
124
|
|
|
'show_in_admin_bar' => false, |
125
|
|
|
'show_in_nav_menus' => false, |
126
|
|
|
'can_export' => true, |
127
|
|
|
'has_archive' => false, |
128
|
|
|
'exclude_from_search' => true, |
129
|
|
|
'publicly_queryable' => false, |
130
|
|
|
'rewrite' => false, |
131
|
|
|
'capabilities' => $order_capabilities, |
132
|
|
|
'show_in_rest' => true, |
133
|
|
|
); |
134
|
|
|
register_post_type( self::$post_type_order, $order_args ); |
135
|
|
|
|
136
|
|
|
/* |
137
|
|
|
* PRODUCT data structure. Holds: |
138
|
|
|
* title - title |
139
|
|
|
* content - description |
140
|
|
|
* thumbnail - image |
141
|
|
|
* metadata: |
142
|
|
|
* spay_price - price |
143
|
|
|
* spay_currency - currency code |
144
|
|
|
* spay_cta - text with "Buy" or other CTA |
145
|
|
|
* spay_email - paypal email |
146
|
|
|
* spay_multiple - allow for multiple items |
147
|
|
|
* spay_status - status. { enabled | disabled } |
148
|
|
|
*/ |
149
|
|
|
$product_capabilities = array( |
150
|
|
|
'edit_post' => 'edit_posts', |
151
|
|
|
'read_post' => 'read_private_posts', |
152
|
|
|
'delete_post' => 'delete_posts', |
153
|
|
|
'edit_posts' => 'edit_posts', |
154
|
|
|
'edit_others_posts' => 'edit_others_posts', |
155
|
|
|
'publish_posts' => 'publish_posts', |
156
|
|
|
'read_private_posts' => 'read_private_posts', |
157
|
|
|
); |
158
|
|
|
$product_args = array( |
159
|
|
|
'label' => __( 'Product', 'jetpack' ), |
160
|
|
|
'description' => __( 'Simple Payments products', 'jetpack' ), |
161
|
|
|
'supports' => array( 'title', 'editor','thumbnail', 'custom-fields' ), |
162
|
|
|
'hierarchical' => false, |
163
|
|
|
'public' => false, |
164
|
|
|
'show_ui' => false, |
165
|
|
|
'show_in_menu' => false, |
166
|
|
|
'show_in_admin_bar' => false, |
167
|
|
|
'show_in_nav_menus' => false, |
168
|
|
|
'can_export' => true, |
169
|
|
|
'has_archive' => false, |
170
|
|
|
'exclude_from_search' => true, |
171
|
|
|
'publicly_queryable' => false, |
172
|
|
|
'rewrite' => false, |
173
|
|
|
'capabilities' => $product_capabilities, |
174
|
|
|
'show_in_rest' => true, |
175
|
|
|
); |
176
|
|
|
register_post_type( self::$post_type_product, $product_args ); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
} |
180
|
|
|
Jetpack_Simple_Payments::getInstance(); |
181
|
|
|
|
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.