class.wpcom-json-api-list-embeds-endpoint.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. new WPCOM_JSON_API_List_Embeds_Endpoint( array(
  3. 'description' => "Get a list of embeds available on a site. Note: The current user must have publishing access.",
  4. 'group' => 'sites',
  5. 'stat' => 'embeds',
  6. 'method' => 'GET',
  7. 'path' => '/sites/%s/embeds',
  8. 'path_labels' => array(
  9. '$site' => '(int|string) Site ID or domain',
  10. ),
  11. 'response_format' => array(
  12. 'embeds' => '(array) A list of supported embeds by their regex pattern.',
  13. ),
  14. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/embeds',
  15. 'example_request_data' => array(
  16. 'headers' => array(
  17. 'authorization' => 'Bearer YOUR_API_TOKEN'
  18. ),
  19. )
  20. ) );
  21. class WPCOM_JSON_API_List_Embeds_Endpoint extends WPCOM_JSON_API_Endpoint {
  22. // /sites/%s/embeds -> $blog_id
  23. function callback( $path = '', $blog_id = 0 ) {
  24. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  25. if ( is_wp_error( $blog_id ) ) {
  26. return $blog_id;
  27. }
  28. // permissions check
  29. if ( ! current_user_can( 'edit_posts' ) ) {
  30. return new WP_Error( 'unauthorized', 'Your token must have permission to post on this blog.', 403 );
  31. }
  32. // list em
  33. $output = array( 'embeds' => array() );
  34. if ( ! function_exists( '_wp_oembed_get_object' ) ) {
  35. require_once( ABSPATH . WPINC . '/class-oembed.php' );
  36. }
  37. global $wp_embed;
  38. $oembed = _wp_oembed_get_object();
  39. foreach( $wp_embed->handlers as $priority => $handlers ) {
  40. foreach( $handlers as $handler ) {
  41. if ( ! empty( $handler['regex'] ) )
  42. $output['embeds'][] = $handler['regex'];
  43. }
  44. }
  45. foreach ( $oembed->providers as $regex => $oembed_info ) {
  46. if ( ! empty( $regex ) )
  47. $output['embeds'][] = $regex;
  48. }
  49. return $output;
  50. }
  51. }