blockcache_alter.module in Block Cache Alter 7
Same filename and directory in other branches
Block cache alter module.
File
blockcache_alter.moduleView source
<?php
/**
* @file
* Block cache alter module.
*/
/**
* Implementation of hook_menu().
*/
function blockcache_alter_menu() {
$items = array();
$items['admin/config/development/performance/default'] = array(
'title' => 'Performance',
'type' => MENU_DEFAULT_LOCAL_TASK,
'file path' => drupal_get_path('module', 'system'),
'weight' => -5,
);
$items['admin/config/development/performance/blockcache_alter'] = array(
'type' => MENU_LOCAL_TASK,
'title' => 'Blockcache Alter',
'description' => 'Debug settings for Blockcache Alter',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'blockcache_alter_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
);
return $items;
}
/**
* Menu callback, settings page for blockcache alter
*/
function blockcache_alter_admin_settings() {
$status = _blockcache_alter_core_patch();
$form['bca_corepatch'] = array(
'#type' => 'checkbox',
'#title' => t('Core patch ?'),
'#description' => t('Check this box if you applied the core patch that comes with this module. This will extend the caching settings fieldset on the block configuration page. Note: if you did not apply the patch but check the box, the additional functionality simply won\'t work at all.<br /><strong>Current status: you have @status applied a blockcache alter patch.</strong>', array(
'@status' => $status,
)),
'#default_value' => variable_get('bca_corepatch', 0),
);
$form['bca_debug'] = array(
'#type' => 'checkbox',
'#title' => t('Show debug information ?'),
'#description' => t('Debug info: shows helpful debugging whenever blocks are rendered, whether they are pulled form cache or not, and when block caches are refreshed.'),
'#default_value' => variable_get('bca_debug', 0),
'#states' => array(
'visible' => array(
':input[name="bca_corepatch"]' => array(
'checked' => TRUE,
),
),
),
);
$form['bca_user1'] = array(
'#type' => 'checkbox',
'#title' => t('Cache blocks for user 1 (uid 1) ?'),
'#description' => t('This enables block caching for the primary Drupal user (uid 1). Can lead to unexpected results on production websites!'),
'#default_value' => variable_get('bca_user1', 0),
'#states' => array(
'visible' => array(
':input[name="bca_corepatch"]' => array(
'checked' => TRUE,
),
),
),
);
return system_settings_form($form);
}
/**
* Implementation of hook_form_alter().
*/
function blockcache_alter_form_alter(&$form, $form_state, $form_id) {
// Add caching fieldset to the block configure page.
if ($form_id == 'block_admin_configure') {
// Core patch applied or not.
$core_patch = variable_get('bca_corepatch', 0);
// Blockcache options.
$block_cache_options = array(
DRUPAL_NO_CACHE => t('Do not cache'),
DRUPAL_CACHE_GLOBAL => t('Cache once for everything (global)'),
DRUPAL_CACHE_PER_PAGE => t('Per page'),
DRUPAL_CACHE_PER_ROLE => t('Per role'),
DRUPAL_CACHE_PER_ROLE | DRUPAL_CACHE_PER_PAGE => t('Per role per page'),
DRUPAL_CACHE_PER_USER => t('Per user'),
DRUPAL_CACHE_PER_USER | DRUPAL_CACHE_PER_PAGE => t('Per user per page'),
);
// Cache clear actions.
$cache_clear_actions = array(
'1' => t('Clear caching for this block only'),
'2' => t('Clear all cache (block and page)'),
);
// Retrieve current cache setting for this block.
$default_value = db_select('block', 'b')
->fields('b', array(
'cache',
))
->condition('module', $form['module']['#value'])
->condition('delta', $form['delta']['#value'])
->execute()
->fetchField();
// Blockcache fieldset.
$form['cache'] = array(
'#type' => 'fieldset',
'#title' => t('Cache settings'),
'#weight' => 1,
'#collapsible' => TRUE,
);
// Cache setting.
$form['cache']['cache_block'] = array(
'#type' => 'select',
'#title' => t('Cache setting'),
'#description' => t('Select the appropriate cache setting for this block.'),
'#options' => $block_cache_options,
'#default_value' => $default_value,
);
// Cache clearing option after saving this block.
$form['cache']['cache_block_clear'] = array(
'#type' => 'radios',
'#title' => t('Clear cache'),
'#description' => t('Select the appropriate cache clearing action after saving this block.'),
'#options' => $cache_clear_actions,
'#default_value' => 1,
'#states' => array(
'invisible' => array(
':input[name="cache_block"]' => array(
'value' => DRUPAL_NO_CACHE,
),
),
),
);
// Show options underneath onl if core patch is applied.
if ($core_patch == TRUE) {
// Blockcache fieldset.
$form['cache']['option_1'] = array(
'#type' => 'fieldset',
'#title' => t('Cache refresh option 1: Cache lifetime'),
'#description' => t('Set a minimum cache lifetime (in seconds). Leave 0 or empty to use the second option.'),
'#states' => array(
'invisible' => array(
':input[name="cache_block"]' => array(
'value' => DRUPAL_NO_CACHE,
),
),
),
);
// Blockcache lifetime.
$form['cache']['option_1']['bc_life'] = array(
'#type' => 'textfield',
'#title' => t('Cache lifetime'),
'#size' => 10,
'#default_value' => variable_get('bc_life_' . $form['module']['#value'] . '_' . $form['delta']['#value'], ''),
);
// Blockcache fieldset.
$form['cache']['option_2'] = array(
'#type' => 'fieldset',
'#title' => t('Cache refresh option 2: Drupal actions'),
'#description' => t('Refresh on certain actions in the Drupal system. When choosing node or comment action, you also need to specify on which node types it should refresh. Note that clicking any of these checkboxes will not have any effect if a minimum lifetime is set in option 1.'),
'#states' => array(
'invisible' => array(
':input[name="cache_block"]' => array(
'value' => DRUPAL_NO_CACHE,
),
),
),
);
// Blockcache refresh.
$options = array(
'node' => t('A node is added/updated/deleted'),
'comment' => t('A comment is added/updated/deleted'),
'user' => t('A user is added/updated/deleted'),
'login' => t('A user logs in our out'),
);
// Nodequeue support.
if (module_exists('nodequeue') && $form['module']['#value'] == 'views' && strstr($form['delta']['#value'], 'nodequeue') !== FALSE) {
$options['nodequeue'] = t('A node is added or removed from a nodequeue');
}
$form['cache']['option_2']['bc_refresh'] = array(
'#type' => 'checkboxes',
'#title' => t('Refresh when'),
'#options' => $options,
'#default_value' => variable_get('bc_refresh_' . $form['module']['#value'] . '_' . $form['delta']['#value'], array()),
);
// Associate with node types.
$node_types = array();
$types = node_type_get_types();
foreach ($types as $type) {
$node_types[$type->type] = $type->name;
}
$form['cache']['option_2']['bc_relate'] = array(
'#type' => 'checkboxes',
'#title' => t('Relation'),
'#options' => $node_types,
'#default_value' => variable_get('bc_relate_' . $form['module']['#value'] . '_' . $form['delta']['#value'], array()),
);
}
// Add own submit handler.
$form['#submit'][] = 'blockcache_alter_save_settings';
}
// Alter the performance settings page: remove the #disabled key from the block_cache form completely
// and add a warning message instead to warn the user that block caching won't work when one
// at least on module implements node_grants and blockcache_alter_no_node_grants is not applied.
// Note, again, we don't check which patch is applied, that's up to the developer.
if ($form_id == 'system_performance_settings') {
$node_grants = $form['caching']['block_cache']['#disabled'];
$form['caching']['block_cache']['#disabled'] = FALSE;
if ($node_grants == TRUE) {
$form['caching']['block_cache']['#description'] = t('WARNING: There are modules implementing node grants. If you want block caching to work, you need to apply the "blockcache_alter.patch"');
}
else {
$form['caching']['block_cache']['#description'] = '';
}
}
}
/**
* Submit callback. Saves cache settings per block.
*/
function blockcache_alter_save_settings($form, &$form_state) {
$bids = db_select('block', 'b')
->fields('b', array(
'bid',
))
->condition('module', $form['module']['#value'])
->condition('delta', $form['delta']['#value'])
->execute()
->fetchCol();
$blockcache_alter = db_select('blockcache_alter', 'b')
->fields('b', array(
'bid',
))
->condition('b.bid', $bids)
->execute()
->fetchCol();
$block = new StdClass();
$block->module = $form_state['values']['module'];
$block->delta = $form_state['values']['delta'];
$block->cache = $form_state['values']['cache_block'];
foreach ($bids as $bid) {
$block->bid = $bid;
drupal_write_record('block', $block, array(
'bid',
));
if (in_array($bid, $blockcache_alter)) {
drupal_write_record('blockcache_alter', $block, array(
'bid',
));
}
else {
drupal_write_record('blockcache_alter', $block);
}
}
// Core patch applied or not.
$core_patch = variable_get('bca_corepatch', 0);
// Cache clearing
switch ($form_state['values']['cache_block_clear']) {
case '1':
cache_clear_all($form_state['values']['module'] . ':' . $form_state['values']['delta'], 'cache_block', TRUE);
break;
case '2':
cache_clear_all();
break;
}
// Store extra variables if core patch is applied.
if ($core_patch == TRUE) {
// Remove old variables to avoid clutter in the variable table.
variable_del('bc_life_' . $form['module']['#value'] . '_' . $form['delta']['#value']);
variable_del('bc_refresh_' . $form['module']['#value'] . '_' . $form['delta']['#value']);
variable_del('bc_relate_' . $form['module']['#value'] . '_' . $form['delta']['#value']);
// Remember block expire time or refresh actions for the future.
if (!empty($form_state['values']['bc_life'])) {
variable_set('bc_life_' . $form['module']['#value'] . '_' . $form['delta']['#value'], $form_state['values']['bc_life']);
}
else {
variable_set('bc_refresh_' . $form['module']['#value'] . '_' . $form['delta']['#value'], $form_state['values']['bc_refresh']);
variable_set('bc_relate_' . $form['module']['#value'] . '_' . $form['delta']['#value'], $form_state['values']['bc_relate']);
}
}
}
/**
* Implementation of hook_block_info_alter().
*/
function blockcache_alter_block_info_alter(&$blocks, $theme, $code_blocks) {
try {
$blockcache_alter = db_select('blockcache_alter', 'b')
->fields('b', array(
'bid',
'module',
'cache',
))
->execute()
->fetchAll();
} catch (PDOException $e) {
$blockcache_alter = array();
watchdog('blockcache alter', 'Block cache alter was unable to load block cache data. You may need to run updates.', array(), WATCHDOG_ERROR);
}
foreach ($blockcache_alter as $bca) {
if (array_key_exists($bca->module, $blocks)) {
foreach ($blocks[$bca->module] as $key => $module) {
if (isset($module['bid']) && $module['bid'] == $bca->bid) {
$blocks[$bca->module][$key]['cache'] = $bca->cache;
}
}
}
}
}
/**
* Implements hook_module_implements_alter().
*/
function blockcache_alter_module_implements_alter(&$implementations, $hook) {
// Ensure our hook_block_info_alter() implementation trumps all others.
if ($hook == 'block_info_alter') {
$bca_group = $implementations['blockcache_alter'];
unset($implementations['blockcache_alter']);
$implementations['blockcache_alter'] = $bca_group;
}
}
/**
* Helper function to check if a blockcache alter patch is found.
* Note: it doesn't check which patch is applied.
*/
function _blockcache_alter_core_patch() {
$status = 'not';
$block_file = drupal_get_filename('module', 'block');
$block_contents = file_get_contents($block_file);
if (strpos($block_contents, 'bca_debug') !== FALSE) {
$status = '';
}
return $status;
}
/**
* Helper function to check if this block should be refreshed or not.
*
* @param stdClass $cache A complete cache object.
* @param int $time The current timestamp.
* @return FALSE or TRUE
*/
function _blockcache_alter_check_expire($cache, $time) {
if ($cache->expire == CACHE_PERMANENT) {
return TRUE;
}
if ($cache->expire > $time) {
return TRUE;
}
return FALSE;
}
/**
* Implementation of hook_node_insert().
*/
function blockcache_alter_node_insert($node) {
_blockcache_alter_cleanup('node', $node->type);
return;
}
/**
* Implementation of hook_node_update().
*/
function blockcache_alter_node_update($node) {
_blockcache_alter_cleanup('node', $node->type);
return;
}
/**
* Implementation of hook_node_delete().
*/
function blockcache_alter_node_delete($node) {
_blockcache_alter_cleanup('node', $node->type);
return;
}
/**
* Implementation of hook_comment_insert().
*/
function blockcache_alter_comment_insert($comment) {
$thiscnode = node_load($comment->nid);
$nodetype = $thiscnode->type;
_blockcache_alter_cleanup('comment', $nodetype);
return;
}
/**
* Implementation of hook_comment_update().
*/
function blockcache_alter_comment_update($comment) {
$thiscnode = node_load($comment->nid);
$nodetype = $thiscnode->type;
_blockcache_alter_cleanup('comment', $nodetype);
return;
}
/**
* Implementation of hook_comment_delete().
*/
function blockcache_alter_comment_delete($comment) {
$thiscnode = node_load($comment->nid);
$nodetype = $thiscnode->type;
_blockcache_alter_cleanup('comment', $nodetype);
return;
}
/**
* Implementation of hook_user_insert().
*/
function blockcache_alter_user_insert(&$edit, $account, $category) {
_blockcache_alter_cleanup('user', 'user_actions');
return;
}
/**
* Implementation of hook_user_update().
*/
function blockcache_alter_user_update(&$edit, $account, $category) {
_blockcache_alter_cleanup('user', 'user_actions');
return;
}
/**
* Implementation of hook_user_delete().
*/
function blockcache_alter_user_delete($account) {
_blockcache_alter_cleanup('user', 'user_actions');
return;
}
/**
* Implementation of hook_user_login().
*/
function blockcache_alter_user_login(&$edit, $account) {
_blockcache_alter_cleanup('login', 'user_actions');
return;
}
/**
* Implementation of hook_user_logout().
*/
function blockcache_alter_user_logout($account) {
_blockcache_alter_cleanup('login', 'user_actions');
return;
}
/**
* Cleanup cached blocks if necessary.
*
* @param string $type operation type.
* @param string $relatednodetype Related node type (if available)
*/
function _blockcache_alter_cleanup($type, $relatednodetype = FALSE) {
$info = array();
$debug = variable_get('bca_debug', FALSE) && user_access('administer site configuration');
$modules = variable_get('blockcache_alter_include_modules', array());
if (!empty($modules)) {
$query = "SELECT module, delta FROM {block} WHERE (status = '1' OR module IN (:modules)) AND cache <> '-1'";
$result = db_query($query, $modules);
}
else {
$query = "SELECT module, delta FROM {block} WHERE status = '1' AND cache <> '-1'";
$result = db_query($query);
}
foreach ($result as $r) {
if (module_exists($r->module)) {
$refresh = variable_get('bc_refresh_' . $r->module . '_' . $r->delta, array());
if (isset($refresh[$type]) && $refresh[$type] === $type) {
$relate = variable_get('bc_relate_' . $r->module . '_' . $r->delta, array());
if ($relatednodetype == 'user_actions' || is_array($relate) && $relate[$relatednodetype] === $relatednodetype) {
cache_clear_all($r->module . ':' . $r->delta, 'cache_block', TRUE);
if ($debug) {
$info[] = t('<br /> - module: %mod, delta: %delta', array(
'%mod' => $r->module,
'%delta' => $r->delta,
));
}
}
}
}
}
// Let other modules cleanup more.
$hook = 'blockcache_alter_cleanup';
foreach (module_implements($hook) as $module) {
$function = $module . '_' . $hook;
$function($type, $relatednodetype, $info);
}
// Implode and put together a LIKE query.
if ($debug && !empty($info)) {
drupal_set_message(t("DEBUG: Block re-cached: " . implode(' ', $info)));
}
}