-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathpmxi_gallery_image.php
130 lines (106 loc) · 3.37 KB
/
pmxi_gallery_image.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
<?php
/**
* ==================================
* Action: pmxi_gallery_image
* ==================================
* This is called for images imported via the default image field and ACF image fields.
*
* This is usually used when a plugin/theme uses a special custom field to store
* the image gallery information
*
* @param $post_id int - The id of the post just created/updated
* @param $att_id int - The attachment id of the image
* @param $file string - The local file path to the full size image
*/
function my_gallery_image($post_id, $att_id, $file)
{
}
add_action('pmxi_gallery_image', 'my_gallery_image', 10, 3);
// ----------------------------
// Example uses below
// ----------------------------
/**
* For image galleries with the format:
*
* array (
* image_id_1 => "image_url_1",
* image_id_2 => "image_url_2",
* ...
* );
*
*/
function gallery_id_url($post_id, $att_id, $filepath, $is_keep_existing_images = '')
{
$key = '_gallery'; // Edit this: Set meta key for gallery array here
$size = 'full'; // Edit this: Set WordPress image size for the URL here (e.g. "full" or "thumb")
$gallery = get_post_meta($post_id, $key, TRUE);
if (empty($gallery)) {
$gallery = array();
}
if (!isset($gallery[$att_id])) {
$src = wp_get_attachment_image_src($att_id, $size);
$gallery[$att_id] = $src[0];
update_post_meta($post_id, $key, $gallery);
}
}
add_action('pmxi_gallery_image', 'gallery_id_url', 10, 4);
/**
* For image galleries with the format:
*
* array (
* 0 => image_id_1,
* 1 => image_id_2,
* ...
* );
*
*/
function gallery_n_id($post_id, $att_id, $filepath, $is_keep_existing_images = '')
{
$key = '_gallery'; // Edit this: Set meta key for gallery array here
$gallery = get_post_meta($post_id, $key, TRUE);
if (empty($gallery)) {
$gallery = array();
}
if (!in_array($att_id, $gallery)) {
$gallery[] = $att_id;
update_post_meta($post_id, $key, $gallery);
}
}
add_action('pmxi_gallery_image', 'gallery_n_id', 10, 4);
/**
* For image galleries where each image id is saved as an individual post meta value with the same key (ie. No arrays)
*
*/
function gallery_meta_id($post_id, $att_id, $filepath, $is_keep_existing_images = '')
{
$key = '_gallery'; // Edit this: Set meta key for gallery array here
$result = get_post_meta($post_id, $key, FALSE);
if (is_array($result)) {
if (!in_array($att_id, $result)) {
add_post_meta($post_id, $key, $att_id);
}
}
}
add_action('pmxi_gallery_image', 'gallery_meta_id', 10, 4);
/**
* For galleries where image id's are saved as a single string
* with a comma or other separators:
*
* "23,25,31"
*
*/
function gallery_ids_in_string($post_id, $att_id, $filepath, $is_keep_existing_images = '')
{
$key = '_gallery'; // Edit this: Set meta key for gallery array here
$separator = ",";
$gallery = get_post_meta($post_id, $key, true);
if (is_string($gallery) || is_empty($gallery) || ($gallery == false)) {
$gallery = explode($separator, $gallery);
if (!in_array($att_id, $gallery)) {
if ($gallery[0] == '') unset($gallery[0]);
$gallery[] = $att_id;
update_post_meta($post_id, $key, implode($separator, $gallery));
}
}
}
add_action('pmxi_gallery_image', 'gallery_ids_in_string', 10, 4);