Refactor and fix Discord webhook functions

This commit is contained in:
Tetrakern 2024-07-29 17:13:28 +02:00
parent 11f1e9691d
commit 202292778e
4 changed files with 34 additions and 72 deletions

View File

@ -295,11 +295,11 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
| `publish_to_draft` | `fictioneer_chapter_to_draft`
| `rest_api_init` | `fictioneer_register_endpoint_get_story_comments`
| `restrict_manage_posts` | `fictioneer_add_chapter_story_filter_dropdown`
| `save_post` | `fictioneer_refresh_chapters_schema`, `fictioneer_refresh_chapter_schema`, `fictioneer_refresh_collections_schema`, `fictioneer_refresh_post_caches`, `fictioneer_refresh_post_schema`, `fictioneer_refresh_recommendations_schema`, `fictioneer_refresh_recommendation_schema`, `fictioneer_refresh_stories_schema`, `fictioneer_refresh_story_schema`, `fictioneer_save_seo_metabox`, `fictioneer_save_word_count`, `fictioneer_track_chapter_and_story_updates`, `fictioneer_update_modified_date_on_story_for_chapter`, `fictioneer_update_shortcode_relationships`, `fictioneer_purge_transients`, `fictioneer_post_story_to_discord`, `fictioneer_post_chapter_to_discord`, `fictioneer_save_story_metaboxes`, `fictioneer_save_chapter_metaboxes`, `fictioneer_save_advanced_metabox`, `fictioneer_save_support_links_metabox`, `fictioneer_save_collection_metaboxes`, `fictioneer_save_recommendation_metaboxes`, `fictioneer_save_post_metaboxes`
| `save_post` | `fictioneer_refresh_chapters_schema`, `fictioneer_refresh_chapter_schema`, `fictioneer_refresh_collections_schema`, `fictioneer_refresh_post_caches`, `fictioneer_refresh_post_schema`, `fictioneer_refresh_recommendations_schema`, `fictioneer_refresh_recommendation_schema`, `fictioneer_refresh_stories_schema`, `fictioneer_refresh_story_schema`, `fictioneer_save_seo_metabox`, `fictioneer_save_word_count`, `fictioneer_track_chapter_and_story_updates`, `fictioneer_update_modified_date_on_story_for_chapter`, `fictioneer_update_shortcode_relationships`, `fictioneer_purge_transients`, `fictioneer_save_story_metaboxes`, `fictioneer_save_chapter_metaboxes`, `fictioneer_save_advanced_metabox`, `fictioneer_save_support_links_metabox`, `fictioneer_save_collection_metaboxes`, `fictioneer_save_recommendation_metaboxes`, `fictioneer_save_post_metaboxes`
| `show_user_profile` | `fictioneer_custom_profile_fields`
| `switch_theme` | `fictioneer_theme_deactivation`
| `template_redirect` | `fictioneer_disable_date_archives`, `fictioneer_generate_epub`, `fictioneer_handle_oauth`, `fictioneer_logout`, `fictioneer_disable_attachment_pages`, `fictioneer_gate_unpublished_content`, `fictioneer_serve_sitemap`, `fictioneer_redirect_story`
| `transition_post_status` | `fictioneer_log_story_chapter_status_changes`, `fictioneer_chapter_future_to_publish`
| `transition_post_status` | `fictioneer_log_story_chapter_status_changes`, `fictioneer_chapter_future_to_publish`, `fictioneer_post_story_to_discord`, `fictioneer_post_chapter_to_discord`
| `trashed_post` | `fictioneer_refresh_post_caches`, `fictioneer_track_chapter_and_story_updates`, `fictioneer_update_modified_date_on_story_for_chapter`, `fictioneer_purge_transients`, `fictioneer_remove_chapter_from_story`
| `untrash_post` | `fictioneer_refresh_post_caches`, `fictioneer_track_chapter_and_story_updates`, `fictioneer_update_modified_date_on_story_for_chapter`, `fictioneer_purge_transients`
| `update_option_*` | `fictioneer_update_option_disable_extended_chapter_list_meta_queries`, `fictioneer_update_option_disable_extended_story_list_meta_queries`

View File

