| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Image Source Control |
| 4 |
Version: 1.1.3 |
| 5 |
Plugin URI: http://webgilde.com/en/image-source-control/ |
| 6 |
Description: The Image Source Control saves the source of an image, lists them and warns if it is missing. |
| 7 |
Author: Thomas Maier |
| 8 |
Author URI: http://www.webgilde.com/ |
| 9 |
License: GPL v3 |
| 10 |
|
| 11 |
Image Source Control Plugin for WordPress |
| 12 |
Copyright (C) 2012, Thomas Maier - thomas.maier@webgilde.com |
| 13 |
|
| 14 |
This program is free software: you can redistribute it and/or modify |
| 15 |
it under the terms of the GNU General Public License as published by |
| 16 |
the Free Software Foundation, either version 3 of the License, or |
| 17 |
(at your option) any later version. |
| 18 |
|
| 19 |
This program is distributed in the hope that it will be useful, |
| 20 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
GNU General Public License for more details. |
| 23 |
|
| 24 |
You should have received a copy of the GNU General Public License |
| 25 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 26 |
* |
| 27 |
* Followed the following tutorials |
| 28 |
* http://wpengineer.com/2076/add-custom-field-attachment-in-wordpress/ |
| 29 |
* http://bueltge.de/eigene-felder-dateiverwaltung-wordpress/1226/ (same like above, but in German) |
| 30 |
* |
| 31 |
* |
| 32 |
*/ |
| 33 |
|
| 34 |
//avoid direct calls to this file |
| 35 |
if (!function_exists('add_action')) { |
| 36 |
header('Status: 403 Forbidden'); |
| 37 |
header('HTTP/1.1 403 Forbidden'); |
| 38 |
exit(); |
| 39 |
} |
| 40 |
|
| 41 |
define('ISCVERSION', '1.1.3'); |
| 42 |
define('ISCNAME', 'Image Source Control'); |
| 43 |
define('ISCTEXTDOMAIN', 'isc'); |
| 44 |
define('ISCDIR', basename(dirname(__FILE__))); |
| 45 |
define('ISCPATH', plugin_dir_path(__FILE__)); |
| 46 |
|
| 47 |
load_plugin_textdomain(ISCTEXTDOMAIN, false, dirname(plugin_basename(__FILE__)) . '/languages/'); |
| 48 |
|
| 49 |
if (!class_exists('ISC_CLASS')) { |
| 50 |
|
| 51 |
class ISC_CLASS |
| 52 |
{ |
| 53 |
/** |
| 54 |
* define default meta fields |
| 55 |
*/ |
| 56 |
protected $_fields = array( |
| 57 |
'image_source' => array( |
| 58 |
'id' => 'isc_image_source', |
| 59 |
'default' => '', |
| 60 |
), |
| 61 |
'image_source_own' => array( |
| 62 |
'id' => 'isc_image_source_own', |
| 63 |
'default' => '', |
| 64 |
) |
| 65 |
); |
| 66 |
|
| 67 |
/** |
| 68 |
* allowed image file types/extensions |
| 69 |
* @since 1.1 |
| 70 |
*/ |
| 71 |
protected $_allowedExtensions = array( |
| 72 |
'jpg', 'png', 'gif' |
| 73 |
); |
| 74 |
|
| 75 |
/** |
| 76 |
* Setup registers filterts and actions. |
| 77 |
*/ |
| 78 |
public function __construct() |
| 79 |
{ |
| 80 |
// insert all function for the frontend here |
| 81 |
|
| 82 |
add_shortcode('isc_list', array($this, 'list_post_attachments_with_sources_shortcode')); |
| 83 |
add_shortcode('isc_list_all', array($this, 'list_all_post_attachments_sources_shortcode')); |
| 84 |
|
| 85 |
// insert all backend functions below this check |
| 86 |
if (!current_user_can('upload_files')) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
add_filter('attachment_fields_to_edit', array($this, 'add_isc_fields'), 10, 2); |
| 91 |
add_filter('attachment_fields_to_save', array($this, 'isc_fields_save'), 10, 2); |
| 92 |
|
| 93 |
add_action('admin_menu', array($this, 'create_menu')); |
| 94 |
|
| 95 |
add_action('admin_enqueue_scripts', array($this, 'add_admin_scripts')); |
| 96 |
|
| 97 |
// ajax function; 'add_meta_fields' is the action defined in isc.js as the action to be called via ajax |
| 98 |
add_action('wp_ajax_add_meta_fields', array($this, 'add_meta_values_to_attachments')); |
| 99 |
|
| 100 |
// save image information in meta field when a post is saved |
| 101 |
add_action('save_post', array($this, 'save_image_information_on_post_save')); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* create the menu pages for isc |
| 106 |
*/ |
| 107 |
public function create_menu() |
| 108 |
{ |
| 109 |
// this page should be accessable by editors and higher |
| 110 |
$menuhook = add_submenu_page('upload.php', 'missing image sources by Image Source Control Plugin', __('Missing Sources', ISCTEXTDOMAIN), 'edit_others_posts', ISCPATH . '/templates/missing_sources.php', ''); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* add scripts to admin pages |
| 115 |
* @since 1.0 |
| 116 |
* @update 1.1.1 |
| 117 |
*/ |
| 118 |
public function add_admin_scripts($hook) |
| 119 |
{ |
| 120 |
if ($hook != 'image-source-control-isc/templates/missing_sources.php') { |
| 121 |
return; |
| 122 |
} |
| 123 |
wp_enqueue_script('isc_script', plugins_url('/js/isc.js', __FILE__), false, ISCVERSION); |
| 124 |
// this is to define ajaxurl to be able to use this in its own js script |
| 125 |
// wp_localize_script( 'isc_script', 'IscAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* add custom field to attachment |
| 130 |
* @param arr $form_fields |
| 131 |
* @param object $post |
| 132 |
* @return arr |
| 133 |
* @since 1.0 |
| 134 |
* @updated 1.1 |
| 135 |
*/ |
| 136 |
public function add_isc_fields($form_fields, $post) |
| 137 |
{ |
| 138 |
// add input field for source |
| 139 |
$form_fields['isc_image_source']['label'] = __('Image Source', ISCTEXTDOMAIN); |
| 140 |
$form_fields['isc_image_source']['value'] = get_post_meta($post->ID, 'isc_image_source', true); |
| 141 |
$form_fields['isc_image_source']['helps'] = __('Include the image source here.', ISCTEXTDOMAIN); |
| 142 |
|
| 143 |
// add checkbox to mark as your own image |
| 144 |
$form_fields['isc_image_source_own']['input'] = 'html'; |
| 145 |
$form_fields['isc_image_source_own']['label'] = ''; |
| 146 |
$form_fields['isc_image_source_own']['helps'] = |
| 147 |
__('Check this box if this is your own image and doesn\'t need a source.', ISCTEXTDOMAIN); |
| 148 |
$form_fields['isc_image_source_own']['html'] = |
| 149 |
"<input type='checkbox' value='1' name='attachments[{$post->ID}][isc_image_source_own]' id='attachments[{$post->ID}][isc_image_source_own]' " |
| 150 |
. checked(get_post_meta($post->ID, 'isc_image_source_own', true), 1, false ) |
| 151 |
. " style=\"width:14px\"/> " |
| 152 |
. __('This is my image', ISCTEXTDOMAIN); |
| 153 |
|
| 154 |
return $form_fields; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* save image source to post_meta |
| 159 |
* @param object $post |
| 160 |
* @param $attachment |
| 161 |
* @return object $post |
| 162 |
*/ |
| 163 |
public function isc_fields_save($post, $attachment) |
| 164 |
{ |
| 165 |
if (isset($attachment['isc_image_source'])) { |
| 166 |
update_post_meta($post['ID'], 'isc_image_source', $attachment['isc_image_source']); |
| 167 |
} |
| 168 |
|
| 169 |
update_post_meta($post['ID'], 'isc_image_source_own', $attachment['isc_image_source_own']); |
| 170 |
return $post; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* create image sources list for all images of this post |
| 175 |
* @since 1.0 |
| 176 |
* @update 1.1 |
| 177 |
* @param int $post_id id of the current post/page |
| 178 |
* @return echo output |
| 179 |
*/ |
| 180 |
public function list_post_attachments_with_sources($post_id = 0) |
| 181 |
{ |
| 182 |
global $post; |
| 183 |
|
| 184 |
if (empty($post_id)) { |
| 185 |
if (!empty($post->ID)) { |
| 186 |
$post_id = $post->ID; |
| 187 |
} else { |
| 188 |
return; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
$attachments = get_post_meta($post_id, 'isc_post_images', true); |
| 193 |
// if attachments is an empty string, search for images in it |
| 194 |
if ($attachments == '') { |
| 195 |
$this->save_image_information_on_load ($post_id, $_content); |
| 196 |
$attachments = get_post_meta($post_id, 'isc_post_images', true); |
| 197 |
} |
| 198 |
|
| 199 |
$return = ''; |
| 200 |
if (!empty($attachments)) { |
| 201 |
$atts = array(); |
| 202 |
foreach ($attachments as $attachment_id => $attachment_array) { |
| 203 |
$atts[$attachment_id]['title'] = get_the_title($attachment_id); |
| 204 |
$own = get_post_meta($attachment_id, 'isc_image_source_own', true); |
| 205 |
$source = get_post_meta($attachment_id, 'isc_image_source', true); |
| 206 |
|
| 207 |
if ( $own == '' && $source == '' ) { |
| 208 |
// remove if no information set |
| 209 |
unset($atts[$attachment_id]); |
| 210 |
continue; |
| 211 |
} elseif ($own != '') { |
| 212 |
$atts[$attachment_id ]['source'] = __('by the author', ISCTEXTDOMAIN); |
| 213 |
} else { |
| 214 |
$atts[$attachment_id ]['source'] = $source; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
$return = $this->_renderAttachments($atts); |
| 219 |
} |
| 220 |
|
| 221 |
return $return; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* @param array $attachments |
| 226 |
*/ |
| 227 |
protected function _renderAttachments($attachments) |
| 228 |
{ |
| 229 |
// don't display anything, if no image sources displayed |
| 230 |
if ($attachments == array()) { |
| 231 |
return ; |
| 232 |
} |
| 233 |
ob_start(); |
| 234 |
// TODO this is almost trivial to replace with sprintf |
| 235 |
?> |
| 236 |
<p class="isc_image_list_title"><?php _e('image sources:', ISCTEXTDOMAIN); ?></p> |
| 237 |
<ul class="isc_image_list"><?php |
| 238 |
|
| 239 |
foreach ($attachments as $atts_id => $atts_array) : |
| 240 |
if (empty($atts_array['source'])) { |
| 241 |
continue; |
| 242 |
} |
| 243 |
// TODO localise |
| 244 |
?><li><?php echo $atts_array['title'] . ': ' . $atts_array['source']; ?></li><?php |
| 245 |
endforeach; |
| 246 |
?></ul><?php |
| 247 |
return ob_get_clean(); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* shortcode function to list all image sources |
| 252 |
* @param arr $atts |
| 253 |
*/ |
| 254 |
public function list_post_attachments_with_sources_shortcode($atts = array()) |
| 255 |
{ |
| 256 |
global $post; |
| 257 |
extract(shortcode_atts(array('id' => 0), $atts)); |
| 258 |
|
| 259 |
// if $id not set, use the current ID from the post |
| 260 |
if (empty($id)) { |
| 261 |
$id = $post->ID; |
| 262 |
} |
| 263 |
|
| 264 |
if (empty($id)) { |
| 265 |
return; |
| 266 |
} |
| 267 |
return $this->list_post_attachments_with_sources($id); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* get all attachments without sources |
| 272 |
* the downside of this function: is there is not even an empty metakey field, nothing is going to be retrieved |
| 273 |
* @todo fix this in WP 3.5 with compare => 'NOT EXISTS' |
| 274 |
*/ |
| 275 |
public function get_attachments_without_sources() |
| 276 |
{ |
| 277 |
$args = array( |
| 278 |
'post_type' => 'attachment', |
| 279 |
'numberposts' => -1, |
| 280 |
'post_status' => null, |
| 281 |
'post_parent' => null, |
| 282 |
'meta_query' => array( |
| 283 |
// image source is empty |
| 284 |
array( |
| 285 |
'key' => 'isc_image_source', |
| 286 |
'value' => '', |
| 287 |
'compare' => '=', |
| 288 |
), |
| 289 |
// and image source is not set |
| 290 |
array( |
| 291 |
'key' => 'isc_image_source_own', |
| 292 |
'value' => '1', |
| 293 |
'compare' => '!=', |
| 294 |
), |
| 295 |
) |
| 296 |
); |
| 297 |
|
| 298 |
$attachments = get_posts($args); |
| 299 |
if (!empty($attachments)) { |
| 300 |
return $attachments; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* add meta values to all attachments |
| 306 |
* @todo probably need to fix this when more fields are added along the way |
| 307 |
* @todo use compare => 'NOT EXISTS' when WP 3.5 is up to retrieve only values where it is not set |
| 308 |
* @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 |
| 309 |
*/ |
| 310 |
public function add_meta_values_to_attachments() |
| 311 |
{ |
| 312 |
// retrieve all attachments |
| 313 |
$args = array( |
| 314 |
'post_type' => 'attachment', |
| 315 |
'numberposts' => -1, |
| 316 |
'post_status' => null, |
| 317 |
'post_parent' => null, |
| 318 |
); |
| 319 |
|
| 320 |
$attachments = get_posts($args); |
| 321 |
if (empty($attachments)) { |
| 322 |
return; |
| 323 |
} |
| 324 |
|
| 325 |
$count = 0; |
| 326 |
foreach ($attachments as $_attachment) { |
| 327 |
$set = false; |
| 328 |
setup_postdata($_attachment); |
| 329 |
foreach ($this->_fields as $_field) { |
| 330 |
$meta = get_post_meta($_attachment->ID, $_field['id'], true); |
| 331 |
if (empty($meta)) { |
| 332 |
update_post_meta($_attachment->ID, $_field['id'], $_field['default']); |
| 333 |
$set = true; |
| 334 |
} |
| 335 |
} |
| 336 |
if ($set) { |
| 337 |
$count++; |
| 338 |
} |
| 339 |
} |
| 340 |
echo sprintf(__('Added meta fields to %d images.', ISCTEXTDOMAIN), $count) . |
| 341 |
'<br/><input type="button" value="' . __('reload page', ISCTEXTDOMAIN ) . '" onClick="window.location.reload()">'; |
| 342 |
die(); |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* show the loading image from wp-admin/images/loading.gif |
| 347 |
* @param bool $display should this be displayed directly or hidden? via inline css |
| 348 |
*/ |
| 349 |
public function show_loading_image($display = true) |
| 350 |
{ |
| 351 |
$img_path = admin_url("/images/loading.gif"); |
| 352 |
$file_path = ABSPATH . "wp-admin/images/loading.gif"; |
| 353 |
if (file_exists($file_path)) { |
| 354 |
echo '<span id="isc_loading_img" style="display: none;"><img src="' . $img_path . '" width="16" height="16" alt="loading"/></span>'; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* this is an entry function to save image information to a post when it is saved |
| 360 |
* @since 1.1 |
| 361 |
* @param type $post_id |
| 362 |
*/ |
| 363 |
public function save_image_information_on_post_save($post_id) |
| 364 |
{ |
| 365 |
// return, if save_post is called more than one time |
| 366 |
if (did_action('save_post') !== 1) { |
| 367 |
return; |
| 368 |
} |
| 369 |
|
| 370 |
if (isset($_POST['post_type']) && 'attachment' == $_POST['post_type']) { |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
// check if this is a revision and if so, use parent post id |
| 375 |
if ($_id = wp_is_post_revision($post_id)) { |
| 376 |
$post_id = $_id; |
| 377 |
} |
| 378 |
|
| 379 |
$_content = stripslashes($_REQUEST['content']); |
| 380 |
$this->save_image_information($post_id, $_content); |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* save image information for a post when it is viewed and the image source list is enabled |
| 385 |
* (this is in case the plugin is new and the current post wasn't saved before) |
| 386 |
* |
| 387 |
* @since 1.1 |
| 388 |
*/ |
| 389 |
public function save_image_information_on_load() |
| 390 |
{ |
| 391 |
global $post; |
| 392 |
if (empty($post->ID)) { |
| 393 |
return; |
| 394 |
} |
| 395 |
|
| 396 |
$post_id = $post->ID; |
| 397 |
$_content = $post->post_content; |
| 398 |
|
| 399 |
$this->save_image_information($post_id, $_content); |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* retrieve images added to a post or page and save all information as a meta value |
| 404 |
* @since 1.1 |
| 405 |
* @todo check for more post types that maybe should not be parsed here |
| 406 |
*/ |
| 407 |
public function save_image_information($post_id, $_content) |
| 408 |
{ |
| 409 |
$_image_urls = $this->_filter_src_attributes($_content); |
| 410 |
$_imgs = array(); |
| 411 |
|
| 412 |
foreach ($_image_urls as $_image_url) { |
| 413 |
// get ID of images by url |
| 414 |
$img_id = $this->get_image_by_url($_image_url); |
| 415 |
$_imgs[$img_id] = array( |
| 416 |
'src' => $_image_url |
| 417 |
); |
| 418 |
} |
| 419 |
|
| 420 |
// add thumbnail information |
| 421 |
$thumb_id = get_post_thumbnail_id($post_id); |
| 422 |
$_imgs[$thumb_id] = array( |
| 423 |
'src' => wp_get_attachment_url($thumb_id), |
| 424 |
'thumbnail' => true |
| 425 |
); |
| 426 |
|
| 427 |
// TODO _imgs is an non-empty array by now |
| 428 |
if (empty($_imgs)) { |
| 429 |
$_imgs = false; |
| 430 |
} |
| 431 |
update_post_meta($post_id, 'isc_post_images', $_imgs); |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* filter image src attribute from text |
| 436 |
* @since 1.1 |
| 437 |
* @updated 1.1.3 |
| 438 |
* @return array with image src uris |
| 439 |
*/ |
| 440 |
public function _filter_src_attributes($content = '') |
| 441 |
{ |
| 442 |
$srcs = array(); |
| 443 |
if (empty($content)) |
| 444 |
return $srcs; |
| 445 |
|
| 446 |
// parse HTML with DOM |
| 447 |
$dom = new DOMDocument; |
| 448 |
$dom->loadHTML($content); |
| 449 |
foreach ($dom->getElementsByTagName('img') as $node) { |
| 450 |
$srcs[] = $node->getAttribute('src'); |
| 451 |
} |
| 452 |
|
| 453 |
return $srcs; |
| 454 |
} |
| 455 |
|
| 456 |
/** |
| 457 |
* get image by url accessing the database directly |
| 458 |
* @since 1.1 |
| 459 |
* @updated 1.1.3 |
| 460 |
* @param string $url url of the image |
| 461 |
* @return id of the image |
| 462 |
*/ |
| 463 |
public function get_image_by_url($url = '') |
| 464 |
{ |
| 465 |
if (empty($url)) { |
| 466 |
return 0; |
| 467 |
} |
| 468 |
$types = implode('|', $this->_allowedExtensions); |
| 469 |
// check for the format 'image-title-(e12452112-)300x200.jpg' and remove the image size and edit mark from it |
| 470 |
$newurl = preg_replace("/(-e\d+){0,1}-(\d+)x(\d+)\.({$types})$/i", '.${4}', $url); |
| 471 |
global $wpdb; |
| 472 |
$query = $wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE guid = %s", $newurl); |
| 473 |
$id = $wpdb->get_var($query); |
| 474 |
return $id; |
| 475 |
} |
| 476 |
|
| 477 |
/** |
| 478 |
* create shortcode to list all image sources in the frontend |
| 479 |
* @param array $atts |
| 480 |
* @since 1.1.3 |
| 481 |
* @todo link to the post |
| 482 |
*/ |
| 483 |
public function list_all_post_attachments_sources_shortcode($atts = array()) |
| 484 |
{ |
| 485 |
|
| 486 |
/** |
| 487 |
* @todo why not translate here with the code below? |
| 488 |
*/ |
| 489 |
extract(shortcode_atts(array( |
| 490 |
'per_page' => 99999, |
| 491 |
'before_links' => '', |
| 492 |
'after_links' => '', |
| 493 |
'prev_text' => '« Previous', |
| 494 |
'next_text' => 'Next »' |
| 495 |
), |
| 496 |
$atts)); |
| 497 |
|
| 498 |
/** |
| 499 |
* @todo why not include this into the array above? |
| 500 |
*/ |
| 501 |
if ('« Previous' == $prev_text) |
| 502 |
$prev_text = __('« Previous', ISCTEXTDOMAIN); |
| 503 |
if ('Next »' == $next_text) |
| 504 |
$next_text = __('Next »', ISCTEXTDOMAIN); |
| 505 |
|
| 506 |
// retrieve all attachments |
| 507 |
$args = array( |
| 508 |
'post_type' => 'attachment', |
| 509 |
'numberposts' => -1, |
| 510 |
'post_status' => null, |
| 511 |
'post_parent' => null, |
| 512 |
/** @todo maybe add offset to not retrieve the first results when not on first page */ |
| 513 |
/** @todo maybe add limit to not retrieve more results than on the current page */ |
| 514 |
); |
| 515 |
|
| 516 |
$attachments = get_posts($args); |
| 517 |
if (empty($attachments)) { |
| 518 |
return; |
| 519 |
} |
| 520 |
|
| 521 |
$connected_atts = array(); |
| 522 |
|
| 523 |
//Keeps only those ones who have parent |
| 524 |
|
| 525 |
foreach ($attachments as $_attachment) { |
| 526 |
if ($_attachment->post_parent) { |
| 527 |
$connected_atts[$_attachment->ID]['source'] = get_post_meta($_attachment->ID, 'isc_image_source', true); |
| 528 |
$connected_atts[$_attachment->ID]['own'] = get_post_meta($_attachment->ID, 'isc_image_source_own', true); |
| 529 |
$connected_atts[$_attachment->ID]['title'] = $_attachment->post_title; |
| 530 |
$connected_atts[$_attachment->ID]['parent'] = get_the_title($_attachment->post_parent); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
$total = count($connected_atts); |
| 535 |
|
| 536 |
if (0 == $total) |
| 537 |
return; |
| 538 |
|
| 539 |
$page = isset($_GET['isc-page']) ? intval($_GET['isc-page']) : 1; |
| 540 |
$down_limit = 1; // First page |
| 541 |
|
| 542 |
$up_limit = 1; |
| 543 |
|
| 544 |
if ($per_page < $total) { |
| 545 |
$rem = $total % $per_page; // The Remainder of $total/$per_page |
| 546 |
$up_limit = ($total - $rem) / $per_page; |
| 547 |
if (0 < $rem) { |
| 548 |
$up_limit++; //If rem is positive, add the last page that contains less than $per_page attachment; |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
ob_start(); |
| 553 |
if ( 2 > $up_limit ) { |
| 554 |
$this->display_all_attachment_list($connected_atts); |
| 555 |
} else { |
| 556 |
$starting_atts = $per_page * ($page - 1); // for page 2 and 3 $per_page start display on $connected_atts[3*(2-1) = 3] |
| 557 |
$paged_atts = array_slice($connected_atts, $starting_atts, $per_page, true); |
| 558 |
$this->display_all_attachment_list($paged_atts); |
| 559 |
$this->pagination_links($up_limit, $before_links, $after_links, $prev_text, $next_text); |
| 560 |
} |
| 561 |
|
| 562 |
$output = ob_get_clean(); |
| 563 |
return $output; |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
/** |
| 568 |
* performs rendering of all attachments list |
| 569 |
* @since 1.1.3 |
| 570 |
*/ |
| 571 |
public function display_all_attachment_list($atts) |
| 572 |
{ |
| 573 |
if (! is_array($atts) || $atts == array()) |
| 574 |
return; |
| 575 |
?> |
| 576 |
<table> |
| 577 |
<thead> |
| 578 |
<?php /* <th><?php _e("Attachment's ID", ISCTEXTDOMAIN); ?></th>*/ ?> |
| 579 |
<th><?php _e('Title', ISCTEXTDOMAIN); ?></th> |
| 580 |
<th><?php _e('Attached to', ISCTEXTDOMAIN); ?></th> |
| 581 |
<th><?php _e('Source', ISCTEXTDOMAIN); ?></th> |
| 582 |
</thead> |
| 583 |
<tbody> |
| 584 |
<?php foreach ($atts as $id => $data) : ?> |
| 585 |
<?php |
| 586 |
/** @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 */ |
| 587 |
$source = __('Not available', ISCTEXTDOMAIN); |
| 588 |
if (1 == $data['own']) { |
| 589 |
/** @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 */ |
| 590 |
$source = __('By the author', ISCTEXTDOMAIN); |
| 591 |
} elseif (!empty($data['source'])) { |
| 592 |
$source = $data['source']; |
| 593 |
} |
| 594 |
?> |
| 595 |
<tr> |
| 596 |
<?php /* <td><?php echo $id ?></td>*/ ?> |
| 597 |
<td><?php echo $data['title']; ?></td><td><?php echo $data['parent']; ?></td><td><?php echo esc_attr($source); ?></td> |
| 598 |
</tr> |
| 599 |
<?php endforeach; ?> |
| 600 |
</tbody> |
| 601 |
</table> |
| 602 |
<?php |
| 603 |
} |
| 604 |
|
| 605 |
/** |
| 606 |
* Render pagination links, use $before_links and after_links to wrap pagination links inside an additional block |
| 607 |
* @param int $max_page total page count |
| 608 |
* @param string $before_links optional html to display before pagination links |
| 609 |
* @param string $after_links optional html to display after pagination links |
| 610 |
* @param string $prev_text text for the previous page link |
| 611 |
* @param string $next_text text for the next page link |
| 612 |
* @since 1.1.3 |
| 613 |
* |
| 614 |
*/ |
| 615 |
public function pagination_links($max_page, $before_links, $after_links, $prev_text, $next_text) |
| 616 |
{ |
| 617 |
if ((! isset($max_page)) || (! isset($before_links)) || (! isset($after_links)) || (! isset($prev_text)) || (! isset($next_text))) |
| 618 |
return; |
| 619 |
if (! empty($before_links)) |
| 620 |
echo $before_links; |
| 621 |
?> |
| 622 |
<div class="isc-paginated-links"> |
| 623 |
<?php |
| 624 |
$page = isset($_GET['isc-page']) ? intval($_GET['isc-page']) : 1; |
| 625 |
if ($max_page < $page) { |
| 626 |
$page = $max_page; |
| 627 |
} |
| 628 |
if ($page < 1) { |
| 629 |
$page = 1; |
| 630 |
} |
| 631 |
$min_page = 1; |
| 632 |
$backward_distance = $page - $min_page; |
| 633 |
$forward_distance = $max_page - $page; |
| 634 |
|
| 635 |
$original_page_link = get_page_link(); |
| 636 |
|
| 637 |
/** |
| 638 |
* Remove the query_string of the page_link (?page_id=xyz for the standard permalink structure), |
| 639 |
* which is already captured in $_SERVER['QUERY_STRING']. |
| 640 |
* @todo replace regex with other value (does WP store the url path without attributes somewhere? |
| 641 |
*/ |
| 642 |
$page_link = preg_replace('#\?.*$#', '', $original_page_link); |
| 643 |
|
| 644 |
/** |
| 645 |
* 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 |
| 646 |
* pagination link. |
| 647 |
*/ |
| 648 |
|
| 649 |
if (isset($_GET['isc-page'])) { |
| 650 |
unset($_GET['isc-page']); |
| 651 |
} |
| 652 |
|
| 653 |
$query_string = http_build_query($_GET); |
| 654 |
|
| 655 |
$isc_query_tag = ''; |
| 656 |
if (empty($query_string)) { |
| 657 |
$isc_query_tag = '?isc-page='; |
| 658 |
} else { |
| 659 |
$query_string = '?' . $query_string; |
| 660 |
$isc_query_tag = '&isc-page='; |
| 661 |
} |
| 662 |
|
| 663 |
if ($min_page != $page) { |
| 664 |
?> |
| 665 |
<a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page-1); ?>" class="prev page-numbers"><?php echo $prev_text; ?></a> |
| 666 |
<?php |
| 667 |
} |
| 668 |
|
| 669 |
if (5 < $max_page) { |
| 670 |
|
| 671 |
if (3 < $backward_distance) { |
| 672 |
?> |
| 673 |
<a href="<?php echo $page_link . $query_string . $isc_query_tag; ?>1" class="page-numbers">1</a> |
| 674 |
<span class="page-numbers dots">...</span> |
| 675 |
<a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page-2);?>" class="page-numbers"><?php echo $page-2; ?></a> |
| 676 |
<a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page-1);?>" class="page-numbers"><?php echo $page-1; ?></a> |
| 677 |
<span class="page-numbers current"><?php echo $page; ?></span> |
| 678 |
<?php |
| 679 |
} else { |
| 680 |
for ($i = 1; $i <= $page; $i++) { |
| 681 |
if ($i == $page) { |
| 682 |
?> |
| 683 |
<span class="page-numbers current"><?php echo $i; ?></span> |
| 684 |
<?php |
| 685 |
} else { |
| 686 |
?> |
| 687 |
<a href="<?php echo $page_link . $query_string . $isc_query_tag . $i;?>" class="page-numbers"><?php echo $i; ?></a> |
| 688 |
<?php |
| 689 |
} |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
if (3 < $forward_distance) { |
| 694 |
?> |
| 695 |
<a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page+1);?>" class="page-numbers"><?php echo $page+1; ?></a> |
| 696 |
<a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page+2);?>" class="page-numbers"><?php echo $page+2; ?></a> |
| 697 |
<span class="page-numbers dots">...</span> |
| 698 |
<a href="<?php echo $page_link . $query_string . $isc_query_tag . $max_page;?>" class="page-numbers"><?php echo $max_page; ?></a> |
| 699 |
<?php |
| 700 |
} else { |
| 701 |
for ($i = $page+1; $i <= $max_page; $i++) { |
| 702 |
?> |
| 703 |
<a href="<?php echo $page_link . $query_string . $isc_query_tag . $i;?>" class="page-numbers"><?php echo $i; ?></a> |
| 704 |
<?php |
| 705 |
} |
| 706 |
} |
| 707 |
} else { |
| 708 |
for ($i = 1; $i <= $max_page; $i++) { |
| 709 |
if ($i == $page) { |
| 710 |
?> |
| 711 |
<span class="page-numbers current"><?php echo $i; ?></span> |
| 712 |
<?php |
| 713 |
} else { |
| 714 |
?> |
| 715 |
<a href="<?php echo $page_link . $query_string . $isc_query_tag . $i;?>" class="page-numbers"><?php echo $i; ?></a> |
| 716 |
<?php |
| 717 |
} |
| 718 |
} |
| 719 |
} |
| 720 |
if ($page != $max_page) { |
| 721 |
?> |
| 722 |
<a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page+1);?>" class="next page-numbers"><?php echo $next_text; ?></a> |
| 723 |
<?php |
| 724 |
} |
| 725 |
?> |
| 726 |
</div> |
| 727 |
<?php |
| 728 |
echo $after_links; |
| 729 |
} |
| 730 |
|
| 731 |
}// end of class |
| 732 |
|
| 733 |
/** |
| 734 |
* |
| 735 |
*/ |
| 736 |
function add_image_source_fields_start() { |
| 737 |
$isc = new ISC_CLASS(); |
| 738 |
} |
| 739 |
|
| 740 |
add_action('plugins_loaded', 'add_image_source_fields_start'); |
| 741 |
|
| 742 |
/** |
| 743 |
* the next functions are just to have an easier access from outside the class |
| 744 |
*/ |
| 745 |
function isc_list($post_id = 0) { |
| 746 |
$isc = new ISC_CLASS(); |
| 747 |
echo $isc->list_post_attachments_with_sources($post_id); |
| 748 |
} |
| 749 |
} |
| 750 |
|