jetpack
/
_inc
/
lib
/
core-api
/
wpcom-endpoints
/
class-wpcom-rest-api-v2-endpoint-tweetstorm-parse.php
business-hours.php
4 years ago
class-wpcom-rest-api-v2-endpoint-admin-menu.php
3 years ago
class-wpcom-rest-api-v2-endpoint-ai.php
2 years ago
class-wpcom-rest-api-v2-endpoint-external-media.php
2 years ago
class-wpcom-rest-api-v2-endpoint-following.php
2 years ago
class-wpcom-rest-api-v2-endpoint-google-docs.php
4 years ago
class-wpcom-rest-api-v2-endpoint-instagram-gallery.php
3 years ago
class-wpcom-rest-api-v2-endpoint-mailchimp.php
3 years ago
class-wpcom-rest-api-v2-endpoint-newsletter-categories-list.php
2 years ago
class-wpcom-rest-api-v2-endpoint-podcast-player.php
3 years ago
class-wpcom-rest-api-v2-endpoint-publicize-share-post.php
3 years ago
class-wpcom-rest-api-v2-endpoint-related-posts.php
2 years ago
class-wpcom-rest-api-v2-endpoint-resolve-redirect.php
2 years ago
class-wpcom-rest-api-v2-endpoint-search.php
4 years ago
class-wpcom-rest-api-v2-endpoint-send-email-preview.php
2 years ago
class-wpcom-rest-api-v2-endpoint-template-loader.php
3 years ago
class-wpcom-rest-api-v2-endpoint-transient.php
5 years ago
class-wpcom-rest-api-v2-endpoint-tweetstorm-gather.php
3 years ago
class-wpcom-rest-api-v2-endpoint-tweetstorm-parse.php
3 years ago
class-wpcom-rest-api-v3-endpoint-blogging-prompts.php
3 years ago
gutenberg-available-extensions.php
4 years ago
hello.php
4 years ago
memberships.php
2 years ago
publicize-connection-test-results.php
3 years ago
publicize-connections.php
3 years ago
publicize-services.php
3 years ago
service-api-keys.php
3 years ago
sites-posts-featured-media-url.php
4 years ago
subscribers.php
3 years ago
trait-wpcom-rest-api-proxy-request-trait.php
2 years ago
class-wpcom-rest-api-v2-endpoint-tweetstorm-parse.php
109 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API endpoint for parsing Tweetstorms out of block content. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | * @since 9.0.0 |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Tweetstorm gatherer. |
| 11 | * |
| 12 | * @since 9.0.0 |
| 13 | */ |
| 14 | class WPCOM_REST_API_V2_Endpoint_Tweetstorm_Parse extends WP_REST_Controller { |
| 15 | /** |
| 16 | * Constructor. |
| 17 | */ |
| 18 | public function __construct() { |
| 19 | $this->namespace = 'wpcom/v2'; |
| 20 | $this->rest_base = 'tweetstorm'; |
| 21 | |
| 22 | if ( ! class_exists( 'Jetpack_Tweetstorm_Helper' ) ) { |
| 23 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-tweetstorm-helper.php'; |
| 24 | } |
| 25 | |
| 26 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Register the route. |
| 31 | */ |
| 32 | public function register_routes() { |
| 33 | register_rest_route( |
| 34 | $this->namespace, |
| 35 | $this->rest_base . '/parse', |
| 36 | array( |
| 37 | 'args' => array( |
| 38 | 'blocks' => array( |
| 39 | 'description' => __( 'An array of serialized blocks, and editor-specific block information.', 'jetpack' ), |
| 40 | 'type' => 'array', |
| 41 | 'required' => true, |
| 42 | ), |
| 43 | ), |
| 44 | 'methods' => WP_REST_Server::EDITABLE, |
| 45 | 'callback' => array( $this, 'parse_tweetstorm' ), |
| 46 | 'allow_blog_token_when_site_is_private' => true, |
| 47 | 'permission_callback' => array( 'Jetpack_Tweetstorm_Helper', 'permissions_check' ), |
| 48 | ) |
| 49 | ); |
| 50 | |
| 51 | register_rest_route( |
| 52 | $this->namespace, |
| 53 | $this->rest_base . '/generate-cards', |
| 54 | array( |
| 55 | 'args' => array( |
| 56 | 'urls' => array( |
| 57 | 'description' => __( 'An array of URLs to generate Twitter card details for.', 'jetpack' ), |
| 58 | 'type' => 'array', |
| 59 | 'required' => true, |
| 60 | ), |
| 61 | ), |
| 62 | 'methods' => WP_REST_Server::EDITABLE, |
| 63 | 'callback' => array( $this, 'generate_cards' ), |
| 64 | 'allow_blog_token_when_site_is_private' => true, |
| 65 | 'permission_callback' => array( 'Jetpack_Tweetstorm_Helper', 'permissions_check' ), |
| 66 | ) |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Parse the content into a tweetstorm. |
| 72 | * |
| 73 | * @param WP_REST_Request $request The request. |
| 74 | * @return mixed |
| 75 | */ |
| 76 | public function parse_tweetstorm( $request ) { |
| 77 | // The "block" attribute is serialised, unserialise it before passing it on. |
| 78 | $blocks = array_map( |
| 79 | function ( $block ) { |
| 80 | $parsed_block = parse_blocks( $block['block'] ); |
| 81 | if ( is_countable( $parsed_block ) && count( $parsed_block ) > 0 ) { |
| 82 | $block['block'] = $parsed_block[0]; |
| 83 | return $block; |
| 84 | } |
| 85 | |
| 86 | return null; |
| 87 | }, |
| 88 | $request['blocks'] |
| 89 | ); |
| 90 | |
| 91 | // Remove any blocks that failed to unserialise. |
| 92 | $blocks = array_values( array_filter( $blocks, 'is_array' ) ); |
| 93 | |
| 94 | return Jetpack_Tweetstorm_Helper::parse( $blocks ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Grab the card content for a list of URLs. |
| 99 | * |
| 100 | * @param WP_REST_Request $request The request. |
| 101 | * @return array The array of cards for the requested URLs. |
| 102 | */ |
| 103 | public function generate_cards( $request ) { |
| 104 | return Jetpack_Tweetstorm_Helper::generate_cards( $request['urls'] ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Tweetstorm_Parse' ); |
| 109 |