Created
November 18, 2022 19:29
-
-
Save sfboss/ccd3f4169d305e84445583b71356a525 to your computer and use it in GitHub Desktop.
A method collection for Wordpress REST API calls via Salesforce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public with sharing class seoBoss_API_WordpressService{ | |
| public static String username = 'replace_with_username'; | |
| public static String password = 'replace_with_password'; | |
| public static Blob headerValue = Blob.valueOf(username + ':' + password); | |
| public static String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue); | |
| public static Map<String, String> headers = new Map<String, String>{ 'Authorization' => authorizationHeader }; | |
| public static String theSiteURL = 'https://sfdcboss.com/index.php/wp-json/wp/v2/'; | |
| public static String saveMediaInLibrary(String imageURL, String keyword){ | |
| String theExtension = imageURL.right(3); | |
| Blob theImage = GoogleAPI_Tenor.getImageBody(imageURL); | |
| String theResults = saveMediaInLibrary(theImage,theExtension,keyword); | |
| return theResults; | |
| } | |
| public static String saveMediaInLibrary(Blob theImage, String theExtension, String keyword){ | |
| String endpointURL = theSiteURL + 'media'; | |
| headers.put('Content-Disposition','attachment; filename='+keyword.replaceAll(' ','_') + '.'+theExtension); | |
| headers.put('Content-Type','image/'+theExtension); | |
| Map<String,String> theResults = doHTTP(headers,endpointURL,theExtension,theImage); | |
| return theResults.get('response-body'); | |
| } | |
| static Map<String,String> doHTTP(Map<String, String> headers, String endpointURL, String theExtension, Blob theBody){ | |
| Http http = new Http(); | |
| HttpRequest httpReq = new HttpRequest(); | |
| HttpResponse httpRes = new HttpResponse(); | |
| httpReq.setHeader('User-Agent', 'seoboss'); | |
| for(String s : headers.keySet()) httpReq.setHeader(s,headers.get(s)); | |
| httpReq.setMethod('POST'); | |
| httpReq.setTimeout(120000); | |
| httpReq.setEndpoint(endpointURL); | |
| if (theBody != null) httpReq.setBodyAsBlob(theBody); | |
| httpRes = new Http().send(httpReq); | |
| return new Map<String,String>{'response-code'=>String.valueOf(httpRes.getStatusCode()),'response-body'=>httpRes.getBody()}; | |
| } | |
| @AuraEnabled | |
| public static String makeWordPressContent(String theSiteURL, String title, String content, String wptype){ | |
| String endpoint = wptype == 'Page' ? 'https://sfdcboss.com/index.php/wp-json/wp/v2/pages' : wptype == 'Post' ? 'https://sfdcboss.com/index.php/wp-json/wp/v2/posts' : ''; | |
| Http h = new Http(); | |
| HttpRequest request = new HttpRequest(); | |
| String username = 'replace_with_username'; | |
| String password = 'replace_with_password'; | |
| Blob headerValue = Blob.valueOf(username + ':' + password); | |
| String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue); | |
| request.setHeader('Authorization', authorizationHeader); | |
| request.setHeader('Content-Type', 'application/x-www-form-urlencoded'); | |
| request.setMethod('POST'); | |
| request.setEndpoint(endpoint); | |
| request.setBody('title=' + title + '&content=' + content); | |
| Http http = new Http(); | |
| HttpResponse response; | |
| String accessToken; | |
| response = http.send(request); | |
| return response.getBody(); | |
| } | |
| } | |
| //seoBoss_Api_WordpressService.getPosts(''); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment