
WordPress.com: 記事公開
この工程は、WordPress.com で作成済みの下書き状態の記事(投稿)を公開します。公開日時を指定しない場合、即座に公開されます。
Basic Configs
- 工程名
- メモ
Configs for this Auto Step
- conf_Auth
- C1: OAuth2 設定 *
- conf_Domain
- C2: WordPress.com サイトのドメイン *
- conf_PostId
- C3: 投稿 ID *
- conf_Date
- C4: 公開日時(未来の日時を指定した場合、公開が予約されます)
Notes
- WordPress.com(Automattic 社の提供する、WordPress サイトをホストするサービス)で使用できる自動工程です
- OAuth2 設定は、1 ユーザ 1 サイトにつき、1 つ作成してください
- 同じユーザが複数の WordPress.com サイトを扱う場合、それぞれのサイトについて OAuth2 設定を作成してください
Capture

See Also
Script (click to open)
- 次のスクリプトが記述されている XML ファイルをダウンロードできます
- wordpress-post-publish.xml (C) Questetra, Inc. (MIT License)
- Professional のワークフロー基盤では、ファイル内容を改変しオリジナルのアドオン自動工程として活用できます
// OAuth2 config sample at [OAuth 2.0 Setting]
// - Authorization Endpoint URL: https://public-api.wordpress.com/oauth2/authorize
// - Token Endpoint URL: https://public-api.wordpress.com/oauth2/token
// - Scope: posts
// - Client ID: (Get by https://developer.wordpress.com/apps/)
// - Client Secret: (Get by https://developer.wordpress.com/apps/)
const API_VERSION = 'v1.1';
const DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssX";
const main = () => {
const auth = configs.getObject('conf_Auth');
const domain = configs.get('conf_Domain');
const postId = retrievePostId();
const date = retrieveDate();
checkPost(auth, domain, postId);
publishPost(auth, domain, postId, date);
};
/**
* config から投稿 ID を読み出す
* 空の場合、数字以外の文字が含まれる場合はエラー
* @returns {String} postId
*/
const retrievePostId = () => {
const postId = engine.findData(configs.getObject('conf_PostId'));
if (postId === null) {
throw 'Post ID is blank.';
}
const idReg = new RegExp('^[0-9]+$');
if (!idReg.test(postId)) {
throw 'Post ID must be a non-negative integer.';
}
return postId;
};
/**
* config から公開日時を文字列として読み出す
* データ項目が選択されていない場合、空文字列を返す
* データ項目が選択されていて値が空の場合は、エラー
* @returns {String} date
*/
const retrieveDate = () => {
const dateDef = configs.getObject('conf_Date');
if (dateDef === null) {
return '';
}
const datetimeObj = engine.findData(dateDef);
if (datetimeObj === null) {
throw 'Publish Datetime is selected but its data is null.';
}
return dateFormatter.format(DATETIME_FORMAT, datetimeObj);
};
/**
* 投稿のステータスを確認し、下書き状態でない場合はエラー
* @param {AuthSettingWrapper} auth
* @param {String} domain
* @param {String} postId
* @returns {any}
*/
const checkPost = (auth, domain, postId) => {
const url = `https://public-api.wordpress.com/rest/${API_VERSION}/sites/${encodeURIComponent(domain)}/posts/${postId}`;
const response = httpClient.begin().authSetting(auth)
.queryParam('fields', 'status')
.get(url);
const status = response.getStatusCode();
const responseStr = response.getResponseAsString();
if (status !== 200) {
engine.log(responseStr);
throw `Failed to get post. status: ${status}`;
}
const postStatus = JSON.parse(responseStr).status;
if (postStatus !== 'draft') {
throw `The status of the post is not draft but ${postStatus}.`;
}
};
/**
* 投稿を公開 / 公開予約する
* @param {AuthSettingWrapper} auth
* @param {String} domain
* @param {String} postId
* @param {String} date
* @returns {any}
*/
const publishPost = (auth, domain, postId, date) => {
const url = `https://public-api.wordpress.com/rest/${API_VERSION}/sites/${encodeURIComponent(domain)}/posts/${postId}`;
const response = httpClient.begin().authSetting(auth)
.queryParam('fields', 'URL,status,date')
.formParam('status', 'publish')
.formParam('date', date)
.post(url);
const status = response.getStatusCode();
const responseStr = response.getResponseAsString();
if (status !== 200) {
engine.log(responseStr);
throw `Failed to publish post. status: ${status}`;
}
engine.log(`Publish request succeeded. ${responseStr}`);
};