thebatclaudio / wp-rest-api-v2-menus

Adding menus endpoints on WP REST API v2
47 stars 23 forks source link

Translations #31

Open millerf opened 4 years ago

millerf commented 4 years ago

Hi there,

First of all, thanks for your work. It is an awesome plugin!

I am trying to retrieve my translated menus through the API, and I don't seem to be able to do it.

I am using TranslatePress for the translation, and although the translation works on WP front, I don't get any translated text via the API.

I am using /{lang}/wp-json/menus/v1/menus/primary to retrieve the menus...

Could someone help out?

thebatclaudio commented 4 years ago

Hi, I'm sorry but this plugin is not compatible with TranslatePress.

millerf commented 4 years ago

For those interested, I made it byt overriding wp_api_v2_menus_get_menu_items like this:

function wp_api_v2_menus_get_menu_items( $id ) {
    $menu_items = wp_get_nav_menu_items( $id );
    // check if there is acf installed
    if ( class_exists( 'acf' ) ) {
        foreach ( $menu_items as $menu_key => $menu_item ) {
            $fields = get_fields( $menu_item->ID );
            if ( ! empty( $fields ) ) {
                foreach ( $fields as $field_key => $item ) {
                    // add all acf custom fields
                    $menu_items[ $menu_key ]->$field_key = $item;
                }
            }
        }
    }

    // wordpress does not group child menu items with parent menu items
    $child_items = [];
    // pull all child menu items into separate object
    foreach ( $menu_items as $key => $item ) {
        $item->data = array(
            'title' => array(
                'text'=>$item->title,
                'rendered' => $item->title
                )
            );
        $item = apply_filters('rest_prepare_nav_menu_item', $item);
        if($item->type == 'post_type') {
            // add slug to menu items
            $slug = basename( get_permalink($item->object_id) );
            $item->slug = $slug;
        } else if($item->type == 'taxonomy') {
            $cat = get_term($item->object_id);
            $item->slug = $cat->slug;
        } else if($item->type == 'post_type_archive') {
            $post_type_data = get_post_type_object($item->object);

            if ($post_type_data->has_archive) {
                $item->slug = $post_type_data->rewrite['slug'];
            }
        }

        if (isset($item->thumbnail_id) && $item->thumbnail_id) {
            $item->thumbnail_src = wp_get_attachment_image_url(intval($item->thumbnail_id), 'post-thumbnail');
        }
        if (isset($item->thumbnail_hover_id) && $item->thumbnail_hover_id) {
            $item->thumbnail_hover_src = wp_get_attachment_image_url(intval($item->thumbnail_hover_id), 'post-thumbnail');
        }

        if ( $item->menu_item_parent ) {
            array_push( $child_items, $item );
            unset( $menu_items[ $key ] );
        }

    }

    // push child items into their parent item in the original object
    do {
        foreach($child_items as $key => $child_item) {
            if(tq_wp_api_v2_menus_dna_test($menu_items, $child_item)) {
                unset($child_items[$key]);
            }
        }
    } while(count($child_items));

    return array_values($menu_items);
}

Note the:

        $item->data = array(
            'title' => array(
                'text'=>$item->title,
                'rendered' => $item->title
                )
            );
        $item = apply_filters('rest_prepare_nav_menu_item', $item);

That is what translates it.

thebatclaudio commented 4 years ago

I leave it here, maybe we can integrate it. Thank you

thebatclaudio commented 4 years ago

Can you help me to understand? Where is defined this filter: rest_prepare_nav_menu_item?

millerf commented 4 years ago

You can see the documentation here

thebatclaudio commented 4 years ago

Great! Thank you!