Kirki_CSS_To_File::get_path()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Writes compiled CSS to a file.
4
 *
5
 * @package     Kirki
6
 * @subpackage  CSS Module
7
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
8
 * @license    https://opensource.org/licenses/MIT
9
 * @since       3.0.0
10
 */
11
12
/**
13
 * Handles writing CSS to a file.
14
 */
15
class Kirki_CSS_To_File {
16
17
	/**
18
	 * Fallback to inline CSS?
19
	 *
20
	 * @access protected
21
	 * @since 3.0.0
22
	 * @var bool
23
	 */
24
	protected $fallback = false;
25
26
	/**
27
	 * Constructor.
28
	 *
29
	 * @access public
30
	 * @since 3.0.0
31
	 */
32
	public function __construct() {
33
34
		// If the file doesn't exist, create it.
35
		if ( ! file_exists( $this->get_path( 'file' ) ) ) {
0 ignored issues
show
Bug introduced by
It seems like $this->get_path('file') can also be of type array; however, parameter $filename of file_exists() does only seem to accept string, 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

35
		if ( ! file_exists( /** @scrutinizer ignore-type */ $this->get_path( 'file' ) ) ) {
Loading history...
36
37
			// If the file-write fails, fallback to inline
38
			// and cache the failure so we don't try again immediately.
39
			$this->write_file();
40
		}
41
		add_action( 'customize_save_after', array( $this, 'write_file' ) );
42
	}
43
44
	/**
45
	 * Gets the path of the CSS file and folder in the filesystem.
46
	 *
47
	 * @access protected
48
	 * @since 3.0.0
49
	 * @param string $context Can be "file" or "folder". If empty, returns both as array.
50
	 * @return string|array
51
	 */
52
	protected function get_path( $context = '' ) {
53
		$upload_dir = wp_upload_dir();
54
		$paths      = array(
55
			'file'   => wp_normalize_path( $upload_dir['basedir'] . '/kirki-css/styles.css' ),
56
			'folder' => wp_normalize_path( $upload_dir['basedir'] . '/kirki-css' ),
57
		);
58
59
		if ( 'file' === $context ) {
60
			return $paths['file'];
61
		}
62
		if ( 'folder' === $context ) {
63
			return $paths['folder'];
64
		}
65
		return $paths;
66
	}
67
68
	/**
69
	 * Gets the URL of the CSS file in the filesystem.
70
	 *
71
	 * @access public
72
	 * @since 3.0.0
73
	 * @return string
74
	 */
75
	public function get_url() {
76
		$upload_dir = wp_upload_dir();
77
		return esc_url_raw( $upload_dir['baseurl'] . '/kirki-css/styles.css' );
78
	}
79
80
	/**
81
	 * Gets the timestamp of the file.
82
	 * This will be used as "version" for cache-busting purposes.
83
	 *
84
	 * @access public
85
	 * @since 3.0.0
86
	 * @return integer|false
87
	 */
88
	public function get_timestamp() {
89
		if ( file_exists( $this->get_path( 'file' ) ) ) {
0 ignored issues
show
Bug introduced by
It seems like $this->get_path('file') can also be of type array; however, parameter $filename of file_exists() does only seem to accept string, 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

89
		if ( file_exists( /** @scrutinizer ignore-type */ $this->get_path( 'file' ) ) ) {
Loading history...
90
			return filemtime( $this->get_path( 'file' ) );
0 ignored issues
show
Bug introduced by
It seems like $this->get_path('file') can also be of type array; however, parameter $filename of filemtime() does only seem to accept string, 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

90
			return filemtime( /** @scrutinizer ignore-type */ $this->get_path( 'file' ) );
Loading history...
91
		}
92
		return false;
93
	}
94
95
	/**
96
	 * Writes the file to disk.
97
	 *
98
	 * @access public
99
	 * @since 3.0.0
100
	 * @return bool
101
	 */
102
	public function write_file() {
103
		$css     = array();
104
		$configs = Kirki::$config;
105
		foreach ( $configs as $config_id => $args ) {
106
			// Get the CSS we want to write.
107
			$css[ $config_id ] = apply_filters( "kirki_{$config_id}_dynamic_css", Kirki_Modules_CSS::loop_controls( $config_id ) );
108
		}
109
		$css = implode( $css, '' );
0 ignored issues
show
Unused Code introduced by
The call to implode() has too many arguments starting with ''. ( Ignorable by Annotation )

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

109
		$css = /** @scrutinizer ignore-call */ implode( $css, '' );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
110
111
		// If the folder doesn't exist, create it.
112
		if ( ! file_exists( $this->get_path( 'folder' ) ) ) {
0 ignored issues
show
Bug introduced by
It seems like $this->get_path('folder') can also be of type array; however, parameter $filename of file_exists() does only seem to accept string, 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

112
		if ( ! file_exists( /** @scrutinizer ignore-type */ $this->get_path( 'folder' ) ) ) {
Loading history...
113
			wp_mkdir_p( $this->get_path( 'folder' ) );
114
		}
115
116
		$filesystem = $this->get_filesystem();
117
		$write_file = (bool) $filesystem->put_contents( $this->get_path( 'file' ), $css );
118
		if ( ! $write_file ) {
119
			$this->fallback = true;
120
			set_transient( 'kirki_css_write_to_file_failed', true, HOUR_IN_SECONDS );
121
		}
122
		return $write_file;
123
	}
124
125
	/**
126
	 * Gets the WP_Filesystem object.
127
	 *
128
	 * @access protected
129
	 * @since 3.0.0
130
	 * @return object
131
	 */
132
	protected function get_filesystem() {
133
134
		// The WordPress filesystem.
135
		global $wp_filesystem;
136
137
		if ( empty( $wp_filesystem ) ) {
138
			require_once wp_normalize_path( ABSPATH . '/wp-admin/includes/file.php' );
139
			WP_Filesystem();
140
		}
141
		return $wp_filesystem;
142
	}
143
}
144