|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Jetpack_Memberships: wrapper for memberships functions. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Jetpack |
|
6
|
|
|
* @since 7.3.0 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Jetpack_Memberships |
|
11
|
|
|
* This class represents the Memberships functionality. |
|
12
|
|
|
*/ |
|
13
|
|
|
class Jetpack_Memberships { |
|
14
|
|
|
/** |
|
15
|
|
|
* CSS class prefix to use in the styling. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
public static $css_classname_prefix = 'jetpack-memberships'; |
|
20
|
|
|
/** |
|
21
|
|
|
* Our CPT type for the product (plan). |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
public static $post_type_plan = 'jp_mem_plan'; |
|
26
|
|
|
/** |
|
27
|
|
|
* Option that will store currently set up account (Stripe etc) id for memberships. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
public static $connected_account_id_option_name = 'jetpack-memberships-connected-account-id'; |
|
32
|
|
|
/** |
|
33
|
|
|
* Button block type to use. |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private static $button_block_name = 'membership-button'; |
|
38
|
|
|
/** |
|
39
|
|
|
* Classic singleton pattern |
|
40
|
|
|
* |
|
41
|
|
|
* @var Jetpack_Memberships |
|
42
|
|
|
*/ |
|
43
|
|
|
private static $instance; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Jetpack_Memberships constructor. |
|
47
|
|
|
*/ |
|
48
|
|
|
private function __construct() {} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The actual constructor initializing the object. |
|
52
|
|
|
* |
|
53
|
|
|
* @return Jetpack_Memberships |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function get_instance() { |
|
56
|
|
|
if ( ! self::$instance ) { |
|
57
|
|
|
self::$instance = new self(); |
|
58
|
|
|
self::$instance->register_init_hook(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return self::$instance; |
|
62
|
|
|
} |
|
63
|
|
|
/** |
|
64
|
|
|
* Get the map that defines the shape of CPT post. keys are names of fields and |
|
65
|
|
|
* 'meta' is the name of actual WP post meta field that corresponds. |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
private static function get_plan_property_mapping() { |
|
70
|
|
|
$meta_prefix = 'jetpack_memberships_'; |
|
71
|
|
|
$properties = array( |
|
72
|
|
|
'price' => array( |
|
73
|
|
|
'meta' => $meta_prefix . 'price', |
|
74
|
|
|
), |
|
75
|
|
|
'currency' => array( |
|
76
|
|
|
'meta' => $meta_prefix . 'currency', |
|
77
|
|
|
), |
|
78
|
|
|
); |
|
79
|
|
|
return $properties; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Inits further hooks on init hook. |
|
84
|
|
|
*/ |
|
85
|
|
|
private function register_init_hook() { |
|
86
|
|
|
add_action( 'init', array( $this, 'init_hook_action' ) ); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Actual hooks initializing on init. |
|
91
|
|
|
*/ |
|
92
|
|
|
public function init_hook_action() { |
|
93
|
|
|
add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) ); |
|
94
|
|
|
add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'allow_sync_post_meta' ) ); |
|
95
|
|
|
$this->setup_cpts(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Sets up the custom post types for the module. |
|
100
|
|
|
*/ |
|
101
|
|
|
private function setup_cpts() { |
|
102
|
|
|
/* |
|
103
|
|
|
* PLAN data structure. |
|
104
|
|
|
*/ |
|
105
|
|
|
$capabilities = array( |
|
106
|
|
|
'edit_post' => 'edit_posts', |
|
107
|
|
|
'read_post' => 'read_private_posts', |
|
108
|
|
|
'delete_post' => 'delete_posts', |
|
109
|
|
|
'edit_posts' => 'edit_posts', |
|
110
|
|
|
'edit_others_posts' => 'edit_others_posts', |
|
111
|
|
|
'publish_posts' => 'publish_posts', |
|
112
|
|
|
'read_private_posts' => 'read_private_posts', |
|
113
|
|
|
); |
|
114
|
|
|
$order_args = array( |
|
115
|
|
|
'label' => esc_html__( 'Plan', 'jetpack' ), |
|
116
|
|
|
'description' => esc_html__( 'Memberships plans', 'jetpack' ), |
|
117
|
|
|
'supports' => array( 'title', 'custom-fields', 'content' ), |
|
118
|
|
|
'hierarchical' => false, |
|
119
|
|
|
'public' => false, |
|
120
|
|
|
'show_ui' => false, |
|
121
|
|
|
'show_in_menu' => false, |
|
122
|
|
|
'show_in_admin_bar' => false, |
|
123
|
|
|
'show_in_nav_menus' => false, |
|
124
|
|
|
'can_export' => true, |
|
125
|
|
|
'has_archive' => false, |
|
126
|
|
|
'exclude_from_search' => true, |
|
127
|
|
|
'publicly_queryable' => false, |
|
128
|
|
|
'rewrite' => false, |
|
129
|
|
|
'capabilities' => $capabilities, |
|
130
|
|
|
'show_in_rest' => false, |
|
131
|
|
|
); |
|
132
|
|
|
register_post_type( self::$post_type_plan, $order_args ); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Allows custom post types to be used by REST API. |
|
137
|
|
|
* |
|
138
|
|
|
* @param array $post_types - other post types. |
|
139
|
|
|
* |
|
140
|
|
|
* @see hook 'rest_api_allowed_post_types' |
|
141
|
|
|
* @return array |
|
142
|
|
|
*/ |
|
143
|
|
|
public function allow_rest_api_types( $post_types ) { |
|
144
|
|
|
$post_types[] = self::$post_type_plan; |
|
145
|
|
|
|
|
146
|
|
|
return $post_types; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Allows custom meta fields to sync. |
|
151
|
|
|
* |
|
152
|
|
|
* @param array $post_meta - previously changet post meta. |
|
153
|
|
|
* |
|
154
|
|
|
* @return array |
|
155
|
|
|
*/ |
|
156
|
|
|
public function allow_sync_post_meta( $post_meta ) { |
|
157
|
|
|
$meta_keys = array_map( |
|
158
|
|
|
array( $this, 'return_meta' ), |
|
159
|
|
|
$this->get_plan_property_mapping() |
|
160
|
|
|
); |
|
161
|
|
|
return array_merge( $post_meta, array_values( $meta_keys ) ); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* This returns meta attribute of passet array. |
|
166
|
|
|
* Used for array functions. |
|
167
|
|
|
* |
|
168
|
|
|
* @param array $map - stuff. |
|
169
|
|
|
* |
|
170
|
|
|
* @return mixed |
|
171
|
|
|
*/ |
|
172
|
|
|
public function return_meta( $map ) { |
|
173
|
|
|
return $map['meta']; |
|
174
|
|
|
} |
|
175
|
|
|
/** |
|
176
|
|
|
* Callback that parses the membership purchase shortcode. |
|
177
|
|
|
* |
|
178
|
|
|
* @param array $attrs - attributes in the shortcode. `id` here is the CPT id of the plan. |
|
179
|
|
|
* |
|
180
|
|
|
* @return string|void |
|
181
|
|
|
*/ |
|
182
|
|
|
public function render_button( $attrs ) { |
|
183
|
|
|
Jetpack_Gutenberg::load_assets_as_required( self::$button_block_name, array( 'thickbox', 'wp-polyfill' ) ); |
|
184
|
|
|
|
|
185
|
|
|
if ( empty( $attrs['planId'] ) ) { |
|
186
|
|
|
return; |
|
187
|
|
|
} |
|
188
|
|
|
$id = intval( $attrs['planId'] ); |
|
189
|
|
|
$product = get_post( $id ); |
|
190
|
|
|
if ( ! $product || is_wp_error( $product ) ) { |
|
191
|
|
|
return; |
|
192
|
|
|
} |
|
193
|
|
|
if ( $product->post_type !== self::$post_type_plan || 'publish' !== $product->post_status ) { |
|
194
|
|
|
return; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$data = array( |
|
198
|
|
|
'blog_id' => self::get_blog_id(), |
|
199
|
|
|
'id' => $id, |
|
200
|
|
|
'button_label' => __( 'Your contribution', 'jetpack' ), |
|
201
|
|
|
'powered_text' => __( 'Powered by WordPress.com', 'jetpack' ), |
|
202
|
|
|
); |
|
203
|
|
|
|
|
204
|
|
|
$classes = array( |
|
205
|
|
|
'components-button', |
|
206
|
|
|
'is-primary', |
|
207
|
|
|
'is-button', |
|
208
|
|
|
'wp-block-jetpack-' . self::$button_block_name, |
|
209
|
|
|
self::$css_classname_prefix . '-' . $data['id'], |
|
210
|
|
|
); |
|
211
|
|
|
if ( isset( $attrs['className'] ) ) { |
|
212
|
|
|
array_push( $classes, $attrs['className'] ); |
|
213
|
|
|
} |
|
214
|
|
|
if ( isset( $attrs['submitButtonText'] ) ) { |
|
215
|
|
|
$data['button_label'] = $attrs['submitButtonText']; |
|
216
|
|
|
} |
|
217
|
|
|
$button_styles = array(); |
|
218
|
|
|
if ( ! empty( $attrs['customBackgroundButtonColor'] ) ) { |
|
219
|
|
|
array_push( |
|
220
|
|
|
$button_styles, |
|
221
|
|
|
sprintf( |
|
222
|
|
|
'background-color: %s', |
|
223
|
|
|
sanitize_hex_color( $attrs['customBackgroundButtonColor'] ) |
|
224
|
|
|
) |
|
225
|
|
|
); |
|
226
|
|
|
} |
|
227
|
|
|
if ( ! empty( $attrs['customTextButtonColor'] ) ) { |
|
228
|
|
|
array_push( |
|
229
|
|
|
$button_styles, |
|
230
|
|
|
sprintf( |
|
231
|
|
|
'color: %s', |
|
232
|
|
|
sanitize_hex_color( $attrs['customTextButtonColor'] ) |
|
233
|
|
|
) |
|
234
|
|
|
); |
|
235
|
|
|
} |
|
236
|
|
|
$button_styles = implode( $button_styles, ';' ); |
|
237
|
|
|
add_thickbox(); |
|
238
|
|
|
return sprintf( |
|
239
|
|
|
'<button data-blog-id="%d" data-powered-text="%s" data-plan-id="%d" data-lang="%s" class="%s" style="%s">%s</button>', |
|
240
|
|
|
esc_attr( $data['blog_id'] ), |
|
241
|
|
|
esc_attr( $data['powered_text'] ), |
|
242
|
|
|
esc_attr( $data['id'] ), |
|
243
|
|
|
esc_attr( get_locale() ), |
|
244
|
|
|
esc_attr( implode( $classes, ' ' ) ), |
|
245
|
|
|
esc_attr( $button_styles ), |
|
246
|
|
|
esc_html( $data['button_label'] ) |
|
247
|
|
|
); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Get current blog id. |
|
252
|
|
|
* |
|
253
|
|
|
* @return int |
|
254
|
|
|
*/ |
|
255
|
|
|
public static function get_blog_id() { |
|
256
|
|
|
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
257
|
|
|
return get_current_blog_id(); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
return Jetpack_Options::get_option( 'id' ); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Get the id of the connected payment acount (Stripe etc). |
|
265
|
|
|
* |
|
266
|
|
|
* @return int|void |
|
267
|
|
|
*/ |
|
268
|
|
|
public static function get_connected_account_id() { |
|
269
|
|
|
return get_option( self::$connected_account_id_option_name ); |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
Jetpack_Memberships::get_instance(); |
|
273
|
|
|
|