-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from uploadcare/feature/download-improve
Downloading images independently of the current project to Uploadcare.
- Loading branch information
Showing
36 changed files
with
9,681 additions
and
9,791 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ composer.lock | |
vendor | ||
node_modules | ||
compiled-js/* | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,110 @@ | ||
<?php declare(strict_types=1); | ||
<?php declare( strict_types=1 ); | ||
|
||
class MediaDataLoader | ||
{ | ||
class MediaDataLoader { | ||
public const POST_PER_PAGE = 20; | ||
|
||
public function loadMedia(int $page = 1, int $postPerPage = null): iterable | ||
{ | ||
$params = $this->imagesQueryParams($page, $postPerPage ?? self::POST_PER_PAGE); | ||
public function loadMedia( int $page = 1, int $postPerPage = null ): iterable { | ||
$params = $this->imagesQueryParams( $page, $postPerPage ?? self::POST_PER_PAGE ); | ||
|
||
return $this->query($params); | ||
return $this->query( $params ); | ||
} | ||
|
||
public function loadMediaForPost(int $postId): iterable | ||
{ | ||
$parameters = $this->imagesQueryParams(1, -1); | ||
public function loadMediaForPost( int $postId ): iterable { | ||
$parameters = $this->imagesQueryParams( 1, - 1 ); | ||
$parameters['post_parent'] = $postId; | ||
|
||
return $this->query($parameters); | ||
return $this->query( $parameters ); | ||
} | ||
|
||
public function countMediaForPost(int $postId): array | ||
{ | ||
$parameters = $this->imagesQueryParams(1, -1); | ||
public function countMediaForPost( int $postId ): array { | ||
$parameters = $this->imagesQueryParams( 1, - 1 ); | ||
$parameters['post_parent'] = $postId; | ||
$totalCount = (new \WP_Query($parameters))->found_posts; | ||
$totalCount = ( new \WP_Query( $parameters ) )->found_posts; | ||
|
||
$parameters['meta_query'] = [ | ||
'relation' => 'AND', | ||
[ | ||
'key' => 'uploadcare_uuid', | ||
'key' => 'uploadcare_uuid', | ||
'compare' => 'EXISTS', | ||
], | ||
[ | ||
'key' => 'uploadcare_uuid', | ||
'value' => null, | ||
'key' => 'uploadcare_uuid', | ||
'value' => null, | ||
'compare' => '!=', | ||
], | ||
]; | ||
$ucCount = (new \WP_Query($parameters))->found_posts; | ||
$ucCount = ( new \WP_Query( $parameters ) )->found_posts; | ||
|
||
return [ | ||
'total' => $totalCount, | ||
'total' => $totalCount, | ||
'uploadcare' => $ucCount, | ||
]; | ||
} | ||
|
||
protected function query(array $params): iterable | ||
{ | ||
$query = new \WP_Query($params); | ||
protected function query( array $params ): iterable { | ||
$query = new \WP_Query( $params ); | ||
$result = []; | ||
|
||
if (!$query->have_posts()) { | ||
if ( ! $query->have_posts() ) { | ||
return $result; | ||
} | ||
|
||
foreach ($query->posts as $post) { | ||
foreach ( $query->posts as $post ) { | ||
$result[] = $post; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function countImageType(bool $local = false): int | ||
{ | ||
$parameters = $this->imagesQueryParams(1, -1); | ||
$metaQuery = [ | ||
public function countImageType( bool $local = false ): int { | ||
$parameters = $this->imagesQueryParams( 1, - 1 ); | ||
$metaQuery = [ | ||
'relation' => 'AND', | ||
[ | ||
'key' => 'uploadcare_uuid', | ||
'key' => 'uploadcare_uuid', | ||
'compare' => 'EXISTS', | ||
], | ||
[ | ||
'key' => 'uploadcare_uuid', | ||
'value' => null, | ||
'key' => 'uploadcare_uuid', | ||
'value' => null, | ||
'compare' => '!=', | ||
], | ||
]; | ||
|
||
if ($local === true) { | ||
if ( $local === true ) { | ||
$metaQuery = [ | ||
'relation' => 'OR', | ||
[ | ||
'key' => 'uploadcare_uuid', | ||
'key' => 'uploadcare_uuid', | ||
'compare' => 'NOT EXISTS', | ||
'value' => '', | ||
'value' => '', | ||
], | ||
[ | ||
'key' => 'uploadcare_uuid', | ||
'value' => null, | ||
'key' => 'uploadcare_uuid', | ||
'value' => null, | ||
'compare' => '=', | ||
], | ||
]; | ||
} | ||
$parameters['meta_query'] = $metaQuery; | ||
|
||
return (new \WP_Query($parameters))->found_posts; | ||
return ( new \WP_Query( $parameters ) )->found_posts; | ||
} | ||
|
||
public function getCount(): int | ||
{ | ||
return (new \WP_Query(['post_type' => 'attachment', 'post_status' => 'any', 'posts_per_page' => -1]))->found_posts; | ||
public function getCount(): int { | ||
return ( new \WP_Query( [ 'post_type' => 'attachment', | ||
'post_status' => 'any', | ||
'posts_per_page' => - 1 | ||
] ) )->found_posts; | ||
} | ||
|
||
protected function imagesQueryParams(int $page, int $postPerPage): array | ||
{ | ||
protected function imagesQueryParams( int $page, int $postPerPage ): array { | ||
return [ | ||
'post_type' => 'attachment', | ||
'post_type' => 'attachment', | ||
'posts_per_page' => $postPerPage, | ||
'paged' => $page, | ||
'post_status' => 'any', | ||
'paged' => $page, | ||
'post_status' => 'any', | ||
]; | ||
} | ||
} | ||
|
Oops, something went wrong.