'Veelgestelde vragen', 'en' => AIPROFILE_INSIDE_DEFAULT_TITLE, 'de' => 'Häufige Fragen', 'fr' => 'Questions fréquentes', 'es' => 'Preguntas frecuentes', 'it' => 'Domande frequenti', ); } /** * The page title in a language: from the list above, else the plugin's * translation in the site's language, else English. * * @param string $lang Two or three letters; '' for the site's language. * @return string */ function aiprofile_inside_page_title( $lang = '' ) { $lang = '' !== (string) $lang ? strtolower( (string) $lang ) : aiprofile_inside_lang( '', '' ); $titles = aiprofile_inside_page_titles(); if ( isset( $titles[ $lang ] ) ) { return $titles[ $lang ]; } $switched = switch_to_locale( get_locale() ); $title = __( 'Frequently asked questions', 'aiprofile-inside' ); if ( $switched ) { restore_previous_locale(); } return $title; } /** * The page title since 1.4.0, per language: "AiProfile of" the business. * Plain formats; %s is the business name. * * @return array Language code to sprintf format. */ function aiprofile_inside_answers_page_titles() { return array( 'nl' => 'AiProfile van %s', 'en' => 'AiProfile of %s', 'de' => 'AiProfile von %s', 'fr' => 'AiProfile de %s', 'es' => 'AiProfile de %s', 'it' => 'AiProfile di %s', ); } /** * The title for the answers page (since 1.4.0): "AiProfile of " in the * language of the answers, from the list above, else the plugin's translation * in the site's language. Without a name, the title of 1.3.x. * * @param string $lang Two or three letters; '' for the site's language. * @param string $name The business name. * @return string */ function aiprofile_inside_answers_page_title( $lang, $name ) { $name = trim( wp_strip_all_tags( (string) $name ) ); if ( '' === $name ) { return aiprofile_inside_page_title( $lang ); } $lang = '' !== (string) $lang ? strtolower( (string) $lang ) : aiprofile_inside_lang( '', '' ); $formats = aiprofile_inside_answers_page_titles(); if ( isset( $formats[ $lang ] ) ) { return sprintf( $formats[ $lang ], $name ); } $switched = switch_to_locale( get_locale() ); /* translators: %s: business name */ $format = __( 'AiProfile of %s', 'aiprofile-inside' ); if ( $switched ) { restore_previous_locale(); } return sprintf( $format, $name ); } /** * Every title the plugin gave the answers page before 1.4.0, with the * language it is in: the titles per language of 1.3.5, and the plugin's * translation of "Frequently asked questions" in the site's language (up to * 1.3.4, and 1.3.5+ for a language without its own title), which is in the * site's language. * * @return array Title to language code. */ function aiprofile_inside_old_page_titles() { $titles = array_flip( aiprofile_inside_page_titles() ); $switched = switch_to_locale( get_locale() ); $local = __( 'Frequently asked questions', 'aiprofile-inside' ); if ( $switched ) { restore_previous_locale(); } if ( ! isset( $titles[ $local ] ) ) { $titles[ $local ] = aiprofile_inside_lang( '', '' ); } return $titles; } /** Option that says whether the 1.4.0 title step is still to do: pending, done, or the time of the next try. */ define( 'AIPROFILE_INSIDE_TITLE_STEP_OPTION', 'aiprofile_inside_title_140' ); /** * 1.4.0, once after the update: the answers page gets the title "AiProfile of * ". Only a page this plugin made itself (its stored page id, not one * the owner made and the plugin adopted), and only while its title is still * exactly one the plugin gave it: a title the owner changed is never touched. * The language is the old title's own ("Veelgestelde vragen" is Dutch), never * a guess from a cached copy that may be gone. The slug stays as it is. * * @param array $settings Current settings. * @return string 'done' (renamed, or nothing to do) or 'retry' (our page with * our title, but the business name is not known yet). */ function aiprofile_inside_upgrade_title_140( $settings ) { $page_id = isset( $settings['page_id'] ) ? (int) $settings['page_id'] : 0; if ( empty( $settings['key'] ) || ! $page_id || ! empty( $settings['adopted'] ) ) { // Not connected, or not our page: a later Connect makes a page with the new title. return 'done'; } $status = get_post_status( $page_id ); if ( ! $status || 'trash' === $status || 'page' !== get_post_type( $page_id ) ) { return 'done'; } $current = (string) get_post_field( 'post_title', $page_id, 'raw' ); $old = aiprofile_inside_old_page_titles(); if ( ! isset( $old[ $current ] ) ) { return 'done'; } $name = isset( $settings['name'] ) ? trim( wp_strip_all_tags( (string) $settings['name'] ) ) : ''; if ( '' === $name ) { return 'retry'; } // "Frequently asked questions" is also what a site in any other language // got when its answers' language was not known; the language kept since // Connect (settings answers_lang) says better, when there is one. $lang = AIPROFILE_INSIDE_DEFAULT_TITLE === $current && ! empty( $settings['answers_lang'] ) ? (string) $settings['answers_lang'] : $old[ $current ]; $title = aiprofile_inside_answers_page_title( $lang, $name ); if ( $title !== $current ) { aiprofile_inside_set_page_title( $page_id, $title ); // The last title the plugin set (since 1.4.1), so the title AiProfile // sends can follow it (aiprofile_inside_retitle_page()). $fresh = aiprofile_inside_settings(); $fresh['title_set'] = $title; update_option( AIPROFILE_INSIDE_SETTINGS_OPTION, $fresh, false ); if ( function_exists( 'aiprofile_inside_purge_page' ) ) { aiprofile_inside_purge_page( $page_id ); } } return 'done'; } /** * The titles the plugin gave the answers page in 1.4.0: "AiProfile of " * in each of its languages and the site's translation. * * @param string $name The business name. * @return string[] */ function aiprofile_inside_titles_140( $name ) { $name = trim( wp_strip_all_tags( (string) $name ) ); if ( '' === $name ) { return array(); } $titles = array(); foreach ( aiprofile_inside_answers_page_titles() as $format ) { $titles[] = sprintf( $format, $name ); } $titles[] = aiprofile_inside_answers_page_title( aiprofile_inside_lang( '', '' ), $name ); return array_values( array_unique( $titles ) ); } /** * The page title and footer alt text AiProfile sent with the kept copy of the * connected profile's answers (since 1.4.1), for Connect. * * @param string $key Profile key. * @return array { pageTitle, footerAlt }, or an empty array. */ function aiprofile_inside_kept_texts( $key ) { $key = aiprofile_inside_normalize_key( $key ); $copy = '' !== $key ? get_transient( aiprofile_inside_cache_key( $key, aiprofile_inside_lang( '', '' ) ) . '_good' ) : false; return is_array( $copy ) && ! empty( $copy['texts'] ) && is_array( $copy['texts'] ) ? $copy['texts'] : array(); } /** * Since 1.4.1 (AIP-703): give the answers page the title AiProfile sends * ("AiProfile van , in "). Only a page this plugin made * (not adopted), and only while its title is still one the plugin gave it: a * 1.3.x default, a 1.4.0 "AiProfile of ", or exactly the last title the * plugin set (`title_set`), so a later change on AiProfile follows too. A * title the owner changed stays. The slug never changes. * * @param array $settings Current settings. * @return bool Whether the title changed. */ function aiprofile_inside_retitle_page( $settings ) { $page_id = isset( $settings['page_id'] ) ? (int) $settings['page_id'] : 0; $target = isset( $settings['texts']['pageTitle'] ) ? trim( (string) $settings['texts']['pageTitle'] ) : ''; if ( '' === $target || empty( $settings['key'] ) || ! $page_id || ! empty( $settings['adopted'] ) ) { return false; } $status = get_post_status( $page_id ); if ( ! $status || 'trash' === $status || 'page' !== get_post_type( $page_id ) ) { return false; } $current = (string) get_post_field( 'post_title', $page_id, 'raw' ); if ( $current === $target ) { return false; } $ours = isset( aiprofile_inside_old_page_titles()[ $current ] ) || in_array( $current, aiprofile_inside_titles_140( isset( $settings['name'] ) ? $settings['name'] : '' ), true ) || ( ! empty( $settings['title_set'] ) && $current === $settings['title_set'] ); if ( ! $ours ) { return false; } aiprofile_inside_set_page_title( $page_id, $target ); $fresh = aiprofile_inside_settings(); $fresh['title_set'] = $target; update_option( AIPROFILE_INSIDE_SETTINGS_OPTION, $fresh, false ); if ( function_exists( 'aiprofile_inside_purge_page' ) ) { aiprofile_inside_purge_page( $page_id ); } return true; } /** * Rename the answers page outside the page being rendered (since 1.4.1): a * rename in the middle of a view would show the old H1 in this response, and * a page cache would keep that copy. So on a visitor's request the rename * waits for shutdown, and the response is kept out of page caches * (DONOTCACHEPAGE); aiprofile_inside_retitle_page() purges the page after it. * In the admin, WP-Cron or WP-CLI it runs at once. */ function aiprofile_inside_retitle_later() { if ( aiprofile_inside_connecting() ) { // Connect renames after its own final save (includes/admin.php). return; } if ( is_admin() || wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) ) { aiprofile_inside_retitle_page( aiprofile_inside_settings() ); return; } if ( ! defined( 'DONOTCACHEPAGE' ) ) { define( 'DONOTCACHEPAGE', true ); } if ( false === has_action( 'shutdown', 'aiprofile_inside_retitle_at_shutdown' ) ) { add_action( 'shutdown', 'aiprofile_inside_retitle_at_shutdown', 20 ); } } /** The deferred rename, with the settings as they are by then. */ function aiprofile_inside_retitle_at_shutdown() { aiprofile_inside_retitle_page( aiprofile_inside_settings() ); } /** * Whether this request is Connect (since 1.4.1): its content request must not * rename the page on its own, Connect does it after its final save. * * @param bool|null $set True or false to set, null to read. * @return bool */ function aiprofile_inside_connecting( $set = null ) { static $connecting = false; if ( null !== $set ) { $connecting = (bool) $set; } return $connecting; } /** * Change only a page's title. The content goes back as it is stored, so kses * on a visitor's request cannot strip anything of the owner's. The slug * (post_name) is not passed, so it stays. * * @param int $page_id The page. * @param string $title The new title. */ function aiprofile_inside_set_page_title( $page_id, $title ) { $had_kses = false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ); kses_remove_filters(); wp_update_post( wp_slash( array( 'ID' => (int) $page_id, 'post_title' => (string) $title, ) ) ); if ( $had_kses ) { kses_init_filters(); } } /** * The language the answers on the page are in: the language AiProfile served * the connected profile in, else the site's language. * * @param string $key Profile key. * @param bool $fetch Ask AiProfile when no copy is kept (at "Connect"); false reads only the kept copy. * @return string */ function aiprofile_inside_answers_lang( $key, $fetch = false ) { $site_lang = aiprofile_inside_lang( '', '' ); $key = aiprofile_inside_normalize_key( $key ); if ( '' === $key ) { return $site_lang; } $copy = get_transient( aiprofile_inside_cache_key( $key, $site_lang ) . '_good' ); if ( ( ! is_array( $copy ) || empty( $copy['html'] ) ) && $fetch ) { $copy = aiprofile_inside_get( $key, $site_lang ); } $lang = is_array( $copy ) && ! empty( $copy['html'] ) ? aiprofile_inside_html_lang( $copy['html'] ) : ''; return '' !== $lang ? $lang : $site_lang; } /** * Clear the sitemap caches we know, so the answers page is in the sitemap * search engines get: * * - Yoast SEO: WPSEO_Sitemaps_Cache::invalidate( 'page' ) (the page sitemap * and the index). * - Rank Math: \RankMath\Sitemap\Cache::invalidate_storage() (its cached * sitemap files). * - All in One SEO and WordPress's own wp-sitemap.xml build the sitemap on * every request: nothing to clear. * - Page caches: the sitemap addresses of WordPress, Yoast, Rank Math and All * in One SEO, through aiprofile_inside_purge_url() (includes/page-cache.php). */ function aiprofile_inside_refresh_sitemaps() { if ( class_exists( 'WPSEO_Sitemaps_Cache' ) && method_exists( 'WPSEO_Sitemaps_Cache', 'invalidate' ) ) { WPSEO_Sitemaps_Cache::invalidate( 'page' ); } if ( class_exists( '\RankMath\Sitemap\Cache' ) && method_exists( '\RankMath\Sitemap\Cache', 'invalidate_storage' ) ) { \RankMath\Sitemap\Cache::invalidate_storage(); } if ( ! function_exists( 'aiprofile_inside_purge_url' ) ) { return; } foreach ( array( 'wp-sitemap.xml', 'wp-sitemap-posts-page-1.xml', 'sitemap_index.xml', 'sitemap.xml', 'page-sitemap.xml' ) as $path ) { aiprofile_inside_purge_url( home_url( '/' . $path ) ); } } /** * The one-time steps after an update, on the first request after it. Each * step runs once, for a site coming from a version before it: * - 1.3.5: the page title in the language of the answers, and the sitemap * caches; * - 1.3.6: purge the answers page from the page caches; * - 1.4.0: the title "AiProfile of " (aiprofile_inside_upgrade_title_140()), * tracked in an option of its own so it can wait for the business name. * The title steps only touch a page this plugin made itself (not one the * owner made and the plugin adopted) whose title the owner never changed. * Reads only kept copies, never asks AiProfile, so a visitor's request does * not wait. */ function aiprofile_inside_upgrade() { $done = (string) get_option( AIPROFILE_INSIDE_UPGRADED_OPTION, '0' ); if ( version_compare( $done, '1.4.0', '<' ) ) { // First, so a slow or failing step below is not repeated on every request. update_option( AIPROFILE_INSIDE_UPGRADED_OPTION, AIPROFILE_INSIDE_VERSION ); $settings = aiprofile_inside_settings(); if ( version_compare( $done, '1.3.5', '<' ) ) { aiprofile_inside_upgrade_135( $settings ); } // 1.3.6: purge the answers page from the page caches once, so a copy made // before the update (on a theme that left out the answers) does not hide // the page from the layout check (includes/layout.php) until it expires. if ( version_compare( $done, '1.3.6', '<' ) && ! empty( $settings['page_id'] ) && function_exists( 'aiprofile_inside_purge_page' ) ) { aiprofile_inside_purge_page( (int) $settings['page_id'] ); } update_option( AIPROFILE_INSIDE_TITLE_STEP_OPTION, 'pending', true ); } // 1.4.0: "AiProfile of ", after the 1.3.5 step, which may just have // given the page its title in the language of the answers. Marked done // only when it is: a page whose name is not known yet is tried again, at // most once a day (the option then holds the time of the next try). $step = (string) get_option( AIPROFILE_INSIDE_TITLE_STEP_OPTION, 'done' ); if ( 'done' === $step || ( is_numeric( $step ) && time() < (int) $step ) ) { return; } if ( 'done' === aiprofile_inside_upgrade_title_140( aiprofile_inside_settings() ) ) { update_option( AIPROFILE_INSIDE_TITLE_STEP_OPTION, 'done', true ); } else { update_option( AIPROFILE_INSIDE_TITLE_STEP_OPTION, (string) ( time() + DAY_IN_SECONDS ), true ); } } /** * 1.3.5: the page title in the language of the answers, and the sitemaps. * Only for a site with a connected profile and a published page this plugin * made itself that still has the English default title. * * @param array $settings Current settings. */ function aiprofile_inside_upgrade_135( $settings ) { $page_id = isset( $settings['page_id'] ) ? (int) $settings['page_id'] : 0; if ( empty( $settings['key'] ) || ! $page_id || ! empty( $settings['adopted'] ) || 'publish' !== get_post_status( $page_id ) ) { return; } $title = aiprofile_inside_page_title( aiprofile_inside_answers_lang( $settings['key'] ) ); if ( AIPROFILE_INSIDE_DEFAULT_TITLE === get_post_field( 'post_title', $page_id, 'raw' ) && AIPROFILE_INSIDE_DEFAULT_TITLE !== $title ) { aiprofile_inside_set_page_title( $page_id, $title ); } aiprofile_inside_refresh_sitemaps(); } add_action( 'init', 'aiprofile_inside_upgrade', 20 );