Conditions | 19 |
Paths | 292 |
Total Lines | 81 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
63 | public function get_rendered_html( $atts ) { |
||
64 | |||
65 | $theme = $this->theme; |
||
|
|||
66 | if ( ! empty( $atts['theme'] ) ) { |
||
67 | $theme = $atts['theme']; |
||
68 | } |
||
69 | |||
70 | $theme = $this->is_theme_available( $theme ); |
||
71 | |||
72 | if ( ! empty( $atts['post_count'] ) ) { |
||
73 | $limit = intval( $atts['post_count'] ); |
||
74 | } else { |
||
75 | $limit = $this->count; |
||
76 | } |
||
77 | |||
78 | $posts = $this->get_posts_to_display(); |
||
79 | |||
80 | if ( empty( $posts ) ) { |
||
81 | return ''; |
||
82 | } |
||
83 | |||
84 | if ( 'curated' === $this->sort && apply_filters( 'monsterinsights_popular_posts_widget_curated_shuffle', true ) ) { |
||
85 | // Randomize the order. |
||
86 | shuffle( $posts ); |
||
87 | } |
||
88 | |||
89 | $theme_styles = $this->get_theme_props( $theme )->get_theme(); |
||
90 | |||
91 | $label_text = ''; |
||
92 | if ( isset( $theme_styles['styles']['label'] ) ) { |
||
93 | $label_text = isset( $atts['label_text'] ) ? esc_html($atts['label_text']) : esc_html($theme_styles['styles']['label']['text']); |
||
94 | } |
||
95 | |||
96 | if ( isset( $atts['widget_title'] ) ) { |
||
97 | $show_title = (bool) $atts['widget_title']; |
||
98 | $title_text = empty( $atts['widget_title_text'] ) ? '' : $atts['widget_title_text']; |
||
99 | } else { |
||
100 | $show_title = $this->title; |
||
101 | $title_text = $this->title_text; |
||
102 | } |
||
103 | |||
104 | $html = '<div class="' . esc_attr($this->get_wrapper_class( $atts )) . '">'; |
||
105 | if ( $show_title ) { |
||
106 | $html .= '<h2 class="monsterinsights-widget-popular-posts-widget-title">' . esc_html( $title_text ) . '</h2>'; |
||
107 | } |
||
108 | |||
109 | $html .= '<ul class="monsterinsights-widget-popular-posts-list">'; |
||
110 | |||
111 | $display_count = 0; |
||
112 | foreach ( $posts as $post ) { |
||
113 | $display_count ++; |
||
114 | if ( $display_count > $limit ) { |
||
115 | break; |
||
116 | } |
||
117 | $this->set_post_shown( $post['id'] ); |
||
118 | $html .= '<li '; |
||
119 | $html .= ! empty( $this->get_element_style( $theme, 'background', $atts ) ) ? 'style="' . esc_attr( $this->get_element_style( $theme, 'background', $atts ) ) . '"' : ''; |
||
120 | $html .= '>'; |
||
121 | $html .= '<a href="' . esc_url($post['link']) . '">'; |
||
122 | if ( ! empty( $theme_styles['image'] ) && ! empty( $post['image'] ) ) { |
||
123 | $html .= '<div class="monsterinsights-widget-popular-posts-image">'; |
||
124 | $html .= '<img src="' . esc_url($post['image']) . '" srcset=" ' . esc_attr($post['srcset']) . ' " alt="' . esc_attr( $post['title'] ) . '" />'; |
||
125 | $html .= '</div>'; |
||
126 | } |
||
127 | $html .= '<div class="monsterinsights-widget-popular-posts-text">'; |
||
128 | if ( isset( $theme_styles['styles']['label'] ) ) { |
||
129 | $html .= '<span class="monsterinsights-widget-popular-posts-label" '; |
||
130 | $html .= ! empty( $this->get_element_style( $theme, 'label', $atts ) ) ? 'style="' . esc_attr( $this->get_element_style( $theme, 'label', $atts ) ) . '"' : ''; |
||
131 | $html .= '>' . esc_html( $label_text ) . '</span>'; |
||
132 | } |
||
133 | $html .= '<span class="monsterinsights-widget-popular-posts-title" '; |
||
134 | $html .= ! empty( $this->get_element_style( $theme, 'title', $atts ) ) ? 'style="' . esc_attr( $this->get_element_style( $theme, 'title', $atts ) ) . '"' : ''; |
||
135 | $html .= '>' . esc_html( $post['title'] ) . '</span>'; |
||
136 | $html .= '</div>'; // monsterinsights-widget-popular-posts-text. |
||
137 | $html .= '</a>'; |
||
138 | $html .= '</li>'; |
||
139 | } |
||
140 | |||
141 | $html .= '</ul></div><p></p>';// Main div. |
||
142 | |||
143 | return $html; |
||
144 | |||
286 |