Completed
Push — master ( e47c17...035b95 )
by
unknown
04:49
created

Assets::load()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 7
eloc 20
nc 18
nop 0
dl 0
loc 34
rs 6.7272
c 3
b 0
f 1
1
<?php
2
/**
3
 * Front End Assets
4
 *
5
 * @package SimpleCalendar;
6
 */
7
namespace SimpleCalendar;
8
9
use SimpleCalendar\Abstracts\Calendar_View;
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * Front end scripts and styles.
17
 *
18
 * Loads scripts and styles based on the requested calendar view.
19
 *
20
 * @since 3.0.0
21
 */
22
class Assets {
23
24
	/**
25
	 * Load minified assets.
26
	 *
27
	 * @access private
28
	 * @var string
29
	 */
30
	private $min = '.min';
31
32
	/**
33
	 * Scripts.
34
	 *
35
	 * @access private
36
	 * @var array
37
	 */
38
	private $scripts = array();
39
40
	/**
41
	 * Styles.
42
	 *
43
	 * @access private
44
	 * @var array
45
	 */
46
	private $styles = array();
47
48
	/**
49
	 * Disable styles.
50
	 *
51
	 * @access public
52
	 * @var bool
53
	 */
54
	public $disable_styles = false;
55
56
	/**
57
	 * Hook in tabs.
58
	 *
59
	 * @since 3.0.0
60
	 */
61
	public function __construct() {
62
63
		$this->min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG == true ) ? '' : '.min';
64
65
		$settings = get_option( 'simple-calendar_settings_advanced' );
66
67
		if ( isset( $settings['assets']['disable_css'] ) ) {
68
			$this->disable_styles = 'yes' == $settings['assets']['disable_css'] ? true : false;
69
		}
70
71
		add_action( 'init', array( $this, 'register' ), 20 );
72
		add_action( 'init', array( $this, 'enqueue' ), 40 );
73
	}
74
75
	/**
76
	 * Register scripts and styles.
77
	 *
78
	 * @since 3.0.0
79
	 */
80
	public function register() {
81
		do_action( 'simcal_register_assets', $this->min );
82
	}
83
84
	/**
85
	 * Enqueue scripts and styles.
86
	 *
87
	 * @since 3.0.0
88
	 */
89
	public function enqueue() {
90
91
		add_action( 'wp_enqueue_scripts', array( $this, 'load' ), 10 );
92
93
		do_action( 'simcal_enqueue_assets', $this->min );
94
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
95
96
		$min = $this->min;
97
		// Improves compatibility with themes and plugins using Isotope and Masonry.
98
		add_action( 'wp_enqueue_scripts',
99
			function () use ( $min ) {
100
				if ( wp_script_is( 'simcal-qtip', 'enqueued' ) ) {
101
					wp_enqueue_script(
102
						'simplecalendar-imagesloaded',
103
						SIMPLE_CALENDAR_ASSETS . 'js/vendor/imagesloaded.pkgd' . $min . '.js',
104
						array( 'simcal-qtip' ),
105
						'3.1.8',
106
						true
107
					);
108
				}
109
			}, 1000 );
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
110
	}
111
112
	/**
113
	 * Load scripts and styles.
114
	 *
115
	 * @since 3.0.0
116
	 */
117
	public function load() {
118
119
		$types = simcal_get_calendar_types();
120
121
		foreach ( $types as $calendar => $views ) {
122
			foreach( $views as $key => $view ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
123
124
				$view = simcal_get_calendar_view( 0, $calendar . '-' . $view );
125
126
				$scripts[] = $view->scripts( $this->min );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$scripts was never initialized. Although not strictly required by PHP, it is generally a good practice to add $scripts = 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...
127
				$styles[] = $view->styles( $this->min );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$styles was never initialized. Although not strictly required by PHP, it is generally a good practice to add $styles = 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...
128
			}
129
		}
130
131
		$this->get_widgets_assets();
132
		$this->scripts = apply_filters( 'simcal_front_end_scripts', $scripts, $this->min );
0 ignored issues
show
Bug introduced by
The variable $scripts does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
133
		// First check if there is a multi-dimensional array of scripts
134
		if ( isset( $this->scripts[0] ) ) {
135
			foreach ( $this->scripts as $script ) {
136
				$this->load_scripts ( $script );
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
137
			}
138
		} else {
139
			$this->load_scripts( $this->scripts );
140
		}
141
		$this->styles = apply_filters( 'simcal_front_end_styles', $styles, $this->min );
0 ignored issues
show
Bug introduced by
The variable $styles does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
142
		// First check if there is a multi-dimensional array of styles
143
		if ( isset( $this->styles[0] ) ) {
144
			foreach( $this->styles as $style ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
145
				$this->load_styles( $style );
146
			}
147
		} else {
148
			$this->load_styles( $this->styles );
149
		}
150
	}
