Polyton Design Pattern

/*
* The Polyton Design Pattern
* By Adam J Nowak ~ http://hyperspatial.com
*
* Creates unique numbered class instances and allows you to run a different set of functions for the first instance of the class.
*
* Methods:
* create(); //Constructor method, Use: $polyton = Polyton::create('Test String');
* get_version(); //Instance version number, Use: $polyton->get_version();
* get_data(); //Value of the $_data class property, Use: $polyton->get_data();
* set_data(); //Set value of the $_data class property, Use: $polyton->set_data('New Test String');
*
* Create Instances:
* 1 - Use static method to instantiate
* 2 - Constructor is private. *Don't Use: $var = new Polyton('new polyton');
* 3 - The one argument is a simple string to demonstrate constructor argument flow
*/

class Polyton{
private static $instance;
private static $version;
private $_version;
private $_data;

private function __construct($data){
$this->_data = $data;
$this->_version = self::$version;
echo "Polyton $this->_version Constructed"; //Replace w/ constructor statements
}
public static function create($data){
if(!isset(self::$instance)){
self::$version = 1;
self::$instance = new Polyton($data); //Create original
echo ' ~ Original<br>'; //Replace w/ original instance statements
}
else{
self::$version ++;
self::$instance = new Polyton($data); //Create subsequent version
echo ' ~ Reproduction<br>'; //Replace w/ instance statements
}
return self::$instance;
}
public function get_version(){ return $this->_version; }
public function get_data(){ return $this->_data; }
public function set_data($new_data){
$this->_data = $new_data;
return $this->_data;
}
}

/*
* Polyton Design Pattern example markup
*
* Create Instances:
* 1 - Use static method to instantiate
* 2 - Constructor is private so you cant use: $var = new Polyton('new polyton');
* 3 - The one argument is a simple string to demonstrate constructor argument flow
*/

$polyton = Polyton::create('Top');
$polyton2 = Polyton::create('Middle');
$polyton3 = Polyton::create('I like to argue');

// Display info about the second object you created
echo '<br>Polyton 2 Info:<br>';
echo "Version = " . $polyton2->get_version() . '<br>';
echo "Data = " . $polyton2->get_data() . '<br>';

// Change the value of the stored argument
echo '<br>Polyton 2 data change:<br>';
$polyton2->set_data('The Very Middle');
echo "Changed Data = " . $polyton2->get_data() . '<br>';

?>

Add tab to activity page

function my_theme_add_activity_tab() {
if ( !is_user_logged_in() )
return false;
?>
<li id="activity-myclass">
<a href="<?php echo site_url( BP_ACTIVITY_SLUG . '/#myclass/' ) ?>" title="<?php _e( 'Activity for my Class Year.', 'buddypress' ) ?>">
<?php printf( __( 'My Class', 'buddypress' ) ) ?>
</a>
</li>
<?php
}
add_action( 'bp_activity_type_tabs', 'my_theme_add_activity_tab' );

Find and Replace

<script type="text/javascript">

var testString = 'This is the subject of my experiment';

//A simple find and replace statement
var simpleFind = 'experiment';
var simpleReplacement = 'simple find and replace';

//Using a regular expression for more advanced find and replace
var regexFind = /experiment/;
var regexReplacement = 'Regex';

document.write('Original: ' + testString + '<br>');
document.write('String replace: ' + testString.replace(simpleFind,simpleReplacement) + '<br>');
document.write('Regex replace: ' + testString.replace(regexFind,regexReplacement));

</script>

User blogs

//Call the function from the members-loop.php file within the members loop
<?php lmc_get_user_blogs(bp_get_member_user_id());?>

