From 240128b76d49a3d4e67a011ef0357da0541ca062 Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 28 Dec 2005 04:27:21 +0000 Subject: [PATCH] Create explicit page attachment rewrite rules rather than matching the attachment name against a regexp. git-svn-id: http://svn.automattic.com/wordpress/trunk@3371 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/classes.php | 36 ++++++++++++++++++++++------------ wp-includes/functions-post.php | 18 ++++++++++++++--- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/wp-includes/classes.php b/wp-includes/classes.php index ac805a1fd6..ecf773b03d 100644 --- a/wp-includes/classes.php +++ b/wp-includes/classes.php @@ -381,7 +381,9 @@ class WP_Query { $where .= " AND (ID = '$reqpage')"; } elseif ('' != $q['attachment']) { - $q['attachment'] = sanitize_title($q['attachment']); + $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment']))); + $attach_paths = '/' . trim($q['attachment'], '/'); + $q['attachment'] = sanitize_title(basename($attach_paths)); $q['name'] = $q['attachment']; $where .= " AND post_name = '" . $q['attachment'] . "'"; } @@ -953,16 +955,22 @@ class WP_Rewrite { function page_rewrite_rules() { $uris = get_settings('page_uris'); + $attachment_uris = get_settings('page_attachment_uris'); $rewrite_rules = array(); $page_structure = $this->get_page_permastruct(); - if( is_array( $uris ) ) - { - foreach ($uris as $uri => $pagename) { - $this->add_rewrite_tag('%pagename%', "($uri)", 'pagename='); - $rewrite_rules += $this->generate_rewrite_rules($page_structure); - } + if( is_array( $attachment_uris ) ) { + foreach ($attachment_uris as $uri => $pagename) { + $this->add_rewrite_tag('%pagename%', "($uri)", 'attachment='); + $rewrite_rules += $this->generate_rewrite_rules($page_structure); } + } + if( is_array( $uris ) ) { + foreach ($uris as $uri => $pagename) { + $this->add_rewrite_tag('%pagename%', "($uri)", 'pagename='); + $rewrite_rules += $this->generate_rewrite_rules($page_structure); + } + } return $rewrite_rules; } @@ -1225,11 +1233,14 @@ class WP_Rewrite { $rewrite = $rewrite + array($pagematch => $pagequery); if ($num_toks) { - $post = 0; + $post = false; + $page = false; if (strstr($struct, '%postname%') || strstr($struct, '%post_id%') || strstr($struct, '%pagename%') || (strstr($struct, '%year%') && strstr($struct, '%monthnum%') && strstr($struct, '%day%') && strstr($struct, '%hour%') && strstr($struct, '%minute') && strstr($struct, '%second%'))) { - $post = 1; + $post = true; + if ( strstr($struct, '%pagename%') ) + $page = true; $trackbackmatch = $match . $trackbackregex; $trackbackquery = $trackbackindex . '?' . $query . '&tb=1'; $match = rtrim($match, '/'); @@ -1257,9 +1268,10 @@ class WP_Rewrite { $rewrite = $rewrite + array($match => $query); if ($post) { - $rewrite = array($trackbackmatch => $trackbackquery) + $rewrite + - array($sub1 => $subquery, $sub1tb => $subtbquery, $sub1feed => $subfeedquery, $sub1feed2 => $subfeedquery) + - array($sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery); + $rewrite = array($trackbackmatch => $trackbackquery) + $rewrite; + if ( ! $page ) + $rewrite = $rewrite + array($sub1 => $subquery, $sub1tb => $subtbquery, $sub1feed => $subfeedquery, $sub1feed2 => $subfeedquery); + $rewrite = $rewrite + array($sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery); } } diff --git a/wp-includes/functions-post.php b/wp-includes/functions-post.php index b34349922a..53d5ddb458 100644 --- a/wp-includes/functions-post.php +++ b/wp-includes/functions-post.php @@ -795,18 +795,30 @@ function generate_page_rewrite_rules() { $posts = array_reverse($posts, true); $page_rewrite_rules = array(); - + $page_attachment_rewrite_rules = array(); + if ($posts) { foreach ($posts as $id => $post) { + // URI => page name $uri = get_page_uri($id); - + $attachments = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_status = 'attachment' AND post_parent = '$id'"); + if ( $attachments ) { + foreach ( $attachments as $attachment ) { + $attach_uri = get_page_uri($attachment->ID); + $page_attachment_rewrite_rules[$attach_uri] = $attachment->post_name; + } + } + $page_rewrite_rules[$uri] = $post; } - + update_option('page_uris', $page_rewrite_rules); + if ( $page_attachment_rewrite_rules ) + update_option('page_attachment_uris', $page_attachment_rewrite_rules); + save_mod_rewrite_rules(); } }