'GET', 'callback' => array( $this, 'get_objects' ), 'permission_callback' => array( $this, 'permission_check' ), 'args' => array( 'module_id' => array( 'description' => __( 'Which module the type belongs to.', 'transtrix' ), 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_key', ), 'post_type' => array( 'description' => __( 'The post type to list.', 'transtrix' ), 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_key', ), 'search' => array( 'description' => __( 'Title fragment to match.', 'transtrix' ), 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ), 'status' => array( 'description' => __( 'Limit to one post status. Empty means every exportable status.', 'transtrix' ), 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_key', 'validate_callback' => static function ( $value ) { return '' === $value || in_array( (string) $value, Object_Query::STATUSES, true ); }, ), 'page' => array( 'description' => __( 'Page number, 1 based.', 'transtrix' ), 'type' => 'integer', 'default' => 1, 'sanitize_callback' => 'absint', ), 'per_page' => array( 'description' => __( 'Rows per page.', 'transtrix' ), 'type' => 'integer', 'default' => 20, 'sanitize_callback' => 'absint', 'validate_callback' => static function ( $value ) { return (int) $value >= 1 && (int) $value <= 100; }, ), ), ), ) ); } /** * One page of a type's objects. * * @param WP_REST_Request $request The request. * @return WP_REST_Response|\WP_Error */ public function get_objects( WP_REST_Request $request ) { $module = Plugin::instance()->modules()->get( (string) $request['module_id'] ); if ( null === $module ) { return $this->error( 'transtrix_unknown_module', __( 'That module is not available on this site.', 'transtrix' ), 404 ); } $post_type = (string) $request['post_type']; // A module may only be asked for the types it owns, so the picker cannot // be turned into a reader for arbitrary post types. if ( ! in_array( $post_type, $module->post_types(), true ) ) { return $this->error( 'transtrix_unknown_post_type', __( 'That content type does not belong to this source.', 'transtrix' ), 400 ); } if ( ! $this->can_export_type( $module, $post_type ) ) { return $this->type_forbidden(); } $status = (string) $request['status']; $page = Object_Query::page( array( $post_type ), (string) $request['search'], max( 1, (int) $request['page'] ), (int) $request['per_page'], '' === $status ? Object_Query::STATUSES : array( $status ) ); // The picker shows an id, and the id is a link to the item itself, so a // user can check a row before deciding to take it. $page['rows'] = array_map( static function ( array $row ): array { $row['edit_url'] = admin_url( 'post.php?post=' . (int) $row['id'] . '&action=edit' ); return $row; }, $page['rows'] ); return rest_ensure_response( $page ); } }