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