-
Notifications
You must be signed in to change notification settings - Fork 2
/
gtm_helpers.php
446 lines (360 loc) · 11.8 KB
/
gtm_helpers.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php
/*** HELPERS ***/
function gtm_debug_query($results, $wp_query)
{
// d(__FUNCTION__ . " query:", $wp_query->request);
return $results;
}
function gtm_gmaps_link($lat_dec, $long_dec)
{
return "<li class='misc-pub-section'><A href='" . gtm_gmaps_url($lat_dec, $long_dec) . "' target='_blank'>Show it on Google Maps (opens in new window)</A></li>";
}
function gtm_gmaps_url($lat_dec, $long_dec)
{
return "//www.google.com/maps/search/?api=1&query=$lat_dec,$long_dec";
}
function gtm_format_md($label, $value)
{
return "<P>$label:<STRONG>$value</STRONG></P>";
}
function gtm_mime_type_image($where, $wp_query)
{
$where .= " AND post_mime_type LIKE 'image%' ";
return $where;
}
function gtm_extract_geodata_from_post($posts)
{
global $i;
$i = 0;
return array_map(function ($post) {
$md = $post->metadata['image_meta'];
$image_sizes = $post->metadata['sizes'];
$image_location = preg_split('/\//', $post->metadata['file']);
$image_location = join('/', array_slice($image_location, 0, 2));
global $i;
if ($i == 0) {
$i = 1;
}
if (!empty($image_sizes['thumbnail'])) {
$thumbnail_filename = $image_location . '/' . $image_sizes['thumbnail']['file'];
$media_dir = wp_upload_dir();
$media_dir = $media_dir['basedir'];
if (!file_exists($media_dir . "/$thumbnail_filename")) {
$thumbnail_filename = '';
}
} else {
debug( $post->post_title . "has no thumb!" );
$thumbnail_filename = 'no-picture.jpg';
}
return array(
'title' => $post->post_title,
'latitude' => (($md['latitude_ref'] == 'S') ? "-" : "") . gtm_geo_dms2dec($md['latitude']),
'longitude' => (($md['longitude_ref'] == 'W') ? "-" : "") . gtm_geo_dms2dec($md['longitude']),
'thumbnail' => $thumbnail_filename,
'post_id' => $post->ID
);
}, $posts);
}
function gtm_geotag_media($md, $coordinates)
{
$lat_dec = doubleval($coordinates[1]);
$long_dec = doubleval($coordinates[0]);
$id = $md['image_data'];
}
/**
* convert decimal coordinates into degree-minite-format
* @param $coord_dec
* @return array
*/
function gtm_coord_dec_to_dms($coord_dec)
{
$coord_dec = abs($coord_dec);
$deg = intval($coord_dec);
$min = intval(($coord_dec - $deg) * 60);
$sec = ($coord_dec - $deg - ($min / 60.0)) * (60.0 * 60.0);
return array($deg, $min, $sec);
}
function gtm_html_format_dms($r_dms, $type)
{
$deg = $r_dms[0];
$min = $r_dms[1];
$sec = round($r_dms[2], 4);
$dir_char = "";
return " {$deg}° {$min}' {$sec}\" " . gtm_orientation_char($deg, $type);
}
function gtm_orientation_char($coord, $type)
{
if ($coord >= 0) {
switch ($type) {
case 'lat':
return 'N';
break;
case 'long':
return 'E';
break;
}
} else {
switch ($type) {
case 'lat':
return 'S';
break;
case 'long':
return 'W';
break;
}
}
}
/**
* convert coordinates from dms into the format used by the exif data
* @param $r_dms
* @return array
*/
function gtm_exif_format_dms($r_dms)
{
return array(
$deg = $r_dms[0] . "/1",
$min = $r_dms[1] . "/1",
$sec = (round($r_dms[2], 4) * 10000) . "/10000"
);
}
function gtm_field_for_form($label, $value, $post_id)
{
return array(
'value' => $value,
'label' => ucfirst($label),
'html' => "<INPUT type='text' class='text' id='attachments-$post_id-$label' readonly='readonly' name='[attachments][$post_id][$label]' value='$value'>"
);
}
function gtm_geo_frac2dec($str)
{
@list($n, $d) = explode('/', $str);
if (!empty($d)) {
return $n / $d;
}
return $str;
}
function gtm_geo_pretty_fracs2dec($fracs)
{
return gtm_geo_frac2dec($fracs[0]) . '° ' .
gtm_geo_frac2dec($fracs[1]) . '′ ' .
gtm_geo_frac2dec($fracs[2]) . '″ ';
}
/**
* @param $fracs
* @param string $coord_ref the direction of the coordinate (south, west)
* @return float|int
*/
function gtm_geo_dms2dec($fracs, $coord_ref = '')
{
list($deg, $frac) = preg_split("/\//", $fracs[0]);
$deg = intval($deg);
list($min, $frac) = preg_split("/\//", $fracs[1]);
$min = intval($min);
list($sec, $frac) = preg_split("/\//", $fracs[2]);
$sec = floatval($sec) / floatval($frac);
$final = $deg + $min * (1.0 / 60.0) + $sec * (1.0 / (60.0 * 60.0));
if ($coord_ref == 'S' || $coord_ref == 'W') {
$final = -1.0 * $final;
}
return $final;
}
/**
* generates html SCRIPT tags for a js library, along with all its dependencies
* @param $scripts_r an array with the registered name of the library (as used by wp_register_script)
* @return string SCRIPT tags for all the dependencies of the library
*/
function gtm_output_scripts_html($scripts_r)
{
$script_locations = gtm_registered_scripts_locations($scripts_r);
$buf = '';
foreach ($script_locations as $loc) {
$buf .= "<script src='" . site_url() . $loc . "'></script>\n";
}
return $buf;
}
function gtm_registered_scripts_locations($scripts_r)
{
global $wp_scripts;
$registered_scripts = $wp_scripts->registered;
$all_deps = array();
foreach ($scripts_r as $script) {
if (isset($registered_scripts[$script])) {
$this_script = $registered_scripts[$script];
$all_deps[] = $this_script->handle;
$all_deps = array_merge($all_deps, $this_script->deps);
}
}
$scripts_locs = array();
foreach ($all_deps as $scr_key) {
$script = $registered_scripts[$scr_key];
if ($script->src) {
$scripts_locs[] = $script->src;
}
}
return array_unique($scripts_locs);
}
function dump_all_styles()
{
global $wp_styles;
$registered_styles = $wp_styles->registered;
$style_names = array_keys($registered_styles);
sort($style_names);
dump($style_names);
}
function gtm_output_styles_html($styles_r)
{
$style_locations = gtm_registered_styles_locations($styles_r);
$buf = '';
foreach ($style_locations as $loc) {
$buf .= "<link rel=\"stylesheet\" href='" . site_url() . "$loc. ' type='text/css'/>\n";
}
return $buf;
}
function gtm_registered_styles_locations($styles_r)
{
global $wp_styles;
$registered_styles = $wp_styles->registered;
$all_deps = array();
foreach ($styles_r as $style) {
if (isset($registered_styles[$style])) {
$this_style = $registered_styles[$style];
$all_deps[] = $this_style->handle;
$all_deps = array_merge($all_deps, $this_style->deps);
}
}
$styles_locs = array();
foreach ($all_deps as $scr_key) {
$style = $registered_styles[$scr_key];
$styles_locs[] = $style->src;
}
return array_unique($styles_locs);
}
function gtm_composer_phar_exists()
{
return file_exists(__DIR__ . '/' . 'composer.phar');
}
function gtm_does_vendor_dir_exists()
{
$plugin_dir = plugin_dir_path(__FILE__);
return (file_exists("$plugin_dir/vendor") && is_dir("$plugin_dir/vendor"));
}
function gtm_download_composer()
{
$composer_download_url = 'https://getcomposer.org/composer.phar';
if (gtm_composer_phar_exists()) {
return null;
}
echo "<P>Downloading $composer_download_url ... </P>";
$ch = curl_init($composer_download_url);
$plugin_path = plugin_dir_path(__FILE__);
$fp = fopen("$plugin_path/composer.phar", 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
echo "<P>Composer Download complete! </P>";
return true;
}
function gtm_get_geotagged_photos()
{
$args = array(
'post_type' => 'attachment',
'post_status' => 'any',
'orderby' => 'date',
'order' => 'DESC',
'nopaging' => true,
);
if (!empty($_REQUEST['taxonomy'])) {
$cat_id = get_cat_ID($_REQUEST['taxonomy']);
// debug('cat_id', $cat_id);
$args['cat'] = $cat_id;
}
if (!empty($_REQUEST['tags'])) {
$tag_tokens = preg_split('/,/',$_REQUEST['tags']);
if (count($tag_tokens) > 0) {
// $args['tags'] = join("+",$tag_tokens);
// debug('tag_tokens',$tag_tokens);
// $tag_tokens[] = $tag_data->term_id;
$tag_data = array();
foreach ($tag_tokens as $tag_token) {
$a_tag = get_term_by('name',$tag_token,'post_tag');
$tag_data[] = $a_tag->term_id;
}
$args['tag__and'] = $tag_data;
}
}
// debug("query args", $args);
$query = new WP_Query($args);
// debug("after instantiation", $query);
add_filter('posts_where', 'gtm_mime_type_image', 10, 2);
add_action('the_post', 'gtm_add_metadata_field');
$images_only_geodata = null;
// debug("after filters", $query);
$geocoded_images = array();
// debug(__FUNCTION__ . " query:", $query->request);
//d($query->request);
if ($query->have_posts()) {
$posts = $query->get_posts();
foreach ($posts as $post) {
$md = wp_get_attachment_metadata($post->ID);
if ($md && !empty($md['image_meta']['latitude'])) {
$post->metadata = $md;
$geocoded_images[] = $post;
}
}
echo "";
$images_only_geodata = gtm_extract_geodata_from_post($geocoded_images);
}
remove_filter('posts_where', 'gtm_mime_type_image');
return array($images_only_geodata, $query->post_count);
}
function gtm_add_metadata_field(&$postObj)
{
$postObj->metadata = wp_get_attachment_metadata($postObj->ID);
return $postObj;
}
function gtm_category_names_for_geotagged_photos()
{
global $wpdb;
$results = $wpdb->get_results("select wp_terms.name, wp_terms.slug from wp_term_taxonomy,wp_term_relationships, wp_posts, wp_terms where wp_term_relationships.object_id = wp_posts.ID and post_type='attachment' and wp_term_taxonomy .term_taxonomy_id = wp_term_relationships.term_taxonomy_id and wp_terms.term_id = wp_term_taxonomy.term_id group by wp_terms.name ");
$categories = array();
foreach ($results as $result) {
$categories[$result->slug] = $result->name;
}
//d($categories);
return $categories;
}
/**
* fix broken image metadata so that thumbs can be regenerated
* @see https://snippets.webaware.com.au/snippets/repair-wordpress-image-meta/
*/
function gtm_repair_image_meta($image_post_id) {
global $wpdb;
$image_filepath = get_attached_file($image_post_id);
$image_filename = basename($image_filepath);
$meta = wp_get_attachment_metadata($image_post_id);
// if (!$meta) {
$media_upload_dir = wp_get_upload_dir();
// if (!empty($file_filepath)) {
$info = getimagesize($image_filepath);
$meta = array (
'width' => $info[0],
'height' => $info[1],
'hwstring_small' => "height='{$info[1]}' width='{$info[0]}'",
'file' => $media_upload_dir['subdir'] . "/$image_filename",
// $media_upload_dir['path'] . '/' . $image_filename ,
'sizes' => array(), // thumbnails etc.
'image_meta' => array(), // EXIF data
);
update_post_meta($image_post_id, '_wp_attachment_metadata', $meta);
// }
// }
}
function gtm_media_image_file($image_post_id) {
$image_postdata = get_post($image_post_id);
$image_metadata = wp_get_attachment_metadata($image_post_id);
$media_upload_dir = wp_get_upload_dir();
$absfilepath = $media_upload_dir['basedir'] .'/'. $image_metadata['file'];
return $absfilepath;
}