PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.6.31
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.6.31
1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 1.8.0 All 210 releases
charitable / includes / api / class-charitable-api-route-reports.php

class-charitable-api-route-reports.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.6.31, at includes/api/class-charitable-api-route-reports.php

156 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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) 2020, 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::__construct();
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(
94 array(
95 'slug' => 'donations',
96 'description' => __( 'List of donation reports.', 'charitable' ),
97 '_links' => array(
98 'self' => array(
99 'href' => get_site_url( null, '/wp-json/' . $this->namespace . '/' . $this->base . '/donations/' ),
100 ),
101 ),
102 )
103 );
104 }
105
106 /**
107 * Return a response with the donations report.
108 *
109 * @since 1.6.0
110 *
111 * @param WP_REST_Request $request The API request object.
112 * @return WP_REST_Response|mixed If response generated an error, WP_Error, if response
113 * is already an instance, WP_HTTP_Response, otherwise
114 * returns a new WP_REST_Response instance.
115 */
116 public function get_donations_report( $request ) {
117 $report = new Charitable_Donation_Report(
118 array(
119 'campaigns' => $request->get_param( 'campaigns' ),
120 )
121 );
122
123 return rest_ensure_response( $report->get_reports() );
124 }
125
126 /**
127 * Validate the 'campaigns' argument.
128 *
129 * @since 1.6.0
130 *
131 * @param mixed $param The parameter value.
132 * @return boolean
133 */
134 public function validate_campaigns_arg( $param ) {
135 return 'all' == $param || is_numeric( $param ) || is_array( $param );
136 }
137
138 /**
139 * Sanitize the 'campaigns' argument, ensuring either 'all' or an array is returned.
140 *
141 * @since 1.6.0
142 *
143 * @param mixed $param The parameter value.
144 * @return string|array
145 */
146 public function sanitize_campaigns_arg( $param ) {
147 if ( 'all' == $param || is_array( $param ) ) {
148 return $param;
149 }
150
151 return array( $param );
152 }
153 }
154
155 endif;
156