Code Duplication    Length = 82-82 lines in 2 locations

controls/js/script-legacy.js 1 location

@@ 798-879 (lines=82) @@
795
		 *                                  we can define the key here.
796
		 * @returns {null}
797
		 */
798
		set: function( element, value, key ) {
799
			var setting,
800
			    parts,
801
			    currentNode   = '',
802
			    foundNode     = '',
803
			    subSettingObj = {},
804
			    currentVal,
805
			    subSetting,
806
			    subSettingParts;
807
808
			// Get the setting from the element.
809
			setting = element;
810
			if ( _.isObject( element ) ) {
811
				if ( jQuery( element ).attr( 'data-id' ) ) {
812
					setting = element.attr( 'data-id' );
813
				} else {
814
					setting = element.parents( '[data-id]' ).attr( 'data-id' );
815
				}
816
			}
817
818
			if ( 'undefined' !== typeof wp.customize.control( setting ) ) {
819
				wp.customize.control( setting ).setting.set( value );
820
				return;
821
			}
822
823
			parts = setting.split( '[' ),
824
825
			// Find the setting we're using in the control using the customizer API.
826
			_.each( parts, function( part, i ) {
827
				part = part.replace( ']', '' );
828
829
				// The current part of the setting.
830
				currentNode = ( 0 === i ) ? part : '[' + part + ']';
831
832
				// When we find the node, get the value from it.
833
				// In case of an object we'll need to merge with current values.
834
				if ( ! _.isUndefined( wp.customize.instance( currentNode ) ) ) {
835
					foundNode  = currentNode;
836
					currentVal = wp.customize.instance( foundNode ).get();
837
				}
838
			} );
839
840
			// Get the remaining part of the setting that was unused.
841
			subSetting = setting.replace( foundNode, '' );
842
843
			// If subSetting is not empty, then we're dealing with an object
844
			// and we need to dig deeper and recursively merge the values.
845
			if ( '' !== subSetting ) {
846
				if ( ! _.isObject( currentVal ) ) {
847
					currentVal = {};
848
				}
849
				if ( '[' === subSetting.charAt( 0 ) ) {
850
					subSetting = subSetting.replace( '[', '' );
851
				}
852
				subSettingParts = subSetting.split( '[' );
853
				_.each( subSettingParts, function( subSettingPart, i ) {
854
					subSettingParts[ i ] = subSettingPart.replace( ']', '' );
855
				} );
856
857
				// If using a key, we need to go 1 level deeper.
858
				if ( key ) {
859
					subSettingParts.push( key );
860
				}
861
862
				// Converting to a JSON string and then parsing that to an object
863
				// may seem a bit hacky and crude but it's efficient and works.
864
				subSettingObj = '{"' + subSettingParts.join( '":{"' ) + '":"' + value + '"' + '}'.repeat( subSettingParts.length );
865
				subSettingObj = JSON.parse( subSettingObj );
866
867
				// Recursively merge with current value.
868
				jQuery.extend( true, currentVal, subSettingObj );
869
				value = currentVal;
870
871
			} else {
872
				if ( key ) {
873
					currentVal = ( ! _.isObject( currentVal ) ) ? {} : currentVal;
874
					currentVal[ key ] = value;
875
					value = currentVal;
876
				}
877
			}
878
			wp.customize.control( foundNode ).setting.set( value );
879
		}
880
	}
881
} );
882
/* global ajaxurl */

controls/js/script.js 1 location

@@ 798-879 (lines=82) @@
795
		 *                                  we can define the key here.
796
		 * @returns {null}
797
		 */
798
		set: function( element, value, key ) {
799
			var setting,
800
			    parts,
801
			    currentNode   = '',
802
			    foundNode     = '',
803
			    subSettingObj = {},
804
			    currentVal,
805
			    subSetting,
806
			    subSettingParts;
807
808
			// Get the setting from the element.
809
			setting = element;
810
			if ( _.isObject( element ) ) {
811
				if ( jQuery( element ).attr( 'data-id' ) ) {
812
					setting = element.attr( 'data-id' );
813
				} else {
814
					setting = element.parents( '[data-id]' ).attr( 'data-id' );
815
				}
816
			}
817
818
			if ( 'undefined' !== typeof wp.customize.control( setting ) ) {
819
				wp.customize.control( setting ).setting.set( value );
820
				return;
821
			}
822
823
			parts = setting.split( '[' ),
824
825
			// Find the setting we're using in the control using the customizer API.
826
			_.each( parts, function( part, i ) {
827
				part = part.replace( ']', '' );
828
829
				// The current part of the setting.
830
				currentNode = ( 0 === i ) ? part : '[' + part + ']';
831
832
				// When we find the node, get the value from it.
833
				// In case of an object we'll need to merge with current values.
834
				if ( ! _.isUndefined( wp.customize.instance( currentNode ) ) ) {
835
					foundNode  = currentNode;
836
					currentVal = wp.customize.instance( foundNode ).get();
837
				}
838
			} );
839
840
			// Get the remaining part of the setting that was unused.
841
			subSetting = setting.replace( foundNode, '' );
842
843
			// If subSetting is not empty, then we're dealing with an object
844
			// and we need to dig deeper and recursively merge the values.
845
			if ( '' !== subSetting ) {
846
				if ( ! _.isObject( currentVal ) ) {
847
					currentVal = {};
848
				}
849
				if ( '[' === subSetting.charAt( 0 ) ) {
850
					subSetting = subSetting.replace( '[', '' );
851
				}
852
				subSettingParts = subSetting.split( '[' );
853
				_.each( subSettingParts, function( subSettingPart, i ) {
854
					subSettingParts[ i ] = subSettingPart.replace( ']', '' );
855
				} );
856
857
				// If using a key, we need to go 1 level deeper.
858
				if ( key ) {
859
					subSettingParts.push( key );
860
				}
861
862
				// Converting to a JSON string and then parsing that to an object
863
				// may seem a bit hacky and crude but it's efficient and works.
864
				subSettingObj = '{"' + subSettingParts.join( '":{"' ) + '":"' + value + '"' + '}'.repeat( subSettingParts.length );
865
				subSettingObj = JSON.parse( subSettingObj );
866
867
				// Recursively merge with current value.
868
				jQuery.extend( true, currentVal, subSettingObj );
869
				value = currentVal;
870
871
			} else {
872
				if ( key ) {
873
					currentVal = ( ! _.isObject( currentVal ) ) ? {} : currentVal;
874
					currentVal[ key ] = value;
875
					value = currentVal;
876
				}
877
			}
878
			wp.customize.control( foundNode ).setting.set( value );
879
		}
880
	}
881
} );
882
/* global ajaxurl */