| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* weForms Shortcode Button class |
| 5 |
* |
| 6 |
* @since 1.2.9 |
| 7 |
*/ |
| 8 |
class Weforms_Form_Button { |
| 9 |
|
| 10 |
/** |
| 11 |
* Constructor for shortcode class |
| 12 |
*/ |
| 13 |
public function __construct() { |
| 14 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 15 |
add_action( 'media_buttons', [ $this, 'add_media_button' ], 20 ); |
| 16 |
add_action( 'admin_footer', [ $this, 'media_thickbox_content' ] ); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Enqueue scripts and styles for form builder |
| 21 |
* |
| 22 |
* @global string $pagenow |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function enqueue_scripts() { |
| 27 |
global $pagenow; |
| 28 |
|
| 29 |
if ( !in_array( $pagenow, [ 'post.php', 'post-new.php'] ) ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
wp_enqueue_script( 'weforms-shortcode', WEFORMS_ASSET_URI . '/js/weforms-shortcode.js', ['jquery'] ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Adds a media button (for inserting a form) to the Post Editor |
| 38 |
* |
| 39 |
* @param int $editor_id The editor ID |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public function add_media_button( $editor_id ) { |
| 44 |
?> |
| 45 |
<a href="#TB_inline?width=480&inlineId=weforms-media-dialog" class="button thickbox insert-form" data-editor="<?php echo esc_attr( $editor_id ); ?>" title="<?php esc_html_e( 'Add a Form', 'weforms' ); ?>"> |
| 46 |
<?php echo '<span class="wp-media-buttons-icon dashicons dashicons-welcome-widgets-menus"></span>' . esc_html_e( ' Add Contact Form', 'weforms' ); ?> |
| 47 |
</a> |
| 48 |
<?php |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Prints the thickbox popup content |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function media_thickbox_content() { |
| 57 |
global $pagenow; |
| 58 |
|
| 59 |
if ( !in_array( $pagenow, [ 'post.php', 'post-new.php'] ) ) { |
| 60 |
return; |
| 61 |
} ?> |
| 62 |
|
| 63 |
<div id="weforms-media-dialog" style="display: none;"> |
| 64 |
|
| 65 |
<div class="weforms-popup-container"> |
| 66 |
<?php |
| 67 |
|
| 68 |
$all_forms = weforms()->form->all(); |
| 69 |
$options = sprintf( "<option value='%s'>%s</option>", 0, __( '— Select Form —', 'weforms' ) ); |
| 70 |
|
| 71 |
foreach ( $all_forms['forms'] as $form ) { |
| 72 |
$options .= sprintf( "<option value='%s'>%s</option>", esc_attr( $form->id ), esc_attr( $form->name ) ); |
| 73 |
} ?> |
| 74 |
|
| 75 |
<div class="weforms-form-div"> |
| 76 |
<label><h3><?php esc_html_e( 'Select a form to insert', 'weforms' ); ?></h3></label> |
| 77 |
<select id="weforms-form-select"> |
| 78 |
<?php echo wp_kses( $options, array( 'option' => array( 'value' => array() ) ) ); ?> |
| 79 |
</select> |
| 80 |
</div> |
| 81 |
|
| 82 |
<div class="submit-button weforms-submit-div"> |
| 83 |
<button id="weforms-form-insert" class="button-primary"><?php esc_html_e( 'Insert Form', 'weforms' ); ?></button> |
| 84 |
<button id="weforms-form-close" class="button-secondary" style="margin-left: 5px;" onClick="tb_remove();"><?php esc_html_e( 'Close', 'weforms' ); ?></a></button> |
| 85 |
</div> |
| 86 |
|
| 87 |
</div> |
| 88 |
</div> |
| 89 |
|
| 90 |
<style type="text/css"> |
| 91 |
|
| 92 |
.weforms-form-div { |
| 93 |
padding: 10px; |
| 94 |
clear: left; |
| 95 |
} |
| 96 |
.weforms-submit-div { |
| 97 |
padding: 10px; |
| 98 |
clear: left; |
| 99 |
float: left; |
| 100 |
width: 90%; |
| 101 |
} |
| 102 |
</style> |
| 103 |
|
| 104 |
<?php |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* * Singleton object |
| 109 |
* |
| 110 |
* @staticvar boolean $instance |
| 111 |
* |
| 112 |
* @return \self |
| 113 |
*/ |
| 114 |
public static function init() { |
| 115 |
static $instance = false; |
| 116 |
|
| 117 |
if ( !$instance ) { |
| 118 |
$instance = new Weforms_Form_Button(); |
| 119 |
} |
| 120 |
|
| 121 |
return $instance; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
Weforms_Form_Button::init(); |
| 126 |
|