diff --git a/includes/classes/SiteAutomation/Auth.php b/includes/classes/SiteAutomation/Auth.php deleted file mode 100644 index 3a2b55a6..00000000 --- a/includes/classes/SiteAutomation/Auth.php +++ /dev/null @@ -1,135 +0,0 @@ -environment = get_sophi_settings( 'environment' ); - } - - /** - * Get cached access_token. - * - * @return string|\WP_Error - */ - public function get_access_token() { - $access_token = get_transient( 'sophi_site_automation_access_token' ); - - if ( $access_token ) { - return $access_token; - } - - return $this->refresh_access_token(); - } - - /** - * Refresh the access_token and save it to the database. - * - * @return string|\WP_Error - */ - public function refresh_access_token() { - $response = $this->request_access_token( - get_sophi_settings( 'client_id' ), - get_sophi_settings( 'client_secret' ) - ); - - if ( is_wp_error( $response ) ) { - return $response; - } - - set_transient( 'sophi_site_automation_access_token', $response['access_token'], $response['expires_in'] ); - - return $response['access_token']; - } - - /** - * Request a new access token. - * - * @param string $client_id Client ID. - * @param string $client_secret Client Secret. - * - * @return array|\WP_Error - */ - public function request_access_token( $client_id, $client_secret ) { - $body = [ - 'client_id' => $client_id, - 'client_secret' => $client_secret, - 'audience' => $this->get_audience(), - 'grant_type' => 'client_credentials', - ]; - $args = [ - 'headers' => [ 'Content-Type' => 'application/json' ], - 'body' => wp_json_encode( $body ), - ]; - - $auth_url = $this->get_auth_url(); - - /** This filter is documented in includes/classes/SiteAutomation/Request.php */ - $args = apply_filters( 'sophi_request_args', $args, $auth_url ); - - $result = wp_remote_post( $auth_url, $args ); - - /** This filter is documented in includes/classes/SiteAutomation/Request.php */ - $result = apply_filters( 'sophi_request_result', $result, $args, $auth_url ); - - if ( is_wp_error( $result ) ) { - return $result; - } - - if ( 401 === wp_remote_retrieve_response_code( $result ) ) { - return new \WP_Error( 401, __( 'Invalid credentials! Please confirm your client ID and secret then try again.', 'sophi-wp' ) ); - } - - if ( 200 !== wp_remote_retrieve_response_code( $result ) ) { - return new \WP_Error( $result['response']['code'], $result['response']['message'] ); - } - - $response = wp_remote_retrieve_body( $result ); - - return json_decode( $response, true ); - } - - /** - * Set the environment to be used. - * - * @since 1.0.8 - * @param string $environment Could be 'prod', 'stg' or 'dev' - * @return void - */ - public function set_environment( $environment ) { - $this->environment = $environment; - } - - /** - * Get the API URL to get access_token - * - * @since 1.0.8 - * @return string - */ - protected function get_auth_url() { - return 'prod' === $this->environment ? 'https://sophi-prod.auth0.com/oauth/token' : 'https://sophi-works.auth0.com/oauth/token'; - } - - /** - * Get the audience parameter to get access_token - * - * @since 1.0.8 - * @return string - */ - protected function get_audience() { - return 'prod' === $this->environment ? 'https://api.sophi.io' : 'https://api.sophi.works'; - } -} diff --git a/includes/classes/SiteAutomation/Request.php b/includes/classes/SiteAutomation/Request.php index df86cff0..9876efcd 100644 --- a/includes/classes/SiteAutomation/Request.php +++ b/includes/classes/SiteAutomation/Request.php @@ -14,13 +14,6 @@ */ class Request { - /** - * Auth object which manages access_token. - * - * @var Auth $auth - */ - private $auth; - /** * Site Automation API URL. * @@ -52,11 +45,8 @@ class Request { /** * Class constructor. * - * @param Auth $auth Authentication object. */ - public function __construct( $auth ) { - $this->auth = $auth; - + public function __construct() { add_action( 'sophi_retry_get_curated_data', [ $this, 'do_cron' ], @@ -213,16 +203,11 @@ private function set_status( $data ) { * @return mixed WP_Error on failure or body request on success. */ private function request( $timeout ) { - $access_token = $this->auth->get_access_token(); - - if ( is_wp_error( $access_token ) ) { - return $access_token; - } $args = [ 'headers' => [ 'Content-Type' => 'application/json', - 'Authorization' => 'Bearer ' . $access_token, + 'cache-control' => 'no-cache', ], ]; @@ -309,13 +294,29 @@ private function process( $response, $bypass_cache, $post_id ) { * * @return string */ - private function set_api_url( $page, $widget ) { + private function set_api_url( $page = '', $widget = '' ) { $site_automation_url = get_sophi_settings( 'site_automation_url' ); $site_automation_url = untrailingslashit( $site_automation_url ); + $host = get_sophi_settings( 'host' ); + $tenant_id = get_sophi_settings( 'tenant_id' ); + + $url = sprintf( + '%1$s/%2$s/hosts/%3$s/pages/%4$s', + $site_automation_url, + $tenant_id, + $host, + $page + ); - $host = wp_parse_url( get_home_url(), PHP_URL_HOST ); + if ( ! empty ( $widget ) ) { + $url = sprintf( + '%1$s/widgets/%2$s', + $url, + $widget + ); + } - return sprintf( '%1$s/curatedHosts/%2$s/curator?page=%3$s&widget=%4$s', $site_automation_url, $host, $page, $widget ); + return $url . '.json'; } /** diff --git a/includes/classes/SiteAutomation/Services.php b/includes/classes/SiteAutomation/Services.php index ea5f2ba2..6fd4c4c1 100644 --- a/includes/classes/SiteAutomation/Services.php +++ b/includes/classes/SiteAutomation/Services.php @@ -22,8 +22,7 @@ class Services { * Register services needs for Site Automation. */ public function register() { - $this->auth = new Auth(); - $this->request = new Request( $this->auth ); + $this->request = new Request(); $this->integration = new Integration( $this->request ); } diff --git a/includes/functions/settings.php b/includes/functions/settings.php index 79b6c9be..e66f5eaa 100644 --- a/includes/functions/settings.php +++ b/includes/functions/settings.php @@ -7,7 +7,6 @@ namespace SophiWP\Settings; -use SophiWP\SiteAutomation\Auth; use function SophiWP\Utils\get_domain; use function SophiWP\Utils\is_configured; @@ -156,24 +155,24 @@ function fields_setup() { ); add_settings_field( - 'client_id', - __( 'Client ID', 'sophi-wp' ), + 'host', + __( 'Host', 'sophi-wp' ), __NAMESPACE__ . '\render_input', SETTINGS_GROUP, 'sophi_api', [ - 'label_for' => 'client_id', + 'label_for' => 'host', ] ); add_settings_field( - 'client_secret', - __( 'Client Secret', 'sophi-wp' ), + 'tenant_id', + __( 'Tenant ID', 'sophi-wp' ), __NAMESPACE__ . '\render_input', SETTINGS_GROUP, 'sophi_api', [ - 'label_for' => 'client_secret', + 'label_for' => 'tenant_id', ] ); @@ -232,8 +231,6 @@ function get_default_settings( $key = '' ) { 'environment' => $default_environment, 'collector_url' => 'collector.sophi.io', 'tracker_client_id' => get_domain(), - 'client_id' => '', - 'client_secret' => '', 'site_automation_url' => '', 'query_integration' => 1, ]; @@ -261,30 +258,6 @@ function sanitize_settings( $settings ) { $prev_settings = get_option( SETTINGS_GROUP ); - // Delete the option that holds the access token when the environment is changed - if ( ! empty( $prev_settings['environment'] ) && ! empty( $settings['environment'] ) && $prev_settings['environment'] !== $settings['environment'] ) { - delete_option( 'sophi_site_automation_access_token' ); - } - - if ( ! empty( $settings['client_id'] && ! empty( $settings['client_secret'] ) ) ) { - $auth = new Auth(); - $auth->set_environment( $settings['environment'] ); - $response = $auth->request_access_token( $settings['client_id'], $settings['client_secret'] ); - if ( is_wp_error( $response ) ) { - add_settings_error( - SETTINGS_GROUP, - SETTINGS_GROUP, - $response->get_error_message() - ); - } - } else { - add_settings_error( - SETTINGS_GROUP, - SETTINGS_GROUP, - __( 'Both Client ID and Client Secret are required for Site Automation integration.', 'sophi-wp' ) - ); - } - if ( empty( $settings['site_automation_url']) ) { add_settings_error( SETTINGS_GROUP,