Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.16 KB | None | 0 0
  1.    /*
  2.    ** Caching photos urls on save
  3.    */
  4.    add_action("save_post", function($post_id){
  5.       if (get_current_screen()->post_type == "post"){
  6.          // Reset
  7.          if (!empty(get_post_meta($post_id, "cachedPhotos")))
  8.             delete_post_meta($post_id, "cachedPhotos");
  9.  
  10.          $i = 0;
  11.          $photosToChange = array();
  12.  
  13.          // Get photos
  14.          $checkPhotos = get_post_meta($post_id, "cachedPhotos");
  15.  
  16.          // Process
  17.          $content = get_post($post_id)->post_content;
  18.          $pattern = get_shortcode_regex();
  19.          preg_match_all("/".$pattern."/s", $content, $shortcodes);
  20.  
  21.          foreach ($shortcodes[0] as $photo){
  22.             // ID
  23.             preg_match_all('/id="([^"]+)"/', $photo, $id);
  24.             if (isset($id[1][0])){
  25.                $id = $id[1][0];
  26.                $photosToChange[$i]["id"] = $id;
  27.             }
  28.             // Caption
  29.             preg_match_all('/caption="([^"]+)"/', $photo, $caption);
  30.             if (isset($caption[1][0])){
  31.                $caption = $caption[1][0];
  32.                $photosToChange[$i]["caption"] = $caption;
  33.             }
  34.  
  35.             // If the ID is not empty
  36.             if (isset($id[1][0])){
  37.                // Flickr
  38.                if (preg_match("/^\d{11}$/", $id)){
  39.                   $flickrJSON = file_get_contents("http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=curiousyouare&photo_id={$id}&format=json&nojsoncallback=1");
  40.                   $photosToChange[$i]["link"] = "http://flickr.com/photos/fpeault/{$id}";
  41.                   $photosToChange[$i]["src"] = json_decode($flickrJSON)->sizes->size[7]->source;
  42.                }
  43.                // Instagram
  44.                elseif (preg_match("/([a-zA-Z0-9_-]){10}/", $id)){
  45.                   $igJSON = file_get_contents("http://api.instagram.com/oembed?url=http://instagram.com/p/{$id}");
  46.                   $photosToChange[$i]["link"] = "http://instagram.com/p/{$id}";
  47.                   $photosToChange[$i]["src"] = json_decode($igJSON)->url;
  48.                }
  49.             }
  50.             $i++;
  51.          }
  52.          add_post_meta($post_id, "cachedPhotos", $photosToChange);
  53.       }
  54.    });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement