Add your own features in custom tables plugin

My plugin has a lot of possibilities, but as its possible that you have your own special need, you can easily extend the functionality of my plugin with your own “plugin” without touching the code. You will not loose the update functionality by a new release and your code is working.

Today I show you an implementation of that.

The site we are talking now about has a very high userload, 12k Users on a day with around 60k Pageviews.

Setup of Tables

There are 3 Tables with 1:n Relation implemented:

Tables and Relation

Tables and Relation

The first Table contains the different Companies. which existing. The 2nd Table contains the location where the Company exists and what they sell (not all Warehouses selling all type of stuff everywhere). The 3rd Table contains the opening hours for the store.

Now its possible to define 1 Store, but multiple opening hours for it. That is a relation between 2 tables.

As the site is havely visited and the databases contains very much data, the standard search of the custom tables plugin could be used. But instead of limiting the search to the fields I wanted, I show in this plugin, how its possible to implement an own search possibility.

I will show here snippeds out of the complete plugin, you can download the demo case in the end complete, also with the correct order of programming and class.

New Forms for our page

The following hook will add the possibility to add with [wctsearchform] a search form to my page with its function.

add_shortcode('wctsearchform', array(&$this, 'show_searchform'));
function show_searchform($atts) {
	$out = "<form class=\"art-search\" method=\"get\" name=\"searchform\" action=\"http://www.sonntagsverkaeufe.ch/\">
		<input name=\"st\" type=\"text\" value=\"\" />
		<input class=\"art-search-button\" type=\"submit\" value=\"Search\" />
	</form>";
	return $out;
}

The next Hook will only generate a dropdown where the visitor can select a Date, out of the latest available dates in the database:

add_shortcode('wctdateselect', array(&$this, 'show_dateselect'));
function show_dateselect($atts) {
	global $wpdb;
	$timenow = mktime('1','1','1',date('m',time()),date('d',time()),date("Y",time()));

	$abfrage = "SELECT `datum_von` AS `item`,count(`datum_von`) AS `anz`, `tagname` FROM `".$wpdb->prefix."wct10` WHERE `status`='active' AND `datum_von`>='".$timenow."' GROUP BY `datum_von` ORDER BY `datum_von` ASC;";
	$qry = $wpdb->get_results($abfrage);
	if (count($qry) >= '1') {

		$out = "<select class=\"wct-select\" onChange=\"selectorA('Adate','Adate')\" style=\"font-weight: bold;width:180px;\" id=\"Adatewctdrop_Adate\" name=\"Adatewctdrop_Adate\">
		<option style=\"font-weight: bold;\" value=\"NULL\">Datum</option>";
		foreach ($qry as $row) {
			$jsvalue = $row->item;
			$out .= "<option class=\"wct-option\" style=\"font-weight: normal;\" value=\"".date("d.m.Y",$jsvalue)."\" ";

			if (($_GET['sdate'] == date("d.m.Y",$jsvalue) AND $row->item != '') OR ($_GET['sdate'] == '' AND $row->item == '')) { $out .= " selected"; }

			$out .= ">";
			if ($row->item == '') { $out .= "-- ".__('Empty','wct')." --"; }
			else
			{
				$out .= $row->tagname." (".date("d.m.Y",$row->item).")";
			}
			$out .= "</option>";
		}
		$out .= "</select>";
	}

	$url = $this->generate_pagelink(array("/[&?]+Adatewctdro(p|f)=.*&/","/[&?]+wctstart=[0-9]*/"),array("&",""));
	$out .= "<script type=\"text/javascript\">".
		 "function selectorA(field,fieldb) {".
			"var selection = document.getElementById('Adatewctdrop_' + field).options[document.getElementById('Adatewctdrop_' + field).selectedIndex].value;".
			"location.href= '".$url."sdate=' + selection;".
		"}</script>";

	return $out;
}

Looks like a heavy code, but it only generates the dropdown shown.

Now we have the basic, our own search form and a date select where people can filter very fast the tables on a date. Now we need to add this features to the custom tables, that both together will function.

What are Hooks

To archive new functionality without changing the original code, you need to use the hooks.
Hooks are functions which are within the code implemented and not used. You can then with an own plugin give that hook “functionality” and define how it should modify the original code.

A small example:

$var = "Hello World!";
echo $var;

That will only deliver back “Hello World!” to the screen.

$var = "Hello World!";
echo apply_filters('filter1',$var);

Now we have a filter added. But the filter1 is not defined, so it will not be used. Same result here.

Within an other file you can add now new code:

add_filter('filter1', array(&$this, 'myfilter'));
function myfilter($var) {
	$var = str_replace("Hello World","Hello new World",$var);.
	return $var;
}

First, we define which Hook should be addressed and next which which function. As you can see, that function will replace the test.

After our modification the text which will be shown to the user on the screen is “Hello new World!” without touching the original code! That is highly recommended for security reason (update compatibility possibility).

When this is understand, we will go now to the hooks for the wct plugin.

wuk custom tables hooks

Look inside my plugin for the file you need to modify and search for apply_filter, that’s the way you find my hooks. If there is somewhere a hook missing, send me the line number and the filename you want to have a hook and I can add it within minutes to the code.

For the sites search, I only needed to alter 1 hook in custom tables:

