| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong |
| 4 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded |
| 5 |
namespace Yoast\WP\SEO\Dashboard\Infrastructure\Connection; |
| 6 |
|
| 7 |
use Google\Site_Kit\Core\REST_API\REST_Routes; |
| 8 |
use WP_REST_Request; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class that hold the code to do the REST call to the Site Kit api. |
| 12 |
* |
| 13 |
* @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded |
| 14 |
*/ |
| 15 |
class Site_Kit_Is_Connected_Call { |
| 16 |
|
| 17 |
/** |
| 18 |
* Runs the internal REST api call. |
| 19 |
* |
| 20 |
* @return bool |
| 21 |
*/ |
| 22 |
public function is_setup_completed(): bool { |
| 23 |
if ( ! \class_exists( REST_Routes::class ) ) { |
| 24 |
return false; |
| 25 |
} |
| 26 |
|
| 27 |
$request = new WP_REST_Request( 'GET', '/' . REST_Routes::REST_ROOT . '/core/site/data/connection' ); |
| 28 |
|
| 29 |
$response = \rest_do_request( $request ); |
| 30 |
|
| 31 |
if ( $response->is_error() ) { |
| 32 |
return false; |
| 33 |
} |
| 34 |
return $response->get_data()['setupCompleted']; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Runs the internal REST api call. |
| 39 |
* |
| 40 |
* @return bool |
| 41 |
*/ |
| 42 |
public function is_ga_connected(): bool { |
| 43 |
if ( ! \class_exists( REST_Routes::class ) ) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
$request = new WP_REST_Request( 'GET', '/' . REST_Routes::REST_ROOT . '/core/modules/data/list' ); |
| 47 |
$response = \rest_do_request( $request ); |
| 48 |
|
| 49 |
if ( $response->is_error() ) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
$connected = false; |
| 53 |
foreach ( $response->get_data() as $module ) { |
| 54 |
if ( $module['slug'] === 'analytics-4' ) { |
| 55 |
$connected = $module['connected']; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return $connected; |
| 60 |
} |
| 61 |
} |
| 62 |
|