Dynamic text with shortcode

<?php

//Text Shortcode
/** Print Text **/
function wco_print_text($shortcode = false,$term = '',$poss = false){
if($shortcode) ob_start();
if($poss) $extra_text = '&#8217;s'; //Possessive ('s)
$option = get_blog_option(1,'wco_text_' . $term); //Use get_option() for non-multisite
if($option) echo $option . $extra_text;
else echo '**' . strtoupper($term) . $extra_text;
if($shortcode){
$output = ob_get_contents();
ob_end_clean();
return $output;
}
}
/** Shortcode Print Text [wco_text term="location" poss="1"] **/
function wco_shortcode_print_text($atts){
extract(shortcode_atts(array('term' => '','poss' => false),$atts));
return wco_print_text(true,$term,$poss);
}
add_shortcode('wco_text','wco_shortcode_print_text');

?>

WPMU flush rewrites

<?php
private function set_rewrite_rules(){
$default_structure = '/%category%/%postname%/';
if(!get_option('aio_initialized')){
global $wp_rewrite;
$wp_rewrite->set_permalink_structure($default_structure);
$wp_rewrite->flush_rules();
update_option('aio_initialized',1);
}
}
?>

Decorator Pattern

<?php

abstract class Goggle{
abstract function get_impairement_of_judgement();
}

class BeerGoggle extends Goggle{
private $_impairement_of_judgement = 0;
public function get_impairement_of_judgement(){
return $this->_impairement_of_judgement;
}
}

abstract class GoggleDecorator extends Goggle{
protected $_goggle;
public function __construct(Goggle $goggle){
$this->_goggle = $goggle;
}
}

class SixPack extends GoggleDecorator{
public function get_impairement_of_judgement(){
return $this->_goggle->get_impairement_of_judgement() + 6;
}
}

class TwelvePack extends GoggleDecorator{
public function get_impairement_of_judgement(){
return $this->_goggle->get_impairement_of_judgement() + 12;
}
}

class RedBull extends GoggleDecorator{
public function get_impairement_of_judgement(){
return $this->_goggle->get_impairement_of_judgement() -0.5;
}
}

echo '~~~~~~~~~~~~~ Decorator Example: ~~~~~~~~~~~~~';

$goggle = new BeerGoggle();
echo 'Goggles Only Impairment: ' . $goggle->get_impairement_of_judgement() . '%<br>';

$six_beers = new SixPack(new BeerGoggle());
echo '6 Pack Impairment: ' . $six_beers->get_impairement_of_judgement() . '%<br>';

$twelve_beers = new TwelvePack(new BeerGoggle());
echo '12 Pack Impairment: ' . $twelve_beers->get_impairement_of_judgement() . '%<br>';

$eighteen_beer_goggle = new TwelvePack(new SixPack(new BeerGoggle()));
echo '18 Pack Impairment: ' . $eighteen_beer_goggle->get_impairement_of_judgement() . '%<br>';

$six_beers_and_a_redbull = new RedBull(new SixPack(new BeerGoggle()));
echo 'Six Pack and a Redbull: ' . $six_beers_and_a_redbull->get_impairement_of_judgement() . '%<br><br>';

?>

Composite Pattern

<?php

abstract class Horse{
abstract function get_horsepower();
}

class Pony extends Horse{
public function get_horsepower(){
return 0.7;
}
}

class RidingHorse extends Horse{
public function get_horsepower(){
return 1.0;
}
}

class WarHorse extends Horse{
public function get_horsepower(){
return 1.5;
}
}

/* ~~~~~~~~~~~~~ Wagons ~~~~~~~~~~~~~ */

class SmallWagon{
private $_horses = array();
public function add_horse(Horse $new_horse){
array_push($this->_horses,$new_horse);
}
public function get_horsepower(){
$total_horsepower = 0;
foreach($this->_horses as $horse){
$total_horsepower += $horse->get_horsepower();
}
return $total_horsepower - ($total_horsepower * 0.1); //10% Decrease
}
}

class LargeWagon{
private $_horses = array();
public function add_horse(Horse $new_horse){
array_push($this->_horses,$new_horse);
}
public function get_horsepower(){
$total_horsepower = 0;
foreach($this->_horses as $horse){
$total_horsepower += $horse->get_horsepower();
}
return $total_horsepower - ($total_horsepower * 0.2); //20% Decrease
}
}
echo '~~~~~~~~~~~~~ Composite Example: ~~~~~~~~~~~~~';

$wagon = new SmallWagon();
$wagon->add_horse(new WarHorse());
$wagon->add_horse(new WarHorse());
$wagon->add_horse(new Pony());

$lg_wagon = new LargeWagon();
$lg_wagon->add_horse(new WarHorse());
$lg_wagon->add_horse(new WarHorse());
$lg_wagon->add_horse(new Pony());

echo '<pre>';
print_r($wagon);
echo 'Small Wagon HP: ';
print_r($wagon->get_horsepower());
echo '<br>Large Wagon HP: ';
print_r($lg_wagon->get_horsepower());
echo '</pre><br><br>';

?>

Admin bar on bottom

