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
|
|
|
// Classic singleton pattern: |
9
|
|
|
private static $instance; |
10
|
|
|
private function __construct() {} |
11
|
|
|
static function getInstance() { |
12
|
|
|
if ( ! self::$instance ) { |
13
|
|
|
self::$instance = new self(); |
14
|
|
|
self::$instance->register_init_hook(); |
15
|
|
|
} |
16
|
|
|
return self::$instance; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
private function register_init_hook() { |
20
|
|
|
add_action( 'init', array( $this, 'init_hook_action' ) ); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function init_hook_action() { |
24
|
|
|
add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) ); |
25
|
|
|
$this->setup_cpts(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Allows custom post types to be used by REST API. |
30
|
|
|
* @param $post_types |
31
|
|
|
* @see hook 'rest_api_allowed_post_types' |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
function allow_rest_api_types( $post_types ) { |
35
|
|
|
$post_types[] = self::$post_type_order; |
36
|
|
|
$post_types[] = self::$post_type_product; |
37
|
|
|
return $post_types; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Sets up the custom post types for the module. |
42
|
|
|
*/ |
43
|
|
|
function setup_cpts() { |
44
|
|
|
|
45
|
|
|
/* |
46
|
|
|
* ORDER data structure. holds: |
47
|
|
|
* title = customer name |
48
|
|
|
* metadata: |
49
|
|
|
* spay_paypal_id |
50
|
|
|
* spay_status |
51
|
|
|
* ... (WIP) |
52
|
|
|
*/ |
53
|
|
|
$order_capabilities = array( |
54
|
|
|
'edit_post' => 'edit_posts', |
55
|
|
|
'read_post' => 'read_private_posts', |
56
|
|
|
'delete_post' => 'delete_posts', |
57
|
|
|
'edit_posts' => 'edit_posts', |
58
|
|
|
'edit_others_posts' => 'edit_others_posts', |
59
|
|
|
'publish_posts' => 'publish_posts', |
60
|
|
|
'read_private_posts' => 'read_private_posts', |
61
|
|
|
); |
62
|
|
|
$order_args = array( |
63
|
|
|
'label' => __( 'Order', 'jetpack' ), |
64
|
|
|
'description' => __( 'Simple Payments orders', 'jetpack' ), |
65
|
|
|
'supports' => array( 'custom-fields' ), |
66
|
|
|
'hierarchical' => false, |
67
|
|
|
'public' => false, |
68
|
|
|
'show_ui' => false, |
69
|
|
|
'show_in_menu' => false, |
70
|
|
|
'show_in_admin_bar' => false, |
71
|
|
|
'show_in_nav_menus' => false, |
72
|
|
|
'can_export' => true, |
73
|
|
|
'has_archive' => false, |
74
|
|
|
'exclude_from_search' => true, |
75
|
|
|
'publicly_queryable' => false, |
76
|
|
|
'rewrite' => false, |
77
|
|
|
'capabilities' => $order_capabilities, |
78
|
|
|
'show_in_rest' => true, |
79
|
|
|
); |
80
|
|
|
register_post_type( self::$post_type_order, $order_args ); |
81
|
|
|
|
82
|
|
|
/* |
83
|
|
|
* PRODUCT data structure. Holds: |
84
|
|
|
* title - title |
85
|
|
|
* content - description |
86
|
|
|
* thumbnail - image |
87
|
|
|
* metadata: |
88
|
|
|
* spay_price - price |
89
|
|
|
* spay_currency - currency code |
90
|
|
|
* spay_cta - text with "Buy" or other CTA |
91
|
|
|
* spay_email - paypal email |
92
|
|
|
* spay_multiple - allow for multiple items |
93
|
|
|
*/ |
94
|
|
|
$product_capabilities = array( |
95
|
|
|
'edit_post' => 'edit_posts', |
96
|
|
|
'read_post' => 'read_private_posts', |
97
|
|
|
'delete_post' => 'delete_posts', |
98
|
|
|
'edit_posts' => 'edit_posts', |
99
|
|
|
'edit_others_posts' => 'edit_others_posts', |
100
|
|
|
'publish_posts' => 'publish_posts', |
101
|
|
|
'read_private_posts' => 'read_private_posts', |
102
|
|
|
); |
103
|
|
|
$product_args = array( |
104
|
|
|
'label' => __( 'Product', 'jetpack' ), |
105
|
|
|
'description' => __( 'Simple Payments products', 'jetpack' ), |
106
|
|
|
'supports' => array( 'title', 'editor','thumbnail', 'custom-fields' ), |
107
|
|
|
'hierarchical' => false, |
108
|
|
|
'public' => false, |
109
|
|
|
'show_ui' => false, |
110
|
|
|
'show_in_menu' => false, |
111
|
|
|
'show_in_admin_bar' => false, |
112
|
|
|
'show_in_nav_menus' => false, |
113
|
|
|
'can_export' => true, |
114
|
|
|
'has_archive' => false, |
115
|
|
|
'exclude_from_search' => true, |
116
|
|
|
'publicly_queryable' => false, |
117
|
|
|
'rewrite' => false, |
118
|
|
|
'capabilities' => $product_capabilities, |
119
|
|
|
'show_in_rest' => true, |
120
|
|
|
); |
121
|
|
|
register_post_type( self::$post_type_product, $product_args ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
Jetpack_Simple_Payments::getInstance(); |
126
|
|
|
|
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.