| 1 |
<?php |
| 2 |
|
| 3 |
class EasyOptInsWidgetHelper { |
| 4 |
|
| 5 |
var $settings; |
| 6 |
|
| 7 |
function __construct( $settings = array() ) { |
| 8 |
|
| 9 |
$this->settings = $settings; |
| 10 |
|
| 11 |
// Register widget |
| 12 |
add_action( 'widgets_init', array( $this, 'register_widget' ) ); |
| 13 |
} |
| 14 |
|
| 15 |
function register_widget() { |
| 16 |
|
| 17 |
register_widget( 'EasyOptInsWidget' ); |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
class EasyOptInsWidget extends WP_Widget { |
| 22 |
|
| 23 |
function __construct() { |
| 24 |
|
| 25 |
$widget_ops = array( |
| 26 |
'classname' => 'easy-opt-in-widget', |
| 27 |
'description' => 'Displays your opt-in sign-up form.', |
| 28 |
); |
| 29 |
|
| 30 |
$control_ops = array( |
| 31 |
'width' => 300, |
| 32 |
'height' => 350, |
| 33 |
'id_base' => 'easy-opt-in-widget', |
| 34 |
); |
| 35 |
|
| 36 |
parent::__construct( |
| 37 |
'easy-opt-in-widget' |
| 38 |
, 'Sidebar Optin Form (Optin Cat)' |
| 39 |
, $widget_ops, $control_ops |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
function widget( $args, $instance ) { |
| 44 |
|
| 45 |
global $fca_eoi_shortcodes; |
| 46 |
include_once plugin_dir_path( __FILE__ ) . 'eoi-shortcode.php'; |
| 47 |
|
| 48 |
extract( $args ); |
| 49 |
|
| 50 |
$form_id = |
| 51 |
preg_match( '/^([0-9])+$/sim', $instance[ 'eoi_form_id' ] ) |
| 52 |
? $instance['eoi_form_id'] |
| 53 |
: 0 |
| 54 |
; |
| 55 |
$widget = $fca_eoi_shortcodes->shortcode_content( array( 'id' => $form_id ) ); |
| 56 |
|
| 57 |
echo wp_kses( $before_widget . $widget . $after_widget, K::allowed_html() ); |
| 58 |
} |
| 59 |
|
| 60 |
function update( $new_instance, $old_instance ) { |
| 61 |
|
| 62 |
$instance = $old_instance; |
| 63 |
$instance[ 'eoi_form_id' ] = wp_strip_all_tags( $new_instance[ 'eoi_form_id' ] ); |
| 64 |
|
| 65 |
return $instance; |
| 66 |
} |
| 67 |
|
| 68 |
function form( $instance ) { |
| 69 |
|
| 70 |
global $post; |
| 71 |
|
| 72 |
$defaults = array( 'eoi_form_id' => '' ); |
| 73 |
$instance = wp_parse_args( ( array ) $instance, $defaults ); |
| 74 |
$options = array(); |
| 75 |
$forms = get_posts( array( |
| 76 |
'post_type' => 'easy-opt-ins', |
| 77 |
'post_status' => 'publish', |
| 78 |
'posts_per_page' => -1, |
| 79 |
) ); |
| 80 |
|
| 81 |
// Show error if there are no forms, then exit function |
| 82 |
if( empty ( $forms ) ) { |
| 83 |
K::wrap( |
| 84 |
sprintf( |
| 85 |
'You have not created a form yet. <a href="%s" target="_blank">Create a form</a>.' |
| 86 |
, admin_url( 'post-new.php?post_type=easy-opt-ins' ) |
| 87 |
) |
| 88 |
, null |
| 89 |
, array( 'in' => 'p' ) |
| 90 |
); |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Prepare forms dropdownlist |
| 96 |
* |
| 97 |
* 1. Form must have a thank you page and a list chosen |
| 98 |
* 2. Form must be a widget |
| 99 |
* |
| 100 |
*/ |
| 101 |
foreach ( $forms as $form ) { |
| 102 |
$fca_eoi = get_post_meta( $form->ID, 'fca_eoi', true ); |
| 103 |
$provider = K::get_var( 'provider', $fca_eoi ); |
| 104 |
$layout = K::get_var( 'layout', $fca_eoi ); |
| 105 |
// Exlude forms not validating condition 1. |
| 106 |
if ( ! K::get_var( $provider . '_list_id', $fca_eoi ) || ! K::get_var( 'thank_you_page', $fca_eoi ) ) { |
| 107 |
continue; |
| 108 |
} |
| 109 |
// Exlude forms not validating condition 2. |
| 110 |
if( strpos( $layout , 'layout_' ) === 0 ) { |
| 111 |
/* We're good */ |
| 112 |
} else { |
| 113 |
continue; |
| 114 |
} |
| 115 |
|
| 116 |
$title = $form->post_title |
| 117 |
? $form->post_title |
| 118 |
: 'Untitled form' . ' (#' . $form->ID . ' )' |
| 119 |
; |
| 120 |
$options[ $form->ID ] = $title; |
| 121 |
} |
| 122 |
|
| 123 |
// If no suitable form exists, show an indication and exit function |
| 124 |
if( ! $options ) { |
| 125 |
K::wrap( |
| 126 |
sprintf( |
| 127 |
'Please make sure to connect <a href="%s" target="_blank">your form</a> to your email marketing provider and to select a thank you page.' |
| 128 |
, admin_url( 'edit.php?post_type=easy-opt-ins' ) |
| 129 |
) |
| 130 |
, null |
| 131 |
, array( 'in' => 'p' ) |
| 132 |
); |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
// Add empty choice |
| 137 |
$options = array_reverse( $options, true ); |
| 138 |
$options[ '' ] = 'Not set'; |
| 139 |
$options = array_reverse( $options, true ); |
| 140 |
|
| 141 |
// Show choices |
| 142 |
K::select( $this->get_field_name( 'eoi_form_id' ) |
| 143 |
, array( |
| 144 |
'id' => $this->get_field_id( 'eoi_form_id' ), |
| 145 |
) |
| 146 |
, array( |
| 147 |
'options' => $options, |
| 148 |
'default' => '', |
| 149 |
'selected' => $instance['eoi_form_id'], |
| 150 |
'format' => '<p><label for="' . $this->get_field_id( 'eoi_form_id' ) . '">Select form:</label><p>:select</p>', |
| 151 |
) |
| 152 |
); |
| 153 |
} |
| 154 |
} |
| 155 |
|