| 1 |
<?php |
| 2 |
/** |
| 3 |
* Sets up the /reports/ API route. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_API_Route_Reports |
| 6 |
* @author Eric Daams |
| 7 |
* @copyright Copyright (c) 2018, Studio 164a |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.6.0 |
| 10 |
* @version 1.6.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_API_Route_Reports' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable_API_Route_Reports |
| 22 |
* |
| 23 |
* @since 1.6.0 |
| 24 |
*/ |
| 25 |
class Charitable_API_Route_Reports extends Charitable_API_Route { |
| 26 |
|
| 27 |
/** |
| 28 |
* Route base. |
| 29 |
* |
| 30 |
* @since 1.6.0 |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $base; |
| 35 |
|
| 36 |
/** |
| 37 |
* Set up class instance. |
| 38 |
* |
| 39 |
* @since 1.6.0 |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public function __construct() { |
| 44 |
parent::__constrct(); |
| 45 |
|
| 46 |
$this->base = 'reports'; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Register the routes for this controller. |
| 51 |
* |
| 52 |
* @since 1.6.0 |
| 53 |
*/ |
| 54 |
public function register_routes() { |
| 55 |
register_rest_route( |
| 56 |
$this->namespace, |
| 57 |
'/' . $this->base, |
| 58 |
array( |
| 59 |
'methods' => WP_REST_Server::READABLE, |
| 60 |
'callback' => array( $this, 'get_reports' ), |
| 61 |
'permission_callback' => array( $this, 'user_can_get_charitable_reports' ), |
| 62 |
) |
| 63 |
); |
| 64 |
|
| 65 |
register_rest_route( |
| 66 |
$this->namespace, |
| 67 |
'/' . $this->base . '/donations/', |
| 68 |
array( |
| 69 |
'methods' => WP_REST_Server::READABLE, |
| 70 |
'callback' => array( $this, 'get_donations_report' ), |
| 71 |
'permission_callback' => array( $this, 'user_can_get_charitable_reports' ), |
| 72 |
'args' => array( |
| 73 |
'campaigns' => array( |
| 74 |
'default' => 'all', |
| 75 |
'validate_callback' => array( $this, 'validate_campaigns_arg' ), |
| 76 |
'sanitize_callback' => array( $this, 'sanitize_campaigns_arg' ), |
| 77 |
), |
| 78 |
), |
| 79 |
) |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Return a response with all registered reports. |
| 85 |
* |
| 86 |
* @since 1.6.0 |
| 87 |
* |
| 88 |
* @return WP_REST_Response|mixed If response generated an error, WP_Error, if response |
| 89 |
* is already an instance, WP_HTTP_Response, otherwise |
| 90 |
* returns a new WP_REST_Response instance. |
| 91 |
*/ |
| 92 |
public function get_reports() { |
| 93 |
return rest_ensure_response( array( |
| 94 |
'slug' => 'donations', |
| 95 |
'description' => __( 'List of donation reports.', 'charitable' ), |
| 96 |
'_links' => array( |
| 97 |
'self' => array( |
| 98 |
'href' => get_site_url( null, '/wp-json/' . $this->namespace . '/' . $this->base . '/donations/' ), |
| 99 |
), |
| 100 |
), |
| 101 |
) ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Return a response with the donations report. |
| 106 |
* |
| 107 |
* @since 1.6.0 |
| 108 |
* |
| 109 |
* @param WP_REST_Request $request The API request object. |
| 110 |
* @return WP_REST_Response|mixed If response generated an error, WP_Error, if response |
| 111 |
* is already an instance, WP_HTTP_Response, otherwise |
| 112 |
* returns a new WP_REST_Response instance. |
| 113 |
*/ |
| 114 |
public function get_donations_report( $request ) { |
| 115 |
$report = new Charitable_Donation_Report( array( |
| 116 |
'campaigns' => $request->get_param( 'campaigns' ), |
| 117 |
) ); |
| 118 |
|
| 119 |
return rest_ensure_response( $report->get_reports() ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Validate the 'campaigns' argument. |
| 124 |
* |
| 125 |
* @since 1.6.0 |
| 126 |
* |
| 127 |
* @param mixed $param The parameter value. |
| 128 |
* @return boolean |
| 129 |
*/ |
| 130 |
public function validate_campaigns_arg( $param ) { |
| 131 |
return 'all' == $param || is_numeric( $param ) || is_array( $param ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Sanitize the 'campaigns' argument, ensuring either 'all' or an array is returned. |
| 136 |
* |
| 137 |
* @since 1.6.0 |
| 138 |
* |
| 139 |
* @param mixed $param The parameter value. |
| 140 |
* @return string|array |
| 141 |
*/ |
| 142 |
public function sanitize_campaigns_arg( $param ) { |
| 143 |
if ( 'all' == $param || is_array( $param ) ) { |
| 144 |
return $param; |
| 145 |
} |
| 146 |
|
| 147 |
return array( $param ); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
endif; |
| 152 |
|