Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
2.13% |
1 / 47 |
|
10.00% |
1 / 10 |
CRAP | |
0.00% |
0 / 1 |
| Publicize_Utils | |
2.13% |
1 / 47 |
|
10.00% |
1 / 10 |
873.77 | |
0.00% |
0 / 1 |
| is_social_settings_page | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
12 | |||
| is_jetpack_settings_page | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
12 | |||
| should_block_editor_have_social | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
90 | |||
| is_connected | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| is_publicize_active | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| is_wpcom | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| assert_is_wpcom | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| should_use_jetpack_module_endpoint | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
12 | |||
| endpoint_deprecated_warning | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| make_proper_response | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Publicize_Utils. |
| 4 | * |
| 5 | * @package automattic/jetpack-publicize |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Publicize; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Manager; |
| 11 | use Automattic\Jetpack\Modules; |
| 12 | use Automattic\Jetpack\Status\Host; |
| 13 | use WP_Error; |
| 14 | |
| 15 | /** |
| 16 | * Publicize_Utils class. |
| 17 | */ |
| 18 | class 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 | * Check if we are on WPCOM. |
| 90 | * |
| 91 | * @return bool |
| 92 | */ |
| 93 | public static function is_wpcom() { |
| 94 | return ( new Host() )->is_wpcom_simple(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Assert that the method is only called on WPCOM. |
| 99 | * |
| 100 | * @param string $method The method name. |
| 101 | * |
| 102 | * @throws \Exception If the method is not called on WPCOM. |
| 103 | */ |
| 104 | public static function assert_is_wpcom( $method ) { |
| 105 | if ( ! self::is_wpcom() ) { |
| 106 | throw new \Exception( esc_html( "Method $method can only be called on WordPress.com." ) ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Check if the new module endpoint is available in the used Jetpack version. |
| 112 | * 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. |
| 113 | * More: https://github.com/Automattic/jetpack/pull/41596. |
| 114 | * |
| 115 | * @return bool |
| 116 | */ |
| 117 | public static function should_use_jetpack_module_endpoint() { |
| 118 | return class_exists( 'Jetpack' ) && defined( 'JETPACK__VERSION' ) && ( version_compare( (string) JETPACK__VERSION, '14.3', '>=' ) ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Log a warning that a deprecated endpoint was called. |
| 123 | * |
| 124 | * @param string $function_name The function name. |
| 125 | * @param string $version The version in which the endpoint was deprecated. |
| 126 | * @param string $deprecated_endpoint The deprecated endpoint. |
| 127 | * @param string $alternative_endpoint The alternative endpoint. |
| 128 | */ |
| 129 | public static function endpoint_deprecated_warning( $function_name, $version, $deprecated_endpoint, $alternative_endpoint = '' ) { |
| 130 | |
| 131 | $messages = array( |
| 132 | sprintf( |
| 133 | /* translators: %s: REST API endpoint. */ |
| 134 | esc_html__( '%1$s endpoint has been deprecated.', 'jetpack-publicize-pkg' ), |
| 135 | '"' . $deprecated_endpoint . '"' |
| 136 | ), |
| 137 | ); |
| 138 | |
| 139 | if ( ! empty( $alternative_endpoint ) ) { |
| 140 | $messages[] = sprintf( |
| 141 | /* translators: %s: alternative endpoint. */ |
| 142 | esc_html__( 'Please use %s endpoint instead.', 'jetpack-publicize-pkg' ), |
| 143 | '"' . $alternative_endpoint . '"' |
| 144 | ); |
| 145 | } |
| 146 | |
| 147 | $messages[] = esc_html__( 'Please update all the Jetpack plugins to the latest version.', 'jetpack-publicize-pkg' ); |
| 148 | |
| 149 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- We have done it above. |
| 150 | _doing_it_wrong( esc_html( $function_name ), implode( ' ', $messages ), $version ); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Forward remote response to client with error handling. |
| 155 | * |
| 156 | * @param array|WP_Error $response Response from WPCOM. |
| 157 | * |
| 158 | * @return array|WP_Error |
| 159 | */ |
| 160 | public static function make_proper_response( $response ) { |
| 161 | if ( is_wp_error( $response ) ) { |
| 162 | return $response; |
| 163 | } |
| 164 | |
| 165 | $body = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 166 | $status_code = wp_remote_retrieve_response_code( $response ); |
| 167 | |
| 168 | if ( 200 === $status_code ) { |
| 169 | return $body; |
| 170 | } |
| 171 | |
| 172 | return new WP_Error( |
| 173 | isset( $body['error'] ) ? 'remote-error-' . $body['error'] : 'remote-error', |
| 174 | $body['message'] ?? 'unknown remote error', |
| 175 | array( 'status' => $status_code ) |
| 176 | ); |
| 177 | } |
| 178 | } |