Conditions | 24 |
Paths | 54 |
Total Lines | 202 |
Code Lines | 148 |
Lines | 28 |
Ratio | 13.86 % |
Changes | 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 |
||
38 | $widget_conditions_data['category'][] = array( '', __( 'All category pages', 'jetpack' ) ); |
||
39 | |||
40 | $categories = get_categories( array( 'number' => 1000, 'orderby' => 'count', 'order' => 'DESC' ) ); |
||
41 | usort( $categories, array( __CLASS__, 'strcasecmp_name' ) ); |
||
42 | |||
43 | foreach ( $categories as $category ) { |
||
44 | $widget_conditions_data['category'][] = array( (string) $category->term_id, $category->name ); |
||
45 | } |
||
46 | |||
47 | $widget_conditions_data['loggedin'] = array(); |
||
48 | $widget_conditions_data['loggedin'][] = array( 'loggedin', __( 'Logged In', 'jetpack' ) ); |
||
49 | $widget_conditions_data['loggedin'][] = array( 'loggedout', __( 'Logged Out', 'jetpack' ) ); |
||
50 | |||
51 | $widget_conditions_data['author'] = array(); |
||
52 | $widget_conditions_data['author'][] = array( '', __( 'All author pages', 'jetpack' ) ); |
||
53 | |||
54 | $authors = get_users( array( 'orderby' => 'name', 'exclude_admin' => true ) ); |
||
55 | |||
56 | foreach ( $authors as $author ) { |
||
57 | $widget_conditions_data['author'][] = array( (string) $author->ID, $author->display_name ); |
||
58 | } |
||
59 | |||
60 | $widget_conditions_data['role'] = array(); |
||
61 | |||
62 | global $wp_roles; |
||
63 | |||
64 | foreach ( $wp_roles->roles as $role_key => $role ) { |
||
65 | $widget_conditions_data['role'][] = array( (string) $role_key, $role['name'] ); |
||
66 | } |
||
67 | |||
68 | $widget_conditions_data['tag'] = array(); |
||
69 | $widget_conditions_data['tag'][] = array( '', __( 'All tag pages', 'jetpack' ) ); |
||
70 | |||
71 | $tags = get_tags( array( 'number' => 1000, 'orderby' => 'count', 'order' => 'DESC' ) ); |
||
72 | usort( $tags, array( __CLASS__, 'strcasecmp_name' ) ); |
||
73 | |||
74 | foreach ( $tags as $tag ) { |
||
75 | $widget_conditions_data['tag'][] = array( (string) $tag->term_id, $tag->name ); |
||
76 | } |
||
77 | |||
78 | $widget_conditions_data['date'] = array(); |
||
79 | $widget_conditions_data['date'][] = array( '', __( 'All date archives', 'jetpack' ) ); |
||
80 | $widget_conditions_data['date'][] = array( 'day', __( 'Daily archives', 'jetpack' ) ); |
||
81 | $widget_conditions_data['date'][] = array( 'month', __( 'Monthly archives', 'jetpack' ) ); |
||
82 | $widget_conditions_data['date'][] = array( 'year', __( 'Yearly archives', 'jetpack' ) ); |
||
83 | |||
84 | $widget_conditions_data['page'] = array(); |
||
85 | $widget_conditions_data['page'][] = array( 'front', __( 'Front page', 'jetpack' ) ); |
||
86 | $widget_conditions_data['page'][] = array( 'posts', __( 'Posts page', 'jetpack' ) ); |
||
87 | $widget_conditions_data['page'][] = array( 'archive', __( 'Archive page', 'jetpack' ) ); |
||
88 | $widget_conditions_data['page'][] = array( '404', __( '404 error page', 'jetpack' ) ); |
||
89 | $widget_conditions_data['page'][] = array( 'search', __( 'Search results', 'jetpack' ) ); |
||
90 | |||
91 | $post_types = get_post_types( array( 'public' => true ), 'objects' ); |
||
92 | |||
93 | $widget_conditions_post_types = array(); |
||
94 | |||
95 | foreach ( $post_types as $post_type ) { |
||
96 | $widget_conditions_post_types[] = array( 'post_type-' . $post_type->name, $post_type->labels->singular_name ); |
||
97 | } |
||
98 | |||
99 | $widget_conditions_data['page'][] = array( __( 'Post type:', 'jetpack' ), $widget_conditions_post_types ); |
||
100 | |||
101 | $pages_dropdown = preg_replace( '/<\/?select[^>]*?>/i', '', wp_dropdown_pages( array( 'echo' => false ) ) ); |
||
102 | |||
103 | preg_match_all( '/value=.([0-9]+).[^>]*>([^<]+)</', $pages_dropdown, $page_ids_and_titles, PREG_SET_ORDER ); |
||
104 | |||
105 | $static_pages = array(); |
||
106 | |||
107 | foreach ( $page_ids_and_titles as $page_id_and_title ) { |
||
108 | $static_pages[] = array( (string) $page_id_and_title[1], $page_id_and_title[2] ); |
||
109 | } |
||
110 | |||
111 | $widget_conditions_data['page'][] = array( __( 'Static page:', 'jetpack' ), $static_pages ); |
||
112 | |||
113 | $widget_conditions_data['taxonomy'] = array(); |
||
114 | $widget_conditions_data['taxonomy'][] = array( '', __( 'All taxonomy pages', 'jetpack' ) ); |
||
115 | |||
116 | $taxonomies = get_taxonomies( array( '_builtin' => false ), 'objects' ); |
||
117 | usort( $taxonomies, array( __CLASS__, 'strcasecmp_name' ) ); |
||
118 | |||
119 | foreach ( $taxonomies as $taxonomy ) { |
||
120 | $taxonomy_terms = get_terms( array( $taxonomy->name ), array( 'number' => 250, 'hide_empty' => false ) ); |
||
121 | |||
122 | $widget_conditions_terms = array(); |
||
123 | $widget_conditions_terms[] = array( $taxonomy->name, __( 'All pages', 'jetpack' ) ); |
||
124 | |||
125 | foreach ( $taxonomy_terms as $term ) { |
||
126 | $widget_conditions_terms[] = array( $taxonomy->name . '_tax_' . $term->term_id, $term->name ); |
||
127 | } |
||
128 | |||
129 | $widget_conditions_data['taxonomy'][] = array( $taxonomy->labels->name . ':', $widget_conditions_terms ); |
||
130 | } |
||
131 | |||
132 | wp_localize_script( 'widget-conditions', 'widget_conditions_data', $widget_conditions_data ); |
||
133 | |||
134 | // Save a list of the IDs of all pages that have children for dynamically showing the "Include children" checkbox. |
||
135 | $all_pages = get_pages(); |
||
136 | $all_parents = array(); |
||
137 | |||
138 | foreach ( $all_pages as $page ) { |
||
139 | if ( $page->post_parent ) { |
||
140 | $all_parents[ (string) $page->post_parent ] = true; |
||
141 | } |
||
142 | } |
||
143 | |||
144 | $front_page_id = get_option( 'page_on_front' ); |
||
145 | |||
146 | if ( isset( $all_parents[ $front_page_id ] ) ) { |
||
147 | $all_parents[ 'front' ] = true; |
||
148 | } |
||
149 | |||
150 | wp_localize_script( 'widget-conditions', 'widget_conditions_parent_pages', $all_parents ); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Add the widget conditions to each widget in the admin. |
||
155 | * |
||
156 | * @param $widget unused. |
||
157 | * @param $return unused. |
||
158 | * @param array $instance The widget settings. |
||
159 | */ |
||
160 | public static function widget_conditions_admin( $widget, $return, $instance ) { |
||
161 | $conditions = array(); |
||
162 | |||
163 | if ( isset( $instance['conditions'] ) ) |
||
164 | $conditions = $instance['conditions']; |
||
165 | |||
166 | if ( ! isset( $conditions['action'] ) ) |
||
167 | $conditions['action'] = 'show'; |
||
168 | |||
169 | if ( empty( $conditions['rules'] ) ) |
||
170 | $conditions['rules'][] = array( 'major' => '', 'minor' => '', 'has_children' => '' ); |
||
171 | |||
172 | if ( empty( $conditions['match_all'] ) ) { |
||
173 | $conditions['match_all'] = false; |
||
174 | } |
||
175 | |||
176 | ?> |
||
177 | <div |
||
178 | class=" |
||
179 | widget-conditional |
||
180 | <?php |
||
181 | if ( |
||
182 | empty( $_POST['widget-conditions-visible'] ) |
||
183 | || $_POST['widget-conditions-visible'] == '0' |
||
184 | ) { |
||
185 | ?>widget-conditional-hide<?php |
||
186 | } |
||
187 | ?> |
||
188 | <?php |
||
189 | if ( ! empty( $conditions['match_all'] ) && $conditions['match_all'] ) { |
||
190 | ?>intersection<?php |
||
191 | } else { |
||
192 | ?>conjunction<?php |
||
193 | } |
||
194 | ?> |
||
195 | "> |
||
196 | <input type="hidden" name="widget-conditions-visible" value="<?php if ( isset( $_POST['widget-conditions-visible'] ) ) { echo esc_attr( $_POST['widget-conditions-visible'] ); } else { ?>0<?php } ?>" /> |
||
197 | <?php if ( ! isset( $_POST['widget-conditions-visible'] ) ) { ?><a href="#" class="button display-options"><?php _e( 'Visibility', 'jetpack' ); ?></a><?php } ?> |
||
198 | <div class="widget-conditional-inner"> |
||
199 | <div class="condition-top"> |
||
200 | <?php printf( _x( '%s if:', 'placeholder: dropdown menu to select widget visibility; hide if or show if', 'jetpack' ), '<select name="conditions[action]"><option value="show" ' . selected( $conditions['action'], 'show', false ) . '>' . esc_html_x( 'Show', 'Used in the "%s if:" translation for the widget visibility dropdown', 'jetpack' ) . '</option><option value="hide" ' . selected( $conditions['action'], 'hide', false ) . '>' . esc_html_x( 'Hide', 'Used in the "%s if:" translation for the widget visibility dropdown', 'jetpack' ) . '</option></select>' ); ?> |
||
201 | </div><!-- .condition-top --> |
||
202 | |||
203 | <div class="conditions"> |
||
204 | <?php |
||
205 | |||
206 | foreach ( $conditions['rules'] as $rule_index => $rule ) { |
||
207 | $rule = wp_parse_args( $rule, array( 'major' => '', 'minor' => '', 'has_children' => '' ) ); |
||
208 | ?> |
||
209 | <div class="condition" data-rule-major="<?php echo esc_attr( $rule['major'] ); ?>" data-rule-minor="<?php echo esc_attr( $rule['minor'] ); ?>" data-rule-has-children="<?php echo esc_attr( $rule['has_children'] ); ?>"> |
||
210 | <div class="selection alignleft"> |
||
211 | <select class="conditions-rule-major" name="conditions[rules_major][]"> |
||
212 | <option value="" <?php selected( "", $rule['major'] ); ?>><?php echo esc_html_x( '-- Select --', 'Used as the default option in a dropdown list', 'jetpack' ); ?></option> |
||
213 | <option value="category" <?php selected( "category", $rule['major'] ); ?>><?php esc_html_e( 'Category', 'jetpack' ); ?></option> |
||
214 | <option value="author" <?php selected( "author", $rule['major'] ); ?>><?php echo esc_html_x( 'Author', 'Noun, as in: "The author of this post is..."', 'jetpack' ); ?></option> |
||
215 | |||
216 | <?php if( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { // this doesn't work on .com because of caching ?> |
||
217 | <option value="loggedin" <?php selected( "loggedin", $rule['major'] ); ?>><?php echo esc_html_x( 'User', 'Noun', 'jetpack' ); ?></option> |
||
218 | <option value="role" <?php selected( "role", $rule['major'] ); ?>><?php echo esc_html_x( 'Role', 'Noun, as in: "The user role of that can access this widget is..."', 'jetpack' ); ?></option> |
||
219 | <?php } ?> |
||
220 | |||
221 | <option value="tag" <?php selected( "tag", $rule['major'] ); ?>><?php echo esc_html_x( 'Tag', 'Noun, as in: "This post has one tag."', 'jetpack' ); ?></option> |
||
222 | <option value="date" <?php selected( "date", $rule['major'] ); ?>><?php echo esc_html_x( 'Date', 'Noun, as in: "This page is a date archive."', 'jetpack' ); ?></option> |
||
223 | <option value="page" <?php selected( "page", $rule['major'] ); ?>><?php echo esc_html_x( 'Page', 'Example: The user is looking at a page, not a post.', 'jetpack' ); ?></option> |
||
224 | <option value="post_type" <?php selected( "post_type", $rule['major'] ); ?>><?php echo esc_html_x( 'Post Type', 'Example: the user is viewing a custom post type archive.', 'jetpack' ); ?></option> |
||
225 | <?php if ( get_taxonomies( array( '_builtin' => false ) ) ) : ?> |
||
226 | <option value="taxonomy" <?php selected( "taxonomy", $rule['major'] ); ?>><?php echo esc_html_x( 'Taxonomy', 'Noun, as in: "This post has one taxonomy."', 'jetpack' ); ?></option> |
||
227 | <?php endif; ?> |
||
228 | </select> |
||
229 | |||
230 | <?php _ex( 'is', 'Widget Visibility: {Rule Major [Page]} is {Rule Minor [Search results]}', 'jetpack' ); ?> |
||
231 | |||
232 | <select class="conditions-rule-minor" name="conditions[rules_minor][]" <?php if ( ! $rule['major'] ) { ?> disabled="disabled"<?php } ?>> |
||
233 | <?php /* Include the currently selected value so that if the widget is saved without |
||
234 | expanding the Visibility section, we don't lose the minor part of the rule. |
||
235 | If it is opened, this list is cleared out and populated with all the values. */ ?> |
||
236 | <option value="<?php echo esc_attr( $rule['minor'] ); ?>" selected="selected"></option> |
||
237 | </select> |
||
238 | |||
239 | <span class="conditions-rule-has-children" <?php if ( ! $rule['has_children'] ) { ?> style="display: none;"<?php } ?>> |
||
240 | <label> |
||
697 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.