Skip to content

Commit 68177f8

Browse files
committed
Fix reference for this
Optimize assets loading in frontend editor and backend editor Fix loading in frontend editor Use save_always for checkbox without STD Optimize performance with registering param type only when needed Fix codestyle issues
1 parent ad9d44e commit 68177f8

File tree

4 files changed

+70
-47
lines changed

4 files changed

+70
-47
lines changed

assets/vc-custom-checkbox.js

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,42 @@
11
(function ( $ ) {
2-
/*
3-
Class used in edit form and editor models to save/render shortcode
4-
*/
5-
vc.atts.custom_checkbox = {
6-
/**
7-
* Used to save multiple values in single string for saving/parsing/opening
8-
* @param param
9-
* @returns {string}
2+
$( function () {
3+
/*
4+
Class used in edit form and editor models to save/render param type
105
*/
11-
parse: function ( param ) {
12-
var arr, newValue;
6+
window.vc.atts.custom_checkbox = {
7+
/**
8+
* Used to save multiple values in single string for saving/parsing/opening
9+
* @param param
10+
* @returns {string}
11+
*/
12+
parse: function ( param ) {
13+
var arr, newValue;
1314

14-
arr = [];
15-
newValue = '';
16-
$( 'input[name=' + param.param_name + ']', this.content() ).each( function () {
17-
var self;
15+
arr = [];
16+
newValue = '';
17+
$( 'input[name=' + param.param_name + ']', this.content() ).each( function () {
18+
var self;
1819

19-
self = $( this );
20-
if ( this.checked ) {
21-
arr.push( self.attr( 'value' ) );
20+
self = $( this );
21+
if ( this.checked ) {
22+
arr.push( self.attr( 'value' ) );
23+
}
24+
} );
25+
if ( 0 < arr.length ) {
26+
newValue = arr.join( ',' );
2227
}
23-
} );
24-
if ( 0 < arr.length ) {
25-
newValue = arr.join( ',' );
28+
return newValue;
29+
},
30+
/**
31+
* Used in shortcode saving
32+
* Default: '' empty (unchecked)
33+
* Can be overwritten by 'std'
34+
* @param param
35+
* @returns {string}
36+
*/
37+
defaults: function ( param ) {
38+
return ''; // needed for saving - without this default value for param will be first value in array
2639
}
27-
return newValue;
28-
},
29-
/**
30-
* Used in shortcode saving
31-
* Default: '' empty (unchecked)
32-
* Can be overwritten by 'std'
33-
* @param param
34-
* @returns {string}
35-
*/
36-
defaults: function ( param ) {
37-
return ''; // needed for saving - without this default value for param will be first value in array
38-
}
39-
};
40+
};
41+
} );
4042
})( window.jQuery );

