GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( fc9531...c44ae7 )
by Brad
02:36
created

FooGalleryAttachment::html()   F

Complexity

Conditions 18
Paths 4609

Size

Total Lines 86
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 86
rs 2
cc 18
eloc 43
nc 4609
nop 3

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Class FooGalleryAttachment
4
 *
5
 * An easy to use wrapper class for a FooGallery Attachment
6
 */
7
if ( ! class_exists( 'FooGalleryAttachment' ) ) {
8
9
	class FooGalleryAttachment extends stdClass {
10
		/**
11
		 * public constructor
12
		 *
13
		 * @param null $post
14
		 */
15
		public function __construct( $post = null ) {
16
			$this->set_defaults();
17
18
			if ( $post !== null ) {
19
				$this->load( $post );
20
			}
21
		}
22
23
		/**
24
		 *  Sets the default when a new gallery is instantiated
25
		 */
26
		private function set_defaults() {
27
			$this->_post = null;
28
			$this->ID = 0;
29
			$this->title = '';
30
			$this->caption = '';
31
			$this->description = '';
32
			$this->alt = '';
33
			$this->url = '';
34
			$this->width = 0;
35
			$this->height = 0;
36
			$this->custom_url = '';
37
			$this->custom_target = '';
38
		}
39
40
		/**
41
		 * private attachment load function
42
		 * @param $post
43
		 */
44
		private function load( $post ) {
45
			$this->_post = $post;
46
			$this->ID = $post->ID;
47
			$this->title = trim( $post->post_title );
48
			$this->caption = foogallery_get_caption_title_for_attachment( $post );
49
			$this->description = foogallery_get_caption_desc_for_attachment( $post );
50
			$this->alt = trim( get_post_meta( $this->ID, '_wp_attachment_image_alt', true ) );
51
			$this->custom_url = get_post_meta( $this->ID, '_foogallery_custom_url', true );
52
			$this->custom_target = get_post_meta( $this->ID, '_foogallery_custom_target', true );
53
			$image_attributes = wp_get_attachment_image_src( $this->ID, 'full' );
54
			if ( $image_attributes ) {
55
				$this->url = $image_attributes[0];
56
				$this->width = $image_attributes[1];
57
				$this->height = $image_attributes[2];
58
			}
59
		}
60
61
		/**
62
		 * Static function to load a FooGalleryAttachment instance by passing in a post object
63
		 * @static
64
		 *
65
		 * @param $post
66
		 *
67
		 * @return FooGalleryAttachment
68
		 */
69
		public static function get( $post ) {
70
			return new self( $post );
71
		}
72
73
		/**
74
		 * Static function to load a FooGalleryAttachment instance by passing in an attachment_id
75
		 * @static
76
		 *
77
		 * @param $attachment_id
78
		 *
79
		 * @return FooGalleryAttachment
80
		 */
81
		public static function get_by_id( $attachment_id ) {
82
			$post = get_post( $attachment_id );
83
			return new self( $post );
84
		}
85
86
		/**
87
		 * Returns the HTML img tag for the attachment
88
		 * @param array $args
89
		 *
90
		 * @return string
91
		 */
92
		public function html_img( $args = array() ) {
93
			$attr['src'] = apply_filters( 'foogallery_attachment_resize_thumbnail', $this->url, $args, $this );
94
95
			if ( ! empty( $this->alt ) ) {
96
				$attr['alt'] = $this->alt;
97
			}
98
99
			//pull any custom attributes out the args
100
			if ( isset( $args['image_attributes'] ) && is_array( $args['image_attributes'] ) ) {
101
				$attr = array_merge( $attr, $args['image_attributes'] );
102
			}
103
104
			//check for width and height args and add those to the image
105
			if ( isset( $args['width'] ) && intval( $args['width'] ) > 0 ) {
106
				$attr['width'] = $args['width'];
107
			}
108
			if ( isset( $args['height'] ) && intval( $args['height'] ) > 0 ) {
109
				$attr['height'] = $args['height'];
110
			}
111
112
			$attr = apply_filters( 'foogallery_attachment_html_image_attributes', $attr, $args, $this );
113
			$attr = array_map( 'esc_attr', $attr );
114
			$html = '<img ';
115
			foreach ( $attr as $name => $value ) {
116
				$html .= " $name=" . '"' . $value . '"';
117
			}
118
			$html .= ' />';
119
120
			return apply_filters( 'foogallery_attachment_html_image', $html, $args, $this );
121
		}
122
123
		/**
124
		 * Returns HTML for the attachment
125
		 * @param array $args
126
		 * @param bool $output_image
127
		 * @param bool $output_closing_tag
128
		 *
129
		 * @return string
130
		 */
131
		public function html( $args = array(), $output_image = true, $output_closing_tag = true ) {
132
			if ( empty ( $this->url ) )  {
133
				return '';
134
			}
135
136
			$arg_defaults = array(
137
				'link' => 'image',
138
				'custom_link' => $this->custom_url
139
			);
140
141
			$args = wp_parse_args( $args, $arg_defaults );
142
143
			$link = $args['link'];
144
145
			$img = $this->html_img( $args );
146
147
			//if there is no link, then just return the image tag
148
			if ( 'none' === $link ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
149
				/* 12 Apr 2016 - PLEASE NOTE
150
				We no longer just return the image html when "no link" option is chosen.
151
				It was decided that it is better to return an anchor link with no href or target attributes.
152
				This results in more standardized HTML output for better CSS and JS code
153
				*/
154
155
				// return $img;
156
			}
157
158
			if ( 'page' === $link ) {
159
				//get the URL to the attachment page
160
				$url = get_attachment_link( $this->ID );
161
			} else if ( 'custom' === $link ) {
162
				$url = $args['custom_link'];
163
			} else {
164
				$url = $this->url;
165
			}
166
167
			//fallback for images that might not have a custom url
168
			if ( empty( $url ) ) {
169
				$url = $this->url;
170
			}
171
172
			$attr = array();
173
174
			//only add href and target attributes to the anchor if the link is NOT set to 'none'
175
			if ( $link !== 'none' ){
176
				$attr['href'] = $url;
177
				if ( ! empty( $this->custom_target ) && 'default' !== $this->custom_target ) {
178
					$attr['target'] = $this->custom_target;
179
				}
180
			}
181
182
			if ( ! empty( $this->custom_target ) && 'default' !== $this->custom_target ) {
183
				$attr['target'] = $this->custom_target;
184
			}
185
186
			if ( ! empty( $this->caption ) ) {
187
				$attr['data-caption-title'] = $this->caption;
188
			}
189
190
			if ( !empty( $this->description ) ) {
191
				$attr['data-caption-desc'] = $this->description;
192
			}
193
194
			$attr['data-attachment-id'] = $this->ID;
195
196
			//pull any custom attributes out the args
197
			if ( isset( $args['link_attributes'] ) && is_array( $args['link_attributes'] ) ) {
198
				$attr = array_merge( $attr, $args['link_attributes'] );
199
			}
200
201
			$attr = apply_filters( 'foogallery_attachment_html_link_attributes', $attr, $args, $this );
202
			$attr = array_map( 'esc_attr', $attr );
203
			$html = '<a ';
204
			foreach ( $attr as $name => $value ) {
205
				$html .= " $name=" . '"' . $value . '"';
206
			}
207
			$html .= '>';
208
			if ( $output_image ) {
209
				$html .= $img;
210
			}
211
			if ( $output_closing_tag ) {
212
				$html .= '</a>';
213
			};
214
215
			return apply_filters( 'foogallery_attachment_html_link', $html, $args, $this );
216
		}
217
218
		/**
219
		 * Returns generic html for captions
220
		 *
221
		 * @param $caption_content string Include title, desc, or both
222
		 *
223
		 * @return string
224
		 */
225
		public function html_caption( $caption_content ) {
226
			$html = '';
227
			$caption_html = array();
228
			if ( $this->caption && ( 'title' === $caption_content || 'both' === $caption_content ) ) {
229
				$caption_html[] = '<div class="foogallery-caption-title">' . $this->caption . '</div>';
230
			}
231
			if ( $this->description && ( 'desc' === $caption_content || 'both' === $caption_content ) ) {
232
				$caption_html[] = '<div class="foogallery-caption-desc">' . $this->description . '</div>';
233
			}
234
235
			if ( count($caption_html) > 0 ) {
236
				$html = '<div class="foogallery-caption"><div class="foogallery-caption-inner">';
237
				$html .= implode( $caption_html );
238
				$html .= '</div></div>';
239
			}
240
241
			return apply_filters( 'foogallery_attachment_html_caption', $html, $caption_content, $this );
242
		}
243
	}
244
}
245