diff --git a/README.md b/README.md index fafaecd..005b4a0 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Copy the posting script to e.g. `/usr/local/bin/wordpress-post` so that you can * `--file PATH` — use PATH as the post content instead of opening `$EDITOR`. * `--publish` — shortcut for `STATUS=publish`, posts live immediately. * `--edit POST_ID` — update an existing post instead of creating a new one. +* `--date DATE` — backdate the post to DATE, an ISO 8601 UTC timestamp (e.g. `2026-07-27T10:00:00Z`, matching a Gitea commit date) — sent as the post's `date_gmt`. ## Testing `tests/run_tests.sh` runs post.sh against a local mock of the WordPress REST diff --git a/post.sh b/post.sh index aafb5af..62ed494 100755 --- a/post.sh +++ b/post.sh @@ -22,10 +22,14 @@ CONFIG_DIR="${WORDPRESS_REST_CURL_CONFIG_DIR:-$HOME/.config/wordpress-rest-curl} # --edit POST_ID update an existing post instead of creating a new one # --publish shortcut for STATUS=publish # --file PATH use PATH as the post source instead of opening $EDITOR +# --date DATE backdate the post to DATE, an ISO 8601 UTC timestamp +# (e.g. 2026-07-27T10:00:00Z, matching a Gitea commit +# date) — sent as the post's date_gmt SITE="" POST_ID="" SOURCE_FILE="" PUBLISH=0 +DATE="" while [[ $# -gt 0 ]]; do case "$1" in --site) @@ -36,6 +40,8 @@ while [[ $# -gt 0 ]]; do PUBLISH=1; shift ;; --file) SOURCE_FILE="$2"; shift 2 ;; + --date) + DATE="$2"; shift 2 ;; *) echo "Unknown argument: $1" >&2 exit 1 ;; @@ -104,6 +110,9 @@ echo "Server: $SERVER" echo "Status: $STATUS" echo "Categories: $CATEGORIES" echo "Tags: $TAGS" +if [[ -n "$DATE" ]]; then + echo "Date: $DATE" +fi if [[ -n "$POST_ID" ]]; then echo "Editing post: $POST_ID" fi @@ -126,7 +135,9 @@ PAYLOAD=$(jq -n \ --arg status "$STATUS" \ --argjson categories "$CATEGORIES_JSON" \ --argjson tags "$TAGS_JSON" \ - '{title: $title, content: $content, status: $status, categories: $categories, tags: $tags}') + --arg date_gmt "${DATE%Z}" \ + '{title: $title, content: $content, status: $status, categories: $categories, tags: $tags} + + (if $date_gmt != "" then {date_gmt: $date_gmt} else {} end)') BASE_URL="$SERVER" [[ "$BASE_URL" != http://* && "$BASE_URL" != https://* ]] && BASE_URL="https://$BASE_URL" diff --git a/tests/run_tests.sh b/tests/run_tests.sh index df569e7..586cb42 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -97,6 +97,18 @@ assert_eq "trailing slash: exit code" "0" "$?" REQ=$(cat "$MOCK_WP_REQUEST_FILE") assert_eq "trailing slash: path has no double slash" "/wp-json/wp/v2/posts/" "$(jq -r '.path' <<<"$REQ")" +# --- --date backdates the post via date_gmt --- +rm -f "$MOCK_WP_REQUEST_FILE" "$MOCK_WP_CONTROL_FILE" +run_post_sh --file "$POST_FILE" --date "2026-07-27T10:00:00Z" >/dev/null +assert_eq "date: exit code" "0" "$?" +REQ=$(cat "$MOCK_WP_REQUEST_FILE") +assert_eq "date: date_gmt sent, trailing Z stripped" "2026-07-27T10:00:00" "$(jq -r '.payload.date_gmt' <<<"$REQ")" + +rm -f "$MOCK_WP_REQUEST_FILE" "$MOCK_WP_CONTROL_FILE" +run_post_sh --file "$POST_FILE" >/dev/null +REQ=$(cat "$MOCK_WP_REQUEST_FILE") +assert_eq "no date: date_gmt omitted" "true" "$(jq -r '.payload | has("date_gmt") | not' <<<"$REQ")" + # --- edit an existing post --- rm -f "$MOCK_WP_REQUEST_FILE" "$MOCK_WP_CONTROL_FILE" run_post_sh --file "$POST_FILE" --edit 99 >/dev/null