Passed
Push — master ( 877d43...e9e8ae )
by Brian
06:03
created

WP_Super_Duper_Shortcode::get_fusion_params()   C

Complexity

Conditions 17
Paths 2

Size

Total Lines 67
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 38
nc 2
nop 0
dl 0
loc 67
rs 5.2166
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Contains the shortcode class.
4
 *
5
 * @since 2.0.0
6
 */
7
8
defined( 'ABSPATH' ) || exit;
9
10
/**
11
 *
12
 * The shortcode super duper class.
13
 *
14
 *
15
 * @since 2.0.0
16
 * @version 2.0.0
17
 */
18
class WP_Super_Duper_Shortcode {
19
20
	/**
21
	 * @var WP_Super_Duper
22
	 */
23
	protected $sd;
24
25
	/**
26
	 * Class constructor.
27
	 *
28
	 * @param WP_Super_Duper $super_duper
29
	 */
30
	public function __construct( $super_duper ) {
31
32
		$this->sd = $super_duper;
33
34
		// Registers the shortcode.
35
		add_shortcode( $this->sd->base_id, array( $this, 'shortcode_output' ) );
36
37
		// This makes the insert button work for cornerstone.
38
		add_action( 'wp_print_footer_scripts', array( $this, 'maybe_cornerstone_builder' ) );
39
40
		// Fusion Builder (avada) support
41
		if ( function_exists( 'fusion_builder_map' ) ) {
42
			add_action( 'init', array( $this, 'register_fusion_element' ) );
43
		}
44
45
		// Add shortcode insert button once
46
		add_action( 'media_buttons', array( $this, 'shortcode_insert_button' ) );
47
		add_action( 'wp_ajax_super_duper_get_widget_settings', array( $this, 'get_widget_settings' ) );
48
49
		// generatepress theme sections compatibility
50
		if ( function_exists( 'generate_sections_sections_metabox' ) ) {
51
			add_action( 'generate_sections_metabox', array( $this, 'shortcode_insert_button_script' ) );
52
		}
53
54
		if ( $this->sd->is_preview() ) {
55
			add_action( 'wp_footer', array( $this, 'shortcode_insert_button_script' ) );
56
			// this makes the insert button work for elementor
57
			add_action( 'elementor/editor/after_enqueue_scripts', array(
58
				$this,
59
				'shortcode_insert_button_script'
60
			) ); // for elementor
61
		}
62
63
	}
64
65
	/**
66
	 * Insert shortcode builder button to classic editor (not inside Gutenberg, not needed).
67
	 *
68
	 * @since 1.0.0
69
	 *
70
	 * @param string $editor_id Optional. Shortcode editor id. Default null.
71
	 * @param string $insert_shortcode_function Optional. Insert shortcode function. Default null.
72
	 */
73
	public static function shortcode_insert_button( $editor_id = '', $insert_shortcode_function = '' ) {
74
		global $shortcode_insert_button_once;
75
		if ( $shortcode_insert_button_once ) {
76
			return;
77
		}
78
		add_thickbox();
79
80
		/**
81
		 * Cornerstone makes us play dirty tricks :/
82
		 * 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.
83
		 */
84
		if ( function_exists( 'cornerstone_plugin_init' ) && ! is_admin() ) {
85
			echo '<span id="insert-media-button">';
86
		}
87
88
		echo self::shortcode_button( 'this', 'true' );
89
90
		// see opening note
91
		if ( function_exists( 'cornerstone_plugin_init' ) && ! is_admin() ) {
92
			echo '</span>'; // end #insert-media-button
93
		}
94
95
		// Add separate script for generatepress theme sections
96
		if ( function_exists( 'generate_sections_sections_metabox' ) && did_action( 'generate_sections_metabox' ) ) {
97
		} else {
98
			self::shortcode_insert_button_script( $editor_id, $insert_shortcode_function );
99
		}
100
101
		$shortcode_insert_button_once = true;
102
	}
103
104
	/**
105
	 * Output the shortcode.
106
	 *
107
	 * @param array $args
108
	 * @param string $content
109
	 *
110
	 * @return string
111
	 */
112
	public function shortcode_output( $args = array(), $content = '' ) {
113
		$args = $this->sd->argument_values( $args );
114
115
		// Clean booleans.
116
		$args = $this->sd->string_to_bool( $args );
117
118
		// if we have a enclosed shortcode we add it to the special `html` argument
119
		if ( ! empty( $content ) ) {
120
			$args['html'] = $content;
121
		}
122
123
		$class = isset( $this->sd->options['widget_ops']['classname'] ) ? esc_attr( $this->sd->options['widget_ops']['classname'] ) : '';
124
		$class .= " sdel-" . $this->sd->get_instance_hash();
125
126
		$class = apply_filters( 'wp_super_duper_div_classname', $class, $args, $this->sd, $this );
127
		$class = apply_filters( 'wp_super_duper_div_classname_' . $this->sd->base_id, $class, $args, $this->sd, $this );
128
129
		$attrs = apply_filters( 'wp_super_duper_div_attrs', '', $args, $this->sd, $this );
130
		$attrs = apply_filters( 'wp_super_duper_div_attrs_' . $this->sd->base_id, $attrs, $args, $this->sd, $this );
131
132
		$shortcode_args = array();
133
		$output         = '';
134
		$no_wrap        = ! empty( $this->sd->options['no_wrap'] ) || ! empty( $args['no_wrap'] );
135
136
		$main_content = $this->sd->output( $args, $shortcode_args, $content );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $main_content is correct as $this->sd->output($args,...ortcode_args, $content) targeting WP_Super_Duper::output() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
137
138
		if ( $main_content && ! $no_wrap ) {
0 ignored issues
show
introduced by
$main_content is defined implicitly as null, thus it is always evaluated to false.
Loading history...
139
			// wrap the shortcode in a div with the same class as the widget
140
			$output .= '<div class="' . esc_attr( $class ) . '" ' . $attrs . '>';
141
			if ( ! empty( $args['title'] ) ) {
142
				// if its a shortcode and there is a title try to grab the title wrappers
143
				$shortcode_args = array( 'before_title' => '', 'after_title' => '' );
144
				if ( empty( $instance ) ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $instance seems to never exist and therefore empty should always be true.
Loading history...
145
					global $wp_registered_sidebars;
146
					if ( ! empty( $wp_registered_sidebars ) ) {
147
						foreach ( $wp_registered_sidebars as $sidebar ) {
148
							if ( ! empty( $sidebar['before_title'] ) ) {
149
								$shortcode_args['before_title'] = $sidebar['before_title'];
150
								$shortcode_args['after_title']  = $sidebar['after_title'];
151
								break;
152
							}
153
						}
154
					}
155
				}
156
				$output .= $this->sd->output_title( $shortcode_args, $args );
157
			}
158
			$output .= $main_content;
159
			$output .= '</div>';
160
		} elseif ( $main_content && $no_wrap ) {
0 ignored issues
show
introduced by
$main_content is defined implicitly as null, thus it is always evaluated to false.
Loading history...
161
			$output .= $main_content;
162
		}
163
164
		// if preview, show a placeholder if empty
165
		if ( $this->sd->is_preview() && $output == '' ) {
166
			$output = $this->sd->preview_placeholder_text( "{{" . $this->sd->base_id . "}}" );
167
		}
168
169
		return apply_filters( 'wp_super_duper_widget_output', $output, $args, $shortcode_args, $this );
170
	}
171
172
	public function register_fusion_element() {
173
		$options = $this->sd->options;
174
175
		if ( $this->sd->base_id ) {
176
177
			$params = $this->get_fusion_params();
178
179
			$args = array(
180
				'name'            => $options['name'],
181
				'shortcode'       => $this->sd->base_id,
182
				'icon'            => $options['block-icon'] ? $options['block-icon'] : 'far fa-square',
183
				'allow_generator' => true,
184
			);
185
186
			if ( ! empty( $params ) ) {
187
				$args['params'] = $params;
188
			}
189
190
			fusion_builder_map( $args );
0 ignored issues
show
Bug introduced by
The function fusion_builder_map was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

190
			/** @scrutinizer ignore-call */ 
191
   fusion_builder_map( $args );
Loading history...
191
		}
192
	}
193
194
	protected function get_fusion_params() {
195
		$params    = array();
196
		$arguments = $this->sd->get_arguments();
197
198
		if ( ! empty( $arguments ) ) {
199
			foreach ( $arguments as $key => $val ) {
200
				$param = array();
201
				// type
202
				$param['type'] = str_replace(
203
					array(
204
						"text",
205
						"number",
206
						"email",
207
						"color",
208
						"checkbox"
209
					),
210
					array(
211
						"textfield",
212
						"textfield",
213
						"textfield",
214
						"colorpicker",
215
						"select",
216
					),
217
					$val['type'] );
218
219
				// multiselect
220
				if ( $val['type'] == 'multiselect' || ( ( $param['type'] == 'select' || $val['type'] == 'select' ) && ! empty( $val['multiple'] ) ) ) {
221
					$param['type']     = 'multiple_select';
222
					$param['multiple'] = true;
223
				}
224
225
				// heading
226
				$param['heading'] = $val['title'];
227
228
				// description
229
				$param['description'] = isset( $val['desc'] ) ? $val['desc'] : '';
230
231
				// param_name
232
				$param['param_name'] = $key;
233
234
				// Default
235
				$param['default'] = isset( $val['default'] ) ? $val['default'] : '';
236
237
				// Group
238
				if ( isset( $val['group'] ) ) {
239
					$param['group'] = $val['group'];
240
				}
241
242
				// value
243
				if ( $val['type'] == 'checkbox' ) {
244
					if ( isset( $val['default'] ) && $val['default'] == '0' ) {
245
						unset( $param['default'] );
246
					}
247
					$param['value'] = array( '' => __( "No" ), '1' => __( "Yes" ) );
248
				} elseif ( $param['type'] == 'select' || $param['type'] == 'multiple_select' ) {
249
					$param['value'] = isset( $val['options'] ) ? $val['options'] : array();
250
				} else {
251
					$param['value'] = isset( $val['default'] ) ? $val['default'] : '';
252
				}
253
254
				// setup the param
255
				$params[] = $param;
256
257
			}
258
		}
259
260
		return $params;
261
	}
262
263
	/**
264
	 * Maybe insert the shortcode inserter button in the footer if we are in the cornerstone builder
265
	 */
266
	public function maybe_cornerstone_builder() {
267
		if ( did_action( 'cornerstone_before_boot_app' ) ) {
268
			self::shortcode_insert_button_script();
269
		}
270
	}
271
272
	/**
273
	 * Output the JS and CSS for the shortcode insert button.
274
	 *
275
	 * @since 1.0.6
276
	 *
277
	 * @param string $editor_id
278
	 * @param string $insert_shortcode_function
279
	 */
280
	public static function shortcode_insert_button_script( $editor_id = '', $insert_shortcode_function = '' ) {
281
		?>
282
		<style>
283
			.sd-shortcode-left-wrap {
284
				float: left;
285
				width: 60%;
286
			}
287
288
			.sd-shortcode-left-wrap .gd-help-tip {
289
				float: none;
290
			}
291
292
			.sd-shortcode-left-wrap .widefat {
293
				border-spacing: 0;
294
				width: 100%;
295
				clear: both;
296
				margin: 0;
297
				border: 1px solid #ddd;
298
				box-shadow: inset 0 1px 2px rgba(0, 0, 0, .07);
299
				background-color: #fff;
300
				color: #32373c;
301
				outline: 0;
302
				transition: 50ms border-color ease-in-out;
303
				padding: 3px 5px;
304
			}
305
306
			.sd-shortcode-left-wrap input[type=checkbox].widefat {
307
				border: 1px solid #b4b9be;
308
				background: #fff;
309
				color: #555;
310
				clear: none;
311
				cursor: pointer;
312
				display: inline-block;
313
				line-height: 0;
314
				height: 16px;
315
				margin: -4px 4px 0 0;
316
				margin-top: 0;
317
				outline: 0;
318
				padding: 0 !important;
319
				text-align: center;
320
				vertical-align: middle;
321
				width: 16px;
322
				min-width: 16px;
323
				-webkit-appearance: none;
324
				box-shadow: inset 0 1px 2px rgba(0, 0, 0, .1);
325
				transition: .05s border-color ease-in-out;
326
			}
327
328
			.sd-shortcode-left-wrap input[type=checkbox]:checked:before {
329
				content: "\f147";
330
				margin: -3px 0 0 -4px;
331
				color: #1e8cbe;
332
				float: left;
333
				display: inline-block;
334
				vertical-align: middle;
335
				width: 16px;
336
				font: normal 21px/1 dashicons;
337
				speak: none;
338
				-webkit-font-smoothing: antialiased;
339
				-moz-osx-font-smoothing: grayscale;
340
			}
341
342
			#sd-shortcode-output-actions button,
343
			.sd-advanced-button {
344
				color: #555;
345
				border-color: #ccc;
346
				background: #f7f7f7;
347
				box-shadow: 0 1px 0 #ccc;
348
				vertical-align: top;
349
				display: inline-block;
350
				text-decoration: none;
351
				font-size: 13px;
352
				line-height: 26px;
353
				height: 28px;
354
				margin: 0;
355
				padding: 0 10px 1px;
356
				cursor: pointer;
357
				border-width: 1px;
358
				border-style: solid;
359
				-webkit-appearance: none;
360
				border-radius: 3px;
361
				white-space: nowrap;
362
				box-sizing: border-box;
363
			}
364
365
			button.sd-advanced-button {
366
				background: #0073aa;
367
				border-color: #006799;
368
				box-shadow: inset 0 2px 0 #006799;
369
				vertical-align: top;
370
				color: #fff;
371
				text-decoration: none;
372
				text-shadow: 0 -1px 1px #006799, 1px 0 1px #006799, 0 1px 1px #006799, -1px 0 1px #006799;
373
				float: right;
374
				margin-right: 3px !important;
375
				font-size: 20px !important;
376
			}
377
378
			.sd-shortcode-right-wrap {
379
				float: right;
380
				width: 35%;
381
			}
382
383
			#sd-shortcode-output {
384
				background: rgba(255, 255, 255, .5);
385
				border-color: rgba(222, 222, 222, .75);
386
				box-shadow: inset 0 1px 2px rgba(0, 0, 0, .04);
387
				color: rgba(51, 51, 51, .5);
388
				overflow: auto;
389
				padding: 2px 6px;
390
				line-height: 1.4;
391
				resize: vertical;
392
			}
393
394
			#sd-shortcode-output {
395
				height: 250px;
396
				width: 100%;
397
			}
