| 1 |
<?php |
| 2 |
namespace CatFolders\Rest\Controllers; |
| 3 |
|
| 4 |
use CatFolders\Internals\Modules\MediaMeta; |
| 5 |
use CatFolders\Classes\Helpers; |
| 6 |
|
| 7 |
class MetaController { |
| 8 |
public function register_routes() { |
| 9 |
register_rest_route( |
| 10 |
CATF_ROUTE_NAMESPACE, |
| 11 |
'/generate-sizes', |
| 12 |
array( |
| 13 |
array( |
| 14 |
'methods' => \WP_REST_Server::CREATABLE, |
| 15 |
'callback' => array( $this, 'generate_size_api' ), |
| 16 |
'permission_callback' => array( $this, 'permission_callback' ), |
| 17 |
), |
| 18 |
) |
| 19 |
); |
| 20 |
} |
| 21 |
|
| 22 |
public function permission_callback() { |
| 23 |
return current_user_can( 'upload_files' ); |
| 24 |
} |
| 25 |
|
| 26 |
public function generate_size_api( \WP_REST_Request $request ) { |
| 27 |
if( ! current_user_can( 'manage_options' ) ) { |
| 28 |
return new \WP_Error( 403, __( 'You are not allowed to generate sizes.', 'catfolders' ) ); |
| 29 |
} |
| 30 |
$page = intval( $request->get_param( 'page' ) ); |
| 31 |
if ( $page < 1 ) { |
| 32 |
$page = 1; |
| 33 |
} |
| 34 |
|
| 35 |
$result = array(); |
| 36 |
|
| 37 |
$args = array( |
| 38 |
'post_type' => 'attachment', |
| 39 |
'posts_per_page' => 50, |
| 40 |
'post_status' => 'inherit', |
| 41 |
'fields' => 'ids', |
| 42 |
'paged' => $page, |
| 43 |
); |
| 44 |
|
| 45 |
$query = new \WP_Query( $args ); |
| 46 |
$ids = $query->posts; |
| 47 |
|
| 48 |
wp_reset_postdata(); |
| 49 |
|
| 50 |
if ( is_array( $ids ) && count( $ids ) > 0 ) { |
| 51 |
foreach ( $ids as $id ) { |
| 52 |
$bytes = Helpers::get_bytes( $id ); |
| 53 |
if ( $bytes ) { |
| 54 |
update_post_meta( $id, MediaMeta::SIZE_KEY, $bytes ); |
| 55 |
} |
| 56 |
} |
| 57 |
$result['success'] = true; |
| 58 |
$result['next'] = '1'; |
| 59 |
} else { |
| 60 |
$result['success'] = true; |
| 61 |
$result['next'] = '0'; |
| 62 |
} |
| 63 |
return new \WP_REST_Response( $result ); |
| 64 |
} |
| 65 |
} |
| 66 |
|