Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
12.00% covered (danger)
12.00%
6 / 50
18.18% covered (danger)
18.18%
2 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Publicize_Utils
12.00% covered (danger)
12.00%
6 / 50
18.18% covered (danger)
18.18%
2 / 11
729.83
0.00% covered (danger)
0.00%
0 / 1
 is_social_settings_page
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
12
 is_jetpack_settings_page
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
12
 should_block_editor_have_social
16.67% covered (danger)
16.67%
2 / 12
0.00% covered (danger)
0.00%
0 / 1
55.88
 is_connected
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 is_publicize_active
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 current_user_can_access_publicize_data
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 is_wpcom
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 assert_is_wpcom
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 should_use_jetpack_module_endpoint
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
12
 endpoint_deprecated_warning
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 make_proper_response
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Publicize_Utils.
4 *
5 * @package automattic/jetpack-publicize
6 */
7
8namespace Automattic\Jetpack\Publicize;
9
10use Automattic\Jetpack\Connection\Manager;
11use Automattic\Jetpack\Modules;
12use Automattic\Jetpack\Status\Host;
13use WP_Error;
14
15/**
16 * Publicize_Utils class.
17 */
18class Publicize_Utils {
19
20    /**
21     * Whether the current page is the social settings page.
22     */
23    public static function is_social_settings_page() {
24        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
25
26        return ! empty( $screen ) && 'jetpack_page_jetpack-social' === $screen->base;
27    }
28
29    /**
30     * Whether the current page is the Jetpack settings page.
31     */
32    public static function is_jetpack_settings_page() {
33        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
34
35        return ! empty( $screen ) && 'toplevel_page_jetpack' === $screen->base;
36    }
37
38    /**
39     * Whether the block editor should have the social features.
40     *
41     * @return bool
42     */
43    public static function should_block_editor_have_social() {
44        if ( ! is_admin() ) {
45            return false;
46        }
47
48        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
49
50        if ( empty( $screen ) || ! $screen->is_block_editor() ) {
51            return false;
52        }
53
54        $needs_jetpack_connection = ! ( new Host() )->is_wpcom_platform();
55
56        if ( $needs_jetpack_connection && ! self::is_connected() ) {
57            return false;
58        }
59
60        $post_type = get_post_type();
61
62        if ( empty( $post_type ) || ! post_type_supports( $post_type, 'publicize' ) ) {
63            return false;
64        }
65
66        return true;
67    }
68
69    /**
70     * Helper to check that we have a Jetpack connection.
71     */
72    public static function is_connected() {
73
74        $connection = new Manager();
75
76        return $connection->is_connected() && $connection->has_connected_user();
77    }
78
79    /**
80     * Check if the Publicize module is active.
81     *
82     * @return bool
83     */
84    public static function is_publicize_active() {
85        return ( new Modules() )->is_active( 'publicize' );
86    }
87
88    /**
89     * Whether the current user may be shown Publicize data.
90     *
91     * The single definition of that gate. Both the block editor assets and the
92     * script data ask this, and when they disagreed the script data handed
93     * connection details to users the editor UI was never loaded for.
94     *
95     * Prefers the Publicize instance so any future override is honored, and falls
96     * back to evaluating the capability directly when the global is not set, so the
97     * answer never depends on initialization order.
98     *
99     * @return bool
100     */
101    public static function current_user_can_access_publicize_data() {
102
103        global $publicize;
104
105        if ( $publicize instanceof Publicize_Base ) {
106            return $publicize->current_user_can_access_publicize_data();
107        }
108
109        /** This filter is documented in projects/packages/publicize/src/class-publicize-base.php */
110        return current_user_can( apply_filters( 'jetpack_publicize_capability', 'publish_posts' ) );
111    }
112
113    /**
114     * Check if we are on WPCOM.
115     *
116     * @return bool
117     */
118    public static function is_wpcom() {
119        return ( new Host() )->is_wpcom_simple();
120    }
121
122    /**
123     * Assert that the method is only called on WPCOM.
124     *
125     * @param string $method The method name.
126     *
127     * @throws \Exception If the method is not called on WPCOM.
128     */
129    public static function assert_is_wpcom( $method ) {
130        if ( ! self::is_wpcom() ) {
131            throw new \Exception( esc_html( "Method $method can only be called on WordPress.com." ) );
132        }
133    }
134
135    /**
136     * Check if the new module endpoint is available in the used Jetpack version.
137     * We need the module status in response that's why we do the version check https://github.com/Automattic/jetpack/pull/41461/files#diff-f8e5ef1115599de750b64143dd1901554254eddd95ab4371b6b6b3b2a5914224R638-R642.
138     * More: https://github.com/Automattic/jetpack/pull/41596.
139     *
140     * @return bool
141     */
142    public static function should_use_jetpack_module_endpoint() {
143        return class_exists( 'Jetpack' ) && defined( 'JETPACK__VERSION' ) && ( version_compare( (string) JETPACK__VERSION, '14.3', '>=' ) );
144    }
145
146    /**
147     * Log a warning that a deprecated endpoint was called.
148     *
149     * @param string $function_name        The function name.
150     * @param string $version              The version in which the endpoint was deprecated.
151     * @param string $deprecated_endpoint  The deprecated endpoint.
152     * @param string $alternative_endpoint The alternative endpoint.
153     */
154    public static function endpoint_deprecated_warning( $function_name, $version, $deprecated_endpoint, $alternative_endpoint = '' ) {
155
156        $messages = array(
157            sprintf(
158                /* translators: %s: REST API endpoint. */
159                esc_html__( '%1$s endpoint has been deprecated.', 'jetpack-publicize-pkg' ),
160                '"' . $deprecated_endpoint . '"'
161            ),
162        );
163
164        if ( ! empty( $alternative_endpoint ) ) {
165            $messages[] = sprintf(
166                /* translators: %s: alternative endpoint. */
167                esc_html__( 'Please use %s endpoint instead.', 'jetpack-publicize-pkg' ),
168                '"' . $alternative_endpoint . '"'
169            );
170        }
171
172        $messages[] = esc_html__( 'Please update all the Jetpack plugins to the latest version.', 'jetpack-publicize-pkg' );
173
174        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- We have done it above.
175        _doing_it_wrong( esc_html( $function_name ), implode( ' ', $messages ), $version );
176    }
177
178    /**
179     * Forward remote response to client with error handling.
180     *
181     * @param array|WP_Error $response Response from WPCOM.
182     *
183     * @return array|WP_Error
184     */
185    public static function make_proper_response( $response ) {
186        if ( is_wp_error( $response ) ) {
187            return $response;
188        }
189
190        $body        = json_decode( wp_remote_retrieve_body( $response ), true );
191        $status_code = wp_remote_retrieve_response_code( $response );
192
193        if ( 200 === $status_code ) {
194            return $body;
195        }
196
197        return new WP_Error(
198            isset( $body['error'] ) ? 'remote-error-' . $body['error'] : 'remote-error',
199            $body['message'] ?? 'unknown remote error',
200            array( 'status' => $status_code )
201        );
202    }
203}