minimal.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. defined('ABSPATH') || exit;
  3. if (version_compare(phpversion(), '5.3', '<')) {
  4. return;
  5. }
  6. class NewsletterWidgetMinimal extends WP_Widget {
  7. function __construct() {
  8. parent::__construct(false, $name = 'Newsletter Minimal', array('description' => 'Newsletter widget to add a minimal subscription form'), array('width' => '350px'));
  9. }
  10. function widget($args, $instance) {
  11. $newsletter = Newsletter::instance();
  12. $current_language = $newsletter->get_current_language();
  13. extract($args);
  14. echo $before_widget;
  15. if (!is_array($instance)) {
  16. $instance = array();
  17. }
  18. // Filters are used for WPML
  19. if (!empty($instance['title'])) {
  20. $title = apply_filters('widget_title', $instance['title'], $instance);
  21. echo $before_title . $title . $after_title;
  22. }
  23. $options_profile = NewsletterSubscription::instance()->get_options('profile', $current_language);
  24. if (empty($instance['button'])) {
  25. $instance['button'] = $options_profile['subscribe'];
  26. }
  27. $form = '<div class="tnp tnp-widget-minimal">';
  28. $form .= '<form class="tnp-form" action="' . $newsletter->build_action_url('s') . '" method="post" onsubmit="return newsletter_check(this)">';
  29. if (isset($instance['nl']) && is_array($instance['nl'])) {
  30. foreach ($instance['nl'] as $a) {
  31. $form .= "<input type='hidden' name='nl[]' value='" . ((int) trim($a)) . "'>\n";
  32. }
  33. }
  34. // Referrer
  35. $form .= '<input type="hidden" name="nr" value="widget-minimal"/>';
  36. $form .= '<input class="tnp-email" type="email" required name="ne" value="" placeholder="' . esc_attr($options_profile['email']) . '">';
  37. $form .= '<input class="tnp-submit" type="submit" value="' . esc_attr($instance['button']) . '">';
  38. $form .= '</form></div>';
  39. echo $form;
  40. echo $after_widget;
  41. }
  42. function update($new_instance, $old_instance) {
  43. return $new_instance;
  44. }
  45. function form($instance) {
  46. if (!is_array($instance)) {
  47. $instance = array();
  48. }
  49. $newsletter = Newsletter::instance();
  50. $current_language = $newsletter->get_current_language();
  51. $profile_options = NewsletterSubscription::instance()->get_options('profile', $current_language);
  52. $instance = array_merge(array('title' => '', 'text' => '', 'button' => $profile_options['subscribe'], 'nl' => array()), $instance);
  53. if (!is_array($instance['nl'])) {
  54. $instance['nl'] = array();
  55. }
  56. ?>
  57. <p>
  58. <label for="<?php echo $this->get_field_id('title'); ?>">
  59. Title:
  60. <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']); ?>">
  61. </label>
  62. <label for="<?php echo $this->get_field_id('button'); ?>">
  63. Button label:
  64. <input class="widefat" id="<?php echo $this->get_field_id('button'); ?>" name="<?php echo $this->get_field_name('button'); ?>" type="text" value="<?php echo esc_attr($instance['button']); ?>">
  65. Use a short one!
  66. </label>
  67. </p>
  68. <p>
  69. <?php _e('Automatically subscribe to', 'newsletter') ?>
  70. <br>
  71. <?php
  72. $lists = Newsletter::instance()->get_lists_public();
  73. foreach ($lists as $list) {
  74. ?>
  75. <label for="nl<?php echo $list->id ?>">
  76. <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) ?>
  77. </label>
  78. <br>
  79. <?php } ?>
  80. </p>
  81. <?php
  82. }
  83. }
  84. add_action('widgets_init', function() {
  85. return register_widget("NewsletterWidgetMinimal");
  86. });