Skip to content

Commit

Permalink
Merge pull request #43 from kprajapatii/master
Browse files Browse the repository at this point in the history
Block tab groups mismatch causes JS error - FIXED
  • Loading branch information
kprajapatii authored Mar 21, 2023
2 parents 967d56b + dedd0c1 commit da8143a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 54 deletions.
3 changes: 3 additions & 0 deletions change-log.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
= 1.1.17 =
* Block tab groups mismatch causes JS error - FIXED

= 1.1.16 =
* Better block auto-recovery - ADDED
* Functions added to support BS5 changes and new features - ADDED
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ayecode/wp-super-duper",
"version": "1.1.16",
"version": "1.1.17",
"type": "library",
"description": "Lets you create a widget, block and shortcode all from the one file .",
"keywords": ["WordPress","super duper","wp"],
Expand Down
131 changes: 78 additions & 53 deletions wp-super-duper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

if ( ! class_exists( 'WP_Super_Duper' ) ) {

define( 'SUPER_DUPER_VER', '1.1.16' );
define( 'SUPER_DUPER_VER', '1.1.17' );

/**
* A Class to be able to create a Widget, Shortcode or Block to be able to output content for WordPress.
Expand Down Expand Up @@ -129,8 +129,9 @@ public function __construct( $options ) {
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 admin head
// add generator text to head
add_action( 'admin_head', array( $this, 'generator' ) );
add_action( 'wp_head', array( $this, 'generator' ) );
}

do_action( 'wp_super_duper_widget_init', $options, $this );
Expand Down Expand Up @@ -343,10 +344,25 @@ public static function get_picker( $editor_id = '' ) {
}

/**
* Output the version in the admin header.
* Output the version in the header.
*/
public function generator() {
echo '<meta name="generator" content="WP Super Duper v' . $this->version . '" />';
$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' . $this->version . '"' . ( ! empty( $source[0] ) ? ' data-sd-source="' . esc_attr( $source[0] ) . '"' : '' ) . ' />';
}

/**
Expand Down Expand Up @@ -790,10 +806,8 @@ function sd_copy_to_clipboard() {
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,
Expand Down Expand Up @@ -824,18 +838,15 @@ function sd_get_shortcode_options($this) {
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 = [];

Expand Down Expand Up @@ -878,11 +889,9 @@ function sd_build_shortcode($id) {
$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("]", "");
Expand All @@ -892,7 +901,6 @@ function sd_build_shortcode($id) {
$output = $output + " " + $field_name + '="' + element.value + '"';
}
}

});
}
$output = $output + "]";
Expand All @@ -905,7 +913,6 @@ function sd_build_shortcode($id) {
jQuery('#TB_ajaxContent #sd-shortcode-output').html($output);
}


/*
Delay the init of the textareas for 1 second.
*/
Expand All @@ -919,10 +926,8 @@ function sd_build_shortcode($id) {
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) {
Expand Down Expand Up @@ -994,7 +999,6 @@ function sd_init_textareas() {
// jQuery(this).parent().parent().find('.wpb_element_label').append(sd_shortcode_button());
// }
// });

}

/**
Expand Down Expand Up @@ -1043,7 +1047,6 @@ function sd_shortcode_button($id) {
return '<?php echo self::shortcode_button();?>';
}
}

</script>
<?php
}
Expand Down Expand Up @@ -1708,6 +1711,51 @@ public function group_arguments( $arguments ) {
return $arguments;
}

/**
* Parse used group tabs.
*
* @since 1.1.17
*/
public function group_block_tabs( $tabs, $arguments ) {
if ( ! empty( $tabs ) && ! empty( $arguments ) ) {
$has_sections = false;

foreach ( $this->arguments as $key => $args ) {
if ( isset( $args['group'] ) ) {
$has_sections = true;
break;
}
}

if ( ! $has_sections ) {
return $tabs;
}

$new_tabs = array();

foreach ( $tabs as $tab_key => $tab ) {
$new_groups = array();

if ( ! empty( $tab['groups'] ) && is_array( $tab['groups'] ) ) {
foreach ( $tab['groups'] as $group ) {
if ( isset( $arguments[ $group ] ) ) {
$new_groups[] = $group;
}
}
}

if ( ! empty( $new_groups ) ) {
$tab['groups'] = $new_groups;

$new_tabs[ $tab_key ] = $tab;
}
}

$tabs = $new_tabs;
}

return $tabs;
}

/**
* Output the JS for building the dynamic Guntenberg block.
Expand Down Expand Up @@ -2748,12 +2796,9 @@ function onChangeContent($type) {

}

// print_r( $this->arguments);

//echo '####';

$arguments = $this->group_arguments( $this->arguments );
//print_r($arguments ); exit;
$block_group_tabs = ! empty( $this->options['block_group_tabs'] ) ? $this->group_block_tabs( $this->options['block_group_tabs'], $arguments ) : array();

// Do we have sections?
$has_sections = $arguments == $this->arguments ? false : true;

Expand All @@ -2764,47 +2809,36 @@ function onChangeContent($type) {

$open_tab_groups = array();
$used_tabs = array();
foreach($arguments as $key => $args){

foreach ( $arguments as $key => $args ) {
$close_tab = false;
$close_tabs = false;

if(!empty($this->options['block_group_tabs'])) {
foreach($this->options['block_group_tabs'] as $tab_name => $tab_args){
if(in_array($key,$tab_args['groups'])){

if ( ! empty( $block_group_tabs ) ) {
foreach ( $block_group_tabs as $tab_name => $tab_args ) {
if ( in_array( $key, $tab_args['groups'] ) ) {
$open_tab_groups[] = $key;

if($open_tab != $tab_name){
if ( $open_tab != $tab_name ) {
$tab_args['tab']['tabs_open'] = $open_tab == '' ? true : false;
$tab_args['tab']['open'] = true;

$this->block_tab_start( '', $tab_args );
// echo '###open'.$tab_name;print_r($tab_args);
$open_tab = $tab_name;
$used_tabs[] = $tab_name;
}

if($open_tab_groups == $tab_args['groups']){
//$open_tab = '';
if ( $open_tab_groups == $tab_args['groups'] ) {
$close_tab = true;
$open_tab_groups = array();

// print_r(array_keys($this->options['block_group_tabs']));echo '####';print_r($used_tabs);
if($used_tabs == array_keys($this->options['block_group_tabs'])){
// echo '@@@';
if ( $used_tabs == array_keys( $block_group_tabs ) ) {
$close_tabs = true;
}
}

}
}
}

//

// print_r($arguments);exit;

?>
el(wp.components.PanelBody, {
title: '<?php esc_attr_e( $key ); ?>',
Expand All @@ -2815,11 +2849,7 @@ function onChangeContent($type) {
}?>
},
<?php



foreach ( $args as $k => $a ) {

$this->block_tab_start( $k, $a );
$this->block_row_start( $k, $a );
$this->build_block_arguments( $k, $a );
Expand All @@ -2831,7 +2861,6 @@ function onChangeContent($type) {
<?php
$panel_count ++;


if($close_tab || $close_tabs){
$tab_args = array(
'tab' => array(
Expand Down Expand Up @@ -3183,14 +3212,10 @@ public function block_tab_end($key, $args){

if(!empty($args['tab']['tabs_close'])){
if(false){?><script><?php }?>
],
},
( tab ) => {

return tab.content;

}
)), /* tabs close */
]}, ( tab ) => {
return tab.content;
}
)), /* tabs close */
<?php if(false){ ?></script><?php }
}
}
Expand Down

0 comments on commit da8143a

Please sign in to comment.