Example #1
0
 public function grab_freebies()
 {
     $client = new Client();
     $results = get_option($this->options_name);
     $results = explode(" ", $results["custom"]);
     $shots = $client->getShotsList();
     foreach ($shots as $shot) {
         foreach ($shot->tags as $tag) {
             if (in_array($tag, $results)) {
                 $src = $shot->images->normal;
                 $username = $shot->user->username;
                 //echo "<div id='freebie'><img src=$src /><p>$shot->title</p><p>$shot->description</p></div>";
                 $skip = FALSE;
                 $args = array('numberposts' => '8');
                 $recent_posts = wp_get_recent_posts($args);
                 foreach ($recent_posts as $recent) {
                     if ($recent["post_title"] === $shot->title) {
                         $skip = TRUE;
                     }
                 }
                 if (!$skip) {
                     $post = array('post_content' => "<p>{$shot->description}</p>This post was created by <a href=https://dribble.com/{$username}>{$username}</a> and can be viewed on dribbble <a href=https://dribbble.com/shots/{$shot->id}>here</a>", 'post_name' => $shot->title, 'post_title' => $shot->title, 'post_status' => $results["draft"] == true ? 'publish' : 'draft', 'post_excerpt' => "<p>{$shot->description}</p>This post was created by <a href=https://dribble.com/{$username}>{$username}</a> and can be viewed on dribbble <a href=https://dribbble.com/shots/{$shot->id}>here</a>", 'tags_input' => $shot->tags);
                     //error_reporting(0);
                     //$upload_dir = wp_upload_dir();
                     //$image_data = file_get_contents($src);
                     //$filename = basename($src);
                     //if(wp_mkdir_p($upload_dir['path']))
                     //	$file = $upload_dir['path'] . '/' . $filename;
                     //else
                     //	$file = $upload_dir['basedir'] . '/' . $filename;
                     //file_put_contents($file, $image_data);
                     $post_id = wp_insert_post($post, TRUE);
                     add_action('add_attachment', 'new_attachment');
                     $attach_id = media_sideload_image($src, $post_id, $post_id);
                     // we have the Image now, and the function will have fired too setting the thumbnail ID in the process, so lets remove the hook so we don't cause any more trouble
                     remove_action('add_attachment', 'new_attachment');
                 }
             }
         }
     }
 }