PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wysija-newsletters/widgets/wysija_nl.php

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