'GET', 'permission_callback' => array( $this, 'can_manage' ), 'callback' => array( $this, 'store' ) ) ); register_rest_route( Routes::NS, '/admin/store/refresh', array( 'methods' => 'POST', 'permission_callback' => array( $this, 'can_manage' ), 'callback' => array( $this, 'refresh' ) ) ); register_rest_route( Routes::NS, '/admin/help/kb', array( 'methods' => 'GET', 'permission_callback' => array( $this, 'can_manage' ), 'callback' => array( $this, 'kb' ) ) ); register_rest_route( Routes::NS, '/admin/help/kb/(?P[a-z0-9_-]+)', array( 'methods' => 'GET', 'permission_callback' => array( $this, 'can_manage' ), 'callback' => array( $this, 'article' ) ) ); register_rest_route( Routes::NS, '/admin/help/contact', array( 'methods' => 'POST', 'permission_callback' => array( $this, 'can_manage' ), 'callback' => array( $this, 'contact' ) ) ); } public function store(): \WP_REST_Response { return new \WP_REST_Response( Store::payload() ); } /** Lets Pro re-read its license and live catalog. With Core alone there is nothing to refresh. */ public function refresh(): \WP_REST_Response { do_action( 'dashbread_store_refresh' ); return new \WP_REST_Response( Store::payload() ); } public function kb(): \WP_REST_Response { return new \WP_REST_Response( Store::kb() + array( 'help_url' => Store::site_url() . '/help', 'forum_url' => self::FORUM_URL, 'pro' => Features::pro_active(), 'contact' => (bool) has_filter( 'dashbread_help_contact' ), 'shortcodes' => \Dashbread\Core\Shortcodes::reference(), ) ); } public function article( \WP_REST_Request $r ): \WP_REST_Response|\WP_Error { $a = Store::kb_article( (string) $r['slug'] ); return $a ? new \WP_REST_Response( $a ) : new \WP_Error( 'dashbread_kb', __( 'That guide is not available.', 'dashbread' ), array( 'status' => 404 ) ); } public function contact( \WP_REST_Request $r ): \WP_REST_Response|\WP_Error { $in = (array) $r->get_json_params(); $subject = sanitize_text_field( (string) ( $in['subject'] ?? '' ) ); $message = sanitize_textarea_field( (string) ( $in['message'] ?? '' ) ); if ( '' === trim( $message ) ) { return new \WP_Error( 'dashbread_support', __( 'Tell us what is going on first.', 'dashbread' ), array( 'status' => 400 ) ); } /** * Deliver a support message (Dashbread Pro sends it to dashbread.com). Return an array such as * array( 'ticket' => 'S-1042' ) on success or a WP_Error; leave null when not handled. * * @param array|\WP_Error|null $result * @param string $subject * @param string $message */ $result = apply_filters( 'dashbread_help_contact', null, $subject, $message ); if ( is_wp_error( $result ) ) { return $result; } if ( ! is_array( $result ) ) { return new \WP_Error( 'dashbread_support_forum', __( 'Free support for Dashbread is on the WordPress.org forum.', 'dashbread' ), array( 'status' => 403, 'forum_url' => self::FORUM_URL ) ); } return new \WP_REST_Response( array( 'ok' => true, 'ticket' => (string) ( $result['ticket'] ?? '' ) ) ); } }