Advertisement
Guest User

WP Permalink to blog ID, type, and type ID.

a guest
Aug 9th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.54 KB | None | 0 0
  1.     // CS_ExtractURL() swiped from Ultimate Web Scraper Toolkit.  Renamed for no-conflict.
  2.     // RFC 3986 delimeter splitting implementation.
  3.     function CS_ExtractURL($url)
  4.     {
  5.         $result = array(
  6.             "scheme" => "",
  7.             "authority" => "",
  8.             "login" => "",
  9.             "loginusername" => "",
  10.             "loginpassword" => "",
  11.             "host" => "",
  12.             "port" => "",
  13.             "path" => "",
  14.             "query" => "",
  15.             "queryvars" => array(),
  16.             "fragment" => ""
  17.         );
  18.  
  19.         $pos = strpos($url, "#");
  20.         if ($pos !== false)
  21.         {
  22.             $result["fragment"] = substr($url, $pos + 1);
  23.             $url = substr($url, 0, $pos);
  24.         }
  25.  
  26.         $pos = strpos($url, "?");
  27.         if ($pos !== false)
  28.         {
  29.             $result["query"] = substr($url, $pos + 1);
  30.             $url = substr($url, 0, $pos);
  31.             $vars = explode("&", $result["query"]);
  32.             foreach ($vars as $var)
  33.             {
  34.                 $pos = strpos($var, "=");
  35.                 if ($pos === false)
  36.                 {
  37.                     $name = $var;
  38.                     $value = "";
  39.                 }
  40.                 else
  41.                 {
  42.                     $name = substr($var, 0, $pos);
  43.                     $value = substr($var, $pos + 1);
  44.                 }
  45.                 if (!isset($result["queryvars"][urldecode($name)]))  $result["queryvars"][urldecode($name)] = array();
  46.                 $result["queryvars"][urldecode($name)][] = urldecode($value);
  47.             }
  48.         }
  49.  
  50.         $url = str_replace("\\", "/", $url);
  51.  
  52.         $pos = strpos($url, ":");
  53.         if ($pos !== false)
  54.         {
  55.             $result["scheme"] = substr($url, 0, $pos);
  56.             $url = substr($url, $pos + 1);
  57.         }
  58.  
  59.         if (substr($url, 0, 2) != "//")  $result["path"] = $url;
  60.         else
  61.         {
  62.             $url = substr($url, 2);
  63.             $pos = strpos($url, "/");
  64.             if ($pos !== false)
  65.             {
  66.                 $result["path"] = substr($url, $pos);
  67.                 $url = substr($url, 0, $pos);
  68.             }
  69.             $result["authority"] = $url;
  70.  
  71.             $pos = strpos($url, "@");
  72.             if ($pos !== false)
  73.             {
  74.                 $result["login"] = substr($url, 0, $pos);
  75.                 $url = substr($url, $pos + 1);
  76.                 $pos = strpos($result["login"], ":");
  77.                 if ($pos === false)  $result["loginusername"] = $result["login"];
  78.                 else
  79.                 {
  80.                     $result["loginusername"] = substr($result["login"], 0, $pos);
  81.                     $result["loginpassword"] = substr($result["login"], $pos + 1);
  82.                 }
  83.             }
  84.  
  85.             $pos = strpos($url, ":");
  86.             if ($pos !== false)
  87.             {
  88.                 $result["port"] = substr($url, $pos + 1);
  89.                 $url = substr($url, 0, $pos);
  90.             }
  91.  
  92.             $result["host"] = $url;
  93.         }
  94.  
  95.         return $result;
  96.     }
  97.  
  98.     // Converts WP permalink to useful, structured information.
  99.     function TNI_url_to_internal_info($url)
  100.     {
  101.         global $wpdb;
  102.  
  103.         $url2 = CS_ExtractURL($url);
  104.  
  105.         $path = explode("/", $url2["path"]);
  106.         array_shift($path);
  107.  
  108.         // Rip out the name from subdomain or path to get the blog ID.
  109.         if ( is_subdomain_install() ) {
  110.             if (strpos($url2["host"], $current_site->domain) !== false)  $name = substr($url2["host"], 0, strpos($url2["host"], $current_site->domain));
  111.             else  $name = "";
  112.  
  113.             if (substr($name, -1) == ".")  $name = substr($name, 0, -1);
  114.         } else if ( count($path) ) {
  115.             $name = array_shift($path);
  116.         } else {
  117.             $name = "";
  118.         }
  119.         $blog_id = get_id_from_blogname($name);
  120.  
  121.         // Determine type.
  122.         $id = 0;
  123.         $type = "";
  124.         if (count($path))
  125.         {
  126.             if ($path[0] == "files") {
  127.                 $type = "file";
  128.             } else if ($blog_id) {
  129.                 // Try assuming blog and getting post ID.
  130.                 switch_to_blog($blog_id);
  131.  
  132.                 $id = url_to_postid($url);
  133.                 if ($id > 0)
  134.                 {
  135.                     $type2 = $wpdb->get_var($wpdb->prepare("SELECT post_type FROM {$wpdb->posts} WHERE ID = %d", $id));
  136.                     if (is_string($type2))  $type = $type2;
  137.                 }
  138.  
  139.                 restore_current_blog();
  140.             }
  141.         }
  142.  
  143.         $result = array(
  144.             "blog_id" => $blog_id,
  145.             "type" => $type,
  146.             "type_id" => $id
  147.         );
  148.  
  149.         return $result;
  150.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement