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