Complex classes like Jetpack_Memberships often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Memberships, and based on these observations, apply Extract Interface, too.
| 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 | * The prefix for transients storing cached subscriber statuses. |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private static $subscriber_transient_prefix = 'jetpack-payments-subscriber-'; |
||
| 44 | /** |
||
| 45 | * Cookie name for subscriber session token. |
||
| 46 | * The tokens are identifying WPCOM user_id on WPCOM side. |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private static $subscriber_cookie_name = 'jetpack-payments-subscriber-token'; |
||
| 51 | /** |
||
| 52 | * Subscriber session token. This value should come from cookie or a redirect. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $subscriber_token_value = ''; |
||
| 57 | /** |
||
| 58 | * Cache for the subscriber data for the current session. |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | private $subscriber_data = array(); |
||
| 63 | /** |
||
| 64 | * Array of post IDs where we don't want to render blocks anymore. |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | private $stop_render_for_posts = array(); |
||
| 69 | /** |
||
| 70 | * These are defaults for wp_kses ran on the membership button. |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | private static $tags_allowed_in_the_button = array( 'br' => array() ); |
||
| 75 | /** |
||
| 76 | * Classic singleton pattern |
||
| 77 | * |
||
| 78 | * @var Jetpack_Memberships |
||
| 79 | */ |
||
| 80 | private static $instance; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Jetpack_Memberships constructor. |
||
| 84 | */ |
||
| 85 | private function __construct() {} |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The actual constructor initializing the object. |
||
| 89 | * |
||
| 90 | * @return Jetpack_Memberships |
||
| 91 | */ |
||
| 92 | public static function get_instance() { |
||
| 100 | /** |
||
| 101 | * Get the map that defines the shape of CPT post. keys are names of fields and |
||
| 102 | * 'meta' is the name of actual WP post meta field that corresponds. |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | private static function get_plan_property_mapping() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Inits further hooks on init hook. |
||
| 121 | */ |
||
| 122 | private function register_init_hook() { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * This reads the user cookie or a redirect value and sets the user session token. |
||
| 128 | * User session tokens correspond to a WPCOM user id. |
||
| 129 | */ |
||
| 130 | private function setup_session_token() { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get current subscriber data. Tries to get from cache whenever possible, only in last resort does a WPCOM call to get data. |
||
| 143 | * Cache expires every hour per user. |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | public function get_subscriber_data() { |
||
| 207 | /** |
||
| 208 | * Actual hooks initializing on init. |
||
| 209 | */ |
||
| 210 | public function init_hook_action() { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Sets up the custom post types for the module. |
||
| 220 | */ |
||
| 221 | private function setup_cpts() { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Allows custom post types to be used by REST API. |
||
| 257 | * |
||
| 258 | * @param array $post_types - other post types. |
||
| 259 | * |
||
| 260 | * @see hook 'rest_api_allowed_post_types' |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | public function allow_rest_api_types( $post_types ) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Allows custom meta fields to sync. |
||
| 271 | * |
||
| 272 | * @param array $post_meta - previously changet post meta. |
||
| 273 | * |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | public function allow_sync_post_meta( $post_meta ) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * This returns meta attribute of passet array. |
||
| 286 | * Used for array functions. |
||
| 287 | * |
||
| 288 | * @param array $map - stuff. |
||
| 289 | * |
||
| 290 | * @return mixed |
||
| 291 | */ |
||
| 292 | public function return_meta( $map ) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Hooks into the 'render_block' filter in order to block content access. |
||
| 298 | */ |
||
| 299 | private function setup_paywall() { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * This is hooked into `render_block` filter. |
||
| 305 | * The purpose is to not render any blocks following the paywall block. |
||
| 306 | * This is achieved by storing the list of post_IDs where rendering of the blocks has been turned off. |
||
| 307 | * If we are trying to render a block in one of these posts, we return empty string. |
||
| 308 | * |
||
| 309 | * @param string $block_content - rendered block. |
||
| 310 | * @param array $block - block metadata. |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function do_paywall_for_block( $block_content, $block ) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Marks the rest of the current post as Paywalled. This will stop rendering any further blocks on this post. |
||
| 333 | * |
||
| 334 | * @see $this::do_paywall_for_block. |
||
| 335 | */ |
||
| 336 | private function paywall_the_post() { |
||
| 340 | /** |
||
| 341 | * Callback that parses the membership purchase shortcode. |
||
| 342 | * |
||
| 343 | * @param array $attrs - attributes in the shortcode. `id` here is the CPT id of the plan. |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function render_button( $attrs ) { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Get login URL for WPCOM login flow. |
||
| 397 | * |
||
| 398 | * @param array $data - Plan data. |
||
| 399 | * |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | private function get_login_link( $data ) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Get the HTML for the purchase button. |
||
| 409 | * |
||
| 410 | * @param array $attrs - block attributes. |
||
| 411 | * @param array $data - data for the payment plan. |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | private function get_purchase_button( $attrs, $data ) { |
||
| 463 | |||
| 464 | |||
| 465 | /** |
||
| 466 | * Get current blog id. |
||
| 467 | * |
||
| 468 | * @return int |
||
| 469 | */ |
||
| 470 | public static function get_blog_id() { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Get the id of the connected payment acount (Stripe etc). |
||
| 480 | * |
||
| 481 | * @return int|void |
||
| 482 | */ |
||
| 483 | public static function get_connected_account_id() { |
||
| 486 | } |
||
| 487 | Jetpack_Memberships::get_instance(); |
||
| 488 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.