Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 96
0.00% covered (danger)
0.00%
0 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Publicize_Script_Data
0.00% covered (danger)
0.00%
0 / 96
0.00% covered (danger)
0.00%
0 / 11
870
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 / 27
0.00% covered (danger)
0.00%
0 / 1
56
 get_social_settings
0.00% covered (danger)
0.00%
0 / 10
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
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            'supported_services'   => array(),
118            'urls'                 => array(),
119            'settings'             => self::get_social_settings(),
120            'plugin_info'          => self::get_plugin_info(),
121        );
122
123        if ( ! Utils::is_publicize_active() ) {
124            return $basic_data;
125        }
126
127        // Simple sites don't have a user connection.
128        $is_publicize_configured = ( new Host() )->is_wpcom_simple() || Utils::is_connected();
129
130        if ( ! $is_publicize_configured ) {
131            return $basic_data;
132        }
133
134        return array_merge(
135            $basic_data,
136            array(
137                'supported_services'  => self::get_supported_services(),
138                'urls'                => self::get_urls(),
139                'store_initial_state' => self::get_store_initial_state(),
140            )
141        );
142    }
143
144    /**
145     * Get the social settings.
146     *
147     * @return array
148     */
149    public static function get_social_settings() {
150
151        $settings = ( new Settings() );
152
153        return array(
154            'socialImageGenerator' => $settings->get_image_generator_settings(),
155            'utmSettings'          => $settings->get_utm_settings(),
156            'socialNotes'          => array(
157                'enabled' => $settings->is_social_notes_enabled(),
158                'config'  => $settings->get_social_notes_config(),
159            ),
160            'showPricingPage'      => $settings->should_show_pricing_page(),
161        );
162    }
163
164    /**
165     * Get the plugin info.
166     *
167     * @return array
168     */
169    public static function get_plugin_info() {
170
171        $social_version  = null;
172        $jetpack_version = null;
173
174        if ( defined( 'JETPACK_SOCIAL_PLUGIN_ROOT_FILE' ) ) {
175
176            $plugin_data = get_plugin_data( (string) constant( 'JETPACK_SOCIAL_PLUGIN_ROOT_FILE' ), false, false );
177
178            $social_version = $plugin_data['Version'];
179        }
180
181        if ( defined( 'JETPACK__VERSION' ) ) {
182            $jetpack_version = constant( 'JETPACK__VERSION' );
183        }
184
185        return array(
186            'social'  => array(
187                'version' => $social_version,
188            ),
189            'jetpack' => array(
190                'version' => $jetpack_version,
191            ),
192        );
193    }
194
195    /**
196     * Get the social store initial state.
197     *
198     * @return array
199     */
200    public static function get_store_initial_state() {
201
202        $post = get_post();
203
204        $share_status = array();
205
206        // get_post_share_status is not available on WPCOM yet.
207        if ( Utils::should_block_editor_have_social() && $post ) {
208            $share_status[ $post->ID ] = Share_Status::get_post_share_status( $post->ID );
209        }
210
211        return array(
212            'connectionData' => array(
213                'connections' => Connections::get_all_for_user(),
214            ),
215            'shareStatus'    => $share_status,
216        );
217    }
218
219    /**
220     * Whether the site has the feature flag enabled.
221     *
222     * @deprecated 0.69.1 Use Current_Plan::supports() directly instead.
223     *
224     * @todo Remove this method After March 2026.
225     *
226     * @param string $feature The feature name to check for, without the "social-" prefix.
227     * @return bool
228     */
229    public static function has_feature_flag( $feature ): bool {
230        return Current_Plan::supports( 'social-' . $feature );
231    }
232
233    /**
234     * Get the list of supported Publicize services.
235     *
236     * @return array List of external services and their settings.
237     */
238    public static function get_supported_services() {
239        return Publicize_Services::get_all();
240    }
241
242    /**
243     * Get the API paths.
244     *
245     * @return array
246     */
247    public static function get_api_paths() {
248
249        return array(
250            'refreshConnections' => '/wpcom/v2/publicize/connections?test_connections=1',
251            // The complete path will be like `/jetpack/v4/social/settings`.
252            'socialToggleBase'   => Utils::should_use_jetpack_module_endpoint() ? 'settings' : 'social/settings',
253            'resharePost'        => '/wpcom/v2/publicize/share-post/{postId}',
254        );
255    }
256
257    /**
258     * Get the URLs.
259     *
260     * @return array
261     */
262    public static function get_urls() {
263
264        $urls = array(
265            'connectionsManagementPage' => self::publicize()->publicize_connections_url(),
266        );
267
268        // Escape the URLs.
269        array_walk( $urls, 'esc_url_raw' );
270
271        return $urls;
272    }
273}