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

Tools: Merge existing content in when syncing from live site. #22

Merged
merged 1 commit into from
Jul 27, 2022
Merged
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
33 changes: 25 additions & 8 deletions env/import-content.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/php
<?php
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped

namespace WordPress_org\Parent_2021\ImportTestContent;
namespace WordPress_org\Main_2022\ImportTestContent;

/**
* CLI script for generating local test content, fetched from the live learn.wordpress.org site.
Expand All @@ -25,8 +26,8 @@
die( 'Not safe to run on ' . esc_html( get_site_url() ) );
}

if ( empty( $opts[ 'url' ] ) || $opts[ 'url' ] !== esc_url_raw( $opts[ 'url' ], ['https'] ) ) {
die( 'Invalid url parameter ' . esc_html( $opts[ 'url' ] ) );
if ( empty( $opts['url'] ) || esc_url_raw( $opts['url'], [ 'https' ] ) !== $opts['url'] ) {
die( 'Invalid url parameter ' . esc_html( $opts['url'] ) );
}

/**
Expand Down Expand Up @@ -64,10 +65,10 @@ function import_rest_to_posts( $rest_url ) {
$data = json_decode( $body );

foreach ( $data as $post ) {
echo esc_html( "Got {$post->type} {$post->id} {$post->slug}\n" );
echo "Got {$post->type} {$post->id} {$post->slug}\n";

// Surely there's a neater way to do this.
$newpost = array(
$new_post = array(
'import_id' => $post->id,
'post_date' => gmdate( 'Y-m-d H:i:s', strtotime( $post->date ) ),
'post_name' => $post->slug,
Expand All @@ -81,14 +82,30 @@ function import_rest_to_posts( $rest_url ) {
'meta_input' => sanitize_meta_input( $post->meta ),
);

$new_post_id = wp_insert_post( $newpost, true );
$existing_post = get_post( $post->id, ARRAY_A );

if ( $existing_post ) {
$new_post = array_merge( $existing_post, $new_post );
printf(
"Updating %s [%s]\n",
html_entity_decode( $post->title->rendered ),
$existing_post['ID']
);
} else {
printf(
"Creating %s\n",
html_entity_decode( $post->title->rendered )
);
}

$new_post_id = wp_insert_post( $new_post, true );

if ( is_wp_error( $new_post_id ) ) {
die( esc_html( $new_post_id->get_error_message() ) );
}

echo esc_html( "Inserted $post->type $post->id as $new_post_id\n" );
echo "Inserted $post->type $post->id as $new_post_id\n\n";
}
}

import_rest_to_posts( $opts['url'] );
import_rest_to_posts( $opts['url'] );