ehssl-utils.php
160 lines
| 1 | <?php |
| 2 | |
| 3 | class EHSSL_Utils |
| 4 | { |
| 5 | public static function httpsrdrctn_force_https_embeds($content) |
| 6 | { |
| 7 | $files_to_check = array('jpg', 'jpeg', 'gif', 'png', 'js', 'css'); |
| 8 | |
| 9 | if (strpos(home_url(), 'https') !== false) { |
| 10 | $http_domain = str_replace('https', 'http', home_url()); |
| 11 | } else { |
| 12 | $http_domain = home_url(); |
| 13 | } |
| 14 | |
| 15 | $matches = array(); |
| 16 | $pattern = '/' . str_replace('/', '\/', quotemeta($http_domain)) . '\/[^\"\']*\.[' . implode("|", $files_to_check) . ']+/'; |
| 17 | |
| 18 | preg_match_all($pattern, $content, $matches); |
| 19 | |
| 20 | if (!empty($matches)) { |
| 21 | foreach ($matches[0] as $match) { |
| 22 | $match_https = str_replace('http', 'https', $match); |
| 23 | $content = str_replace($match, $match_https, $content); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | return $content; |
| 28 | } |
| 29 | |
| 30 | public static function redirect_to_url( $url, $delay = '0', $exit = '1' ) { |
| 31 | $url = apply_filters( 'wpec_before_redirect_to_url', $url ); |
| 32 | if ( empty( $url ) ) { |
| 33 | echo '<strong>'; |
| 34 | _e( 'Error! The URL value is empty. Please specify a correct URL value to redirect to!', 'wp-express-checkout' ); |
| 35 | echo '</strong>'; |
| 36 | exit; |
| 37 | } |
| 38 | if ( ! headers_sent() ) { |
| 39 | header( 'Location: ' . $url ); |
| 40 | } else { |
| 41 | echo '<meta http-equiv="refresh" content="' . $delay . ';url=' . $url . '" />'; |
| 42 | } |
| 43 | |
| 44 | if ( $exit == '1' ) {//exit |
| 45 | exit; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * Get the current domain name. Example: example.com |
| 51 | */ |
| 52 | public static function get_domain(){ |
| 53 | // Get the home URL |
| 54 | $home_url = get_home_url(); |
| 55 | |
| 56 | // Parse the URL to extract components |
| 57 | $parsed_url = parse_url($home_url); |
| 58 | |
| 59 | // Get the host part of the URL |
| 60 | $domain = isset($parsed_url['host']) ? $parsed_url['host'] : ''; |
| 61 | |
| 62 | return $domain; |
| 63 | } |
| 64 | |
| 65 | //returns www domain if passed domain is without www |
| 66 | //other wise return without domain |
| 67 | public static function get_domain_variant($domain) |
| 68 | { |
| 69 | // Check if the domain starts with 'www.' |
| 70 | if (substr($domain, 0, 4) === 'www.') { |
| 71 | // Remove 'www.' from the domain |
| 72 | return substr($domain, 4); |
| 73 | } else { |
| 74 | // Add 'www.' to the domain |
| 75 | return 'www.' . $domain; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | |
| 80 | public static function is_domain_accessible($url) { |
| 81 | $ch = curl_init($url); |
| 82 | curl_setopt($ch, CURLOPT_HEADER, true); // we want headers |
| 83 | curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body |
| 84 | curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); |
| 85 | curl_setopt($ch, CURLOPT_TIMEOUT,10); |
| 86 | $output = curl_exec($ch); |
| 87 | $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 88 | curl_close($ch); |
| 89 | |
| 90 | return ($httpcode >= 200 && $httpcode < 400); |
| 91 | } |
| 92 | |
| 93 | public static function parse_timestamp( $timestamp ) { |
| 94 | $timestamp = intval($timestamp); |
| 95 | |
| 96 | $timezone_string = new DateTimeZone(wp_timezone_string()); |
| 97 | |
| 98 | $dateTime = new DateTime(); |
| 99 | $dateTime->setTimestamp( $timestamp ); |
| 100 | $dateTime->setTimezone( $timezone_string ); |
| 101 | |
| 102 | $formatted_date_time = $dateTime->format( get_option('date_format') . ' \a\t ' . get_option( 'time_format' ) ); |
| 103 | |
| 104 | return $formatted_date_time; |
| 105 | } |
| 106 | |
| 107 | public static function parse_date( $timestamp ) { |
| 108 | $timestamp = intval($timestamp); |
| 109 | |
| 110 | $timezone_string = new DateTimeZone(wp_timezone_string()); |
| 111 | |
| 112 | $dateTime = new DateTime(); |
| 113 | $dateTime->setTimestamp( $timestamp ); |
| 114 | $dateTime->setTimezone( $timezone_string ); |
| 115 | |
| 116 | $formatted_date_time = $dateTime->format( get_option('date_format') ); |
| 117 | |
| 118 | return $formatted_date_time; |
| 119 | } |
| 120 | |
| 121 | public static function parse_time( $timestamp ) { |
| 122 | $timestamp = intval($timestamp); |
| 123 | |
| 124 | $timezone_string = new DateTimeZone(wp_timezone_string()); |
| 125 | |
| 126 | $dateTime = new DateTime(); |
| 127 | $dateTime->setTimestamp( $timestamp ); |
| 128 | $dateTime->setTimezone( $timezone_string ); |
| 129 | |
| 130 | $formatted_date_time = $dateTime->format( get_option('time_format') ); |
| 131 | |
| 132 | return $formatted_date_time; |
| 133 | } |
| 134 | |
| 135 | public static function get_missing_extensions() { |
| 136 | $required_extensions = array('curl', 'openssl'); |
| 137 | |
| 138 | $missing_extensions = array(); |
| 139 | foreach ($required_extensions as $extension){ |
| 140 | if (! extension_loaded($extension)){ |
| 141 | $missing_extensions[] = $extension; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return $missing_extensions; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /*** |
| 150 | * Test Code |
| 151 | */ |
| 152 | /* |
| 153 | echo httpsrdrctn_force_https_embeds( '<img src="http://test.com/image.jpg" /> |
| 154 | <a href="http://test.com/image.jpeg"></a> |
| 155 | "http://test.com/image.gif" |
| 156 | <img src="http://test.com/image.png" /> |
| 157 | <link rel="javascript" type="text/javascript" href="http://test.com/somescript.js" /> |
| 158 | <link rel="stylesheet" type="text/css" href="http://test.com/somestyles.css" /> |
| 159 | http://test.com/somescript.bmp' ); |
| 160 | */ |