| 1 |
<?php |
| 2 |
|
| 3 |
class AF_Admin_Forms_Preview { |
| 4 |
|
| 5 |
function __construct() { |
| 6 |
add_action( 'admin_menu', array( $this, 'register_admin_page' ), 10, 0 ); |
| 7 |
add_action( 'af/admin/form/actions', array( $this, 'add_preview_button' ), 10, 1 ); |
| 8 |
add_filter( 'admin_title', array( $this, 'fix_admin_title' ), 10, 2 ); |
| 9 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_acf' ), 10, 1 ); |
| 10 |
|
| 11 |
add_filter( 'af/form/button_attributes', array( $this, 'add_classes_to_button' ), 10, 1 ); |
| 12 |
add_filter( 'af/form/previous_button_atts', array( $this, 'add_classes_to_page_buttons' ), 10, 1 ); |
| 13 |
add_filter( 'af/form/next_button_atts', array( $this, 'add_classes_to_page_buttons' ), 10, 1 ); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Register hidden admin page for form previews. |
| 18 |
* |
| 19 |
* @since 1.4.0 |
| 20 |
*/ |
| 21 |
function register_admin_page() { |
| 22 |
// By using add_submenu_page without a parent the page won't be shown in the admin menu. |
| 23 |
add_submenu_page( |
| 24 |
'admin.php', |
| 25 |
'Preview form', |
| 26 |
'Preview', |
| 27 |
'edit_pages', |
| 28 |
'af_preview_form', |
| 29 |
[ $this, 'preview_page' ] |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Display form preview page |
| 35 |
* |
| 36 |
* @since 1.4.0 |
| 37 |
*/ |
| 38 |
function preview_page() { |
| 39 |
if ( ! isset( $_GET['form_id'] ) ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$form = af_form_from_post( $_GET['form_id'] ); |
| 44 |
|
| 45 |
echo '<div class="af-form-preview wrap">'; |
| 46 |
|
| 47 |
echo sprintf( '<h1 class="wp-heading-inline">%s "%s"</h1>', __( 'Previewing', 'advanced-forms' ), $form['title'] ); |
| 48 |
echo sprintf( '<a href="%s" class="page-title-action">%s</a>', get_edit_post_link( $form['post_id'] ), __( 'Back to form', 'advanced-forms' ) ); |
| 49 |
echo '<hr class="wp-header-end">'; |
| 50 |
|
| 51 |
if ( af_has_submission() && ! af_submission_failed() ) { |
| 52 |
echo '<div id="message" class="notice notice-success">'; |
| 53 |
echo sprintf( '<p>%s. <a href="%s">%s</a></p>', __( 'Form successfully submitted', 'advanced-forms' ), '', __( 'Test again', 'advanced-forms' ) ); |
| 54 |
echo '</div>'; |
| 55 |
} |
| 56 |
|
| 57 |
echo '<div id="poststuff">'; |
| 58 |
echo '<div class="postbox acf-postbox">'; |
| 59 |
echo '<div class="inside acf-fields">'; |
| 60 |
|
| 61 |
advanced_form( $form['post_id'] ); |
| 62 |
|
| 63 |
echo '</div>'; |
| 64 |
echo '</div>'; |
| 65 |
echo '</div>'; |
| 66 |
echo '</div>'; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Wordpress won't display a page title, probably of the submenu hack in register_admin_page. |
| 71 |
* This function is hooked to the admin_title filter and fixes the issue. |
| 72 |
* |
| 73 |
* @since 1.4.0 |
| 74 |
*/ |
| 75 |
function fix_admin_title( $admin_title, $title ) { |
| 76 |
if ( get_current_screen()->id != 'admin_page_af_preview_form' ) { |
| 77 |
return $admin_title; |
| 78 |
} |
| 79 |
|
| 80 |
return __( 'Preview form', 'advanced-forms' ) . $admin_title; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* We need to enqueue ACF early for it to be included in the head. |
| 85 |
* ACF needs to be enqueued early in the admin panel. |
| 86 |
* |
| 87 |
* @since 1.5.3 |
| 88 |
*/ |
| 89 |
function enqueue_acf( $page ) { |
| 90 |
if ( 'admin_page_af_preview_form' != $page ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
acf_enqueue_scripts(); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Add preview button to form publish box |
| 99 |
* |
| 100 |
* @since 1.4.0 |
| 101 |
*/ |
| 102 |
function add_preview_button( $form_id ) { |
| 103 |
$test_url = add_query_arg( 'form_id', $form_id, menu_page_url( 'af_preview_form', false ) ); |
| 104 |
echo sprintf( '<a href="%s" class="button button-large">%s</a>', $test_url, __( 'Preview', 'advanced-forms' ) ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Add WP button classes to form submit buttons in admin |
| 109 |
* |
| 110 |
* @since 1.4.0 |
| 111 |
*/ |
| 112 |
function add_classes_to_button( $attributes ) { |
| 113 |
if ( ! is_admin() ) { |
| 114 |
return $attributes; |
| 115 |
} |
| 116 |
|
| 117 |
$attributes['class'] .= ' button button-primary button-large'; |
| 118 |
|
| 119 |
return $attributes; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Add WP button classes to multi-page form buttons |
| 124 |
* |
| 125 |
* @since 1.5.0 |
| 126 |
*/ |
| 127 |
function add_classes_to_page_buttons( $attributes ) { |
| 128 |
if ( ! is_admin() ) { |
| 129 |
return $attributes; |
| 130 |
} |
| 131 |
|
| 132 |
$attributes['class'] .= ' button button-large'; |
| 133 |
|
| 134 |
return $attributes; |
| 135 |
} |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
return new AF_Admin_Forms_Preview(); |