PluginProbe
Image Source Control Lite – Show Image Credits and Captions / 1.1.1
Image Source Control Lite – Show Image Credits and Captions v1.1.1
3.12.0 3.11.0 trunk 1.1 1.1.1 1.1.2 1.1.2.1 1.1.3 1.10 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.2 1.2.0.1 1.2.0.2 1.2.0.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.4.1 1.3.5 All 110 releases
image-source-control-isc / isc.php

isc.php in Image Source Control Lite – Show Image Credits and Captions 1.1.1, at isc.php

463 lines 16.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Image Source Control
4 Version: 1.1
5 Plugin URI: none
6 Description: The Image Source Control saves the source of an image, lists them and warns if it is missing.
7 Author: Thomas Maier
8 Author URI: http://www.webgilde.com
9 License: GPL v3
10
11 Image Source Control Plugin for WordPress
12 Copyright (C) 2012, Thomas Maier - thomas.maier@webgilde.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 3 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, see <http://www.gnu.org/licenses/>.
26 *
27 * Followed the following tutorials
28 * http://wpengineer.com/2076/add-custom-field-attachment-in-wordpress/
29 * http://bueltge.de/eigene-felder-dateiverwaltung-wordpress/1226/ (same like above, but in German)
30 *
31 *
32 */
33
34 //avoid direct calls to this file
35 if (!function_exists('add_action')) {
36 header('Status: 403 Forbidden');
37 header('HTTP/1.1 403 Forbidden');
38 exit();
39 }
40
41 define('ISCVERSION', '1.1.1');
42 define('ISCNAME', 'Image Source Control');
43 define('ISCTEXTDOMAIN', 'isc');
44 define('ISCDIR', basename(dirname(__FILE__)));
45 define('ISCPATH', plugin_dir_path(__FILE__));
46
47 load_plugin_textdomain(ISCTEXTDOMAIN, false, dirname(plugin_basename(__FILE__)) . '/languages/');
48
49 if (!class_exists('ISC_CLASS')) {
50
51 class ISC_CLASS {
52
53 /**
54 * define default meta fields
55 */
56 var $_fields = array(
57 'image_source' => array(
58 'id' => 'isc_image_source',
59 'default' => '',
60 ),
61 'image_source_own' => array(
62 'id' => 'isc_image_source_own',
63 'default' => '',
64 )
65 );
66
67 /**
68 * allowed image file types/extensions
69 * @since 1.1
70 */
71 var $_allowedExtensions = array(
72 'jpg', 'png', 'gif'
73 );
74
75 public function __construct() {
76
77 if (!current_user_can('upload_files'))
78 return FALSE;
79
80 add_filter('attachment_fields_to_edit', array(&$this, 'add_isc_fields'), 10, 2);
81 add_filter('attachment_fields_to_save', array(&$this, 'isc_fields_save'), 10, 2);
82
83 add_action('admin_menu', array($this, 'create_menu'));
84
85 add_shortcode('isc_list', array($this, 'list_post_attachments_with_sources_shortcode'));
86
87 add_action('admin_enqueue_scripts', array($this, 'add_admin_scripts'));
88
89 // ajax function; 'add_meta_fields' is the action defined in isc.js as the action to be called via ajax
90 add_action('wp_ajax_add_meta_fields', array($this, 'add_meta_values_to_attachments'));
91
92 // save image information in meta field when a post is saved
93 add_action('save_post', array($this, 'save_image_information_on_post_save'));
94 }
95
96 /**
97 * create the menu pages for isc
98 */
99 public function create_menu() {
100
101 // this page should be accessable by editors and higher
102 $menuhook = add_submenu_page('upload.php', 'missing image sources by Image Source Control Plugin', __('Missing Sources', ISCTEXTDOMAIN), 'edit_others_posts', ISCPATH . '/templates/missing_sources.php', '');
103 }
104
105 /**
106 * add scripts to admin pages
107 * @since 1.0
108 * @update 1.1.1
109 */
110 public function add_admin_scripts($hook) {
111 if ('image-source-control-isc/templates/missing_sources.php' != $hook)
112 return;
113 wp_enqueue_script('isc_script', plugins_url('/js/isc.js', __FILE__), false, ISCVERSION);
114 // this is to define ajaxurl to be able to use this in its own js script
115 // wp_localize_script( 'isc_script', 'IscAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
116 }
117
118 /**
119 * add custom field to attachment
120 * @param arr $form_fields
121 * @param object $post
122 * @return arr
123 * @since 1.0
124 * @updated 1.1
125 */
126 public function add_isc_fields($form_fields, $post) {
127 // add input field for source
128 $form_fields['isc_image_source']['label'] = __('Image Source', ISCTEXTDOMAIN);
129 $form_fields['isc_image_source']['value'] = get_post_meta($post->ID, 'isc_image_source', true);
130 $form_fields['isc_image_source']['helps'] = __('Include the image source here.', ISCTEXTDOMAIN);
131
132 // add checkbox to mark as your own image
133 $form_fields['isc_image_source_own']['input'] = 'html';
134 $form_fields['isc_image_source_own']['helps'] = __('Check this box if this is your own image and doesn\'t need a source.', ISCTEXTDOMAIN);
135 $form_fields['isc_image_source_own']['html'] = "<input type='checkbox' value='1' name='attachments[{$post->ID}][isc_image_source_own]' id='attachments[{$post->ID}][isc_image_source_own]' " . checked(get_post_meta($post->ID, 'isc_image_source_own', true), 1, false ) . "/> "
136 . __('This is my image', ISCTEXTDOMAIN);
137
138 return $form_fields;
139 }
140
141 /**
142 * save image source to post_meta
143 * @param object $post
144 * @param $attachment
145 * @return object $post
146 */
147 public function isc_fields_save($post, $attachment) {
148
149 if (isset($attachment['isc_image_source']))
150 update_post_meta($post['ID'], 'isc_image_source', $attachment['isc_image_source']);
151
152 update_post_meta($post['ID'], 'isc_image_source_own', $attachment['isc_image_source_own']);
153 return $post;
154 }
155
156 /**
157 * create image sources list for all images of this post
158 * @since 1.0
159 * @update 1.1
160 * @param int $post_id id of the current post/page
161 * @return echo output
162 */
163 public function list_post_attachments_with_sources($post_id = 0) {
164
165 if (empty($post_id)) {
166 global $post;
167 if (!empty($post->ID)) {
168 $post_id = $post->ID;
169 }
170 }
171
172 if (empty($post_id))
173 return;
174
175 $attachments = get_post_meta($post_id, 'isc_post_images', true);
176 // if attachments is an empty string, search for images in it
177 if ( $attachments == '' ) {
178 $this->save_image_information_on_load ($post_id, $_content);
179 $attachments = get_post_meta($post_id, 'isc_post_images', true);
180 }
181
182 $return = '';
183 if (!empty($attachments)) :
184 ob_start();
185 ?>
186 <p class="isc_image_list_title"><?php _e('image sources:', ISCTEXTDOMAIN); ?></p>
187 <ul class="isc_image_list"><?php
188 $atts = array();
189 foreach ($attachments as $attachment_id => $attachment_array) {
190
191 $atts[ $attachment_id ]['title'] = get_the_title($attachment_id);
192 $own = get_post_meta($attachment_id, 'isc_image_source_own', true);
193 $source = get_post_meta($attachment_id, 'isc_image_source', true);
194
195 if ( $own == '' AND $source == '' ) {
196 // remove if no information set
197 unset( $atts[ $attachment_id ] );
198 continue;
199 } elseif ( $own != '' ) {
200 $atts[ $attachment_id ]['source'] = __('by the author', ISCTEXTDOMAIN);
201 } else {
202 $atts[ $attachment_id ]['source'] = $source;
203 }
204
205 }
206
207 foreach ($atts as $atts_id => $atts_array) :
208 if ( empty( $atts_array['source'] ) ) continue;
209 ?><li><?php echo $atts_array['title'] . ': ' . $atts_array['source']; ?></li><?php
210 endforeach;
211 ?></ul><?php
212 $return = ob_get_clean();
213
214 endif;
215
216 // don't display anything, if no image sources displayed
217 if ( count( $atts ) === 0 ) return;
218
219 return $return;
220 }
221
222 /**
223 * shortcode function to list all image sources
224 * @param arr $atts
225 */
226 public function list_post_attachments_with_sources_shortcode($atts = array()) {
227
228 extract(shortcode_atts(array(
229 'id' => 0,
230 ), $atts));
231
232 // if $id not set, use the current ID from the post
233 if (empty($id)) {
234 global $post;
235 $id = $post->ID;
236 }
237
238 if (empty($id))
239 return;
240 return $this->list_post_attachments_with_sources($id);
241 }
242
243 /**
244 * get all attachments without sources
245 * the downside of this function: is there is not even an empty metakey field, nothing is going to be retrieved
246 * @todo fix this in WP 3.5 with compare => 'NOT EXISTS'
247 */
248 public function get_attachments_without_sources() {
249
250 $args = array(
251 'post_type' => 'attachment',
252 'numberposts' => -1,
253 'post_status' => null,
254 'post_parent' => null,
255 'meta_query' => array(
256 // image source is empty
257 array(
258 'key' => 'isc_image_source',
259 'value' => '',
260 'compare' => '=',
261 ),
262 // and image source is not set
263 array(
264 'key' => 'isc_image_source_own',
265 'value' => '1',
266 'compare' => '!=',
267 ),
268 )
269 );
270 $attachments = get_posts($args);
271 if (!empty($attachments)) {
272 return $attachments;
273 }
274 }
275
276 /**
277 * add meta values to all attachments
278 * @todo probably need to fix this when more fields are added along the way
279 * @todo use compare => 'NOT EXISTS' when WP 3.5 is up to retrieve only values where it is not set
280 * @todo this currently updates all empty fields; empty in this context is empty string, 0, false or not existing; add check if meta field already existed before
281 */
282 public function add_meta_values_to_attachments() {
283
284 // retrieve all attachments
285 $args = array(
286 'post_type' => 'attachment',
287 'numberposts' => -1,
288 'post_status' => null,
289 'post_parent' => null,
290 );
291
292 $attachments = get_posts($args);
293 if (empty($attachments))
294 return;
295
296 $count = 0;
297 foreach ($attachments as $_attachment) {
298 $set = 0;
299 setup_postdata($_attachment);
300 foreach ($this->_fields as $_field) {
301 $meta = get_post_meta($_attachment->ID, $_field['id'], true);
302 if (empty($meta)) {
303 update_post_meta($_attachment->ID, $_field['id'], $_field['default']);
304 $set = 1;
305 }
306 }
307 if ($set)
308 $count++;
309 }
310 echo sprintf(__('Added meta fields to %d images.', ISCTEXTDOMAIN), $count) .
311 '<br/><input type="button" value="' . __('reload page', ISCTEXTDOMAIN ) . '" onClick="window.location.reload()">';
312 die();
313 }
314
315 /**
316 * show the loading image from wp-admin/images/loading.gif
317 * @param bool $display should this be displayed directly or hidden? via inline css
318 */
319 public function show_loading_image($display = true) {
320
321 $img_path = admin_url("/images/loading.gif");
322 $file_path = ABSPATH . "wp-admin/images/loading.gif";
323 if (file_exists($file_path)) {
324 echo '<span id="isc_loading_img" style="display: none;"><img src="' . $img_path . '" width="16" height="16" alt="loading"/></span>';
325 }
326 }
327
328 /**
329 * this is an entry function to save image information to a post when it is saved
330 * @since 1.1
331 * @param type $post_id
332 */
333 public function save_image_information_on_post_save($post_id) {
334
335 // return, if save_post is called more than one time
336 if (did_action('save_post') !== 1)
337 return;
338
339 if ('attachment' == $_POST['post_type']) {
340 return;
341 }
342
343 // check if this is a revision and if so, use parent post id
344 if ($_id = wp_is_post_revision($post_id))
345 $post_id = $_id;
346
347 $_content = stripslashes($_REQUEST['content']);
348 $this->save_image_information($post_id, $_content);
349
350 }
351
352 /**
353 * save image information for a post when it is viewed and the image source list is enabled
354 * (this is in case the plugin is new and the current post wasn't saved before)
355 *
356 * @since 1.1
357 */
358
359 public function save_image_information_on_load( ) {
360
361 global $post;
362 if (empty( $post->ID )) return;
363
364 $post_id = $post->ID;
365 $_content = $post->post_content;
366
367 $this->save_image_information($post_id, $_content);
368
369 }
370
371 /**
372 * retrieve images added to a post or page and save all information as a meta value
373 * @since 1.1
374 * @todo check for more post types that maybe should not be parsed here
375 */
376 public function save_image_information( $post_id, $_content ) {
377
378 $_image_urls = $this->_filter_src_attributes($_content);
379
380 $_imgs = array();
381
382 foreach ($_image_urls as $_image_url) {
383 // get ID of images by url
384 $img_id = $this->get_image_by_url($_image_url);
385 $_imgs[$img_id] = array(
386 'src' => $_image_url
387 );
388 }
389
390 // add thumbnail information
391 $thumb_id = get_post_thumbnail_id($post_id);
392 $_imgs[$thumb_id] = array(
393 'src' => wp_get_attachment_url($thumb_id),
394 'thumbnail' => true
395 );
396
397 if (empty($_imgs))
398 $_imgs = false;
399 update_post_meta($post_id, 'isc_post_images', $_imgs);
400 }
401
402 /**
403 * filter image src attribute from text
404 * @since 1.1
405 * @return array with image src uris
406 */
407 public function _filter_src_attributes($content = '') {
408
409 $srcs = array();
410 if (empty($content))
411 return $srcs;
412
413 // parse HTML with DOM
414 $dom = new DOMDocument;
415 $dom->loadHTML($content);
416 foreach ($dom->getElementsByTagName('img') as $node) {
417 $srcs[] = $node->getAttribute('src');
418 }
419
420 return $srcs;
421 }
422
423 /**
424 * get image by url accessing the database directly
425 * @since 1.1
426 * @param string $url url of the image
427 * @return id of the image
428 */
429 public function get_image_by_url($url = '') {
430
431 if (empty($url))
432 return 0;
433 $types = implode('|', $this->_allowedExtensions);
434 // check for the format 'image-title-300x200.jpg' and remove the image size from it
435 $newurl = preg_replace("/-(\d+)x(\d+)\.({$types})$/i", '.${3}', $url);
436 global $wpdb;
437 $query = $wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE guid = %s", $newurl);
438 $id = $wpdb->get_var($query);
439 return $id;
440 }
441
442 }
443
444 function add_image_source_fields_start() {
445
446 $isc = new ISC_CLASS();
447 }
448
449 add_action('plugins_loaded', 'add_image_source_fields_start');
450
451 /**
452 * the next functions are just to have an easier access from outside the class
453 */
454 function isc_list($post_id = 0) {
455
456 $isc = new ISC_CLASS();
457 echo $isc->list_post_attachments_with_sources($post_id);
458
459 }
460
461 }
462
463