Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. <?php
  2.  
  3. // Registers a REST field for the Activity Endpoints.
  4. function example_register_activity_rest_field() {
  5. bp_rest_register_field(
  6. 'activity', // Id of the BuddyPress component the REST field is about
  7. 'example_field', // Used into the REST response/request
  8. array(
  9. 'get_callback' => 'example_get_rest_field_callback', // The function to use to get the value of the REST Field
  10. 'update_callback' => 'example_update_rest_field_callback', // The function to use to update the value of the REST Field
  11. 'schema' => array( // The example_field REST schema.
  12. 'description' => 'Example of Activity Meta Field',
  13. 'type' => 'string',
  14. 'context' => array( 'view', 'edit' ),
  15. ),
  16. )
  17. );
  18. }
  19. add_action( 'bp_rest_api_init', 'example_register_activity_rest_field' );
  20.  
  21. /**
  22. * The function to use to get the value of the REST Field.
  23. *
  24. * @param array $array The list of properties of the BuddyPress component's object.
  25. * @param string $attribute The REST Field key used into the REST response.
  26. * @return string The value of the REST Field to include into the REST response.
  27. */
  28. function example_get_rest_field_callback( $array, $attribute ) {
  29. // The key of the metadata can be different from the REST Field key.
  30. $metadata_key = '_example_metadata_key';
  31.  
  32. return bp_activity_get_meta( $array['id'], $metadata_key );
  33. }
  34.  
  35. /**
  36. * The function to use to update the value of the REST Field.
  37. *
  38. * @param object $object The BuddyPress component's object that was just created/updated during the request.
  39. * (in this case the BP_Activity_Activity object).
  40. * @return string $value The value of the REST Field to save.
  41. * @param string $attribute The REST Field key used into the REST response.
  42. */
  43. function example_update_rest_field_callback( $object, $value, $attribute ) {
  44. // The key of the metadata can be different from the REST Field key.
  45. $metadata_key = '_example_metadata_key';
  46.  
  47. bp_activity_update_meta( $object->id, $metadata_key, $value );
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement