namespace, '/delivery', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_delivery' ), 'permission_callback' => array( $this, 'check_permission' ), ) ); register_rest_route( $this->namespace, '/delivery/postshiba', array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'connect' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => array( 'api_key' => array( 'type' => 'string', 'default' => '', ), 'team_id' => array( 'type' => 'string', 'default' => '', ), 'cluster_id' => array( 'type' => 'string', 'default' => '', ), ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'disconnect' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); register_rest_route( $this->namespace, '/delivery/postshiba/refresh', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'refresh' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => array( 'cluster_id' => array( 'type' => 'string', 'default' => '', ), ), ) ); register_rest_route( $this->namespace, '/delivery/postshiba/domains/(?P[A-Za-z0-9_-]+)/verify', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'verify_domain' ), 'permission_callback' => array( $this, 'check_permission' ), ) ); register_rest_route( $this->namespace, '/delivery/warmup', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'set_warmup' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => array( 'enabled' => array( 'type' => 'boolean', 'required' => true, ), ), ) ); register_rest_route( $this->namespace, '/delivery/test', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'send_test' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => array( 'email' => array( 'type' => 'string', 'required' => true, ), ), ) ); } /** * @return WP_REST_Response */ public function get_delivery() { Mailpuff_PostShiba::maybe_refresh(); return rest_ensure_response( self::payload() ); } /** * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function connect( $request ) { $result = Mailpuff_PostShiba::connect( (string) $request->get_param( 'api_key' ), (string) $request->get_param( 'team_id' ), sanitize_text_field( (string) $request->get_param( 'cluster_id' ) ) ); return is_wp_error( $result ) ? $result : rest_ensure_response( self::payload() ); } /** * @return WP_REST_Response|WP_Error */ public function disconnect() { $result = Mailpuff_PostShiba::disconnect(); return is_wp_error( $result ) ? $result : rest_ensure_response( self::payload() ); } /** * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function refresh( $request ) { $result = Mailpuff_PostShiba::refresh( sanitize_text_field( (string) $request->get_param( 'cluster_id' ) ) ); return is_wp_error( $result ) ? $result : rest_ensure_response( self::payload() ); } /** * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function verify_domain( $request ) { $result = Mailpuff_PostShiba::verify_domain( (string) $request['id'] ); return is_wp_error( $result ) ? $result : rest_ensure_response( self::payload() ); } /** * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function set_warmup( $request ) { $result = Mailpuff_PostShiba::set_warmup( (bool) $request->get_param( 'enabled' ), Mailpuff_Mailer::get_instance()->default_from()['email'] ); return is_wp_error( $result ) ? $result : rest_ensure_response( self::payload() ); } /** * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function send_test( $request ) { $result = Mailpuff_Outbox::send_delivery_test( (string) $request->get_param( 'email' ) ); return is_wp_error( $result ) ? $result : rest_ensure_response( self::payload() ); } /** * Everything Settings, Sending draws. * * @return array */ public static function payload() { $status = Mailpuff_Mailer::get_instance()->status(); return array( 'status' => $status, 'postshiba' => self::postshiba( $status['fromEmail'] ), 'mailyard' => self::mailyard(), 'caps' => array( 'hour' => self::cap( Mailpuff_Send_Cap::window() ), 'day' => self::cap( Mailpuff_Send_Cap::day_window() ), ), ); } /** * A cap window for the screen. * * @param array $window From Mailpuff_Send_Cap. * @return array|null { cap, used, wait } or null with no cap; `wait` is * the seconds until a full window has room, else 0. */ private static function cap( array $window ) { return $window['cap'] > 0 ? array( 'cap' => (int) $window['cap'], 'used' => (int) $window['used'], 'wait' => (int) $window['wait'], ) : null; } /** * PostShiba's side: what is connected, what the From domain needs, and * where its warm-up stands (`Mailpuff_Send_Cap::warmup()`). * * @param string $from_email Default From address. * @return array */ private static function postshiba( $from_email ) { $state = Mailpuff_PostShiba::state(); $hook = (array) $state['webhook']; $block = Mailpuff_PostShiba::blocked(); $domains = array(); foreach ( (array) $state['domains'] as $row ) { $domains[] = array( 'id' => (string) ( $row['id'] ?? '' ), 'name' => (string) ( $row['name'] ?? '' ), 'sending_allowed' => ! empty( $row['sending_allowed'] ), ); } return array( 'connected' => Mailpuff_PostShiba::connected(), 'keyHint' => Mailpuff_PostShiba::key_hint(), 'teamId' => Mailpuff_PostShiba::team_id(), 'fromConstant' => Mailpuff_PostShiba::from_constant(), 'cluster' => Mailpuff_PostShiba::cluster(), 'clusters' => array_values( (array) $state['clusters'] ), 'domains' => $domains, 'fromDomain' => Mailpuff_PostShiba::domain_for( $from_email ), 'webhook' => array( 'ok' => ! empty( $hook['id'] ) && ! empty( $hook['secret'] ), 'error' => (string) ( $hook['error'] ?? '' ), ), 'blocked' => empty( $block ) ? null : $block, 'checkedAt' => (int) $state['checked_at'], 'keysUrl' => Mailpuff_PostShiba::KEYS_URL, 'warmup' => Mailpuff_Send_Cap::warmup(), ); } /** * Mailyard's side: whether it is there, set up, and what it sends through, * whether or not Mailpuff's own mail takes it (with PostShiba connected, * Mailyard still sends the rest of the site's mail). * * @return array */ private static function mailyard() { $installed = file_exists( WP_PLUGIN_DIR . '/' . self::MAILYARD ); $active = function_exists( 'mailyard_is_active' ); $route = Mailpuff_Mailer::get_instance()->mailyard_state(); $routing = null !== $route; $install = ''; if ( ! $installed && current_user_can( 'install_plugins' ) ) { $install = self_admin_url( 'plugin-install.php?s=Mailyard&tab=search&type=term' ); } $activate = ''; if ( $installed && ! $active && current_user_can( 'activate_plugins' ) ) { // Not wp_nonce_url(): it escapes & for HTML, and this goes out as JSON. $activate = add_query_arg( array( 'action' => 'activate', 'plugin' => rawurlencode( self::MAILYARD ), '_wpnonce' => wp_create_nonce( 'activate-plugin_' . self::MAILYARD ), ), self_admin_url( 'plugins.php' ) ); } return array( 'installed' => $installed, 'active' => $active, 'configured' => $routing && ! $route['offline'], 'offline' => $routing && $route['offline'], 'provider' => $routing ? $route['provider'] : '', 'settingsUrl' => $active ? admin_url( 'options-general.php?page=mailyard' ) : '', 'installUrl' => $install, 'activateUrl' => $activate, ); } }