PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wysija-newsletters/helpers/toolbox.php

https://gitlab.com/Gashler/sg
PHP | 427 lines | 225 code | 58 blank | 144 comment | 58 complexity | a23867999200d0d52ca86df74c245f51 MD5 | raw file
  1. <?php
  2. defined('WYSIJA') or die('Restricted access');
  3. class WYSIJA_help_toolbox extends WYSIJA_object{
  4. function WYSIJA_help_toolbox(){
  5. }
  6. /**
  7. * Get the url of a wysija file based on the filename and the wysija folder
  8. * @param type $filename
  9. * @param type $folder
  10. * @return string
  11. */
  12. function url($filename,$folder='temp'){
  13. $upload_dir = wp_upload_dir();
  14. if(file_exists($upload_dir['basedir'].DS.'wysija')){
  15. $url=$upload_dir['baseurl'].'/wysija/'.$folder.'/'.$filename;
  16. }else{
  17. $url=$upload_dir['baseurl'].'/'.$filename;
  18. }
  19. return $url;
  20. }
  21. function closetags($html) {
  22. #put all opened tags into an array
  23. preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
  24. $openedtags = $result[1]; #put all closed tags into an array
  25. preg_match_all('#</([a-z]+)>#iU', $html, $result);
  26. $closedtags = $result[1];
  27. $len_opened = count($openedtags);
  28. # all tags are closed
  29. if(count($closedtags) === $len_opened) {
  30. return $html;
  31. }
  32. $openedtags = array_reverse($openedtags);
  33. # close tags
  34. for($i=0; $i < $len_opened; $i++) {
  35. if(!in_array($openedtags[$i], $closedtags)){
  36. $html .= '</'.$openedtags[$i].'>';
  37. } else {
  38. unset($closedtags[array_search($openedtags[$i], $closedtags)]);
  39. }
  40. }
  41. return $html;
  42. }
  43. /**
  44. * make an excerpt with a certain number of words
  45. * @param type $text
  46. * @param type $num_words
  47. * @param type $more
  48. * @return type
  49. */
  50. function excerpt($text, $num_words = 8, $more = ' &hellip;'){
  51. return wp_trim_words($text, $num_words, $more);
  52. }
  53. /**
  54. * make a domain name out of a url
  55. * @param type $url
  56. * @return type
  57. */
  58. function _make_domain_name($url=false){
  59. if(!$url) $url=admin_url('admin.php');
  60. $domain_name=str_replace(array('https://','http://','www.'),'',strtolower($url));
  61. //$domain_name=preg_replace(array('#^https?://(www\.)*#i','#^www\.#'),'',$url);
  62. $domain_name=explode('/',$domain_name);
  63. return $domain_name[0];
  64. }
  65. /**
  66. * get base url of the current site or base url of a specific url WITHOUT http, https, www
  67. * @param string $url
  68. * @return string
  69. */
  70. function get_base_uri($url = null) {
  71. $url = !empty($url) ? $url : site_url();
  72. return str_replace(array('https://','http://','www.'),'',strtolower($url));
  73. }
  74. /**
  75. * Detect if this is an internal link, otherwise, it will be an external one
  76. * @param string $url
  77. * @return boolean
  78. */
  79. function is_internal_link($url) {
  80. $str_pos = strpos($this->get_base_uri($url), $this->get_base_uri());
  81. // an internal link must CONTAIN base_uri of the current site and must START with that base_uri
  82. return ($str_pos !== false && $str_pos === 0);
  83. }
  84. /**
  85. * creates a duration string to tell when is the next batch to be processed for instance
  86. * TODO we should add an estimation parameter so that instead of having a precise remaining time 39 minutes left,
  87. * we have something based on the sending frequency : about 45 minutes left becomes about 30 minutes left, becomes about 15 minutes left
  88. * with a 15 minutes sending frequency
  89. * @param int $value_seconds it can be a duration or a timestamp
  90. * @param boolean $entered_value_is_duration
  91. * @param int $number_of_units should I get just on unit days, or should I get 2 units days and hours
  92. * @param int $precision how precise should be the duration calculated, until the second ? or just a number of hours by default in minutes
  93. * @return string
  94. */
  95. function duration_string($value_seconds, $entered_value_is_duration=false, $number_of_units=1, $precision=4){
  96. if($entered_value_is_duration){
  97. // the time entered is already a duration
  98. $duration = $value_seconds;
  99. }else{
  100. // the time entered is a unix time
  101. $duration = time() - $value_seconds;
  102. }
  103. //Clever Maths
  104. $array_duration = $this->convert_seconds_to_array($duration);
  105. // when we don't show the seconds in our result we need to add one minute to the min key
  106. if($precision == 4){
  107. $array_duration['mins'] ++;
  108. }
  109. // Display for date, can be modified more to take the S off
  110. $str = '';
  111. $current_level = 0;
  112. if ($current_level < $number_of_units && $array_duration['years'] >= 1) { $str .= sprintf(_n( '%1$s year', '%1$s years', $array_duration['years'], WYSIJA ),$array_duration['years']).' ';$current_level++; }
  113. if ($precision >0 && $current_level < $number_of_units && $array_duration['weeks'] >= 1) { $str .= sprintf(_n( '%1$s week', '%1$s weeks', $array_duration['weeks'], WYSIJA ),$array_duration['weeks']).' ';$current_level++; }
  114. if ($precision >1 && $current_level < $number_of_units && $array_duration['days'] >= 1) { $str .= sprintf(_n( '%1$s day', '%1$s days', $array_duration['days'], WYSIJA ),$array_duration['days']).' ';$current_level++; }
  115. if ($precision >2 && $current_level < $number_of_units && $array_duration['hours'] >= 1) { $str .= sprintf(_n( '%1$s hour', '%1$s hours', $array_duration['hours'], WYSIJA ),$array_duration['hours']).' ';$current_level++; }
  116. if ($precision >3 && $current_level < $number_of_units && $array_duration['mins'] >= 1) { $str .= sprintf(_n( '%1$s minute', '%1$s minutes', $array_duration['mins'], WYSIJA ),$array_duration['mins']).' ';$current_level++; }
  117. if ($precision >4 && $current_level < $number_of_units && $array_duration['secs'] >= 1) { $str .= sprintf(_n( '%1$s second', '%1$s seconds', $array_duration['secs'], WYSIJA ),$array_duration['secs']).' ';$current_level++; }
  118. return $str;
  119. }
  120. /**
  121. * this array is to be used in some of the functions below
  122. * @return array
  123. */
  124. private function get_duration_units($unit = false){
  125. // keep it in that order otherwise it will count first the second and will just retunr the number of seconds left
  126. $units = array(
  127. 'years' => 60*60*24*365,
  128. 'weeks' => 60*60*24*7,
  129. 'days' => 60*60*24,
  130. 'hours' => 60*60,
  131. 'mins' => 60,
  132. 'secs' => 1,
  133. );
  134. if($unit === false ) return $units;
  135. elseif(isset($units[$unit])) return $units[$unit];
  136. }
  137. /**
  138. * enter a number of seconds as input it will return an array calculating the duration in years,weeks,days,hours and seconds
  139. * @param int $duration
  140. * @param boolean $split_the_duration_between_each_unit I didn't know how to explain that value,
  141. * basically it's either 1hour 33min 7seconds translate into
  142. * array(years=>0, weeks=>0, days=>0, hours=>1, mins=>33, secs=7)
  143. * or
  144. * array(years=>0, weeks=>0, days=>0, hours=>1, mins=>93, secs=5587)
  145. * @return array with each units
  146. */
  147. public function convert_seconds_to_array( $duration , $split_the_duration_between_each_unit = true){
  148. $result = array();
  149. $duration = array( 'seconds_left' => $duration);
  150. $units = $this->get_duration_units();
  151. foreach($units as $unit => $unit_in_seconds){
  152. if($split_the_duration_between_each_unit){
  153. $duration = $this->get_duration( $duration['seconds_left'] , $unit);
  154. $result[$unit] = $duration['number'];
  155. }else{
  156. $result_duration = $this->get_duration( $duration['seconds_left'] , $unit);
  157. $result[$unit] = $result_duration['number'];
  158. }
  159. }
  160. return $result;
  161. }
  162. /**
  163. * convert one duration in seconds to a unit you specify (mins,hours,days,weeks,years)
  164. * @param int $duration
  165. * @param string $unit
  166. * @return array
  167. */
  168. public function get_duration($duration, $unit = 'days'){
  169. $result = array();
  170. $result['number'] = floor($duration / $this->get_duration_units($unit));
  171. $result['seconds_left'] = $duration % $this->get_duration_units($unit);
  172. return $result;
  173. }
  174. /**
  175. *
  176. * @param type $time
  177. * @param type $justtime
  178. * @return type
  179. */
  180. function localtime($time,$justtime=false){
  181. if($justtime) $time=strtotime($time);
  182. $time_format = get_option('time_format');
  183. // in some rare cases the time format option may be empty in which case we want it to default to g:i a
  184. if(empty($time_format)) $time_format = 'g:i a';
  185. $time = date($time_format, $time);
  186. return $time;
  187. }
  188. /**
  189. * return the offseted time formated(used in post notifications)
  190. * @param type $val
  191. * @return string
  192. */
  193. function time_tzed($val=false){
  194. return gmdate( 'Y-m-d H:i:s', $this->servertime_to_localtime($val) );
  195. }
  196. /**
  197. * specify a unix server time int and it will convert it to the local time if you don't specify any unixTime value it will convert the current time
  198. * @param type $unixTime
  199. * @return int
  200. */
  201. function servertime_to_localtime($unixTime=false){
  202. //this should get GMT-0 time in int date('Z') is the server's time offset compared to GMT-0
  203. $current_server_time = time();
  204. $gmt_time = $current_server_time - date('Z');
  205. //this is the local time on this site : current time at GMT-0 + the offset chosen in WP settings
  206. $current_local_time = $gmt_time + ( get_option( 'gmt_offset' ) * 3600 );
  207. if(!$unixTime) return $current_local_time;
  208. else{
  209. //if we've specified a time value in the function, we calculate the difference between the current servertime and the offseted current time
  210. $time_difference = $current_local_time - $current_server_time;
  211. //unix time was recorded non offseted so it's the server's time we add the timedifference to it to get the local time
  212. return $unixTime + $time_difference;
  213. }
  214. }
  215. /**
  216. * specify a local time int and we will convert it to the server time
  217. * mostly used with values produced with strtotime() strtotime converts Monday 5pm to the server time's 5pm
  218. * and if we want to get Monday 5pm of the local time in the server time we need to do a conversion of that value from local to server
  219. * @param int $server_time time value recorded in the past using time() or strtotime()
  220. * @return int
  221. */
  222. function localtime_to_servertime($server_time){
  223. //this should get GMT-0 time in int date('Z') is the server's time offset compared to GMT-0
  224. $current_server_time = time();
  225. $gmt_time = $current_server_time - date('Z');
  226. //this is the local time on this site : current time at GMT-0 + the offset chosen in WP settings
  227. $current_local_time = $gmt_time + ( get_option( 'gmt_offset' ) * 3600 );
  228. //this is the time difference between the t
  229. $time_difference = $current_local_time - $current_server_time;
  230. //unix time was recorded as local time we substract to it the time difference
  231. return $server_time - $time_difference;
  232. }
  233. function site_current_time($date_format = 'H:i:s'){
  234. // display the current time
  235. $current_server_time = time();
  236. $gmt_time = $current_server_time - date('Z');
  237. //this is the local time on this site : current time at GMT-0 + the offset chosen in WP settings
  238. $current_local_time = $gmt_time + ( get_option( 'gmt_offset' ) * 3600 );
  239. return date($date_format , $current_local_time);
  240. }
  241. /**
  242. * get the translated day name based on a lowercase day namekey
  243. * @param type $day if specified we return only one value otherwise we return the entire array
  244. * @return mixed
  245. */
  246. function getday($day=false){
  247. $days=array('monday'=>__('Monday',WYSIJA),
  248. 'tuesday'=>__('Tuesday',WYSIJA),
  249. 'wednesday'=>__('Wednesday',WYSIJA),
  250. 'thursday'=>__('Thursday',WYSIJA),
  251. 'friday'=>__('Friday',WYSIJA),
  252. 'saturday'=>__('Saturday',WYSIJA),
  253. 'sunday'=>__('Sunday',WYSIJA));
  254. if(!$day || !isset($days[$day])) return $days;
  255. else return $days[$day];
  256. }
  257. /**
  258. * get the translated day name based on a lowercase day namekey
  259. * @param type $week if specified we return only one, otherwise we return the entire array
  260. * @return mixed
  261. */
  262. function getweeksnumber($week=false){
  263. $weeks=array(
  264. '1'=>__('1st',WYSIJA),
  265. '2'=>__('2nd',WYSIJA),
  266. '3'=>__('3rd',WYSIJA),
  267. '4'=>__('Last',WYSIJA),
  268. );
  269. if(!$week || !isset($weeks[$week])) return $weeks;
  270. else return $weeks[$week];
  271. }
  272. /**
  273. * get the translated day number based on the number in the month until 29th
  274. * @param type $day if specified we just return one otherwise we return the entire array
  275. * @return mixed
  276. */
  277. function getdaynumber($day=false){
  278. $daynumbers=array();
  279. //prepare an array of numbers
  280. for($i = 1;$i < 29;$i++) {
  281. switch($i){
  282. case 1:
  283. $number=__('1st',WYSIJA);
  284. break;
  285. case 2:
  286. $number=__('2nd',WYSIJA);
  287. break;
  288. case 3:
  289. $number=__('3rd',WYSIJA);
  290. break;
  291. default:
  292. $number=sprintf(__('%1$sth',WYSIJA),$i);
  293. }
  294. $daynumbers[$i] = $number;
  295. }
  296. if(!$day || !isset($daynumbers[$day])) return $daynumbers;
  297. else return $daynumbers[$day];
  298. }
  299. /**
  300. * we use to deal with the WPLANG constant but that's silly considering there are plugins like
  301. * WPML which needs to alter that value
  302. * @param type $get_country when true if we find pt_BR as the language code we return BR if we find en_GB we return GB
  303. * @return string
  304. */
  305. function get_language_code($get_country = false){
  306. // in WP Multisite if we have a WPLANG defined in the wp-config,
  307. // it won't be used each site needs to have a WPLANG option defined and if it's not defined it will be empty and default to en_US
  308. if ( is_multisite() ) {
  309. // Don't check blog option when installing.
  310. if ( defined( 'WP_INSTALLING' ) || ( false === $ms_locale = get_option( 'WPLANG' ) ) )
  311. $ms_locale = get_site_option('WPLANG');
  312. if ( $ms_locale !== false )
  313. $locale = $ms_locale;
  314. // make sure we don't default to en_US if we have an empty locale and a WPLANG defined
  315. if(empty($locale) && defined('WPLANG')) $locale = WPLANG;
  316. else $locale = get_locale();
  317. }else{
  318. $locale = get_locale();
  319. }
  320. if($locale!=''){
  321. if(strpos($locale, '_')!==false){
  322. $locale = explode('_',$locale);
  323. if($get_country === true){
  324. $language_code = $locale[1];
  325. }else{
  326. $language_code = $locale[0];
  327. }
  328. }else{
  329. $language_code = $locale;
  330. }
  331. }else{
  332. $language_code = 'en';
  333. }
  334. return $language_code;
  335. }
  336. /**
  337. * check if a domain exist
  338. * @param type $domain
  339. * @return boolean
  340. */
  341. function check_domain_exist($domain){
  342. $mxhosts = array();
  343. // 1 - Check if the domain exists
  344. $checkDomain = getmxrr($domain, $mxhosts);
  345. // 2 - Sometimes the returned host is checkyouremailaddress-hostnamedoesnotexist262392208.com ... not sure why!
  346. // But we remove it if it's the case...
  347. if(!empty($mxhosts) && strpos($mxhosts[0],'hostnamedoesnotexist')) array_shift($mxhosts);
  348. if(!$checkDomain || empty($mxhosts)){
  349. // 3 - Lets check with another function in case of...
  350. $dns = @dns_get_record($domain, DNS_A);
  351. if(empty($dns)) return false;
  352. }
  353. return true;
  354. }
  355. function check_email_domain($email){
  356. return $this->check_domain_exist(substr($email,strrpos($email,'@')+1));
  357. }
  358. /**
  359. * let us know if the visitor is a european member
  360. * @return boolean
  361. */
  362. function is_european(){
  363. $european_members = array('AT','BE','BG','CY','CZ','DK','EE','DE','PT','EL','ES','FI','HU','LU','MT','SI',
  364. 'FR','GB','IE','IT','LV','LT','NL','PL','SK','RO','SE','HR');
  365. // if the language of the site is an european
  366. if(in_array(strtoupper($this->get_language_code(true)), $european_members) ) return true;
  367. return false;
  368. }
  369. }