Commit 8962b287 authored by hefox's avatar hefox Committed by Spleshka

Issue #2145739 by hefox, Grayside, Spleshka: Expire pages in a menu on menu update

parent 3393c307
......@@ -425,6 +425,51 @@ function expire_admin_settings_form() {
}
// MENU LINK SETTINGS.
$form['tabs']['menu_links'] = array(
'#type' => 'fieldset',
'#title' => t('Menu links expiration'),
'#group' => 'tabs',
'#weight' => 1,
);
$form['tabs']['menu_links']['actions'] = array(
'#type' => 'fieldset',
'#title' => t('Menu link actions'),
);
$form['tabs']['menu_links']['actions']['expire_menu_link_actions'] = array(
'#type' => 'checkboxes',
'#description' => t('Page cache for user will be expired after selected actions.'),
'#options' => array(
EXPIRE_MENU_LINK_INSERT => t('Menu link insert'),
EXPIRE_MENU_LINK_UPDATE => t('Menu link update'),
EXPIRE_MENU_LINK_DELETE => t('Menu link delete'),
),
'#default_value' => variable_get('expire_menu_link_actions', array()),
);
$form['tabs']['menu_links']['expire_menu_link_override_menus'] = array(
'#type' => 'fieldset',
'#tree' => TRUE,
'#title' => t('What menus should expire their page urls (and at what depth) when menu is updated?'),
);
$options = array();
for ($i = 0; $i <= MENU_MAX_DEPTH; $i++) {
$options[$i] = $i ? t('@depth depth', array('@depth' => $i)) : t('None');
}
$menu_options = array_filter(variable_get('expire_menu_link_override_menus', array()));
foreach (menu_get_menus() as $menu_name => $menu_label) {
$form['tabs']['menu_links']['expire_menu_link_override_menus'][$menu_name] = array(
'#type' => 'select',
'#options' => $options,
'#default_value' => !empty($menu_options[$menu_name]) ? $menu_options[$menu_name] : 0,
'#title' => check_plain($menu_label),
);
}
// Add the token tree UI.
if (module_exists('token')) {
......
......@@ -9,6 +9,7 @@ files[] = includes/expire.api.inc
files[] = includes/expire.comment.inc
files[] = includes/expire.domain.inc
files[] = includes/expire.interface.inc
files[] = includes/expire.menu_link.inc
files[] = includes/expire.node.inc
files[] = includes/expire.user.inc
files[] = includes/expire.votingapi.inc
......
......@@ -16,3 +16,10 @@ function expire_uninstall() {
cache_clear_all('variables', 'cache_bootstrap');
}
/**
* Run updates to rebuild a registry.
*/
function expire_update_7201() {
}
......@@ -40,6 +40,11 @@ define('EXPIRE_VOTINGAPI_DELETE', 2);
define('EXPIRE_FILE_UPDATE', 1);
define('EXPIRE_FILE_DELETE', 2);
// Menu link actions.
define('EXPIRE_MENU_LINK_INSERT', 1);
define('EXPIRE_MENU_LINK_UPDATE', 2);
define('EXPIRE_MENU_LINK_DELETE', 3);
// Debug levels.
define('EXPIRE_DEBUG_DISABLED', 0);
define('EXPIRE_DEBUG_WATCHDOG', 1);
......@@ -205,6 +210,27 @@ function expire_file_delete($file) {
expire_execute_expiration('file', $file, EXPIRE_FILE_DELETE);
}
/**
* Implements hook_menu_link_delete().
*/
function expire_menu_link_delete($link) {
expire_execute_expiration('menu_link', $link, EXPIRE_MENU_LINK_DELETE);
}
/**
* Implements hook_menu_link_update().
*/
function expire_menu_link_update($link) {
expire_execute_expiration('menu_link', $link, EXPIRE_MENU_LINK_UPDATE);
}
/**
* Implements hook_menu_link_insert().
*/
function expire_menu_link_insert($link) {
expire_execute_expiration('menu_link', $link, EXPIRE_MENU_LINK_INSERT);
}
/**
* Execute expiration method for object.
*/
......
<?php
/**
* @file
* Provides class that expires menulinks.
*/
class ExpireMenuLink implements ExpireInterface {
/**
* Executes expiration actions for menu link.
*
* @param $menu_link
* Array with settings of menu links like from menu_link_load().
*
* @param $action
* Action that has been executed.
*
* @param $skip_action_check
* Shows whether should we check executed action or just expire menu items.
*/
function expire($menu_link, $action, $skip_action_check = FALSE) {
// Stop further expiration if executed action is not selected by admin.
if (!$skip_action_check && !in_array($action, array_filter(variable_get('expire_menu_link_actions', array())))) {
return;
}
// Ensure that this menu is expirable.
$settings = array_filter(variable_get('expire_menu_link_override_menus', array()));
if (empty($menu_link['mlid']) || !array_key_exists($menu_link['menu_name'], $settings)) {
return;
}
// Ensure that current menu item level is not higher than required.
$depth = $settings[$menu_link['menu_name']];
if (!empty($menu_link['depth']) && $menu_link['depth'] > $depth) {
return;
}
// Collect menu items' urls.
$expire_urls = array();
if ($links = _menu_build_tree($menu_link['menu_name'], array('max_depth' => $depth))) {
$expire_urls = $this->get_urls_from_menu_tree($links['tree']);
}
// Flush page cache for expired urls.
if (!empty($expire_urls)) {
ExpireAPI::executeExpiration(array_unique($expire_urls), 'menu_link', $menu_link);
}
}
/**
* Fetches an internal urls from a menu items tree.
*/
private function get_urls_from_menu_tree($tree) {
$links = array();
foreach ($tree as $item) {
if (empty($item['link']['external']) || $item['link']['link_path'] == '<front>') {
$links[] = $item['link']['link_path'];
}
if (!empty($item['below'])) {
$links = array_merge($links, $this->get_urls_from_menu_tree($item['below']));
}
}
return $links;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment