if ( ! defined( 'ABSPATH' ) ) {
* Functionality related to the admin TinyMCE editor.
class WPForms_Admin_Editor {
* Primary class constructor.
public function __construct() {
add_action( 'media_buttons', [ $this, 'media_button' ], 15 );
* Allow easy shortcode insertion via a custom media button.
* @param string $editor_id Editor Id.
public function media_button( $editor_id ) {
if ( ! \wpforms_current_user_can( 'view_forms' ) ) {
// Provide the ability to conditionally disable the button, so it can be
// disabled for custom fields or front-end use such as bbPress. We default
// to only showing within the post editor page.
if ( ! apply_filters( 'wpforms_display_media_button', $this->is_post_editor_page(), $editor_id ) ) {
// Setup the icon - currently using a dashicon.
$icon = '<span class="wp-media-buttons-icon wpforms-menu-icon" style="font-size:16px;margin-top:-2px;"><svg width="18" height="18" viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path d="M643 911v128h-252v-128h252zm0-255v127h-252v-127h252zm758 511v128h-341v-128h341zm0-256v128h-672v-128h672zm0-255v127h-672v-127h672zm135 860v-1240q0-8-6-14t-14-6h-32l-378 256-210-171-210 171-378-256h-32q-8 0-14 6t-6 14v1240q0 8 6 14t14 6h1240q8 0 14-6t6-14zm-855-1110l185-150h-406zm430 0l221-150h-406zm553-130v1240q0 62-43 105t-105 43h-1240q-62 0-105-43t-43-105v-1240q0-62 43-105t105-43h1240q62 0 105 43t43 105z" fill="#82878c"/></svg></span>';
'<button type="button" class="button wpforms-insert-form-button" data-editor="%s">%s %s</button>',
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
esc_html__( 'Add Form', 'wpforms-lite' )
$min = wpforms_get_min_suffix();
// If we have made it this far then load the JS.
WPFORMS_PLUGIN_URL . "assets/js/admin/admin-editor{$min}.js",
add_action( 'admin_footer', [ $this, 'shortcode_modal' ] );
* Check if we are on the post editor admin page.
* @returns boolean True if it is post editor admin page.
public function is_post_editor_page() {
// get_current_screen() is loaded after 'admin_init' hook and may not exist yet.
if ( ! function_exists( 'get_current_screen' ) ) {
$screen = get_current_screen();
return $screen !== null && $screen->parent_base === 'edit';
* Modal window for inserting the form shortcode into TinyMCE.
* Thickbox is old and busted so we don't use that. Creating a custom view in
* Backbone would make me pull my hair out. So instead we offer a small clean
* modal that is based off of the WordPress insert link modal.
public function shortcode_modal() {
<div id="wpforms-modal-backdrop" style="display: none"></div>
<div id="wpforms-modal-wrap" style="display: none">
<form id="wpforms-modal" tabindex="-1">
<div id="wpforms-modal-title">
<?php esc_html_e( 'Insert Form', 'wpforms-lite' ); ?>
<button type="button" id="wpforms-modal-close"><span class="screen-reader-text"><?php esc_html_e( 'Close', 'wpforms-lite' ); ?></span></button>
<div id="wpforms-modal-inner">
<div id="wpforms-modal-options">
echo '<p id="wpforms-modal-notice">';
wp_kses( /* translators: %s - WPForms documentation URL. */
__( 'Heads up! Don\'t forget to test your form. <a href="%s" target="_blank" rel="noopener noreferrer">Check out our complete guide</a>!', 'wpforms-lite' ),
'https://wpforms.com/docs/how-to-properly-test-your-wordpress-forms-before-launching-checklist/'
$args = apply_filters( 'wpforms_modal_select', [] );
$forms = wpforms()->obj( 'form' )->get( '', $args );
if ( ! empty( $forms ) ) {
printf( '<p><label for="wpforms-modal-select-form">%s</label></p>', esc_html__( 'Select a form below to insert', 'wpforms-lite' ) );
echo '<select id="wpforms-modal-select-form">';
foreach ( $forms as $form ) {
printf( '<option value="%d">%s</option>', esc_html( $form->ID ), esc_html( $form->post_title ) );
printf( '<p class="wpforms-modal-inline"><input type="checkbox" id="wpforms-modal-checkbox-title"><label for="wpforms-modal-checkbox-title">%s</label></p>', esc_html__( 'Show form name', 'wpforms-lite' ) );
printf( '<p class="wpforms-modal-inline"><input type="checkbox" id="wpforms-modal-checkbox-description"><label for="wpforms-modal-checkbox-description">%s</label></p>', esc_html__( 'Show form description', 'wpforms-lite' ) );
/* translators: %s - WPForms Builder page. */
__( 'Whoops, you haven\'t created a form yet. Want to <a href="%s">give it a go</a>?', 'wpforms-lite' ),
esc_url( admin_url( 'admin.php?page=wpforms-builder' ) )
<div id="wpforms-modal-cancel">
<a class="submitdelete deletion" href="#"><?php esc_html_e( 'Cancel', 'wpforms-lite' ); ?></a>
<?php if ( ! empty( $forms ) ) : ?>
<div id="wpforms-modal-update">
<button class="button button-primary" id="wpforms-modal-submit"><?php esc_html_e( 'Add Form', 'wpforms-lite' ); ?></button>
.wpforms-insert-form-button svg path {
.wpforms-insert-form-button:hover svg path {
-webkit-box-shadow: 0 3px 6px rgba(0, 0, 0, 0.3);
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.3);
-webkit-transition: height 0.2s, margin-top 0.2s;
transition: height 0.2s, margin-top 0.2s;
#wpforms-modal-backdrop {
filter: alpha(opacity=70);
border-bottom: 1px solid #dfdfdf;
#wpforms-modal-close:before {
font: normal 20px/36px 'dashicons';
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
#wpforms-modal-close:hover,
#wpforms-modal-close:focus {
#wpforms-modal-close:focus {
-webkit-box-shadow: 0 0 0 1px #5b9dd9,
0 0 2px 1px rgba(30, 140, 190, .8);
box-shadow: 0 0 0 1px #5b9dd9,
0 0 2px 1px rgba(30, 140, 190, .8);
#wpforms-modal-search-toggle:after {
font: normal 20px/1 'dashicons';
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
background-color: #d9edf7;
border: 1px solid #bce8f1;
#wpforms-modal #wpforms-modal-options {
#wpforms-modal #wpforms-modal-options .wpforms-modal-inline {
#wpforms-modal-select-form {
#wpforms-modal .submitbox {
border-top: 1px solid #dfdfdf;
@media screen and ( max-width: 782px ) {
@media screen and ( max-width: 520px ) {
@media screen and ( max-height: 520px ) {
-webkit-transition: none;
@media screen and ( max-height: 290px ) {
height: -webkit-calc(100% - 92px);
height: calc(100% - 92px);
new WPForms_Admin_Editor();