Issues (4138)

classes/frontend/class-gallery.php (97 issues)

1
<?php
0 ignored issues
show
This file is missing a doc comment.
Loading history...
2
namespace lsx_health_plan\classes\frontend;
3
4
/**
5
 * Contains the gallery functionality.
6
 *
7
 * @package lsx-health-plan
8
 */
9
class Gallery {
10
11
	/**
12
	 * Holds class instance
13
	 *
14
	 * @var object \lsx_health_plan\classes\lib\Gallery()
15
	 */
16
	protected static $instance = null;
17
18
	/**
19
	 * The current item ID.
20
	 *
21
	 * @var boolean | int
22
	 */
23
	public $item_id = false;
24
25
	/**
26
	 * The current item post_type used in the custom field retrival..
27
	 *
28
	 * @var boolean | int
29
	 */
30
	public $post_type = false;
31
32
	/**
33
	 * Holds the the default parameters for the gallery output.
34
	 *
35
	 * @var array
36
	 */
37
	public $defaults = array();
38
39
	/**
40
	 * If the current post has a gallery.
41
	 *
42
	 * @var boolean
43
	 */
44
	public $has_gallery = false;
45
46
	/**
47
	 * Holds the array of gallery images.
48
	 *
49
	 * @var array
50
	 */
51
	public $gallery = array();
52
53
	/**
54
	 * Holds the html for the current gallery being output.
55
	 *
56
	 * @var array
57
	 */
58
	public $html = array();
59
60
	/**
61
	 * Holds the parameters for the current gallery being output.
62
	 *
63
	 * @var array
64
	 */
65
	public $args = array();
66
67
	/**
68
	 * Constructor
69
	 */
70
	public function __construct() {
0 ignored issues
show
Expected 2 blank lines before function; 1 found
Loading history...
71
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
72
73
	/**
74
	 * Return an instance of this class.
75
	 *
76
	 * @since 1.0.0
77
	 *
78
	 * @return    object \lsx_health_plan\classes\lib\Gallery()    A single instance of this class.
79
	 */
80
	public static function get_instance() {
81
		// If the single instance hasn't been set, set it now.
82
		if ( null === self::$instance ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
83
			self::$instance = new self();
84
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
85
		return self::$instance;
86
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
87
88
	/**
89
	 * Check if the item has a gallery of images returns true or false.
90
	 *
91
	 * @param  string $item_id
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
92
	 * @param  string $post_type
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
93
	 * @return boolean
94
	 */
95
	public function has_gallery( $item_id = '', $post_type = '' ) {
96
		if ( '' === $item_id ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
97
			$this->item_id = get_the_ID();
98
		} else {
99
			$this->item_id = $item_id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $item_id of type string is incompatible with the declared type boolean|integer of property $item_id.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
100
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
101
		$this->has_gallery = false;
102
		if ( '' === $post_type ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
103
			$this->post_type = get_post_type( $this->item_id );
0 ignored issues
show
Documentation Bug introduced by
It seems like get_post_type($this->item_id) of type string is incompatible with the declared type boolean|integer of property $post_type.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
It seems like $this->item_id can also be of type false and string; however, parameter $post of get_post_type() does only seem to accept WP_Post|integer|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
			$this->post_type = get_post_type( /** @scrutinizer ignore-type */ $this->item_id );
Loading history...
104
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
105
		$gallery = get_post_meta( $this->item_id, $this->post_type . '_gallery', true );
0 ignored issues
show
It seems like $this->item_id can also be of type false and string; however, parameter $post_id of get_post_meta() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
		$gallery = get_post_meta( /** @scrutinizer ignore-type */ $this->item_id, $this->post_type . '_gallery', true );
Loading history...
106
107
		if ( ! empty( $gallery ) && ( '' !== $gallery ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
108
			$this->gallery     = $gallery;
0 ignored issues
show
Documentation Bug introduced by
It seems like $gallery can also be of type string. However, the property $gallery is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
109
			$this->has_gallery = true;
110
			wp_enqueue_script( 'slick', LSX_HEALTH_PLAN_URL . 'assets/js/src/slick.min.js', array( 'jquery' ), LSX_HEALTH_PLAN_VER, true );
111
			wp_enqueue_script( 'lsx-health-plan-slider', LSX_HEALTH_PLAN_URL . 'assets/js/src/lsx-health-plan-slider.js', array( 'slick' ), LSX_HEALTH_PLAN_VER, true );
112
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
113
		return $this->has_gallery;
114
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
115
116
	/**
117
	 * Returns the defaults for the gallery, after grabbing the setting from the item.
118
	 *
119
	 * @param  string $item_id
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
120
	 * @param  string $post_type
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
121
	 * @return array
122
	 */
123
	public function get_defaults( $item_id = '', $post_type = '' ) {
124
		if ( '' === $item_id ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
125
			$item_id = $this->item_id;
126
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
127
		if ( '' === $post_type ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
128
			$post_type = $this->post_type;
129
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
130
		$this->defaults = array(
131
			'columns'   => '3',
132
			'layout'    => 'slider',
133
			'interval'  => false,
134
			'css_class' => false,
135
		);
136
		foreach ( $this->defaults as $key => $default ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
137
			$override = get_post_meta( $item_id, $this->post_type . '_gallery_' . $key, true );
0 ignored issues
show
It seems like $item_id can also be of type boolean and string; however, parameter $post_id of get_post_meta() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

137
			$override = get_post_meta( /** @scrutinizer ignore-type */ $item_id, $this->post_type . '_gallery_' . $key, true );
Loading history...
138
			if ( '' !== $override && false !== $override && ! empty( $override ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
139
				$this->defaults[ $key ] = $override;
140
			}
141
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
142
		return $this->defaults;
143
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
144
145
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$args" missing
Loading history...
146
	 * Gets and returns the gallery html.
147
	 *
148
	 * @param string $item_id
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
149
	 * @param string $post_type
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
150
	 * @return void
0 ignored issues
show
Function return type is void, but function contains return statement
Loading history...
151
	 */
152
	public function get_gallery( $item_id = '', $post_type = '', $args = array() ) {
153
		$return     = '';
154
		$this->html = array();
155
		$this->args = wp_parse_args( $args, $this->get_defaults( $item_id, $post_type ) );
156
		if ( ! empty( $this->gallery ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
157
			$this->args['count'] = 1;
158
			if ( '' !== $post_type ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
159
				$this->args['post_type'] = $post_type;
160
			} else {
161
				$this->args['post_type'] = $this->post_type;
162
			}
163
164
			// output the opening boostrap row divs.
165
			$this->before_loop();
166
167
			foreach ( $this->gallery as $key => $gallery ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
Blank line found at start of control structure
Loading history...
168
169
				$this->loop_start();
170
171
				if ( isset( $gallery['exercise_gallery_image_id'] ) && ! empty( $gallery['exercise_gallery_image_id'] ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
172
					$size         = apply_filters( 'lsx_hp_exercise_gallery_size', 'full' );
173
					$thumbnail    = wp_get_attachment_image( $gallery['exercise_gallery_image_id'], $size );
174
					$this->html[] = $thumbnail;
175
				} elseif ( isset( $gallery['exercise_gallery_external'] ) && ! empty( $gallery['exercise_gallery_external'] ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
176
					$this->html[] = $gallery['exercise_gallery_external']; // WPCS: XSS OK.
177
				} elseif ( isset( $gallery['exercise_gallery_embed'] ) && ! empty( $gallery['exercise_gallery_embed'] ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
178
					$embed_args = array(
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
179
						'width' => '530',
180
					);
181
					$embed        = wp_oembed_get( $gallery['exercise_gallery_embed'], $embed_args );
182
					$this->html[] = str_replace( 'width="530"', 'width="100%"', $embed ); // WPCS: XSS OK.
183
				}
184
185
				$this->loop_end();
186
187
				$this->args['count']++;
188
			}
189
190
			// output the closing boostrap row divs.
191
			$this->after_loop();
192
		}
193
194
		// Join the html output if its not empty.
195
		if ( ! empty( $this->html ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
196
			$return = implode( '', $this->html );
197
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
198
		return $return;
199
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
200
201
	/**
202
	 * Outputs the CSS class for the panels
203
	 *
204
	 * @param string $columns
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Superfluous parameter comment
Loading history...
205
	 * @return string
206
	 */
207
	public function column_class() {
208
		$cols  = 'col-xs-12 col-sm-';
209
		$cols .= '5' === $this->args['columns'] ? '15' : 12 / $this->args['columns'];
210
		return $cols;
211
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
212
213
	/**
214
	 * Runs just after the if and before the while statement in $this->output()
215
	 */
216
	public function before_loop() {
217
		if ( 'slider' === $this->args['layout'] ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
218
			$this->carousel_id = wp_rand( 20, 20000 );
0 ignored issues
show
Bug Best Practice introduced by
The property carousel_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
219
			$this->html[]      = "<div class='lsx-hp-widget-items slick-slider slick-dotted slick-has-arrows {$this->args['css_class']} ' data-interval='{$this->args['interval']}' data-slick='{ \"slidesToShow\": 1, \"slidesToScroll\": 1'>";
220
		} else {
221
			$this->html[] = "<div class='lsx-hp-widget-items widget-item-grid-layout'>";
222
		}
223
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
224
225
	/**
226
	 * Runs at the very end of the loop before it runs again.
227
	 */
228
	public function loop_start() {
229
		// Get the call for the active slide.
230
		if ( 'slider' === $this->args['layout'] ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
231
			$this->html[] = "<div class='lsx-hp-widget-item-wrap lsx-{$this->args['post_type']}'>";
232
		} else {
233
			if ( 1 === $this->args['count'] ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
234
				$this->html[] = "<div class='row'>";
235
			}
0 ignored issues
show
No blank line found after control structure
Loading history...
236
			$this->html[] = '<div class="' . $this->column_class() . '">';
237
		}
238
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
239
240
	/**
241
	 * Runs at the very end of the loop before it runs again.
242
	 */
243
	public function loop_end() {
244
		if ( 'slider' !== $this->args['layout'] ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
245
			$this->html[] = '</div>';
246
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
247
		// Close the current slide panel.
248
		if ( 'slider' === $this->args['layout'] ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
249
			$this->html[] = '</div>';
250
		} elseif ( 0 === $this->args['count'] % $this->args['columns'] || count( $this->gallery ) === $this->args['count'] ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
251
			$this->html[] = '</div>';
252
253
			if ( $this->args['count'] < count( $this->gallery ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
254
				$this->html[] = "<div class='row'>";
255
			}
256
		}
257
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
258
259
	/**
260
	 * Runs just after the if and before the while statement in $this->output()
261
	 */
262
	public function after_loop() {
263
		// Slider output Closing.
264
		if ( 'slider' === $this->args['layout'] ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
265
			$this->html[] = '</div>';
266
		} else {
267
			$this->html[] = '</div>';
268
		}
269
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 0 found
Loading history...
270
}
271