| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Handles the admin part of the forms |
| 5 |
* |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
*/ |
| 9 |
class AF_Admin_Forms { |
| 10 |
function __construct() { |
| 11 |
// Actions |
| 12 |
add_action( 'admin_init', array( $this, 'add_fields_meta_box' ), 10, 0 ); |
| 13 |
add_action( 'edit_form_after_title', array( $this, 'display_form_key' ), 10, 0 ); |
| 14 |
add_filter( 'acf/prepare_field/name=form_shortcode_message', array( $this, 'display_form_shortcode' ), 10, 1 ); |
| 15 |
add_action( 'save_post', array( $this, 'add_form_key' ), 10, 3 ); |
| 16 |
add_filter( 'add_post_metadata', array( $this, 'should_add_form_key_meta' ), 10, 3 ); |
| 17 |
add_action( 'acf/init', array( $this, 'register_fields' ), 10, 0 ); |
| 18 |
add_action( 'media_buttons', array( $this, 'add_wysiwyg_content_field_inserter' ), 10, 1 ); |
| 19 |
add_action( 'admin_footer', array( $this, 'add_forms_sidebar' ), 10, 0 ); |
| 20 |
|
| 21 |
add_action( 'post_submitbox_start', array( $this, 'add_actions' ), 10, 1 ); |
| 22 |
|
| 23 |
add_filter( 'manage_af_form_posts_columns', array( $this, 'manage_columns' ), 10, 1 ); |
| 24 |
add_action( 'manage_af_form_posts_custom_column', array( $this, 'custom_columns_content' ), 10, 2 ); |
| 25 |
add_filter( 'disable_months_dropdown', array( $this, 'disable_months_filter' ), 10, 2 ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Adds a form key to a form if one doesn't exist |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
* |
| 33 |
*/ |
| 34 |
function add_form_key( $post_id, $post, $update ) { |
| 35 |
if ( 'af_form' == $post->post_type && ! get_post_meta( $post->ID, 'form_key', true ) ) { |
| 36 |
$form_key = 'form_' . uniqid(); |
| 37 |
update_post_meta( $post->ID, 'form_key', $form_key ); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Stops new form keys from being saved to a form post if a key already exists. |
| 43 |
* Some plugins that duplicate posts will cause trouble as forms will end up with multiple form keys. |
| 44 |
* |
| 45 |
* @since 1.9.0 |
| 46 |
* |
| 47 |
*/ |
| 48 |
function should_add_form_key_meta( $check, $object_id, $meta_key ) { |
| 49 |
if ( 'form_key' !== $meta_key ) { |
| 50 |
return $check; |
| 51 |
} |
| 52 |
|
| 53 |
// If a form key already exists, we don't want to save another one |
| 54 |
if ( metadata_exists( 'post', $object_id, $meta_key ) ) { |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
return $check; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Displays the form key after the title |
| 63 |
* |
| 64 |
* @since 1.0.0 |
| 65 |
* |
| 66 |
*/ |
| 67 |
function display_form_key() { |
| 68 |
global $post; |
| 69 |
|
| 70 |
if ( 'af_form' == $post->post_type && $form_key = get_post_meta( $post->ID, 'form_key', true ) ) { |
| 71 |
echo '<div id="edit-slug-box">'; |
| 72 |
echo sprintf( '<strong>%s </strong>%s', __( 'Form key:', 'advanced-forms' ), $form_key ); |
| 73 |
echo '</div>'; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Display the form shortcode in the form settings. |
| 79 |
* |
| 80 |
* @since 1.6.4 |
| 81 |
* |
| 82 |
*/ |
| 83 |
function display_form_shortcode( $field ) { |
| 84 |
global $post; |
| 85 |
|
| 86 |
if ( $post && $key = get_post_meta( $post->ID, 'form_key', true ) ) { |
| 87 |
$shortcode = sprintf( '[advanced_form form="%s"]', $key ); |
| 88 |
$field['message'] = sprintf( |
| 89 |
'<span class="af-shortcode-field">' |
| 90 |
. '<code>%s</code>' |
| 91 |
. '<button type="button" class="button button-small af-copy-button" data-copied-text="%s">%s</button>' |
| 92 |
. '</span>', |
| 93 |
esc_html( $shortcode ), |
| 94 |
esc_attr__( 'Copied!', 'advanced-forms' ), |
| 95 |
esc_html__( 'Copy', 'advanced-forms' ) |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
return $field; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Adds a meta box to the form edit page used to display the form fields |
| 104 |
* |
| 105 |
* @since 1.0.0 |
| 106 |
* |
| 107 |
*/ |
| 108 |
function add_fields_meta_box() { |
| 109 |
add_meta_box( 'af_form_fields', __( 'Fields', 'advanced-forms' ), array( $this, 'fields_meta_box_callback' ), 'af_form', 'normal', 'default', null ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Callback for the fields meta box |
| 114 |
* Displays all fields registered to the current form |
| 115 |
* |
| 116 |
* @since 1.0.0 |
| 117 |
* |
| 118 |
*/ |
| 119 |
function fields_meta_box_callback() { |
| 120 |
global $post; |
| 121 |
|
| 122 |
$form = af_get_form( $post->ID ); |
| 123 |
|
| 124 |
// Get field groups for the current form |
| 125 |
$field_groups = af_get_form_field_groups( $form['key'] ); |
| 126 |
?> |
| 127 |
|
| 128 |
<p><?php _e( 'Add fields by setting the location of your fields group to this form.', 'advanced-forms' ); ?></p> |
| 129 |
|
| 130 |
<table class="widefat af-field-group-table"> |
| 131 |
<thead> |
| 132 |
<tr> |
| 133 |
<th scope="col"><?php _e( 'Label', 'advanced-forms' ) ?></th> |
| 134 |
<th scope="col"><?php _e( 'Name', 'advanced-forms' ) ?></th> |
| 135 |
<th scope="col"><?php _e( 'Type', 'advanced-forms' ) ?></th> |
| 136 |
</tr> |
| 137 |
</thead> |
| 138 |
<tbody> |
| 139 |
<?php if ( ! empty( $field_groups ) ) : ?> |
| 140 |
<?php foreach ( $field_groups as $field_group ) : ?> |
| 141 |
<?php |
| 142 |
// Get all fields for this field group |
| 143 |
$fields = acf_get_fields( $field_group ); |
| 144 |
?> |
| 145 |
<tr class="field-group-heading"> |
| 146 |
<td colspan="3"> |
| 147 |
<a href="<?php echo get_edit_post_link( $field_group['ID'] ); ?>"><?php echo $field_group['title']; ?></a> |
| 148 |
</td> |
| 149 |
</tr> |
| 150 |
<?php foreach ( $fields as $field ) : ?> |
| 151 |
<tr> |
| 152 |
<td><?php echo $field['label']; ?></td> |
| 153 |
<td><?php echo $field['name']; ?></td> |
| 154 |
<td><?php echo acf_get_field_type_label( $field['type'] ); ?></td> |
| 155 |
</tr> |
| 156 |
<?php endforeach; ?> |
| 157 |
<?php endforeach; ?> |
| 158 |
<?php else: ?> |
| 159 |
<tr> |
| 160 |
<td colspan="3"> |
| 161 |
<?php _e( 'No field groups connected to this form', 'advanced-forms' ); ?> |
| 162 |
</td> |
| 163 |
</tr> |
| 164 |
<?php endif; ?> |
| 165 |
</tbody> |
| 166 |
</table> |
| 167 |
<a href="<?php echo admin_url( 'post-new.php?post_type=acf-field-group' ); ?>" class="button"> |
| 168 |
<?php _e( 'Create field group', 'advanced-forms' ); ?> |
| 169 |
</a> |
| 170 |
<?php |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Adds custom columns to the listings page |
| 175 |
* |
| 176 |
* @since 1.0.0 |
| 177 |
* |
| 178 |
*/ |
| 179 |
function manage_columns( $columns ) { |
| 180 |
$new_columns = array( |
| 181 |
'key' => __( 'Key', 'advanced-forms' ), |
| 182 |
'fields' => __( 'Fields', 'advanced-forms' ), |
| 183 |
'entries' => __( 'Entries', 'advanced-forms' ), |
| 184 |
); |
| 185 |
|
| 186 |
// Remove date column |
| 187 |
unset( $columns['date'] ); |
| 188 |
|
| 189 |
return array_merge( array_splice( $columns, 0, 2 ), $new_columns, $columns ); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Outputs the content for the custom columns |
| 194 |
* |
| 195 |
* @since 1.0.0 |
| 196 |
* |
| 197 |
*/ |
| 198 |
function custom_columns_content( $column, $post_id ) { |
| 199 |
$form = af_get_form( $post_id ); |
| 200 |
|
| 201 |
if ( 'key' == $column ) { |
| 202 |
echo get_post_meta( $post_id, 'form_key', true ); |
| 203 |
} elseif ( 'fields' == $column ) { |
| 204 |
$count = 0; |
| 205 |
$field_groups = af_get_form_field_groups( $form['key'] ); |
| 206 |
|
| 207 |
// Count the number of fields in all field groups |
| 208 |
foreach ( $field_groups as $field_group ) { |
| 209 |
$fields = acf_get_fields( $field_group ); |
| 210 |
$count += count( $fields ); |
| 211 |
} |
| 212 |
echo $count; |
| 213 |
} elseif ( 'entries' == $column ) { |
| 214 |
$entries = af_get_entries( $form['key'] ); |
| 215 |
echo sprintf( '<a href="%s">%s</a>', admin_url() . '/edit.php?post_type=af_entry&entry_form=' . $form['key'], count( $entries ) ); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Add an "Insert field" button to WYSIWYG fields |
| 221 |
* |
| 222 |
* @since 1.0.0 |
| 223 |
* |
| 224 |
*/ |
| 225 |
function add_wysiwyg_content_field_inserter( $id ) { |
| 226 |
global $post; |
| 227 |
|
| 228 |
if ( ! $post ) { |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
$form = af_form_from_post( $post ); |
| 233 |
if ( ! $form ) { |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
if ( 'acf-editor' == substr($id, 0, 10) ) { |
| 238 |
_af_field_inserter_button( $form, 'all', false ); |
| 239 |
} |
| 240 |
|
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Outputs the sidebar on the forms list page |
| 245 |
* Moved up by Javascript |
| 246 |
* |
| 247 |
* @since 1.5.0 |
| 248 |
* |
| 249 |
*/ |
| 250 |
function add_forms_sidebar() { |
| 251 |
$title = AF()->pro ? 'Advanced Forms Pro' : 'Advanced Forms'; |
| 252 |
$doc_url = 'https://advancedforms.github.io'; |
| 253 |
$pro_url = 'https://hookturn.io/downloads/advanced-forms'; |
| 254 |
$icon = '<i aria-hidden="true" class="dashicons dashicons-external"></i>'; |
| 255 |
?> |
| 256 |
<script type="text/html" id="af-sidebar-template"> |
| 257 |
<div class="af-sidebar"> |
| 258 |
<div class="acf-box"> |
| 259 |
<div class="inner"> |
| 260 |
<h2><?php echo $title; ?></h2> |
| 261 |
|
| 262 |
<h3><?php _e( 'Resources','advanced-forms' ); ?></h3> |
| 263 |
<ul> |
| 264 |
<li><a href="<?php echo $doc_url; ?>#guides"><?php echo $icon; ?> <?php _e( 'Guides', 'advanced-forms' ); ?></a></li> |
| 265 |
<li><a href="<?php echo $doc_url; ?>#actions"><?php echo $icon; ?> <?php _e( 'Actions', 'advanced-forms' ); ?></a></li> |
| 266 |
<li><a href="<?php echo $doc_url; ?>#filters"><?php echo $icon; ?> <?php _e( 'Filters', 'advanced-forms' ); ?></a></li> |
| 267 |
</ul> |
| 268 |
|
| 269 |
<?php if ( ! AF()->pro ) : ?> |
| 270 |
<h3><?php _e( 'Pro','advanced-forms' ); ?></h3> |
| 271 |
<ul class="feature-list"> |
| 272 |
<li><?php _e( 'Create and edit posts and users', 'advanced-forms' ); ?></li> |
| 273 |
<li><?php _e( 'Integrate with Slack, Mailchimp, and Zapier', 'advanced-forms' ); ?></li> |
| 274 |
<li><?php _e( 'Protect your form against spam with Google reCAPTCHA', 'advanced-forms' ); ?></li> |
| 275 |
<li><?php _e( 'Use calculated fields to give fields to give users immediate feedback', 'advanced-forms' ); ?></li> |
| 276 |
<li><?php _e( 'Get direct, priority support', 'advanced-forms' ); ?></li> |
| 277 |
</ul> |
| 278 |
<a href="<?php echo $pro_url; ?>"><?php echo $icon; ?> <?php _e( 'Available from hookturn.io', 'advanced-forms' ); ?></a> |
| 279 |
<?php endif; ?> |
| 280 |
|
| 281 |
<h3><?php _e( 'Support','advanced-forms' ); ?></h3> |
| 282 |
<p> |
| 283 |
<?php _e( 'Issues, questions, or suggestions?', 'advanced-forms' ); ?> |
| 284 |
<?php if ( AF()->pro ) : ?> |
| 285 |
<?php _e( 'Contact us directly', 'advanced-forms' ); ?><a href="mailto:support@hookturn.io?subject=Advanced%20Forms%20Pro"></a> <?php _e( 'for priority support', 'advanced-forms' ); ?>. |
| 286 |
<?php else : ?> |
| 287 |
<?php _e( 'Create a ticket on the', 'advanced-forms' ); ?> <a href="https://wordpress.org/support/plugin/advanced-forms"><?php _e( 'Wordpress support forums', 'advanced-forms' ); ?></a>. |
| 288 |
<?php endif; ?> |
| 289 |
</p> |
| 290 |
</div> |
| 291 |
</div> |
| 292 |
</div> |
| 293 |
</script> |
| 294 |
<?php |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Adds an wrapper for action buttons to the publish box. |
| 299 |
* Refactored out of admin-preview.php. |
| 300 |
* |
| 301 |
* @since 1.5.0 |
| 302 |
* |
| 303 |
*/ |
| 304 |
function add_actions( $post ) { |
| 305 |
if ( empty( $post ) || 'af_form' != $post->post_type ) { |
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
echo '<div class="af-form-actions-wrapper">'; |
| 310 |
do_action( 'af/admin/form/actions', $post->ID ); |
| 311 |
echo '</div>'; |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Hides the months filter on the forms listing page. |
| 316 |
* |
| 317 |
* @since 1.6.5 |
| 318 |
* |
| 319 |
*/ |
| 320 |
function disable_months_filter( $disabled, $post_type ) { |
| 321 |
if ( 'af_form' != $post_type ) { |
| 322 |
return $disabled; |
| 323 |
} |
| 324 |
|
| 325 |
return true; |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Registers the form settings fields |
| 330 |
* |
| 331 |
* @since 1.0.0 |
| 332 |
* |
| 333 |
*/ |
| 334 |
function register_fields() { |
| 335 |
$settings_field_group = array ( |
| 336 |
'key' => 'group_form_settings', |
| 337 |
'title' => __( 'Form settings', 'advanced-forms' ), |
| 338 |
'fields' => array ( |
| 339 |
array ( |
| 340 |
'key' => 'field_form_display_tab', |
| 341 |
'label' => '<span class="dashicons dashicons-visibility"></span>' . __( 'Display', 'advanced-forms' ), |
| 342 |
'name' => '', |
| 343 |
'type' => 'tab', |
| 344 |
'instructions' => '', |
| 345 |
'required' => 0, |
| 346 |
'conditional_logic' => 0, |
| 347 |
'wrapper' => array ( |
| 348 |
'width' => '', |
| 349 |
'class' => '', |
| 350 |
'id' => '', |
| 351 |
), |
| 352 |
'placement' => 'left', |
| 353 |
'endpoint' => 0, |
| 354 |
), |
| 355 |
array( |
| 356 |
'key' => 'field_form_shortcode_message', |
| 357 |
'label' => __( 'Shortcode', 'advanced-forms' ), |
| 358 |
'name' => 'form_shortcode_message', |
| 359 |
'type' => 'message', |
| 360 |
), |
| 361 |
array ( |
| 362 |
'key' => 'field_form_description', |
| 363 |
'label' => __( 'Description', 'advanced-forms' ), |
| 364 |
'name' => 'form_description', |
| 365 |
'type' => 'textarea', |
| 366 |
'instructions' => '', |
| 367 |
'required' => 0, |
| 368 |
'conditional_logic' => 0, |
| 369 |
'wrapper' => array ( |
| 370 |
'width' => '', |
| 371 |
'class' => '', |
| 372 |
'id' => '', |
| 373 |
), |
| 374 |
'default_value' => '', |
| 375 |
'tabs' => 'all', |
| 376 |
'toolbar' => 'full', |
| 377 |
'media_upload' => 1, |
| 378 |
), |
| 379 |
array ( |
| 380 |
'key' => 'field_form_success_message', |
| 381 |
'label' => __( 'Success message', 'advanced-forms' ), |
| 382 |
'name' => 'form_success_message', |
| 383 |
'type' => 'wysiwyg', |
| 384 |
'instructions' => __( 'The message displayed after a successful submission.', 'advanced-forms' ), |
| 385 |
'required' => 0, |
| 386 |
'conditional_logic' => 0, |
| 387 |
'wrapper' => array ( |
| 388 |
'width' => '', |
| 389 |
'class' => '', |
| 390 |
'id' => '', |
| 391 |
), |
| 392 |
'default_value' => '', |
| 393 |
'tabs' => 'all', |
| 394 |
'toolbar' => 'full', |
| 395 |
'media_upload' => 1, |
| 396 |
), |
| 397 |
array ( |
| 398 |
'key' => 'field_form_statistics_tab', |
| 399 |
'label' => '<span class="dashicons dashicons-chart-bar"></span>' . __( 'Statistics', 'advanced-forms' ), |
| 400 |
'name' => '', |
| 401 |
'type' => 'tab', |
| 402 |
'instructions' => '', |
| 403 |
'required' => 0, |
| 404 |
'conditional_logic' => 0, |
| 405 |
'wrapper' => array ( |
| 406 |
'width' => '', |
| 407 |
'class' => '', |
| 408 |
'id' => '', |
| 409 |
), |
| 410 |
'placement' => 'left', |
| 411 |
'endpoint' => 0, |
| 412 |
), |
| 413 |
array ( |
| 414 |
'key' => 'field_form_num_of_submissions', |
| 415 |
'label' => __( 'Number of submissions', 'advanced-forms' ), |
| 416 |
'name' => 'form_num_of_submissions', |
| 417 |
'type' => 'number', |
| 418 |
'instructions' => '', |
| 419 |
'required' => 0, |
| 420 |
'conditional_logic' => 0, |
| 421 |
'wrapper' => array ( |
| 422 |
'width' => '50', |
| 423 |
'class' => '', |
| 424 |
'id' => '', |
| 425 |
), |
| 426 |
'default_value' => 0, |
| 427 |
'placeholder' => '', |
| 428 |
'prepend' => '', |
| 429 |
'append' => '', |
| 430 |
'min' => '', |
| 431 |
'max' => '', |
| 432 |
'step' => '', |
| 433 |
'readonly' => true, |
| 434 |
), |
| 435 |
array ( |
| 436 |
'key' => 'field_form_num_of_views', |
| 437 |
'label' => __( 'Number of times viewed', 'advanced-forms' ), |
| 438 |
'name' => 'form_num_of_views', |
| 439 |
'type' => 'number', |
| 440 |
'instructions' => '', |
| 441 |
'required' => 0, |
| 442 |
'conditional_logic' => 0, |
| 443 |
'wrapper' => array ( |
| 444 |
'width' => '50', |
| 445 |
'class' => '', |
| 446 |
'id' => '', |
| 447 |
), |
| 448 |
'default_value' => 0, |
| 449 |
'placeholder' => '', |
| 450 |
'prepend' => '', |
| 451 |
'append' => '', |
| 452 |
'min' => '', |
| 453 |
'max' => '', |
| 454 |
'step' => '', |
| 455 |
'readonly' => true, |
| 456 |
), |
| 457 |
), |
| 458 |
'location' => array ( |
| 459 |
array ( |
| 460 |
array ( |
| 461 |
'param' => 'post_type', |
| 462 |
'operator' => '==', |
| 463 |
'value' => 'af_form', |
| 464 |
), |
| 465 |
), |
| 466 |
), |
| 467 |
'menu_order' => 0, |
| 468 |
'position' => 'normal', |
| 469 |
'style' => 'default', |
| 470 |
'label_placement' => 'top', |
| 471 |
'instruction_placement' => 'label', |
| 472 |
'hide_on_screen' => '', |
| 473 |
'active' => 1, |
| 474 |
'description' => '', |
| 475 |
); |
| 476 |
|
| 477 |
$settings_field_group = apply_filters( 'af/form/settings_fields', $settings_field_group ); |
| 478 |
acf_add_local_field_group( $settings_field_group ); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
return new AF_Admin_Forms(); |