WordPress.Com: Posts New

WordPress.Com: Posts New
Creates a post as a draft. Content is stored as a draft HTML of POST type. If you want to dynamically change the WORDPRESS DOMAIN using the EL expression, set Scope to “global”.
https://support.questetra.com/addons/wordpress-com-media-new/
2019-01-09 (C) Questetra, Inc. (MIT License)
Configs
  • A: Set OAuth2 Config Name (at [OAuth 2.0 Setting]) *
  • B: Set WORDPRESS DOMAIN to Create Post (e.g. example.com or ID) * #{EL}
  • C: Select DATETIME or DATE DATA for Creation Time
  • D: Select STRING DATA for Post Title
  • E: Select STRING DATA for Post Slug
  • F: Select STRING DATA for Post Excerpt
  • G: Select STRING DATA for Post Content
  • H: Select STRING/NUMERIC DATA for Post ID (update)
  • I: Select STRING DATA for Post URL (update)
Script
// (c) 2018, Questetra, Inc. (the MIT License)

//// == OAuth2 Setting example ==
// Authorization Endpoint URL:
//  "https://public-api.wordpress.com/oauth2/authorize"
// Token Endpoint URL:
//  "https://public-api.wordpress.com/oauth2/token"
// Scope: "global" (or "auth")
// Client ID: ( from https://developer.wordpress.com/apps/ )
// Consumer Secret: ( from https://developer.wordpress.com/apps/ )
// -- "Redirect URLs": "https://s.questetra.net/oauth2callback"
// -more-
// https://developer.wordpress.com/docs/oauth2/
// https://developer.wordpress.com/docs/api/1.2/post/sites/%24site/posts/new/


//////// START "main()" ////////
main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const oauth2   = configs.get( "conf_OAuth2"  ) + "";
const wpDomain = configs.get( "conf_WpDomain") + "";
const dataIdC  = configs.get( "conf_DataIdC" ) + "";
const dataIdD  = configs.get( "conf_DataIdD" ) + "";
const dataIdE  = configs.get( "conf_DataIdE" ) + "";
const dataIdF  = configs.get( "conf_DataIdF" ) + "";
const dataIdG  = configs.get( "conf_DataIdG" ) + "";
const dataIdH  = configs.get( "conf_DataIdH" ) + "";
const dataIdI  = configs.get( "conf_DataIdI" ) + "";
// 'java.lang.String' (String Obj) to javascript primitive 'string'
if(wpDomain === ""){
  throw new Error( '\n WordPress Domain not Defined. \n' );
}


//// == Data Retrieving / ワークフローデータの参照 ==
let postTime = "";
if( dataIdC !== "" ){
  const offsetMin = engine.getTimeZoneOffsetInMinutes() - 0; // "540" "-480" etc
  const absOffset = Math.abs( offsetMin );
  const timeHour  = Math.floor( absOffset / 60 ); // e.g. "9" "-8" etc
  const timeMin   = absOffset - timeHour * 60; // e.g. "0" "30" etc
  let timeZoneStr = ""; // "+09:00"
  if( offsetMin >= 0 ){
    timeZoneStr   = "+" + ( "0" + timeHour ).slice(-2) + ":" + ( "0" + timeMin ).slice(-2);
  }else{
    timeZoneStr   = "-" + ( "0" + timeHour ).slice(-2) + ":" + ( "0" + timeMin ).slice(-2);
  } // ISO 8601, Zero Padding
  if( engine.findDataDefinitionByNumber( dataIdC ).matchDataType( "DATETIME" ) ){
    postTime   = engine.findDataByNumber( dataIdC ) + ""; // e.g. "1970-01-01 01:23"
    postTime   = postTime.replace( " ", "T" );        // e.g. "1970-01-01T01:23"
    postTime   = postTime + timeZoneStr;            // e.g. "1970-01-01T01:23+09:00"
  }else{
    postTime   = engine.findDataByNumber( dataIdC ) + ""; // e.g. "1970-01-01"
    postTime   = postTime + "T08:55";               // e.g. "1970-01-01T08:55"
    postTime   = postTime + timeZoneStr;            // e.g. "1970-01-01T08:55+09:00"
  }
}
let postTitle  = "";
if( dataIdD  !== "" ){
    postTitle  = engine.findDataByNumber( dataIdD ) + "";
}
let postSlug   = "";
if( dataIdE  !== "" ){
    postSlug   = engine.findDataByNumber( dataIdE ) + "";
}
let postExcerpt = "";
if( dataIdF   !== "" ){
    postExcerpt = engine.findDataByNumber( dataIdF ) + "";
}
let postContent = "";
if( dataIdG   !== "" ){
    postContent = engine.findDataByNumber( dataIdG ) + "";
}
if(postTitle === "" && postSlug === "" && postContent === "" ){
  throw new Error( '\n Title, Slug or Content is either required. \n' );
}


//// == Calculating / 演算 ==
/// obtain OAuth2 Access Token
const token   = httpClient.getOAuth2Token( oauth2 );

/// prepare API Request
let apiRequest = httpClient.begin(); // HttpRequestWrapper
// - com.questetra.bpms.core.event.scripttask.HttpClientWrapper
apiRequest     = apiRequest.bearer( token );
apiRequest     = apiRequest.formParam( "status", "draft" );
if( postTime !== ""){
  apiRequest   = apiRequest.formParam( "date", postTime );
}
if( postTitle !== ""){
  apiRequest   = apiRequest.formParam( "title", postTitle );
}
if( postSlug !== ""){
  apiRequest   = apiRequest.formParam( "slug", postSlug );
}
if( postExcerpt !== ""){
  apiRequest   = apiRequest.formParam( "excerpt", postExcerpt );
}
if( postContent !== ""){
  apiRequest   = apiRequest.formParam( "content", postContent );
}

/// access API (with post(), get(), put(), etc.)
const apiUri = "https://public-api.wordpress.com/rest/v1.2/sites/"
               + wpDomain + "/posts/new";
const response = apiRequest.post( apiUri );
// - HttpResponseWrapper
const httpStatus  = response.getStatusCode() + "";
engine.log( "STATUS: " + httpStatus );
const responseStr = response.getResponseAsString() + "";
engine.log( "RESPONSE: \n" + responseStr );

/// parse Response-JSON
const responseObj = JSON.parse( responseStr );


//// == Data Updating / ワークフローデータへの代入 ==
if( dataIdH   !== "" ){
  if( engine.findDataDefinitionByNumber( dataIdH ).matchDataType( "DECIMAL" ) ){
    engine.setDataByNumber( dataIdH, new java.math.BigDecimal(responseObj.ID) );
  }else{
    engine.setDataByNumber( dataIdH, responseObj.ID );
  }
}
if( dataIdI   !== "" ){
  engine.setDataByNumber( dataIdI, responseObj.URL );
}

} //////// END "main()" ////////

Download

WordPress-Com-Posts-New.xml

Capture

Notes

  1. The Gutenberg code is recommended.
<!-- wp:heading -->
<h2>Section Title</h2>
<!-- /wp:heading -->

See also

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading