PluginProbe
CSS & JavaScript Toolbox / 8.0
CSS & JavaScript Toolbox v8.0
trunk 0.3 0.8 10 10.1 11 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.9.1 12 12.0 12.0.1 12.0.3 12.0.4 12.0.5 12.0.6 12.0.7 6.0 6.0.11 All 60 releases
css-javascript-toolbox / controllers / metabox.php

metabox.php in CSS & JavaScript Toolbox 8.0, at controllers/metabox.php

192 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Add metabox.
42 $this->showMetabox();
43 }
44 }
45
46 /**
47 * Create block metabox for specific post object.
48 *
49 * @param integer Post id.
50 * @return array New block object consist of block id and new block metabox view content.
51 */
52 public function createAction() {
53 // Get reserved block id from post object.
54 $blockId = $this->model->getMetaboxId();
55 if ($blockId) {
56 // Set request paremeters for blocks-ajax controller::createBlockAction.
57 $_GET['name'] = cssJSToolbox::getText(sprintf(cssJSToolbox::getText('CJT Block - Post #%d'), $this->model->getPost()->ID));
58 $_GET['state'] = 'active';
59 // Create post metabox.
60 $this->model->create($pin)->save();
61 // Create new block.
62 $blocksController = CJTController::create('blocks-ajax');
63 $blocksController->createBlockAction($blockId, 'metabox', $pin->flag);
64 // Get metabox block view object.
65 $this->view = CJTView::create('blocks/metabox');
66 $this->view->setBlock(CJTModel::create('blocks')->getBlock($blockId, array('returnCodeFile' => true)));
67 $this->view->setSecurityToken($this->createSecurityToken());
68 // Send Javascript & CSS files needed for the metabox view to work.
69 $this->response['references'] = self::getReferencesQueue();
70 $this->response['view'] = $this->view->setOption('customizeMetabox', true)->getTemplate('metabox');
71 }
72 }
73
74 /**
75 * put your comment there...
76 *
77 */
78 public function deleteAction() {
79 // Get input vars.
80 $metaboxBlockId = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
81 // Delete metabox block.
82 $blocks = CJTModel::create('blocks');
83 $blocks->delete($metaboxBlockId)->save();
84 // Unassociate post -- Mark post as HAS-NO-BLOCK-ASSOCIATED.
85 $this->model->delete();
86 // Load create-metabox view.
87 $this->view = CJTView::create('blocks/create-metabox');
88 // Create DUMMY block object.
89 $block = (object) array();
90 $block->id = $metaboxBlockId;
91 $block->name = cssJSToolbox::getText('CJT Block');
92 // Push vars into the view.
93 $this->view->setBlock($block);
94 $this->view->setSecurityToken($this->createSecurityToken());
95 // Send Javascript & CSS files needed for the metabox view to work.
96 $this->response['references'] = self::getReferencesQueue();
97 // create-metabox view content.
98 $this->response['view'] = $this->view->setOption('customizeMetabox', true)->getTemplate('create');
99 }
100
101 /**
102 * put your comment there...
103 *
104 */
105 protected static function getReferencesQueue() {
106 $result = array();
107 /**
108 Get all scripts needed to be loaded for block metabox to work.
109 --------------------------------------------------------------
110 * 1. Suppress the output we need the SRC URLs not the HTML tags.
111 * 2. Fire scripts & styles action so the view would enqueue all the files.
112 * 3. Merge script and styles into a single array.
113 * 4. Get only files start with absoult URI (e.g start by HTTP).
114 * files without HTTP may be already loaded (e.g jQuery, etc..)
115 */
116 ob_start(); do_action('admin_print_scripts'); do_action('admin_print_styles'); ob_end_clean();
117 global $wp_scripts;
118 $references = array_merge($GLOBALS['wp_scripts']->registered, $GLOBALS['wp_styles']->registered);
119 $queue = array_merge($GLOBALS['wp_scripts']->queue, $GLOBALS['wp_styles']->queue);
120 foreach ($references as $script) {
121 // use only items on the queue!
122 if (in_array($script->handle, $queue)) {
123 // Scripts with absoult URL is only for the metabox block
124 // but not for Wordpress default scripts (e.g jquery, thickbox, etc...).
125 if (strpos($script->src, 'http') === 0) {
126 // For JS to work properly Always have cjt object set.
127 if (!isset($script->cjt)) {
128 $script->cjt = (object) array();
129 }
130 // Refine $script object and get only src, cjt and extra->data/localization properties!
131 $script = (object) array_intersect_key(((array) $script), array_flip(array('src', 'cjt' , 'extra')));
132 // Organize references into JS and CSS files lists.
133 if (preg_match('/\.js$/', $script->src)) {
134 $result['scripts'][] = $script;
135 }
136 else {
137 $result['styles'][] = $script;
138 }
139 }
140 }
141 }
142 // Return references.
143 return $result;
144 }
145
146 /**
147 * Select which metabox to load.
148 *
149 * create-metabox view will be loaded if user doesnt
150 * created a block for current post yet.
151 *
152 * metabox view will be loaded if there is a block
153 * already created for current post.
154 *
155 * Callback for add_meta_boxes action.
156 */
157 public function showMetabox() {
158 // Import blocks view.
159 CJTView::import('blocks/manager');
160 /// Get block id.
161 $metaboxId = $this->model->reservedMetaboxBlockId();
162 // User didn't create block for this post yet.
163 // Show create-metabox view.
164 if (!$this->model->hasBlock()) {
165 // Set view template name.
166 $viewName = 'create-metabox';
167 // Create DUMMY block object.
168 $block = (object) array();
169 $block->id = $metaboxId;
170 $block->name = cssJSToolbox::getText('CJT Block');
171 }
172 /*
173 * Block post is already created.
174 * This condition is only when the page first loaded
175 * and has nothing to do with "create" action!
176 */
177 else {
178 // Set view template name.
179 $viewName = 'metabox';
180 // Get real block data.
181 $block = CJTModel::create('blocks')->getBlock($metaboxId, array('returnCodeFile' => true));
182 }
183 // Get block meta box view object instance.
184 $this->view = CJTView::create("blocks/{$viewName}");
185 // Push view vars.
186 $this->view->setBlock($block);
187 $this->view->setSecurityToken($this->createSecurityToken());
188 // Add metabox.
189 add_meta_box($this->view->getMetaboxId(), $this->view->getMetaboxName(), array(&$this->view, 'display'), $this->model->getPost()->post_type, 'normal');
190 }
191
192 } // End class.