Skip to content

Commit c72532d

Browse files
committed
Fix weather and user data retrieval
1 parent 73da478 commit c72532d

11 files changed

+179
-201
lines changed

includes/class-astronomical-calculator.php

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ public function get_timestamp( $timestamp, $type = 'sunrise' ) {
120120
switch ( $type ) {
121121
case 'sunset':
122122
return $times['sunset'];
123-
$function = 'date_sunset';
124-
break;
123+
/* $function = 'date_sunset';
124+
break; */
125125
case 'moonset':
126126
$moon = $this->get_moon_times( $timestamp );
127127
return $moon['moonset'];
@@ -130,9 +130,9 @@ public function get_timestamp( $timestamp, $type = 'sunrise' ) {
130130
return $moon['moonrise'];
131131
default:
132132
return $times['sunrise'];
133-
$function = 'date_sunrise';
133+
/* $function = 'date_sunrise'; */
134134
}
135-
return call_user_func( $function, $timestamp, SUNFUNCS_RET_TIMESTAMP, $this->latitude, $this->longitude, self::get_zenith( $this->elevation ) );
135+
// return call_user_func( $function, $timestamp, SUNFUNCS_RET_TIMESTAMP, $this->latitude, $this->longitude, self::get_zenith( $this->elevation ) );
136136
}
137137

138138

@@ -675,33 +675,4 @@ public function hour_angle( $t_utc ) {
675675
}
676676
return $omega;
677677
}
678-
679-
680-
/**
681-
* Uses mean wm/2 for the last hour to estimate cloudiness.
682-
*
683-
* @param float $wm2 WM/2.
684-
* @param int $humidity Humidity.
685-
* @return int Cloudiness as a percentage.
686-
*/
687-
public function cloudiness( $wm2, $humidity ) {
688-
$clear = self::clear_sky_radiation( $this->sun_radiation( 1.0 ) );
689-
$mean = $wm2 * 0.0036;
690-
if ( $clear ) {
691-
// Return Cloudiness as a percentage number.
692-
return round( $mean / $clear * 100 );
693-
} else {
694-
// If it is nighttime you cannot tell how cloudy it is this way, therefore estimate based on humidity.
695-
if ( $humidity > 80 ) {
696-
// Humid - Lots of Clouds.
697-
return 70;
698-
} elseif ( $humidity > 40 ) {
699-
// Somewhat humid. Modest cloud clover.
700-
return 50;
701-
} else {
702-
// Low humidity no clouds.
703-
return 20;
704-
}
705-
}
706-
}
707678
}

includes/class-geo-data.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ public static function delete_geodata( $type, $id, $key ) {
222222
*/
223223
$check = apply_filters( "delete_{$type}_geodata", null, $type, $id, $key );
224224

225+
if ( ! is_null( $check ) ) {
226+
return $check;
227+
}
225228
return delete_metadata( $type, $id, 'geo_' . $key );
226229
}
227230

@@ -985,7 +988,7 @@ public static function get_location( $type, $id, $args = array() ) {
985988
'wrapper-class' => array( 'sloc-display' ), // Class or classes to wrap the entire location in
986989
'wrapper-type' => 'div', // HTML type to wrap the entire location in
987990
);
988-
$default = apply_filters( 'simple_location_display_defaults', $defaults );
991+
$defaults = apply_filters( 'simple_location_display_defaults', $defaults );
989992
$args = wp_parse_args( $args, $defaults );
990993
$args = array_merge( $loc, $args );
991994
$map = Loc_Config::map_provider();
@@ -1009,7 +1012,7 @@ public static function get_location( $type, $id, $args = array() ) {
10091012
$c[] = $args['description'];
10101013
}
10111014

