Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 106
0.00% covered (danger)
0.00%
0 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
Publicize_Script_Data
0.00% covered (danger)
0.00%
0 / 106
0.00% covered (danger)
0.00%
0 / 12
930
0.00% covered (danger)
0.00%
0 / 1
 publicize
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 configure
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 set_admin_script_data
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 set_wpcom_user_data
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 get_admin_script_data
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
56
 get_social_settings
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 get_plugin_info
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
 get_store_initial_state
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 has_feature_flag
n/a
0 / 0
n/a
0 / 0
1
 get_supported_services
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_api_paths
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 get_urls
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 get_nonces
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Publicize_Script_Data.
4 *
5 * @package automattic/jetpack-publicize
6 */
7
8namespace Automattic\Jetpack\Publicize;
9
10use Automattic\Jetpack\Connection\Manager;
11use Automattic\Jetpack\Current_Plan;
12use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings;
13use Automattic\Jetpack\Publicize\Publicize_Utils as Utils;
14use Automattic\Jetpack\Publicize\Services as Publicize_Services;
15use Automattic\Jetpack\Status\Host;
16
17/**
18 * Publicize_Script_Data class.
19 */
20class Publicize_Script_Data {
21
22    /**
23     * Get the publicize instance - properly typed
24     *
25     * @return Publicize
26     */
27    public static function publicize() {
28        /**
29         * Publicize instance.
30         *
31         * @var Publicize $publicize
32         */
33        global $publicize;
34
35        if ( ! $publicize && function_exists( 'publicize_init' ) ) {
36            // @phan-suppress-next-line PhanUndeclaredFunction - phan is dumb not to see the function_exists check
37            publicize_init();
38        }
39
40        return $publicize;
41    }
42
43    /**
44     * Configure script data.
45     */
46    public static function configure() {
47        add_filter( 'jetpack_admin_js_script_data', array( __CLASS__, 'set_admin_script_data' ), 10, 1 );
48    }
49
50    /**
51     * Set script data.
52     *
53     * @param array $data The script data.
54     */
55    public static function set_admin_script_data( $data ) {
56
57        $data['social'] = apply_filters( 'jetpack_social_admin_script_data', self::get_admin_script_data(), $data );
58
59        if ( empty( $data['site']['plan']['product_slug'] ) ) {
60            $data['site']['plan'] = Current_Plan::get();
61        }
62
63        // Override features for simple sites.
64        if ( ( new Host() )->is_wpcom_simple() ) {
65            $data['site']['plan']['features'] = Current_Plan::get_simple_site_specific_features();
66        }
67
68        $data['site']['wpcom']['blog_id'] = Manager::get_site_id( true );
69
70        self::set_wpcom_user_data( $data['user']['current_user'] );
71
72        return $data;
73    }
74
75    /**
76     * Set wpcom user data.
77     *
78     * @param array $user_data The user data.
79     */
80    private static function set_wpcom_user_data( &$user_data ) {
81        if ( ( new Host() )->is_wpcom_simple() ) {
82            $wpcom_user_data = array(
83                'ID'    => get_current_user_id(),
84                'login' => wp_get_current_user()->user_login,
85            );
86        } else {
87            $wpcom_user_data = ( new Manager() )->get_connected_user_data();
88        }
89
90        $user_data['wpcom'] = array_merge(
91            $user_data['wpcom'] ?? array(),
92            $wpcom_user_data ? $wpcom_user_data : array()
93        );
94    }
95
96    /**
97     * Get the script data for admin UI.
98     *
99     * @return array
100     */
101    public static function get_admin_script_data() {
102
103        // Only set script data on the social settings page,
104        // the Jetpack settings page, or the block editor.
105        $should_set_script_data = Utils::is_jetpack_settings_page()
106            || Utils::is_social_settings_page()
107            || Utils::should_block_editor_have_social();
108
109        if ( ! $should_set_script_data ) {
110            return array();
111        }
112
113        $basic_data = array(
114            'api_paths'            => self::get_api_paths(),
115            'assets_url'           => plugins_url( '/build/', __DIR__ ),
116            'is_publicize_enabled' => Utils::is_publicize_active(),
117            'message_templates'    => array(),
118            'supported_services'   => array(),
119            'urls'                 => array(),
120            'settings'             => self::get_social_settings(),
121            'plugin_info'          => self::get_plugin_info(),
122            'nonces'               => self::get_nonces(),
123        );
124
125        if ( ! Utils::is_publicize_active() ) {
126            return $basic_data;
127        }
128
129        // Simple sites don't have a user connection.
130        $is_publicize_configured = ( new Host() )->is_wpcom_simple() || Utils::is_connected();
131
132        if ( ! $is_publicize_configured ) {
133            return $basic_data;
134        }
135
136        return array_merge(
137            $basic_data,
138            array(
139                'supported_services'  => self::get_supported_services(),
140                'urls'                => self::get_urls(),
141                'store_initial_state' => self::get_store_initial_state(),
142                'message_templates'   => array(
143                    'placeholders' => Message_Templates_Placeholders::get_all(),
144                ),
145            )
146        );
147    }
148
149    /**
150     * Get the social settings.
151     *
152     * @return array
153     */
154    public static function get_social_settings() {
155
156        $settings = ( new Settings() );
157
158        return array(
159            'socialImageGenerator' => $settings->get_image_generator_settings(),
160            'openGraphSettings'    => $settings->get_open_graph_settings(),
161            'utmSettings'          => $settings->get_utm_settings(),
162            'socialNotes'          => array(
163                'enabled' => $settings->is_social_notes_enabled(),
164                'config'  => $settings->get_social_notes_config(),
165            ),
166            'showPricingPage'      => $settings->should_show_pricing_page(),
167            'messageTemplate'      => $settings->get_message_template(),
168        );
169    }
170
171    /**
172     * Get the plugin info.
173     *
174     * @return array
175     */
176    public static function get_plugin_info() {
177
178        $social_version  = null;
179        $jetpack_version = null;
180
181        if ( defined( 'JETPACK_SOCIAL_PLUGIN_ROOT_FILE' ) ) {
182
183            $plugin_data = get_plugin_data( (string) constant( 'JETPACK_SOCIAL_PLUGIN_ROOT_FILE' ), false, false );
184
185            $social_version = $plugin_data['Version'];
186        }
187
188        if ( defined( 'JETPACK__VERSION' ) ) {
189            $jetpack_version = constant( 'JETPACK__VERSION' );
190        }
191
192        return array(
193            'social'  => array(
194                'version' => $social_version,
195            ),
196            'jetpack' => array(
197                'version' => $jetpack_version,
198            ),
199        );
200    }
201
202    /**
203     * Get the social store initial state.
204     *
205     * @return array
206     */
207    public static function get_store_initial_state() {
208
209        $post = get_post();
210
211        $share_status = array();
212
213        // get_post_share_status is not available on WPCOM yet.
214        if ( Utils::should_block_editor_have_social() && $post ) {
215            $share_status[ $post->ID ] = Share_Status::get_post_share_status( $post->ID );
216        }
217
218        return array(
219            'connectionData' => array(
220                'connections' => Connections::get_all_for_user(),
221            ),
222            'shareStatus'    => $share_status,
223        );
224    }
225
226    /**
227     * Whether the site has the feature flag enabled.
228     *
229     * @deprecated 0.69.1 Use Current_Plan::supports() directly instead.
230     *
231     * @todo Remove this method After March 2026.
232     *
233     * @param string $feature The feature name to check for, without the "social-" prefix.
234     * @return bool
235     */
236    public static function has_feature_flag( $feature ): bool {
237        return Current_Plan::supports( 'social-' . $feature );
238    }
239
240    /**
241     * Get the list of supported Publicize services.
242     *
243     * @return array List of external services and their settings.
244     */
245    public static function get_supported_services() {
246        return Publicize_Services::get_all();
247    }
248
249    /**
250     * Get the API paths.
251     *
252     * @return array
253     */
254    public static function get_api_paths() {
255
256        return array(
257            'refreshConnections' => '/wpcom/v2/publicize/connections?test_connections=1',
258            // The complete path will be like `/jetpack/v4/social/settings`.
259            'socialToggleBase'   => Utils::should_use_jetpack_module_endpoint() ? 'settings' : 'social/settings',
260            'resharePost'        => '/wpcom/v2/publicize/share-post/{postId}',
261        );
262    }
263
264    /**
265     * Get the URLs.
266     *
267     * @return array
268     */
269    public static function get_urls() {
270
271        $urls = array(
272            'connectionsManagementPage' => self::publicize()->publicize_connections_url(),
273        );
274
275        // Escape the URLs.
276        array_walk( $urls, 'esc_url_raw' );
277
278        return $urls;
279    }
280
281    /**
282     * Get nonces required by the Social admin UI.
283     *
284     * @return array
285     */
286    private static function get_nonces() {
287        return array(
288            'refresh_plan' => wp_create_nonce( Social_Admin_Page::REFRESH_PLAN_NONCE_ACTION ),
289        );
290    }
291}