'any', 'post_status' => 'publish', 's' => '[aiprofile', 'sentence' => true, 'posts_per_page' => 20, 'no_found_rows' => true, 'suppress_filters' => true, ) ); foreach ( $found as $post ) { if ( has_shortcode( (string) $post->post_content, 'aiprofile' ) ) { $ids[] = (int) $post->ID; } } /** * Filters the posts whose page cache is purged when the answers change. * * @param int[] $ids Post ids. */ $ids = apply_filters( 'aiprofile_inside_purge_post_ids', $ids ); return array_values( array_unique( array_filter( array_map( 'absint', (array) $ids ) ) ) ); } /** * Purge one post from WordPress's post cache and from the page cache of * every cache plugin we know. The calls are each plugin's documented public * API, read from its own source: * * - WP-Optimize: WPO_Page_Cache::delete_single_post_cache( $post_id ). * - LiteSpeed Cache: the action litespeed_purge_post. * - WP Rocket: rocket_clean_post( $post_id ). * - W3 Total Cache: w3tc_flush_post( $post_id ). * - WP Super Cache: wpsc_delete_post_cache( $post_id ), or on old versions * wp_cache_post_change( $post_id ). * - WP Fastest Cache: wpfc_clear_post_cache_by_id( $post_id ). * - SiteGround Optimizer: sg_cachepress_purge_cache( $url ). * - Breeze: the action purge_post_cache, which Breeze registers for this. * - Batcache (WordPress.com): batcache_clear_url( $url ). * * @param int $post_id The post. */ function aiprofile_inside_purge_page( $post_id ) { $post_id = absint( $post_id ); if ( ! $post_id ) { return; } clean_post_cache( $post_id ); $url = get_permalink( $post_id ); if ( class_exists( 'WPO_Page_Cache' ) && method_exists( 'WPO_Page_Cache', 'delete_single_post_cache' ) ) { WPO_Page_Cache::delete_single_post_cache( $post_id ); } if ( has_action( 'litespeed_purge_post' ) ) { do_action( 'litespeed_purge_post', $post_id ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- LiteSpeed Cache's own purge hook. } if ( function_exists( 'rocket_clean_post' ) ) { rocket_clean_post( $post_id ); } if ( function_exists( 'w3tc_flush_post' ) ) { w3tc_flush_post( $post_id ); } if ( function_exists( 'wpsc_delete_post_cache' ) ) { wpsc_delete_post_cache( $post_id ); } elseif ( function_exists( 'wp_cache_post_change' ) ) { wp_cache_post_change( $post_id ); } if ( function_exists( 'wpfc_clear_post_cache_by_id' ) ) { wpfc_clear_post_cache_by_id( $post_id ); } if ( $url && function_exists( 'sg_cachepress_purge_cache' ) ) { sg_cachepress_purge_cache( $url ); } if ( class_exists( 'Breeze_PurgeCache' ) && has_action( 'purge_post_cache' ) ) { do_action( 'purge_post_cache', $post_id ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Breeze's own purge hook. } if ( $url && function_exists( 'batcache_clear_url' ) ) { batcache_clear_url( $url ); } } /** * Purge one address from the page caches: the front page when it is the * list of latest posts, so there is no post to name. The calls, as above: * WPO_Page_Cache::delete_cache_by_url(), the action litespeed_purge_url, * rocket_clean_files(), w3tc_flush_url(), wpsc_delete_url_cache(), * wpfc_clear_cache_by_url(), sg_cachepress_purge_cache() and * batcache_clear_url(). Breeze has no call for one address. * * @param string $url The address. */ function aiprofile_inside_purge_url( $url ) { $url = esc_url_raw( (string) $url, array( 'http', 'https' ) ); if ( '' === $url ) { return; } if ( class_exists( 'WPO_Page_Cache' ) && method_exists( 'WPO_Page_Cache', 'delete_cache_by_url' ) ) { WPO_Page_Cache::delete_cache_by_url( $url ); } if ( has_action( 'litespeed_purge_url' ) ) { do_action( 'litespeed_purge_url', $url ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- LiteSpeed Cache's own purge hook. } if ( function_exists( 'rocket_clean_files' ) ) { rocket_clean_files( array( $url ) ); } if ( function_exists( 'w3tc_flush_url' ) ) { w3tc_flush_url( $url ); } if ( function_exists( 'wpsc_delete_url_cache' ) ) { wpsc_delete_url_cache( $url ); } if ( function_exists( 'wpfc_clear_cache_by_url' ) ) { wpfc_clear_cache_by_url( $url ); } if ( function_exists( 'sg_cachepress_purge_cache' ) ) { sg_cachepress_purge_cache( $url ); } if ( function_exists( 'batcache_clear_url' ) ) { batcache_clear_url( $url ); } } /** * Purge every page that shows the answers: the pages with the block, and * the front page when it shows the block or the footer link. */ function aiprofile_inside_purge_block_pages() { $ids = aiprofile_inside_block_post_ids(); $front_id = 'page' === get_option( 'show_on_front' ) ? (int) get_option( 'page_on_front' ) : 0; $footer = function_exists( 'aiprofile_inside_footer_active' ) && aiprofile_inside_footer_active(); foreach ( $ids as $post_id ) { aiprofile_inside_purge_page( $post_id ); } if ( $front_id && in_array( $front_id, $ids, true ) ) { // The answers on the front page itself, purged above. Breeze leaves // the home page out of its per-post purge and has no call for one // address, so here it gets its own "clear all". if ( class_exists( 'Breeze_PurgeCache' ) && has_action( 'breeze_clear_all_cache' ) ) { do_action( 'breeze_clear_all_cache' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Breeze's own purge hook. } } elseif ( $footer && $front_id ) { aiprofile_inside_purge_page( $front_id ); } elseif ( $footer ) { // The latest posts on the front page: only the footer link is there. aiprofile_inside_purge_url( home_url( '/' ) ); } } /** * The fingerprint of a copy of the answers: its HTML and its JSON-LD. * * @param array $payload { html, jsonld }. * @return string */ function aiprofile_inside_payload_hash( $payload ) { if ( ! is_array( $payload ) || empty( $payload['html'] ) ) { return ''; } $jsonld = isset( $payload['jsonld'] ) ? wp_json_encode( $payload['jsonld'] ) : ''; return md5( (string) $payload['html'] . '|' . (string) $jsonld ); } /** * The profiles and languages shown on this site: the connected profile in * the site's language, plus any other the shortcode fetched since 1.3.2 * (their cached copies say which). * * @return array[] List of array( key, lang ). */ function aiprofile_inside_shown_profiles() { $pairs = array(); $settings = aiprofile_inside_settings(); if ( ! empty( $settings['key'] ) ) { $key = aiprofile_inside_normalize_key( $settings['key'] ); if ( '' !== $key ) { $pairs[ $key . '|' . aiprofile_inside_lang( '', $key ) ] = array( $key, aiprofile_inside_lang( '', $key ) ); } } $keys = get_option( AIPROFILE_INSIDE_KEYS_OPTION, array() ); foreach ( is_array( $keys ) ? array_slice( $keys, -20 ) : array() as $cache_key ) { $good = get_transient( (string) $cache_key . '_good' ); if ( is_array( $good ) && ! empty( $good['key'] ) && ! empty( $good['lang'] ) ) { $pairs[ $good['key'] . '|' . $good['lang'] ] = array( (string) $good['key'], (string) $good['lang'] ); } } return array_values( $pairs ); } /** * The twice-daily check: fetch every shown profile again and, only when its * content changed (its hash differs from the kept copy), store it as the * fresh copy and purge the page caches. An unchanged copy is only kept for * another 7 days. Nothing is changed when AiProfile cannot be reached. */ function aiprofile_inside_refresh() { $changed = false; foreach ( aiprofile_inside_shown_profiles() as $pair ) { list( $key, $lang ) = $pair; $cache_key = aiprofile_inside_cache_key( $key, $lang ); $good = get_transient( $cache_key . '_good' ); $result = aiprofile_inside_fetch( $key, $lang ); if ( 'ok' === $result['status'] ) { $payload = aiprofile_inside_payload( $key, $lang, $result ); // No kept copy counts as changed: the page cache may still hold an old one. $is_new = aiprofile_inside_payload_hash( $payload ) !== aiprofile_inside_payload_hash( $good ); aiprofile_inside_remember_key( $cache_key ); // Kept for another 7 days either way, so an unchanged copy never runs out. set_transient( $cache_key . '_good', $payload, AIPROFILE_INSIDE_GOOD_TTL ); if ( $is_new ) { set_transient( $cache_key, $payload, AIPROFILE_INSIDE_FRESH_TTL ); $changed = true; } } elseif ( 'not_found' === $result['status'] && is_array( $good ) && ! empty( $good['html'] ) ) { // The profile is gone or has no answers any more. set_transient( $cache_key, array(), AIPROFILE_INSIDE_FRESH_TTL ); delete_transient( $cache_key . '_good' ); $changed = true; } } if ( $changed ) { aiprofile_inside_purge_block_pages(); } } add_action( AIPROFILE_INSIDE_REFRESH_EVENT, 'aiprofile_inside_refresh' ); /** * Keep the event scheduled while a profile is connected. Also for sites that * updated from an older version, where the activation hook did not run. */ function aiprofile_inside_schedule_refresh() { $settings = aiprofile_inside_settings(); $next = wp_next_scheduled( AIPROFILE_INSIDE_REFRESH_EVENT ); if ( empty( $settings['key'] ) ) { if ( $next ) { wp_clear_scheduled_hook( AIPROFILE_INSIDE_REFRESH_EVENT ); } return; } if ( ! $next ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'twicedaily', AIPROFILE_INSIDE_REFRESH_EVENT ); } } add_action( 'init', 'aiprofile_inside_schedule_refresh' ); /** On deactivation the event goes; uninstall.php clears it too. */ function aiprofile_inside_unschedule_refresh() { wp_clear_scheduled_hook( AIPROFILE_INSIDE_REFRESH_EVENT ); } register_deactivation_hook( AIPROFILE_INSIDE_FILE, 'aiprofile_inside_unschedule_refresh' );