PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.3
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-publicize / src / class-publicize-utils.php

class-publicize-utils.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.3, at jetpack_vendor/automattic/jetpack-publicize/src/class-publicize-utils.php

204 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * 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 }
204