WordPress.com: Get Tags

Basic Configs
Step Name
Note
Auto Step icon
Configs for this Auto Step
conf_Domain
C1: Domain of the WordPress.com site *
conf_Names
C2: Data item that stores / to save tag names *
conf_Ids
C3: Data item to save tag IDs *

Notes

  • This Auto Step is for WordPress.com (a service that hosts WordPress sites, provided by Automattic itself)

Capture

See also

Script (click to open)
  • An XML file that contains the code below is available to download
    • wordpress-tag-get.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on auto step

// This item requires no authentication.

const API_VERSION = 'v1.1';
const MAX_NUMBER = 1000; // 一度のリクエストで取得できる最大件数

const main = () => {
    ////// == 工程コンフィグ・ワークフローデータの参照 / Config & Data Retrieving ==
    const domain = configs.get('conf_Domain');
    const names = retrieveNamesAsList();

    ////// == 演算 / Calculating ==
    let tags = getTags(domain);
    if (names.length > 0) { // 名前が指定されている場合は、その名前のタグのみに絞る
        tags = names.map(name => {
            const tag = tags.find(item => item.name === name);
            if (tag === undefined) {
                throw new Error(`Tag not found: ${name}`);
            }
            return tag;
        });
    }

    ////// == ワークフローデータへの代入 / Data Updating ==
    if (names.length === 0) {
        saveData('conf_Names', tags.map(item => item.name).join('\n'));
    }
    saveData('conf_Ids', tags.map(item => item.ID).join('\n'));
};

/**
  * config に設定された名前の一覧を読み出す
  * @return {Array<String>} ids
  */
const retrieveNamesAsList = () => {
    const dataDef = configs.getObject('conf_Names');
    let dataStr = engine.findData(dataDef);
    if (dataStr === null) {
        return [];
    }
    // 末尾に改行がある場合、削除
    if (dataStr.endsWith('\n')) {
        dataStr = dataStr.slice(0, -1);
    }
    const dataArray = dataStr.split('\n');
    // 残りの配列に空文字が含まれる場合、エラー
    if (dataArray.some(item => item === '')) {
        throw new Error('Empty lines are not allowed for tag names.');
    }
    return dataArray;
};

/**
 * タグ一覧を取得する
 * 一度のリクエストで取得しきれなかった場合、エラー
 * @param {String} domain
 * @return {Array<Object>} tags
 */
const getTags = (domain) => {
    const url = `https://public-api.wordpress.com/rest/${API_VERSION}/sites/${encodeURIComponent(domain)}/tags`;
    const response = httpClient.begin()
        .queryParam('fields', 'ID,name')
        .queryParam('number', MAX_NUMBER.toString())
        .get(url);
    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();
    if (status !== 200) {
        engine.log(responseStr);
        throw new Error(`Failed to get tags. status: ${status}`);
    }
    const json = JSON.parse(responseStr);
    if (json.found > MAX_NUMBER) {
        throw new Error('Too many tags.');
    }
    return json.tags;
};

/**
 * データ項目への保存
 * @param configName
 * @param data
 */
const saveData = (configName, data) => {
    const def = configs.getObject(configName);
    engine.setData(def, data);
};

    

Discover more from Questetra Support

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

Continue reading