Completed
Push — update/recurring-payments-use-... ( 75b0c5...161bad )
by Bernhard
07:10
created

Jetpack_Memberships::get_plan_property_mapping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
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 = 'recurring-payments';
38
39
	/**
40
	 * These are defaults for wp_kses ran on the membership button.
41
	 *
42
	 * @var array
43
	 */
44
	private static $tags_allowed_in_the_button = array( 'br' => array() );
45
46
	/**
47
	 * The minimum required plan for this Gutenberg block.
48
	 *
49
	 * @var Jetpack_Memberships
50
	 */
51
	private static $required_plan;
52
53
	/**
54
	 * Classic singleton pattern
55
	 *
56
	 * @var Jetpack_Memberships
57
	 */
58
	private static $instance;
59
60
	/**
61
	 * Jetpack_Memberships constructor.
62
	 */
63
	private function __construct() {}
64
65
	/**
66
	 * The actual constructor initializing the object.
67
	 *
68
	 * @return Jetpack_Memberships
69
	 */
70 View Code Duplication
	public static function get_instance() {
71
		if ( ! self::$instance ) {
72
			self::$instance = new self();
73
			self::$instance->register_init_hook();
74
			// Yes, `personal-bundle` with a dash, `jetpack_personal` with an underscore. Check the v1.5 endpoint to verify.
75
			self::$required_plan = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? 'personal-bundle' : 'jetpack_personal';
0 ignored issues
show
Documentation Bug introduced by
It seems like defined('IS_WPCOM') && I...e' : 'jetpack_personal' of type string is incompatible with the declared type object<Jetpack_Memberships> of property $required_plan.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
76
		}
77
78
		return self::$instance;
79
	}
80
	/**
81
	 * Get the map that defines the shape of CPT post. keys are names of fields and
82
	 * 'meta' is the name of actual WP post meta field that corresponds.
83
	 *
84
	 * @return array
85
	 */
86
	private static function get_plan_property_mapping() {
87
		$meta_prefix = 'jetpack_memberships_';
88
		$properties  = array(
89
			'price'    => array(
90
				'meta' => $meta_prefix . 'price',
91
			),
92
			'currency' => array(
93
				'meta' => $meta_prefix . 'currency',
94
			),
95
		);
96
		return $properties;
97
	}
98
99
	/**
100
	 * Inits further hooks on init hook.
101
	 */
102
	private function register_init_hook() {
103
		add_action( 'init', array( $this, 'init_hook_action' ) );
104
		add_action( 'jetpack_register_gutenberg_extensions', array( $this, 'register_gutenberg_block' ) );
105
	}
106
107
	/**
108
	 * Actual hooks initializing on init.
109
	 */
110
	public function init_hook_action() {
111
		add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) );
112
		add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'allow_sync_post_meta' ) );
113
		$this->setup_cpts();
114
	}
115
116
	/**
117
	 * Sets up the custom post types for the module.
118
	 */
119
	private function setup_cpts() {
120
		/*
121
		 * PLAN data structure.
122
		 */
123
		$capabilities = array(
124
			'edit_post'          => 'edit_posts',
125
			'read_post'          => 'read_private_posts',
126
			'delete_post'        => 'delete_posts',
127
			'edit_posts'         => 'edit_posts',
128
			'edit_others_posts'  => 'edit_others_posts',
129
			'publish_posts'      => 'publish_posts',
130
			'read_private_posts' => 'read_private_posts',
131
		);
132
		$order_args   = array(
133
			'label'               => esc_html__( 'Plan', 'jetpack' ),
134
			'description'         => esc_html__( 'Recurring Payments plans', 'jetpack' ),
135
			'supports'            => array( 'title', 'custom-fields', 'content' ),
136
			'hierarchical'        => false,
137
			'public'              => false,
138
			'show_ui'             => false,
139
			'show_in_menu'        => false,
140
			'show_in_admin_bar'   => false,
141
			'show_in_nav_menus'   => false,
142
			'can_export'          => true,
143
			'has_archive'         => false,
144
			'exclude_from_search' => true,
145
			'publicly_queryable'  => false,
146
			'rewrite'             => false,
147
			'capabilities'        => $capabilities,
148
			'show_in_rest'        => false,
149
		);
150
		register_post_type( self::$post_type_plan, $order_args );
151
	}
152
153
	/**
154
	 * Allows custom post types to be used by REST API.
155
	 *
156
	 * @param array $post_types - other post types.
157
	 *
158
	 * @see hook 'rest_api_allowed_post_types'
159
	 * @return array
160
	 */
161
	public function allow_rest_api_types( $post_types ) {
162
		$post_types[] = self::$post_type_plan;
163
164
		return $post_types;
165
	}
166
167
	/**
168
	 * Allows custom meta fields to sync.
169
	 *
170
	 * @param array $post_meta - previously changet post meta.
171
	 *
172
	 * @return array
173
	 */
174
	public function allow_sync_post_meta( $post_meta ) {
175
		$meta_keys = array_map(
176
			array( $this, 'return_meta' ),
177
			$this->get_plan_property_mapping()
178
		);
179
		return array_merge( $post_meta, array_values( $meta_keys ) );
180
	}
