Skip to content

Actions of controllers not called #255

@gogolander

Description

@gogolander

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions