PluginProbe
Auto Featured Image (Auto Post Thumbnail) / 2.0
Auto Featured Image (Auto Post Thumbnail) v2.0
5.0.6 5.0.5 5.0.4 4.0.0 4.0.1 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.2.0 4.2.1 4.2.2 5.0.0 5.0.1 5.0.2 5.0.3 trunk 1.0 1.1 2.0 3.0 3.1 3.2.3 All 43 releases
auto-post-thumbnail / auto-post-thumbnail.php

auto-post-thumbnail.php in Auto Featured Image (Auto Post Thumbnail) 2.0, at auto-post-thumbnail.php

199 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 Plugin Name: Auto Post Thumbnail
5 Plugin URI: http://www.sanisoft.com/blog/2010/04/19/wordpress-plugin-automatic-post-thumbnail/
6 Description: Automatically generate the Post Thumbnail from the first image in post only if Post Thumbnail is not set manually.
7 Version: 2.0
8 Author: Aditya Mooley <adityamooley@sanisoft.com>
9 Author URI: http://www.sanisoft.com/blog/author/adityamooley/
10 */
11
12 /* Copyright 2009 Aditya Mooley (email : adityamooley@sanisoft.com)
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29
30 add_action('publish_post', 'apt_publish_post');
31 add_action('transition_post_status', 'apt_check_required_transition'); // Plugin should work for scheduled posts as well
32 add_action( 'admin_notices', 'apt_check_perms');
33
34 /**
35 * Check whether the required directory structure is available so that the plugin can create thumbnails if needed.
36 * If not, don't allow plugin activation.
37 */
38 function apt_check_perms() {
39 $uploads = wp_upload_dir(current_time('mysql'));
40
41 if ($uploads['error']) {
42 echo '<div class="updated"><p>';
43 echo $uploads['error'];
44
45 if ( function_exists('deactivate_plugins') ) {
46 deactivate_plugins('auto-post-thumbnail/auto-post-thumbnail.php', 'auto-post-thumbnail.php' );
47 echo '<br /> This plugin has been automatically deactivated.';
48 }
49
50 echo '</p></div>';
51 }
52 }
53
54 /**
55 * Function to check whether scheduled post is being published. If so, apt_publish_post should be called.
56 *
57 * @param $new_status
58 * @param $old_status
59 * @param $post
60 * @return void
61 */
62 function apt_check_required_transition($new_status='', $old_status='', $post='') {
63 if ('publish' == $new_status && 'future' == $old_status) {
64 apt_publish_post($post->ID);
65 }
66 }
67
68 /**
69 * Function to save first image in post as post thumbmail.
70 */
71 function apt_publish_post($post_id)
72 {
73 global $wpdb;
74
75 // First check whether Post Thumbnail is already set for this post.
76 if (get_post_meta($post_id, '_thumbnail_id', true) || get_post_meta($post_id, 'skip_post_thumb', true)) {
77 return;
78 }
79
80 $post = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE id = $post_id");
81
82 // Initialize variable used to store list of matched images as per provided regular expression
83 $matches = array();
84
85 // Get all images from post's body
86 preg_match_all('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'>]*)/i', $post[0]->post_content, $matches);
87
88 if (count($matches)) {
89 foreach ($matches[0] as $key => $image) {
90 /**
91 * If the image is from wordpress's own media gallery, then it appends the thumbmail id to a css class.
92 * Look for this id in the IMG tag.
93 */
94 preg_match('/wp-image-([\d]*)/i', $image, $thumb_id);
95 $thumb_id = $thumb_id[1];
96
97 // Ok. No id found. Some other way used to insert the image in post. Fetch the image from URL and do the needful.
98 if (!$thumb_id) {
99 $thumb_id = apt_generate_post_thumb($matches, $key);
100 }
101
102 // If we succeed in generating thumg, let's update post meta
103 if ($thumb_id) {
104 update_post_meta( $post_id, '_thumbnail_id', $thumb_id );
105 break;
106 }
107 }
108 }
109 }// end apt_publish_post()
110
111 /**
112 * Function to fetch the image from URL and generate the required thumbnails
113 */
114 function apt_generate_post_thumb($matches, $key)
115 {
116 $imageUrl = $matches[1][$key];
117
118 // Get the file name
119 $filename = substr($imageUrl, (strrpos($imageUrl, '/'))+1);
120
121 if (!(($uploads = wp_upload_dir(current_time('mysql')) ) && false === $uploads['error'])) {
122 return null;
123 }
124
125 // Generate unique file name
126 $filename = wp_unique_filename( $uploads['path'], $filename );
127
128 // Move the file to the uploads dir
129 $new_file = $uploads['path'] . "/$filename";
130
131 if (!ini_get('allow_url_fopen')) {
132 $file_data = curl_get_file_contents($imageUrl);
133 } else {
134 $file_data = @file_get_contents($imageUrl);
135 }
136
137 if (!$file_data) {
138 return null;
139 }
140
141 file_put_contents($new_file, $file_data);
142
143 // Set correct file permissions
144 $stat = stat( dirname( $new_file ));
145 $perms = $stat['mode'] & 0000666;
146 @ chmod( $new_file, $perms );
147
148 // Get the file type. Must to use it as a post thumbnail.
149 $wp_filetype = wp_check_filetype( $filename, $mimes );
150
151 extract( $wp_filetype );
152
153 // No file type! No point to proceed further
154 if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) ) {
155 return null;
156 }
157
158 // Compute the URL
159 $url = $uploads['url'] . "/$filename";
160
161 // Construct the attachment array
162 $attachment = array(
163 'post_mime_type' => $type,
164 'guid' => $url,
165 'post_parent' => null,
166 'post_title' => '',
167 'post_content' => '',
168 );
169
170 $thumb_id = wp_insert_attachment($attachment, $file, $post_id);
171 if ( !is_wp_error($thumb_id) ) {
172 require_once(ABSPATH . '/wp-admin/includes/image.php');
173
174 wp_update_attachment_metadata( $thumb_id, wp_generate_attachment_metadata( $thumb_id, $new_file ) );
175
176 return $thumb_id;
177 }
178
179 return null;
180 }
181
182 /**
183 * Function to fetch the contents of URL using curl in absense of allow_url_fopen.
184 *
185 * Copied from user comment on php.net (http://in.php.net/manual/en/function.file-get-contents.php#82255)
186 */
187 function curl_get_file_contents($URL) {
188 $c = curl_init();
189 curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
190 curl_setopt($c, CURLOPT_URL, $URL);
191 $contents = curl_exec($c);
192 curl_close($c);
193
194 if ($contents) {
195 return $contents;
196 }
197
198 return FALSE;
199 }