Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actions of controllers not called #255

Open
gogolander opened this issue Oct 26, 2022 · 2 comments
Open

Actions of controllers not called #255

gogolander opened this issue Oct 26, 2022 · 2 comments

Comments

@gogolander
Copy link

gogolander commented Oct 26, 2022

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.

@kevindees
Copy link
Member

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.

@gogolander
Copy link
Author

gogolander commented Oct 31, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants