Conditions | 31 |
Paths | > 20000 |
Total Lines | 198 |
Code Lines | 134 |
Lines | 28 |
Ratio | 14.14 % |
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 |
||
46 | public function update_posts( $posts ) { |
||
47 | |||
48 | foreach ( $posts as $post ) { |
||
49 | |||
50 | $post_id = $post->ID; |
||
51 | |||
52 | // Assign a feed taxonomy term (feed type) to legacy posts. |
||
53 | wp_set_object_terms( $post_id, 'google', 'calendar_feed' ); |
||
54 | |||
55 | // Assign a calendar taxonomy term (calendar type) to legacy posts. |
||
56 | wp_set_object_terms( $post_id, 'default-calendar', 'calendar_type' ); |
||
57 | |||
58 | // Convert legacy list/grid view to default calendar view. |
||
59 | $display = get_post_meta( $post_id, 'gce_display_mode', true ); |
||
60 | $views = array(); |
||
61 | $range = false; |
||
62 | if ( 'list' == $display ) { |
||
63 | $views['default-calendar'] = 'list'; |
||
64 | } elseif ( 'list-grouped' == $display ) { |
||
65 | $views['default-calendar'] = 'list'; |
||
66 | } elseif ( 'date-range-list' == $display ) { |
||
67 | $views['default-calendar'] = 'list'; |
||
68 | $range = true; |
||
69 | } elseif ( 'date-range-grid' == $display ) { |
||
70 | $views['default-calendar'] = 'grid'; |
||
71 | $range = true; |
||
72 | } else { |
||
73 | $views['default-calendar'] = 'grid'; |
||
74 | } |
||
75 | update_post_meta( $post_id, '_calendar_view', $views ); |
||
76 | |||
77 | // List calendar settings. |
||
78 | $list_span = get_post_meta( $post_id, 'gce_events_per_page', true ); |
||
79 | $list_range = max( absint( get_post_meta( $post_id, 'gce_per_page_num', true ) ), 1 ); |
||
80 | if ( 'days' == $list_span ) { |
||
81 | $list_type = 'daily'; |
||
82 | } elseif ( 'week' == $list_span ) { |
||
83 | $list_type = 'weekly'; |
||
84 | $list_range = 1; |
||
85 | } elseif ( 'month' == $list_span ) { |
||
86 | $list_type = 'monthly'; |
||
87 | $list_range = 1; |
||
88 | } else { |
||
89 | $list_type = 'events'; |
||
90 | } |
||
91 | update_post_meta( $post_id, '_default_calendar_list_range_type', $list_type ); |
||
92 | update_post_meta( $post_id, '_default_calendar_list_range_span', $list_range ); |
||
93 | |||
94 | $calendar_begins = 'today'; |
||
95 | |||
96 | // Custom calendar range. |
||
97 | if ( $range === true ) { |
||
98 | |||
99 | $begins = get_post_meta( $post_id, 'gce_feed_range_start', true ); |
||
100 | $ends = get_post_meta( $post_id, 'gce_feed_range_end', true ); |
||
101 | |||
102 | if ( $begins && $ends ) { |
||
103 | update_post_meta( $post_id, '_calendar_begins', 'custom_date' ); |
||
104 | update_post_meta( $post_id, '_calendar_begins_custom_date', $this->convert_legacy_range( $begins ) ); |
||
105 | } else { |
||
106 | update_post_meta( $post_id, '_calendar_begins', $calendar_begins ); |
||
107 | } |
||
108 | |||
109 | } else { |
||
110 | |||
111 | // Legacy list calendars may have a start offset. |
||
112 | $offset = absint( get_post_meta( $post_id, 'gce_list_start_offset_num', true ) ); |
||
113 | if ( 'list' == $display && $offset > 0 ) { |
||
114 | $calendar_begins = 'back' == get_post_meta( $post_id, 'gce_list_start_offset_direction', true ) ? 'days_before' : 'days_after'; |
||
115 | update_post_meta( $post_id, '_calendar_begins_nth', $offset ); |
||
116 | } |
||
117 | |||
118 | update_post_meta( $post_id, '_calendar_begins', $calendar_begins ); |
||
119 | } |
||
120 | |||
121 | // Earliest event. |
||
122 | $start_before = get_post_meta( $post_id, 'gce_feed_start', true ); |
||
123 | $start_amt = absint( get_post_meta( $post_id, 'gce_feed_start_num', true ) ); |
||
124 | View Code Duplication | if ( $start_amt > 0 ) { |
|
|
|||
125 | if ( 'years' == $start_before ) { |
||
126 | $earliest = 'years_before'; |
||
127 | } elseif ( 'months' == $start_before ) { |
||
128 | $earliest = 'months_before'; |
||
129 | } else { |
||
130 | $earliest = 'days_before'; |
||
131 | } |
||
132 | update_post_meta( $post_id, '_feed_earliest_event_date', $earliest ); |
||
133 | update_post_meta( $post_id, '_feed_earliest_event_date_range', $start_amt ); |
||
134 | } else { |
||
135 | update_post_meta( $post_id, '_feed_earliest_event_date', 'calendar_start' ); |
||
136 | update_post_meta( $post_id, '_feed_earliest_event_date_range', 1 ); |
||
137 | } |
||
138 | |||
139 | // Latest event. |
||
140 | $end_after = get_post_meta( $post_id, 'gce_feed_end', true ); |
||
141 | $end_amt = absint( get_post_meta( $post_id, 'gce_feed_end_num', true ) ); |
||
142 | View Code Duplication | if ( $end_amt > 0 ) { |
|
143 | if ( 'years' == $end_after ) { |
||
144 | $latest = 'years_after'; |
||
145 | } elseif ( 'months' == $end_after ) { |
||
146 | $latest = 'months_after'; |
||
147 | } else { |
||
148 | $latest = 'days_after'; |
||
149 | } |
||
150 | update_post_meta( $post_id, '_feed_latest_event_date', $latest ); |
||
151 | update_post_meta( $post_id, '_feed_latest_event_date_range', $end_amt ); |
||
152 | } else { |
||
153 | update_post_meta( $post_id, '_feed_latest_event_date', 'calendar_start' ); |
||
154 | update_post_meta( $post_id, '_feed_latest_event_date_range', 1 ); |
||
155 | } |
||
156 | |||
157 | // Static calendar. |
||
158 | if ( false === get_post_meta( $post_id, 'gce_paging', true ) ) { |
||
159 | update_post_meta( $post_id, '_calendar_is_static', 'yes' ); |
||
160 | } |
||
161 | |||
162 | // Default calendar bubble trigger (click was unavailable before 3.0.0). |
||
163 | update_post_meta( $post_id, '_default_calendar_event_bubble_trigger', 'hover' ); |
||
164 | |||
165 | // Default calendar multiple day events. |
||
166 | if ( get_post_meta( $post_id, 'gce_multi_day_events', true ) ) { |
||
167 | update_post_meta( $post_id, '_default_calendar_expand_multi_day_events', 'yes' ); |
||
168 | } else { |
||
169 | update_post_meta( $post_id, '_default_calendar_expand_multi_day_events', 'no' ); |
||
170 | } |
||
171 | |||
172 | // Google Calendar ID. |
||
173 | $google_id = get_post_meta( $post_id, 'gce_feed_url', true ); |
||
174 | update_post_meta( $post_id, '_google_calendar_id', base64_encode( trim( $google_id ) ) ); |
||
175 | |||
176 | // Google max results. |
||
177 | update_post_meta( $post_id, '_google_events_max_results', 2500 ); |
||
178 | |||
179 | // Google calendar feed search terms. |
||
180 | $google_search = get_post_meta( $post_id, 'gce_search_query', true ); |
||
181 | if ( ! empty( $google_search ) ) { |
||
182 | update_post_meta( $post_id, '_google_events_search_query', trim( $google_search ) ); |
||
183 | } |
||
184 | |||
185 | // Google recurring events. |
||
186 | if ( get_post_meta( $post_id, 'gce_expand_recurring', true ) ) { |
||
187 | update_post_meta( $post_id, '_google_events_recurring', 'show' ); |
||
188 | } else { |
||
189 | update_post_meta( $post_id, '_google_events_recurring', 'first-only' ); |
||
190 | } |
||
191 | |||
192 | // Date and time format. |
||
193 | $date_format = get_post_meta( $post_id, 'gce_date_format', true ); |
||
194 | if ( ! empty( $date_format ) ) { |
||
195 | update_post_meta( $post_id, '_calendar_date_format_setting', 'use_custom_php' ); |
||
196 | update_post_meta( $post_id, '_calendar_date_format_php', $date_format ); |
||
197 | } else { |
||
198 | update_post_meta( $post_id, '_calendar_date_format_setting', 'use_site' ); |
||
199 | } |
||
200 | $time_format = get_post_meta( $post_id, 'gce_time_format', true ); |
||
201 | if ( ! empty( $time_format ) ) { |
||
202 | update_post_meta( $post_id, '_calendar_time_format_setting', 'use_custom_php' ); |
||
203 | update_post_meta( $post_id, '_calendar_time_format_php', $time_format ); |
||
204 | } else { |
||
205 | update_post_meta( $post_id, '_calendar_time_format_setting', 'use_site' ); |
||
206 | } |
||
207 | update_post_meta( $post_id, '_calendar_datetime_separator', '@' ); |
||
208 | update_post_meta( $post_id, '_calendar_week_starts_on_setting', 'use_site' ); |
||
209 | |||
210 | // Feed transient cache duration. |
||
211 | $cache = get_post_meta( $post_id, 'gce_cache', true ); |
||
212 | if ( is_numeric( $cache ) ) { |
||
213 | $seconds = absint( $cache ); |
||
214 | if ( $seconds < 3600 ) { |
||
215 | $amount = $seconds / 60; |
||
216 | $unit = 60; |
||
217 | } elseif ( $seconds < 86400 ) { |
||
218 | $amount = $seconds / 3600; |
||
219 | $unit = 3600; |
||
220 | } elseif ( $seconds < 604800 ) { |
||
221 | $amount = $seconds / 86400; |
||
222 | $unit = 86400; |
||
223 | } else { |
||
224 | $amount = $seconds / 604800; |
||
225 | $unit = 604800; |
||
226 | } |
||
227 | $amount = max( ceil( $amount ), 1 ); |
||
228 | update_post_meta( $post_id, '_feed_cache_user_unit', $unit ); |
||
229 | update_post_meta( $post_id, '_feed_cache_user_amount', $amount ); |
||
230 | update_post_meta( $post_id, '_feed_cache', $unit * $amount ); |
||
231 | } else { |
||
232 | update_post_meta( $post_id, '_feed_cache_user_unit', 2 ); |
||
233 | update_post_meta( $post_id, '_feed_cache_user_amount', 3600 ); |
||
234 | update_post_meta( $post_id, '_feed_cache', 7200 ); |
||
235 | } |
||
236 | |||
237 | $this->delete_post_meta( $post_id ); |
||
238 | |||
239 | // Post updated. |
||
240 | update_post_meta( $post_id, '_calendar_version', '3.0.0' ); |
||
241 | } |
||
242 | |||
243 | } |
||
244 | |||
387 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.