WYSIJA_help_toolbox

The MailPoet Newsletters WYSIJA help toolbox class.

Defined (1)

The class is defined in the following location(s).

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