Skip to content

Commit

Permalink
Merge pull request #5 from humanmade/get-2-colors
Browse files Browse the repository at this point in the history
restrict-and-validate
  • Loading branch information
pdewouters committed Jul 3, 2015
2 parents 9312cbd + a75a8b1 commit dd81917
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions inc/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function plugins_loaded() {

/**
* Adds the style attribute to the image HTML.
*
*
* @param $html
* @param $id
* @param $caption
Expand Down Expand Up @@ -161,8 +161,8 @@ public function extract_colors( $image_path ) {

$image = $client->loadJpeg( $image_path );

// Get three most used color hex code
return $image->extract( 4 );
// Get 2 most used color hex code
return $image->extract( 2 );

}

Expand Down Expand Up @@ -200,8 +200,18 @@ public function get_colors_for_attachment( $id ) {
* @param $id
* @param array $colors
*/
public function save_colors_for_attachment( $id, $colors = array() ) {
public function save_colors_for_attachment( $id, Array $colors = array() ) {

// Validate hex codes.
$colors = array_filter( $colors, function( $color ) {
return preg_match( '/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/', $color );
} );

// Restrict to 2 items.
$colors = array_slice( $colors, 0, 2 );

update_post_meta( $id, 'hmgp_image_colors', $colors );

}

/**
Expand Down

0 comments on commit dd81917

Please sign in to comment.