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.

FooGalleryAttachment::html_caption()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 8
nop 1
dl 0
loc 18
rs 8.4444
c 0
b 0
f 0
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
			do_action( 'foogallery_attachment_instance_after_load', $this, $post );
61
		}
62
63
		/**
64
		 * Static function to load a FooGalleryAttachment instance by passing in a post object
65
		 * @static
66
		 *
67
		 * @param $post
68
		 *
69
		 * @return FooGalleryAttachment
70
		 */
71
		public static function get( $post ) {
72
			return new self( $post );
73
		}
74
75
		/**
76
		 * Static function to load a FooGalleryAttachment instance by passing in an attachment_id
77
		 * @static
78
		 *
79
		 * @param $attachment_id
80
		 *
81
		 * @return FooGalleryAttachment
82
		 */
83
		public static function get_by_id( $attachment_id ) {
84
			$post = get_post( $attachment_id );
85
			return new self( $post );
86
		}
87
88
		/**
89
		 * Returns the image source only
90
		 *
91
		 * @param array $args
92
		 * @return string
93
		 */
94
		public function html_img_src( $args = array() ) {
95
			return apply_filters( 'foogallery_attachment_resize_thumbnail', $this->url, $args, $this );
96
		}
97
98
		/**
99
		 * Returns the HTML img tag for the attachment
100
		 * @param array $args
101
		 *
102
		 * @return string
103
		 */
104
		public function html_img( $args = array() ) {
105
			$attr['src'] = $this->html_img_src( $args );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attr was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attr = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
106
107
			if ( ! empty( $this->alt ) ) {
108
				$attr['alt'] = $this->alt;
109
			}
110
111
			//pull any custom attributes out the args
112
			if ( isset( $args['image_attributes'] ) && is_array( $args['image_attributes'] ) ) {
113
				$attr = array_merge( $attr, $args['image_attributes'] );
114
			}
115
116
			//check for width and height args and add those to the image
117
			if ( isset( $args['width'] ) && intval( $args['width'] ) > 0 ) {
118
				$attr['width'] = $args['width'];
119
			}
120
			if ( isset( $args['height'] ) && intval( $args['height'] ) > 0 ) {
121
				$attr['height'] = $args['height'];
122
			}
123
124
			$attr = apply_filters( 'foogallery_attachment_html_image_attributes', $attr, $args, $this );
125
			$attr = array_map( 'esc_attr', $attr );
126
			$html = '<img ';
127
			foreach ( $attr as $name => $value ) {
128
				$html .= " $name=" . '"' . $value . '"';
129
			}
130
			$html .= ' />';
131
132
			return apply_filters( 'foogallery_attachment_html_image', $html, $args, $this );
133
		}
134
135
		/**
136
		 * Returns HTML for the attachment
137
		 * @param array $args
138
		 * @param bool $output_image
139
		 * @param bool $output_closing_tag
140
		 *
141
		 * @return string
142
		 */
143
		public function html( $args = array(), $output_image = true, $output_closing_tag = true ) {
144
			if ( empty ( $this->url ) )  {
145
				return '';
146
			}
147
148
			$arg_defaults = array(
149
				'link' => 'image',
150
				'custom_link' => $this->custom_url
151
			);
152
153
			$args = wp_parse_args( $args, $arg_defaults );
154
155
			$link = $args['link'];
156
157
			$img = $this->html_img( $args );
158
159
			/* 12 Apr 2016 - PLEASE NOTE
160
			We no longer just return the image html when "no link" option is chosen.
161
			It was decided that it is better to return an anchor link with no href or target attributes.
162
			This results in more standardized HTML output for better CSS and JS code
163
			*/
164
165
			if ( 'page' === $link ) {
166
				//get the URL to the attachment page
167
				$url = get_attachment_link( $this->ID );
168
			} else if ( 'custom' === $link ) {
169
				$url = $args['custom_link'];
170
			} else {
171
				$url = $this->url;
172
			}
173
174
			//fallback for images that might not have a custom url
175
			if ( empty( $url ) ) {
176
				$url = $this->url;
177
			}
178
179
			$attr = array();
180
181
			//only add href and target attributes to the anchor if the link is NOT set to 'none'
182
			if ( $link !== 'none' ){
183
				$attr['href'] = $url;
184
				if ( ! empty( $this->custom_target ) && 'default' !== $this->custom_target ) {
185
					$attr['target'] = $this->custom_target;
186
				}
187
			}
188
189
			if ( ! empty( $this->caption ) ) {
190
				$attr['data-caption-title'] = $this->caption;
191
			}
192
193
			if ( !empty( $this->description ) ) {
194
				$attr['data-caption-desc'] = $this->description;
195
			}
196
197
			$attr['data-attachment-id'] = $this->ID;
198
199
			//pull any custom attributes out the args
200
			if ( isset( $args['link_attributes'] ) && is_array( $args['link_attributes'] ) ) {
201
				$attr = array_merge( $attr, $args['link_attributes'] );
202
			}
203
204
			$attr = apply_filters( 'foogallery_attachment_html_link_attributes', $attr, $args, $this );
205
			$attr = array_map( 'esc_attr', $attr );
206
			$html = '<a ';
207
			foreach ( $attr as $name => $value ) {
208
				$html .= " $name=" . '"' . $value . '"';
209
			}
210
			$html .= '>';
211
			if ( $output_image ) {
212
				$html .= $img;
213
			}
214
			if ( $output_closing_tag ) {
215
				$html .= '</a>';
216
			};
217
218
			return apply_filters( 'foogallery_attachment_html_link', $html, $args, $this );
219
		}
220
221
		/**
222
		 * Returns generic html for captions
223
		 *
224
		 * @param $caption_content string Include title, desc, or both
225
		 *
226
		 * @return string
227
		 */
228
		public function html_caption( $caption_content ) {
229
			$html = '';
230
			$caption_html = array();
231
			if ( $this->caption && ( 'title' === $caption_content || 'both' === $caption_content ) ) {
232
				$caption_html[] = '<div class="foogallery-caption-title">' . $this->caption . '</div>';
233
			}
234
			if ( $this->description && ( 'desc' === $caption_content || 'both' === $caption_content ) ) {
235
				$caption_html[] = '<div class="foogallery-caption-desc">' . $this->description . '</div>';
236
			}
237
238
			if ( count($caption_html) > 0 ) {
239
				$html = '<div class="foogallery-caption"><div class="foogallery-caption-inner">';
240
				$html .= implode( $caption_html );
241
				$html .= '</div></div>';
242
			}
243
244
			return apply_filters( 'foogallery_attachment_html_caption', $html, $caption_content, $this );
245
		}
246
	}
247
}
248