Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Central configuration for child account menus.
- * Modify this array to add or change custom account menu items.
- * Keys are internal endpoint keys; each item may include: slug, label, title, text, url, after
- */
- if ( ! function_exists( 'jnews_child_account_menus' ) ) {
- function jnews_child_account_menus() {
- return array(
- 'custom-menu' => array(
- 'slug' => 'custom-menu',
- 'label' => 'custom_menu',
- 'title' => esc_html__( 'Custom Menu', 'jnews-child' ),
- 'text' => esc_html__( 'Custom Menu', 'jnews-child' ),
- 'url' => esc_url( 'https://www.jegtheme.com/' ),
- 'after' => 'order',
- ),
- // Example additional item
- 'another-menu' => array(
- 'slug' => 'another-menu',
- 'label' => 'another_menu',
- 'title' => esc_html__( 'Another', 'jnews-child' ),
- 'text' => esc_html__( 'Another', 'jnews-child' ),
- 'url' => esc_url( 'https://jnews.local/test-page/' ),
- ),
- );
- }
- }
- if ( ! function_exists( 'jnews_custom_menu_account_page' ) ) {
- /**
- * Add Custom Menu in Account page
- *
- * @param mixed $endpoint
- * @return array
- */
- function jnews_custom_menu_account_page( $endpoint ) {
- $menus = function_exists( 'jnews_child_account_menus' ) ? jnews_child_account_menus() : array();
- $to_add = array();
- foreach ( $menus as $mkey => $m ) {
- $to_add[ $mkey ] = array(
- 'slug' => isset( $m['slug'] ) ? $m['slug'] : $mkey,
- 'label' => isset( $m['label'] ) ? $m['label'] : $mkey,
- 'title' => isset( $m['title'] ) ? $m['title'] : ( isset( $m['text'] ) ? $m['text'] : $mkey ),
- 'url' => isset( $m['url'] ) ? esc_url( $m['url'] ) : '',
- );
- }
- if ( ! empty( $to_add ) ) {
- $endpoint = array_merge( $endpoint, $to_add );
- }
- return $endpoint;
- }
- add_filter( 'jnews_account_page_endpoint', 'jnews_custom_menu_account_page', 99 );
- }
- /**
- * Redirect account endpoint path to external/full URL when configured
- */
- if ( ! function_exists( 'jnews_custom_menu_account_redirect' ) ) {
- function jnews_custom_menu_account_redirect() {
- if ( is_admin() ) {
- return;
- }
- if ( empty( $_SERVER['REQUEST_URI'] ) ) {
- return;
- }
- // Get account slug via AccountPage if available
- $account_slug = 'account';
- $endpoints = array();
- if ( class_exists( 'JNews\\AccountPage' ) ) {
- $account_obj = JNews\AccountPage::getInstance();
- if ( $account_obj && method_exists( $account_obj, 'get_endpoint' ) ) {
- $endpoints = (array) $account_obj->get_endpoint();
- if ( isset( $endpoints['account']['slug'] ) ) {
- $account_slug = $endpoints['account']['slug'];
- }
- }
- }
- // Load configured menus from central config
- $menus = function_exists( 'jnews_child_account_menus' ) ? jnews_child_account_menus() : array();
- if ( empty( $menus ) ) {
- return;
- }
- // Normalize request path (strip query string)
- $request_uri = wp_unslash( $_SERVER['REQUEST_URI'] );
- $path = strtok( $request_uri, '?' );
- $path = untrailingslashit( $path );
- // If WP is installed in a subdirectory, remove the site path prefix
- $home_path = parse_url( home_url(), PHP_URL_PATH );
- if ( $home_path && '/' !== $home_path && 0 === strpos( $path, $home_path ) ) {
- $path = substr( $path, strlen( $home_path ) );
- if ( '' === $path ) {
- $path = '/';
- }
- }
- foreach ( $menus as $mkey => $m ) {
- $slug = isset( $m['slug'] ) ? $m['slug'] : $mkey;
- $expected = '/' . trim( $account_slug, '/' ) . '/' . trim( $slug, '/' );
- if ( $path === $expected || substr( $path, -strlen( $expected ) ) === $expected ) {
- $target_url = '';
- if ( isset( $m['url'] ) && ! empty( $m['url'] ) ) {
- $target_url = $m['url'];
- } elseif ( isset( $endpoints[ $mkey ]['url'] ) && ! empty( $endpoints[ $mkey ]['url'] ) ) {
- $target_url = $endpoints[ $mkey ]['url'];
- }
- if ( empty( $target_url ) ) {
- continue;
- }
- wp_redirect( esc_url_raw( $target_url ) );
- exit;
- }
- }
- }
- add_action( 'template_redirect', 'jnews_custom_menu_account_redirect', 1 );
- }
Advertisement
Add Comment
Please, Sign In to add comment