Giter VIP home page Giter VIP logo

posty's Introduction

Posty

Posty is an object orientated post type manager for WordPress. It makes it a breeze to manage your post types and any custom columns.

Roadmap

  • Post Type
  • Columns
  • Sortable Columns
  • Statuses
  • Tags/Taxonomies

Installation

Posty has no dependencies, and requires PHP >= 7.4.

composer require keironlowe/posty

Usage

Posty provides a fluent API for managing both your post types and columns. To get started, just use the make method, providing the singular and plural names, to create a new post type. It's important to note that the register method must always be called last. Any changes made after the register method won't take effect.

Posty\Posty::make('Product', 'Products')->register();

This post type slug/ID will be automatically generated based on the singular name, so in this case it would be product. Optionally, you can pass a third argument to define this yourself.

Setting labels and arguments

Posty handles setting up all the labels, along with some sensible default arguments, but we know that one size doesn't fit all, so you can update these using the setLabels and setArguments methods.

Posty\Posty::make('Product', 'Products')
    ->setLabels()
    ->setArguments()
    ->register();

Both setLabels and setArguments should receive an array, this can either by passed directly, or as a result of a callback function.

Posty\Posty::make('Product', 'Products')
    ->setLabels([
        // All labels
    ])
    ->register();
Posty\Posty::make('Product', 'Products')
    ->setLabels(function ($labels) {
        $labels['menu_name'] = 'Overwrite value'

        return $labels;
    })
    ->register();

Columns

To manage the columns, we first need to grab the ColumnRepository instance using the columns method. This class has the add, remove and reorder methods. Each of these methods should receive an array, this can either by passed directly, or as a result of a callback function.

$products = Posty\Posty::make('Product', 'Products');
$columns  = $products->columns();

Adding Columns

The add method should receive an array of columns. Each column should be an array of key => value pairs, with two required elements, label and value

The label is the label for the column, and the value should be a function which takes the ID of the post, and returns the correct value. Optionally, there is also the order element, which should be an integer and allows you to reorder the column.

The ID of the field is automatically generated from the label, but in the case you need to manually set this, you can use the id element.

You can make the column sortable by setting the sort element to either alpha or numeric.

$columns->add([
    [
        'label' => 'Price'
        'value' => fn (int $post_id) => get_field('price', $post_id)
        'order' => 2,
        'sort'  => 'numeric'
    ],
    [
        'label' => 'Image'
        'value' => fn (int $post_id) => get_field('image', $post_id)
        'order' => 3,
        'id'    => 'alternate_image',
        'sort'  => 'alpha'
    ]
]);

$columns->add(function (array $existingColumns) {
    // Return column array
});

Removing Columns

The remove method should receive an array of column IDs to be removed. By default, custom post types have cb (checkbox), title, author and date columns which you can remove if neccessary.

$columns->remove(['author', 'date']);

Reordering Columns

The reorder method should receive an array of column IDs in the order that you wish. By default, custom post types have cb (checkbox), title, author and date columns which you should bear in mind when reordering. Any columns that aren't included in the array will be added at the end.

$columns->reorder(['cb', 'title', 'price', 'image']);

posty's People

Contributors

keironlowe avatar

Stargazers

 avatar Thibaud Allie avatar Maciek Palmowski avatar Pierre Lannoy avatar Nicolas Lemoine avatar George Mamadashvili avatar Chris Van Patten avatar Philipp avatar Tang Rufus avatar Eric Saner avatar Carmelo Santana avatar Chris Vasey avatar QWp6t avatar Max Hoogenbosch avatar Nikolay Nikolaev avatar Arman Poghosyan avatar Bryan Erwin avatar Gary Blankenship avatar Chuck Adams avatar Adam Thewlis avatar  avatar Benedict avatar

Watchers

James Cloos avatar  avatar

posty's Issues

Add column instances, rather than array.

For v2, I'm considering updating the ColumnRepository::add method to just accept an array of Column instances, rather than an array or callback.

The primary reason for this is flexibility, by adding instances directly, you can easily call any other Column methods needed, while with an array it's something I'll constantly need to update and could potentially end up with a lot of options for the array.

So something like...

$columns->add([
    new Column('Price, fn (int $postId) => get_field('price', $postId))
]);

rather than

$columns->add([
    [
        'label' => 'Price',
        'value' => fn (int $postId) => get_field('price', $postId)
    ]
]);

Let me know if there are any objections.

Default columns are hard coded

To effectively manage the columns, we need to store the default columns WP uses into the ColumnRepository instance. At the moment, I can't find a good way to do this so it's had to be hard coded in the getDefaultColumns method.

Using the filters or actions ends up in race conditions, where at the point of Posty being used, the actions haven't run.

I need to find a reliable way of storing the default columns, any help is appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.