398
399
			<?php if ( function_exists( 'generate_sections_sections_metabox' ) ) { ?>
400
			.generate-sections-modal #custom-media-buttons > .sd-lable-shortcode-inserter {
401
				display: inline;
402
			}
403
404
			<?php } ?>
405
		</style>
406
		<?php
407
		if ( class_exists( 'SiteOrigin_Panels' ) ) {
408
			echo "<script>" . WP_Super_Duper::siteorigin_js() . "</script>";
409
		}
410
		?>
411
		<script>
412
			<?php
413
			if(! empty( $insert_shortcode_function )){
414
				echo $insert_shortcode_function;
415
			} else {
416
417
			/**
418
			 * Function for super duper insert shortcode.
419
			 *
420
			 * @since 1.0.0
421
			 */
422
			?>
423
			function sd_insert_shortcode($editor_id) {
424
				$shortcode = jQuery('#TB_ajaxContent #sd-shortcode-output').val();
425
				if ($shortcode) {
426
					if (!$editor_id) {
427
						<?php
428
						if ( isset( $_REQUEST['et_fb'] ) ) {
429
							echo '$editor_id = "#main_content_content_vb_tiny_mce";';
430
						} elseif ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) {
431
							echo '$editor_id = "#elementor-controls .wp-editor-container textarea";';
432
						} else {
433
							echo '$editor_id = "#wp-content-editor-container textarea";';
434
						}
435
						?>
436
					} else {
437
						$editor_id = '#' + $editor_id;
438
					}
439
					tmceActive = jQuery($editor_id).attr("aria-hidden") == "true" ? true : false;
440
					/* GeneratePress */
441
					if (jQuery('#generate-sections-modal-dialog ' + $editor_id).length) {
442
						$editor_id = '#generate-sections-modal-dialog ' + $editor_id;
443
						tmceActive = jQuery($editor_id).closest('.wp-editor-wrap').hasClass('tmce-active') ? true : false;
444
					}
445
					if (tinyMCE && tinyMCE.activeEditor && tmceActive) {
446
						tinyMCE.execCommand('mceInsertContent', false, $shortcode);
447
					} else {
448
						var $txt = jQuery($editor_id);
449
						var caretPos = $txt[0].selectionStart;
450
						var textAreaTxt = $txt.val();
451
						var txtToAdd = $shortcode;
452
						var textareaValue = textAreaTxt.substring(0, caretPos) + txtToAdd + textAreaTxt.substring(caretPos);
453
						$txt.focus().val(textareaValue).change().keydown().blur().keyup().keypress().trigger('input').trigger('change');
454
						// set Divi react input value
455
						var input = document.getElementById("main_content_content_vb_tiny_mce");
456
						if (input) {
457
							sd_setNativeValue(input, textareaValue);
458
						}
459
					}
460
					tb_remove();
461
				}
462
			}
