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 http://opensource.org/licenses/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' ) ) ) { |
36
|
|
|
// If the file-write fails, fallback to inline |
37
|
|
|
// and cache the failure so we don't try again immediately. |
38
|
|
|
$this->write_file(); |
39
|
|
|
} |
40
|
|
|
add_action( 'customize_save_after', array( $this, 'write_file' ) ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Gets the path of the CSS file and folder in the filesystem. |
45
|
|
|
* |
46
|
|
|
* @access protected |
47
|
|
|
* @since 3.0.0 |
48
|
|
|
* @param string $context Can be "file" or "folder". If empty, returns both as array. |
49
|
|
|
* @return string|array |
50
|
|
|
*/ |
51
|
|
|
protected function get_path( $context = '' ) { |
52
|
|
|
|
53
|
|
|
$upload_dir = wp_upload_dir(); |
54
|
|
|
|
55
|
|
|
$paths = array( |
56
|
|
|
'file' => wp_normalize_path( $upload_dir['basedir'] . '/kirki-css/styles.css' ), |
57
|
|
|
'folder' => wp_normalize_path( $upload_dir['basedir'] . '/kirki-css' ), |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
if ( 'file' === $context ) { |
61
|
|
|
return $paths['file']; |
62
|
|
|
} |
63
|
|
|
if ( 'folder' === $context ) { |
64
|
|
|
return $paths['folder']; |
65
|
|
|
} |
66
|
|
|
return $paths; |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Gets the URL of the CSS file in the filesystem. |
72
|
|
|
* |
73
|
|
|
* @access public |
74
|
|
|
* @since 3.0.0 |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function get_url() { |
78
|
|
|
|
79
|
|
|
$upload_dir = wp_upload_dir(); |
80
|
|
|
return esc_url_raw( $upload_dir['baseurl'] . '/kirki-css/styles.css' ); |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Gets the timestamp of the file. |
86
|
|
|
* This will be used as "version" for cache-busting purposes. |
87
|
|
|
* |
88
|
|
|
* @access public |
89
|
|
|
* @since 3.0.0 |
90
|
|
|
* @return string|bool |
|
|
|
|
91
|
|
|
*/ |
92
|
|
|
public function get_timestamp() { |
93
|
|
|
|
94
|
|
|
if ( file_exists( $this->get_path( 'file' ) ) ) { |
95
|
|
|
return filemtime( $this->get_path( 'file' ) ); |
96
|
|
|
} |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Writes the file to disk. |
102
|
|
|
* |
103
|
|
|
* @access public |
104
|
|
|
* @since 3.0.0 |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function write_file() { |
108
|
|
|
|
109
|
|
|
$css = array(); |
110
|
|
|
$configs = Kirki::$config; |
111
|
|
|
foreach ( $configs as $config_id => $args ) { |
112
|
|
|
// Get the CSS we want to write. |
113
|
|
|
$css[ $config_id ] = apply_filters( "kirki/{$config_id}/dynamic_css", Kirki_Modules_CSS::loop_controls( $config_id ) ); |
114
|
|
|
} |
115
|
|
|
$css = implode( $css, '' ); |
116
|
|
|
|
117
|
|
|
// Minimize the CSS a bit. |
118
|
|
|
$css = str_replace( array( "\n", "\t", "\r\n" ), '', $css ); |
119
|
|
|
$css = str_replace( array( '{ ', '{ ', '{ ' ), '{', $css ); |
120
|
|
|
$css = str_replace( ': ', ':', $css ); |
121
|
|
|
$css = str_replace( array( '; ', '; ', '; ' ), ';', $css ); |
122
|
|
|
$css = explode( '}', $css ); |
123
|
|
|
$css = array_unique( $css ); |
124
|
|
|
$css = implode( $css, '}' ); |
125
|
|
|
|
126
|
|
|
// If the folder doesn't exist, create it. |
127
|
|
|
if ( ! file_exists( $this->get_path( 'folder' ) ) ) { |
128
|
|
|
wp_mkdir_p( $this->get_path( 'folder' ) ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$filesystem = $this->get_filesystem(); |
132
|
|
|
$write_file = (bool) $filesystem->put_contents( $this->get_path( 'file' ), $css ); |
133
|
|
|
if ( ! $write_file ) { |
134
|
|
|
$this->fallback = true; |
135
|
|
|
set_transient( 'kirki_css_write_to_file_failed', true, HOUR_IN_SECONDS ); |
136
|
|
|
} |
137
|
|
|
return $write_file; |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Gets the WP_Filesystem object. |
143
|
|
|
* |
144
|
|
|
* @access protected |
145
|
|
|
* @since 3.0.0 |
146
|
|
|
* @return object |
147
|
|
|
*/ |
148
|
|
|
protected function get_filesystem() { |
149
|
|
|
|
150
|
|
|
// The Wordpress filesystem. |
151
|
|
|
global $wp_filesystem; |
152
|
|
|
|
153
|
|
|
if ( empty( $wp_filesystem ) ) { |
154
|
|
|
require_once wp_normalize_path( ABSPATH . '/wp-admin/includes/file.php' ); |
155
|
|
|
WP_Filesystem(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $wp_filesystem; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.