" in the language of the * answers as its alt text, so that is the link's name for search engines and * screen readers. The stored label stays the text "AiProfile": the image is * swapped in when the link is printed (nav_menu_item_title and * render_block_core/navigation-link), so with the plugin deactivated the menu * shows the plain text. A label the owner changed stays text. * * @package aiprofile-inside */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** The class on our menu item, navigation link and fallback line. */ define( 'AIPROFILE_INSIDE_FOOTER_CLASS', 'aiprofile-footer-link' ); /** * The footer link as stored in the settings. * * @return array { status: added|fallback|removed, route: classic|block|fallback, where, item_id, nav_id } or an empty array. */ function aiprofile_inside_footer_state() { $settings = aiprofile_inside_settings(); return isset( $settings['footer'] ) && is_array( $settings['footer'] ) ? $settings['footer'] : array(); } /** * Is the footer link in place? * * @return bool */ function aiprofile_inside_footer_active() { $footer = aiprofile_inside_footer_state(); return isset( $footer['status'] ) && in_array( $footer['status'], array( 'added', 'fallback' ), true ); } /** The label the plugin gives its menu item and navigation link. */ define( 'AIPROFILE_INSIDE_FOOTER_LABEL', 'AiProfile' ); /** The class on a link that shows the logo, so footer.css leaves out the old mark. */ define( 'AIPROFILE_INSIDE_LOGO_LINK_CLASS', 'aiprofile-has-logo' ); /** * The link's name per language (since 1.4.0), the alt text of the logo. * Plain formats; %s is the business name. * * @return array Language code to array( with name, without name ). */ function aiprofile_inside_footer_labels() { return array( 'nl' => array( 'Vragen en antwoorden over %s', 'Vragen en antwoorden' ), 'en' => array( 'Questions and answers about %s', 'Questions and answers' ), 'de' => array( 'Fragen und Antworten zu %s', 'Fragen und Antworten' ), 'fr' => array( 'Questions et réponses sur %s', 'Questions et réponses' ), 'es' => array( 'Preguntas y respuestas sobre %s', 'Preguntas y respuestas' ), 'it' => array( 'Domande e risposte su %s', 'Domande e risposte' ), ); } /** * The footer link's name (since 1.4.0): "Questions and answers about " * in the language of the answers (settings `answers_lang`), else the plugin's * translation in the site's language. * * @return string Plain text; escape it for an attribute. */ function aiprofile_inside_footer_label() { static $label = null; if ( null !== $label ) { return $label; } $settings = aiprofile_inside_settings(); $name = isset( $settings['name'] ) ? trim( wp_strip_all_tags( (string) $settings['name'] ) ) : ''; // Kept in the settings at Connect and on every fetch that brings another // language (aiprofile_inside_get()), so no transient read on each page. // A site that updated from 1.3.x reads the kept copy until its next fetch. if ( ! empty( $settings['answers_lang'] ) ) { $lang = (string) $settings['answers_lang']; } else { $lang = function_exists( 'aiprofile_inside_answers_lang' ) ? aiprofile_inside_answers_lang( isset( $settings['key'] ) ? (string) $settings['key'] : '' ) : aiprofile_inside_lang( '', '' ); } $labels = aiprofile_inside_footer_labels(); if ( isset( $labels[ $lang ] ) ) { $label = '' !== $name ? sprintf( $labels[ $lang ][0], $name ) : $labels[ $lang ][1]; } elseif ( '' !== $name ) { /* translators: %s: business name */ $label = sprintf( __( 'Questions and answers about %s', 'aiprofile-inside' ), $name ); } else { $label = __( 'Questions and answers', 'aiprofile-inside' ); } return $label; } /** * The logo as an image (since 1.4.0), the content of the footer link. Our own * file from assets/, never loaded from elsewhere; inline styles, so it is 20 * px high also where footer.css does not load. Only tags, attributes and * styles that wp_kses_post() keeps, so a theme or plugin that runs the menu * through it still shows the logo. * * @return string HTML. */ function aiprofile_inside_footer_logo() { return sprintf( '', esc_url( plugins_url( 'assets/logo.svg', AIPROFILE_INSIDE_FILE ) ), esc_attr( aiprofile_inside_footer_label() ), esc_attr( 'display:inline-block;width:auto;height:20px;max-width:none;margin:0;padding:0;border:0;border-radius:10px;vertical-align:middle' ) ); } /** * Is this label still the one the plugin gave the link? * * @param string $label The label as it would print. * @return bool */ function aiprofile_inside_footer_label_is_ours( $label ) { return AIPROFILE_INSIDE_FOOTER_LABEL === trim( html_entity_decode( wp_strip_all_tags( (string) $label ), ENT_QUOTES, 'UTF-8' ) ); } /** The small stylesheet that puts the AiProfile mark in front of the link. */ function aiprofile_inside_enqueue_footer_style() { if ( is_admin() || ! aiprofile_inside_footer_active() ) { return; } wp_enqueue_style( 'aiprofile-inside-footer', plugins_url( 'assets/footer.css', AIPROFILE_INSIDE_FILE ), array(), AIPROFILE_INSIDE_VERSION ); } add_action( 'wp_enqueue_scripts', 'aiprofile_inside_enqueue_footer_style' ); /** Route 3: the plugin's own line at the bottom of every page. */ function aiprofile_inside_print_footer_line() { $footer = aiprofile_inside_footer_state(); if ( ! isset( $footer['status'] ) || 'fallback' !== $footer['status'] ) { return; } $settings = aiprofile_inside_settings(); $page_id = isset( $settings['page_id'] ) ? (int) $settings['page_id'] : 0; if ( ! $page_id || 'publish' !== get_post_status( $page_id ) ) { return; } // Since 1.4.0 the logo instead of the text; the title stays the last attribute. echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built from escaped parts. } add_action( 'wp_footer', 'aiprofile_inside_print_footer_line' ); /** * Route 1 (since 1.3.9): our footer menu item gets the title "Questions * answered by the owner of ..., via AiProfile" when it renders, also an item * made by an older version. A title the owner gave the item himself stays. * * @param array $atts The link's attributes. * @param WP_Post $item Menu item. * @return array */ function aiprofile_inside_footer_item_title( $atts, $item ) { if ( ! is_object( $item ) || ! aiprofile_inside_is_footer_item( $item ) ) { return $atts; } if ( empty( $atts['title'] ) ) { $atts['title'] = aiprofile_inside_credit_title(); } // Since 1.4.0: the link shows the logo (aiprofile_inside_footer_item_logo()). if ( isset( $item->title ) && aiprofile_inside_footer_label_is_ours( $item->title ) ) { $atts['class'] = trim( ( isset( $atts['class'] ) ? $atts['class'] . ' ' : '' ) . AIPROFILE_INSIDE_LOGO_LINK_CLASS ); } return $atts; } add_filter( 'nav_menu_link_attributes', 'aiprofile_inside_footer_item_title', 10, 2 ); /** * Route 1 (since 1.4.0): our footer menu item shows the logo instead of the * text "AiProfile" when it renders. The stored label stays text; a label the * owner changed stays as he wrote it. * * @param string $title The item's title, as it will print. * @param WP_Post $item Menu item. * @return string */ function aiprofile_inside_footer_item_logo( $title, $item ) { if ( is_object( $item ) && aiprofile_inside_is_footer_item( $item ) && aiprofile_inside_footer_label_is_ours( $title ) ) { return aiprofile_inside_footer_logo(); } return $title; } add_filter( 'nav_menu_item_title', 'aiprofile_inside_footer_item_logo', 10, 2 ); /** * Route 2 (since 1.3.9): the same title on our navigation link in a block * theme's footer, when it renders, unless the link has a title of its own. * Since 1.4.0 also the logo in place of the label "AiProfile" (a label the * owner changed stays text). * * @param string $content The block's HTML. * @param array $block Parsed block. * @return string */ function aiprofile_inside_footer_block_title( $content, $block ) { if ( ! aiprofile_inside_is_footer_block( $block ) ) { return $content; } $logo = preg_match( '#(.*?)#s', (string) $content, $label ) && aiprofile_inside_footer_label_is_ours( $label[1] ); $tags = class_exists( 'WP_HTML_Tag_Processor' ) ? new WP_HTML_Tag_Processor( (string) $content ) : null; if ( $tags && $tags->next_tag( 'a' ) ) { if ( empty( $block['attrs']['title'] ) && null === $tags->get_attribute( 'title' ) ) { $tags->set_attribute( 'title', aiprofile_inside_credit_title() ); } if ( $logo ) { $tags->add_class( AIPROFILE_INSIDE_LOGO_LINK_CLASS ); } $content = $tags->get_updated_html(); } if ( ! $logo ) { return $content; } return (string) preg_replace_callback( '#()(.*?)()#s', function ( $m ) { return aiprofile_inside_footer_label_is_ours( $m[2] ) ? $m[1] . aiprofile_inside_footer_logo() . $m[3] : $m[0]; }, (string) $content, 1 ); } add_filter( 'render_block_core/navigation-link', 'aiprofile_inside_footer_block_title', 10, 2 ); /** * The menu ids that a header location shows (any location that is not a * footer one), so the footer link never lands in the main menu as well. * * @return int[] */ function aiprofile_inside_header_menu_ids() { $ids = array(); foreach ( get_nav_menu_locations() as $location => $menu_id ) { if ( $menu_id && ! in_array( $location, aiprofile_inside_footer_locations(), true ) && false === strpos( (string) $location, 'footer' ) ) { $ids[] = (int) $menu_id; } } return $ids; } /** * The footer locations we try, most likely first. * * @return string[] */ function aiprofile_inside_footer_locations() { return array( 'footer', 'footer-menu', 'footer_menu', 'footer-1', 'footer-navigation', 'menu-2', 'secondary' ); } /** * Classic theme: the menu assigned to a footer location, unless the header * shows that same menu. * * @return WP_Term|null */ function aiprofile_inside_footer_menu() { $locations = get_nav_menu_locations(); $header = aiprofile_inside_header_menu_ids(); foreach ( aiprofile_inside_footer_locations() as $location ) { if ( empty( $locations[ $location ] ) || in_array( (int) $locations[ $location ], $header, true ) ) { continue; } $menu = wp_get_nav_menu_object( $locations[ $location ] ); if ( $menu ) { return $menu; } } return null; } /** * Is this menu item ours? * * @param WP_Post $item Menu item. * @return bool */ function aiprofile_inside_is_footer_item( $item ) { return is_array( $item->classes ) && in_array( AIPROFILE_INSIDE_FOOTER_CLASS, $item->classes, true ); } /** * Route 1: a menu item "AiProfile" in the footer menu. * * @param int $page_id The answers page. * @return array|null The footer state, or null when the theme has no footer menu. */ function aiprofile_inside_footer_classic( $page_id ) { $menu = aiprofile_inside_footer_menu(); if ( ! $menu ) { return null; } foreach ( (array) wp_get_nav_menu_items( $menu->term_id, array( 'post_status' => 'any' ) ) as $item ) { if ( aiprofile_inside_is_footer_item( $item ) ) { return array( 'status' => 'added', 'route' => 'classic', 'where' => $menu->name, 'item_id' => (int) $item->ID, ); } } $item_id = wp_update_nav_menu_item( $menu->term_id, 0, array( 'menu-item-object-id' => $page_id, 'menu-item-object' => 'page', 'menu-item-type' => 'post_type', 'menu-item-status' => 'publish', 'menu-item-title' => AIPROFILE_INSIDE_FOOTER_LABEL, 'menu-item-classes' => AIPROFILE_INSIDE_FOOTER_CLASS, ) ); if ( is_wp_error( $item_id ) || ! $item_id ) { return null; } return array( 'status' => 'added', 'route' => 'classic', 'where' => $menu->name, 'item_id' => (int) $item_id, ); } /** * The wp_navigation post a template part's Navigation block shows through * its `ref`. A Navigation block without a ref (links written into the theme, * or WordPress's shared fallback menu) is not ours to change. * * @param string $part Template part slug: header or footer. * @param bool $fallback Also count WordPress's fallback: a Navigation block * without ref and without links of its own shows the * most recently published wp_navigation menu. * @return int Post id, or 0. */ function aiprofile_inside_part_nav_id( $part, $fallback = false ) { $template = get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' ); if ( ! $template || empty( $template->content ) ) { return 0; } $nav_block = aiprofile_inside_find_block( parse_blocks( $template->content ), 'core/navigation' ); if ( ! $nav_block ) { return 0; } if ( empty( $nav_block['attrs']['ref'] ) ) { if ( ! $fallback || ! empty( $nav_block['innerBlocks'] ) ) { return 0; } // What WordPress shows then, read without making a menu. $recent = get_posts( array( 'post_type' => 'wp_navigation', 'post_status' => 'publish', 'posts_per_page' => 1, 'orderby' => 'date', 'order' => 'DESC', 'fields' => 'ids', ) ); return $recent ? (int) $recent[0] : 0; } $nav = get_post( (int) $nav_block['attrs']['ref'] ); return $nav && 'wp_navigation' === $nav->post_type ? (int) $nav->ID : 0; } /** * Find the first block of a kind in a block tree. * * @param array $blocks Parsed blocks. * @param string $name Block name, such as core/navigation. * @param int $depth How many patterns deep we are (guards against loops). * @return array|null */ function aiprofile_inside_find_block( $blocks, $name, $depth = 0 ) { foreach ( $blocks as $block ) { if ( isset( $block['blockName'] ) && $name === $block['blockName'] ) { return $block; } // Block themes such as Twenty Twenty-Five put the header in a pattern. if ( isset( $block['blockName'] ) && 'core/pattern' === $block['blockName'] && ! empty( $block['attrs']['slug'] ) && $depth < 5 ) { $pattern = WP_Block_Patterns_Registry::get_instance()->get_registered( $block['attrs']['slug'] ); if ( $pattern && ! empty( $pattern['content'] ) ) { $hit = aiprofile_inside_find_block( parse_blocks( $pattern['content'] ), $name, $depth + 1 ); if ( $hit ) { return $hit; } } } if ( ! empty( $block['innerBlocks'] ) ) { $hit = aiprofile_inside_find_block( $block['innerBlocks'], $name, $depth ); if ( $hit ) { return $hit; } } } return null; } /** * Does a navigation link carry our class? * * @param array $block Parsed block. * @return bool */ function aiprofile_inside_is_footer_block( $block ) { return isset( $block['blockName'], $block['attrs']['className'] ) && 'core/navigation-link' === $block['blockName'] && in_array( AIPROFILE_INSIDE_FOOTER_CLASS, preg_split( '/\s+/', (string) $block['attrs']['className'] ), true ); } /** * Route 2: a navigation link "AiProfile" in the footer's own navigation. * * @param int $page_id The answers page. * @return array|null The footer state, or null when there is no such navigation. */ function aiprofile_inside_footer_block( $page_id ) { $nav_id = aiprofile_inside_part_nav_id( 'footer' ); // The same menu in the header would get the link too. if ( ! $nav_id || aiprofile_inside_part_nav_id( 'header', true ) === $nav_id ) { return null; } $nav = get_post( $nav_id ); $where = '' !== $nav->post_title ? $nav->post_title : __( 'Footer navigation', 'aiprofile-inside' ); $state = array( 'status' => 'added', 'route' => 'block', 'where' => $where, 'nav_id' => $nav_id, ); foreach ( parse_blocks( $nav->post_content ) as $block ) { if ( aiprofile_inside_is_footer_block( $block ) ) { return $state; } } $link = serialize_block( array( 'blockName' => 'core/navigation-link', 'attrs' => array( 'label' => AIPROFILE_INSIDE_FOOTER_LABEL, 'type' => 'page', 'id' => (int) $page_id, 'url' => get_permalink( $page_id ), 'kind' => 'post-type', 'className' => AIPROFILE_INSIDE_FOOTER_CLASS, ), 'innerBlocks' => array(), 'innerHTML' => '', 'innerContent' => array(), ) ); $updated = wp_update_post( wp_slash( array( 'ID' => $nav_id, 'post_content' => rtrim( $nav->post_content ) . "\n" . $link, ) ), true ); return is_wp_error( $updated ) || ! $updated ? null : $state; } /** * Put the link in the footer: a footer menu, else the footer navigation of a * block theme, else the plugin's own line. * * @param int $page_id The answers page. * @return array The footer state. */ function aiprofile_inside_ensure_footer_link( $page_id ) { $block_theme = function_exists( 'wp_is_block_theme' ) && wp_is_block_theme(); $state = $block_theme ? aiprofile_inside_footer_block( $page_id ) : aiprofile_inside_footer_classic( $page_id ); if ( $state ) { return $state; } return array( 'status' => 'fallback', 'route' => 'fallback', 'where' => '', ); } /** * Take the link out again: only our own menu item or navigation link. * * @param array $footer The footer state. * @return array The new state. */ function aiprofile_inside_remove_footer_link( $footer ) { $route = isset( $footer['route'] ) ? $footer['route'] : ''; if ( 'classic' === $route && ! empty( $footer['item_id'] ) ) { $item = get_post( (int) $footer['item_id'] ); if ( $item && 'nav_menu_item' === $item->post_type ) { $item = wp_setup_nav_menu_item( $item ); if ( aiprofile_inside_is_footer_item( $item ) ) { wp_delete_post( (int) $footer['item_id'], true ); } } } elseif ( 'block' === $route && ! empty( $footer['nav_id'] ) ) { aiprofile_inside_nav_remove( (int) $footer['nav_id'], 'aiprofile_inside_is_footer_block' ); } return array( 'status' => 'removed' ); } /** * Remove the top-level blocks of a wp_navigation post that match, and save * the rest unchanged. * * @param int $nav_id The wp_navigation post. * @param callable $matches Gets a parsed block, says whether it goes. * @param bool $one Only the last match. * @return bool Whether something was removed. */ function aiprofile_inside_nav_remove( $nav_id, $matches, $one = false ) { $nav = get_post( $nav_id ); if ( ! $nav || 'wp_navigation' !== $nav->post_type ) { return false; } $blocks = parse_blocks( $nav->post_content ); $gone = array(); foreach ( $blocks as $i => $block ) { if ( call_user_func( $matches, $block ) ) { $gone[] = $i; } } if ( empty( $gone ) ) { return false; } foreach ( $one ? array( end( $gone ) ) : $gone as $i ) { unset( $blocks[ $i ] ); } $updated = wp_update_post( wp_slash( array( 'ID' => $nav_id, 'post_content' => trim( serialize_blocks( array_values( $blocks ) ) ), ) ), true ); return ! is_wp_error( $updated ) && $updated; }