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 string Plan slug |
||
| 50 | */ |
||
| 51 | private static $required_plan; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Track recurring payments block registration. |
||
| 55 | * |
||
| 56 | * @var boolean True if block registration has been executed. |
||
| 57 | */ |
||
| 58 | private static $has_registered_block = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Classic singleton pattern |
||
| 62 | * |
||
| 63 | * @var Jetpack_Memberships |
||
| 64 | */ |
||
| 65 | private static $instance; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Jetpack_Memberships constructor. |
||
| 69 | */ |
||
| 70 | private function __construct() {} |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The actual constructor initializing the object. |
||
| 74 | * |
||
| 75 | * @return Jetpack_Memberships |
||
| 76 | */ |
||
| 77 | View Code Duplication | public static function get_instance() { |
|
| 87 | /** |
||
| 88 | * Get the map that defines the shape of CPT post. keys are names of fields and |
||
| 89 | * 'meta' is the name of actual WP post meta field that corresponds. |
||
| 90 | * |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | private static function get_plan_property_mapping() { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Inits further hooks on init hook. |
||
| 108 | */ |
||
| 109 | private function register_init_hook() { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Actual hooks initializing on init. |
||
| 116 | */ |
||
| 117 | public function init_hook_action() { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Sets up the custom post types for the module. |
||
| 125 | */ |
||
| 126 | private function setup_cpts() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Allows custom post types to be used by REST API. |
||
| 162 | * |
||
| 163 | * @param array $post_types - other post types. |
||
| 164 | * |
||
| 165 | * @see hook 'rest_api_allowed_post_types' |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | public function allow_rest_api_types( $post_types ) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Allows custom meta fields to sync. |
||
| 176 | * |
||
| 177 | * @param array $post_meta - previously changet post meta. |
||
| 178 | * |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | public function allow_sync_post_meta( $post_meta ) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * This returns meta attribute of passet array. |
||
| 191 | * Used for array functions. |
||
| 192 | * |
||
| 193 | * @param array $map - stuff. |
||
| 194 | * |
||
| 195 | * @return mixed |
||
| 196 | */ |
||
| 197 | public function return_meta( $map ) { |
||
| 200 | /** |
||
| 201 | * Callback that parses the membership purchase shortcode. |
||
| 202 | * |
||
| 203 | * @param array $attrs - attributes in the shortcode. `id` here is the CPT id of the plan. |
||
| 204 | * |
||
| 205 | * @return string|void |
||
| 206 | */ |
||
| 207 | public function render_button( $attrs ) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get current blog id. |
||
| 279 | * |
||
| 280 | * @return int |
||
| 281 | */ |
||
| 282 | public static function get_blog_id() { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Get the id of the connected payment acount (Stripe etc). |
||
| 292 | * |
||
| 293 | * @return int|void |
||
| 294 | */ |
||
| 295 | public static function get_connected_account_id() { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Whether Recurring Payments are enabled. |
||
| 301 | * |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | public static function is_enabled_jetpack_recurring_payments() { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Register the Recurring Payments Gutenberg block |
||
| 321 | */ |
||
| 322 | public function register_gutenberg_block() { |
||
| 350 | } |
||
| 351 | Jetpack_Memberships::get_instance(); |
||
| 352 |