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 setup script and filter to import content from remote REST API #18

Merged
merged 2 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add setup script and filter to import content from remote REST API
  • Loading branch information
tellyworth committed Jul 26, 2022
commit 05d385ec27afc21b6e0d52ee44200637abfb8217
94 changes: 94 additions & 0 deletions env/import-content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/php
<?php

namespace WordPress_org\Parent_2021\ImportTestContent;

/**
* CLI script for generating local test content, fetched from the live learn.wordpress.org site.
*
* This needs to be run in a wp-env, for example:
*
* yarn run wp-env run cli "php bin/import-test-content.php"
*/

// This script should only be called in a CLI environment.
if ( 'cli' != php_sapi_name() ) {
die();
}


$opts = getopt( '', array( 'url:' ) );

require dirname( dirname( __FILE__ ) ) . '/wp-load.php';

if ( 'local' !== wp_get_environment_type() ) {
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' ] ) );
}

/**
* Sanitize postmeta from the rest API for the format required by wp_insert_post.
*
* @return array An array suitable for meta_input.
*/
function sanitize_meta_input( $meta ) {
$meta = array( $meta );
foreach ( $meta as $k => $v ) {
if ( is_array( $v ) ) {
$meta[ $k ] = implode( ',', $v );
}
}

return $meta;
}

/**
* Import posts from a remote REST API to the local test site.
*
* @param string $rest_url The remote REST API endpoint URL.
*/
function import_rest_to_posts( $rest_url ) {
$response = wp_remote_get( $rest_url );
$status_code = wp_remote_retrieve_response_code( $response );

if ( is_wp_error( $response ) ) {
die( esc_html( $response->get_error_message() ) );
} elseif ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
die( esc_html( "HTTP Error $status_code \n" ) );
}

$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body );

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

// Surely there's a neater way to do this.
$newpost = array(
'import_id' => $post->id,
'post_date' => gmdate( 'Y-m-d H:i:s', strtotime( $post->date ) ),
'post_name' => $post->slug,
'post_title' => $post->title,
'post_status' => $post->status,
'post_type' => $post->type,
'post_title' => $post->title->rendered,
'post_content' => ( $post->content_raw ?? $post->content->rendered ),
'post_parent' => $post->parent,
'comment_status' => $post->comment_status,
'meta_input' => sanitize_meta_input( $post->meta ),
);

$new_post_id = wp_insert_post( $newpost, 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" );
}
}

import_rest_to_posts( $opts['url'] );
3 changes: 3 additions & 0 deletions env/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ wp import "${root}/env/data.xml" --authors=create

wp option update show_on_front 'page'
wp option update page_on_front 8891

npm run wp-env run cli "php env/import-content.php --url 'https://wordpress.org/main-test/wp-json/wp/v2/posts?context=wporg_export&per_page=50'"
npm run wp-env run cli "php env/import-content.php --url 'https://wordpress.org/main-test/wp-json/wp/v2/pages?context=wporg_export&per_page=50'"
11 changes: 11 additions & 0 deletions source/wp-content/themes/wporg-main-2022/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,14 @@ function enqueue_assets() {
filemtime( __DIR__ . '/style.css' )
);
}

/**
* Make posts and pages available for export from the staging site, so the import script can
* fetch them to a local dev environment.
*/
add_filter( 'wporg_export_context_post_types', function( $types ) {
return array_merge( $types, [
'post',
'page',
]);
} );