From 24534efd703dce7b0df1314fd1576709e1c0359b Mon Sep 17 00:00:00 2001 From: Nick Stolwijk Date: Tue, 16 May 2023 18:06:24 +0200 Subject: [PATCH] Add delay option to live tracking. --- README.md | 4 ++++ class-trackserver-shortcode.php | 29 +++++++++++++++++++++-------- readme.txt | 1 + trackserver.php | 2 +- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 69ed4f4..5083e01 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,10 @@ For the [tsmap] shortcode: If this parameter is given, all user tracks that have not been updated in the last X amount of time, will not be displayed. The value is a time expression in the form of a number and a unit, for example: '120s', '30m', '2h', '3d'. +* **delay**: the delay of a live track for its locations to appear on the map. + If this parameter is given, all user tracks only show their data up to a point + in time X amount ago. The value is a time expression in + the form of a number and a unit, for example: '120s', '30m', '2h', '3d'. * **width**: map width, default: 100%. * **height**: map height, default: 480px. * **align**: 'left', 'center' or 'right', default: not set. diff --git a/class-trackserver-shortcode.php b/class-trackserver-shortcode.php index 0ff689e..a677b5d 100644 --- a/class-trackserver-shortcode.php +++ b/class-trackserver-shortcode.php @@ -86,6 +86,7 @@ public function handle_shortcode1( $atts ) { 'points' => false, 'zoom' => false, 'maxage' => false, + 'delay' => false, ); $atts = shortcode_atts( $defaults, $atts, $this->shortcode1 ); @@ -117,7 +118,8 @@ public function handle_shortcode1( $atts ) { $points = $this->get_points( $atts, false ); // result is not used $markers = $this->get_markers( $atts, false ); // result is not used $markersize = $this->get_markersize( $atts, false ); // result is not used - $maxage = $this->get_maxage( $atts['maxage'] ); + $maxage = $this->get_seconds( $atts['maxage'] ); + $delay = $this->get_seconds( $atts['delay'] ); list( $validated_track_ids, $validated_user_ids ) = $this->validate_ids( $atts ); @@ -144,7 +146,7 @@ public function handle_shortcode1( $atts ) { $query = base64_encode( $query ); $query_nonce = wp_create_nonce( 'gettrack_' . $query . '_p' . $post_id ); $alltracks_url = $gettrack_url_prefix . '?query=' . rawurlencode( $query ) . "&p=$post_id&format=" . - $this->trackserver->track_format . "&maxage=$maxage&_wpnonce=$query_nonce"; + $this->trackserver->track_format . "&maxage=$maxage&delay=$delay&_wpnonce=$query_nonce"; $following = false; //foreach ( $validated_track_ids as $validated_id ) { @@ -189,7 +191,7 @@ public function handle_shortcode1( $atts ) { $trk['track_type'] = 'polylinexhr'; } $trk['track_url'] = $gettrack_url_prefix . '?id=' . $validated_id . "&p=$post_id&format=" . - $this->trackserver->track_format . "&maxage=$maxage&_wpnonce=$nonce"; + $this->trackserver->track_format . "&maxage=$maxage&delay=$delay&_wpnonce=$nonce"; } $tracks[] = $trk; @@ -327,6 +329,7 @@ public function handle_shortcode3( $atts, $content = '' ) { 'user' => false, 'format' => 'gpx', 'maxage' => false, + 'delay' => false, 'href_only' => false, ); @@ -343,7 +346,8 @@ public function handle_shortcode3( $atts, $content = '' ) { $atts['track'] = $atts['id']; } - $maxage = $this->get_maxage( $atts['maxage'] ); + $maxage = $this->get_seconds( $atts['maxage'] ); + $delay = $this->get_seconds( $atts['delay'] ); list( $validated_track_ids, $validated_user_ids ) = $this->validate_ids( $atts ); @@ -364,7 +368,7 @@ public function handle_shortcode3( $atts, $content = '' ) { ); $query = base64_encode( $query ); $query_nonce = wp_create_nonce( 'gettrack_' . $query . '_p' . $post_id ); - $alltracks_url = get_home_url( null, $this->trackserver->url_prefix . '/' . $this->trackserver->options['gettrack_slug'] . '/?query=' . rawurlencode( $query ) . "&p=$post_id&format=$track_format&maxage=$maxage&_wpnonce=$query_nonce" ); + $alltracks_url = get_home_url( null, $this->trackserver->url_prefix . '/' . $this->trackserver->options['gettrack_slug'] . '/?query=' . rawurlencode( $query ) . "&p=$post_id&format=$track_format&maxage=$maxage&delay=$delay&_wpnonce=$query_nonce" ); $text = $atts['text'] . $content; if ( $text === '' ) { @@ -521,13 +525,13 @@ private function get_markersize( $atts = false, $shift = true ) { } /** - * Return maxage in seconds for a time expression + * Return seconds for a time expression * * Takes an expression like 120s, 5m, 3h, 7d and turns it into seconds. No unit equals seconds. * * @since 3.1 */ - private function get_maxage( $str ) { + private function get_seconds( $str ) { if ( $str === false ) { return 0; } @@ -642,6 +646,7 @@ private function handle_gettrack_query() { $query_string = stripslashes( $_REQUEST['query'] ); $maxage = ( isset( $_REQUEST['maxage'] ) ? (int) $_REQUEST['maxage'] : 0 ); + $delay = ( isset( $_REQUEST['delay'] ) ? (int) $_REQUEST['delay'] : 0 ); $post_id = ( isset( $_REQUEST['p'] ) ? intval( $_REQUEST['p'] ) : 0 ); $format = $_REQUEST['format']; $is_admin = array_key_exists( 'admin', $_REQUEST ) ? true : false; @@ -676,7 +681,15 @@ private function handle_gettrack_query() { // @codingStandardsIgnoreStart $sql_in = "('" . implode( "','", $track_ids ) . "')"; $sql = 'SELECT trip_id, latitude, longitude, altitude, speed, occurred, t.user_id, t.name, t.distance, t.comment FROM ' . $this->trackserver->tbl_locations . - ' l INNER JOIN ' . $this->trackserver->tbl_tracks . ' t ON l.trip_id = t.id WHERE trip_id IN ' . $sql_in . ' AND l.hidden = 0 ORDER BY trip_id, occurred'; + ' l INNER JOIN ' . $this->trackserver->tbl_tracks . ' t ON l.trip_id = t.id WHERE trip_id IN ' . $sql_in . ' AND l.hidden = 0'; + + if ( $delay > 0 ) { + $ts = gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) - $delay ) ); + $sql .= " AND l.occurred < '$ts' "; + } + + $sql .= " ORDER BY trip_id, occurred"; + $res = $wpdb->get_results( $sql, ARRAY_A ); // @codingStandardsIgnoreEnd diff --git a/readme.txt b/readme.txt index e2091da..8e67224 100644 --- a/readme.txt +++ b/readme.txt @@ -89,6 +89,7 @@ For the [tsmap] shortcode: * user: one or more user IDs, separated by commas, who's latest track to follow 'live'. A literal '@' means the author of the post (you). When viewing the map, the liveupdate feature will follow the track of the first user specified. When the end-marker is enabled for a live track (and why shouldn't it?), clicking it will change the focus of the liveupdate to that track. The map view will follow the latest location and the infobar (if present) will display its information. * live: true (or 't', 'yes' or 'y'), or false (default), to force live tracking for this map. This can be used for example with an externally updated GPX or KML file. * maxage: the maximum age of a live track for it to be included on the map. If this parameter is given, all user tracks that have not been updated in the last X amount of time, will not be displayed. The value is a time expression in the form of a number and a unit, for example: '120s', '30m', '2h', '3d'. +* delay: the delay of a live track for its locations to appear on the map. If this parameter is given, all user tracks only show their data up to a point in time X amount ago. The value is a time expression in the form of a number and a unit, for example: '120s', '30m', '2h', '3d'. * width: map width, default: 100%. * height: map height, default: 480px. * align: 'left', 'center' or 'right', default: not set. diff --git a/trackserver.php b/trackserver.php index def7e66..8480315 100644 --- a/trackserver.php +++ b/trackserver.php @@ -53,7 +53,7 @@ define( 'TRACKSERVER_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); define( 'TRACKSERVER_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); define( 'TRACKSERVER_JSLIB', TRACKSERVER_PLUGIN_URL . 'lib/' ); -define( 'TRACKSERVER_VERSION', '4.3-20190906' ); +define( 'TRACKSERVER_VERSION', '5.0.2-20230305' ); require_once( TRACKSERVER_PLUGIN_DIR . 'class-trackserver.php' );