Skip to content
This repository was archived by the owner on Dec 2, 2022. It is now read-only.

Commit 5d3632b

Browse files
authored
Merge pull request #172 from essamamdani/master
Added: Upload Button
2 parents f28dc5e + e47fda1 commit 5d3632b

File tree

2 files changed

+134
-110
lines changed

2 files changed

+134
-110
lines changed

redux-core/inc/extensions/import_export/import_export/class-redux-import-export.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ class="button-secondary">
8989
class="button-secondary">
9090
<?php esc_html_e( 'Import from URL', 'redux-framework' ); ?>
9191
</a>
92+
93+
<a
94+
href="#"
95+
id="redux-import-upload"
96+
class="button-secondary">
97+
<?php esc_html_e( 'Upload file', 'redux-framework' ); ?>
98+
</a>
99+
<input type="file" id="redux-import-upload-file" multiple size="50" style='display:none;'>
92100
</p>
93101
<div id="redux-import-code-wrapper">
94102
<p class="description" id="import-code-description">
@@ -110,7 +118,7 @@ class="large-text no-update" rows="3"></textarea>
110118
class="large-text no-update"
111119
id="import-link-value"
112120
name="<?php echo esc_attr( $this->parent->args['opt_name'] ); ?>[import_link]"
113-
rows="2" />
121+
rows="2"/>
114122
</div>
115123
<p id="redux-import-action">
116124
<input
@@ -138,19 +146,18 @@ class="button-primary"
138146
</div>
139147
<?php $link = admin_url( 'admin-ajax.php?action=redux_download_options-' . $this->parent->args['opt_name'] . '&secret=' . $secret ); ?>
140148
<p>
141-
<button id="redux-export-code-copy" class="button-secondary" data-secret="<?php echo esc_attr( $secret ); ?>" data-copy="<?php esc_attr_e( 'Copy Data', 'redux-framework' ); ?>" data-copied="<?php esc_attr_e( 'Data Copied!', 'redux-framework' ); ?>">
149+
<button id="redux-export-code-copy" class="button-secondary" data-secret="<?php echo esc_attr( $secret ); ?>" data-copy="<?php esc_attr_e( 'Copy Data', 'redux-framework' ); ?>" data-copied="<?php esc_attr_e( 'Data Copied!', 'redux-framework' ); ?>">
142150
<?php esc_html_e( 'Copy Data', 'redux-framework' ); ?>
143-
</button>
151+
</button>
144152
<a href="<?php echo esc_url( $link ); ?>" id="redux-export-code-dl" class="button-primary">
145-
<?php esc_html_e( 'Export File', 'redux-framework' ); ?>
153+
<?php esc_html_e( 'Export File', 'redux-framework' ); ?>
146154
</a>
147-
<a href="javascript:void(0);" id="redux-export-link" class="button-secondary" data-copy="<?php esc_attr_e( 'Copy Export URL', 'redux-framework' ); ?>" data-copied="<?php esc_attr_e( 'URL Copied!', 'redux-framework' ); ?>" data-url="<?php echo esc_url( $link ); ?>">
155+
<a href="javascript:void(0);" id="redux-export-link" class="button-secondary" data-copy="<?php esc_attr_e( 'Copy Export URL', 'redux-framework' ); ?>" data-copied="<?php esc_attr_e( 'URL Copied!', 'redux-framework' ); ?>" data-url="<?php echo esc_url( $link ); ?>">
148156
<?php esc_html_e( 'Copy Export URL', 'redux-framework' ); ?>
149157
</a>
150158
</p>
151159
<p></p>
152160
<textarea class="large-text no-update" id="redux-export-code" rows="1"></textarea>
153-
154161
<?php
155162
}
156163

Lines changed: 121 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,221 +1,236 @@
11
/* global jQuery, document, redux, ajaxurl */
22