463
464
			/*
465
			 Set the value of elements controled via react.
466
			 */
467
			function sd_setNativeValue(element, value) {
468
				let lastValue = element.value;
469
				element.value = value;
470
				let event = new Event("input", {target: element, bubbles: true});
471
				// React 15
472
				event.simulated = true;
473
				// React 16
474
				let tracker = element._valueTracker;
475
				if (tracker) {
476
					tracker.setValue(lastValue);
477
				}
478
				element.dispatchEvent(event);
479
			}
480
			<?php }?>
481
482
			/*
483
			 Copies the shortcode to the clipboard.
484
			 */
485
			function sd_copy_to_clipboard() {
486
				/* Get the text field */
487
				var copyText = document.querySelector("#TB_ajaxContent #sd-shortcode-output");
488
				//un-disable the field
489
				copyText.disabled = false;
490
				/* Select the text field */
491
				copyText.select();
492
				/* Copy the text inside the text field */
493
				document.execCommand("Copy");
494
				//re-disable the field
495
				copyText.disabled = true;
496
				/* Alert the copied text */
497
				alert("Copied the text: " + copyText.value);
498
			}
499
500
			/*
501
			 Gets the shortcode options.
502
			 */
503
			function sd_get_shortcode_options($this) {
504
				$short_code = jQuery($this).val();
505
				if ($short_code) {
506
507
					var data = {
508
						'action': 'super_duper_get_widget_settings',
509
						'shortcode': $short_code,
510
						'attributes': 123,
511
						'post_id': 321,
512
						'_ajax_nonce': '<?php echo wp_create_nonce( 'super_duper_output_shortcode' );?>'
513
					};
514
515
					if (typeof ajaxurl === 'undefined') {
516
						var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' );?>";
517
					}
518
519
					jQuery.post(ajaxurl, data, function (response) {
520
						jQuery('#TB_ajaxContent .sd-shortcode-settings').html(response);
521
						jQuery('#' + $short_code).on('change', 'select', function () {
522
							sd_build_shortcode($short_code);
523
						}); // take care of select tags
524
525
						jQuery('#' + $short_code).on('change keypress keyup', 'input,textarea', function () {
526
							sd_build_shortcode($short_code);
527
						});
528
529
						sd_build_shortcode($short_code);
530
						// resize the window to fit
531
						setTimeout(function () {
532
							jQuery('#TB_ajaxContent').css('width', 'auto').css('height', '75vh');
533
						}, 200);
534
535
						return response;
536
					});
537
				}
538
			}
