<?php

namespace Blazemedia\Affiliation\Utils;


class SimpleDB {

    protected static $instance = null;

    protected $db;

    protected $mapping_table_name = 'gt_link_shortcode_mapping';

    protected function __construct() {
    
        global $wpdb;

        try {
            $this->db = $wpdb;
            
        } catch (\Exception $e) {
            print "Error!: " . $e->getMessage() . "<br/>";
            die();
        }
    }

    
    function getNewShortcode( $old_shortcode ) : string|null {

        $old_shortcode = urlencode( $old_shortcode );

        $sql = "SELECT new_shortcode from $this->mapping_table_name WHERE old_shortcode ='$old_shortcode'";

        $result = $this->db->get_results( $sql );
        
        $new_shortcode = empty($result) ? null : urldecode( $result[0]->new_shortcode );

        return $new_shortcode;
        
    }


    public static function getInstance(){

        if( self::$instance == null ) {   

           $c = __CLASS__;
           self::$instance = new $c();
        }
        
        return self::$instance;
    }
    
}
