standard.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. defined('ABSPATH') || exit;
  3. if (version_compare(phpversion(), '5.3', '<')) {
  4. return;
  5. }
  6. /**
  7. * Newsletter widget version 2.0: it'll replace the old version left for compatibility.
  8. */
  9. class NewsletterWidget extends WP_Widget {
  10. function __construct() {
  11. parent::__construct(false, $name = 'Newsletter', array('description' => 'Newsletter widget to add subscription forms on sidebars'), array('width' => '350px'));
  12. }
  13. static function get_widget_form($instance) {
  14. $field_wrapper_tag = 'div';
  15. if (!isset($instance['nl']) || !is_array($instance['nl']))
  16. $instance['nl'] = array();
  17. $instance = array_merge(array('lists_layout' => '',
  18. 'lists_empty_label' => '',
  19. 'lists_field_label' => ''), $instance);
  20. $options_profile = get_option('newsletter_profile');
  21. $form = '';
  22. $form .= '<div class="tnp tnp-widget">';
  23. $form .= NewsletterSubscription::instance()->get_subscription_form('widget', null, array(
  24. 'list' => implode(',', $instance['nl']),
  25. 'lists_field_layout' => $instance['lists_layout'],
  26. 'lists_field_empty_label' => $instance['lists_empty_label'],
  27. 'lists_field_label' => $instance['lists_field_label'],
  28. ));
  29. $form .= "</div>\n";
  30. return $form;
  31. }
  32. function widget($args, $instance) {
  33. $newsletter = Newsletter::instance();
  34. extract($args);
  35. if (empty($instance))
  36. $instance = array();
  37. $instance = array_merge(array('text' => '', 'title' => ''), $instance);
  38. echo $before_widget;
  39. // Filters are used for WPML
  40. if (!empty($instance['title'])) {
  41. $title = apply_filters('widget_title', $instance['title'], $instance);
  42. echo $before_title . $title . $after_title;
  43. }
  44. $buffer = apply_filters('widget_text', $instance['text'], $instance);
  45. $options = get_option('newsletter');
  46. $options_profile = get_option('newsletter_profile');
  47. if (stripos($instance['text'], '<form') === false) {
  48. $form = NewsletterWidget::get_widget_form($instance);
  49. // Canot user directly the replace, since the form is different on the widget...
  50. if (strpos($buffer, '{subscription_form}') !== false)
  51. $buffer = str_replace('{subscription_form}', $form, $buffer);
  52. else {
  53. if (strpos($buffer, '{subscription_form_') !== false) {
  54. // TODO: Optimize with a method to replace only the custom forms
  55. $buffer = $newsletter->replace($buffer);
  56. } else {
  57. $buffer .= $form;
  58. }
  59. }
  60. } else {
  61. $buffer = str_ireplace('<form', '<form method="post" action="' . esc_attr($newsletter->get_subscribe_url()) . '" onsubmit="return newsletter_check(this)"', $buffer);
  62. $buffer = str_ireplace('</form>', '<input type="hidden" name="nr" value="widget"/></form>', $buffer);
  63. }
  64. // That replace all the remaining tags
  65. $buffer = $newsletter->replace($buffer);
  66. echo $buffer;
  67. echo $after_widget;
  68. }
  69. function update($new_instance, $old_instance) {
  70. return $new_instance;
  71. }
  72. function form($instance) {
  73. if (!is_array($instance)) {
  74. $instance = array();
  75. }
  76. $instance = array_merge(array('title' => '', 'text' => '', 'lists_layout' => '', 'lists_empty_label' => '', 'lists_field_label' => ''), $instance);
  77. $options_profile = get_option('newsletter_profile');
  78. if (!isset($instance['nl']) || !is_array($instance['nl']))
  79. $instance['nl'] = array();
  80. ?>
  81. <p>
  82. <label for="<?php echo $this->get_field_id('title'); ?>">
  83. <?php _e('Title')?>:
  84. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($instance['title']); ?>" />
  85. </label>
  86. <label for="<?php echo $this->get_field_id('text'); ?>">
  87. Introduction:
  88. <textarea class="widefat" rows="10" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo esc_html($instance['text']); ?></textarea>
  89. </label>
  90. <label>
  91. Show lists as:
  92. <select name="<?php echo $this->get_field_name('lists_layout'); ?>" id="<?php echo $this->get_field_id('lists_layout'); ?>" style="width: 100%">
  93. <option value="">Checkboxes</option>
  94. <option value="dropdown" <?php echo $instance['lists_layout'] == 'dropdown' ? 'selected' : '' ?>>Dropdown</option>
  95. </select>
  96. </label>
  97. <br>
  98. <label for="<?php echo $this->get_field_id('lists_empty_label'); ?>">
  99. First dropdown entry label:
  100. <input class="widefat" id="<?php echo $this->get_field_id('lists_empty_label'); ?>" name="<?php echo $this->get_field_name('lists_empty_label'); ?>" type="text" value="<?php echo esc_attr($instance['lists_empty_label']); ?>" />
  101. </label>
  102. <br>
  103. <label for="<?php echo $this->get_field_id('lists_field_label'); ?>">
  104. Lists field label:
  105. <input class="widefat" id="<?php echo $this->get_field_id('lists_field_label'); ?>" name="<?php echo $this->get_field_name('lists_field_label'); ?>" type="text" value="<?php echo esc_attr($instance['lists_field_label']); ?>" />
  106. </label>
  107. <br><br>
  108. <?php _e('Automatically subscribe to', 'newsletter') ?>
  109. <br>
  110. <?php
  111. $lists = Newsletter::instance()->get_lists_public();
  112. foreach ($lists as $list) {
  113. ?>
  114. <label for="nl<?php echo $list->id ?>">
  115. <input type="checkbox" value="<?php echo $list->id ?>" name="<?php echo $this->get_field_name('nl[]') ?>" <?php echo array_search($list->id, $instance['nl']) !== false ? 'checked' : '' ?>> <?php echo esc_html($list->name) ?>
  116. </label>
  117. <br>
  118. <?php } ?>
  119. <br>
  120. </p>
  121. <p>
  122. Use the tag {subscription_form} to place the subscription form within your personal text.
  123. </p>
  124. <?php
  125. }
  126. }
  127. add_action('widgets_init', function () {
  128. return register_widget("NewsletterWidget");
  129. });