Skip to content

Commit

Permalink
feat: add location
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenn00dle committed Jan 8, 2025
1 parent 4bae5aa commit 63d997c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
75 changes: 46 additions & 29 deletions includes/corrections/class-corrections.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class Corrections {
*/
const CORRECTIONS_ACTIVE_META = 'newspack_corrections_active';

/**
* Meta key for post corrections location meta.
*/
const CORRECTIONS_LOCATION_META = 'newspack_corrections_location';

/**
* Meta key for post corrections ids meta.
*/
Expand Down Expand Up @@ -101,7 +106,6 @@ public static function register_post_type() {
'revisions',
'custom-fields',
];

$labels = [
'name' => _x( 'Corrections', 'post type general name', 'newspack-plugin' ),
'singular_name' => _x( 'Correction', 'post type singular name', 'newspack-plugin' ),
Expand Down Expand Up @@ -133,7 +137,6 @@ public static function register_post_type() {
'item_link' => __( 'Correction Link', 'newspack-plugin' ),
'item_link_description' => __( 'A link to a correction.', 'newspack-plugin' ),
];

$args = array(
'labels' => $labels,
'description' => 'Post type used to store corrections and clarifications.',
Expand Down Expand Up @@ -206,8 +209,8 @@ public static function save_corrections( $post_id, $corrections ) {
/**
* Update correction.
*
* @param int $correction_id The post ID.
* @param array $correction The correction.
* @param int $correction_id the post id.
* @param array $correction the correction.
*/
public static function update_correction( $correction_id, $correction ) {
wp_update_post(
Expand All @@ -222,7 +225,7 @@ public static function update_correction( $correction_id, $correction ) {
/**
* Delete corrections for post.
*
* @param array $correction_ids Correction IDs.
* @param array $correction_ids correction ids.
*/
public static function delete_corrections( $correction_ids ) {
$stored_correction_ids = get_post_meta( $post_id, self::CORRECTIONS_IDS_META, true );
Expand All @@ -248,15 +251,15 @@ public static function add_corrections_shortcode() {
/**
* Handles the corrections shortcode.
*
* @return string The shortcode output.
* @return string the shortcode output.
*/
public static function handle_corrections_shortcode() {
global $wpdb;

$post_ids = get_posts(
[
'posts_per_page' => -1,
'meta_key' => self::CORRECTIONS_ACTIVE_META, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_key' => self::CORRECTIONS_ACTIVE_META,
'meta_value' => 1, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
'fields' => 'ids',
'orderby' => 'date',
Expand Down Expand Up @@ -305,7 +308,7 @@ public static function handle_corrections_shortcode() {
/**
* Adds the corrections metabox.
*
* @param string $post_type The post type.
* @param string $post_type the post type.
*/
public static function add_corrections_metabox( $post_type ) {
$valid_post_types = [ 'article_legacy', 'content_type_blog', 'post', 'press_release' ];
Expand All @@ -324,17 +327,25 @@ public static function add_corrections_metabox( $post_type ) {
/**
* Renders the corrections metabox.
*
* @param \WP_Post $post The post object.
* @param \WP_Post $post the post object.
*/
public static function render_corrections_metabox( $post ) {
$is_active = (bool) get_post_meta( $post->ID, self::CORRECTIONS_ACTIVE_META, true );
$location = get_post_meta( $post->ID, self::CORRECTIONS_LOCATION_META, true );
$corrections = self::get_corrections( $post->ID );
?>
<div class="corrections-metabox-container">
<div class="activate-corrections">
<input type="hidden" value="0" name="<?php echo esc_attr( self::CORRECTIONS_ACTIVE_META ); ?>" />
<input type="checkbox" value="1" name="<?php echo esc_attr( self::CORRECTIONS_ACTIVE_META ); ?>" <?php checked( $is_active ); ?> />
<?php echo esc_html( __( 'Activate Corrections', 'newspack-plugin' ) ); ?>
<input type="checkbox" class="activate-corrections-checkbox" value="1" name="<?php echo esc_attr( self::CORRECTIONS_ACTIVE_META ); ?>" <?php checked( $is_active ); ?> />
<?php echo esc_html( __( 'activate corrections', 'newspack-plugin' ) ); ?>
</div>
<div class="display-corrections">
<select name="<?php echo esc_attr( self::CORRECTIONS_LOCATION_META ); ?>" />
<option value=""><?php echo esc_html( __( 'Select Location', 'newspack-plugin' ) ); ?></option>
<option value="bottom" <?php selected( $location, 'bottom' ); ?>><?php echo esc_html( __( 'Bottom', 'newspack-plugin' ) ); ?></option>
<option value="top" <?php selected( $location, 'top' ); ?>><?php echo esc_html( __( 'Top', 'newspack-plugin' ) ); ?></option>
</select>
</div>
<div class="manage-corrections">
<fieldset name="existing-corrections[]" class="existing-corrections">
Expand Down Expand Up @@ -366,55 +377,60 @@ public static function render_corrections_metabox( $post ) {
/**
* Saves the corrections metabox.
*
* @param int $post_id The post ID.
* @param int $post_id the post id.
*/
public static function save_corrections_metabox( $post_id ) {
// Return early if we are saving a correction.
// return early if we are saving a correction.
if ( self::POST_TYPE === get_post_type( $post_id ) ) {
return;
}

$corrections_active = filter_input( INPUT_POST, self::CORRECTIONS_ACTIVE_META, FILTER_VALIDATE_BOOLEAN );
$corrections_data = filter_input_array(
$corrections_active = filter_input( INPUT_POST, self::CORRECTIONS_ACTIVE_META, FILTER_VALIDATE_BOOLEAN );
$corrections_location = filter_input( INPUT_POST, self::CORRECTIONS_LOCATION_META, FILTER_SANITIZE_STRING );
$corrections_data = filter_input_array(
INPUT_POST,
[
'existing-corrections' => [
'flags' => FILTER_REQUIRE_ARRAY,
'filter' => FILTER_SANITIZE_STRING,
'filter' => FILTER_DEFAULT,
],
'new-corrections' => [
'flags' => FILTER_REQUIRE_ARRAY,
'filter' => FILTER_SANITIZE_STRING,
'filter' => FILTER_DEFAULT,
],
'deleted-corrections' => [
'flags' => FILTER_REQUIRE_ARRAY,
'filter' => FILTER_SANITIZE_NUMBER_INT,
],
]
);
// Return early if there is no corrections data.
if ( false === $corrections_active && empty( $corrections_data ) ) {
// return early if there is no corrections data.
if ( false === $corrections_active && false === $corrections_location && empty( $corrections_data ) ) {
return;
}
// Update active flag if present.
// update active flag if present.
if ( (bool) $corrections_active !== (bool) get_post_meta( $post_id, self::CORRECTIONS_ACTIVE_META, true ) ) {
update_post_meta( $post_id, self::CORRECTIONS_ACTIVE_META, (bool) $corrections_active );
}
// Update existing corrections if present.
// update location flag if present.
if ( $corrections_location !== get_post_meta( $post_id, self::CORRECTIONS_LOCATION_META, true ) ) {
update_post_meta( $post_id, self::CORRECTIONS_LOCATION_META, sanitize_text_field( $corrections_location ) );
}
// update existing corrections if present.
if ( ! empty( $corrections_data['existing-corrections'] ) ) {
foreach ( $corrections_data['existing-corrections'] as $correction_id => $correction ) {
// Don't save empty corrections.
// don't save empty corrections.
if ( empty( trim( $correction['content'] ) ) ) {
continue;
}
self::update_correction( $correction_id, $correction );
}
}
// Save new corrections if present.
// save new corrections if present.
if ( ! empty( $corrections_data['new-corrections'] ) ) {
$corrections = [];
foreach ( $corrections_data['new-corrections'] as $correction ) {
// Don't save empty corrections.
// don't save empty corrections.
if ( empty( trim( $correction['content'] ) ) ) {
continue;
}
Expand All @@ -425,7 +441,7 @@ public static function save_corrections_metabox( $post_id ) {
}
self::save_corrections( $post_id, $corrections );
}
// Delete corrections if present.
// delete corrections if present.
if ( ! empty( $corrections_data['deleted-corrections'] ) ) {
$correction_ids = array_map( 'intval', $corrections_data['deleted-corrections'] );
self::delete_corrections( $correction_ids );
Expand All @@ -435,9 +451,9 @@ public static function save_corrections_metabox( $post_id ) {
/**
* Outputs corrections on the post content.
*
* @param string $content The post content.
* @param string $content the post content.
*
* @return string The post content with corrections.
* @return string the post content with corrections.
*/
public static function output_corrections_on_post( $content ) {
if ( is_admin() || ! is_single() ) {
Expand Down Expand Up @@ -480,8 +496,9 @@ public static function output_corrections_on_post( $content ) {
</div>
<!-- /wp:group -->
<?php
$markup = do_blocks( ob_get_clean() );
return $content . $markup;
$markup = do_blocks( ob_get_clean() );
$content = 'top' === get_post_meta( get_the_ID(), self::CORRECTIONS_LOCATION_META, true ) ? $markup . $content : $content . $markup;
return $content;
}
}
Corrections::init();
12 changes: 12 additions & 0 deletions src/other-scripts/corrections/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ domReady( () => {
// Handle admin metabox for article corrections.
const metaboxContainer = document.querySelector( '.corrections-metabox-container' );
if ( metaboxContainer ) {
// Handle displaying location select.
const activateCorrections = metaboxContainer.querySelector( 'input.activate-corrections-checkbox' );
const locationSelect = metaboxContainer.querySelector( '.display-corrections' );
locationSelect.style.display = activateCorrections.checked ? 'block' : 'none';
activateCorrections.addEventListener( 'change', () => {
if ( activateCorrections.checked ) {
locationSelect.style.display = 'block';
} else {
locationSelect.style.display = 'none';
}
} );

// Handle deletion of existing corrections.
metaboxContainer.querySelectorAll( '.existing-corrections button.delete-correction' )
.forEach( button => {
Expand Down

0 comments on commit 63d997c

Please sign in to comment.