sumitpore / mvc-plugin-boilerplate-for-wordpress

An MVC WordPress Plugin Boilerplate with clear separation of concerns. Will make your experience of creating a WordPress Plugin enjoyable!
176 stars 22 forks source link

Cannot save template on page editor #24

Closed wpplumber closed 4 years ago

wpplumber commented 4 years ago

I had the template save operation working before while using an OOP architecture but when I moved the plugin code to this WP boilerplate I got the following message error!

image

Is it something with permission? I used the following route for backoffice page.

$router
    ->register_route_of_type( ROUTE_TYPE::ADMIN)//ADMIN_WITH_POSSIBLE_AJAX )
    ->with_controller( 'Admin_Settings@register_hook_callbacks' ) 
    ->with_model( 'Admin_Settings' ) 
    ->with_view( 'Admin_Settings' ); 
sumitpore commented 4 years ago

Inside register_hook_callbacks of Admin_Settings controller, you can add your own add_action and add_filter calls.

wpplumber commented 4 years ago

I did, but when accessing the page which should use the custom template it doesn't show and seems using the theme template?

wpplumber commented 4 years ago

should it be something on locate_template()? any wiki in case you're busy?

sumitpore commented 4 years ago

I have not created a separate wiki for it. For Frontend related activities, ideally a separate controller should be created and that should be hooked on ROUTE_TYPE::FRONTEND Router. I suppose as of now you have created a single controller and have added everything into it. You can try replacing ROUTE_TYPE::ADMIN with ROUTE_TYPE::ANY in routes.php & see if it works.

If that does not work too, please provide me a psuedo code which worked w/o MVC.

wpplumber commented 4 years ago
$router
    ->register_route_of_type(ROUTE_TYPE::FRONTEND)
    ->with_controller('Dashboard@register_hook_callbacks')
    ->with_model('Dashboard')
    ->with_view('Dashboard');

Result: image

$router
    ->register_route_of_type(ROUTE_TYPE::ANY)
    ->with_controller('Dashboard@register_hook_callbacks')
    ->with_model('Dashboard')
    ->with_view('Dashboard');

image

sumitpore commented 4 years ago

With ROUTE_TYPE::ANY, full class name (including namespace) should be passed to with_controller, with_model and with_view.

wpplumber commented 4 years ago

I was able to see the template called in debug mode after adding the following to /core/class-view.php

$template =  $template_path .  $template_name; // To test
        // Get default template.
        if ( ! $template ) {
            $template = $default_path . $template_name;
        }

Do you use a solution for pages using custom template to manage them since you use the following:

public static function render_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
        if ( $args && is_array( $args ) ) {
            extract( $args ); // @codingStandardsIgnoreLine.
        }

        $located = static::locate_template( $template_name, $template_path, $default_path );
        if ( false == $located ) {
            return;
        }

        ob_start();
        do_action( 'plugin_name_before_template_render', $template_name, $template_path, $located, $args );
        include( $located );
        do_action( 'plugin_name_after_template_render', $template_name, $template_path, $located, $args );

        return ob_get_clean(); // @codingStandardsIgnoreLine.
    }
wpplumber commented 4 years ago

Using the following hook related to template within the controller of that permalink fixed the issue to load the custom template.

    public function register_hook_callbacks(){
                    // Enqueue Styles & Scripts.
                    add_action( 'wp_enqueue_scripts', 
                    array( $this, 'enqueue_scripts' ) );

add_filter('template_include', 
    array( $this, 'view_project_template') 
);
    }
wpplumber commented 4 years ago

thank you @sumitpore for this work repository.