| Total Complexity | 45 |
| Total Lines | 221 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Output 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 Output, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Output { |
||
| 10 | |||
| 11 | |||
| 12 | /** |
||
| 13 | * Holds class instance |
||
| 14 | * |
||
| 15 | * @since 1.0.0 |
||
| 16 | * |
||
| 17 | * @var object \lsx\sharing\classes\frontend\Output() |
||
| 18 | */ |
||
| 19 | protected static $instance = null; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Contructor |
||
| 23 | */ |
||
| 24 | public function __construct() { |
||
| 25 | add_action('wp_enqueue_scripts', array( $this, 'assets' ), 5); |
||
| 26 | add_filter('wp_kses_allowed_html', array( $this, 'wp_kses_allowed_html' ), 10, 2); |
||
| 27 | add_shortcode('lsx_sharing_buttons', array( $this, 'sharing_buttons_shortcode' )); |
||
| 28 | // Storefront (storefront_loop_post, storefront_single_post). |
||
| 29 | add_action('storefront_post_content_before', array( $this, 'sharing_buttons_template' ), 20); |
||
| 30 | // WooCommerce. |
||
| 31 | add_action('woocommerce_share', array( $this, 'sharing_buttons_template' )); |
||
| 32 | |||
| 33 | // General Post Types. |
||
| 34 | add_action('lsx_entry_after', array( $this, 'output_sharing' )); |
||
| 35 | |||
| 36 | // Tribe Events. |
||
| 37 | add_filter('tribe_events_ical_single_event_links', array( $this, 'output_event_sharing' ), 10, 1); |
||
| 38 | |||
| 39 | // Sensei Integration. |
||
| 40 | add_action('sensei_pagination', array( $this, 'output_sharing' ), 20); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Return an instance of this class. |
||
| 45 | * |
||
| 46 | * @since 1.0.0 |
||
| 47 | * |
||
| 48 | * @return object \lsx\sharing\classes\frontend\Output() A single instance of this class. |
||
| 49 | */ |
||
| 50 | public static function get_instance() { |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Enques the assets. |
||
| 60 | */ |
||
| 61 | public function assets() { |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Display/return sharing buttons. |
||
| 96 | */ |
||
| 97 | public function sharing_buttons( $buttons = array( 'facebook', 'twitter', 'pinterest' ), $echo = false, $post_id = false ) { |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Sharing buttons shortcode. |
||
| 160 | */ |
||
| 161 | public function sharing_buttons_shortcode( $atts ) { |
||
| 162 | $atts = shortcode_atts( |
||
| 163 | array( |
||
| 164 | 'buttons' => '', |
||
| 165 | ), $atts, 'lsx_sharing_buttons' |
||
| 166 | ); |
||
| 167 | |||
| 168 | if ( empty($atts['buttons']) ) { |
||
| 169 | return ''; |
||
| 170 | } |
||
| 171 | |||
| 172 | $no_whitespaces = preg_replace('/\s*,\s*/', ',', filter_var($atts['buttons'], FILTER_SANITIZE_STRING)); |
||
| 173 | $buttons = explode(',', $no_whitespaces); |
||
| 174 | |||
| 175 | if ( is_array($buttons) && count($buttons) > 0 ) { |
||
| 176 | return $this->sharing_buttons($buttons); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Display buttons (template hook). |
||
| 182 | */ |
||
| 183 | public function sharing_buttons_template() { |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Allow data params for Bootstrap modal. |
||
| 189 | */ |
||
| 190 | public function wp_kses_allowed_html( $allowedtags, $context ) { |
||
| 191 | $allowedtags['a']['data-toggle'] = true; |
||
| 192 | $allowedtags['a']['data-link'] = true; |
||
| 193 | return $allowedtags; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Outputs the sharing to the templates. |
||
| 198 | * |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | public function output_sharing() { |
||
| 202 | if ( is_main_query() && is_single() && ! is_singular(array( 'post', 'page', 'product' )) ) { |
||
| 203 | |||
| 204 | if ( \lsx\sharing\includes\functions\is_disabled() || \lsx\sharing\includes\functions\is_pt_disabled(get_post_type()) || in_array(get_post_type(), \lsx\sharing\includes\functions\get_restricted_post_types()) || in_array(get_post_type(), \lsx\sharing\includes\functions\get_to_post_types()) || in_array(get_post_type(), \lsx\sharing\includes\functions\get_hp_post_types()) ) { |
||
| 205 | return ''; |
||
| 206 | } |
||
| 207 | ?> |
||
| 208 | <footer class="lsx-sharing-wrapper footer-meta clearfix"> |
||
| 209 | <div class="post-tags-wrapper"> |
||
| 210 | <?php $this->sharing_buttons_template(); ?> |
||
| 211 | </div> |
||
| 212 | </footer><!-- .footer-meta --> |
||
| 213 | <?php |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Outputs the sharing below the events. |
||
| 219 | * |
||
| 220 | * @param string $ical_links |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public function output_event_sharing( $ical_links = '' ) { |
||
| 230 | } |
||
| 231 | } |
||
| 232 |