1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Give Form Widget |
4
|
|
|
* |
5
|
|
|
* @package WordImpress |
6
|
|
|
* @subpackage Admin/Forms |
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
8
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
9
|
|
|
* @since 1.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
14
|
|
|
exit; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Give Form widget |
19
|
|
|
* |
20
|
|
|
* @since 1.0 |
21
|
|
|
*/ |
22
|
|
|
class Give_Forms_Widget extends WP_Widget{ |
23
|
|
|
/** |
24
|
|
|
* The widget class name |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $self; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Instantiate the class |
32
|
|
|
*/ |
33
|
|
|
public function __construct(){ |
34
|
|
|
$this->self = get_class( $this ); |
35
|
|
|
|
36
|
|
|
parent::__construct( |
37
|
|
|
strtolower( $this->self ), |
38
|
|
|
esc_html__( 'Give - Donation Form', 'give' ), |
39
|
|
|
array( |
40
|
|
|
'description' => esc_html__( 'Display a Give Donation Form in your theme\'s widget powered sidebar.', 'give' ) |
41
|
|
|
) |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
add_action( 'widgets_init', array( $this, 'widget_init' ) ); |
45
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'admin_widget_scripts' ) ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Load widget assets only on the widget page |
50
|
|
|
* |
51
|
|
|
* @param string $hook |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
public function admin_widget_scripts( $hook ){ |
56
|
|
|
// Directories of assets |
57
|
|
|
$js_dir = GIVE_PLUGIN_URL . 'assets/js/admin/'; |
58
|
|
|
$js_plugins = GIVE_PLUGIN_URL . 'assets/js/plugins/'; |
59
|
|
|
$css_dir = GIVE_PLUGIN_URL . 'assets/css/'; |
60
|
|
|
|
61
|
|
|
// Use minified libraries if SCRIPT_DEBUG is turned off |
62
|
|
|
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
63
|
|
|
|
64
|
|
|
// Widget Script |
65
|
|
|
if ( $hook == 'widgets.php' ) { |
66
|
|
|
|
67
|
|
|
wp_enqueue_style( 'give-qtip-css', $css_dir . 'jquery.qtip' . $suffix . '.css' ); |
68
|
|
|
|
69
|
|
|
wp_enqueue_script( 'give-qtip', $js_plugins . 'jquery.qtip' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION ); |
70
|
|
|
|
71
|
|
|
wp_enqueue_script( 'give-admin-widgets-scripts', $js_dir . 'admin-widgets' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false ); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Echo the widget content. |
77
|
|
|
* |
78
|
|
|
* @param array $args Display arguments including before_title, after_title, |
79
|
|
|
* before_widget, and after_widget. |
80
|
|
|
* @param array $instance The settings for the particular instance of the widget. |
81
|
|
|
*/ |
82
|
|
|
public function widget( $args, $instance ){ |
83
|
|
|
$title = !empty( $instance['title'] ) ? $instance['title'] : ''; |
84
|
|
|
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
85
|
|
|
|
86
|
|
|
echo $args['before_widget']; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Fires before widget settings form in the admin area. |
90
|
|
|
* |
91
|
|
|
* @since 1.0 |
92
|
|
|
*/ |
93
|
|
|
do_action( 'give_before_forms_widget' ); |
94
|
|
|
|
95
|
|
|
echo $title ? $args['before_title'] . $title . $args['after_title'] : ''; |
96
|
|
|
|
97
|
|
|
give_get_donation_form( $instance ); |
98
|
|
|
|
99
|
|
|
echo $args['after_widget']; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Fires after widget settings form in the admin area. |
103
|
|
|
* |
104
|
|
|
* @since 1.0 |
105
|
|
|
*/ |
106
|
|
|
do_action( 'give_after_forms_widget' ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Output the settings update form. |
111
|
|
|
* |
112
|
|
|
* @param array $instance Current settings. |
113
|
|
|
* |
114
|
|
|
* @return string |
|
|
|
|
115
|
|
|
*/ |
116
|
|
|
public function form( $instance ){ |
117
|
|
|
$defaults = array( |
118
|
|
|
'title' => '', |
119
|
|
|
'id' => '', |
120
|
|
|
'float_labels' => 'global', |
121
|
|
|
'display_style' => 'modal', |
122
|
|
|
'show_content' => 'none', |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$instance = wp_parse_args( (array) $instance, $defaults ); |
126
|
|
|
|
127
|
|
|
// Backward compatibility: Set float labels as default if, it was set as empty previous. |
128
|
|
|
$instance['float_labels'] = empty( $instance['float_labels'] ) ? 'global' : $instance['float_labels']; |
129
|
|
|
|
130
|
|
|
// Query Give Forms |
131
|
|
|
$args = array( |
132
|
|
|
'post_type' => 'give_forms', |
133
|
|
|
'posts_per_page' => - 1, |
134
|
|
|
'post_status' => 'publish', |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$give_forms = get_posts( $args ); |
138
|
|
|
|
139
|
|
|
// Widget: Title |
140
|
|
|
|
141
|
|
|
?><p> |
142
|
|
|
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'give' ); ?></label> |
143
|
|
|
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php esc_attr_e( $instance['title'] ); ?>" /><br> |
144
|
|
|
<small class="give-field-description"><?php esc_html_e( 'Leave blank to hide the widget title.', 'give' ); ?></small> |
145
|
|
|
</p><?php |
146
|
|
|
|
147
|
|
|
// Widget: Give Form |
148
|
|
|
|
149
|
|
|
?><p> |
150
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'id' ) ); ?>"><?php esc_html_e( 'Give Form:', 'give' ); ?></label> |
151
|
|
|
<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'id' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'id' ) ); ?>"> |
152
|
|
|
<option value="current"><?php esc_html_e( '- Select -', 'give' ); ?></option> |
153
|
|
|
<?php foreach ( $give_forms as $give_form ) { ?> |
154
|
|
|
<?php $form_title = empty( $give_form->post_title ) ? sprintf( __( 'Untitled (#%s)', 'give' ), $give_form->ID ) : $give_form->post_title; ?> |
155
|
|
|
<option <?php selected( absint( $instance['id'] ), $give_form->ID ); ?> value="<?php echo esc_attr( $give_form->ID ); ?>"><?php echo $form_title; ?></option> |
156
|
|
|
<?php } ?> |
157
|
|
|
</select><br> |
158
|
|
|
<small class="give-field-description"><?php esc_html_e( 'Select a Give Form to embed in this widget.', 'give' ); ?></small> |
159
|
|
|
</p> |
160
|
|
|
|
161
|
|
|
<?php // Widget: Display Style ?> |
162
|
|
|
<p> |
163
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>"><?php esc_html_e( 'Display Style:', 'give' ); ?></label><br> |
164
|
|
|
<label for="<?php echo $this->get_field_id( 'display_style' ); ?>-onpage"><input type="radio" class="widefat" id="<?php echo $this->get_field_id( 'display_style' ); ?>-onpage" name="<?php echo $this->get_field_name( 'display_style' ); ?>" value="onpage" <?php checked( $instance['display_style'], 'onpage' ); ?>> <?php echo esc_html__( 'All Fields', 'give' ); ?></label> |
165
|
|
|
<label for="<?php echo $this->get_field_id( 'display_style' ); ?>-reveal"><input type="radio" class="widefat" id="<?php echo $this->get_field_id( 'display_style' ); ?>-reveal" name="<?php echo $this->get_field_name( 'display_style' ); ?>" value="reveal" <?php checked( $instance['display_style'], 'reveal' ); ?>> <?php echo esc_html__( 'Reveal', 'give' ); ?></label> |
166
|
|
|
<label for="<?php echo $this->get_field_id( 'display_style' ); ?>-modal"><input type="radio" class="widefat" id="<?php echo $this->get_field_id( 'display_style' ); ?>-modal" name="<?php echo $this->get_field_name( 'display_style' ); ?>" value="modal" <?php checked( $instance['display_style'], 'modal' ); ?>> <?php echo esc_html__( 'Modal', 'give' ); ?></label> |
167
|
|
|
<label for="<?php echo $this->get_field_id( 'display_style' ); ?>-button"><input type="radio" class="widefat" id="<?php echo $this->get_field_id( 'display_style' ); ?>-button" name="<?php echo $this->get_field_name( 'display_style' ); ?>" value="button" <?php checked( $instance['display_style'], 'button' ); ?>> <?php echo esc_html__( 'Button', 'give' ); ?></label><br> |
168
|
|
|
<small class="give-field-description"> |
169
|
|
|
<?php echo esc_html__( 'Select a Give Form style.', 'give' ); ?> |
170
|
|
|
</small> |
171
|
|
|
</p> |
172
|
|
|
|
173
|
|
|
<?php // Widget: Floating Labels ?> |
174
|
|
|
<p> |
175
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'float_labels' ) ); ?>"><?php esc_html_e( 'Floating Labels (optional):', 'give' ); ?></label><br> |
176
|
|
|
<label for="<?php echo $this->get_field_id( 'float_labels' ); ?>-global"><input type="radio" class="widefat" id="<?php echo $this->get_field_id( 'float_labels' ); ?>-global" name="<?php echo $this->get_field_name( 'float_labels' ); ?>" value="global" <?php checked( $instance['float_labels'], 'global' ); ?>> <?php echo esc_html__( 'Global Option', 'give' ); ?></label> |
177
|
|
|
<label for="<?php echo $this->get_field_id( 'float_labels' ); ?>-enabled"><input type="radio" class="widefat" id="<?php echo $this->get_field_id( 'float_labels' ); ?>-enabled" name="<?php echo $this->get_field_name( 'float_labels' ); ?>" value="enabled" <?php checked( $instance['float_labels'], 'enabled' ); ?>> <?php echo esc_html__( 'Yes', 'give' ); ?></label> |
178
|
|
|
<label for="<?php echo $this->get_field_id( 'float_labels' ); ?>-disabled"><input type="radio" class="widefat" id="<?php echo $this->get_field_id( 'float_labels' ); ?>-disabled" name="<?php echo $this->get_field_name( 'float_labels' ); ?>" value="disabled" <?php checked( $instance['float_labels'], 'disabled' ); ?>> <?php echo esc_html__( 'No', 'give' ); ?></label><br> |
179
|
|
|
<small class="give-field-description"> |
180
|
|
|
<?php |
181
|
|
|
printf( |
182
|
|
|
/* translators: %s: https://givewp.com/documentation/core/give-forms/creating-give-forms/#floating-labels */ |
183
|
|
|
__( 'Override the <a href="%s" target="_blank">floating labels</a> setting for this Give form.', 'give' ), |
184
|
|
|
esc_url( 'https://givewp.com/documentation/core/give-forms/creating-give-forms/#floating-labels' ) |
185
|
|
|
); |
186
|
|
|
?></small> |
187
|
|
|
</p> |
188
|
|
|
|
189
|
|
|
<?php // Widget: Display Content ?> |
190
|
|
|
<p> |
191
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>"><?php esc_html_e( 'Display Content (optional):', 'give' ); ?></label><br> |
192
|
|
|
<label for="<?php echo $this->get_field_id( 'show_content' ); ?>-none"><input type="radio" class="widefat" id="<?php echo $this->get_field_id( 'show_content' ); ?>-none" name="<?php echo $this->get_field_name( 'show_content' ); ?>" value="none" <?php checked( $instance['show_content'], 'none' ); ?>> <?php echo esc_html__( 'None', 'give' ); ?></label> |
193
|
|
|
<label for="<?php echo $this->get_field_id( 'show_content' ); ?>-above"><input type="radio" class="widefat" id="<?php echo $this->get_field_id( 'show_content' ); ?>-above" name="<?php echo $this->get_field_name( 'show_content' ); ?>" value="above" <?php checked( $instance['show_content'], 'above' ); ?>> <?php echo esc_html__( 'Above', 'give' ); ?></label> |
194
|
|
|
<label for="<?php echo $this->get_field_id( 'show_content' ); ?>-below"><input type="radio" class="widefat" id="<?php echo $this->get_field_id( 'show_content' ); ?>-below" name="<?php echo $this->get_field_name( 'show_content' ); ?>" value="below" <?php checked( $instance['show_content'], 'below' ); ?>> <?php echo esc_html__( 'Below', 'give' ); ?></label><br> |
195
|
|
|
<small class="give-field-description"><?php esc_html_e( 'Override the display content setting for this Give form.', 'give' ); ?></small> |
196
|
|
|
<?php |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Register the widget |
201
|
|
|
* |
202
|
|
|
* @return void |
203
|
|
|
*/ |
204
|
|
|
function widget_init(){ |
|
|
|
|
205
|
|
|
register_widget( $this->self ); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Update the widget |
210
|
|
|
* |
211
|
|
|
* @param array $new_instance |
212
|
|
|
* @param array $old_instance |
213
|
|
|
* |
214
|
|
|
* @return array |
215
|
|
|
*/ |
216
|
|
|
public function update( $new_instance, $old_instance ){ |
217
|
|
|
$this->flush_widget_cache(); |
218
|
|
|
|
219
|
|
|
return $new_instance; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Flush widget cache |
224
|
|
|
* |
225
|
|
|
* @return void |
226
|
|
|
*/ |
227
|
|
|
public function flush_widget_cache(){ |
228
|
|
|
wp_cache_delete( $this->self, 'widget' ); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
new Give_Forms_Widget; |
233
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.