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