'GET', 'callback' => array( $this, 'get_history' ), 'permission_callback' => array( $this, 'permission_check' ), 'args' => array( 'limit' => array( 'description' => __( 'How many jobs to return.', 'transtrix' ), 'type' => 'integer', 'default' => 20, 'sanitize_callback' => 'absint', 'validate_callback' => static function ( $value ) { return (int) $value >= 1 && (int) $value <= 50; }, ), ), ), ) ); } /** * The most recent jobs. * * @param WP_REST_Request $request The request. * @return \WP_REST_Response */ public function get_history( WP_REST_Request $request ) { Job_Store::collect_garbage( array( Tasks::class, 'for_job' ) ); $records = Job_Store::recent( (int) $request['limit'] ); if ( ! $this->can_access_full_site() ) { $records = array_values( array_filter( $records, function ( $record ) { return 'full-site' !== ( $record['kind'] ?? '' ) && $this->owns_job( $record ); } ) ); } $show_path = $this->can_access_full_site(); foreach ( $records as &$record ) { $package_id = (string) ( $record['package_id'] ?? '' ); $path = '' === $package_id ? '' : Package_Store::relative_path( $package_id ); // The path names the secret package folder; on a server that ignores // .htaccess it is a way in. Only an administrator sees it. $record['stored'] = '' !== $path; $record['package_path'] = $show_path ? $path : ''; if ( $record['stored'] ) { $record['package_name'] = Package_Store::display_name( $record ); // Runs recorded before size was kept on every job. if ( empty( $record['size'] ) ) { $record['size'] = Job_Store::package_size( $package_id ); } } } unset( $record ); return rest_ensure_response( $records ); } }