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 | } |
||
| 97 | |||
| 98 | add_action( 'rest_api_init', array( $manager, 'initialize_rest_api_registration_connector' ) ); |
||
| 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 ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 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 ) { |
||
| 709 | $tokens = \Jetpack_Options::get_option( 'user_tokens' ); |
||
| 710 | if ( ! $tokens ) { |
||
| 711 | return false; |
||
| 712 | } |
||
| 713 | |||
| 714 | $user_id = empty( $user_id ) ? get_current_user_id() : intval( $user_id ); |
||
| 715 | |||
| 716 | if ( \Jetpack_Options::get_option( 'master_user' ) === $user_id ) { |
||
| 717 | return false; |
||
| 718 | } |
||
| 719 | |||
| 720 | if ( ! isset( $tokens[ $user_id ] ) ) { |
||
| 721 | return false; |
||
| 722 | } |
||
| 723 | |||
| 724 | $xml = new \Jetpack_IXR_Client( compact( 'user_id' ) ); |
||
| 725 | $xml->query( 'jetpack.unlink_user', $user_id ); |
||
| 726 | |||
| 727 | unset( $tokens[ $user_id ] ); |
||
| 728 | |||
| 729 | \Jetpack_Options::update_option( 'user_tokens', $tokens ); |
||
| 730 | |||
| 731 | // Delete cached connected user data. |
||
| 732 | $transient_key = "jetpack_connected_user_data_$user_id"; |
||
| 733 | delete_transient( $transient_key ); |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Fires after the current user has been unlinked from WordPress.com. |
||
| 737 | * |
||
| 738 | * @since 4.1.0 |
||
| 739 | * |
||
| 740 | * @param int $user_id The current user's ID. |
||
| 741 | */ |
||
| 742 | do_action( 'jetpack_unlinked_user', $user_id ); |
||
| 743 | |||
| 744 | return true; |
||
| 745 | } |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Returns the requested Jetpack API URL. |
||
| 749 | * |
||
| 750 | * @param String $relative_url the relative API path. |
||
| 751 | * @return String API URL. |
||
| 752 | */ |
||
| 753 | public function api_url( $relative_url ) { |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Returns the Jetpack XMLRPC WordPress.com API endpoint URL. |
||
| 793 | * |
||
| 794 | * @return String XMLRPC API URL. |
||
| 795 | */ |
||
| 796 | public function xmlrpc_api_url() { |
||
| 804 | |||
| 805 | /** |
||
| 806 | * Attempts Jetpack registration which sets up the site for connection. Should |
||
| 807 | * remain public because the call to action comes from the current site, not from |
||
| 808 | * WordPress.com. |
||
| 809 | * |
||
| 810 | * @param String $api_endpoint (optional) an API endpoint to use, defaults to 'register'. |
||
| 811 | * @return true|WP_Error The error object. |
||
| 812 | */ |
||
| 813 | public function register( $api_endpoint = 'register' ) { |
||
| 814 | add_action( 'pre_update_jetpack_option_register', array( '\\Jetpack_Options', 'delete_option' ) ); |
||
| 949 | |||
| 950 | /** |
||
| 951 | * Takes the response from the Jetpack register new site endpoint and |
||
| 952 | * verifies it worked properly. |
||
| 953 | * |
||
| 954 | * @since 2.6 |
||
| 955 | * |
||
| 956 | * @param Mixed $response the response object, or the error object. |
||
| 957 | * @return string|WP_Error A JSON object on success or WP_Error on failures |
||
| 958 | **/ |
||
| 959 | protected function validate_remote_register_response( $response ) { |
||
| 1028 | |||
| 1029 | /** |
||
| 1030 | * Adds a used nonce to a list of known nonces. |
||
| 1031 | * |
||
| 1032 | * @param int $timestamp the current request timestamp. |
||
| 1033 | * @param string $nonce the nonce value. |
||
| 1034 | * @return bool whether the nonce is unique or not. |
||
| 1035 | */ |
||
| 1036 | public function add_nonce( $timestamp, $nonce ) { |
||
| 1074 | |||
| 1075 | /** |
||
| 1076 | * Cleans nonces that were saved when calling ::add_nonce. |
||
| 1077 | * |
||
| 1078 | * @todo Properly prepare the query before executing it. |
||
| 1079 | * |
||
| 1080 | * @param bool $all whether to clean even non-expired nonces. |
||
| 1081 | */ |
||
| 1082 | public function clean_nonces( $all = false ) { |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * Sets the Connection custom capabilities. |
||
| 1106 | * |
||
| 1107 | * @param string[] $caps Array of the user's capabilities. |
||
| 1108 | * @param string $cap Capability name. |
||
| 1109 | * @param int $user_id The user ID. |
||
| 1110 | * @param array $args Adds the context to the cap. Typically the object ID. |
||
| 1111 | */ |
||
| 1112 | public function jetpack_connection_custom_caps( $caps, $cap, $user_id, $args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 1143 | |||
| 1144 | /** |
||
| 1145 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
| 1146 | * |
||
| 1147 | * Based on local php max_execution_time in php.ini |
||
| 1148 | * |
||
| 1149 | * @since 5.4 |
||
| 1150 | * @return int |
||
| 1151 | **/ |
||
| 1152 | public function get_max_execution_time() { |
||
| 1161 | |||
| 1162 | /** |
||
| 1163 | * Sets a minimum request timeout, and returns the current timeout |
||
| 1164 | * |
||
| 1165 | * @since 5.4 |
||
| 1166 | * @param Integer $min_timeout the minimum timeout value. |
||
| 1167 | **/ |
||
| 1168 | View Code Duplication | public function set_min_time_limit( $min_timeout ) { |
|
| 1176 | |||
| 1177 | /** |
||
| 1178 | * Get our assumed site creation date. |
||
| 1179 | * Calculated based on the earlier date of either: |
||
| 1180 | * - Earliest admin user registration date. |
||
| 1181 | * - Earliest date of post of any post type. |
||
| 1182 | * |
||
| 1183 | * @since 7.2.0 |
||
| 1184 | * |
||
| 1185 | * @return string Assumed site creation date and time. |
||
| 1186 | */ |
||
| 1187 | public function get_assumed_site_creation_date() { |
||
| 1226 | |||
| 1227 | /** |
||
| 1228 | * Adds the activation source string as a parameter to passed arguments. |
||
| 1229 | * |
||
| 1230 | * @todo Refactor to use rawurlencode() instead of urlencode(). |
||
| 1231 | * |
||
| 1232 | * @param array $args arguments that need to have the source added. |
||
| 1233 | * @return array $amended arguments. |
||
| 1234 | */ |
||
| 1235 | View Code Duplication | public static function apply_activation_source_to_args( $args ) { |
|
| 1250 | |||
| 1251 | /** |
||
| 1252 | * Returns the callable that would be used to generate secrets. |
||
| 1253 | * |
||
| 1254 | * @return Callable a function that returns a secure string to be used as a secret. |
||
| 1255 | */ |
||
| 1256 | protected function get_secret_callable() { |
||
| 1268 | |||
| 1269 | /** |
||
| 1270 | * Runs the wp_generate_password function with the required parameters. This is the |
||
| 1271 | * default implementation of the secret callable, can be overridden using the |
||
| 1272 | * jetpack_connection_secret_generator filter. |
||
| 1273 | * |
||
| 1274 | * @return String $secret value. |
||
| 1275 | */ |
||
| 1276 | private function secret_callable_method() { |
||
| 1279 | |||
| 1280 | /** |
||
| 1281 | * Generates two secret tokens and the end of life timestamp for them. |
||
| 1282 | * |
||
| 1283 | * @param String $action The action name. |
||
| 1284 | * @param Integer $user_id The user identifier. |
||
| 1285 | * @param Integer $exp Expiration time in seconds. |
||
| 1286 | */ |
||
| 1287 | public function generate_secrets( $action, $user_id = false, $exp = 600 ) { |
||
| 1319 | |||
| 1320 | /** |
||
| 1321 | * Returns two secret tokens and the end of life timestamp for them. |
||
| 1322 | * |
||
| 1323 | * @param String $action The action name. |
||
| 1324 | * @param Integer $user_id The user identifier. |
||
| 1325 | * @return string|array an array of secrets or an error string. |
||
| 1326 | */ |
||
| 1327 | public function get_secrets( $action, $user_id ) { |
||
| 1345 | |||
| 1346 | /** |
||
| 1347 | * Deletes secret tokens in case they, for example, have expired. |
||
| 1348 | * |
||
| 1349 | * @param String $action The action name. |
||
| 1350 | * @param Integer $user_id The user identifier. |
||
| 1351 | */ |
||
| 1352 | public function delete_secrets( $action, $user_id ) { |
||
| 1363 | |||
| 1364 | /** |
||
| 1365 | * Deletes all connection tokens and transients from the local Jetpack site. |
||
| 1366 | * If the plugin object has been provided in the constructor, the function first checks |
||
| 1367 | * whether it's the only active connection. |
||
| 1368 | * If there are any other connections, the function will do nothing and return `false` |
||
| 1369 | * (unless `$ignore_connected_plugins` is set to `true`). |
||
| 1370 | * |
||
| 1371 | * @param bool $ignore_connected_plugins Delete the tokens even if there are other connected plugins. |
||
| 1372 | * |
||
| 1373 | * @return bool True if disconnected successfully, false otherwise. |
||
| 1374 | */ |
||
| 1375 | public function delete_all_connection_tokens( $ignore_connected_plugins = false ) { |
||
| 1412 | |||
| 1413 | /** |
||
| 1414 | * Tells WordPress.com to disconnect the site and clear all tokens from cached site. |
||
| 1415 | * If the plugin object has been provided in the constructor, the function first check |
||
| 1416 | * whether it's the only active connection. |
||
| 1417 | * If there are any other connections, the function will do nothing and return `false` |
||
| 1418 | * (unless `$ignore_connected_plugins` is set to `true`). |
||
| 1419 | * |
||
| 1420 | * @param bool $ignore_connected_plugins Delete the tokens even if there are other connected plugins. |
||
| 1421 | * |
||
| 1422 | * @return bool True if disconnected successfully, false otherwise. |
||
| 1423 | */ |
||
| 1424 | public function disconnect_site_wpcom( $ignore_connected_plugins = false ) { |
||
| 1444 | |||
| 1445 | /** |
||
| 1446 | * Disconnect the plugin and remove the tokens. |
||
| 1447 | * This function will automatically perform "soft" or "hard" disconnect depending on whether other plugins are using the connection. |
||
| 1448 | * This is a proxy method to simplify the Connection package API. |
||
| 1449 | * |
||
| 1450 | * @see Manager::disable_plugin() |
||
| 1451 | * @see Manager::disconnect_site_wpcom() |
||
| 1452 | * @see Manager::delete_all_connection_tokens() |
||
| 1453 | * |
||
| 1454 | * @return bool |
||
| 1455 | */ |
||
| 1456 | public function remove_connection() { |
||
| 1463 | |||
| 1464 | /** |
||
| 1465 | * Completely clearing up the connection, and initiating reconnect. |
||
| 1466 | * |
||
| 1467 | * @return true|WP_Error True if reconnected successfully, a `WP_Error` object otherwise. |
||
| 1468 | */ |
||
| 1469 | public function reconnect() { |
||
| 1475 | |||
| 1476 | /** |
||
| 1477 | * Responds to a WordPress.com call to register the current site. |
||
| 1478 | * Should be changed to protected. |
||
| 1479 | * |
||
| 1480 | * @param array $registration_data Array of [ secret_1, user_id ]. |
||
| 1481 | */ |
||
| 1482 | public function handle_registration( array $registration_data ) { |
||
| 1490 | |||
| 1491 | /** |
||
| 1492 | * Verify a Previously Generated Secret. |
||
| 1493 | * |
||
| 1494 | * @param string $action The type of secret to verify. |
||
| 1495 | * @param string $secret_1 The secret string to compare to what is stored. |
||
| 1496 | * @param int $user_id The user ID of the owner of the secret. |
||
| 1497 | * @return \WP_Error|string WP_Error on failure, secret_2 on success. |
||
| 1498 | */ |
||
| 1499 | public function verify_secrets( $action, $secret_1, $user_id ) { |
||
| 1635 | |||
| 1636 | /** |
||
| 1637 | * Responds to a WordPress.com call to authorize the current user. |
||
| 1638 | * Should be changed to protected. |
||
| 1639 | */ |
||
| 1640 | public function handle_authorization() { |
||
| 1643 | |||
| 1644 | /** |
||
| 1645 | * Obtains the auth token. |
||
| 1646 | * |
||
| 1647 | * @param array $data The request data. |
||
| 1648 | * @return object|\WP_Error Returns the auth token on success. |
||
| 1649 | * Returns a \WP_Error on failure. |
||
| 1650 | */ |
||
| 1651 | public function get_token( $data ) { |
||
| 1792 | |||
| 1793 | /** |
||
| 1794 | * Increases the request timeout value to 30 seconds. |
||
| 1795 | * |
||
| 1796 | * @return int Returns 30. |
||
| 1797 | */ |
||
| 1798 | public function increase_timeout() { |
||
| 1801 | |||
| 1802 | /** |
||
| 1803 | * Builds a URL to the Jetpack connection auth page. |
||
| 1804 | * |
||
| 1805 | * @param WP_User $user (optional) defaults to the current logged in user. |
||
| 1806 | * @param String $redirect (optional) a redirect URL to use instead of the default. |
||
| 1807 | * @return string Connect URL. |
||
| 1808 | */ |
||
| 1809 | public function get_authorization_url( $user = null, $redirect = null ) { |
||
| 1895 | |||
| 1896 | /** |
||
| 1897 | * Authorizes the user by obtaining and storing the user token. |
||
| 1898 | * |
||
| 1899 | * @param array $data The request data. |
||
| 1900 | * @return string|\WP_Error Returns a string on success. |
||
| 1901 | * Returns a \WP_Error on failure. |
||
| 1902 | */ |
||
| 1903 | public function authorize( $data = array() ) { |
||
| 1989 | |||
| 1990 | /** |
||
| 1991 | * Disconnects from the Jetpack servers. |
||
| 1992 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
| 1993 | */ |
||
| 1994 | public function disconnect_site() { |
||
| 1997 | |||
| 1998 | /** |
||
| 1999 | * The Base64 Encoding of the SHA1 Hash of the Input. |
||
| 2000 | * |
||
| 2001 | * @param string $text The string to hash. |
||
| 2002 | * @return string |
||
| 2003 | */ |
||
| 2004 | public function sha1_base64( $text ) { |
||
| 2007 | |||
| 2008 | /** |
||
| 2009 | * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
||
| 2010 | * |
||
| 2011 | * @param string $domain The domain to check. |
||
| 2012 | * |
||
| 2013 | * @return bool|WP_Error |
||
| 2014 | */ |
||
| 2015 | public function is_usable_domain( $domain ) { |
||
| 2102 | |||
| 2103 | /** |
||
| 2104 | * Gets the requested token. |
||
| 2105 | * |
||
| 2106 | * Tokens are one of two types: |
||
| 2107 | * 1. Blog Tokens: These are the "main" tokens. Each site typically has one Blog Token, |
||
| 2108 | * though some sites can have multiple "Special" Blog Tokens (see below). These tokens |
||
| 2109 | * are not associated with a user account. They represent the site's connection with |
||
| 2110 | * the Jetpack servers. |
||
| 2111 | * 2. User Tokens: These are "sub-"tokens. Each connected user account has one User Token. |
||
| 2112 | * |
||
| 2113 | * All tokens look like "{$token_key}.{$private}". $token_key is a public ID for the |
||
| 2114 | * token, and $private is a secret that should never be displayed anywhere or sent |
||
| 2115 | * over the network; it's used only for signing things. |
||
| 2116 | * |
||
| 2117 | * Blog Tokens can be "Normal" or "Special". |
||
| 2118 | * * Normal: The result of a normal connection flow. They look like |
||
| 2119 | * "{$random_string_1}.{$random_string_2}" |
||
| 2120 | * That is, $token_key and $private are both random strings. |
||
| 2121 | * Sites only have one Normal Blog Token. Normal Tokens are found in either |
||
| 2122 | * Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN |
||
| 2123 | * constant (rare). |
||
| 2124 | * * Special: A connection token for sites that have gone through an alternative |
||
| 2125 | * connection flow. They look like: |
||
| 2126 | * ";{$special_id}{$special_version};{$wpcom_blog_id};.{$random_string}" |
||
| 2127 | * That is, $private is a random string and $token_key has a special structure with |
||
| 2128 | * lots of semicolons. |
||
| 2129 | * Most sites have zero Special Blog Tokens. Special tokens are only found in the |
||
| 2130 | * JETPACK_BLOG_TOKEN constant. |
||
| 2131 | * |
||
| 2132 | * In particular, note that Normal Blog Tokens never start with ";" and that |
||
| 2133 | * Special Blog Tokens always do. |
||
| 2134 | * |
||
| 2135 | * When searching for a matching Blog Tokens, Blog Tokens are examined in the following |
||
| 2136 | * order: |
||
| 2137 | * 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
| 2138 | * 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' )) |
||
| 2139 | * 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
| 2140 | * |
||
| 2141 | * @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
||
| 2142 | * @param string|false $token_key If provided, check that the token matches the provided input. |
||
| 2143 | * @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. |
||
| 2144 | * |
||
| 2145 | * @return object|false |
||
| 2146 | */ |
||
| 2147 | public function get_access_token( $user_id = false, $token_key = false, $suppress_errors = true ) { |
||
| 2242 | |||
| 2243 | /** |
||
| 2244 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths |
||
| 2245 | * since it is passed by reference to various methods. |
||
| 2246 | * Capture it here so we can verify the signature later. |
||
| 2247 | * |
||
| 2248 | * @param array $methods an array of available XMLRPC methods. |
||
| 2249 | * @return array the same array, since this method doesn't add or remove anything. |
||
| 2250 | */ |
||
| 2251 | public function xmlrpc_methods( $methods ) { |
||
| 2255 | |||
| 2256 | /** |
||
| 2257 | * Resets the raw post data parameter for testing purposes. |
||
| 2258 | */ |
||
| 2259 | public function reset_raw_post_data() { |
||
| 2262 | |||
| 2263 | /** |
||
| 2264 | * Registering an additional method. |
||
| 2265 | * |
||
| 2266 | * @param array $methods an array of available XMLRPC methods. |
||
| 2267 | * @return array the amended array in case the method is added. |
||
| 2268 | */ |
||
| 2269 | public function public_xmlrpc_methods( $methods ) { |
||
| 2275 | |||
| 2276 | /** |
||
| 2277 | * Handles a getOptions XMLRPC method call. |
||
| 2278 | * |
||
| 2279 | * @param array $args method call arguments. |
||
| 2280 | * @return an amended XMLRPC server options array. |
||
| 2281 | */ |
||
| 2282 | public function jetpack_get_options( $args ) { |
||
| 2323 | |||
| 2324 | /** |
||
| 2325 | * Adds Jetpack-specific options to the output of the XMLRPC options method. |
||
| 2326 | * |
||
| 2327 | * @param array $options standard Core options. |
||
| 2328 | * @return array amended options. |
||
| 2329 | */ |
||
| 2330 | public function xmlrpc_options( $options ) { |
||
| 2348 | |||
| 2349 | /** |
||
| 2350 | * Resets the saved authentication state in between testing requests. |
||
| 2351 | */ |
||
| 2352 | public function reset_saved_auth_state() { |
||
| 2355 | |||
| 2356 | /** |
||
| 2357 | * Sign a user role with the master access token. |
||
| 2358 | * If not specified, will default to the current user. |
||
| 2359 | * |
||
| 2360 | * @access public |
||
| 2361 | * |
||
| 2362 | * @param string $role User role. |
||
| 2363 | * @param int $user_id ID of the user. |
||
| 2364 | * @return string Signed user role. |
||
| 2365 | */ |
||
| 2366 | public function sign_role( $role, $user_id = null ) { |
||
| 2382 | |||
| 2383 | /** |
||
| 2384 | * Set the plugin instance. |
||
| 2385 | * |
||
| 2386 | * @param Plugin $plugin_instance The plugin instance. |
||
| 2387 | * |
||
| 2388 | * @return $this |
||
| 2389 | */ |
||
| 2390 | public function set_plugin_instance( Plugin $plugin_instance ) { |
||
| 2395 | |||
| 2396 | /** |
||
| 2397 | * Retrieve the plugin management object. |
||
| 2398 | * |
||
| 2399 | * @return Plugin |
||
| 2400 | */ |
||
| 2401 | public function get_plugin() { |
||
| 2404 | |||
| 2405 | /** |
||
| 2406 | * Get all connected plugins information, excluding those disconnected by user. |
||
| 2407 | * WARNING: the method cannot be called until Plugin_Storage::configure is called, which happens on plugins_loaded |
||
| 2408 | * Even if you don't use Jetpack Config, it may be introduced later by other plugins, |
||
| 2409 | * so please make sure not to run the method too early in the code. |
||
| 2410 | * |
||
| 2411 | * @return array|WP_Error |
||
| 2412 | */ |
||
| 2413 | public function get_connected_plugins() { |
||
| 2422 | |||
| 2423 | /** |
||
| 2424 | * Force plugin disconnect. After its called, the plugin will not be allowed to use the connection. |
||
| 2425 | * Note: this method does not remove any access tokens. |
||
| 2426 | * |
||
| 2427 | * @return bool |
||
| 2428 | */ |
||
| 2429 | public function disable_plugin() { |
||
| 2436 | |||
| 2437 | /** |
||
| 2438 | * Force plugin reconnect after user-initiated disconnect. |
||
| 2439 | * After its called, the plugin will be allowed to use the connection again. |
||
| 2440 | * Note: this method does not initialize access tokens. |
||
| 2441 | * |
||
| 2442 | * @return bool |
||
| 2443 | */ |
||
| 2444 | public function enable_plugin() { |
||
| 2451 | |||
| 2452 | /** |
||
| 2453 | * Whether the plugin is allowed to use the connection, or it's been disconnected by user. |
||
| 2454 | * If no plugin slug was passed into the constructor, always returns true. |
||
| 2455 | * |
||
| 2456 | * @return bool |
||
| 2457 | */ |
||
| 2458 | public function is_plugin_enabled() { |
||
| 2465 | |||
| 2466 | } |
||
| 2467 |
This check looks for
@paramannotations 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.