| 1 |
<?php |
| 2 |
/** |
| 3 |
* @version post.php $ Id; 03-08-2012 00:51:00 Ahmed Said ; |
| 4 |
*/ |
| 5 |
|
| 6 |
// Disallow direct access. |
| 7 |
defined('ABSPATH') or die("Access denied"); |
| 8 |
|
| 9 |
// import dependencies. |
| 10 |
cssJSToolbox::import('framework:mvc:controller-ajax.inc.php'); |
| 11 |
|
| 12 |
/** |
| 13 |
* Controll Block metabox actions. |
| 14 |
* |
| 15 |
* @version 6.0 |
| 16 |
* @package CJT |
| 17 |
* @subpackage Controllers |
| 18 |
* @author Ahmed Said |
| 19 |
*/ |
| 20 |
class CJTMetaboxController extends CJTAjaxController { |
| 21 |
|
| 22 |
/** |
| 23 |
* Initialize post controller. |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public function __construct($_unused_hasView = null, $request = null) { |
| 28 |
// Initialize parent. |
| 29 |
parent::__construct(false, $request); |
| 30 |
// Initialize. |
| 31 |
$postId = $this->getRequestParameter('post'); |
| 32 |
// Instantiate model object. |
| 33 |
$this->model = self::getModel('metabox', array($postId)); |
| 34 |
// Don't regiter Ajax actions unless AAP (Ajax Access Point) is loaded! |
| 35 |
$connectedObject =& CJTAccessPoint::isConnected(); |
| 36 |
if ($connectedObject && ($connectedObject->getName() == 'ajax')) { |
| 37 |
$this->registryAction('create'); |
| 38 |
$this->registryAction('delete'); |
| 39 |
} |
| 40 |
else if ($this->model->doPost()) { |
| 41 |
$current_screen = get_current_screen(); |
| 42 |
// Add metabox. |
| 43 |
// Hiding metabox for Gutenberg editors. |
| 44 |
if ( ! $this->hasGutenberg() ) $this->showMetabox(); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Check for Gutenberg editor. |
| 50 |
* |
| 51 |
* @return boolean |
| 52 |
*/ |
| 53 |
public function hasGutenberg() { |
| 54 |
if ( function_exists( 'is_gutenberg_page' ) && |
| 55 |
is_gutenberg_page() |
| 56 |
) { |
| 57 |
return true; |
| 58 |
} |
| 59 |
$current_screen = get_current_screen(); |
| 60 |
if ( method_exists( $current_screen, 'is_block_editor' ) && |
| 61 |
$current_screen->is_block_editor() |
| 62 |
) { |
| 63 |
// Gutenberg page on 5+. |
| 64 |
return true; |
| 65 |
} |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Create block metabox for specific post object. |
| 71 |
* |
| 72 |
* @param integer Post id. |
| 73 |
* @return array New block object consist of block id and new block metabox view content. |
| 74 |
*/ |
| 75 |
public function createAction() { |
| 76 |
// Get reserved block id from post object. |
| 77 |
$blockId = $this->model->getMetaboxId(); |
| 78 |
if ($blockId) { |
| 79 |
// Set request paremeters for blocks-ajax controller::createBlockAction. |
| 80 |
$_GET['name'] = cssJSToolbox::getText(sprintf(cssJSToolbox::getText('CJT Block - Post #%d'), $this->model->getPost()->ID)); |
| 81 |
$_GET['state'] = 'active'; |
| 82 |
// Create post metabox. |
| 83 |
$this->model->create($pin)->save(); |
| 84 |
// Create new block. |
| 85 |
$blocksController = CJTController::create('blocks-ajax'); |
| 86 |
$blocksController->createBlockAction($blockId, 'metabox', $pin->flag); |
| 87 |
// Get metabox block view object. |
| 88 |
$this->view = CJTView::create('blocks/metabox'); |
| 89 |
$this->view->setBlock(CJTModel::create('blocks')->getBlock($blockId, array('returnCodeFile' => true))); |
| 90 |
$this->view->setSecurityToken($this->createSecurityToken()); |
| 91 |
// Send JavaScript & CSS files needed for the metabox view to work. |
| 92 |
$this->response['references'] = self::getReferencesQueue(); |
| 93 |
$this->response['view'] = $this->view->setOption('customizeMetabox', true)->getTemplate('metabox'); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* put your comment there... |
| 99 |
* |
| 100 |
*/ |
| 101 |
public function deleteAction() { |
| 102 |
// Get input vars. |
| 103 |
$metaboxBlockId = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT); |
| 104 |
// Delete metabox block. |
| 105 |
$blocks = CJTModel::create('blocks'); |
| 106 |
$blocks->delete($metaboxBlockId)->save(); |
| 107 |
// Unassociate post -- Mark post as HAS-NO-BLOCK-ASSOCIATED. |
| 108 |
$this->model->delete(); |
| 109 |
// Load create-metabox view. |
| 110 |
$this->view = CJTView::create('blocks/create-metabox'); |
| 111 |
// Create DUMMY block object. |
| 112 |
$block = (object) array(); |
| 113 |
$block->id = $metaboxBlockId; |
| 114 |
$block->name = cssJSToolbox::getText('CJT Block'); |
| 115 |
// Push vars into the view. |
| 116 |
$this->view->setBlock($block); |
| 117 |
$this->view->setSecurityToken($this->createSecurityToken()); |
| 118 |
// Send JavaScript & CSS files needed for the metabox view to work. |
| 119 |
$this->response['references'] = self::getReferencesQueue(); |
| 120 |
// create-metabox view content. |
| 121 |
$this->response['view'] = $this->view->setOption('customizeMetabox', true)->getTemplate('create'); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* put your comment there... |
| 126 |
* |
| 127 |
*/ |
| 128 |
protected static function getReferencesQueue() { |
| 129 |
$result = array(); |
| 130 |
/** |
| 131 |
Get all scripts needed to be loaded for block metabox to work. |
| 132 |
-------------------------------------------------------------- |
| 133 |
* 1. Suppress the output we need the SRC URLs not the HTML tags. |
| 134 |
* 2. Fire scripts & styles action so the view would enqueue all the files. |
| 135 |
* 3. Merge script and styles into a single array. |
| 136 |
* 4. Get only files start with absoult URI (e.g start by HTTP). |
| 137 |
* files without HTTP may be already loaded (e.g jQuery, etc..) |
| 138 |
*/ |
| 139 |
ob_start(); do_action('admin_print_scripts'); do_action('admin_print_styles'); ob_end_clean(); |
| 140 |
global $wp_scripts; |
| 141 |
$references = array_merge($GLOBALS['wp_scripts']->registered, $GLOBALS['wp_styles']->registered); |
| 142 |
$queue = array_merge($GLOBALS['wp_scripts']->queue, $GLOBALS['wp_styles']->queue); |
| 143 |
foreach ($references as $script) { |
| 144 |
// use only items on the queue! |
| 145 |
if (in_array($script->handle, $queue)) { |
| 146 |
// Scripts with absoult URL is only for the metabox block |
| 147 |
// but not for Wordpress default scripts (e.g jquery, thickbox, etc...). |
| 148 |
if (strpos($script->src, 'http') === 0) { |
| 149 |
// For JS to work properly Always have cjt object set. |
| 150 |
if (!isset($script->cjt)) { |
| 151 |
$script->cjt = (object) array(); |
| 152 |
} |
| 153 |
// Refine $script object and get only src, cjt and extra->data/localization properties! |
| 154 |
$script = (object) array_intersect_key(((array) $script), array_flip(array('src', 'cjt' , 'extra'))); |
| 155 |
// Organize references into JS and CSS files lists. |
| 156 |
if (preg_match('/\.js$/', $script->src)) { |
| 157 |
$result['scripts'][] = $script; |
| 158 |
} |
| 159 |
else { |
| 160 |
$result['styles'][] = $script; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
// Return references. |
| 166 |
return $result; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Select which metabox to load. |
| 171 |
* |
| 172 |
* create-metabox view will be loaded if user doesnt |
| 173 |
* created a block for current post yet. |
| 174 |
* |
| 175 |
* metabox view will be loaded if there is a block |
| 176 |
* already created for current post. |
| 177 |
* |
| 178 |
* Callback for add_meta_boxes action. |
| 179 |
*/ |
| 180 |
public function showMetabox() { |
| 181 |
// Import blocks view. |
| 182 |
CJTView::import('blocks/manager'); |
| 183 |
/// Get block id. |
| 184 |
$metaboxId = $this->model->reservedMetaboxBlockId(); |
| 185 |
// User didn't create block for this post yet. |
| 186 |
// Show create-metabox view. |
| 187 |
if (!$this->model->hasBlock()) { |
| 188 |
// Set view template name. |
| 189 |
$viewName = 'create-metabox'; |
| 190 |
// Create DUMMY block object. |
| 191 |
$block = (object) array(); |
| 192 |
$block->id = $metaboxId; |
| 193 |
$block->name = cssJSToolbox::getText('CJT Block'); |
| 194 |
} |
| 195 |
/* |
| 196 |
* Block post is already created. |
| 197 |
* This condition is only when the page first loaded |
| 198 |
* and has nothing to do with "create" action! |
| 199 |
*/ |
| 200 |
else { |
| 201 |
// Set view template name. |
| 202 |
$viewName = 'metabox'; |
| 203 |
// Get real block data. |
| 204 |
$block = CJTModel::create('blocks')->getBlock($metaboxId, array('returnCodeFile' => true)); |
| 205 |
} |
| 206 |
// Get block meta box view object instance. |
| 207 |
$this->view = CJTView::create("blocks/{$viewName}"); |
| 208 |
// Push view vars. |
| 209 |
$this->view->setBlock($block); |
| 210 |
$this->view->setSecurityToken($this->createSecurityToken()); |
| 211 |
// Add metabox. |
| 212 |
add_meta_box($this->view->getMetaboxId(), $this->view->getMetaboxName(), array(&$this->view, 'display'), $this->model->getPost()->post_type, 'normal'); |
| 213 |
} |
| 214 |
|
| 215 |
} // End class. |