class-wp-sweep-admin.php
5 days ago
class-wp-sweep-api.php
2 weeks ago
class-wp-sweep-command.php
2 weeks ago
class-wp-sweep-list-table.php
5 days ago
class-wp-sweep.php
5 days ago
index.php
2 weeks ago
class-wp-sweep-api.php
159 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The REST API routes. |
| 4 | * |
| 5 | * @package WP-Sweep |
| 6 | */ |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | /** |
| 11 | * Exposes count, details and sweep over the REST API. |
| 12 | * |
| 13 | * The namespace is the bare noun `sweep/v1` rather than the plugin slug. A |
| 14 | * `wp-` prefix is a wordpress.org directory convention for keeping one |
| 15 | * plugin's download page apart from another's; it says nothing about what the |
| 16 | * plugin should call the things it registers, and the ecosystem names those |
| 17 | * after the brand rather than the directory entry. It is also the namespace the |
| 18 | * released 1.2.0 shipped, so no client that already talks to WP-Sweep has to be |
| 19 | * edited. Another plugin can claim the same bare noun and WordPress will not |
| 20 | * detect it; that is the accepted trade for a word as distinctive as `sweep`. |
| 21 | */ |
| 22 | class WP_Sweep_API { |
| 23 | |
| 24 | /** |
| 25 | * The REST namespace every route is registered under. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | const REST_NAMESPACE = 'sweep/v1'; |
| 30 | |
| 31 | /** |
| 32 | * Register the routes once the REST API is up. |
| 33 | */ |
| 34 | public function __construct() { |
| 35 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Register the plugin's routes. |
| 40 | * |
| 41 | * @return void |
| 42 | */ |
| 43 | public function register_routes() { |
| 44 | $name = array( |
| 45 | 'name' => array( |
| 46 | 'required' => true, |
| 47 | 'validate_callback' => array( $this, 'is_sweep_name_valid' ), |
| 48 | ), |
| 49 | ); |
| 50 | |
| 51 | register_rest_route( |
| 52 | self::REST_NAMESPACE, |
| 53 | 'count/(?P<name>\w+)', |
| 54 | array( |
| 55 | 'methods' => WP_REST_Server::READABLE, |
| 56 | 'callback' => array( $this, 'count' ), |
| 57 | 'permission_callback' => array( $this, 'permission_check' ), |
| 58 | 'args' => $name, |
| 59 | ) |
| 60 | ); |
| 61 | |
| 62 | register_rest_route( |
| 63 | self::REST_NAMESPACE, |
| 64 | 'details/(?P<name>\w+)', |
| 65 | array( |
| 66 | 'methods' => WP_REST_Server::READABLE, |
| 67 | 'callback' => array( $this, 'details' ), |
| 68 | 'permission_callback' => array( $this, 'permission_check' ), |
| 69 | 'args' => $name, |
| 70 | ) |
| 71 | ); |
| 72 | |
| 73 | register_rest_route( |
| 74 | self::REST_NAMESPACE, |
| 75 | 'sweep/(?P<name>\w+)', |
| 76 | array( |
| 77 | 'methods' => WP_REST_Server::DELETABLE, |
| 78 | 'callback' => array( $this, 'sweep' ), |
| 79 | 'permission_callback' => array( $this, 'permission_check' ), |
| 80 | 'args' => $name, |
| 81 | ) |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * How many items a sweep would remove. |
| 87 | * |
| 88 | * @param WP_REST_Request $request Request. |
| 89 | * @return WP_REST_Response |
| 90 | */ |
| 91 | public function count( $request ) { |
| 92 | $name = $request->get_param( 'name' ); |
| 93 | |
| 94 | return new WP_REST_Response( |
| 95 | array( |
| 96 | 'name' => $name, |
| 97 | 'count' => (int) WP_Sweep::get_instance()->count( $name ), |
| 98 | ) |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * A sample of the items a sweep would remove. |
| 104 | * |
| 105 | * @param WP_REST_Request $request Request. |
| 106 | * @return WP_REST_Response |
| 107 | */ |
| 108 | public function details( $request ) { |
| 109 | $name = $request->get_param( 'name' ); |
| 110 | $details = WP_Sweep::get_instance()->details( $name ); |
| 111 | |
| 112 | return new WP_REST_Response( |
| 113 | array( |
| 114 | 'name' => $name, |
| 115 | 'count' => count( $details ), |
| 116 | 'data' => $details, |
| 117 | ) |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Run a sweep. |
| 123 | * |
| 124 | * @param WP_REST_Request $request Request. |
| 125 | * @return WP_REST_Response |
| 126 | */ |
| 127 | public function sweep( $request ) { |
| 128 | $name = $request->get_param( 'name' ); |
| 129 | $results = WP_Sweep::get_instance()->sweep( $name ); |
| 130 | |
| 131 | return new WP_REST_Response( |
| 132 | array( |
| 133 | 'success' => ! empty( $results ), |
| 134 | 'name' => $name, |
| 135 | 'message' => empty( $results ) ? __( 'No items left to sweep.', 'wp-sweep' ) : $results, |
| 136 | ) |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Whether a sweep name is one this plugin implements. |
| 142 | * |
| 143 | * @param string $name Sweep name. |
| 144 | * @return bool |
| 145 | */ |
| 146 | public function is_sweep_name_valid( $name ) { |
| 147 | return WP_Sweep::get_instance()->is_sweep_name_valid( $name ); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Whether the caller may sweep. |
| 152 | * |
| 153 | * @return bool |
| 154 | */ |
| 155 | public function permission_check() { |
| 156 | return current_user_can( WP_Sweep::capability( 'rest' ) ); |
| 157 | } |
| 158 | } |
| 159 |