AiProfile

'; } add_action( 'wp_footer', 'aiprofile_inside_print_footer_line' ); /** * 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', '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', '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; }