PluginProbe
Image Source Control Lite – Show Image Credits and Captions / 1.3.0
Image Source Control Lite – Show Image Credits and Captions v1.3.0
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.3.0, at isc.php

1,524 lines 64.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.3.0
5 Plugin URI: http://webgilde.com/en/image-source-control/
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.3.0');
42 define('ISCNAME', 'Image Source Control');
43 define('ISCTEXTDOMAIN', 'isc');
44 define('ISCDIR', basename(dirname(__FILE__)));
45 define('ISCPATH', plugin_dir_path(__FILE__));
46 define('WEBGILDE', 'http://webgilde.com/en/image-source-control');
47
48 load_plugin_textdomain(ISCTEXTDOMAIN, false, dirname(plugin_basename(__FILE__)) . '/languages/');
49
50 if (!class_exists('ISC_CLASS')) {
51
52 class ISC_CLASS
53 {
54 /**
55 * define default meta fields
56 */
57 protected $_fields = array(
58 'image_source' => array(
59 'id' => 'isc_image_source',
60 'default' => '',
61 ),
62 'image_source_own' => array(
63 'id' => 'isc_image_source_own',
64 'default' => '',
65 ),
66 'image_posts' => array(
67 'id' => 'isc_image_posts',
68 'default' => array()
69 )
70 );
71
72 /**
73 * Commonly used text elements
74 */
75 protected $_common_texts = array();
76
77 /**
78 * allowed image file types/extensions
79 * @since 1.1
80 */
81 protected $_allowedExtensions = array(
82 'jpg', 'png', 'gif'
83 );
84
85 /**
86 * Thumbnail size in list of all images.
87 * @since 1.2
88 */
89 protected $_thumbnail_size = array('thumbnail', 'medium', 'large', 'custom');
90
91 /**
92 * options saved in the db
93 * @since 1.2
94 */
95 protected $_options = array();
96
97 /**
98 * Position of image's caption
99 */
100 protected $_caption_position = array(
101 'top-left',
102 'top-center',
103 'top-right',
104 'center',
105 'bottom-left',
106 'bottom-center',
107 'bottom-right'
108 );
109
110 /**
111 * Setup registers filterts and actions.
112 */
113 public function __construct()
114 {
115 // load all plugin options
116 $this->_options = get_option('isc_options');
117 $this->_common_texts['not_available'] = __('Not available', ISCTEXTDOMAIN);
118
119 // insert all function for the frontend here
120
121 add_shortcode('isc_list', array($this, 'list_post_attachments_with_sources_shortcode'));
122 add_shortcode('isc_list_all', array($this, 'list_all_post_attachments_sources_shortcode'));
123 add_action('wp_enqueue_scripts', array($this, 'front_scripts'));
124 add_action('wp_head', array($this, 'front_head'));
125 add_action('the_content', array($this, 'content_filter'));
126 // insert all backend functions below this check
127 if (!current_user_can('upload_files')) {
128 return false;
129 }
130
131 register_activation_hook(ISCPATH . '/isc.php', array($this, 'activation'));
132
133 add_action('add_attachment', array($this, 'attachment_added'), 10, 2);
134 add_filter('attachment_fields_to_edit', array($this, 'add_isc_fields'), 10, 2);
135 add_filter('attachment_fields_to_save', array($this, 'isc_fields_save'), 10, 2);
136
137 add_action('admin_notices', array($this, 'admin_notices'));
138
139 add_action('admin_menu', array($this, 'create_menu'));
140 add_action('admin_init', array($this, 'SAPI_init'));
141
142 add_action('admin_enqueue_scripts', array($this, 'add_admin_scripts'));
143 add_action( 'admin_print_scripts', array($this, 'admin_headjs') );
144
145 // save image information in meta field when a post is saved
146 add_action('save_post', array($this, 'save_image_information_on_post_save'));
147 }
148
149 public function get_source_by_url($url)
150 {
151 $id = $this->get_image_by_url($url);
152 $metadata['source'] = get_post_meta($id, 'isc_image_source', true);
153 $metadata['own'] = get_post_meta($id, 'isc_image_source_own', true);
154
155 $source = $this->_common_texts['not_available'];
156
157 $att_post = get_post($id);
158
159 if ('' != $metadata['own']) {
160 if ($this->_options['use_authorname']) {
161 if (!empty($att_post)) {
162 $source = get_the_author_meta('display_name', $att_post->post_author);
163 }
164 } else {
165 $source = $this->options['by_author_text'];
166 }
167 } else {
168 if ('' != $metadata['source']) {
169 $source = $metadata['source'];
170 }
171 }
172 return $source;
173 }
174
175 public function content_filter($content)
176 {
177 $options = $this->get_isc_options();
178 if ($options['source_on_image']) {
179 $pattern = '#(\[caption.*align="(.+)"[^\]*]{0,}\])? *(<a [^>]+>)? *(<img .*class=".*(align\d{4,})?.*wp-image-(\d+)\D*".*src="(.+)".*/?>).*(?(3)(?:</a>)|.*).*(?(1)(?:\[/caption\])|.*)#isU';
180 $count = preg_match_all($pattern, $content, $matches);
181 if (false !== $count) {
182 for ($i=0; $i < $count; $i++) {
183 $id = $matches[6][$i];
184 $src = $matches[7][$i];
185 $source = '<p class="isc-source-text">' . $options['source_pretext'] . ' ' . $this->get_source_by_url($src) . '</p>';
186 $old_content = $matches[0][$i];
187 $new_content = str_replace('wp-image-' . $id, 'wp-image-' . $id . ' with-source', $old_content);
188 $alignment = (!empty($matches[1][$i]))? $matches[2][$i] : $matches[5][$i];
189
190 $content = str_replace($old_content, '<div id="isc_attachment_' . $id . '" class="isc-source ' . $alignment . '"> ' . $new_content . $source . '</div>', $content);
191 }
192 }
193 }
194 return $content;
195 }
196
197 public function attachment_added($att_id)
198 {
199 foreach ($this->_fields as $field) {
200 update_post_meta($att_id, $field['id'], $field['default']);
201 }
202 }
203
204 /**
205 * Front-end scripts in <head /> section.
206 */
207 public function front_head()
208 {
209 $options = $this->get_isc_options();
210 ?>
211 <script type="text/javascript">
212 /* <![CDATA[ */
213 var isc_front_data =
214 {
215 caption_position : '<?php echo $options['caption_position']; ?>',
216 }
217 /* ]]> */
218 </script>
219 <?php
220 }
221
222 /**
223 * Enqueue scripts for the front-end.
224 */
225 public function front_scripts() {
226 wp_enqueue_script('isc_front_js', plugins_url('/js/front-js.js', __FILE__), array('jquery'), ISCVERSION);
227 }
228
229 /**
230 * create the menu pages for isc
231 */
232 public function create_menu()
233 {
234 global $isc_missing;
235 global $isc_setting;
236
237 /**
238 * Check if the page is already created.
239 */
240 if (empty($isc_missing)) {
241 // these pages should be accessible by editors and higher
242 $isc_missing = add_submenu_page('upload.php', 'missing image sources by Image Source Control Plugin', __('Missing Sources', ISCTEXTDOMAIN), 'edit_others_posts', ISCPATH . '/templates/missing_sources.php', '');
243 $isc_setting = add_options_page(__('Image control - ISC plugin', ISCTEXTDOMAIN), __('Image Control', ISCTEXTDOMAIN), 'edit_others_posts', 'isc_settings_page', array($this, 'render_isc_settings_page'));
244 }
245 }
246
247 /**
248 * add scripts to admin pages
249 * @since 1.0
250 * @update 1.1.1
251 */
252 public function add_admin_scripts($hook)
253 {
254 global $isc_setting;
255 if ('post.php' == $hook) {
256 wp_enqueue_script('isc_postphp_script', plugins_url('/js/post.php.js', __FILE__), array('jquery'), ISCVERSION);
257 }
258 if ($hook == $isc_setting) {
259 wp_enqueue_script('isc_script', plugins_url('/js/isc.js', __FILE__), false, ISCVERSION);
260 wp_enqueue_style('isc_image_settings_css', plugins_url('/css/image-settings.css', __FILE__), false, ISCVERSION);
261 }
262 }
263
264 /**
265 * add custom field to attachment
266 * @param arr $form_fields
267 * @param object $post
268 * @return arr
269 * @since 1.0
270 * @updated 1.1
271 */
272 public function add_isc_fields($form_fields, $post)
273 {
274 // add input field for source
275 $form_fields['isc_image_source']['label'] = __('Image Source', ISCTEXTDOMAIN);
276 $form_fields['isc_image_source']['value'] = get_post_meta($post->ID, 'isc_image_source', true);
277 $form_fields['isc_image_source']['helps'] = __('Include the image source here.', ISCTEXTDOMAIN);
278
279 // add checkbox to mark as your own image
280 $form_fields['isc_image_source_own']['input'] = 'html';
281 $form_fields['isc_image_source_own']['label'] = '';
282 $form_fields['isc_image_source_own']['helps'] =
283 __('Check this box if this is your own image and doesn\'t need a source.', ISCTEXTDOMAIN);
284 $form_fields['isc_image_source_own']['html'] =
285 "<input type='checkbox' value='1' name='attachments[{$post->ID}][isc_image_source_own]' id='attachments[{$post->ID}][isc_image_source_own]' "
286 . checked(get_post_meta($post->ID, 'isc_image_source_own', true), 1, false )
287 . " style=\"width:14px\"/> "
288 . __('This is my image', ISCTEXTDOMAIN);
289
290 return $form_fields;
291 }
292
293 /**
294 * save image source to post_meta
295 * @param object $post
296 * @param $attachment
297 * @return object $post
298 */
299 public function isc_fields_save($post, $attachment)
300 {
301 if (isset($attachment['isc_image_source'])) {
302 update_post_meta($post['ID'], 'isc_image_source', $attachment['isc_image_source']);
303 }
304 update_post_meta($post['ID'], 'isc_image_source_own', $attachment['isc_image_source_own']);
305 return $post;
306 }
307
308 /**
309 * create image sources list for all images of this post
310 * @since 1.0
311 * @update 1.1
312 * @param int $post_id id of the current post/page
313 * @return echo output
314 */
315 public function list_post_attachments_with_sources($post_id = 0)
316 {
317 global $post;
318
319 if (empty($post_id)) {
320 if (!empty($post->ID)) {
321 $post_id = $post->ID;
322 } else {
323 return;
324 }
325 }
326
327 $attachments = get_post_meta($post_id, 'isc_post_images', true);
328 // if attachments is an empty string, search for images in it
329 if ($attachments == '') {
330 $this->save_image_information_on_load();
331 $this->update_image_posts_meta($post_id, $post->post_content);
332
333 $attachments = get_post_meta($post_id, 'isc_post_images', true);
334 }
335
336 $return = '';
337 if (!empty($attachments)) {
338 $atts = array();
339 foreach ($attachments as $attachment_id => $attachment_array) {
340 $atts[$attachment_id]['title'] = get_the_title($attachment_id);
341 $own = get_post_meta($attachment_id, 'isc_image_source_own', true);
342 $source = get_post_meta($attachment_id, 'isc_image_source', true);
343
344 if ( $own == '' && $source == '' ) {
345 // remove if no information set
346 unset($atts[$attachment_id]);
347 continue;
348 } elseif ($own != '') {
349 if ($this->_options['use_authorname']) {
350 $authorname = '';
351 $att_post = get_post($attachment_id);
352 if (null !== $att_post) {
353 $authorname = get_the_author_meta('display_name', $att_post->post_author);
354 }
355 $atts[$attachment_id ]['source'] = $authorname;
356 } else {
357 $atts[$attachment_id ]['source'] = $this->_options['by_author_text'];
358 }
359 } else {
360 $atts[$attachment_id ]['source'] = $source;
361 }
362 }
363
364 $return = $this->_renderAttachments($atts);
365 }
366
367 return $return;
368 }
369
370 /**
371 * @param array $attachments
372 */
373 protected function _renderAttachments($attachments)
374 {
375 // don't display anything, if no image sources displayed
376 if ($attachments == array()) {
377 return ;
378 }
379
380 $options = $this->get_isc_options();
381 $show_text = __('Show the list', ISCTEXTDOMAIN);
382 $hide_text = __('Hide the list', ISCTEXTDOMAIN);
383
384 ob_start();
385 $headline = $this->_options['image_list_headline'];
386 $hide_style = ($options['hide_list'])? 'style="height: 0px; overflow: hidden;"': 'style="height: 100%; overflow: hidden;"';
387 $hide_class = ($options['hide_list'])? ' isc-list-up': ' isc-list-down';
388 $hide_title = ($options['hide_list'])? $show_text : $hide_text;
389 printf('<p class="isc_image_list_title" title="%2$s" style="cursor: pointer;">%1$s</p>', $headline, $hide_title); ?>
390 <script type="text/javascript">
391 /* <!--[CDATA[ */
392 isc_jstext = {
393 show_list: "<?php echo esc_attr($show_text); ?>",
394 hide_list: "<?php echo esc_attr($hide_text); ?>"
395 }
396 /* ]]--> */
397 </script>
398 <ul class="isc_image_list <?php echo $hide_class; ?>"<?php echo $hide_style; ?>><?php
399
400 foreach ($attachments as $atts_id => $atts_array) {
401 if (empty($atts_array['source'])) {
402 continue;
403 }
404 printf('<li>%1$s: %2$s</li>', $atts_array['title'], $atts_array['source']);
405 }
406 ?></ul><?php
407 return ob_get_clean();
408 }
409
410 /**
411 * shortcode function to list all image sources
412 * @param arr $atts
413 */
414 public function list_post_attachments_with_sources_shortcode($atts = array())
415 {
416 global $post;
417 extract(shortcode_atts(array('id' => 0), $atts));
418
419 // if $id not set, use the current ID from the post
420 if (empty($id)) {
421 $id = $post->ID;
422 }
423
424 if (empty($id)) {
425 return;
426 }
427 return $this->list_post_attachments_with_sources($id);
428 }
429
430 /**
431 * get all attachments without sources
432 * the downside of this function: is there is not even an empty metakey field, nothing is going to be retrieved
433 * @todo fix this in WP 3.5 with compare => 'NOT EXISTS'
434 */
435 public function get_attachments_without_sources()
436 {
437 $args = array(
438 'post_type' => 'attachment',
439 'numberposts' => -1,
440 'post_status' => null,
441 'post_parent' => null,
442 'meta_query' => array(
443 // image source is empty
444 array(
445 'key' => 'isc_image_source',
446 'value' => '',
447 'compare' => '=',
448 ),
449 // and image source is not set
450 array(
451 'key' => 'isc_image_source_own',
452 'value' => '1',
453 'compare' => '!=',
454 ),
455 )
456 );
457
458 $attachments = get_posts($args);
459 if (!empty($attachments)) {
460 return $attachments;
461 }
462 }
463
464 /**
465 * add meta values to all attachments
466 * @todo probably need to fix this when more fields are added along the way
467 * @todo use compare => 'NOT EXISTS' when WP 3.5 is up to retrieve only values where it is not set
468 * @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
469 */
470 public function add_meta_values_to_attachments()
471 {
472 // retrieve all attachments
473 $args = array(
474 'post_type' => 'attachment',
475 'numberposts' => -1,
476 'post_status' => null,
477 'post_parent' => null,
478 );
479
480 $attachments = get_posts($args);
481 if (empty($attachments)) {
482 return;
483 }
484
485 $count = 0;
486 foreach ($attachments as $_attachment) {
487 $set = false;
488 setup_postdata($_attachment);
489 foreach ($this->_fields as $_field) {
490 $meta = get_post_meta($_attachment->ID, $_field['id'], true);
491 if (empty($meta)) {
492 update_post_meta($_attachment->ID, $_field['id'], $_field['default']);
493 $set = true;
494 }
495 }
496 if ($set) {
497 $count++;
498 }
499 }
500 }
501
502 /**
503 * Display scripts in <head></head> section of admin page. Useful for creating js variables in the js global namespace.
504 */
505 public function admin_headjs()
506 {
507 global $pagenow;
508 $options = $this->get_isc_options();
509 if ('post.php' == $pagenow) {
510 ?>
511 <script type="text/javascript">
512 /* <![CDATA[ */
513 isc_data = {
514 warning_nosource : <?php echo (($options['warning_nosource'])? 'true' : 'false'); ?>,
515 block_form_message : '<?php _e('Please specify the image source', ISCTEXTDOMAIN); ?>'
516 }
517 /* ]]> */
518 </script>
519 <?php
520 }
521 }
522
523 /**
524 * show the loading image from wp-admin/images/loading.gif
525 * @param bool $display should this be displayed directly or hidden? via inline css
526 */
527 public function show_loading_image($display = true)
528 {
529 $img_path = admin_url("/images/loading.gif");
530 $file_path = ABSPATH . "wp-admin/images/loading.gif";
531 if (file_exists($file_path)) {
532 echo '<span id="isc_loading_img" style="display: none;"><img src="' . $img_path . '" width="16" height="16" alt="loading"/></span>';
533 }
534 }
535
536 /**
537 * this is an entry function to save image information to a post when it is saved
538 * @since 1.1
539 * @param type $post_id
540 */
541 public function save_image_information_on_post_save($post_id)
542 {
543 // return, if save_post is called more than one time
544 if (did_action('save_post') !== 1) {
545 return;
546 }
547
548 if (isset($_POST['post_type']) && 'attachment' == $_POST['post_type']) {
549 return;
550 }
551
552 // check if this is a revision and if so, use parent post id
553 if ($_id = wp_is_post_revision($post_id)) {
554 $post_id = $_id;
555 }
556
557 $_content = '';
558 if ( !empty( $_REQUEST['content']) ) $_content = stripslashes($_REQUEST['content']);
559
560 // Needs to be called before the 'isc_post_images' field is updated.
561 $this->update_image_posts_meta($post_id, $_content);
562
563 $this->save_image_information($post_id, $_content);
564 }
565
566 /**
567 * save image information for a post when it is viewed and the image source list is enabled
568 * (this is in case the plugin is new and the current post wasn't saved before)
569 *
570 * @since 1.1
571 */
572 public function save_image_information_on_load()
573 {
574 global $post;
575 if (empty($post->ID)) {
576 return;
577 }
578
579 $post_id = $post->ID;
580 $_content = $post->post_content;
581
582 $this->save_image_information($post_id, $_content);
583 }
584
585 /**
586 * retrieve images added to a post or page and save all information as a meta value
587 * @since 1.1
588 * @todo check for more post types that maybe should not be parsed here
589 */
590 public function save_image_information($post_id, $_content)
591 {
592 $_image_urls = $this->_filter_src_attributes($_content);
593 $_imgs = array();
594
595 foreach ($_image_urls as $_image_url) {
596 // get ID of images by url
597 $img_id = $this->get_image_by_url($_image_url);
598 $_imgs[$img_id] = array(
599 'src' => $_image_url
600 );
601 }
602
603 // add thumbnail information
604 $thumb_id = get_post_thumbnail_id($post_id);
605
606 /**
607 * if an image is used both inside the post and as post thumbnail, the thumbnail entry overrides the regular image.
608 */
609 if ( !empty( $thumb_id )) {
610 $_imgs[$thumb_id] = array(
611 'src' => wp_get_attachment_url($thumb_id),
612 'thumbnail' => true
613 );
614 }
615
616 if (empty($_imgs)) {
617 $_imgs = false;
618 }
619 update_post_meta($post_id, 'isc_post_images', $_imgs);
620 }
621
622 /**
623 * filter image src attribute from text
624 * @since 1.1
625 * @updated 1.1.3
626 * @return array with image src uris
627 */
628 public function _filter_src_attributes($content = '')
629 {
630 $srcs = array();
631 if (empty($content))
632 return $srcs;
633
634 // parse HTML with DOM
635 $dom = new DOMDocument;
636
637 libxml_use_internal_errors(true);
638 $dom->loadHTML($content);
639
640 // Prevents from sending E_WARNINGs notice (Outputs are forbidden during activation)
641 libxml_clear_errors();
642
643 foreach ($dom->getElementsByTagName('img') as $node) {
644 $srcs[] = $node->getAttribute('src');
645 }
646
647 return $srcs;
648 }
649
650 /**
651 * get image by url accessing the database directly
652 * @since 1.1
653 * @updated 1.1.3
654 * @param string $url url of the image
655 * @return id of the image
656 */
657 public function get_image_by_url($url = '')
658 {
659 if (empty($url)) {
660 return 0;
661 }
662 $types = implode('|', $this->_allowedExtensions);
663 // check for the format 'image-title-(e12452112-)300x200.jpg' and remove the image size and edit mark from it
664 $newurl = preg_replace("/(-e\d+){0,1}-(\d+)x(\d+)\.({$types})$/i", '.${4}', $url);
665 global $wpdb;
666 $query = $wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE guid = %s", $newurl);
667 $id = $wpdb->get_var($query);
668 return $id;
669 }
670
671 /**
672 * Update isc_image_posts meta field for all images found in a post with a given ID.
673 * @param $post_id ID of the target post
674 * @param $content content of the target post
675 */
676 public function update_image_posts_meta($post_id, $content)
677 {
678 $image_urls = $this->_filter_src_attributes($content);
679 $image_ids = array();
680 $added_images = array();
681 $removed_images = array();
682
683 $isc_post_images = get_post_meta($post_id, 'isc_post_images', true);
684
685 foreach ($image_urls as $url) {
686 $id = intval($this->get_image_by_url($url));
687 array_push($image_ids, $id);
688 if (is_array($isc_post_images) && !array_key_exists($id, $isc_post_images)) {
689 array_push($added_images, $id);
690 }
691 }
692 if (is_array($isc_post_images)) {
693 foreach ($isc_post_images as $old_id => $value) {
694 if (!in_array($old_id, $image_ids)) {
695 array_push($removed_images, $old_id);
696 } else {
697 if (!empty($old_id)) {
698 $meta = get_post_meta($old_id, 'isc_image_posts', true);
699 if (empty($meta)) {
700 update_post_meta($old_id, 'isc_image_posts', array($post_id));
701 } else {
702 // In case the isc_image_posts is not up to date
703 if (is_array($meta) && !in_array($post_id, $meta)) {
704 array_push($meta, $post_id);
705 update_post_meta($old_id, 'isc_image_posts', $meta);
706 }
707 }
708 }
709 }
710 }
711 }
712
713 foreach ($added_images as $id) {
714 $meta = get_post_meta($id, 'isc_image_posts', true);
715 if (!is_array($meta) || array() == $meta) {
716 update_post_meta($id, 'isc_image_posts', array($post_id));
717 } else {
718 array_push($meta, $post_id);
719 update_post_meta($id, 'isc_image_posts', $meta);
720 }
721 }
722
723 foreach ($removed_images as $id) {
724 $image_meta = get_post_meta($id, 'isc_image_posts', true);
725 if (is_array($image_meta)) {
726 $offset = array_search($post_id, $image_meta);
727 if (false !== $offset) {
728 array_splice($image_meta, $offset, 1);
729 update_post_meta($id, 'isc_image_posts', $image_meta);
730 }
731 }
732 }
733 }
734
735 /**
736 * create shortcode to list all image sources in the frontend
737 * @param array $atts
738 * @since 1.1.3
739 * @todo link to the post
740 */
741 public function list_all_post_attachments_sources_shortcode($atts = array())
742 {
743
744 /**
745 * @todo why not translate here with the code below?
746 * > Because the two if statements below will need to call gettext (again) for comparing values.
747 */
748 extract(shortcode_atts(array(
749 'per_page' => 99999,
750 'before_links' => '',
751 'after_links' => '',
752 'prev_text' => '&#171; Previous',
753 'next_text' => 'Next &#187;'
754 ),
755 $atts));
756
757 /**
758 * @todo why not include this into the array above?
759 */
760 if ('&#171; Previous' == $prev_text)
761 $prev_text = __('&#171; Previous', ISCTEXTDOMAIN);
762 if ('Next &#187;' == $next_text)
763 $next_text = __('Next &#187;', ISCTEXTDOMAIN);
764
765 // retrieve all attachments
766 $args = array(
767 'post_type' => 'attachment',
768 'numberposts' => -1,
769 'post_status' => null,
770 'post_parent' => null,
771 'meta_query' => array(
772 array(
773 'key' => 'isc_image_posts',
774 'value' => 'a:0:{}',
775 'compare' => '!='
776 )
777 )
778 /** @todo maybe add offset to not retrieve the first results when not on first page */
779 /** @todo maybe add limit to not retrieve more results than on the current page */
780 /** >No, we need to get total count of attachment with parents for $max_page in the pagination link. */
781 );
782
783 $attachments = get_posts($args);
784 if (empty($attachments)) {
785 return;
786 }
787
788 $options = $this->get_isc_options();
789
790 $connected_atts = array();
791
792 //Keeps only those ones who have parent
793
794 foreach ($attachments as $_attachment) {
795 $connected_atts[$_attachment->ID]['source'] = get_post_meta($_attachment->ID, 'isc_image_source', true);
796 $connected_atts[$_attachment->ID]['own'] = get_post_meta($_attachment->ID, 'isc_image_source_own', true);
797 $connected_atts[$_attachment->ID]['title'] = $_attachment->post_title;
798 $connected_atts[$_attachment->ID]['author_name'] = '';
799 if ('' != $connected_atts[$_attachment->ID]['own']) {
800 $connected_atts[$_attachment->ID]['author_name'] = get_the_author_meta('display_name', $_attachment->post_author);
801 }
802
803 $metadata = get_post_meta($_attachment->ID, 'isc_image_posts', true);
804 $usage_data = '';
805
806 if (is_array($metadata) && array() != $metadata) {
807 $usage_data .= "<ul style='margin: 0;'>";
808 foreach($metadata as $data) {
809 $usage_data .= sprintf(__('<li><a href="%1$s" title="View %2$s">%3$s</a></li>', ISCTEXTDOMAIN),
810 esc_url(get_permalink($data)),
811 esc_attr(get_the_title($data)),
812 esc_html(get_the_title($data))
813 );
814 }
815 $usage_data .= "</ul>";
816 }
817
818 $connected_atts[$_attachment->ID]['posts'] = $usage_data;
819 }
820
821 $total = count($connected_atts);
822
823 if (0 == $total)
824 return;
825
826 $page = isset($_GET['isc-page']) ? intval($_GET['isc-page']) : 1;
827 $down_limit = 1; // First page
828
829 $up_limit = 1;
830
831 if ($per_page < $total) {
832 $rem = $total % $per_page; // The Remainder of $total/$per_page
833 $up_limit = ($total - $rem) / $per_page;
834 if (0 < $rem) {
835 $up_limit++; //If rem is positive, add the last page that contains less than $per_page attachment;
836 }
837 }
838
839 ob_start();
840 if ( 2 > $up_limit ) {
841 $this->display_all_attachment_list($connected_atts);
842 } else {
843 $starting_atts = $per_page * ($page - 1); // for page 2 and 3 $per_page start display on $connected_atts[3*(2-1) = 3]
844 $paged_atts = array_slice($connected_atts, $starting_atts, $per_page, true);
845 $this->display_all_attachment_list($paged_atts);
846 $this->pagination_links($up_limit, $before_links, $after_links, $prev_text, $next_text);
847 }
848 if (isset($options['webgilde']) && true == $options['webgilde']) {
849 ?>
850 <p class="isc-backlink"><?php printf(__('Image list created by <a href="%s" title="Image Source Control">Image Source Control Plugin</a>', ISCTEXTDOMAIN), WEBGILDE); ?></p>
851 <?php
852 }
853
854 $output = ob_get_clean();
855 return $output;
856 }
857
858
859 /**
860 * performs rendering of all attachments list
861 * @since 1.1.3
862 */
863 public function display_all_attachment_list($atts)
864 {
865 if (!is_array($atts) || $atts == array())
866 return;
867 $options = $this->get_isc_options();
868 ?>
869 <table>
870 <thead>
871 <?php if ($options['thumbnail_in_list']) : ?>
872 <th><?php _e('Thumbnail', ISCTEXTDOMAIN); ?></th>
873 <?php endif; ?>
874 <th><?php _e("Attachment's ID", ISCTEXTDOMAIN); ?></th>
875 <th><?php _e('Title', ISCTEXTDOMAIN); ?></th>
876 <th><?php _e('Attached to', ISCTEXTDOMAIN); ?></th>
877 <th><?php _e('Source', ISCTEXTDOMAIN); ?></th>
878 </thead>
879 <tbody>
880 <?php foreach ($atts as $id => $data) : ?>
881 <?php
882 $source = $this->_common_texts['not_available'];
883 if ('' != $data['own']) {
884 /** @todo ment for later: this text was used above already; find a place to but it so it is defined only once and used where needed */
885 if ($this->_options['use_authorname']) {
886 $source = $data['author_name'];
887 } else {
888 $source = $this->_options['by_author_text'];
889 }
890 } else {
891 if (!empty($data['source']))
892 $source = $data['source'];
893 }
894 ?>
895 <tr>
896 <?php
897 $v_align = '';
898 if ($options['thumbnail_in_list']) :
899 $v_align = 'style="vertical-align: top;"';
900 ?>
901 <?php if ('custom' != $options['thumbnail_size']) : ?>
902 <td><?php echo wp_get_attachment_image($id, $options['thumbnail_size']); ?></td>
903 <?php else : ?>
904 <td><?php echo wp_get_attachment_image($id, array($options['thumbnail_width'], $options['thumbnail_height'])); ?></td>
905 <?php endif; ?>
906 <?php endif; ?>
907 <td <?php echo $v_align;?>><?php echo $id; ?></td>
908 <td <?php echo $v_align;?>><?php echo $data['title']; ?></td>
909 <td <?php echo $v_align;?>><?php echo $data['posts']; ?></td>
910 <td <?php echo $v_align;?>><?php echo esc_html($source); ?></td>
911 </tr>
912 <?php endforeach; ?>
913 </tbody>
914 </table>
915 <?php
916 }
917
918 /**
919 * Render pagination links, use $before_links and after_links to wrap pagination links inside an additional block
920 * @param int $max_page total page count
921 * @param string $before_links optional html to display before pagination links
922 * @param string $after_links optional html to display after pagination links
923 * @param string $prev_text text for the previous page link
924 * @param string $next_text text for the next page link
925 * @since 1.1.3
926 *
927 */
928 public function pagination_links($max_page, $before_links, $after_links, $prev_text, $next_text)
929 {
930 if ((!isset($max_page)) || (!isset($before_links)) || (!isset($after_links)) || (!isset($prev_text)) || (!isset($next_text)))
931 return;
932 if (!empty($before_links))
933 echo $before_links;
934 ?>
935 <div class="isc-paginated-links">
936 <?php
937 $page = isset($_GET['isc-page']) ? intval($_GET['isc-page']) : 1;
938 if ($max_page < $page) {
939 $page = $max_page;
940 }
941 if ($page < 1) {
942 $page = 1;
943 }
944 $min_page = 1;
945 $backward_distance = $page - $min_page;
946 $forward_distance = $max_page - $page;
947
948 $page_link = get_page_link();
949
950 /**
951 * Remove the query_string of the page_link (?page_id=xyz for the standard permalink structure),
952 * which is already captured in $_SERVER['QUERY_STRING'].
953 * @todo replace regex with other value (does WP store the url path without attributes somewhere?
954 * >get_page_link() returns the permalink but for the default WP permalink structure, the permalink looks like "http://domain.tld/?p=52", while $_GET
955 * still has a field named 'page_id' with the same value of 52.
956 */
957
958 $pos = strpos($page_link, '?');
959 if (false !== $pos) {
960 $page_link = substr($page_link, 0, $pos);
961 }
962
963 /**
964 * Unset the actual "$_GET['isc-page']" variable (if is set). Pagination variable will be appended to the new query string with a different value for each
965 * pagination link.
966 */
967
968 if (isset($_GET['isc-page'])) {
969 unset($_GET['isc-page']);
970 }
971
972 $query_string = http_build_query($_GET);
973
974 $isc_query_tag = '';
975 if (empty($query_string)) {
976 $isc_query_tag = '?isc-page=';
977 } else {
978 $query_string = '?' . $query_string;
979 $isc_query_tag = '&isc-page=';
980 }
981
982 if ($min_page != $page) {
983 ?>
984 <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page-1); ?>" class="prev page-numbers"><?php echo $prev_text; ?></a>
985 <?php
986 }
987
988 if (5 < $max_page) {
989
990 if (3 < $backward_distance) {
991 ?>
992 <a href="<?php echo $page_link . $query_string . $isc_query_tag; ?>1" class="page-numbers">1</a>
993 <span class="page-numbers dots">...</span>
994 <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page-2);?>" class="page-numbers"><?php echo $page-2; ?></a>
995 <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page-1);?>" class="page-numbers"><?php echo $page-1; ?></a>
996 <span class="page-numbers current"><?php echo $page; ?></span>
997 <?php
998 } else {
999 for ($i = 1; $i <= $page; $i++) {
1000 if ($i == $page) {
1001 ?>
1002 <span class="page-numbers current"><?php echo $i; ?></span>
1003 <?php
1004 } else {
1005 ?>
1006 <a href="<?php echo $page_link . $query_string . $isc_query_tag . $i;?>" class="page-numbers"><?php echo $i; ?></a>
1007 <?php
1008 }
1009 }
1010 }
1011
1012 if (3 < $forward_distance) {
1013 ?>
1014 <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page+1);?>" class="page-numbers"><?php echo $page+1; ?></a>
1015 <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page+2);?>" class="page-numbers"><?php echo $page+2; ?></a>
1016 <span class="page-numbers dots">...</span>
1017 <a href="<?php echo $page_link . $query_string . $isc_query_tag . $max_page;?>" class="page-numbers"><?php echo $max_page; ?></a>
1018 <?php
1019 } else {
1020 for ($i = $page+1; $i <= $max_page; $i++) {
1021 ?>
1022 <a href="<?php echo $page_link . $query_string . $isc_query_tag . $i;?>" class="page-numbers"><?php echo $i; ?></a>
1023 <?php
1024 }
1025 }
1026 } else {
1027 for ($i = 1; $i <= $max_page; $i++) {
1028 if ($i == $page) {
1029 ?>
1030 <span class="page-numbers current"><?php echo $i; ?></span>
1031 <?php
1032 } else {
1033 ?>
1034 <a href="<?php echo $page_link . $query_string . $isc_query_tag . $i;?>" class="page-numbers"><?php echo $i; ?></a>
1035 <?php
1036 }
1037 }
1038 }
1039 if ($page != $max_page) {
1040 ?>
1041 <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page+1);?>" class="next page-numbers"><?php echo $next_text; ?></a>
1042 <?php
1043 }
1044 ?>
1045 </div>
1046 <?php
1047 echo $after_links;
1048 }
1049
1050 /**
1051 * The activation function
1052 */
1053 public function activation()
1054 {
1055 if (!is_array(get_option('isc_options'))) {
1056 update_option( 'isc_options', $this->default_options() );
1057 }
1058 $options = $this->get_isc_options();
1059 if (!$options['installed']) {
1060 /**
1061 * Here, all jobs to perform during first activation, especially options and custom fields.
1062 * Important: NO add_action('something', 'somefunction') here.
1063 */
1064
1065 // adds meta fields for attachments
1066 $this->add_meta_values_to_attachments();
1067
1068 // set all isc_image_posts meta fields.
1069 $this->init_image_posts_metafield();
1070
1071 $options['installed'] = true;
1072 update_option('isc_options', $options);
1073 }
1074 }
1075
1076 /**
1077 * Returns default options
1078 */
1079 public function default_options()
1080 {
1081 $default['image_list_headline'] = __('image sources', ISCTEXTDOMAIN);
1082 $default['use_authorname'] = true;
1083 $default['by_author_text'] = __('Owned by the author', ISCTEXTDOMAIN);
1084 $default['installed'] = false;
1085 $default['version'] = ISCVERSION;
1086 $default['webgilde'] = false;
1087 $default['thumbnail_in_list'] = false;
1088 $default['thumbnail_size'] = 'thumbnail';
1089 $default['thumbnail_width'] = 150;
1090 $default['thumbnail_height'] = 150;
1091 $default['warning_nosource'] = true;
1092 $default['warning_onesource_missing'] = true;
1093 $default['hide_list'] = false;
1094 $default['caption_position'] = 'top-left';
1095 $default['source_on_image'] = false;
1096 $default['source_pretext'] = __('Source:', ISCTEXTDOMAIN);
1097 return $default;
1098 }
1099
1100 /**
1101 * Settings API initialization
1102 */
1103 public function SAPI_init()
1104 {
1105 $this->upgrade_management();
1106 register_setting('isc_options_group', 'isc_options', array($this, 'settings_validation'));
1107 add_settings_section('isc_settings_section', '', '__return_false', 'isc_settings_page');
1108
1109 // Starts Page/Post settings group
1110 add_settings_field('image_list_headline', __('Image list headline', ISCTEXTDOMAIN), array($this, 'renderfield_list_headline'), 'isc_settings_page', 'isc_settings_section');
1111 /**
1112 * All new setting in Page/Post group Here!
1113 */
1114 add_settings_field('hide_list', __('Hide the image list', ISCTEXTDOMAIN), array($this, 'renderfield_hide_list'), 'isc_settings_page', 'isc_settings_section');
1115 // Ends Page/Post settings group
1116
1117 // Starts Full images list group
1118 add_settings_field('use_thumbnail', __("Use thumbnails in images list", ISCTEXTDOMAIN), array($this, 'renderfield_use_thumbnail'), 'isc_settings_page', 'isc_settings_section');
1119 /**
1120 * All new setting in Full images list group Here!
1121 */
1122 add_settings_field('thumbnail_width', __("Thumbnails max-width", ISCTEXTDOMAIN), array($this, 'renderfield_thumbnail_width'), 'isc_settings_page', 'isc_settings_section');
1123 add_settings_field('thumbnail_height', __("Thumbnails max-height", ISCTEXTDOMAIN), array($this, 'renderfield_thumbnail_height'), 'isc_settings_page', 'isc_settings_section');
1124 // Ends Full images list group
1125
1126 // Starts Misc settings group
1127 add_settings_field('use_authorname', __('Use authors names', ISCTEXTDOMAIN), array($this, 'renderfield_use_authorname'), 'isc_settings_page', 'isc_settings_section');
1128 add_settings_field('by_author_text', __('Custom text for owned images', ISCTEXTDOMAIN), array($this, 'renderfield_byauthor_text'), 'isc_settings_page', 'isc_settings_section');
1129 add_settings_field('webgilde_backlink', __("Link to webgilde's website", ISCTEXTDOMAIN), array($this, 'renderfield_webgile'), 'isc_settings_page', 'isc_settings_section');
1130 /**
1131 * All new setting in Misc settings group Here!
1132 */
1133 add_settings_field('source_caption', __("Source as caption on image", ISCTEXTDOMAIN), array($this, 'renderfield_source_caption'), 'isc_settings_page', 'isc_settings_section');
1134 add_settings_field('caption_position', __("Caption position", ISCTEXTDOMAIN), array($this, 'renderfield_caption_pos'), 'isc_settings_page', 'isc_settings_section');
1135 add_settings_field('warning_one_source', __("Warning when there is at least one missing source", ISCTEXTDOMAIN), array($this, 'renderfield_warning_onesource_misisng'), 'isc_settings_page', 'isc_settings_section');
1136 add_settings_field('warning_nosource', __("Warnings when source not available", ISCTEXTDOMAIN), array($this, 'renderfield_warning_nosource'), 'isc_settings_page', 'isc_settings_section');
1137 // Ends Misc settings group
1138 }
1139
1140 /**
1141 * manage data structure upgrading of outdated versions
1142 */
1143 public function upgrade_management() {
1144
1145 /*
1146 * Since the activation hook is not executed on plugin upgrade, this function checks options in database
1147 * during the admin_init hook to handle plugin's upgrade.
1148 */
1149
1150 $options = get_option('isc_options');
1151
1152 if (!is_array($options)) {
1153 // special case for version prior to 1.2 (which don't have options)
1154 $options = $this->default_options();
1155 $this->init_image_posts_metafield();
1156 $options['installed'] = true;
1157 update_option('isc_options', $options);
1158 }
1159
1160 if (ISCVERSION != $options['version']) {
1161 $options = $options + $this->default_options();
1162 $options['version'] = ISCVERSION;
1163 update_option('isc_options', $options);
1164 }
1165 }
1166
1167 /**
1168 * Image_control's page callback
1169 */
1170 public function render_isc_settings_page()
1171 {
1172 ?>
1173 <div id="icon-options-general" class="icon32"><br></div>
1174 <h2><?php _e('Images control settings', ISCTEXTDOMAIN); ?></h2>
1175 <div id="isc-admin-wrap">
1176 <form id="image-control-form" method="post" action="options.php">
1177 <div class="postbox isc-setting-group"><?php // Open the div for the first settings group ?>
1178 <h3 class="setting-group-head"><?php _e('Post / Page images list', ISCTEXTDOMAIN); ?></h3>
1179 <?php
1180 settings_fields( 'isc_options_group' );
1181 do_settings_sections( 'isc_settings_page' );
1182 ?>
1183 </div><?php //Close the last settings group div ?>
1184 <p class="submit">
1185 <input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes">
1186 </p>
1187 </form>
1188 </div><!-- #isc-admin-wrap -->
1189 <?php
1190 }
1191
1192 /**
1193 * image_list field callbacks
1194 */
1195 public function renderfield_list_headline()
1196 {
1197 $options = $this->get_isc_options();
1198 $description = __('The headline of the image list added via shortcode or function in your theme.', ISCTEXTDOMAIN);
1199 ?>
1200 <div id="image-list-headline-block">
1201 <label for="list-head"><?php __('Image list headline', ISCTEXTDOMAIN); ?></label>
1202 <input type="text" name="isc_options[image_list_headline_field]" id="list-head" value="<?php echo $options['image_list_headline'] ?>" class="regular-text" />
1203 <p><em><?php echo $description; ?></em></p>
1204 </div>
1205 <?php
1206 }
1207
1208 public function renderfield_hide_list()
1209 {
1210 $options = $this->get_isc_options();
1211 $description = __("Hide the list when the post is loaded. A simple click on the list headline will show the list content.", ISCTEXTDOMAIN);
1212 ?>
1213 <div id="hide-list-block">
1214 <label for="hide-list"><?php _e('Hide the image list of a post', ISCTEXTDOMAIN) ?></label>
1215 <input type="checkbox" name="isc_options[hide_list]" id="hide-list" <?php checked($options['hide_list']); ?> />
1216 <p><em><?php echo $description; ?></em></p>
1217 </div>
1218 </td></tr></tbody></table>
1219 </div><!-- .postbox -->
1220 <div class="postbox isc-setting-group">
1221 <h3 class="setting-group-head"><?php _e('Full images list', ISCTEXTDOMAIN) ?></h3>
1222 <table class="form-table"><tbody><tr><td>
1223 <?php
1224 }
1225
1226 public function renderfield_use_authorname()
1227 {
1228 $options = $this->get_isc_options();
1229 $description = __("Display the author's public name as source when the image is owned by the author (the uploader of the image, not necessarily the author of the post the image is displayed on). Uncheck to use a custom text instead.", ISCTEXTDOMAIN);
1230
1231 ?>
1232 <div id="use-authorname-block">
1233 <label for="use_authorname"><?php _e('Use author name', ISCTEXTDOMAIN) ?></label>
1234 <input type="checkbox" name="isc_options[use_authorname_ckbox]" id="use_authorname" <?php checked($options['use_authorname']); ?> />
1235 <p><em><?php echo $description; ?></em></p>
1236 </div>
1237 <?php
1238 }
1239
1240 public function renderfield_byauthor_text()
1241 {
1242 $options = $this->get_isc_options();
1243 $description = __("Enter the custom text to display if you do not want to use the author's public name.", ISCTEXTDOMAIN);
1244 ?>
1245 <div id="by-author-text">
1246 <input type="text" id="byauthor" name="isc_options[by_author_text_field]" value="<?php echo $options['by_author_text']; ?>" <?php disabled($options['use_authorname']); ?> class="regular-text" />
1247 <p><em><?php echo $description; ?></em></p>
1248 </div>
1249 <?php
1250 }
1251
1252 public function renderfield_webgile()
1253 {
1254 $options = $this->get_isc_options();
1255 $description = sprintf(__('Display a link to <a href="%s">Image Source Control plugin&#39;s website</a> below the list of all images in the blog?', ISCTEXTDOMAIN), WEBGILDE);
1256 ?>
1257 <div id="webgilde-block">
1258 <input type="checkbox" id="webgilde-link" name="isc_options[webgilde_field]" <?php checked($options['webgilde']); ?> />
1259 <p><em><?php echo $description; ?></em></p>
1260 </div>
1261 <?php
1262 }
1263
1264 public function renderfield_use_thumbnail()
1265 {
1266 $options = $this->get_isc_options();
1267 $description = __('Display thumbnails on the list of all images in the blog.' ,ISCTEXTDOMAIN);
1268 ?>
1269 <div id="use-thumbnail-block">
1270 <input type="checkbox" id="use-thumbnail" name="isc_options[use_thumbnail]" value="1" <?php checked($options['thumbnail_in_list']); ?> />
1271 <select id="thumbnail-size-select" name="isc_options[size_select]" <?php disabled(!$options['thumbnail_in_list']) ?>>
1272 <?php foreach ($this->_thumbnail_size as $size) : ?>
1273 <option value="<?php echo $size; ?>" <?php selected($size, $options['thumbnail_size']);?>><?php echo $size; ?></option>
1274 <?php endforeach; ?>
1275 </select>
1276 <p><em><?php echo $description; ?></em></p>
1277 </div>
1278 <?php
1279 }
1280
1281 public function renderfield_thumbnail_width()
1282 {
1283 $options = $this->get_isc_options();
1284 $description = __('Custom value of the maximum allowed width for thumbnail.' ,ISCTEXTDOMAIN);
1285 ?>
1286 <div id="thumbnail-custom-width">
1287 <input type="text" id="custom-width" name="isc_options[thumbnail_width]" class="small-text" value="<?php echo $options['thumbnail_width'] ?>" /> px
1288 <p><em><?php echo $description; ?></em></p>
1289 </div>
1290 <?php
1291 }
1292
1293 public function renderfield_thumbnail_height()
1294 {
1295 $options = $this->get_isc_options();
1296 $description = __('Custom value of the maximum allowed height for thumbnail.' ,ISCTEXTDOMAIN);
1297 ?>
1298 <div id="thumbnail-custom-height">
1299 <input type="text" id="custom-height" name="isc_options[thumbnail_height]" class="small-text" value="<?php echo $options['thumbnail_height'] ?>"/> px
1300 <p><em><?php echo $description; ?></em></p>
1301 </div>
1302 </td></tr></tbody></table>
1303 </div><!-- .postbox -->
1304 <div class="postbox isc-setting-group">
1305 <h3 class="setting-group-head"><?php _e('Miscellaneous settings', ISCTEXTDOMAIN); ?></h3>
1306 <table class="form-table"><tbody><tr><td>
1307 <?php
1308 }
1309
1310 public function renderfield_warning_nosource()
1311 {
1312 $options = $this->get_isc_options();
1313 $description = __('Warn and prevent data to be saved when an attachment is edited and the source has not been specified.' ,ISCTEXTDOMAIN);
1314 ?>
1315 <div id="no-source-block">
1316 <input type="checkbox" id="no-source" name="isc_options[no_source]"value="1" <?php checked($options['warning_nosource']); ?>/>
1317 <p><em><?php echo $description; ?></em></p>
1318 </div>
1319 <?php
1320 }
1321
1322 public function renderfield_warning_onesource_misisng()
1323 {
1324 $options = $this->get_isc_options();
1325 $description = __('Display an admin notice in admin pages when one or more image sources are missing.' ,ISCTEXTDOMAIN);
1326 ?>
1327 <div id="one-source-block">
1328 <input type="checkbox" id="one-source" name="isc_options[one_source]"value="1" <?php checked($options['warning_onesource_missing']); ?>/>
1329 <p><em><?php echo $description; ?></em></p>
1330 </div>
1331 <?php
1332 }
1333
1334 public function renderfield_caption_pos()
1335 {
1336 $options = $this->get_isc_options();
1337 $description = __('Position of captions into images' ,ISCTEXTDOMAIN);
1338 ?>
1339 <div id="caption-position-block">
1340 <select id="caption-pos" name="isc_options[cap_pos]">
1341 <?php foreach ($this->_caption_position as $pos) : ?>
1342 <option value="<?php echo $pos; ?>" <?php selected($pos, $options['caption_position']); ?>><?php echo $pos; ?></option>
1343 <?php endforeach; ?>
1344 </select>
1345 <p><em><?php echo $description; ?></em></p>
1346 </div>
1347 <?php
1348 }
1349
1350 public function renderfield_source_caption()
1351 {
1352 $options = $this->get_isc_options();
1353 $description_checkbox = __('Tick to display source onto each image.' ,ISCTEXTDOMAIN);
1354 $description_textfield = __('The text preceding the source on each image.' ,ISCTEXTDOMAIN);
1355 ?>
1356 <div id="caption-block">
1357 <input type="checkbox" id="source-on-image" value="1" name="isc_options[source_on_image]" <?php checked($options['source_on_image']); ?> />
1358 <p><em><?php echo $description_checkbox; ?></em></p>
1359 <input type="text" id='source-pretext' name="isc_options[source_pretext]" value="<?php echo $options['source_pretext']; ?>" />
1360 <p><em><?php echo $description_textfield; ?></em></p>
1361 </div>
1362 <?php
1363 }
1364
1365 /**
1366 * Returns isc_options if it exists, returns the default options otherwise.
1367 */
1368 public function get_isc_options() {
1369 return get_option('isc_options', $this->default_options());
1370 }
1371
1372 /*
1373 * Input validation function.
1374 * @param array $input values from the admin panel
1375 */
1376 public function settings_validation($input)
1377 {
1378 $output = $this->get_isc_options();
1379 $output['image_list_headline'] = esc_html($input['image_list_headline_field']);
1380 if (isset($input['use_authorname_ckbox'])) {
1381 // Don't worry about the custom text if the author name is selected.
1382 $output['use_authorname'] = true;
1383 } else {
1384 $output['use_authorname'] = false;
1385 $output['by_author_text'] = esc_html($input['by_author_text_field']);
1386 }
1387 if (isset($input['webgilde_field'])) {
1388 $output['webgilde'] = true;
1389 } else {
1390 $output['webgilde'] = false;
1391 }
1392 if (isset($input['use_thumbnail'])) {
1393 $output['thumbnail_in_list'] = true;
1394 if (in_array($input['size_select'], $this->_thumbnail_size)) {
1395 $output['thumbnail_size'] = $input['size_select'];
1396 }
1397 if ('custom' == $input['size_select']) {
1398 if (is_numeric($input['thumbnail_width'])) {
1399 // Ensures that the value stored in database in a positive integer.
1400 $output['thumbnail_width'] = abs(intval(round($input['thumbnail_width'])));
1401 }
1402 if (is_numeric($input['thumbnail_height'])) {
1403 $output['thumbnail_height'] = abs(intval(round($input['thumbnail_height'])));
1404 }
1405 }
1406 } else {
1407 $output['thumbnail_in_list'] = false;
1408 }
1409 if (isset($input['no_source'])) {
1410 $output['warning_nosource'] = true;
1411 } else {
1412 $output['warning_nosource'] = false;
1413 }
1414 if (isset($input['one_source'])) {
1415 $output['warning_onesource_missing'] = true;
1416 } else {
1417 $output['warning_onesource_missing'] = false;
1418 }
1419 if (isset($input['hide_list'])){
1420 $output['hide_list'] = true;
1421 } else {
1422 $output['hide_list'] = false;
1423 }
1424 if (in_array($input['cap_pos'], $this->_caption_position))
1425 $output['caption_position'] = $input['cap_pos'];
1426 if (isset($input['source_on_image'])) {
1427 $output['source_on_image'] = true;
1428 $output['source_pretext'] = $input['source_pretext'];
1429 } else {
1430 $output['source_on_image'] = false;
1431 }
1432 return $output;
1433 }
1434
1435 /**
1436 * Adds isc_image_posts on all attachments. Launched during first installation.
1437 */
1438 public function init_image_posts_metafield()
1439 {
1440 $args = array(
1441 'post_type' => 'any',
1442 'numberposts' => -1,
1443 'post_status' => null,
1444 'post_parent' => null,
1445 );
1446 $posts = get_posts($args);
1447 foreach ($posts as $post) {
1448 setup_postdata($post);
1449 $image_urls = $this->_filter_src_attributes($post->post_content);
1450 $image_ids = array();
1451 foreach ($image_urls as $url) {
1452 $image_id = intval($this->get_image_by_url($url));
1453 array_push($image_ids,$image_id);
1454 }
1455 foreach ($image_ids as $id) {
1456 $meta = get_post_meta($id, 'isc_image_posts', true);
1457 if (empty($meta)) {
1458 update_post_meta($id, 'isc_image_posts', array($post->ID));
1459 } else {
1460 if (!in_array($post->ID, $meta)) {
1461 array_push($meta, $post->ID);
1462 update_post_meta($id, 'isc_image_posts', $meta);
1463 }
1464 }
1465 }
1466 }
1467 }
1468
1469 /**
1470 *
1471 */
1472 public function admin_notices()
1473 {
1474 $args = array(
1475 'post_type' => 'attachment',
1476 'numberposts' => -1,
1477 'post_status' => null,
1478 'post_parent' => null,
1479 'meta_query' => array(
1480 array(
1481 'key' => 'isc_image_source',
1482 'value' => '',
1483 'compare' => '='
1484 ),
1485 array(
1486 'key' => 'isc_image_source_own',
1487 'value' => '',
1488 'compare' => ''
1489 )
1490 )
1491 );
1492 $attachments = get_posts($args);
1493 $options = $this->get_isc_options();
1494 if (!empty($attachments) && $options['warning_onesource_missing'] ) {
1495 $missing_src = esc_url(admin_url('upload.php?page=image-source-control-isc/templates/missing_sources.php'));
1496 ?>
1497 <div class="updated"><p><?php printf(__('One or more attachments still have no source. See the <a href="%s">missing sources</a> list', ISCTEXTDOMAIN), $missing_src);?></p></div>
1498 <?php
1499 }
1500 }
1501
1502 }// end of class
1503
1504 $inc_path = ABSPATH . 'wp-includes/';
1505 /**
1506 * "pluggable.php" is not defined at this point. Not sure about the reason.
1507 */
1508 require_once($inc_path . 'pluggable.php');
1509
1510 /**
1511 * Need an instance of ISC_CLASS when the register_activation_hook is called (earlier than the "plugins_loaded" hook).
1512 */
1513 global $my_isc;
1514 $my_isc = new ISC_CLASS();
1515
1516 /**
1517 * the next functions are just to have an easier access from outside the class
1518 */
1519 function isc_list($post_id = 0) {
1520 $isc = new ISC_CLASS();
1521 echo $isc->list_post_attachments_with_sources($post_id);
1522 }
1523 }
1524