From 2ed9cd4d5feae45537abddfd5a0eeb0a74bf242e Mon Sep 17 00:00:00 2001 From: stiofan Date: Wed, 12 Aug 2020 16:35:36 +0100 Subject: [PATCH 1/4] 1.0.20 --- change-log.txt | 7 +++ composer.json | 2 +- wp-super-duper.php | 107 +++++++++++++++++++++++++++++++++++++++------ 3 files changed, 102 insertions(+), 14 deletions(-) diff --git a/change-log.txt b/change-log.txt index bcfa097..3e3c428 100644 --- a/change-log.txt +++ b/change-log.txt @@ -1,3 +1,10 @@ += 1.0.20 = +* On change content don't compare the actual content, only attributes - CHANGED +* Call AUI init JS function if defined on each block edit - ADDED +* is_preview() now included block preview - CHANGED +* Empty block previews can show broken class input - FIXED +* color input for block now uses colorPicker - CHANGED + = 1.0.19 = * Put non grouped block settings in to a "settings" group - CHANGED diff --git a/composer.json b/composer.json index 92b416d..b9e7414 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "ayecode/wp-super-duper", - "version": "1.0.19", + "version": "1.0.20", "type": "library", "description": "Lets you create a widget, block and shortcode all from the one file .", "keywords": ["WordPress","super duper","wp"], diff --git a/wp-super-duper.php b/wp-super-duper.php index baa73a5..19b17db 100755 --- a/wp-super-duper.php +++ b/wp-super-duper.php @@ -17,11 +17,12 @@ */ class WP_Super_Duper extends WP_Widget { - public $version = "1.0.19"; + public $version = "1.0.20"; 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; @@ -1333,6 +1334,7 @@ public function shortcode_output( $args = array(), $content = '' ) { } $class = isset( $this->options['widget_ops']['classname'] ) ? esc_attr( $this->options['widget_ops']['classname'] ) : ''; + $class .= " sdel-".$this->get_instance_hash(); $class = apply_filters( 'wp_super_duper_div_classname', $class, $args, $this ); $class = apply_filters( 'wp_super_duper_div_classname_' . $this->base_id, $class, $args, $this ); @@ -1375,7 +1377,7 @@ public function shortcode_output( $args = array(), $content = '' ) { // if preview show a placeholder if empty if ( $this->is_preview() && $output == '' ) { - $output = $this->preview_placeholder_text( "[{" . $this->base_id . "}]" ); + $output = $this->preview_placeholder_text( "{{" . $this->base_id . "}}" ); } return apply_filters( 'wp_super_duper_widget_output', $output, $args, $shortcode_args, $this ); @@ -1494,9 +1496,7 @@ public function output( $args = array(), $widget_args = array(), $content = '' ) public function register_block() { wp_add_inline_script( 'wp-blocks', $this->block() ); if ( class_exists( 'SiteOrigin_Panels' ) ) { - wp_add_inline_script( 'wp-blocks', $this->siteorigin_js() ); - } } @@ -1761,9 +1761,17 @@ public function block() { function onChangeContent() { - if (!is_fetching && prev_attributes[props.id] != props.attributes) { + $refresh = false; + + // Set the old content the same as the new one so we only compare all other attributes + if(typeof(prev_attributes[props.id]) != 'undefined'){ + prev_attributes[props.id].content = props.attributes.content; + }else if(props.attributes.content === ""){ + // if first load and content empty then refresh + $refresh = true; + } - //console.log(props); + if ( ( !is_fetching && JSON.stringify(prev_attributes[props.id]) != JSON.stringify(props.attributes) ) || $refresh ) { is_fetching = true; var data = { @@ -1788,6 +1796,11 @@ function onChangeContent() { props.setAttributes({content: env}); is_fetching = false; prev_attributes[props.id] = props.attributes; + + // if AUI is active call the js init function + if (typeof aui_init === "function") { + aui_init(); + } }); @@ -1824,8 +1837,8 @@ function onChangeContent() { if ( $show_advanced ) { ?> el('div', { - style: {'padding-left': '16px','padding-right': '16px'} - }, + style: {'padding-left': '16px','padding-right': '16px'} + }, el( wp.components.ToggleControl, { @@ -2006,8 +2019,9 @@ public function build_block_arguments( $key, $args ) { } $onchange = "props.setAttributes({ $key: $key } )"; + $onchangecomplete = ""; $value = "props.attributes.$key"; - $text_type = array( 'text', 'password', 'number', 'email', 'tel', 'url', 'color' ); + $text_type = array( 'text', 'password', 'number', 'email', 'tel', 'url', 'colorx' ); if ( in_array( $args['type'], $text_type ) ) { $type = 'TextControl'; // Save numbers as numbers and not strings @@ -2015,9 +2029,20 @@ public function build_block_arguments( $key, $args ) { $onchange = "props.setAttributes({ $key: Number($key) } )"; } } - // elseif ( $args['type'] == 'color' ) { //@todo ColorPicker labels are not shown yet, we use html5 color input for now https://github.com/WordPress/gutenberg/issues/14378 - // $type = 'ColorPicker'; - // } + elseif ( $args['type'] == 'color' ) { + $type = 'ColorPicker'; + $onchange = ""; + $extra = "color: $value,"; + if(!empty($args['disable_alpha'])){ + $extra .= "disableAlpha: true,"; + } + $onchangecomplete = "onChangeComplete: function($key) { + value = $key.rgb.a && $key.rgb.a < 1 ? \"rgba(\"+$key.rgb.r+\",\"+$key.rgb.g+\",\"+$key.rgb.b+\",\"+$key.rgb.a+\")\" : $key.hex; + props.setAttributes({ + $key: value + }); + },"; + } elseif ( $args['type'] == 'checkbox' ) { $type = 'CheckboxControl'; $extra .= "checked: props.attributes.$key,"; @@ -2052,6 +2077,11 @@ public function build_block_arguments( $key, $args ) { if ( ! empty( $args['element_require'] ) ) { echo $this->block_props_replace( $args['element_require'], true ) . " && "; } + + // color input does not show the labels so we add them + if($args['type']=='color'){ + echo "el('div', {style: {'marginBottom': '8px'}}, '".addslashes( $args['title'] )."'),"; + } ?> el( wp.components., { label: '', @@ -2068,11 +2098,18 @@ public function build_block_arguments( $key, $args ) { + onChange: function ( ) { } } ), options['widget_ops']['classname']; + $class = $this->options['widget_ops']['classname']." sdel-".$this->get_instance_hash(); + // Before widget $before_widget = $args['before_widget']; + $before_widget = str_replace($class_original,$class,$before_widget); $before_widget = apply_filters( 'wp_super_duper_before_widget', $before_widget, $args, $instance, $this ); $before_widget = apply_filters( 'wp_super_duper_before_widget_' . $this->base_id, $before_widget, $args, $instance, $this ); @@ -2247,7 +2289,7 @@ public function widget( $args, $instance ) { echo $before_widget; // elementor strips the widget wrapping div so we check for and add it back if needed if ( $this->is_elementor_widget_output() ) { - echo ! empty( $this->options['widget_ops']['classname'] ) ? "" : ''; + echo ! empty( $this->options['widget_ops']['classname'] ) ? "" : ''; } echo $this->output_title( $args, $instance ); echo $output; @@ -2410,6 +2452,8 @@ public function is_preview() { $preview = true; } elseif ( $this->is_oxygen_preview() ) { $preview = true; + } elseif( $this->is_block_content_call() ) { + $preview = true; } return $preview; @@ -2796,6 +2840,43 @@ public function is_block_content_call() { return $result; } + /** + * Get an instance hash that will be unique to the type and settings. + * + * @since 1.0.20 + * @return string + */ + public function get_instance_hash(){ + $instance_string = $this->base_id.serialize($this->instance); + return hash('crc32b',$instance_string); + } + + /** + * Generate and return inline styles from CSS rules that will match the unique class of the instance. + * + * @param array $rules + * + * @since 1.0.20 + * @return string + */ + public function get_instance_style($rules = array()){ + $css = ''; + + if(!empty($rules)){ + $rules = array_unique($rules); + $instance_hash = $this->get_instance_hash(); + $css .= ""; + } + + + return $css; + + } + } } \ No newline at end of file From c60fe9b7b94ff861aae88bf197deb297fbe9b51e Mon Sep 17 00:00:00 2001 From: justlevine Date: Tue, 18 Aug 2020 03:00:51 +0300 Subject: [PATCH 2/4] Update wp-super-duper.php --- wp-super-duper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wp-super-duper.php b/wp-super-duper.php index 19b17db..0c42416 100755 --- a/wp-super-duper.php +++ b/wp-super-duper.php @@ -1322,7 +1322,7 @@ public static function render_shortcode() { * @return string */ public function shortcode_output( $args = array(), $content = '' ) { - $args = self::argument_values( $args ); + $args = $this->argument_values( $args ); // add extra argument so we know its a output to gutenberg //$args @@ -2879,4 +2879,4 @@ public function get_instance_style($rules = array()){ } -} \ No newline at end of file +} From 5eef584ab0b7ae4e595f00eba60c11f23bffa289 Mon Sep 17 00:00:00 2001 From: stiofan Date: Tue, 18 Aug 2020 10:17:36 +0100 Subject: [PATCH 3/4] Category update on post_type change --- change-log.txt | 3 ++ wp-super-duper.php | 120 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 103 insertions(+), 20 deletions(-) diff --git a/change-log.txt b/change-log.txt index 3e3c428..72af350 100644 --- a/change-log.txt +++ b/change-log.txt @@ -1,3 +1,6 @@ += 1.0.21 = +* Block Category can now update on post_type change - FIXED + = 1.0.20 = * On change content don't compare the actual content, only attributes - CHANGED * Call AUI init JS function if defined on each block edit - ADDED diff --git a/wp-super-duper.php b/wp-super-duper.php index 19b17db..a743085 100755 --- a/wp-super-duper.php +++ b/wp-super-duper.php @@ -1644,6 +1644,10 @@ public function block() { var is_fetching = false; var prev_attributes = []; + var term_query_type = ''; + var post_type_rest_slugs = arguments ) && isset($this->arguments['post_type']['onchange_rest']['values'])){echo "[".json_encode($this->arguments['post_type']['onchange_rest']['values'])."]";}else{echo "[]";} ?>; + const taxonomies_id);?> = [{label: "Please wait", value: 0}]; + /** * Register Basic Block. * @@ -1757,6 +1761,40 @@ public function block() { // The "edit" property must be a valid function. edit: function (props) { + + + arguments['post_type']) && isset($this->arguments['category']) && isset($this->arguments['category']['post_type_linked']) ){ + ?> + if(typeof(prev_attributes[props.id]) != 'undefined' ){ + $pt = props.attributes.post_type; + if(post_type_rest_slugs.length){ + $value = post_type_rest_slugs[0][$pt]; + } + if('post_type' in prev_attributes[props.id] && 'category' in prev_attributes[props.id] && $pt != term_query_type ){ + term_query_type = $pt; + wp.apiFetch({path: "arguments['post_type']['onchange_rest']['path'])){echo $this->arguments['post_type']['onchange_rest']['path'];}else{'/wp/v2/"+$value+"/categories';} ?>"}).then(terms => { + while (taxonomies_id);?>.length) { + taxonomies_id);?>.pop(); + } + taxonomies_id);?>.push({label: "All", value: 0}); + jQuery.each( terms, function( key, val ) { + taxonomies_id);?>.push({label: val.name, value: val.id}); + }); + + // setting the value back and fourth fixes the no update issue that sometimes happens where it won't update the options. + var $old_cat_value = props.attributes.category + props.setAttributes({category: [0] }); + props.setAttributes({category: $old_cat_value }); + + return taxonomies_id);?>; + }); + } + } + + + var content = props.attributes.content; function onChangeContent() { @@ -2018,6 +2056,13 @@ public function build_block_arguments( $key, $args ) { return; } + // require advanced + $require_advanced = ! empty( $args['advanced'] ) ? "props.attributes.show_advanced && " : ""; + + // element require + $element_require = ! empty( $args['element_require'] ) ? $this->block_props_replace( $args['element_require'], true ) . " && " : ""; + + $onchange = "props.setAttributes({ $key: $key } )"; $onchangecomplete = ""; $value = "props.attributes.$key"; @@ -2029,6 +2074,40 @@ public function build_block_arguments( $key, $args ) { $onchange = "props.setAttributes({ $key: Number($key) } )"; } } +/* + * https://www.wptricks.com/question/set-current-tab-on-a-gutenberg-tabpanel-component-from-outside-that-component/ es5 layout + elseif($args['type']=='tabs'){ + ?> + + $option_label ) { - $options .= "{ value: '" . esc_attr( $option_val ) . "', label: '" . addslashes( $option_label ) . "' },"; + + if($args['name'] == 'category' && !empty($args['post_type_linked'])){ + $options .= "options: taxonomies_".str_replace("-","_", $this->id).","; + }else { + + if ( ! empty( $args['options'] ) ) { + $options .= "options: ["; + foreach ( $args['options'] as $option_val => $option_label ) { + $options .= "{ value: '" . esc_attr( $option_val ) . "', label: '" . addslashes( $option_label ) . "' },"; + } + $options .= "],"; } - $options .= "],"; } if ( isset( $args['multiple'] ) && $args['multiple'] ) { //@todo multiselect does not work at the moment: https://github.com/WordPress/gutenberg/issues/5550 $extra .= ' multiple: true, '; - //$onchange = "props.setAttributes({ $key: ['edit'] } )"; - //$value = "['edit', 'delete']"; } } elseif ( $args['type'] == 'alignment' ) { $type = 'AlignmentToolbar'; // @todo this does not seem to work but cant find a example @@ -2069,19 +2152,21 @@ public function build_block_arguments( $key, $args ) { return;// if we have not implemented the control then don't break the JS. } - // add show only if advanced - if ( ! empty( $args['advanced'] ) ) { - echo "props.attributes.show_advanced && "; - } - // add setting require if defined - if ( ! empty( $args['element_require'] ) ) { - echo $this->block_props_replace( $args['element_require'], true ) . " && "; - } + // color input does not show the labels so we add them if($args['type']=='color'){ + // add show only if advanced + echo $require_advanced; + // add setting require if defined + echo $element_require; echo "el('div', {style: {'marginBottom': '8px'}}, '".addslashes( $args['title'] )."'),"; } + + // add show only if advanced + echo $require_advanced; + // add setting require if defined + echo $element_require; ?> el( wp.components., { label: '', @@ -2104,11 +2189,6 @@ public function build_block_arguments( $key, $args ) { } } ), Date: Tue, 18 Aug 2020 16:52:04 +0100 Subject: [PATCH 4/4] 1.0.21 --- change-log.txt | 1 + composer.json | 2 +- wp-super-duper.php | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/change-log.txt b/change-log.txt index 72af350..8b707c2 100644 --- a/change-log.txt +++ b/change-log.txt @@ -1,5 +1,6 @@ = 1.0.21 = * Block Category can now update on post_type change - FIXED +* Non static method 'argument_values' should not be called statically - FIXED = 1.0.20 = * On change content don't compare the actual content, only attributes - CHANGED diff --git a/composer.json b/composer.json index b9e7414..40c7d04 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "ayecode/wp-super-duper", - "version": "1.0.20", + "version": "1.0.21", "type": "library", "description": "Lets you create a widget, block and shortcode all from the one file .", "keywords": ["WordPress","super duper","wp"], diff --git a/wp-super-duper.php b/wp-super-duper.php index cb0daab..986350f 100755 --- a/wp-super-duper.php +++ b/wp-super-duper.php @@ -17,7 +17,7 @@ */ class WP_Super_Duper extends WP_Widget { - public $version = "1.0.20"; + public $version = "1.0.21"; public $font_awesome_icon_version = "5.11.2"; public $block_code; public $options;