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 | * 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 ) { |
||
| 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' ) ); |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get current blog id. |
||
| 261 | * |
||
| 262 | * @return int |
||
| 263 | */ |
||
| 264 | public static function get_blog_id() { |
||
| 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() { |
||
| 280 | } |
||
| 281 | Jetpack_Memberships::get_instance(); |
||
| 282 |