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 ( 053b8e...e8a206 )
by Brad
03:23
created

FooGalleryAttachment::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 16
rs 9.4285
cc 2
eloc 14
nc 2
nop 1
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 = trim( $post->post_excerpt );
49
			$this->description = trim( $post->post_content );
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 ) {
149
				return $img;
150
			}
151
152
			if ( 'page' === $link ) {
153
				//get the URL to the attachment page
154
				$url = get_attachment_link( $this->ID );
155
			} else if ( 'custom' === $link ) {
156
				$url = $args['custom_link'];
157
			} else {
158
				$url = $this->url;
159
			}
160
161
			//fallback for images that might not have a custom url
162
			if ( empty( $url ) ) {
163
				$url = $this->url;
164
			}
165
166
			$attr = array();
167
168
			$attr['href'] = $url;
169
170
			if ( ! empty( $this->custom_target ) ) {
171
				$attr['target'] = $this->custom_target;
172
			}
173
174
			if ( ! empty( $this->caption ) ) {
175
				$attr['data-caption-title'] = $this->caption;
176
			}
177
178
			if ( !empty( $this->description ) ) {
179
				$attr['data-caption-desc'] = $this->description;
180
			}
181
182
			$attr['data-attachment-id'] = $this->ID;
183
184
			//pull any custom attributes out the args
185
			if ( isset( $args['link_attributes'] ) && is_array( $args['link_attributes'] ) ) {
186
				$attr = array_merge( $attr, $args['link_attributes'] );
187
			}
188
189
			$attr = apply_filters( 'foogallery_attachment_html_link_attributes', $attr, $args, $this );
190
			$attr = array_map( 'esc_attr', $attr );
191
			$html = '<a ';
192
			foreach ( $attr as $name => $value ) {
193
				$html .= " $name=" . '"' . $value . '"';
194
			}
195
			$html .= '>';
196
			if ( $output_image ) {
197
				$html .= $img;
198
			}
199
			if ( $output_closing_tag ) {
200
				$html .= '</a>';
201
			};
202
203
			return apply_filters( 'foogallery_attachment_html_link', $html, $args, $this );
204
		}
205
206
		/**
207
		 * Returns generic html for captions
208
		 *
209
		 * @param $caption_content string Include title, desc, or both
210
		 *
211
		 * @return string
212
		 */
213
		public function html_caption( $caption_content ) {
214
			$html = '';
215
			$caption_html = array();
216
			if ( $this->caption && ( 'title' === $caption_content || 'both' === $caption_content ) ) {
217
				$caption_html[] = '<div class="foogallery-caption-title">' . $this->caption . '</div>';
218
			}
219
			if ( $this->description && ( 'desc' === $caption_content || 'both' === $caption_content ) ) {
220
				$caption_html[] = '<div class="foogallery-caption-desc">' . $this->description . '</div>';
221
			}
222
223
			if ( count($caption_html) > 0 ) {
224
				$html = '<div class="foogallery-caption">';
225
				$html .= implode( $caption_html );
226
				$html .= '</div>';
227
			}
228
229
			return apply_filters( 'foogallery_attachment_html_caption', $html, $caption_content, $this );
230
		}
231
	}
232
}
233