id = isset( $data['id'] ) ? (string) $data['id'] : ''; $c->type = isset( $data['type'] ) && self::TYPE_SERVICE_ACCOUNT === $data['type'] ? self::TYPE_SERVICE_ACCOUNT : self::TYPE_OAUTH; $c->label = isset( $data['label'] ) ? (string) $data['label'] : ''; $c->client_id = isset( $data['client_id'] ) ? (string) $data['client_id'] : ''; $c->account_email = isset( $data['account_email'] ) ? (string) $data['account_email'] : ''; $c->scopes = isset( $data['scopes'] ) && is_array( $data['scopes'] ) ? array_values( array_map( 'strval', $data['scopes'] ) ) : array(); $c->want_business_profile = ! empty( $data['want_business_profile'] ); $c->status = isset( $data['status'] ) && self::STATUS_CONNECTED === $data['status'] ? self::STATUS_CONNECTED : self::STATUS_PENDING; $c->created = isset( $data['created'] ) ? (int) $data['created'] : 0; return $c; } /** * Data to store. * * @return array */ public function to_array() { return array( 'id' => $this->id, 'type' => $this->type, 'label' => $this->label, 'client_id' => $this->client_id, 'account_email' => $this->account_email, 'scopes' => $this->scopes, 'want_business_profile' => $this->want_business_profile, 'status' => $this->status, 'created' => $this->created, ); } /** * Whether the connection has finished setup. * * @return bool */ public function is_connected() { return self::STATUS_CONNECTED === $this->status; } /** * Whether Google granted a scope. * * @param string $scope Scope URL. * @return bool */ public function has_scope( $scope ) { return in_array( $scope, $this->scopes, true ); } /** * Name to show in lists. * * @return string */ public function display_name() { if ( '' !== $this->label ) { return $this->label; } return '' !== $this->account_email ? $this->account_email : __( 'Unnamed connection', 'turtlereport' ); } /** * Human label for the connection type. * * @return string */ public function type_label() { return self::TYPE_SERVICE_ACCOUNT === $this->type ? __( 'Service-account key', 'turtlereport' ) : __( 'Google sign-in (OAuth)', 'turtlereport' ); } }