@ -1494,7 +1494,6 @@ define( 'CONSTANT_NAME', value );
| FICTIONEER_STORY_COMMENT_COUNT_TIMEOUT | integer | Timeout between comment count refreshes for stories in _seconds_. Default `900`.
| FICTIONEER_REQUESTS_PER_MINUTE | integer | Maximum requests per minute and action if the rate limit is enabled. Default `5`.
| FICTIONEER_QUERY_ID_ARRAY_LIMIT | integer | Maximum allowed IDs in 'post__{not}_in' query arguments. Default `100`.
| FICTIONEER_OLD_POST_THRESHOLD | integer | Age of published post that is considered "old" in seconds. Default `30`.
| FICTIONEER_PATREON_EXPIRATION_TIME | integer | Time until a users Patreon data expires in seconds. Default `WEEK_IN_SECONDS`.
| FICTIONEER_PARTIAL_CACHE_EXPIRATION_TIME | integer | Time until a cached partial expires in seconds. Default `4 * HOUR_IN_SECONDS`.
| FICTIONEER_CACHE_PURGE_ASSIST | boolean | Whether to call the cache purge assist function on post updates. Default `true`.

View File

@ -278,11 +278,6 @@ if ( ! defined( 'FICTIONEER_QUERY_ID_ARRAY_LIMIT' ) ) {
define( 'FICTIONEER_QUERY_ID_ARRAY_LIMIT', 100 );
}
// Integer: After how many seconds after saving is a post considered "old"
if ( ! defined( 'FICTIONEER_OLD_POST_THRESHOLD' ) ) {
define( 'FICTIONEER_OLD_POST_THRESHOLD', 30 );
}
// Integer: Time until a user's Patreon data expires in seconds
if ( ! defined( 'FICTIONEER_PATREON_EXPIRATION_TIME' ) ) {
define( 'FICTIONEER_PATREON_EXPIRATION_TIME', WEEK_IN_SECONDS );
@ -646,6 +641,12 @@ if ( get_option( 'fictioneer_enable_seo' ) && ! fictioneer_seo_plugin_active() )
require_once __DIR__ . '/includes/functions/_module-schemas.php';
}
/**
* Communicate with the Discord API.
*/
require_once __DIR__ . '/includes/functions/_module-discord.php';
// =============================================================================
// ADMIN ONLY
// =============================================================================
@ -666,12 +667,6 @@ if ( is_admin() ) {
require_once __DIR__ . '/includes/functions/_module-forms.php';
}
/**
* Communicate with the Discord API.
*/
require_once __DIR__ . '/includes/functions/_module-discord.php';
}
// =============================================================================

View File

