WYSIJA_help_shortcodes

The MailPoet Newsletters WYSIJA help shortcodes class.

Defined (1)

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

/helpers/shortcodes.php  
  1. class WYSIJA_help_shortcodes extends WYSIJA_object { 
  2.  
  3. // Email object. 
  4. private $email; 
  5. // Receiver object. 
  6. private $receiver; 
  7. // User model. 
  8. private $userM; 
  9. // Shortcodes found. 
  10. private $find; 
  11. // Replacement values for shortcodes found. 
  12. private $replace; 
  13.  
  14. function __construct() { 
  15. parent::__construct(); 
  16.  
  17. /** 
  18. * This serves for custom: shortcodes only 
  19. * @return type 
  20. */ 
  21. function getReceiver() { 
  22. return $this->receiver; 
  23.  
  24. // Main function. Call it assigning an Email object and a Receiver object. 
  25. private function initialize($email, $receiver) { 
  26.  
  27. // Set current object properties. 
  28. $this->email = $email; 
  29. $this->receiver = $receiver; 
  30. $this->userM = WYSIJA::get('user', 'model'); 
  31. $this->userM->getFormat = OBJECT; 
  32. $this->find = array(); 
  33. $this->replace = array(); 
  34.  
  35.  
  36. public function replace_body($email, $receiver = NULL) { 
  37.  
  38. $this->initialize($email, $receiver); 
  39.  
  40. $body_tags = $this->email->tags; 
  41. $this->loop_tags($body_tags); 
  42. $replaced_body = str_replace($this->find, $this->replace, $this->email->body); 
  43. return $replaced_body; 
  44.  
  45.  
  46. public function replace_subject($email, $receiver = NULL) { 
  47.  
  48. $this->initialize($email, $receiver); 
  49.  
  50. $subject_tags = $this->email->subject_tags; 
  51. $this->loop_tags($subject_tags); 
  52. $replaced_subject = str_replace($this->find, $this->replace, $this->email->subject); 
  53. return $replaced_subject; 
  54.  
  55.  
  56.  
  57. /** 
  58. * Loop through tags and fetch data (replacement) of each tag 
  59. * @param array $tags 
  60. * [[user:displayname | default:myvalue]] => Array <br/> 
  61. (<br/> 
  62. [0] => Array<br/> 
  63. (<br/> 
  64. [0] => user<br/> 
  65. [1] => displayname<br/> 
  66. )<br/> 
  67. [1] => Array<br/> 
  68. (<br/> 
  69. [0] => default<br/> 
  70. [1] => myvalue<br/> 
  71. )<br/> 
  72. )<br/> 
  73. */ 
  74. private function loop_tags(Array $tags) { 
  75. $this->find = array(); 
  76. $this->replace = array(); 
  77.  
  78. // Loop through the shortcodes array and call private group functions. 
  79. foreach($tags as $tag_find => $tag_replace) { 
  80. foreach($tag_replace as $couple_value) { 
  81. $couple_value[0] = strip_tags($couple_value[0]);// strip html tags 
  82. $couple_value[1] = strip_tags($couple_value[1]);// strip html tags 
  83. switch ($couple_value[0]) { 
  84.  
  85. // [user:xxx | default:xxx] 
  86. case 'user': 
  87. $replacement = $this->replace_user_shortcodes($couple_value[1]); 
  88. // "subscriber" or "member" means: we don't find out a right value for the tag. 
  89. // The next loop should be the tag "default". 
  90. // Let's "continue" and get the default value instead. 
  91. if ($replacement === 'subscriber' || $replacement === 'member') { 
  92. continue; 
  93. break(2); 
  94. case 'default': 
  95. $replacement = $couple_value[1]; 
  96. break(2); 
  97.  
  98. // [newsletter:xxx] 
  99. case 'newsletter': 
  100. $replacement = $this->replace_newsletter_shortcodes($couple_value[1]); 
  101. break; 
  102.  
  103. // [date:xxx] 
  104. case 'date': 
  105. $replacement = $this->replace_date_shortcodes($couple_value[1]); 
  106. break; 
  107.  
  108. // [global:xxx] 
  109. case 'global': 
  110. $replacement = $this->replace_global_shortcodes($couple_value[1]); 
  111. break; 
  112.  
  113. // [custom:xxx] 
  114. case 'custom': 
  115. $replacement = $this->replace_custom_shortcodes($couple_value[1]); 
  116. break; 
  117.  
  118. // [field:xxx] 
  119. case 'field': 
  120. $replacement = $this->replace_cfield_shortcodes($couple_value[1]); 
  121. break; 
  122.  
  123. default: 
  124. break; 
  125.  
  126. if (isset($replacement)) { 
  127. $this->find[] = $tag_find; 
  128. $this->replace[] = $replacement; 
  129. } else { 
  130. $this->find[] = $tag_find; 
  131. $this->replace[] = do_shortcode($tag_find); 
  132. $replacement = ''; 
  133.  
  134.  
  135.  
  136. // [user:firstname] 
  137. // [user:lastname] 
  138. // [user:email] 
  139. // [user:displayname] 
  140. // [user:count] 
  141. private function replace_user_shortcodes($tag_value) { 
  142. $replacement = ''; 
  143. if (($tag_value === 'firstname') || ($tag_value === 'lastname') || ($tag_value === 'email')) { 
  144. if(isset($this->receiver->$tag_value) && $this->receiver->$tag_value) { 
  145. // uppercase the initials of the first name and last name when replacing it 
  146. if (($tag_value === 'firstname') || ($tag_value === 'lastname')) { 
  147. $use_default_case = apply_filters('mpoet_shortcode_names_default_case', false); 
  148. if ($use_default_case) { 
  149. $replacement = $this->receiver->$tag_value; 
  150. } else { 
  151. if (function_exists('mb_convert_case') && function_exists('mb_strtolower')) { 
  152. // http://stackoverflow.com/questions/9823703/using-ucwords-for-non-english-characters 
  153. $replacement = mb_convert_case(mb_strtolower($this->receiver->$tag_value), MB_CASE_TITLE, 'UTF-8'); 
  154. } else { 
  155. $replacement = ucwords(strtolower($this->receiver->$tag_value)); 
  156. }else{ 
  157. $replacement = $this->receiver->$tag_value; 
  158. } else { 
  159. $replacement = 'subscriber'; 
  160.  
  161. if ($tag_value === 'displayname') { 
  162. $replacement = 'member'; 
  163. if(!empty($this->receiver->wpuser_id)) 
  164. $user_info = get_userdata($this->receiver->wpuser_id); 
  165. if(!empty($user_info->display_name) && $user_info->display_name != false) { 
  166. $replacement = $user_info->display_name; 
  167. } elseif(!empty($user_info->user_nicename) && $user_info->user_nicename != false) { 
  168. $replacement = $user_info->user_nicename; 
  169. if ($tag_value === 'count') { 
  170. $model_config = WYSIJA::get('config', 'model'); 
  171. $replacement = $model_config->getValue('total_subscribers'); 
  172.  
  173. return $replacement; 
  174.  
  175.  
  176. // [global:unsubscribe] 
  177. // [global:manage] 
  178. // [global:browser] 
  179. private function replace_global_shortcodes($tag_value) { 
  180. $replacement = ''; 
  181. if (($tag_value === 'unsubscribe')) { 
  182. $replacement = $this->userM->getUnsubLink($this->receiver); 
  183.  
  184. if ($tag_value === 'manage') { 
  185. $replacement = $this->userM->getEditsubLink($this->receiver); 
  186.  
  187. if ($tag_value === 'browser') { 
  188. $replacement = ''; 
  189.  
  190. return $replacement; 
  191.  
  192.  
  193. // [newsletter:subject] 
  194. // [newsletter:total] 
  195. // [newsletter:post_title] 
  196. // [newsletter:number] 
  197. private function replace_newsletter_shortcodes($tag_value) { 
  198. $replacement = ''; 
  199. switch ($tag_value) { 
  200. case 'subject': 
  201. $replacement = $this->email->subject; 
  202. break; 
  203.  
  204. case 'total': 
  205. if(isset($this->email->params['autonl']['articles']['count'])) { 
  206. $replacement = $this->email->params['autonl']['articles']['count']; 
  207. break; 
  208.  
  209. case 'post_title': 
  210. if(isset($this->email->params['autonl']['articles']['first_subject'])) { 
  211. $replacement = $this->email->params['autonl']['articles']['first_subject']; 
  212. break; 
  213.  
  214. case 'number': 
  215. // number is the issue number not the number of articles that were sent since the beginning. 
  216. $replacement = (isset($this->email->params['autonl']['total_child']) ? (int)$this->email->params['autonl']['total_child'] : 1); 
  217. break; 
  218.  
  219. default: 
  220. $replacement = ''; 
  221. break; 
  222.  
  223. return $replacement; 
  224.  
  225.  
  226. // [date:d] 
  227. // [date:m] 
  228. // [date:y] 
  229. // [date:dtext] 
  230. // [date:mtext] 
  231. // [date:dordinal] 
  232. private function replace_date_shortcodes($tag_value) { 
  233.  
  234. $current_time = current_time('timestamp'); 
  235.  
  236. switch ($tag_value) { 
  237. case 'd': 
  238. $replacement = date_i18n( 'j', $current_time); 
  239. break; 
  240.  
  241. case 'm': 
  242. $replacement = date_i18n( 'n', $current_time); 
  243. break; 
  244.  
  245. case 'y': 
  246. $replacement = date_i18n( 'Y', $current_time); 
  247. break; 
  248.  
  249. case 'dtext': 
  250. $replacement = date_i18n( 'l', $current_time); 
  251. break; 
  252.  
  253. case 'mtext': 
  254. $replacement = date_i18n( 'F', $current_time); 
  255. break; 
  256.  
  257. case 'dordinal': 
  258. $replacement = date_i18n( 'jS', $current_time); 
  259. break; 
  260.  
  261. default: 
  262. $replacement = ''; 
  263. break; 
  264.  
  265. return $replacement; 
  266.  
  267.  
  268. /** 
  269. * We pass the value of the tag, the string after custom: 
  270. * To the external filter, and we expect the filter to return a string. 
  271. */ 
  272. // [custom:xxx] 
  273. private function replace_custom_shortcodes($tag_value) { 
  274. $user_id = 0; 
  275.  
  276. if (!empty($this->receiver) && isset($this->receiver->user_id)) { 
  277. $user_id = (int) $this->receiver->user_id; 
  278.  
  279. return apply_filters('wysija_shortcodes', $tag_value, $user_id); 
  280.  
  281. // [field:1] 
  282. private function replace_cfield_shortcodes($tag_value) { 
  283. $replacement = ''; 
  284. if (isset($tag_value)) { 
  285. $user_id = (int) $this->receiver->user_id; 
  286. $field_id = (int) $tag_value; 
  287. $field_user = new WJ_FieldUser(); 
  288. $field_user->set(array( 
  289. 'user_id' => $user_id,  
  290. 'field_id' => $field_id 
  291. )); 
  292. $value = $field_user->value(); 
  293. // If we don't have a value, we return the empty string. 
  294. if (isset($value)) { 
  295. // Check if the field value needs formatting output. 
  296. switch ($field_user->field->type) { 
  297. case 'checkbox': 
  298. if ($value == 1) { 
  299. $value = "Yes"; 
  300. } else { 
  301. $value ="No"; 
  302. break; 
  303. case 'date': 
  304. $value = date('F j, Y', $value); 
  305. break; 
  306. default: 
  307. break; 
  308. $replacement = $value; 
  309. } else { 
  310. $replacement = ''; 
  311. return $replacement; 
  312.