Serializing URL via wp add_shortcode to auto select from option list onload

Scope:
Use of the wordpress add_shortcode method to use url string to automatically select options in a scraped table on this page (may require couple of refreshes):

http://tenhealthandfitness.com/proto/personal-training-timetable/

Aim is to pass in something like this to the url to auto select an option onLoad:

http://tenhealthandfitness.com/proto/personal-training-timetable/?action=go&staff[]=Adam%20Cooper

Background:
The tech is a scraped appointment widget from Healcode, and in the wp template the option drops are dynamically generated by including things like this:

<label>instructors</label>
   <?=$schedule->getElement("staff"); ?>

This then calls the parser which has the source:

<?php

require_once("simple_html_dom.php");

class schedule{

    protected $_content;
    protected $_dom;
    protected $_schedule;
    protected $_scheduleWidget = "https://widgets.healcode.com/widgets/mb/appointments/c81156841c2.js?mobile=false";

    protected $_cacheFolder;


    /**
     * when the class is first called it sets the cache folder and tries to get the timetable (most of the time from the cache)
     *
     * @param string $cacheFolder
     */
    function __construct($cacheFolder,$date=null){
        $this->_cacheFolder = $cacheFolder;
        $this->_schedule = $this->getSchedule($this->_scheduleWidget);
    }


    function saveSchedule($data,$startDate){
        // set TTL (1800 seconds/30 mins) and create cache filename with timestamp
        $ttl = 1800;
        $cacheFilename = "-cacheschedule-".str_replace("-","",$startDate)."-_".$ttl . '_' . time();
        file_put_contents($this->_cacheFolder.$cacheFilename, serialize($data));
    }


    function getSchedule($url){
        $found = false;
        $date = date("Ymd");
        foreach (scandir($this->_cacheFolder) as $file) {
            if(strpos($file,"schedule-".$date)>0){
                $info = explode("_",$file);
                $expiry = $info[1]+$info[2];
                $currentTime = time();
                if($expiry >= $currentTime){
                    $found = $file;
                    break;
                }
            }
        }
        if(!$found){
            return $this->parseSchedule($date);
        } else {
            return unserialize(file_get_contents($this->_cacheFolder."/".$found));

        }
    }


    function search($params){
        $url = "https://widgets.healcode.com/widgets/mb/appointments/c81156841c2/results.json?callback=%3F&callback=jQuery18104174805885557632_1429783411238&utf8=%E2%9C%93".$_SERVER['QUERY_STRING'];
    //  $url= "https://widgets.healcode.com/widgets/mb/appointments/c81156841c2/results.json?callback=%3F&callback=jQuery18109260513786728719_1429783974883&utf8=%E2%9C%93&options[session_type_ids]=21&options[staff_ids][]=&options[location_ids][]=450&options[start_date]=2015-05-01&options[end_date]=2015-05-07&_=1429789051232";
        $html = file_get_contents($url);
        $content = stripslashes(str_replace("n",chr(10),$html));
        $content = str_replace("u003C","<",$content);
        $content = str_replace("u003E",">",$content);
        $content = str_replace('jQuery18104174805885557632_1429783411238({"contents":"',"",$content);
        $content = str_replace('"});',"",$content);
        $content = str_replace('rel="modal"','rel="booking"',$content);
        $content = str_replace('u0026amp;','&',$content); 
        if(strpos($content,"Network timeout occurred. Please try again.")>0){
            $content="There was an issue returning results, please try again later";
        }

        return $content;
    }


    function parseSchedule($date){
        $obj = new stdClass();
        $html = @file_get_contents($this->_scheduleWidget);
        $content = stripslashes(str_replace("n",chr(10),$html));
        file_put_contents($this->_cacheFolder."schedule".$date.".html",$content);
        $this->_dom = str_get_html(file_get_contents($this->_cacheFolder."schedule".$date.".html"));
        $obj->session = $this->_dom->find("#session_type",0);;
        $obj->staff = $this->_dom->find("#options_staff_ids_",0);
        $obj->location = $this->_dom->find("#options_location_ids_",0);
        $obj->startDate =  $this->_dom->find("#options_start_date",0);
        $obj->endDate =  $this->_dom->find("#options_end_date",0);
        $this->saveSchedule($obj,$date);

    }

    function getElement($name){
        return $this->_schedule->$name;
    }

}

Shortcode code:

// shortcode logic to link to PT timetable
function ptLinks( $atts ) {

    extract( shortcode_atts(
    array(
    'session' => '',
    'staff' => '',
    'location' => '',
    ), $atts )
    );


    $link = get_permalink( get_page_by_path( 'personal-training-timetable' ) )."?action=go";
    if(isset($atts['session']) && $atts['session']!=""){
        $atts['session'] = str_replace("-","",$atts['session']);
        $link.="&session[]=".$atts['session'];
    }

    if(isset($atts['staff']) && $atts['staff']!=""){
        $atts['staff'] = str_replace("-"," ",$atts['staff']);
        $link.="&staff[]=".$atts['staff'];
    }

    if(isset($atts['location']) && $atts['location']!=""){
        $atts['location'] = str_replace("-"," ",$atts['location']);
        $link.="&location[]=".$atts['location'];
    }

    return $link;
}
add_shortcode( 'ptLinks', 'ptLinks' );

I think I’m missing a few more steps here to get it sorted. Can anyone advise?

Many thanks indeed!


Source: shortcode

Leave a Reply