| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Widgets; |
| 4 |
|
| 5 |
class SidebarWidgets extends \WP_Widget |
| 6 |
{ |
| 7 |
public function __construct() |
| 8 |
{ |
| 9 |
parent::__construct( |
| 10 |
'fluentform_widget', |
| 11 |
esc_html__('Fluent Forms Widget', 'fluentform'), |
| 12 |
['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 |
public function form($instance) |
| 39 |
{ |
| 40 |
$selectedForm = empty($instance['allforms']) ? '' : $instance['allforms']; |
| 41 |
|
| 42 |
if (isset($instance['title'])) { |
| 43 |
$title = $instance['title']; |
| 44 |
} else { |
| 45 |
$title = __('', 'fluentform'); |
| 46 |
} |
| 47 |
// Widget admin form |
| 48 |
?> |
| 49 |
<p> |
| 50 |
<label |
| 51 |
for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php _e('Title (optional):', 'fluentform'); ?></label> |
| 52 |
<input class="widefat" |
| 53 |
id="<?php echo esc_attr($this->get_field_id('title')); ?>" |
| 54 |
name="<?php echo esc_attr($this->get_field_name('title')); ?>" |
| 55 |
type="text" value="<?php echo esc_attr($title); ?>" /> |
| 56 |
</p> |
| 57 |
<?php |
| 58 |
$forms = wpFluent()->table('fluentform_forms') |
| 59 |
->select(['id', 'title']) |
| 60 |
->orderBy('id', 'DESC') |
| 61 |
->get(); |
| 62 |
?> |
| 63 |
|
| 64 |
<label |
| 65 |
for="<?php echo esc_attr($this->get_field_id('allforms')); ?>">Select |
| 66 |
a form: |
| 67 |
<select style="margin-bottom: 12px;" class='widefat' |
| 68 |
id="<?php echo esc_attr($this->get_field_id('allforms')); ?>" |
| 69 |
name="<?php echo esc_attr($this->get_field_name('allforms')); ?>" |
| 70 |
type="text"> |
| 71 |
<?php |
| 72 |
foreach ($forms as $item) { |
| 73 |
?> |
| 74 |
<option <?php if ($item->id == $selectedForm) { |
| 75 |
echo 'selected'; |
| 76 |
} ?> value='<?php echo esc_attr($item->id); ?>'> |
| 77 |
<?php echo esc_html($item->title); ?> (<?php echo esc_attr($item->id); ?>) |
| 78 |
</option> |
| 79 |
<?php |
| 80 |
} |
| 81 |
?> |
| 82 |
</select> |
| 83 |
</label> |
| 84 |
<?php |
| 85 |
} |
| 86 |
|
| 87 |
public function update($new_instance, $old_instance) |
| 88 |
{ |
| 89 |
$instance = []; |
| 90 |
$instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : ''; |
| 91 |
$instance['allforms'] = intval($new_instance['allforms']); |
| 92 |
return $instance; |
| 93 |
} |
| 94 |
} |
| 95 |
|