typerocket icon indicating copy to clipboard operation
typerocket copied to clipboard

Actions of controllers not called

Open gogolander opened this issue 3 years ago • 2 comments

Hi @kevindees, I'm at it again, sorry. This is a follow-up on #250. I need WP to interoperate with a custom set of APIs and, to do that, I need to either use the actions or, better still, to override the update/destroy methods for a custom WPUser CPT and a number of other WPPost CPTs. Following Your answer at #250 I got the onActionSave and it works fine but I couldn't make the destroy actions work. Following, there are barebones examples of functions.php, CarController, UserController, Car, User. I don't see any evident issue but deleting posts or users don't cause any callback to be called nonetheless. Note: I'm assuming that the destory action is called when I want to delete a user or when I delete a post from the trash and the action of trashing the post as an update, which I think is correct. Thanks

  • functions.php:
<?php
$settings = ['capability' => 'administrator'];


tr_post_type('car', _("Cars"))
    ->setPosition(6)
    ->setTitlePlaceholder(_('Enter car name here'))
    ->setIcon('dashicons-car')
    ->forceDisableGutenberg()
    ->setModelClass(\App\Models\Car::class)
    ->setMainForm(function() {
        $form = tr_form();
        $form->input('Price')->setType('number');
    });

tr_post_type('user')
    ->setModelClass(\App\Models\User::class)
    ->forceDisableGutenberg()
    ->setMainForm(function() {
        $form = tr_form();
        $form->input('Credits')->setType('number');
});
add_filter('typerocket_hook_verified', function($verified, $context, $id) {
    if(($context === 'posts' || $context === 'users') && get_post_type($id) === 'car' ) {
        return true;
    }

    return $verified;
}, 10, 3);
/** [...] */
  • Car model:
<?php

namespace App\Models;

use TypeRocket\Models\WPPost;

class Car extends WPPost
{
    public const POST_TYPE = 'car';
}
  • CarController:
<?php
namespace App\Controllers;

use App\Models\Car;
use TypeRocket\Controllers\WPPostController;
use TypeRocket\Http\Request;
use TypeRocket\Http\Response;
use TypeRocket\Models\AuthUser;

class CarController extends WPPostController
{
    protected $modelClass = Car::class;

    public function onActionSave($args) {
            /** my custom logic here */
    }

    public function onActionUpdate($car) {
            /** my custom logic here */
    }

    public function onActionDestroy($car) {
            /** my custom logic here */
   }
     
    public function update($id, Request $request, Response $response, AuthUser $user)
    {
    	/** my custom logic before update here */
        parent::update($id, $request, $response, $user);
    }
    
    public function destroy($id, Request $request, Response $response, AuthUser $user)
    {
    	/** my custom logic before destroy here */
        parent::destroy($id, $request, $response, $user);
    }
}
  • User:
<?php
namespace App\Models;

use TypeRocket\Models\WPUser;

class User extends WPUser
{

}
  • UserController:
<?php
namespace App\Controllers;

use App\Models\User;
use TypeRocket\Controllers\WPUserController;

class UserController extends WPUserController
{
    protected $modelClass = User::class;
    
    public function onActionSave($args) {
            /** my custom logic here */
    }

    public function onActionUpdate($car) {
            /** my custom logic here */
    }

    public function onActionDestroy($car) {
            /** my custom logic here */
   }
}

All the above is run using Typerocket v5.1 on WP 6 and PHP 7.4. I tested the behavior on TR installed as a mu-plugin using composer and on TR installed as a plugin.

gogolander avatar Oct 26 '22 09:10 gogolander

Hey @gogolander

This is a good question.

TLDR; The short answer is that the TypeRocket does not call a controller's destroy() method when using WordPress delete functionality. This only happens for the edit screen. You will need to use WordPress hooks to implement your functionality for deletes.


The destroy function only works when using the TypeRocket REST-like API. The main reason for the implementation, the way it is now, is that delete hooks would happen at the model level and not the controller level - unless you only wanted specific actions to happen on a delete-controller-specific request (which might be what you are wanting). This is also true for edit/create functionality.

We may look into connecting the destroy controller methods down the road but WordPress does not make building those connections very straight forward.

kevindees avatar Oct 26 '22 17:10 kevindees

Hi @kevindees,

is there a way simply fire the delete event in the controllers whenever the WP hook is called? Someway to get the flow back to the standard lifecycle if though some actions are thrown unconventionally, so to speak.

As a note to the first part of Your answer:

This only happens for the edit screen.

I tried as I understood your comment, I hit the delete button on the edit screen and it does not fire the delete action but an update because it actually is a "trash" action.

Thanks

gogolander avatar Oct 31 '22 08:10 gogolander