3-
(function( $ ) {
3+
(function ($) {
44
'use strict';
55

66
redux.field_objects = redux.field_objects || {};
77
redux.field_objects.import_export = redux.field_objects.import_export || {};
88

9-
redux.field_objects.import_export.copy_text = function( $text ) {
10-
var copyFrom = document.createElement( 'textarea' );
11-
document.body.appendChild( copyFrom );
9+
redux.field_objects.import_export.copy_text = function ($text) {
10+
var copyFrom = document.createElement('textarea');
11+
document.body.appendChild(copyFrom);
1212
copyFrom.textContent = $text;
1313
copyFrom.select();
14-
document.execCommand( 'copy' );
14+
document.execCommand('copy');
1515
copyFrom.remove();
1616
};
1717

18-
redux.field_objects.import_export.get_options = function( $secret ) {
19-
var $el = $( '#redux-export-code-copy' );
18+
redux.field_objects.import_export.get_options = function ($secret) {
19+
var $el = $('#redux-export-code-copy');
2020
var url = ajaxurl + '?download=0&action=redux_download_options-' + redux.optName.args.opt_name + '&secret=' + $secret;
21-
$el.addClass( 'disabled' ).attr( 'disabled', 'disabled' );
22-
$el.text( $el.data( 'copy' ) );
23-
$.get( url, function( data ) {
24-
redux.field_objects.import_export.copy_text( data );
25-
$el.removeClass( 'disabled' );
26-
$el.text( $el.data( 'copied' ) );
27-
setTimeout( function() {
28-
$el.text( $el.data( 'copy' ) ).removeClass( 'disabled' ).removeAttr( 'disabled' );
29-
}, 2000 );
30-
} );
21+
$el.addClass('disabled').attr('disabled', 'disabled');
22+
$el.text($el.data('copy'));
23+
$.get(url, function (data) {
24+
redux.field_objects.import_export.copy_text(data);
25+
$el.removeClass('disabled');
26+
$el.text($el.data('copied'));
27+
setTimeout(function () {
28+
$el.text($el.data('copy')).removeClass('disabled').removeAttr('disabled');
29+
}, 2000);
30+
});
3131
};
3232

33-
redux.field_objects.import_export.init = function( selector ) {
34-
selector = $.redux.getSelector( selector, 'import_export' );
33+
redux.field_objects.import_export.init = function (selector) {
34+
selector = $.redux.getSelector(selector, 'import_export');
3535

36-
$( selector ).each(
37-
function() {
36+
$(selector).each(
37+
function () {
3838
var textBox1;
3939
var textBox2;
4040

41-
var el = $( this );
41+
var el = $(this);
4242
var parent = el;
4343

44-
if ( ! el.hasClass( 'redux-field-container' ) ) {
45-
parent = el.parents( '.redux-field-container:first' );
44+
if (!el.hasClass('redux-field-container')) {
45+
parent = el.parents('.redux-field-container:first');
4646
}
4747

48-
if ( parent.is( ':hidden' ) ) {
48+
if (parent.is(':hidden')) {
4949
return;
5050
}
5151

52-
if ( parent.hasClass( 'redux-field-init' ) ) {
53-
parent.removeClass( 'redux-field-init' );
52+
if (parent.hasClass('redux-field-init')) {
53+
parent.removeClass('redux-field-init');
5454
} else {
5555
return;
5656
}
5757

5858
el.each(
59-
function() {
60-
$( '#redux-import' ).click(
61-
function( e ) {
62-
if ( '' === $( '#import-code-value' ).val() && '' === $(
63-
'#import-link-value' ).val() ) {
59+
function () {
60+
$('#redux-import').click(
61+
function (e) {
62+
if ('' === $('#import-code-value').val() && '' === $(
63+
'#import-link-value').val()) {
6464
e.preventDefault();
6565
return false;
6666
}
6767
}
6868
);
6969

70-
$( this ).find( '#redux-import-code-button' ).click(
71-
function() {
72-
var $el = $( '#redux-import-code-wrapper' );
73-
if ( $( '#redux-import-link-wrapper' ).is( ':visible' ) ) {
74-
$( '#import-link-value' ).val( '' );
75-
$( '#redux-import-link-wrapper' ).fadeOut(
70+
$(this).find('#redux-import-code-button').click(
71+
function () {
72+
var $el = $('#redux-import-code-wrapper');
73+
if ($('#redux-import-link-wrapper').is(':visible')) {
74+
$('#import-link-value').val('');
75+
$('#redux-import-link-wrapper').fadeOut(
7676
'fast',
77-
function() {
77+
function () {
7878
$el.fadeIn(
7979
'fast',
80-
function() {
81-
$( '#import-code-value' ).focus();
80+
function () {
81+
$('#import-code-value').focus();
8282
}
8383
);
8484
}
8585
);
8686
} else {
87-
if ( $el.is( ':visible' ) ) {
87+
if ($el.is(':visible')) {
8888
$el.fadeOut();
8989
} else {
9090
$el.fadeIn(
9191
'medium',
92-
function() {
93-
$( '#import-code-value' ).focus();
92+
function () {
93+
$('#import-code-value').focus();
9494
}
9595
);
9696
}
9797
}
9898
}
9999
);
100100

101-
$( this ).find( '#redux-import-link-button' ).click(
102-
function() {
103-
var $el = $( '#redux-import-link-wrapper' );
104-
if ( $( '#redux-import-code-wrapper' ).is( ':visible' ) ) {
105-
$( '#import-code-value' ).text( '' );
106-
$( '#redux-import-code-wrapper' ).fadeOut(
101+
$(this).find('#redux-import-link-button').click(
102+
function () {
103+
var $el = $('#redux-import-link-wrapper');
104+
if ($('#redux-import-code-wrapper').is(':visible')) {
105+
$('#import-code-value').text('');
106+
$('#redux-import-code-wrapper').fadeOut(
107107
'fast',
108-
function() {
108+
function () {
109109
$el.fadeIn(
110110
'fast',
111-
function() {
112-
$( '#import-link-value' ).focus();
111+
function () {
112+
$('#import-link-value').focus();
113113
}
114114
);
115115
}
116116
);
117117
} else {
118-
if ( $el.is( ':visible' ) ) {
118+
if ($el.is(':visible')) {
119119
$el.fadeOut();
120120
} else {
121121
$el.fadeIn(
122122
'medium',
123-
function() {
124-
$( '#import-link-value' ).focus();
123+
function () {
124+
$('#import-link-value').focus();
125125
}
126126
);
127127
}
128128
}
129129
}
130130
);
131-
$( this ).find( '#redux-export-code-dl' ).click( function( e ) {
131+
$(this).find('#redux-export-code-dl').click(function (e) {
132132
e.preventDefault();
133133

134-
if ( !! window.onbeforeunload ) {
135-
if ( confirm( 'Your panel has unchanged values, would you like to save them now?' ) ) {
136-
$( '#redux_top_save' ).click();
137-
setTimeout( function() {
138-
window.open( $( this ).attr( 'href' ) );
139-
}, 2000 );
134+
if (!!window.onbeforeunload) {
135+
if (confirm('Your panel has unchanged values, would you like to save them now?')) {
136+
$('#redux_top_save').click();
137+
setTimeout(function () {
138+
window.open($(this).attr('href'));
139+
}, 2000);
140140
}
141141
} else {
142-
window.open( $( this ).attr( 'href' ) );
142+
window.open($(this).attr('href'));
143143
}
144-
} );
145-
$( this ).find( '#redux-export-code-copy' ).click(
146-
function( e ) {
147-
var $el = $( '#redux-export-code' );
148-
var $secret = $( this ).data( 'secret' );
144+
});
145+
$(this).find('#redux-import-upload').click(function () {
146+
$('#redux-import-upload-file').click();
147+
})
148+
149+
document.getElementById('redux-import-upload-file').addEventListener('change', function () {
150+
var file_to_read = document.getElementById('redux-import-upload-file').files[0];
151+
var fileread = new FileReader();
152+
fileread.onload = function (e) {
153+
var content = e.target.result;
154+
// console.log(content);
155+
$(this).find('#import-code-value').text(content);
156+
$(this).find('#redux-import').click();
157+
};
158+
fileread.readAsText(file_to_read);
159+
});
160+
$(this).find('#redux-export-code-copy').click(
161+
function (e) {
162+
var $el = $('#redux-export-code');
163+
var $secret = $(this).data('secret');
149164
e.preventDefault();
150-
if ( !! window.onbeforeunload ) {
151-
if ( confirm(
152-
'Your panel has unchanged values, would you like to save them now?' ) ) {
153-
$( '#redux_top_save' ).click();
154-
setTimeout( function() {
155-
redux.field_objects.import_export.get_options( $secret, $el );
156-
}, 2000 );
165+
if (!!window.onbeforeunload) {
166+
if (confirm(
167+
'Your panel has unchanged values, would you like to save them now?')) {
168+
$('#redux_top_save').click();
169+
setTimeout(function () {
170+
redux.field_objects.import_export.get_options($secret, $el);
171+
}, 2000);
157172
}
158173
} else {
159-
redux.field_objects.import_export.get_options( $secret, $el );
174+
redux.field_objects.import_export.get_options($secret, $el);
160175
}
161176
}
162177
);
163-
$( this ).find( 'textarea' ).focusout(
164-
function() {
165-
var $id = $( this ).attr( 'id' );
166-
var $el = $( this );
178+
$(this).find('textarea').focusout(
179+
function () {
180+
var $id = $(this).attr('id');
181+
var $el = $(this);
167182
var $container = $el;
168183

169-
if ( 'import-link-value' === $id || 'import-code-value' === $id ) {
170-
$container = $( this ).parent();
184+
if ('import-link-value' === $id || 'import-code-value' === $id) {
185+
$container = $(this).parent();
171186
}
172187

173188
$container.fadeOut(
174189
'medium',
175-
function() {
176-
if ( 'redux-export-link-value' !== $id ) {
177-
$el.text( '' );
190+
function () {
191+
if ('redux-export-link-value' !== $id) {
192+
$el.text('');
178193
}
179194
}
180195
);
181196
}
182197
);
183198

184-
$( this ).find( '#redux-export-link' ).click(
185-
function() {
186-
var $el = $( this );
187-
$el.addClass( 'disabled' ).attr( 'disabled', 'disabled' );
188-
$el.text( $el.data( 'copy' ) );
189-
redux.field_objects.import_export.copy_text( $el.data( 'url' ) );
190-
$el.removeClass( 'disabled' );
191-
$el.text( $el.data( 'copied' ) );
192-
setTimeout( function() {
193-
$el.text( $el.data( 'copy' ) ).removeClass( 'disabled' ).removeAttr( 'disabled' );
194-
}, 2000 );
199+
$(this).find('#redux-export-link').click(
200+
function () {
201+
var $el = $(this);
202+
$el.addClass('disabled').attr('disabled', 'disabled');
203+
$el.text($el.data('copy'));
204+
redux.field_objects.import_export.copy_text($el.data('url'));
205+
$el.removeClass('disabled');
206+
$el.text($el.data('copied'));
207+
setTimeout(function () {
208+
$el.text($el.data('copy')).removeClass('disabled').removeAttr('disabled');
209+
}, 2000);
195210
}
196211
);
197212

198-
textBox1 = document.getElementById( 'redux-export-code' );
213+
textBox1 = document.getElementById('redux-export-code');
199214

200-
textBox1.onfocus = function() {
215+
textBox1.onfocus = function () {
201216
textBox1.select();
202217

203218
// Work around Chrome's little problem.
204-
textBox1.onmouseup = function() {
219+
textBox1.onmouseup = function () {
205220

206221
// Prevent further mouseup intervention.
207222
textBox1.onmouseup = null;
208223
return false;
209224
};
210225
};
211226

212-
textBox2 = document.getElementById( 'import-code-value' );
227+
textBox2 = document.getElementById('import-code-value');
213228

214-
textBox2.onfocus = function() {
229+
textBox2.onfocus = function () {
215230
textBox2.select();
216231

217232
// Work around Chrome's little problem.
218-
textBox2.onmouseup = function() {
233+
textBox2.onmouseup = function () {
219234

220235
// Prevent further mouseup intervention.
221236
textBox2.onmouseup = null;
@@ -228,4 +243,6 @@
228243
}
229244
);
230245
};
231-
})( jQuery );
246+
})(jQuery);
247+
248+

0 commit comments

Comments
 (0)