param/class-vc-custom-checkbox.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,34 @@ class VcCustomCheckbox {
44

55
public function __construct() {
66
add_action( 'vc_load_default_params', array(
7-
&$this,
7+
$this,
88
'vc_load_vc_custom_checkbox_param',
99
) );
1010

11-
add_action( 'vc_backend_editor_render', array(
12-
&$this,
13-
'vc_enqueue_editor_scripts_befe',
11+
add_action( 'vc_backend_editor_enqueue_js_css', array(
12+
$this,
13+
'vc_enqueue_editor_scripts_be',
1414
) );
15+
add_action( 'vc_frontend_editor_enqueue_js_css', array(
16+
$this,
17+
'vc_enqueue_editor_scripts_fe',
18+
) );
19+
}
20+
21+
public function vc_enqueue_editor_scripts_be() {
22+
wp_enqueue_script( 'vc-custom-checbox-fe', preg_replace( '/\s/', '%20', plugins_url( 'assets/vc-custom-checkbox.js', vc_custom_checkbox_path() ) ) );
1523
}
1624

17-
public function vc_enqueue_editor_scripts_befe() {
18-
wp_enqueue_script( 'vc-custom-checbox-befe', preg_replace( '/\s/', '%20', plugins_url( 'assets/vc-custom-checkbox.js', vc_custom_checkbox_path() ) ) );
25+
public function vc_enqueue_editor_scripts_fe() {
26+
wp_enqueue_script( 'vc-custom-checbox-fe', preg_replace( '/\s/', '%20', plugins_url( 'assets/vc-custom-checkbox.js', vc_custom_checkbox_path() ) ) );
1927
}
2028

2129
/**
2230
* Add custom param to system
2331
*/
2432
public function vc_load_vc_custom_checkbox_param() {
2533
vc_add_shortcode_param( 'custom_checkbox', array(
26-
&$this,
34+
$this,
2735
'vc_custom_checkbox_form_field',
2836
) );
2937
}

shortcode/shortcode-vc-test-custom-checkbox-data.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
'type' => 'custom_checkbox',
1111
'heading' => __( 'Checked1', 'js_composer' ),
1212
'param_name' => 'custom_checkbox',
13-
'std' => '', // default unchecked
13+
'std' => '',
14+
// default unchecked
1415
'value' => array(
1516
__( 'Test1', 'js_composer' ) => 'Test1',
1617
__( 'Test2', 'js_composer' ) => 'Test2',
1718
__( 'Test3', 'js_composer' ) => 'Test3',
1819
),
1920
),
20-
// No STD means that default is first value in array
21+
// No STD means that default is first value in array, and if empty it will take first as Default value
2122
array(
2223
'type' => 'custom_checkbox',
2324
'heading' => __( 'Checked2', 'js_composer' ),
@@ -28,6 +29,18 @@
2829
__( 'Test3', 'js_composer' ) => 'Test3',
2930
),
3031
),
32+
array(
33+
'type' => 'custom_checkbox',
34+
'heading' => __( 'Checked2-with save always', 'js_composer' ),
35+
'param_name' => 'custom_checkbox2_save_always',
36+
'value' => array(
37+
__( 'Test1', 'js_composer' ) => 'Test1',
38+
__( 'Test2', 'js_composer' ) => 'Test2',
39+
__( 'Test3', 'js_composer' ) => 'Test3',
40+
),
41+
'save_always' => true,
42+
// This will allow to force save empty value (otherwise in case if emtpy it will take first value in array)
43+
),
3144
// Std='Test2' - means that default is Test2 checked
3245
array(
3346
'type' => 'custom_checkbox',

vc-custom-checkbox.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Plugin Name: Vc Custom Checkbox
44
Plugin URI: http://vc.wpbakery.com/
55
Description: Custom checkbox Visual Composer on Steroids
6-
Version: 1.0
6+
Version: 1.1
77
Author: WPBakery
88
Author URI: http://wpbakery.com
99
License: http://codecanyon.net/licenses
@@ -12,16 +12,16 @@
1212
die( '' ); // Don't call directly
1313
}
1414

15-
add_action( 'vc_after_init', 'vc_custom_checkbox_init' );
15+
add_action( 'vc_build_admin_page', 'vc_custom_checkbox_init' );
1616
add_action( 'vc_after_init', 'vc_custom_checkbox_shortcode' );
1717
function vc_custom_checkbox_init() {
18-
require_once "param/class-vc-custom-checkbox.php";
18+
require_once 'param/class-vc-custom-checkbox.php';
1919
$checkbox = new VcCustomCheckbox();
2020
}
2121

2222
function vc_custom_checkbox_shortcode() {
23-
require_once "shortcode/shortcode-vc-test-custom-checkbox-lean-map.php";
24-
require_once "shortcode/shortcode-vc-test-custom-checkbox-class.php";
23+
require_once 'shortcode/shortcode-vc-test-custom-checkbox-lean-map.php';
24+
require_once 'shortcode/shortcode-vc-test-custom-checkbox-class.php';
2525
}
2626

2727
function vc_custom_checkbox_path() {

0 commit comments

Comments
 (0)