1 || post_password_required( $page_id ) ) { return false; } return has_shortcode( (string) get_post_field( 'post_content', $page_id, 'raw' ), 'aiprofile' ); } /** * template_include: the plugin's layout for the answers page, when it is on. * Only in place of a template of the theme itself, so a page builder or * another plugin that chose its own template keeps it. * * @param string $template The template WordPress is about to load. * @return string */ function aiprofile_inside_layout_template( $template ) { if ( ! aiprofile_inside_is_theme_file( $template ) || ! aiprofile_inside_is_answers_view() || ! aiprofile_inside_use_own_layout() ) { return $template; } // A theme can bring its own version: aiprofile-inside/answers-page.php. $own = locate_template( 'aiprofile-inside/answers-page.php' ); if ( '' === $own ) { $own = plugin_dir_path( AIPROFILE_INSIDE_FILE ) . 'templates/answers-page.php'; } return is_readable( $own ) ? $own : $template; } add_filter( 'template_include', 'aiprofile_inside_layout_template', 999 ); /** * template_include, last: note the template that is actually loaded. * * @param string $template The template. * @return string The same template. */ function aiprofile_inside_layout_note_template( $template ) { aiprofile_inside_layout_seen( 'template', is_string( $template ) ? $template : '' ); return $template; } add_filter( 'template_include', 'aiprofile_inside_layout_note_template', PHP_INT_MAX ); /** * wp_footer on the answers page: did the theme show the answers? Counts the * misses; in auto mode the second one switches the plugin's layout on. In * theme mode the count is kept only for the warning on the settings screen. */ function aiprofile_inside_layout_check() { // Only anonymous views count: a logged-in user may see a page builder's // editor (Divi's ?et_fb=1 swaps the content for its app) or content a // membership plugin hides. Page caches only see anonymous views anyway. if ( is_user_logged_in() || 'plugin' === aiprofile_inside_layout_mode() || ! did_action( 'wp_head' ) ) { return; } $template = aiprofile_inside_layout_seen( 'template' ); if ( aiprofile_inside_layout_seen( 'checked' ) || ! aiprofile_inside_is_theme_file( $template ) || ! aiprofile_inside_is_answers_view() || ! aiprofile_inside_layout_possible() ) { return; } // Once per request, also when a theme calls wp_footer() twice. aiprofile_inside_layout_seen( 'checked', true ); $state = aiprofile_inside_layout_state(); if ( empty( $state ) ) { $state = array( 'page_id' => aiprofile_inside_layout_page_id(), 'theme' => get_stylesheet(), 'misses' => 0, 'active' => false, ); } // Switch on by itself only in auto mode, and never on the front page: there // the plugin would replace the whole home page design. Otherwise the count // only feeds the warning on the settings screen. $auto = 'auto' === aiprofile_inside_layout_mode() && ! is_front_page(); if ( $auto && ! empty( $state['active'] ) ) { return; } $misses = isset( $state['misses'] ) ? (int) $state['misses'] : 0; if ( aiprofile_inside_layout_seen( 'rendered' ) ) { if ( $misses > 0 ) { // The answers showed after all: start counting again. $state['misses'] = 0; update_option( AIPROFILE_INSIDE_LAYOUT_OPTION, $state, false ); } return; } if ( ! $auto && $misses >= AIPROFILE_INSIDE_LAYOUT_MISSES ) { return; // No switch here: the warning is known, no need to write again. } $state['misses'] = $misses + 1; $state['template'] = str_replace( trailingslashit( wp_normalize_path( get_theme_root() ) ), '', wp_normalize_path( (string) $template ) ); if ( $auto && $state['misses'] >= AIPROFILE_INSIDE_LAYOUT_MISSES ) { $state['active'] = true; $state['since'] = time(); $state['purged'] = false; } update_option( AIPROFILE_INSIDE_LAYOUT_OPTION, $state, false ); if ( $auto && ! empty( $state['active'] ) && ! wp_next_scheduled( AIPROFILE_INSIDE_LAYOUT_PURGE_EVENT ) ) { wp_schedule_single_event( time() + MINUTE_IN_SECONDS, AIPROFILE_INSIDE_LAYOUT_PURGE_EVENT ); } if ( $auto ) { // This view lacks the answers: keep it out of the page caches, so the // next view renders again (and, after the switch, with the answers). if ( ! defined( 'DONOTCACHEPAGE' ) ) { define( 'DONOTCACHEPAGE', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- the constant page cache plugins read. } do_action( 'litespeed_control_set_nocache', 'AiProfile Inside: answers missing' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- LiteSpeed Cache's own hook. } } add_action( 'wp_footer', 'aiprofile_inside_layout_check', 1 ); /** * After a first miss, before the second view renders: no-cache headers, so * an edge or server cache (Varnish, Nginx, Cloudflare, a host's cache) that * ignores DONOTCACHEPAGE does not keep the view without the answers either, * and the second view reaches WordPress. Headers are not sent yet here. */ function aiprofile_inside_layout_nocache() { if ( is_user_logged_in() || 'auto' !== aiprofile_inside_layout_mode() || is_front_page() || ! aiprofile_inside_is_answers_view() ) { return; } $state = aiprofile_inside_layout_state(); if ( ! empty( $state['misses'] ) && empty( $state['active'] ) && ! headers_sent() ) { nocache_headers(); } } add_action( 'template_redirect', 'aiprofile_inside_layout_nocache' ); /** * Shortly after the switch: purge the answers page from the page caches, * once, so a copy without the answers is not served any longer. Not in the * request that switched: a page cache stores that view at its very end. A * single WP-Cron event, so no other request pays for it. */ function aiprofile_inside_layout_purge() { $state = aiprofile_inside_layout_state(); if ( empty( $state['active'] ) || ! empty( $state['purged'] ) ) { return; } $state['purged'] = true; update_option( AIPROFILE_INSIDE_LAYOUT_OPTION, $state, false ); if ( function_exists( 'aiprofile_inside_purge_page' ) ) { aiprofile_inside_purge_page( (int) $state['page_id'] ); } } add_action( AIPROFILE_INSIDE_LAYOUT_PURGE_EVENT, 'aiprofile_inside_layout_purge' ); /** * Another theme: start over, also with the owner's own choice, which was made * for the old theme. */ function aiprofile_inside_layout_reset() { delete_option( AIPROFILE_INSIDE_LAYOUT_OPTION ); $settings = aiprofile_inside_settings(); if ( isset( $settings['layout'] ) && 'auto' !== $settings['layout'] ) { $settings['layout'] = 'auto'; update_option( AIPROFILE_INSIDE_SETTINGS_OPTION, $settings, false ); } } add_action( 'switch_theme', 'aiprofile_inside_layout_reset' ); /** * body_class on the answers page in the plugin's layout. * * @param string[] $classes Body classes. * @return string[] */ function aiprofile_inside_layout_body_class( $classes ) { $classes[] = 'aiprofile-inside-layout'; return $classes; }