Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Setup
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 3
420
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 generate_token
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 generate_token_on_save
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
240
1<?php
2/**
3 * Setup class.
4 *
5 * @package automattic/jetpack-publicize
6 */
7
8namespace Automattic\Jetpack\Publicize\Social_Image_Generator;
9
10use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings;
11
12/**
13 * Class for setting up Social Image Generator-related functionality.
14 */
15class Setup {
16    /**
17     * Initialise SIG-related functionality.
18     */
19    public function init() {
20        if ( ! ( new Settings() )->is_sig_available() ) {
21            return;
22        }
23
24        // Be wary of any code that you add to this file, since this function is called on plugin load.
25        // We're using the `wp_after_insert_post` hook because we need access to the updated post meta. By using the default priority
26        // of 10 we make sure that our code runs before Sync processes the post.
27        add_action( 'wp_after_insert_post', array( $this, 'generate_token_on_save' ), 10, 3 );
28        add_action( 'rest_api_init', array( new REST_Token_Controller(), 'register_routes' ) );
29
30        // Flagged to be removed after deprecation.
31        // @deprecated 0.38.3
32        add_action( 'rest_api_init', array( new REST_Settings_Controller(), 'register_routes' ) );
33    }
34
35    /**
36     * Get a token from WPCOM to generate the social image for the post, and save it locally.
37     *
38     * @param Post_Settings $post_settings A Post_Settings object that can be used to save the generated token.
39     */
40    public function generate_token( $post_settings ) {
41        if ( ! $post_settings->is_enabled() ) {
42            return;
43        }
44
45        $token = fetch_token(
46            $post_settings->get_custom_text(),
47            $post_settings->get_image_url(),
48            $post_settings->get_template(),
49            $post_settings->get_font()
50        );
51
52        if ( is_wp_error( $token ) ) {
53            return;
54        }
55
56        $post_settings->update_setting( 'token', sanitize_text_field( $token ) );
57    }
58
59    /**
60     * Trigger token generation for a post if SIG is enabled.
61     *
62     * @param int      $post_id     Post ID.
63     * @param \WP_Post $post        The post object being saved.
64     * @param bool     $update      Whether this is an update to a post.
65     */
66    public function generate_token_on_save( $post_id, $post, $update ) {
67        if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
68            return;
69        }
70
71        // If we're not using the block editor for this post, do not continue.
72        if ( ! use_block_editor_for_post( $post ) ) {
73            return;
74        }
75
76        global $publicize;
77
78        if ( ! $publicize->post_type_is_publicizeable( $post->post_type ) ) {
79            return;
80        }
81
82        $settings = new Settings();
83
84        if ( ! $settings->is_sig_available() ) {
85            return;
86        }
87
88        if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post_id ) ) {
89            return;
90        }
91
92        // Set SIG to be enabled by default for new posts if the toggle is on.
93        $post_settings = new Post_Settings( $post_id );
94        if (
95            ! $update &&
96            'auto-draft' === $post->post_status &&
97            ! empty( $settings->get_settings()['socialImageGeneratorSettings']['enabled'] ) &&
98            empty( $post_settings->get_settings( true ) ) &&
99            'jetpack-social-note' !== $post->post_type
100        ) {
101            $post_settings->update_setting( 'enabled', true );
102            return;
103        }
104
105        if ( $post->post_status === 'auto-draft' ) {
106            return;
107        }
108
109        if ( ! $post_settings->is_enabled() ) {
110            return;
111        }
112
113        $this->generate_token( $post_settings );
114    }
115}