539
540
			/*
541
			 Builds and inserts the shortcode into the viewer.
542
			 */
543
			function sd_build_shortcode($id) {
544
				var multiSelects = {};
545
				var multiSelectsRemove = [];
546
547
				$output = "[" + $id;
548
				$form_data = jQuery("#" + $id).serializeArray();
549
550
				// run checks for multiselects
551
				jQuery.each($form_data, function (index, element) {
552
					if (element && element.value) {
553
						$field_name = element.name.substr(element.name.indexOf("][") + 2);
554
						$field_name = $field_name.replace("]", "");
555
						// check if its a multiple
556
						if ($field_name.includes("[]")) {
557
							multiSelectsRemove[multiSelectsRemove.length] = index;
558
							$field_name = $field_name.replace("[]", "");
559
							if ($field_name in multiSelects) {
560
								multiSelects[$field_name] = multiSelects[$field_name] + "," + element.value;
561
							} else {
562
								multiSelects[$field_name] = element.value;
563
							}
564
						}
565
					}
566
				});
567
568
				// fix multiselects if any are found
569
				if (multiSelectsRemove.length) {
570
					// remove all multiselects
571
					multiSelectsRemove.reverse();
572
					multiSelectsRemove.forEach(function (index) {
573
						$form_data.splice(index, 1);
574
					});
575
					$ms_arr = [];
576
					// add multiselets back
577
					jQuery.each(multiSelects, function (index, value) {
578
						$ms_arr[$ms_arr.length] = {"name": "[][" + index + "]", "value": value};
579
					});
580
					$form_data = $form_data.concat($ms_arr);
581
				}
582
583
				if ($form_data) {
584
					$content = '';
585
					$form_data.forEach(function (element) {
586
						if (element.value) {
587
							$field_name = element.name.substr(element.name.indexOf("][") + 2);
588
							$field_name = $field_name.replace("]", "");
589
							if ($field_name == 'html') {
590
								$content = element.value;
591
							} else {
592
								$output = $output + " " + $field_name + '="' + element.value + '"';
593
							}
594
						}
595
					});
596
				}
597
				$output = $output + "]";
598
599
				// check for content field
600
				if ($content) {
601
					$output = $output + $content + "[/" + $id + "]";
602
				}
603
604
				jQuery('#TB_ajaxContent #sd-shortcode-output').html($output);
605
			}
