-
Notifications
You must be signed in to change notification settings - Fork 904
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
319 additions
and
9 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/dashboard/application/sessions/organic-sessions-data-provider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Yoast\WP\SEO\Dashboard\Application\Sessions; | ||
Check warning on line 3 in src/dashboard/application/sessions/organic-sessions-data-provider.php GitHub Actions / Check code style
|
||
|
||
use Yoast\WP\SEO\Dashboard\Domain\Data_Provider\Data_Container; | ||
use Yoast\WP\SEO\Dashboard\Domain\Data_Provider\Data_Provider_Interface; | ||
use Yoast\WP\SEO\Dashboard\Domain\Data_Provider\Parameters; | ||
use Yoast\WP\SEO\Dashboard\Infrastructure\Google_Analytics\Site_Kit_Google_Analytics_Adapter; | ||
use Yoast\WP\SEO\Dashboard\Infrastructure\Search_Rankings\Organic_Sessions_Parser; | ||
|
||
class Organic_Sessions_Data_Provider implements Data_Provider_Interface { | ||
Check failure on line 11 in src/dashboard/application/sessions/organic-sessions-data-provider.php GitHub Actions / Check code style
|
||
|
||
/** | ||
* The adapter. | ||
* | ||
* @var Site_Kit_Google_Analytics_Adapter $site_kit_google_analytics_adapter | ||
*/ | ||
private $site_kit_google_analytics_adapter; | ||
|
||
/** | ||
* The parser. | ||
* @var Organic_Sessions_Parser $organic_sessions_parser | ||
*/ | ||
private $organic_sessions_parser; | ||
|
||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param Site_Kit_Google_Analytics_Adapter $site_kit_google_analytics_adapter The adapter. | ||
* @param Organic_Sessions_Parser $organic_sessions_parser The parser. | ||
*/ | ||
public function __construct( Site_Kit_Google_Analytics_Adapter $site_kit_google_analytics_adapter,Organic_Sessions_Parser $organic_sessions_parser ) { | ||
Check failure on line 33 in src/dashboard/application/sessions/organic-sessions-data-provider.php GitHub Actions / Check code style
|
||
$this->site_kit_google_analytics_adapter = $site_kit_google_analytics_adapter; | ||
$this->organic_sessions_parser = $organic_sessions_parser; | ||
} | ||
|
||
/** | ||
* Method to get search related data from a provider. | ||
* | ||
* @param Parameters $parameters The parameter to get the search data for. | ||
* | ||
* @return Data_Container | ||
*/ | ||
public function get_data( Parameters $parameters ): Data_Container { | ||
|
||
$results = $this->site_kit_google_analytics_adapter->get_data( $parameters ); | ||
return $this->organic_sessions_parser->parse( $results ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/dashboard/infrastructure/google-analytics/google-analytics-parameters.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. | ||
namespace Yoast\WP\SEO\Dashboard\Infrastructure\Google_Analytics; | ||
|
||
use Yoast\WP\SEO\Dashboard\Domain\Data_Provider\Parameters; | ||
|
||
/** | ||
* Domain object to add search console specific data to the parameters. | ||
*/ | ||
class Google_Analytics_Parameters extends Parameters { | ||
|
||
/** | ||
* The search dimension_filters to query. | ||
* | ||
* @var string[] $dimension_filters | ||
*/ | ||
private $dimension_filters; | ||
/** | ||
* The search metrics to query. | ||
* | ||
* @var string[] $metrics | ||
*/ | ||
private $metrics; | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param string[] $dimension_filters The search dimensionFilters to query. | ||
* @param string[] $metrics The search metrics to query. | ||
*/ | ||
public function __construct( array $dimension_filters,array $metrics ) { | ||
$this->dimension_filters = $dimension_filters; | ||
$this->metrics = $metrics; | ||
} | ||
|
||
/** | ||
* Getter for the dimension_filters. | ||
* | ||
* @return string[] | ||
*/ | ||
public function get_dimension_filters(): array { | ||
return $this->dimension_filters; | ||
} | ||
|
||
/** | ||
* Getter for the metrics parameters. | ||
* | ||
* @return string[] | ||
*/ | ||
public function get_metrics(): array { | ||
return $this->metrics; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/dashboard/infrastructure/google-analytics/site-kit-google-analytics-adapter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong | ||
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded | ||
namespace Yoast\WP\SEO\Dashboard\Infrastructure\Google_Analytics; | ||
|
||
use Google\Site_Kit\Core\Modules\Module; | ||
use Google\Site_Kit\Core\Modules\Modules; | ||
use Google\Site_Kit\Modules\Search_Console; | ||
use Google\Site_Kit\Plugin; | ||
use Google\Site_Kit_Dependencies\Google\Service\SearchConsole\ApiDataRow; | ||
use WP_Error; | ||
|
||
/** | ||
* The site API adapter to make calls via the Site_Kit plugin. | ||
*/ | ||
class Site_Kit_Google_Analytics_Adapter { | ||
|
||
/** | ||
* The search console module class from Site kit. | ||
* | ||
* @var Module | ||
*/ | ||
private static $search_console_module; | ||
|
||
/** | ||
* The register method that sets the instance in the adapter. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() { | ||
if ( \class_exists( 'Google\Site_Kit\Plugin' ) ) { | ||
$site_kit_plugin = Plugin::instance(); | ||
$modules = new Modules( $site_kit_plugin->context() ); | ||
self::$search_console_module = $modules->get_module( Search_Console::MODULE_SLUG ); | ||
} | ||
} | ||
|
||
/** | ||
* The wrapper method to add our parameters to a Site Kit API request. | ||
* | ||
* @param Google_Analytics_Parameters $parameters The parameters. | ||
* | ||
* @return ApiDataRow[]|WP_Error Data on success, or WP_Error on failure. | ||
*/ | ||
public function get_data( Google_Analytics_Parameters $parameters ) { | ||
$api_parameters = [ | ||
'slug' => "analytics-4", | ||
'datapoint' => 'report', | ||
'startDate' => $parameters->get_start_date(), | ||
'endDate' => $parameters->get_end_date(), | ||
'limit' => $parameters->get_limit(), | ||
'dimensionFilters' => $parameters->get_dimension_filters(), | ||
'metrics' => [ $parameters->get_metrics() ], | ||
]; | ||
|
||
return self::$search_console_module->get_data( 'report', $api_parameters ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/dashboard/infrastructure/sessions/organic-sessions-parser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong | ||
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded | ||
namespace Yoast\WP\SEO\Dashboard\Infrastructure\Search_Rankings; | ||
|
||
use Google\Site_Kit_Dependencies\Google\Service\SearchConsole\ApiDataRow; | ||
use Yoast\WP\SEO\Dashboard\Domain\Data_Provider\Data_Container; | ||
use Yoast\WP\SEO\Dashboard\Domain\Search_Rankings\Search_Data; | ||
|
||
/** | ||
* This class contains all logic to parse the raw API response to usable domain objects for the rest of the system. | ||
*/ | ||
class Organic_Sessions_Parser { | ||
|
||
/** | ||
* Parses the raw API response to a Search Data object containing the API response and SEO score. | ||
* | ||
* @param ApiDataRow[] $results The raw results. | ||
* | ||
* @return Data_Container The parsed data. | ||
*/ | ||
public function parse( array $results ): Data_Container { | ||
$search_data_container = new Data_Container(); | ||
foreach ( $results as $ranking ) { | ||
$search_data_container->add_data( new Search_Data( $ranking->clicks, $ranking->ctr, $ranking->impressions, $ranking->position, $ranking->keys[0] ) ); | ||
} | ||
|
||
return $search_data_container; | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
src/dashboard/user-interface/sessions/organic-sessions-route.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. | ||
namespace Yoast\WP\SEO\Dashboard\User_Interface\Search_Rankings; | ||
|
||
use Exception; | ||
use WP_REST_Request; | ||
use WP_REST_Response; | ||
use WPSEO_Capability_Utils; | ||
use Yoast\WP\SEO\Conditionals\No_Conditionals; | ||
use Yoast\WP\SEO\Dashboard\Application\Sessions\Organic_Sessions_Data_Provider; | ||
use Yoast\WP\SEO\Dashboard\Infrastructure\Google_Analytics\Google_Analytics_Parameters; | ||
use Yoast\WP\SEO\Main; | ||
use Yoast\WP\SEO\Routes\Route_Interface; | ||
|
||
/** | ||
* organic sessions route. | ||
*/ | ||
class Organic_Sessions_Route implements Route_Interface { | ||
|
||
use No_Conditionals; | ||
|
||
/** | ||
* The namespace of the rout. | ||
* | ||
* @var string | ||
*/ | ||
public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE; | ||
/** | ||
* The prefix of the rout. | ||
* | ||
* @var string | ||
*/ | ||
public const ROUTE_PREFIX = '/organic_sessions'; | ||
|
||
/** | ||
* The data provider. | ||
* | ||
* @var Organic_Sessions_Data_Provider $organic_sessions_data_provider | ||
*/ | ||
private $organic_sessions_data_provider; | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param Organic_Sessions_Data_Provider $organic_sessions_data_provider The data provider. | ||
*/ | ||
public function __construct( Organic_Sessions_Data_Provider $organic_sessions_data_provider ) { | ||
$this->organic_sessions_data_provider = $organic_sessions_data_provider; | ||
} | ||
|
||
/** | ||
* Registers routes for scores. | ||
* | ||
* @return void | ||
*/ | ||
public function register_routes() { | ||
\register_rest_route( | ||
self::ROUTE_NAMESPACE, | ||
self::ROUTE_PREFIX, | ||
[ | ||
[ | ||
'methods' => 'GET', | ||
'callback' => [ $this, 'get_sessions' ], | ||
//'permission_callback' => [ $this, 'permission_manage_options' ], | ||
'args' => [ | ||
'limit' => [ | ||
'required' => true, | ||
'type' => 'int', | ||
'sanitize_callback' => 'sanitize_text_field', | ||
'default' => 5, | ||
], | ||
|
||
], | ||
], | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Gets the rankings of a specific amount of pages. | ||
* | ||
* @param WP_REST_Request $request The request object. | ||
* | ||
* @return WP_REST_Response The success or failure response. | ||
*/ | ||
public function get_sessions( WP_REST_Request $request ): WP_REST_Response { | ||
try { | ||
$request_parameters = new Google_Analytics_Parameters( ["sessionDefaultChannelGrouping" => "Organic Search"], [ | ||
'name' => 'sessions', | ||
]); | ||
|
||
$request_parameters->set_limit( $request->get_param( 'limit' ) ); | ||
$request_parameters->set_start_date( '2024-01-01' ); | ||
$request_parameters->set_end_date( '2025-01-01' ); | ||
|
||
$search_data_container = $this->organic_sessions_data_provider->get_data( $request_parameters ); | ||
} catch ( Exception $exception ) { | ||
return new WP_REST_Response( | ||
[ | ||
'error' => $exception->getMessage(), | ||
], | ||
$exception->getCode() | ||
); | ||
} | ||
|
||
return new WP_REST_Response( | ||
$search_data_container->to_array(), | ||
200 | ||
); | ||
} | ||
|
||
/** | ||
* Permission callback. | ||
* | ||
* @return bool True when user has the 'wpseo_manage_options' capability. | ||
*/ | ||
public function permission_manage_options() { | ||
return WPSEO_Capability_Utils::current_user_can( 'wpseo_manage_options' ); | ||
} | ||
} |