Add option to expire post passwords

This commit is contained in:
Tetrakern 2024-05-09 23:20:36 +02:00
parent f77414413e
commit bf2b02cdc1
5 changed files with 136 additions and 2 deletions

View File

@ -286,7 +286,7 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
| `map_meta_cap` | `fcn_read_others_files`, `fictioneer_edit_others_files`, `fictioneer_delete_others_files`, `fictioneer_override_default_taxonomy_capability_check`, `fictioneer_edit_comments`
| `navigation_markup_template` | `fictioneer_pagination_markup`
| `nav_menu_link_attributes` | `fictioneer_add_menu_link_attributes`
| `post_password_required` | `fictioneer_bypass_password`
| `post_password_required` | `fictioneer_bypass_password`, `fictioneer_expire_post_password`
| `post_stuck` | `fictioneer_prevent_post_sticky`
| `postbox_classes_{$screen_id}_{$box_id}` | `fictioneer_append_metabox_classes`
| `posts_where` | `fictioneer_exclude_protected_posts`

File diff suppressed because one or more lines are too long

View File

@ -350,3 +350,51 @@ function fictioneer_chapter_to_draft( $post ) {
}
add_action( 'publish_to_draft', 'fictioneer_chapter_to_draft' );
add_action( 'private_to_draft', 'fictioneer_chapter_to_draft' );
// =============================================================================
// POST PASSWORD EXPIRATION
// =============================================================================
/**
* Expire post password
*
* Note: This just hijacks the password check to remove the password.
* The actual password requirement is not affected.
*
* @since 5.17.0
*
* @param bool $required Whether the user needs to supply a password.
* @param WP_Post $post Post object.
*
* @return bool True or false.
*/
function fictioneer_expire_post_password( $required, $post ) {
// Static variable cache
static $cache = [];
$cache_key = $post->ID . '_' . (int) $required;
if ( isset( $cache[ $cache_key ] ) ) {
return $cache[ $cache_key ];
}
// Setup
$password_expiration_date_utc = get_post_meta( $post->ID, 'fictioneer_post_password_expiration_date', true );
if ( $password_expiration_date_utc ) {
$current_date_utc = current_time( 'mysql', true );
if ( strtotime( $current_date_utc ) > strtotime( $password_expiration_date_utc ) ) {
delete_post_meta( $post->ID, 'fictioneer_post_password_expiration_date' );
wp_update_post( array( 'ID' => $post->ID, 'post_password' => '' ) );
}
}
// Cache
$cache[ $cache_key ] = $required;
// Continue filter
return $required;
}
add_filter( 'post_password_required', 'fictioneer_expire_post_password', 11, 2 );

View File

@ -155,6 +155,63 @@ function fictioneer_get_metabox_number( $post, $meta_key, $args = [] ) {
return fictioneer_get_metabox_text( $post, $meta_key, $args );
}
/**
* Returns HTML for a datetime-local input meta field
*
* @since 5.17.0
*
* @param WP_Post $post The post.
* @param string $meta_key The meta key.
* @param array $args {
* Optional. An array of additional arguments.
*
* @type string $label Label above the field.
* @type string $description Description below the field.
* @type array $attributes Additional attributes.
* }
*
* @return string The HTML markup for the field.
*/
function fictioneer_get_metabox_datetime( $post, $meta_key, $args = [] ) {
// Setup
$meta_value = esc_attr( get_post_meta( $post->ID, $meta_key, true ) );
$label = strval( $args['label'] ?? '' );
$description = strval( $args['description'] ?? '' );
$attributes = implode( ' ', $args['attributes'] ?? [] );
if ( $meta_value ) {
$utc_datetime = new DateTime( $meta_value, new DateTimeZone( 'UTC' ) );
$utc_datetime->setTimezone( new DateTimeZone( get_option( 'timezone_string' ) ) );
$meta_value = $utc_datetime->format( 'Y-m-d\TH:i:s' );
}
ob_start();
// Start HTML ---> ?>
<div class="fictioneer-meta-field fictioneer-meta-field--datetime">
<?php if ( $label ) : ?>
<label class="fictioneer-meta-field__label" for="<?php echo $meta_key; ?>"><?php echo $label; ?></label>
<?php endif; ?>
<input type="hidden" name="<?php echo $meta_key; ?>" value="0" autocomplete="off">
<div class="fictioneer-meta-field__wrapper">
<input type="datetime-local" id="<?php echo $meta_key; ?>" class="fictioneer-meta-field__input" name="<?php echo $meta_key; ?>" value="<?php echo $meta_value; ?>" autocomplete="off" <?php echo $attributes; ?>>
</div>
<?php if ( $description ) : ?>
<div class="fictioneer-meta-field__description"><?php echo $description; ?></div>
<?php endif; ?>
</div>
<?php // <--- End HTML
return ob_get_clean();
}
/**
* Returns HTML for a URL meta field
*
@ -3193,6 +3250,16 @@ function fictioneer_render_extra_metabox( $post ) {
}
}
// Password expiration datetime
$output['fictioneer_post_password_expiration_date'] = fictioneer_get_metabox_datetime(
$post,
'fictioneer_post_password_expiration_date',
array(
'label' => _x( 'Expire Post Password', 'Password expiration meta field label.', 'fictioneer' ),
'description' => __( 'Removes the password after the date.', 'fictioneer' ),
)
);
// Checkbox: Disable new comments
if ( in_array( $post->post_type, ['post', 'page', 'fcn_story', 'fcn_chapter'] ) ) {
$output['flags_heading'] = '<div class="fictioneer-meta-field-heading">' .
@ -3375,6 +3442,20 @@ function fictioneer_save_extra_metabox( $post_id ) {
}
}
// Password expiration datetime
if ( isset( $_POST['fictioneer_post_password_expiration_date'] ) ) {
$expiration_date = $_POST['fictioneer_post_password_expiration_date'];
if ( ! empty( $expiration_date ) ) {
$local_datetime = new DateTime( $expiration_date, new DateTimeZone( get_option( 'timezone_string' ) ) );
$local_datetime->setTimezone( new DateTimeZone( 'UTC' ) );
$fields['fictioneer_post_password_expiration_date'] = $local_datetime->format( 'Y-m-d H:i:s' );
} else {
$fields['fictioneer_post_password_expiration_date'] = 0;
}
}
// Checkbox: Disable new comments
if (
isset( $_POST['fictioneer_disable_commenting'] ) &&

View File

@ -618,6 +618,11 @@ td.comment {
padding-left: 24px;
}
&__input[type="datetime-local"] {
padding-top: 0;
padding-bottom: 0;
}
&__select:is(select) {
box-sizing: border-box;
display: block;