| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Widgets; |
| 4 |
|
| 5 |
class SidebarWidgets extends \WP_Widget |
| 6 |
{ |
| 7 |
function __construct() |
| 8 |
{ |
| 9 |
parent::__construct( |
| 10 |
'fluentform_widget', |
| 11 |
esc_html__('Fluent Forms Widget', 'fluentform'), |
| 12 |
array('description' => esc_html__('Add your form by Fluent Forms', 'fluentform'),) |
| 13 |
); |
| 14 |
} |
| 15 |
|
| 16 |
public function widget($args, $instance) |
| 17 |
{ |
| 18 |
$selectedForm = empty($instance['allforms']) ? '' : intval($instance['allforms']); |
| 19 |
|
| 20 |
if(!$selectedForm) { |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
echo $args['before_widget']; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped |
| 25 |
|
| 26 |
if ( ! empty( $instance['title'] ) ) { |
| 27 |
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped |
| 28 |
} |
| 29 |
|
| 30 |
if ($selectedForm != '') { |
| 31 |
$shortcode = "[fluentform id='$selectedForm']"; |
| 32 |
echo do_shortcode($shortcode); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped |
| 33 |
} |
| 34 |
|
| 35 |
echo $args['after_widget']; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
public function form($instance) |
| 40 |
{ |
| 41 |
$selectedForm = empty($instance['allforms']) ? '' : $instance['allforms']; |
| 42 |
|
| 43 |
if (isset($instance['title'])) { |
| 44 |
$title = $instance['title']; |
| 45 |
} else { |
| 46 |
$title = __('', 'fluentform'); |
| 47 |
} |
| 48 |
// Widget admin form |
| 49 |
?> |
| 50 |
<p> |
| 51 |
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php _e('Title (optional):', 'fluentform'); ?></label> |
| 52 |
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" |
| 53 |
name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" |
| 54 |
value="<?php echo esc_attr($title); ?>"/> |
| 55 |
</p> |
| 56 |
<?php |
| 57 |
$forms = wpFluent()->table('fluentform_forms') |
| 58 |
->select(array('id', 'title')) |
| 59 |
->orderBy('id', 'DESC') |
| 60 |
->get(); |
| 61 |
?> |
| 62 |
|
| 63 |
<label for="<?php echo esc_attr($this->get_field_id('allforms')); ?>">Select a form: |
| 64 |
<select style="margin-bottom: 12px;" class='widefat' id="<?php echo esc_attr($this->get_field_id('allforms')); ?>" |
| 65 |
name="<?php echo esc_attr($this->get_field_name('allforms')); ?>" type="text" |
| 66 |
> |
| 67 |
<?php |
| 68 |
foreach ($forms as $item) { |
| 69 |
?> |
| 70 |
<option <?php if ($item->id == $selectedForm) { |
| 71 |
echo 'selected'; |
| 72 |
} ?> value='<?php echo esc_attr($item->id); ?>'> |
| 73 |
<?php echo esc_html($item->title); ?> (<?php echo esc_attr($item->id); ?>) |
| 74 |
</option> |
| 75 |
<?php |
| 76 |
} |
| 77 |
?> |
| 78 |
</select> |
| 79 |
</label> |
| 80 |
<?php |
| 81 |
} |
| 82 |
|
| 83 |
public function update($new_instance, $old_instance) |
| 84 |
{ |
| 85 |
$instance = array(); |
| 86 |
$instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : ''; |
| 87 |
$instance['allforms'] = intval($new_instance['allforms']); |
| 88 |
return $instance; |
| 89 |
} |
| 90 |
} |
| 91 |
|