Struggling with a Custom Endpoint

Support MB REST API Struggling with a Custom EndpointResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #20198
    LockeLocke
    Participant

    I'm trying to get a custom endpoint in a plugin, I'm replicating a logic from another plugin to achieve this. But I got stuck in the important part that is getting de JSON.

    First I created the Big List of Metaboxes:

    <?php
    class MenuPageMetabox {
    
    public function initialize() 
    {
        add_filter('rwmb_meta_boxes', array($this, 'menus_metaboxes'));
    }
    
    function menus_metaboxes($meta_boxes__menus)
    {
        $prefix = '_menus_pcc_';
    
        $pp_dy = 'desayunos';
        $pp_bb = 'bebidas';
        $pp_en = 'entradas';
        $pp_sp = 'sopas';
        $pp_dc = 'dulces';
        $pp_sl = 'saludables';
        $pp_ft = 'fuertes';
        $pp_ts = 'tacos';
        $pp_sh = 'sandwiches_hamburguesas';
    
        $icon_dy = _ASSETS. 'assets/Iconos-01.png';
        $icon_bb = _ASSETS. 'assets/Iconos-06.png';
        $icon_en = _ASSETS. 'assets/Iconos-02.png';
        $icon_sp = _ASSETS. 'assets/Iconos-03.png';
        $icon_dc = _ASSETS. 'assets/Iconos-04.png';
        $icon_sl = _ASSETS. 'assets/Iconos-05.png';
        $icon_ft = _ASSETS. 'assets/Iconos-07.png';
        $icon_ts = _ASSETS. 'assets/Iconos-09.png';
        $icon_sh = _ASSETS. 'assets/Iconos-08.png';
    
        $placehodler = _ASSETS. 'assets/placeholder.png';
    
    $meta_boxes__menus[] = array(
        'title'     => 'Secciones de Menú',
        'id'    => $prefix . 'secciones_menu', 
        'post_types' => 'page',
        'context' => 'advanced',
        'include' => array(
            'template'        => array( 'page-menu.php' ),
        ),
        'tabs'      => array(
            $pp_dy => array(
                'label' => 'Desayunos',
                'icon'  => $icon_dy,
            ),
            $pp_en  => array(
                'label' => 'Las Entradas',
                'icon'  => $icon_en,
            ),
            $pp_bb  => array(
                'label' => 'Las Bebidas',
                'icon'  => $icon_bb,
            ),
            $pp_sp  => array(
                'label' => 'Las Sopas',
                'icon'  => $icon_sp,
            ),
            $pp_dc  => array(
                'label' => 'Lo Dulce',
                'icon'  => $icon_dc,
            ),
            $pp_sl  => array(
                'label' => 'Lo Saludable',
                'icon'  => $icon_sl,
            ),
            $pp_ft  => array(
                'label' => 'Los Fuertes',
                'icon'  => $icon_ft,
            ),
            $pp_ts  => array(
                'label' => 'Los Tacos',
                'icon'  => $icon_ts,
            ),
            $pp_sh  => array(
                'label' => 'Sándwiches y Hamburguesas',
                'icon'  => $icon_sh,
            ),
    
        ),
        'tab_style' => 'default',
        'tab_wrapper' => true,
        'fields' => array(
            // Desayunos 
            array(
                'id'     => $prefix . $pp_dy. 'header',
                'type'   => 'heading',
                'name' => '<img src="'. $icon_dy .'" height="50px" width="50px" />' . $pp_dy . '',
                'tab'  => $pp_dy,
            ),
            array(
                'name' => $pp_dy,
                'id'     => $prefix . $pp_dy,
                'type'   => 'group',
                'clone'  => true,
                'sort_clone' => true,
                'autosave'   => true,
                'tab'  => $pp_dy,
                'fields' => array(
                    array(
                        'name' => 'Existe SubCategoria en el Menu?',
                        'id'   => $prefix . 'sm'. $pp_dy,
                        'type'      => 'switch',                        
                    ),
                    array(
                        'name' => 'Nombre de SubCategoria de Menu?',
                        'id'   => $prefix . 'nsm'. $pp_dy,
                        'type'      => 'text',
                        'visible' => [ $prefix . 'sm', true ]
                    ),
                    array(
                        'name' => 'Nombre del Platillo (Normal)',
                        'id'   => $prefix . 'npn'. $pp_dy,
                        'type'      => 'text',                        
                    ),
                    array(
                        'name' => 'Descripcion del Platillo (Normal)',
                        'id'   => $prefix . 'dpn' . $pp_dy,
                        'type'      => 'textarea',                        
                    ),
                    array (
                        'name' => 'Imagen',
                        'id' => $prefix . 'ipn' . $pp_dy,
                        'type' => 'image_advanced',
                        'max_file_uploads' => 1, 
                        'max_status'       => 'false',
                        'std' => "{$placehodler}",
                    )
                )
            ),
    
            // Entradas 
            array(
                'id'     => $prefix . $pp_en. 'header',
                'type'   => 'heading',
                'name' => '<img src="'.$icon_en.'" height="50px" width="50px" />' . $pp_en . '',
                'tab'  => $pp_en,
            ),
            array(
                'id'     => $prefix . $pp_en,
                'type'   => 'group',
                'clone'  => true,
                'sort_clone' => true,
                'autosave'   => true,
                'tab'  => $pp_en,
                'fields' => array(
                    array(
                        'name' => 'Existe SubCategoria en el Menu?',
                        'id'   => $prefix . 'sm' . $pp_en,
                        'type'      => 'switch',                        
                    ),
                    array(
                        'name' => 'Nombre de SubCategoria de Menu?',
                        'id'   => $prefix . 'nsm' . $pp_en,
                        'type'      => 'text',
                        'visible' => [ $prefix . 'sm' . $pp_en, true ]
    
                    ),
    
                    array(
                        'name' => 'Nombre del Platillo (Normal)',
                        'id'   => $prefix . 'npn' . $pp_en,
                        'type'      => 'text',                        
                    ),
                    array(
                        'name' => 'Descripcion del Platillo (Normal)',
                        'id'   => $prefix . 'dpn' . $pp_en,
                        'type'      => 'textarea',                        
                    ),
                    array (
                        'name' => 'Imagen',
                        'id' => $prefix . 'ipn' . $pp_en,
                        'type' => 'image_advanced',
                        'max_file_uploads' => 1, 
                        'max_status'       => 'false',
                        'std' => "{$placehodler}",
                    )
                )
            ),
    
            // Bebidas 
            array(
                'id'     => $prefix . $pp_bb. 'header',
                'type'   => 'heading',
                'name' => '<img src="'. $icon_bb .'" height="50px" width="50px" />' . $pp_bb . '',
                'tab'  => $pp_bb,
            ),
            array(
                'id'     => $prefix . $pp_bb,
                'type'   => 'group',
                'clone'  => true,
                'sort_clone' => true,
                'autosave'   => true,
                'tab'  => $pp_bb,
                'fields' => array(
                    array(
                        'name' => 'Existe SubCategoria en el Menu?',
                        'id'   => $prefix . 'sm' . $pp_bb,
                        'type'      => 'switch',                        
                    ),
                    array(
                        'name' => 'Nombre de SubCategoria de Menu?',
                        'id'   => $prefix . 'nsm' . $pp_bb,
                        'type'      => 'text',
                        'visible' => [ $prefix . 'sm' . $pp_bb, true ]
    
                    ),
    
                    array(
                        'name' => 'Nombre del Platillo (Normal)',
                        'id'   => $prefix . 'npn' . $pp_bb,
                        'type'      => 'text',                        
                    ),
                    array(
                        'name' => 'Descripcion del Platillo (Normal)',
                        'id'   => $prefix . 'dpn' . $pp_bb,
                        'type'      => 'textarea',                        
                    ),
                    array (
                        'name' => 'Imagen',
                        'id' => $prefix . 'ipn' . $pp_bb,
                        'type' => 'image_advanced',
                        'max_file_uploads' => 1, 
                        'max_status'       => 'false',
                        'std' => "{$placehodler}",
                    )
                )
            ),
    
            // Sopas
            array(
                'id'     => $prefix . $pp_sp. 'header',
                'type'   => 'heading',
                'name' => '<img src="'. $icon_sp .'" height="50px" width="50px" />' . $pp_sp . '',
                'tab'  => $pp_sp,
            ), 
            array(
                'id'     => $prefix . $pp_sp,
                'type'   => 'group',
                'clone'  => true,
                'sort_clone' => true,
                'autosave'   => true,
                'tab'  => $pp_sp,
                'fields' => array(
                    array(
                        'name' => 'Existe SubCategoria en el Menu?',
                        'id'   => $prefix . 'sm' . $pp_sp,
                        'type'      => 'switch',                        
                    ),
                    array(
                        'name' => 'Nombre de SubCategoria de Menu?',
                        'id'   => $prefix . 'nsm' . $pp_sp,
                        'type'      => 'text',
                        'visible' => [ $prefix . 'sm' . $pp_sp, true ]
    
                    ),
    
                    array(
                        'name' => 'Nombre del Platillo (Normal)',
                        'id'   => $prefix . 'npn' . $pp_sp,
                        'type'      => 'text',                        
                    ),
                    array(
                        'name' => 'Descripcion del Platillo (Normal)',
                        'id'   => $prefix . 'dpn' . $pp_sp,
                        'type'      => 'textarea',                        
                    ),
                    array (
                        'name' => 'Imagen',
                        'id' => $prefix . 'ipn' . $pp_sp,
                        'type' => 'image_advanced',
                        'max_file_uploads' => 1, 
                        'max_status'       => 'false',
                        'std' => "{$placehodler}",
                    )
                )
            ),
    
            // Dulce
            array(
                'id'     => $prefix . $pp_dc. 'header',
                'type'   => 'heading',
                'name' => '<img src="'. $icon_dc .'" height="50px" width="50px" />' . $pp_dc . '',
                'tab'  => $pp_dc,
            ),
            array(
                'id'     => $prefix . $pp_dc,
                'type'   => 'group',
                'clone'  => true,
                'sort_clone' => true,
                'autosave'   => true,
                'tab'  => $pp_dc,
                'fields' => array(
                    array(
                        'name' => 'Existe SubCategoria en el Menu?',
                        'id'   => $prefix . 'sm' . $pp_dc,
                        'type'      => 'switch',                        
                    ),
                    array(
                        'name' => 'Nombre de SubCategoria de Menu?',
                        'id'   => $prefix . 'nsm' . $pp_dc,
                        'type'      => 'text',
                        'visible' => [ $prefix . 'sm' . $pp_dc, true ]
    
                    ),
    
                    array(
                        'name' => 'Nombre del Platillo (Normal)',
                        'id'   => $prefix . 'npn' . $pp_dc,
                        'type'      => 'text',                        
                    ),
                    array(
                        'name' => 'Descripcion del Platillo (Normal)',
                        'id'   => $prefix . 'dpn' . $pp_dc,
                        'type'      => 'textarea',                        
                    ),
                    array (
                        'name' => 'Imagen',
                        'id' => $prefix . 'ipn' . $pp_dc,
                        'type' => 'image_advanced',
                        'max_file_uploads' => 1, 
                        'max_status'       => 'false',
                        'std' => "{$placehodler}",
                    )
                )
            ),
    
            // Saludable
            array(
                'id'     => $prefix . $pp_sl. 'header',
                'type'   => 'heading',
                'name' => '<img src="'. $icon_sl .'" height="50px" width="50px" />' . $pp_sl . '',
                'tab'  => $pp_sl,
            ),
            array(
                'id'     => $prefix . $pp_sl,
                'type'   => 'group',
                'clone'  => true,
                'sort_clone' => true,
                'autosave'   => true,
                'tab'  => $pp_sl,
                'fields' => array(
                    array(
                        'name' => 'Existe SubCategoria en el Menu?',
                        'id'   => $prefix . 'sm' . $pp_sl,
                        'type'      => 'switch',                        
                    ),
                    array(
                        'name' => 'Nombre de SubCategoria de Menu?',
                        'id'   => $prefix . 'nsm',
                        'type'      => 'text',
                        'visible' => [ $prefix . 'sm' . $pp_sl, true ]
    
                    ),
    
                    array(
                        'name' => 'Nombre del Platillo (Normal)',
                        'id'   => $prefix . 'npn' . $pp_sl,
                        'type'      => 'text',                        
                    ),
                    array(
                        'name' => 'Descripcion del Platillo (Normal)',
                        'id'   => $prefix . 'dpn' . $pp_sl,
                        'type'      => 'textarea',                        
                    ),
                    array (
                        'name' => 'Imagen',
                        'id' => $prefix . 'ipn' . $pp_sl,
                        'type' => 'image_advanced',
                        'max_file_uploads' => 1, 
                        'max_status'       => 'false',
                        'std' => "{$placehodler}",
                    )
                )
            ),
    
            // Platillos Fuertes
            array(
                'id'     => $prefix . $pp_ft. 'header',
                'type'   => 'heading',
                'name' => '<img src="'. $icon_ft .'" height="50px" width="50px" />' . $pp_ft . '',
                'tab'  => $pp_ft,
            ),
    
            array(
                'id'     => $prefix . $pp_ft,
                'type'   => 'group',
                'clone'  => true,
                'sort_clone' => true,
                'autosave'   => true,
                'tab'  => $pp_ft,
                'fields' => array(
                    array(
                        'name' => 'Existe SubCategoria en el Menu?',
                        'id'   => $prefix . 'sm' . $pp_ft,
                        'type'      => 'switch',                        
                    ),
                    array(
                        'name' => 'Nombre de SubCategoria de Menu?',
                        'id'   => $prefix . 'nsm' . $pp_ft,
                        'type'      => 'text',
                        'visible' => [ $prefix . 'sm' . $pp_ft, true ]
    
                    ),
    
                    array(
                        'name' => 'Nombre del Platillo (Normal)',
                        'id'   => $prefix . 'npn' . $pp_ft,
                        'type'      => 'text',                        
                    ),
                    array(
                        'name' => 'Descripcion del Platillo (Normal)',
                        'id'   => $prefix . 'dpn' . $pp_ft,
                        'type'      => 'textarea',                        
                    ),
                    array (
                        'name' => 'Imagen',
                        'id' => $prefix . 'ipn' . $pp_ft,
                        'type' => 'image_advanced',
                        'max_file_uploads' => 1, 
                        'max_status'       => 'false',
                        'std' => "{$placehodler}",
                    )
                )
            ),
    
            // Tacos
            array(
                'id'     => $prefix . $pp_ts. 'header',
                'type'   => 'heading',
                'name' => '<img src="'. $icon_ts .'" height="50px" width="50px" />' . $pp_ts . '',
                'tab'  => $pp_ts,
            ),
            array(
                'id'     => $prefix . $pp_ts,
                'type'   => 'group',
                'clone'  => true,
                'sort_clone' => true,
                'autosave'   => true,
                'tab'  => $pp_ts,
                'fields' => array(
                    array(
                        'name' => 'Existe SubCategoria en el Menu?',
                        'id'   => $prefix . 'sm' . $pp_ts,
                        'type'      => 'switch',                        
                    ),
                    array(
                        'name' => 'Nombre de SubCategoria de Menu?',
                        'id'   => $prefix . 'nsm' . $pp_ts,
                        'type'      => 'text',
                        'visible' => [ $prefix . 'sm' . $pp_ts, true ]
    
                    ),
    
                    array(
                        'name' => 'Nombre del Platillo (Normal)',
                        'id'   => $prefix . 'npn' . $pp_ts,
                        'type'      => 'text',                        
                    ),
                    array(
                        'name' => 'Descripcion del Platillo (Normal)',
                        'id'   => $prefix . 'dpn' . $pp_ts,
                        'type'      => 'textarea',                        
                    ),
                    array (
                        'name' => 'Imagen',
                        'id' => $prefix . 'ipn' . $pp_ts,
                        'type' => 'image_advanced',
                        'max_file_uploads' => 1, 
                        'max_status'       => 'false',
                        'std' => "{$placehodler}",
                    )
                )
            ),
    
            // Sanwiches y Hambuguesas
            array(
                'id'     => $prefix . $pp_sh. 'header',
                'type'   => 'heading',
                'name' => '<img src="'. $icon_sh .'" height="50px" width="50px" />' . $pp_sh . '',
                'tab'  => $pp_sh,
            ),
            array(
                'id'     => $prefix . $pp_sh,
                'type'   => 'group',
                'clone'  => true,
                'sort_clone' => true,
                'autosave'   => true,
                'tab'  => $pp_sh,
                'fields' => array(
                    array(
                        'name' => 'Existe SubCategoria en el Menu?',
                        'id'   => $prefix . 'sm' . $pp_sh,
                        'type'      => 'switch',                        
                    ),
                    array(
                        'name' => 'Nombre de SubCategoria de Menu?',
                        'id'   => $prefix . 'nsm' . $pp_sh,
                        'type'      => 'text',
                        'visible' => [ $prefix . 'sm' . $pp_sh, true ]
    
                    ),
    
                    array(
                        'name' => 'Nombre del Platillo (Normal)',
                        'id'   => $prefix . 'npn' . $pp_sh,
                        'type'      => 'text',                        
                    ),
                    array(
                        'name' => 'Descripcion del Platillo (Normal)',
                        'id'   => $prefix . 'dpn' . $pp_sh,
                        'type'      => 'textarea',                        
                    ),
                    array (
                        'name' => 'Imagen',
                        'id' => $prefix . 'ipn' . $pp_sh,
                        'type' => 'image_advanced',
                        'max_file_uploads' => 1, 
                        'max_status'       => 'false',
                        'std' => "{$placehodler}",
                    )
                )
            )
        )
    );
    
        return $meta_boxes__menus;
    }
    
    }

    then creating the endpoints:

    require_once(CLASSES . 'class-repository.php');
    
    class Play_Enpoints_Menus {
        public function init() {
            $this->register('/menus/', array('\API\Play_Repository_Menus', 'get_menus'));
        }
    
    public function register($path, $callback, $method = 'GET', $namespace = 'api/v1') {
        add_action('rest_api_init', function() use ($path, $callback, $method, $namespace) {
            register_rest_route($namespace, $path, array(
                'methods' => $method,
                'callback' => $callback
            ));
        });
    }
    }
    
    <?php
    namespace API;
    use WP_Query;
    use stdClass;
    use RW_Meta_Box;
    use RWMB_Image_Field;
    
    class Play_Repository_Menus 
    {
    
    public static function get_menus()
    {
        $prefix = 'pinno_menus_pcc_';
    
        $pp_dy = $prefix.'desayunos';
        $pp_bb = $prefix.'bebidas';
        $pp_en = $prefix.'entradas';
        $pp_sp = $prefix.'sopas';
        $pp_dc = $prefix.'dulces';
        $pp_sl = $prefix.'saludables';
        $pp_ft = $prefix.'fuertes';
        $pp_ts = $prefix.'tacos';
        $pp_sh = $prefix.'sandwiches_hamburguesas';   
    
        $mb_menu = '';
    
        $pp_dy_ep = rwmb_meta($pp_dy);
    
        if(!empty(self::$pp_dy_ep)) {
            foreach ( self::$pp_dy_ep as self::$ppdyep ) {
                $mb_menu .= isset( self::$ppdyep[$prefix . 'npn'. $pp_dy] ) ? self::$ppdyep[$prefix . 'npn'. $pp_dy] : '';
            }
        }
    
        return $mb_menu;
    }
    
    }

    But the Metabox come with an empty string.

    I know totally that I'm not understanding at all the logic of how can I create my own custom endpoint.

    But I feel that is about the ID of The page.

    by the way, my plugin is creating the MB and creates a Custom Page Also.

    Hope someone can help me .

    #20212
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The variable $prefix in the function menus_metaboxes() has value _menus_pcc_ while it has value pinno_menus_pcc_ in the function get_menus(). Could you please check it again?

    #20216
    LockeLocke
    Participant

    Got it!

    check this out:
    https://pastebin.com/MR0mGF5q

    The only thing now I'm trying is to validate if the endpoint coexists in a page_template, but that doesn't seem to work:

    ...
            $menu_list = array();
            $menu_list["MenuList"] = $mb_menu;
            $not_available = array();
            $not_available["Status"] = 'Endpoint not available';
    
    if(get_page_template('page-menu.php')) {
                    return $menu_list;
    } else {
       return $not_available;
    }

    I don't know if I need to use all the PathURL for the get_template_path or is no logic to call that function for the endpoint (that I feel that is the right answer, lol).

    In another case which validation you recommend guys?

    Thank you

    #20221
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The function get_template_path() returns the path of page template in current or parent template. If you want to check the specific template, please try to use the function is_page_template(). For more information, please follow these documentations
    https://developer.wordpress.org/reference/functions/is_page_template/
    https://developer.wordpress.org/reference/functions/get_page_template/.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.