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 |
||
20 | class Manager { |
||
21 | |||
22 | const SECRETS_MISSING = 'secrets_missing'; |
||
23 | const SECRETS_EXPIRED = 'secrets_expired'; |
||
24 | const SECRETS_OPTION_NAME = 'jetpack_secrets'; |
||
25 | const MAGIC_NORMAL_TOKEN_KEY = ';normal;'; |
||
26 | const JETPACK_MASTER_USER = true; |
||
27 | |||
28 | /** |
||
29 | * The procedure that should be run to generate secrets. |
||
30 | * |
||
31 | * @var Callable |
||
32 | */ |
||
33 | protected $secret_callable; |
||
34 | |||
35 | /** |
||
36 | * A copy of the raw POST data for signature verification purposes. |
||
37 | * |
||
38 | * @var String |
||
39 | */ |
||
40 | protected $raw_post_data; |
||
41 | |||
42 | /** |
||
43 | * Verification data needs to be stored to properly verify everything. |
||
44 | * |
||
45 | * @var Object |
||
46 | */ |
||
47 | private $xmlrpc_verification = null; |
||
48 | |||
49 | /** |
||
50 | * Plugin management object. |
||
51 | * |
||
52 | * @var Plugin |
||
53 | */ |
||
54 | private $plugin = null; |
||
55 | |||
56 | /** |
||
57 | * Initialize the object. |
||
58 | * Make sure to call the "Configure" first. |
||
59 | * |
||
60 | * @param string $plugin_slug Slug of the plugin using the connection (optional, but encouraged). |
||
|
|||
61 | * |
||
62 | * @see \Automattic\Jetpack\Config |
||
63 | */ |
||
64 | public function __construct( $plugin_slug = null ) { |
||
65 | if ( $plugin_slug && is_string( $plugin_slug ) ) { |
||
66 | $this->set_plugin_instance( new Plugin( $plugin_slug ) ); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Initializes required listeners. This is done separately from the constructors |
||
72 | * because some objects sometimes need to instantiate separate objects of this class. |
||
73 | * |
||
74 | * @todo Implement a proper nonce verification. |
||
75 | */ |
||
76 | public static function configure() { |
||
77 | $manager = new self(); |
||
78 | |||
79 | add_filter( |
||
80 | 'jetpack_constant_default_value', |
||
81 | __NAMESPACE__ . '\Utils::jetpack_api_constant_filter', |
||
82 | 10, |
||
83 | 2 |
||
84 | ); |
||
85 | |||
86 | $manager->setup_xmlrpc_handlers( |
||
87 | $_GET, // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
88 | $manager->is_active(), |
||
89 | $manager->verify_xml_rpc_signature() |
||
90 | ); |
||
91 | |||
92 | $manager->error_handler = Error_Handler::get_instance(); |
||
93 | |||
94 | if ( $manager->is_active() ) { |
||
95 | add_filter( 'xmlrpc_methods', array( $manager, 'public_xmlrpc_methods' ) ); |
||
96 | } else { |
||
97 | add_action( 'rest_api_init', array( $manager, 'initialize_rest_api_registration_connector' ) ); |
||
98 | } |
||
99 | |||
100 | add_action( 'jetpack_clean_nonces', array( $manager, 'clean_nonces' ) ); |
||
101 | if ( ! wp_next_scheduled( 'jetpack_clean_nonces' ) ) { |
||
102 | wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
||
103 | } |
||
104 | |||
105 | add_action( 'plugins_loaded', __NAMESPACE__ . '\Plugin_Storage::configure', 100 ); |
||
106 | |||
107 | add_filter( 'map_meta_cap', array( $manager, 'jetpack_connection_custom_caps' ), 1, 4 ); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Sets up the XMLRPC request handlers. |
||
112 | * |
||
113 | * @param array $request_params incoming request parameters. |
||
114 | * @param Boolean $is_active whether the connection is currently active. |
||
115 | * @param Boolean $is_signed whether the signature check has been successful. |
||
116 | * @param \Jetpack_XMLRPC_Server $xmlrpc_server (optional) an instance of the server to use instead of instantiating a new one. |
||
117 | */ |
||
118 | public function setup_xmlrpc_handlers( |
||
119 | $request_params, |
||
120 | $is_active, |
||
121 | $is_signed, |
||
122 | \Jetpack_XMLRPC_Server $xmlrpc_server = null |
||
123 | ) { |
||
124 | add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ), 1000, 2 ); |
||
125 | |||
126 | if ( |
||
127 | ! isset( $request_params['for'] ) |
||
128 | || 'jetpack' !== $request_params['for'] |
||
129 | ) { |
||
130 | return false; |
||
131 | } |
||
132 | |||
133 | // Alternate XML-RPC, via ?for=jetpack&jetpack=comms. |
||
134 | if ( |
||
135 | isset( $request_params['jetpack'] ) |
||
136 | && 'comms' === $request_params['jetpack'] |
||
137 | ) { |
||
138 | if ( ! Constants::is_defined( 'XMLRPC_REQUEST' ) ) { |
||
139 | // Use the real constant here for WordPress' sake. |
||
140 | define( 'XMLRPC_REQUEST', true ); |
||
141 | } |
||
142 | |||
143 | add_action( 'template_redirect', array( $this, 'alternate_xmlrpc' ) ); |
||
144 | |||
145 | add_filter( 'xmlrpc_methods', array( $this, 'remove_non_jetpack_xmlrpc_methods' ), 1000 ); |
||
146 | } |
||
147 | |||
148 | if ( ! Constants::get_constant( 'XMLRPC_REQUEST' ) ) { |
||
149 | return false; |
||
150 | } |
||
151 | // Display errors can cause the XML to be not well formed. |
||
152 | @ini_set( 'display_errors', false ); // phpcs:ignore |
||
153 | |||
154 | if ( $xmlrpc_server ) { |
||
155 | $this->xmlrpc_server = $xmlrpc_server; |
||
156 | } else { |
||
157 | $this->xmlrpc_server = new \Jetpack_XMLRPC_Server(); |
||
158 | } |
||
159 | |||
160 | $this->require_jetpack_authentication(); |
||
161 | |||
162 | if ( $is_active ) { |
||
163 | // Hack to preserve $HTTP_RAW_POST_DATA. |
||
164 | add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
||
165 | |||
166 | if ( $is_signed ) { |
||
167 | // The actual API methods. |
||
168 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) ); |
||
169 | } else { |
||
170 | // The jetpack.authorize method should be available for unauthenticated users on a site with an |
||
171 | // active Jetpack connection, so that additional users can link their account. |
||
172 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'authorize_xmlrpc_methods' ) ); |
||
173 | } |
||
174 | } else { |
||
175 | // The bootstrap API methods. |
||
176 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) ); |
||
177 | |||
178 | if ( $is_signed ) { |
||
179 | // The jetpack Provision method is available for blog-token-signed requests. |
||
180 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'provision_xmlrpc_methods' ) ); |
||
181 | } else { |
||
182 | new XMLRPC_Connector( $this ); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | // Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on. |
||
187 | add_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
||
188 | return true; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Initializes the REST API connector on the init hook. |
||
193 | */ |
||
194 | public function initialize_rest_api_registration_connector() { |
||
195 | new REST_Connector( $this ); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Since a lot of hosts use a hammer approach to "protecting" WordPress sites, |
||
200 | * and just blanket block all requests to /xmlrpc.php, or apply other overly-sensitive |
||
201 | * security/firewall policies, we provide our own alternate XML RPC API endpoint |
||
202 | * which is accessible via a different URI. Most of the below is copied directly |
||
203 | * from /xmlrpc.php so that we're replicating it as closely as possible. |
||
204 | * |
||
205 | * @todo Tighten $wp_xmlrpc_server_class a bit to make sure it doesn't do bad things. |
||
206 | */ |
||
207 | public function alternate_xmlrpc() { |
||
208 | // phpcs:disable PHPCompatibility.Variables.RemovedPredefinedGlobalVariables.http_raw_post_dataDeprecatedRemoved |
||
209 | // phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited |
||
210 | global $HTTP_RAW_POST_DATA; |
||
211 | |||
212 | // Some browser-embedded clients send cookies. We don't want them. |
||
213 | $_COOKIE = array(); |
||
214 | |||
215 | // A fix for mozBlog and other cases where '<?xml' isn't on the very first line. |
||
216 | if ( isset( $HTTP_RAW_POST_DATA ) ) { |
||
217 | $HTTP_RAW_POST_DATA = trim( $HTTP_RAW_POST_DATA ); |
||
218 | } |
||
219 | |||
220 | // phpcs:enable |
||
221 | |||
222 | include_once ABSPATH . 'wp-admin/includes/admin.php'; |
||
223 | include_once ABSPATH . WPINC . '/class-IXR.php'; |
||
224 | include_once ABSPATH . WPINC . '/class-wp-xmlrpc-server.php'; |
||
225 | |||
226 | /** |
||
227 | * Filters the class used for handling XML-RPC requests. |
||
228 | * |
||
229 | * @since 3.1.0 |
||
230 | * |
||
231 | * @param string $class The name of the XML-RPC server class. |
||
232 | */ |
||
233 | $wp_xmlrpc_server_class = apply_filters( 'wp_xmlrpc_server_class', 'wp_xmlrpc_server' ); |
||
234 | $wp_xmlrpc_server = new $wp_xmlrpc_server_class(); |
||
235 | |||
236 | // Fire off the request. |
||
237 | nocache_headers(); |
||
238 | $wp_xmlrpc_server->serve_request(); |
||
239 | |||
240 | exit; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Removes all XML-RPC methods that are not `jetpack.*`. |
||
245 | * Only used in our alternate XML-RPC endpoint, where we want to |
||
246 | * ensure that Core and other plugins' methods are not exposed. |
||
247 | * |
||
248 | * @param array $methods a list of registered WordPress XMLRPC methods. |
||
249 | * @return array filtered $methods |
||
250 | */ |
||
251 | public function remove_non_jetpack_xmlrpc_methods( $methods ) { |
||
252 | $jetpack_methods = array(); |
||
253 | |||
254 | foreach ( $methods as $method => $callback ) { |
||
255 | if ( 0 === strpos( $method, 'jetpack.' ) ) { |
||
256 | $jetpack_methods[ $method ] = $callback; |
||
257 | } |
||
258 | } |
||
259 | |||
260 | return $jetpack_methods; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Removes all other authentication methods not to allow other |
||
265 | * methods to validate unauthenticated requests. |
||
266 | */ |
||
267 | public function require_jetpack_authentication() { |
||
268 | // Don't let anyone authenticate. |
||
269 | $_COOKIE = array(); |
||
270 | remove_all_filters( 'authenticate' ); |
||
271 | remove_all_actions( 'wp_login_failed' ); |
||
272 | |||
273 | if ( $this->is_active() ) { |
||
274 | // Allow Jetpack authentication. |
||
275 | add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 ); |
||
276 | } |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Authenticates XML-RPC and other requests from the Jetpack Server |
||
281 | * |
||
282 | * @param WP_User|Mixed $user user object if authenticated. |
||
283 | * @param String $username username. |
||
284 | * @param String $password password string. |
||
285 | * @return WP_User|Mixed authenticated user or error. |
||
286 | */ |
||
287 | public function authenticate_jetpack( $user, $username, $password ) { |
||
288 | if ( is_a( $user, '\\WP_User' ) ) { |
||
289 | return $user; |
||
290 | } |
||
291 | |||
292 | $token_details = $this->verify_xml_rpc_signature(); |
||
293 | |||
294 | if ( ! $token_details ) { |
||
295 | return $user; |
||
296 | } |
||
297 | |||
298 | if ( 'user' !== $token_details['type'] ) { |
||
299 | return $user; |
||
300 | } |
||
301 | |||
302 | if ( ! $token_details['user_id'] ) { |
||
303 | return $user; |
||
304 | } |
||
305 | |||
306 | nocache_headers(); |
||
307 | |||
308 | return new \WP_User( $token_details['user_id'] ); |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Verifies the signature of the current request. |
||
313 | * |
||
314 | * @return false|array |
||
315 | */ |
||
316 | public function verify_xml_rpc_signature() { |
||
317 | if ( is_null( $this->xmlrpc_verification ) ) { |
||
318 | $this->xmlrpc_verification = $this->internal_verify_xml_rpc_signature(); |
||
319 | |||
320 | if ( is_wp_error( $this->xmlrpc_verification ) ) { |
||
321 | /** |
||
322 | * Action for logging XMLRPC signature verification errors. This data is sensitive. |
||
323 | * |
||
324 | * @since 7.5.0 |
||
325 | * |
||
326 | * @param WP_Error $signature_verification_error The verification error |
||
327 | */ |
||
328 | do_action( 'jetpack_verify_signature_error', $this->xmlrpc_verification ); |
||
329 | |||
330 | Error_Handler::get_instance()->report_error( $this->xmlrpc_verification ); |
||
331 | |||
332 | } |
||
333 | } |
||
334 | |||
335 | return is_wp_error( $this->xmlrpc_verification ) ? false : $this->xmlrpc_verification; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Verifies the signature of the current request. |
||
340 | * |
||
341 | * This function has side effects and should not be used. Instead, |
||
342 | * use the memoized version `->verify_xml_rpc_signature()`. |
||
343 | * |
||
344 | * @internal |
||
345 | * @todo Refactor to use proper nonce verification. |
||
346 | */ |
||
347 | private function internal_verify_xml_rpc_signature() { |
||
504 | |||
505 | /** |
||
506 | * Returns true if the current site is connected to WordPress.com. |
||
507 | * |
||
508 | * @return Boolean is the site connected? |
||
509 | */ |
||
510 | public function is_active() { |
||
513 | |||
514 | /** |
||
515 | * Returns true if the site has both a token and a blog id, which indicates a site has been registered. |
||
516 | * |
||
517 | * @access public |
||
518 | * |
||
519 | * @return bool |
||
520 | */ |
||
521 | public function is_registered() { |
||
526 | |||
527 | /** |
||
528 | * Checks to see if the connection owner of the site is missing. |
||
529 | * |
||
530 | * @return bool |
||
531 | */ |
||
532 | public function is_missing_connection_owner() { |
||
540 | |||
541 | /** |
||
542 | * Returns true if the user with the specified identifier is connected to |
||
543 | * WordPress.com. |
||
544 | * |
||
545 | * @param Integer|Boolean $user_id the user identifier. |
||
546 | * @return Boolean is the user connected? |
||
547 | */ |
||
548 | public function is_user_connected( $user_id = false ) { |
||
556 | |||
557 | /** |
||
558 | * Returns the local user ID of the connection owner. |
||
559 | * |
||
560 | * @return string|int Returns the ID of the connection owner or False if no connection owner found. |
||
561 | */ |
||
562 | View Code Duplication | public function get_connection_owner_id() { |
|
571 | |||
572 | /** |
||
573 | * Returns an array of user_id's that have user tokens for communicating with wpcom. |
||
574 | * Able to select by specific capability. |
||
575 | * |
||
576 | * @param string $capability The capability of the user. |
||
577 | * @return array Array of WP_User objects if found. |
||
578 | */ |
||
579 | public function get_connected_users( $capability = 'any' ) { |
||
596 | |||
597 | /** |
||
598 | * Get the wpcom user data of the current|specified connected user. |
||
599 | * |
||
600 | * @todo Refactor to properly load the XMLRPC client independently. |
||
601 | * |
||
602 | * @param Integer $user_id the user identifier. |
||
603 | * @return Object the user object. |
||
604 | */ |
||
605 | View Code Duplication | public function get_connected_user_data( $user_id = null ) { |
|
631 | |||
632 | /** |
||
633 | * Returns a user object of the connection owner. |
||
634 | * |
||
635 | * @return object|false False if no connection owner found. |
||
636 | */ |
||
637 | View Code Duplication | public function get_connection_owner() { |
|
647 | |||
648 | /** |
||
649 | * Returns true if the provided user is the Jetpack connection owner. |
||
650 | * If user ID is not specified, the current user will be used. |
||
651 | * |
||
652 | * @param Integer|Boolean $user_id the user identifier. False for current user. |
||
653 | * @return Boolean True the user the connection owner, false otherwise. |
||
654 | */ |
||
655 | View Code Duplication | public function is_connection_owner( $user_id = false ) { |
|
664 | |||
665 | /** |
||
666 | * Connects the user with a specified ID to a WordPress.com user using the |
||
667 | * remote login flow. |
||
668 | * |
||
669 | * @access public |
||
670 | * |
||
671 | * @param Integer $user_id (optional) the user identifier, defaults to current user. |
||
672 | * @param String $redirect_url the URL to redirect the user to for processing, defaults to |
||
673 | * admin_url(). |
||
674 | * @return WP_Error only in case of a failed user lookup. |
||
675 | */ |
||
676 | public function connect_user( $user_id = null, $redirect_url = null ) { |
||
696 | |||
697 | /** |
||
698 | * Unlinks the current user from the linked WordPress.com user. |
||
699 | * |
||
700 | * @access public |
||
701 | * @static |
||
702 | * |
||
703 | * @todo Refactor to properly load the XMLRPC client independently. |
||
704 | * |
||
705 | * @param Integer $user_id the user identifier. |
||
706 | * @return Boolean Whether the disconnection of the user was successful. |
||
707 | */ |
||
708 | public static function disconnect_user( $user_id = null ) { |
||
742 | |||
743 | /** |
||
744 | * Returns the requested Jetpack API URL. |
||
745 | * |
||
746 | * @param String $relative_url the relative API path. |
||
747 | * @return String API URL. |
||
748 | */ |
||
749 | public function api_url( $relative_url ) { |
||
786 | |||
787 | /** |
||
788 | * Returns the Jetpack XMLRPC WordPress.com API endpoint URL. |
||
789 | * |
||
790 | * @return String XMLRPC API URL. |
||
791 | */ |
||
792 | public function xmlrpc_api_url() { |
||
800 | |||
801 | /** |
||
802 | * Attempts Jetpack registration which sets up the site for connection. Should |
||
803 | * remain public because the call to action comes from the current site, not from |
||
804 | * WordPress.com. |
||
805 | * |
||
806 | * @param String $api_endpoint (optional) an API endpoint to use, defaults to 'register'. |
||
807 | * @return Integer zero on success, or a bitmask on failure. |
||
808 | */ |
||
809 | public function register( $api_endpoint = 'register' ) { |
||
945 | |||
946 | /** |
||
947 | * Takes the response from the Jetpack register new site endpoint and |
||
948 | * verifies it worked properly. |
||
949 | * |
||
950 | * @since 2.6 |
||
951 | * |
||
952 | * @param Mixed $response the response object, or the error object. |
||
953 | * @return string|WP_Error A JSON object on success or WP_Error on failures |
||
954 | **/ |
||
955 | protected function validate_remote_register_response( $response ) { |
||
1024 | |||
1025 | /** |
||
1026 | * Adds a used nonce to a list of known nonces. |
||
1027 | * |
||
1028 | * @param int $timestamp the current request timestamp. |
||
1029 | * @param string $nonce the nonce value. |
||
1030 | * @return bool whether the nonce is unique or not. |
||
1031 | */ |
||
1032 | public function add_nonce( $timestamp, $nonce ) { |
||
1070 | |||
1071 | /** |
||
1072 | * Cleans nonces that were saved when calling ::add_nonce. |
||
1073 | * |
||
1074 | * @todo Properly prepare the query before executing it. |
||
1075 | * |
||
1076 | * @param bool $all whether to clean even non-expired nonces. |
||
1077 | */ |
||
1078 | public function clean_nonces( $all = false ) { |
||
1099 | |||
1100 | /** |
||
1101 | * Sets the Connection custom capabilities. |
||
1102 | * |
||
1103 | * @param string[] $caps Array of the user's capabilities. |
||
1104 | * @param string $cap Capability name. |
||
1105 | * @param int $user_id The user ID. |
||
1106 | * @param array $args Adds the context to the cap. Typically the object ID. |
||
1107 | */ |
||
1108 | public function jetpack_connection_custom_caps( $caps, $cap, $user_id, $args ) { |
||
1109 | $is_development_mode = ( new Status() )->is_development_mode(); |
||
1110 | switch ( $cap ) { |
||
1111 | case 'jetpack_connect': |
||
1112 | case 'jetpack_reconnect': |
||
1113 | if ( $is_development_mode ) { |
||
1114 | $caps = array( 'do_not_allow' ); |
||
1115 | break; |
||
1116 | } |
||
1117 | // Pass through. If it's not development mode, these should match disconnect. |
||
1118 | // Let users disconnect if it's development mode, just in case things glitch. |
||
1119 | case 'jetpack_disconnect': |
||
1120 | /** |
||
1121 | * Filters the jetpack_disconnect capability. |
||
1122 | * |
||
1123 | * @since 8.7.0 |
||
1124 | * |
||
1125 | * @param array An array containing the capability name. |
||
1126 | */ |
||
1127 | $caps = apply_filters( 'jetpack_disconnect_cap', array( 'manage_options' ) ); |
||
1128 | break; |
||
1129 | case 'jetpack_connect_user': |
||
1130 | if ( $is_development_mode ) { |
||
1131 | $caps = array( 'do_not_allow' ); |
||
1132 | break; |
||
1133 | } |
||
1134 | $caps = array( 'read' ); |
||
1135 | break; |
||
1136 | } |
||
1137 | return $caps; |
||
1138 | } |
||
1139 | |||
1140 | /** |
||
1141 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
1142 | * |
||
1143 | * Based on local php max_execution_time in php.ini |
||
1144 | * |
||
1145 | * @since 5.4 |
||
1146 | * @return int |
||
1147 | **/ |
||
1148 | public function get_max_execution_time() { |
||
1149 | $timeout = (int) ini_get( 'max_execution_time' ); |
||
1150 | |||
1151 | // Ensure exec time set in php.ini. |
||
1152 | if ( ! $timeout ) { |
||
1153 | $timeout = 30; |
||
1154 | } |
||
1155 | return $timeout; |
||
1156 | } |
||
1157 | |||
1158 | /** |
||
1159 | * Sets a minimum request timeout, and returns the current timeout |
||
1160 | * |
||
1161 | * @since 5.4 |
||
1162 | * @param Integer $min_timeout the minimum timeout value. |
||
1163 | **/ |
||
1164 | View Code Duplication | public function set_min_time_limit( $min_timeout ) { |
|
1165 | $timeout = $this->get_max_execution_time(); |
||
1166 | if ( $timeout < $min_timeout ) { |
||
1167 | $timeout = $min_timeout; |
||
1168 | set_time_limit( $timeout ); |
||
1169 | } |
||
1170 | return $timeout; |
||
1171 | } |
||
1172 | |||
1173 | /** |
||
1174 | * Get our assumed site creation date. |
||
1175 | * Calculated based on the earlier date of either: |
||
1176 | * - Earliest admin user registration date. |
||
1177 | * - Earliest date of post of any post type. |
||
1178 | * |
||
1179 | * @since 7.2.0 |
||
1180 | * |
||
1181 | * @return string Assumed site creation date and time. |
||
1182 | */ |
||
1183 | public function get_assumed_site_creation_date() { |
||
1184 | $cached_date = get_transient( 'jetpack_assumed_site_creation_date' ); |
||
1185 | if ( ! empty( $cached_date ) ) { |
||
1186 | return $cached_date; |
||
1187 | } |
||
1188 | |||
1189 | $earliest_registered_users = get_users( |
||
1190 | array( |
||
1191 | 'role' => 'administrator', |
||
1192 | 'orderby' => 'user_registered', |
||
1193 | 'order' => 'ASC', |
||
1194 | 'fields' => array( 'user_registered' ), |
||
1195 | 'number' => 1, |
||
1196 | ) |
||
1197 | ); |
||
1198 | $earliest_registration_date = $earliest_registered_users[0]->user_registered; |
||
1199 | |||
1200 | $earliest_posts = get_posts( |
||
1201 | array( |
||
1202 | 'posts_per_page' => 1, |
||
1203 | 'post_type' => 'any', |
||
1204 | 'post_status' => 'any', |
||
1205 | 'orderby' => 'date', |
||
1206 | 'order' => 'ASC', |
||
1207 | ) |
||
1208 | ); |
||
1209 | |||
1210 | // If there are no posts at all, we'll count only on user registration date. |
||
1211 | if ( $earliest_posts ) { |
||
1212 | $earliest_post_date = $earliest_posts[0]->post_date; |
||
1213 | } else { |
||
1214 | $earliest_post_date = PHP_INT_MAX; |
||
1215 | } |
||
1216 | |||
1217 | $assumed_date = min( $earliest_registration_date, $earliest_post_date ); |
||
1218 | set_transient( 'jetpack_assumed_site_creation_date', $assumed_date ); |
||
1219 | |||
1220 | return $assumed_date; |
||
1221 | } |
||
1222 | |||
1223 | /** |
||
1224 | * Adds the activation source string as a parameter to passed arguments. |
||
1225 | * |
||
1226 | * @todo Refactor to use rawurlencode() instead of urlencode(). |
||
1227 | * |
||
1228 | * @param array $args arguments that need to have the source added. |
||
1229 | * @return array $amended arguments. |
||
1230 | */ |
||
1231 | View Code Duplication | public static function apply_activation_source_to_args( $args ) { |
|
1232 | list( $activation_source_name, $activation_source_keyword ) = get_option( 'jetpack_activation_source' ); |
||
1233 | |||
1234 | if ( $activation_source_name ) { |
||
1235 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.urlencode_urlencode |
||
1236 | $args['_as'] = urlencode( $activation_source_name ); |
||
1237 | } |
||
1238 | |||
1239 | if ( $activation_source_keyword ) { |
||
1240 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.urlencode_urlencode |
||
1241 | $args['_ak'] = urlencode( $activation_source_keyword ); |
||
1242 | } |
||
1243 | |||
1244 | return $args; |
||
1245 | } |
||
1246 | |||
1247 | /** |
||
1248 | * Returns the callable that would be used to generate secrets. |
||
1249 | * |
||
1250 | * @return Callable a function that returns a secure string to be used as a secret. |
||
1251 | */ |
||
1252 | protected function get_secret_callable() { |
||
1253 | if ( ! isset( $this->secret_callable ) ) { |
||
1254 | /** |
||
1255 | * Allows modification of the callable that is used to generate connection secrets. |
||
1256 | * |
||
1257 | * @param Callable a function or method that returns a secret string. |
||
1258 | */ |
||
1259 | $this->secret_callable = apply_filters( 'jetpack_connection_secret_generator', array( $this, 'secret_callable_method' ) ); |
||
1260 | } |
||
1261 | |||
1262 | return $this->secret_callable; |
||
1263 | } |
||
1264 | |||
1265 | /** |
||
1266 | * Runs the wp_generate_password function with the required parameters. This is the |
||
1267 | * default implementation of the secret callable, can be overridden using the |
||
1268 | * jetpack_connection_secret_generator filter. |
||
1269 | * |
||
1270 | * @return String $secret value. |
||
1271 | */ |
||
1272 | private function secret_callable_method() { |
||
1273 | return wp_generate_password( 32, false ); |
||
1274 | } |
||
1275 | |||
1276 | /** |
||
1277 | * Generates two secret tokens and the end of life timestamp for them. |
||
1278 | * |
||
1279 | * @param String $action The action name. |
||
1280 | * @param Integer $user_id The user identifier. |
||
1281 | * @param Integer $exp Expiration time in seconds. |
||
1282 | */ |
||
1283 | public function generate_secrets( $action, $user_id = false, $exp = 600 ) { |
||
1284 | if ( false === $user_id ) { |
||
1285 | $user_id = get_current_user_id(); |
||
1286 | } |
||
1287 | |||
1288 | $callable = $this->get_secret_callable(); |
||
1289 | |||
1290 | $secrets = \Jetpack_Options::get_raw_option( |
||
1291 | self::SECRETS_OPTION_NAME, |
||
1292 | array() |
||
1293 | ); |
||
1294 | |||
1295 | $secret_name = 'jetpack_' . $action . '_' . $user_id; |
||
1296 | |||
1297 | if ( |
||
1298 | isset( $secrets[ $secret_name ] ) && |
||
1299 | $secrets[ $secret_name ]['exp'] > time() |
||
1300 | ) { |
||
1301 | return $secrets[ $secret_name ]; |
||
1302 | } |
||
1303 | |||
1304 | $secret_value = array( |
||
1305 | 'secret_1' => call_user_func( $callable ), |
||
1306 | 'secret_2' => call_user_func( $callable ), |
||
1307 | 'exp' => time() + $exp, |
||
1308 | ); |
||
1309 | |||
1310 | $secrets[ $secret_name ] = $secret_value; |
||
1311 | |||
1312 | \Jetpack_Options::update_raw_option( self::SECRETS_OPTION_NAME, $secrets ); |
||
1313 | return $secrets[ $secret_name ]; |
||
1314 | } |
||
1315 | |||
1316 | /** |
||
1317 | * Returns two secret tokens and the end of life timestamp for them. |
||
1318 | * |
||
1319 | * @param String $action The action name. |
||
1320 | * @param Integer $user_id The user identifier. |
||
1321 | * @return string|array an array of secrets or an error string. |
||
1322 | */ |
||
1323 | public function get_secrets( $action, $user_id ) { |
||
1324 | $secret_name = 'jetpack_' . $action . '_' . $user_id; |
||
1325 | $secrets = \Jetpack_Options::get_raw_option( |
||
1326 | self::SECRETS_OPTION_NAME, |
||
1327 | array() |
||
1328 | ); |
||
1329 | |||
1330 | if ( ! isset( $secrets[ $secret_name ] ) ) { |
||
1331 | return self::SECRETS_MISSING; |
||
1332 | } |
||
1333 | |||
1334 | if ( $secrets[ $secret_name ]['exp'] < time() ) { |
||
1335 | $this->delete_secrets( $action, $user_id ); |
||
1336 | return self::SECRETS_EXPIRED; |
||
1337 | } |
||
1338 | |||
1339 | return $secrets[ $secret_name ]; |
||
1340 | } |
||
1341 | |||
1342 | /** |
||
1343 | * Deletes secret tokens in case they, for example, have expired. |
||
1344 | * |
||
1345 | * @param String $action The action name. |
||
1346 | * @param Integer $user_id The user identifier. |
||
1347 | */ |
||
1348 | public function delete_secrets( $action, $user_id ) { |
||
1349 | $secret_name = 'jetpack_' . $action . '_' . $user_id; |
||
1350 | $secrets = \Jetpack_Options::get_raw_option( |
||
1351 | self::SECRETS_OPTION_NAME, |
||
1352 | array() |
||
1353 | ); |
||
1354 | if ( isset( $secrets[ $secret_name ] ) ) { |
||
1355 | unset( $secrets[ $secret_name ] ); |
||
1356 | \Jetpack_Options::update_raw_option( self::SECRETS_OPTION_NAME, $secrets ); |
||
1357 | } |
||
1358 | } |
||
1359 | |||
1360 | /** |
||
1361 | * Deletes all connection tokens and transients from the local Jetpack site. |
||
1362 | * If the plugin object has been provided in the constructor, the function first checks |
||
1363 | * whether it's the only active connection. |
||
1364 | * If there are any other connections, the function will do nothing and return `false` |
||
1365 | * (unless `$ignore_connected_plugins` is set to `true`). |
||
1366 | * |
||
1367 | * @param bool $ignore_connected_plugins Delete the tokens even if there are other connected plugins. |
||
1368 | * |
||
1369 | * @return bool True if disconnected successfully, false otherwise. |
||
1370 | */ |
||
1371 | public function delete_all_connection_tokens( $ignore_connected_plugins = false ) { |
||
1372 | View Code Duplication | if ( ! $ignore_connected_plugins && null !== $this->plugin && ! $this->plugin->is_only() ) { |
|
1373 | return false; |
||
1374 | } |
||
1375 | |||
1376 | /** |
||
1377 | * Fires upon the disconnect attempt. |
||
1378 | * Return `false` to prevent the disconnect. |
||
1379 | * |
||
1380 | * @since 8.7.0 |
||
1381 | */ |
||
1382 | if ( ! apply_filters( 'jetpack_connection_delete_all_tokens', true, $this ) ) { |
||
1383 | return false; |
||
1384 | } |
||
1385 | |||
1386 | \Jetpack_Options::delete_option( |
||
1387 | array( |
||
1388 | 'blog_token', |
||
1389 | 'user_token', |
||
1390 | 'user_tokens', |
||
1391 | 'master_user', |
||
1392 | 'time_diff', |
||
1393 | 'fallback_no_verify_ssl_certs', |
||
1394 | ) |
||
1395 | ); |
||
1396 | |||
1397 | \Jetpack_Options::delete_raw_option( 'jetpack_secrets' ); |
||
1398 | |||
1399 | // Delete cached connected user data. |
||
1400 | $transient_key = 'jetpack_connected_user_data_' . get_current_user_id(); |
||
1401 | delete_transient( $transient_key ); |
||
1402 | |||
1403 | // Delete all XML-RPC errors. |
||
1404 | Error_Handler::get_instance()->delete_all_errors(); |
||
1405 | |||
1406 | return true; |
||
1407 | } |
||
1408 | |||
1409 | /** |
||
1410 | * Tells WordPress.com to disconnect the site and clear all tokens from cached site. |
||
1411 | * If the plugin object has been provided in the constructor, the function first check |
||
1412 | * whether it's the only active connection. |
||
1413 | * If there are any other connections, the function will do nothing and return `false` |
||
1414 | * (unless `$ignore_connected_plugins` is set to `true`). |
||
1415 | * |
||
1416 | * @param bool $ignore_connected_plugins Delete the tokens even if there are other connected plugins. |
||
1417 | * |
||
1418 | * @return bool True if disconnected successfully, false otherwise. |
||
1419 | */ |
||
1420 | public function disconnect_site_wpcom( $ignore_connected_plugins = false ) { |
||
1440 | |||
1441 | /** |
||
1442 | * Disconnect the plugin and remove the tokens. |
||
1443 | * This function will automatically perform "soft" or "hard" disconnect depending on whether other plugins are using the connection. |
||
1444 | * This is a proxy method to simplify the Connection package API. |
||
1445 | * |
||
1446 | * @see Manager::disable_plugin() |
||
1447 | * @see Manager::disconnect_site_wpcom() |
||
1448 | * @see Manager::delete_all_connection_tokens() |
||
1449 | * |
||
1450 | * @return bool |
||
1451 | */ |
||
1452 | public function remove_connection() { |
||
1459 | |||
1460 | /** |
||
1461 | * Responds to a WordPress.com call to register the current site. |
||
1462 | * Should be changed to protected. |
||
1463 | * |
||
1464 | * @param array $registration_data Array of [ secret_1, user_id ]. |
||
1465 | */ |
||
1466 | public function handle_registration( array $registration_data ) { |
||
1474 | |||
1475 | /** |
||
1476 | * Verify a Previously Generated Secret. |
||
1477 | * |
||
1478 | * @param string $action The type of secret to verify. |
||
1479 | * @param string $secret_1 The secret string to compare to what is stored. |
||
1480 | * @param int $user_id The user ID of the owner of the secret. |
||
1481 | * @return \WP_Error|string WP_Error on failure, secret_2 on success. |
||
1482 | */ |
||
1483 | public function verify_secrets( $action, $secret_1, $user_id ) { |
||
1619 | |||
1620 | /** |
||
1621 | * Responds to a WordPress.com call to authorize the current user. |
||
1622 | * Should be changed to protected. |
||
1623 | */ |
||
1624 | public function handle_authorization() { |
||
1627 | |||
1628 | /** |
||
1629 | * Obtains the auth token. |
||
1630 | * |
||
1631 | * @param array $data The request data. |
||
1632 | * @return object|\WP_Error Returns the auth token on success. |
||
1633 | * Returns a \WP_Error on failure. |
||
1634 | */ |
||
1635 | public function get_token( $data ) { |
||
1776 | |||
1777 | /** |
||
1778 | * Increases the request timeout value to 30 seconds. |
||
1779 | * |
||
1780 | * @return int Returns 30. |
||
1781 | */ |
||
1782 | public function increase_timeout() { |
||
1785 | |||
1786 | /** |
||
1787 | * Builds a URL to the Jetpack connection auth page. |
||
1788 | * |
||
1789 | * @param WP_User $user (optional) defaults to the current logged in user. |
||
1790 | * @param String $redirect (optional) a redirect URL to use instead of the default. |
||
1791 | * @return string Connect URL. |
||
1792 | */ |
||
1793 | public function get_authorization_url( $user = null, $redirect = null ) { |
||
1879 | |||
1880 | /** |
||
1881 | * Authorizes the user by obtaining and storing the user token. |
||
1882 | * |
||
1883 | * @param array $data The request data. |
||
1884 | * @return string|\WP_Error Returns a string on success. |
||
1885 | * Returns a \WP_Error on failure. |
||
1886 | */ |
||
1887 | public function authorize( $data = array() ) { |
||
1973 | |||
1974 | /** |
||
1975 | * Disconnects from the Jetpack servers. |
||
1976 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
1977 | */ |
||
1978 | public function disconnect_site() { |
||
1981 | |||
1982 | /** |
||
1983 | * The Base64 Encoding of the SHA1 Hash of the Input. |
||
1984 | * |
||
1985 | * @param string $text The string to hash. |
||
1986 | * @return string |
||
1987 | */ |
||
1988 | public function sha1_base64( $text ) { |
||
1991 | |||
1992 | /** |
||
1993 | * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
||
1994 | * |
||
1995 | * @param string $domain The domain to check. |
||
1996 | * |
||
1997 | * @return bool|WP_Error |
||
1998 | */ |
||
1999 | public function is_usable_domain( $domain ) { |
||
2086 | |||
2087 | /** |
||
2088 | * Gets the requested token. |
||
2089 | * |
||
2090 | * Tokens are one of two types: |
||
2091 | * 1. Blog Tokens: These are the "main" tokens. Each site typically has one Blog Token, |
||
2092 | * though some sites can have multiple "Special" Blog Tokens (see below). These tokens |
||
2093 | * are not associated with a user account. They represent the site's connection with |
||
2094 | * the Jetpack servers. |
||
2095 | * 2. User Tokens: These are "sub-"tokens. Each connected user account has one User Token. |
||
2096 | * |
||
2097 | * All tokens look like "{$token_key}.{$private}". $token_key is a public ID for the |
||
2098 | * token, and $private is a secret that should never be displayed anywhere or sent |
||
2099 | * over the network; it's used only for signing things. |
||
2100 | * |
||
2101 | * Blog Tokens can be "Normal" or "Special". |
||
2102 | * * Normal: The result of a normal connection flow. They look like |
||
2103 | * "{$random_string_1}.{$random_string_2}" |
||
2104 | * That is, $token_key and $private are both random strings. |
||
2105 | * Sites only have one Normal Blog Token. Normal Tokens are found in either |
||
2106 | * Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN |
||
2107 | * constant (rare). |
||
2108 | * * Special: A connection token for sites that have gone through an alternative |
||
2109 | * connection flow. They look like: |
||
2110 | * ";{$special_id}{$special_version};{$wpcom_blog_id};.{$random_string}" |
||
2111 | * That is, $private is a random string and $token_key has a special structure with |
||
2112 | * lots of semicolons. |
||
2113 | * Most sites have zero Special Blog Tokens. Special tokens are only found in the |
||
2114 | * JETPACK_BLOG_TOKEN constant. |
||
2115 | * |
||
2116 | * In particular, note that Normal Blog Tokens never start with ";" and that |
||
2117 | * Special Blog Tokens always do. |
||
2118 | * |
||
2119 | * When searching for a matching Blog Tokens, Blog Tokens are examined in the following |
||
2120 | * order: |
||
2121 | * 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
2122 | * 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' )) |
||
2123 | * 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
2124 | * |
||
2125 | * @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
||
2126 | * @param string|false $token_key If provided, check that the token matches the provided input. |
||
2127 | * @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. |
||
2128 | * |
||
2129 | * @return object|false |
||
2130 | */ |
||
2131 | public function get_access_token( $user_id = false, $token_key = false, $suppress_errors = true ) { |
||
2217 | |||
2218 | /** |
||
2219 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths |
||
2220 | * since it is passed by reference to various methods. |
||
2221 | * Capture it here so we can verify the signature later. |
||
2222 | * |
||
2223 | * @param array $methods an array of available XMLRPC methods. |
||
2224 | * @return array the same array, since this method doesn't add or remove anything. |
||
2225 | */ |
||
2226 | public function xmlrpc_methods( $methods ) { |
||
2230 | |||
2231 | /** |
||
2232 | * Resets the raw post data parameter for testing purposes. |
||
2233 | */ |
||
2234 | public function reset_raw_post_data() { |
||
2237 | |||
2238 | /** |
||
2239 | * Registering an additional method. |
||
2240 | * |
||
2241 | * @param array $methods an array of available XMLRPC methods. |
||
2242 | * @return array the amended array in case the method is added. |
||
2243 | */ |
||
2244 | public function public_xmlrpc_methods( $methods ) { |
||
2250 | |||
2251 | /** |
||
2252 | * Handles a getOptions XMLRPC method call. |
||
2253 | * |
||
2254 | * @param array $args method call arguments. |
||
2255 | * @return an amended XMLRPC server options array. |
||
2256 | */ |
||
2257 | public function jetpack_get_options( $args ) { |
||
2298 | |||
2299 | /** |
||
2300 | * Adds Jetpack-specific options to the output of the XMLRPC options method. |
||
2301 | * |
||
2302 | * @param array $options standard Core options. |
||
2303 | * @return array amended options. |
||
2304 | */ |
||
2305 | public function xmlrpc_options( $options ) { |
||
2323 | |||
2324 | /** |
||
2325 | * Resets the saved authentication state in between testing requests. |
||
2326 | */ |
||
2327 | public function reset_saved_auth_state() { |
||
2330 | |||
2331 | /** |
||
2332 | * Sign a user role with the master access token. |
||
2333 | * If not specified, will default to the current user. |
||
2334 | * |
||
2335 | * @access public |
||
2336 | * |
||
2337 | * @param string $role User role. |
||
2338 | * @param int $user_id ID of the user. |
||
2339 | * @return string Signed user role. |
||
2340 | */ |
||
2341 | public function sign_role( $role, $user_id = null ) { |
||
2357 | |||
2358 | /** |
||
2359 | * Set the plugin instance. |
||
2360 | * |
||
2361 | * @param Plugin $plugin_instance The plugin instance. |
||
2362 | * |
||
2363 | * @return $this |
||
2364 | */ |
||
2365 | public function set_plugin_instance( Plugin $plugin_instance ) { |
||
2370 | |||
2371 | /** |
||
2372 | * Retrieve the plugin management object. |
||
2373 | * |
||
2374 | * @return Plugin |
||
2375 | */ |
||
2376 | public function get_plugin() { |
||
2379 | |||
2380 | /** |
||
2381 | * Get all connected plugins information, excluding those disconnected by user. |
||
2382 | * WARNING: the method cannot be called until Plugin_Storage::configure is called, which happens on plugins_loaded |
||
2383 | * Even if you don't use Jetpack Config, it may be introduced later by other plugins, |
||
2384 | * so please make sure not to run the method too early in the code. |
||
2385 | * |
||
2386 | * @return array|WP_Error |
||
2387 | */ |
||
2388 | public function get_connected_plugins() { |
||
2397 | |||
2398 | /** |
||
2399 | * Force plugin disconnect. After its called, the plugin will not be allowed to use the connection. |
||
2400 | * Note: this method does not remove any access tokens. |
||
2401 | * |
||
2402 | * @return bool |
||
2403 | */ |
||
2404 | public function disable_plugin() { |
||
2411 | |||
2412 | /** |
||
2413 | * Force plugin reconnect after user-initiated disconnect. |
||
2414 | * After its called, the plugin will be allowed to use the connection again. |
||
2415 | * Note: this method does not initialize access tokens. |
||
2416 | * |
||
2417 | * @return bool |
||
2418 | */ |
||
2419 | public function enable_plugin() { |
||
2426 | |||
2427 | /** |
||
2428 | * Whether the plugin is allowed to use the connection, or it's been disconnected by user. |
||
2429 | * If no plugin slug was passed into the constructor, always returns true. |
||
2430 | * |
||
2431 | * @return bool |
||
2432 | */ |
||
2433 | public function is_plugin_enabled() { |
||
2440 | |||
2441 | } |
||
2442 |
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.