| 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', array( $this, 'enqueue_scripts' ) ); |
| 15 |
add_action( 'media_buttons', array( $this, 'add_media_button' ), 20 ); |
| 16 |
add_action( 'admin_footer', array( $this, 'media_thickbox_content' ) ); |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
/** |
| 21 |
* Enqueue scripts and styles for form builder |
| 22 |
* |
| 23 |
* @global string $pagenow |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
function enqueue_scripts() { |
| 27 |
global $pagenow; |
| 28 |
|
| 29 |
if ( !in_array( $pagenow, array( 'post.php', 'post-new.php') ) ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
wp_enqueue_script( 'weforms-shortcode', WEFORMS_ASSET_URI . '/js/weforms-shortcode.js', array('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 |
* @return void |
| 41 |
*/ |
| 42 |
function add_media_button( $editor_id ) { |
| 43 |
?> |
| 44 |
<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 _e( 'Add a Form', 'weforms' ); ?>"> |
| 45 |
<?php echo '<span class="wp-media-buttons-icon dashicons dashicons-welcome-widgets-menus"></span>' . __( ' Add Contact Form', 'weforms' ); ?> |
| 46 |
</a> |
| 47 |
<?php |
| 48 |
|
| 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, array( 'post.php', 'post-new.php') ) ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
?> |
| 63 |
|
| 64 |
<div id="weforms-media-dialog" style="display: none;"> |
| 65 |
|
| 66 |
<div class="weforms-popup-container"> |
| 67 |
<?php |
| 68 |
|
| 69 |
$all_forms = weforms()->form->all(); |
| 70 |
$options = sprintf( "<option value='%s'>%s</option>", 0, __('— Select Form —', 'weforms') ); |
| 71 |
foreach ( $all_forms['forms'] as $form ) { |
| 72 |
$options.= sprintf( "<option value='%s'>%s</option>", $form->id, $form->name ); |
| 73 |
} |
| 74 |
?> |
| 75 |
|
| 76 |
<div class="weforms-form-div"> |
| 77 |
<label><h3><?php _e( 'Select a form to insert', 'weforms' ); ?></h3></label> |
| 78 |
<select id="weforms-form-select"> |
| 79 |
<?php echo $options; ?> |
| 80 |
</select> |
| 81 |
</div> |
| 82 |
|
| 83 |
<div class="submit-button weforms-submit-div"> |
| 84 |
<button id="weforms-form-insert" class="button-primary"><?php _e( 'Insert Form', 'weforms' ); ?></button> |
| 85 |
<button id="weforms-form-close" class="button-secondary" style="margin-left: 5px;" onClick="tb_remove();"><?php _e( 'Close', 'weforms' ); ?></a></button> |
| 86 |
</div> |
| 87 |
|
| 88 |
</div> |
| 89 |
</div> |
| 90 |
|
| 91 |
<style type="text/css"> |
| 92 |
|
| 93 |
.weforms-form-div { |
| 94 |
padding: 10px; |
| 95 |
clear: left; |
| 96 |
} |
| 97 |
.weforms-submit-div { |
| 98 |
padding: 10px; |
| 99 |
clear: left; |
| 100 |
float: left; |
| 101 |
width: 90%; |
| 102 |
} |
| 103 |
</style> |
| 104 |
|
| 105 |
<?php |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
/** |
| 111 |
* * Singleton object |
| 112 |
* |
| 113 |
* @staticvar boolean $instance |
| 114 |
* |
| 115 |
* @return \self |
| 116 |
*/ |
| 117 |
public static function init() { |
| 118 |
static $instance = false; |
| 119 |
|
| 120 |
if ( !$instance ) { |
| 121 |
$instance = new Weforms_Form_Button(); |
| 122 |
} |
| 123 |
|
| 124 |
return $instance; |
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
Weforms_Form_Button::init(); |
| 130 |
|