| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
// Disallow direct access. |
| 7 |
defined('ABSPATH') or die("Access denied"); |
| 8 |
|
| 9 |
// Import dependencies. |
| 10 |
cssJSToolbox::import('tables:blocks.php'); |
| 11 |
|
| 12 |
/** |
| 13 |
* |
| 14 |
*/ |
| 15 |
class CJTCouplingModel extends CJTHookableClass { |
| 16 |
|
| 17 |
/** |
| 18 |
* put your comment there... |
| 19 |
* |
| 20 |
* @var mixed |
| 21 |
*/ |
| 22 |
protected $onexpressionsandlinkedblocks = array('parameters' => array('blocks')); |
| 23 |
|
| 24 |
/** |
| 25 |
* put your comment there... |
| 26 |
* |
| 27 |
* @var mixed |
| 28 |
*/ |
| 29 |
protected $ongetorder = array('parameters' => array('order')); |
| 30 |
|
| 31 |
/** |
| 32 |
* put your comment there... |
| 33 |
* |
| 34 |
* @var mixed |
| 35 |
*/ |
| 36 |
protected $onquerylinkedtemplates = array('parameters' => array('query')); |
| 37 |
|
| 38 |
/** |
| 39 |
* put your comment there... |
| 40 |
* |
| 41 |
* @var mixed |
| 42 |
*/ |
| 43 |
protected $onpinsblockfilters = array('parameters' => array( |
| 44 |
'params' => array('linksExpressionFlag', 'pinPoint', 'customPins') |
| 45 |
) |
| 46 |
); |
| 47 |
|
| 48 |
/** |
| 49 |
* put your comment there... |
| 50 |
* |
| 51 |
* @var mixed |
| 52 |
*/ |
| 53 |
protected $onrequestblocks = array('parameters' => array('blocks')); |
| 54 |
|
| 55 |
/** |
| 56 |
* put your comment there... |
| 57 |
* |
| 58 |
* @var mixed |
| 59 |
*/ |
| 60 |
public static $templateTypes = array('scripts' => 'javascript', 'styles' => 'css'); |
| 61 |
|
| 62 |
/** |
| 63 |
* put your comment there... |
| 64 |
* |
| 65 |
* @param mixed $id |
| 66 |
*/ |
| 67 |
public function getBlockCode($id) { |
| 68 |
// Initialize. |
| 69 |
$code = ''; |
| 70 |
// Initialize. |
| 71 |
$tblCodeFiles = new CJTBlockFilesTable(cssJSToolbox::getInstance()->getDBDriver()); |
| 72 |
// Query Block Code Files. |
| 73 |
$codeFiles = $tblCodeFiles->set('blockId', $id) |
| 74 |
->fetchAll(); |
| 75 |
// Merge all code files. |
| 76 |
foreach ($codeFiles as $codeFile) { |
| 77 |
// Wrap by Tag + Merge files. |
| 78 |
$code .= $codeFile['tag'] ? sprintf($codeFile['tag'], " {$codeFile['code']} ") : $codeFile['code']; |
| 79 |
} |
| 80 |
// Return final code text. |
| 81 |
return $code; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* put your comment there... |
| 86 |
* |
| 87 |
* @param mixed $blockId |
| 88 |
*/ |
| 89 |
public function getExecTemplatesCode($blockId) { |
| 90 |
// Initialize. |
| 91 |
$code = ''; |
| 92 |
// Get all HTML and PHP templates linked to the block. |
| 93 |
$templates = $this->getLinkedTemplates($blockId, array('php', 'html')); |
| 94 |
if (!empty($templates)) { |
| 95 |
// Instantiate template model. |
| 96 |
$templateModel = CJTModel::getInstance('template'); |
| 97 |
// Concat their codes. |
| 98 |
foreach ($templates as $template) { |
| 99 |
// Fetch template record with code loaded from file and decrypted if PHP! |
| 100 |
$templateModel->inputs['id'] = $template->id; |
| 101 |
$template = $templateModel->getItem(); |
| 102 |
// Concat |
| 103 |
$code .= $template->code; |
| 104 |
} |
| 105 |
} |
| 106 |
return $code; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* put your comment there... |
| 111 |
* |
| 112 |
*/ |
| 113 |
public function getOrder() { |
| 114 |
return $this->ongetorder(get_option('meta-box-order_cjtoolbox')); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* put your comment there... |
| 119 |
* |
| 120 |
* @param mixed $linksExpressionFlag |
| 121 |
* @param mixed $pinPoint |
| 122 |
* @param mixed $customPins |
| 123 |
* @param mixed $hookParams |
| 124 |
*/ |
| 125 |
public function getPinsBlocks( $linksExpressionFlag, $pinPoint, $customPins, $hookParams ) |
| 126 |
{ |
| 127 |
// Extendable! |
| 128 |
extract( $this->onpinsblockfilters( compact( 'linksExpressionFlag', 'pinPoint', 'customPins' ) ) ); |
| 129 |
|
| 130 |
// Import required libraries for CJTPinsBlockSQLView. |
| 131 |
require_once CJTOOLBOX_TABLES_PATH . '/pins-blocks-view.php'; |
| 132 |
|
| 133 |
// Initialize new CJTPinsBlockSQLView view object. |
| 134 |
$dbDriver = new CJTMYSQLQueueDriver( $GLOBALS[ 'wpdb' ] ); |
| 135 |
$view = new CJTPinsBlockSQLView( $dbDriver ); |
| 136 |
|
| 137 |
// Apply filter to view. |
| 138 |
$view->filters( $pinPoint, $customPins ); |
| 139 |
|
| 140 |
# Allow Plugins/Extensions to change block core query |
| 141 |
do_action( |
| 142 |
|
| 143 |
CJTPluggableHelper::ACTION_BLOCK_QUERY_BLOCKS, |
| 144 |
|
| 145 |
$view, |
| 146 |
|
| 147 |
$hookParams |
| 148 |
|
| 149 |
); |
| 150 |
|
| 151 |
// retreiving blocks data associated with current request. |
| 152 |
$blocks = $this->onrequestblocks( $view->exec() ); |
| 153 |
|
| 154 |
# Filter queue blocks |
| 155 |
$blocks = apply_filters( CJTPluggableHelper::FILTER_BLOCKS_COUPLING_MODEL_BLOCKS_QUEUE, $blocks, $hookParams ); |
| 156 |
|
| 157 |
// Get links & expressions Blocks. |
| 158 |
// NOTE: We need only blocks not presented in $blocks var -- exclude their id. |
| 159 |
$view->filters( $linksExpressionFlag, array(), 'active', array_keys( $blocks ) ); |
| 160 |
|
| 161 |
// We'll process all blocks inside single loop. |
| 162 |
$blocks = $blocks + $this->onexpressionsandlinkedblocks( $view->exec() ); |
| 163 |
|
| 164 |
return $blocks; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* put your comment there... |
| 169 |
* |
| 170 |
* @param mixed $blocks |
| 171 |
* @param mixed $types |
| 172 |
*/ |
| 173 |
public function getLinkedTemplates($blocks, $types = array('javascript', 'css')) { |
| 174 |
// Accept single id too! |
| 175 |
$blocks = (array) $blocks; |
| 176 |
// Import dependencies! |
| 177 |
cssJSToolbox::import('framework:db:mysql:xtable.inc.php', 'tables:template-revision.php'); |
| 178 |
// Initialize vars. |
| 179 |
$templates = array(); |
| 180 |
$query['select'] = 'SELECT t.id, t.type, t.queueName, r.version, r.file, bt.blockId'; |
| 181 |
$query['from'] = 'FROM #__cjtoolbox_block_templates bt LEFT JOIN #__cjtoolbox_templates t |
| 182 |
ON bt.templateId = t.id |
| 183 |
LEFT JOIN #__cjtoolbox_template_revisions r |
| 184 |
ON bt.templateId = r.templateId'; |
| 185 |
// Filter by blocks ids and getting the last revision of the template! |
| 186 |
$query['where']['blocks'] = implode(',', $blocks); |
| 187 |
$query['where']['attributes'] = CJTTemplateRevisionTable::FLAG_LAST_REVISION; |
| 188 |
// Filter by type. |
| 189 |
$query['where']['types'] = '"' . implode('","', $types) . '"'; |
| 190 |
// Where clause. |
| 191 |
$query['where'] = "WHERE bt.blockId IN ({$query['where']['blocks']}) AND |
| 192 |
(r.attributes & {$query['where']['attributes']}) AND |
| 193 |
t.type IN ({$query['where']['types']})"; |
| 194 |
// Filering! |
| 195 |
$query = $this->onquerylinkedtemplates($query); |
| 196 |
$query = "{$query['select']} {$query['from']} {$query['where']}"; |
| 197 |
$templates = cssJSToolbox::getInstance()->getDBDriver()->select($query); |
| 198 |
return $templates; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* put your comment there... |
| 203 |
* |
| 204 |
* @param mixed $type |
| 205 |
*/ |
| 206 |
public function getQueueObject($type) { |
| 207 |
// Make sure Queue Object is ready/instantiated! |
| 208 |
$globalQueueObjectName = "wp_{$type}"; |
| 209 |
if (!isset($GLOBALS[$globalQueueObjectName])) { |
| 210 |
$queueClass = 'WP_' . ucfirst($type); |
| 211 |
$GLOBALS[$globalQueueObjectName] = new $queueClass(); |
| 212 |
} |
| 213 |
return $GLOBALS[$globalQueueObjectName]; |
| 214 |
} |
| 215 |
|
| 216 |
} // End class. |
| 217 |
|
| 218 |
|
| 219 |
// Hookable! |
| 220 |
CJTCouplingModel::define('CJTCouplingModel', array('hookType' => CJTWordpressEvents::HOOK_FILTER)); |