request = $request; return is_user_logged_in() && current_user_can( 'edit_posts' ); } public function register_routes() { $this->get( $this->endpoint, [ $this, 'search' ] ); } public function search() { $term = trim( (string) $this->get_param( 'q', '' ) ); $limit = min( self::MAX_LIMIT, max( 1, (int) $this->get_param( 'limit', 24, 'absint' ) ) ); // Below two characters every query matches half the catalog, which costs a // cloud round trip to tell the user nothing. if ( mb_strlen( $term ) < 2 ) { return $this->success( [ 'patterns' => [], 'term' => $term ] ); } $sync = PatternSync::get_instance(); if ( null === $sync->plan_key() ) { return $this->success( [ 'patterns' => [], 'term' => $term ] ); } $registrar = PatternRegistrar::get_instance(); $patterns = []; foreach ( $sync->search( $term, $limit ) as $item ) { $patterns[] = $registrar->describe_item( $item ); } return $this->success( [ 'patterns' => $patterns, 'term' => $term ] ); } }