MC_POP_MM_Sidebar_Widget

The MailChimp Popup with Exit Support MC POP MM Sidebar Widget class.

Defined (1)

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

/inc/sidebar_widget.php  
  1. class MC_POP_MM_Sidebar_Widget extends WP_Widget { 
  2.  
  3. /** 
  4. * Register widget with WordPress. 
  5. */ 
  6. function __construct() { 
  7. parent::__construct( 
  8. 'mc_pop_mm_widget', // Base ID 
  9. __('Sidebar MailChimp Form', 'text_domain'), // Name 
  10. array( 'description' => __( 'Displays a MailMunch optin form in Sidebar', 'text_domain' ), ) // Args 
  11. ); 
  12.  
  13. /** 
  14. * Front-end display of widget. 
  15. * @see WP_Widget::widget() 
  16. * @param array $args Widget arguments. 
  17. * @param array $instance Saved values from database. 
  18. */ 
  19. public function widget( $args, $instance ) { 
  20. if ( isset( $instance[ 'form_id' ] ) ) { 
  21. $form_id = $instance[ 'form_id' ]; 
  22.  
  23. if (!empty($form_id)) { 
  24. echo $args['before_widget']; 
  25. if ( ! empty( $instance['title'] ) ) { 
  26. echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title']; 
  27. echo "<div class='mailmunch-wordpress-widget mailmunch-wordpress-widget-".$form_id."' style='display: none !important;'></div>"; 
  28. echo $args['after_widget']; 
  29.  
  30. /** 
  31. * Back-end widget form. 
  32. * @see WP_Widget::form() 
  33. * @param array $instance Previously saved values from database. 
  34. */ 
  35. public function form( $instance ) { 
  36. if ( isset( $instance[ 'title' ] ) ) { 
  37. $title = $instance[ 'title' ]; 
  38. else { 
  39. $title = __( 'Optin Form', 'text_domain' ); 
  40.  
  41. if ( isset( $instance[ 'form_id' ] ) ) { 
  42. $form_id = $instance[ 'form_id' ]; 
  43.  
  44. $mc_pop_mm_data = unserialize(get_option("mc_pop_mm_data")); 
  45. $mc_pop_mm_user_email = get_option("mc_pop_mm_user_email"); 
  46. $mc_pop_mm_user_password = get_option("mc_pop_mm_user_password"); 
  47. if (!empty($mc_pop_mm_user_email) && !empty($mc_pop_mm_user_password) && !empty($mc_pop_mm_data["site_id"])) { 
  48. $mc_pop_mm_site_id = $mc_pop_mm_data["site_id"]; 
  49. $mm = new MailchimppopupMailmunchApi($mc_pop_mm_user_email, $mc_pop_mm_user_password, "http://".MAILCHIMP_POPUP_MAILMUNCH_URL); 
  50. $result = $mm->widgets($mc_pop_mm_site_id, "Sidebar"); 
  51. if ( !is_wp_error( $result ) ) { 
  52. $widgets = json_decode($result['body']); 
  53. } else { 
  54. ?> 
  55. <p>No MailMunch account found. <a href="<?php echo admin_url( 'admin.php?page='.MAILCHIMP_POPUP_MAILMUNCH_SLUG ); ?>">Go Here First</a></p> 
  56. <?php 
  57. return; 
  58. ?> 
  59. <script type="text/javascript"> 
  60. window.onmessage = function (e) { 
  61. if (e.data === 'refresh') { 
  62. top.location.reload(); 
  63. }; 
  64. </script> 
  65. <?php 
  66. if (sizeof($widgets) > 0) { 
  67. ?> 
  68. <p> 
  69. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>  
  70. <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( $title ); ?>"> 
  71. </p> 
  72.  
  73. <p> 
  74. <label for="<?php echo $this->get_field_id( 'form_id' ); ?>"><?php _e( 'Optin Form:' ); ?></label> 
  75. <select class="widefat" id="<?php echo $this->get_field_id( 'form_id' ); ?>" name="<?php echo $this->get_field_name( 'form_id' ); ?>"> 
  76. <option value="">None</option> 
  77. <?php 
  78. foreach ($widgets as $widget) { 
  79. echo "<option value='".$widget->id."'"; 
  80. if ($form_id == $widget->id) { echo " selected"; }; 
  81. echo ">".$widget->name."</option>"; 
  82. ?> 
  83. </select> 
  84. </p> 
  85.  
  86. <p><a href="//<?php echo MAILCHIMP_POPUP_MAILMUNCH_URL ?>/sso?email=<?php echo urlencode($mc_pop_mm_user_email) ?>&password=<?php echo urlencode($mc_pop_mm_user_password) ?>&next_url=<?php echo urlencode("/sites/".$mc_pop_mm_data["site_id"]."/widgets/new?wp_layout=1&widget_type=Sidebar") ?>" target="_blank">Create New Sidebar Form</a></p> 
  87. <?php  
  88. } else { 
  89. ?> 
  90. <p>No sidebar forms found. <a href="//<?php echo MAILCHIMP_POPUP_MAILMUNCH_URL ?>/sso?email=<?php echo urlencode($mc_pop_mm_user_email) ?>&password=<?php echo urlencode($mc_pop_mm_user_password) ?>&next_url=<?php echo urlencode("/sites/".$mc_pop_mm_data["site_id"]."/widgets/new?wp_layout=1&widget_type=Sidebar") ?>" target="_blank">Create Your First One</a></p> 
  91. <?php 
  92.  
  93.  
  94. /** 
  95. * Sanitize widget form values as they are saved. 
  96. * @see WP_Widget::update() 
  97. * @param array $new_instance Values just sent to be saved. 
  98. * @param array $old_instance Previously saved values from database. 
  99. * @return array Updated safe values to be saved. 
  100. */ 
  101. public function update( $new_instance, $old_instance ) { 
  102. $instance = array(); 
  103. $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; 
  104. $instance['form_id'] = ( ! empty( $new_instance['form_id'] ) ) ? strip_tags( $new_instance['form_id'] ) : ''; 
  105.  
  106. return $instance; 
  107.