PluginProbe
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification / trunk
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification vtrunk
5.11.0 5.10.0 5.9.0 5.8.0 5.8.1 5.7.0 5.6.2 5.6.3 5.6.1 5.6.0 5.5.0 5.4.0 5.3.2 5.3.1 5.1.6 5.1.5 trunk 2.1.5 2.11 2.12 2.13 2.15 3.0.0 3.0.1 3.0.2 All 44 releases
double-opt-in / src / Help / HelpRestController.php

HelpRestController.php in Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification trunk, at src/Help/HelpRestController.php

101 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST routes for the in-plugin help.
4 *
5 * `GET f12-doi/v1/help` table of contents
6 * `GET f12-doi/v1/help/<id>` one article rendered to HTML
7 *
8 * Both require `manage_options`; the REST cookie nonce is the CSRF protection.
9 * Read-only, nothing is written.
10 *
11 * @package Forge12\DoubleOptIn\Help
12 * @since 5.11.0
13 */
14
15 declare( strict_types=1 );
16
17 namespace Forge12\DoubleOptIn\Help;
18
19 if ( ! defined( 'ABSPATH' ) ) {
20 exit;
21 }
22
23 class HelpRestController {
24
25 public const API_NAMESPACE = 'f12-doi/v1';
26
27 /** @var HelpSourceLocator */
28 private $locator;
29
30 public function __construct( HelpSourceLocator $locator ) {
31 $this->locator = $locator;
32 }
33
34 public function init(): void {
35 add_action( 'rest_api_init', array( $this, 'registerRoutes' ) );
36 }
37
38 public function checkPermission(): bool {
39 return current_user_can( 'manage_options' );
40 }
41
42 public function registerRoutes(): void {
43 register_rest_route(
44 self::API_NAMESPACE,
45 '/help',
46 array(
47 'methods' => \WP_REST_Server::READABLE,
48 'callback' => array( $this, 'listArticles' ),
49 'permission_callback' => array( $this, 'checkPermission' ),
50 )
51 );
52 register_rest_route(
53 self::API_NAMESPACE,
54 '/help/(?P<id>[a-z0-9-]+)',
55 array(
56 'methods' => \WP_REST_Server::READABLE,
57 'callback' => array( $this, 'getArticle' ),
58 'permission_callback' => array( $this, 'checkPermission' ),
59 )
60 );
61 }
62
63 public function listArticles(): \WP_REST_Response {
64 return new \WP_REST_Response(
65 array(
66 'success' => true,
67 'data' => array( 'articles' => $this->repository()->listArticles() ),
68 ),
69 200
70 );
71 }
72
73 public function getArticle( \WP_REST_Request $request ): \WP_REST_Response {
74 $article = $this->repository()->getArticle( (string) $request->get_param( 'id' ) );
75 if ( null === $article ) {
76 return new \WP_REST_Response(
77 array(
78 'success' => false,
79 'message' => __( 'This help article does not exist.', 'double-opt-in' ),
80 'code' => 'HELP_NOT_FOUND',
81 ),
82 404
83 );
84 }
85 // The HTML is produced by MarkdownRenderer from files shipped with the
86 // plugins; kses is a second wall in case a shipped file is ever wrong.
87 $article['html'] = wp_kses_post( $article['html'] );
88 return new \WP_REST_Response(
89 array(
90 'success' => true,
91 'data' => $article,
92 ),
93 200
94 );
95 }
96
97 private function repository(): HelpRepository {
98 return new HelpRepository( $this->locator->locate(), determine_locale() );
99 }
100 }
101