WYSIJA_NL_Widget

The MailPoet Newsletters WYSIJA NL Widget class.

Defined (1)

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

/widgets/wysija_nl.php  
  1. class WYSIJA_NL_Widget extends WP_Widget { 
  2. var $classid = ''; 
  3.  
  4. var $iFrame = false; 
  5.  
  6.  
  7. function __construct( $core_only = false ) { 
  8. static $script_registered; 
  9.  
  10. if ( WYSIJA_SIDE == 'front' ) { 
  11.  
  12. if ( ! $script_registered ) { 
  13. if ( ! isset( $_REQUEST['controller'] ) || ( isset( $_REQUEST['controller'] ) && $_REQUEST['controller'] == 'confirm' && isset( $_REQUEST['wysija-key'] ) ) ) { 
  14. $controller = 'subscribers'; 
  15. } else { 
  16. $controller = $_REQUEST['controller']; 
  17.  
  18. $model_config = WYSIJA::get( 'config', 'model' ); 
  19.  
  20. $this->params_ajax = array( 
  21. 'action' => 'wysija_ajax',  
  22. 'controller' => $controller,  
  23. 'ajaxurl' => admin_url( 'admin-ajax.php', ( $model_config->getValue( 'relative_ajax' ) == '' ? 'admin' : 'relative' ) ),  
  24. ); 
  25.  
  26. $script_registered = true; 
  27.  
  28. if ( $core_only === true ) { 
  29. $this->core_only = true; 
  30.  
  31. $namekey = 'wysija'; 
  32. $title = __( 'MailPoet Subscription Form', WYSIJA ); 
  33. $params = array( 'description' => __( 'Subscription form for your newsletters.', WYSIJA ) ); 
  34. $sizeWindow = array( 'width' => 400 ); 
  35.  
  36. $this->add_translated_default(); 
  37.  
  38. add_action( 'init', array( $this, 'add_translated_default' ) ); 
  39.  
  40. $this->classid = strtolower( str_replace( __CLASS__ . '_', '', get_class( $this ) ) ); 
  41. parent::__construct($namekey, $title, $params, $sizeWindow ); 
  42.  
  43.  
  44. function add_translated_default() { 
  45. $this->name = __( 'MailPoet Subscription Form', WYSIJA ); 
  46. $this->widget_options['description'] = __( 'Subscription form for your newsletters.', WYSIJA ); 
  47.  
  48. //if the js array for the ajax request is set we declare it 
  49. if ( isset( $this->params_ajax ) ) { 
  50. $this->params_ajax['loadingTrans'] = __( 'Loading...', WYSIJA ); 
  51. $this->params_ajax['is_rtl'] = is_rtl(); 
  52. wp_localize_script( 'wysija-front-subscribers', 'wysijaAJAX', $this->params_ajax ); 
  53.  
  54. $config = WYSIJA::get( 'config', 'model' ); 
  55.  
  56. $this->successmsgconf = __( 'Check your inbox now to confirm your subscription.', WYSIJA ); 
  57. $this->successmsgsub = __( 'You\'ve successfully subscribed.', WYSIJA ); 
  58.  
  59. if ( $config->getValue( 'confirm_dbleoptin' ) ) { 
  60. $successmsg = $this->successmsgconf; 
  61. } else { 
  62. $successmsg = $this->successmsgsub; 
  63.  
  64. $this->fields = array( 
  65. 'title' => array( 
  66. 'label' => __( 'Title:', WYSIJA ),  
  67. 'default' => __( 'Subscribe to our Newsletter', WYSIJA ),  
  68. ),  
  69. 'instruction' => array( 
  70. 'label' => '',  
  71. 'default' => __( 'To subscribe to our dandy newsletter simply add your email below. A confirmation email will be sent to you!', WYSIJA ),  
  72. ),  
  73. 'lists' => array( 
  74. 'core' => 1,  
  75. 'label' => __( 'Select list(s):', WYSIJA ),  
  76. 'default' => array( 1 ),  
  77. ),  
  78. 'autoregister' => array( 
  79. 'core' => 1,  
  80. 'label' => __( 'Let subscribers select their lists:', WYSIJA ),  
  81. 'default' => 'not_auto_register',  
  82. ),  
  83. 'customfields' => array( 
  84. 'core' => 1,  
  85. 'label' => __( 'Ask for:', WYSIJA ),  
  86. 'default' => array( 
  87. 'email' => array( 
  88. 'label' => __( 'Email', WYSIJA ),  
  89. ),  
  90. ),  
  91. ),  
  92. 'labelswithin' => array( 
  93. 'core' => 1,  
  94. 'default' => true,  
  95. 'label' => __( 'Display labels in inputs', WYSIJA ),  
  96. 'hidden' => 1,  
  97. ),  
  98. 'submit' => array( 
  99. 'core' => 1,  
  100. 'label' => __( 'Button label:', WYSIJA ),  
  101. 'default' => __( 'Subscribe!', WYSIJA ),  
  102. ),  
  103. 'success' => array( 
  104. 'core' => 1,  
  105. 'label' => __( 'Success message:', WYSIJA ),  
  106. 'default' => $successmsg,  
  107. ),  
  108. 'iframe' => array( 
  109. 'core' => 1,  
  110. 'label' => __( 'iframe version', WYSIJA ) 
  111. ),  
  112. ); 
  113.  
  114.  
  115. function update( $new_instance, $old_instance ) { 
  116. $instance = $old_instance; 
  117.  
  118. // keep all of the fields passed from the new instance 
  119. foreach ( $new_instance as $key => $value ) { 
  120. $instance[ $key ] = $value; 
  121.  
  122. return $instance; 
  123.  
  124. function form( $instance ) { 
  125. $helper_forms = WYSIJA::get( 'forms', 'helper' ); 
  126.  
  127. $html = '<div class="wysija-widget-form">'; 
  128.  
  129. $this->fields = array( 
  130. 'title' => array( 
  131. 'label' => __( 'Title:', WYSIJA ),  
  132. 'default' => __( 'Subscribe to our Newsletter', WYSIJA ) 
  133. ),  
  134. 'select_form' => array( 
  135. 'core' => 1,  
  136. 'label' => __( 'Select a form:', WYSIJA ) 
  137. ),  
  138. 'edit_link' => array( 
  139. 'core' => 1,  
  140. 'label' => __( 'Create a new form', WYSIJA ),  
  141. 'nolabel' => 1,  
  142. ); 
  143.  
  144. $model_forms = WYSIJA::get( 'forms', 'model' ); 
  145. $model_forms->reset(); 
  146. $forms = $model_forms->getRows( array( 'form_id', 'name' ) ); 
  147. if ( empty( $forms ) ) { 
  148. unset($this->fields['title']); 
  149. unset($this->fields['select_form']); 
  150.  
  151. foreach ( $this->fields as $field => $field_params ) { 
  152. $extrascriptLabel = ''; 
  153. $value_field = ''; 
  154.  
  155. if ( ( isset( $field_params['hidden'] ) && $field_params['hidden'] ) || ( isset( $this->core_only ) && $this->core_only && ! isset( $field_params['core'] ) ) ) { 
  156. continue; 
  157.  
  158. if ( isset( $instance[ $field ] ) ) { 
  159.  
  160. if ( $field === 'success' && $instance[ $field ] == $this->successmsgsub . ' ' . $this->successmsgconf ) { 
  161. $config = WYSIJA::get( 'config', 'model' ); 
  162.  
  163. if ( ! $config->getValue( 'confirm_dbleoptin' ) ) { 
  164. $value_field = $this->successmsgsub; 
  165. } else { 
  166. $value_field = $instance[ $field ]; 
  167. } else { 
  168. $value_field = $instance[ $field ]; 
  169. } elseif ( isset( $field_params['default'] ) ) { 
  170. $value_field = $field_params['default']; 
  171.  
  172. $class_div_label = $field_html = ''; 
  173. switch ( $field ) { 
  174. case 'select_form': 
  175. // offer the possibility to select a form 
  176.  
  177. $field_html = '<select name="' . $this->get_field_name( 'form' ) . '" id="' . $this->get_field_id( 'form' ) . '">'; 
  178.  
  179. foreach ( $forms as $form ) { 
  180. $checked = ''; 
  181. // preselect the saved form 
  182. if ( ( ( isset( $instance['form'] ) && (int) $instance['form'] === (int) $form['form_id'] ) ) || ( isset( $instance['default_form'] ) && (int) $instance['default_form'] === (int) $form['form_id'] ) ) { 
  183. $checked = ' selected="selected"'; 
  184. $field_html .= '<option value="'.$form['form_id'].'"'.$checked.'>'.$form['name'].'</option>'; 
  185. $field_html .= '</select>'; 
  186. break; 
  187. case 'edit_link': 
  188.  
  189. $field_html = '<a href="admin.php?page=wysija_config&action=form_add&_wpnonce='.wp_create_nonce('wysija_config-action_form_add').'" target="_blank" title="'.$field_params['label'].'">'.$field_params['label'].'</a>'; 
  190. break; 
  191. default: 
  192. $field_html = $helper_forms->input( 
  193. array( 
  194. 'id' => $this->get_field_id( $field ),  
  195. 'name' => $this->get_field_name( $field ),  
  196. ),  
  197. $value_field,  
  198. ' size="40" ' 
  199. ); 
  200.  
  201. $html .= '<div class="divblocks">'; 
  202. if ( ! isset( $field_params['nolabel'] ) ) { 
  203. $html .= '<div ' . $class_div_label.'><label for="' . $this->get_field_id( $field ) . '" ' . $extrascriptLabel . '>' . $field_params['label'] . '</label></div>'; 
  204. $html .= $field_html; 
  205. $html .= '<div style="clear:both;"></div></div>'; 
  206.  
  207. $html .= '</div>'; 
  208. echo $html; 
  209.  
  210.  
  211. function widget( $args, $instance = null ) { 
  212. extract( $args ); 
  213.  
  214. //in some case we may pass only one argument, in which case we will just assign the first to the second 
  215. if ( $instance === null ) { 
  216. $instance = $args; 
  217. // we need that part on every form for the different instances of a form on the same page 
  218. //widget id should include the type of widget iframe, php, etc... 
  219. if ( isset( $args['widget_id'] ) ) { 
  220. // we come here only for classic wordpress widgetized area 
  221. $instance['id_form'] = str_replace( '_', '-', $args['widget_id'] ); 
  222. } else { 
  223. // we come here everywhere else 
  224. if ( isset( $instance['form'] ) && isset( $instance['form_type'] ) ) { 
  225. //make the id of the form truly unique 
  226. $instance['id_form'] = str_replace( '_', '-', 'wysija-' . uniqid( $instance['form_type'] ) . '-' . $instance['form'] ); 
  227.  
  228. if ( isset( $instance['form'] ) && (int) $instance['form'] > 0 ) { 
  229.  
  230. // new form editor 
  231. $title = ''; 
  232. if ( ! isset( $this->core_only ) ) { 
  233. $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); 
  234. if ( ! isset( $before_widget ) ) { 
  235. $before_widget = ''; 
  236. if ( ! isset( $after_widget ) ) { 
  237. $after_widget = ''; 
  238. if ( ! isset( $before_title ) ) { 
  239. $before_title = ''; 
  240. if ( ! isset( $after_title ) ) { 
  241. $after_title = ''; 
  242.  
  243. if ( ! isset( $this->core_only ) ) { 
  244. $title = $before_title.$title.$after_title; 
  245.  
  246. $view = WYSIJA::get( 'widget_nl', 'view', 'front' ); 
  247.  
  248. $output = $before_widget; 
  249. $output .= $view->display( $title, $instance, false, $this->iFrame ); 
  250. $output .= $after_widget; 
  251.  
  252. if ( $this->iFrame ) { 
  253. $output = $view->wrap( $output ); 
  254.  
  255. if ( isset( $this->core_only ) && $this->core_only ) { 
  256. return $output; 
  257. } else { 
  258. echo $output; 
  259. } else { 
  260. $model_config = WYSIJA::get( 'config', 'model' ); 
  261. //if(!$config->getValue("sending_emails_ok")) return; 
  262.  
  263. foreach ( $this->fields as $field => $field_params ) { 
  264. if ( isset( $this->core_only ) && $this->core_only && ! isset( $field_params['core'] ) ) { 
  265. continue; 
  266. if ( $field == 'success' && !empty($instance[ $field ]) && $instance[ $field ] == $this->successmsgsub . ' ' . $this->successmsgconf ) { 
  267. if ( ! $model_config->getValue( 'confirm_dbleoptin' ) ) { 
  268. $instance[ $field ] = $this->successmsgsub; 
  269.  
  270. if ( ! isset( $this->core_only ) ) { 
  271. $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); 
  272.  
  273. //some worpress weird thing for widgets management 
  274. if ( ! isset( $before_widget ) ) { 
  275. $before_widget = ''; 
  276. if ( ! isset( $after_widget ) ) { 
  277. $after_widget = ''; 
  278. if ( ! isset( $before_title ) ) { 
  279. $before_title = ''; 
  280. if ( ! isset( $after_title ) ) { 
  281. $after_title = ''; 
  282.  
  283. $content_html = $before_widget; 
  284. if ( ! isset( $this->core_only ) && $title ) { 
  285. $title = $before_title . $title . $after_title; 
  286. } else { 
  287. $title = ''; 
  288.  
  289. $view = WYSIJA::get( 'widget_nl', 'view', 'front' ); 
  290. $content_html .= $view->display( $title, $instance, false, $this->iFrame ); 
  291.  
  292. $content_html .= $after_widget; 
  293.  
  294. if ( $this->iFrame ) { 
  295. $content_html = $view->wrap( $content_html ); 
  296.  
  297. if ( isset( $this->core_only ) && $this->core_only ) { 
  298. return $content_html; 
  299. } else { 
  300. echo $content_html;