PHP code example of pwpf / demo

1. Go to this page and download the library: Download pwpf/demo 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/ */

    

pwpf / demo example snippets



// Full Class Name with Namespace
$router
    ->registerRouteOfType( RouteType::FRONTEND )
    ->withController( 'Example_Me\App\Controllers\Frontend\Print_Posts_Shortcode@registerShortcode' )
    ->withModel( 'Example_Me\App\Models\Frontend\Print_Posts_Shortcode' );

// ------------- OR --------------------

// Class Names Without specifying Namespaces explicitly. Boilerplate will automatically figure out the class based on the Route Type.
$router
    ->registerRouteOfType( RouteType::FRONTEND )
    ->withController( 'Print_Posts_Shortcode@registerShortcode' )
    ->withModel( 'Print_Posts_Shortcode' );



// file: example-me/app/controllers/frontend/class-print-posts-shortcode.php

namespace Example_Me\App\Controllers\Frontend;

if ( ! class_exists( __NAMESPACE__ . '\\' . 'Print_Posts_Shortcode' ) ) {
    /**
     * Class that handles `example_me_print_posts` shortcode
     *
     * @since      1.0.0
     * @package    Example_Me
     * @subpackage Example_Me/Controllers/Frontend
     */
    class Print_Posts_Shortcode extends Base_Controller {

        /**
         * Registers the `example_me_print_posts` shortcode
         *
         * @return void
         * @since 1.0.0
         */
        public function registerShortcode() {
            add_shortcode( 'example_me_print_posts', array( $this, 'print_posts_callback' ) );
        }

        /**
         * @ignore Blank Method
         */
        protected function register_hook_callbacks(){}

        /**
         * Callback to handle `example_me_print_posts` shortcode
         *
         * @return void
         * @since 1.0.0
         */
        public function print_posts_callback( $atts ) {
            return     $this->get_view()->render_template(
                'frontend/print-posts-shortcode.php',
                [
                    'fetched_posts'    =>    $this->get_model()->get_posts_for_shortcode( 'example_me_print_posts', $atts )
                ]
            );

        }

    }
}



// file: `example-me/app/models/frontend/class-print-posts-shortcode.php`

namespace Example_Me\App\Models\Frontend;

if ( ! class_exists( __NAMESPACE__ . '\\' . 'Print_Posts_Shortcode' ) ) {
    /**
     * Class to handle data related operations of `example_me_print_posts` shortcode
     *
     * @since      1.0.0
     * @package    Example_Me
     * @subpackage Example_Me/Models/Frontend
     */
    class Print_Posts_Shortcode extends Example_Me\App\Models\AbstratModel {
        /**
         * Fetches posts from database
         *
         * @param string $shortcode Shortcode for which posts should be fetched
         * @param array $atts Arguments passed to shortcode
         * @return \WP_Query WP_Query Object
         */
        public function get_posts_for_shortcode( $shortcode, $atts ) {
            $atts = shortcode_atts(
                array(
                    'number_of_posts' => '10',
                ), $atts, $shortcode
            );

            $args = array(
                'post_type' => 'post',
                'posts_per_page' => is_int( $atts['number_of_posts'] ) ? $atts['number_of_posts'] : 10,
            );

            return new \WP_Query( $args );
        }
    }
}


 

    /**  */

    public function __construct(Model $model, $view = false)
    {
        $config = [
            'appName' => 'Plugin_Name'
        ];

        $view = new View($config);
        parent::__construct($model, $view);
    }



// file: `example-me/app/templates/frontend/print-posts-shortcode.php`

 if ( $fetched_posts->have_posts() ) :