Skip to content

Commit

Permalink
update to 1.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
houke committed Dec 24, 2018
1 parent 60e4d47 commit 3a74609
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 102 deletions.
113 changes: 113 additions & 0 deletions assets/js/generate-palette-upload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
jQuery(document).on('ready', function() {
//old uploader
if (typeof uploader != 'undefined') {
uploader.bind('UploadComplete', function(up, files, info) {
if (files) {
setTimeout(function() {
jQuery(files).each(function() {
var file = jQuery(this).get(0);
var id = file.id;
var img = jQuery('#media-item-' + id)
.find('img')
.attr('src');
var image = img.replace(cpg_upload.thumb, '');
var edit = jQuery('#media-item-' + id)
.find('a.edit-attachment')
.attr('href');
var edit_split = edit.split('?');
var id = false;
if (edit_split.length > 1) {
var params = cpg_upload_getQueryParams(edit_split[1]);
if (params.post) {
id = params.post;
}
}
cpg_generate_on_upload(image, id);
});
}, 1000);
}
});
}

function cpg_upload_getQueryParams(qs) {
qs = qs.split('+').join(' ');

var params = {},
tokens,
re = /[?&]?([^=]+)=([^&]*)/g;

while ((tokens = re.exec(qs))) {
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
}

return params;
}

//new uploader
if (typeof wp.Uploader !== 'undefined') {
if (typeof wp.Uploader.queue !== 'undefined') {
wp.Uploader.queue.on('reset', function(up, files) {
if (
typeof files !== 'undefined' &&
files.previousModels &&
files.previousModels.length > 0
) {
jQuery(files.previousModels).each(function() {
var file = $(this).get(0);
cpg_generate_on_upload(file.changed.url, file.changed.id);
});
}
});
} else {
jQuery.extend(wp.Uploader.prototype, {
init: function() {
// plupload 'PostInit'
this.uploader.bind('BeforeUpload', function(file) {});
},
success: function(file) {
cpg_generate_on_upload(file.changed.url, file.changed.id);
}
});
}
}

function cpg_generate_on_upload(src, id) {
var img = new Image();
img.src = src;
var nonce =
typeof cpg_upload != 'undefined' ? cpg_upload._wpnonce : 'undefined';
var colors =
typeof cpg_upload != 'undefined' ? parseInt(cpg_upload.colors) : 10;
img.onload = function() {
cpg_colorThief = new ColorThief();
cpg_AddColorsForImageUpload(img, id, nonce, colors, src);
};
}

function cpg_AddColorsForImageUpload(image, id, nonce, colors, file) {
var cpg_dominant = jQuery.Deferred();
var cpg_palette = jQuery.Deferred();
cpg_dominant.resolve(cpg_colorThief.getColor(image));
cpg_palette.resolve(cpg_colorThief.getPalette(image, colors));
jQuery
.when(cpg_dominant, cpg_palette)
.then(function(cpg_dominant, cpg_palette) {
jQuery.ajax({
url: ajaxurl,
type: 'post',
dataType: 'JSON',
timeout: 300000,
data: {
action: 'cpg_add_palette',
dominant: cpg_dominant,
palette: cpg_palette,
nonce: nonce,
id: id,
file: !id ? file : false
},
success: function(response) {},
error: function(jqXHR, exception) {}
});
});
}
});
177 changes: 86 additions & 91 deletions assets/js/generate-palette.js
Original file line number Diff line number Diff line change
@@ -1,98 +1,93 @@
jQuery(document).on('ready', function() {
var cpg_colorThief;
var cpg_colorThief;

jQuery(document).on('click', '.cpg-button-palette', function(e) {
if (
!jQuery(this).attr('disabled') &&
!jQuery(this).hasClass('disabled')
) {
e.preventDefault();
var elem = jQuery(this);
var img = new Image();
var params = cpg_parseParams(elem.attr('href').split('?')[1]);
elem
.text(cpg.generating + '...')
.attr('disabled', 'disabled')
.addClass('disabled');
img.src = elem.data('src');
var id = params.post_id;
var nonce = params._wpnonce;
var colors = params.colors;
img.onload = function() {
cpg_colorThief = new ColorThief();
var color = cpg_AddColorsForImage(elem, img, id, nonce, colors);
};
}
});
jQuery(document).on('click', '.cpg-button-palette', function(e) {
if (!jQuery(this).attr('disabled') && !jQuery(this).hasClass('disabled')) {
e.preventDefault();
var elem = jQuery(this);
var img = new Image();
var params = cpg_parseParams(elem.attr('href').split('?')[1]);
elem
.text(cpg.generating + '...')
.attr('disabled', 'disabled')
.addClass('disabled');
img.src = elem.data('src');
var id = params.post_id;
var nonce = params._wpnonce;
var colors = params.colors;
img.onload = function() {
cpg_colorThief = new ColorThief();
var color = cpg_AddColorsForImage(elem, img, id, nonce, colors);
};
}
});

jQuery(document).on('click', '.cpg-button-palette-trash', function(e) {
e.preventDefault();
var elem = jQuery(this);
var params = cpg_parseParams(elem.attr('href').split('?')[1]);
var r = confirm(cpg.confirm_trash);
if (r == true) {
cpg_RemoveColorsForImage(elem, params);
}
});
jQuery(document).on('click', '.cpg-button-palette-trash', function(e) {
e.preventDefault();
var elem = jQuery(this);
var params = cpg_parseParams(elem.attr('href').split('?')[1]);
var r = confirm(cpg.confirm_trash);
if (r == true) {
cpg_RemoveColorsForImage(elem, params);
}
});

function cpg_AddColorsForImage(elem, image, id, nonce, colors) {
var cpg_dominant = jQuery.Deferred();
var cpg_palette = jQuery.Deferred();
var cpg_td = elem.parent();
function cpg_AddColorsForImage(elem, image, id, nonce, colors) {
var cpg_dominant = jQuery.Deferred();
var cpg_palette = jQuery.Deferred();
var cpg_td = elem.parent();

cpg_dominant.resolve(cpg_colorThief.getColor(image));
cpg_palette.resolve(cpg_colorThief.getPalette(image, colors));
jQuery.when(cpg_dominant, cpg_palette).then(
function(cpg_dominant, cpg_palette) {
jQuery.ajax({
url: ajaxurl,
type: 'post',
dataType: 'JSON',
timeout: 300000,
data: {
action: 'cpg_add_palette',
dominant: cpg_dominant,
palette: cpg_palette,
nonce: nonce,
id: id
},
success: function(response) {
cpg_td.html(response);
},
error: function(jqXHR, exception) {
var msg = cpg_showErrors(jqXHR, exception);
cpg_td.html(
'<span style="color:#f00;">' + msg + '</span>'
);
}
});
},
function() {
cpg_td.html('<span style="color:#f00;">' + msg + '</span>');
}
);
}
cpg_dominant.resolve(cpg_colorThief.getColor(image));
cpg_palette.resolve(cpg_colorThief.getPalette(image, colors));
jQuery.when(cpg_dominant, cpg_palette).then(
function(cpg_dominant, cpg_palette) {
jQuery.ajax({
url: ajaxurl,
type: 'post',
dataType: 'JSON',
timeout: 300000,
data: {
action: 'cpg_add_palette',
dominant: cpg_dominant,
palette: cpg_palette,
nonce: nonce,
id: id
},
success: function(response) {
cpg_td.html(response);
},
error: function(jqXHR, exception) {
var msg = cpg_showErrors(jqXHR, exception);
cpg_td.html('<span style="color:#f00;">' + msg + '</span>');
}
});
},
function() {
cpg_td.html('<span style="color:#f00;">' + msg + '</span>');
}
);
}

function cpg_RemoveColorsForImage(elem, params) {
var cpg_td = elem.parents('.cpg_dominant_color_column');
cpg_td.html(cpg.trashing + '...');
jQuery.ajax({
url: ajaxurl,
type: 'post',
dataType: 'JSON',
timeout: 300000,
data: {
action: params.action,
id: params.post_id,
nonce: params._wpnonce
},
success: function(response) {
cpg_td.html(response);
},
error: function(jqXHR, exception) {
var msg = cpg_showErrors(jqXHR, exception);
cpg_td.html('<span style="color:#f00;">' + msg + '</span>');
}
});
}
function cpg_RemoveColorsForImage(elem, params) {
var cpg_td = elem.parents('.cpg_dominant_color_column');
cpg_td.html(cpg.trashing + '...');
jQuery.ajax({
url: ajaxurl,
type: 'post',
dataType: 'JSON',
timeout: 300000,
data: {
action: params.action,
id: params.post_id,
nonce: params._wpnonce
},
success: function(response) {
cpg_td.html(response);
},
error: function(jqXHR, exception) {
var msg = cpg_showErrors(jqXHR, exception);
cpg_td.html('<span style="color:#f00;">' + msg + '</span>');
}
});
}
});
4 changes: 2 additions & 2 deletions color-palette-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Color Palette Generator
Plugin URI: https://github.com/houke/color-palette-generator
Description: Generates color palettes for your media uploads (jpg, png, gif), shows them on your website and allows you to filter images per color
Version: 1.6.2
Version: 1.7.0
Author: Houke de Kwant
Author URI: https://github.com/houke/
Text Domain: cpg
Expand All @@ -16,7 +16,7 @@

