Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
20 / 25
66.67% covered (warning)
66.67%
2 / 3
CRAP
n/a
0 / 0
Automattic\Jetpack\Publicize\Social_Image_Generator\get_image_url
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
Automattic\Jetpack\Publicize\Social_Image_Generator\get_token_body
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
Automattic\Jetpack\Publicize\Social_Image_Generator\fetch_token
54.55% covered (warning)
54.55%
6 / 11
0.00% covered (danger)
0.00%
0 / 1
3.85
1<?php
2/**
3 * Utilities.
4 *
5 * @package automattic/jetpack-publicize
6 */
7
8namespace Automattic\Jetpack\Publicize\Social_Image_Generator;
9
10use Automattic\Jetpack\Publicize\Publicize_Utils as Utils;
11use Automattic\Jetpack\Publicize\REST_API\Proxy_Requests;
12use Automattic\Jetpack\Redirect;
13use WP_Error;
14use WP_REST_Request;
15
16/**
17 * Given a post ID, returns the image URL for the generated image.
18 *
19 * @param int $post_id Post ID to get the URL for.
20 * @return string
21 */
22function get_image_url( $post_id ) {
23    $post_settings = new Post_Settings( $post_id );
24    $token         = $post_settings->get_token();
25
26    if ( ! $post_settings->is_enabled() || empty( $token ) ) {
27        return '';
28    }
29
30    return add_query_arg(
31        array( 'query' => rawurlencode( 't=' . $token ) ),
32        Redirect::get_url( 'sigenerate', array( 'site' => null ) )
33    );
34}
35
36/**
37 * Get the parameters for the token body.
38 *
39 * @param string $text Text to use in the generated image.
40 * @param string $image_url Image to use in the generated image.
41 * @param string $template Template to use in the generated image.
42 * @param string $font Font to use in the generated image.
43 * @return array
44 */
45function get_token_body( $text, $image_url, $template, $font = '' ) {
46    return array(
47        'text'      => $text,
48        'image_url' => $image_url,
49        'template'  => $template,
50        'font'      => $font,
51    );
52}
53
54/**
55 * Fetch a token from the WPCOM endpoint.
56 *
57 * @param string $text      The text that will be displayed on the generated image.
58 * @param string $image_url The background image URL to be used in the generated image.
59 * @param string $template  The template slug to use for generating the image.
60 * @param string $font The font slug to use for the text in the image.
61 * @return string|WP_Error  The generated token or a WP_Error object if there's been a problem.
62 */
63function fetch_token( $text, $image_url, $template, $font = '' ) {
64
65    $args = get_token_body( $text, $image_url, $template, $font );
66
67    if ( Utils::is_wpcom() ) {
68        require_lib( 'social-image-generator-token' );
69        require_lib( 'publicize/util/social-image-generator' );
70
71        if ( ! \Publicize\Social_Image_Generator\is_enabled() ) {
72            return new WP_Error( 'social_image_generator_not_enabled', __( 'Social Image Generator is not enabled.', 'jetpack-publicize-pkg' ) );
73        }
74
75        return \Social_Image_Generator\generate_token( $args );
76    }
77
78    $proxy = new Proxy_Requests( 'publicize/social-image-generator' );
79
80    $request = new WP_REST_Request( 'POST' );
81
82    $request->set_body( wp_json_encode( $args, JSON_UNESCAPED_SLASHES ) );
83
84    return $proxy->proxy_request_to_wpcom_as_blog( $request, 'generate-token' );
85}