2323class SiteOrigin_CSS {
2424 private $ theme ;
2525 private $ snippet_paths ;
26+ private $ css_file ;
2627
2728 function __construct () {
2829 $ this ->theme = basename ( get_template_directory () );
@@ -89,13 +90,52 @@ static function single() {
8990 */
9091 function get_custom_css ( $ theme , $ post_id = null ) {
9192 $ css_key = 'siteorigin_custom_css[ ' . $ theme . '] ' ;
92- if ( empty ( $ post_id ) ) {
93- return get_option ( $ css_key , '' );
93+ if ( empty ( $ post_id ) && WP_Filesystem () ) {
94+ $ custom_css_file = apply_filters ( 'siteorigin_custom_css_file ' , false );
95+ if (
96+ ! empty ( $ custom_css_file ) &&
97+ ! empty ( $ custom_css_file ['file ' ] )
98+ ) {
99+ // Did we previously load the CSS file? If not, load it.
100+ if ( empty ( $ this ->css_file ) || isset ( $ _POST ['siteorigin_custom_css ' ] ) ) {
101+ global $ wp_filesystem ;
102+
103+ // If custom file doesn't exist, create it.
104+ if ( ! $ wp_filesystem ->exists ( $ custom_css_file ['file ' ] ) ) {
105+ $ wp_filesystem ->touch ( $ custom_css_file ['file ' ] );
106+ }
107+
108+ if ( empty ( get_option ( 'siteorigin_custom_file ' ) ) ) {
109+ update_option ( 'siteorigin_custom_file ' , true , true );
110+ }
111+
112+ if ( $ wp_filesystem ->is_writable ( $ custom_css_file ['file ' ] ) ) {
113+ $ this ->css_file = $ wp_filesystem ->get_contents ( $ custom_css_file ['file ' ] );
114+ }
115+ }
116+ return $ this ->css_file ;
117+ } elseif ( ! empty ( get_option ( 'siteorigin_custom_file ' ) ) ) {
118+ // If the custom file filter was previously active we need to
119+ // generate the global CSS file to avoid no CSS outputting
120+ // without modification.
121+ delete_option ( 'siteorigin_custom_file ' , true );
122+ $ css_file_path = $ this ->get_css_file_name ( $ theme );
123+
124+ global $ wp_filesystem ;
125+ $ wp_filesystem ->put_contents (
126+ $ css_file_path ,
127+ get_option ( $ css_key , '' )
128+ );
129+ }
130+ }
131+
132+ if ( ! empty ( $ post_id ) ) {
133+ return get_post_meta ( $ post_id , $ css_key , true );
94134 }
95135
96- return get_post_meta ( $ post_id , $ css_key , true );
136+ return get_option ( $ css_key , '' );
97137 }
98-
138+
99139 /**
100140 * Save custom CSS for a given theme and post id combination.
101141 *
@@ -122,7 +162,26 @@ function save_custom_css( $custom_css, $theme, $post_id = null ) {
122162
123163 return add_post_meta ( $ post_id , $ css_key , $ custom_css );
124164 }
125-
165+
166+ /**
167+ * Returns the file name of the CSS file we're editing.
168+ *
169+ * @param $theme
170+ * @param null $post_id
171+ */
172+ function get_css_file_name ( $ theme , $ post_id = null ) {
173+ global $ wp_filesystem ;
174+ $ upload_dir = wp_upload_dir ();
175+ $ upload_dir_path = $ upload_dir ['basedir ' ] . '/so-css/ ' ;
176+
177+ if ( ! $ wp_filesystem ->is_dir ( $ upload_dir_path ) ) {
178+ $ wp_filesystem ->mkdir ( $ upload_dir_path );
179+ }
180+
181+ $ css_file_name = 'so-css- ' . $ theme . ( ! empty ( $ post_id ) ? '_ ' . $ post_id : '' );
182+ return $ upload_dir_path . $ css_file_name . '.css ' ;
183+ }
184+
126185 /**
127186 * Save custom CSS for a given theme and post id combination to a file in the uploads directory to allow for caching.
128187 *
@@ -133,20 +192,23 @@ function save_custom_css( $custom_css, $theme, $post_id = null ) {
133192 function save_custom_css_file ( $ custom_css , $ theme , $ post_id = null ) {
134193 if ( WP_Filesystem () ) {
135194 global $ wp_filesystem ;
136- $ upload_dir = wp_upload_dir ();
137- $ upload_dir_path = $ upload_dir ['basedir ' ] . '/so-css/ ' ;
138-
139- if ( ! $ wp_filesystem ->is_dir ( $ upload_dir_path ) ) {
140- $ wp_filesystem ->mkdir ( $ upload_dir_path );
141- }
142-
143- $ css_file_name = 'so-css- ' . $ theme . ( ! empty ( $ post_id ) ? '_ ' . $ post_id : '' );
144- $ css_file_path = $ upload_dir_path . $ css_file_name . '.css ' ;
145-
146- if ( file_exists ( $ css_file_path ) ) {
147- $ wp_filesystem ->delete ( $ css_file_path );
195+ $ css_file_path = apply_filters ( 'siteorigin_custom_css_file ' , false );
196+
197+ if (
198+ empty ( $ css_file_path ) ||
199+ empty ( $ css_file_path ['file ' ] ) ||
200+ ! $ wp_filesystem ->is_writable ( $ css_file_path ['file ' ] )
201+ ) {
202+ $ css_file_path = $ this ->get_css_file_name ( $ theme , $ post_id );
203+
204+ if ( file_exists ( $ css_file_path ) ) {
205+ $ wp_filesystem ->delete ( $ css_file_path );
206+ }
207+ } else {
208+ $ css_file_path = $ css_file_path ['file ' ];
209+ $ this ->css_file = $ custom_css ;
148210 }
149-
211+
150212 $ wp_filesystem ->put_contents (
151213 $ css_file_path ,
152214 $ custom_css
@@ -227,21 +289,32 @@ function enqueue_css() {
227289 *
228290 */
229291 function enqueue_custom_css ( $ theme , $ post_id = null ) {
230-
231- $ upload_dir = wp_upload_dir ();
232- $ upload_dir_path = $ upload_dir ['basedir ' ] . '/so-css/ ' ;
233-
234292 $ css_id = $ theme . ( ! empty ( $ post_id ) ? '_ ' . $ post_id : '' );
235- $ css_file_name = 'so-css- ' . $ css_id ;
236- $ css_file_path = $ upload_dir_path . $ css_file_name . '.css ' ;
237-
238- if ( empty ( $ _GET ['so_css_preview ' ] ) && ! is_admin () && file_exists ( $ css_file_path ) ) {
239- wp_enqueue_style (
240- 'so-css- ' . $ css_id ,
241- set_url_scheme ( $ upload_dir ['baseurl ' ] . '/so-css/ ' . $ css_file_name . '.css ' ),
242- array (),
243- $ this ->get_latest_revision_timestamp ()
244- );
293+ if (
294+ empty ( $ _GET ['so_css_preview ' ] ) &&
295+ ! is_admin () &&
296+ apply_filters ( 'siteorigin_css_enqueue_css ' , true )
297+ ) {
298+ $ custom_css_file = apply_filters ( 'siteorigin_custom_css_file ' , array () );
299+ if ( ! empty ( $ post_id ) || empty ( $ custom_css_file ) ) {
300+ $ upload_dir = wp_upload_dir ();
301+ $ upload_dir_path = $ upload_dir ['basedir ' ] . '/so-css/ ' ;
302+ $ css_file_name = 'so-css- ' . $ css_id ;
303+ $ css_file_path = $ upload_dir_path . $ css_file_name . '.css ' ;
304+ $ css_file_url = $ upload_dir ['baseurl ' ] . '/so-css/ ' . $ css_file_name . '.css ' ;
305+ } elseif ( isset ( $ custom_css_file ['url ' ] ) ) {
306+ $ css_file_path = $ custom_css_file ['file ' ];
307+ $ css_file_url = $ custom_css_file ['url ' ];
308+ }
309+
310+ if ( ! empty ( $ css_file_path ) && file_exists ( $ css_file_path ) ) {
311+ wp_enqueue_style (
312+ 'so-css- ' . $ css_id ,
313+ set_url_scheme ( $ css_file_url ),
314+ array (),
315+ $ this ->get_latest_revision_timestamp ()
316+ );
317+ }
245318 } else {
246319 $ custom_css = $ this ->get_custom_css ( $ theme , $ post_id );
247320 // We just need to enqueue a dummy style
@@ -273,8 +346,12 @@ function action_admin_menu() {
273346 $ custom_css = self ::sanitize_css ( filter_input ( INPUT_POST , 'siteorigin_custom_css ' ) );
274347 $ socss_post_id = filter_input ( INPUT_GET , 'socss_post_id ' , FILTER_VALIDATE_INT );
275348
276- $ current = $ this ->get_custom_css ( $ this ->theme , $ socss_post_id );
277- $ this ->save_custom_css ( $ custom_css , $ this ->theme , $ socss_post_id );
349+ if ( empty ( $ this ->css_file ) ) {
350+ $ current = $ this ->get_custom_css ( $ this ->theme , $ socss_post_id );
351+ $ this ->save_custom_css ( $ custom_css , $ this ->theme , $ socss_post_id );
352+ } else {
353+ $ current = $ this ->css_file ;
354+ }
278355
279356 // If this has changed, then add a revision.
280357 if ( $ current != $ custom_css ) {
@@ -594,9 +671,13 @@ function admin_action_save_css() {
594671 if ( current_user_can ( 'edit_theme_options ' ) && isset ( $ _POST ['css ' ] ) ) {
595672 // Sanitize CSS input. Should keep most tags, apart from script and style tags.
596673 $ custom_css = self ::sanitize_css ( stripslashes ( $ _POST ['css ' ] ) );
597-
598- $ current = $ this ->get_custom_css ( $ this ->theme );
599- $ this ->save_custom_css ( $ custom_css , $ this ->theme );
674+
675+ if ( empty ( $ this ->css_file ) ) {
676+ $ current = $ this ->get_custom_css ( $ this ->theme );
677+ $ this ->save_custom_css ( $ custom_css , $ this ->theme );
678+ } else {
679+ $ current = $ this ->css_file ;
680+ }
600681
601682 // If this has changed, then add a revision.
602683 if ( $ current != $ custom_css ) {
0 commit comments