PageRenderTime 64ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/buddypress/bp-groups/classes/class-bp-rest-attachments-group-avatar-endpoint.php

https://github.com/livinglab/openlab
PHP | 500 lines | 252 code | 53 blank | 195 comment | 14 complexity | 224fe8b940f76a5361d865ddddddf191 MD5 | raw file
  1. <?php
  2. /**
  3. * BP REST: BP_REST_Attachments_Group_Avatar_Endpoint class
  4. *
  5. * @package BuddyPress
  6. * @since 5.0.0
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. /**
  10. * Group Avatar endpoints.
  11. *
  12. * @since 5.0.0
  13. */
  14. class BP_REST_Attachments_Group_Avatar_Endpoint extends WP_REST_Controller {
  15. use BP_REST_Attachments;
  16. /**
  17. * Reuse some parts of the BP_REST_Groups_Endpoint class.
  18. *
  19. * @since 5.0.0
  20. *
  21. * @var BP_REST_Groups_Endpoint
  22. */
  23. protected $groups_endpoint;
  24. /**
  25. * BP_Attachment_Avatar Instance.
  26. *
  27. * @since 5.0.0
  28. *
  29. * @var BP_Attachment_Avatar
  30. */
  31. protected $avatar_instance;
  32. /**
  33. * Hold the group object.
  34. *
  35. * @since 5.0.0
  36. *
  37. * @var BP_Groups_Group
  38. */
  39. protected $group;
  40. /**
  41. * Group object type.
  42. *
  43. * @since 5.0.0
  44. *
  45. * @var string
  46. */
  47. protected $object = 'group';
  48. /**
  49. * Constructor.
  50. *
  51. * @since 5.0.0
  52. */
  53. public function __construct() {
  54. $this->namespace = bp_rest_namespace() . '/' . bp_rest_version();
  55. $this->rest_base = buddypress()->groups->id;
  56. $this->groups_endpoint = new BP_REST_Groups_Endpoint();
  57. $this->avatar_instance = new BP_Attachment_Avatar();
  58. }
  59. /**
  60. * Register the component routes.
  61. *
  62. * @since 5.0.0
  63. */
  64. public function register_routes() {
  65. register_rest_route(
  66. $this->namespace,
  67. '/' . $this->rest_base . '/(?P<group_id>[\d]+)/avatar',
  68. array(
  69. 'args' => array(
  70. 'group_id' => array(
  71. 'description' => __( 'A unique numeric ID for the Group.', 'buddypress' ),
  72. 'type' => 'integer',
  73. ),
  74. ),
  75. array(
  76. 'methods' => WP_REST_Server::READABLE,
  77. 'callback' => array( $this, 'get_item' ),
  78. 'permission_callback' => array( $this, 'get_item_permissions_check' ),
  79. 'args' => $this->get_item_collection_params(),
  80. ),
  81. array(
  82. 'methods' => WP_REST_Server::CREATABLE,
  83. 'callback' => array( $this, 'create_item' ),
  84. 'permission_callback' => array( $this, 'create_item_permissions_check' ),
  85. ),
  86. array(
  87. 'methods' => WP_REST_Server::DELETABLE,
  88. 'callback' => array( $this, 'delete_item' ),
  89. 'permission_callback' => array( $this, 'delete_item_permissions_check' ),
  90. ),
  91. 'schema' => array( $this, 'get_item_schema' ),
  92. )
  93. );
  94. }
  95. /**
  96. * Fetch an existing group avatar.
  97. *
  98. * @since 5.0.0
  99. *
  100. * @param WP_REST_Request $request Full details about the request.
  101. * @return WP_REST_Response|WP_Error
  102. */
  103. public function get_item( $request ) {
  104. $args = array();
  105. foreach ( array( 'full', 'thumb' ) as $type ) {
  106. $args[ $type ] = bp_core_fetch_avatar(
  107. array(
  108. 'object' => $this->object,
  109. 'type' => $type,
  110. 'item_id' => (int) $this->group->id,
  111. 'html' => (bool) $request->get_param( 'html' ),
  112. 'alt' => $request->get_param( 'alt' ),
  113. )
  114. );
  115. }
  116. // Get the avatar object.
  117. $avatar = $this->get_avatar_object( $args );
  118. if ( ! $avatar->full && ! $avatar->thumb ) {
  119. return new WP_Error(
  120. 'bp_rest_attachments_group_avatar_no_image',
  121. __( 'Sorry, there was a problem fetching this group avatar.', 'buddypress' ),
  122. array(
  123. 'status' => 500,
  124. )
  125. );
  126. }
  127. $retval = array(
  128. $this->prepare_response_for_collection(
  129. $this->prepare_item_for_response( $avatar, $request )
  130. ),
  131. );
  132. $response = rest_ensure_response( $retval );
  133. /**
  134. * Fires after a group avatar is fetched via the REST API.
  135. *
  136. * @since 5.0.0
  137. *
  138. * @param string $avatar The group avatar.
  139. * @param WP_REST_Response $response The response data.
  140. * @param WP_REST_Request $request The request sent to the API.
  141. */
  142. do_action( 'bp_rest_attachments_group_avatar_get_item', $avatar, $response, $request );
  143. return $response;
  144. }
  145. /**
  146. * Checks if a given request has access to get a group avatar.
  147. *
  148. * @since 5.0.0
  149. *
  150. * @param WP_REST_Request $request Full details about the request.
  151. * @return true|WP_Error
  152. */
  153. public function get_item_permissions_check( $request ) {
  154. $retval = new WP_Error(
  155. 'bp_rest_group_invalid_id',
  156. __( 'Invalid group ID.', 'buddypress' ),
  157. array(
  158. 'status' => 404,
  159. )
  160. );
  161. $this->group = $this->groups_endpoint->get_group_object( $request );
  162. if ( false !== $this->group ) {
  163. $retval = true;
  164. }
  165. /**
  166. * Filter the group avatar `get_item` permissions check.
  167. *
  168. * @since 5.0.0
  169. *
  170. * @param true|WP_Error $retval Returned value.
  171. * @param WP_REST_Request $request The request sent to the API.
  172. */
  173. return apply_filters( 'bp_rest_attachments_group_avatar_get_item_permissions_check', $retval, $request );
  174. }
  175. /**
  176. * Upload a group avatar.
  177. *
  178. * @since 5.0.0
  179. *
  180. * @param WP_REST_Request $request Full details about the request.
  181. * @return WP_REST_Response|WP_Error
  182. */
  183. public function create_item( $request ) {
  184. $request->set_param( 'context', 'edit' );
  185. // Get the image file from $_FILES.
  186. $files = $request->get_file_params();
  187. if ( empty( $files ) ) {
  188. return new WP_Error(
  189. 'bp_rest_attachments_group_avatar_no_image_file',
  190. __( 'Sorry, you need an image file to upload.', 'buddypress' ),
  191. array(
  192. 'status' => 500,
  193. )
  194. );
  195. }
  196. // Upload the avatar.
  197. $avatar = $this->upload_avatar_from_file( $files );
  198. if ( is_wp_error( $avatar ) ) {
  199. return $avatar;
  200. }
  201. $retval = array(
  202. $this->prepare_response_for_collection(
  203. $this->prepare_item_for_response( $avatar, $request )
  204. ),
  205. );
  206. $response = rest_ensure_response( $retval );
  207. /**
  208. * Fires after a group avatar is uploaded via the REST API.
  209. *
  210. * @since 5.0.0
  211. *
  212. * @param stdClass $avatar The group avatar object.
  213. * @param WP_REST_Response $response The response data.
  214. * @param WP_REST_Request $request The request sent to the API.
  215. */
  216. do_action( 'bp_rest_attachments_group_avatar_create_item', $avatar, $response, $request );
  217. return $response;
  218. }
  219. /**
  220. * Checks if a given request has access to upload a group avatar.
  221. *
  222. * @since 5.0.0
  223. *
  224. * @param WP_REST_Request $request Full details about the request.
  225. * @return true|WP_Error
  226. */
  227. public function create_item_permissions_check( $request ) {
  228. $retval = $this->get_item_permissions_check( $request );
  229. if ( ! is_wp_error( $retval ) ) {
  230. if ( bp_disable_group_avatar_uploads() || false === buddypress()->avatar->show_avatars ) {
  231. $retval = new WP_Error(
  232. 'bp_rest_attachments_group_avatar_disabled',
  233. __( 'Sorry, group avatar upload is disabled.', 'buddypress' ),
  234. array(
  235. 'status' => 500,
  236. )
  237. );
  238. } elseif ( groups_is_user_admin( bp_loggedin_user_id(), $this->group->id ) || current_user_can( 'bp_moderate' ) ) {
  239. $retval = true;
  240. } else {
  241. $retval = new WP_Error(
  242. 'bp_rest_authorization_required',
  243. __( 'Sorry, you are not authorized to perform this action.', 'buddypress' ),
  244. array(
  245. 'status' => rest_authorization_required_code(),
  246. )
  247. );
  248. }
  249. }
  250. /**
  251. * Filter the group avatar `create_item` permissions check.
  252. *
  253. * @since 5.0.0
  254. *
  255. * @param true|WP_Error $retval Returned value.
  256. * @param WP_REST_Request $request The request sent to the API.
  257. */
  258. return apply_filters( 'bp_rest_attachments_group_avatar_create_item_permissions_check', $retval, $request );
  259. }
  260. /**
  261. * Delete an existing group avatar.
  262. *
  263. * @since 5.0.0
  264. *
  265. * @param WP_REST_Request $request Full details about the request.
  266. * @return WP_REST_Response|WP_Error
  267. */
  268. public function delete_item( $request ) {
  269. $request->set_param( 'context', 'edit' );
  270. $group_id = (int) $this->group->id;
  271. if ( ! bp_get_group_has_avatar( $group_id ) ) {
  272. return new WP_Error(
  273. 'bp_rest_attachments_group_avatar_no_uploaded_avatar',
  274. __( 'Sorry, there are no uploaded avatars for this group on this site.', 'buddypress' ),
  275. array(
  276. 'status' => 404,
  277. )
  278. );
  279. }
  280. $args = array();
  281. foreach ( array( 'full', 'thumb' ) as $type ) {
  282. $args[ $type ] = bp_core_fetch_avatar(
  283. array(
  284. 'object' => $this->object,
  285. 'type' => $type,
  286. 'item_id' => $group_id,
  287. 'html' => false,
  288. )
  289. );
  290. }
  291. // Get the avatar object before deleting it.
  292. $avatar = $this->get_avatar_object( $args );
  293. $deleted = bp_core_delete_existing_avatar(
  294. array(
  295. 'object' => $this->object,
  296. 'item_id' => $group_id,
  297. )
  298. );
  299. if ( ! $deleted ) {
  300. return new WP_Error(
  301. 'bp_rest_attachments_group_avatar_delete_failed',
  302. __( 'Sorry, there was a problem deleting this group avatar.', 'buddypress' ),
  303. array(
  304. 'status' => 500,
  305. )
  306. );
  307. }
  308. // Build the response.
  309. $response = new WP_REST_Response();
  310. $response->set_data(
  311. array(
  312. 'deleted' => true,
  313. 'previous' => $avatar,
  314. )
  315. );
  316. /**
  317. * Fires after a group avatar is deleted via the REST API.
  318. *
  319. * @since 5.0.0
  320. *
  321. * @param WP_REST_Response $response The response data.
  322. * @param WP_REST_Request $request The request sent to the API.
  323. */
  324. do_action( 'bp_rest_attachments_group_avatar_delete_item', $response, $request );
  325. return $response;
  326. }
  327. /**
  328. * Checks if a given request has access to delete a group avatar.
  329. *
  330. * @since 5.0.0
  331. *
  332. * @param WP_REST_Request $request Full details about the request.
  333. * @return true|WP_Error
  334. */
  335. public function delete_item_permissions_check( $request ) {
  336. $retval = $this->create_item_permissions_check( $request );
  337. /**
  338. * Filter the group avatar `delete_item` permissions check.
  339. *
  340. * @since 5.0.0
  341. *
  342. * @param true|WP_Error $retval Returned value.
  343. * @param WP_REST_Request $request The request sent to the API.
  344. */
  345. return apply_filters( 'bp_rest_attachments_group_avatar_delete_item_permissions_check', $retval, $request );
  346. }
  347. /**
  348. * Prepares avatar data to return as an object.
  349. *
  350. * @since 5.0.0
  351. *
  352. * @param stdClass|string $avatar Avatar object or string with url or image with html.
  353. * @param WP_REST_Request $request Full details about the request.
  354. * @return WP_REST_Response
  355. */
  356. public function prepare_item_for_response( $avatar, $request ) {
  357. $data = array(
  358. 'full' => $avatar->full,
  359. 'thumb' => $avatar->thumb,
  360. );
  361. $context = ! empty( $request->get_param( 'context' ) ) ? $request->get_param( 'context' ) : 'view';
  362. $data = $this->add_additional_fields_to_object( $data, $request );
  363. $data = $this->filter_response_by_context( $data, $context );
  364. $response = rest_ensure_response( $data );
  365. /**
  366. * Filter a group avatar value returned from the API.
  367. *
  368. * @since 5.0.0
  369. *
  370. * @param WP_REST_Response $response Response.
  371. * @param WP_REST_Request $request Request used to generate the response.
  372. * @param stdClass|string $avatar Avatar object or string with url or image with html.
  373. */
  374. return apply_filters( 'bp_rest_attachments_group_avatar_prepare_value', $response, $request, $avatar );
  375. }
  376. /**
  377. * Get the plugin schema, conforming to JSON Schema.
  378. *
  379. * @since 5.0.0
  380. *
  381. * @return array
  382. */
  383. public function get_item_schema() {
  384. if ( is_null( $this->schema ) ) {
  385. $this->schema = array(
  386. '$schema' => 'http://json-schema.org/draft-04/schema#',
  387. 'title' => 'bp_attachments_group_avatar',
  388. 'type' => 'object',
  389. 'properties' => array(
  390. 'full' => array(
  391. 'context' => array( 'view', 'edit' ),
  392. 'description' => __( 'Full size of the image file.', 'buddypress' ),
  393. 'type' => 'string',
  394. 'format' => 'uri',
  395. 'readonly' => true,
  396. ),
  397. 'thumb' => array(
  398. 'context' => array( 'view', 'edit' ),
  399. 'description' => __( 'Thumb size of the image file.', 'buddypress' ),
  400. 'type' => 'string',
  401. 'format' => 'uri',
  402. 'readonly' => true,
  403. ),
  404. ),
  405. );
  406. }
  407. /**
  408. * Filters the attachments group avatar schema.
  409. *
  410. * @param array $schema The endpoint schema.
  411. */
  412. return apply_filters( 'bp_rest_attachments_group_avatar_schema', $this->add_additional_fields_schema( $this->schema ) );
  413. }
  414. /**
  415. * Get the query params for the `get_item`.
  416. *
  417. * @since 5.0.0
  418. *
  419. * @return array
  420. */
  421. public function get_item_collection_params() {
  422. $params = parent::get_collection_params();
  423. $params['context']['default'] = 'view';
  424. // Removing unused params.
  425. unset( $params['search'], $params['page'], $params['per_page'] );
  426. $params['html'] = array(
  427. 'description' => __( 'Whether to return an <img> HTML element, vs a raw URL to a group avatar.', 'buddypress' ),
  428. 'default' => false,
  429. 'type' => 'boolean',
  430. 'sanitize_callback' => 'rest_sanitize_boolean',
  431. 'validate_callback' => 'rest_validate_request_arg',
  432. );
  433. $params['alt'] = array(
  434. 'description' => __( 'The alt attribute for the <img> element.', 'buddypress' ),
  435. 'default' => '',
  436. 'type' => 'string',
  437. 'sanitize_callback' => 'sanitize_text_field',
  438. 'validate_callback' => 'rest_validate_request_arg',
  439. );
  440. /**
  441. * Filters the item collection query params.
  442. *
  443. * @param array $params Query params.
  444. */
  445. return apply_filters( 'bp_rest_attachments_group_avatar_collection_params', $params );
  446. }
  447. }