PHP functions in class are returning empty values

Viewed 366

I am using a WordPress plugin that displays posts as markers on a Google Map. The front-end of the website properly displays the markers using the defined latitude and longitude, but the values are missing from the individual posts section in the WP admin panel.

The plugin outputs the latitude and longitude like this <?php echo esc_js($this->latitude()) ?> and <?php echo esc_js($this->longitude()) ?> in the JavaScript file that renders the map in the editor pane. As stated, these return no values for the Google Map.

In the same section, but using different code, the latitude and longitude values are returned using <?php esc_attr_e( $marker->latitude() )?> and <?php esc_attr_e( $marker->longitude() )?>. I have attached a screenshot showing how it displays. The map by default uses the global latitude and longitude to center the marker. default Google Map

When I var_dump the variable $marker_list, which contains the values for the location, the latitude and longitude values are returned.

array(1) {
    [21]=>object(Marker)#11821(12) {
        ["id":"Marker":private]=>int(21)
        ["post_id":"Marker":private]=>int(406)
        ["is_main":"Marker":private]=>bool(!0)
        ["title":"Marker":private]=>string(16)"Vancouver Island"
        ["categories":"Marker":private]=>string(23)"a:1:{i:3;s:6:"canada";}"
        ["tags":"Marker":private]=>string(6)"a:0:{}"
        ["latitude":"Marker":private]=>float(49.4178)
        ["longitude":"Marker":private]=>float(-125.197)
        ["marker_logo_id":"Marker":private]=>int(10)
        ["infobox_descritpion":"Marker":private]=>string(367)"Text"
        ["infobox_image":"Marker":private]=>string(1)"0"
        ["infobox_style":"Marker":private]=>string(30)"novo-map_infobox_style_default"
    }
}

Marker class

class Marker {
    /**
     * Unique id of the Marker object
     *
     * @access   private
     * @var      int
     * @since    1.0.0
     */
    private $id;

    /**
     * Id of the post where the marker was defined
     *
     * @access   private
     * @var      int
     * @since    1.0.0
     */
    private $post_id;

    /**
     * define if it is the main pin for a specific post
     * there should be only on main marker per post
     *
     * @access   private
     * @var      boolean
     * @since    1.0.0
     */
    private $is_main=true;

    /**
     * title of the marker
     *
     * @access   private
     * @var      string
     * @since    1.0.0
     */
    private $title;

    /**
     * Should reflect the categories (list of categories id) of the post_id
     *
     * @access   private
     * @var      string
     * @since    1.0.0
     */
    private $categories='';

    /**
     * Should reflect the tags (list og tags id) of the post id
     *
     * @access   private
     * @var      string
     * @since    1.0.0
     */
    private $tags='';

    /**
     * lattitude of the marker
     *
     * @access   private
     * @var      array
     * @since    1.0.0
     */
    private $latitude=0;

    /**
     * longitude of the marker
     *
     * @access   private
     * @var      array
     * @since    1.0.0
     */
    private $longitude=0;

    /**
     * marker_logo object attached to the marker
     *
     * @access   private
     * @var      object
     * @since    1.0.0
     */
    private $marker_logo_id=2;

    /**
     * description that appears in the infobox. Can be HTML
     *
     * @access   private
     * @var      string
     * @since    1.0.0
     */
    private $infobox_descritpion;

    /**
     * image id that should be displayed in the infobox
     * default value should not be changed as it is a small trick used later
     *
     * @access   private
     * @var      int
     * @since    1.0.0
     */
    private $infobox_image = -1;

    /**
     * infobox_style object attached to the marker
     *
     * @access   private
     * @var      string
     * @since    1.0.0
     */
    private $infobox_style='novo-map_infobox_style_default';

    /**
     * Marker constructor.
     * @param array $data
     * @since    1.0.0
     */
    public function __construct(array $data) {
        $this->hydrate($data);
    }

