PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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