Skip to content

Commit

Permalink
Fix missing thumbnail in Discord story embed
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetrakern committed Jan 18, 2025
1 parent 623d7ff commit d145405
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
4 changes: 2 additions & 2 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,14 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
| `publish_to_draft` | `fictioneer_chapter_to_draft` (10)
| `rest_api_init` | `fictioneer_register_endpoint_get_story_comments` (10)
| `restrict_manage_posts` | `fictioneer_add_chapter_story_filter_dropdown` (10)
| `save_post` | `fictioneer_refresh_chapters_schema` (20), `fictioneer_refresh_chapter_schema` (20), `fictioneer_refresh_collections_schema` (20), `fictioneer_refresh_post_caches` (20), `fictioneer_refresh_post_schema` (20), `fictioneer_refresh_recommendations_schema` (20), `fictioneer_refresh_recommendation_schema` (20), `fictioneer_refresh_stories_schema` (20), `fictioneer_refresh_story_schema` (20), `fictioneer_save_seo_metabox` (10), `fictioneer_save_word_count` (10), `fictioneer_track_chapter_and_story_updates` (10), `fictioneer_update_modified_date_on_story_for_chapter` (10), `fictioneer_update_shortcode_relationships` (10), `fictioneer_purge_transients_after_update` (10), `fictioneer_save_story_metaboxes` (10), `fictioneer_save_chapter_metaboxes` (10), `fictioneer_save_extra_metabox` (10), `fictioneer_save_support_links_metabox` (10), `fictioneer_save_collection_metaboxes` (10), `fictioneer_save_recommendation_metaboxes` (10), `fictioneer_save_post_metaboxes` (10), `fictioneer_delete_cached_story_card_after_update` (10), `fictioneer_rebuild_story_data_collection` (999), `fictioneer_post_chapter_to_discord` (99)
| `save_post` | `fictioneer_refresh_chapters_schema` (20), `fictioneer_refresh_chapter_schema` (20), `fictioneer_refresh_collections_schema` (20), `fictioneer_refresh_post_caches` (20), `fictioneer_refresh_post_schema` (20), `fictioneer_refresh_recommendations_schema` (20), `fictioneer_refresh_recommendation_schema` (20), `fictioneer_refresh_stories_schema` (20), `fictioneer_refresh_story_schema` (20), `fictioneer_save_seo_metabox` (10), `fictioneer_save_word_count` (10), `fictioneer_track_chapter_and_story_updates` (10), `fictioneer_update_modified_date_on_story_for_chapter` (10), `fictioneer_update_shortcode_relationships` (10), `fictioneer_purge_transients_after_update` (10), `fictioneer_save_story_metaboxes` (10), `fictioneer_save_chapter_metaboxes` (10), `fictioneer_save_extra_metabox` (10), `fictioneer_save_support_links_metabox` (10), `fictioneer_save_collection_metaboxes` (10), `fictioneer_save_recommendation_metaboxes` (10), `fictioneer_save_post_metaboxes` (10), `fictioneer_delete_cached_story_card_after_update` (10), `fictioneer_rebuild_story_data_collection` (999), `fictioneer_post_chapter_to_discord` (99), `fictioneer_post_story_to_discord` (99)
| `send_headers` | `fictioneer_block_pages_from_indexing` (10)
| `set_logged_in_cookie` | `fictioneer_set_logged_in_cookie` (10)
| `show_user_profile` | `fictioneer_custom_profile_fields` (20)
| `shutdown` | `fictioneer_save_story_card_cache` (10)
| `switch_theme` | `fictioneer_theme_deactivation` (10)
| `template_redirect` | `fictioneer_generate_epub` (10), `fictioneer_oauth2_process` (10), `fictioneer_logout` (10), `fictioneer_disable_attachment_pages` (10), `fictioneer_gate_unpublished_content` (10), `fictioneer_serve_sitemap` (10), `fictioneer_redirect_story` (10), `fictioneer_redirect_scheduled_chapter_404` (10)
| `transition_post_status` | `fictioneer_log_story_chapter_status_changes` (10), `fictioneer_chapter_future_to_publish` (10), `fictioneer_post_story_to_discord` (99)
| `transition_post_status` | `fictioneer_log_story_chapter_status_changes` (10), `fictioneer_chapter_future_to_publish` (10)
| `trashed_post` | `fictioneer_refresh_post_caches` (20), `fictioneer_track_chapter_and_story_updates` (10), `fictioneer_update_modified_date_on_story_for_chapter` (10), `fictioneer_purge_transients_after_update` (10), `fictioneer_remove_chapter_from_story` (10)
| `untrash_post` | `fictioneer_refresh_post_caches` (20), `fictioneer_track_chapter_and_story_updates` (10), `fictioneer_update_modified_date_on_story_for_chapter` (10), `fictioneer_purge_transients_after_update` (10)
| `update_option_*` | `fictioneer_update_option_disable_extended_chapter_list_meta_queries` (10), `fictioneer_update_option_disable_extended_story_list_meta_queries` (10)
Expand Down
4 changes: 2 additions & 2 deletions includes/functions/_development.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ function fictioneer_generate_test_content() {

// Disable Discord
remove_action( 'comment_post', 'fictioneer_post_comment_to_discord', 99 );
remove_action( 'transition_post_status', 'fictioneer_post_story_to_discord', 99 );
remove_action( 'transition_post_status', 'fictioneer_post_chapter_to_discord', 99 );
remove_action( 'save_post', 'fictioneer_post_story_to_discord', 99 );
remove_action( 'save_post', 'fictioneer_post_chapter_to_discord', 99 );

// Setup
$user_count = absint( $_GET['users'] ?? 1 );
Expand Down
35 changes: 24 additions & 11 deletions includes/functions/_module-discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,29 @@ function fictioneer_post_comment_to_discord( $comment_id, $comment_approved ) {
*
* @since 5.6.0
* @since 5.21.2 - Refactored.
* @since 5.27.2 - Switch back to save_post hook to ensure the thumbnail is saved.
*
* @param string $new_status New post status.
* @param string $new_status Old post status.
* @param WP_Post $post Post object.
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated. Unreliable.
*/

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' ) {
function fictioneer_post_story_to_discord( $post_id, $post, $update ) {
// Prevent multi-fire
if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}

// Only if published chapter
if ( $post->post_type !== 'fcn_story' || $post->post_status !== 'publish' ) {
return;
}

// Only if published less than 10 minutes ago
$post_timestamp = get_post_time( 'U', true, $post_id );
$current_timestamp = current_time( 'U', true );

if ( $update && ( $current_timestamp - $post_timestamp ) >= 600 ) {
return;
}

Expand All @@ -210,14 +224,13 @@ function fictioneer_post_story_to_discord( $new_status, $old_status, $post ) {

// Data
$title = html_entity_decode( get_the_title( $post ) );
$url = get_permalink( $post->ID );

// Message
$message = array(
'content' => sprintf(
_x( "New story published: [%s](%s)!\n_ _", 'Discord message for new story.', 'fictioneer' ),
$title,
$url
get_permalink( $post_id )
),
'embeds' => array(
array(
Expand Down Expand Up @@ -258,11 +271,11 @@ function fictioneer_post_story_to_discord( $new_status, $old_status, $post ) {
update_post_meta( $post->ID, 'fictioneer_discord_post_trigger', true );

// Unhook if done to avoid additional triggers (if any)
remove_action( 'transition_post_status', 'fictioneer_post_story_to_discord', 99 );
remove_action( 'save_post', 'fictioneer_post_story_to_discord', 99 );
}

if ( get_option( 'fictioneer_discord_channel_stories_webhook' ) ) {
add_action( 'transition_post_status', 'fictioneer_post_story_to_discord', 99, 3 );
add_action( 'save_post', 'fictioneer_post_story_to_discord', 99, 3 );
}

// =============================================================================
Expand All @@ -278,7 +291,7 @@ function fictioneer_post_story_to_discord( $new_status, $old_status, $post ) {
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated.
* @param bool $update Whether this is an existing post being updated. Unreliable.
*/

function fictioneer_post_chapter_to_discord( $post_id, $post, $update ) {
Expand Down

0 comments on commit d145405

Please sign in to comment.