Conditions | 22 |
Paths | 1228 |
Total Lines | 95 |
Code Lines | 50 |
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 |
||
27 | function resize( $original_image_src, $args, $thumbnail_object ) { |
||
28 | global $current_foogallery; |
||
29 | global $foogallery_last_generated_thumb_url; |
||
30 | |||
31 | $arg_defaults = array( |
||
32 | 'width' => 0, |
||
33 | 'height' => 0, |
||
34 | 'crop' => true, |
||
35 | 'jpeg_quality' => foogallery_thumbnail_jpeg_quality(), |
||
36 | 'thumb_resize_animations' => foogallery_get_setting( 'thumb_resize_animations' ), |
||
37 | 'foogallery_attachment_id'=> $thumbnail_object->ID |
||
38 | ); |
||
39 | |||
40 | if ( isset( $current_foogallery ) ) { |
||
41 | $arg_defaults['foogallery_id'] = $current_foogallery->ID; |
||
42 | } |
||
43 | |||
44 | $args = wp_parse_args( $args, $arg_defaults ); |
||
45 | |||
46 | //allow for plugins to change the thumbnail creation args |
||
47 | $args = apply_filters( 'foogallery_thumbnail_resize_args', $args, $original_image_src, $thumbnail_object ); |
||
48 | |||
49 | //check the current arguments passed in by the shortcode |
||
50 | global $current_foogallery_arguments; |
||
51 | if ( isset( $current_foogallery_arguments ) && isset( $current_foogallery_arguments['template'] ) ) { |
||
52 | $thumbnail_args = apply_filters( 'foogallery_calculate_thumbnail_dimensions-' . $current_foogallery_arguments['template'], $args, $current_foogallery_arguments ); |
||
53 | $args = wp_parse_args( $thumbnail_args, $args ); |
||
54 | } |
||
55 | |||
56 | $width = (int)$args['width']; |
||
57 | $height = (int)$args['height']; |
||
58 | $crop = (bool)$args['crop']; |
||
59 | |||
60 | if ( 0 === $width && 0 === $height ) { |
||
61 | return $original_image_src; |
||
62 | } |
||
63 | |||
64 | //we can force the use of the originally uploaded full-size image |
||
65 | $force_use_original_image = isset( $args['force_use_original_image'] ) && true === $args['force_use_original_image']; |
||
66 | |||
67 | if ( $force_use_original_image ) { |
||
68 | $fullsize = wp_get_attachment_image_src( $thumbnail_object->ID, 'fullsize' ); |
||
69 | |||
70 | return $fullsize[0]; |
||
71 | } |
||
72 | |||
73 | //we can force the use of the original WP icon or WP-generated thumb by passing through args individually |
||
74 | $force_use_original_thumb = isset( $args['force_use_original_thumb'] ) && true === $args['force_use_original_thumb']; |
||
75 | |||
76 | if ( $force_use_original_thumb ) { |
||
77 | $thumbnail_icon = wp_get_attachment_image_src( $thumbnail_object->ID, array( $width, $height ) ); |
||
78 | |||
79 | return $thumbnail_icon[0]; |
||
80 | } |
||
81 | |||
82 | //we can force the use of original WP thumbs by passing through args individually, or by saved settings |
||
83 | $use_original_thumbs = ( isset( $args['use_original_thumbs'] ) && true === $args['use_original_thumbs'] ) || 'on' === foogallery_get_setting( 'use_original_thumbs' ); |
||
84 | |||
85 | if ( $use_original_thumbs ) { |
||
86 | |||
87 | $option_thumbnail_size_w = get_option( 'thumbnail_size_w' ); |
||
88 | $option_thumbnail_size_h = get_option( 'thumbnail_size_h' ); |
||
89 | $option_thumbnail_crop = get_option( 'thumbnail_crop' ); |
||
90 | |||
91 | //check if we are trying to get back the default thumbnail that we already have |
||
92 | if ( $thumbnail_object->ID > 0 && $width == $option_thumbnail_size_w && $height == $option_thumbnail_size_h && $crop == $option_thumbnail_crop ) { |
||
93 | $thumbnail_attributes = wp_get_attachment_image_src( $thumbnail_object->ID ); |
||
94 | |||
95 | return $thumbnail_attributes[0]; |
||
96 | } |
||
97 | } |
||
98 | |||
99 | if ( $thumbnail_object->ID > 0 ) { |
||
100 | $crop_from_position = get_post_meta( $thumbnail_object->ID, 'wpthumb_crop_pos', true ); |
||
101 | |||
102 | if ( !empty( $crop_from_position ) ) { |
||
103 | $args['crop_from_position'] = $crop_from_position; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | //remove invalid resize args |
||
108 | if ( array_key_exists( 'height', $args ) && 0 === $args['height'] ) { |
||
109 | unset( $args['height'] ); |
||
110 | } |
||
111 | |||
112 | //do some checks to see if the image is smaller |
||
113 | if ( $this->should_resize( $thumbnail_object, $args ) ) { |
||
114 | //save the generated thumb url to a global so that we can use it later if needed |
||
115 | $foogallery_last_generated_thumb_url = wpthumb( $original_image_src, $args ); |
||
116 | } else { |
||
117 | $foogallery_last_generated_thumb_url = $original_image_src; |
||
118 | } |
||
119 | |||
120 | return $foogallery_last_generated_thumb_url; |
||
121 | } |
||
122 | |||
170 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.