'; } public function description() { return __( 'Subscribe people to a Moosend mailing list with custom fields.', 'fieldwren' ); } public function help() { return '

' . esc_html__( 'Moosend → Settings → API key. Double opt-in follows the list\'s own setting.', 'fieldwren' ) . '

'; } public function credential_fields() { return array( 'api_key' => array( 'label' => __( 'API key', 'fieldwren' ), 'type' => 'password', 'required' => true ), ); } private function api( $method, $path, $body = null, $key = null ) { $key = null === $key ? $this->cred( 'api_key' ) : $key; $args = array(); if ( null !== $body ) { $args['body'] = $body; } $res = $this->http( $method, add_query_arg( 'apikey', rawurlencode( $key ), 'https://api.moosend.com/v3' . $path ), $args ); if ( ! is_wp_error( $res ) && is_array( $res['body'] ) && ! empty( $res['body']['Code'] ) ) { return new \WP_Error( 'fwren_moosend', (string) ( $res['body']['Error'] ?? __( 'Moosend error', 'fieldwren' ) ) ); } return $res; } protected function error_message( $body, $code ) { return is_array( $body ) && ! empty( $body['Error'] ) ? (string) $body['Error'] : parent::error_message( $body, $code ); } public function verify( array $creds ) { $b = self::body( $this->api( 'GET', '/lists/1/1.json', null, (string) $creds['api_key'] ) ); return is_wp_error( $b ) ? $b : true; } public function options( $source, array $settings ) { if ( 'lists' === $source ) { $b = self::body( $this->api( 'GET', '/lists/1/200.json' ) ); return is_wp_error( $b ) ? $b : self::pluck( $b['Context']['MailingLists'] ?? array(), 'ID', 'Name' ); } if ( 'fields' === $source ) { $b = self::body( $this->api( 'GET', '/lists/' . rawurlencode( (string) ( $settings['list_id'] ?? '' ) ) . '/details.json' ) ); if ( is_wp_error( $b ) ) { return $b; } $out = array(); foreach ( (array) ( $b['Context']['CustomFieldsDefinition'] ?? array() ) as $f ) { $out[] = array( 'key' => $f['Name'], 'label' => $f['Name'], 'required' => ! empty( $f['IsRequired'] ) ); } return $out; } return parent::options( $source, $settings ); } public function feed_fields( $form_id ) { return array_merge( array( array( 'key' => 'list_id', 'type' => 'select', 'source' => 'lists', 'label' => __( 'Mailing list', 'fieldwren' ), 'required' => true ) ), $this->contact_fields( array( 'email', 'name', 'phone' ) ), array( array( 'key' => 'map', 'type' => 'map', 'source' => 'fields', 'depends' => array( 'list_id' ), 'label' => __( 'Custom fields', 'fieldwren' ) ) ) ); } public function process( array $feed, array $entry, array $values ) { $s = $feed['settings']; $c = $this->contact(); $err = $this->require_email( $c ); if ( $err ) { return $err; } $custom = array(); foreach ( $this->mapped( 'map' ) as $k => $v ) { $custom[] = $k . '=' . $v; } $body = array_filter( array( 'Email' => $c['email'], 'Name' => $c['name'], 'Mobile' => $c['phone'], 'CustomFields' => $custom, ) ); $body['HasExternalDoubleOptIn'] = false; $res = $this->api( 'POST', '/subscribers/' . rawurlencode( (string) ( $s['list_id'] ?? '' ) ) . '/subscribe.json', $body ); /* translators: %s email */ return is_wp_error( $res ) ? $res : sprintf( __( '%s subscribed.', 'fieldwren' ), $c['email'] ); } }