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

1,528 lines 64.7 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.2
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.2');
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 // add thumbnail information
684 $thumb_id = get_post_thumbnail_id($post_id);
685 if ( !empty( $thumb_id )) { $image_urls[] = wp_get_attachment_url($thumb_id); }
686
687 $isc_post_images = get_post_meta($post_id, 'isc_post_images', true);
688
689 foreach ($image_urls as $url) {
690 $id = intval($this->get_image_by_url($url));
691 array_push($image_ids, $id);
692 if (is_array($isc_post_images) && !array_key_exists($id, $isc_post_images)) {
693 array_push($added_images, $id);
694 }
695 }
696 if (is_array($isc_post_images)) {
697 foreach ($isc_post_images as $old_id => $value) {
698 if (!in_array($old_id, $image_ids)) {
699 array_push($removed_images, $old_id);
700 } else {
701 if (!empty($old_id)) {
702 $meta = get_post_meta($old_id, 'isc_image_posts', true);
703 if (empty($meta)) {
704 update_post_meta($old_id, 'isc_image_posts', array($post_id));
705 } else {
706 // In case the isc_image_posts is not up to date
707 if (is_array($meta) && !in_array($post_id, $meta)) {
708 array_push($meta, $post_id);
709 update_post_meta($old_id, 'isc_image_posts', $meta);
710 }
711 }
712 }
713 }
714 }
715 }
716
717 foreach ($added_images as $id) {
718 $meta = get_post_meta($id, 'isc_image_posts', true);
719 if (!is_array($meta) || array() == $meta) {
720 update_post_meta($id, 'isc_image_posts', array($post_id));
721 } else {
722 array_push($meta, $post_id);
723 update_post_meta($id, 'isc_image_posts', $meta);
724 }
725 }
726
727 foreach ($removed_images as $id) {
728 $image_meta = get_post_meta($id, 'isc_image_posts', true);
729 if (is_array($image_meta)) {
730 $offset = array_search($post_id, $image_meta);
731 if (false !== $offset) {
732 array_splice($image_meta, $offset, 1);
733 update_post_meta($id, 'isc_image_posts', $image_meta);
734 }
735 }
736 }
737 }
738
739 /**
740 * create shortcode to list all image sources in the frontend
741 * @param array $atts
742 * @since 1.1.3
743 * @todo link to the post
744 */
745 public function list_all_post_attachments_sources_shortcode($atts = array())
746 {
747
748 /**
749 * @todo why not translate here with the code below?
750 * > Because the two if statements below will need to call gettext (again) for comparing values.
751 */
752 extract(shortcode_atts(array(
753 'per_page' => 99999,
754 'before_links' => '',
755 'after_links' => '',
756 'prev_text' => '&#171; Previous',
757 'next_text' => 'Next &#187;'
758 ),
759 $atts));
760
761 /**
762 * @todo why not include this into the array above?
763 */
764 if ('&#171; Previous' == $prev_text)
765 $prev_text = __('&#171; Previous', ISCTEXTDOMAIN);
766 if ('Next &#187;' == $next_text)
767 $next_text = __('Next &#187;', ISCTEXTDOMAIN);
768
769 // retrieve all attachments
770 $args = array(
771 'post_type' => 'attachment',
772 'numberposts' => -1,
773 'post_status' => null,
774 'post_parent' => null,
775 'meta_query' => array(
776 array(
777 'key' => 'isc_image_posts',
778 'value' => 'a:0:{}',
779 'compare' => '!='
780 )
781 )
782 /** @todo maybe add offset to not retrieve the first results when not on first page */
783 /** @todo maybe add limit to not retrieve more results than on the current page */
784 /** >No, we need to get total count of attachment with parents for $max_page in the pagination link. */
785 );
786
787 $attachments = get_posts($args);
788 if (empty($attachments)) {
789 return;
790 }
791
792 $options = $this->get_isc_options();
793
794 $connected_atts = array();
795
796 //Keeps only those ones who have parent
797
798 foreach ($attachments as $_attachment) {
799 $connected_atts[$_attachment->ID]['source'] = get_post_meta($_attachment->ID, 'isc_image_source', true);
800 $connected_atts[$_attachment->ID]['own'] = get_post_meta($_attachment->ID, 'isc_image_source_own', true);
801 $connected_atts[$_attachment->ID]['title'] = $_attachment->post_title;
802 $connected_atts[$_attachment->ID]['author_name'] = '';
803 if ('' != $connected_atts[$_attachment->ID]['own']) {
804 $connected_atts[$_attachment->ID]['author_name'] = get_the_author_meta('display_name', $_attachment->post_author);
805 }
806
807 $metadata = get_post_meta($_attachment->ID, 'isc_image_posts', true);
808 $usage_data = '';
809
810 if (is_array($metadata) && array() != $metadata) {
811 $usage_data .= "<ul style='margin: 0;'>";
812 foreach($metadata as $data) {
813 $usage_data .= sprintf(__('<li><a href="%1$s" title="View %2$s">%3$s</a></li>', ISCTEXTDOMAIN),
814 esc_url(get_permalink($data)),
815 esc_attr(get_the_title($data)),
816 esc_html(get_the_title($data))
817 );
818 }
819 $usage_data .= "</ul>";
820 }
821
822 $connected_atts[$_attachment->ID]['posts'] = $usage_data;
823 }
824
825 $total = count($connected_atts);
826
827 if (0 == $total)
828 return;
829
830 $page = isset($_GET['isc-page']) ? intval($_GET['isc-page']) : 1;
831 $down_limit = 1; // First page
832
833 $up_limit = 1;
834
835 if ($per_page < $total) {
836 $rem = $total % $per_page; // The Remainder of $total/$per_page
837 $up_limit = ($total - $rem) / $per_page;
838 if (0 < $rem) {
839 $up_limit++; //If rem is positive, add the last page that contains less than $per_page attachment;
840 }
841 }
842
843 ob_start();
844 if ( 2 > $up_limit ) {
845 $this->display_all_attachment_list($connected_atts);
846 } else {
847 $starting_atts = $per_page * ($page - 1); // for page 2 and 3 $per_page start display on $connected_atts[3*(2-1) = 3]
848 $paged_atts = array_slice($connected_atts, $starting_atts, $per_page, true);
849 $this->display_all_attachment_list($paged_atts);
850 $this->pagination_links($up_limit, $before_links, $after_links, $prev_text, $next_text);
851 }
852 if (isset($options['webgilde']) && true == $options['webgilde']) {
853 ?>
854 <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>
855 <?php
856 }
857
858 $output = ob_get_clean();
859 return $output;
860 }
861
862
863 /**
864 * performs rendering of all attachments list
865 * @since 1.1.3
866 */
867 public function display_all_attachment_list($atts)
868 {
869 if (!is_array($atts) || $atts == array())
870 return;
871 $options = $this->get_isc_options();
872 ?>
873 <table>
874 <thead>
875 <?php if ($options['thumbnail_in_list']) : ?>
876 <th><?php _e('Thumbnail', ISCTEXTDOMAIN); ?></th>
877 <?php endif; ?>
878 <th><?php _e("Attachment's ID", ISCTEXTDOMAIN); ?></th>
879 <th><?php _e('Title', ISCTEXTDOMAIN); ?></th>
880 <th><?php _e('Attached to', ISCTEXTDOMAIN); ?></th>
881 <th><?php _e('Source', ISCTEXTDOMAIN); ?></th>
882 </thead>
883 <tbody>
884 <?php foreach ($atts as $id => $data) : ?>
885 <?php
886 $source = $this->_common_texts['not_available'];
887 if ('' != $data['own']) {
888 /** @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 */
889 if ($this->_options['use_authorname']) {
890 $source = $data['author_name'];
891 } else {
892 $source = $this->_options['by_author_text'];
893 }
894 } else {
895 if (!empty($data['source']))
896 $source = $data['source'];
897 }
898 ?>
899 <tr>
900 <?php
901 $v_align = '';
902 if ($options['thumbnail_in_list']) :
903 $v_align = 'style="vertical-align: top;"';
904 ?>
905 <?php if ('custom' != $options['thumbnail_size']) : ?>
906 <td><?php echo wp_get_attachment_image($id, $options['thumbnail_size']); ?></td>
907 <?php else : ?>
908 <td><?php echo wp_get_attachment_image($id, array($options['thumbnail_width'], $options['thumbnail_height'])); ?></td>
909 <?php endif; ?>
910 <?php endif; ?>
911 <td <?php echo $v_align;?>><?php echo $id; ?></td>
912 <td <?php echo $v_align;?>><?php echo $data['title']; ?></td>
913 <td <?php echo $v_align;?>><?php echo $data['posts']; ?></td>
914 <td <?php echo $v_align;?>><?php echo esc_html($source); ?></td>
915 </tr>
916 <?php endforeach; ?>
917 </tbody>
918 </table>
919 <?php
920 }
921
922 /**
923 * Render pagination links, use $before_links and after_links to wrap pagination links inside an additional block
924 * @param int $max_page total page count
925 * @param string $before_links optional html to display before pagination links
926 * @param string $after_links optional html to display after pagination links
927 * @param string $prev_text text for the previous page link
928 * @param string $next_text text for the next page link
929 * @since 1.1.3
930 *
931 */
932 public function pagination_links($max_page, $before_links, $after_links, $prev_text, $next_text)
933 {
934 if ((!isset($max_page)) || (!isset($before_links)) || (!isset($after_links)) || (!isset($prev_text)) || (!isset($next_text)))
935 return;
936 if (!empty($before_links))
937 echo $before_links;
938 ?>
939 <div class="isc-paginated-links">
940 <?php
941 $page = isset($_GET['isc-page']) ? intval($_GET['isc-page']) : 1;
942 if ($max_page < $page) {
943 $page = $max_page;
944 }
945 if ($page < 1) {
946 $page = 1;
947 }
948 $min_page = 1;
949 $backward_distance = $page - $min_page;
950 $forward_distance = $max_page - $page;
951
952 $page_link = get_page_link();
953
954 /**
955 * Remove the query_string of the page_link (?page_id=xyz for the standard permalink structure),
956 * which is already captured in $_SERVER['QUERY_STRING'].
957 * @todo replace regex with other value (does WP store the url path without attributes somewhere?
958 * >get_page_link() returns the permalink but for the default WP permalink structure, the permalink looks like "http://domain.tld/?p=52", while $_GET
959 * still has a field named 'page_id' with the same value of 52.
960 */
961
962 $pos = strpos($page_link, '?');
963 if (false !== $pos) {
964 $page_link = substr($page_link, 0, $pos);
965 }
966
967 /**
968 * 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
969 * pagination link.
970 */
971
972 if (isset($_GET['isc-page'])) {
973 unset($_GET['isc-page']);
974 }
975
976 $query_string = http_build_query($_GET);
977
978 $isc_query_tag = '';
979 if (empty($query_string)) {
980 $isc_query_tag = '?isc-page=';
981 } else {
982 $query_string = '?' . $query_string;
983 $isc_query_tag = '&isc-page=';
984 }
985
986 if ($min_page != $page) {
987 ?>
988 <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page-1); ?>" class="prev page-numbers"><?php echo $prev_text; ?></a>
989 <?php
990 }
991
992 if (5 < $max_page) {
993
994 if (3 < $backward_distance) {
995 ?>
996 <a href="<?php echo $page_link . $query_string . $isc_query_tag; ?>1" class="page-numbers">1</a>
997 <span class="page-numbers dots">...</span>
998 <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page-2);?>" class="page-numbers"><?php echo $page-2; ?></a>
999 <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page-1);?>" class="page-numbers"><?php echo $page-1; ?></a>
1000 <span class="page-numbers current"><?php echo $page; ?></span>
1001 <?php
1002 } else {
1003 for ($i = 1; $i <= $page; $i++) {
1004 if ($i == $page) {
1005 ?>
1006 <span class="page-numbers current"><?php echo $i; ?></span>
1007 <?php
1008 } else {
1009 ?>
1010 <a href="<?php echo $page_link . $query_string . $isc_query_tag . $i;?>" class="page-numbers"><?php echo $i; ?></a>
1011 <?php
1012 }
1013 }
1014 }
1015
1016 if (3 < $forward_distance) {
1017 ?>
1018 <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page+1);?>" class="page-numbers"><?php echo $page+1; ?></a>
1019 <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page+2);?>" class="page-numbers"><?php echo $page+2; ?></a>
1020 <span class="page-numbers dots">...</span>
1021 <a href="<?php echo $page_link . $query_string . $isc_query_tag . $max_page;?>" class="page-numbers"><?php echo $max_page; ?></a>
1022 <?php
1023 } else {
1024 for ($i = $page+1; $i <= $max_page; $i++) {
1025 ?>
1026 <a href="<?php echo $page_link . $query_string . $isc_query_tag . $i;?>" class="page-numbers"><?php echo $i; ?></a>
1027 <?php
1028 }
1029 }
1030 } else {
1031 for ($i = 1; $i <= $max_page; $i++) {
1032 if ($i == $page) {
1033 ?>
1034 <span class="page-numbers current"><?php echo $i; ?></span>
1035 <?php
1036 } else {
1037 ?>
1038 <a href="<?php echo $page_link . $query_string . $isc_query_tag . $i;?>" class="page-numbers"><?php echo $i; ?></a>
1039 <?php
1040 }
1041 }
1042 }
1043 if ($page != $max_page) {
1044 ?>
1045 <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page+1);?>" class="next page-numbers"><?php echo $next_text; ?></a>
1046 <?php
1047 }
1048 ?>
1049 </div>
1050 <?php
1051 echo $after_links;
1052 }
1053
1054 /**
1055 * The activation function
1056 */
1057 public function activation()
1058 {
1059 if (!is_array(get_option('isc_options'))) {
1060 update_option( 'isc_options', $this->default_options() );
1061 }
1062 $options = $this->get_isc_options();
1063 if (!$options['installed']) {
1064 /**
1065 * Here, all jobs to perform during first activation, especially options and custom fields.
1066 * Important: NO add_action('something', 'somefunction') here.
1067 */
1068
1069 // adds meta fields for attachments
1070 $this->add_meta_values_to_attachments();
1071
1072 // set all isc_image_posts meta fields.
1073 $this->init_image_posts_metafield();
1074
1075 $options['installed'] = true;
1076 update_option('isc_options', $options);
1077 }
1078 }
1079
1080 /**
1081 * Returns default options
1082 */
1083 public function default_options()
1084 {
1085 $default['image_list_headline'] = __('image sources', ISCTEXTDOMAIN);
1086 $default['use_authorname'] = true;
1087 $default['by_author_text'] = __('Owned by the author', ISCTEXTDOMAIN);
1088 $default['installed'] = false;
1089 $default['version'] = ISCVERSION;
1090 $default['webgilde'] = false;
1091 $default['thumbnail_in_list'] = false;
1092 $default['thumbnail_size'] = 'thumbnail';
1093 $default['thumbnail_width'] = 150;
1094 $default['thumbnail_height'] = 150;
1095 $default['warning_nosource'] = true;
1096 $default['warning_onesource_missing'] = true;
1097 $default['hide_list'] = false;
1098 $default['caption_position'] = 'top-left';
1099 $default['source_on_image'] = false;
1100 $default['source_pretext'] = __('Source:', ISCTEXTDOMAIN);
1101 return $default;
1102 }
1103
1104 /**
1105 * Settings API initialization
1106 */
1107 public function SAPI_init()
1108 {
1109 $this->upgrade_management();
1110 register_setting('isc_options_group', 'isc_options', array($this, 'settings_validation'));
1111 add_settings_section('isc_settings_section', '', '__return_false', 'isc_settings_page');
1112
1113 // Starts Page/Post settings group
1114 add_settings_field('image_list_headline', __('Image list headline', ISCTEXTDOMAIN), array($this, 'renderfield_list_headline'), 'isc_settings_page', 'isc_settings_section');
1115 /**
1116 * All new setting in Page/Post group Here!
1117 */
1118 add_settings_field('hide_list', __('Hide the image list', ISCTEXTDOMAIN), array($this, 'renderfield_hide_list'), 'isc_settings_page', 'isc_settings_section');
1119 // Ends Page/Post settings group
1120
1121 // Starts Full images list group
1122 add_settings_field('use_thumbnail', __("Use thumbnails in images list", ISCTEXTDOMAIN), array($this, 'renderfield_use_thumbnail'), 'isc_settings_page', 'isc_settings_section');
1123 /**
1124 * All new setting in Full images list group Here!
1125 */
1126 add_settings_field('thumbnail_width', __("Thumbnails max-width", ISCTEXTDOMAIN), array($this, 'renderfield_thumbnail_width'), 'isc_settings_page', 'isc_settings_section');
1127 add_settings_field('thumbnail_height', __("Thumbnails max-height", ISCTEXTDOMAIN), array($this, 'renderfield_thumbnail_height'), 'isc_settings_page', 'isc_settings_section');
1128 // Ends Full images list group
1129
1130 // Starts Misc settings group
1131 add_settings_field('use_authorname', __('Use authors names', ISCTEXTDOMAIN), array($this, 'renderfield_use_authorname'), 'isc_settings_page', 'isc_settings_section');
1132 add_settings_field('by_author_text', __('Custom text for owned images', ISCTEXTDOMAIN), array($this, 'renderfield_byauthor_text'), 'isc_settings_page', 'isc_settings_section');
1133 add_settings_field('webgilde_backlink', __("Link to webgilde's website", ISCTEXTDOMAIN), array($this, 'renderfield_webgile'), 'isc_settings_page', 'isc_settings_section');
1134 /**
1135 * All new setting in Misc settings group Here!
1136 */
1137 add_settings_field('source_caption', __("Source as caption on image", ISCTEXTDOMAIN), array($this, 'renderfield_source_caption'), 'isc_settings_page', 'isc_settings_section');
1138 add_settings_field('caption_position', __("Caption position", ISCTEXTDOMAIN), array($this, 'renderfield_caption_pos'), 'isc_settings_page', 'isc_settings_section');
1139 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');
1140 add_settings_field('warning_nosource', __("Warnings when source not available", ISCTEXTDOMAIN), array($this, 'renderfield_warning_nosource'), 'isc_settings_page', 'isc_settings_section');
1141 // Ends Misc settings group
1142 }
1143
1144 /**
1145 * manage data structure upgrading of outdated versions
1146 */
1147 public function upgrade_management() {
1148
1149 /*
1150 * Since the activation hook is not executed on plugin upgrade, this function checks options in database
1151 * during the admin_init hook to handle plugin's upgrade.
1152 */
1153
1154 $options = get_option('isc_options');
1155
1156 if (!is_array($options)) {
1157 // special case for version prior to 1.2 (which don't have options)
1158 $options = $this->default_options();
1159 $this->init_image_posts_metafield();
1160 $options['installed'] = true;
1161 update_option('isc_options', $options);
1162 }
1163
1164 if (ISCVERSION != $options['version']) {
1165 $options = $options + $this->default_options();
1166 $options['version'] = ISCVERSION;
1167 update_option('isc_options', $options);
1168 }
1169 }
1170
1171 /**
1172 * Image_control's page callback
1173 */
1174 public function render_isc_settings_page()
1175 {
1176 ?>
1177 <div id="icon-options-general" class="icon32"><br></div>
1178 <h2><?php _e('Images control settings', ISCTEXTDOMAIN); ?></h2>
1179 <div id="isc-admin-wrap">
1180 <form id="image-control-form" method="post" action="options.php">
1181 <div class="postbox isc-setting-group"><?php // Open the div for the first settings group ?>
1182 <h3 class="setting-group-head"><?php _e('Post / Page images list', ISCTEXTDOMAIN); ?></h3>
1183 <?php
1184 settings_fields( 'isc_options_group' );
1185 do_settings_sections( 'isc_settings_page' );
1186 ?>
1187 </div><?php //Close the last settings group div ?>
1188 <p class="submit">
1189 <input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes">
1190 </p>
1191 </form>
1192 </div><!-- #isc-admin-wrap -->
1193 <?php
1194 }
1195
1196 /**
1197 * image_list field callbacks
1198 */
1199 public function renderfield_list_headline()
1200 {
1201 $options = $this->get_isc_options();
1202 $description = __('The headline of the image list added via shortcode or function in your theme.', ISCTEXTDOMAIN);
1203 ?>
1204 <div id="image-list-headline-block">
1205 <label for="list-head"><?php __('Image list headline', ISCTEXTDOMAIN); ?></label>
1206 <input type="text" name="isc_options[image_list_headline_field]" id="list-head" value="<?php echo $options['image_list_headline'] ?>" class="regular-text" />
1207 <p><em><?php echo $description; ?></em></p>
1208 </div>
1209 <?php
1210 }
1211
1212 public function renderfield_hide_list()
1213 {
1214 $options = $this->get_isc_options();
1215 $description = __("Hide the list when the post is loaded. A simple click on the list headline will show the list content.", ISCTEXTDOMAIN);
1216 ?>
1217 <div id="hide-list-block">
1218 <label for="hide-list"><?php _e('Hide the image list of a post', ISCTEXTDOMAIN) ?></label>
1219 <input type="checkbox" name="isc_options[hide_list]" id="hide-list" <?php checked($options['hide_list']); ?> />
1220 <p><em><?php echo $description; ?></em></p>
1221 </div>
1222 </td></tr></tbody></table>
1223 </div><!-- .postbox -->
1224 <div class="postbox isc-setting-group">
1225 <h3 class="setting-group-head"><?php _e('Full images list', ISCTEXTDOMAIN) ?></h3>
1226 <table class="form-table"><tbody><tr><td>
1227 <?php
1228 }
1229
1230 public function renderfield_use_authorname()
1231 {
1232 $options = $this->get_isc_options();
1233 $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);
1234
1235 ?>
1236 <div id="use-authorname-block">
1237 <label for="use_authorname"><?php _e('Use author name', ISCTEXTDOMAIN) ?></label>
1238 <input type="checkbox" name="isc_options[use_authorname_ckbox]" id="use_authorname" <?php checked($options['use_authorname']); ?> />
1239 <p><em><?php echo $description; ?></em></p>
1240 </div>
1241 <?php
1242 }
1243
1244 public function renderfield_byauthor_text()
1245 {
1246 $options = $this->get_isc_options();
1247 $description = __("Enter the custom text to display if you do not want to use the author's public name.", ISCTEXTDOMAIN);
1248 ?>
1249 <div id="by-author-text">
1250 <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" />
1251 <p><em><?php echo $description; ?></em></p>
1252 </div>
1253 <?php
1254 }
1255
1256 public function renderfield_webgile()
1257 {
1258 $options = $this->get_isc_options();
1259 $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);
1260 ?>
1261 <div id="webgilde-block">
1262 <input type="checkbox" id="webgilde-link" name="isc_options[webgilde_field]" <?php checked($options['webgilde']); ?> />
1263 <p><em><?php echo $description; ?></em></p>
1264 </div>
1265 <?php
1266 }
1267
1268 public function renderfield_use_thumbnail()
1269 {
1270 $options = $this->get_isc_options();
1271 $description = __('Display thumbnails on the list of all images in the blog.' ,ISCTEXTDOMAIN);
1272 ?>
1273 <div id="use-thumbnail-block">
1274 <input type="checkbox" id="use-thumbnail" name="isc_options[use_thumbnail]" value="1" <?php checked($options['thumbnail_in_list']); ?> />
1275 <select id="thumbnail-size-select" name="isc_options[size_select]" <?php disabled(!$options['thumbnail_in_list']) ?>>
1276 <?php foreach ($this->_thumbnail_size as $size) : ?>
1277 <option value="<?php echo $size; ?>" <?php selected($size, $options['thumbnail_size']);?>><?php echo $size; ?></option>
1278 <?php endforeach; ?>
1279 </select>
1280 <p><em><?php echo $description; ?></em></p>
1281 </div>
1282 <?php
1283 }
1284
1285 public function renderfield_thumbnail_width()
1286 {
1287 $options = $this->get_isc_options();
1288 $description = __('Custom value of the maximum allowed width for thumbnail.' ,ISCTEXTDOMAIN);
1289 ?>
1290 <div id="thumbnail-custom-width">
1291 <input type="text" id="custom-width" name="isc_options[thumbnail_width]" class="small-text" value="<?php echo $options['thumbnail_width'] ?>" /> px
1292 <p><em><?php echo $description; ?></em></p>
1293 </div>
1294 <?php
1295 }
1296
1297 public function renderfield_thumbnail_height()
1298 {
1299 $options = $this->get_isc_options();
1300 $description = __('Custom value of the maximum allowed height for thumbnail.' ,ISCTEXTDOMAIN);
1301 ?>
1302 <div id="thumbnail-custom-height">
1303 <input type="text" id="custom-height" name="isc_options[thumbnail_height]" class="small-text" value="<?php echo $options['thumbnail_height'] ?>"/> px
1304 <p><em><?php echo $description; ?></em></p>
1305 </div>
1306 </td></tr></tbody></table>
1307 </div><!-- .postbox -->
1308 <div class="postbox isc-setting-group">
1309 <h3 class="setting-group-head"><?php _e('Miscellaneous settings', ISCTEXTDOMAIN); ?></h3>
1310 <table class="form-table"><tbody><tr><td>
1311 <?php
1312 }
1313
1314 public function renderfield_warning_nosource()
1315 {
1316 $options = $this->get_isc_options();
1317 $description = __('Warn and prevent data to be saved when an attachment is edited and the source has not been specified.' ,ISCTEXTDOMAIN);
1318 ?>
1319 <div id="no-source-block">
1320 <input type="checkbox" id="no-source" name="isc_options[no_source]"value="1" <?php checked($options['warning_nosource']); ?>/>
1321 <p><em><?php echo $description; ?></em></p>
1322 </div>
1323 <?php
1324 }
1325
1326 public function renderfield_warning_onesource_misisng()
1327 {
1328 $options = $this->get_isc_options();
1329 $description = __('Display an admin notice in admin pages when one or more image sources are missing.' ,ISCTEXTDOMAIN);
1330 ?>
1331 <div id="one-source-block">
1332 <input type="checkbox" id="one-source" name="isc_options[one_source]"value="1" <?php checked($options['warning_onesource_missing']); ?>/>
1333 <p><em><?php echo $description; ?></em></p>
1334 </div>
1335 <?php
1336 }
1337
1338 public function renderfield_caption_pos()
1339 {
1340 $options = $this->get_isc_options();
1341 $description = __('Position of captions into images' ,ISCTEXTDOMAIN);
1342 ?>
1343 <div id="caption-position-block">
1344 <select id="caption-pos" name="isc_options[cap_pos]">
1345 <?php foreach ($this->_caption_position as $pos) : ?>
1346 <option value="<?php echo $pos; ?>" <?php selected($pos, $options['caption_position']); ?>><?php echo $pos; ?></option>
1347 <?php endforeach; ?>
1348 </select>
1349 <p><em><?php echo $description; ?></em></p>
1350 </div>
1351 <?php
1352 }
1353
1354 public function renderfield_source_caption()
1355 {
1356 $options = $this->get_isc_options();
1357 $description_checkbox = __('Tick to display source onto each image.' ,ISCTEXTDOMAIN);
1358 $description_textfield = __('The text preceding the source on each image.' ,ISCTEXTDOMAIN);
1359 ?>
1360 <div id="caption-block">
1361 <input type="checkbox" id="source-on-image" value="1" name="isc_options[source_on_image]" <?php checked($options['source_on_image']); ?> />
1362 <p><em><?php echo $description_checkbox; ?></em></p>
1363 <input type="text" id='source-pretext' name="isc_options[source_pretext]" value="<?php echo $options['source_pretext']; ?>" />
1364 <p><em><?php echo $description_textfield; ?></em></p>
1365 </div>
1366 <?php
1367 }
1368
1369 /**
1370 * Returns isc_options if it exists, returns the default options otherwise.
1371 */
1372 public function get_isc_options() {
1373 return get_option('isc_options', $this->default_options());
1374 }
1375
1376 /*
1377 * Input validation function.
1378 * @param array $input values from the admin panel
1379 */
1380 public function settings_validation($input)
1381 {
1382 $output = $this->get_isc_options();
1383 $output['image_list_headline'] = esc_html($input['image_list_headline_field']);
1384 if (isset($input['use_authorname_ckbox'])) {
1385 // Don't worry about the custom text if the author name is selected.
1386 $output['use_authorname'] = true;
1387 } else {
1388 $output['use_authorname'] = false;
1389 $output['by_author_text'] = esc_html($input['by_author_text_field']);
1390 }
1391 if (isset($input['webgilde_field'])) {
1392 $output['webgilde'] = true;
1393 } else {
1394 $output['webgilde'] = false;
1395 }
1396 if (isset($input['use_thumbnail'])) {
1397 $output['thumbnail_in_list'] = true;
1398 if (in_array($input['size_select'], $this->_thumbnail_size)) {
1399 $output['thumbnail_size'] = $input['size_select'];
1400 }
1401 if ('custom' == $input['size_select']) {
1402 if (is_numeric($input['thumbnail_width'])) {
1403 // Ensures that the value stored in database in a positive integer.
1404 $output['thumbnail_width'] = abs(intval(round($input['thumbnail_width'])));
1405 }
1406 if (is_numeric($input['thumbnail_height'])) {
1407 $output['thumbnail_height'] = abs(intval(round($input['thumbnail_height'])));
1408 }
1409 }
1410 } else {
1411 $output['thumbnail_in_list'] = false;
1412 }
1413 if (isset($input['no_source'])) {
1414 $output['warning_nosource'] = true;
1415 } else {
1416 $output['warning_nosource'] = false;
1417 }
1418 if (isset($input['one_source'])) {
1419 $output['warning_onesource_missing'] = true;
1420 } else {
1421 $output['warning_onesource_missing'] = false;
1422 }
1423 if (isset($input['hide_list'])){
1424 $output['hide_list'] = true;
1425 } else {
1426 $output['hide_list'] = false;
1427 }
1428 if (in_array($input['cap_pos'], $this->_caption_position))
1429 $output['caption_position'] = $input['cap_pos'];
1430 if (isset($input['source_on_image'])) {
1431 $output['source_on_image'] = true;
1432 $output['source_pretext'] = $input['source_pretext'];
1433 } else {
1434 $output['source_on_image'] = false;
1435 }
1436 return $output;
1437 }
1438
1439 /**
1440 * Adds isc_image_posts on all attachments. Launched during first installation.
1441 */
1442 public function init_image_posts_metafield()
1443 {
1444 $args = array(
1445 'post_type' => 'any',
1446 'numberposts' => -1,
1447 'post_status' => null,
1448 'post_parent' => null,
1449 );
1450 $posts = get_posts($args);
1451 foreach ($posts as $post) {
1452 setup_postdata($post);
1453 $image_urls = $this->_filter_src_attributes($post->post_content);
1454 $image_ids = array();
1455 foreach ($image_urls as $url) {
1456 $image_id = intval($this->get_image_by_url($url));
1457 array_push($image_ids,$image_id);
1458 }
1459 foreach ($image_ids as $id) {
1460 $meta = get_post_meta($id, 'isc_image_posts', true);
1461 if (empty($meta)) {
1462 update_post_meta($id, 'isc_image_posts', array($post->ID));
1463 } else {
1464 if (!in_array($post->ID, $meta)) {
1465 array_push($meta, $post->ID);
1466 update_post_meta($id, 'isc_image_posts', $meta);
1467 }
1468 }
1469 }
1470 }
1471 }
1472
1473 /**
1474 *
1475 */
1476 public function admin_notices()
1477 {
1478 $args = array(
1479 'post_type' => 'attachment',
1480 'numberposts' => -1,
1481 'post_status' => null,
1482 'post_parent' => null,
1483 'meta_query' => array(
1484 array(
1485 'key' => 'isc_image_source',
1486 'value' => '',
1487 'compare' => '='
1488 ),
1489 array(
1490 'key' => 'isc_image_source_own',
1491 'value' => '',
1492 'compare' => ''
1493 )
1494 )
1495 );
1496 $attachments = get_posts($args);
1497 $options = $this->get_isc_options();
1498 if (!empty($attachments) && $options['warning_onesource_missing'] ) {
1499 $missing_src = esc_url(admin_url('upload.php?page=image-source-control-isc/templates/missing_sources.php'));
1500 ?>
1501 <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>
1502 <?php
1503 }
1504 }
1505
1506 }// end of class
1507
1508 $inc_path = ABSPATH . 'wp-includes/';
1509 /**
1510 * "pluggable.php" is not defined at this point. Not sure about the reason.
1511 */
1512 require_once($inc_path . 'pluggable.php');
1513
1514 /**
1515 * Need an instance of ISC_CLASS when the register_activation_hook is called (earlier than the "plugins_loaded" hook).
1516 */
1517 global $my_isc;
1518 $my_isc = new ISC_CLASS();
1519
1520 /**
1521 * the next functions are just to have an easier access from outside the class
1522 */
1523 function isc_list($post_id = 0) {
1524 $isc = new ISC_CLASS();
1525 echo $isc->list_post_attachments_with_sources($post_id);
1526 }
1527 }
1528