WYSIJA_help_articles

The MailPoet Newsletters WYSIJA help articles class.

Defined (1)

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

/helpers/articles.php  
  1. class WYSIJA_help_articles extends WYSIJA_object { 
  2.  
  3. function __construct() { 
  4. parent::__construct(); 
  5.  
  6. function stripShortcodes($content) { 
  7. if(strlen(trim($content)) === 0) { 
  8. return ''; 
  9. // remove captions 
  10. $content = preg_replace("/\[caption.*?\](.*<\/a>)(.*?)\[\/caption\]/", '$1', $content); 
  11.  
  12. // remove other shortcodes 
  13. $content = preg_replace('/\[[^\[\]]*\]/', '', $content); 
  14.  
  15. return $content; 
  16.  
  17. function convertPostToBlock($post, $params = array()) { 
  18.  
  19. // defaults 
  20. $defaults = array( 
  21. 'title_tag' => 'h1',  
  22. 'title_alignment' => 'left',  
  23. 'title_position' => 'inside',  
  24. 'image_alignment' => 'left',  
  25. 'image_width' => 325,  
  26. 'readmore' => __('Read online.', WYSIJA),  
  27. 'post_content' => 'full',  
  28. 'author_show' => 'no',  
  29. 'author_label' => '',  
  30. 'category_show' => 'no',  
  31. 'category_label' => '' 
  32. ); 
  33.  
  34. // merge params with default params 
  35. $params = array_merge($defaults, $params); 
  36.  
  37. if($params['post_content'] === 'full') { 
  38. $content = $post['post_content']; 
  39. } else if($params['post_content'] === 'title') { 
  40. $content = $this->getPostTitle($post, $params); 
  41. } else { 
  42. // get excerpt 
  43. if(!empty($post['post_excerpt'])) { 
  44. $content = $post['post_excerpt']; 
  45. } else { 
  46. // strip shortcodes before getting the excerpt 
  47. $post['post_content'] = $this->stripShortcodes($post['post_content']); 
  48.  
  49. // if excerpt is empty then try to find the "more" tag 
  50. $excerpts = explode('<!--more-->', $post['post_content']); 
  51. if(count($excerpts) > 1) { 
  52. $content = $excerpts[0]; 
  53. }else{ 
  54. // finally get a made up excerpt if there is no other choice 
  55. $helper_toolbox = WYSIJA::get('toolbox', 'helper'); 
  56. $content = $helper_toolbox->excerpt($post['post_content'], apply_filters('mpoet_excerpt_length', 60)); 
  57. // strip title tags from excerpt 
  58. $content = preg_replace('/<([\/])?h[123456](.*?)>/', '<$1p$2>', $content); 
  59.  
  60. // convert new lines into <p> 
  61. $content = wpautop($content); 
  62.  
  63. // remove images 
  64. $content = preg_replace('/<img[^>]+./', '', $content); 
  65.  
  66. // strip shortcodes 
  67. $content = $this->stripShortcodes($content); 
  68.  
  69. // remove wysija nl shortcode 
  70. $content= preg_replace('/\<div class="wysija-register">(.*?)\<\/div>/', '', $content); 
  71.  
  72. // convert embedded content if necessary 
  73. $content = $this->convertEmbeddedContent($content); 
  74.  
  75. // convert h4 h5 h6 to h3 
  76. $content = preg_replace('/<([\/])?h[456](.*?)>/', '<$1h3$2>', $content); 
  77.  
  78. // convert ol to ul 
  79. $content = preg_replace('/<([\/])?ol(.*?)>/', '<$1ul$2>', $content); 
  80.  
  81. // convert currency signs 
  82. $content = str_replace(array('$', '*', '', ''), array('$', '€', '£', '¥'), $content); 
  83.  
  84. // strip useless tags 
  85. // TODO should we add table, tr, td and th into that list it could create issues in some cases 
  86. $tags_not_being_stripped = array('<p>', '<em>', '<span>', '<b>', '<strong>', '<i>', '<h1>', '<h2>', '<h3>', '<a>', '<ul>', '<ol>', '<li>', '<br>'); 
  87.  
  88. // filter to modify that list 
  89. $tags_not_being_stripped = apply_filters('mpoet_strip_tags_ignored', $tags_not_being_stripped); 
  90.  
  91. $content = strip_tags($content, implode('', $tags_not_being_stripped)); 
  92.  
  93. // post meta (author, categories) 
  94. $post_meta_above = ''; 
  95. // if the author or categories are displayed, open a new paragraph 
  96. if($params['author_show'] === 'above' || $params['category_show'] === 'above') { 
  97. $post_meta_above .= '<p>'; 
  98.  
  99. // author above 
  100. if($params['author_show'] === 'above') { 
  101. $post_meta_above .= $this->getPostAuthor($post, $params); 
  102. // categories above 
  103. if($params['category_show'] === 'above') { 
  104. // if there is an author already, we need to add an extra break 
  105. if($params['author_show'] === 'above') { 
  106. $post_meta_above .= '<br />'; 
  107. // display post categories 
  108. $post_meta_above .= $this->getPostCategories($post, $params); 
  109.  
  110. // close the paragraph around author and categories 
  111. if($params['author_show'] === 'above' || $params['category_show'] === 'above') { 
  112. $post_meta_above .= '</p>'; 
  113.  
  114. if($params['post_content'] !== 'title') { 
  115. if($params['title_position'] === 'inside') { 
  116. // add title 
  117. $content = $this->getPostTitle($post, $params).$post_meta_above.$content; 
  118. } else { 
  119. $content = $post_meta_above.$content; 
  120. } else { 
  121. $content = $post_meta_above.$content; 
  122.  
  123. if($params['post_content'] !== 'title') { 
  124. // add read online link 
  125. $content .= '<p><a href="'.get_permalink($post['ID']).'" target="_blank">'.stripslashes($params['readmore']).'</a></p>'; 
  126.  
  127. // post meta (author, categories) below 
  128. $post_meta_below = ''; 
  129.  
  130. // if the author or categories are displayed, open a new paragraph 
  131. if($params['author_show'] === 'below' || $params['category_show'] === 'below') { 
  132. $post_meta_below .= '<p>'; 
  133.  
  134. // author below 
  135. if($params['author_show'] === 'below') { 
  136. $post_meta_below .= $this->getPostAuthor($post, $params); 
  137.  
  138. // categories below 
  139. if($params['category_show'] === 'below') { 
  140. // if there is an author already, we need to add an extra break 
  141. if($params['author_show'] === 'below') { 
  142. $post_meta_below .= '<br />'; 
  143. $post_meta_below .= $this->getPostCategories($post, $params); 
  144.  
  145. // close the paragraph around author and categories 
  146. if($params['author_show'] === 'below' || $params['category_show'] === 'below') { 
  147. $post_meta_below .= '</p>'; 
  148.  
  149. if($post_meta_below !== '') $content .= $post_meta_below; 
  150.  
  151. // set image/text alignment based on present data 
  152. $post_image = null; 
  153.  
  154. if(($params['title_tag'] !== 'list') && isset($post['post_image'])) { 
  155. $post_image = $post['post_image']; 
  156.  
  157. // set image alignment to match block's 
  158. $post_image['alignment'] = $params['image_alignment']; 
  159.  
  160. // constrain size depending on alignment 
  161. if(empty($post_image['height']) or $post_image['height'] === 0) { 
  162. $post_image = null; 
  163. } else { 
  164. $ratio = round(($post_image['width'] / $post_image['height']) * 1000) / 1000; 
  165. switch($params['image_alignment']) { 
  166. case 'alternate': 
  167. case 'left': 
  168. case 'right': 
  169. // constrain width to 325px max 
  170. $post_image['width'] = min($params['image_width'], 325); 
  171. break; 
  172. case 'center': 
  173. // constrain width to 564px max 
  174. $post_image['width'] = min($params['image_width'], 564); 
  175. break; 
  176.  
  177. if($ratio > 0) { 
  178. // if ratio has been calculated, deduce image height 
  179. $post_image['height'] = (int)($post_image['width'] / $ratio); 
  180. } else { 
  181. // otherwise skip the image 
  182. $post_image = null; 
  183.  
  184. $position = 0; 
  185. if(isset($params['position']) && (int)$params['position'] > 0) { 
  186. $position = (int)$params['position']; 
  187.  
  188. $block = array( 
  189. 'position' => $position,  
  190. 'type' => 'content',  
  191. 'text' => array( 
  192. 'value' => base64_encode($content) 
  193. ),  
  194. 'image' => $post_image,  
  195. 'alignment' => $params['image_alignment'] 
  196. ); 
  197.  
  198. return $block; 
  199.  
  200. public function getPostAuthor($post = array(), $params = array()) { 
  201. $content = ''; 
  202.  
  203. if(isset($post['post_author'])) { 
  204. $author_name = get_the_author_meta('display_name', (int)$post['post_author']); 
  205.  
  206. // check if the user specified a label to be displayed before the author's name 
  207. if(strlen(trim($params['author_label'])) > 0) { 
  208. $author_name = stripslashes(trim($params['author_label'])).' '.$author_name; 
  209. $content .= $author_name; 
  210.  
  211. return $content; 
  212.  
  213. public function getPostCategories($post = array(), $params = array()) { 
  214. $content = ''; 
  215.  
  216. // get categories 
  217. //$categories = get_the_category($post['ID']); 
  218. $helper_wp_tools = WYSIJA::get('wp_tools', 'helper'); 
  219. $categories = $helper_wp_tools->get_post_categories($post); 
  220.  
  221. if(empty($categories) === false) { 
  222. // check if the user specified a label to be displayed before the author's name 
  223. if(strlen(trim($params['category_label'])) > 0) { 
  224. $content = stripslashes($params['category_label']).' '; 
  225.  
  226. $content .= join(', ', $categories); 
  227.  
  228. return $content; 
  229.  
  230. public function getPostTitle($post = array(), $params = array()) { 
  231. $content = ''; 
  232.  
  233. if(strlen(trim($post['post_title'])) > 0) { 
  234. // cleanup post title and convert currency signs 
  235. $post_title = trim(str_replace(array('$', '*', '', ''), array('$', '€', '£', '¥'), strip_tags($post['post_title']))); 
  236.  
  237. // open title tag 
  238. if($params['title_tag'] === 'list') { 
  239. $params['title_tag'] = 'li'; 
  240.  
  241. $content .= '<'.$params['title_tag'].' class="align-'.$params['title_alignment'].'">'; 
  242. // set title link 
  243. $content .= '<a href="'.get_permalink($post['ID']).'" target="_blank">'; 
  244. // set title 
  245. $content .= $post_title; 
  246. // close title link 
  247. $content .= '</a>'; 
  248. // close title tag 
  249. $content .= '</'.$params['title_tag'].'>'; 
  250.  
  251.  
  252. return $content; 
  253.  
  254. function getImage($post) { 
  255. $image_info = null; 
  256. $post_image = null; 
  257.  
  258. // check if has_post_thumbnail function exists, if not, include wordpress class 
  259. if(!function_exists('has_post_thumbnail')) { 
  260. require_once(ABSPATH.WPINC.'/post-thumbnail-template.php'); 
  261.  
  262. // check for post thumbnail 
  263. if(has_post_thumbnail($post['ID'])) { 
  264. $post_thumbnail = get_post_thumbnail_id($post['ID']); 
  265.  
  266. // get attachment data (src, width, height) 
  267. $image_info = wp_get_attachment_image_src($post_thumbnail, 'wysija-newsletters-max'); 
  268.  
  269. // get alt text 
  270. $altText = trim(strip_tags(get_post_meta($post_thumbnail, '_wp_attachment_image_alt', true))); 
  271. if(strlen($altText) === 0) { 
  272. // if the alt text is empty then use the post title 
  273. $altText = trim(strip_tags($post['post_title'])); 
  274.  
  275. if($image_info !== null) { 
  276. $post_image = array( 
  277. 'src' => $image_info[0],  
  278. 'width' => $image_info[1],  
  279. 'height' => $image_info[2],  
  280. 'alt' => urlencode($altText) 
  281. ); 
  282. } else { 
  283. $matches = $matches2 = array(); 
  284.  
  285. $output = preg_match_all('/<img.+src=['."'".'"]([^'."'".'"]+)['."'".'"].*>/i', $post['post_content'], $matches); 
  286.  
  287. if(isset($matches[0][0])) { 
  288. preg_match_all('/(src|height|width|alt)="([^"]*)"/i', $matches[0][0], $matches2); 
  289.  
  290. if(isset($matches2[1])) { 
  291. foreach($matches2[1] as $k2 => $v2) { 
  292. if(in_array($v2, array('src', 'width', 'height', 'alt'))) { 
  293. if($post_image === null) $post_image = array(); 
  294.  
  295. if($v2 === 'alt') { 
  296. // special case for alt text as it requireds url encoding 
  297. $post_image[$v2] = urlencode($matches2[2][$k2]); 
  298. } else { 
  299. // otherwise simply get the value 
  300. $post_image[$v2] = $matches2[2][$k2]; 
  301.  
  302. $helper_images = WYSIJA::get('image', 'helper'); 
  303. $post_image = $helper_images->valid_image($post_image); 
  304.  
  305. if($post_image===null) return $post_image; 
  306. return array_merge($post_image, array('url' => get_permalink($post['ID']))); 
  307.  
  308.  
  309.  
  310. function convertEmbeddedContent($content = '') { 
  311. // remove embedded video and replace with links 
  312. $content = preg_replace('#<iframe.*?src=\"(.+?)\".*><\/iframe>#', '<a href="$1">'.__('Click here to view media.', WYSIJA).'</a>', $content); 
  313.  
  314. // replace youtube links 
  315. $content = preg_replace('#http://www.youtube.com/embed/([a-zA-Z0-9_-]*)#Ui', 'http://www.youtube.com/watch?v=$1', $content); 
  316.  
  317. return $content; 
  318.  
  319. function field_select_post_type( $params = array() ) { 
  320. if ( array_key_exists( 'value', $params ) ) { 
  321. $value = $params['value']; 
  322. } else { 
  323. return; 
  324.  
  325. // make sure value is null if it's an empty string 
  326. if ( $value !== null && strlen( trim( $value ) ) === 0 ) { 
  327. $value = null; 
  328.  
  329. // get all post types 
  330. $helper_wp_tools = WYSIJA::get( 'wp_tools', 'helper' ); 
  331. $post_types = $helper_wp_tools->get_post_types(); 
  332.  
  333. // build post type selection 
  334. $output = '<select name="post_type" data-placeholder="' . esc_attr__( 'Filter by type', WYSIJA ) . '" class="mailpoet-field-select2-simple" data-minimum-results-for-search="-1" style="margin-right: 5px;" width="150" id="post_type">'; 
  335. $output .= '<option></option>'; // This is require because of Select2 placeholding structure 
  336. $output .= '<option value="post"' . ( ( $value === 'post' ) ? ' selected="selected"' : '' ) . '>' . __( 'Posts', WYSIJA ) . '</option>'; 
  337. $output .= '<option value="page"' . ( ( $value === 'page' ) ? ' selected="selected"' : '' ) . '>' . __( 'Pages', WYSIJA ) . '</option>'; 
  338.  
  339. foreach ( $post_types as $key => $object_post_type ) { 
  340. $selected = ($value === $key) ? ' selected="selected"' : ''; 
  341. $output .= '<option value="'.$key.'"'.$selected.'>'.$object_post_type->labels->name.'</option>'; 
  342. $output .= '</select>'; 
  343. return $output; 
  344.  
  345. function field_select_terms() { 
  346. return '<input name="post_category" data-placeholder="' . esc_attr__( 'Categories and tags', WYSIJA ) . '" class="mailpoet-field-select2-terms post_category" style="margin-right: 5px; width: 180px" width="180" value="" data-multilple="false" type="hidden">'; 
  347.  
  348.  
  349. function field_select_status( $current_status = 'publish' ) { 
  350. $output = ''; 
  351.  
  352. $helper_wp_tools = WYSIJA::get( 'wp_tools', 'helper' ); 
  353. $statuses = $helper_wp_tools->get_post_statuses(); 
  354.  
  355. $output .= '<select data-placeholder="' . esc_attr__( 'Filter by status', WYSIJA ) . '" class="mailpoet-field-select2-simple post_status" data-minimum-results-for-search="-1" id="post_status" name="post_status" width="150">'; 
  356. $output .= '<option></option>'; // This is require because of Select2 placeholding structure 
  357. foreach ( $statuses as $key => $label ) { 
  358. $is_selected = ( $current_status === $key ) ? 'selected="selected"' : ''; 
  359. $output .= '<option value="' . $key . '" ' . $is_selected . '>' . $label . '</option>'; 
  360. $output .= '</select>'; 
  361. return $output;