//Add to your functions.php file
function lmc_get_user_blogs($user_id){
global $wpdb;

$blog_query = $wpdb->get_results("SELECT blog_id FROM wp_bp_user_blogs
WHERE user_id = $user_id");
if(!$blog_query) return;
else echo bp_member_name() . ''s Blogs:</span>';
foreach($blog_query as $blog){
if($blog->blog_id == 1) continue;
$domain_query = $wpdb->get_results("SELECT domain FROM wp_blogs
WHERE blog_id = $blog->blog_id");
$complete_url = $domain_query[0]->domain;
$pos = strpos($complete_url,'.');
$subdomain = substr_replace($complete_url,"",$pos);
?>
<a class="lmc-blog-link" href="<?php echo 'http://' . $complete_url ?>" title="Visit Blog"><?php echo $subdomain ?></a>
<?php
}
}

Object prototype example

<script type="text/javascript">

String.prototype.toBoldGreenCaps = function(){
var rawString = this;
rawString = rawString.bold();
rawString = rawString.fontcolor('green');
finalString = rawString.toUpperCase();
return finalString;
}

var testString = "My test string to format - toBoldGreenCaps()";
document.write(testString.toBoldGreenCaps());

</script>

Refine search by category

//Add this to the search.php file, just above the wordpress loop
<?php $search_category = $_GET['category']; ?>
<?php query_posts('&cat=' . $search_category); ?>

//Add this wherever you want to display the search form

<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/" class="search-form">
<input type="text" value="Search" name="s" id="s" class="field" />
<select name="category">
<?php
$categories = get_categories();
foreach ($categories as $cat){ ?>
<option value="<?php echo $cat->cat_ID; ?>" <?php if($cat->cat_ID == 3) echo 'selected="selected"'; ?>><?php echo $cat->cat_name; ?></option>
<?php
}
?>
</select>
<input class="submitsearch button" type="submit" name="submit" value="Search" />
</form>

Objects for function arguments

<script type="text/javascript">

argumentObject = {arg1: "Argument One", arg2: "Argument Two", arg3: "Argument Three", arg4: "Argument Four"};

function argumentTest(objArgs){
alert(objArgs.arg1 + 'n' + objArgs.arg2 + 'n' + objArgs.arg3 + 'n' + objArgs.arg4);
}

argumentTest(argumentObject);

</script>

Ajax Request

<script type="text/javascript">
function ajaxLoadFile()
{
if(window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");//For Ie5+Ie6

xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) document.getElementById("ajax-target-div").innerHTML = xmlhttp.responseText;
}
xmlhttp.open("GET","/ajax-test.php",true);
xmlhttp.send();
}
</script>

<div id="ajax-target-div">
<p>Text to be replaced by file loaded via Ajax</p>
</div>

<button type="button" onclick="ajaxLoadFile();">Load file with Ajax</button>

Control Iframe style

<script type="text/javascript">

if(top.location.href == 'http://www.skwebo.com/create-event/'){
document.write('<style type="text/css">html{background-color:transparent;}#adminmenu,#contextual-help-link-wrap,#wphead,#wp-admin-bar{display:none;}</style>');
}

</script>

Enlarge image rollover

/*
*  Cycle Image Size Rollover. This script written for buddypress to create rollover effect on avatars.
*  The 'enlargementRatio' argument is the multiplyer applied to the original icon size
*  The 'childNumber' argument is to target the right img node, typically is 1 or zero
*  Add this to the avatar <a> parent:  onmouseover="cycleChildImage(this,1.1);" onmouseout="cycleChildImage(this,1.1);"
*  Add this to any image:  onmouseover="cycleImage(this,1.1,1);" onmouseout="cycleImage(this,1.1,1);"
*/

var existingWidth;
var existingHeight;
var isLarge = false;

function cycleImage(element,enlargementRatio){
var imageElement = element;
if(isLarge == true) reduceImage(imageElement,enlargementRatio);
else enlargeImage(imageElement,enlargementRatio);
}

function cycleChildImage(element,enlargementRatio,childNumber){
var imageElement = element.childNodes.item(childNumber);
if(isLarge == true) reduceImage(imageElement,enlargementRatio);
else enlargeImage(imageElement,enlargementRatio);
}

function enlargeImage(imageElement,enlargementRatio){
existingWidth = imageElement.width;
existingHeight = imageElement.height;
imageElement.style.width = (existingWidth * enlargementRatio) + 'px';
imageElement.style.height = (existingHeight * enlargementRatio) + 'px';
isLarge = true;
}

function reduceImage(imageElement,enlargementRatio){
imageElement.style.width = existingWidth + 'px';
imageElement.style.height = existingHeight + 'px';
isLarge = false;
}