PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.3.1
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.3.1
2.15.0 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 All 62 releases
hurrytimer / includes / CSS_Builder.php

CSS_Builder.php in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce 2.3.1, at includes/CSS_Builder.php

93 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hurrytimer;
4
5 use Hurrytimer\Traits\Singleton;
6 use \MatthiasMullie\Minify;
7
8 class CSS_Builder
9 {
10 use Singleton;
11
12 public function get_build_url()
13 {
14 $hash = $this->get_current_build_hash();
15
16 $file = $this->get_build_path( $hash );
17
18 if ( !file_exists( $file ) ) {
19 $hash = $this->build();
20 }
21
22 $upload_dir = wp_get_upload_dir();
23
24 return set_url_scheme( trailingslashit( $upload_dir[ 'baseurl' ] ) . $this->get_build_relative_path( $hash ) );
25 }
26
27 private function get_current_build_hash()
28 {
29 return get_option( 'hurrytimer_css_build_hash' );
30 }
31
32 public function delete_build( $hash )
33 {
34 $file = $this->get_build_path( $hash );
35
36 @unlink( $file );
37 }
38
39 /**
40 * Re/Build CSS file
41 */
42 public function build()
43 {
44 ob_start();
45 include HURRYT_DIR . 'assets/css/main.css';
46 include __DIR__ . '/css_template.php';
47
48 $css = ob_get_clean();
49
50 $this->delete_build( $this->get_current_build_hash() );
51
52 $hash = substr( md5( time() ), 0, 16 );
53
54 $file = $this->get_build_path( $hash );
55
56 if ( filter_var( apply_filters( 'hurryt_minify_css', false ), FILTER_VALIDATE_BOOLEAN ) ) {
57 $minifier = new Minify\CSS();
58 $minifier->add( $css );
59 $minifier->minify( $file );
60
61 } else {
62 file_put_contents( $file, $css );
63 }
64
65 update_option( 'hurrytimer_css_build_hash', $hash );
66
67 return $hash;
68 }
69
70 private function get_build_relative_path( $hash )
71 {
72 $uploads_dir = wp_get_upload_dir();
73
74 $relative_dir = implode( DIRECTORY_SEPARATOR, [ 'hurrytimer', 'css' ] );
75
76 $path = trailingslashit( $uploads_dir[ 'basedir' ] ) . $relative_dir;
77
78 if ( !file_exists( $path ) ) {
79 mkdir( $path, 0777, true );
80 }
81
82 return apply_filters( 'hurryt_css_path', trailingslashit( $relative_dir ) . $hash . '.css' );
83 }
84
85 public function get_build_path( $hash )
86 {
87 $upload_dir = wp_get_upload_dir();
88
89 return trailingslashit( $upload_dir[ 'basedir' ] ) . $this->get_build_relative_path( $hash );
90
91 }
92
93 }