BaseProvider.php
104 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Base Provider |
| 5 | */ |
| 6 | |
| 7 | namespace SmashBalloon\Reviews\Common\Integrations\Providers; |
| 8 | |
| 9 | if (! defined('ABSPATH')) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | use SmashBalloon\Reviews\Common\Exceptions\RelayResponseException; |
| 14 | use SmashBalloon\Reviews\Common\Integrations\SBRelay; |
| 15 | use SmashBalloon\Reviews\Common\Utils\AjaxUtil; |
| 16 | use Smashballoon\Stubs\Services\ServiceProvider; |
| 17 | |
| 18 | abstract class BaseProvider extends ServiceProvider |
| 19 | { |
| 20 | protected $name; |
| 21 | protected $endpoint; |
| 22 | protected $sources_endpoint; |
| 23 | protected $sources_remove; |
| 24 | private $relay; |
| 25 | protected $uses_connect = false; |
| 26 | protected $friendly_name; |
| 27 | |
| 28 | public function __construct(SBRelay $relay) |
| 29 | { |
| 30 | $this->relay = $relay; |
| 31 | $this->endpoint = 'reviews/' . $this->name; |
| 32 | $this->sources_endpoint = 'sources/' . $this->name; |
| 33 | $this->sources_remove = 'source/remove'; |
| 34 | } |
| 35 | |
| 36 | public function register() |
| 37 | { |
| 38 | add_action('rest_api_init', [$this, 'registerRestEndpoint']); |
| 39 | add_filter('sbr_supported_providers', [$this, 'registerProvider']); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @throws RelayResponseException |
| 44 | */ |
| 45 | public function getAllReviews($args = []): void |
| 46 | { |
| 47 | try { |
| 48 | $response = $this->relay->call($this->endpoint, $args->get_params(), 'GET', true); |
| 49 | wp_send_json($response); |
| 50 | } catch (RelayResponseException $exception) { |
| 51 | AjaxUtil::send_json_error($exception->getMessage(), $exception->getCode()); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @throws RelayResponseException |
| 57 | */ |
| 58 | public function getSourcesInfo($args = []) |
| 59 | { |
| 60 | $response = $this->relay->call($this->sources_endpoint, $args, 'GET', true); |
| 61 | if (isset($response['success']) && false === $response['success']) { |
| 62 | return wp_json_encode($response); |
| 63 | } |
| 64 | |
| 65 | if (isset($response['data']) && $response['data']) { |
| 66 | return wp_json_encode($response['data']); |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | public function registerRestEndpoint() |
| 72 | { |
| 73 | register_rest_route(SBR_REST_DOMAIN, '/' . $this->endpoint, array( |
| 74 | 'methods' => 'GET', |
| 75 | 'callback' => [$this, 'getAllReviews'], |
| 76 | 'permission_callback' => '__return_true' |
| 77 | )); |
| 78 | } |
| 79 | |
| 80 | public function registerProvider($providers) |
| 81 | { |
| 82 | $providers[] = [ |
| 83 | 'name' => $this->name, |
| 84 | 'endpoint' => get_rest_url(null, SBR_REST_DOMAIN . '/' . $this->endpoint), |
| 85 | 'uses_connect' => $this->uses_connect, |
| 86 | 'friendly_name' => $this->friendly_name |
| 87 | ]; |
| 88 | |
| 89 | return $providers; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @throws RelayResponseException |
| 94 | */ |
| 95 | public function removeSource($args = []) |
| 96 | { |
| 97 | $response = $this->relay->call($this->sources_remove, $args, 'POST', true); |
| 98 | if (isset($response)) { |
| 99 | return wp_json_encode($response); |
| 100 | } |
| 101 | return false; |
| 102 | } |
| 103 | } |
| 104 |