Skip to content

Commit

Permalink
fix(starter-content): make the starter content generation idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek committed Apr 30, 2024
1 parent 3a65f51 commit d5e10ff
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
52 changes: 33 additions & 19 deletions includes/starter_content/class-starter-content-generated.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class Starter_Content_Generated extends Starter_Content_Provider {
protected static $starter_categories = [ 'Featured', 'Sports', 'Entertainment', 'Opinion', 'News', 'Events', 'Longform', 'Arts', 'Politics', 'Science', 'Tech', 'Health' ];

/**
* Meta key to store starter category data.
* Prefix for starter categories.
*
* @var string
*/
private static $starter_categories_meta = '_newspack_starter_content_categories';
private static $starter_category_prefix = '_newspack_';

/**
* Initialize starter content provider.
Expand All @@ -49,7 +49,7 @@ public static function initialize( $args = [] ) {
public static function create_post( $post_index ) {
$existing_post_id = self::get_starter_post( $post_index );
if ( $existing_post_id ) {
return $existing_post_id;
wp_trash_post( $existing_post_id );
}

if ( ! function_exists( 'wp_insert_post' ) ) {
Expand Down Expand Up @@ -121,8 +121,8 @@ function( $paragraph ) {
]
);

$category_ids = get_option( self::$starter_categories_meta );
$category_id = Starter_Content::is_e2e() ? $category_ids[ $post_index ] : $categories[0];
$category_ids = self::get_starter_categories_ids();
$category_id = Starter_Content::is_e2e() ? $category_ids[ $post_index ] : $categories[0];

wp_set_post_categories( $post_id, $category_id );

Expand All @@ -147,12 +147,11 @@ public static function create_categories() {
self::remove_starter_categories();
$category_ids = array_map(
function( $category ) {
$created_category = wp_insert_term( $category, 'category', [ 'slug' => '_newspack_' . $category ] );
$created_category = wp_insert_term( $category, 'category', [ 'slug' => self::$starter_category_prefix . $category ] );
return $created_category['term_id'];
},
self::$starter_categories
);
update_option( self::$starter_categories_meta, $category_ids );
return $category_ids;
}

Expand All @@ -164,17 +163,19 @@ function( $category ) {
public static function create_homepage() {
$existing_homepage_id = self::get_starter_homepage();
if ( $existing_homepage_id ) {
return $existing_homepage_id;
wp_delete_post( $existing_homepage_id, true );
}

if ( ! function_exists( 'wp_insert_post' ) ) {
require_once ABSPATH . 'wp-admin/includes/post.php';
}
$categories = get_categories(
[
'hide_empty' => false,
]
);
$categories = [];
foreach ( self::get_starter_categories_ids() as $category_id ) {
$categories[] = (object) [
'term_id' => $category_id,
'name' => get_term( $category_id )->name,
];
}
ob_start();
?>
<!-- wp:columns {"className":"is-style-borders"} --><div class="wp-block-columns is-style-borders">
Expand Down Expand Up @@ -374,16 +375,29 @@ public static function remove_starter_content() {
parent::remove_starter_content();
}

/**
* Get starter categories IDs.
*/
private static function get_starter_categories_ids() {
global $wpdb;
$category_ids = array_column(
$wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->prepare(
"SELECT term_id FROM $wpdb->terms WHERE slug LIKE %s",
self::$starter_category_prefix . '%'
)
),
'term_id'
);
return $category_ids;
}

/**
* Removes starter categories.
*/
public static function remove_starter_categories() {
$category_ids = get_option( self::$starter_categories_meta, [] );
if ( ! empty( $category_ids ) ) {
foreach ( $category_ids as $category_id ) {
wp_delete_category( $category_id );
}
delete_option( self::$starter_categories_meta );
foreach ( self::get_starter_categories_ids() as $category_id ) {
wp_delete_category( $category_id );
}
}
}
8 changes: 4 additions & 4 deletions includes/starter_content/class-starter-content-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ protected static function mark_starter_homepage( $post_id ) {
protected static function get_starter_post( $post_index ) {
global $wpdb;
$meta_key = self::$starter_post_meta_prefix . $post_index;
$existing_post_id = $wpdb->get_row( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key=%s;", $meta_key ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$existing_post_id = $wpdb->get_row( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key=%s;", $meta_key ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( $existing_post_id ) {
return $existing_post_id['post_id'];
return $existing_post_id->post_id;
}
return false;
}
Expand All @@ -135,9 +135,9 @@ protected static function get_starter_post( $post_index ) {
protected static function get_starter_homepage() {
global $wpdb;
$meta_key = self::$starter_homepage_meta;
$existing_post_id = $wpdb->get_row( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key=%s;", $meta_key ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$existing_post_id = $wpdb->get_row( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key=%s;", $meta_key ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( $existing_post_id ) {
return $existing_post_id;
return $existing_post_id->post_id;
}
return false;
}
Expand Down

0 comments on commit d5e10ff

Please sign in to comment.