/wp-content/plugins/subscribe2/classes/class-s2-form-widget.php

https://github.com/livinglab/openlab · PHP · 198 lines · 167 code · 14 blank · 17 comment · 14 complexity · 1f9a564cdd6e0da64b37d845dac03a34 MD5 · raw file

  1. <?php
  2. class S2_Form_Widget extends WP_Widget {
  3. // Display the widget’s instance in the REST API
  4. public $show_instance_in_rest = true;
  5. /**
  6. * Declares the Subscribe2 widget class.
  7. */
  8. public function __construct() {
  9. $widget_ops = array(
  10. 'classname' => 's2_form_widget',
  11. 'description' => esc_html__( 'Sidebar Widget for Subscribe2', 'subscribe2' ),
  12. 'customize_selective_refresh' => true,
  13. 'show_instance_in_rest' => true,
  14. );
  15. // add_filter( 'widget_text', 'shortcode_unautop' );
  16. // add_filter( 'widget_text', 'do_shortcode' );
  17. $control_ops = array(
  18. 'width' => 250,
  19. 'height' => 300,
  20. );
  21. parent::__construct( 's2_form_widget', esc_html__( 'Subscribe2 Widget', 'subscribe2' ), $widget_ops, $control_ops );
  22. }
  23. /**
  24. * Displays the Widget
  25. */
  26. public function widget( $args, $instance ) {
  27. $title = empty( $instance['title'] ) ? __( 'Subscribe2', 'subscribe2' ) : $instance['title'];
  28. $div = empty( $instance['div'] ) ? 'search' : $instance['div'];
  29. $widgetprecontent = empty( $instance['widgetprecontent'] ) ? '' : $instance['widgetprecontent'];
  30. $widgetpostcontent = empty( $instance['widgetpostcontent'] ) ? '' : $instance['widgetpostcontent'];
  31. $textbox_size = empty( $instance['size'] ) ? 20 : $instance['size'];
  32. $hidebutton = empty( $instance['hidebutton'] ) ? 'none' : $instance['hidebutton'];
  33. $postto = empty( $instance['postto'] ) ? '' : $instance['postto'];
  34. $js = empty( $instance['js'] ) ? '' : $instance['js'];
  35. $noantispam = empty( $instance['noantispam'] ) ? '' : $instance['noantispam'];
  36. $nowrap = empty( $instance['nowrap'] ) ? '' : $instance['nowrap'];
  37. $hide = '';
  38. if ( 'subscribe' === $hidebutton || 'unsubscribe' === $hidebutton ) {
  39. $hide = ' hide="' . $hidebutton . '"';
  40. } elseif ( 'link' === $hidebutton ) {
  41. $hide = ' link="' . __( '(Un)Subscribe to Posts', 'subscribe2' ) . '"';
  42. }
  43. $postid = '';
  44. if ( ! empty( $postto ) ) {
  45. $postid = ' id="' . $postto . '"';
  46. }
  47. $size = ' size="' . $textbox_size . '"';
  48. $nojs = '';
  49. if ( $js ) {
  50. $nojs = ' nojs="true"';
  51. }
  52. if ( $noantispam ) {
  53. $noantispam = ' noantispam="true"';
  54. }
  55. if ( $nowrap ) {
  56. $nowrap = ' wrap="false"';
  57. }
  58. $shortcode = '[subscribe2' . $hide . $postid . $size . $nojs . $noantispam . $nowrap . ' widget="true"]';
  59. echo wp_kses_post( $args['before_widget'] );
  60. if ( ! empty( $title ) ) {
  61. echo wp_kses_post( $args['before_title'] ) . esc_attr( $title ) . wp_kses_post( $args['after_title'] );
  62. }
  63. echo '<div class="' . esc_attr( $div ) . '">';
  64. if ( ! empty( $widgetprecontent ) ) {
  65. echo wp_kses_post( $widgetprecontent );
  66. }
  67. echo do_shortcode( $shortcode );
  68. if ( ! empty( $widgetpostcontent ) ) {
  69. echo wp_kses_post( $widgetpostcontent );
  70. }
  71. echo '</div>';
  72. echo wp_kses_post( $args['after_widget'] );
  73. }
  74. /**
  75. * Saves the widgets settings.
  76. */
  77. public function update( $new_instance, $old_instance ) {
  78. $instance = $old_instance;
  79. $instance['title'] = wp_strip_all_tags( stripslashes( $new_instance['title'] ) );
  80. $instance['div'] = wp_strip_all_tags( stripslashes( $new_instance['div'] ) );
  81. $instance['widgetprecontent'] = stripslashes( $new_instance['widgetprecontent'] );
  82. $instance['widgetpostcontent'] = stripslashes( $new_instance['widgetpostcontent'] );
  83. $instance['size'] = intval( stripslashes( $new_instance['size'] ) );
  84. $instance['hidebutton'] = wp_strip_all_tags( stripslashes( $new_instance['hidebutton'] ) );
  85. $instance['postto'] = stripslashes( $new_instance['postto'] );
  86. $instance['js'] = stripslashes( $new_instance['js'] );
  87. $instance['noantispam'] = stripslashes( $new_instance['noantispam'] );
  88. $instance['nowrap'] = stripslashes( $new_instance['nowrap'] );
  89. return $instance;
  90. }
  91. /**
  92. * Creates the edit form for the widget.
  93. */
  94. public function form( $instance ) {
  95. // set some defaults, getting any old options first
  96. $options = get_option( 'widget_subscribe2widget' );
  97. if ( false === $options ) {
  98. $defaults = array(
  99. 'title' => 'Subscribe2',
  100. 'div' => 'search',
  101. 'widgetprecontent' => '',
  102. 'widgetpostcontent' => '',
  103. 'size' => 20,
  104. 'hidebutton' => 'none',
  105. 'postto' => '',
  106. 'js' => '',
  107. 'noantispam' => '',
  108. 'nowrap' => '',
  109. );
  110. } else {
  111. $defaults = array(
  112. 'title' => $options['title'],
  113. 'div' => $options['div'],
  114. 'widgetprecontent' => $options['widgetprecontent'],
  115. 'widgetpostcontent' => $options['widgetpostcontent'],
  116. 'size' => $options['size'],
  117. 'hidebutton' => $options['hidebutton'],
  118. 'postto' => $options['postto'],
  119. 'js' => $options['js'],
  120. 'noantispam' => $options['noantispam'],
  121. 'nowrap' => $options['nowrap'],
  122. );
  123. delete_option( 'widget_subscribe2widget' );
  124. }
  125. // code to obtain old settings too
  126. $instance = wp_parse_args( (array) $instance, $defaults );
  127. $title = htmlspecialchars( $instance['title'], ENT_QUOTES );
  128. $div = htmlspecialchars( $instance['div'], ENT_QUOTES );
  129. $widgetprecontent = htmlspecialchars( $instance['widgetprecontent'], ENT_QUOTES );
  130. $widgetpostcontent = htmlspecialchars( $instance['widgetpostcontent'], ENT_QUOTES );
  131. $size = htmlspecialchars( $instance['size'], ENT_QUOTES );
  132. $hidebutton = htmlspecialchars( $instance['hidebutton'], ENT_QUOTES );
  133. $postto = htmlspecialchars( $instance['postto'], ENT_QUOTES );
  134. $js = htmlspecialchars( $instance['js'], ENT_QUOTES );
  135. $noantispam = htmlspecialchars( $instance['noantispam'], ENT_QUOTES );
  136. $nowrap = htmlspecialchars( $instance['nowrap'], ENT_QUOTES );
  137. global $wpdb, $mysubscribe2;
  138. $sql = "SELECT ID, post_title FROM $wpdb->posts WHERE post_type='page' AND post_status='publish'";
  139. echo '<div>' . "\r\n";
  140. echo '<p><label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">' . esc_html__( 'Title', 'subscribe2' ) . ':' . "\r\n";
  141. echo '<input class="widefat" id="' . esc_attr( $this->get_field_id( 'title' ) ) . '" name="' . esc_attr( $this->get_field_name( 'title' ) ) . '" type="text" value="' . esc_attr( $title ) . '" /></label></p>' . "\r\n";
  142. echo '<p><label for="' . esc_attr( $this->get_field_id( 'div' ) ) . '">' . esc_html__( 'Div class name', 'subscribe2' ) . ':' . "\r\n";
  143. echo '<input class="widefat" id="' . esc_attr( $this->get_field_id( 'div' ) ) . '" name="' . esc_attr( $this->get_field_name( 'div' ) ) . '" type="text" value="' . esc_attr( $div ) . '" /></label></p>' . "\r\n";
  144. echo '<p><label for="' . esc_attr( $this->get_field_id( 'widgetprecontent' ) ) . '">' . esc_html__( 'Pre-Content', 'subscribe2' ) . ':' . "\r\n";
  145. echo '<textarea class="widefat" id="' . esc_attr( $this->get_field_id( 'widgetprecontent' ) ) . '" name="' . esc_attr( $this->get_field_name( 'widgetprecontent' ) ) . '" rows="2" cols="25">' . esc_attr( $widgetprecontent ) . '</textarea></label></p>' . "\r\n";
  146. echo '<p><label for="' . esc_attr( $this->get_field_id( 'widgetpostcontent' ) ) . '">' . esc_html__( 'Post-Content', 'subscribe2' ) . ':' . "\r\n";
  147. echo '<textarea class="widefat" id="' . esc_attr( $this->get_field_id( 'widgetpostcontent' ) ) . '" name="' . esc_attr( $this->get_field_name( 'widgetpostcontent' ) ) . '" rows="2" cols="25">' . esc_attr( $widgetpostcontent ) . '</textarea></label></p>' . "\r\n";
  148. echo '<p><label for="' . esc_attr( $this->get_field_id( 'size' ) ) . '">' . esc_html__( 'Text Box Size', 'subscribe2' ) . ':' . "\r\n";
  149. echo '<input class="widefat" id="' . esc_attr( $this->get_field_id( 'size' ) ) . '" name="' . esc_attr( $this->get_field_name( 'size' ) ) . '" type="text" value="' . esc_attr( $size ) . '" /></label></p>' . "\r\n";
  150. echo '<p>' . esc_html__( 'Display options', 'subscribe2' ) . ':<br>' . "\r\n";
  151. echo '<label for="' . esc_attr( $this->get_field_id( 'hidebutton' ) ) . 'complete"><input id="' . esc_attr( $this->get_field_id( 'hidebutton' ) ) . 'complete" name="' . esc_attr( $this->get_field_name( 'hidebutton' ) ) . '" type="radio" value="none"' . checked( 'none', $hidebutton, false ) . '/> ' . esc_html__( 'Show complete form', 'subscribe2' ) . '</label>' . "\r\n";
  152. echo '<br><label for="' . esc_attr( $this->get_field_id( 'hidebutton' ) ) . 'subscribe"><input id="' . esc_attr( $this->get_field_id( 'hidebutton' ) ) . 'subscribe" name="' . esc_attr( $this->get_field_name( 'hidebutton' ) ) . '" type="radio" value="subscribe"' . checked( 'subscribe', $hidebutton, false ) . '/> ' . esc_html__( 'Hide Subscribe button', 'subscribe2' ) . '</label>' . "\r\n";
  153. echo '<br><label for="' . esc_attr( $this->get_field_id( 'hidebutton' ) ) . 'unsubscribe"><input id="' . esc_attr( $this->get_field_id( 'hidebutton' ) ) . 'unsubscribe" name="' . esc_attr( $this->get_field_name( 'hidebutton' ) ) . '" type="radio" value="unsubscribe"' . checked( 'unsubscribe', $hidebutton, false ) . '/> ' . esc_html__( 'Hide Unsubscribe button', 'subscribe2' ) . '</label>' . "\r\n";
  154. if ( '1' === $mysubscribe2->subscribe2_options['ajax'] ) {
  155. echo '<br><label for="' . esc_attr( $this->get_field_id( 'hidebutton' ) ) . 'ajax"><input id="' . esc_attr( $this->get_field_id( 'hidebutton' ) ) . 'ajax" name="' . esc_attr( $this->get_field_name( 'hidebutton' ) ) . '" type="radio" value="link"' . checked( 'link', $hidebutton, false ) . '/> ' . esc_html__( 'Show as link', 'subscribe2' ) . '</label>' . "\r\n";
  156. }
  157. echo '</p>' . "\r\n";
  158. echo '<p><label for="' . esc_attr( $this->get_field_id( 'postto' ) ) . '">' . esc_html__( 'Post form content to page', 'subscribe2' ) . ':' . "\r\n";
  159. echo '<select class="widefat" id="' . esc_attr( $this->get_field_id( 'postto' ) ) . '" name="' . esc_attr( $this->get_field_name( 'postto' ) ) . '">' . "\r\n";
  160. echo '<option value="' . esc_attr( $mysubscribe2->subscribe2_options['s2page'] ) . '">' . esc_html__( 'Use Subscribe2 Default', 'subscribe2' ) . '</option>' . "\r\n";
  161. echo '<option value="home"';
  162. if ( 'home' === $postto ) {
  163. echo ' selected="selected"';
  164. }
  165. echo '>' . esc_html__( 'Use Home Page', 'subscribe2' ) . '</option>' . "\r\n";
  166. echo '<option value="self"';
  167. if ( 'self' === $postto ) {
  168. echo ' selected="selected"';
  169. }
  170. echo '>' . esc_html__( 'Use Referring Page', 'subscribe2' ) . '</option>' . "\r\n";
  171. $mysubscribe2->pages_dropdown( $postto );
  172. echo '</select></label></p>' . "\r\n";
  173. echo '<p><label for="' . esc_attr( $this->get_field_id( 'js' ) ) . '">' . esc_html__( 'Disable JavaScript', 'subscribe2' ) . ':' . "\r\n";
  174. echo '<input id="' . esc_attr( $this->get_field_id( 'js' ) ) . '" name ="' . esc_attr( $this->get_field_name( 'js' ) ) . '" value="true" type="checkbox"' . checked( 'true', $js, false ) . '/>';
  175. echo '</label></p>' . "\r\n";
  176. echo '<p><label for="' . esc_attr( $this->get_field_id( 'noantispam' ) ) . '">' . esc_html__( 'Disable Anti-spam measures', 'subscribe2' ) . ':' . "\r\n";
  177. echo '<input id="' . esc_attr( $this->get_field_id( 'noantispam' ) ) . '" name ="' . esc_attr( $this->get_field_name( 'noantispam' ) ) . '" value="true" type="checkbox"' . checked( 'true', $noantispam, false ) . '/>';
  178. echo '</label></p>' . "\r\n";
  179. echo '<p><label for="' . esc_attr( $this->get_field_id( 'nowrap' ) ) . '">' . esc_html__( 'Disable wrapping of form buttons', 'subscribe2' ) . ':' . "\r\n";
  180. echo '<input id="' . esc_attr( $this->get_field_id( 'nowrap' ) ) . '" name ="' . esc_attr( $this->get_field_name( 'nowrap' ) ) . '" value="true" type="checkbox"' . checked( 'true', $nowrap, false ) . '/>';
  181. echo '</label></p>' . "\r\n";
  182. echo '</div>' . "\r\n";
  183. }
  184. } // End S2_Form_widget class