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 |
||
| 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'; |
||
|
|
|||
| 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() { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Sets up the custom post types for the module. |
||
| 118 | */ |
||
| 119 | private function setup_cpts() { |
||
| 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 ) { |
||
| 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 ) { |
||
| 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 ) { |
||
| 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() { |
||
| 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() { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Whether Recurring Payments are enabled. |
||
| 294 | * |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | public static function is_enabled_jetpack_recurring_payments() { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Register the Recurring Payments Gutenberg block |
||
| 310 | */ |
||
| 311 | public function register_gutenberg_block() { |
||
| 330 | } |
||
| 331 | Jetpack_Memberships::get_instance(); |
||
| 332 |
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..