PHP code example of devstudio-rs / wpmvc

1. Go to this page and download the library: Download devstudio-rs/wpmvc library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devstudio-rs / wpmvc example snippets


$config = array(
    'components' => array(
        'request' => array(
            'class' => Request::class,
        ),
    ),
);

( new App( $config ) )->init();

App::$app
App::$app->request->post();

class Event extends \wpmvc\models\Post_Model {

    public $post_type = 'event';
    
    // Example of custom attributes with default values.
    public $event_date     = 1707422767;
    public $event_location = 'Bratislava';
    
    public function registry() : array{
        return array(
            'public'             => true,
            'publicly_queryable' => true,
            'show_ui'            => true,
            ...
        );
    }
    
    public function registry_labels() : array {
        return array(
            'name' => __( 'Event' ),
            ...
        );
    }

}

$event = Event::find_one( 24 );
$event = new Event();

$event->set_attribute( 'post_title', 'Great Event' );
$event->set_attribute( 'event_location', 'Bratislava' );

$event->set_attributes( array( 
    'post_title'     => 'Great Event',
    'event_location' => 'Bratislava',
) );

$event->load( array( 
    'Event' => array(
        'post_title'     => 'Great Event',
        'event_location' => 'Bratislava',
    ),
) );

$event->save();
$event->delete();

Event::register( array(
    'labels' => array(
        'name' => __( 'Events' ),
        ...
    ),
) );

class Event_Category extends \wpmvc\models\Taxonomy_Model {

    public $taxonomy = 'event-category';

    public function registry() : array {
        return array(
            'hierarchical'      => true,
            'public'            => true,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'show_in_rest'      => true,
        );
    }

    public function registry_labels() : array {
        return array(
            'name' => __( 'Event Category' ),
            ...
        );
    }

    public function registry_object_type() : array {
        return array( 'event' );
    }

}

$events = Event::find()
    ->where_taxonomy( Event_Category::class, array( 'slug' => 'taxonomy_slug' ) )
    ->all();

class Site_Controller extends \wpmvc\web\Controller {

    public function action_index() {
        // Action logic.
    }

}

App::$app->router->add_route( 'site', array( Site_Controller::class, 'action_index' ) );

class Event_Options_Controller extends \wpmvc\web\Meta_Box_Controller {

    public function on_action( $model ) {
        echo View::render( 'views/events/meta-box', array(
            'model' => $model,
        ) );
    }

    public function on_save( $model ) {
        if ( $model->load( App::$app->request->post() ) ) {
            $model->save();
        }
    }

}

class Event_Options_Meta_Box extends \wpmvc\models\Meta_Box_Model {

    public $id         = 'event_options';
    public $controller = Event_Options_Controller::class;

    public function init() {
        $this->set_title( __( 'Options' ) );
    }

}

class Event extends \wpmvc\models\Post_Model {

    public $post_type = 'event';
    
    public function init() {
        $this->add_meta_box( Event_Options_Meta_Box::class );
    }

}