Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active May 25, 2017 17:12
Show Gist options
  • Save tommcfarlin/38c72277ddbd0fa70a74c03598070703 to your computer and use it in GitHub Desktop.
Save tommcfarlin/38c72277ddbd0fa70a74c03598070703 to your computer and use it in GitHub Desktop.
[WordPress] Rapid Prototyping: Prototype To Code, Part 2
<?php
/**
* Display content for the meta box when requested.
*
* @author Tom McFarlin
* @since 0.2.0
*/
namespace McFarlin\TRP\Utility;
use McFarlin\TRP\Utility\Post_Query;
/**
* Retrieves information from the class responsible for querying the database and
* renders it in the context of our meta box when called via the Meta Box Display.
*
* @author Tom McFarlin
* @since 0.2.0
*/
class Post_Messenger {
// Snip for brevity.
}
<?php
/**
* Instantiates the class by setting a reference to the query.
*
* @param string $plugin_dir The path to the root of the plugin directory.
*/
public function __construct( $plugin_dir ) {
$this->query = new Post_Query();
$this->plugin_dir = trailingslashit( $plugin_dir );
}
<?php
/**
* Displays the description of the content of the meta box.
*
* @access private
*/
private function get_post_message() {
include_once $this->plugin_dir . 'Display/Views/post-list.php';
}
/**
* Displays the description of the content of the meta box.
*
* @access private
*/
private function get_description() {
include_once $this->plugin_dir . 'Display/Views/message-description.php';
}
/**
* Displays a message of there are no recent posts.
*
* @access private
*/
private function get_no_posts_message() {
include_once $this->plugin_dir . 'Display/Views/no-post-list.php';
}
<?php
namespace McFarlin\TRP\Utility;
/**
* Queries the database for three most recent posts. Returns the query to the
* caller so that it can be interrogates for posts or not.
*
* @author Tom McFarlin
* @since 0.2.0
*/
class Post_Query {
// Snip for brevity.
private function get_posts() {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'desc',
);
$this->query = new \WP_Query( $args );
return $this->query;
}
}
<?php
namespace McFarlin\TRP\Display;
use McFarlin\TRP\Display\Meta_Box_Display;
/**
* Registers the Meta Box with WordPress. Defines the ID, title, display function,
* and the post type on which it will live.
*
* @author Tom McFarlin
* @since 0.2.0
*/
class Meta_Box {
/**
* A reference to the class that will display the contents in the meta box.
*
* @access private
* @var Meta_Box_Display
*/
private $meta_box_display;
/**
* Instantiates the class by setting its property equal to a reference to its display.
*
* @param string $plugin_dir A reference to the root of the plugin's directory.
*/
public function __construct( $plugin_dir ) {
$this->meta_box_display = new Meta_Box_Display( $plugin_dir );
}
// Snip for brevity.
}
<?php
/**
* Defines the display for the meta box.
*
* @author Tom McFarlin
* @since 0.2.0
*/
namespace McFarlin\TRP\Display;
use McFarlin\TRP\Utility\Post_Messenger;
/**
* Defines the display for the meta box that will render the content in the
* context of its meta box.
*
* @author Tom McFarlin
* @since 0.2.0
*/
class Meta_Box_Display {
/**
* A reference to the class that will display the contents in the meta box.
*
* @access private
* @var Post_Messenger
*/
private $messenger;
/**
* Instantiates the object by setting a property equal to that of the class
* responsible for rendering the messages from the post query.
*
* @param string $plugin_dir A reference to the root of the plugin's directory.
*/
public function __construct( $plugin_dir ) {
$this->messenger = new Post_Messenger( $plugin_dir );
}
/**
* If there are posts to display, renders them in the metabox. Otherwise, displays
* a note that there are no posts to display.
*/
public function display() {
$this->messenger->get_message();
}
}
<?php
namespace McFarlin\TRP;
use McFarlin\TRP\Display\Meta_Box;
include 'Display/class-meta-box.php';
include 'Display/class-meta-box-display.php';
include 'Utility/class-post-messenger.php';
include 'Utility/class-post-query.php';
add_action( 'add_meta_boxes', __NAMESPACE__ . '\\trp_start' );
/**
* Starts the plugin.
*/
function trp_start() {
$meta_box = new Meta_Box( dirname( __FILE__ ) );
$meta_box->init();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment