-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherpress.php
274 lines (234 loc) · 7.71 KB
/
weatherpress.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
<?php
/*
Plugin Name: Weatherpress
Plugin URI: https:/https://github.com/NRubn/weatherpress/
Description: Wetterdaten auf deine Wordpresseite
Version: 1.1.1
Author: Ruben
Author URI: https://google.de/
License: GPL-2.0+
License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
//$openweathermapkey = '';
$file_path = 'openweathermap.txt';
if (file_exists($file_path)) {
$openweathermapkey = file_get_contents($file_path);
}else{
$openweathermapkey = 'PLEASE INSERT API KEY';
}
function weatherpress_add_menu_page() {
add_menu_page(
'Weatherpress Settings', // Seitentitel
'Weatherpress', // Menütitel
'manage_options', // Berechtigungslevel
'weatherpress-settings', // Slug der Seite
'weatherpress_settings_page', // Callback-Funktion zum Rendern der Seite
'dashicons-palmtree', // Icon-URL
10 // Position im Menü
);
}
add_action( 'admin_menu', 'weatherpress_add_menu_page' );
// Hier kommen die Funktionen des Plugins
// Funktion zum Aktivieren des Plugins
function weatherpress_activate() {
// Code zum Aktivieren des Plugins
}
register_activation_hook( __FILE__, 'weatherpress_activate_activate' );
// Funktion zum Deaktivieren des Plugins
function weatherpress_activate_deactivate() {
// Code zum Deaktivieren des Plugins
}
register_deactivation_hook( __FILE__, 'weatherpress_activate_deactivate' );
// Funktion zum Entfernen des Plugins
function weatherpress_activate_uninstall() {
// Code zum Entfernen des Plugins
}
register_uninstall_hook( __FILE__, 'mein_plugin_uninstall' );
function weatherpress_settings_page() {
global $openweathermapkey;
echo "Start";
if ( isset( $_POST['city_name'] ) ) {
echo 'City: '.$_POST['city_name'].'<br>';
$city_name = $_POST['city_name'];
} else {
$city_name = 'Dortmund';
}
if ( isset( $_POST['icon'] ) ) {
echo 'icon: '.$_POST['icon'].'<br>';
$icon = $_POST['icon'];
} else {
$icon = 'openweathermap';
}
if (isset($_POST['Openweathermapkey'])) {
$filename = 'openweathermap.txt';
$handle = fopen($filename, 'w');
fwrite($handle, $_POST['Openweathermapkey']);
fclose($handle);
echo "Der OpenWeatherMap API-Schlüssel wurde erfolgreich in der Datei $filename gespeichert.";
}
?>
<div class="wrap">
<h1>My Plugin Settings</h1>
<h2>OpenWeatherMap API-Schlüssel</h2>
<form method="POST">
<label for="Openweathermapkey">OpenWeatherMap API-Schlüssel:</label>
<input type="text" name="Openweathermapkey" value="<?php echo $openweathermapkey; ?>">
<input type="submit" value="Speichern">
</form>
<h2>Stadt auswählen</h2>
<form method="post" action="admin.php?page=weatherpress-settings">
<table class="form-table">
<tr valign="top">
<th scope="row"><span class="dashicons dashicons-location-alt"></span>Stadt:</th>
<td><input type="text" name="city_name" value="<?php echo $city_name ?>" /></td>
</tr>
<tr valign="top">
<th scope="row"><span class="dashicons dashicons-admin-site"></span>Icon:</th>
<td>
<select name="icon">
<option value="openweathermap" <?php selected($icon, 'openweathermap'); ?>>openweathermap</option>
<option value="emoji" <?php selected($icon, 'emoji'); ?>>emojie</option>
<option value="none" <?php selected($icon, 'none'); ?>>none</option>
</select>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
<br>
<p>Shortcode:<div id="shortcode">[weatherpress city="<?php echo $city_name ?>" unit="metric" icon="<?php echo $icon ?>"]</div></p>
<button onclick="copyToClipboard()">In Zwischenablage kopieren</button>
<br>
</div>
<?php
$citydata = citydatacurl($city_name);
$citylat = $citydata[0]['lat'];
$citylon = $citydata[0]['lon'];
$unit = 'metric';
$weatherdata = weathercurl($citylat, $citylon);
$html = generateweatheroutput($city_name, $unit, $icon);
$html .= '<script>function copyToClipboard() {
var copyText = document.getElementById("shortcode");
var range = document.createRange();
range.selectNode(copyText);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
document.execCommand("copy");
}
</script>';
echo '<br>';
echo $html;
}
function citydatacurl($city_name){
global $openweathermapkey;
$url = 'https://api.openweathermap.org/geo/1.0/direct?q='.$city_name.'&appid='.$openweathermapkey;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = curl_exec($curl);
$responsejson = json_decode($response, true);
curl_close($curl);
return $responsejson;
}
function weathercurl($lat, $lon, $unit = 'metric'){
global $openweathermapkey;
$url = 'https://api.openweathermap.org/data/2.5/weather?lat='.$lat.'&lon='.$lon.'&appid='.$openweathermapkey.'&units='.$unit.'&lang=de';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = curl_exec($curl);
$responsejson = json_decode($response, true);
curl_close($curl);
return $responsejson;
}
//Shortcode
function weatherpress_shortcode( $atts ) {
// Verarbeite Shortcode-Attribute
$atts = shortcode_atts( array(
'city' => 'Dortmund',
'unit' => 'metric',
'icon' => 'openweathermap'
), $atts );
$city_name = $atts['city'];
$unit = $atts['unit'];
$icon = $atts['icon'];
$output = '';
$output = generateweatheroutput($city_name, $unit, $icon);
return $output;
}
// [weatherpress city="Your City" unit="metric" icon="true"]
add_shortcode( 'weatherpress', 'weatherpress_shortcode' );
// HTML erstellen:
function generateweatheroutput($city = "dortmund", $unit = "metric", $icon = "openweathermap"){
$output = '';
$citydata = citydatacurl($city);
$citylat = $citydata[0]['lat'];
$citylon = $citydata[0]['lon'];
$weatherdata = weathercurl($citylat, $citylon, $unit);
// Füge den Shortcode-Inhalt hinzu
$sky = $weatherdata['weather'][0]['description'];
$skyicon = $weatherdata['weather'][0]['icon'];
$temp = $weatherdata['main']['temp'];
//Prüfen ob openweathermap.org Icon erlaubt ist
switch ($icon) {
case 'openweathermap';
$weathericon = '<img class="weatherpressicon" width="50" height="50" src="https://openweathermap.org/img/wn/'.$skyicon.'@2x.png">';
break;
case 'emojie';
$weathericon = ' 🌡️';
break;
case 'none';
$weathericon = ' ️';
break;
}
if($temp<= 7){
$itis ='cold';
}elseif($temp>= 20){
$itis ='hot';
}else{
$itis ='normal';
};
$output = '<div class="weatherpressapi '.$itis.'"><b>Das Wetter</b>';
$output .= '<br>';
$output .= '<div class="weatherpresscity">Stadt: '.$city.'</div>';
$output .= '<div class="weatherpresssky">Himmel: '.$sky.$weathericon.'</div>';
$output .= '<div class="weatherpresstemp">Temperatur: '.$temp.'</div>';
$output .= '</div>';
$output .= '<style>
.weatherpresssky img.weatherpressicon{
margin: -5px 0px 0px 10px;
border-radius: 50%;
margin: 0 10px;
}
.weatherpressapi{
padding: 5px;
}
.weatherpressapi.cold {
background: #0000ff3d;
}
.weatherpressapi.hot {
background: #ff00003d;
}
.weatherpresscity {
text-transform: capitalize;
}
</style>';
return $output;
};
?>