Description
Implement [bw_tides] shortcode for UK-tidesDisplay information about high and low tides obtained from www.tidetimes.org.uk or www.tidetimes.co.uk The data is stored as transient data until midnight, after which we expect new figures for the next day. If the site is going to display more than one set of tide information then you will need to indicate a special code for storing the information. I would have liked to have extracted the location from the tideurl but got distracted with set_transient crashing when passed a SimpleXML object.
Usage
$string = bw_tides( $atts, $content, $tag );Parameters
- $atts
- ( array ) optional – shortcode parameters
- $content
- ( string ) optional – not expected
- $tag
- ( string ) optional – the shortcode
Returns
string the generated HTMLSource
File name: uk-tides/shortcodes/uk-tides.phpLines:
1 to 64 of 64
function bw_tides( $atts=null, $content=null, $tag=null ) { $tideurl = bw_array_get( $atts, "tideurl", "http://www.tidetimes.org.uk/chichester-harbour-entrance-tide-times.rss" ); $tideurl = bw_tideurl_namify( $tideurl ); $store = bw_array_get( $atts, "store", "1" ); $force = bw_array_get( $atts, "force", null ); bw_trace2( $tideurl, 'tideurl', true, BW_TRACE_DEBUG ); bw_trace2( $store, 'store', false, BW_TRACE_VERBOSE ); $desc = get_transient( 'bw_tides_desc_'. $store ); $title = get_transient( 'bw_tides_title_'. $store ); $link = get_transient( 'bw_tides_link_'. $store ); if ( $desc === FALSE || $title === FALSE || $link === FALSE || $force ) { $tideinfo = bw_get_tide_info( $tideurl ); if ( is_wp_error( $tideinfo ) || !$tideinfo || !$tideinfo->channel ) { p( "Tide times and heights not available for $tideurl" ); } else { $channel = $tideinfo->channel; bw_trace2( (string) $channel, 'channel', false, BW_TRACE_DEBUG ); $link = (string) $channel->link; /* cast to a string since otherwise there can be a problem with attempting to serialise a simpleXML element */ $desc = (string) $channel->item->description; bw_trace2( $desc, 'desc', false, BW_TRACE_DEBUG ); /* We may need to strip some unwanted advertising which appears in an anchor tag <a */ /* $desc = preg_replace('/<a (.*?)<\/a>/', "\\2", $desc); $allowed = array( 'b' => array(), 'br' => array() ); $desc = wp_kses( $desc, $allowed ); */ $title = (string) $channel->item->title; // $title = $channel->item->title; uncomment this to cause set_transient to fail $secs = bw_time_of_day_secs(); $secs = 86400 - $secs; set_transient( "bw_tides_desc_" . $store, $desc, $secs); set_transient( "bw_tides_title_" . $store, $title, $secs); set_transient( "bw_tides_link_" . $store, $link, $secs); } } else { // We got all the data from the transient store } bw_trace2( $desc, 'desc', false, BW_TRACE_DEBUG ); bw_trace2( $title, 'title', false, BW_TRACE_DEBUG ); bw_trace2( $link, 'link', false, BW_TRACE_DEBUG ); // Now that tidetimes.org.uk creates the links itself we only need to display the informaton in span // with class tides, to allow for custom CSS styling //alink( "tides", $link , $desc , $title ); $css_class = bw_get_css_classname( $store ); span( "bw_tides $css_class" ); bw_tides_format_desc( $desc ); epan( "bw_tides" ); return( bw_ret()); }View on GitHub View on Trac
Invoked by
Calls
1 to 10 of 10
- bw_array_get() – Return the array[index] or array->index (for an object) or a default value if not set
- bw_get_css_classname() – Return a CSS class name for the given value
- bw_get_tide_info() – Gets tide information from the $tide_url
- bw_ret() – Use bw_ret() to return the contents of $bwecho, leaving the global value as NULL
- bw_tides_format_desc() – Format the description
- bw_tideurl_namify() – Form an URL for the given location assuming UK based
- bw_time_of_day_secs() – Return the time of day in seconds
- bw_trace2() – Trace $value to the trace log file if tracing is active
- epan() –
- span() –
