Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WP CLI bash script to migrate tutorials to lessons #2911

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions bin/wp-cli-tutorial-to-lesson-conversion-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/bin/bash

# Function to display usage information
usage() {
echo "Usage: $0 [-d] (-u <post_url> | -f <file_path>)"
echo " -d Dry run mode (don't make any changes)"
echo " -u Single post URL"
echo " -f File containing list of URLs (one per line)"
exit 1
}

# Function to check if a command was successful
check_success() {
if [ $? -ne 0 ]; then
echo "Error: $1"
return 1
fi
return 0
}

# Function to process a single URL
process_url() {
local post_url=$1
local dry_run=$2

# Extract post ID from URL
post_id=$(wp post list --post_type=wporg_workshop --field=ID --format=ids --search="$post_url")
if ! check_success "Failed to find post with URL: $post_url"; then
return 1
fi

echo "Found post with ID: $post_id"

# Change post type directly
if [ "$dry_run" = true ]; then
echo "Dry run: Would change post type of post (ID: $post_id) to 'lesson'"
else
wp post update $post_id --post_type=lesson
if ! check_success "Failed to change post type to lesson"; then
return 1
fi
echo "Changed post type of post (ID: $post_id) to 'lesson'"
fi

echo "Processing completed for URL: $post_url"
return 0
}

# Parse command line arguments
dry_run=false
url_mode=""
while getopts ":du:f:" opt; do
case ${opt} in
d )
dry_run=true
;;
u )
url_mode="single"
post_url=$OPTARG
;;
f )
url_mode="file"
file_path=$OPTARG
;;
\? )
usage
;;
esac
done

# Check if a URL or file was provided
if [ -z "$url_mode" ]; then
usage
fi

# Process URLs
if [ "$url_mode" = "single" ]; then
process_url "$post_url" "$dry_run"
elif [ "$url_mode" = "file" ]; then
if [ ! -f "$file_path" ]; then
echo "Error: File not found: $file_path"
exit 1
fi

while IFS= read -r url || [[ -n "$url" ]]; do
process_url "$url" "$dry_run"
done < "$file_path"
fi

echo "All operations completed"
111 changes: 111 additions & 0 deletions bin/wp-cli-tutorial-to-new-lesson-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/bin/bash

# Function to display usage information
usage() {
echo "Usage: $0 [-d] (-u <post_url> | -f <file_path>)"
echo " -d Dry run mode (don't make any changes)"
echo " -u Single post URL"
echo " -f File containing list of URLs (one per line)"
exit 1
}

# Function to check if a command was successful
check_success() {
if [ $? -ne 0 ]; then
echo "Error: $1"
return 1
fi
return 0
}

# Function to process a single URL
process_url() {
local post_url=$1
local dry_run=$2

# Extract post ID from URL
post_id=$(wp post list --post_type=wporg_workshop --field=ID --format=ids --search="$post_url")
if ! check_success "Failed to find post with URL: $post_url"; then
return 1
fi

echo "Found post with ID: $post_id"

# Get post details
post_title=$(wp post get $post_id --field=post_title)
post_name=$(wp post get $post_id --field=post_name)
post_date=$(wp post get $post_id --field=post_date)
post_content=$(wp post get $post_id --field=post_content)

if ! check_success "Failed to get post details"; then
return 1
fi

# Create new lesson post
if [ "$dry_run" = true ]; then
echo "Dry run: Would create new 'lesson' post with title: $post_title"
else
new_post_id=$(wp post create --post_type=lesson --post_status=publish --post_title="$post_title" --post_name="$post_name" --post_date="$post_date" --post_content="$post_content" --porcelain)
if ! check_success "Failed to create new lesson post"; then
return 1
fi
echo "Created new lesson post with ID: $new_post_id"
fi

# Set original post to draft
if [ "$dry_run" = true ]; then
echo "Dry run: Would set original post (ID: $post_id) to draft"
else
wp post update $post_id --post_status=draft
if ! check_success "Failed to set original post to draft"; then
return 1
fi
echo "Set original post (ID: $post_id) to draft"
fi

echo "Processing completed for URL: $post_url"
return 0
}

# Parse command line arguments
dry_run=false
url_mode=""
while getopts ":du:f:" opt; do
case ${opt} in
d )
dry_run=true
;;
u )
url_mode="single"
post_url=$OPTARG
;;
f )
url_mode="file"
file_path=$OPTARG
;;
\? )
usage
;;
esac
done

# Check if a URL or file was provided
if [ -z "$url_mode" ]; then
usage
fi

# Process URLs
if [ "$url_mode" = "single" ]; then
process_url "$post_url" "$dry_run"
elif [ "$url_mode" = "file" ]; then
if [ ! -f "$file_path" ]; then
echo "Error: File not found: $file_path"
exit 1
fi

while IFS= read -r url || [[ -n "$url" ]]; do
process_url "$url" "$dry_run"
done < "$file_path"
fi

echo "All operations completed"