| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
// No direct access allowed. |
| 7 |
defined('ABSPATH') or die("Access denied"); |
| 8 |
|
| 9 |
// Import dependencies. |
| 10 |
cssJSToolbox::import('tables:blocks.php'); |
| 11 |
|
| 12 |
/** |
| 13 |
* |
| 14 |
*/ |
| 15 |
class CJTMetaboxModel { |
| 16 |
|
| 17 |
/** */ |
| 18 |
const STATE_CREATED = 'created'; |
| 19 |
|
| 20 |
/** */ |
| 21 |
const STATE_DELETED = 'deleted'; |
| 22 |
|
| 23 |
/** |
| 24 |
* put your comment there... |
| 25 |
* |
| 26 |
* @var mixed |
| 27 |
*/ |
| 28 |
private $dbDriver = null; |
| 29 |
|
| 30 |
/** |
| 31 |
* put your comment there... |
| 32 |
* |
| 33 |
* @var mixed |
| 34 |
*/ |
| 35 |
protected $post = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* put your comment there... |
| 39 |
* |
| 40 |
* @param mixed $post |
| 41 |
* @return CJTPostModel |
| 42 |
*/ |
| 43 |
public function __construct($params) { |
| 44 |
// Get post object from post Id. |
| 45 |
$this->post = get_post($params[0]); |
| 46 |
// Import dependencies. |
| 47 |
cssJSToolbox::import('framework:db:mysql:queue-driver.inc.php', 'framework:db:mysql:table.inc.php'); |
| 48 |
// Instantiate MYSQL DB Driver object. |
| 49 |
$this->dbDriver = new CJTMYSQLQueueDriver($GLOBALS['wpdb']); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* put your comment there... |
| 54 |
* |
| 55 |
* @param mixed $blockId |
| 56 |
*/ |
| 57 |
public function create(& $pin = null) { |
| 58 |
// Import dependecnes. |
| 59 |
cssJSToolbox::import('tables:block-pins.php'); |
| 60 |
// Change metabox status to be created. |
| 61 |
$this->setState(self::STATE_CREATED); |
| 62 |
// Add post pin to pins table. |
| 63 |
$blockPinsTable = new CJTBlockPinsTable($this->dbDriver); |
| 64 |
// Pin data. |
| 65 |
$pin = (object) array(); |
| 66 |
// Import CJTBlockModel class. |
| 67 |
CJTModel::import('block'); |
| 68 |
/** @todo Only temporary in version 6.0. Later versions should group all post types by post type name! */ |
| 69 |
switch ($this->getPost()->post_type) { |
| 70 |
case 'page': |
| 71 |
$pin->pin = 'pages'; |
| 72 |
$pin->flag = CJTBlockModel::PINS_PAGES_CUSTOM_PAGE; |
| 73 |
break; |
| 74 |
default: |
| 75 |
$pin->pin = 'posts'; |
| 76 |
$pin->flag = CJTBlockModel::PINS_POSTS_CUSTOM_POST; |
| 77 |
break; |
| 78 |
} |
| 79 |
$pin->value = $this->getPost()->ID; |
| 80 |
// Add pin record. |
| 81 |
$blockPinsTable->insertRaw($this->getMetaboxId(), array($pin)); |
| 82 |
// Chains! |
| 83 |
return $this; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* put your comment there... |
| 88 |
* |
| 89 |
*/ |
| 90 |
public function delete() { |
| 91 |
return $this->setState(self::STATE_DELETED); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Check whether is the current post type |
| 96 |
* can has CJT Block metabox? |
| 97 |
* |
| 98 |
*/ |
| 99 |
public function doPost() { |
| 100 |
// Get all post types selected by user. |
| 101 |
$metaboxSettings = CJTModel::create('settings')->loadPage('metabox'); |
| 102 |
// Return true is post types selected, otherwise return false. |
| 103 |
$allowedPostTypes = $metaboxSettings->postTypes; |
| 104 |
return in_array($this->getPost()->post_type, $allowedPostTypes); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* put your comment there... |
| 109 |
* |
| 110 |
*/ |
| 111 |
public function getMetaboxId() { |
| 112 |
$metaboxId = get_post_meta($this->getPost()->ID, CJTBlocksTable::BLOCK_META_BOX_ID_META_NAME, true); |
| 113 |
// Cast Metabox to integer. |
| 114 |
$metaboxId = (int) $metaboxId; |
| 115 |
return $metaboxId; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* put your comment there... |
| 120 |
* |
| 121 |
*/ |
| 122 |
public function getPost() { |
| 123 |
return $this->post; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* put your comment there... |
| 128 |
* |
| 129 |
*/ |
| 130 |
public function hasBlock() { |
| 131 |
// If $postMeta seto to a blockId then it has block otherwise(0) it hasn't. |
| 132 |
$metaboxStatus = get_post_meta($this->getPost()->ID, CJTBlocksTable::BLOCK_META_BOX_STATUS_META_NAME, true); |
| 133 |
return ($metaboxStatus == self::STATE_CREATED); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* put your comment there... |
| 138 |
* |
| 139 |
*/ |
| 140 |
public function reservedMetaboxBlockId() { |
| 141 |
// Reserved if only if not already taken! |
| 142 |
if (!$reservedId = $this->getMetaboxId()) { |
| 143 |
// Get Blocks table instance. |
| 144 |
$BlocksTable = new CJTBlocksTable($this->dbDriver); |
| 145 |
// Get next Id. |
| 146 |
$reservedId = $BlocksTable->getNextId(); |
| 147 |
// Set metabox reserved id. |
| 148 |
update_post_meta($this->getPost()->ID, CJTBlocksTable::BLOCK_META_BOX_ID_META_NAME, $reservedId); |
| 149 |
} |
| 150 |
return $reservedId; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* put your comment there... |
| 155 |
* |
| 156 |
*/ |
| 157 |
public function save() { |
| 158 |
$this->dbDriver->processQueue(); |
| 159 |
// Chains |
| 160 |
return $this; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* put your comment there... |
| 165 |
* |
| 166 |
* @param mixed $state |
| 167 |
*/ |
| 168 |
public function setState($state) { |
| 169 |
// Update state post meta var. |
| 170 |
update_post_meta($this->getPost()->ID, CJTBlocksTable::BLOCK_META_BOX_STATUS_META_NAME, $state); |
| 171 |
// Chaining! |
| 172 |
return $this; |
| 173 |
} |
| 174 |
|
| 175 |
} // End class. |