Skip to content

Commit

Permalink
refactor creation of rokka stack name
Browse files Browse the repository at this point in the history
  • Loading branch information
tschortsch committed Sep 20, 2017
1 parent e027248 commit c64fa2c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
26 changes: 18 additions & 8 deletions src/class-rokka-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ public function rokka_sync_stacks() {

foreach ( $stacks_to_sync as $stack ) {
// handle full size stack specially
if ( $stack['name'] === $this->get_stack_prefix() . $this->get_rokka_full_size_stack_name() ) {
if ( $stack['name'] === $this->get_prefixed_stack_name( $this->get_rokka_full_size_stack_name() ) ) {
if ( self::STACK_SYNC_OPERATION_CREATE === $stack['operation'] ) {
$this->create_noop_stack( $stack['name'] );
}
Expand Down Expand Up @@ -555,13 +555,14 @@ public function get_stacks_to_sync() {
* @var \Rokka\Client\Core\Stack[] $stacks_on_rokka
*/
$stacks_on_rokka = array_filter( $stack_collection->getStacks(), function ( $stack ) {
// filter out all non prefixed stacks
return substr( $stack->name, 0, strlen( $this->get_stack_prefix() ) ) === $this->get_stack_prefix();
} );

$stacks_to_sync = array();

// Create a noop stack (full size stack) if it not exists already
$noop_stackname = $this->get_stack_prefix() . $this->get_rokka_full_size_stack_name();
$noop_stackname = $this->get_prefixed_stack_name( $this->get_rokka_full_size_stack_name() );
try {
$client->getStack( $noop_stackname );
$stacks_to_sync[ $noop_stackname ] = array(
Expand All @@ -585,7 +586,7 @@ public function get_stacks_to_sync() {
$width = $size[0];
$height = $size[1];
$crop = $size[2];
$prefixed_name = $this->get_stack_prefix() . $name;
$prefixed_name = $this->get_prefixed_stack_name( $name );
$stack_already_on_rokka = false;

// loop through all stacks which are already on rokka
Expand Down Expand Up @@ -647,12 +648,12 @@ public function get_stacks_to_sync() {
if ( ! empty( $stacks_on_rokka ) && ! empty( $sizes ) ) {
foreach ( $stacks_on_rokka as $stack ) {
// full size stack should never be deleted
if ( $stack->name === $this->get_stack_prefix() . $this->get_rokka_full_size_stack_name() ) {
if ( $stack->name === $this->get_prefixed_stack_name( $this->get_rokka_full_size_stack_name() ) ) {
continue;
}
$stack_still_exists_in_wp = false;
foreach ( $sizes as $name => $size ) {
$prefixed_name = $this->get_stack_prefix() . $name;
$prefixed_name = $this->get_prefixed_stack_name( $name );
if ( $stack->name === $prefixed_name ) {
$stack_still_exists_in_wp = true;
break;
Expand Down Expand Up @@ -745,9 +746,8 @@ public function get_rokka_full_size_stack_name() {
*/
public function get_rokka_url( $hash, $filename, $size = 'thumbnail' ) {
if ( is_array( $size ) ) {
$stack = null;

// if size is requested as width / height array -> find matching or nearest rokka size
$stack = null;
$rokka_sizes = $this->get_available_image_sizes();
foreach ( $rokka_sizes as $size_name => $size_values ) {
if ( $size[0] <= $size_values[0] ) {
Expand All @@ -765,7 +765,17 @@ public function get_rokka_url( $hash, $filename, $size = 'thumbnail' ) {
// use fallback image name if empty
$filename = 'image.jpg';
}
return $this->get_rokka_scheme() . '://' . $this->get_rokka_domain() . '/' . $this->get_stack_prefix() . $stack . '/' . $hash . '/' . $this->sanitize_rokka_filename( $filename );
return $this->get_rokka_scheme() . '://' . $this->get_rokka_domain() . '/' . $this->get_prefixed_stack_name( $stack ) . '/' . $hash . '/' . $this->sanitize_rokka_filename( $filename );
}

/**
* Returns prefixes stack name
*
* @param string $stack_name Stack name without prefix.
* @return string Prefixed stack name.
*/
public function get_prefixed_stack_name( $stack_name ) {
return $this->get_stack_prefix() . $stack_name;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/class-rokka-integration-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public function settings_page() {
}
?>
<tr class="<?php echo esc_attr( $stack['operation'] ); ?>">
<?php if ( $this->rokka_helper->get_stack_prefix() . $this->rokka_helper->get_rokka_full_size_stack_name() === $stack['name'] || Rokka_Helper::STACK_SYNC_OPERATION_DELETE === $stack['operation'] ) : ?>
<?php if ( $this->rokka_helper->get_prefixed_stack_name( $this->rokka_helper->get_rokka_full_size_stack_name() ) === $stack['name'] || Rokka_Helper::STACK_SYNC_OPERATION_DELETE === $stack['operation'] ) : ?>
<td><?php echo esc_html( $stack['name'] ); ?></td>
<td>-</td>
<td>-</td>
Expand Down

0 comments on commit c64fa2c

Please sign in to comment.