181
182
	/**
183
	 * This returns meta attribute of passet array.
184
	 * Used for array functions.
185
	 *
186
	 * @param array $map - stuff.
187
	 *
188
	 * @return mixed
189
	 */
190
	public function return_meta( $map ) {
191
		return $map['meta'];
192
	}
193
	/**
194
	 * Callback that parses the membership purchase shortcode.
195
	 *
196
	 * @param array $attrs - attributes in the shortcode. `id` here is the CPT id of the plan.
197
	 *
198
	 * @return string|void
199
	 */
200
	public function render_button( $attrs ) {
201
		Jetpack_Gutenberg::load_assets_as_required( self::$button_block_name, array( 'thickbox', 'wp-polyfill' ) );
202
203
		if ( empty( $attrs['planId'] ) ) {
204
			return;
205
		}
206
		$id      = intval( $attrs['planId'] );
207
		$product = get_post( $id );
208
		if ( ! $product || is_wp_error( $product ) ) {
209
			return;
210
		}
211
		if ( $product->post_type !== self::$post_type_plan || 'publish' !== $product->post_status ) {
212
			return;
213
		}
214
215
		$data = array(
216
			'blog_id'      => self::get_blog_id(),
217
			'id'           => $id,
218
			'button_label' => __( 'Your contribution', 'jetpack' ),
219
			'powered_text' => __( 'Powered by WordPress.com', 'jetpack' ),
220
		);
221
222
		$classes = Jetpack_Gutenberg::block_classes(
223
			self::$button_block_name,
224
			$attrs,
225
			array(
226
				'wp-block-button__link',
227
				'components-button',
228
				'is-primary',
229
				'is-button',
230
				self::$css_classname_prefix . '-' . $data['id'],
231
			)
232
		);
233
234
		if ( isset( $attrs['submitButtonText'] ) ) {
235
			$data['button_label'] = $attrs['submitButtonText'];
236
		}
237
		$button_styles = array();
238 View Code Duplication
		if ( ! empty( $attrs['customBackgroundButtonColor'] ) ) {
239
			array_push(
240
				$button_styles,
241
				sprintf(
242
					'background-color: %s',
243
					sanitize_hex_color( $attrs['customBackgroundButtonColor'] )
244
				)
245
			);
246
		}
247 View Code Duplication
		if ( ! empty( $attrs['customTextButtonColor'] ) ) {
248
			array_push(
249
				$button_styles,
250
				sprintf(
251
					'color: %s',
252
					sanitize_hex_color( $attrs['customTextButtonColor'] )
253
				)
254
			);
255
		}
256
		$button_styles = implode( $button_styles, ';' );
257
		add_thickbox();
258
		return sprintf(
259
			'<button data-blog-id="%d" data-powered-text="%s" data-plan-id="%d" data-lang="%s" class="%s" style="%s">%s</button>',
260
			esc_attr( $data['blog_id'] ),
261
			esc_attr( $data['powered_text'] ),
262
			esc_attr( $data['id'] ),
263
			esc_attr( get_locale() ),
264
			esc_attr( $classes ),
265
			esc_attr( $button_styles ),
266
			wp_kses( $data['button_label'], self::$tags_allowed_in_the_button )
267
		);
268
	}
269
270
	/**
271
	 * Get current blog id.
272
	 *
273
	 * @return int
274
	 */
275
	public static function get_blog_id() {
276
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
277
			return get_current_blog_id();
278
		}
279
280
		return Jetpack_Options::get_option( 'id' );
281
	}
282
283
	/**
284
	 * Get the id of the connected payment acount (Stripe etc).
285
	 *
286
	 * @return int|void
287
	 */
288
	public static function get_connected_account_id() {
289
		return get_option( self::$connected_account_id_option_name );
290
	}
291
292
	/**
293
	 * Whether Recurring Payments are enabled.
294
	 *
295
	 * @return bool
296
	 */
297
	public static function is_enabled_jetpack_recurring_payments() {
298
		// For WPCOM sites.
299 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'has_any_blog_stickers' ) ) {
300
			$site_id = get_current_blog_id();
301
			return has_any_blog_stickers( array( 'premium-plan', 'business-plan', 'ecommerce-plan' ), $site_id );
302
		}
303
304
		// For all Jetpack sites.
305
		return Jetpack::is_active() && Jetpack_Plan::supports( 'recurring-payments' );
306
	}
307
308
	/**
309
	 * Register the Recurring Payments Gutenberg block
310
	 */
311
	public function register_gutenberg_block() {
312
		if ( self::is_enabled_jetpack_recurring_payments() ) {
313
			jetpack_register_block(
314
				'jetpack/recurring-payments',
315
				array(
316
					'render_callback' => array( $this, 'render_button' ),
317
				)
318
			);
319
		} else {
320
			Jetpack_Gutenberg::set_extension_unavailable(
321
				'jetpack/recurring-payments',
322
				'missing_plan',
323
				array(
324
					'required_feature' => 'memberships',
325
					'required_plan'    => self::$required_plan,
326
				)
327
			);
328
		}
329
	}
330
}
331
Jetpack_Memberships::get_instance();
332