@ -178,50 +178,34 @@ if ( get_option( 'fictioneer_discord_channel_comments_webhook' ) ) {
}
// =============================================================================
// POST NEW STORY TO DISCORD
// POST PUBLISHED STORY TO DISCORD
// =============================================================================
/**
* Sends a notification to Discord when a new story is first published
* Sends a notification to Discord when a story is first published
*
* @since 5.6.0
* @since 5.21.2 - Refactored.
*
* @param string $post_id The post ID.
* @param string $new_status New post status.
* @param string $new_status Old post status.
* @param WP_Post $post Post object.
*/
function fictioneer_post_story_to_discord( $post_id ) {
// Prevent multi-fire
if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}
// Setup
$post = get_post( $post_id );
// Published chapter?
if ( $post->post_type !== 'fcn_story' || $post->post_status !== 'publish' ) {
return;
}
// Older than n (30) seconds? The $update param is unreliable with block editor.
$publishing_time = get_post_time( 'U', true, $post_id );
$current_time = current_time( 'timestamp', true );
if ( $current_time - $publishing_time > FICTIONEER_OLD_POST_THRESHOLD ) {
function fictioneer_post_story_to_discord( $new_status, $old_status, $post ) {
// Only if story going from non-publish status to publish
if ( $post->post_type !== 'fcn_story' || $new_status !== 'publish' || $old_status === 'publish' ) {
return;
}
// Already triggered once?
if ( get_post_meta( $post_id, 'fictioneer_discord_post_trigger', true ) ) {
if ( get_post_meta( $post->ID, 'fictioneer_discord_post_trigger', true ) ) {
return;
}
// Remove story webhook to prevent miss-fire
remove_action( 'save_post', 'fictioneer_post_chapter_to_discord', 99 );
// Data
$title = html_entity_decode( get_the_title( $post ) );
$url = get_permalink( $post_id );
$url = get_permalink( $post->ID );
// Message
$message = array(
@ -234,7 +218,7 @@ function fictioneer_post_story_to_discord( $post_id ) {
array(
'title' => $title,
'description' => html_entity_decode( get_the_excerpt( $post ) ),
'url' => get_permalink( $post_id ),
'url' => get_permalink( $post->ID ),
'color' => FICTIONEER_DISCORD_EMBED_COLOR,
'author' => array(
'name' => get_the_author_meta( 'display_name', $post->post_author ),
@ -270,51 +254,35 @@ function fictioneer_post_story_to_discord( $post_id ) {
}
if ( get_option( 'fictioneer_discord_channel_stories_webhook' ) ) {
add_action( 'save_post', 'fictioneer_post_story_to_discord', 99 );
add_action( 'transition_post_status', 'fictioneer_post_story_to_discord', 99, 3 );
}
// =============================================================================
// POST NEW CHAPTER TO DISCORD
// POST PUBLISHED CHAPTER TO DISCORD
// =============================================================================
/**
* Sends a notification to Discord when a new chapter is first published
* Sends a notification to Discord when a chapter is first published
*
* @since 5.6.0
* @since 5.21.2 - Refactored.
*
* @param string $post_id The post ID.
* @param string $new_status New post status.
* @param string $new_status Old post status.
* @param WP_Post $post Post object.
*/
function fictioneer_post_chapter_to_discord( $post_id ) {
// Prevent multi-fire
if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}
// Setup
$post = get_post( $post_id );
// Published chapter?
if ( $post->post_type !== 'fcn_chapter' || $post->post_status !== 'publish' ) {
return;
}
// Older than n (30) seconds? The $update param is unreliable with block editor.
$publishing_time = get_post_time( 'U', true, $post_id );
$current_time = current_time( 'timestamp', true );
if ( $current_time - $publishing_time > FICTIONEER_OLD_POST_THRESHOLD ) {
function fictioneer_post_chapter_to_discord( $new_status, $old_status, $post ) {
// Only if chapter going from non-publish status to publish
if ( $post->post_type !== 'fcn_chapter' || $new_status !== 'publish' || $old_status === 'publish' ) {
return;
}
// Already triggered once?
if ( get_post_meta( $post_id, 'fictioneer_discord_post_trigger', true ) ) {
if ( get_post_meta( $post->ID, 'fictioneer_discord_post_trigger', true ) ) {
return;
}
// Remove story webhook to prevent miss-fire
remove_action( 'save_post', 'fictioneer_post_story_to_discord', 99 );
// Message
$message = array(
'content' => _x( "New chapter published!\n_ _", 'Discord message for new chapter.', 'fictioneer' ),
@ -322,7 +290,7 @@ function fictioneer_post_chapter_to_discord( $post_id ) {
array(
'title' => html_entity_decode( get_the_title( $post ) ),
'description' => html_entity_decode( get_the_excerpt( $post ) ),
'url' => get_permalink( $post_id ),
'url' => get_permalink( $post->ID ),
'color' => FICTIONEER_DISCORD_EMBED_COLOR,
'author' => array(
'name' => get_the_author_meta( 'display_name', $post->post_author ),
@ -334,7 +302,7 @@ function fictioneer_post_chapter_to_discord( $post_id ) {
);
// Story?
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = get_post_meta( $post->ID, 'fictioneer_chapter_story', true );
$story_status = get_post_status( $story_id );
$story_title = get_the_title( $story_id );
$story_url = get_permalink( $story_id );
@ -383,7 +351,7 @@ function fictioneer_post_chapter_to_discord( $post_id ) {
}
if ( get_option( 'fictioneer_discord_channel_chapters_webhook' ) ) {
add_action( 'save_post', 'fictioneer_post_chapter_to_discord', 99 );
add_action( 'transition_post_status', 'fictioneer_post_chapter_to_discord', 99, 3 );
}
// =============================================================================