Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
57.52% |
65 / 113 |
|
33.33% |
5 / 15 |
CRAP | |
0.00% |
0 / 1 |
| Publicize_Script_Data | |
57.52% |
65 / 113 |
|
33.33% |
5 / 15 |
141.93 | |
0.00% |
0 / 1 |
| publicize | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| configure | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| set_admin_script_data | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| set_wpcom_user_data | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| get_social_script_data | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_admin_script_data | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
20 | |||
| build_admin_script_data | |
60.71% |
17 / 28 |
|
0.00% |
0 / 1 |
4.97 | |||
| get_upgrade_data | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| get_social_settings | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| get_plugin_info | |
70.59% |
12 / 17 |
|
0.00% |
0 / 1 |
4.41 | |||
| get_store_initial_state | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
4.02 | |||
| has_feature_flag | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| get_supported_services | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_api_paths | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| get_urls | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| get_nonces | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Publicize_Script_Data. |
| 4 | * |
| 5 | * @package automattic/jetpack-publicize |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Publicize; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Manager; |
| 11 | use Automattic\Jetpack\Current_Plan; |
| 12 | use Automattic\Jetpack\Plans; |
| 13 | use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings; |
| 14 | use Automattic\Jetpack\Publicize\Publicize_Utils as Utils; |
| 15 | use Automattic\Jetpack\Publicize\Services as Publicize_Services; |
| 16 | use Automattic\Jetpack\Status\Host; |
| 17 | |
| 18 | /** |
| 19 | * Publicize_Script_Data class. |
| 20 | */ |
| 21 | class Publicize_Script_Data { |
| 22 | |
| 23 | /** |
| 24 | * The WordPress.com plan that unlocks Social's paid features on Simple sites. |
| 25 | */ |
| 26 | private const UPGRADE_PLAN_SLUG = 'business-bundle'; |
| 27 | |
| 28 | /** |
| 29 | * Get the publicize instance - properly typed |
| 30 | * |
| 31 | * @return Publicize |
| 32 | */ |
| 33 | public static function publicize() { |
| 34 | /** |
| 35 | * Publicize instance. |
| 36 | * |
| 37 | * @var Publicize $publicize |
| 38 | */ |
| 39 | global $publicize; |
| 40 | |
| 41 | if ( ! $publicize && function_exists( 'publicize_init' ) ) { |
| 42 | // @phan-suppress-next-line PhanUndeclaredFunction - phan is dumb not to see the function_exists check |
| 43 | publicize_init(); |
| 44 | } |
| 45 | |
| 46 | return $publicize; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Configure script data. |
| 51 | */ |
| 52 | public static function configure() { |
| 53 | add_filter( 'jetpack_admin_js_script_data', array( __CLASS__, 'set_admin_script_data' ), 10, 1 ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Set script data. |
| 58 | * |
| 59 | * @param array $data The script data. |
| 60 | */ |
| 61 | public static function set_admin_script_data( $data ) { |
| 62 | |
| 63 | /** |
| 64 | * Filters the data the Social UI reads as `JetpackScriptData.social`. |
| 65 | * |
| 66 | * @param array $social_data The social script data. |
| 67 | * @param array $script_data The rest of the Jetpack script data, empty when the Social dashboard fetches it. |
| 68 | */ |
| 69 | $data['social'] = apply_filters( 'jetpack_social_admin_script_data', self::get_admin_script_data(), $data ); |
| 70 | |
| 71 | if ( empty( $data['site']['plan']['product_slug'] ) ) { |
| 72 | $data['site']['plan'] = Current_Plan::get(); |
| 73 | } |
| 74 | |
| 75 | // Override features for simple sites. |
| 76 | if ( ( new Host() )->is_wpcom_simple() ) { |
| 77 | $data['site']['plan']['features'] = Current_Plan::get_simple_site_specific_features(); |
| 78 | } |
| 79 | |
| 80 | $data['site']['wpcom']['blog_id'] = Manager::get_site_id( true ); |
| 81 | |
| 82 | self::set_wpcom_user_data( $data['user']['current_user'] ); |
| 83 | |
| 84 | return $data; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Set wpcom user data. |
| 89 | * |
| 90 | * @param array $user_data The user data. |
| 91 | */ |
| 92 | private static function set_wpcom_user_data( &$user_data ) { |
| 93 | if ( ( new Host() )->is_wpcom_simple() ) { |
| 94 | $wpcom_user_data = array( |
| 95 | 'ID' => get_current_user_id(), |
| 96 | 'login' => wp_get_current_user()->user_login, |
| 97 | ); |
| 98 | } else { |
| 99 | $wpcom_user_data = ( new Manager() )->get_connected_user_data(); |
| 100 | } |
| 101 | |
| 102 | $user_data['wpcom'] = array_merge( |
| 103 | $user_data['wpcom'] ?? array(), |
| 104 | $wpcom_user_data ? $wpcom_user_data : array() |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get the social script data for the Social dashboard when it opens on another admin page, via `wpcom/v2/publicize/script-data`. |
| 110 | * |
| 111 | * @since 0.89.0 |
| 112 | * |
| 113 | * @return array |
| 114 | */ |
| 115 | public static function get_social_script_data() { |
| 116 | /** This filter is documented in set_admin_script_data(). */ |
| 117 | return apply_filters( 'jetpack_social_admin_script_data', self::build_admin_script_data(), array() ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get the script data for admin UI, on the pages that print it inline: Social, Jetpack Settings and the block editor. |
| 122 | * |
| 123 | * @return array |
| 124 | */ |
| 125 | public static function get_admin_script_data() { |
| 126 | if ( ! Utils::is_social_settings_page() && ! Utils::is_jetpack_settings_page() && ! Utils::should_block_editor_have_social() ) { |
| 127 | return array(); |
| 128 | } |
| 129 | |
| 130 | return self::build_admin_script_data(); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Build the script data for admin UI, whatever the current page. |
| 135 | * |
| 136 | * @return array |
| 137 | */ |
| 138 | private static function build_admin_script_data() { |
| 139 | |
| 140 | $basic_data = array( |
| 141 | 'api_paths' => self::get_api_paths(), |
| 142 | 'assets_url' => plugins_url( '/build/', __DIR__ ), |
| 143 | 'is_publicize_enabled' => Utils::is_publicize_active(), |
| 144 | 'message_templates' => array(), |
| 145 | 'supported_services' => array(), |
| 146 | 'upgrade' => self::get_upgrade_data(), |
| 147 | 'urls' => array(), |
| 148 | 'settings' => self::get_social_settings(), |
| 149 | 'plugin_info' => self::get_plugin_info(), |
| 150 | 'nonces' => self::get_nonces(), |
| 151 | ); |
| 152 | |
| 153 | if ( ! Utils::is_publicize_active() ) { |
| 154 | return $basic_data; |
| 155 | } |
| 156 | |
| 157 | // Simple sites don't have a user connection. |
| 158 | $is_publicize_configured = ( new Host() )->is_wpcom_simple() || Utils::is_connected(); |
| 159 | |
| 160 | if ( ! $is_publicize_configured ) { |
| 161 | return $basic_data; |
| 162 | } |
| 163 | |
| 164 | return array_merge( |
| 165 | $basic_data, |
| 166 | array( |
| 167 | 'supported_services' => self::get_supported_services(), |
| 168 | 'urls' => self::get_urls(), |
| 169 | 'store_initial_state' => self::get_store_initial_state(), |
| 170 | 'message_templates' => array( |
| 171 | 'placeholders' => Message_Templates_Placeholders::get_all(), |
| 172 | ), |
| 173 | ) |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Get the plan a site needs to unlock Social's paid features. |
| 179 | * |
| 180 | * Null off Simple, which keeps the uncached `Plans::get_plan_short_name()` lookup |
| 181 | * away from a path the block editor runs on every post edit. |
| 182 | * |
| 183 | * @return array|null The plan slug and short name, or null when there's nothing to upsell. |
| 184 | */ |
| 185 | public static function get_upgrade_data() { |
| 186 | |
| 187 | if ( ! ( new Host() )->is_wpcom_simple() ) { |
| 188 | return null; |
| 189 | } |
| 190 | |
| 191 | if ( Current_Plan::supports( 'social-enhanced-publishing' ) ) { |
| 192 | return null; |
| 193 | } |
| 194 | |
| 195 | return array( |
| 196 | 'plan_slug' => self::UPGRADE_PLAN_SLUG, |
| 197 | 'plan_name' => Plans::get_plan_short_name( self::UPGRADE_PLAN_SLUG ), |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Get the social settings. |
| 203 | * |
| 204 | * @return array |
| 205 | */ |
| 206 | public static function get_social_settings() { |
| 207 | |
| 208 | $settings = ( new Settings() ); |
| 209 | |
| 210 | return array( |
| 211 | 'socialImageGenerator' => $settings->get_image_generator_settings(), |
| 212 | 'utmSettings' => $settings->get_utm_settings(), |
| 213 | 'socialNotes' => array( |
| 214 | 'enabled' => $settings->is_social_notes_enabled(), |
| 215 | 'config' => $settings->get_social_notes_config(), |
| 216 | ), |
| 217 | 'showPricingPage' => $settings->should_show_pricing_page(), |
| 218 | 'messageTemplate' => $settings->get_message_template(), |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Get the plugin info. |
| 224 | * |
| 225 | * @return array |
| 226 | */ |
| 227 | public static function get_plugin_info() { |
| 228 | |
| 229 | $social_version = null; |
| 230 | $jetpack_version = null; |
| 231 | |
| 232 | if ( defined( 'JETPACK_SOCIAL_PLUGIN_ROOT_FILE' ) ) { |
| 233 | // REST requests don't load the admin includes. |
| 234 | if ( ! function_exists( 'get_plugin_data' ) ) { |
| 235 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 236 | } |
| 237 | |
| 238 | $plugin_data = get_plugin_data( (string) constant( 'JETPACK_SOCIAL_PLUGIN_ROOT_FILE' ), false, false ); |
| 239 | |
| 240 | $social_version = $plugin_data['Version']; |
| 241 | } |
| 242 | |
| 243 | if ( defined( 'JETPACK__VERSION' ) ) { |
| 244 | $jetpack_version = constant( 'JETPACK__VERSION' ); |
| 245 | } |
| 246 | |
| 247 | return array( |
| 248 | 'social' => array( |
| 249 | 'version' => $social_version, |
| 250 | ), |
| 251 | 'jetpack' => array( |
| 252 | 'version' => $jetpack_version, |
| 253 | ), |
| 254 | ); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Get the social store initial state. |
| 259 | * |
| 260 | * @return array |
| 261 | */ |
| 262 | public static function get_store_initial_state() { |
| 263 | |
| 264 | $post = get_post(); |
| 265 | |
| 266 | $share_status = array(); |
| 267 | |
| 268 | // get_post_share_status is not available on WPCOM yet. |
| 269 | if ( Utils::should_block_editor_have_social() && $post ) { |
| 270 | $share_status[ $post->ID ] = Share_Status::get_post_share_status( $post->ID ); |
| 271 | } |
| 272 | |
| 273 | return array( |
| 274 | 'connectionData' => array( |
| 275 | // Same gate the block editor assets are enqueued behind, so users who |
| 276 | // never get the Social UI are not handed connection details either. |
| 277 | 'connections' => Utils::current_user_can_access_publicize_data() ? Connections::get_all_for_user() : array(), |
| 278 | ), |
| 279 | 'shareStatus' => $share_status, |
| 280 | ); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Whether the site has the feature flag enabled. |
| 285 | * |
| 286 | * @deprecated 0.69.1 Use Current_Plan::supports() directly instead. |
| 287 | * |
| 288 | * @todo Remove this method After March 2026. |
| 289 | * |
| 290 | * @param string $feature The feature name to check for, without the "social-" prefix. |
| 291 | * @return bool |
| 292 | */ |
| 293 | public static function has_feature_flag( $feature ): bool { |
| 294 | return Current_Plan::supports( 'social-' . $feature ); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Get the list of supported Publicize services. |
| 299 | * |
| 300 | * @return array List of external services and their settings. |
| 301 | */ |
| 302 | public static function get_supported_services() { |
| 303 | return Publicize_Services::get_all(); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Get the API paths. |
| 308 | * |
| 309 | * @return array |
| 310 | */ |
| 311 | public static function get_api_paths() { |
| 312 | |
| 313 | return array( |
| 314 | // The complete path will be like `/jetpack/v4/social/settings`. |
| 315 | 'socialToggleBase' => Utils::should_use_jetpack_module_endpoint() ? 'settings' : 'social/settings', |
| 316 | 'resharePost' => '/wpcom/v2/publicize/share-post/{postId}', |
| 317 | ); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Get the URLs. |
| 322 | * |
| 323 | * @return array |
| 324 | */ |
| 325 | public static function get_urls() { |
| 326 | |
| 327 | $urls = array( |
| 328 | 'connectionsManagementPage' => self::publicize()->publicize_connections_url(), |
| 329 | ); |
| 330 | |
| 331 | // Escape the URLs. |
| 332 | array_walk( $urls, 'esc_url_raw' ); |
| 333 | |
| 334 | return $urls; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Get nonces required by the Social admin UI. |
| 339 | * |
| 340 | * @return array |
| 341 | */ |
| 342 | private static function get_nonces() { |
| 343 | return array( |
| 344 | 'refresh_plan' => wp_create_nonce( Social_Admin_Page::REFRESH_PLAN_NONCE_ACTION ), |
| 345 | ); |
| 346 | } |
| 347 | } |