Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 105
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 / 105
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 / 11
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            'utmSettings'          => $settings->get_utm_settings(),
161            'socialNotes'          => array(
162                'enabled' => $settings->is_social_notes_enabled(),
163                'config'  => $settings->get_social_notes_config(),
164            ),
165            'showPricingPage'      => $settings->should_show_pricing_page(),
166            'messageTemplate'      => $settings->get_message_template(),
167        );
168    }
169
170    /**
171     * Get the plugin info.
172     *
173     * @return array
174     */
175    public static function get_plugin_info() {
176
177        $social_version  = null;
178        $jetpack_version = null;
179
180        if ( defined( 'JETPACK_SOCIAL_PLUGIN_ROOT_FILE' ) ) {
181
182            $plugin_data = get_plugin_data( (string) constant( 'JETPACK_SOCIAL_PLUGIN_ROOT_FILE' ), false, false );
183
184            $social_version = $plugin_data['Version'];
185        }
186
187        if ( defined( 'JETPACK__VERSION' ) ) {
188            $jetpack_version = constant( 'JETPACK__VERSION' );
189        }
190
191        return array(
192            'social'  => array(
193                'version' => $social_version,
194            ),
195            'jetpack' => array(
196                'version' => $jetpack_version,
197            ),
198        );
199    }
200
201    /**
202     * Get the social store initial state.
203     *
204     * @return array
205     */
206    public static function get_store_initial_state() {
207
208        $post = get_post();
209
210        $share_status = array();
211
212        // get_post_share_status is not available on WPCOM yet.
213        if ( Utils::should_block_editor_have_social() && $post ) {
214            $share_status[ $post->ID ] = Share_Status::get_post_share_status( $post->ID );
215        }
216
217        return array(
218            'connectionData' => array(
219                'connections' => Connections::get_all_for_user(),
220            ),
221            'shareStatus'    => $share_status,
222        );
223    }
224
225    /**
226     * Whether the site has the feature flag enabled.
227     *
228     * @deprecated 0.69.1 Use Current_Plan::supports() directly instead.
229     *
230     * @todo Remove this method After March 2026.
231     *
232     * @param string $feature The feature name to check for, without the "social-" prefix.
233     * @return bool
234     */
235    public static function has_feature_flag( $feature ): bool {
236        return Current_Plan::supports( 'social-' . $feature );
237    }
238
239    /**
240     * Get the list of supported Publicize services.
241     *
242     * @return array List of external services and their settings.
243     */
244    public static function get_supported_services() {
245        return Publicize_Services::get_all();
246    }
247
248    /**
249     * Get the API paths.
250     *
251     * @return array
252     */
253    public static function get_api_paths() {
254
255        return array(
256            'refreshConnections' => '/wpcom/v2/publicize/connections?test_connections=1',
257            // The complete path will be like `/jetpack/v4/social/settings`.
258            'socialToggleBase'   => Utils::should_use_jetpack_module_endpoint() ? 'settings' : 'social/settings',
259            'resharePost'        => '/wpcom/v2/publicize/share-post/{postId}',
260        );
261    }
262
263    /**
264     * Get the URLs.
265     *
266     * @return array
267     */
268    public static function get_urls() {
269
270        $urls = array(
271            'connectionsManagementPage' => self::publicize()->publicize_connections_url(),
272        );
273
274        // Escape the URLs.
275        array_walk( $urls, 'esc_url_raw' );
276
277        return $urls;
278    }
279
280    /**
281     * Get nonces required by the Social admin UI.
282     *
283     * @return array
284     */
285    private static function get_nonces() {
286        return array(
287            'refresh_plan' => wp_create_nonce( Social_Admin_Page::REFRESH_PLAN_NONCE_ACTION ),
288        );
289    }
290}