| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('Access denied.'); |
| 4 |
|
| 5 |
if (!class_exists('WP_Optimize_Utils')) : |
| 6 |
|
| 7 |
class WP_Optimize_Utils { |
| 8 |
|
| 9 |
/** |
| 10 |
* Returns the folder path for log files |
| 11 |
* |
| 12 |
* @return string - Folder path for log files with trailing slash |
| 13 |
*/ |
| 14 |
public static function get_log_folder_path() { |
| 15 |
$upload_base = self::get_base_upload_dir(); |
| 16 |
if (!is_dir($upload_base . 'wpo/logs')) { |
| 17 |
wp_mkdir_p($upload_base . 'wpo/logs'); |
| 18 |
} |
| 19 |
return $upload_base . 'wpo/logs/'; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Generates a log file name based on the given prefix. |
| 24 |
* |
| 25 |
* @param string $prefix The prefix to be added to the log file name. |
| 26 |
* @return string The generated log file name. |
| 27 |
*/ |
| 28 |
public static function get_log_file_name($prefix) { |
| 29 |
return $prefix . '-' . substr(md5(wp_salt()), 0, 20) . '.log'; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Returns the file path for the log file. |
| 34 |
* |
| 35 |
* @param string $prefix The prefix to be added to the log file name. |
| 36 |
* @return string The file path for the log file. |
| 37 |
*/ |
| 38 |
public static function get_log_file_path($prefix) { |
| 39 |
return self::get_log_folder_path() . self::get_log_file_name($prefix); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns WordPress GMT offset in seconds. |
| 44 |
* |
| 45 |
* @return int |
| 46 |
*/ |
| 47 |
public static function get_gmt_offset() { |
| 48 |
$timezone_string = get_option('timezone_string'); |
| 49 |
|
| 50 |
if (!empty($timezone_string)) { |
| 51 |
$timezone = new DateTimeZone($timezone_string); |
| 52 |
$gmt_offset = $timezone->getOffset(new DateTime()); |
| 53 |
} else { |
| 54 |
$gmt_offset_option = get_option('gmt_offset'); |
| 55 |
$gmt_offset = (int) (3600 * $gmt_offset_option); |
| 56 |
} |
| 57 |
|
| 58 |
return $gmt_offset; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Returns the folder path for the upload directory with trailing slash |
| 63 |
* |
| 64 |
* @return string - Folder path for the upload directory with trailing slash |
| 65 |
*/ |
| 66 |
public static function get_base_upload_dir() { |
| 67 |
$upload_dir = wp_upload_dir(); |
| 68 |
return trailingslashit($upload_dir['basedir']); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Get the file path |
| 73 |
* |
| 74 |
* @param string $url |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
public static function get_file_path($url) { |
| 78 |
if (is_multisite()) { |
| 79 |
if (function_exists('get_main_site_id')) { |
| 80 |
$site_id = get_main_site_id(); |
| 81 |
} else { |
| 82 |
$network = get_network(); |
| 83 |
$site_id = $network->site_id; |
| 84 |
} |
| 85 |
switch_to_blog($site_id); |
| 86 |
} |
| 87 |
$upload_dir = wp_upload_dir(); |
| 88 |
$uploads_url = trailingslashit($upload_dir['baseurl']); |
| 89 |
$uploads_dir = trailingslashit($upload_dir['basedir']); |
| 90 |
if (is_multisite()) { |
| 91 |
restore_current_blog(); |
| 92 |
} |
| 93 |
$possible_urls = array( |
| 94 |
WP_CONTENT_URL => WP_CONTENT_DIR, |
| 95 |
WP_PLUGIN_URL => WP_PLUGIN_DIR, |
| 96 |
$uploads_url => $uploads_dir, |
| 97 |
get_template_directory_uri() => get_template_directory(), |
| 98 |
untrailingslashit(includes_url()) => ABSPATH . WPINC, |
| 99 |
); |
| 100 |
$file = ''; |
| 101 |
foreach ($possible_urls as $possible_url => $path) { |
| 102 |
$pos = strpos($url, $possible_url); |
| 103 |
if (0 === $pos) { |
| 104 |
$file = substr_replace($url, $path, $pos, strlen($possible_url)); |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
return $file; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Parse tag attributes and return array with them. |
| 113 |
* |
| 114 |
* @param string $tag |
| 115 |
* @return array |
| 116 |
*/ |
| 117 |
public static function parse_attributes($tag) { |
| 118 |
$attributes = array(); |
| 119 |
|
| 120 |
$_attributes = wp_kses_hair($tag, wp_allowed_protocols()); |
| 121 |
|
| 122 |
if (empty($_attributes)) return $attributes; |
| 123 |
|
| 124 |
foreach ($_attributes as $key => $value) { |
| 125 |
$attributes[$key] = $value['value']; |
| 126 |
} |
| 127 |
|
| 128 |
return $attributes; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Checks whether supplied string is a valid html document or not |
| 133 |
* |
| 134 |
* @param string $html - HTML document as string |
| 135 |
* @return bool |
| 136 |
*/ |
| 137 |
public static function is_valid_html($html) { |
| 138 |
if (is_feed()) return false; |
| 139 |
|
| 140 |
// To prevent issue with `simple_html_dom` class |
| 141 |
// Exit if it doesn't look like HTML |
| 142 |
// https://github.com/rosell-dk/webp-express/issues/228 |
| 143 |
if (!preg_match("#^\\s*<#", $html)) return false; |
| 144 |
|
| 145 |
if ('' === $html) return false; |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Include simple html dom script if not available |
| 151 |
*/ |
| 152 |
public static function maybe_include_simple_html_dom() { |
| 153 |
if (!function_exists('str_get_html')) { |
| 154 |
require_once WPO_PLUGIN_MAIN_PATH . 'vendor/simplehtmldom/simplehtmldom/simple_html_dom.php'; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Returns simplehtmldom\HtmlDocument object |
| 160 |
* |
| 161 |
* @param string $html_buffer - HTML document as string |
| 162 |
* @return simplehtmldom\HtmlDocument | boolean |
| 163 |
*/ |
| 164 |
public static function get_simple_html_dom_object($html_buffer) { |
| 165 |
self::maybe_include_simple_html_dom(); |
| 166 |
return str_get_html($html_buffer, false, false, |
| 167 |
get_option('blog_charset'), false, DEFAULT_BR_TEXT, |
| 168 |
DEFAULT_SPAN_TEXT, false); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
endif; |
| 173 |
|