| Total Complexity | 42 |
| Total Lines | 210 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Frontend 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.
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 Frontend, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Frontend { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Holds class instance |
||
| 13 | * |
||
| 14 | * @since 1.0.0 |
||
| 15 | * |
||
| 16 | * @var object \lsx_health_plan\classes\Frontend() |
||
| 17 | */ |
||
| 18 | protected static $instance = null; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var object \lsx_health_plan\classes\Endpoints(); |
||
| 22 | */ |
||
| 23 | public $endpoints; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var object \lsx_health_plan\classes\Modals(); |
||
| 27 | */ |
||
| 28 | public $modals; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Contructor |
||
| 32 | */ |
||
| 33 | public function __construct() { |
||
| 34 | add_action( 'wp_enqueue_scripts', array( $this, 'assets' ), 5 ); |
||
| 35 | |||
| 36 | require_once LSX_HEALTH_PLAN_PATH . 'classes/frontend/class-endpoints.php'; |
||
| 37 | $this->endpoints = Endpoints::get_instance(); |
||
| 38 | |||
| 39 | require_once LSX_HEALTH_PLAN_PATH . 'classes/frontend/class-modals.php'; |
||
| 40 | $this->modals = Modals::get_instance(); |
||
| 41 | |||
| 42 | require_once LSX_HEALTH_PLAN_PATH . 'classes/frontend/class-gallery.php'; |
||
| 43 | $this->gallery = frontend\Gallery::get_instance(); |
||
|
|
|||
| 44 | |||
| 45 | if ( is_admin() ) { |
||
| 46 | add_filter( 'lsx_customizer_colour_selectors_body', array( $this, 'customizer_body_colours_handler' ), 15, 2 ); |
||
| 47 | } |
||
| 48 | |||
| 49 | // Handle the template redirects. |
||
| 50 | add_filter( 'template_include', array( $this, 'archive_template_include' ), 99 ); |
||
| 51 | add_filter( 'template_include', array( $this, 'single_template_include' ), 99 ); |
||
| 52 | add_filter( 'template_include', array( $this, 'taxonomy_template_include' ), 99 ); |
||
| 53 | add_action( 'template_redirect', array( $this, 'redirect' ) ); |
||
| 54 | |||
| 55 | add_action( 'init', array( $this, 'handle_day_action' ), 100 ); |
||
| 56 | add_filter( 'wp_kses_allowed_html', array( $this, 'wpkses_post_tags' ), 100, 2 ); |
||
| 57 | |||
| 58 | add_filter( 'get_the_archive_title', array( $this, 'get_the_archive_title' ), 99, 1 ); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Return an instance of this class. |
||
| 63 | * |
||
| 64 | * @since 1.0.0 |
||
| 65 | * |
||
| 66 | * @return object \lsx_health_plan\classes\Frontend() A single instance of this class. |
||
| 67 | */ |
||
| 68 | public static function get_instance() { |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Registers the plugin frontend assets |
||
| 78 | * |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | public function assets() { |
||
| 82 | wp_enqueue_style( 'lsx-health-plan', LSX_HEALTH_PLAN_URL . 'assets/css/lsx-health-plan.css', array(), LSX_HEALTH_PLAN_VER ); |
||
| 83 | wp_style_add_data( 'lsx-health-plan', 'rtl', 'replace' ); |
||
| 84 | wp_enqueue_script( 'lsx-health-plan-scripts', LSX_HEALTH_PLAN_URL . 'assets/js/src/lsx-health-plan-admin.js', array( 'jquery' ) ); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Handle body colours that might be change by LSX Customizer. |
||
| 89 | */ |
||
| 90 | public function customizer_body_colours_handler( $css, $colors ) { |
||
| 91 | $css .= ' |
||
| 92 | @import "' . LSX_HEALTH_PLAN_PATH . '/assets/css/scss/partials/customizer-health-plan-body-colours"; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * LSX Customizer - Body (LSX Health Plan) |
||
| 96 | */ |
||
| 97 | @include customizer-health-plan-body-colours ( |
||
| 98 | $bg: ' . $colors['background_color'] . ', |
||
| 99 | $breaker: ' . $colors['body_line_color'] . ', |
||
| 100 | $color: ' . $colors['body_text_color'] . ', |
||
| 101 | $link: ' . $colors['body_link_color'] . ', |
||
| 102 | $hover: ' . $colors['body_link_hover_color'] . ', |
||
| 103 | $small: ' . $colors['body_text_small_color'] . ' |
||
| 104 | ); |
||
| 105 | '; |
||
| 106 | |||
| 107 | return $css; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Archive template. |
||
| 112 | */ |
||
| 113 | public function archive_template_include( $template ) { |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Single template. |
||
| 126 | */ |
||
| 127 | public function single_template_include( $template ) { |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Redirect WordPress to the taxonomy located in the plugin |
||
| 140 | * |
||
| 141 | * @param $template string |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | public function taxonomy_template_include( $template ) { |
||
| 145 | $applicable_taxonomies = apply_filters( 'lsx_health_plan_taxonomies_template', array() ); |
||
| 146 | if ( is_main_query() && is_tax( $applicable_taxonomies ) ) { |
||
| 147 | $current_taxonomy = get_query_var( 'taxonomy' ); |
||
| 148 | if ( '' === locate_template( array( 'taxonomy-' . $current_taxonomy . '.php' ) ) && file_exists( LSX_HEALTH_PLAN_PATH . 'templates/taxonomy-' . $current_taxonomy . '.php' ) ) { |
||
| 149 | $template = LSX_HEALTH_PLAN_PATH . 'templates/taxonomy-' . $current_taxonomy . '.php'; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | return $template; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Redirect the user from the cart or checkout page if they have purchased the product already. |
||
| 157 | * |
||
| 158 | * @return void |
||
| 159 | */ |
||
| 160 | public function redirect() { |
||
| 161 | if ( ! is_user_logged_in() || ! function_exists( 'wc_get_page_id' ) || is_home() ) { |
||
| 162 | return; |
||
| 163 | } |
||
| 164 | if ( lsx_health_plan_user_has_purchase() && ( is_page( wc_get_page_id( 'cart' ) ) || is_page( wc_get_page_id( 'checkout' ) ) ) ) { |
||
| 165 | wp_redirect( get_permalink( wc_get_page_id( 'myaccount' ) ) ); |
||
| 166 | die; |
||
| 167 | } |
||
| 168 | |||
| 169 | $product_id = \lsx_health_plan\functions\get_option( 'membership_product', false ); |
||
| 170 | if ( false !== $product_id && is_single( $product_id ) ) { |
||
| 171 | wp_redirect( home_url() ); |
||
| 172 | die; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Registers the rewrites. |
||
| 178 | */ |
||
| 179 | public function handle_day_action() { |
||
| 180 | if ( isset( $_POST['lsx-health-plan-actions'] ) && wp_verify_nonce( $_POST['lsx-health-plan-actions'], 'complete' ) ) { |
||
| 181 | update_user_meta( get_current_user_id(), 'day_' . sanitize_key( $_POST['lsx-health-plan-id'] ) . '_complete', true ); |
||
| 182 | wp_safe_redirect( get_permalink( wc_get_page_id( 'myaccount' ) ) ); |
||
| 183 | } |
||
| 184 | |||
| 185 | if ( isset( $_POST['lsx-health-plan-actions'] ) && wp_verify_nonce( $_POST['lsx-health-plan-actions'], 'unlock' ) ) { |
||
| 186 | delete_user_meta( get_current_user_id(), 'day_' . sanitize_key( $_POST['lsx-health-plan-id'] ) . '_complete' ); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Registers the rewrites. |
||
| 192 | */ |
||
| 193 | public function wpkses_post_tags( $tags, $context ) { |
||
| 204 | } |
||
| 205 | /** |
||
| 206 | * Remove the "Archives:" from the post type recipes. |
||
| 207 | * |
||
| 208 | * @param string $title the term title. |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function get_the_archive_title( $title ) { |
||
| 219 | } |
||
| 220 | } |
||
| 221 |