Completed
Push — add/new-disconnect-dialog ( b4649f...72298c )
by
unknown
23:43 queued 16:44
created

Jetpack_Memberships   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 268
Duplicated Lines 6.72 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 0
Metric Value
dl 18
loc 268
rs 10
c 0
b 0
f 0
wmc 23
lcom 3
cbo 2

12 Methods

Rating   Name   Duplication   Size   Complexity  
A return_meta() 0 3 1
A __construct() 0 1 1
A get_instance() 0 8 2
A get_plan_property_mapping() 0 12 1
A register_init_hook() 0 3 1
A init_hook_action() 0 5 1
A setup_cpts() 0 33 1
A allow_rest_api_types() 0 5 1
A allow_sync_post_meta() 0 7 1
B render_button() 18 69 9
A get_blog_id() 0 7 3
A get_connected_account_id() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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