606
607
			/*
608
			 Delay the init of the textareas for 1 second.
609
			 */
610
			(function () {
611
				setTimeout(function () {
612
					sd_init_textareas();
613
				}, 1000);
614
			})();
615
616
			/*
617
			 Init the textareas to be able to show the shortcode builder button.
618
			 */
619
			function sd_init_textareas() {
620
				// General textareas
621
				jQuery(document).on('focus', 'textarea', function () {
622
					if (jQuery(this).hasClass('wp-editor-area')) {
623
						// insert the shortcode button to the textarea lable if not there already
624
						if (!jQuery(this).parent().find('.sd-lable-shortcode-inserter').length) {
625
							jQuery(this).parent().find('.quicktags-toolbar').append(sd_shortcode_button(jQuery(this).attr('id')));
626
						}
627
					} else {
628
						// insert the shortcode button to the textarea lable if not there already
629
						if (!jQuery("label[for='" + jQuery(this).attr('id') + "']").find('.sd-lable-shortcode-inserter').length) {
630
							jQuery("label[for='" + jQuery(this).attr('id') + "']").append(sd_shortcode_button(jQuery(this).attr('id')));
631
						}
632
					}
633
				});
634
635
				// The below tries to add the shortcode builder button to the builders own raw/shortcode sections.
636
637
				// DIVI
638
				jQuery(document).on('focusin', '.et-fb-codemirror', function () {
639
					// insert the shortcode button to the textarea lable if not there already
640
					if (!jQuery(this).closest('.et-fb-form__group').find('.sd-lable-shortcode-inserter').length) {
641
						jQuery(this).closest('.et-fb-form__group').find('.et-fb-form__label-text').append(sd_shortcode_button());
642
					}
643
				});
644
645
				// Beaver
646
				jQuery(document).on('focusin', '.fl-code-field', function () {
647
					// insert the shortcode button to the textarea lable if not there already
648
					if (!jQuery(this).closest('.fl-field-control-wrapper').find('.sd-lable-shortcode-inserter').length) {
649
						jQuery(this).closest('.fl-field-control-wrapper').prepend(sd_shortcode_button());
650
					}
651
				});
652
653
				// Fushion builder (avada)
654
				jQuery(document).on('focusin', '.CodeMirror.cm-s-default', function () {
655
					// insert the shortcode button to the textarea lable if not there already
656
					if (!jQuery(this).parent().find('.sd-lable-shortcode-inserter').length) {
657
						jQuery(sd_shortcode_button()).insertBefore(this);
658
					}
659
				});
660
661
				// Avia builder (enfold)
662
				jQuery(document).on('focusin', '#aviaTBcontent', function () {
663
					// insert the shortcode button to the textarea lable if not there already
664
					if (!jQuery(this).parent().parent().find('.avia-name-description ').find('.sd-lable-shortcode-inserter').length) {
665
						jQuery(this).parent().parent().find('.avia-name-description strong').append(sd_shortcode_button(jQuery(this).attr('id')));
666
					}
667
				});
668
669
				// Cornerstone textareas
670
				jQuery(document).on('focusin', '.cs-control.cs-control-textarea', function () {
671
					// insert the shortcode button to the textarea lable if not there already
672
					if (!jQuery(this).find('.cs-control-header label').find('.sd-lable-shortcode-inserter').length) {
673
						jQuery(this).find('.cs-control-header label').append(sd_shortcode_button());
674
					}
675
				});
676
677
				// Cornerstone main bar
678
				setTimeout(function () {
679
					// insert the shortcode button to the textarea lable if not there already
680
					if (!jQuery('.cs-bar-btns').find('.sd-lable-shortcode-inserter').length) {
681
						jQuery('<li style="text-align: center;padding: 5px;list-style: none;">' + sd_shortcode_button() + '</li>').insertBefore('.cs-action-toggle-custom-css');
682
					}
683
				}, 2000);
684
685
			}
