-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwp-super-duper.php
5447 lines (4706 loc) · 190 KB
/
wp-super-duper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WP_Super_Duper' ) ) {
define( 'SUPER_DUPER_VER', '1.2.19' );
/**
* A Class to be able to create a Widget, Shortcode or Block to be able to output content for WordPress.
*
* Should not be called direct but extended instead.
*
* Class WP_Super_Duper
* @since 1.0.16 change log moved to file change-log.txt - CHANGED
* @ver 1.1.1
*/
class WP_Super_Duper extends WP_Widget {
public $version = SUPER_DUPER_VER;
public $font_awesome_icon_version = "5.11.2";
public $block_code;
public $options;
public $base_id;
public $settings_hash;
public $arguments = array();
public $instance = array();
private $class_name;
/**
* The relative url to the current folder.
*
* @var string
*/
public $url = '';
/**
* Take the array options and use them to build.
*/
public function __construct( $options ) {
global $sd_widgets;
$sd_widgets[ $options['base_id'] ] = array(
'name' => $options['name'],
'class_name' => $options['class_name'],
'output_types' => !empty($options['output_types']) ? $options['output_types'] : array()
);
$this->base_id = $options['base_id'];
// lets filter the options before we do anything
$options = apply_filters( "wp_super_duper_options", $options );
$options = apply_filters( "wp_super_duper_options_{$this->base_id}", $options );
$options = $this->add_name_from_key( $options );
$this->options = $options;
$this->base_id = $options['base_id'];
$this->arguments = isset( $options['arguments'] ) ? $options['arguments'] : array();
// nested blocks can't work as a widget
if(!empty($this->options['nested-block'])){
if(empty($this->options['output_types'])){
$this->options['output_types'] = array('shortcode','block');
}elseif (($key = array_search('widget', $this->options['output_types'])) !== false) {
unset($this->options['output_types'][$key]);
}
}
// init parent
if(empty($this->options['output_types']) || in_array('widget',$this->options['output_types'])){
parent::__construct( $options['base_id'], $options['name'], $options['widget_ops'] );
}
if ( isset( $options['class_name'] ) ) {
// register widget
$this->class_name = $options['class_name'];
// register shortcode, this needs to be done even for blocks and widgets
$this->register_shortcode();
// Fusion Builder (avada) support
if ( function_exists( 'fusion_builder_map' ) ) {
add_action( 'init', array( $this, 'register_fusion_element' ) );
}
// maybe load the Bricks transformer class
if( class_exists('\Bricks\Elements', false) ){
add_action( 'init', array( $this, 'load_bricks_element_class' ) );
}
// register block
if(empty($this->options['output_types']) || in_array('block',$this->options['output_types'])){
add_action( 'admin_enqueue_scripts', array( $this, 'register_block' ) );
}
}
// add the CSS and JS we need ONCE
global $sd_widget_scripts;
if ( ! $sd_widget_scripts ) {
wp_add_inline_script( 'admin-widgets', $this->widget_js() );
wp_add_inline_script( 'customize-controls', $this->widget_js() );
wp_add_inline_style( 'widgets', $this->widget_css() );
// maybe add elementor editor styles
add_action( 'elementor/editor/after_enqueue_styles', array( $this, 'elementor_editor_styles' ) );
$sd_widget_scripts = true;
// add shortcode insert button once
add_action( 'media_buttons', array( $this, 'wp_media_buttons' ), 1 );
add_action( 'media_buttons', array( $this, 'shortcode_insert_button' ) );
// generatepress theme sections compatibility
if ( function_exists( 'generate_sections_sections_metabox' ) ) {
add_action( 'generate_sections_metabox', array( $this, 'shortcode_insert_button_script' ) );
}
/* Load script on Divi theme builder page */
if ( function_exists( 'et_builder_is_tb_admin_screen' ) && et_builder_is_tb_admin_screen() ) {
add_thickbox();
add_action( 'admin_footer', array( $this, 'shortcode_insert_button_script' ) );
}
if ( $this->is_preview() ) {
add_action( 'wp_footer', array( $this, 'shortcode_insert_button_script' ) );
// this makes the insert button work for elementor
add_action( 'elementor/editor/after_enqueue_scripts', array(
$this,
'shortcode_insert_button_script'
) ); // for elementor
}
// this makes the insert button work for cornerstone
add_action( 'wp_print_footer_scripts', array( __CLASS__, 'maybe_cornerstone_builder' ) );
add_action( 'wp_ajax_super_duper_get_widget_settings', array( __CLASS__, 'get_widget_settings' ) );
add_action( 'wp_ajax_super_duper_get_picker', array( __CLASS__, 'get_picker' ) );
// add generator text to head
add_action( 'admin_head', array( $this, 'generator' ), 99 );
add_action( 'wp_head', array( $this, 'generator' ), 99 );
}
do_action( 'wp_super_duper_widget_init', $options, $this );
}
/**
* Load the Bricks conversion class if we are running Bricks.
* @return void
*/
public function load_bricks_element_class() {
include_once __DIR__ . '/includes/class-super-duper-bricks-element.php';
}
/**
* The register widget function
* @return void
*/
public function _register() {
if(empty($this->options['output_types']) || in_array('widget',$this->options['output_types'])){
parent::_register();
}
}
/**
* Add our widget CSS to elementor editor.
*/
public function elementor_editor_styles() {
wp_add_inline_style( 'elementor-editor', $this->widget_css( false ) );
}
public function register_fusion_element() {
$options = $this->options;
if ( $this->base_id ) {
$params = $this->get_fusion_params();
$args = array(
'name' => $options['name'],
'shortcode' => $this->base_id,
'icon' => $options['block-icon'] ? $options['block-icon'] : 'far fa-square',
'allow_generator' => true,
);
if ( ! empty( $params ) ) {
$args['params'] = $params;
}
fusion_builder_map( $args );
}
}
public function get_fusion_params() {
$params = array();
$arguments = $this->get_arguments();
if ( ! empty( $arguments ) ) {
foreach ( $arguments as $key => $val ) {
$param = array();
// type
$param['type'] = str_replace(
array(
"text",
"number",
"email",
"color",
"checkbox"
),
array(
"textfield",
"textfield",
"textfield",
"colorpicker",
"select",
),
$val['type'] );
// multiselect
if ( $val['type'] == 'multiselect' || ( ( $param['type'] == 'select' || $val['type'] == 'select' ) && ! empty( $val['multiple'] ) ) ) {
$param['type'] = 'multiple_select';
$param['multiple'] = true;
}
// heading
$param['heading'] = isset( $val['title'] ) ? $val['title'] : '';
// description
$param['description'] = isset( $val['desc'] ) ? $val['desc'] : '';
// param_name
$param['param_name'] = $key;
// Default
$param['default'] = isset( $val['default'] ) ? $val['default'] : '';
// Group
if ( isset( $val['group'] ) ) {
$param['group'] = $val['group'];
}
// value
if ( $val['type'] == 'checkbox' ) {
if ( isset( $val['default'] ) && $val['default'] == '0' ) {
unset( $param['default'] );
}
$param['value'] = array( '0' => __( "No", 'ayecode-connect' ), '1' => __( "Yes", 'ayecode-connect' ) );
} elseif ( $param['type'] == 'select' || $param['type'] == 'multiple_select' ) {
$param['value'] = isset( $val['options'] ) ? $val['options'] : array();
} else {
$param['value'] = isset( $val['default'] ) ? $val['default'] : '';
}
// setup the param
$params[] = $param;
}
}
return $params;
}
/**
* Maybe insert the shortcode inserter button in the footer if we are in the cornerstone builder
*/
public static function maybe_cornerstone_builder() {
if ( did_action( 'cornerstone_before_boot_app' ) ) {
self::shortcode_insert_button_script();
}
}
/**
* A function to ge the shortcode builder picker html.
*
* @param string $editor_id
*
* @return string
*/
public static function get_picker( $editor_id = '' ) {
ob_start();
if ( isset( $_POST['editor_id'] ) ) {
$editor_id = esc_attr( $_POST['editor_id'] );
} elseif ( isset( $_REQUEST['et_fb'] ) ) {
$editor_id = 'main_content_content_vb_tiny_mce';
}
global $sd_widgets;
// print_r($sd_widgets);exit;
?>
<div class="sd-shortcode-left-wrap">
<?php
ksort( $sd_widgets );
// print_r($sd_widgets);exit;
if ( ! empty( $sd_widgets ) ) {
echo '<select class="widefat" onchange="sd_get_shortcode_options(this);">';
echo "<option>" . __( 'Select shortcode', 'ayecode-connect' ) . "</option>";
foreach ( $sd_widgets as $shortcode => $class ) {
if(!empty($class['output_types']) && !in_array('shortcode', $class['output_types'])){ continue; }
echo "<option value='" . esc_attr( $shortcode ) . "'>" . esc_attr( $shortcode ) . " (" . esc_attr( $class['name'] ) . ")</option>";
}
echo "</select>";
}
?>
<div class="sd-shortcode-settings"></div>
</div>
<div class="sd-shortcode-right-wrap">
<textarea id='sd-shortcode-output' disabled></textarea>
<div id='sd-shortcode-output-actions'>
<?php if ( $editor_id != '' ) { ?>
<button class="button sd-insert-shortcode-button"
onclick="sd_insert_shortcode(<?php if ( ! empty( $editor_id ) ) {
echo "'" . $editor_id . "'";
} ?>)"><?php _e( 'Insert shortcode', 'ayecode-connect' ); ?></button>
<?php } ?>
<button class="button"
onclick="sd_copy_to_clipboard()"><?php _e( 'Copy shortcode' ); ?></button>
</div>
</div>
<?php
$html = ob_get_clean();
if ( wp_doing_ajax() ) {
echo $html;
$should_die = true;
// some builder get the editor via ajax so we should not die on those occasions
$dont_die = array(
'parent_tag',// WP Bakery
'avia_request' // enfold
);
foreach ( $dont_die as $request ) {
if ( isset( $_REQUEST[ $request ] ) ) {
$should_die = false;
}
}
if ( $should_die ) {
wp_die();
}
} else {
return $html;
}
return '';
}
/**
* Output the version in the header.
*/
public function generator() {
$file = str_replace( array( "/", "\\" ), "/", realpath( __FILE__ ) );
$plugins_dir = str_replace( array( "/", "\\" ), "/", realpath( WP_PLUGIN_DIR ) );
// Find source plugin/theme of SD
$source = array();
if ( strpos( $file, $plugins_dir ) !== false ) {
$source = explode( "/", plugin_basename( $file ) );
} else if ( function_exists( 'get_theme_root' ) ) {
$themes_dir = str_replace( array( "/", "\\" ), "/", realpath( get_theme_root() ) );
if ( strpos( $file, $themes_dir ) !== false ) {
$source = explode( "/", ltrim( str_replace( $themes_dir, "", $file ), "/" ) );
}
}
echo '<meta name="generator" content="WP Super Duper v' . esc_attr( $this->version ) . '"' . ( ! empty( $source[0] ) ? ' data-sd-source="' . esc_attr( $source[0] ) . '"' : '' ) . ' />';
}
/**
* Get widget settings.
*
* @since 1.0.0
*/
public static function get_widget_settings() {
global $sd_widgets;
$shortcode = isset( $_REQUEST['shortcode'] ) && $_REQUEST['shortcode'] ? sanitize_title_with_dashes( $_REQUEST['shortcode'] ) : '';
if ( ! $shortcode ) {
wp_die();
}
$widget_args = isset( $sd_widgets[ $shortcode ] ) ? $sd_widgets[ $shortcode ] : '';
if ( ! $widget_args ) {
wp_die();
}
$class_name = isset( $widget_args['class_name'] ) && $widget_args['class_name'] ? $widget_args['class_name'] : '';
if ( ! $class_name ) {
wp_die();
}
// invoke an instance method
$widget = new $class_name;
ob_start();
$widget->form( array() );
$form = ob_get_clean();
echo "<form id='$shortcode'>" . $form . "<div class=\"widget-control-save\"></div></form>";
echo "<style>" . $widget->widget_css() . "</style>";
echo "<script>" . $widget->widget_js() . "</script>";
?>
<?php
wp_die();
}
/**
* Insert shortcode builder button to classic editor (not inside Gutenberg, not needed).
*
* @param string $editor_id Optional. Shortcode editor id. Default null.
* @param string $insert_shortcode_function Optional. Insert shortcode function. Default null.
*
*@since 1.0.0
*
*/
public static function shortcode_insert_button( $editor_id = '', $insert_shortcode_function = '' ) {
global $sd_widgets, $shortcode_insert_button_once;
if ( $shortcode_insert_button_once ) {
return;
}
add_thickbox();
/**
* Cornerstone makes us play dirty tricks :/
* All media_buttons are removed via JS unless they are two specific id's so we wrap our content in this ID so it is not removed.
*/
if ( function_exists( 'cornerstone_plugin_init' ) && ! is_admin() ) {
echo '<span id="insert-media-button">';
}
echo self::shortcode_button( 'this', 'true' );
// see opening note
if ( function_exists( 'cornerstone_plugin_init' ) && ! is_admin() ) {
echo '</span>'; // end #insert-media-button
}
// Add separate script for generatepress theme sections
if ( function_exists( 'generate_sections_sections_metabox' ) && did_action( 'generate_sections_metabox' ) ) {
} else {
self::shortcode_insert_button_script( $editor_id, $insert_shortcode_function );
}
$shortcode_insert_button_once = true;
}
/**
* Gets the shortcode insert button html.
*
* @param string $id
* @param string $search_for_id
*
* @return mixed
*/
public static function shortcode_button( $id = '', $search_for_id = '' ) {
ob_start();
?>
<span class="sd-lable-shortcode-inserter">
<a onclick="sd_ajax_get_picker(<?php echo $id;
if ( $search_for_id ) {
echo "," . $search_for_id;
} ?>);" href="#TB_inline?width=100%&height=550&inlineId=super-duper-content-ajaxed"
class="thickbox button super-duper-content-open" title="Add Shortcode">
<span style="vertical-align: middle;line-height: 18px;font-size: 20px;"
class="dashicons dashicons-screenoptions"></span>
</a>
<div id="super-duper-content-ajaxed" style="display:none;">
<span>Loading</span>
</div>
</span>
<?php
$html = ob_get_clean();
// remove line breaks so we can use it in js
return preg_replace( "/\r|\n/", "", trim( $html ) );
}
/**
* Makes SD work with the siteOrigin page builder.
*
* @return mixed
*@since 1.0.6
*/
public static function siteorigin_js() {
ob_start();
?>
<script>
/**
* Check a form to see what items should be shown or hidden.
*/
function sd_so_show_hide(form) {
jQuery(form).find(".sd-argument").each(function () {
var $element_require = jQuery(this).data('element_require');
if ($element_require) {
$element_require = $element_require.replace("'", "'"); // replace single quotes
$element_require = $element_require.replace(""", '"'); // replace double quotes
if (eval($element_require)) {
jQuery(this).removeClass('sd-require-hide');
} else {
jQuery(this).addClass('sd-require-hide');
}
}
});
}
/**
* Toggle advanced settings visibility.
*/
function sd_so_toggle_advanced($this) {
var form = jQuery($this).parents('form,.form,.so-content');
form.find('.sd-advanced-setting').toggleClass('sd-adv-show');
return false;// prevent form submit
}
/**
* Initialise a individual widget.
*/
function sd_so_init_widget($this, $selector) {
if (!$selector) {
$selector = 'form';
}
// only run once.
if (jQuery($this).data('sd-widget-enabled')) {
return;
} else {
jQuery($this).data('sd-widget-enabled', true);
}
var $button = '<button title="<?php _e( 'Advanced Settings', 'ayecode-connect' );?>" class="button button-primary right sd-advanced-button" onclick="sd_so_toggle_advanced(this);return false;"><i class="fas fa-sliders-h" aria-hidden="true"></i></button>';
var form = jQuery($this).parents('' + $selector + '');
if (jQuery($this).val() == '1' && jQuery(form).find('.sd-advanced-button').length == 0) {
jQuery(form).append($button);
}
// show hide on form change
jQuery(form).on("change", function () {
sd_so_show_hide(form);
});
// show hide on load
sd_so_show_hide(form);
}
jQuery(function () {
jQuery(document).on('open_dialog', function (w, e) {
setTimeout(function () {
if (jQuery('.so-panels-dialog-wrapper:visible .so-content.panel-dialog .sd-show-advanced').length) {
if (jQuery('.so-panels-dialog-wrapper:visible .so-content.panel-dialog .sd-show-advanced').val() == '1') {
sd_so_init_widget('.so-panels-dialog-wrapper:visible .so-content.panel-dialog .sd-show-advanced', 'div');
}
}
}, 200);
});
});
</script>
<?php
$output = ob_get_clean();
/*
* We only add the <script> tags for code highlighting, so we strip them from the output.
*/
return str_replace( array(
'<script>',
'</script>'
), '', $output );
}
/**
* Output the JS and CSS for the shortcode insert button.
*
* @param string $editor_id
* @param string $insert_shortcode_function
*
*@since 1.0.6
*
*/
public static function shortcode_insert_button_script( $editor_id = '', $insert_shortcode_function = '' ) {
?>
<style>
.sd-shortcode-left-wrap {
float: left;
width: 60%;
}
.sd-shortcode-left-wrap .gd-help-tip {
float: none;
}
.sd-shortcode-left-wrap .widefat {
border-spacing: 0;
width: 100%;
clear: both;
margin: 0;
border: 1px solid #ddd;
box-shadow: inset 0 1px 2px rgba(0, 0, 0, .07);
background-color: #fff;
color: #32373c;
outline: 0;
transition: 50ms border-color ease-in-out;
padding: 3px 5px;
}
.sd-shortcode-left-wrap input[type=checkbox].widefat {
border: 1px solid #b4b9be;
background: #fff;
color: #555;
clear: none;
cursor: pointer;
display: inline-block;
line-height: 0;
height: 16px;
margin: -4px 4px 0 0;
margin-top: 0;
outline: 0;
padding: 0 !important;
text-align: center;
vertical-align: middle;
width: 16px;
min-width: 16px;
-webkit-appearance: none;
box-shadow: inset 0 1px 2px rgba(0, 0, 0, .1);
transition: .05s border-color ease-in-out;
}
.sd-shortcode-left-wrap input[type=checkbox]:checked:before {
content: "\f147";
margin: -3px 0 0 -4px;
color: #1e8cbe;
float: left;
display: inline-block;
vertical-align: middle;
width: 16px;
font: normal 21px/1 dashicons;
speak: none;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
#sd-shortcode-output-actions button,
.sd-advanced-button {
color: #555;
border-color: #ccc;
background: #f7f7f7;
box-shadow: 0 1px 0 #ccc;
vertical-align: top;
display: inline-block;
text-decoration: none;
font-size: 13px;
line-height: 26px;
height: 28px;
margin: 0;
padding: 0 10px 1px;
cursor: pointer;
border-width: 1px;
border-style: solid;
-webkit-appearance: none;
border-radius: 3px;
white-space: nowrap;
box-sizing: border-box;
}
button.sd-advanced-button {
background: #0073aa;
border-color: #006799;
box-shadow: inset 0 2px 0 #006799;
vertical-align: top;
color: #fff;
text-decoration: none;
text-shadow: 0 -1px 1px #006799, 1px 0 1px #006799, 0 1px 1px #006799, -1px 0 1px #006799;
float: right;
margin-right: 3px !important;
font-size: 20px !important;
}
.sd-shortcode-right-wrap {
float: right;
width: 35%;
}
#sd-shortcode-output {
background: rgba(255, 255, 255, .5);
border-color: rgba(222, 222, 222, .75);
box-shadow: inset 0 1px 2px rgba(0, 0, 0, .04);
color: rgba(51, 51, 51, .5);
overflow: auto;
padding: 2px 6px;
line-height: 1.4;
resize: vertical;
}
#sd-shortcode-output {
height: 250px;
width: 100%;
}
<?php if ( function_exists( 'generate_sections_sections_metabox' ) ) { ?>
.generate-sections-modal #custom-media-buttons > .sd-lable-shortcode-inserter {
display: inline;
}
<?php } ?>
<?php if ( function_exists( 'et_builder_is_tb_admin_screen' ) && et_builder_is_tb_admin_screen() ) { ?>
body.divi_page_et_theme_builder div#TB_window.gd-tb-window{z-index:9999999}
<?php } ?>
</style>
<?php
if ( class_exists( 'SiteOrigin_Panels' ) ) {
echo "<script>" . self::siteorigin_js() . "</script>";
}
?>
<script>
<?php
if(! empty( $insert_shortcode_function )){
echo $insert_shortcode_function;
}else{
/**
* Function for super duper insert shortcode.
*
* @since 1.0.0
*/
?>
function sd_insert_shortcode($editor_id) {
$shortcode = jQuery('#TB_ajaxContent #sd-shortcode-output').val();
if ($shortcode) {
if (!$editor_id) {
<?php
if ( isset( $_REQUEST['et_fb'] ) ) {
echo '$editor_id = "#main_content_content_vb_tiny_mce";';
} elseif ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) {
echo '$editor_id = "#elementor-controls .wp-editor-container textarea";';
} else {
echo '$editor_id = "#wp-content-editor-container textarea";';
}
?>
} else {
$editor_id = '#' + $editor_id;
}
tmceActive = jQuery($editor_id).attr("aria-hidden") == "true" ? true : false;
/* GeneratePress */
if (jQuery('#generate-sections-modal-dialog ' + $editor_id).length) {
$editor_id = '#generate-sections-modal-dialog ' + $editor_id;
tmceActive = jQuery($editor_id).closest('.wp-editor-wrap').hasClass('tmce-active') ? true : false;
}
if (typeof tinyMCE !== 'undefined' && tinyMCE.activeEditor && tmceActive) {
tinyMCE.execCommand('mceInsertContent', false, $shortcode);
} else {
var $txt = jQuery($editor_id);
var caretPos = $txt[0].selectionStart;
var textAreaTxt = $txt.val();
var txtToAdd = $shortcode;
var textareaValue = textAreaTxt.substring(0, caretPos) + txtToAdd + textAreaTxt.substring(caretPos);
$txt.focus().val(textareaValue).change().keydown().blur().keyup().keypress().trigger('input').trigger('change');
// set Divi react input value
var input = document.getElementById("main_content_content_vb_tiny_mce");
if (input) {
sd_setNativeValue(input, textareaValue);
}
}
tb_remove();
}
}
/*
Set the value of elements controlled via react.
*/
function sd_setNativeValue(element, value) {
let lastValue = element.value;
element.value = value;
let event = new Event("input", {target: element, bubbles: true});
// React 15
event.simulated = true;
// React 16
let tracker = element._valueTracker;
if (tracker) {
tracker.setValue(lastValue);
}
element.dispatchEvent(event);
}
<?php }?>
/*
Copies the shortcode to the clipboard.
*/
function sd_copy_to_clipboard() {
/* Get the text field */
var copyText = document.querySelector("#TB_ajaxContent #sd-shortcode-output");
//un-disable the field
copyText.disabled = false;
/* Select the text field */
copyText.select();
/* Copy the text inside the text field */
document.execCommand("Copy");
//re-disable the field
copyText.disabled = true;
/* Alert the copied text */
alert("Copied the text: " + copyText.value);
}
/*
Gets the shortcode options.
*/
function sd_get_shortcode_options($this) {
$short_code = jQuery($this).val();
if ($short_code) {
var data = {
'action': 'super_duper_get_widget_settings',
'shortcode': $short_code,
'attributes': 123,
'post_id': 321,
'_ajax_nonce': '<?php echo wp_create_nonce( 'super_duper_output_shortcode' );?>'
};
if (typeof ajaxurl === 'undefined') {
var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' );?>";
}
jQuery.post(ajaxurl, data, function (response) {
jQuery('#TB_ajaxContent .sd-shortcode-settings').html(response);
jQuery('#' + $short_code).on('change', 'select', function () {
sd_build_shortcode($short_code);
}); // take care of select tags
jQuery('#' + $short_code).on('change keypress keyup', 'input,textarea', function () {
sd_build_shortcode($short_code);
});
sd_build_shortcode($short_code);
// resize the window to fit
setTimeout(function () {
jQuery('#TB_ajaxContent').css('width', 'auto').css('height', '75vh');
}, 200);
return response;
});
}
}
/*
Builds and inserts the shortcode into the viewer.
*/
function sd_build_shortcode($id) {
var multiSelects = {};
var multiSelectsRemove = [];
$output = "[" + $id;
$form_data = jQuery("#" + $id).serializeArray();
// run checks for multiselects
jQuery.each($form_data, function (index, element) {
if (element && element.value) {
$field_name = element.name.substr(element.name.indexOf("][") + 2);
$field_name = $field_name.replace("]", "");
// check if its a multiple
if ($field_name.includes("[]")) {
multiSelectsRemove[multiSelectsRemove.length] = index;
$field_name = $field_name.replace("[]", "");
if ($field_name in multiSelects) {
multiSelects[$field_name] = multiSelects[$field_name] + "," + element.value;
} else {
multiSelects[$field_name] = element.value;
}
}
}
});
// fix multiselects if any are found
if (multiSelectsRemove.length) {
// remove all multiselects
multiSelectsRemove.reverse();
multiSelectsRemove.forEach(function (index) {
$form_data.splice(index, 1);
});
$ms_arr = [];
// add multiselets back
jQuery.each(multiSelects, function (index, value) {
$ms_arr[$ms_arr.length] = {"name": "[][" + index + "]", "value": value};
});
$form_data = $form_data.concat($ms_arr);
}
if ($form_data) {
$content = '';
$form_data.forEach(function (element) {
if (element.value) {
$field_name = element.name.substr(element.name.indexOf("][") + 2);
$field_name = $field_name.replace("]", "");
if ($field_name == 'html') {
$content = element.value;
} else {
$output = $output + " " + $field_name + '="' + element.value + '"';
}
}
});
}
$output = $output + "]";
// check for content field
if ($content) {
$output = $output + $content + "[/" + $id + "]";
}
jQuery('#TB_ajaxContent #sd-shortcode-output').html($output);
}
/*
Delay the init of the textareas for 1 second.
*/
(function () {
setTimeout(function () {
sd_init_textareas();
}, 1000);
})();
/*
Init the textareas to be able to show the shortcode builder button.
*/
function sd_init_textareas() {
// General textareas
jQuery(document).on('focus', 'textarea', function () {
if (jQuery(this).hasClass('wp-editor-area')) {
// insert the shortcode button to the textarea lable if not there already
if (!(jQuery(this).parent().find('.sd-lable-shortcode-inserter').length || (jQuery(this).closest('.wp-editor-wrap').length && jQuery(this).closest('.wp-editor-wrap').find('.sd-lable-shortcode-inserter').length))) {
jQuery(this).parent().find('.quicktags-toolbar').append(sd_shortcode_button(jQuery(this).attr('id')));
}
} else {
// insert the shortcode button to the textarea lable if not there already
if (!jQuery("label[for='" + jQuery(this).attr('id') + "']").find('.sd-lable-shortcode-inserter').length) {
jQuery("label[for='" + jQuery(this).attr('id') + "']").append(sd_shortcode_button(jQuery(this).attr('id')));
}
}
});
// The below tries to add the shortcode builder button to the builders own raw/shortcode sections.
// DIVI
jQuery(document).on('focusin', '.et-fb-codemirror', function () {
// insert the shortcode button to the textarea lable if not there already
if (!jQuery(this).closest('.et-fb-form__group').find('.sd-lable-shortcode-inserter').length) {
jQuery(this).closest('.et-fb-form__group').find('.et-fb-form__label-text').append(sd_shortcode_button());
}
});
// Beaver
jQuery(document).on('focusin', '.fl-code-field', function () {
// insert the shortcode button to the textarea lable if not there already
if (!jQuery(this).closest('.fl-field-control-wrapper').find('.sd-lable-shortcode-inserter').length) {
jQuery(this).closest('.fl-field-control-wrapper').prepend(sd_shortcode_button());
}
});
// Fusion builder (avada)
jQuery(document).on('focusin', '.CodeMirror.cm-s-default', function () {
// insert the shortcode button to the textarea lable if not there already
if (!jQuery(this).parent().find('.sd-lable-shortcode-inserter').length) {
jQuery(sd_shortcode_button()).insertBefore(this);
}
});
// Avia builder (enfold)
jQuery(document).on('focusin', '#aviaTBcontent', function () {
// insert the shortcode button to the textarea lable if not there already
if (!jQuery(this).parent().parent().find('.avia-name-description ').find('.sd-lable-shortcode-inserter').length) {
jQuery(this).parent().parent().find('.avia-name-description strong').append(sd_shortcode_button(jQuery(this).attr('id')));
}
});
// Cornerstone textareas
jQuery(document).on('focusin', '.cs-control.cs-control-textarea', function () {
// insert the shortcode button to the textarea lable if not there already
if (!jQuery(this).find('.cs-control-header label').find('.sd-lable-shortcode-inserter').length) {
jQuery(this).find('.cs-control-header label').append(sd_shortcode_button());
}
});
// Cornerstone main bar