<?php
function admin_bar_on_bottom() { ?>
<style type="text/css">
body {
margin-top: -28px;
padding-bottom: 28px;
}
body.admin-bar #wphead {
padding-top: 0;
}
body.admin-bar #footer {
padding-bottom: 28px;
}
#wpadminbar {
top: auto !important;
bottom: 0;
}
#wpadminbar .quicklinks .menupop ul {
bottom: 28px;
}
</style>
<?php }
add_action( 'wp_head', 'admin_bar_on_bottom' );
add_action( 'admin_head', 'admin_bar_on_bottom' );
?>

Prototype Pattern

<?php

class Cone{}
class PlainCone extends Cone{}
class WaffleCone extends Cone{}

class IceCream{}
class VanillaIceCream extends IceCream{
private $brand = 'Ben & Jerry's';//For something to look at in the clone
}
class ChocolateIceCream extends IceCream{}

class Topping{}
class Sprinkles extends Topping{}
class Nuts extends Topping{}

class DesertFactory{
private $_cone,$_ice_cream,$_topping;
public function __construct(Cone $cone,IceCream $ice_cream,Topping $topping){
$this->_cone = $cone;
$this->_ice_cream = $ice_cream;
$this->_topping = $topping;
}
public function get_cone(){
return clone $this->_cone;
}
public function get_ice_cream(){
return clone $this->_ice_cream;
}
public function get_topping(){
return clone $this->_topping;
}
}

echo '~~~~~~~~~~~~~ Prototype Example: ~~~~~~~~~~~~~';
$treat = new DesertFactory(new WaffleCone(),new VanillaIceCream(),new Sprinkles());
echo '<pre>';
print_r($treat);
echo '</pre><br><br>';

echo '<pre>';
print_r($treat->get_ice_cream());//Calling get_ice_cream() creates a clone
echo '</pre><br><br>';

?>

WP file upload

require( $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/file.php");
require( $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/image.php");

$merchant_id = $_POST['merchant_id'];

$upload_overrides = array( 'test_form' => FALSE );
$uploaded_file = wp_handle_upload($_FILES['logo'], $upload_overrides);

wp_create_thumbnail( $uploaded_file['file'], 150 );

$uploads = wp_upload_dir();

$wp_filetype = wp_check_filetype( basename( $uploaded_file['file'] ), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/.[^.]+$/', '', basename( $uploaded_file['file'] ) ),
'post_content' => '',
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_parent' => $merchant_id,
'guid' => $uploads['baseurl'] . '/' . $_FILES['logo']['name']
);
$attachment_id = wp_insert_post( $attachment );
$attach_data = wp_generate_attachment_metadata( $attachment_id, $uploaded_file['file'] );
wp_update_attachment_metadata( $attachment_id, $attach_data );
set_post_thumbnail( $merchant_id, $attachment_id );

header("Location: /merchant-profile/?updated=true");

Abstract Factory Pattern

<?php

/* ~~~~~~~~~~~~~ Factory Classes ~~~~~~~~~~~~~ */

abstract class RobotFactory{
abstract function build_male_robot();
abstract function build_female_robot();
}

class WorkRobotFactory extends RobotFactory{
function build_male_robot(){ return new MaleRobot('Work'); }
function build_female_robot(){ return new FemaleRobot('Work'); }
}

class EntertainmentRobotFactory extends RobotFactory{
function build_male_robot(){ return new MaleRobot('Entertainment'); }
function build_female_robot(){ return new FemaleRobot('Entertainment'); }
}

/* ~~~~~~~~~~~~~ Robots ~~~~~~~~~~~~~ */

abstract class Robot{
abstract function get_gender();
abstract function get_type();
}

class MaleRobot extends Robot{
private $_gender,$_type;
function __construct($type){
$this->_gender = "Male";
$this->_type = $type;
}
public function get_gender(){ return $this->_gender; }
public function get_type(){ return $this->_type; }
}

class FemaleRobot extends Robot{
private $_gender,$_type;
function __construct($type){
$this->_gender = "Female";
$this->_type = $type;
}
public function get_gender(){ return $this->_gender; }
public function get_type(){ return $this->_type; }
}

/* ~~~~~~~~~~~~~ Creator Class ~~~~~~~~~~~~~ */

class RobotFactoryCreator{
const WORK = "Work Robot";
const ENTERTAINMENT = "Entertainment Robot";

public static function CreateRobotFactory($factory_type){
switch($factory_type){
case self::WORK :
return new WorkRobotFactory();
break;
case self::ENTERTAINMENT :
return new EntertainmentRobotFactory();
break;
}
}
}

echo '~~~~~~~~~~~~~ Abstract Factory Example: ~~~~~~~~~~~~~';

//Direct Build
$entertainment_robot_factory = new WorkRobotFactory();
$male_bot = $entertainment_robot_factory->build_male_robot();
echo '<pre>';
var_dump($male_bot);
echo '</pre><br>';

//Creator Build
$entertainment_robot = RobotFactoryCreator::CreateRobotFactory(RobotFactoryCreator::ENTERTAINMENT);
$female_bot = $entertainment_robot->build_female_robot();

echo '<pre>';
var_dump($female_bot);
echo '</pre><br>';

?>