    /**
     * Hydrate the objects on construct with the given values.
     * The provided array should have the right structure
     *
     * @param array $data
     * @since    1.0.0
     */
    public function hydrate(array $data){
        foreach ($data as $key => $value) {
            //check if the key starts with novo-map- for unique names in wp admin
            if (substr( $key, 0, 16 ) === 'novo-map-marker-') {
                $key = str_replace('novo-map-marker-','',$key);
            }
            $method = 'set_'.$key;
            if (method_exists($this, $method)) {
                $this->$method($value);
            }
        }
    }

    /**
     * All getters and setters for the Marker class
     *
     * Data validation should occur in the setters
     */

    public function id() {
        return $this->id;
    }

    public function set_id($id) {
        $this->id = (int)$id;
    }

    public function post_id() {
        return $this->post_id;
    }

    public function set_post_id($post_id) {
        $this->post_id = (int)$post_id;
    }

    public function is_main() {
        return $this->is_main;
    }

    public function set_is_main($is_main) {
        $this->is_main = (bool)$is_main;
    }

    public function title() {
        return $this->title;
    }

    public function set_title($title) {
        $this->title = sanitize_text_field($title);
    }

    public function categories() {
        return $this->categories;
    }

    public function set_categories($categories) {
        $this->categories = (string)$categories;
    }

    public function tags() {
        return $this->tags;
    }

    public function set_tags($tags) {
        $this->tags = (string)$tags;
    }

    public function latitude() {
        return $this->latitude;
    }

    public function set_latitude($latitude) {
        $this->latitude = (float)$latitude;
    }

    public function longitude() {
        return $this->longitude;
    }

    public function set_longitude($longitude) {
        $this->longitude = (float)$longitude;
    }

    public function marker_logo_id() {
        return $this->marker_logo_id;
    }

    public function set_marker_logo_id($marker_logo_id) {
        $this->marker_logo_id = (int)$marker_logo_id;
    }

    public function infobox_description() {
        return $this->infobox_descritpion;
    }

    public function set_infobox_description($infobox_description) {
        $this->infobox_descritpion = wp_kses_post($infobox_description);
    }

    public function infobox_image() {
        return $this->infobox_image;
    }

    public function set_infobox_image($infobox_image) {
        $this->infobox_image = (string)$infobox_image;
    }

    public function infobox_style() {
        return $this->infobox_style;
    }

    public function set_infobox_style($infobox_style) {
        $this->infobox_style = (string)$infobox_style;
    }

    /**
     * generate the marker object part of the gmap script
     *
     * @since    1.0.0
     */
    public function generate_script($gmap, $gmap_name, $infobox_style_list) {
        //generate the script from template
        ob_start();
        include( plugin_dir_path(dirname( __FILE__ )) .'public/partials/marker-script.php' );
        $marker_script = ob_get_contents();
        ob_end_clean();
        echo fn_minify_js($marker_script);
    }
}

How would I go about debugging the PHP to find out why there are no values returned?

3 Answers

I think esc_js accept string value. $this->latitude() and $this->longitude() data type is float. you should convert them to string

try this. it might help you

<?php echo esc_js($this->latitude().'') ?>
<?php echo esc_js($this->longitude().'') ?>

esc_js Function accepts only strings, as explained here

You need to cast the float value to string, like this.

<?php
echo esc_js((string)$this->latitude());
echo esc_js((string)$this->longitude());

Your send a "float" but esc_js expects a "string". In this case, I recommend not to use the esc_js function. So:

If your are 100% sure that your type is always a "float". Your can display your value directly :

<?php echo $this->longitude() ?>

But! If your are not 100% sure, to prevent XSS exploit, you can force the float type like this (look also floatval):

<?php echo (float) $this->longitude() ?>

An another possible way, if you want to respect original cast is to use json_encode, like this :

<?php echo json_encode($this->longitude()) ?>

The second way, is probably what you want to do.

Related