Created
August 20, 2014 01:23
-
-
Save leocaseiro/9b99a714341937d73c3e to your computer and use it in GitHub Desktop.
WordPress Plugin with Custom Post and Metabox using Singleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( !class_exists('Primped_Base_Singleton') ): | |
abstract class Primped_Base_Singleton { | |
/** | |
* Parsed options for module | |
* | |
* @var array | |
*/ | |
protected $options; | |
/** | |
* List of default options for module | |
* | |
* @var array | |
*/ | |
static protected $default_options = array(); | |
/** | |
* Collection of modules objects | |
* | |
* @var array | |
*/ | |
static private $instances; | |
static public function get_instance($options =array()) { | |
$class = get_called_class(); | |
if ( !isset(self::$instances[$class]) ) { | |
if( !is_array($options) || !is_array($class::$default_options) ) { | |
throw new InvalidArgumentException('Module options have to be an Array.'); | |
} | |
self::$instances[$class] = new $class($options); | |
} | |
return self::$instances[$class]; | |
} | |
/** | |
* Protected constructor | |
* @param array | |
*/ | |
protected function __construct($options =array()) { | |
$this->options = $this->parseOptions($options); | |
} | |
/** | |
* Parses options array for module | |
* @param array | |
* @return array | |
*/ | |
protected function parseOptions($options) { | |
return $options; | |
} | |
final private function __clone() { } | |
} | |
endif; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once( __DIR__ . '/class-wp-cpt-plugin-base-singleton.php' ); | |
if ( !class_exists('WP_CPT_Plugin') { | |
class WP_CPT_Plugin extends Primped_Base_Singleton { | |
const PLUGIN_VERSION = '1.0'; | |
const OPTIONS_PREFIX = 'wp_cpt_plugin_'; | |
const CPT_SW_NAME = 'wp_cpt_plugin'; | |
const CPT_SW_NAME_SINGULAR = 'WP CPT Plugin'; | |
const CPT_SW_NAME_PLURAL = 'WP CPT Plugins'; | |
protected function __construct( $options = array() ) { | |
parent::__construct( $options ); | |
// Registering CPT | |
add_action( 'init', array($this, 'register_cpt_wpcptplugins') ); | |
//Saving a POST including Custom Fields | |
add_action( 'save_post', array($this, 'save_post'), 10, 2 ); | |
} | |
/** | |
* Create Temporary Table for Property Photos Process | |
*/ | |
public static function do_install() { | |
global $wpdb; | |
update_option(self::OPTIONS_PREFIX . 'version', self::PLUGIN_VERSION); | |
} | |
/** | |
* Register custom post type for wpcptplugins | |
*/ | |
function register_cpt_wpcptplugins() { | |
$labels = array( | |
'name' => _x( self::CPT_SW_NAME_PLURAL, self::CPT_SW_NAME ), | |
'singular_name' => _x( self::CPT_SW_NAME_SINGULAR, self::CPT_SW_NAME ), | |
'add_new' => _x( 'Add New', self::CPT_SW_NAME ), | |
'add_new_item' => _x( 'Add New ', strtolower(self::CPT_SW_NAME_SINGULAR), self::CPT_SW_NAME ), | |
'edit_item' => _x( 'Edit ' . self::CPT_SW_NAME_SINGULAR, self::CPT_SW_NAME ), | |
'new_item' => _x( 'New ' . self::CPT_SW_NAME_SINGULAR, self::CPT_SW_NAME ), | |
'view_item' => _x( 'View ' . self::CPT_SW_NAME_SINGULAR, self::CPT_SW_NAME ), | |
'search_items' => _x( 'Search ' . self::CPT_SW_NAME_PLURAL, self::CPT_SW_NAME ), | |
'not_found' => _x( 'No agencies found', self::CPT_SW_NAME ), | |
'not_found_in_trash' => _x( 'No ' . strtolower(self::CPT_SW_NAME_PLURAL) . ' found in Trash', self::CPT_SW_NAME ), | |
'parent_item_colon' => _x( 'Parent ' . self::CPT_SW_NAME_PLURAL, self::CPT_SW_NAME ), | |
'menu_name' => _x( self::CPT_SW_NAME_PLURAL, self::CPT_SW_NAME ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'hierarchical' => false, | |
'description' => 'Switcher for PRIMPED', | |
'supports' => array( 'title', 'thumbnail', 'custom-fields' ), | |
'public' => false, | |
'menu_icon' => 'dashicons-format-gallery', | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'menu_position' => 5, | |
'show_in_nav_menus' => true, | |
'publicly_queryable' => true, | |
'exclude_from_search' => true, | |
'has_archive' => false, | |
'query_var' => true, | |
'can_export' => true, | |
'rewrite' => true, | |
'capability_type' => 'page', | |
'register_meta_box_cb' => 'WP_CPT_Plugin::wpcptplugins_add_meta_box' | |
); | |
register_post_type( self::CPT_SW_NAME, $args ); | |
} | |
public static function wpcptplugins_add_meta_box() { | |
add_meta_box( | |
'wpcptplugin-metabox', | |
'Settings', | |
__CLASS__ . '::markup_meta_boxes', | |
self::CPT_SW_NAME, | |
'normal', | |
'core' | |
); | |
} | |
/** | |
* Builds the markup for all meta boxes | |
* | |
* @param object $post | |
* @param array $box | |
*/ | |
public static function markup_meta_boxes( $post, $box ) { | |
$variables = array(); | |
switch ( $box['id'] ) { | |
case 'wpcptplugin-metabox': | |
$variables['exampleBoxField'] = get_post_meta( $post->ID, 'wpcptplugin-metabox-field', true ); | |
echo '<p>Meta box introduction / user instructions</p> | |
<table id="wpcptplugin-metabox" class="form-table"> | |
<tbody> | |
<tr> | |
<th> | |
<label for="wpcptplugin-metabox-field">Caption</label> | |
</th> | |
<td> | |
<input id="wpcptplugin-metabox-field" name="wpcptplugin-metabox-field" type="text" class="regular-text" value="' . esc_attr( $variables['exampleBoxField'] ) . '" /> | |
</td> | |
</tr> | |
</tbody> | |
</table>'; | |
break; | |
/* | |
case 'wpps_some-other-box': | |
$variables['someOtherField'] = get_post_meta( $post->ID, 'wpps_some-other-field', true ); | |
$view = 'wpps-cpt-example/metabox-another-box.php'; | |
break; | |
*/ | |
default: | |
$view = false; | |
break; | |
} | |
} | |
/** | |
* Saves values of the the custom post type's extra fields | |
* | |
* @param int $post_id | |
* @param object $post | |
*/ | |
public static function save_post( $post_id, $revision ) { | |
global $post; | |
$ignored_actions = array( 'trash', 'untrash', 'restore' ); | |
if ( isset( $_GET['action'] ) && in_array( $_GET['action'], $ignored_actions ) ) { | |
return; | |
} | |
if ( ! $post || $post->post_type != self::CPT_SW_NAME ) { | |
return; | |
} | |
self::save_custom_fields( $post_id, $_POST ); | |
} | |
/** | |
* Validates and saves values of the the custom post type's extra fields | |
* | |
* @param int $post_id | |
* @param array $new_values | |
*/ | |
protected static function save_custom_fields( $post_id, $new_values ) { | |
if ( isset( $new_values[ 'wpcptplugin-metabox-field' ] ) ) { | |
if ( true ) { // some business logic check | |
update_post_meta( $post_id, 'wpcptplugin-metabox-field', $new_values[ 'wpcptplugin-metabox-field' ] ); | |
} else { | |
echo( 'Example of failing validation'); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: WP CPT Plugin | |
Plugin URI: # | |
Description: Example of CPT Plugin with Metabox | |
Version: 1.0 | |
Author: Leo Caseiro | |
Author URI: http://leocaseiro.com.br/ | |
*/ | |
require_once( __DIR__ . '/class-wp-cpt-plugin.php' ); | |
register_activation_hook(__FILE__, array('WP_CPT_Plugin', 'do_install') ); | |
WP_CPT_Plugin::get_instance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment