| Conditions | 13 |
| Paths | 9 |
| Total Lines | 96 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 161 | public function to_json( $posts ) { |
||
| 162 | |||
| 163 | // If there are no events, return empty JSON |
||
| 164 | if ( empty( $posts ) || is_null( $posts ) ) { |
||
| 165 | return ''; |
||
| 166 | } |
||
| 167 | |||
| 168 | // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background. |
||
| 169 | $display_images_as = isset( $_REQUEST['display_images_as'] ) ? $_REQUEST['display_images_as'] : 'media'; |
||
| 170 | |||
| 171 | // The number of words for the excerpt (by default 55, as WordPress). |
||
| 172 | $this->excerpt_length = isset( $_REQUEST['excerpt_words'] ) && is_numeric( $_REQUEST['excerpt_words'] ) ? $_REQUEST['excerpt_words'] : 55; |
||
| 173 | add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
||
| 174 | |||
| 175 | // Add a filter to remove the [...] after excerpts, since we're adding |
||
| 176 | // a link to the post itself. |
||
| 177 | add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) ); |
||
| 178 | |||
| 179 | // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates. |
||
| 180 | $start_at_slide = 0; |
||
| 181 | $event_index = - 1; |
||
| 182 | $now = time(); |
||
| 183 | |||
| 184 | // Prepare the timeline variable. |
||
| 185 | $timeline = array(); |
||
| 186 | |||
| 187 | // Populate the arrays. |
||
| 188 | $timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as ) { |
||
| 189 | |||
| 190 | // Get the start and end dates. |
||
| 191 | $start_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, TRUE ) ); |
||
| 192 | $end_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, TRUE ) ); |
||
| 193 | |||
| 194 | // Set the starting slide. |
||
| 195 | $event_index ++; |
||
| 196 | if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) { |
||
| 197 | $start_at_slide = $event_index; |
||
| 198 | } |
||
| 199 | |||
| 200 | // Load thumbnail |
||
| 201 | if ( '' !== ( $thumbnail_id = get_post_thumbnail_id( $item->ID ) ) |
||
| 202 | && FALSE !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) ) |
||
| 203 | ) { |
||
| 204 | |||
| 205 | // Set the thumbnail URL. |
||
| 206 | if ( 'background' === $display_images_as ) { |
||
| 207 | $date['background'] = array( 'url' => $attachment[0], ); |
||
| 208 | $date['media'] = array( 'thumbnail' => $attachment[0], ); |
||
| 209 | } else { |
||
| 210 | $date['media'] = array( |
||
| 211 | 'url' => $attachment[0], |
||
| 212 | 'thumbnail' => $attachment[0], |
||
| 213 | ); |
||
| 214 | } |
||
| 215 | |||
| 216 | } |
||
| 217 | |||
| 218 | // Set the start/end dates by converting them to TimelineJS required format. |
||
| 219 | $date['start_date'] = Wordlift_Timeline_Service::date( $start_date ); |
||
| 220 | $date['end_date'] = Wordlift_Timeline_Service::date( $end_date ); |
||
| 221 | |||
| 222 | setup_postdata( $GLOBALS['post'] = &$item ); |
||
| 223 | |||
| 224 | $more_link_text = sprintf( |
||
| 225 | '<span aria-label="%1$s">%2$s</span>', |
||
| 226 | sprintf( |
||
| 227 | /* translators: %s: Name of current post */ |
||
| 228 | __( 'Continue reading %s' ), |
||
| 229 | the_title_attribute( array( 'echo' => FALSE ) ) |
||
| 230 | ), |
||
| 231 | __( '(more…)' ) |
||
| 232 | ); |
||
| 233 | |||
| 234 | // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352). |
||
| 235 | $date['text'] = array( |
||
| 236 | 'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>', |
||
| 237 | ); |
||
| 238 | |||
| 239 | // If we have an excerpt, set it. |
||
| 240 | if ( 0 < $this->excerpt_length ) { |
||
| 241 | $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt( $item ), get_permalink(), $more_link_text ); |
||
| 242 | } |
||
| 243 | |||
| 244 | return $date; |
||
| 245 | |||
| 246 | }, $posts ); |
||
| 247 | |||
| 248 | // Finally remove the excerpt filter. |
||
| 249 | remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
||
| 250 | |||
| 251 | // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html |
||
| 252 | return array( |
||
| 253 | 'timeline' => $timeline, |
||
| 254 | 'start_at_slide' => $start_at_slide, |
||
| 255 | ); |
||
| 256 | } |
||
| 257 | |||
| 309 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: