function _textpattern()
{
global $plugins_ver, $pretext, $prefs, $plugin_callback;
$this->debug('Plugin: ' . $this->plugin_name . ' - ' . $plugins_ver[$this->plugin_name]);
$this->debug('Function: ' . __FUNCTION__ . '()');
// URI
$req = $pretext['req'];
$req = preg_replace('%\\?[^\\/]+$%', '', $req);
$this->debug('Request URI: ' . $req);
$uri = explode('/', trim($req, '/'));
// The number of components comes in useful when determining the best partial match.
$uri_component_count = count($uri);
// Permanent links
$permlinks = $this->get_all_permlinks(1);
// Force Textpattern and tags to use messy URLs - these are easier to
// find in regex
$this->set_permlink_mode();
if (count($permlinks)) {
// We also want to match the front page of the site (for page numbers / feeds etc..).
// Add a permlinks rule which will do that.
$permlinks['default'] = array('components' => array(), 'settings' => array('pl_name' => 'gbp_permanent_links_default', 'pl_precedence' => '', 'pl_preview' => '/', 'con_section' => '', 'con_category' => '', 'des_section' => '', 'des_category' => '', 'des_permlink' => '', 'des_feed' => '', 'des_location' => '', 'des_page' => ''));
// Extend the pretext_replacement scope outside the foreach permlink loop
$pretext_replacement = NULL;
foreach ($permlinks as $id => $pl) {
// Extract the permlink settings
$pl_settings = $pl['settings'];
extract($pl_settings);
$this->debug('Permlink name: ' . $pl_name);
$this->debug('Permlink id: ' . $id);
$this->debug('Preview: ' . $pl_preview);
$pl_components = $pl['components'];
// URI components
$uri_components = $uri;
$this->debug('PL component count: ' . count($pl_components));
$this->debug('URL component count: ' . count($uri_components));
$date = false;
$title_page_feed = false;
foreach ($pl_components as $pl_c) {
// Are we expecting a date component? If so the number of pl and uri components won't match
if ($pl_c['type'] == 'date') {
$date = true;
} else {
if (in_array($pl_c['type'], array('title', 'page', 'feed'))) {
$title_page_feed = true;
}
}
}
if (!$title_page_feed) {
// If there isn't a title, page or feed component then append a special type for cleaver partial matching
$pl_components[] = array('type' => 'title_page_feed', 'prefix' => '', 'suffix' => '', 'regex' => '', 'text' => '');
}
// Exit early if there are more URL components than PL components,
// taking into account whether there is a data component
if (!$uri_components[0] || count($uri_components) > count($pl_components) + ($date ? 2 : 0)) {
$this->debug('More URL components than PL components');
continue;
}
// Reset pretext_replacement as we are about to start another comparison
$pretext_replacement = array('permlink_id' => $id);
// Reset the article context string
$context = array();
unset($context_str);
if (!empty($des_section)) {
$context[] = "`Section` = '{$des_section}'";
}
if (!empty($des_category)) {
$context[] = "(`Category1` = '{$des_category}' OR `Category2` = '{$des_category}')";
}
$context_str = count($context) > 0 ? 'and ' . join(' and ', $context) : '';
// Assume there is no match
$partial_match = false;
$cleaver_partial_match = false;
// Loop through the permlink components
foreach ($pl_components as $pl_c_index => $pl_c) {
// Assume there is no match
$match = false;
// Check to see if there are still URI components to be checked.
if (count($uri_components)) {
// Get the next component.
$uri_c = array_shift($uri_components);
} else {
if (!$title_page_feed && count($pl_components) - 1 == $uri_component_count) {
// If we appended a title_page_feed component earlier and permlink and URI components
// counts are equal, we must of finished checking this permlink, and it matches so break.
$match = true;
break;
} else {
// If there are no more URI components then we have a partial match.
// Store the partial match data unless there has been a preceding permlink with the
// same number of components, as permlink have already been sorted by precedence.
if (!array_key_exists($uri_component_count, $this->partial_matches)) {
$this->partial_matches[$uri_component_count] = $pretext_replacement;
}
// Unset pretext_replacement as changes could of been made in a preceding component
unset($pretext_replacement);
// Break early form the foreach permlink components loop.
$partial_match = true;
break;
}
}
//.........这里部分代码省略.........
请发表评论