add_filter('wct_searchfilter', array(&$this, 'search_date'),999,3);
function search_date($val, $attr = array(), $content = '') {
	if ($_GET['sdate'] != '' AND $_GET['sdate'] != 'NULL') {
		extract(shortcode_atts(array('searchfilter' => '','filter'=>'1'), $attr));
		if ($searchfilter != '') {
			$query = wct_fixspecialchars($_GET['sdate']);
			$query = mb_convert_encoding($query ,mb_detect_encoding($query),'utf8');

			if (preg_match("/^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{4})$/", $query, $regs)) { $db_timestamp = mktime(1,1,1,$regs[2], $regs[1], $regs[3]); }
			elseif (preg_match("/^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{2})$/", $query, $regs)) { $db_timestamp = mktime(1,1,1,$regs[2], $regs[1], '20'.$regs[3]); }
			elseif (preg_match("/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})$/", $query, $regs)) { $db_timestamp = mktime(1,1,1,$regs[1], $regs[2], $regs[3]); }
			elseif (preg_match("/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2})$/", $query, $regs)) { $db_timestamp = mktime(1,1,1,$regs[1], $regs[2], '20'.$regs[3]); }
			elseif (preg_match("/^([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})$/", $query, $regs)) { $db_timestamp = mktime(1,1,1,$regs[1], $regs[2], $regs[3]); }
			elseif (preg_match("/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$/", $query, $regs)) { $db_timestamp = mktime(1,1,1,$regs[3], $regs[2], $regs[1]); }
			elseif (preg_match("/^([0-9]{1,2})\.([0-9]{1,2})\.$/", $query, $regs)) { $db_timestamp = mktime(1,1,1,$regs[2], $regs[1], date("Y",time())); }
			else {	unset($db_timestamp); }
			
			if (isset($db_timestamp)) {
				if ($searchfilter == "date1") {
					if ($filter == "1") {
						$val = preg_replace("/WHERE.*ORDER/","WHERE `10`.`status`='active' AND `10`.`datum_von`='".$db_timestamp."' ORDER",$val);
					}
					elseif ($filter == "2") {
						$val = preg_replace("/WHERE.*;/","WHERE `10`.`status`='active' AND `10`.`datum_von`='".$db_timestamp."';",$val);
					}
				}
				elseif ($searchfilter == "date2") {
					$val = str_replace("WHERE "," INNER JOIN `blog_wct10` as `10` ON `12`.`id`=`10`.`relkat` WHERE `10`.`datum_von`='".$db_timestamp."' AND",$val);							
				}
			}
		}
	}
	if ($_GET['st'] != '' AND $_GET['st'] != 'NULL') {
		extract(shortcode_atts(array('searchfilter' => '','filter'=>'1'), $attr));
		if ($searchfilter != '') {
			$query = wct_fixspecialchars($_GET['st']);
			$query = mb_convert_encoding($query ,mb_detect_encoding($query),'utf8'); //latin1 gegebenfalls
			if ($searchfilter == "date1") {
				if ($filter == "1") {
					$val = preg_replace("/WHERE.*ORDER/","WHERE (`12`.`name` LIKE '%".$query."%' OR `12`.`ort` LIKE '%".$query."%') ORDER",$val);
				}
				elseif ($filter == "2") {
					$val = preg_replace("/WHERE.*;/","WHERE (`12`.`name` LIKE '%".$query."%' OR `12`.`ort` LIKE '%".$query."%');",$val);
				}
			}
			elseif ($searchfilter == "date2" OR $searchfilter == "ort1") {
				$val = str_replace("WHERE ","WHERE (`12`.`name` LIKE '%".$query."%' OR `12`.`ort` LIKE '%".$query."%') AND",$val);							
			}
			elseif($searchfilter == "kats") {
				$val = str_replace("WHERE ","WHERE (`12`.`name` LIKE '".$query."%' OR `12`.`ort` LIKE '".$query."%') AND `9`.`kategorie`='".mres()."' AND",$val);	
			}
		}	
	}
	if ($_GET['wsta'] != '' AND $_GET['wcat'] != '') {
		extract(shortcode_atts(array('searchfilter' => '','filter'=>'1'), $attr));
		if ($searchfilter != '') {
			if($searchfilter == "kats") {
				if ($_GET['wcat'] == 'stadt') {
					$val = str_replace("WHERE ","WHERE `12`.`ort` LIKE '".mres($_GET['wsta'])."%' AND `9`.`kategorie`='".mres($_GET['wcat'])."' AND",$val);	
				}
				elseif ($_GET['wcat'] == 'shopping') {
					$val = str_replace("WHERE ","WHERE `12`.`name` LIKE '".mres($_GET['wsta'])."%' AND `9`.`kategorie`='".mres($_GET['wcat'])."' AND",$val);	
				}
				else {
					$val = str_replace("WHERE ","WHERE (`12`.`name` LIKE '".mres($_GET['wsta'])."%' OR `12`.`ort` LIKE '".mres($_GET['wsta'])."%') AND `9`.`kategorie`='".mres($_GET['wcat'])."' AND",$val);	
				}
			}
		}	
	}
	return $val;
}

Thats it. Study the code and implement your own functionalities within custom tables plugin, or share it with me!

Here you can download the demo file: demo-addon file