Skip to content

Instantly share code, notes, and snippets.

@vimes1984
Created January 24, 2015 15:52
Show Gist options
  • Save vimes1984/7528540d01f495e8eb2a to your computer and use it in GitHub Desktop.
Save vimes1984/7528540d01f495e8eb2a to your computer and use it in GitHub Desktop.
<?php
/**
* OIFN extended
*
* integrates google map into a location based buddypress
*
* @package oifn-extended
* @author Christopher James Chruchill <churchill.c.j@gmail.com>
* @license GPL-2.0+
* @link http://buildawebdoctor.com
* @copyright 5-7-2014 BAWD
*
* @wordpress-plugin
* Plugin Name: OIFN extended
* Plugin URI: http://buildawebdoctor.com
* Description: integrates google map into a location based buddypress
* Version: 1.0.0
* Author: Christopher James Chruchill
* Author URI: http://buildawebdoctor.com
* Text Domain: oifn-extended-locale
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Domain Path: /lang
*/
// If this file is called directly, abort.
if (!defined("WPINC")) {
die;
}
/*GET plugin options*/
$PluginOptsArray = get_option( 'ofin_options' );
$geturl = $PluginOptsArray['redircturl'];
//var_dump($geturl);
require_once(plugin_dir_path(__FILE__) . "OIFNExtended.php");
// Register hooks that are fired when the plugin is activated, deactivated, and uninstalled, respectively.
register_activation_hook(__FILE__, array("OIFNExtended", "activate"));
register_deactivation_hook(__FILE__, array("OIFNExtended", "deactivate"));
OIFNExtended::get_instance();
/*Add a filter to filter the redirect url for login*/
add_filter("login_redirect","bpdev_redirect_profile_edit",100,3);
function bpdev_redirect_profile_edit($redirect_to_calculated,$redirect_url_specified,$user){
/*if no redirect was specified,let us think ,user wants to be in wp-dashboard*/
if(empty($redirect_to_calculated))
$redirect_to_calculated=admin_url();
/*if the user is not site admin,redirect to his/her profile*/
if(!is_super_admin($user->ID)){
global $PluginOptsArray, $geturl;
$getuserprofileurl = bp_core_get_user_domain($user->ID );
$combinethem = $getuserprofileurl . $geturl;
return $combinethem;
}
else{
return $redirect_to_calculated; /*if site admin or not logged in,do not do anything much*/
}
}
/*end redirect login*/
/********************************************************************************************************************
| |
| Start profile page edits this will drive me mad i know it!! |
| |
| |
********************************************************************************************************************/
//check postcode
function validate_character_count( $result, $value, $form, $field, $locale ) {
//$validator = new Zend_Validate_PostCode($locale);
$regex = '/[a-z][0-9][a-z][- ]?[0-9][a-z][0-9]$/i';
if(!preg_match($regex, $value)) {
$result['is_valid'] = false;
$result['message'] = sprintf( $this->_args['validation_message'], $this->_args['min_chars'] );
}
return $result;
}
function valid_postcode ($data) {
global $bp;
if(isset($_POST['field_23'])){
$postcodecheck = $_POST['field_23'];
$regex = '/[a-z][0-9][a-z][- ]?[0-9][a-z][0-9]$/i';
if(!preg_match($regex, $postcodecheck)) {
bp_core_add_message( __( 'That Postal code is invalid. Check the formatting and try again.', 'buddypress' ), 'error' );
wp_redirect( $bp->loggedin_user->domain . 'profile/edit/group/2/' );
exit();
}elseif ($postcodecheck == '') {
bp_core_add_message( __( 'You need to fill out the Postal Code.', 'buddypress' ), 'error' );
wp_redirect( $bp->loggedin_user->domain . 'profile/edit/group/2/' );
exit();
}
}
return $data;
}
add_action( 'xprofile_data_before_save', 'valid_postcode');
function provincecheck ($data) {
global $bp;
if(isset($_POST['field_22'])){
$postcodecheck = $_POST['field_22'];
if($postcodecheck != 'Ontario') {
bp_core_add_message( __( 'The Province must be Ontario. Check the formatting and try again.', 'buddypress' ), 'error' );
wp_redirect( $bp->loggedin_user->domain . 'profile/edit/group/2/' );
exit();
}elseif ($postcodecheck == '') {
bp_core_add_message( __( 'The Province field must be Ontario.', 'buddypress' ), 'error' );
wp_redirect( $bp->loggedin_user->domain . 'profile/edit/group/2/' );
exit();
}
}
return $data;
}
add_action( 'xprofile_data_before_save', 'provincecheck');
function addprovince($data){
global $current_user;
get_currentuserinfo();
$newAddress = 'Ontario';
//var_dump(xprofile_get_field_data( 'Province' ,bp_get_member_user_id()));
xprofile_set_field_data('Province', $current_user->id, $newAddress);
}
add_action( 'xprofile_screen_edit_profile', 'addprovince');
/********************************************
| PUT buddypress data into table 'makers' |
| Now this function is spagetti Junction, |
| I'll comment my way through it |
| hope all makes sense |
********************************************/
function updatedb($data){
//Global varibales
global $wpdb;
//Grab our userid We'll need this for later
$user_ID = get_current_user_id();
//this is to check if this user has a DB entry or not...
$result = $wpdb->get_results ("SELECT id FROM markers WHERE userID = '".$user_ID."'");
/*********************************
|ok let's geoencode! the address |
**********************************/
//this checks the buinesss name field and post code, to make sure we have that
if(isset($_POST['field_23']) && $_POST['field_23'] != '' && isset($_POST['field_15']) && $_POST['field_15'] != ''){
//OK here we grab the $_POST fields the var names describe what each is...
$organaddress = $_POST['field_15'];
$street = $_POST['field_21'];
$postcode = $_POST['field_23'];
$combineaddress = $organaddress . ', ' . $street . ', Ontario, ' . $postcode;
//urlenocde the combined address
$Address = urlencode($combineaddress);
//Ask google to convert to LAT/LNG for us this can be flaky at times...
//Poss ask to move to a paid service?
$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$Address."&sensor=true";
//ask the URL
$xml = simplexml_load_file($request_url);
//set the satus
$status = $xml->status;
//If the status checks out we nip into here
// LIKE A THEIF IN THE NIGHT!
if ($status=="OK") {
/************************************************************
// Ok sparky we made it this far //
// as Yazz would say "the only way is up!! BABABYYY " //
// Good god the eighties where bad.. //
************************************************************/
//these two do as they say grab lat/lng from that xml we loaded earlier!
$Lat = $xml->result->geometry->location->lat;
$Lon = $xml->result->geometry->location->lng;
//Yet another if/else maybe this should be a while or a catch?
if(isset($_POST['field_13']) && $_POST['field_13'] != ''){
$jsontype = $_POST['field_2'];
$name = $_POST['field_13'];
$description = $_POST['field_27'];
$county = $_POST['field_65'];
// and the last if else this checks wether the results
// var we delcared right at the begining are larger than 0 if they are
// we UPDATE if not we create a brand spanking new table!
// Either way we save the results to the KML file
if (count ($result) > 0) {
$wpdb->query ("UPDATE markers SET name = '" . $name . "', address = '". $combineaddress ."', county = '". $county ."', type = '". $jsontype ."', description = '". $description ."', lat = '". $Lat ."', lng = '". $Lon ."' WHERE userID = '".$user_ID."'");
} else {
$wpdb->replace('markers', array( 'userID' => $user_ID, 'name' => $name, 'address' => $combineaddress, 'description' => $description, 'lat' => $Lat, 'lng' => $Lon, 'type' => $jsontype, 'county' => $county ));
}
}
}else{
//if the status doesn't check out we drop to here...
// :'( it also makes us sad...
// this is a flaky un sure way to do this but it's what we have currently!
bp_core_add_message( __( 'The geolocation service is currently unavailble, please try again later.( revisit this page and resave your user account if you do not see your account on the Find a faciliator page)', 'buddypress' ), 'error' );
wp_redirect( $bp->loggedin_user->domain . 'profile/edit/group/2/' );
exit();
}
}else{
//Drop to here if no Organizqation name is set
/*bp_core_add_message( __( 'You must have a Organization name, if you are a indivual use your First & Last name.', 'buddypress' ), 'error' );
wp_redirect( $bp->loggedin_user->domain . 'profile/edit/group/2/' );
exit();*/
}
}
add_action( 'xprofile_data_after_save', 'updatedb');
function saveKML(){
global $wpdb;
// Selects all the rows in the markers table.
$result_kml = $wpdb->get_results( 'SELECT * FROM markers');
if (!$result_kml){
die('Invalid query: ' . mysql_error());
}
/*
echo "<pre>";
var_dump($result_kml);
echo "</pre>";
die;
*/
// Creates the Document.
$dom = new DOMDocument('1.0', 'UTF-8');
// Creates the root KML element and appends it to the root document.
$node = $dom->createElementNS('http://earth.google.com/kml/2.1', 'kml');
$parNode = $dom->appendChild($node);
// Creates a KML Document element and append it to the KML element.
$dnode = $dom->createElement('Document');
$docNode = $parNode->appendChild($dnode);
// Creates the six Style elements,for the profile type, and append the elements to the Document element.
$IndividualStyleNode = $dom->createElement('Style');
$IndividualStyleNode->setAttribute('id', 'IndividualStyle');
$IndividualIconstyleNode = $dom->createElement('IconStyle');
$IndividualIconstyleNode->setAttribute('id', 'IndividualIcon');
$IndividualIconNode = $dom->createElement('Icon');
$IndividualHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/shapes/wheel_chair_accessible.png');
$IndividualIconNode->appendChild($IndividualHref);
$IndividualIconstyleNode->appendChild($IndividualIconNode);
$IndividualStyleNode->appendChild($IndividualIconstyleNode);
$docNode->appendChild($IndividualStyleNode);
$OrganisationStyleNode = $dom->createElement('Style');
$OrganisationStyleNode->setAttribute('id', 'OrganisationStyle');
$OrganisationIconstyleNode = $dom->createElement('IconStyle');
$OrganisationIconstyleNode->setAttribute('id', 'OrganisationIcon');
$OrganisationIconNode = $dom->createElement('Icon');
$OrganisationHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/shapes/arts.png');
$OrganisationIconNode->appendChild($OrganisationHref);
$OrganisationIconstyleNode->appendChild($OrganisationIconNode);
$OrganisationStyleNode->appendChild($OrganisationIconstyleNode);
$docNode->appendChild($OrganisationStyleNode);
$NetworkStyleNode = $dom->createElement('Style');
$NetworkStyleNode->setAttribute('id', 'Network or CommunityStyle');
$NetworkIconstyleNode = $dom->createElement('IconStyle');
$NetworkIconstyleNode->setAttribute('id', 'NetworkIcon');
$NetworkIconNode = $dom->createElement('Icon');
$NetworkHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/shapes/truck.png');
$NetworkIconNode->appendChild($NetworkHref);
$NetworkIconstyleNode->appendChild($NetworkIconNode);
$NetworkStyleNode->appendChild($NetworkIconstyleNode);
$docNode->appendChild($NetworkStyleNode);
$TrainingStyleNode = $dom->createElement('Style');
$TrainingStyleNode->setAttribute('id', 'Training EntityStyle');
$TrainingIconstyleNode = $dom->createElement('IconStyle');
$TrainingIconstyleNode->setAttribute('id', 'TrainingIcon');
$TrainingIconNode = $dom->createElement('Icon');
$TrainingHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/shapes/subway.png');
$TrainingIconNode->appendChild($TrainingHref);
$TrainingIconstyleNode->appendChild($TrainingIconNode);
$TrainingStyleNode->appendChild($TrainingIconstyleNode);
$docNode->appendChild($TrainingStyleNode);
$OfficeStyleNode = $dom->createElement('Style');
$OfficeStyleNode->setAttribute('id', 'DSO OfficeStyle');
$OfficeIconstyleNode = $dom->createElement('IconStyle');
$OfficeIconstyleNode->setAttribute('id', 'OfficeIcon');
$OfficeIconNode = $dom->createElement('Icon');
$OfficeHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/shapes/police.png');
$OfficeIconNode->appendChild($OfficeHref);
$OfficeIconstyleNode->appendChild($OfficeIconNode);
$OfficeStyleNode->appendChild($OfficeIconstyleNode);
$docNode->appendChild($OfficeStyleNode);
$IndividualtrainStyleNode = $dom->createElement('Style');
$IndividualtrainStyleNode->setAttribute('id', 'Individual Training EntityStyle');
$IndividualtrainIconstyleNode = $dom->createElement('IconStyle');
$IndividualtrainIconstyleNode->setAttribute('id', 'IndividualtrainIcon');
$IndividualtrainIconNode = $dom->createElement('Icon');
$IndividualtrainHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/shapes/target.png');
$IndividualtrainIconNode->appendChild($IndividualtrainHref);
$IndividualtrainIconstyleNode->appendChild($IndividualtrainIconNode);
$IndividualtrainStyleNode->appendChild($IndividualtrainIconstyleNode);
$docNode->appendChild($IndividualtrainStyleNode);
$OrganisationtrainStyleNode = $dom->createElement('Style');
$OrganisationtrainStyleNode->setAttribute('id', 'Organisation Training EntityStyle');
$OrganisationtrainIconstyleNode = $dom->createElement('IconStyle');
$OrganisationtrainIconstyleNode->setAttribute('id', 'OrganisationtrainIcon');
$OrganisationtrainIconNode = $dom->createElement('Icon');
$OrganisationtrainHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/pal2/icon63.png');
$OrganisationtrainIconNode->appendChild($OrganisationtrainHref);
$OrganisationtrainIconstyleNode->appendChild($OrganisationtrainIconNode);
$OrganisationtrainStyleNode->appendChild($OrganisationtrainIconstyleNode);
$docNode->appendChild($OrganisationtrainStyleNode);
// Iterates through the MySQL results, creating one Placemark for each row.
if ($result_kml = $wpdb->get_results( 'SELECT * FROM markers')) {
foreach ($result_kml as $key => $value) {
$buistype = $value->type;
// Creates a Placemark and append it to the Document.
$node = $dom->createElement('Placemark');
$placeNode = $docNode->appendChild($node);
// Creates an id attribute and assign it the value of id column.
$placeNode->setAttribute('id', 'placemark' . $value->userID);
// Create name, and description elements and assigns them the values of the name and address columns from the results.
$nameNode = $dom->createElement('name',htmlentities($value->name));
$placeNode->appendChild($nameNode);
$descNode = $dom->createElement('description', $value->description . ' <span class="all_hide">' . $buistype . ' all ' . $value->county .' ' . $value->name . '</span>' );
$placeNode->appendChild($descNode);
$styleUrl = $dom->createElement('styleUrl', '#' . $buistype . 'Style');
$placeNode->appendChild($styleUrl);
// Creates a Point element.
$pointNode = $dom->createElement('Point');
$placeNode->appendChild($pointNode);
// Creates a coordinates element and gives it the value of the lng and lat columns from the results.
$coorStr = $value->lng. ',' . $value->lat;
$coorNode = $dom->createElement('coordinates', $coorStr);
$pointNode->appendChild($coorNode);
}
}
$kmlOutput = $dom->saveXML();
$locationtosave = realpath(dirname(__FILE__)) . '/kml/kmlOutput.kml';
$dom->save($locationtosave);
// header('Content-type: application/vnd.google-earth.kml+xml');
}
add_action( 'xprofile_data_after_save', 'saveKML');
/********************************************************************************************************************
| |
| END PROFILE PAGE EDITS |
| |
| |
********************************************************************************************************************/
/********************************************************************************************************************
| |
| Dump hooks very very usefull for debugging Since Buddypress is a bit of a bugger when it comes to priorities! |
| |
| |
********************************************************************************************************************/
function dump_hook( $tag, $hook ) {
ksort($hook);
echo "<pre>\t$tag<br>";
foreach( $hook as $priority => $functions ) {
echo $priority;
foreach( $functions as $function )
if( $function['function'] != 'list_hook_details' ) {
echo "\t";
if( is_string( $function['function'] ) )
echo $function['function'];
elseif( is_string( $function['function'][0] ) )
echo $function['function'][0] . ' -> ' . $function['function'][1];
elseif( is_object( $function['function'][0] ) )
echo "(object) " . get_class( $function['function'][0] ) . ' -> ' . $function['function'][1];
else
print_r($function);
echo ' (' . $function['accepted_args'] . ') <br>';
}
}
echo '</pre>';
}
function list_hooks( $filter = false ){
global $wp_filter;
$hooks = $wp_filter;
ksort( $hooks );
foreach( $hooks as $tag => $hook )
if ( false === $filter || false !== strpos( $tag, $filter ) )
dump_hook($tag, $hook);
}
function list_hook_details( $input = NULL ) {
global $wp_filter;
$tag = current_filter();
if( isset( $wp_filter[$tag] ) )
dump_hook( $tag, $wp_filter[$tag] );
return $input;
}
function list_live_hooks( $hook = false ) {
if ( false === $hook )
$hook = 'all';
add_action( $hook, 'list_hook_details', -1 );
}
function dumpitall(){
echo "<pre>";
var_dump( current_filter() );
echo "</pre>";
}
/*add_action( 'all', 'dumpitall' );*/
/********************************************************************************************************************
| |
| END DEBUGGING |
| |
| |
********************************************************************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment