Total Complexity | 60 |
Total Lines | 296 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like LSX_Team 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 LSX_Team, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class LSX_Team { |
||
12 | |||
13 | public $options; |
||
14 | |||
15 | public function __construct() { |
||
16 | $this->options = team_get_options(); |
||
17 | |||
18 | add_action( 'init', array( $this, 'custom_image_sizes' ) ); |
||
19 | add_filter( 'lsx_banner_allowed_post_types', array( $this, 'lsx_banner_allowed_post_types' ) ); |
||
20 | |||
21 | } |
||
22 | |||
23 | /** |
||
24 | * Enable project custom post type on LSX Banners. |
||
25 | */ |
||
26 | public function custom_image_sizes( $post_types ) { |
||
27 | add_image_size( 'lsx-team-archive', 170, 170, true ); |
||
28 | add_image_size( 'lsx-team-single', 320, 320, true ); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Enable project custom post type on LSX Banners. |
||
33 | */ |
||
34 | public function lsx_banner_allowed_post_types( $post_types ) { |
||
35 | $post_types[] = 'team'; |
||
36 | return $post_types; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Return the team thumbnail. |
||
41 | */ |
||
42 | public function get_thumbnail( $post_id, $size ) { |
||
43 | add_filter( 'lsx_placeholder_url', array( $this, 'placeholder' ), 10, 1 ); |
||
44 | add_filter( 'lsx_to_placeholder_url', array( $this, 'placeholder' ), 10, 1 ); |
||
45 | |||
46 | if ( is_numeric( $size ) ) { |
||
47 | $thumb_size = array( $size, $size ); |
||
48 | } else { |
||
49 | $thumb_size = $size; |
||
50 | } |
||
51 | |||
52 | $thumbnail_class = 'img-responsive'; |
||
53 | |||
54 | if ( ! empty( get_the_post_thumbnail( $post_id ) ) || ! empty( get_post_meta( $post_id, 'lsx_email_gravatar', true ) ) ) { |
||
55 | if ( ! empty( get_the_post_thumbnail( $post_id ) ) ) { |
||
56 | $thumbnail = get_the_post_thumbnail( $post_id, $thumb_size, array( |
||
57 | 'class' => $thumbnail_class, |
||
58 | ) ); |
||
59 | } else { |
||
60 | $thumbnail = get_avatar( get_post_meta( $post_id, 'lsx_email_gravatar', true ), $size, $this->options['display']['team_placeholder'], false, array( |
||
61 | 'class' => $thumbnail_class, |
||
62 | ) ); |
||
63 | } |
||
64 | } |
||
65 | if ( empty( $thumbnail ) ) { |
||
66 | if ( $this->options['display'] && ! empty( $this->options['display']['team_placeholder'] ) ) { |
||
67 | $thumbnail = '<img loading="lazy" class="img-responsive wp-post-image" src="' . $this->options['display']['team_placeholder'] . '" width="' . $size . '" />'; |
||
68 | } else { |
||
69 | $thumbnail = '<img loading="lazy" class="img-responsive wp-post-image" src="https://www.gravatar.com/avatar/none?d=mm&s=' . $size . '" width="' . $size . '" />'; |
||
70 | } |
||
71 | } |
||
72 | |||
73 | remove_filter( 'lsx_placeholder_url', array( $this, 'placeholder' ), 10, 1 ); |
||
74 | remove_filter( 'lsx_to_placeholder_url', array( $this, 'placeholder' ), 10, 1 ); |
||
75 | |||
76 | return $thumbnail; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Replaces the widget with Mystery Man |
||
81 | */ |
||
82 | public function placeholder( $image ) { |
||
83 | $image = array( |
||
84 | LSX_TEAM_URL . 'assets/img/mystery-man-square.png', |
||
85 | 512, |
||
86 | 512, |
||
87 | true, |
||
88 | ); |
||
89 | |||
90 | return $image; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Returns the shortcode output markup |
||
95 | */ |
||
96 | public function output( $atts ) { |
||
307 | } |
||
308 | } |
||
309 | |||
310 | } |
||
311 | |||
312 | global $lsx_team; |
||
314 |