Skip to content

Commit

Permalink
fix: add readme and fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenn00dle committed Jan 9, 2025
1 parent 6aed1c0 commit fea9890
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 26 deletions.
34 changes: 34 additions & 0 deletions includes/corrections/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Corrections

This feature allows authors to add article corrections to the top or bottom of a post.

## How it works

When you enable this feature, a new meta box will be added to the post editor screen. You can use this meta box to add a correction to the post. The correction will be displayed at the top or bottom of the post, depending on the settings.

## Usage

This feature can be enabled by adding the following constant to your `wp-config.php`:

```php
define( 'NEWSPACK_CORRECTIONS_ENABLED', true );
```

## Data

Corrections are stored as `newspack_correction` custom post type. A correction consists of the following fields:

| Name | Type | Stored As | Description |
| ----------------------------- | -------- | -------------- | --------------------------------------------------------------- |
| `title` | `string` | `post_title` | The correction title. Defaults to 'Correction for [post title]' |
| `content` | `string` | `post_content` | The correction text. |
| `newspack_correction_date` | `string` | `post_meta` | Correction date. Note this is different from the post date. |
| `newspack_correction-post-id` | `int` | `post_meta` | The ID of the post to which the correction is associated. |

In addition, some correction data is stored in the associated post as post meta:

| Name | Type | Stored As | Description |
| ------------------------------- | ----------------------- | ----------- | ------------------------------------------------------------- |
| `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. |
53 changes: 27 additions & 26 deletions includes/corrections/class-corrections.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static function register_post_type() {
'public_queryable' => true,
'query_var' => true,
'rewrite' => [ 'slug' => 'correction' ],
'show_ui' => true,
'show_ui' => false,
'show_in_rest' => true,
'supports' => $supports,
'taxonomies' => [],
Expand All @@ -154,34 +154,13 @@ public static function register_post_type() {
\register_post_type( self::POST_TYPE, $args );
}

/**
* Get corrections for post.
*
* @param int $post_id The post ID.
*
* @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,
]
);
}

/**
* Save corrections for post.
*
* @param int $post_id The post ID.
* @param array $corrections The corrections.
*/
public static function save_corrections( $post_id, $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 = [];
Expand All @@ -206,6 +185,27 @@ public static function save_corrections( $post_id, $corrections ) {
update_post_meta( $post_id, self::CORRECTIONS_IDS_META, $correction_ids );
}

/**
* Get corrections for post.
*
* @param int $post_id The post ID.
*
* @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,
]
);
}

/**
* Update correction.
*
Expand All @@ -225,9 +225,10 @@ public static function update_correction( $correction_id, $correction ) {
/**
* Delete corrections for post.
*
* @param int $post_id the post id.
* @param array $correction_ids correction ids.
*/
public static function delete_corrections( $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 = [];
Expand Down Expand Up @@ -439,12 +440,12 @@ public static function save_corrections_metabox( $post_id ) {
'date' => ! empty( $correction['date'] ) ? sanitize_text_field( $correction['date'] ) : gmdate( 'Y-m-d' ),
];
}
self::save_corrections( $post_id, $corrections );
self::add_corrections( $post_id, $corrections );
}
// delete corrections if present.
if ( ! empty( $corrections_data['deleted-corrections'] ) ) {
$correction_ids = array_map( 'intval', $corrections_data['deleted-corrections'] );
self::delete_corrections( $correction_ids );
self::delete_corrections( $post_id, $correction_ids );
}
}

Expand Down

0 comments on commit fea9890

Please sign in to comment.