-
Notifications
You must be signed in to change notification settings - Fork 2k
/
undocumented.js
2538 lines (2272 loc) · 69.3 KB
/
undocumented.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* External dependencies
*/
import debugFactory from 'debug';
import { camelCase, isPlainObject, omit, pick, snakeCase, set } from 'lodash';
import { stringify } from 'qs';
/**
* Internal dependencies.
*/
import Site from './site';
import Me from './me';
import MailingList from './mailing-list';
import config from 'calypso/config';
import { getLanguage, getLocaleSlug } from 'calypso/lib/i18n-utils';
import readerContentWidth from 'calypso/reader/lib/content-width';
const debug = debugFactory( 'calypso:wpcom-undocumented:undocumented' );
const { Blob } = globalThis; // The linter complains if I don't do this...?
/**
* Some endpoints are restricted by OAuth client IDs and secrets
* to prevent them being spammed. This adds these keys to the request
* so that they will be successful. This is not a sufficent measure
* against spam as these keys are exposed publicly
*
* @param { object } query - Add client_id and client_secret to the query.
*/
function restrictByOauthKeys( query ) {
query.client_id = config( 'wpcom_signup_id' );
query.client_secret = config( 'wpcom_signup_key' );
}
/**
* Create an `Undocumented` instance
*
* @param {object} wpcom - The request handler
* @returns {Undocumented} - An instance of Undocumented
*/
function Undocumented( wpcom ) {
if ( ! ( this instanceof Undocumented ) ) {
return new Undocumented( wpcom );
}
this.wpcom = wpcom;
}
Undocumented.prototype.site = function ( id ) {
return new Site( id, this.wpcom );
};
Undocumented.prototype.me = function () {
return new Me( this.wpcom );
};
Undocumented.prototype.mailingList = function ( category ) {
return new MailingList( category, this.wpcom );
};
/*
* Retrieve Jetpack modules data for a site with id siteid.
* Uses the REST API of the Jetpack site.
*
* @param {number} [siteId]
* @param {Function} fn
*/
Undocumented.prototype.getJetpackModules = function ( siteId, fn ) {
return this.wpcom.req.get(
{ path: '/jetpack-blogs/' + siteId + '/rest-api/' },
{ path: '/jetpack/v4/module/all/' },
fn
);
};
/*
* Activate a Jetpack module with slug moduleSlug for a site with id siteid.
* Uses the REST API of the Jetpack site.
*
* @param {number} [siteId]
* @param {string} [moduleSlug]
* @param {Function} fn
*/
Undocumented.prototype.jetpackModuleActivate = function ( siteId, moduleSlug, fn ) {
return this.wpcom.req.post(
{ path: '/jetpack-blogs/' + siteId + '/rest-api/' },
{
path: '/jetpack/v4/module/' + moduleSlug + '/active/',
body: JSON.stringify( { active: true } ),
},
fn
);
};
/*
* Deactivate a Jetpack module with slug moduleSlug for a site with id siteid.
* Uses the REST API of the Jetpack site.
*
* @param {number} [siteId]
* @param {string} [moduleSlug]
* @param {Function} fn
*/
Undocumented.prototype.jetpackModuleDeactivate = function ( siteId, moduleSlug, fn ) {
return this.wpcom.req.post(
{ path: '/jetpack-blogs/' + siteId + '/rest-api/' },
{
path: '/jetpack/v4/module/' + moduleSlug + '/active/',
body: JSON.stringify( { active: false } ),
},
fn
);
};
/**
* Fetches settings for the Monitor module.
*
* @param {number} [siteId] The site ID
* @param {Function} fn The callback function
*/
Undocumented.prototype.fetchMonitorSettings = function ( siteId, fn ) {
debug( '/jetpack-blogs/:site_id: query' );
return this.wpcom.req.get( { path: '/jetpack-blogs/' + siteId }, fn );
};
Undocumented.prototype.updateMonitorSettings = function (
siteId,
emailNotifications,
wpNoteNotifications,
fn
) {
debug( '/jetpack-blogs/:site_id: query' );
return this.wpcom.req.post(
{ path: '/jetpack-blogs/' + siteId },
{},
{ email_notifications: emailNotifications, wp_note_notifications: wpNoteNotifications },
fn
);
};
/**
* Disconnects a Jetpack site with id siteId from WP.com
*
* @param {number} [siteId] The site ID
* @param {Function} fn The callback function
*
*/
Undocumented.prototype.disconnectJetpack = function ( siteId, fn ) {
debug( '/jetpack-blogs/:site_id:/mine/delete query' );
return this.wpcom.req.post( { path: '/jetpack-blogs/' + siteId + '/mine/delete' }, fn );
};
/**
* Fetches plugin registration keys for WordPress.org sites with paid services
*
* @param {number} [siteId] The site ID
* @param {Function} fn The callback function
*/
Undocumented.prototype.fetchJetpackKeys = function ( siteId, fn ) {
debug( '/jetpack-blogs/:site_id:/keys query' );
return this.wpcom.req.get( { path: '/jetpack-blogs/' + siteId + '/keys' }, fn );
};
/**
* Test if a Jetpack Site is connected to .com
*
* @param {number} [siteId] The site ID
* @param {Function} fn The callback function
*/
Undocumented.prototype.testConnectionJetpack = function ( siteId, fn ) {
debug( '/jetpack-blogs/:site_id:/test-connection query' );
return this.wpcom.req.get( { path: '/jetpack-blogs/' + siteId + '/test-connection' }, fn );
};
/*
* Retrieve current connection status of a Jetpack site.
*
* @param {number} [siteId]
* @param {Function} fn
*/
Undocumented.prototype.getJetpackConnectionStatus = function ( siteId, fn ) {
return this.wpcom.req.get(
{ path: '/jetpack-blogs/' + siteId + '/rest-api/' },
{ path: '/jetpack/v4/connection/' },
fn
);
};
/*
* Retrieve current user's connection data for a Jetpack site.
*
* @param {number} [siteId]
* @param {Function} fn
*/
Undocumented.prototype.getJetpackUserConnectionData = function ( siteId, fn ) {
return this.wpcom.req.get(
{ path: '/jetpack-blogs/' + siteId + '/rest-api/' },
{ path: '/jetpack/v4/connection/data/' },
fn
);
};
Undocumented.prototype.jetpackLogin = function ( siteId, _wp_nonce, redirect_uri, scope, state ) {
debug( '/jetpack-blogs/:site_id:/jetpack-login query' );
const endpointUrl = '/jetpack-blogs/' + siteId + '/jetpack-login';
const params = { _wp_nonce, redirect_uri, scope, state };
return this.wpcom.req.get( { path: endpointUrl }, params );
};
Undocumented.prototype.jetpackAuthorize = function (
siteId,
code,
state,
redirect_uri,
secret,
jp_version,
from
) {
debug( '/jetpack-blogs/:site_id:/authorize query' );
const endpointUrl = '/jetpack-blogs/' + siteId + '/authorize';
const params = { code, state, redirect_uri, secret, jp_version, from };
return this.wpcom.req.post( { path: endpointUrl }, params );
};
Undocumented.prototype.jetpackValidateSSONonce = function ( siteId, ssoNonce, fn ) {
debug( '/jetpack-blogs/:site_id:/sso-validate query' );
const endpointUrl = '/jetpack-blogs/' + siteId + '/sso-validate';
const params = { sso_nonce: ssoNonce };
return this.wpcom.req.post( { path: endpointUrl }, params, fn );
};
Undocumented.prototype.jetpackAuthorizeSSONonce = function ( siteId, ssoNonce, fn ) {
debug( '/jetpack-blogs/:site_id:/sso-authorize query' );
const endpointUrl = '/jetpack-blogs/' + siteId + '/sso-authorize';
const params = { sso_nonce: ssoNonce };
return this.wpcom.req.post( { path: endpointUrl }, params, fn );
};
Undocumented.prototype.jetpackIsUserConnected = function ( siteId ) {
debug( '/sites/:site_id:/jetpack-connect/is-user-connected query' );
const endpointUrl = '/sites/' + siteId + '/jetpack-connect/is-user-connected';
return this.wpcom.req.get( { path: endpointUrl, apiNamespace: 'wpcom/v2' } );
};
/**
* Gets the current status of a full sync for a Jetpack site.
*
* @param {number|string} siteId The site ID
* @param {Function} fn The callback function
*/
Undocumented.prototype.getJetpackSyncStatus = function ( siteId, fn ) {
debug( '/sites/:site_id:/sync/status query' );
const endpointPath = '/sites/' + siteId + '/sync/status';
return this.wpcom.req.get( { path: endpointPath }, fn );
};
/**
* Schedules a full sync for a Jetpack site.
*
* @param {number|string} siteId The site ID
* @param {Function} fn The callback function
*/
Undocumented.prototype.scheduleJetpackFullysync = function ( siteId, fn ) {
debug( '/sites/:site_id:/sync query' );
const endpointPath = '/sites/' + siteId + '/sync';
return this.wpcom.req.post( { path: endpointPath }, {}, fn );
};
Undocumented.prototype.invitesList = function ( siteId, data = {}, fn ) {
debug( '/sites/:site_id:/invites query', siteId, data );
return this.wpcom.req.get( '/sites/' + siteId + '/invites', data, fn );
};
Undocumented.prototype.getInvite = function ( siteId, inviteKey, fn ) {
debug( '/sites/:site_id:/invites/:inviteKey:/ query' );
return this.wpcom.req.get( { path: '/sites/' + siteId + '/invites/' + inviteKey }, fn );
};
Undocumented.prototype.acceptInvite = function ( invite, fn ) {
debug( '/sites/:site_id:/invites/:inviteKey:/accept query' );
const apiVersion = '1.2';
return this.wpcom.req.get(
'/sites/' + invite.site.ID + '/invites/' + invite.inviteKey + '/accept',
{
activate: invite.activationKey,
include_domain_only: true,
apiVersion,
},
fn
);
};
Undocumented.prototype.sendInvites = function (
siteId,
usernamesOrEmails,
role,
message,
isExternal,
fn
) {
debug( '/sites/:site_id:/invites/new query' );
return this.wpcom.req.post(
'/sites/' + siteId + '/invites/new',
{},
{
invitees: usernamesOrEmails,
is_external: isExternal,
role: role,
message: message,
source: 'calypso',
},
fn
);
};
Undocumented.prototype.resendInvite = function ( siteId, inviteId, fn ) {
debug( '/sites/:site_id:/invites/:invite_id:/resend query' );
return this.wpcom.req.post( '/sites/' + siteId + '/invites/' + inviteId + '/resend', {}, {}, fn );
};
Undocumented.prototype.createInviteValidation = function ( siteId, usernamesOrEmails, role, fn ) {
debug( '/sites/:site_id:/invites/validate query' );
return this.wpcom.req.post(
'/sites/' + siteId + '/invites/validate',
{},
{
invitees: usernamesOrEmails,
role: role,
},
fn
);
};
// Used to preserve backslash in some known settings fields like custom time and date formats.
function encode_backslash( value ) {
if ( typeof value !== 'string' || value.indexOf( '\\' ) === -1 ) {
return value;
}
return value.replace( /\\/g, '\\\\' );
}
/**
* GET/POST site settings
*
* @param {number|string} [siteId] The site ID
* @param {string} [method] The request method
* @param {object} [data] The POST data
* @param {Function} fn The callback function
*/
Undocumented.prototype.settings = function ( siteId, method = 'get', data = {}, fn ) {
debug( '/sites/:site_id:/settings query' );
if ( 'function' === typeof method ) {
fn = method;
method = 'get';
data = {};
}
// If no apiVersion was specified, use the settings api version with the widest support (1.1)
const apiVersion = data.apiVersion || '1.1';
const body = omit( data, [ 'apiVersion' ] );
const path = '/sites/' + siteId + '/settings';
if ( 'get' === method ) {
return this.wpcom.req.get( path, { apiVersion }, fn );
}
// special treatment to preserve backslash in date_format
if ( body.date_format ) {
body.date_format = encode_backslash( body.date_format );
}
if ( body.time_format ) {
body.time_format = encode_backslash( body.time_format );
}
return this.wpcom.req.post( { path }, { apiVersion }, body, fn );
};
/**
* Get site keyrings
*
* @param {number|string} [siteId] The site ID
* @param {Function} fn The callback function
*
* @returns {Promise} A promise that resolves when the request completes
*/
Undocumented.prototype.getSiteKeyrings = function getSiteKeyrings( siteId, fn ) {
return this.wpcom.req.get( '/sites/' + siteId + '/keyrings', { apiVersion: '1.1' }, fn );
};
/**
* Create a site keyring
*
* @param {number|string} [siteId] The site ID
* @param {object} [data] site keyring object to create with properties:
* - keyring_id {number} the keyring id link the site to
* - external_user_id {string} Optional. The external user id to link the site to
* - service {string} service name for this keyring id
* @param {Function} fn The callback function
*
* @returns {Promise} A promise that resolves when the request completes
*/
Undocumented.prototype.createSiteKeyring = function createSiteKeyring( siteId, data, fn ) {
return this.wpcom.req.post( '/sites/' + siteId + '/keyrings', { apiVersion: '1.1' }, data, fn );
};
/**
* Update a site keyring
*
* @param {number|string} [siteId] The site ID
* @param {number} [keyringId] The keyring id to update,
* @param {string} [externalUserId] The external user id to update on the site keyring
* @param {Function} fn The callback function
*
* @returns {Promise} A promise that resolves when the request completes
*/
Undocumented.prototype.updateSiteKeyring = function updateSiteKeyring(
siteId,
keyringId,
externalUserId,
fn
) {
return this.wpcom.req.post(
'/sites/' + siteId + '/keyrings/' + keyringId,
{ apiVersion: '1.1' },
{
external_user_id: externalUserId,
},
fn
);
};
/**
* Delete a site keyring
*
* @param {number|string} [siteId] The site ID
* @param {number} keyringId The keyring id
* @param {string|null} externalUserId Optional, the external user id
* @param {Function} fn The callback function
*
* @returns {Promise} A promise that resolves when the request completes
*/
Undocumented.prototype.deleteSiteKeyring = function deleteSiteKeyring(
siteId,
keyringId,
externalUserId,
fn
) {
if ( ! fn && typeof externalUserId === 'function' ) {
fn = externalUserId;
externalUserId = null;
}
return this.wpcom.req.post(
'/sites/' + siteId + '/keyrings/' + keyringId + '/delete',
{ apiVersion: '1.1' },
{
external_user_id: externalUserId,
},
fn
);
};
Undocumented.prototype._sendRequest = function ( originalParams, fn ) {
const { apiVersion, method } = originalParams;
const updatedParams = omit( originalParams, [ 'apiVersion', 'method' ] );
if ( apiVersion ) {
// TODO: temporary solution for apiVersion until https://github.com/Automattic/wpcom.js/issues/152 is resolved
return this.wpcom.req[ method.toLowerCase() ]( updatedParams, { apiVersion }, fn );
}
return this.wpcom.req[ method.toLowerCase() ]( updatedParams, fn );
};
/**
* Determine whether a domain name is available for registration
*
* @param {string} domain - The domain name to check.
* @param {number} blogId - Optional blogId to determine if domain is used on another site.
* @param {boolean} isCartPreCheck - specifies whether this availability check is for a domain about to be added to the cart.
* @param {Function} fn The callback function
* @returns {Promise} A promise that resolves when the request completes
*/
Undocumented.prototype.isDomainAvailable = function ( domain, blogId, isCartPreCheck, fn ) {
return this.wpcom.req.get(
`/domains/${ encodeURIComponent( domain ) }/is-available`,
{
blog_id: blogId,
apiVersion: '1.3',
is_cart_pre_check: isCartPreCheck,
},
fn
);
};
/**
* Get the inbound transfer status for this domain
*
* @param {string} domain - The domain name to check.
* @param {string} authCode - The auth code for the given domain to check.
* @param {Function} fn The callback function
* @returns {Promise} A promise that resolves when the request completes
*/
Undocumented.prototype.checkAuthCode = function ( domain, authCode, fn ) {
return this.wpcom.req.get(
`/domains/${ encodeURIComponent( domain ) }/inbound-transfer-check-auth-code`,
{ auth_code: authCode },
fn
);
};
/**
* Get the inbound transfer status for this domain
*
* @param {string} domain - The domain name to check.
* @param {Function} fn The callback function
* @returns {Promise} A promise that resolves when the request completes
*/
Undocumented.prototype.getInboundTransferStatus = function ( domain, fn ) {
return this.wpcom.req.get(
{
path: `/domains/${ encodeURIComponent( domain ) }/inbound-transfer-status`,
},
fn
);
};
/**
* Starts an inbound domain transfer that is in the pending_start state.
*
* @param {number|string} siteId The site ID
* @param {string} domain The domain name
* @param {string} authCode The auth code for the transfer
* @param {Function} fn The callback function
* @returns {Promise} A promise that resolves when the request completes
*/
Undocumented.prototype.startInboundTransfer = function ( siteId, domain, authCode, fn ) {
let query = {};
if ( authCode && authCode !== '' ) {
query = { auth_code: authCode };
}
return this.wpcom.req.get(
`/domains/${ encodeURIComponent( domain ) }/inbound-transfer-start/${ siteId }`,
query,
fn
);
};
/**
* Fetches a list of available top-level domain names ordered by popularity.
*
* @param {object} query Optional query parameters
* @returns {Promise} A promise that resolves when the request completes
*/
Undocumented.prototype.getAvailableTlds = function ( query = {} ) {
return this.wpcom.req.get( '/domains/suggestions/tlds', query );
};
/**
* Determine whether a domain name can be used for Site Redirect
*
* @param {number|string} siteId The site ID
* @param {string} domain The domain name to check
* @param {Function} fn The callback function
* @returns {Promise} A promise that resolves when the request completes
*/
Undocumented.prototype.canRedirect = function ( siteId, domain, fn ) {
domain = encodeURIComponent( domain.toLowerCase() );
return this.wpcom.req.get( { path: '/domains/' + siteId + '/' + domain + '/can-redirect' }, fn );
};
/**
* Retrieves the target of the site redirect.
*
* @param {number|string} siteId The site ID
* @param {Function} fn The callback function
*/
Undocumented.prototype.getSiteRedirect = function ( siteId, fn ) {
debug( '/sites/:site_id/domains/redirect query' );
return this.wpcom.req.get( { path: '/sites/' + siteId + '/domains/redirect' }, fn );
};
/**
* Points the site redirect to the specified location.
*
* @param {number|string} siteId The site ID
* @param {string} location The location to redirect the site to
* @param {Function} fn The callback function
*/
Undocumented.prototype.setSiteRedirect = function ( siteId, location, fn ) {
debug( '/sites/:site_id/domains/redirect' );
return this.wpcom.req.post(
{ path: '/sites/' + siteId + '/domains/redirect' },
{ location },
fn
);
};
/**
* Retrieves the domain contact information of the user.
*
* @param {Function} fn The callback function
*/
Undocumented.prototype.getDomainContactInformation = function ( fn ) {
debug( '/me/domain-contact-information query' );
return this._sendRequest(
{
path: '/me/domain-contact-information',
method: 'get',
},
function ( error, data ) {
if ( error ) {
return fn( error );
}
const newData = mapKeysRecursively( data, function ( key ) {
return key === '_headers' ? key : camelCase( key );
} );
fn( null, newData );
}
);
};
/**
*
* @param domain {string}
* @param fn {function}
*/
Undocumented.prototype.getDomainPrice = function ( domain, fn ) {
return this.wpcom.req.get(
`/domains/${ encodeURIComponent( domain ) }/price`,
{
apiVersion: '1.1',
},
fn
);
};
Undocumented.prototype.getDomainRegistrationSupportedStates = function ( countryCode, fn ) {
debug( '/domains/supported-states/ query' );
return this._sendRequest(
{
path: '/domains/supported-states/' + countryCode,
method: 'get',
},
fn
);
};
function mapKeysRecursively( object, fn ) {
return Object.keys( object ).reduce( function ( mapped, key ) {
let value = object[ key ];
if ( isPlainObject( value ) ) {
value = mapKeysRecursively( value, fn );
}
mapped[ fn( key ) ] = value;
return mapped;
}, {} );
}
/**
* Validates the specified domain contact information against a list of domain names.
*
* @param {object} contactInformation - user's contact information
* @param {string[]} domainNames - list of domain names
* @param {Function} fn The callback function
* @param {object} query Query object for the call to wpcom.req.post
*/
Undocumented.prototype.validateDomainContactInformation = function (
contactInformation,
domainNames,
fn,
query
) {
let data = {
contactInformation: contactInformation,
domainNames: domainNames,
};
debug( '/me/domain-contact-information/validate query' );
data = mapKeysRecursively( data, snakeCase );
// Due to backend limitations some versions of this endpoint
// serialize a nested object in the response, encoding e.g.
// { foo: { bar: { baz: [ "error" ] } } }
// as
// { foo.bar.baz: [ "error" ] }
// here we decide whether to rehydrate this.
const shouldReshapeResponse = query?.apiVersion === '1.2';
return this.wpcom.req.post(
{ path: '/me/domain-contact-information/validate' },
query,
data,
function ( error, successData ) {
if ( error ) {
return fn( error );
}
// Reshape the error messages to a nested object
if ( successData.messages && shouldReshapeResponse ) {
successData.messages = Object.keys( successData.messages ).reduce( ( obj, key ) => {
set( obj, key, successData.messages[ key ] );
return obj;
}, {} );
}
const newData = mapKeysRecursively( successData, function ( key ) {
return key === '_headers' ? key : camelCase( key );
} );
fn( null, newData );
}
);
};
/**
* Validates the specified Google Apps contact information
*
* The contactInformation keys can be in camelCase or snake_case, and will be
* converted to snake_case before being submitted. The returned data keys will
* be converted to camelCase before being passed to the callback or the resolved
* Promise.
*
* @param {object} contactInformation - user's contact information
* @param {string[]} domainNames - domain names for which GSuite is being purchased
* @param {(error: string, data: object) => void} [callback] The callback function.
* @returns {Promise|undefined} If no callback, returns a Promise that resolves when the request completes
*/
Undocumented.prototype.validateGoogleAppsContactInformation = function (
contactInformation,
domainNames,
callback
) {
const { contact_information } = mapKeysRecursively( { contactInformation }, snakeCase );
debug( '/me/google-apps/validate', contact_information, domainNames );
const stripHeadersFromHttpResponse = ( httpResponse ) =>
Object.keys( httpResponse )
.filter( ( key ) => key !== '_headers' )
.reduce( ( newResponse, key ) => ( { ...newResponse, [ key ]: httpResponse[ key ] } ), {} );
const camelCaseKeys = ( httpResponse ) =>
mapKeysRecursively( stripHeadersFromHttpResponse( httpResponse ), ( key ) => camelCase( key ) );
const camelCaseCallback = ( error, httpResponse ) =>
callback( error, error ? null : camelCaseKeys( httpResponse ) );
const result = this.wpcom.req.post(
{ path: '/me/google-apps/validate', body: { contact_information, domain_names: domainNames } },
callback ? camelCaseCallback : null
);
return result.then?.( camelCaseKeys );
};
/**
* Retrieves the Titan order provisioning URL for a domain.
*
* @param domain the domain name
* @param fn The callback function
*/
Undocumented.prototype.getTitanOrderProvisioningURL = function ( domain, fn ) {
return this.wpcom.req.get(
{
path: `/emails/titan/${ encodeURIComponent( domain ) }/order-provisioning-url`,
apiNamespace: 'wpcom/v2',
},
fn
);
};
/**
* Retrieves the auto login link to Titan's control panel.
*
* @param orderId the Titan order ID
* @param fn The callback function
*/
Undocumented.prototype.getTitanControlPanelAutoLoginURL = function ( orderId, fn ) {
return this.wpcom.req.get(
{
path: `/emails/titan/${ encodeURIComponent( orderId ) }/control-panel-auto-login-url`,
apiNamespace: 'wpcom/v2',
},
fn
);
};
/**
* Get a list of WordPress.com products
*
* @param {Function} fn The callback function
*/
Undocumented.prototype.getProducts = function ( fn ) {
debug( '/products query' );
return this._sendRequest(
{
path: '/products',
method: 'get',
},
fn
);
};
/**
* Get site specific details for WordPress.com products
*
* @param {Function} siteDomain The site slug
* @param {Function} fn The callback function
*/
Undocumented.prototype.getSiteProducts = function ( siteDomain, fn ) {
debug( '/sites/:site_domain:/products query' );
// the site domain could be for a jetpack site installed in
// a subdirectory. encode any forward slash present before making
// the request
siteDomain = encodeURIComponent( siteDomain );
return this._sendRequest(
{
path: '/sites/' + siteDomain + '/products',
method: 'get',
},
fn
);
};
/**
* Get a site specific details for WordPress.com plans
*
* @param {Function} siteDomain The site slug
* @param {Function} fn The callback function
*/
Undocumented.prototype.getSitePlans = function ( siteDomain, fn ) {
debug( '/sites/:site_domain:/plans query' );
// the site domain could be for a jetpack site installed in
// a subdirectory. encode any forward slash present before making
// the request
siteDomain = encodeURIComponent( siteDomain );
return this._sendRequest(
{
path: '/sites/' + siteDomain + '/plans',
method: 'get',
apiVersion: '1.3',
},
fn
);
};
/**
* Get cart.
*
* @param {string} cartKey The cart's key.
* @param {Function} fn The callback function.
*/
Undocumented.prototype.getCart = function ( cartKey, fn ) {
debug( 'GET: /me/shopping-cart/:cart-key' );
return this._sendRequest(
{
path: '/me/shopping-cart/' + cartKey,
method: 'GET',
},
fn
);
};
/**
* Set cart.
*
* @param {string} cartKey The cart's key.
* @param {object} data The POST data.
* @param {Function} fn The callback function.
*/
Undocumented.prototype.setCart = function ( cartKey, data, fn ) {
debug( 'POST: /me/shopping-cart/:cart-key', data );
return this._sendRequest(
{
path: '/me/shopping-cart/' + cartKey,
method: 'POST',
body: data,
},
fn
);
};
/**
* Get a list of the user's stored cards
*
* @param {Function} [fn] The callback function.
*
* @returns {Promise} Returns a promise when the `callback` is not provided.
*/
Undocumented.prototype.getStoredCards = function ( fn ) {
debug( '/me/stored-cards query' );
return this.wpcom.req.get( { path: '/me/stored-cards' }, fn );
};
/**
* Get a list of the user's stored payment methods
*
* @param {object} query The query parameters
* @param {Function} [fn] The callback function.
*
* @returns {Promise} Returns a promise when the `callback` is not provided.
*/
Undocumented.prototype.getPaymentMethods = function ( query, fn ) {
debug( '/me/payment-methods query', { query } );
return this.wpcom.req.get( '/me/payment-methods', query, fn );
};
/**
* Return a list of third-party services that WordPress.com can integrate with for a specific site
*
* @param {number|string} siteId The site ID or domain
* @param {Function} fn The callback function
* @returns {Promise} A Promise to resolve when complete
*/
Undocumented.prototype.sitesExternalServices = function ( siteId, fn ) {
debug( '/sites/:site-id:/external-services query' );
return this.wpcom.req.get(
{
path: '/sites/' + siteId + '/external-services',
apiNamespace: 'wpcom/v2',
},
fn
);
};
/**
* Return a list of happiness engineers gravatar urls
*
* @param {Function} fn The callback function
*/
Undocumented.prototype.getHappinessEngineers = function ( fn ) {
debug( 'meta/happiness-engineers/ query' );
return this.wpcom.req.get( { path: '/meta/happiness-engineers/' }, fn );
};
/**
* Return a list of sharing buttons for the specified site, with optional
* query parameters
*
* @param {number|string} siteId The site ID or domain
* @param {object} query Optional query parameters
* @param {Function} fn Method to invoke when request is complete
*/
Undocumented.prototype.sharingButtons = function ( siteId, query, fn ) {
if ( 'undefined' === typeof fn && 'function' === typeof query ) {
fn = query;
query = {};
}
debug( '/sites/:site_id:/sharing-buttons query' );
return this.wpcom.req.get( '/sites/' + siteId + '/sharing-buttons', query, fn );
};
/**
* Saves the set of sharing buttons for the specified site
*
* @param {number|string} siteId The site ID or domain
* @param {Array} buttons An array of sharing button objects
* @param {Function} fn Method to invoke when request is complete
*/
Undocumented.prototype.saveSharingButtons = function ( siteId, buttons, fn ) {
debug( '/sites/:site_id:/sharing-buttons query' );
return this.wpcom.req.post(
{
path: '/sites/' + siteId + '/sharing-buttons',
body: { sharing_buttons: buttons },
apiVersion: '1.1',
},
fn
);
};
/**
* Return a list of user's connected services
*
* @param {*} forceExternalUsersRefetch ???
* @param {Function} fn The callback function
* @returns {Promise} A Promise to resolve when complete.
*/
Undocumented.prototype.mekeyringConnections = function ( forceExternalUsersRefetch, fn ) {
debug( '/me/connections query' );
// set defaults, first argument is actually a callback
if ( typeof forceExternalUsersRefetch === 'function' ) {
fn = forceExternalUsersRefetch;
forceExternalUsersRefetch = false;