diff --git a/.gitignore b/.gitignore index c3dff8378e7..328dc6a0dc2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ node_modules bower_components -/vendor +vendor +/third-party release composer.lock phpunit.xml diff --git a/.travis.yml b/.travis.yml index a356450e430..7f899d39c1d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,6 +38,12 @@ branches: # Before install, failures in this section will result in build status 'errored' before_install: + - npm install || exit 1 + - docker run --rm -v "$PWD:/app" composer install + - | + if [[ "$PHP" == "1" ]] || [[ "$JS" == "1" ]] || [[ "$SNIFF" == "1" ]]; then + npm install -g gulp-cli + fi - | if [[ "$WP_VERSION" == "latest" ]]; then curl -s http://api.wordpress.org/core/version-check/1.7/ > /tmp/wp-latest.json @@ -47,17 +53,6 @@ before_install: if [[ "$PHP" == "1" ]]; then git clone --depth=1 --branch="$WP_VERSION" git://develop.git.wordpress.org/ /tmp/wordpress cp /tmp/wordpress/wp-tests-config-sample.php /tmp/wordpress/tests/phpunit/wp-tests-config.php - fi - -before_script: - - npm install || exit 1 - - composer install - - | - if [[ "$PHP" == "1" ]] || [[ "$JS" == "1" ]] || [[ "$SNIFF" == "1" ]]; then - npm install -g gulp-cli - fi - - | - if [[ "$PHP" == "1" ]]; then export WP_DEVELOP_DIR=/tmp/wordpress export WP_TESTS_DIR=/tmp/wordpress/tests/phpunit bash tests/bin/install-wp-tests.sh wordpress_test root '' localhost diff --git a/composer.json b/composer.json index 3f8a374b6b9..24f302df2a6 100644 --- a/composer.json +++ b/composer.json @@ -37,6 +37,21 @@ } }, "scripts": { + "post-install-cmd": [ + "@prefix-dependencies" + ], + "post-update-cmd": [ + "@prefix-dependencies" + ], + "prefix-dependencies": [ + "rm -rf php-scoper && mkdir php-scoper", + "cd php-scoper && composer init -q && composer config minimum-stability dev && composer config prefer-stable true && composer require humbug/php-scoper", + "php-scoper/vendor/bin/php-scoper add --output-dir=./third-party --force", + "cd includes && echo '{ \"autoload\": { \"classmap\": [\"\"] } }' > composer.json && composer dump-autoload --classmap-authoritative --no-interaction && rm composer.json", + "cd third-party && echo '{ \"autoload\": { \"classmap\": [\"\"] } }' > composer.json && composer dump-autoload --classmap-authoritative --no-interaction && rm composer.json", + "cp vendor/composer/autoload_files.php third-party/vendor/composer/autoload_files.php", + "rm -rf php-scoper" + ], "lint": "vendor/bin/phpcs", "lint-fix": "vendor/bin/phpcbf" } diff --git a/gulp-tasks/copy.js b/gulp-tasks/copy.js index cd452a23006..8fc1246283e 100644 --- a/gulp-tasks/copy.js +++ b/gulp-tasks/copy.js @@ -15,35 +15,9 @@ gulp.task( 'copy', () => { 'dist/assets/**/*', 'bin/**/*', 'includes/**/*', - 'vendor/google/apiclient/**/*', - 'vendor/google/auth/**/*', - 'vendor/google/apiclient-services/src/Google/Service/Analytics.php', - 'vendor/google/apiclient-services/src/Google/Service/Analytics/**/*', - 'vendor/google/apiclient-services/src/Google/Service/AnalyticsReporting.php', - 'vendor/google/apiclient-services/src/Google/Service/AnalyticsReporting/**/*', - 'vendor/google/apiclient-services/src/Google/Service/AdSense.php', - 'vendor/google/apiclient-services/src/Google/Service/AdSense/**/*', - 'vendor/google/apiclient-services/src/Google/Service/Pagespeedonline.php', - 'vendor/google/apiclient-services/src/Google/Service/Pagespeedonline/**/*', - 'vendor/google/apiclient-services/src/Google/Service/Webmasters.php', - 'vendor/google/apiclient-services/src/Google/Service/Webmasters/**/*', - 'vendor/google/apiclient-services/src/Google/Service/SiteVerification.php', - 'vendor/google/apiclient-services/src/Google/Service/SiteVerification/**/*', - 'vendor/google/apiclient-services/src/Google/Service/TagManager.php', - 'vendor/google/apiclient-services/src/Google/Service/TagManager/**/*', - 'vendor/google/apiclient-services/src/Google/Service/PeopleService.php', - 'vendor/google/apiclient-services/src/Google/Service/PeopleService/**/*', - 'vendor/firebase/**/*', - 'vendor/guzzlehttp/**/*', - 'vendor/psr/**/*', - 'vendor/monolog/**/*', - 'vendor/symfony/**/*', - 'vendor/react/**/*', - 'vendor/ralouphie/**/*', - 'vendor/composer/*', - 'vendor/autoload.php', - '!vendor/**/**/{tests,Tests,doc?(s),examples}/**/*', - '!vendor/**/**/{*.md,*.yml,phpunit.*}', + 'third-party/**/*', + '!third-party/**/**/{tests,Tests,doc?(s),examples}/**/*', + '!third-party/**/**/{*.md,*.yml,phpunit.*}', '!**/*.map', '!bin/local-env/**/*', '!bin/local-env/', diff --git a/includes/Core/Authentication/Clients/Google_Proxy_Client.php b/includes/Core/Authentication/Clients/Google_Proxy_Client.php index 2228cbf3bf1..cbe3a873612 100644 --- a/includes/Core/Authentication/Clients/Google_Proxy_Client.php +++ b/includes/Core/Authentication/Clients/Google_Proxy_Client.php @@ -10,12 +10,12 @@ namespace Google\Site_Kit\Core\Authentication\Clients; -use Google_Client; -use Google\Auth\OAuth2; -use Google\Auth\HttpHandler\HttpHandlerFactory; -use Google\Auth\HttpHandler\HttpClientCache; -use GuzzleHttp\Psr7; -use GuzzleHttp\Psr7\Request; +use Google\Site_Kit_Dependencies\Google_Client; +use Google\Site_Kit_Dependencies\Google\Auth\OAuth2; +use Google\Site_Kit_Dependencies\Google\Auth\HttpHandler\HttpHandlerFactory; +use Google\Site_Kit_Dependencies\Google\Auth\HttpHandler\HttpClientCache; +use Google\Site_Kit_Dependencies\GuzzleHttp\Psr7; +use Google\Site_Kit_Dependencies\GuzzleHttp\Psr7\Request; use Exception; use InvalidArgumentException; use LogicException; diff --git a/includes/Core/Authentication/Clients/OAuth_Client.php b/includes/Core/Authentication/Clients/OAuth_Client.php index ac1b41a4e43..fcb958a091f 100644 --- a/includes/Core/Authentication/Clients/OAuth_Client.php +++ b/includes/Core/Authentication/Clients/OAuth_Client.php @@ -18,7 +18,7 @@ use Google\Site_Kit\Core\Authentication\Credentials; use Google\Site_Kit\Core\Authentication\Verification; use Google\Site_Kit\Modules\Search_Console; -use Google_Client; +use Google\Site_Kit_Dependencies\Google_Client; use Exception; /** diff --git a/includes/Core/Authentication/Profile.php b/includes/Core/Authentication/Profile.php index 5ec7a7c3a79..e88386c16cb 100644 --- a/includes/Core/Authentication/Profile.php +++ b/includes/Core/Authentication/Profile.php @@ -13,6 +13,7 @@ use Google\Site_Kit\Helpers; use Google\Site_Kit\Core\Storage\User_Options; use Google\Site_Kit\Core\Authentication\Clients\OAuth_Client; +use Google\Site_Kit_Dependencies\Google_Service_PeopleService; /** * Class controlling the user's Google profile. @@ -125,7 +126,7 @@ private function retrieve_google_profile_from_api() { // Retrieve and store the user's Google profile data. try { $client = $this->auth_client->get_client(); - $people_service = new \Google_Service_PeopleService( $client ); + $people_service = new Google_Service_PeopleService( $client ); $profile = $people_service->people->get( 'people/me', array( 'personFields' => 'emailAddresses,photos' ) ); if ( isset( $profile['emailAddresses'][0]['value'] ) && isset( $profile['photos'][0]['url'] ) ) { diff --git a/includes/Core/Modules/Module.php b/includes/Core/Modules/Module.php index a8988018a64..e00218d449b 100644 --- a/includes/Core/Modules/Module.php +++ b/includes/Core/Modules/Module.php @@ -15,11 +15,11 @@ use Google\Site_Kit\Core\Storage\User_Options; use Google\Site_Kit\Core\Storage\Cache; use Google\Site_Kit\Core\Authentication\Authentication; -use Google_Client; -use Google_Service; -use Google_Service_Exception; -use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; +use Google\Site_Kit_Dependencies\Google_Client; +use Google\Site_Kit_Dependencies\Google_Service; +use Google\Site_Kit_Dependencies\Google_Service_Exception; +use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface; +use Google\Site_Kit_Dependencies\Psr\Http\Message\ResponseInterface; use WP_Error; use Exception; diff --git a/includes/Core/REST_API/REST_Routes.php b/includes/Core/REST_API/REST_Routes.php index 84b99a0be1a..2e3a74ed82d 100644 --- a/includes/Core/REST_API/REST_Routes.php +++ b/includes/Core/REST_API/REST_Routes.php @@ -18,6 +18,7 @@ use Google\Site_Kit\Core\Authentication\Authentication; use Google\Site_Kit\Core\Authentication\Clients\OAuth_Client; use Google\Site_Kit\Core\Util\Reset; +use Google\Site_Kit_Dependencies\Google_Collection; use WP_Post; use WP_REST_Server; use WP_REST_Request; @@ -672,7 +673,7 @@ private function parse_google_response_data( $data ) { return $data; } - // There is an compatibility issue with \Google_Collection object and wp_json_encode in PHP 5.4 only. + // There is an compatibility issue with Google_Collection object and wp_json_encode in PHP 5.4 only. // These lines will encode/decode to deep convert objects, ensuring all data is returned. if ( version_compare( PHP_VERSION, '5.5.0', '<' ) ) { $data = json_decode( json_encode( $data ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode diff --git a/includes/Core/Storage/Cache.php b/includes/Core/Storage/Cache.php index 962a7ff6310..43312b8b044 100644 --- a/includes/Core/Storage/Cache.php +++ b/includes/Core/Storage/Cache.php @@ -10,6 +10,8 @@ namespace Google\Site_Kit\Core\Storage; +use Google\Site_Kit_Dependencies\Google_Service_Exception; + /** * Class providing a server side caching framework. * @@ -122,7 +124,7 @@ public function cache_batch_results( $batch_requests, $results ) { $request_keys = wp_list_pluck( $batch_requests, 'key' ); foreach ( $results as $key => $result ) { - if ( $result instanceof \Exception || $result instanceof \Google_Service_Exception ) { + if ( $result instanceof \Exception || $result instanceof Google_Service_Exception ) { continue; } $key = str_replace( 'response-', '', $key ); diff --git a/includes/Modules/AdSense.php b/includes/Modules/AdSense.php index f4506939406..38cb48d95bc 100644 --- a/includes/Modules/AdSense.php +++ b/includes/Modules/AdSense.php @@ -15,8 +15,10 @@ use Google\Site_Kit\Core\Modules\Module_With_Screen_Trait; use Google\Site_Kit\Core\Modules\Module_With_Scopes; use Google\Site_Kit\Core\Modules\Module_With_Scopes_Trait; -use Google_Client; -use Psr\Http\Message\RequestInterface; +use Google\Site_Kit_Dependencies\Google_Client; +use Google\Site_Kit_Dependencies\Google_Service_AdSense; +use Google\Site_Kit_Dependencies\Google_Service_AdSense_Alert; +use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface; use WP_Error; /** @@ -488,7 +490,7 @@ protected function create_data_request( $method, $datapoint, array $data = array } $alerts = array_filter( $alerts, - function( \Google_Service_AdSense_Alert $alert ) { + function( Google_Service_AdSense_Alert $alert ) { return 'SEVERE' === $alert->getSeverity(); } ); @@ -501,7 +503,7 @@ function( \Google_Service_AdSense_Alert $alert ) { /** * First Alert * - * @var \Google_Service_AdSense_Alert $alert + * @var Google_Service_AdSense_Alert $alert */ $alert = array_shift( $alerts ); return array( @@ -852,7 +854,7 @@ protected function setup_info() { */ protected function setup_services( Google_Client $client ) { return array( - 'adsense' => new \Google_Service_AdSense( $client ), + 'adsense' => new Google_Service_AdSense( $client ), ); } } diff --git a/includes/Modules/Analytics.php b/includes/Modules/Analytics.php index 1d258715028..dbbcb409183 100644 --- a/includes/Modules/Analytics.php +++ b/includes/Modules/Analytics.php @@ -15,9 +15,24 @@ use Google\Site_Kit\Core\Modules\Module_With_Screen_Trait; use Google\Site_Kit\Core\Modules\Module_With_Scopes; use Google\Site_Kit\Core\Modules\Module_With_Scopes_Trait; -use Google_Client; -use Google_Service_Exception; -use Psr\Http\Message\RequestInterface; +use Google\Site_Kit_Dependencies\Google_Client; +use Google\Site_Kit_Dependencies\Google_Service_Exception; +use Google\Site_Kit_Dependencies\Google_Service_Analytics; +use Google\Site_Kit_Dependencies\Google_Service_AnalyticsReporting; +use Google\Site_Kit_Dependencies\Google_Service_AnalyticsReporting_GetReportsRequest; +use Google\Site_Kit_Dependencies\Google_Service_AnalyticsReporting_ReportRequest; +use Google\Site_Kit_Dependencies\Google_Service_AnalyticsReporting_Dimension; +use Google\Site_Kit_Dependencies\Google_Service_AnalyticsReporting_DimensionFilter; +use Google\Site_Kit_Dependencies\Google_Service_AnalyticsReporting_DimensionFilterClause; +use Google\Site_Kit_Dependencies\Google_Service_AnalyticsReporting_DateRange; +use Google\Site_Kit_Dependencies\Google_Service_AnalyticsReporting_Metric; +use Google\Site_Kit_Dependencies\Google_Service_AnalyticsReporting_OrderBy; +use Google\Site_Kit_Dependencies\Google_Service_Analytics_Accounts; +use Google\Site_Kit_Dependencies\Google_Service_Analytics_Account; +use Google\Site_Kit_Dependencies\Google_Service_Analytics_Webproperties; +use Google\Site_Kit_Dependencies\Google_Service_Analytics_Webproperty; +use Google\Site_Kit_Dependencies\Google_Service_Analytics_Profile; +use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface; use WP_Error; use Exception; @@ -706,7 +721,7 @@ protected function create_data_request( $method, $datapoint, array $data = array $dimensions = array_map( function ( $name ) { - $dimension = new \Google_Service_AnalyticsReporting_Dimension(); + $dimension = new Google_Service_AnalyticsReporting_Dimension(); $dimension->setName( $name ); return $dimension; @@ -743,7 +758,7 @@ function ( $name ) { $date_ranges = array_map( function ( $date_range ) { list ( $start_date, $end_date ) = $date_range; - $date_range = new \Google_Service_AnalyticsReporting_DateRange(); + $date_range = new Google_Service_AnalyticsReporting_DateRange(); $date_range->setStartDate( $start_date ); $date_range->setEndDate( $end_date ); @@ -762,7 +777,7 @@ function ( $metric_def ) { ), (array) $metric_def ); - $metric = new \Google_Service_AnalyticsReporting_Metric(); + $metric = new Google_Service_AnalyticsReporting_Metric(); $metric->setAlias( $metric_def['alias'] ); $metric->setExpression( $metric_def['expression'] ); @@ -784,7 +799,7 @@ function ( $order_def ) { ), (array) $order_def ); - $order_by = new \Google_Service_AnalyticsReporting_OrderBy(); + $order_by = new Google_Service_AnalyticsReporting_OrderBy(); $order_by->setFieldName( $order_def['fieldName'] ); $order_by->setSortOrder( $order_def['sortOrder'] ); @@ -795,7 +810,7 @@ function ( $order_def ) { $request->setOrderBys( $orderby ); // Batch reports requests. - $body = new \Google_Service_AnalyticsReporting_GetReportsRequest(); + $body = new Google_Service_AnalyticsReporting_GetReportsRequest(); $body->setReportRequests( array( $request ) ); return $this->get_analyticsreporting_service()->reports->batchGet( $body ); @@ -915,7 +930,7 @@ function ( $order_def ) { $client = $this->get_client(); $orig_defer = $client->shouldDefer(); $client->setDefer( false ); - $property = new \Google_Service_Analytics_Webproperty(); + $property = new Google_Service_Analytics_Webproperty(); $property->setName( wp_parse_url( $this->context->get_reference_site_url(), PHP_URL_HOST ) ); try { $property = $this->get_service( 'analytics' )->management_webproperties->insert( $data['accountID'], $property ); @@ -943,7 +958,7 @@ function ( $order_def ) { $client = $this->get_client(); $orig_defer = $client->shouldDefer(); $client->setDefer( false ); - $profile = new \Google_Service_Analytics_Profile(); + $profile = new Google_Service_Analytics_Profile(); $profile->setName( __( 'All Web Site Data', 'google-site-kit' ) ); try { $profile = $this->get_service( 'analytics' )->management_profiles->insert( $data['accountID'], $property_id, $profile ); @@ -968,7 +983,7 @@ function ( $order_def ) { $client = $this->get_client(); $orig_defer = $client->shouldDefer(); $client->setDefer( false ); - $property = new \Google_Service_Analytics_Webproperty(); + $property = new Google_Service_Analytics_Webproperty(); $property->setDefaultProfileId( $profile_id ); try { $property = $this->get_service( 'analytics' )->management_webproperties->patch( $data['accountID'], $property_id, $property ); @@ -1025,10 +1040,10 @@ protected function parse_data_response( $method, $datapoint, $response ) { // TODO: Parse this response to a regular array. break; case 'accounts-properties-profiles': - /* @var \Google_Service_Analytics_Accounts $response listManagementAccounts response. */ + /* @var Google_Service_Analytics_Accounts $response listManagementAccounts response. */ $accounts = (array) $response->getItems(); $account_ids = array_map( - function ( \Google_Service_Analytics_Account $account ) { + function ( Google_Service_Analytics_Account $account ) { return $account->getId(); }, $accounts @@ -1057,7 +1072,7 @@ function ( \Google_Service_Analytics_Account $account ) { // Iterate over each account in reverse so if there is no match, // the last $properties_profiles will be from the first account (selected by default). foreach ( array_reverse( $accounts ) as $account ) { - /* @var \Google_Service_Analytics_Account $account Analytics account object. */ + /* @var Google_Service_Analytics_Account $account Analytics account object. */ $properties_profiles = $this->get_data( 'properties-profiles', array( 'accountID' => $account->getId() ) ); if ( ! is_wp_error( $properties_profiles ) && isset( $properties_profiles['matchedProperty'] ) ) { @@ -1073,7 +1088,7 @@ function ( \Google_Service_Analytics_Account $account ) { return array_merge( compact( 'accounts' ), $properties_profiles ); case 'properties-profiles': - /* @var \Google_Service_Analytics_Webproperties $response listManagementWebproperties response. */ + /* @var Google_Service_Analytics_Webproperties $response listManagementWebproperties response. */ $properties = (array) $response->getItems(); $response = array( 'properties' => $properties, @@ -1084,7 +1099,7 @@ function ( \Google_Service_Analytics_Account $account ) { return $response; } - $found_property = new \Google_Service_Analytics_Webproperty(); + $found_property = new Google_Service_Analytics_Webproperty(); $current_url = $this->context->get_reference_site_url(); // If requested for a specific property, only match by property ID. @@ -1098,7 +1113,7 @@ function ( \Google_Service_Analytics_Account $account ) { // If there's no match for the saved account ID, try to find a match using the properties of each account. foreach ( $properties as $property ) { - /* @var \Google_Service_Analytics_Webproperty $property Property instance. */ + /* @var Google_Service_Analytics_Webproperty $property Property instance. */ if ( // Attempt to match by property ID. $property->getId() === $property_id || @@ -1169,7 +1184,7 @@ function ( \Google_Service_Analytics_Account $account ) { * @type string $page Specific page URL to filter by. Default empty string. * @type int $row_limit Limit of rows to return. Default 100. * } - * @return \Google_Service_AnalyticsReporting_ReportRequest|WP_Error Analytics site request instance. + * @return Google_Service_AnalyticsReporting_ReportRequest|WP_Error Analytics site request instance. */ protected function create_analytics_site_data_request( array $args = array() ) { $args = wp_parse_args( @@ -1188,7 +1203,7 @@ protected function create_analytics_site_data_request( array $args = array() ) { return $profile_id; } - $request = new \Google_Service_AnalyticsReporting_ReportRequest(); + $request = new Google_Service_AnalyticsReporting_ReportRequest(); $request->setViewId( $profile_id ); if ( ! empty( $args['dimensions'] ) ) { @@ -1196,19 +1211,19 @@ protected function create_analytics_site_data_request( array $args = array() ) { } if ( ! empty( $args['start_date'] ) && ! empty( $args['end_date'] ) ) { - $date_range = new \Google_Service_AnalyticsReporting_DateRange(); + $date_range = new Google_Service_AnalyticsReporting_DateRange(); $date_range->setStartDate( $args['start_date'] ); $date_range->setEndDate( $args['end_date'] ); $request->setDateRanges( array( $date_range ) ); } if ( ! empty( $args['page'] ) ) { - $dimension_filter = new \Google_Service_AnalyticsReporting_DimensionFilter(); + $dimension_filter = new Google_Service_AnalyticsReporting_DimensionFilter(); $dimension_filter->setDimensionName( 'ga:pagePath' ); $dimension_filter->setOperator( 'EXACT' ); $args['page'] = str_replace( trim( $this->context->get_reference_site_url(), '/' ), '', $args['page'] ); $dimension_filter->setExpressions( array( $args['page'] ) ); - $dimension_filter_clause = new \Google_Service_AnalyticsReporting_DimensionFilterClause(); + $dimension_filter_clause = new Google_Service_AnalyticsReporting_DimensionFilterClause(); $dimension_filter_clause->setFilters( array( $dimension_filter ) ); $request->setDimensionFilterClauses( array( $dimension_filter_clause ) ); } @@ -1243,7 +1258,7 @@ protected function setup_info() { /** * Gets the configured Analytics Reporting service object instance. * - * @return \Google_Service_AnalyticsReporting The Analytics Reporting API service. + * @return Google_Service_AnalyticsReporting The Analytics Reporting API service. */ private function get_analyticsreporting_service() { return $this->get_service( 'analyticsreporting' ); @@ -1263,8 +1278,8 @@ private function get_analyticsreporting_service() { */ protected function setup_services( Google_Client $client ) { return array( - 'analytics' => new \Google_Service_Analytics( $client ), - 'analyticsreporting' => new \Google_Service_AnalyticsReporting( $client ), + 'analytics' => new Google_Service_Analytics( $client ), + 'analyticsreporting' => new Google_Service_AnalyticsReporting( $client ), ); } @@ -1314,7 +1329,7 @@ function( $property ) use ( $property_id ) { /** * Determines whether the given metrics are for an adsense request and sets the temporary state if found. * - * @param \Google_Service_AnalyticsReporting_Metric[] $metrics Array of metrics objects. + * @param Google_Service_AnalyticsReporting_Metric[] $metrics Array of metrics objects. */ private function detect_adsense_request_from_metrics( array $metrics ) { foreach ( $metrics as $metric ) { diff --git a/includes/Modules/Optimize.php b/includes/Modules/Optimize.php index 4b6b8043857..dcdd4a3c4ce 100644 --- a/includes/Modules/Optimize.php +++ b/includes/Modules/Optimize.php @@ -11,8 +11,8 @@ namespace Google\Site_Kit\Modules; use Google\Site_Kit\Core\Modules\Module; -use Google_Client; -use Psr\Http\Message\RequestInterface; +use Google\Site_Kit_Dependencies\Google_Client; +use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface; use WP_Error; /** diff --git a/includes/Modules/PageSpeed_Insights.php b/includes/Modules/PageSpeed_Insights.php index 5013a953919..37f2c54ef90 100644 --- a/includes/Modules/PageSpeed_Insights.php +++ b/includes/Modules/PageSpeed_Insights.php @@ -13,8 +13,9 @@ use Google\Site_Kit\Core\Modules\Module; use Google\Site_Kit\Core\Modules\Module_With_Scopes; use Google\Site_Kit\Core\Modules\Module_With_Scopes_Trait; -use Google_Client; -use Psr\Http\Message\RequestInterface; +use Google\Site_Kit_Dependencies\Google_Client; +use Google\Site_Kit_Dependencies\Google_Service_Pagespeedonline; +use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface; use WP_Error; /** @@ -177,7 +178,7 @@ protected function setup_info() { */ protected function setup_services( Google_Client $client ) { return array( - 'pagespeedonline' => new \Google_Service_Pagespeedonline( $client ), + 'pagespeedonline' => new Google_Service_Pagespeedonline( $client ), ); } diff --git a/includes/Modules/Search_Console.php b/includes/Modules/Search_Console.php index d57eaa93a87..aa43cfc6d23 100644 --- a/includes/Modules/Search_Console.php +++ b/includes/Modules/Search_Console.php @@ -15,10 +15,16 @@ use Google\Site_Kit\Core\Modules\Module_With_Screen_Trait; use Google\Site_Kit\Core\Modules\Module_With_Scopes; use Google\Site_Kit\Core\Modules\Module_With_Scopes_Trait; -use Google_Client; -use Google_Service_Exception; -use Psr\Http\Message\ResponseInterface; -use Psr\Http\Message\RequestInterface; +use Google\Site_Kit_Dependencies\Google_Client; +use Google\Site_Kit_Dependencies\Google_Service_Exception; +use Google\Site_Kit_Dependencies\Google_Service_Webmasters; +use Google\Site_Kit_Dependencies\Google_Service_Webmasters_SitesListResponse; +use Google\Site_Kit_Dependencies\Google_Service_Webmasters_WmxSite; +use Google\Site_Kit_Dependencies\Google_Service_Webmasters_SearchAnalyticsQueryRequest; +use Google\Site_Kit_Dependencies\Google_Service_Webmasters_ApiDimensionFilter; +use Google\Site_Kit_Dependencies\Google_Service_Webmasters_ApiDimensionFilterGroup; +use Google\Site_Kit_Dependencies\Psr\Http\Message\ResponseInterface; +use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface; use WP_Error; /** @@ -245,10 +251,10 @@ protected function parse_data_response( $method, $datapoint, $response ) { if ( 'GET' === $method ) { switch ( $datapoint ) { case 'sites': - /* @var \Google_Service_Webmasters_SitesListResponse $response Response object. */ + /* @var Google_Service_Webmasters_SitesListResponse $response Response object. */ return $this->map_sites( (array) $response->getSiteEntry() ); case 'matched-sites': - /* @var \Google_Service_Webmasters_SitesListResponse $response Response object. */ + /* @var Google_Service_Webmasters_SitesListResponse $response Response object. */ $sites = $this->map_sites( (array) $response->getSiteEntry() ); $current_url = $this->context->get_reference_site_url(); $current_host = wp_parse_url( $current_url, PHP_URL_HOST ); @@ -288,13 +294,13 @@ function ( $match, array $site ) use ( $current_url ) { /** * Map Site model objects to primitives used for API responses. * - * @param \Google_Service_Webmasters_WmxSite[] $sites Site objects. + * @param array $sites Site objects. * * @return array */ private function map_sites( $sites ) { return array_map( - function ( \Google_Service_Webmasters_WmxSite $site ) { + function ( Google_Service_Webmasters_WmxSite $site ) { return array( 'siteURL' => $site->getSiteUrl(), 'permissionLevel' => $site->getPermissionLevel(), @@ -332,7 +338,7 @@ protected function create_search_analytics_data_request( array $args = array() ) ) ); - $request = new \Google_Service_Webmasters_SearchAnalyticsQueryRequest(); + $request = new Google_Service_Webmasters_SearchAnalyticsQueryRequest(); if ( ! empty( $args['dimensions'] ) ) { $request->setDimensions( (array) $args['dimensions'] ); } @@ -343,10 +349,10 @@ protected function create_search_analytics_data_request( array $args = array() ) $request->setEndDate( $args['end_date'] ); } if ( ! empty( $args['page'] ) ) { - $filter = new \Google_Service_Webmasters_ApiDimensionFilter(); + $filter = new Google_Service_Webmasters_ApiDimensionFilter(); $filter->setDimension( 'page' ); $filter->setExpression( esc_url_raw( $args['page'] ) ); - $filters = new \Google_Service_Webmasters_ApiDimensionFilterGroup(); + $filters = new Google_Service_Webmasters_ApiDimensionFilterGroup(); $filters->setFilters( array( $filter ) ); $request->setDimensionFilterGroups( array( $filters ) ); } @@ -446,7 +452,7 @@ protected function setup_info() { /** * Get the configured Webmasters service instance. * - * @return \Google_Service_Webmasters The Search Console API service. + * @return Google_Service_Webmasters The Search Console API service. */ private function get_webmasters_service() { return $this->get_service( 'webmasters' ); @@ -466,7 +472,7 @@ private function get_webmasters_service() { */ protected function setup_services( Google_Client $client ) { return array( - 'webmasters' => new \Google_Service_Webmasters( $client ), + 'webmasters' => new Google_Service_Webmasters( $client ), ); } } diff --git a/includes/Modules/Site_Verification.php b/includes/Modules/Site_Verification.php index 28591d75e13..04587fb9ea5 100644 --- a/includes/Modules/Site_Verification.php +++ b/includes/Modules/Site_Verification.php @@ -13,11 +13,16 @@ use Google\Site_Kit\Core\Modules\Module; use Google\Site_Kit\Core\Modules\Module_With_Scopes; use Google\Site_Kit\Core\Modules\Module_With_Scopes_Trait; -use Google_Client; -use Google_Service; -use Google_Service_Exception; -use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; +use Google\Site_Kit_Dependencies\Google_Client; +use Google\Site_Kit_Dependencies\Google_Service; +use Google\Site_Kit_Dependencies\Google_Service_Exception; +use Google\Site_Kit_Dependencies\Google_Service_SiteVerification; +use Google\Site_Kit_Dependencies\Google_Service_SiteVerification_SiteVerificationWebResourceGettokenRequest; +use Google\Site_Kit_Dependencies\Google_Service_SiteVerification_SiteVerificationWebResourceGettokenRequestSite; +use Google\Site_Kit_Dependencies\Google_Service_SiteVerification_SiteVerificationWebResourceResource; +use Google\Site_Kit_Dependencies\Google_Service_SiteVerification_SiteVerificationWebResourceResourceSite; +use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface; +use Google\Site_Kit_Dependencies\Psr\Http\Message\ResponseInterface; use WP_Error; use Exception; @@ -115,10 +120,10 @@ protected function create_data_request( $method, $datapoint, array $data = array } $current_url = ! empty( $data['siteURL'] ) ? $data['siteURL'] : $this->context->get_reference_site_url(); - $site = new \Google_Service_SiteVerification_SiteVerificationWebResourceGettokenRequestSite(); + $site = new Google_Service_SiteVerification_SiteVerificationWebResourceGettokenRequestSite(); $site->setIdentifier( $current_url ); $site->setType( 'SITE' ); - $request = new \Google_Service_SiteVerification_SiteVerificationWebResourceGettokenRequest(); + $request = new Google_Service_SiteVerification_SiteVerificationWebResourceGettokenRequest(); $request->setSite( $site ); $request->setVerificationMethod( 'META' ); @@ -166,10 +171,10 @@ protected function create_data_request( $method, $datapoint, array $data = array $errors = new WP_Error(); foreach ( $this->permute_site_url( $data['siteURL'] ) as $url ) { - $site = new \Google_Service_SiteVerification_SiteVerificationWebResourceResourceSite(); + $site = new Google_Service_SiteVerification_SiteVerificationWebResourceResourceSite(); $site->setType( 'SITE' ); $site->setIdentifier( $url ); - $resource = new \Google_Service_SiteVerification_SiteVerificationWebResourceResource(); + $resource = new Google_Service_SiteVerification_SiteVerificationWebResourceResource(); $resource->setSite( $site ); try { @@ -318,7 +323,7 @@ protected function setup_info() { /** * Get the configured siteverification service instance. * - * @return \Google_Service_SiteVerification The Site Verification API service. + * @return Google_Service_SiteVerification The Site Verification API service. */ private function get_siteverification_service() { return $this->get_service( 'siteverification' ); @@ -338,7 +343,7 @@ private function get_siteverification_service() { */ protected function setup_services( Google_Client $client ) { return array( - 'siteverification' => new \Google_Service_SiteVerification( $client ), + 'siteverification' => new Google_Service_SiteVerification( $client ), ); } } diff --git a/includes/Modules/TagManager.php b/includes/Modules/TagManager.php index ce8f3d4744d..94ad35e37b5 100644 --- a/includes/Modules/TagManager.php +++ b/includes/Modules/TagManager.php @@ -13,9 +13,11 @@ use Google\Site_Kit\Core\Modules\Module; use Google\Site_Kit\Core\Modules\Module_With_Scopes; use Google\Site_Kit\Core\Modules\Module_With_Scopes_Trait; -use Google_Client; -use Google_Service_Exception; -use Psr\Http\Message\RequestInterface; +use Google\Site_Kit_Dependencies\Google_Client; +use Google\Site_Kit_Dependencies\Google_Service_Exception; +use Google\Site_Kit_Dependencies\Google_Service_TagManager; +use Google\Site_Kit_Dependencies\Google_Service_TagManager_Container; +use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface; use WP_Error; use Exception; @@ -487,7 +489,7 @@ protected function create_container( $account_id ) { $client->setDefer( false ); - $container = new \Google_Service_TagManager_Container(); + $container = new Google_Service_TagManager_Container(); $container->setName( remove_accents( get_bloginfo( 'name' ) ) ); $container->setUsageContext( array( 'web' ) ); @@ -606,7 +608,7 @@ protected function setup_info() { */ protected function setup_services( Google_Client $client ) { return array( - 'tagmanager' => new \Google_Service_TagManager( $client ), + 'tagmanager' => new Google_Service_TagManager( $client ), ); } } diff --git a/includes/loader.php b/includes/loader.php index 49354c94d44..9145fd27c2a 100644 --- a/includes/loader.php +++ b/includes/loader.php @@ -14,8 +14,28 @@ define( 'GOOGLESITEKIT_PLUGIN_BASENAME', plugin_basename( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) ); define( 'GOOGLESITEKIT_PLUGIN_DIR_PATH', plugin_dir_path( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) ); -// Autoload vendor files. -require_once GOOGLESITEKIT_PLUGIN_DIR_PATH . 'vendor/autoload.php'; +// Autoload files. +require_once GOOGLESITEKIT_PLUGIN_DIR_PATH . 'includes/vendor/autoload.php'; +require_once GOOGLESITEKIT_PLUGIN_DIR_PATH . 'third-party/vendor/autoload.php'; + +/** + * Loads vendor files containing functions etc. + * + * This integrates with the dependency prefixing script. Its autoloader loads all classes, but not the other files. + * + * @since 1.0.0 + * @access private + */ +function autoload_vendor_files() { + $files = require GOOGLESITEKIT_PLUGIN_DIR_PATH . 'third-party/vendor/composer/autoload_files.php'; + foreach ( $files as $file_identifier => $file ) { + $file = str_replace( 'third-party/vendor', 'third-party', $file ); + if ( file_exists( $file ) ) { + require_once $file; + } + } +} +autoload_vendor_files(); // Initialize the plugin. Plugin::load( GOOGLESITEKIT_PLUGIN_MAIN_FILE ); diff --git a/phpcs.xml b/phpcs.xml index 1ec430f971f..09ecf0353a8 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -31,6 +31,7 @@ */bower-components/* */node_modules/* + */third-party/* */vendor/* diff --git a/scoper.inc.php b/scoper.inc.php new file mode 100644 index 00000000000..3fea1298ea5 --- /dev/null +++ b/scoper.inc.php @@ -0,0 +1,103 @@ + 'Google\\Site_Kit_Dependencies', + 'finders' => array( + + // General dependencies, except Google API services. + Finder::create() + ->files() + ->ignoreVCS( true ) + ->notName( '/LICENSE|.*\\.md|.*\\.dist|Makefile|composer\\.(json|lock)/' ) + ->exclude( + array( + 'doc', + 'test', + 'test_old', + 'tests', + 'Tests', + 'vendor-bin', + ) + ) + ->path( '#^firebase/#' ) + ->path( '#^google/apiclient/#' ) + ->path( '#^google/auth/#' ) + ->path( '#^guzzlehttp/#' ) + ->path( '#^monolog/#' ) + ->path( '#^psr/#' ) + ->path( '#^ralouphie/#' ) + ->path( '#^react/#' ) + ->in( 'vendor' ), + + // Google API service infrastructure classes. + Finder::create() + ->files() + ->ignoreVCS( true ) + ->notName( '/LICENSE|.*\\.md|.*\\.dist|Makefile|composer\\.json|composer\\.lock/' ) + ->exclude( + array( + 'doc', + 'test', + 'test_old', + 'tests', + 'Tests', + 'vendor-bin', + ) + ) + ->path( "#^google/apiclient-services/src/Google/Service/($google_services)/#" ) + ->in( 'vendor' ), + + // Google API service entry classes. + Finder::create() + ->files() + ->ignoreVCS( true ) + ->name( "#($google_services)\.php#" ) + ->in( 'vendor/google/apiclient-services/src/Google/Service' ), + ), + 'files-whitelist' => array( + + // This dependency is a global function which should remain global. + 'vendor/ralouphie/getallheaders/src/getallheaders.php', + ), + 'patchers' => array( + function( $file_path, $prefix, $contents ) { + if ( preg_match( '#google/apiclient/src/Google/Http/REST\.php$#', $file_path ) ) { + $contents = str_replace( "\\$prefix\\intVal", '\\intval', $contents ); + } + if ( false !== strpos( $file_path, 'vendor/google/apiclient-services/' ) ) { + $contents = str_replace( "'Google_Service_", "'" . $prefix . '\Google_Service_', $contents ); + $contents = str_replace( '"Google_Service_', '"' . $prefix . '\Google_Service_', $contents ); + } + return $contents; + }, + ), + 'whitelist' => array(), + 'whitelist-global-constants' => false, + 'whitelist-global-classes' => false, + 'whitelist-global-functions' => false, +); diff --git a/tests/phpunit/includes/Core/Modules/FakeModule.php b/tests/phpunit/includes/Core/Modules/FakeModule.php index f5df3da4bf8..064167b3a59 100644 --- a/tests/phpunit/includes/Core/Modules/FakeModule.php +++ b/tests/phpunit/includes/Core/Modules/FakeModule.php @@ -11,7 +11,7 @@ namespace Google\Site_Kit\Tests\Core\Modules; use Google\Site_Kit\Core\Modules\Module; -use Google_Client; +use Google\Site_Kit_Dependencies\Google_Client; use Psr\Http\Message\RequestInterface; use WP_Error; diff --git a/tests/phpunit/integration/Core/Authentication/AuthenticationTest.php b/tests/phpunit/integration/Core/Authentication/AuthenticationTest.php index 700e6ad4dba..d218a01bdb5 100644 --- a/tests/phpunit/integration/Core/Authentication/AuthenticationTest.php +++ b/tests/phpunit/integration/Core/Authentication/AuthenticationTest.php @@ -148,7 +148,7 @@ public function test_register_wp_login() { $this->assertTrue( $auth->is_authenticated() ); // Set a refresh token and expect it to be passed to the Google Client. $client->set_refresh_token( 'test-refresh-token' ); - $mock_google_client = $this->getMock( 'Google_Client', array( + $mock_google_client = $this->getMock( 'Google\Site_Kit_Dependencies\Google_Client', array( 'fetchAccessTokenWithRefreshToken', 'revokeToken' ) ); @@ -246,7 +246,7 @@ public function test_disconnect() { $user_options->set( $key, "test-$key-value" ); } - $mock_google_client = $this->getMock( 'Google_Client', array( 'revokeToken' ) ); + $mock_google_client = $this->getMock( 'Google\Site_Kit_Dependencies\Google_Client', array( 'revokeToken' ) ); $mock_google_client->expects( $this->once() )->method( 'revokeToken' ); $this->force_set_property( $auth->get_oauth_client(), 'google_client', $mock_google_client ); diff --git a/tests/phpunit/integration/Core/Authentication/Clients/OAuth_ClientTest.php b/tests/phpunit/integration/Core/Authentication/Clients/OAuth_ClientTest.php index 5a8a0f37947..ec5f2b50f2d 100644 --- a/tests/phpunit/integration/Core/Authentication/Clients/OAuth_ClientTest.php +++ b/tests/phpunit/integration/Core/Authentication/Clients/OAuth_ClientTest.php @@ -26,7 +26,7 @@ class OAuth_ClientTest extends TestCase { public function test_get_client() { $client = new OAuth_Client( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) ); - $this->assertInstanceOf( 'Google_Client', $client->get_client() ); + $this->assertInstanceOf( 'Google\Site_Kit_Dependencies\Google_Client', $client->get_client() ); } public function test_refresh_token() { @@ -236,7 +236,7 @@ public function test_authorize_user() { $success_redirect = admin_url( 'success-redirect' ); $client->get_authentication_url( $success_redirect ); // No other way around this but to mock the Google_Client - $google_client_mock = $this->getMock( 'Google_Client', array( 'fetchAccessTokenWithAuthCode' ) ); + $google_client_mock = $this->getMock( 'Google\Site_Kit_Dependencies\Google_Client', array( 'fetchAccessTokenWithAuthCode' ) ); $google_client_mock->method( 'fetchAccessTokenWithAuthCode' )->willReturn( array( 'access_token' => 'test-access-token' ) ); $this->force_set_property( $client, 'google_client', $google_client_mock );