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

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

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