-
Notifications
You must be signed in to change notification settings - Fork 2
/
gtm_ajax.php
157 lines (129 loc) · 4.79 KB
/
gtm_ajax.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
<?php
require_once "gtm_geocode_lib.php";
// this code called directly without using the 'heartbeat' define on wpajaxurl
if (!function_exists('add_action')) {
if (!defined('ABSPATH')) {
define('ABSPATH','../../..');
}
$wp_load_location = ABSPATH .'/wp-load.php';
if (file_exists($wp_load_location)) {
require_once $wp_load_location;
if ($_REQUEST['action'] && $_REQUEST['action'] == 'gtm_get_options_values') {
ajax_get_options_values();
}
}
}
/**** AJAX ACTIONS ****/
add_action('wp_ajax_gtm_geocoded_media', 'ajax_get_geotagged_media');
add_action('wp_ajax_nopriv_gtm_geocoded_media', 'ajax_get_geotagged_media');
function ajax_get_geotagged_media()
{
header('Content-type: application/json');
echo json_encode(gtm_get_geotagged_photos());
wp_die();
}
add_action('wp_ajax_dismiss_activation_notice', 'ajax_get_dismiss_activation_notice');
function ajax_get_dismiss_activation_notice()
{
header('Content-type: application/json');
$result = update_user_meta(get_current_user_id(), "gtm_first_configure", TRUE);
echo json_encode($result);
wp_die();
}
add_action('wp_ajax_gtm_get_options_values', 'ajax_get_options_values');
function ajax_get_options_values()
{
header('Content-type: application/json');
echo json_encode(get_option('gtm_options'));
wp_die();
}
add_action('wp_ajax_gtm_download_composer', function () {
$composer_downloaded = gtm_download_composer();
if ($composer_downloaded === true) {
echo "Composer downloaded with sucess!";
} elseif ($composer_downloaded == null) {
echo "Composer already downloaded!";
} else {
echo "Composer failed to download!";
}
echo "<P>Now that composer was downloaded, click the following button to get the dependencies: <BUTTON class='button' id='btn_composer_init_vendor'>Click here to download dependencies</BUTTON></P>";
wp_die();
});
add_action('wp_ajax_gtm_install_deps', function () {
require_once("gtm_install_deps.php");
if (!gtm_does_vendor_dir_exists()) {
$composer_output = gtm_install_deps();
echo $composer_output;
echo "Dependencies installed with success!";
} else {
echo "Dependencies already installed!";
}
wp_die();
});
add_action('wp_ajax_gtm_html_url', function () {
// debug(__FUNCTION__ . " request:", $_REQUEST);
$url = urldecode($_REQUEST['url']);
echo file_get_contents(home_url() . "/$url");
wp_die();
});
function ajax_gtm_get_mapsources_keys()
{
$gtm_options = get_option('gtm_options');
header('Content-type: application/json');
echo json_encode(array(
'key_bingmaps' => $gtm_options['key_bingmaps'],
'key_thunderforest' => $gtm_options['key_thunderforest'],
'key_mapbox' => $gtm_options['key_mapbox']
));
wp_die();
}
add_action('wp_ajax_gtm_get_mapsources_keys', 'ajax_gtm_get_mapsources_keys');
add_action('wp_ajax_nopriv_gtm_get_mapsources_keys', 'ajax_gtm_get_mapsources_keys');
add_action('wp_ajax_gtm_geomark', function () {
$coordinates = $_REQUEST['coordinates'];
$post_id = $_REQUEST['post_id'];
$md = wp_get_attachment_metadata($post_id);
$original_md = $md;
$lat_r = gtm_coord_dec_to_dms($coordinates[1]);
$long_r = gtm_coord_dec_to_dms($coordinates[0]);
$md['image_meta']['latitude'] = gtm_exif_format_dms($lat_r);
$md['image_meta']['longitude'] = gtm_exif_format_dms($long_r);
$md['image_meta']['latitude_ref'] = gtm_orientation_char($coordinates[1], 'lat');
$md['image_meta']['longitude_ref'] = gtm_orientation_char($coordinates[0], 'long');
$success = wp_update_attachment_metadata(
$post_id,
$md
);
// FIXME remove when the geotag is being properly stored!
header('Content-type: application/json');
echo json_encode(array('success' => $success));
wp_die();
});
add_action('wp_ajax_getcoord', function () {
header('Content-type: application/json');
$image_md = wp_get_attachment_metadata($_REQUEST['post_id']);
if ( empty( $image_md['image_meta']['latitude'] ) ) {
echo json_encode('');
} else {
$md = $image_md['image_meta'];
$lat_dec = gtm_geo_dms2dec($md['latitude'], $md['latitude_ref']);
$long_dec = gtm_geo_dms2dec($md['longitude'], $md['longitude_ref']);
// debug(__METHOD__, array($lat_dec, $long_dec));
echo json_encode([$long_dec, $lat_dec]);
}
wp_die();
});
add_action( 'wp_ajax_geocode_search', function () {
header( 'Content-type: application/json' );
$geoname = $_REQUEST['geoname'];
$results = gtm_geocode( $geoname );
$names = array_map( function ( $result ) {
return array(
'name' => $result['name'],
'lat' => $result['latitude'],
'long' => $result['longitude']
);
}, $results );
echo json_encode( $names );
wp_die();
} );