//Constants
$prefix = 'CPG_';
$version = '1.6.2';
$version = '1.7.0';

//Define variables
$cpg_constants = array(
Expand Down
32 changes: 25 additions & 7 deletions inc/cpg-admin-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,31 @@ function cpg_add_palette() {
$dominant = isset( $_POST['dominant'] ) ? sanitize_text_field( $_POST['dominant'] ) : "";
$palette = isset( $_POST['palette'] ) ? array_map( 'sanitize_text_field', $_POST['palette'] ) : "";
$nonce = isset( $_POST['nonce'] ) ? sanitize_key( $_POST['nonce'] ) : "" ;
$options = get_option('cpg_options');
$autogenerate = isset( $options['autogenerate'] ) ? $options['autogenerate'] : false;

$PKR = new PKRoundColor();
$parent = $PKR->getRoundedColor($dominant);

if(
if( wp_verify_nonce( $nonce, 'cpg_add_palette_on_upload_nonce') &&
empty( $post_id ) &&
isset($_POST['file']) &&
!empty($_POST['file'])
) {
$post_id = attachment_url_to_postid( $_POST['file'] );
if( empty( $post_id ) ) {
$output = __( 'No file found', 'cpg');
echo json_encode( $output );
wp_die();
}
}

if( wp_verify_nonce( $nonce, 'cpg_add_palette_on_upload_nonce') && !$autogenerate ){
$output = __( 'Auto generation of palettes disabled', 'cpg');
echo json_encode( $output );
}elseif(
get_post_type( $post_id ) == 'attachment' &&
wp_verify_nonce( $nonce, 'cpg_add_palette_'.$post_id.'_nonce' )
(
wp_verify_nonce( $nonce, 'cpg_add_palette_'.$post_id.'_nonce' ) ||
wp_verify_nonce( $nonce, 'cpg_add_palette_on_upload_nonce')
)
){
wp_cache_flush();
wp_defer_term_counting( true );
Expand Down Expand Up @@ -114,9 +132,9 @@ function cpg_bulk_add_palette() {

if( $regenerate === 'reset' ){
cpg_setup_taxonomies( false, true );
$result = array(
$result = array(
'more' => false,
'reset' => true
'reset' => true
);
}elseif( $regenerate == true && wp_verify_nonce( $nonce, 'cpg_bulk_regenerate_palette_nonce' ) ) {
cpg_setup_taxonomies( true, false );
Expand Down
Loading

0 comments on commit 3a74609

Please sign in to comment.