forked from Labs64/credit-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredit-tracker-functions.php
129 lines (116 loc) · 4.06 KB
/
credit-tracker-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* @package Credit_Tracker
* @author Labs64 <[email protected]>
* @license GPL-2.0+
* @link http://www.labs64.com
* @copyright 2013 Labs64
*/
/**
* Returns array of images with attributes.
*
* Sample input parameters:
* $attr = array(
* 'size' => 'thumbnail', // image size (thumbnail, medium, large or full)
* 'include' => '121,122,123', // image id or comma delimited list of image id's
* 'numberposts' => -1 // max quantity of records to return; default = -1 (all)
* );
*/
function get_images($attr)
{
$defaults = array(
'size' => 'thumbnail',
'include' => '0',
'numberposts' => -1
);
// merge defaults with user input
$attr = wp_parse_args($attr, $defaults);
extract($attr);
$args = array(
'include' => $include,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'post__in',
'numberposts' => $numberposts
);
$items = array();
$item = array();
// get all attachements
$attachments = get_posts($args);
if (empty($attachments)) {
return $items;
}
foreach ($attachments as $attachment) {
$imgSrc = wp_get_attachment_image_src($attachment->ID, $size, false);
if (!empty($imgSrc)) {
$item['ID'] = $attachment->ID;
$item['url'] = $imgSrc[0];
$item['width'] = $imgSrc[1];
$item['height'] = $imgSrc[2];
$item['alt'] = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$item['title'] = wptexturize($attachment->post_title);
$item['desc'] = wptexturize($attachment->post_content);
$item['caption'] = wptexturize($attachment->post_excerpt);
$item['ident_nr'] = wptexturize(get_post_meta($attachment->ID, 'credit-tracker-ident_nr', true));
$item['source'] = wptexturize(get_post_meta($attachment->ID, 'credit-tracker-source', true));
$item['author'] = wptexturize(get_post_meta($attachment->ID, 'credit-tracker-author', true));
$item['publisher'] = wptexturize(get_post_meta($attachment->ID, 'credit-tracker-publisher', true));
$item['license'] = wptexturize(get_post_meta($attachment->ID, 'credit-tracker-license', true));
$items[] = $item;
}
}
return $items;
}
/**
* Returns copyright string formatted according to the given format.
*/
function process_item_copyright($item, $string)
{
$string = str_replace("%ident_nr%", $item['ident_nr'], $string);
$string = str_replace("%source%", $item['source'], $string);
$string = str_replace("%author%", $item['author'], $string);
$string = str_replace("%publisher%", $item['publisher'], $string);
$string = str_replace("%license%", $item['license'], $string);
$string = str_replace("%title%", $item['title'], $string);
$string = str_replace("%caption%", $item['caption'], $string);
return $string;
}
/**
* Prints a combobox based on options and selected=match value
*
* Parameters:
* $options = array of options (suggest using helper functions)
* $selected = which of those options should be selected (allows just one; is case sensitive)
*
* Outputs (based on array ( $key => $value ):
* <option value=$key>$value</option>
* <option value=$key selected="selected">$value</option>
*/
function get_combobox_options($options, $selected)
{
$ret = '';
foreach ($options as $key => $value) {
$ret .= '<option value="' . $key . '"';
if ($key == $selected) {
$ret .= ' selected="selected"';
}
$ret .= '>' . $value . '</option>';
}
return $ret;
}
/**
* Shorten an URL
*
* @param string $url
* @return string
*/
function strip_url($url, $len = 20)
{
$short_url = str_replace(array('http://', 'https://', 'www.'), '', $url);
$short_url = preg_replace('/[^a-zA-Z0-9_-]/', '', $short_url);
if (strlen($short_url) > $len) {
$short_url = substr($short_url, 0, $len);
}
return $short_url;
}