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' ) ) ) { |
|
|
|
|
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' ) ) ) { |
|
|
|
|
90
|
|
|
return filemtime( $this->get_path( 'file' ) ); |
|
|
|
|
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, '' ); |
|
|
|
|
110
|
|
|
|
111
|
|
|
// If the folder doesn't exist, create it. |
112
|
|
|
if ( ! file_exists( $this->get_path( 'folder' ) ) ) { |
|
|
|
|
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
|
|
|
|