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->setup_cpts(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
function parse_shortcode( $attrs, $content = false ) { |
39
|
|
|
$config = shortcode_atts( array( |
|
|
|
|
40
|
|
|
'class' => 'caption', |
41
|
|
|
), $attrs ); |
42
|
|
|
wp_add_inline_script( 'paypal-express-checkout', 'try{PaypalExpressCheckoutButton( { type: 2 } );}catch(e){}' ); |
43
|
|
|
|
44
|
|
|
return "<b>POTATO</b>"; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Allows custom post types to be used by REST API. |
49
|
|
|
* @param $post_types |
50
|
|
|
* @see hook 'rest_api_allowed_post_types' |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
function allow_rest_api_types( $post_types ) { |
54
|
|
|
$post_types[] = self::$post_type_order; |
55
|
|
|
$post_types[] = self::$post_type_product; |
56
|
|
|
return $post_types; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Sets up the custom post types for the module. |
61
|
|
|
*/ |
62
|
|
|
function setup_cpts() { |
63
|
|
|
|
64
|
|
|
/* |
65
|
|
|
* ORDER data structure. holds: |
66
|
|
|
* title = customer_name | 4xproduct_name |
67
|
|
|
* excerpt = customer_name + customer contact info + customer notes from paypal form |
68
|
|
|
* metadata: |
69
|
|
|
* spay_paypal_id - paypal id of transaction |
70
|
|
|
* spay_status |
71
|
|
|
* spay_product_id - post_id of bought product |
72
|
|
|
* spay_quantity - quantity of product |
73
|
|
|
* spay_price - item price at the time of purchase |
74
|
|
|
* spay_customer_email - customer email |
75
|
|
|
* ... (WIP) |
76
|
|
|
*/ |
77
|
|
|
$order_capabilities = array( |
78
|
|
|
'edit_post' => 'edit_posts', |
79
|
|
|
'read_post' => 'read_private_posts', |
80
|
|
|
'delete_post' => 'delete_posts', |
81
|
|
|
'edit_posts' => 'edit_posts', |
82
|
|
|
'edit_others_posts' => 'edit_others_posts', |
83
|
|
|
'publish_posts' => 'publish_posts', |
84
|
|
|
'read_private_posts' => 'read_private_posts', |
85
|
|
|
); |
86
|
|
|
$order_args = array( |
87
|
|
|
'label' => __( 'Order', 'jetpack' ), |
88
|
|
|
'description' => __( 'Simple Payments orders', 'jetpack' ), |
89
|
|
|
'supports' => array( 'custom-fields', 'excerpt' ), |
90
|
|
|
'hierarchical' => false, |
91
|
|
|
'public' => false, |
92
|
|
|
'show_ui' => false, |
93
|
|
|
'show_in_menu' => false, |
94
|
|
|
'show_in_admin_bar' => false, |
95
|
|
|
'show_in_nav_menus' => false, |
96
|
|
|
'can_export' => true, |
97
|
|
|
'has_archive' => false, |
98
|
|
|
'exclude_from_search' => true, |
99
|
|
|
'publicly_queryable' => false, |
100
|
|
|
'rewrite' => false, |
101
|
|
|
'capabilities' => $order_capabilities, |
102
|
|
|
'show_in_rest' => true, |
103
|
|
|
); |
104
|
|
|
register_post_type( self::$post_type_order, $order_args ); |
105
|
|
|
|
106
|
|
|
/* |
107
|
|
|
* PRODUCT data structure. Holds: |
108
|
|
|
* title - title |
109
|
|
|
* content - description |
110
|
|
|
* thumbnail - image |
111
|
|
|
* metadata: |
112
|
|
|
* spay_price - price |
113
|
|
|
* spay_currency - currency code |
114
|
|
|
* spay_cta - text with "Buy" or other CTA |
115
|
|
|
* spay_email - paypal email |
116
|
|
|
* spay_multiple - allow for multiple items |
117
|
|
|
* spay_status - status. { enabled | disabled } |
118
|
|
|
*/ |
119
|
|
|
$product_capabilities = array( |
120
|
|
|
'edit_post' => 'edit_posts', |
121
|
|
|
'read_post' => 'read_private_posts', |
122
|
|
|
'delete_post' => 'delete_posts', |
123
|
|
|
'edit_posts' => 'edit_posts', |
124
|
|
|
'edit_others_posts' => 'edit_others_posts', |
125
|
|
|
'publish_posts' => 'publish_posts', |
126
|
|
|
'read_private_posts' => 'read_private_posts', |
127
|
|
|
); |
128
|
|
|
$product_args = array( |
129
|
|
|
'label' => __( 'Product', 'jetpack' ), |
130
|
|
|
'description' => __( 'Simple Payments products', 'jetpack' ), |
131
|
|
|
'supports' => array( 'title', 'editor','thumbnail', 'custom-fields' ), |
132
|
|
|
'hierarchical' => false, |
133
|
|
|
'public' => false, |
134
|
|
|
'show_ui' => false, |
135
|
|
|
'show_in_menu' => false, |
136
|
|
|
'show_in_admin_bar' => false, |
137
|
|
|
'show_in_nav_menus' => false, |
138
|
|
|
'can_export' => true, |
139
|
|
|
'has_archive' => false, |
140
|
|
|
'exclude_from_search' => true, |
141
|
|
|
'publicly_queryable' => false, |
142
|
|
|
'rewrite' => false, |
143
|
|
|
'capabilities' => $product_capabilities, |
144
|
|
|
'show_in_rest' => true, |
145
|
|
|
); |
146
|
|
|
register_post_type( self::$post_type_product, $product_args ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
Jetpack_Simple_Payments::getInstance(); |
151
|
|
|
|
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.