| 1 |
<?php |
| 2 |
/** |
| 3 |
* Widget Subscription Form |
| 4 |
* |
| 5 |
* @link https://codesupply.co |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @package Powerkit |
| 9 |
* @subpackage Powerkit/widgets |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Widget Subscription Form |
| 14 |
*/ |
| 15 |
class Powerkit_Subscription_Form_Widget extends WP_Widget { |
| 16 |
|
| 17 |
/** |
| 18 |
* Sets up a new widget instance. |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
|
| 22 |
$this->default_settings = apply_filters( 'powerkit_opt_in_subscription_widget_settings', array( |
| 23 |
'title' => esc_html__( 'Subscription Form', 'powerkit' ), |
| 24 |
'privacy' => powerkit_mailchimp_get_privacy_text(), |
| 25 |
'text' => '', |
| 26 |
'bg_image_id' => false, |
| 27 |
'list_id' => null, |
| 28 |
'type' => 'widget', |
| 29 |
'display_name' => false, |
| 30 |
) ); |
| 31 |
|
| 32 |
$widget_details = array( |
| 33 |
'classname' => 'powerkit_opt_in_subscription_widget', |
| 34 |
'description' => esc_html__( 'Add a subscription form to your sidebar.', 'powerkit' ), |
| 35 |
); |
| 36 |
|
| 37 |
// Actions. |
| 38 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 39 |
|
| 40 |
parent::__construct( 'powerkit_opt_in_subscription_widget', esc_html__( 'Subscription Form', 'powerkit' ), $widget_details ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Outputs the content for the current widget instance. |
| 45 |
* |
| 46 |
* @param array $args Display arguments including 'before_title', 'after_title', |
| 47 |
* 'before_widget', and 'after_widget'. |
| 48 |
* @param array $instance Settings for the current widget instance. |
| 49 |
*/ |
| 50 |
public function widget( $args, $instance ) { |
| 51 |
|
| 52 |
$params = array_merge( $this->default_settings, $instance ); |
| 53 |
|
| 54 |
// Before Widget. |
| 55 |
echo $args['before_widget']; // XSS. |
| 56 |
|
| 57 |
if ( $params['title'] ) { |
| 58 |
$params['title'] = $args['before_title'] . apply_filters( 'widget_title', $params['title'], $instance, $this->id_base ) . $args['after_title']; |
| 59 |
} |
| 60 |
?> |
| 61 |
|
| 62 |
<div class="widget-body"> |
| 63 |
<?php do_action( 'powerkit_subscribe_template', $params ); ?> |
| 64 |
</div> |
| 65 |
<?php |
| 66 |
|
| 67 |
// After Widget. |
| 68 |
echo $args['after_widget']; // XSS. |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Handles updating settings for the current widget instance. |
| 73 |
* |
| 74 |
* @param array $new_instance New settings for this instance as input by the user via |
| 75 |
* WP_Widget::form(). |
| 76 |
* @param array $old_instance Old settings for this instance. |
| 77 |
* @return array Settings to save or bool false to cancel saving. |
| 78 |
*/ |
| 79 |
public function update( $new_instance, $old_instance ) { |
| 80 |
$instance = $new_instance; |
| 81 |
|
| 82 |
// Display first name field. |
| 83 |
if ( ! isset( $instance['display_name'] ) ) { |
| 84 |
$instance['display_name'] = false; |
| 85 |
} |
| 86 |
|
| 87 |
return $instance; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Outputs the widget settings form. |
| 92 |
* |
| 93 |
* @param array $instance Current settings. |
| 94 |
*/ |
| 95 |
public function form( $instance ) { |
| 96 |
$params = array_merge( $this->default_settings, $instance ); |
| 97 |
|
| 98 |
$bg_image_url = $params['bg_image_id'] ? wp_get_attachment_image_url( intval( $params['bg_image_id'] ), 'large' ) : ''; |
| 99 |
?> |
| 100 |
<!-- Title --> |
| 101 |
<p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'powerkit' ); ?></label> |
| 102 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $params['title'] ); ?>" /></p> |
| 103 |
|
| 104 |
<!-- Background Image container --> |
| 105 |
<div class="subscription-upload-image upload-img-container" data-frame-title="<?php esc_html_e( 'Select or upload background image', 'powerkit' ); ?>" data-frame-btn-text="<?php esc_html_e( 'Set background image', 'powerkit' ); ?>"> |
| 106 |
<p class="uploaded-img-box"> |
| 107 |
<label for="<?php echo esc_attr( $this->get_field_id( 'bg_image_id' ) ); ?>"><?php esc_html_e( 'Background image:', 'powerkit' ); ?></label> |
| 108 |
|
| 109 |
<span class="uploaded-image"> |
| 110 |
<?php if ( $bg_image_url ) : ?> |
| 111 |
<img src="<?php echo esc_url( $bg_image_url ); ?>" style="display: block; margin-top: 5px; max-width:100%;" /> |
| 112 |
<?php endif; ?> |
| 113 |
</span> |
| 114 |
|
| 115 |
<input id="<?php echo esc_attr( $this->get_field_id( 'bg_image_id' ) ); ?>" class="uploaded-img-id" name="<?php echo esc_attr( $this->get_field_name( 'bg_image_id' ) ); ?>" type="hidden" value="<?php echo esc_attr( $params['bg_image_id'] ); ?>" /> |
| 116 |
</p> |
| 117 |
|
| 118 |
<!-- Add & remove image links --> |
| 119 |
<p class="hide-if-no-js"> |
| 120 |
<a class="upload-img-link button button-primary <?php echo esc_attr( $bg_image_url ? 'hidden' : '' ); ?>" href="#"><?php esc_html_e( 'Add Image', 'powerkit' ); ?></a> |
| 121 |
<a class="delete-img-link button button-secondary <?php echo esc_attr( ! $bg_image_url ? 'hidden' : '' ); ?>" href="#"><?php esc_html_e( 'Remove Image', 'powerkit' ); ?></a> |
| 122 |
</p> |
| 123 |
</div> |
| 124 |
|
| 125 |
<!-- Subscribe Message --> |
| 126 |
<p><label for="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>"><?php esc_html_e( 'Subscribe message:', 'powerkit' ); ?></label> |
| 127 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'text' ) ); ?>" type="text" value="<?php echo esc_attr( $params['text'] ); ?>" /></p> |
| 128 |
|
| 129 |
<!-- List --> |
| 130 |
<?php |
| 131 |
$token = get_option( 'powerkit_mailchimp_token' ); |
| 132 |
|
| 133 |
if ( $token ) { |
| 134 |
|
| 135 |
$data = powerkit_mailchimp_request( 'GET', 'lists', array( |
| 136 |
'sort_field' => 'date_created', |
| 137 |
'sort_dir' => 'DESC', |
| 138 |
'count' => 1000, |
| 139 |
) ); |
| 140 |
|
| 141 |
if ( is_array( $data ) && isset( $data['lists'] ) && $data['lists'] ) { |
| 142 |
?> |
| 143 |
|
| 144 |
<p><label for="<?php echo esc_attr( $this->get_field_id( 'list_id' ) ); ?>"><?php esc_html_e( 'List:', 'powerkit' ); ?></label> |
| 145 |
<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'list_id' ) ); ?>" id="<?php echo esc_attr( $this->get_field_name( 'list_id' ) ); ?>"> |
| 146 |
<option value="default"><?php esc_html_e( 'Default', 'powerkit' ); ?></option> |
| 147 |
<?php foreach ( $data['lists'] as $item ) : ?> |
| 148 |
<option <?php selected( $item['id'], $params['list_id'] ); ?> value="<?php echo esc_attr( $item['id'] ); ?>"><?php echo esc_html( $item['name'] ); ?></option> |
| 149 |
<?php endforeach; ?> |
| 150 |
</select> |
| 151 |
</p> |
| 152 |
<?php |
| 153 |
} |
| 154 |
} |
| 155 |
?> |
| 156 |
|
| 157 |
<!-- Display first name field --> |
| 158 |
<p><input id="<?php echo esc_attr( $this->get_field_id( 'display_name' ) ); ?>" class="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'display_name' ) ); ?>" type="checkbox" <?php checked( (bool) $params['display_name'] ); ?> /> |
| 159 |
<label for="<?php echo esc_attr( $this->get_field_id( 'display_name' ) ); ?>"><?php esc_html_e( 'Display first name field', 'powerkit' ); ?></label> |
| 160 |
<span class="howto">(<?php esc_html_e( 'Make sure you map the field in the MailChimp settings', 'powerkit' ); ?>)</span></p> |
| 161 |
<?php |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Admin Enqunue Scripts |
| 166 |
* |
| 167 |
* @param string $page Current page. |
| 168 |
*/ |
| 169 |
public function admin_enqueue_scripts( $page ) { |
| 170 |
if ( 'widgets.php' === $page ) { |
| 171 |
wp_enqueue_script( 'jquery' ); |
| 172 |
|
| 173 |
wp_enqueue_media(); |
| 174 |
|
| 175 |
ob_start(); |
| 176 |
?> |
| 177 |
<script> |
| 178 |
jQuery( document ).ready(function( $ ) { |
| 179 |
|
| 180 |
var powerkitMediaFrame; |
| 181 |
/* Set all variables to be used in scope */ |
| 182 |
var metaBox = '.subscription-upload-image'; |
| 183 |
|
| 184 |
/* Add Image Link */ |
| 185 |
$( metaBox ).find( '.upload-img-link' ).on( 'click', function( event ){ |
| 186 |
event.preventDefault(); |
| 187 |
|
| 188 |
var parentContainer = $( this ).parents( metaBox ); |
| 189 |
|
| 190 |
// Options. |
| 191 |
var options = { |
| 192 |
title: parentContainer.data( 'frame-title' ) ? parentContainer.data( 'frame-title' ) : 'Select or Upload Media', |
| 193 |
button: { |
| 194 |
text: parentContainer.data( 'frame-btn-text' ) ? parentContainer.data( 'frame-btn-text' ) : 'Use this media', |
| 195 |
}, |
| 196 |
library : { type : 'image' }, |
| 197 |
multiple: false // Set to true to allow multiple files to be selected. |
| 198 |
}; |
| 199 |
|
| 200 |
// Create a new media frame |
| 201 |
powerkitMediaFrame = wp.media( options ); |
| 202 |
|
| 203 |
// When an image is selected in the media frame... |
| 204 |
powerkitMediaFrame.on( 'select', function() { |
| 205 |
|
| 206 |
// Get media attachment details from the frame state. |
| 207 |
var attachment = powerkitMediaFrame.state().get('selection').first().toJSON(); |
| 208 |
|
| 209 |
// Send the attachment URL to our custom image input field. |
| 210 |
parentContainer.find( '.uploaded-image' ).html( '<img src="' + attachment.url + '" style="display: block; margin-top: 5px; max-width:100%;"/>' ); |
| 211 |
parentContainer.find( '.uploaded-img-id' ).val( attachment.id ).change(); |
| 212 |
parentContainer.find( '.upload-img-link' ).addClass( 'hidden' ); |
| 213 |
parentContainer.find( '.delete-img-link' ).removeClass( 'hidden' ); |
| 214 |
|
| 215 |
powerkitMediaFrame.close(); |
| 216 |
}); |
| 217 |
|
| 218 |
// Finally, open the modal on click. |
| 219 |
powerkitMediaFrame.open(); |
| 220 |
}); |
| 221 |
|
| 222 |
|
| 223 |
/* Delete Image Link */ |
| 224 |
$( metaBox ).find( '.delete-img-link' ).on( 'click', function( event ){ |
| 225 |
event.preventDefault(); |
| 226 |
|
| 227 |
$( this ).parents( metaBox ).find( '.uploaded-image' ).html( '' ); |
| 228 |
$( this ).parents( metaBox ).find( '.upload-img-link' ).removeClass( 'hidden' ); |
| 229 |
$( this ).parents( metaBox ).find( '.delete-img-link' ).addClass( 'hidden' ); |
| 230 |
$( this ).parents( metaBox ).find( '.uploaded-img-id' ).val( '' ).change(); |
| 231 |
}); |
| 232 |
}); |
| 233 |
</script> |
| 234 |
<?php |
| 235 |
wp_add_inline_script( 'jquery', str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ); |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Register Widget |
| 242 |
*/ |
| 243 |
function powerkit_widget_init_subscription_form() { |
| 244 |
register_widget( 'Powerkit_Subscription_Form_Widget' ); |
| 245 |
} |
| 246 |
add_action( 'widgets_init', 'powerkit_widget_init_subscription_form' ); |
| 247 |
|