| @@ -1,64 +1,85 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | namespace Hurrytimer; |
| 3 | 4 | |
| 4 | 5 | use Hurrytimer\Traits\Singleton; |
| 5 | 6 | |
| 6 | -class CSS_Builder{ | |
| 7 | +class CSS_Builder | |
| 8 | +{ | |
| 9 | + use Singleton; | |
| 7 | 10 | |
| 8 | - use Singleton; | |
| 11 | + public function get_build_url() | |
| 12 | + { | |
| 13 | + $hash = $this->get_current_build_hash(); | |
| 9 | 14 | |
| 10 | - const OPTION_NAME = 'hurrytimer_css_build_hash'; | |
| 15 | + $file = $this->get_build_path( $hash ); | |
| 11 | 16 | |
| 12 | - public function generate_css(){ | |
| 17 | + if ( !file_exists( $file ) ) { | |
| 18 | + $hash = $this->build(); | |
| 19 | + } | |
| 20 | + | |
| 21 | + $upload_dir = wp_get_upload_dir(); | |
| 22 | + | |
| 23 | + return set_url_scheme( trailingslashit( $upload_dir[ 'baseurl' ] ) . $this->get_build_relative_path( $hash ) ); | |
| 24 | + } | |
| 25 | + | |
| 26 | + private function get_current_build_hash() | |
| 27 | + { | |
| 28 | + return get_option( 'hurrytimer_css_build_hash' ); | |
| 29 | + } | |
| 30 | + | |
| 31 | + public function delete_build( $hash ) | |
| 32 | + { | |
| 33 | + $file = $this->get_build_path( $hash ); | |
| 34 | + | |
| 35 | + @unlink( $file ); | |
| 36 | + } | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Re/Build CSS file | |
| 40 | + */ | |
| 41 | + public function build() | |
| 42 | + { | |
| 13 | 43 | ob_start(); |
| 14 | - include HURRYT_DIR . '/assets/css/main.css'; | |
| 15 | - include HURRYT_DIR . '/includes/css_template.php'; | |
| 44 | + include HURRYT_DIR . 'assets/css/main.css'; | |
| 45 | + include __DIR__ . '/css_template.php'; | |
| 46 | + | |
| 16 | 47 | $css = ob_get_clean(); |
| 17 | - $css = wp_strip_all_tags($css); | |
| 18 | 48 | |
| 19 | - $uploads = wp_upload_dir(); | |
| 49 | + $this->delete_build( $this->get_current_build_hash() ); | |
| 20 | 50 | |
| 21 | - $uploads_dir = $uploads['basedir']; | |
| 22 | - | |
| 23 | - $path = $uploads_dir . '/hurrytimer/css/'; | |
| 51 | + $hash = substr( md5( time() ), 0, 16 ); | |
| 24 | 52 | |
| 25 | - if( ! file_exists( $path ) ){ | |
| 26 | - wp_mkdir_p( $path ); | |
| 27 | - } | |
| 53 | + $file = $this->get_build_path( $hash ); | |
| 28 | 54 | |
| 29 | - if(!file_exists($uploads_dir . '/hurrytimer/index.html')){ | |
| 30 | - file_put_contents($uploads_dir . '/hurrytimer/index.html', ''); | |
| 31 | - } | |
| 32 | - if(!file_exists($uploads_dir . '/hurrytimer/css/index.html')){ | |
| 33 | - file_put_contents($uploads_dir . '/hurrytimer/css/index.html', ''); | |
| 34 | - } | |
| 35 | - @array_map('unlink', glob($path . "*.css")); | |
| 36 | - $build_version = substr( md5( time() ), 0, 16 ); | |
| 37 | - $css_path = $uploads_dir . '/hurrytimer/css/' . $build_version . '.css'; | |
| 38 | - file_put_contents($css_path, $css); | |
| 55 | + file_put_contents( $file, $css ); | |
| 39 | 56 | |
| 40 | - update_option(self::OPTION_NAME, $build_version); | |
| 57 | + update_option( 'hurrytimer_css_build_hash', $hash ); | |
| 41 | 58 | |
| 42 | - return $css_path; | |
| 59 | + return $hash; | |
| 43 | 60 | } |
| 44 | 61 | |
| 45 | - public function get_css_url(){ | |
| 46 | - $uploads = wp_upload_dir(); | |
| 47 | - $uploads_dir = $uploads['basedir']; | |
| 62 | + private function get_build_relative_path( $hash ) | |
| 63 | + { | |
| 64 | + $uploads_dir = wp_get_upload_dir(); | |
| 48 | 65 | |
| 49 | - $version = get_option(self::OPTION_NAME); | |
| 50 | - if(empty($version)){ | |
| 51 | - $css_path = $this->generate_css(); | |
| 66 | + $relative_dir = implode( DIRECTORY_SEPARATOR, [ 'hurrytimer', 'css' ] ); | |
| 67 | + | |
| 68 | + $path = trailingslashit( $uploads_dir[ 'basedir' ] ) . $relative_dir; | |
| 69 | + | |
| 70 | + if ( !file_exists( $path ) ) { | |
| 71 | + mkdir( $path, 0777, true ); | |
| 52 | 72 | } |
| 53 | - $subpath = '/hurrytimer/css/' . $version . '.css'; | |
| 54 | - $css_path = $uploads_dir . $subpath; | |
| 55 | 73 | |
| 56 | - // Ensure the file exists, otherwise generate it. | |
| 57 | - if( ! file_exists( $css_path ) ){ | |
| 58 | - $css_path = $this->generate_css(); | |
| 59 | - } | |
| 74 | + return apply_filters( 'hurryt_css_path', trailingslashit( $relative_dir ) . $hash . '.css' ); | |
| 75 | + } | |
| 60 | 76 | |
| 61 | - return set_url_scheme( $uploads['baseurl'] . $subpath); | |
| 77 | + public function get_build_path( $hash ) | |
| 78 | + { | |
| 79 | + $upload_dir = wp_get_upload_dir(); | |
| 80 | + | |
| 81 | + return trailingslashit( $upload_dir[ 'basedir' ] ) . $this->get_build_relative_path( $hash ); | |
| 82 | + | |
| 62 | 83 | } |
| 63 | 84 | |
| 64 | 85 | } |