-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpcf7cf-options.php
315 lines (254 loc) · 13.9 KB
/
wpcf7cf-options.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
define('WPCF7CF_SLUG', 'wpcf7cf');
define('WPCF7CF_OPTIONS', WPCF7CF_SLUG.'_options');
define('WPCF7CF_TEXT_DOMAIN', WPCF7CF_SLUG.'_text_domain');
define('WPCF7CF_DEFAULT_ANIMATION', 'yes');
define('WPCF7CF_DEFAULT_ANIMATION_INTIME', 200);
define('WPCF7CF_DEFAULT_ANIMATION_OUTTIME', 200);
define('WPCF7CF_DEFAULT_CONDITIONS_UI', 'normal');
define('WPCF7CF_DEFAULT_NOTICE_DISMISSED', false);
if ( ! defined( 'WPCF7_ADMIN_READ_WRITE_CAPABILITY' ) ) {
define( 'WPCF7_ADMIN_READ_WRITE_CAPABILITY', 'publish_pages' );
}
global $wpcf7cf_default_settings_glob;
$wpcf7cf_default_settings_glob = false;
function wpcf7cf_get_default_settings() {
global $wpcf7cf_default_settings_glob;
if ($wpcf7cf_default_settings_glob) return $wpcf7cf_default_settings_glob;
$wpcf7cf_default_settings_glob = array(
'animation' => WPCF7CF_DEFAULT_ANIMATION,
'animation_intime' => WPCF7CF_DEFAULT_ANIMATION_INTIME,
'animation_outtime' => WPCF7CF_DEFAULT_ANIMATION_OUTTIME,
'conditions_ui' => WPCF7CF_DEFAULT_CONDITIONS_UI,
'notice_dismissed' => WPCF7CF_DEFAULT_NOTICE_DISMISSED
);
$wpcf7cf_default_settings_glob = apply_filters('wpcf7cf_default_options', $wpcf7cf_default_settings_glob);
return $wpcf7cf_default_settings_glob;
}
global $wpcf7cf_settings_glob;
$wpcf7cf_settings_glob = false;
function wpcf7cf_get_settings() {
global $wpcf7cf_settings_glob;
if ($wpcf7cf_settings_glob) {
return $wpcf7cf_settings_glob;
}
$wpcf7cf_default_settings = wpcf7cf_get_default_settings();
$wpcf7cf_saved_settings = get_option(WPCF7CF_OPTIONS);
if (!$wpcf7cf_saved_settings) {
$wpcf7cf_saved_settings = [];
}
$wpcf7cf_settings_glob = array_merge($wpcf7cf_default_settings,$wpcf7cf_saved_settings);
return $wpcf7cf_settings_glob;
}
function wpcf7cf_get_dismiss_notice_nonce() {
// We use the same nonce for all admin notices, because we don't care about users trying to hack themselves.
// This nonce is only intended to prevent CSRF attacks.
static $nonce = false;
if (!$nonce) {
$nonce = wp_create_nonce( 'wpcf7cf_dismiss_notice' );
}
return $nonce;
}
function wpcf7cf_set_options($settings) {
global $wpcf7cf_settings_glob;
$wpcf7cf_settings_glob = $settings;
update_option(WPCF7CF_OPTIONS, $wpcf7cf_settings_glob);
}
function wpcf7cf_reset_options() {
delete_option(WPCF7CF_OPTIONS);
}
add_action( 'admin_enqueue_scripts', 'wpcf7cf_load_page_options_wp_admin_style' );
function wpcf7cf_load_page_options_wp_admin_style() {
wp_register_style( 'wpcf7cf_admin_css', plugins_url('admin-style.css',__FILE__), [], WPCF7CF_VERSION );
wp_enqueue_style( 'wpcf7cf_admin_css' );
}
add_action('admin_menu', 'wpcf7cf_admin_add_page');
function wpcf7cf_admin_add_page() {
add_submenu_page('wpcf7', __( 'Conditional Fields', 'cf7-conditional-fields' ), __( 'Conditional Fields', 'cf7-conditional-fields' ), WPCF7_ADMIN_READ_WRITE_CAPABILITY, 'wpcf7cf', 'wpcf7cf_options_page' );
}
function wpcf7cf_options_page() {
$settings = wpcf7cf_get_settings();
if (isset($_POST['reset'])) {
echo '<div id="message" class="updated fade"><p><strong>' . __( 'Settings restored to defaults', 'cf7-conditional-fields' ) . '</strong></p></div>';
} else if (isset($_REQUEST['settings-updated'])) {
echo '<div id="message" class="updated fade"><p><strong>' . __( 'Settings updated', 'cf7-conditional-fields' ) . '</strong></p></div>';
}
?>
<div class="wrap wpcf7cf-admin-wrap">
<h2><?php _e( 'Conditional Fields for Contact Form 7 Settings', 'cf7-conditional-fields'); ?></h2>
<?php if (!$settings['notice_dismissed']) { ?>
<div class="wpcf7cf-admin-notice notice notice-warning is-dismissible" data-notice-id="" data-nonce="<?php echo wpcf7cf_get_dismiss_notice_nonce() ?>">
<div style="padding: 10px 0;">
<?php _e( '<strong>Notice</strong>: These are global settings for Conditional Fields for Contact Form 7.', 'cf7-conditional-fields'); ?>
<br><br>
<strong><?php _e( 'How to create/edit conditional fields?', 'cf7-conditional-fields'); ?></strong>
<ol>
<li><?php _e( 'Create a new Contact Form or edit an existing one', 'cf7-conditional-fields'); ?></li>
<li><?php _e( 'Create at least one [group] inside the form', 'cf7-conditional-fields'); ?></li>
<li><?php _e( 'Save the Contact Form', 'cf7-conditional-fields'); ?></li>
<li><?php _e( 'Go to the <strong><em>Conditional Fields</em></strong> Tab', 'cf7-conditional-fields'); ?></li>
</ol>
<a href="https://conditional-fields-cf7.bdwm.be/conditional-fields-for-contact-form-7-tutorial/" target="_blank"><?php _e( 'Show me an example', 'cf7-conditional-fields'); ?></a> | <a class="notice-dismiss-alt" href="#"><?php _e( 'Dismiss notice', 'cf7-conditional-fields'); ?></a>
</div>
</div>
<?php } ?>
<form action="options.php" method="post">
<?php settings_fields(WPCF7CF_OPTIONS); ?>
<input type="hidden" name="<?php echo WPCF7CF_OPTIONS.'[notice_dismissed]' ?>" value="<?php echo $settings['notice_dismissed'] ?>" />
<?php
echo '<h3>' . __( 'Default animation Settings', 'cf7-conditional-fields') . '</h3>';
wpcf7cf_input_fields_wrapper_start();
wpcf7cf_input_select('animation', array(
'label' => __( 'Animation', 'cf7-conditional-fields'),
'description' => __( 'Use animations while showing/hiding groups', 'cf7-conditional-fields'),
'select_options' => array('yes' => __( 'Enabled', 'cf7-conditional-fields'), 'no'=> __( 'Disabled', 'cf7-conditional-fields'))
));
wpcf7cf_input_field('animation_intime', array(
'label' => __( 'Animation In time', 'cf7-conditional-fields'),
'description' => __( 'A positive integer value indicating the time, in milliseconds, it will take for each group to show.', 'cf7-conditional-fields'),
));
wpcf7cf_input_field('animation_outtime', array(
'label' => __( 'Animation Out Time', 'cf7-conditional-fields'),
'description' => __( 'A positive integer value indicating the time, in milliseconds, it will take for each group to hide.', 'cf7-conditional-fields'),
));
wpcf7cf_input_fields_wrapper_end();
submit_button();
/* @RRZE
if (!WPCF7CF_IS_PRO) {
?>
<h3><?php _e( 'Conditional Fields PRO', 'cf7-conditional-fields'); ?></h3>
<?php _e( 'Get Conditional Fields PRO to unlock the full potential of CF7', 'cf7-conditional-fields'); ?>
<ul class="wpcf7cf-list">
<li><?php _e( 'Repeaters', 'cf7-conditional-fields'); ?></li>
<li><?php _e( 'Regular expressions', 'cf7-conditional-fields'); ?></li>
<li><?php _e( 'Toggle buttons', 'cf7-conditional-fields'); ?></li>
<li><?php _e( 'Additional operators', 'cf7-conditional-fields'); ?>< <code><</code> <code>></code> <code>≤</code> <code>≥</code> <code><?php _e( 'is empty', 'cf7-conditional-fields'); ?></code></li>
<li><?php _e( 'Multistep (with Summary)', 'cf7-conditional-fields'); ?></li>
<li><?php _e( 'More comming soon (Calculated Fields, ...)', 'cf7-conditional-fields'); ?></li>
</ul>
<p><a target="_blank" class="button button-primary" href="https://conditional-fields-cf7.bdwm.be/contact-form-7-conditional-fields-pro/"><?php _e( 'Get PRO', 'cf7-conditional-fields'); ?></a></p>
<?php
}
*/
do_action('wpcf7cf_after_animation_settings');
echo '<h3>' . __( 'Advanced Settings', 'cf7-conditional-fields') . '</h3>';
wpcf7cf_input_fields_wrapper_start();
wpcf7cf_input_select('conditions_ui', array(
'label' => __( 'Conditional Fields UI', 'cf7-conditional-fields'),
'description' => sprintf(
// translators: max recommended conditions
__( 'If you want to add more than %s conditions, it\'s recommended to switch to <strong>Text mode</strong> mode for better performance.', 'cf7-conditional-fields' ), WPCF7CF_MAX_RECOMMENDED_CONDITIONS ),
'select_options' => array('normal'=> __( 'Normal', 'cf7-conditional-fields'), 'text_only' => __( 'Text mode', 'cf7-conditional-fields'))
));
wpcf7cf_input_fields_wrapper_end();
submit_button();
?>
</form></div>
<h3><?php _e( 'Restore Default Settings', 'cf7-conditional-fields' ); ?></h3>
<form method="post" id="reset-form" action="">
<p class="submit">
<input name="reset" class="button button-secondary" type="submit" value="<?php _e( 'Restore defaults', 'cf7-conditional-fields' ); ?>" >
<?php wp_nonce_field( 'wpcf7cf_reset_options' ); ?>
<input type="hidden" name="action" value="reset" />
</p>
</form>
<script>
(function($){
$('#reset-form').submit(function() {
return confirm( __( 'Are you sure you want to reset the plugin settings to the default values? All changes you have previously made will be lost.', 'cf7-conditional-fields' ) );
});
}(jQuery))
</script>
<?php
}
function wpcf7cf_input_fields_wrapper_start() {
echo '<table class="form-table" role="presentation"><tbody>';
}
function wpcf7cf_input_fields_wrapper_end() {
echo '</tbody></table>';
}
function wpcf7cf_input_field($slug, $args) {
$settings = wpcf7cf_get_settings();
$defaults = array(
'label'=>'',
'desription' => '',
'default' => wpcf7cf_get_default_settings()[$slug],
'label_editable' => false
);
$args = wp_parse_args( $args, $defaults );
extract($args);
$label; $description; $default; $label_editable;
if (!key_exists($slug, $settings)) {
$settings[$slug] = $default;
$settings[$slug.'_label'] = $label;
}
?>
<tr>
<th scope="row">
<?php if ($label_editable) { ?>
<span class="label editable"><input type="text" data-default-value="<?php echo $label ?>" value="<?php echo $settings[$slug.'_label'] ?>" id="<?php echo WPCF7CF_OPTIONS.'_'.$slug.'_label' ?>" name="<?php echo WPCF7CF_OPTIONS.'['.$slug.'_label]' ?>"></span>
<?php } else { ?>
<label for="<?php echo WPCF7CF_OPTIONS.'_'.$slug ?>"><?php echo $label ?></label>
<?php } ?>
</th>
<td>
<input type="text" data-default-value="<?php echo $default ?>" value="<?php echo htmlspecialchars($settings[$slug]) ?>" id="<?php echo WPCF7CF_OPTIONS.'_'.$slug ?>" name="<?php echo WPCF7CF_OPTIONS.'['.$slug.']' ?>">
<p class="description" id="<?php echo WPCF7CF_OPTIONS.'_'.$slug ?>-description">
<?php echo $description ?><?php if (!empty($default)) echo ' (' . __( 'Default:', 'cf7-conditional-fields' ) . ' '.$default.')' ?>
</p>
</td>
</tr>
<?php
}
function wpcf7cf_input_select($slug, $args) {
$settings = wpcf7cf_get_settings();
$defaults = array(
'label'=>'',
'desription' => '',
'select_options' => array(), // array($name => $value)
'default' => wpcf7cf_get_default_settings()[$slug],
);
$args = wp_parse_args( $args, $defaults );
extract($args);
$label; $description; $select_options; $default;
if (!key_exists($slug, $settings)) {
$settings[$slug] = $default;
}
?>
<tr>
<th scope="row"><label for="<?php echo WPCF7CF_OPTIONS.'_'.$slug ?>"><?php echo $label ?></label></th>
<td>
<select id="<?php echo WPCF7CF_OPTIONS.'_'.$slug ?>" data-default-value="<?php echo $default ?>" name="<?php echo WPCF7CF_OPTIONS.'['.$slug.']' ?>">
<?php foreach($select_options as $value => $text) { ?>
<option value="<?php echo htmlspecialchars( $value ) ?>" <?php echo $settings[$slug]==$value?'selected':'' ?>><?php echo $text ?></option>
<?php } ?>
</select>
<p class="description" id="<?php echo WPCF7CF_OPTIONS.'_'.$slug ?>-description">
<?php echo $description ?><?php if (!empty($default)) echo ' (' . __( 'Default:', 'cf7-conditional-fields' ) . ' '.$select_options[$default].')' ?>
</p>
</td>
</tr>
<?php
}
add_action('admin_init', 'wpcf7cf_admin_init');
function wpcf7cf_admin_init(){
if(isset($_POST['reset']) && current_user_can( 'wpcf7_edit_contact_forms' ) && wp_verify_nonce( $_POST['_wpnonce'], 'wpcf7cf_reset_options' )) {
wpcf7cf_reset_options();
}
register_setting( WPCF7CF_OPTIONS, WPCF7CF_OPTIONS, 'wpcf7cf_options_sanitize' );
}
function wpcf7cf_options_sanitize($input) {
return $input;
}
add_action( 'wp_ajax_wpcf7cf_dismiss_notice', 'wpcf7cf_dismiss_notice' );
function wpcf7cf_dismiss_notice() {
// check nonce
if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'wpcf7cf_dismiss_notice' ) ) {
wp_send_json_error( array( 'message' => __( 'Nonce verification failed', 'cf7-conditional-fields' ) ) );
}
$notice_id = sanitize_text_field($_POST['noticeId'] ?? '');
$notice_suffix = $notice_id ? '_'.$notice_id : $notice_id;
$settings = wpcf7cf_get_settings();
$settings['notice_dismissed'.$notice_suffix] = true;
wpcf7cf_set_options($settings);
}