GutenPress

An OOP toolset for WordPress.

At the moment, GutenPress it's a rough-draft for a bunch of tools for developing themes or plugins for WordPress.

Currently working:

Installation

Usage

Custom Post Type generator

Using the custom query class

Let's suppose we just created a Songs custom post type.

Getting a list of songs works just like directly calling WP_Query, so you can use all the arguments you could use with it, including tax queries and meta queries:

$latest_songs = new SongsQuery( array(
    'posts_per_page' => '10',
    'tax_query' => array(
        array(
            'taxonomy' => 'performed_by',
            'terms' => 'pink-floyd',
            'field' => 'slug'
        )
    ),
    'meta_query' => array(
        array(
            'key' => 'author_composer',
            'value' => 'David Gilmour'
        )
    )
) );

Since SongsQuery (or whatever your generated CPT class is called) implements the Iterator interface, you can loop through the objects with a simple foreach. It also implements the Countable interface, so you can check if your query actually has items:

if ( count($latest_songs) ) :
    foreach ( $latest_songs as $song ) :
        // do stuff
        // each $song will be an instance of SongObject
        echo $song->authors;
    endforeach;
endif;

Using and extending custom post type objects

You can extend your "Object" class to add custom methods around WP_Post:

class SongObject extends \GutenPress\Model\PostObject{
    public function getAuthors(){
        $composer = $this->post->author_composer;
        $lyrics   = $this->post->author_lyrics;
        if ( $composer === $lyrics ) {
            return 'Music and Lyrics by '. $lyrics;
        } else {
            return 'Music: '. $composer .' / Lyrics: '. $lyrics;
        }
    }
    /**
     * You can also overwrite the __get() magic method
     */
    public function __get( $key ){
        if ( $key === 'authors' ) {
            return $this->getAuthors();
        }
        // pass it on to WP_Post
        parent::__get( $key );
    }
}

Adding metaboxes

You can use the \GutenPress\Model\PostMeta class to add a metabox to your CPT:

// using the \GutenPress\Model namespace as Model;
class SongAuthors extends Model\PostMeta{
    protected function setId(){
        // will be used for the metabox ID
        // will be prepended to the metadata defined by this class
        return 'author';
    }
    protected function setDataModel(){
        return array(
            new Model\PostMetaData(
                'composer',
                'Composer',
                '\GutenPress\Forms\Element\InputText', // can be any of the Elements defined on the corresponding folder
                array(
                    'placeholder' => 'Who composed the music for this song?'
                )
            ),
            new Model\PostMetaData(
                'lyrics',
                'Lyrics',
                '\GutenPress\Forms\Element\InputText',
                array(
                    'placeholder' => 'Who wrote the lyrics?'
                )
            )
        );
    }
}

// finally, register as metabox
new Model\Metabox( 'SongAuthors', 'Authorship information', 'song', array('priority' => 'high') );