151
152
	/**
153
	 * Get widgets assets.
154
	 *
155
	 * @since 3.0.0
156
	 */
157
	public function get_widgets_assets() {
158
159
		$widgets = get_option( 'widget_gce_widget' );
160
161
		if ( ! empty( $widgets ) && is_array( $widgets ) ) {
162
163
			foreach ( $widgets as $settings ) {
164
165
				if ( ! empty( $settings ) && is_array( $settings ) ) {
166
167
					if ( isset( $settings['calendar_id'] ) ) {
168
169
						$view = simcal_get_calendar_view( absint( $settings['calendar_id'] ) );
170
171
						if ( $view instanceof Calendar_View ) {
172
							add_filter( 'simcal_front_end_scripts', function ( $scripts, $min ) use ( $view ) {
173
								return array_merge( $scripts, $view->scripts( $min ) );
174
							}, 100, 2 );
175
							add_filter( 'simcal_front_end_styles', function ( $styles, $min ) use ( $view ) {
176
								return array_merge( $styles, $view->styles( $min ) );
177
							}, 100, 2 );
178
						}
179
180
					}
181
182
				}
183
			}
184
185
		}
186
	}
187
188
	/**
189
	 * Scripts.
190
	 *
191
	 * @since 3.0.0
192
	 *
193
	 * @param array $scripts
194
	 */
195
	public function load_scripts( $scripts ) {
196
197
		// Only load if not disabled in the settings
198
		if ( ! empty( $scripts ) && is_array( $scripts ) ) {
199
200
			foreach ( $scripts as $script => $v ) {
201
202
				if ( ! empty( $v['src'] ) ) {
203
204
					$src        = esc_url( $v['src'] );
205
					$deps       = isset( $v['deps'] )        ? $v['deps']       : array();
206
					$ver        = isset( $v['ver'] )         ? $v['ver']        : SIMPLE_CALENDAR_VERSION;
207
					$in_footer  = isset( $v['in_footer'] )   ? $v['in_footer']  : false;
208
209
					wp_enqueue_script( $script, $src, $deps, $ver, $in_footer );
210
211
					if ( ! empty( $v['localize'] ) && is_array( $v['localize'] ) ) {
212
						foreach ( $v['localize'] as $object => $l10n ) {
213
							wp_localize_script( $script, $object, $l10n );
214
						}
215
					}
216
217
				} elseif ( is_string( $v ) && ! empty( $v ) ) {
218
219
					wp_enqueue_script( $v );
220
				}
221
			}
222
223
		}
224
	}
225
226
	/**
227
	 * Styles.
228
	 *
229
	 * @since 3.0.0
230
	 *
231
	 * @param array $styles
232
	 */
233
	public function load_styles( $styles ) {
234
235
		// Only load if not disabled in the settings
236
		if ( ! empty( $styles ) && is_array( $styles ) && false === $this->disable_styles ) {
237
238
			foreach ( $styles as $style => $v ) {
239
240
				if ( ! empty( $v['src'] ) ) {
241
242
					$src    = esc_url( $v['src'] );
243
					$deps   = isset( $v['deps'] )   ? $v['deps']    : array();
244
					$ver    = isset( $v['ver'] )    ? $v['ver']     : SIMPLE_CALENDAR_VERSION;
245
					$media  = isset( $v['media'] )  ? $v['media']   : 'all';
246
247
					wp_enqueue_style( $style, $src, $deps, $ver, $media );
248
249
				} elseif ( is_string( $v ) && ! empty( $v ) ) {
250
251
					wp_enqueue_style( $v );
252
				}
253
254
			}
255
256
		}
257
	}
258
}
259