post_type, CFSP_URLs::post_types(), true ) ) { return; } if ( 'publish' !== $new_status && 'publish' !== $old_status ) { return; // Draft -> draft etc. is invisible on the live site. } $kind = 'publish' === $new_status ? ( 'publish' === $old_status ? 'trigger_update' : 'trigger_publish' ) : 'trigger_remove'; if ( ! CFSP_Settings::get( $kind ) ) { return; } $deletes = array(); $old_file = get_post_meta( $post->ID, self::META, true ); if ( CFSP_URLs::is_public_post( $post ) ) { $new_file = self::file_for_post( $post ); if ( $old_file && $new_file && $old_file !== $new_file ) { $deletes[] = $old_file; // Slug / parent changed. } $reason = 'publish' === $old_status ? 'updated' : 'published'; } else { if ( $old_file ) { $deletes[] = $old_file; } delete_post_meta( $post->ID, self::META ); $reason = 'unpublished'; } CFSP_Job::enqueue_post( $post->ID, $deletes, sprintf( '%s "%s"', $reason, get_the_title( $post ) ) ); } /** Catch slug/parent changes and unpublishing using the pre-save copy of the post. */ public static function on_updated( $post_id, $after, $before ) { if ( ! CFSP_URLs::is_public_post( $before ) ) { return; } $old_file = self::file_for_post( $before ); $new_file = CFSP_URLs::is_public_post( $after ) ? self::file_for_post( $after ) : null; if ( $old_file && $old_file !== $new_file ) { CFSP_Job::enqueue_changes( array(), array( $old_file ) ); } // for_post() only knows the post's new author and date, so its old author page // and date archives would keep listing it. if ( ! CFSP_Settings::get( 'trigger_update' ) ) { return; } $urls = array(); if ( (int) $before->post_author !== (int) $after->post_author && CFSP_Settings::crawler( 'authors' ) && post_type_supports( $before->post_type, 'author' ) ) { $urls[] = get_author_posts_url( $before->post_author ); } if ( 'post' === $before->post_type && CFSP_Settings::crawler( 'date_archives' ) && substr( $before->post_date, 0, 7 ) !== substr( $after->post_date, 0, 7 ) ) { $time = strtotime( $before->post_date ); $urls[] = get_year_link( gmdate( 'Y', $time ) ); $urls[] = get_month_link( gmdate( 'Y', $time ), gmdate( 'm', $time ) ); } if ( $urls ) { CFSP_Job::enqueue_changes( $urls ); } } public static function remember_state( $post_id ) { self::$was_public[ (int) $post_id ] = CFSP_URLs::is_public_post( $post_id ); } public static function forget_state( $post_id ) { unset( self::$was_public[ (int) $post_id ] ); } /** * A post left a category or tag. for_post() only knows the terms the post has now, * so the archive of a removed term is queued here, or it keeps listing the post. */ public static function on_terms_set( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) { if ( $append ) { return; // Appending never removes a term. } $removed = array_diff( array_map( 'intval', (array) $old_tt_ids ), array_map( 'intval', (array) $tt_ids ) ); if ( ! $removed ) { return; } $tax = get_taxonomy( $taxonomy ); if ( ! $tax || ! $tax->public || 'post_format' === $taxonomy ) { return; } $post = get_post( $object_id ); if ( ! $post || ! in_array( $post->post_type, CFSP_URLs::post_types(), true ) ) { return; } // Only a post that was listed on the static site before this save; a post being // published for the first time was never on its old terms' pages. $id = (int) $post->ID; $was = array_key_exists( $id, self::$was_public ) ? self::$was_public[ $id ] : CFSP_URLs::is_public_post( $post ); if ( ! $was ) { return; } $urls = array(); $names = array(); foreach ( $removed as $tt_id ) { $term = get_term_by( 'term_taxonomy_id', $tt_id, $taxonomy ); if ( ! $term ) { continue; } $link = get_term_link( $term ); if ( is_wp_error( $link ) ) { continue; } $urls[] = $link; $names[] = $term->name; if ( CFSP_Settings::crawler( 'feeds' ) ) { $urls[] = get_term_feed_link( $term->term_id, $term->taxonomy ); } } if ( $urls ) { CFSP_Job::enqueue_changes( $urls, array(), sprintf( 'removed "%s" from %s', get_the_title( $post ), implode( ', ', $names ) ) ); } } public static function on_delete( $post_id ) { $post = get_post( $post_id ); if ( ! $post || ! in_array( $post->post_type, CFSP_URLs::post_types(), true ) ) { return; } $old_file = get_post_meta( $post_id, self::META, true ); if ( 'publish' === $post->post_status ) { $old_file = self::file_for_post( $post ); } if ( ! $old_file ) { return; } // The post is about to disappear, so compute listing URLs now instead of at push time. $urls = 'publish' === $post->post_status ? CFSP_URLs::for_post( $post_id ) : array( home_url( '/' ) ); CFSP_Job::enqueue_changes( $urls, $old_file ? array( $old_file ) : array(), sprintf( 'deleted "%s"', get_the_title( $post ) ) ); } public static function on_new_comment( $comment_id, $approved ) { if ( 1 === $approved || '1' === $approved ) { $comment = get_comment( $comment_id ); self::queue_comment_post( $comment ); } } public static function on_comment_status( $new_status, $old_status, $comment ) { if ( 'approved' === $new_status || 'approved' === $old_status ) { self::queue_comment_post( $comment ); } } public static function on_edit_comment( $comment_id ) { $comment = get_comment( $comment_id ); if ( $comment && '1' === $comment->comment_approved ) { self::queue_comment_post( $comment ); } } private static function queue_comment_post( $comment ) { if ( $comment && CFSP_URLs::is_public_post( $comment->comment_post_ID ) ) { CFSP_Job::enqueue_changes( array( get_permalink( $comment->comment_post_ID ) ), array(), 'comment' ); } } public static function on_term_edit( $term_id, $tt_id, $taxonomy ) { $tax = get_taxonomy( $taxonomy ); if ( ! $tax || ! $tax->public ) { return; } $link = get_term_link( (int) $term_id, $taxonomy ); if ( ! is_wp_error( $link ) ) { CFSP_Job::enqueue_changes( array( $link, home_url( '/' ) ), array(), 'term edited' ); } } public static function on_term_delete( $term_id, $taxonomy ) { $tax = get_taxonomy( $taxonomy ); if ( ! $tax || ! $tax->public ) { return; } $link = get_term_link( (int) $term_id, $taxonomy ); if ( ! is_wp_error( $link ) ) { $file = CFSP_Exporter::url_to_file( $link ); CFSP_Job::enqueue_changes( array( home_url( '/' ) ), $file ? array( $file ) : array(), 'term deleted' ); } } public static function site_wide() { CFSP_Job::enqueue_changes( array(), array(), current_filter(), true ); } /** Store where a post currently lives in the export so we can remove it after slug changes. */ public static function remember_file( $post_id ) { if ( CFSP_URLs::is_public_post( $post_id ) ) { $file = self::file_for_post( get_post( $post_id ) ); if ( $file ) { update_post_meta( $post_id, self::META, $file ); } } } private static function file_for_post( $post ) { $url = CFSP_Exporter::normalize( get_permalink( $post ) ); return $url ? CFSP_Exporter::url_to_file( $url ) : null; } }