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

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

https://gitlab.com/Gashler/sg
PHP | 289 lines | 200 code | 49 blank | 40 comment | 54 complexity | 9e788d8c42c251c07c67e28027539f2b MD5 | raw file
  1. <?php
  2. defined('WYSIJA') or die('Restricted access');
  3. /**
  4. * class managing the admin vital part to integrate
  5. */
  6. class WYSIJA_help_front extends WYSIJA_help{
  7. function WYSIJA_help_front(){
  8. parent::WYSIJA_help();
  9. // wysija form shortcode
  10. add_shortcode('wysija_form', array($this,'scan_form_shortcode'));
  11. // wysija total of subscribers shortcode
  12. add_shortcode('wysija_subscribers_count', array($this,'scan_subscribers_count_shortcode'));
  13. // init shortcode [wysija_archive]
  14. require_once(WYSIJA_CORE.'controller.php');
  15. require_once(WYSIJA_CORE.'module'.DS.'module.php');// @todo: move to autoloader
  16. $archive_std = WYSIJA_module::get_instance_by_name('archive_std');// implement hook "wysija_front_init()
  17. if (!empty($archive_std) && is_a($archive_std, 'WYSIJA_module'))
  18. $archive_std->front_init();
  19. /* We try to process the least possible code */
  20. if(isset($_REQUEST['wysija-page']) || isset($_REQUEST['wysija-launch'])){
  21. if(defined('DOING_AJAX')){
  22. add_action('wp_ajax_nopriv_wysija_ajax', array($this, 'ajax'));
  23. }else{
  24. $paramscontroller=$_REQUEST['controller'];
  25. //this is an exception on one server this params stats was not accepted
  26. if($paramscontroller=='stat') $paramscontroller='stats';
  27. $this->controller=WYSIJA::get($paramscontroller,'controller');
  28. if(isset($_REQUEST['action']) && method_exists($this->controller, $_REQUEST['action'])){
  29. add_action('init',array($this->controller,$_REQUEST['action']));
  30. //$this->controller->$_REQUEST['action']();
  31. }else $this->error('Action does not exist.');
  32. if(isset($_REQUEST['wysija-page'])){
  33. /* set the content filter to replace the shortcode */
  34. add_filter('wp_title', array($this,'meta_page_title'));
  35. add_filter( 'the_title', array($this,'scan_title'));
  36. add_filter( 'the_content', array($this,'scan_content'),98);
  37. if(isset($_REQUEST['message_success'])){
  38. add_filter( 'the_content', array($this,'scan_content_NLform'),99 );
  39. }
  40. }
  41. }
  42. }else{
  43. add_filter('the_content', array($this,'scan_content_NLform'),99 );
  44. //if the comment form checkbox option is activated we add some hooks to process it
  45. $model_config = WYSIJA::get('config','model');
  46. if($model_config->getValue('commentform')){
  47. add_action('comment_form', array($this,'comment_form_extend'));
  48. add_action('comment_post', array($this,'comment_posted'), 60,2);
  49. }
  50. // if the register form checkbox option is activated we add some hooks to process it
  51. if($model_config->getValue('registerform')){
  52. if(is_multisite()){
  53. add_action('signup_extra_fields', array($this,'register_form_extend'));
  54. // we need this condition otherwise we will send two confirmation emails when on ms with buddypress
  55. if(!WYSIJA::is_plugin_active('buddypress/bp-loader.php')){
  56. add_filter('wpmu_validate_user_signup', array($this,'registerms_posted'), 60,3);
  57. }
  58. }else{
  59. add_action('register_form', array($this,'register_form_extend'));
  60. add_action('register_post', array($this,'register_posted'), 60,3);
  61. }
  62. // special case when buddypress is activated
  63. if(WYSIJA::is_plugin_active('buddypress/bp-loader.php')){
  64. add_action('bp_after_signup_profile_fields', array($this,'register_form_bp_extend'));
  65. add_action('bp_signup_validate', array($this,'register_bp'),60,3);
  66. // we can have just one confirmation email for the wp user and the wysija confirmation when bp and multisite are activated
  67. if(is_multisite()){
  68. add_action('wpmu_activate_user', array($this,'wpmu_activate_user'));
  69. }
  70. }
  71. }
  72. }
  73. }
  74. /**
  75. * In MS when user account is activated we auto confirm the subscriber
  76. * @param type $wpuser_id
  77. * @return boolean
  78. */
  79. function wpmu_activate_user($wpuser_id){
  80. if((int)$wpuser_id>0){
  81. $model_user = WYSIJA::get('user','model');
  82. $result_subscriber = $model_user->getOne(false , array('wpuser_id'=>$wpuser_id));
  83. if(!empty($result_subscriber)){
  84. $helper_user = WYSIJA::get('user','helper');
  85. $helper_user->confirm_user($result_subscriber['user_id']);
  86. }
  87. }
  88. return true;
  89. }
  90. function meta_page_title($title){
  91. //Here I can echo the result and see that it's actually triggered
  92. if($this->is_default_mailpoet_page()){
  93. // when user have kept the default page we modify the title based on the controller
  94. if(!empty($this->controller->title)) return $this->controller->title;
  95. }
  96. return $title;
  97. }
  98. function register_form_bp_extend(){
  99. if ( !is_user_logged_in()){
  100. $this->register_form_extend();
  101. }
  102. }
  103. function register_form_extend(){
  104. $checkbox= '<p class="wysija-after-register">';
  105. $checkbox.='<label for="wysija-box-after-register">';
  106. $checkbox.='<input type="checkbox" id="wysija-box-after-register" value="1" name="wysija[register_subscribe]">';
  107. $mConfig=WYSIJA::get('config','model');
  108. $checkbox.=$mConfig->getValue('registerform_linkname').'</label></p>';
  109. echo '<div class="register-section" id="profile-details-section-wysija"><div class="editfield">'.$checkbox.'</div></div>';
  110. }
  111. function register_bp(){
  112. global $bp;
  113. if ( !isset($bp->signup->errors) && isset($_POST['wysija']['register_subscribe']) && $_POST['wysija']['register_subscribe'] ) {
  114. $model_config=WYSIJA::get('config','model');
  115. $helper_user=WYSIJA::get('user','helper');
  116. $data=array('user'=>array('email'=>$bp->signup->email),'user_list'=>array('list_ids'=>$model_config->getValue('registerform_lists')));
  117. if(is_multisite()){
  118. $helper_user->no_confirmation_email=true;
  119. }
  120. $helper_user->addSubscriber($data);
  121. }
  122. }
  123. function registerms_posted($result){
  124. if ( empty($result['errors']->errors) && isset($_POST['wysija']['register_subscribe']) && $_POST['wysija']['register_subscribe']) {
  125. $mConfig=WYSIJA::get('config','model');
  126. $userHelper=WYSIJA::get('user','helper');
  127. $data=array('user'=>array('email'=>$result['user_email']),'user_list'=>array('list_ids'=>$mConfig->getValue('registerform_lists')));
  128. $userHelper->addSubscriber($data);
  129. }
  130. return $result;
  131. }
  132. function register_posted($login,$email,$errors){
  133. if ( empty($errors->errors) && isset($_POST['wysija']['register_subscribe']) && $_POST['wysija']['register_subscribe']) {
  134. $mConfig=WYSIJA::get('config','model');
  135. $userHelper=WYSIJA::get('user','helper');
  136. $data=array('user'=>array('email'=>$email),'user_list'=>array('list_ids'=>$mConfig->getValue('registerform_lists')));
  137. $userHelper->addSubscriber($data);
  138. }
  139. }
  140. function comment_form_extend(){
  141. echo '<p class="wysija-after-comment">';
  142. echo '<label for="wysija-box-after-comment">';
  143. echo '<input type="checkbox" id="wysija-box-after-comment" value="1" name="wysija[comment_subscribe]">';
  144. $mConfig=WYSIJA::get('config','model');
  145. echo $mConfig->getValue('commentform_linkname').'</label></p>';
  146. }
  147. function comment_posted($cid,$comment){
  148. $cid = (int) $cid;
  149. if ( !is_object($comment) )
  150. $comment = get_comment($cid);
  151. //before recording the subscriber, make sure that it is not a spam comment or it needs to be approved first
  152. if($comment->comment_approved=='spam') return;
  153. if(isset($_POST['wysija']['comment_subscribe']) && $_POST['wysija']['comment_subscribe']) {
  154. if($comment->comment_approved=='0') add_comment_meta($cid, 'wysija_comment_subscribe', 1);
  155. else{
  156. $mConfig=WYSIJA::get('config','model');
  157. $userHelper=WYSIJA::get('user','helper');
  158. $data=array('user'=>array('email'=>$comment->comment_author_email,'firstname'=>$comment->comment_author),'user_list'=>array('list_ids'=>$mConfig->getValue('commentform_lists')));
  159. $userHelper->addSubscriber($data);
  160. }
  161. }
  162. }
  163. function scan_title($title){
  164. /*careful WordPress global*/
  165. global $post;
  166. if($this->is_default_mailpoet_page()){
  167. // when user have kept the default page we modify the title based on the controller
  168. if(trim($title)==trim(single_post_title( '', false )) && !empty($this->controller->title)){
  169. $post->comment_status='close';
  170. $post->post_password='';
  171. return $this->controller->title;
  172. }
  173. }
  174. return $title;
  175. }
  176. function scan_content($content){
  177. $wysija_content = $wysija_undo = '';
  178. if(!empty($this->controller->subtitle)) $wysija_content = $this->controller->subtitle;
  179. if(!empty($this->controller->optional_subtitle)) $wysija_content = $this->controller->optional_subtitle;
  180. if(!empty($this->controller->undo_unsubscribe)){
  181. $wysija_undo = $this->controller->undo_unsubscribe;
  182. }
  183. // only force our edit subscription screen at the bottom of the content of the page
  184. // if it's the right action and there is no wysija_page shortcode in teh page
  185. if(!empty($_REQUEST['action']) && $_REQUEST['action'] == 'subscriptions' && strpos($content, '[wysija_page]') === false){
  186. // we append the subscription form at the bottom of the page if we can't detect it
  187. return $content.'<div class="mpoet_profile_edit">'.$wysija_content.'</div>';
  188. }else{
  189. // we replace the shortcode by our automatic content, other wise if there is no shortcode the page stays the same
  190. return str_replace('[wysija_page]', $wysija_content.$wysija_undo, $content);
  191. }
  192. }
  193. function is_default_mailpoet_page(){
  194. global $post;
  195. $model_config = WYSIJA::get('config','model');
  196. if($post->ID == $model_config->getValue('confirm_email_link')) return true;
  197. else return false;
  198. }
  199. /**
  200. * this is for the new kind of shortcodes [wysija_form form="1"]
  201. * @param array $attributes
  202. * @return string html
  203. */
  204. function scan_form_shortcode($attributes) {
  205. // IMPORTANT: this is to make sure MagicMember won't scan our form and find [user_list] as a code they should replace.
  206. remove_shortcode('user_list');
  207. if(isset($attributes['id']) && (int)$attributes['id']>0){
  208. $widget_data=array();
  209. $widget_data['form']=(int)$attributes['id'];
  210. $widget_data['form_type'] = 'shortcode';
  211. $widget_NL=new WYSIJA_NL_Widget(true);
  212. return $widget_NL->widget($widget_data);
  213. }
  214. return '';
  215. }
  216. /**
  217. * this is for the new kind of shortcodes [wysija_form form="1"]
  218. * @param array $attributes
  219. * @return string html
  220. */
  221. function scan_subscribers_count_shortcode($attributes) {
  222. $user = WYSIJA::get('user','model');
  223. $list_ids = !empty($attributes['list_id']) ? explode(',', $attributes['list_id']) : array();
  224. $confirmed_subscribers = !empty($attributes['confirmed_subscribers']) ? (bool)$attributes['confirmed_subscribers'] : true;
  225. return $user->countSubscribers($list_ids, $confirmed_subscribers);
  226. }
  227. function scan_content_NLform($content){
  228. preg_match_all('/\<div class="wysija-register">(.*?)\<\/div>/i',$content,$matches);
  229. if(!empty($matches[1]) && count($matches[1])>0) require_once(WYSIJA_WIDGETS.'wysija_nl.php');
  230. foreach($matches[1] as $key => $mymatch){
  231. if($mymatch){
  232. $widgetdata=unserialize(base64_decode($mymatch));
  233. $widgetNL=new WYSIJA_NL_Widget(true);
  234. $contentTABLE= $widgetNL->widget($widgetdata,$widgetdata);
  235. $content=str_replace($matches[0][$key],$contentTABLE,$content);
  236. }//endif
  237. }//endforeach
  238. return $content;
  239. }
  240. }