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