ID, self::META_KEY, true ); if ( is_array( $cached ) && isset( $cached['version'], $cached['hash'], $cached['body'] ) && ADG_AIM_VERSION === $cached['version'] && self::content_hash( $post ) === $cached['hash'] ) { return $cached['body']; } } $body = self::build( $post ); if ( $enabled && $store ) { update_post_meta( $post->ID, self::META_KEY, array( 'version' => ADG_AIM_VERSION, 'hash' => self::content_hash( $post ), 'body' => $body, ) ); } return $body; } /** * Convert the post's the_content to Markdown (without cache). * * @param WP_Post $post Post. * @return string */ private static function build( WP_Post $post ) { $html = apply_filters( 'the_content', $post->post_content ); $converter = new ADG_AIM_HTML_To_Markdown(); return $converter->convert( $html ); } /** * Hash of the source content (to detect staleness even without save_post, * e.g. after a programmatic post change). * * @param WP_Post $post Post. * @return string */ private static function content_hash( WP_Post $post ) { return md5( $post->post_content ); } /** * Invalidate the post cache on save. * * @param int $post_id ID. */ public static function invalidate( $post_id ) { if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) { return; } delete_post_meta( $post_id, self::META_KEY ); } /** * Clear the entire cache (used by the "regenerate" button in the admin). */ public static function clear_all() { delete_post_meta_by_key( self::META_KEY ); } }