-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathWPRestAPI.livecode
2754 lines (2144 loc) · 84.7 KB
/
WPRestAPI.livecode
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
script "WPRestAPI"
local sWPAPI
local sAccessToken
local sTokenLastUpdateTime
-------------------------------------------------------------------------------------------------------------
-- logIn
-- DESCRIPTION:
-- This function get WPAPI properties and check them if everything is good, returns "true"
-- PARAMETERS:
-- pWPAPI: Array which is contains all properties which we need for rest functionality
-- put "CLIENT ID" into tWPAPI["clientid"]
-- put "CLIENT SECRET" into tWPAPI["secret"]
-- put "https://yourwebsite.com" into tWPAPI["endpoint"]
-- put "USERNAME" into tWPAPI["username"]
-- put "PASSWORD" into tWPAPI["password"]
-- put "https://yourwebsite.com/wp-json/wp/v2/" into tWPAPI["post_url"]
-- CHANGES:
-- 22/04/2016 Narek: Created function
-------------------------------------------------------------------------------------------------------------
function logIn pWPAPI
put pWPAPI into sWPAPI
--put empty into sAccessToken
put pWPAPI["secret"] into tSecret
-- Setting the http headers
put "Content-Type: application/json" & cr after tHeader
put "Cache-Control: no-cache" & cr after tHeader
-- Setting http headers
set the httpheaders to tHeader
put accessTokenGet() into sAccessToken
if sAccessToken is empty then
return false
end if
if sAccessToken["access_token"] is empty then
put accessTokenGet() into sAccessToken
-- Try one more time
if sAccessToken["access_token"] is empty then
return false
end if
end if
return true
end logIn
-------------------------------------------------------------------------------------------------------------
-- accessTokenGet
-- DESCRIPTION:
-- This function returns access token
-- PARAMETERS:
-- none:
-- CHANGES:
-- 21/04/2016 Narek: Created function
-------------------------------------------------------------------------------------------------------------
function accessTokenGet
put the seconds into tTokenRequestTime
-- If it is empty then we need new access token
if sTokenLastUpdateTime is not empty then
-- One access token we can use within one hour
if (tTokenRequestTime - sTokenLastUpdateTime) > 3000 or sAccessToken is empty then
-- Continue script to get access token
else
if sAccessToken["access_token"] is empty then
-- Continue script to get access token
else
return sAccessToken
end if
end if
end if
-- Setting the http headers
put "Content-Type: application/json" & cr after tHeader
put "Cache-Control: no-cache" & cr after tHeader
-- Basic authentication
put "Authorization: Basic " after tHeader
put base64encode(sWPAPI["clientid"] & ":" & sWPAPI["secret"]) into tH
replace cr with "" in tH
put tH after tHeader
set the httpheaders to tHeader
libURLSetSSLVerification false
put sWPAPI["username"] into tUserInfo["username"]
put sWPAPI["password"] into tUserInfo["password"]
put "password" into tUserInfo["grant_type"]
-- Convert to Json for posting
put ArrayToJSON(tUserInfo) into tUserInfoJson
-- Build the URL
put sWPAPI["endpoint"] into tURL
put "/?oauth=token" after tURL
-- Post json to builded URL for getting the access token
post tUserInfoJson to URL tURL
put the result into tok
put it into tResponse
if tResponse is empty then
return empty
end if
-- added by Chi
replace "<!-- Really Simple SSL mixed content fixer active -->" with "" in tResponse
-- Put access token get time for further checking
put the seconds into sTokenLastUpdateTime
-- Convert received json to array and get access token
put JSONToArray(tResponse) into tResultArray
return tResultArray
end accessTokenGet
-------------------------------------------------------------------------------------------------------------
-- accessTokenCheck
-- DESCRIPTION:
-- This function check can we use access token or we need new one
-- PARAMETERS:
-- none:
-- CHANGES:
-- 21/04/2016 Narek: Created function
-------------------------------------------------------------------------------------------------------------
command accessTokenCheck
-- Request sending time
put the seconds into tTokenRequestTime
-- If it is empty then we need new access token
if sTokenLastUpdateTime is not empty then
-- One access token we can use within one hour
if (tTokenRequestTime - sTokenLastUpdateTime) > 3000 or sAccessToken is empty then
put accessTokenGet() into sAccessToken
else
if sAccessToken contains "error" or sAccessToken contains "AccessToken" then
put accessTokenGet() into sAccessToken
end if
end if
else
put accessTokenGet() into sAccessToken
end if
end accessTokenCheck
-------------------------------------------------------------------------------------------------------------
-- postsGetList
-- DESCRIPTION:
-- This function returns all posts data
-- PARAMETERS:
-- pParameters: Filters for getting list
-- CHANGES:
-- 30/03/2016 Gohar: Created function
-------------------------------------------------------------------------------------------------------------
function postsGetList pParameters
libURLSetSSLVerification false
-- Maximum number of items to be returned in result set. Default: 10
if pParameters["per_page"] is not empty then
put "&per_page=" & pParameters["per_page"] after tFilters
end if
-- Scope under which the request is made; determines fields present in response. View, Embed or Edit
if pParameters["context"] is not empty then
if pParameters["context"] is "edit" then
accessTokenCheck
put "?access_token=" & sAccessToken["access_token"] into tAPICall
put "&context=" & pParameters["context"] after tFilters
else
put "&context=" & pParameters["context"] after tFilters
end if
end if
-- Current page of the collection. Default: 1
if pParameters["page"] is not empty then
put "&page=" & pParameters["page"] after tFilters
end if
-- The order of the collection. Can either be asc or desc.
if pParameters["order"] is not empty then
put "&order=" & pParameters["order"] after tFilters
end if
-- Sorting attribute of the collection. Possible values can be id, title or slug.
if pParameters["orderby"] is not empty then
put "&orderby=" & pParameters["orderby"] after tFilters
end if
-- Limit the results to a post having a specific slug.
if pParameters["slug"] is not empty then
put "&slug=" & pParameters["slug"] after tFilters
end if
-- Used to limit the collection of the posts having a particular status.
if pParameters["status"] is not empty then
put "&status=" & pParameters["status"] after tFilters
end if
-- The author ID. Used to limit results belonging to a specific author.
if pParameters["author"] is not empty then
put "&author=" & pParameters["author"] after tFilters
end if
-- The search query. Limit results to the matching string.
if pParameters["search"] is not empty then
put "&search=" & pParameters["search"] after tFilters
end if
put "posts/" before tAPICall
-- Get post URL
put sWPAPI["post_url"] into tPostURL
if tAPICall contains "?" then
else
put "?" into char 1 of tFilters
end if
-- Get all posts list
get URL(tPostURL & tAPICall & tFilters)
put it into tJsonData
-- Converting postes data json to array for usability
put JSONToArray(tJsonData) into tPagesList
-- Convert JSON to array
return tPagesList
end postsGetList
-------------------------------------------------------------------------------------------------------------
-- postGetByID
-- DESCRIPTION:
-- This function returns post data by ID
-- PARAMETERS:
-- pPostId: Posst ID which data we want to return
-- pContext: Scope under which the request is made; determines fields present in response.
-- CHANGES:
-- 30/03/2016 Gohar: Created function
-------------------------------------------------------------------------------------------------------------
function postGetByID pPostId, pContext
--Check for required parameters
if pPostID is empty then
return "Error: PostId is required" //postid is required
end if
-- Build the api call
put "posts/" into tAPICall
put pPostId after tAPICall
-- Checkin context parameter
if pContext is "view" or pContext is "embed" then
put "/?context=" & pContext after tAPICall
end if
-- For Edit context we need a access token
if pContext is "edit" then
accessTokenCheck
put "/?access_token=" & sAccessToken["access_token"] after tAPICall
put "&context=" & pContext after tAPICall
end if
-- Get post URL
put sWPAPI["post_url"] into tPostURL
-- Create the URL used to call the REST service
get URL(tPostURL & tAPICall)
put it into tJsonData
put JSONToArray(tJsonData) into tArray
return tArray
end postGetByID
-------------------------------------------------------------------------------------------------------------
-- postCreate
-- DESCRIPTION:
-- This function creates new post
-- PARAMETERS:
-- pPostData: Creating post data
-- CHANGES:
-- 20/04/2016 Narek: Created function
-------------------------------------------------------------------------------------------------------------
function postCreate pPostData
accessTokenCheck
--Checking and Buidling Parameters
if pPostData["title"] is empty then
return "Error: Title is required" //title is required
end if
if pPostData["content"] is empty then
return "Error: Content Raw is required"
end if
-- Setting the http headers
put "Content-Type: application/json" & cr after tHeader
set the httpheaders to tHeader
libURLSetSSLVerification false
-- Getting access token for authentification
put sAccessToken["access_token"] into tAccessToken
-- Convert passed array to Json for posting
put ArrayToJSON(pPostData) into tPostJson
-- Create the URL used to call the REST service
put "posts" into tAPICall
put "/?access_token=" & tAccessToken after tAPICall
put sWPAPI["post_url"] into tPostURL
-- Posting for creating the post
post tPostJson to URL(tPostURL & tAPICall)
put it into tResponse
put JSONToArray(tResponse) into tReponseArray
if tReponseArray is empty then
return false
end if
return tReponseArray
end postCreate
-------------------------------------------------------------------------------------------------------------
-- postDelete
-- DESCRIPTION:
-- This function deletes post by ID
-- PARAMETERS:
-- pPostID: Post ID which we want to delete
-- pForce: Whether to bypass trash and force deletion.
-- by default it is false and post goes to the trash
-- if set it to true, the post will not be able to be restored by the user.
-- CHANGES:
-- 20/04/2016 Narek: Created function
-------------------------------------------------------------------------------------------------------------
function postDelete pPostID, pForce
accessTokenCheck
--Checking and Buidling Parameters
if pPostID is empty then
return "Error: PostID is required" //pPostID is required
end if
if pForce is true then
put "&force=true" into tForce
end if
-- Getting access token for authentification
put sAccessToken["access_token"] into tAccessToken
-- Create the URL used to call the REST service
put "posts/" into tAPICall
put pPostID & "/?access_token=" & tAccessToken & tForce after tAPICall
put sWPAPI["post_url"] into tPostURL
delete URL (tPostURL & tAPICall)
put the result into tResults
if tResults is empty then
return 0
end if
return tResults
end postDelete
-------------------------------------------------------------------------------------------------------------
-- postEdit
-- DESCRIPTION:
-- This function edit post by ID
-- PARAMETERS:
-- pPostID: Post ID which we want to edit
-- pPostData: Editing post data
-- CHANGES:
-- 20/03/2016 Gohar: Created function
-------------------------------------------------------------------------------------------------------------
function postEdit pPostID, pPostData
accessTokenCheck
-- Post title is required
if pPostData["title"] is empty then
return "Error: Title is required"
end if
-- Setting the http headers
put "Content-Type: application/json" into tHeader
set the httpheaders to tHeader
-- Getting access token for authentification
put sAccessToken["access_token"] into tAccessToken
-- Convert passed array to Json for posting
put ArrayToJSON(pPostData) into tPostJson
-- Create the URL used to call the REST service
put "posts/" into tAPICall
put pPostID & "/?access_token=" & tAccessToken after tAPICall
put sWPAPI["post_url"] into tPostURL
-- Posting for editing the post
post tPostJson to URL(tPostURL & tAPICall)
put it into tResults
put JSONToArray(tResults) into tResults
if tResults is empty then
return 0
end if
return tResults
end postEdit
-------------------------------------------------------------------------------------------------------------
-- postEditWithLang
-- DESCRIPTION:
-- This function edit post with languages
-- PARAMETERS:
-- pPostID : ID of post which we want to edit
-- pPostData: Editing post data
-- CHANGES:
-- 02/05/2016 Narek: Created function
-------------------------------------------------------------------------------------------------------------
function postEditWithLang pPostID, pPostData
accessTokenCheck
-- Setting the http headers
put "Content-Type: application/json" & cr after tHeader
set the httpheaders to tHeader
libURLSetSSLVerification false
-- Getting access token for authentification
put sAccessToken["access_token"] into tAccessToken
-- Get url for posting
put sWPAPI["post_url"] into tPostURL
-- Create the URL used to call the REST service
put "posts/" & pPostID into tAPICall
-- We need context for get the all languages list of our post
put "/?context=edit" after tAPICall
put "&access_token=" & tAccessToken after tAPICall
-- Create the URL used to call the REST service
get URL(tPostURL & tAPICall)
put it into tEditingPostJson
put JSONToArray(tEditingPostJson) into tEditingPostArray
-- Get languages of post title
put tEditingPostArray["title"]["raw"] into tTitleLangList
-- Put all title languages into array for changes
repeat with tIndex = 1 to the len of tTitleLangList
if char tIndex of tTitleLangList is "]" then
put char tIndex of tTitleLangList after tChar
put char tIndex-2 to tIndex-1 of tTitleLangList into tTitleLang
put tIndex into tCalc
else
put char tIndex of tTitleLangList after tChar
if tIndex is not 1 then
if char tIndex of tTitleLangList is "[" then
put char tCalc+1 to tIndex-1 of tTitleLangList into tTitleValue
put tTitleValue into tPostLang["title"][tTitleLang]
end if
end if
end if
end repeat
-- Add languages which we want to edit or add
repeat for each key tKey in pPostData["title"]
put pPostData["title"][tKey] into tPostLang["title"][tKey]
end repeat
-- Get languages of post content
put tEditingPostArray["content"]["raw"] into tContentLangList
-- Put all content languages into array for changes
repeat with tIndex = 1 to the len of tContentLangList
if char tIndex of tContentLangList is "]" then
put char tIndex of tContentLangList after tChar
put char tIndex-2 to tIndex-1 of tContentLangList into tContentLang
put tIndex into tCalc
else
put char tIndex of tContentLangList after tChar
if tIndex is not 1 then
if char tIndex of tContentLangList is "[" then
put char tCalc+1 to tIndex-1 of tContentLangList into tContentValue
put tContentValue into tPostLang["content"][tContentLang]
end if
end if
end if
end repeat
-- Add languages which we want to edit or add
repeat for each key tKey in pPostData["content"]
put pPostData["content"][tKey] into tPostLang["content"][tKey]
end repeat
-- Build final line for title and content with all needed shortcodes
put empty into tTurnToContent
repeat for each element tElement in tPostLang
if tTurnToContent is not empty then
put "[vc_row][vc_column][vc_column_text]" into tContent
repeat for each key tKey in tElement
put "[:" & tKey & "]" & tElement[tKey] after tContent
end repeat
put "[/vc_column_text][/vc_column][/vc_row]" after tContent
else
repeat for each key tKey in tElement
put "[:" & tKey & "]" & tElement[tKey] after tTitle
put "content" into tTurnToContent
end repeat
end if
end repeat
-- Build final array for posting
put tTitle into tEditPostArray["title"]
put tContent into tEditPostArray["content"]
repeat for each key tKey in pPostData
if tKey is not "title" and tKey is not "content" then
put pPostData[tKey] into tEditPostArray[tKey]
end if
end repeat
-- Convert to json for posting
put ArrayToJSON(tEditPostArray) into tPostJson
-- Build URL
put "posts/" & pPostID into tAPICall
put "/?access_token=" & tAccessToken after tAPICall
-- Post json data for editing
post tPostJson to URL (tPostURL & tAPICall)
put it into tResults
if tResults is empty then
return 0
end if
return tResults
end postEditWithLang
-------------------------------------------------------------------------------------------------------------
-- postRevisionsGetList
-- DESCRIPTION:
-- This function returns post's revisions list
-- PARAMETERS:
-- pPostID: Post ID whcih revisions we want to get
-- CHANGES:
-- 19/04/2016 Narek: Created function
-------------------------------------------------------------------------------------------------------------
function postRevisionsGetList pPostID
accessTokenCheck
--Check for required parameters
if pPostID is empty then
return "Error: PostId is required"
end if
-- Getting access token for authentification
put sAccessToken["access_token"] into tAccessToken
put "posts/" & pPostID & "/revisions" into tAPICall
put "/?access_token=" & tAccessToken after tAPICall
-- Get post URL
put sWPAPI["post_url"] into tPostURL
-- Get all posts list
get URL(tPostURL & tAPICall)
put it into tJsonData
-- Converting postes data json to array for usability
put JSONToArray(tJsonData) into tPagesList
-- Convert JSON to array
return tPagesList
end postRevisionsGetList
-------------------------------------------------------------------------------------------------------------
-- postRevisionGetByID
-- DESCRIPTION:
-- This function returns post's revision by ID
-- PARAMETERS:
-- pPostID: Post ID whcih revisions we want to get
-- pRevisionID: Revision id which we want to get
-- CHANGES:
-- 25/04/2016 Narek: Created function
-------------------------------------------------------------------------------------------------------------
function postRevisionGetByID pPostID, pRevisionID
accessTokenCheck
--Check for required parameters
if pPostID is empty then
return "Error: PostID is required"
end if
if pRevisionID is empty then
return "Error: RevisionID is required"
end if
-- Getting access token for authentification
put sAccessToken["access_token"] into tAccessToken
put "posts/" & pPostID & "/revisions/" & pRevisionID into tAPICall
put "/?access_token=" & tAccessToken after tAPICall
-- Get post URL
put sWPAPI["post_url"] into tPostURL
-- Get all posts list
get URL(tPostURL & tAPICall)
put it into tJsonData
put JSONToArray(tJsonData) into tArray
return tArray
end postRevisionGetByID
-------------------------------------------------------------------------------------------------------------
-- postRevisionDelete
-- DESCRIPTION:
-- This function deletes post's revision
-- PARAMETERS:
-- pPostID: Post ID whcih revisions we want to delete
-- pRevisionID: Revision id which we want to delete
-- CHANGES:
-- 25/04/2016 Narek: Created function
-------------------------------------------------------------------------------------------------------------
function postRevisionDelete pPostID, pRevisionID
accessTokenCheck
--Check for required parameters
if pPostID is empty then
return "Error: PostID is required"
end if
if pRevisionID is empty then
return "Error: RevisionID is required"
end if
-- Getting access token for authentification
put sAccessToken["access_token"] into tAccessToken
put "posts/" & pPostID & "/revisions/" & pRevisionID into tAPICall
put "/?access_token=" & tAccessToken after tAPICall
-- Get post URL
put sWPAPI["post_url"] into tPostURL
-- Get all posts list
delete URL(tPostURL & tAPICall)
put the result into tResult
return tResult
end postRevisionDelete
-------------------------------------------------------------------------------------------------------------
-- postMetaCreate
-- DESCRIPTION:
-- This function creates new meta for post
-- PARAMETERS:
-- pPostID: Post id for whose we want to create meta
-- pMetaData: Creating meta's data
-- CHANGES:
-- 25/04/2016 Narek: Created function
-------------------------------------------------------------------------------------------------------------
function postMetaCreate pPostID, pMetaData
accessTokenCheck
--Checking and Buidling Parameters
if pPostID is empty then
return "Post ID is required"
end if
if pMetaData["key"] is empty then
return "Error: Key is required"
end if
if pMetaData["value"] is empty then
return "Error: Value is required"
end if
-- Setting the http headers
put "Content-Type: application/json" & cr after tHeader
set the httpheaders to tHeader
libURLSetSSLVerification false
-- Getting access token for authentification
put sAccessToken["access_token"] into tAccessToken
-- Convert passed array to Json for posting
put ArrayToJSON(pMetaData) into tPostJson
-- Create the URL used to call the REST service
put "posts/" & pPostID & "/meta" into tAPICall
put "/?access_token=" & tAccessToken after tAPICall
put sWPAPI["post_url"] into tPostURL
-- Posting for creating the post
post tPostJson to URL(tPostURL & tAPICall)
put it into tResponse
if tResponse is empty then
return false
end if
return tResponse
end postMetaCreate
-------------------------------------------------------------------------------------------------------------
-- postMetaGetByID
-- DESCRIPTION:
-- This function get meta for post by id
-- PARAMETERS:
-- pPostID: Post id for which meta(s) we want to get
-- pMetaID: Meta id which we want to get
-- CHANGES:
-- 25/04/2016 Narek: Created function
-------------------------------------------------------------------------------------------------------------
function postMetaGetByID pPostID, pMetaID
accessTokenCheck
--Check for required parameters
if pPostID is empty then
return "Error: PostId is required"
end if
-- Bulild api call
put "posts/" & pPostID & "/meta/" into tAPICall
-- If we don't have meta id then we get all metas of chosen post
-- in other case we get specific meta
if pMetaID is not empty then
put pMetaID & "/" after tAPICall
end if
-- Getting access token for authentification
put sAccessToken["access_token"] into tAccessToken
put "?access_token=" & tAccessToken after tAPICall
put sWPAPI["post_url"] into tPostURL
get URL (tPostURL & tAPICall)
put it into tMetaJson
end postMetaGetByID
-------------------------------------------------------------------------------------------------------------
-- postMetaGetByID
-- DESCRIPTION:
-- This function edit meta for post
-- PARAMETERS:
-- pPostID: Post id for which meta we want to edit
-- pMetaID: Meta id which we want to edit
-- pMetaData: Editing meta's data
-- CHANGES:
-- 25/04/2016 Narek: Created function
-------------------------------------------------------------------------------------------------------------
function postMetaEdit pPostID, pMetaID, pMetaData
accessTokenCheck
--Checking and Buidling Parameters
if pMetaData["key"] is empty then
return "Error: Key is required"
end if
if pMetaData["value"] is empty then
return "Error: Value is required"
end if
-- Setting the http headers
put "Content-Type: application/json" & cr after tHeader
set the httpheaders to tHeader
libURLSetSSLVerification false
-- Getting access token for authentification
put sAccessToken["access_token"] into tAccessToken
-- Bulild api call
put "posts/" & pPostID & "/meta/" & pMetaID into tAPICall
put "/?access_token=" & tAccessToken after tAPICall
put sWPAPI["post_url"] into tPostURL
put ArrayToJSON(pMetaData) into tPostJson
post tPostJson to URL(tPostURL & tAPICall)
put it into tResults
if tResults is empty then
return 0
end if
return tResults
end postMetaEdit
-------------------------------------------------------------------------------------------------------------
-- postMetaDelete
-- DESCRIPTION:
-- This function creates new meta for post
-- PARAMETERS:
-- pPostID: Post id for which meta we want to edit
-- pMetaID: Meta id which we want to edit
-- CHANGES:
-- 25/04/2016 Narek: Created function
-------------------------------------------------------------------------------------------------------------
function postMetaDelete pPostID, pMetaID
accessTokenCheck
--Checking and Buidling Parameters
if pPostID is empty then
return "Error: PostID is required"
end if
if pMetaID is empty then
return "Error: MetaID is required"
end if
-- Setting the http headers
put "Content-Type: application/json" & cr after tHeader
set the httpheaders to tHeader
libURLSetSSLVerification false
-- Getting access token for authentification
put sAccessToken["access_token"] into tAccessToken
-- Bulild api call
put "posts/" & pPostID & "/meta/" & pMetaID into tAPICall
put "/?access_token=" & tAccessToken after tAPICall
put sWPAPI["post_url"] into tPostURL
-- Deleting meta
delete URL (tPostURL & tAPICall)
put it into tResults
if tResults is empty then
return 0
end if
return tResults
end postMetaDelete
-------------------------------------------------------------------------------------------------------------
-- taxonomiesGetList
-- DESCRIPTION:
-- This function returns all taxonomies data
-- PARAMETERS:
-- none:
-- CHANGES:
-- 25/04/2016 Narek: Created function
-------------------------------------------------------------------------------------------------------------
function taxonomiesGetList
-- Create the URL used to call the REST service
put "taxonomies" into tAPICall
put sWPAPI["post_url"] into tPostURL
-- Get all taxonomies list
get URL(tPostURL & tAPICall)
put it into tJsonData
put JSONToArray(tJsonData) into tTaxonomiesList
return tTaxonomiesList
end taxonomiesGetList
-------------------------------------------------------------------------------------------------------------
-- taxonomyGetByID
-- DESCRIPTION:
-- This function returns taxonomy data by ID
-- PARAMETERS:
-- pTaxonomyID: Taxonomy which we want to get
-- CHANGES:
-- 25/04/2016 Narek: Created function
-------------------------------------------------------------------------------------------------------------
function taxonomyGetByID pTaxonomyID
put sWPAPI["post_url"] into tPostURL
-- Create the URL used to call the REST service
put "taxonomies/" into tAPICall
put pTaxonomyID after tAPICall
-- Get all taxonomies list
get URL(tPostURL & tAPICall)
put it into tTaxJsonData
put JSONToArray(tTaxJsonData) into tTaxData
return tTaxData
end taxonomyGetByID
-------------------------------------------------------------------------------------------------------------
-- userCreate
-- DESCRIPTION:
-- This function creates new user
-- PARAMETERS:
-- pUserData: Creating user data
-- CHANGES:
-- 25/04/2016 Narek: Created function
-------------------------------------------------------------------------------------------------------------
function userCreate pUserData
accessTokenCheck
--Check for required parameters
if pUserData["username"] is empty then
return "Error: Username is required"
end if
if pUserData["email"] is empty then
return "Error: Email is required"
end if
if pUserData["password"] is empty then
return "Error: Password is required"
end if
-- Setting the http headers
put "Content-Type: application/json" & cr after tHeader
set the httpheaders to tHeader
libURLSetSSLVerification false
-- Getting access token for authentification
put sAccessToken["access_token"] into tAccessToken
-- Convert passed array to Json for posting
put ArrayToJSON(pUserData) into tPostJson
-- Create the URL used to call the REST service
put "users" into tAPICall
put "/?access_token=" & tAccessToken after tAPICall
put sWPAPI["post_url"] into tPostURL
-- Posting for creating the post
post tPostJson to URL(tPostURL & tAPICall)
put it into tResults
put JSONToArray(tResults) into tResults
if tResults is empty then
return false
end if
return tResults
end userCreate
-------------------------------------------------------------------------------------------------------------
-- userEdit
-- DESCRIPTION:
-- This function edit user
-- PARAMETERS:
-- pUserID: User id which we want to edit
-- pUserData: Editing user data
-- CHANGES:
-- 25/04/2016 Narek: Created function
-------------------------------------------------------------------------------------------------------------
function userEdit pUserID pUserData
accessTokenCheck
--Checking and Buidling Parameters
if pUserData is empty then
return "Error: data is required" //username is required
end if