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

feat: store prompt activation date #1340

Merged
merged 3 commits into from
Sep 11, 2024
Merged
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
44 changes: 44 additions & 0 deletions includes/class-newspack-popups.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function __construct() {
add_filter( 'display_post_states', [ __CLASS__, 'display_post_states' ], 10, 2 );
add_action( 'save_post_' . self::NEWSPACK_POPUPS_CPT, [ __CLASS__, 'popup_default_fields' ], 10, 3 );
add_action( 'transition_post_status', [ __CLASS__, 'prevent_default_category_on_publish' ], 10, 3 );
add_action( 'transition_post_status', [ __CLASS__, 'store_activation_dates' ], 10, 3 );
add_action( 'pre_delete_term', [ __CLASS__, 'prevent_default_category_on_term_delete' ], 10, 2 );
add_filter( 'show_admin_bar', [ __CLASS__, 'show_admin_bar' ], 10, 2 ); // phpcs:ignore WordPressVIPMinimum.UserExperience.AdminBarRemoval.RemovalDetected
add_filter( 'newspack_blocks_should_deduplicate', [ __CLASS__, 'newspack_blocks_should_deduplicate' ], 10, 2 );
Expand Down Expand Up @@ -537,6 +538,30 @@ public static function register_meta() {
]
);

\register_meta(
dkoo marked this conversation as resolved.
Show resolved Hide resolved
'post',
'activation_date',
[
'object_subtype' => self::NEWSPACK_POPUPS_CPT,
'show_in_rest' => true,
'type' => 'string',
'single' => true,
'auth_callback' => '__return_true',
]
);

\register_meta(
'post',
'deactivation_date',
[
'object_subtype' => self::NEWSPACK_POPUPS_CPT,
'show_in_rest' => true,
'type' => 'string',
'single' => true,
'auth_callback' => '__return_true',
]
);

// Meta field for all post types.
\register_meta(
'post',
Expand Down Expand Up @@ -1020,6 +1045,25 @@ public static function prevent_default_category_on_publish( $new_status, $old_st
}
}

/**
* Stores the activation date of the prompt.
*
* @param string $new_status New status.
* @param string $old_status Old status.
* @param bool $post Post.
*/
public static function store_activation_dates( $new_status, $old_status, $post ) {
if ( self::NEWSPACK_POPUPS_CPT !== $post->post_type ) {
return;
}
if ( 'publish' !== $old_status && 'publish' === $new_status ) {
update_post_meta( $post->ID, 'activation_date', gmdate( 'Y-m-d H:i:s' ) );
}
if ( 'publish' === $old_status && 'publish' !== $new_status ) {
update_post_meta( $post->ID, 'deactivation_date', gmdate( 'Y-m-d H:i:s' ) );
}
}

/**
* When a category is deleted, any posts that have only that category assigned
* are automatically assigned the site's default category (usually "Uncategorized").
Expand Down