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

fix(corrections): remove newspack_corrections_ids meta #3675

Merged
merged 1 commit into from
Jan 14, 2025
Merged
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
1 change: 0 additions & 1 deletion includes/corrections/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ In addition, some correction data is stored in the associated post as post meta:
| ------------------------------- | ----------------------- | ----------- | ------------------------------------------------------------- |
| `newspack_corrections_active` | `bool` | `post_meta` | Whether the feature is enabled for the post. |
| `newspack_corrections_location` | `string` | `post_meta` | Where the correction should be displayed. (`top` or `bottom`) |
| `newspack_corrections_ids` | `array` | `post_meta` | An array of IDs of the corrections associated with the post. |
27 changes: 4 additions & 23 deletions includes/corrections/class-corrections.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ class Corrections {
*/
const CORRECTIONS_LOCATION_META = 'newspack_corrections_location';

/**
* Meta key for post corrections ids meta.
*/
const CORRECTIONS_IDS_META = 'newspack_corrections_ids';

/**
* Initializes the class.
*/
Expand Down Expand Up @@ -156,10 +151,6 @@ public static function register_post_type() {
* @param array $corrections The corrections.
*/
public static function add_corrections( $post_id, $corrections ) {
$correction_ids = get_post_meta( $post_id, self::CORRECTIONS_IDS_META, true );
if ( ! is_array( $correction_ids ) ) {
$correction_ids = [];
}
foreach ( $corrections as $correction ) {
$id = wp_insert_post(
[
Expand All @@ -177,7 +168,6 @@ public static function add_corrections( $post_id, $corrections ) {
$correction_ids[] = $id;
}
}
update_post_meta( $post_id, self::CORRECTIONS_IDS_META, $correction_ids );
}

/**
Expand All @@ -188,15 +178,14 @@ public static function add_corrections( $post_id, $corrections ) {
* @return array The corrections.
*/
public static function get_corrections( $post_id ) {
$correction_ids = get_post_meta( $post_id, self::CORRECTIONS_IDS_META, true );
if ( ! is_array( $correction_ids ) ) {
return [];
}
return get_posts(
[
'posts_per_page' => -1,
'post_type' => self::POST_TYPE,
'include' => $correction_ids,
'meta_key' => self::CORRECTION_POST_ID_META, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => $post_id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
'orderby' => 'date',
'order' => 'DESC',
]
);
}
Expand Down Expand Up @@ -224,17 +213,9 @@ public static function update_correction( $correction_id, $correction ) {
* @param array $correction_ids correction ids.
*/
public static function delete_corrections( $post_id, $correction_ids ) {
$stored_correction_ids = get_post_meta( $post_id, self::CORRECTIONS_IDS_META, true );
if ( ! is_array( $stored_correction_ids ) ) {
$stored_correction_ids = [];
}
foreach ( $correction_ids as $id ) {
wp_delete_post( $id, true );
if ( isset( $stored_correction_ids[ $id ] ) ) {
unset( $stored_correction_ids[ $id ] );
}
}
update_post_meta( $post_id, self::CORRECTIONS_IDS_META, $stored_correction_ids );
}

/**
Expand Down
Loading