Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Manager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Manager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Manager { |
||
23 | |||
24 | const SECRETS_MISSING = 'secrets_missing'; |
||
25 | const SECRETS_EXPIRED = 'secrets_expired'; |
||
26 | const SECRETS_OPTION_NAME = 'jetpack_secrets'; |
||
27 | const MAGIC_NORMAL_TOKEN_KEY = ';normal;'; |
||
28 | const JETPACK_MASTER_USER = true; |
||
29 | |||
30 | /** |
||
31 | * The procedure that should be run to generate secrets. |
||
32 | * |
||
33 | * @var Callable |
||
34 | */ |
||
35 | protected $secret_callable; |
||
36 | |||
37 | /** |
||
38 | * A copy of the raw POST data for signature verification purposes. |
||
39 | * |
||
40 | * @var String |
||
41 | */ |
||
42 | protected $raw_post_data; |
||
43 | |||
44 | /** |
||
45 | * Verification data needs to be stored to properly verify everything. |
||
46 | * |
||
47 | * @var Object |
||
48 | */ |
||
49 | private $xmlrpc_verification = null; |
||
50 | |||
51 | /** |
||
52 | * Plugin management object. |
||
53 | * |
||
54 | * @var Plugin |
||
55 | */ |
||
56 | private $plugin = null; |
||
57 | |||
58 | /** |
||
59 | * Initialize the object. |
||
60 | * Make sure to call the "Configure" first. |
||
61 | * |
||
62 | * @param string $plugin_slug Slug of the plugin using the connection (optional, but encouraged). |
||
|
|||
63 | * |
||
64 | * @see \Automattic\Jetpack\Config |
||
65 | */ |
||
66 | public function __construct( $plugin_slug = null ) { |
||
67 | if ( $plugin_slug && is_string( $plugin_slug ) ) { |
||
68 | $this->set_plugin_instance( new Plugin( $plugin_slug ) ); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Initializes required listeners. This is done separately from the constructors |
||
74 | * because some objects sometimes need to instantiate separate objects of this class. |
||
75 | * |
||
76 | * @todo Implement a proper nonce verification. |
||
77 | */ |
||
78 | public static function configure() { |
||
79 | $manager = new self(); |
||
80 | |||
81 | add_filter( |
||
82 | 'jetpack_constant_default_value', |
||
83 | __NAMESPACE__ . '\Utils::jetpack_api_constant_filter', |
||
84 | 10, |
||
85 | 2 |
||
86 | ); |
||
87 | |||
88 | $manager->setup_xmlrpc_handlers( |
||
89 | $_GET, // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
90 | $manager->is_active(), |
||
91 | $manager->verify_xml_rpc_signature() |
||
92 | ); |
||
93 | |||
94 | $manager->error_handler = Error_Handler::get_instance(); |
||
95 | |||
96 | if ( $manager->is_active() ) { |
||
97 | add_filter( 'xmlrpc_methods', array( $manager, 'public_xmlrpc_methods' ) ); |
||
98 | } |
||
99 | |||
100 | add_action( 'rest_api_init', array( $manager, 'initialize_rest_api_registration_connector' ) ); |
||
101 | |||
102 | add_action( 'jetpack_clean_nonces', array( $manager, 'clean_nonces' ) ); |
||
103 | if ( ! wp_next_scheduled( 'jetpack_clean_nonces' ) ) { |
||
104 | wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
||
105 | } |
||
106 | |||
107 | add_action( 'plugins_loaded', __NAMESPACE__ . '\Plugin_Storage::configure', 100 ); |
||
108 | |||
109 | add_filter( 'map_meta_cap', array( $manager, 'jetpack_connection_custom_caps' ), 1, 4 ); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Sets up the XMLRPC request handlers. |
||
114 | * |
||
115 | * @param array $request_params incoming request parameters. |
||
116 | * @param Boolean $is_active whether the connection is currently active. |
||
117 | * @param Boolean $is_signed whether the signature check has been successful. |
||
118 | * @param \Jetpack_XMLRPC_Server $xmlrpc_server (optional) an instance of the server to use instead of instantiating a new one. |
||
119 | */ |
||
120 | public function setup_xmlrpc_handlers( |
||
121 | $request_params, |
||
122 | $is_active, |
||
123 | $is_signed, |
||
124 | \Jetpack_XMLRPC_Server $xmlrpc_server = null |
||
125 | ) { |
||
126 | add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ), 1000, 2 ); |
||
127 | |||
128 | if ( |
||
129 | ! isset( $request_params['for'] ) |
||
130 | || 'jetpack' !== $request_params['for'] |
||
131 | ) { |
||
132 | return false; |
||
133 | } |
||
134 | |||
135 | // Alternate XML-RPC, via ?for=jetpack&jetpack=comms. |
||
136 | if ( |
||
137 | isset( $request_params['jetpack'] ) |
||
138 | && 'comms' === $request_params['jetpack'] |
||
139 | ) { |
||
140 | if ( ! Constants::is_defined( 'XMLRPC_REQUEST' ) ) { |
||
141 | // Use the real constant here for WordPress' sake. |
||
142 | define( 'XMLRPC_REQUEST', true ); |
||
143 | } |
||
144 | |||
145 | add_action( 'template_redirect', array( $this, 'alternate_xmlrpc' ) ); |
||
146 | |||
147 | add_filter( 'xmlrpc_methods', array( $this, 'remove_non_jetpack_xmlrpc_methods' ), 1000 ); |
||
148 | } |
||
149 | |||
150 | if ( ! Constants::get_constant( 'XMLRPC_REQUEST' ) ) { |
||
151 | return false; |
||
152 | } |
||
153 | // Display errors can cause the XML to be not well formed. |
||
154 | @ini_set( 'display_errors', false ); // phpcs:ignore |
||
155 | |||
156 | if ( $xmlrpc_server ) { |
||
157 | $this->xmlrpc_server = $xmlrpc_server; |
||
158 | } else { |
||
159 | $this->xmlrpc_server = new \Jetpack_XMLRPC_Server(); |
||
160 | } |
||
161 | |||
162 | $this->require_jetpack_authentication(); |
||
163 | |||
164 | if ( $is_active ) { |
||
165 | // Hack to preserve $HTTP_RAW_POST_DATA. |
||
166 | add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
||
167 | |||
168 | if ( $is_signed ) { |
||
169 | // The actual API methods. |
||
170 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) ); |
||
171 | } else { |
||
172 | // The jetpack.authorize method should be available for unauthenticated users on a site with an |
||
173 | // active Jetpack connection, so that additional users can link their account. |
||
174 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'authorize_xmlrpc_methods' ) ); |
||
175 | } |
||
176 | } else { |
||
177 | // The bootstrap API methods. |
||
178 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) ); |
||
179 | |||
180 | if ( $is_signed ) { |
||
181 | // The jetpack Provision method is available for blog-token-signed requests. |
||
182 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'provision_xmlrpc_methods' ) ); |
||
183 | } else { |
||
184 | new XMLRPC_Connector( $this ); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | // Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on. |
||
189 | add_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
||
190 | return true; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Initializes the REST API connector on the init hook. |
||
195 | */ |
||
196 | public function initialize_rest_api_registration_connector() { |
||
197 | new REST_Connector( $this ); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Since a lot of hosts use a hammer approach to "protecting" WordPress sites, |
||
202 | * and just blanket block all requests to /xmlrpc.php, or apply other overly-sensitive |
||
203 | * security/firewall policies, we provide our own alternate XML RPC API endpoint |
||
204 | * which is accessible via a different URI. Most of the below is copied directly |
||
205 | * from /xmlrpc.php so that we're replicating it as closely as possible. |
||
206 | * |
||
207 | * @todo Tighten $wp_xmlrpc_server_class a bit to make sure it doesn't do bad things. |
||
208 | */ |
||
209 | public function alternate_xmlrpc() { |
||
210 | // phpcs:disable PHPCompatibility.Variables.RemovedPredefinedGlobalVariables.http_raw_post_dataDeprecatedRemoved |
||
211 | // phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited |
||
212 | global $HTTP_RAW_POST_DATA; |
||
213 | |||
214 | // Some browser-embedded clients send cookies. We don't want them. |
||
215 | $_COOKIE = array(); |
||
216 | |||
217 | // A fix for mozBlog and other cases where '<?xml' isn't on the very first line. |
||
218 | if ( isset( $HTTP_RAW_POST_DATA ) ) { |
||
219 | $HTTP_RAW_POST_DATA = trim( $HTTP_RAW_POST_DATA ); |
||
220 | } |
||
221 | |||
222 | // phpcs:enable |
||
223 | |||
224 | include_once ABSPATH . 'wp-admin/includes/admin.php'; |
||
225 | include_once ABSPATH . WPINC . '/class-IXR.php'; |
||
226 | include_once ABSPATH . WPINC . '/class-wp-xmlrpc-server.php'; |
||
227 | |||
228 | /** |
||
229 | * Filters the class used for handling XML-RPC requests. |
||
230 | * |
||
231 | * @since 3.1.0 |
||
232 | * |
||
233 | * @param string $class The name of the XML-RPC server class. |
||
234 | */ |
||
235 | $wp_xmlrpc_server_class = apply_filters( 'wp_xmlrpc_server_class', 'wp_xmlrpc_server' ); |
||
236 | $wp_xmlrpc_server = new $wp_xmlrpc_server_class(); |
||
237 | |||
238 | // Fire off the request. |
||
239 | nocache_headers(); |
||
240 | $wp_xmlrpc_server->serve_request(); |
||
241 | |||
242 | exit; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Removes all XML-RPC methods that are not `jetpack.*`. |
||
247 | * Only used in our alternate XML-RPC endpoint, where we want to |
||
248 | * ensure that Core and other plugins' methods are not exposed. |
||
249 | * |
||
250 | * @param array $methods a list of registered WordPress XMLRPC methods. |
||
251 | * @return array filtered $methods |
||
252 | */ |
||
253 | public function remove_non_jetpack_xmlrpc_methods( $methods ) { |
||
254 | $jetpack_methods = array(); |
||
255 | |||
256 | foreach ( $methods as $method => $callback ) { |
||
257 | if ( 0 === strpos( $method, 'jetpack.' ) ) { |
||
258 | $jetpack_methods[ $method ] = $callback; |
||
259 | } |
||
260 | } |
||
261 | |||
262 | return $jetpack_methods; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Removes all other authentication methods not to allow other |
||
267 | * methods to validate unauthenticated requests. |
||
268 | */ |
||
269 | public function require_jetpack_authentication() { |
||
270 | // Don't let anyone authenticate. |
||
271 | $_COOKIE = array(); |
||
272 | remove_all_filters( 'authenticate' ); |
||
273 | remove_all_actions( 'wp_login_failed' ); |
||
274 | |||
275 | if ( $this->is_active() ) { |
||
276 | // Allow Jetpack authentication. |
||
277 | add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 ); |
||
278 | } |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Authenticates XML-RPC and other requests from the Jetpack Server |
||
283 | * |
||
284 | * @param WP_User|Mixed $user user object if authenticated. |
||
285 | * @param String $username username. |
||
286 | * @param String $password password string. |
||
287 | * @return WP_User|Mixed authenticated user or error. |
||
288 | */ |
||
289 | public function authenticate_jetpack( $user, $username, $password ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
290 | if ( is_a( $user, '\\WP_User' ) ) { |
||
291 | return $user; |
||
292 | } |
||
293 | |||
294 | $token_details = $this->verify_xml_rpc_signature(); |
||
295 | |||
296 | if ( ! $token_details ) { |
||
297 | return $user; |
||
298 | } |
||
299 | |||
300 | if ( 'user' !== $token_details['type'] ) { |
||
301 | return $user; |
||
302 | } |
||
303 | |||
304 | if ( ! $token_details['user_id'] ) { |
||
305 | return $user; |
||
306 | } |
||
307 | |||
308 | nocache_headers(); |
||
309 | |||
310 | return new \WP_User( $token_details['user_id'] ); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Verifies the signature of the current request. |
||
315 | * |
||
316 | * @return false|array |
||
317 | */ |
||
318 | public function verify_xml_rpc_signature() { |
||
319 | if ( is_null( $this->xmlrpc_verification ) ) { |
||
320 | $this->xmlrpc_verification = $this->internal_verify_xml_rpc_signature(); |
||
321 | |||
322 | if ( is_wp_error( $this->xmlrpc_verification ) ) { |
||
323 | /** |
||
324 | * Action for logging XMLRPC signature verification errors. This data is sensitive. |
||
325 | * |
||
326 | * @since 7.5.0 |
||
327 | * |
||
328 | * @param WP_Error $signature_verification_error The verification error |
||
329 | */ |
||
330 | do_action( 'jetpack_verify_signature_error', $this->xmlrpc_verification ); |
||
331 | |||
332 | Error_Handler::get_instance()->report_error( $this->xmlrpc_verification ); |
||
333 | |||
334 | } |
||
335 | } |
||
336 | |||
337 | return is_wp_error( $this->xmlrpc_verification ) ? false : $this->xmlrpc_verification; |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Verifies the signature of the current request. |
||
342 | * |
||
343 | * This function has side effects and should not be used. Instead, |
||
344 | * use the memoized version `->verify_xml_rpc_signature()`. |
||
345 | * |
||
346 | * @internal |
||
347 | * @todo Refactor to use proper nonce verification. |
||
348 | */ |
||
349 | private function internal_verify_xml_rpc_signature() { |
||
506 | |||
507 | /** |
||
508 | * Returns true if the current site is connected to WordPress.com. |
||
509 | * |
||
510 | * @return Boolean is the site connected? |
||
511 | */ |
||
512 | public function is_active() { |
||
515 | |||
516 | /** |
||
517 | * Returns true if the site has both a token and a blog id, which indicates a site has been registered. |
||
518 | * |
||
519 | * @access public |
||
520 | * |
||
521 | * @return bool |
||
522 | */ |
||
523 | public function is_registered() { |
||
528 | |||
529 | /** |
||
530 | * Checks to see if the connection owner of the site is missing. |
||
531 | * |
||
532 | * @return bool |
||
533 | */ |
||
534 | public function is_missing_connection_owner() { |
||
542 | |||
543 | /** |
||
544 | * Returns true if the user with the specified identifier is connected to |
||
545 | * WordPress.com. |
||
546 | * |
||
547 | * @param Integer|Boolean $user_id the user identifier. |
||
548 | * @return Boolean is the user connected? |
||
549 | */ |
||
550 | public function is_user_connected( $user_id = false ) { |
||
558 | |||
559 | /** |
||
560 | * Returns the local user ID of the connection owner. |
||
561 | * |
||
562 | * @return string|int Returns the ID of the connection owner or False if no connection owner found. |
||
563 | */ |
||
564 | View Code Duplication | public function get_connection_owner_id() { |
|
573 | |||
574 | /** |
||
575 | * Returns an array of user_id's that have user tokens for communicating with wpcom. |
||
576 | * Able to select by specific capability. |
||
577 | * |
||
578 | * @param string $capability The capability of the user. |
||
579 | * @return array Array of WP_User objects if found. |
||
580 | */ |
||
581 | public function get_connected_users( $capability = 'any' ) { |
||
598 | |||
599 | /** |
||
600 | * Get the wpcom user data of the current|specified connected user. |
||
601 | * |
||
602 | * @todo Refactor to properly load the XMLRPC client independently. |
||
603 | * |
||
604 | * @param Integer $user_id the user identifier. |
||
605 | * @return Object the user object. |
||
606 | */ |
||
607 | View Code Duplication | public function get_connected_user_data( $user_id = null ) { |
|
633 | |||
634 | /** |
||
635 | * Returns a user object of the connection owner. |
||
636 | * |
||
637 | * @return object|false False if no connection owner found. |
||
638 | */ |
||
639 | View Code Duplication | public function get_connection_owner() { |
|
649 | |||
650 | /** |
||
651 | * Returns true if the provided user is the Jetpack connection owner. |
||
652 | * If user ID is not specified, the current user will be used. |
||
653 | * |
||
654 | * @param Integer|Boolean $user_id the user identifier. False for current user. |
||
655 | * @return Boolean True the user the connection owner, false otherwise. |
||
656 | */ |
||
657 | View Code Duplication | public function is_connection_owner( $user_id = false ) { |
|
666 | |||
667 | /** |
||
668 | * Connects the user with a specified ID to a WordPress.com user using the |
||
669 | * remote login flow. |
||
670 | * |
||
671 | * @access public |
||
672 | * |
||
673 | * @param Integer $user_id (optional) the user identifier, defaults to current user. |
||
674 | * @param String $redirect_url the URL to redirect the user to for processing, defaults to |
||
675 | * admin_url(). |
||
676 | * @return WP_Error only in case of a failed user lookup. |
||
677 | */ |
||
678 | public function connect_user( $user_id = null, $redirect_url = null ) { |
||
698 | |||
699 | /** |
||
700 | * Unlinks the current user from the linked WordPress.com user. |
||
701 | * |
||
702 | * @access public |
||
703 | * @static |
||
704 | * |
||
705 | * @todo Refactor to properly load the XMLRPC client independently. |
||
706 | * |
||
707 | * @param Integer $user_id the user identifier. |
||
708 | * @param bool $can_overwrite_primary_user Allow for the primary user to be disconnected. |
||
709 | * @return Boolean Whether the disconnection of the user was successful. |
||
710 | */ |
||
711 | public static function disconnect_user( $user_id = null, $can_overwrite_primary_user = false ) { |
||
749 | |||
750 | /** |
||
751 | * Returns the requested Jetpack API URL. |
||
752 | * |
||
753 | * @param String $relative_url the relative API path. |
||
754 | * @return String API URL. |
||
755 | */ |
||
756 | public function api_url( $relative_url ) { |
||
793 | |||
794 | /** |
||
795 | * Returns the Jetpack XMLRPC WordPress.com API endpoint URL. |
||
796 | * |
||
797 | * @return String XMLRPC API URL. |
||
798 | */ |
||
799 | public function xmlrpc_api_url() { |
||
807 | |||
808 | /** |
||
809 | * Attempts Jetpack registration which sets up the site for connection. Should |
||
810 | * remain public because the call to action comes from the current site, not from |
||
811 | * WordPress.com. |
||
812 | * |
||
813 | * @param String $api_endpoint (optional) an API endpoint to use, defaults to 'register'. |
||
814 | * @return true|WP_Error The error object. |
||
815 | */ |
||
816 | public function register( $api_endpoint = 'register' ) { |
||
952 | |||
953 | /** |
||
954 | * Takes the response from the Jetpack register new site endpoint and |
||
955 | * verifies it worked properly. |
||
956 | * |
||
957 | * @since 2.6 |
||
958 | * |
||
959 | * @param Mixed $response the response object, or the error object. |
||
960 | * @return string|WP_Error A JSON object on success or WP_Error on failures |
||
961 | **/ |
||
962 | protected function validate_remote_register_response( $response ) { |
||
1031 | |||
1032 | /** |
||
1033 | * Adds a used nonce to a list of known nonces. |
||
1034 | * |
||
1035 | * @param int $timestamp the current request timestamp. |
||
1036 | * @param string $nonce the nonce value. |
||
1037 | * @return bool whether the nonce is unique or not. |
||
1038 | */ |
||
1039 | public function add_nonce( $timestamp, $nonce ) { |
||
1077 | |||
1078 | /** |
||
1079 | * Cleans nonces that were saved when calling ::add_nonce. |
||
1080 | * |
||
1081 | * @todo Properly prepare the query before executing it. |
||
1082 | * |
||
1083 | * @param bool $all whether to clean even non-expired nonces. |
||
1084 | */ |
||
1085 | public function clean_nonces( $all = false ) { |
||
1106 | |||
1107 | /** |
||
1108 | * Sets the Connection custom capabilities. |
||
1109 | * |
||
1110 | * @param string[] $caps Array of the user's capabilities. |
||
1111 | * @param string $cap Capability name. |
||
1112 | * @param int $user_id The user ID. |
||
1113 | * @param array $args Adds the context to the cap. Typically the object ID. |
||
1114 | */ |
||
1115 | public function jetpack_connection_custom_caps( $caps, $cap, $user_id, $args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
1146 | |||
1147 | /** |
||
1148 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
1149 | * |
||
1150 | * Based on local php max_execution_time in php.ini |
||
1151 | * |
||
1152 | * @since 5.4 |
||
1153 | * @return int |
||
1154 | **/ |
||
1155 | public function get_max_execution_time() { |
||
1164 | |||
1165 | /** |
||
1166 | * Sets a minimum request timeout, and returns the current timeout |
||
1167 | * |
||
1168 | * @since 5.4 |
||
1169 | * @param Integer $min_timeout the minimum timeout value. |
||
1170 | **/ |
||
1171 | View Code Duplication | public function set_min_time_limit( $min_timeout ) { |
|
1179 | |||
1180 | /** |
||
1181 | * Get our assumed site creation date. |
||
1182 | * Calculated based on the earlier date of either: |
||
1183 | * - Earliest admin user registration date. |
||
1184 | * - Earliest date of post of any post type. |
||
1185 | * |
||
1186 | * @since 7.2.0 |
||
1187 | * |
||
1188 | * @return string Assumed site creation date and time. |
||
1189 | */ |
||
1190 | public function get_assumed_site_creation_date() { |
||
1229 | |||
1230 | /** |
||
1231 | * Adds the activation source string as a parameter to passed arguments. |
||
1232 | * |
||
1233 | * @todo Refactor to use rawurlencode() instead of urlencode(). |
||
1234 | * |
||
1235 | * @param array $args arguments that need to have the source added. |
||
1236 | * @return array $amended arguments. |
||
1237 | */ |
||
1238 | View Code Duplication | public static function apply_activation_source_to_args( $args ) { |
|
1253 | |||
1254 | /** |
||
1255 | * Returns the callable that would be used to generate secrets. |
||
1256 | * |
||
1257 | * @return Callable a function that returns a secure string to be used as a secret. |
||
1258 | */ |
||
1259 | protected function get_secret_callable() { |
||
1271 | |||
1272 | /** |
||
1273 | * Runs the wp_generate_password function with the required parameters. This is the |
||
1274 | * default implementation of the secret callable, can be overridden using the |
||
1275 | * jetpack_connection_secret_generator filter. |
||
1276 | * |
||
1277 | * @return String $secret value. |
||
1278 | */ |
||
1279 | private function secret_callable_method() { |
||
1282 | |||
1283 | /** |
||
1284 | * Generates two secret tokens and the end of life timestamp for them. |
||
1285 | * |
||
1286 | * @param String $action The action name. |
||
1287 | * @param Integer $user_id The user identifier. |
||
1288 | * @param Integer $exp Expiration time in seconds. |
||
1289 | */ |
||
1290 | public function generate_secrets( $action, $user_id = false, $exp = 600 ) { |
||
1322 | |||
1323 | /** |
||
1324 | * Returns two secret tokens and the end of life timestamp for them. |
||
1325 | * |
||
1326 | * @param String $action The action name. |
||
1327 | * @param Integer $user_id The user identifier. |
||
1328 | * @return string|array an array of secrets or an error string. |
||
1329 | */ |
||
1330 | public function get_secrets( $action, $user_id ) { |
||
1348 | |||
1349 | /** |
||
1350 | * Deletes secret tokens in case they, for example, have expired. |
||
1351 | * |
||
1352 | * @param String $action The action name. |
||
1353 | * @param Integer $user_id The user identifier. |
||
1354 | */ |
||
1355 | public function delete_secrets( $action, $user_id ) { |
||
1366 | |||
1367 | /** |
||
1368 | * Deletes all connection tokens and transients from the local Jetpack site. |
||
1369 | * If the plugin object has been provided in the constructor, the function first checks |
||
1370 | * whether it's the only active connection. |
||
1371 | * If there are any other connections, the function will do nothing and return `false` |
||
1372 | * (unless `$ignore_connected_plugins` is set to `true`). |
||
1373 | * |
||
1374 | * @param bool $ignore_connected_plugins Delete the tokens even if there are other connected plugins. |
||
1375 | * |
||
1376 | * @return bool True if disconnected successfully, false otherwise. |
||
1377 | */ |
||
1378 | public function delete_all_connection_tokens( $ignore_connected_plugins = false ) { |
||
1415 | |||
1416 | /** |
||
1417 | * Tells WordPress.com to disconnect the site and clear all tokens from cached site. |
||
1418 | * If the plugin object has been provided in the constructor, the function first check |
||
1419 | * whether it's the only active connection. |
||
1420 | * If there are any other connections, the function will do nothing and return `false` |
||
1421 | * (unless `$ignore_connected_plugins` is set to `true`). |
||
1422 | * |
||
1423 | * @param bool $ignore_connected_plugins Delete the tokens even if there are other connected plugins. |
||
1424 | * |
||
1425 | * @return bool True if disconnected successfully, false otherwise. |
||
1426 | */ |
||
1427 | public function disconnect_site_wpcom( $ignore_connected_plugins = false ) { |
||
1447 | |||
1448 | /** |
||
1449 | * Disconnect the plugin and remove the tokens. |
||
1450 | * This function will automatically perform "soft" or "hard" disconnect depending on whether other plugins are using the connection. |
||
1451 | * This is a proxy method to simplify the Connection package API. |
||
1452 | * |
||
1453 | * @see Manager::disable_plugin() |
||
1454 | * @see Manager::disconnect_site_wpcom() |
||
1455 | * @see Manager::delete_all_connection_tokens() |
||
1456 | * |
||
1457 | * @return bool |
||
1458 | */ |
||
1459 | public function remove_connection() { |
||
1466 | |||
1467 | /** |
||
1468 | * Completely clearing up the connection, and initiating reconnect. |
||
1469 | * |
||
1470 | * @return true|WP_Error True if reconnected successfully, a `WP_Error` object otherwise. |
||
1471 | */ |
||
1472 | public function reconnect() { |
||
1478 | |||
1479 | /** |
||
1480 | * Validate the tokens, and refresh the invalid ones. |
||
1481 | * |
||
1482 | * @return string|true|WP_Error True if connection restored or string indicating what's to be done next. A `WP_Error` object otherwise. |
||
1483 | */ |
||
1484 | public function restore() { |
||
1510 | |||
1511 | /** |
||
1512 | * Determine whether we can restore the connection, or the full reconnect is needed. |
||
1513 | * |
||
1514 | * @param array $invalid_tokens The array the invalid tokens are stored in, provided by reference. |
||
1515 | * |
||
1516 | * @return bool `True` if the connection can be restored, `false` otherwise. |
||
1517 | */ |
||
1518 | public function can_restore( &$invalid_tokens ) { |
||
1538 | |||
1539 | /** |
||
1540 | * Perform the API request to validate the blog and user tokens. |
||
1541 | * |
||
1542 | * @param int|null $user_id ID of the user we need to validate token for. Current user's ID by default. |
||
1543 | * |
||
1544 | * @return array|false|WP_Error The API response: `array( 'blog_token_is_healthy' => true|false, 'user_token_is_healthy' => true|false )`. |
||
1545 | */ |
||
1546 | public function validate_tokens( $user_id = null ) { |
||
1577 | |||
1578 | /** |
||
1579 | * Responds to a WordPress.com call to register the current site. |
||
1580 | * Should be changed to protected. |
||
1581 | * |
||
1582 | * @param array $registration_data Array of [ secret_1, user_id ]. |
||
1583 | */ |
||
1584 | public function handle_registration( array $registration_data ) { |
||
1592 | |||
1593 | /** |
||
1594 | * Verify a Previously Generated Secret. |
||
1595 | * |
||
1596 | * @param string $action The type of secret to verify. |
||
1597 | * @param string $secret_1 The secret string to compare to what is stored. |
||
1598 | * @param int $user_id The user ID of the owner of the secret. |
||
1599 | * @return \WP_Error|string WP_Error on failure, secret_2 on success. |
||
1600 | */ |
||
1601 | public function verify_secrets( $action, $secret_1, $user_id ) { |
||
1737 | |||
1738 | /** |
||
1739 | * Responds to a WordPress.com call to authorize the current user. |
||
1740 | * Should be changed to protected. |
||
1741 | */ |
||
1742 | public function handle_authorization() { |
||
1745 | |||
1746 | /** |
||
1747 | * Obtains the auth token. |
||
1748 | * |
||
1749 | * @param array $data The request data. |
||
1750 | * @return object|\WP_Error Returns the auth token on success. |
||
1751 | * Returns a \WP_Error on failure. |
||
1752 | */ |
||
1753 | public function get_token( $data ) { |
||
1894 | |||
1895 | /** |
||
1896 | * Increases the request timeout value to 30 seconds. |
||
1897 | * |
||
1898 | * @return int Returns 30. |
||
1899 | */ |
||
1900 | public function increase_timeout() { |
||
1903 | |||
1904 | /** |
||
1905 | * Builds a URL to the Jetpack connection auth page. |
||
1906 | * |
||
1907 | * @param WP_User $user (optional) defaults to the current logged in user. |
||
1908 | * @param String $redirect (optional) a redirect URL to use instead of the default. |
||
1909 | * @return string Connect URL. |
||
1910 | */ |
||
1911 | public function get_authorization_url( $user = null, $redirect = null ) { |
||
1997 | |||
1998 | /** |
||
1999 | * Authorizes the user by obtaining and storing the user token. |
||
2000 | * |
||
2001 | * @param array $data The request data. |
||
2002 | * @return string|\WP_Error Returns a string on success. |
||
2003 | * Returns a \WP_Error on failure. |
||
2004 | */ |
||
2005 | public function authorize( $data = array() ) { |
||
2091 | |||
2092 | /** |
||
2093 | * Disconnects from the Jetpack servers. |
||
2094 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
2095 | */ |
||
2096 | public function disconnect_site() { |
||
2099 | |||
2100 | /** |
||
2101 | * The Base64 Encoding of the SHA1 Hash of the Input. |
||
2102 | * |
||
2103 | * @param string $text The string to hash. |
||
2104 | * @return string |
||
2105 | */ |
||
2106 | public function sha1_base64( $text ) { |
||
2109 | |||
2110 | /** |
||
2111 | * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
||
2112 | * |
||
2113 | * @param string $domain The domain to check. |
||
2114 | * |
||
2115 | * @return bool|WP_Error |
||
2116 | */ |
||
2117 | public function is_usable_domain( $domain ) { |
||
2204 | |||
2205 | /** |
||
2206 | * Gets the requested token. |
||
2207 | * |
||
2208 | * Tokens are one of two types: |
||
2209 | * 1. Blog Tokens: These are the "main" tokens. Each site typically has one Blog Token, |
||
2210 | * though some sites can have multiple "Special" Blog Tokens (see below). These tokens |
||
2211 | * are not associated with a user account. They represent the site's connection with |
||
2212 | * the Jetpack servers. |
||
2213 | * 2. User Tokens: These are "sub-"tokens. Each connected user account has one User Token. |
||
2214 | * |
||
2215 | * All tokens look like "{$token_key}.{$private}". $token_key is a public ID for the |
||
2216 | * token, and $private is a secret that should never be displayed anywhere or sent |
||
2217 | * over the network; it's used only for signing things. |
||
2218 | * |
||
2219 | * Blog Tokens can be "Normal" or "Special". |
||
2220 | * * Normal: The result of a normal connection flow. They look like |
||
2221 | * "{$random_string_1}.{$random_string_2}" |
||
2222 | * That is, $token_key and $private are both random strings. |
||
2223 | * Sites only have one Normal Blog Token. Normal Tokens are found in either |
||
2224 | * Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN |
||
2225 | * constant (rare). |
||
2226 | * * Special: A connection token for sites that have gone through an alternative |
||
2227 | * connection flow. They look like: |
||
2228 | * ";{$special_id}{$special_version};{$wpcom_blog_id};.{$random_string}" |
||
2229 | * That is, $private is a random string and $token_key has a special structure with |
||
2230 | * lots of semicolons. |
||
2231 | * Most sites have zero Special Blog Tokens. Special tokens are only found in the |
||
2232 | * JETPACK_BLOG_TOKEN constant. |
||
2233 | * |
||
2234 | * In particular, note that Normal Blog Tokens never start with ";" and that |
||
2235 | * Special Blog Tokens always do. |
||
2236 | * |
||
2237 | * When searching for a matching Blog Tokens, Blog Tokens are examined in the following |
||
2238 | * order: |
||
2239 | * 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
2240 | * 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' )) |
||
2241 | * 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
2242 | * |
||
2243 | * @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
||
2244 | * @param string|false $token_key If provided, check that the token matches the provided input. |
||
2245 | * @param bool|true $suppress_errors If true, return a falsy value when the token isn't found; When false, return a descriptive WP_Error when the token isn't found. |
||
2246 | * |
||
2247 | * @return object|false |
||
2248 | */ |
||
2249 | public function get_access_token( $user_id = false, $token_key = false, $suppress_errors = true ) { |
||
2344 | |||
2345 | /** |
||
2346 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths |
||
2347 | * since it is passed by reference to various methods. |
||
2348 | * Capture it here so we can verify the signature later. |
||
2349 | * |
||
2350 | * @param array $methods an array of available XMLRPC methods. |
||
2351 | * @return array the same array, since this method doesn't add or remove anything. |
||
2352 | */ |
||
2353 | public function xmlrpc_methods( $methods ) { |
||
2357 | |||
2358 | /** |
||
2359 | * Resets the raw post data parameter for testing purposes. |
||
2360 | */ |
||
2361 | public function reset_raw_post_data() { |
||
2364 | |||
2365 | /** |
||
2366 | * Registering an additional method. |
||
2367 | * |
||
2368 | * @param array $methods an array of available XMLRPC methods. |
||
2369 | * @return array the amended array in case the method is added. |
||
2370 | */ |
||
2371 | public function public_xmlrpc_methods( $methods ) { |
||
2377 | |||
2378 | /** |
||
2379 | * Handles a getOptions XMLRPC method call. |
||
2380 | * |
||
2381 | * @param array $args method call arguments. |
||
2382 | * @return an amended XMLRPC server options array. |
||
2383 | */ |
||
2384 | public function jetpack_get_options( $args ) { |
||
2425 | |||
2426 | /** |
||
2427 | * Adds Jetpack-specific options to the output of the XMLRPC options method. |
||
2428 | * |
||
2429 | * @param array $options standard Core options. |
||
2430 | * @return array amended options. |
||
2431 | */ |
||
2432 | public function xmlrpc_options( $options ) { |
||
2450 | |||
2451 | /** |
||
2452 | * Resets the saved authentication state in between testing requests. |
||
2453 | */ |
||
2454 | public function reset_saved_auth_state() { |
||
2457 | |||
2458 | /** |
||
2459 | * Sign a user role with the master access token. |
||
2460 | * If not specified, will default to the current user. |
||
2461 | * |
||
2462 | * @access public |
||
2463 | * |
||
2464 | * @param string $role User role. |
||
2465 | * @param int $user_id ID of the user. |
||
2466 | * @return string Signed user role. |
||
2467 | */ |
||
2468 | public function sign_role( $role, $user_id = null ) { |
||
2484 | |||
2485 | /** |
||
2486 | * Set the plugin instance. |
||
2487 | * |
||
2488 | * @param Plugin $plugin_instance The plugin instance. |
||
2489 | * |
||
2490 | * @return $this |
||
2491 | */ |
||
2492 | public function set_plugin_instance( Plugin $plugin_instance ) { |
||
2497 | |||
2498 | /** |
||
2499 | * Retrieve the plugin management object. |
||
2500 | * |
||
2501 | * @return Plugin |
||
2502 | */ |
||
2503 | public function get_plugin() { |
||
2506 | |||
2507 | /** |
||
2508 | * Get all connected plugins information, excluding those disconnected by user. |
||
2509 | * WARNING: the method cannot be called until Plugin_Storage::configure is called, which happens on plugins_loaded |
||
2510 | * Even if you don't use Jetpack Config, it may be introduced later by other plugins, |
||
2511 | * so please make sure not to run the method too early in the code. |
||
2512 | * |
||
2513 | * @return array|WP_Error |
||
2514 | */ |
||
2515 | public function get_connected_plugins() { |
||
2524 | |||
2525 | /** |
||
2526 | * Force plugin disconnect. After its called, the plugin will not be allowed to use the connection. |
||
2527 | * Note: this method does not remove any access tokens. |
||
2528 | * |
||
2529 | * @return bool |
||
2530 | */ |
||
2531 | public function disable_plugin() { |
||
2538 | |||
2539 | /** |
||
2540 | * Force plugin reconnect after user-initiated disconnect. |
||
2541 | * After its called, the plugin will be allowed to use the connection again. |
||
2542 | * Note: this method does not initialize access tokens. |
||
2543 | * |
||
2544 | * @return bool |
||
2545 | */ |
||
2546 | public function enable_plugin() { |
||
2553 | |||
2554 | /** |
||
2555 | * Whether the plugin is allowed to use the connection, or it's been disconnected by user. |
||
2556 | * If no plugin slug was passed into the constructor, always returns true. |
||
2557 | * |
||
2558 | * @return bool |
||
2559 | */ |
||
2560 | public function is_plugin_enabled() { |
||
2567 | |||
2568 | /** |
||
2569 | * Perform the API request to refresh the blog token. |
||
2570 | * Note that we are making this request on behalf of the Jetpack master user, |
||
2571 | * given they were (most probably) the ones that registered the site at the first place. |
||
2572 | * |
||
2573 | * @return WP_Error|bool The result of updating the blog_token option. |
||
2574 | */ |
||
2575 | public static function refresh_blog_token() { |
||
2624 | |||
2625 | /** |
||
2626 | * Fetches a signed token. |
||
2627 | * |
||
2628 | * @param object $token the token. |
||
2629 | * @return WP_Error|string a signed token |
||
2630 | */ |
||
2631 | public function get_signed_token( $token ) { |
||
2679 | |||
2680 | } |
||
2681 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.