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

/wp-content/plugins/myMail/classes/helper.class.php

https://bitbucket.org/seyekuyinu/highlifer
PHP | 496 lines | 341 code | 138 blank | 17 comment | 56 complexity | defa7f4af0dbb064e028f7c4a4b491a5 MD5 | raw file
Possible License(s): MIT, Apache-2.0, LGPL-2.1
  1. <?php if (!defined('ABSPATH')) die('not allowed');
  2. class mymail_helper {
  3. public function __construct() {}
  4. public function create_image($attach_id = NULL, $img_url = NULL, $width, $height = NULL, $crop = false) {
  5. if ($attach_id) {
  6. $attach_id = intval($attach_id);
  7. $image_src = wp_get_attachment_image_src($attach_id, 'full');
  8. $actual_file_path = get_attached_file($attach_id);
  9. if (!$width && !$height) {
  10. $orig_size = getimagesize($actual_file_path);
  11. $width = $orig_size[0];
  12. $height = $orig_size[1];
  13. }
  14. } else if ($img_url) {
  15. $file_path = parse_url($img_url);
  16. if(file_exists($img_url)){
  17. $actual_file_path = $img_url;
  18. $img_url = str_replace(ABSPATH, site_url('/'), $img_url);
  19. }else{
  20. $actual_file_path = realpath($_SERVER['DOCUMENT_ROOT']) . $file_path['path'];
  21. /* todo: reconize URLs */
  22. if(!file_exists($actual_file_path)){
  23. return array(
  24. 'id' => $attach_id,
  25. 'url' => $img_url,
  26. 'width' => $width,
  27. 'height' => NULL,
  28. 'asp' => NULL,
  29. '_' => 1,
  30. );
  31. }
  32. }
  33. $actual_file_path = ltrim($file_path['path'], '/');
  34. $actual_file_path = rtrim(ABSPATH, '/') . $file_path['path'];
  35. if(file_exists($actual_file_path)){
  36. $orig_size = getimagesize($actual_file_path);
  37. }else{
  38. $actual_file_path = ABSPATH.str_replace(site_url('/'), '', $img_url);
  39. $orig_size = getimagesize($actual_file_path);
  40. }
  41. $image_src[0] = $img_url;
  42. $image_src[1] = $orig_size[0];
  43. $image_src[2] = $orig_size[1];
  44. }
  45. if (!$height && isset($image_src[2])) $height = round($width /($image_src[1]/$image_src[2]));
  46. $file_info = pathinfo($actual_file_path);
  47. $extension = $file_info['extension'];
  48. $no_ext_path = $file_info['dirname'] . '/' . $file_info['filename'];
  49. $cropped_img_path = $no_ext_path . '-' . $width . 'x' . $height . '.' . $extension;
  50. if ($image_src[1] > $width || $image_src[2] > $height) {
  51. if (file_exists($cropped_img_path)) {
  52. $cropped_img_url = str_replace(basename($image_src[0]), basename($cropped_img_path), $image_src[0]);
  53. return array(
  54. 'id' => $attach_id,
  55. 'url' => $cropped_img_url,
  56. 'width' => $width,
  57. 'height' => $height,
  58. 'asp' => $height ? $width/$height : NULL,
  59. '_' => 2,
  60. );
  61. }
  62. if ($crop == false) {
  63. $proportional_size = wp_constrain_dimensions($image_src[1], $image_src[2], $width, $height);
  64. $resized_img_path = $no_ext_path . '-' . $proportional_size[0] . 'x' . $proportional_size[1] . '.' . $extension;
  65. if (file_exists($resized_img_path)) {
  66. $resized_img_url = str_replace(basename($image_src[0]), basename($resized_img_path), $image_src[0]);
  67. return array(
  68. 'id' => $attach_id,
  69. 'url' => $resized_img_url,
  70. 'width' => $proportional_size[0],
  71. 'height' => $proportional_size[1],
  72. 'asp' => $proportional_size[1] ? $proportional_size[0]/$proportional_size[1] : NULL,
  73. '_' => 3,
  74. );
  75. }
  76. }
  77. if(function_exists( 'wp_get_image_editor' )){
  78. $image = wp_get_image_editor($actual_file_path);
  79. if(!is_wp_error($image)){
  80. $image->resize( $width, $height, $crop );
  81. $imageobj = $image->save();
  82. $new_img_path = $imageobj['path'];
  83. }else{
  84. $new_img_path = image_resize($actual_file_path, $width, $height, $crop);
  85. }
  86. }else{
  87. $new_img_path = image_resize($actual_file_path, $width, $height, $crop);
  88. }
  89. if(is_wp_error($new_img_path)) $new_img_path = $actual_file_path;
  90. $new_img_size = getimagesize($new_img_path);
  91. $new_img = str_replace(basename($image_src[0]), basename($new_img_path), $image_src[0]);
  92. return array(
  93. 'id' => $attach_id,
  94. 'url' => $new_img,
  95. 'width' => $new_img_size[0],
  96. 'height' => $new_img_size[1],
  97. 'asp' => $new_img_size[1] ? $new_img_size[0]/$new_img_size[1] : NULL,
  98. '_' => 4
  99. );
  100. }
  101. return array(
  102. 'id' => $attach_id,
  103. 'url' => $image_src[0],
  104. 'width' => $image_src[1],
  105. 'height' => $image_src[2],
  106. 'asp' => $image_src[2] ? $image_src[1]/$image_src[2] : NULL,
  107. '_' => 5
  108. );
  109. }
  110. public function get_wpuser_meta_fields() {
  111. global $wpdb;
  112. $cache_key = 'wpuser_meta_fields';
  113. if(false === ($meta_values = mymail_cache_get( $cache_key ))){
  114. $exclude = array('comment_shortcuts', 'first_name', 'last_name', 'nickname', 'use_ssl', 'default_password_nag', 'dismissed_wp_pointers', 'rich_editing', 'show_admin_bar_front', 'show_welcome_panel', 'admin_color', 'screen_layout_dashboard', 'screen_layout_newsletter');
  115. $meta_values = $wpdb->get_col("SELECT meta_key FROM {$wpdb->usermeta} WHERE meta_value NOT LIKE '%{%}%' AND meta_key NOT LIKE '{$wpdb->base_prefix}%' AND meta_key NOT IN ('".implode("', '", $exclude)."') GROUP BY meta_key ASC");
  116. mymail_cache_set( $cache_key, $meta_values );
  117. }
  118. return $meta_values;
  119. }
  120. public function link_query( $args = array(), $countonly = false ) {
  121. global $wpdb;
  122. $pts = get_post_types( array( 'public' => true ), 'objects' );
  123. $pt_names = array_keys( $pts );
  124. $defaults = array(
  125. 'post_type' => $pt_names,
  126. 'suppress_filters' => true,
  127. 'update_post_term_cache' => false,
  128. 'update_post_meta_cache' => false,
  129. 'post_status' => 'publish',
  130. 'order' => 'DESC',
  131. 'orderby' => 'post_date',
  132. 'posts_per_page' => -1,
  133. 'offset' => 0,
  134. );
  135. $query = wp_parse_args($args, $defaults);
  136. if ( isset( $args['s'] ) )
  137. $query['s'] = $args['s'];
  138. if($countonly){
  139. // Do main query with only one result to reduce server load
  140. $get_posts = new WP_Query(wp_parse_args(array('posts_per_page' => 1, 'offset' => 0), $query));
  141. return $wpdb->query(str_ireplace('LIMIT 0, 1', '', $get_posts->request));
  142. }
  143. // Do main query.
  144. $get_posts = new WP_Query($query);
  145. $sql = str_replace('posts.ID', 'posts.*', $get_posts->request);
  146. $posts = $wpdb->get_results($sql);
  147. // Build results.
  148. $results = array();
  149. foreach ( $posts as $post ) {
  150. if ( 'post' == $post->post_type )
  151. $info = mysql2date( __( 'Y/m/d', 'mymail' ), $post->post_date );
  152. else
  153. $info = $pts[ $post->post_type ]->labels->singular_name;
  154. $results[] = array(
  155. 'ID' => $post->ID,
  156. 'title' => trim( esc_html( strip_tags( get_the_title( $post ) ) ) ),
  157. 'permalink' => get_permalink( $post->ID ),
  158. 'info' => $info,
  159. );
  160. }
  161. return $results;
  162. }
  163. public function get_next_date_in_future($utc_start, $interval, $time_frame, $weekdays = array(), $in_future = true) {
  164. //in local time
  165. $offset = $this->gmt_offset(true);
  166. $now = time() + $offset;
  167. $utc_start += $offset;
  168. $times = 1;
  169. //must be in future and starttime in the past
  170. if($in_future && $utc_start - $now < 0){
  171. //get how many $time_frame are in the time between now and the starttime
  172. switch ($time_frame) {
  173. case 'year':
  174. $count = date('Y', $now)-date('Y', $utc_start);
  175. break;
  176. case 'month':
  177. $count = (date('Y', $now) - date('Y', $utc_start))*12 + (date('m', $now) - date('m', $utc_start));
  178. break;
  179. case 'week':
  180. $count = floor((($now - $utc_start)/86400)/7);
  181. break;
  182. case 'day':
  183. $count = floor(($now - $utc_start)/86400);
  184. break;
  185. case 'hour':
  186. $count = floor(($now - $utc_start)/3600);
  187. break;
  188. default:
  189. $count = $interval;
  190. break;
  191. }
  192. $times = $interval ? ceil($count/$interval) : 0;
  193. }
  194. $nextdate = strtotime(date('Y-m-d H:i:s', $utc_start)." +".($interval*$times)." {$time_frame}");
  195. //add a single entity if date is still in the past or just now
  196. if($in_future && ($nextdate - $now < 0 || $nextdate == $utc_start))
  197. $nextdate = strtotime(date('Y-m-d H:i:s', $utc_start)." +".($interval*$times+$interval)." {$time_frame}");
  198. if(!empty($weekdays) && count($weekdays) < 7){
  199. $dayofweek = date('w', $nextdate);
  200. $i = 0;
  201. if(!$interval) $interval = 1;
  202. while(!in_array($dayofweek, $weekdays)){
  203. //try next $time_frame
  204. $nextdate = strtotime("+{$interval} {$time_frame}", $nextdate);
  205. $dayofweek = date('w', $nextdate);
  206. //force a break to prevent infinity loops
  207. if($i > 500) break;
  208. $i++;
  209. }
  210. }
  211. //return as UTC
  212. return $nextdate-$offset;
  213. }
  214. public function get_post_term_dropdown($post_type = 'post', $labels = true, $names = false, $values = array()) {
  215. $taxonomies = get_object_taxonomies( $post_type, 'objects' );
  216. $html = '';
  217. $taxwraps = array();
  218. foreach($taxonomies as $id => $taxonomy){
  219. $tax = '<div class="dynamic_embed_options_taxonomy_container">'.($labels ? '<label class="dynamic_embed_options_taxonomy_label">'.$taxonomy->labels->name.': </label>' : '').'<span class="dynamic_embed_options_taxonomy_wrap">';
  220. $cats = get_categories( array('hide_empty' => false, 'taxonomy' => $id, 'type' => $post_type, 'orderby' => 'id' ,'number' => 999) );
  221. if(!isset($values[$id])) $values[$id] = array('-1');
  222. $selects = array();
  223. foreach($values[$id] as $term){
  224. $select = '<select class="dynamic_embed_options_taxonomy check-for-posts" '.($names ? 'name="mymail_data[autoresponder][terms]['.$id.'][]"': '').'>';
  225. $select .= '<option value="-1">'.sprintf(__('any %s', 'mymail'), $taxonomy->labels->singular_name).'</option>';
  226. foreach($cats as $cat){
  227. $select .= '<option value="'.$cat->term_id.'" '.selected($cat->term_id, $term, false).'>'.$cat->name.'</option>';
  228. }
  229. $select .= '</select>';
  230. $selects[] = $select;
  231. }
  232. $tax .= implode(' '.__('or', 'mymail').' ', $selects);
  233. $tax .= '</span><div class="mymail-list-operator"><span class="operator-and">' .__('and', 'mymail') . '</span></div></div>';
  234. $taxwraps[] = $tax;
  235. }
  236. $html = (!empty($taxwraps))
  237. ? implode(($labels
  238. ? '<label class="dynamic_embed_options_taxonomy_label">&nbsp;</label>'
  239. : ''), $taxwraps)
  240. : '';
  241. return $html;
  242. }
  243. public function social_services(){
  244. include MYMAIL_DIR . 'includes/social_services.php';
  245. return $mymail_social_services;
  246. }
  247. public function using_permalinks(){
  248. global $wp_rewrite;
  249. return is_object($wp_rewrite) && $wp_rewrite->using_permalinks();
  250. }
  251. public function gmt_offset($in_seconds = false, $timestamp = NULL){
  252. $offset = get_option('gmt_offset');
  253. if($offset == ''){
  254. $tzstring = get_option('timezone_string');
  255. $current = date_default_timezone_get();
  256. date_default_timezone_set($tzstring);
  257. $offset = date('Z')/3600;
  258. date_default_timezone_set($current);
  259. }
  260. //check if timestamp has DST
  261. if(!is_null($timestamp)){
  262. $l = localtime($timestamp, true);
  263. if($l['tm_isdst']) $offset++;
  264. }
  265. return $in_seconds ? $offset*3600 : (int) $offset;
  266. }
  267. public function format_html($html, $body = false){
  268. $doc = new DOMDocument();
  269. $doc->preserveWhiteSpace = FALSE;
  270. $doc->loadHTML($html);
  271. $doc->formatOutput = TRUE;
  272. //remove <!DOCTYPE
  273. $doc->removeChild($doc->doctype);
  274. //remove <html><body></body></html>
  275. if(!$body)
  276. $doc->replaceChild($doc->firstChild->firstChild->firstChild, $doc->firstChild);
  277. return trim($doc->saveHTML());
  278. }
  279. public function get_bounce_message($status, $original = NULL){
  280. include MYMAIL_DIR . 'classes/libs/bounce/bounce_statuscodes.php';
  281. if(is_null($original)) $original = $status;
  282. if(isset($status_code_classes[$status])){
  283. return $status_code_classes[$status];
  284. }
  285. if(isset($status_code_subclasses[$status])){
  286. return $status_code_subclasses[$status];
  287. }
  288. if($status = substr($status, 0,strrpos($status, '.'))){
  289. return $this->get_bounce_message($status, $original);
  290. }
  291. return array('title' => __('unknown', 'mymail'), 'descr' => __('error is unknown', 'mymail'));
  292. }
  293. public function wp_print_embedded_scripts( $handle, $minifiy = true ){
  294. global $wp_scripts;
  295. if(!$wp_scripts->registered[$handle]) return;
  296. $path = untrailingslashit(ABSPATH);
  297. foreach ($wp_scripts->registered[$handle]->deps as $h) {
  298. $this->wp_print_embedded_scripts($h);
  299. }
  300. if(isset($wp_scripts->registered[$handle]->extra['data']))
  301. echo '<script>'.$wp_scripts->registered[$handle]->extra['data'].'</script>';
  302. ob_start();
  303. (file_exists($path. $wp_scripts->registered[$handle]->src))
  304. ? include $path. $wp_scripts->registered[$handle]->src
  305. : include str_replace(MYMAIL_URI, MYMAIL_DIR, $wp_scripts->registered[$handle]->src);
  306. $output = ob_get_contents();
  307. ob_end_clean();
  308. //$output = preg_replace('/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:)\/\/.*))/', '', $output);
  309. //$output = trim(str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' '), '', $output));
  310. //$output = preg_replace('/\s+/', ' ', $output);
  311. if($minifiy){
  312. $output = str_replace(' {', '{', $output);
  313. $output = str_replace(' }', '}', $output);
  314. }
  315. echo "<script id='$handle'>$output</script>";
  316. }
  317. public function wp_print_embedded_styles( $handle, $minifiy = true ){
  318. global $wp_styles;
  319. if(!$wp_styles->registered[$handle]) return;
  320. $path = untrailingslashit(ABSPATH);
  321. foreach ($wp_styles->registered[$handle]->deps as $h) {
  322. $this->wp_print_embedded_styles($h);
  323. }
  324. ob_start();
  325. (file_exists($path. $wp_styles->registered[$handle]->src))
  326. ? include $path. $wp_styles->registered[$handle]->src
  327. : include str_replace(MYMAIL_URI, MYMAIL_DIR, $wp_styles->registered[$handle]->src);
  328. $output = ob_get_contents();
  329. ob_end_clean();
  330. preg_match_all('#url\((\'|")?([^\'"]+)(\'|")?\)#i', $output, $urls);
  331. $base = trailingslashit(dirname($wp_styles->registered[$handle]->src));
  332. foreach($urls[0] as $i => $url){
  333. if(substr($urls[2][$i], 0, 5) == 'data:') continue;
  334. $output = str_replace('url('.$urls[1][$i].$urls[2][$i].$urls[3][$i].')', 'url('.$urls[1][$i].$base.$urls[2][$i].$urls[3][$i].')', $output);
  335. }
  336. if($minifiy){
  337. $output = preg_replace('#/\*(?:.(?!/)|[^\*](?=/)|(?<!\*)/)*\*/#s', '', $output);
  338. $output = trim(str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' '), '', $output));
  339. $output = preg_replace('/\s+/', ' ', $output);
  340. $output = str_replace(' {', '{', $output);
  341. $output = str_replace(' }', '}', $output);
  342. $output = str_replace(': ', ':', $output);
  343. }
  344. echo "<style id='$handle' type='text/css'>$output</style>";
  345. }
  346. public function object_to_array($obj) {
  347. if(is_object($obj)) $obj = (array) $obj;
  348. if(is_array($obj)) {
  349. $new = array();
  350. foreach($obj as $key => $val) {
  351. $new[$key] = $this->object_to_array($val);
  352. }
  353. }else{
  354. $new = $obj;
  355. }
  356. return $new;
  357. }
  358. }