| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FirePlugins Framework |
| 4 | - * @version 1.0.27 | |
| 4 | + * @version 1.2.6 | |
| 5 | 5 | * |
| 6 | 6 | * @author FirePlugins <[email protected]> |
| 7 | 7 | * @link https://www.fireplugins.com |
| 8 | - * @copyright Copyright © 2022 FirePlugins All Rights Reserved | |
| 8 | + * @copyright Copyright © 2026 FirePlugins All Rights Reserved | |
| 9 | 9 | * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | namespace FPFramework\Base; |
| @@ -17,8 +17,127 @@ | ||
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | class Functions |
| 20 | 20 | { |
| 21 | + /** | |
| 22 | + * Applies the site's or the user's timezone to a given date. | |
| 23 | + * | |
| 24 | + * @param string $date | |
| 25 | + * @param string $format | |
| 26 | + * | |
| 27 | + * @return string | |
| 28 | + */ | |
| 29 | + public static function applySiteTimezoneToUTCDate($date, $format = 'Y-m-d H:i:s') | |
| 30 | + { | |
| 31 | + // Get timezone | |
| 32 | + $tz = new \DateTimeZone(wp_timezone()->getName()); | |
| 33 | + | |
| 34 | + // Apply server timezone | |
| 35 | + $date = (new \DateTime($date))->setTimezone($tz)->format($format); | |
| 36 | + | |
| 37 | + return $date; | |
| 38 | + } | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * Return's a URL with the Google Analytics Campaign Parameters appended to the end | |
| 42 | + * | |
| 43 | + * @param string $url The URL The URL to which UTM parameters will be applied to | |
| 44 | + * @param string $campaign Campaign Name The campaign name i.e. black friday sale, freeversion | |
| 45 | + * @param string $medium Campaign Medium How did the visitor find your link? i.e. social, email, cpc, upgradebutton | |
| 46 | + * @param string $content Campaign Content The call to action that brought the traffic, i.e. buy now, support, read more | |
| 47 | + * | |
| 48 | + * @return string | |
| 49 | + */ | |
| 50 | + public static function getUTMURL($url, $campaign = null, $medium = 'upgradebutton', $content = '') | |
| 51 | + { | |
| 52 | + if (!$url) | |
| 53 | + { | |
| 54 | + return; | |
| 55 | + } | |
| 56 | + | |
| 57 | + if (!$campaign) | |
| 58 | + { | |
| 59 | + $campaign = 'firebox-' . (FBOX_LICENSE_TYPE === 'lite' ? 'free' : 'pro'); | |
| 60 | + } | |
| 61 | + | |
| 62 | + // Make UTM Campaign filterable | |
| 63 | + $campaign = apply_filters('fpframework/utmurl/campaign', $campaign); | |
| 64 | + | |
| 65 | + $utm = 'utm_source=product&utm_campaign=' . $campaign . '&utm_medium=' . $medium; | |
| 66 | + | |
| 67 | + if ($content) | |
| 68 | + { | |
| 69 | + $utm .= '&utm_content=' . $content; | |
| 70 | + } | |
| 71 | + | |
| 72 | + $char = strpos($url, '?') === false ? '?' : '&'; | |
| 73 | + | |
| 74 | + // Check if the URL has a hashtag and something, add it at the end | |
| 75 | + if (strpos($url, '#') !== false) | |
| 76 | + { | |
| 77 | + $url_parts = explode('#', $url); | |
| 78 | + $url = $url_parts[0] . $char . $utm . '#' . $url_parts[1]; | |
| 79 | + } | |
| 80 | + else | |
| 81 | + { | |
| 82 | + $url .= $char . $utm; | |
| 83 | + } | |
| 84 | + | |
| 85 | + return $url; | |
| 86 | + } | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * Attempt to convert a subject to array | |
| 90 | + * | |
| 91 | + * @param mixed $subject | |
| 92 | + * | |
| 93 | + * @return array | |
| 94 | + */ | |
| 95 | + public static function makeArray($subject) | |
| 96 | + { | |
| 97 | + if (empty($subject)) | |
| 98 | + { | |
| 99 | + return []; | |
| 100 | + } | |
| 101 | + | |
| 102 | + if (is_object($subject)) | |
| 103 | + { | |
| 104 | + return (array) $subject; | |
| 105 | + } | |
| 106 | + | |
| 107 | + if (!is_array($subject)) | |
| 108 | + { | |
| 109 | + // replace newlines with commas | |
| 110 | + $subject = str_replace(PHP_EOL, ',', $subject); | |
| 111 | + | |
| 112 | + // split keywords on commas | |
| 113 | + $subject = explode(',', $subject); | |
| 114 | + } | |
| 115 | + | |
| 116 | + // Now that we have an array, run some housekeeping. | |
| 117 | + $arr = $subject; | |
| 118 | + | |
| 119 | + // Trim entries | |
| 120 | + $arr = array_map(function($value) | |
| 121 | + { | |
| 122 | + return trim((string) $value); | |
| 123 | + }, $subject); | |
| 124 | + | |
| 125 | + // Remove empty items. We use a custom callback here because the default behavior of array_filter removes 0 values as well. | |
| 126 | + $arr = array_filter($arr, function($value) | |
| 127 | + { | |
| 128 | + return ($value !== null && $value !== false && $value !== ''); | |
| 129 | + }); | |
| 130 | + | |
| 131 | + // Unique only items | |
| 132 | + $arr = array_unique($arr); | |
| 133 | + | |
| 134 | + // Reset keys | |
| 135 | + $arr = array_values($arr); | |
| 136 | + | |
| 137 | + return $arr; | |
| 138 | + } | |
| 139 | + | |
| 21 | 140 | /** |
| 22 | 141 | * Checks if an array of values (needle) exists in a text (haystack) |
| 23 | 142 | * |
| 24 | 143 | * @param array $needle The searched array of values. |
| @@ -43,6 +162,142 @@ | ||
| 43 | 162 | } |
| 44 | 163 | } |
| 45 | 164 | |
| 46 | 165 | return false; |
| 166 | + } | |
| 167 | + | |
| 168 | + /** | |
| 169 | + * Tries to fix date based on its syntax. Sets it to null if incorrect. | |
| 170 | + * | |
| 171 | + * @param string $date | |
| 172 | + * | |
| 173 | + * @return void | |
| 174 | + */ | |
| 175 | + public static function fixDate(&$date) | |
| 176 | + { | |
| 177 | + if (!$date) | |
| 178 | + { | |
| 179 | + $date = null; | |
| 180 | + | |
| 181 | + return; | |
| 182 | + } | |
| 183 | + | |
| 184 | + $date = trim($date); | |
| 185 | + | |
| 186 | + // Check if date has correct syntax: 00-00-00 00:00:00 | |
| 187 | + if (preg_match('#^[0-9]+-[0-9]+-[0-9]+( [0-9][0-9]:[0-9][0-9]:[0-9][0-9])$#', $date)) | |
| 188 | + { | |
| 189 | + return; | |
| 190 | + } | |
| 191 | + | |
| 192 | + // Check if date has syntax: 00-00-00 00:00 | |
| 193 | + // If so, add :00 (seconds) | |
| 194 | + if (preg_match('#^[0-9]+-[0-9]+-[0-9]+ [0-9][0-9]:[0-9][0-9]$#', $date)) | |
| 195 | + { | |
| 196 | + $date .= ':00'; | |
| 197 | + | |
| 198 | + return; | |
| 199 | + } | |
| 200 | + | |
| 201 | + // Check if date has a prepending date syntax: 00-00-00 ... | |
| 202 | + // If so, add 00:00:00 (hours:mins;secs) | |
| 203 | + if (preg_match('#^([0-9]+-[0-9]+-[0-9]+)#', $date, $match)) | |
| 204 | + { | |
| 205 | + $date = $match[1] . ' 00:00:00'; | |
| 206 | + | |
| 207 | + return; | |
| 208 | + } | |
| 209 | + | |
| 210 | + // Date format is not correct, so return null | |
| 211 | + $date = null; | |
| 212 | + } | |
| 213 | + | |
| 214 | + /** | |
| 215 | + * Change date's timezone to UTC by modyfing the offset | |
| 216 | + * | |
| 217 | + * @param string $date The date in timezone other than UTC | |
| 218 | + * | |
| 219 | + * @return string The date in UTC | |
| 220 | + * | |
| 221 | + * @deprecated Use dateToUTC() | |
| 222 | + */ | |
| 223 | + public static function fixDateOffset(&$date) | |
| 224 | + { | |
| 225 | + $date = self::dateToUTC($date); | |
| 226 | + } | |
| 227 | + | |
| 228 | + /** | |
| 229 | + * Fixes date by adding offset | |
| 230 | + * | |
| 231 | + * @param string $date | |
| 232 | + * | |
| 233 | + * @return string | |
| 234 | + */ | |
| 235 | + public static function dateToUTC($date) | |
| 236 | + { | |
| 237 | + if ($date <= 0) | |
| 238 | + { | |
| 239 | + $date = 0; | |
| 240 | + | |
| 241 | + return; | |
| 242 | + } | |
| 243 | + | |
| 244 | + return get_gmt_from_date($date, 'Y-m-d H:i:s'); | |
| 245 | + } | |
| 246 | + | |
| 247 | + /** | |
| 248 | + * Returns current date | |
| 249 | + * | |
| 250 | + * @return string | |
| 251 | + */ | |
| 252 | + public static function dateTimeNow() | |
| 253 | + { | |
| 254 | + $factory = new \FPFramework\Base\Factory(); | |
| 255 | + return $factory->getDate()->format("Y-m-d H:i:s"); | |
| 256 | + } | |
| 257 | + | |
| 258 | + /** | |
| 259 | + * Checks whether all array values are equal. | |
| 260 | + * | |
| 261 | + * @param array $array | |
| 262 | + * | |
| 263 | + * @return bool | |
| 264 | + */ | |
| 265 | + public static function allArrayValuesEqual($array) | |
| 266 | + { | |
| 267 | + if (count($array) !== self::getTotalNonEmptyArrayValues($array)) | |
| 268 | + { | |
| 269 | + return false; | |
| 270 | + } | |
| 271 | + | |
| 272 | + return count(array_unique($array)) === 1; | |
| 273 | + } | |
| 274 | + | |
| 275 | + /** | |
| 276 | + * Given a one dimension array, it returns the total non empty values. | |
| 277 | + * | |
| 278 | + * @param array $array | |
| 279 | + * | |
| 280 | + * @return int | |
| 281 | + */ | |
| 282 | + public static function getTotalNonEmptyArrayValues($array) | |
| 283 | + { | |
| 284 | + $total = 0; | |
| 285 | + | |
| 286 | + foreach ($array as $value) | |
| 287 | + { | |
| 288 | + if (is_array($value)) | |
| 289 | + { | |
| 290 | + continue; | |
| 291 | + } | |
| 292 | + | |
| 293 | + if ($value === '') | |
| 294 | + { | |
| 295 | + continue; | |
| 296 | + } | |
| 297 | + | |
| 298 | + $total++; | |
| 299 | + } | |
| 300 | + | |
| 301 | + return $total; | |
| 47 | 302 | } |
| 48 | 303 | } |