1012-
if ( ( 'post' === $type ) && ( in_array( get_post_type( $id ), get_post_types_by_support( 'geo-location' ) ) ) ) {
1015+
if ( ( 'post' === $type ) && ( in_array( get_post_type( $id ), get_post_types_by_support( 'geo-location' ), true ) ) ) {
10131016
$term = Location_Taxonomy::get_post_location( $id );
10141017
$venue = Post_Venue::get_post_venue( $id );
10151018
// If by some chance there is a venue but no term set the location to the venue location.
@@ -1093,6 +1096,6 @@ public static function get_location( $type, $id, $args = array() ) {
10931096
}
10941097

10951098
$return = implode( PHP_EOL, $c );
1096-
return sprintf( '<%1s class="%2$s">%3$s</%1$s>', $args['wrapper-type'], implode( ' ', $class ), $return );
1099+
return sprintf( $wrap, $args['wrapper-type'], implode( ' ', $class ), $return );
10971100
}
10981101
}

includes/class-weather-data.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ public static function set_object_weatherdata( $type, $id, $key, $weather ) {
211211
*/
212212
$check = apply_filters( "set_{$type}_weatherdata", null, $id, $type, $key, $weather );
213213

214+
if ( ! is_null( $check ) ) {
215+
return $check;
216+
}
217+
214218
if ( $key ) {
215219
return update_metadata( $type, $id, 'weather_' . $key, $weather );
216220
}
@@ -275,6 +279,10 @@ public static function delete_object_weatherdata( $type, $id, $key ) {
275279
*/
276280
$check = apply_filters( "delete_{$type}_weatherdata", null, $type, $id, $key );
277281

282+
if ( ! is_null( $check ) ) {
283+
return $check;
284+
}
285+
278286
return delete_metadata( $type, $id, 'weather_' . $key );
279287
}
280288

includes/class-weather-provider.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function __construct( $args = array() ) {
8585
public function is_active() {
8686
$option = get_option( 'sloc_weather_provider' );
8787
$fallback = get_option( 'sloc_fallback_weather_provider' );
88-
return ( in_array( $this->slug, array( $option, $fallback ) ) );
88+
return ( in_array( $this->slug, array( $option, $fallback ), true ) );
8989
}
9090

9191
/**
@@ -133,10 +133,6 @@ public function extra_data( $return, $timestamp = null ) {
133133

134134
$return['localtime'] = $datetime->format( DATE_W3C );
135135

136-
/*
137-
if ( array_key_exists( 'radiation', $return ) && ! array_key_exists( 'cloudiness', $return ) ) {
138-
$return['cloudiness'] = $calc->cloudiness( $return['radiation'], $return['humidity'] );
139-
} */
140136
return array_filter( $return );
141137
}
142138

@@ -190,7 +186,6 @@ public function set_cache( $value ) {
190186
* @return string Cache Key.
191187
**/
192188
private function cache_key() {
193-
$key = array();
194189
if ( ! empty( $this->station_id ) ) {
195190
return implode( '_', array( get_called_class(), md5( $this->station_id ) ) );
196191
}

includes/data-functions.php

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ function set_comment_geodata( $comment, $key, $geodata ) {
2323
}
2424

2525
function set_user_geodata( $id, $key, $geodata ) {
26-
return Geo_Data::set_geodata( 'user', $id, $key, $geodata );
26+
$user = sloc_get_user( $id );
27+
if ( $user ) {
28+
return false;
29+
}
30+
return Geo_Data::set_geodata( 'user', $user->ID, $key, $geodata );
2731
}
2832

2933
function set_term_geodata( $id, $key, $geodata ) {
@@ -72,6 +76,9 @@ function get_array_post_geodata( $posts ) {
7276
*/
7377
function is_day_post( $post = null ) {
7478
$post = get_post( $post );
79+
if ( ! $post ) {
80+
return false;
81+
}
7582
$day = get_post_geodata( $post, 'day' );
7683
if ( ! empty( $day ) ) {
7784
return $day;
@@ -121,8 +128,12 @@ function is_day_comment( $comment = null ) {
121128
* Wrapper around get_geodata for user IDs or objects
122129
*
123130
*/
124-
function get_user_geodata( $user_id, $key = '' ) {
125-
return Geo_Data::get_geodata( 'user', $user_id, $key );
131+
function get_user_geodata( $user = null, $key = '' ) {
132+
$user = sloc_get_user( $user );
133+
if ( ! $user ) {
134+
return false;
135+
}
136+
return Geo_Data::get_geodata( 'user', $user->ID, $key );
126137
}
127138

128139
/*
@@ -133,27 +144,36 @@ function get_term_geodata( $term_id, $key = '' ) {
133144
return Geo_Data::get_geodata( 'term', $term_id, $key );
134145
}
135146

136-
function set_post_weatherdata( $post_id, $key, $weather ) {
137-
return Sloc_Weather_Data::set_object_weatherdata( 'post', $post_id, $key, $weather );
147+
function set_post_weatherdata( $post, $key, $weather ) {
148+
$post = get_post( $post );
149+
if ( ! $post ) {
150+
return false;
151+
}
152+
return Sloc_Weather_Data::set_object_weatherdata( 'post', $post->ID, $key, $weather );
138153
}
139154

140155
function set_comment_weatherdata( $comment_id, $key, $weather ) {
141156
return Sloc_Weather_Data::set_object_weatherdata( 'comment', $comment_id, $key, $weather );
142157
}
143158

144-
function set_user_weatherdata( $user_id, $weather ) {
145-
return Sloc_Weather_Data::set_object_weatherdata( 'user', $user_id, $key, $weather );
159+
function set_user_weatherdata( $user, $weather ) {
160+
$user = sloc_get_user( $user );
161+
if ( ! $user ) {
162+
return false;
163+
}
164+
return Sloc_Weather_Data::set_object_weatherdata( 'user', $user->ID, $key, $weather );
146165
}
147166

148167
function set_term_weatherdata( $term_id, $weather ) {
149168
return Sloc_Weather_Data::set_object_weatherdata( 'term', $term_id, $key, $weather );
150169
}
151170

152-
function get_post_weatherdata( $post_id = null, $key = '' ) {
153-
if ( ! $post_id ) {
154-
$post_id = get_the_ID();
171+
function get_post_weatherdata( $post = null, $key = '' ) {
172+
$post = get_post( $post );
173+
if ( ! $post ) {
174+
return false;
155175
}
156-
return Sloc_Weather_Data::get_object_weatherdata( 'post', $post_id, $key );
176+
return Sloc_Weather_Data::get_object_weatherdata( 'post', $post->ID, $key );
157177
}
158178

159179
function get_comment_weatherdata( $comment_id, $key = '' ) {
@@ -164,19 +184,28 @@ function get_term_weatherdata( $term_id, $key = '' ) {
164184
return Sloc_Weather_Data::get_object_weatherdata( 'term', $term_id, $key );
165185
}
166186

167-
function get_user_weatherdata( $user_id, $key = '' ) {
168-
return Sloc_Weather_Data::get_object_weatherdata( 'user', $user_id, $key );
187+
function get_user_weatherdata( $user, $key = '' ) {
188+
$user = sloc_get_user( $user );
189+
if ( ! $user ) {
190+
return false;
191+
}
192+
return Sloc_Weather_Data::get_object_weatherdata( 'user', $user->ID, $key );
169193
}
170194

171-
function get_post_map( $post_id = null, $args = array() ) {
172-
if ( ! $post_id ) {
173-
$post_id = get_the_ID();
195+
function get_post_map( $post = null, $args = array() ) {
196+
$post = get_post( $post );
197+
if ( ! $post ) {
198+
return false;
174199
}
175-
return Geo_Data::get_map( 'post', $post_id, $args );
200+
return Geo_Data::get_map( 'post', $post->ID, $args );
176201
}
177202

178-
function get_user_map( $user_id, $args = array() ) {
179-
return Geo_Data::get_map( 'user', $user_id, $args );
203+
function get_user_map( $user, $args = array() ) {
204+
$user = sloc_get_user( $user );
205+
if ( ! $user ) {
206+
return false;
207+
}
208+
return Geo_Data::get_map( 'user', $user->ID, $args );
180209
}
181210

182211
function get_comment_map( $comment_id, $args = array() ) {
@@ -195,19 +224,24 @@ function get_simple_location( $object = null, $args = array() ) {
195224
return Geo_Data::get_location( sloc_get_type_from_object( $object ), sloc_get_id_from_object( $object ), $args );
196225
}
197226

198-
function get_post_location( $id = null, $args = array() ) {
199-
if ( ! $id ) {
200-
$id = get_the_ID();
227+
function get_post_location( $post = null, $args = array() ) {
228+
$post = get_post( $post );
229+
if ( ! $post ) {
230+
return false;
201231
}
202-
return Geo_Data::get_location( 'post', $id, $args );
232+
return Geo_Data::get_location( 'post', $post->ID, $args );
203233
}
204234

205235
function get_comment_location( $id, $args = array() ) {
206236
return Geo_Data::get_location( 'comment', $id, $args );
207237
}
208238

209-
function get_user_location( $id, $args = array() ) {
210-
return Geo_Data::get_location( 'user', $id, $args );
239+
function get_user_location( $user, $args = array() ) {
240+
$user = sloc_get_user( $user );
241+
if ( ! $user ) {
242+
return false;
243+
}
244+
return Geo_Data::get_location( 'user', $user->ID, $args );
211245
}
212246

213247
function get_term_location( $id, $args = array() ) {

includes/functions.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ function sloc_get_attachment_datetime( $attachment, $field = 'created' ) {
589589
return false;
590590
}
591591

592-
if ( ! in_array( $field, array( 'created', 'date', 'modified' ) ) ) {
592+
if ( ! in_array( $field, array( 'created', 'date', 'modified' ), true ) ) {
593593
$field = 'created';
594594
}
595595

@@ -650,3 +650,10 @@ function sloc_get_contents_array( $file ) {
650650
$wpfile = new WP_Filesystem_Direct( false );
651651
return $wpfile->get_contents_array( $file );
652652
}
653+
654+
function sloc_get_user( $user_id = null ) {
655+
if ( ! is_numeric( $user_id ) ) {
656+
$user_id = get_current_user_id();
657+
}
658+
return get_user_by( 'id', $user_id );
659+
}

includes/widgets/class-sloc-airport-widget.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

33
/**
4-
* adds widget to display weather station data with per-provider support
4+
* Adds widget to display weather station data with per-provider support
55
*/
66
class Sloc_Airport_Widget extends Sloc_Weather_Widget {
77

88
/**
9-
* widget constructor
9+
* Widget constructor
1010
*/
1111
public function __construct() {
1212
WP_Widget::__construct(
@@ -21,20 +21,16 @@ public function __construct() {
2121

2222
public static function provider_list( $option, $name, $id ) {
2323
$providers = Loc_Config::weather_providers( true );
24-
if ( count( $providers ) > 1 ) {
25-
printf( '<select name="%1$s">', esc_attr( $name ) );
26-
foreach ( $providers as $key => $value ) {
27-
printf( '<option value="%1$s" %2$s>%3$s</option>', $key, selected( $option, $key ), $value ); // phpcs:ignore
28-
}
29-
echo '</select>';
30-
echo '<br /><br />';
31-
} else {
32-
printf( '<input name="%1$s" type="radio" id="%1$s" value="%2$s" checked /><span>%3$s</span>', esc_attr( $name ), esc_attr( $value ), esc_html( reset( $providers ) ) );
24+
printf( '<select name="%1$s">', esc_attr( $name ) );
25+
foreach ( $providers as $key => $value ) {
26+
printf( '<option value="%1$s" %2$s>%3$s</option>', $key, selected( $option, $key ), $value ); // phpcs:ignore
3327
}
28+
echo '</select>';
29+
echo '<br /><br />';
3430
}
3531

3632
/**
37-
* widget worker
33+
* Widget worker
3834
*
3935
* @param mixed $args widget parameters
4036
* @param mixed $instance saved widget data
@@ -86,7 +82,7 @@ public function widget( $args, $instance ) {
8682
}
8783

8884
/**
89-
* widget form
85+
* Widget form
9086
*
9187
* @param mixed $instance
9288
*

0 commit comments

Comments
 (0)