*/ public static function pairs_of_rule( $rule ) { $out = array(); if ( is_array( $rule ) && isset( $rule['tree'] ) ) { self::pairs( $rule['tree'], $out, 0 ); } return $out; } /** * Walk a tree. * * @param mixed $node Node. * @param array $out Output (by reference). * @param int $depth Depth guard. */ private static function pairs( $node, array &$out, $depth ) { if ( ! is_array( $node ) || $depth > 64 || count( $out ) >= self::MAX ) { return; } if ( isset( $node['cond'] ) && is_string( $node['cond'] ) && 'date_between' === strtolower( $node['cond'] ) ) { $args = isset( $node['args'] ) && is_array( $node['args'] ) ? array_values( $node['args'] ) : array(); $s = isset( $args[0] ) && is_string( $args[0] ) ? $args[0] : ''; $e = isset( $args[1] ) && is_string( $args[1] ) ? $args[1] : ''; $out[] = array( substr( $s, 0, 32 ), substr( $e, 0, 32 ) ); return; } if ( isset( $node['items'] ) && is_array( $node['items'] ) ) { foreach ( $node['items'] as $c ) { self::pairs( $c, $out, $depth + 1 ); } } if ( isset( $node['item'] ) ) { self::pairs( $node['item'], $out, $depth + 1 ); } } /** * Pairs used by every block rule in content. * * @param string $content Content. * @return array */ private static function pairs_of_content( $content ) { $out = array(); if ( ! Block_Rules::has_rules( $content ) || false === strpos( $content, 'date_between' ) ) { return $out; } $rules = array(); Block_Rules::collect( parse_blocks( $content ), $rules ); foreach ( $rules as $r ) { $out = array_merge( $out, self::pairs_of_rule( $r ) ); } return array_slice( $out, 0, self::MAX ); } /** * Stored list. * * @return array{widgets:array, posts:array} */ public static function stored() { $v = get_option( self::OPTION, array() ); $v = is_array( $v ) ? $v : array(); return array( 'widgets' => isset( $v['widgets'] ) && is_array( $v['widgets'] ) ? $v['widgets'] : array(), 'posts' => isset( $v['posts'] ) && is_array( $v['posts'] ) ? $v['posts'] : array(), ); } /** * Write the list (never autoloaded) and reschedule. * * @param array $data List. */ private static function store( array $data ) { if ( false === get_option( self::OPTION, false ) ) { add_option( self::OPTION, $data, '', false ); } else { update_option( self::OPTION, $data, false ); } self::reschedule(); } /** * Rebuild the widget part from the widget options. */ public static function rebuild_widgets() { $pairs = array(); foreach ( Widget_Store::all_rules() as $raw ) { $pairs = array_merge( $pairs, self::pairs_of_rule( $raw ) ); } $blocks = get_option( 'widget_block', array() ); if ( is_array( $blocks ) ) { foreach ( $blocks as $inst ) { if ( is_array( $inst ) && isset( $inst['content'] ) && is_string( $inst['content'] ) ) { $pairs = array_merge( $pairs, self::pairs_of_content( $inst['content'] ) ); } } } $data = self::stored(); $data['widgets'] = array_slice( self::unique( $pairs ), 0, self::MAX ); self::store( $data ); } /** * A post was saved. * * @param int $post_id Post ID. * @param \WP_Post $post Post. */ public static function on_post_saved( $post_id, $post ) { if ( ! $post instanceof \WP_Post || 'revision' === $post->post_type || 'auto-draft' === $post->post_status ) { return; } $data = self::stored(); $key = (string) (int) $post_id; $pairs = 'trash' === $post->post_status ? array() : self::unique( self::pairs_of_content( (string) $post->post_content ) ); if ( array() === $pairs && ! isset( $data['posts'][ $key ] ) ) { return; // Nothing scheduled here before or now: no write. } if ( array() === $pairs ) { unset( $data['posts'][ $key ] ); } else { $data['posts'][ $key ] = $pairs; } self::store( $data ); } /** * A post was deleted. * * @param int $post_id Post ID. */ public static function on_post_deleted( $post_id ) { $data = self::stored(); $key = (string) (int) $post_id; if ( isset( $data['posts'][ $key ] ) ) { unset( $data['posts'][ $key ] ); self::store( $data ); } } /** * Unique pairs. * * @param array $pairs Pairs. * @return array */ private static function unique( array $pairs ) { $out = array(); foreach ( $pairs as $p ) { $out[ $p[0] . '|' . $p[1] ] = array( (string) $p[0], (string) $p[1] ); } return array_values( $out ); } /** * Upcoming boundary timestamps, sorted, from the stored values and the current site time zone. * * @return array */ public static function upcoming() { $tz = wp_timezone(); $now = Clock::now(); $data = self::stored(); $all = $data['widgets']; foreach ( $data['posts'] as $pairs ) { if ( is_array( $pairs ) ) { $all = array_merge( $all, $pairs ); } } $ts = array(); foreach ( $all as $p ) { if ( ! is_array( $p ) ) { continue; } if ( isset( $p[0] ) && '' !== $p[0] ) { $s = Conditions_Schedule::to_ts( (string) $p[0], $tz, false ); if ( null !== $s && $s > $now ) { $ts[ $s ] = true; } } if ( isset( $p[1] ) && '' !== $p[1] ) { $e = Conditions_Schedule::to_ts( (string) $p[1], $tz, true ); if ( null !== $e && $e + 1 > $now ) { $ts[ $e + 1 ] = true; } } } $ts = array_keys( $ts ); sort( $ts ); return $ts; } /** * Put the single event at the next boundary (or remove it). */ public static function reschedule() { $next = self::upcoming(); $current = wp_next_scheduled( self::HOOK ); $want = $next ? (int) $next[0] : 0; if ( (int) $current === $want ) { return; } wp_clear_scheduled_hook( self::HOOK ); if ( $want > 0 ) { wp_schedule_single_event( $want, self::HOOK ); } } /** * The event: purge page caches, then schedule the next boundary. */ public static function on_boundary() { self::purge_caches(); wp_clear_scheduled_hook( self::HOOK ); self::reschedule(); } /** * Ask common page-cache plugins to purge, only when they are present. * Each call is the plugin's own public purge function or action. * * @return array What was called. */ public static function purge_caches() { $called = array(); $fns = array( 'wp_cache_clear_cache', 'rocket_clean_domain', 'w3tc_flush_all', 'wpfc_clear_all_cache', 'sg_cachepress_purge_cache', 'breeze_cache_flush' ); foreach ( $fns as $fn ) { if ( function_exists( $fn ) ) { try { call_user_func( $fn ); $called[] = $fn; } catch ( \Throwable $e ) { unset( $e ); // A failing purge never stops the others. } } } // The cache plugins' own purge actions, fired only when the plugin is present. if ( defined( 'LSCWP_V' ) ) { do_action( 'litespeed_purge_all' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- LiteSpeed Cache's purge action. $called[] = 'litespeed_purge_all'; } if ( class_exists( 'Cache_Enabler' ) ) { do_action( 'cache_enabler_clear_complete_cache' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Cache Enabler's purge action. $called[] = 'cache_enabler_clear_complete_cache'; } if ( class_exists( 'WP_Hummingbird' ) ) { do_action( 'wphb_clear_page_cache' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Hummingbird's purge action. $called[] = 'wphb_clear_page_cache'; } return $called; } /** * Remove the event (deactivation). */ public static function clear() { wp_clear_scheduled_hook( self::HOOK ); } }