Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
38.89% covered (danger)
38.89%
14 / 36
22.22% covered (danger)
22.22%
2 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Publicize_Utils
38.89% covered (danger)
38.89%
14 / 36
22.22% covered (danger)
22.22%
2 / 9
180.28
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
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
90
 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
 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
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
2
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;
13
14/**
15 * Publicize_Utils class.
16 */
17class Publicize_Utils {
18
19    /**
20     * Whether the current page is the social settings page.
21     */
22    public static function is_social_settings_page() {
23        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
24
25        return ! empty( $screen ) && 'jetpack_page_jetpack-social' === $screen->base;
26    }
27
28    /**
29     * Whether the current page is the Jetpack settings page.
30     */
31    public static function is_jetpack_settings_page() {
32        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
33
34        return ! empty( $screen ) && 'toplevel_page_jetpack' === $screen->base;
35    }
36
37    /**
38     * Whether the block editor should have the social features.
39     *
40     * @return bool
41     */
42    public static function should_block_editor_have_social() {
43        if ( ! is_admin() ) {
44            return false;
45        }
46
47        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
48
49        if ( empty( $screen ) || ! $screen->is_block_editor() ) {
50            return false;
51        }
52
53        $needs_jetpack_connection = ! ( new Host() )->is_wpcom_platform();
54
55        if ( $needs_jetpack_connection && ! self::is_connected() ) {
56            return false;
57        }
58
59        $post_type = get_post_type();
60
61        if ( empty( $post_type ) || ! post_type_supports( $post_type, 'publicize' ) ) {
62            return false;
63        }
64
65        return true;
66    }
67
68    /**
69     * Helper to check that we have a Jetpack connection.
70     */
71    public static function is_connected() {
72
73        $connection = new Manager();
74
75        return $connection->is_connected() && $connection->has_connected_user();
76    }
77
78    /**
79     * Check if the Publicize module is active.
80     *
81     * @return bool
82     */
83    public static function is_publicize_active() {
84        return ( new Modules() )->is_active( 'publicize' );
85    }
86
87    /**
88     * Check if we are on WPCOM.
89     *
90     * @return bool
91     */
92    public static function is_wpcom() {
93        return ( new Host() )->is_wpcom_simple();
94    }
95
96    /**
97     * Assert that the method is only called on WPCOM.
98     *
99     * @param string $method The method name.
100     *
101     * @throws \Exception If the method is not called on WPCOM.
102     */
103    public static function assert_is_wpcom( $method ) {
104        if ( ! self::is_wpcom() ) {
105            throw new \Exception( esc_html( "Method $method can only be called on WordPress.com." ) );
106        }
107    }
108
109    /**
110     * Check if the new module endpoint is available in the used Jetpack version.
111     * 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.
112     * More: https://github.com/Automattic/jetpack/pull/41596.
113     *
114     * @return bool
115     */
116    public static function should_use_jetpack_module_endpoint() {
117        return class_exists( 'Jetpack' ) && defined( 'JETPACK__VERSION' ) && ( version_compare( (string) JETPACK__VERSION, '14.3', '>=' ) );
118    }
119
120    /**
121     * Log a warning that a deprecated endpoint was called.
122     *
123     * @param string $function_name        The function name.
124     * @param string $version              The version in which the endpoint was deprecated.
125     * @param string $deprecated_endpoint  The deprecated endpoint.
126     * @param string $alternative_endpoint The alternative endpoint.
127     */
128    public static function endpoint_deprecated_warning( $function_name, $version, $deprecated_endpoint, $alternative_endpoint = '' ) {
129
130        $messages = array(
131            sprintf(
132                /* translators: %s: REST API endpoint. */
133                esc_html__( '%1$s endpoint has been deprecated.', 'jetpack-publicize-pkg' ),
134                '"' . $deprecated_endpoint . '"'
135            ),
136        );
137
138        if ( ! empty( $alternative_endpoint ) ) {
139            $messages[] = sprintf(
140                /* translators: %s: alternative endpoint. */
141                esc_html__( 'Please use %s endpoint instead.', 'jetpack-publicize-pkg' ),
142                '"' . $alternative_endpoint . '"'
143            );
144        }
145
146        $messages[] = esc_html__( 'Please update all the Jetpack plugins to the latest version.', 'jetpack-publicize-pkg' );
147
148        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- We have done it above.
149        _doing_it_wrong( esc_html( $function_name ), implode( ' ', $messages ), $version );
150    }
151}