686
687
			/**
688
			 * Gets the html for the picker via ajax and updates it on the fly.
689
			 *
690
			 * @param $id
691
			 * @param $search
692
			 */
693
			function sd_ajax_get_picker($id, $search) {
694
				if ($search) {
695
					$this = $id;
696
					$id = jQuery($this).closest('.wp-editor-wrap').find('.wp-editor-container textarea').attr('id');
697
				}
698
699
				var data = {
700
					'action': 'super_duper_get_picker',
701
					'editor_id': $id,
702
					'_ajax_nonce': '<?php echo wp_create_nonce( 'super_duper_picker' );?>'
703
				};
704
705
				if (!ajaxurl) {
706
					var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
707
				}
708
709
				jQuery.post(ajaxurl, data, function (response) {
710
					jQuery('#TB_ajaxContent').html(response);
711
					//return response;
712
				}).then(function (env) {
713
					jQuery('body').on('thickbox:removed', function () {
714
						jQuery('#super-duper-content-ajaxed').html('');
715
					});
716
				});
717
			}
718
719
			/**
720
			 * Get the html for the shortcode inserter button depending on if a textarea id is available.
721
			 *
722
			 * @param $id string The textarea id.
723
			 * @returns {string}
724
			 */
725
			function sd_shortcode_button($id) {
726
				if ($id) {
727
					return '<?php echo self::shortcode_button( "\\''+\$id+'\\'" );?>';
728
				} else {
729
					return '<?php echo self::shortcode_button();?>';
730
				}
731
			}
