diff --git a/class.jetpack-data.php b/class.jetpack-data.php index 53c48681a9a2..78031c5fe51d 100644 --- a/class.jetpack-data.php +++ b/class.jetpack-data.php @@ -1,226 +1,13 @@ $valid_token, - 'external_user_id' => (int) $user_id, - ); - } - - /** - * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. - * - * @param $domain - * @param array $extra - * - * @return bool|WP_Error - */ - public static function is_usable_domain( $domain, $extra = array() ) { - - // If it's empty, just fail out. - if ( ! $domain ) { - return new WP_Error( 'fail_domain_empty', sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as it is empty.', 'jetpack' ), $domain ) ); - } - - /** - * Skips the usuable domain check when connecting a site. - * - * Allows site administrators with domains that fail gethostname-based checks to pass the request to WP.com - * - * @since 4.1.0 - * - * @param bool If the check should be skipped. Default false. - */ - if ( apply_filters( 'jetpack_skip_usuable_domain_check', false ) ) { - return true; - } - - // None of the explicit localhosts. - $forbidden_domains = array( - 'wordpress.com', - 'localhost', - 'localhost.localdomain', - '127.0.0.1', - 'local.wordpress.test', // VVV - 'local.wordpress-trunk.test', // VVV - 'src.wordpress-develop.test', // VVV - 'build.wordpress-develop.test', // VVV - ); - if ( in_array( $domain, $forbidden_domains ) ) { - return new WP_Error( 'fail_domain_forbidden', sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as it is in the forbidden array.', 'jetpack' ), $domain ) ); - } - - // No .test or .local domains - if ( preg_match( '#\.(test|local)$#i', $domain ) ) { - return new WP_Error( 'fail_domain_tld', sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as it uses an invalid top level domain.', 'jetpack' ), $domain ) ); - } - - // No WPCOM subdomains - if ( preg_match( '#\.wordpress\.com$#i', $domain ) ) { - return new WP_Error( 'fail_subdomain_wpcom', sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as it is a subdomain of WordPress.com.', 'jetpack' ), $domain ) ); - } - - // If PHP was compiled without support for the Filter module (very edge case) - if ( ! function_exists( 'filter_var' ) ) { - // Just pass back true for now, and let wpcom sort it out. - return true; - } - - return true; - } - - /** - * Returns true if the IP address passed in should not be in a reserved range, even if PHP says that it is. - * See: https://bugs.php.net/bug.php?id=66229 and https://github.com/php/php-src/commit/d1314893fd1325ca6aa0831101896e31135a2658 - * - * This function mirrors Jetpack_Data::php_bug_66229_check() in the WPCOM codebase. - */ - public static function php_bug_66229_check( $ip ) { - if ( ! filter_var( $ip, FILTER_VALIDATE_IP ) ) { - return false; - } - - $ip_arr = array_map( 'intval', explode( '.', $ip ) ); - - if ( 128 == $ip_arr[0] && 0 == $ip_arr[1] ) { - return true; - } - - if ( 191 == $ip_arr[0] && 255 == $ip_arr[1] ) { - return true; - } - - return false; + $connection = new Connection_Manager(); + return $connection->get_access_token( $user_id, $token_key ); } } diff --git a/class.jetpack.php b/class.jetpack.php index f1343065661a..4708b125a7f8 100644 --- a/class.jetpack.php +++ b/class.jetpack.php @@ -3355,7 +3355,7 @@ public static function try_registration() { 'homeurl' => parse_url( get_home_url(), PHP_URL_HOST ), ) ); foreach ( $domains_to_check as $domain ) { - $result = Jetpack_Data::is_usable_domain( $domain ); + $result = self::connection()->is_usable_domain( $domain ); if ( is_wp_error( $result ) ) { return $result; } @@ -4878,6 +4878,10 @@ public static function xmlrpc_api_url() { return untrailingslashit( $base ) . '/xmlrpc.php'; } + public static function connection() { + return self::init()->connection_manager; + } + /** * Creates two secret tokens and the end of life timestamp for them. * @@ -4891,11 +4895,11 @@ public static function generate_secrets( $action, $user_id = false, $exp = 600 ) $user_id = get_current_user_id(); } - return self::init()->connection_manager->generate_secrets( $action, $user_id, $exp ); + return self::connection()->generate_secrets( $action, $user_id, $exp ); } public static function get_secrets( $action, $user_id ) { - $secrets = self::init()->connection_manager->get_secrets( $action, $user_id ); + $secrets = self::connection()->get_secrets( $action, $user_id ); if ( Connection_Manager::SECRETS_MISSING === $secrets ) { return new WP_Error( 'verify_secrets_missing', 'Verification secrets not found' ); @@ -4908,8 +4912,14 @@ public static function get_secrets( $action, $user_id ) { return $secrets; } + /** + * @deprecated 7.5 Use Connection_Manager instead. + * + * @param $action + * @param $user_id + */ public static function delete_secrets( $action, $user_id ) { - return self::init()->connection_manager->delete_secrets( $action, $user_id ); + return self::connection()->delete_secrets( $action, $user_id ); } /** diff --git a/modules/comments/comments.php b/modules/comments/comments.php index 2e2389359f35..bf1e4e874320 100644 --- a/modules/comments/comments.php +++ b/modules/comments/comments.php @@ -1,6 +1,7 @@ $valid_token, + 'external_user_id' => (int) $user_id, + ); + } } diff --git a/tests/php/general/test_class.jetpack-data.php b/tests/php/general/test_class.jetpack-data.php index d4f6db31e398..5ecdd3544d3a 100644 --- a/tests/php/general/test_class.jetpack-data.php +++ b/tests/php/general/test_class.jetpack-data.php @@ -1,6 +1,7 @@ connection = new Connection_Manager(); Jetpack_Options::update_option( 'blog_token', self::STORED ); Jetpack_Options::update_option( 'user_tokens', [ 1 => 'user-one.uno.1', @@ -31,20 +34,19 @@ public function tearDown() { Jetpack_Options::delete_option( 'master_user' ); Constants_Manager::clear_constants(); - + $this->connection = null; parent::tearDown(); } public function test_get_access_token_with_no_args_returns_false_when_no_blog_token() { Jetpack_Options::delete_option( 'blog_token' ); - $token = Jetpack_Data::get_access_token(); + $token = $this->connection->get_access_token(); $this->assertFalse( $token ); } public function test_get_access_token_with_no_args_returns_blog_token() { - $token = Jetpack_Data::get_access_token(); - + $token = $this->connection->get_access_token(); $this->assertEquals( self::STORED, $token->secret ); $this->assertEquals( 0, $token->external_user_id ); } @@ -52,7 +54,7 @@ public function test_get_access_token_with_no_args_returns_blog_token() { public function test_get_access_token_with_no_args_returns_defined_blog_token_when_constant_set() { Constants_Manager::set_constant( 'JETPACK_BLOG_TOKEN', self::DEFINED ); - $token = Jetpack_Data::get_access_token(); + $token = $this->connection->get_access_token(); $this->assertEquals( self::DEFINED, $token->secret ); $this->assertEquals( 0, $token->external_user_id ); @@ -62,7 +64,7 @@ public function test_get_access_token_with_no_args_returns_defined_blog_token_wh Jetpack_Options::delete_option( 'blog_token' ); Constants_Manager::set_constant( 'JETPACK_BLOG_TOKEN', self::DEFINED ); - $token = Jetpack_Data::get_access_token(); + $token = $this->connection->get_access_token(); $this->assertEquals( self::DEFINED, $token->secret ); $this->assertEquals( 0, $token->external_user_id ); @@ -72,7 +74,7 @@ public function test_get_access_token_with_no_args_returns_defined_blog_token_wh public function test_get_access_token_with_stored_key_returns_stored_blog_token() { Constants_Manager::set_constant( 'JETPACK_BLOG_TOKEN', self::DEFINED ); - $token = Jetpack_Data::get_access_token( false, '12345' ); + $token = $this->connection->get_access_token( false, '12345' ); $this->assertEquals( self::STORED, $token->secret ); $this->assertEquals( 0, $token->external_user_id ); @@ -81,7 +83,7 @@ public function test_get_access_token_with_stored_key_returns_stored_blog_token( public function test_get_access_token_with_magic_key_returns_stored_blog_token() { Constants_Manager::set_constant( 'JETPACK_BLOG_TOKEN', self::DEFINED ); - $token = Jetpack_Data::get_access_token( false, Jetpack_Data::MAGIC_NORMAL_TOKEN_KEY ); + $token = $this->connection->get_access_token( false, Connection_Manager::MAGIC_NORMAL_TOKEN_KEY ); $this->assertEquals( self::STORED, $token->secret ); $this->assertEquals( 0, $token->external_user_id ); @@ -92,7 +94,7 @@ public function test_get_access_token_with_magic_key_returns_defined_blog_token_ Jetpack_Options::delete_option( 'blog_token' ); Constants_Manager::set_constant( 'JETPACK_BLOG_TOKEN', self::STORED ); - $token = Jetpack_Data::get_access_token( false, Jetpack_Data::MAGIC_NORMAL_TOKEN_KEY ); + $token = $this->connection->get_access_token( false, Connection_Manager::MAGIC_NORMAL_TOKEN_KEY ); $this->assertEquals( self::STORED, $token->secret ); $this->assertEquals( 0, $token->external_user_id ); @@ -101,7 +103,7 @@ public function test_get_access_token_with_magic_key_returns_defined_blog_token_ public function test_get_access_token_with_no_args_returns_first_defined_blog_token_when_constant_multi_set() { Constants_Manager::set_constant( 'JETPACK_BLOG_TOKEN', self::DEFINED_MULTI ); - $token = Jetpack_Data::get_access_token(); + $token = $this->connection->get_access_token(); $this->assertEquals( ';hello;.world', $token->secret ); $this->assertEquals( 0, $token->external_user_id ); @@ -111,7 +113,7 @@ public function test_get_access_token_with_no_args_returns_first_defined_blog_to Jetpack_Options::delete_option( 'blog_token' ); Constants_Manager::set_constant( 'JETPACK_BLOG_TOKEN', self::DEFINED_MULTI ); - $token = Jetpack_Data::get_access_token(); + $token = $this->connection->get_access_token(); $this->assertEquals( ';hello;.world', $token->secret ); $this->assertEquals( 0, $token->external_user_id ); @@ -120,7 +122,7 @@ public function test_get_access_token_with_no_args_returns_first_defined_blog_to public function test_get_access_token_with_token_key_returns_matching_token_when_constant_multi_set() { Constants_Manager::set_constant( 'JETPACK_BLOG_TOKEN', self::DEFINED_MULTI ); - $token = Jetpack_Data::get_access_token( false, ';foo;' ); + $token = $this->connection->get_access_token( false, ';foo;' ); $this->assertEquals( ';foo;.bar', $token->secret ); $this->assertEquals( 0, $token->external_user_id ); @@ -130,7 +132,7 @@ public function test_get_access_token_with_token_key_returns_matching_token_when Jetpack_Options::delete_option( 'blog_token' ); Constants_Manager::set_constant( 'JETPACK_BLOG_TOKEN', self::DEFINED_MULTI ); - $token = Jetpack_Data::get_access_token( false, ';foo;' ); + $token = $this->connection->get_access_token( false, ';foo;' ); $this->assertEquals( ';foo;.bar', $token->secret ); $this->assertEquals( 0, $token->external_user_id ); @@ -139,7 +141,7 @@ public function test_get_access_token_with_token_key_returns_matching_token_when public function test_get_access_token_with_magic_key_returns_stored_token_when_constant_multi_set() { Constants_Manager::set_constant( 'JETPACK_BLOG_TOKEN', self::DEFINED_MULTI ); - $token = Jetpack_Data::get_access_token( false, Jetpack_Data::MAGIC_NORMAL_TOKEN_KEY ); + $token = $this->connection->get_access_token( false, Connection_Manager::MAGIC_NORMAL_TOKEN_KEY ); $this->assertEquals( self::STORED, $token->secret ); $this->assertEquals( 0, $token->external_user_id ); @@ -149,7 +151,7 @@ public function test_get_access_token_with_magic_key_returns_matching_token_when Jetpack_Options::delete_option( 'blog_token' ); Constants_Manager::set_constant( 'JETPACK_BLOG_TOKEN', self::DEFINED_MULTI ); - $token = Jetpack_Data::get_access_token( false, Jetpack_Data::MAGIC_NORMAL_TOKEN_KEY ); + $token = $this->connection->get_access_token( false, Connection_Manager::MAGIC_NORMAL_TOKEN_KEY ); $this->assertEquals( 'looks-like-a.stored-token', $token->secret ); $this->assertEquals( 0, $token->external_user_id ); @@ -158,7 +160,7 @@ public function test_get_access_token_with_magic_key_returns_matching_token_when public function test_get_access_token_with_token_key_requires_full_key() { Constants_Manager::set_constant( 'JETPACK_BLOG_TOKEN', self::DEFINED_MULTI ); - $token = Jetpack_Data::get_access_token( false, ';fo' ); + $token = $this->connection->get_access_token( false, ';fo' ); $this->assertFalse( $token ); } @@ -166,55 +168,55 @@ public function test_get_access_token_with_token_key_requires_full_key() { public function test_get_access_token_with_user_id_returns_false_when_no_user_tokens() { Jetpack_Options::delete_option( 'user_tokens' ); - $token = Jetpack_Data::get_access_token( 1 ); + $token = $this->connection->get_access_token( 1 ); $this->assertFalse( $token ); } public function test_get_access_token_with_user_id() { - $token = Jetpack_Data::get_access_token( 1 ); + $token = $this->connection->get_access_token( 1 ); $this->assertEquals( 'user-one.uno', $token->secret ); } public function test_get_access_token_with_master_user_returns_false_when_no_master_user() { Jetpack_Options::delete_option( 'master_user' ); - $token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); + $token = $this->connection->get_access_token( JETPACK_MASTER_USER ); $this->assertFalse( $token ); } public function test_get_access_token_with_master_user() { - $token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); + $token = $this->connection->get_access_token( JETPACK_MASTER_USER ); $this->assertEquals( 'user-two.dos', $token->secret ); } public function test_get_access_token_with_unconnected_user() { - $token = Jetpack_Data::get_access_token( 3 ); + $token = $this->connection->get_access_token( 3 ); $this->assertFalse( $token ); } public function test_get_access_token_with_malformed_token_with_missing_user_id() { - $token = Jetpack_Data::get_access_token( 4 ); + $token = $this->connection->get_access_token( 4 ); $this->assertFalse( $token ); } public function test_get_access_token_with_malformed_token_with_wrong_structure() { - $token = Jetpack_Data::get_access_token( 5 ); + $token = $this->connection->get_access_token( 5 ); $this->assertFalse( $token ); } public function test_get_access_token_with_malformed_token_with_falsey_value() { - $token = Jetpack_Data::get_access_token( 6 ); + $token = $this->connection->get_access_token( 6 ); $this->assertFalse( $token ); } public function test_get_access_token_with_malformed_token_with_wrong_user_id() { - $token = Jetpack_Data::get_access_token( 7 ); + $token = $this->connection->get_access_token( 7 ); $this->assertFalse( $token ); }