radiate-framework / framework

A WordPress plugin and theme framework
https://radiate-framework.github.io/
MIT License
4 stars 0 forks source link

Meta save/update/delete methods #80

Closed BenRutlandWeb closed 3 years ago

BenRutlandWeb commented 3 years ago

Is your feature request related to a problem? Please describe. The HasMeta trait allows for the object meta to be fetched. Nice enough. But it would be cool if you could update/delete the meta data too.

Describe the solution you'd like A method or getter/setter to update and delete the meta data attached to the particular post. E.g.

// new method to update multiple attributes
User::find(1)->meta->update([
  'first_name' => 'changed',
]);

$meta = User::find(1)->meta;
$meta->first_name = 'changed'; // this already works, but doesn't persist in the db
$meta->save(); // new method to persist the entire Meta object

// new method to delete a single property
Post::find(1)->meta->delete('meta_description');

Describe alternatives you've considered Using update_metadata and delete_metadata

Additional context The Meta model already exists, additional methods would need to be added to use update_metadata and delete_metadata under the hood.

BenRutlandWeb commented 3 years ago

The meta property returns an instance of Meta which currently has no knowledge of the object type calling the HasMeta trait.

Passing the object type to the Meta constructor would allow for the Meta object to have the save, update methods.

delete works quite differently from the Models version, in that the property is passed instead of deleting the whole "model". I think this should work more like the getters/setters, as it's unlikely that one would want to delete all meta.

The Meta class would also need to handle clean/dirty values so as to not update every property on save. ANother thing to consider is the meta caching strategy. Currently, the getter stores the entire object meta as a property instead of querying the db on every call.

BenRutlandWeb commented 3 years ago

The Meta class also needs to know the Model relationship. Perhaps it's better to pass the Model to the Meta class in order to perform the actions related to the model.

BenRutlandWeb commented 3 years ago

Instead of using an "accessor" for meta, the meta should be accessed via a method invocation. This is consistent with laravel's relationships - the return is an instance of Meta instead of a Builder.

There isn't a simple way to use WordPress APIs to bulk update/delete attributes. These will need to be iterated over to update/delete. The getDirty method is useful here to only update the desired properties.

Another point to make is: unsetting attributes don't change the Meta class' clean/dirty state. Upon save, the meta values are updated, but not deleted. Meta will have to be manually and obviously deleted with the provided method rather than relying on the model's state to stay in sync with the db.