732
		</script>
733
		<?php
734
	}
735
736
	/**
737
	 * Gets the shortcode insert button html.
738
	 *
739
	 * @param string $id
740
	 * @param string $search_for_id
741
	 *
742
	 * @return mixed
743
	 */
744
	public static function shortcode_button( $id = '', $search_for_id = '' ) {
745
		ob_start();
746
		?>
747
		<span class="sd-lable-shortcode-inserter">
748
			<a onclick="sd_ajax_get_picker(<?php echo $id;
749
			if ( $search_for_id ) {
750
				echo "," . $search_for_id;
751
			} ?>);" href="#TB_inline?width=100%&height=550&inlineId=super-duper-content-ajaxed"
752
			   class="thickbox button super-duper-content-open" title="Add Shortcode">
753
				<span style="vertical-align: middle;line-height: 18px;font-size: 20px;"
754
				      class="dashicons dashicons-screenoptions"></span>
755
			</a>
756
			<div id="super-duper-content-ajaxed" style="display:none;">
757
				<span>Loading</span>
758
			</div>
759
		</span>
760
761
		<?php
762
		$html = ob_get_clean();
763
764
		// remove line breaks so we can use it in js
765
		return preg_replace( "/\r|\n/", "", trim( $html ) );
766
	}
767
768
	/**
769
	 * Get widget settings.
770
	 *
771
	 * @since 1.0.0
772
	 */
773
	public function get_widget_settings() {
774
775
		if ( isset( $_REQUEST['shortcode'] ) && $this->sd->base_id == sanitize_title_with_dashes( $_REQUEST['shortcode'] ) ) {
776
777
			$shortcode = sanitize_title_with_dashes( $_REQUEST['shortcode'] );
778
779
			ob_start();
780
			$this->sd->form( array() );
781
			$form = ob_get_clean();
782
783
			echo "<form id='$shortcode'>" . $form . "<div class='widget-control-save'></div></form>";
784
			echo "<style>" . WP_Super_Duper::widget_css() . "</style>";
785
			echo "<script>" . WP_Super_Duper::widget_js() . "</script>";
786
			exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
787
788
		}
789
790
	}
791
792
}
793