| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\API\Endpoints\Logs; |
| 4 |
|
| 5 |
use WP_REST_Request; |
| 6 |
use WP_REST_Response; |
| 7 |
use Give\Log\LogRepository; |
| 8 |
use Give\Log\ValueObjects\LogType; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class GetLogs |
| 12 |
* @package Give\API\Endpoints\Logs |
| 13 |
* |
| 14 |
* @since 2.10.0 |
| 15 |
*/ |
| 16 |
class GetLogs extends Endpoint { |
| 17 |
|
| 18 |
/** @var string */ |
| 19 |
protected $endpoint = 'logs/get-logs'; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var LogRepository |
| 23 |
*/ |
| 24 |
private $logRepository; |
| 25 |
|
| 26 |
/** |
| 27 |
* GetLogs constructor. |
| 28 |
* |
| 29 |
* @param LogRepository $repository |
| 30 |
*/ |
| 31 |
public function __construct( LogRepository $repository ) { |
| 32 |
$this->logRepository = $repository; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @inheritDoc |
| 37 |
*/ |
| 38 |
public function registerRoute() { |
| 39 |
register_rest_route( |
| 40 |
'give-api/v2', |
| 41 |
$this->endpoint, |
| 42 |
[ |
| 43 |
[ |
| 44 |
'methods' => 'GET', |
| 45 |
'callback' => [ $this, 'handleRequest' ], |
| 46 |
'permission_callback' => [ $this, 'permissionsCheck' ], |
| 47 |
'args' => [ |
| 48 |
'page' => [ |
| 49 |
'validate_callback' => function( $param ) { |
| 50 |
return filter_var( $param, FILTER_VALIDATE_INT ); |
| 51 |
}, |
| 52 |
'default' => '1', |
| 53 |
], |
| 54 |
'type' => [ |
| 55 |
'validate_callback' => function( $param ) { |
| 56 |
if ( empty( $param ) || ( 'all' === $param ) ) { |
| 57 |
return true; |
| 58 |
} |
| 59 |
return LogType::isValid( $param ); |
| 60 |
}, |
| 61 |
'default' => 'all', |
| 62 |
], |
| 63 |
'category' => [ |
| 64 |
'validate_callback' => function( $param ) { |
| 65 |
return is_string( $param ); |
| 66 |
}, |
| 67 |
'default' => '', |
| 68 |
], |
| 69 |
'source' => [ |
| 70 |
'validate_callback' => function( $param ) { |
| 71 |
return is_string( $param ); |
| 72 |
}, |
| 73 |
'default' => '', |
| 74 |
], |
| 75 |
'sort' => [ |
| 76 |
'validate_callback' => function( $param ) { |
| 77 |
if ( empty( $param ) ) { |
| 78 |
return true; |
| 79 |
} |
| 80 |
return in_array( $param, $this->logRepository->getSortableColumns(), true ); |
| 81 |
}, |
| 82 |
'default' => 'id', |
| 83 |
], |
| 84 |
'direction' => [ |
| 85 |
'validate_callback' => function( $param ) { |
| 86 |
if ( empty( $param ) ) { |
| 87 |
return true; |
| 88 |
} |
| 89 |
return in_array( strtoupper( $param ), [ 'ASC', 'DESC' ], true ); |
| 90 |
}, |
| 91 |
'default' => 'DESC', |
| 92 |
], |
| 93 |
], |
| 94 |
], |
| 95 |
'schema' => [ $this, 'getSchema' ], |
| 96 |
] |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @return array |
| 102 |
*/ |
| 103 |
public function getSchema() { |
| 104 |
return [ |
| 105 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 106 |
'title' => 'logs', |
| 107 |
'type' => 'object', |
| 108 |
'properties' => [ |
| 109 |
'page' => [ |
| 110 |
'type' => 'integer', |
| 111 |
'description' => esc_html__( 'Current page', 'give' ), |
| 112 |
], |
| 113 |
'type' => [ |
| 114 |
'type' => 'string', |
| 115 |
'description' => esc_html__( 'Log type', 'give' ), |
| 116 |
], |
| 117 |
'category' => [ |
| 118 |
'type' => 'string', |
| 119 |
'description' => esc_html__( 'Log category', 'give' ), |
| 120 |
], |
| 121 |
'source' => [ |
| 122 |
'type' => 'string', |
| 123 |
'description' => esc_html__( 'Log source', 'give' ), |
| 124 |
], |
| 125 |
'direction' => [ |
| 126 |
'type' => 'string', |
| 127 |
'description' => esc_html__( 'Sort direction', 'give' ), |
| 128 |
], |
| 129 |
'sort' => [ |
| 130 |
'type' => 'string', |
| 131 |
'description' => esc_html__( 'Sort by column', 'give' ), |
| 132 |
], |
| 133 |
], |
| 134 |
]; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* @param WP_REST_Request $request |
| 139 |
* |
| 140 |
* @return WP_REST_Response |
| 141 |
*/ |
| 142 |
public function handleRequest( WP_REST_Request $request ) { |
| 143 |
$data = []; |
| 144 |
$logs = $this->logRepository->getLogsForRequest( $request ); |
| 145 |
$total = $this->logRepository->getLogCountForRequest( $request ); |
| 146 |
|
| 147 |
foreach ( $logs as $log ) { |
| 148 |
$data[] = [ |
| 149 |
'id' => $log->getId(), |
| 150 |
'log_type' => $log->getType(), |
| 151 |
'category' => $log->getCategory(), |
| 152 |
'source' => $log->getSource(), |
| 153 |
'message' => $log->getMessage(), |
| 154 |
'context' => $log->getContext(), |
| 155 |
'date' => $log->getDate(), |
| 156 |
]; |
| 157 |
} |
| 158 |
|
| 159 |
return new WP_REST_Response( |
| 160 |
[ |
| 161 |
'status' => true, |
| 162 |
'data' => $data, |
| 163 |
'pages' => ceil( $total / $this->logRepository->getLogsPerPageLimit() ), |
| 164 |
'categories' => $this->logRepository->getCategories(), |
| 165 |
'sources' => $this->logRepository->getSources(), |
| 166 |
'statuses' => LogType::getTypesTranslated(), |
| 167 |
] |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
} |
| 172 |
|