| 1 |
<?php |
| 2 |
namespace Enteraddons\Classes; |
| 3 |
|
| 4 |
/** |
| 5 |
* Enteraddons helper class |
| 6 |
* |
| 7 |
* @package Enteraddons |
| 8 |
* @author ThemeLooks |
| 9 |
* @copyright 2022 ThemeLooks |
| 10 |
* @license GPL-2.0-or-later |
| 11 |
* |
| 12 |
* |
| 13 |
*/ |
| 14 |
if( !defined( 'WPINC' ) ) { |
| 15 |
die; |
| 16 |
} |
| 17 |
|
| 18 |
class Assets_Cache { |
| 19 |
|
| 20 |
const FILEPREFXI = 'enteraddons-'; |
| 21 |
|
| 22 |
protected static $postId; |
| 23 |
|
| 24 |
protected static $widgets; |
| 25 |
|
| 26 |
protected static $upload_path; |
| 27 |
|
| 28 |
protected static $upload_url; |
| 29 |
|
| 30 |
function __construct( $post_id, $widgets = null ) { |
| 31 |
|
| 32 |
self::$postId = $post_id; |
| 33 |
self::$widgets = $widgets; |
| 34 |
// |
| 35 |
$this->getUploadDir(); |
| 36 |
} |
| 37 |
|
| 38 |
public function getUploadDir() { |
| 39 |
|
| 40 |
$upload_dir = wp_upload_dir(); |
| 41 |
self::$upload_path = trailingslashit( $upload_dir['basedir'] ); |
| 42 |
self::$upload_url = trailingslashit( $upload_dir['baseurl'] ); |
| 43 |
} |
| 44 |
|
| 45 |
public static function getCacheDirName() { |
| 46 |
return trailingslashit('enteraddons') . trailingslashit('css'); |
| 47 |
} |
| 48 |
|
| 49 |
public static function getCacheDir() { |
| 50 |
return self::$upload_path . self::getCacheDirName(); |
| 51 |
} |
| 52 |
|
| 53 |
public static function getCacheDirUrl() { |
| 54 |
return self::$upload_url . self::getCacheDirName(); |
| 55 |
} |
| 56 |
|
| 57 |
public static function createCacheFolder() { |
| 58 |
if( !is_dir( self::getCacheDir() ) ) { |
| 59 |
@mkdir( self::getCacheDir() , 0777, true); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public static function getFileName() { |
| 64 |
$post_id = self::$postId; |
| 65 |
return self::getCacheDir() . self::FILEPREFXI . "{$post_id}.css"; |
| 66 |
} |
| 67 |
|
| 68 |
public static function getFileUrl() { |
| 69 |
$post_id = self::$postId; |
| 70 |
// Set global variable |
| 71 |
global $ea_cache_dir_url, $ea_cache_file_prefix; |
| 72 |
$ea_cache_dir_url = self::getCacheDirUrl(); |
| 73 |
$ea_cache_file_prefix = self::FILEPREFXI; |
| 74 |
|
| 75 |
return file_exists( self::getFileName() ) ? self::getCacheDirUrl() . self::FILEPREFXI . "{$post_id}.css" : ''; |
| 76 |
} |
| 77 |
|
| 78 |
public static function deleteCacheFile() { |
| 79 |
if( file_exists( self::getFileName() ) ) { |
| 80 |
unlink( self::getFileName() ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public static function enqueue() { |
| 85 |
if( !self::getFileUrl() ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
$post_id = self::$postId; |
| 90 |
$version = ENTERADDONS_VERSION.'.'.get_post_modified_time(); |
| 91 |
wp_enqueue_style( 'enteraddons-'.$post_id, self::getFileUrl(), array('elementor-frontend'), $version, false ); |
| 92 |
} |
| 93 |
|
| 94 |
public static function putStyle() { |
| 95 |
self::createCacheFolder(); |
| 96 |
file_put_contents( self::getFileName() , self::getStyle( self::$widgets ) ); |
| 97 |
} |
| 98 |
|
| 99 |
public static function getStyle( $filesName , $is_pro = false ) { |
| 100 |
$style = ''; |
| 101 |
foreach( $filesName as $widgetName ) { |
| 102 |
|
| 103 |
$getFile = ENTERADDONS_PLUGIN_MODE == 'DEV' ? "css/{$widgetName}.css" : "min-css/{$widgetName}.min.css"; |
| 104 |
|
| 105 |
$file_path = ENTERADDONS_DIR_PATH."assets/widgets-css/{$getFile}"; |
| 106 |
|
| 107 |
$filePath = apply_filters( 'ea_css_assets_path_inject', $file_path, $widgetName ); |
| 108 |
|
| 109 |
if( is_readable($filePath) ) { |
| 110 |
$style .= file_get_contents($filePath); |
| 111 |
} |
| 112 |
} |
| 113 |
return $style; |
| 114 |
} |
| 115 |
|
| 116 |
} |
| 117 |
|