PluginProbe
Authorsy – Author Box, Multiple Authors, Guest Authors & Post Rating / trunk
Authorsy – Author Box, Multiple Authors, Guest Authors & Post Rating vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8
authorsy / base / api.php

api.php in Authorsy – Author Box, Multiple Authors, Guest Authors & Post Rating trunk, at base/api.php

86 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Abstract api class
4 *
5 * @package Authorsy
6 */
7 namespace Authorsy\Base;
8 defined( 'ABSPATH' ) || exit;
9 use WP_Error;
10
11 /**
12 * Abstract class Api.
13 *
14 * @since 1.0.0
15 */
16 abstract class Api extends \WP_REST_Controller {
17
18 /**
19 * Api Constructor function.
20 *
21 * @since 1.0.0
22 *
23 * @return void
24 */
25 public function __construct() {
26 add_action( 'rest_api_init', [ $this, 'register_routes' ] );
27 }
28
29 /**
30 * Register routes
31 *
32 * @since 1.0.0
33 *
34 * @return void
35 */
36 public function register_routes() {
37 // Need to override the function on child class.
38 }
39
40 /**
41 * Validate input fields.
42 *
43 * @since 1.0.0
44 *
45 * @param array $data Accepts data in array format.
46 * @param array $fields Optional. Default empty array.
47 *
48 * @return bool | WP_error
49 */
50 public function validate( $data, $fields = [] ) {
51 $error = new WP_Error();
52
53 foreach ( $fields as $field ) {
54 if ( empty( $data[ $field ] ) ) {
55 /* translators: Error Message */
56 $error->add( $field . '_error', sprintf( esc_html__( '%s can\'t be empty', 'authorsy' ), $field ) );
57 }
58 }
59
60 if ( ! empty( $error->errors ) ) {
61 return $error;
62 }
63
64 return true;
65 }
66
67
68 /**
69 * Verify nonce
70 *
71 * @param WP_Rest_Request $request
72 *
73 * @return bool|\WP_Error
74 */
75 public static function verify_nonce($request) {
76 $nonce = $request->get_header('X-WP-Nonce');
77
78 if (!wp_verify_nonce($nonce, 'wp_rest')) {
79 return new \WP_Error('invalid_nonce', __('Invalid nonce.', 'authorsy'), ['status' => 403]);
80 }
81
82 return true;
83 }
84
85 }
86