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

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