| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class weForms_Widget |
| 5 |
* |
| 6 |
* Widget class for weForms Form widget |
| 7 |
* |
| 8 |
* @since 1.2.7 |
| 9 |
*/ |
| 10 |
class weForms_Widget extends WP_Widget { |
| 11 |
|
| 12 |
/** |
| 13 |
* weForms_Widget constructor. |
| 14 |
*/ |
| 15 |
function __construct() { |
| 16 |
|
| 17 |
parent::__construct( |
| 18 |
'weforms_widget', |
| 19 |
__('weForms Widget', 'weforms'), |
| 20 |
array( 'description' => __( 'Add weForms Form to Sidebar', 'weforms' ), ) |
| 21 |
); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Render widget |
| 26 |
* |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public function widget( $args, $instance ) { |
| 30 |
|
| 31 |
$title = apply_filters( 'widget_title', $instance['title'] ); |
| 32 |
$form_id = apply_filters( 'weforms_widget_form_id', $instance['form_id'] ); |
| 33 |
|
| 34 |
echo $args['before_widget']; |
| 35 |
|
| 36 |
if ( ! empty( $title ) ) { |
| 37 |
echo $args['before_title'] . $title . $args['after_title']; |
| 38 |
} |
| 39 |
|
| 40 |
echo do_shortcode( sprintf( '[weforms id ="%s" ]', $form_id ) ); |
| 41 |
|
| 42 |
echo $args['after_widget']; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* weForms widget backend |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function form( $instance ) { |
| 51 |
|
| 52 |
$title = isset( $instance[ 'title' ] ) ? $instance[ 'title' ] : __( 'Form', 'weforms' ); |
| 53 |
$form_id = isset( $instance[ 'form_id' ] ) ? $instance[ 'form_id' ] : 0; |
| 54 |
|
| 55 |
$all_forms = weforms()->form->all(); |
| 56 |
$options = sprintf( "<option value='%s'>%s</option>", 0, __('Select Form', 'weforms') ); |
| 57 |
foreach ( $all_forms['forms'] as $form ) { |
| 58 |
$options.= sprintf( "<option value='%s' %s >%s</option>", $form->id, selected( $form_id, $form->id, false) ,$form->name ); |
| 59 |
} |
| 60 |
?> |
| 61 |
|
| 62 |
<p> |
| 63 |
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'weforms' ); ?></label> |
| 64 |
<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 ); ?>" /> |
| 65 |
</p> |
| 66 |
<p> |
| 67 |
<label for="<?php echo $this->get_field_id( 'form_id' ); ?>"> <?php _e( 'Form:', 'weforms' ); ?></label> |
| 68 |
<select id="<?php echo $this->get_field_id( 'form_id' ); ?>" class="widefat" name="<?php echo $this->get_field_name( 'form_id' ); ?>"> |
| 69 |
<?php echo $options ?> |
| 70 |
</select> |
| 71 |
</p> |
| 72 |
|
| 73 |
<?php |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Updating widget replacing old instances with new |
| 78 |
* |
| 79 |
* @return $instance |
| 80 |
*/ |
| 81 |
public function update( $new_instance, $old_instance ) { |
| 82 |
|
| 83 |
$instance = array(); |
| 84 |
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; |
| 85 |
$instance['form_id'] = ( ! empty( $new_instance['form_id'] ) ) ? strip_tags( $new_instance['form_id'] ) : ''; |
| 86 |
return $instance; |
| 87 |
} |
| 88 |
|
| 89 |
} |
| 90 |
/** |
| 91 |
* Register weForms widget |
| 92 |
* |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
function weforms_register_widget() { |
| 96 |
register_widget( 'weforms_widget' ); |
| 97 |
} |
| 98 |
add_action( 'widgets_init', 'weforms_register_widget' ); |
| 99 |
|