Skip to content

Instantly share code, notes, and snippets.

@franz-josef-kaiser
Created July 25, 2013 21:16
Show Gist options
  • Save franz-josef-kaiser/6083849 to your computer and use it in GitHub Desktop.
Save franz-josef-kaiser/6083849 to your computer and use it in GitHub Desktop.
(WCM) Fields - A set of classes to build reusable form fields, labels, descriptions and tables. This is work in progress. The final version (which will possibly never happen), will be used in the WordPress admin UI. But lots of the code should be reusable in some standalone solution as well, as it mainly utilizes the DOMDocument class and similar.
<?php
namespace WCMFields;
defined( 'ABSPATH' ) OR exit;
/**
* Plugin Name: (WCM) Fields
* Plugin URl:
* Description: <strong>Needs PHP 5.3+!</strong>
* Author: wecodemore, Franz Josef Kaiser
* Author URl: http://unserkaiser.com
* License: MIT
*/
\register_activation_hook( __FILE__, array( __NAMESPACE__.'\Setup', 'on_activation' ) );
class Setup
{
public function on_activation()
{
! version_compare( phpversion(), '5.3', '>=' )
AND wp_die( 'You need at least PHP 5.3 or higher to run this plugin.' );
}
}
\add_action( 'plugins_loaded', array( __NAMESPACE__.'\Bootstrap', 'init' ) );
class Bootstrap
{
protected static $instance = null;
public static function init()
{
null === self::$instance AND self::$instance = new self;
return self::$instance;
}
public function __construct()
{
# @TODO Load classes/files
}
}
\add_action( 'admin_menu', array ( __NAMESPACE__.'\Controller', 'init' ) );
class Controller
{
protected static $instance = null;
public static function init()
{
null === self::$instance AND self::$instance = new self;
return self::$instance;
}
protected function __construct()
{
# @TODO Move configuration outside
$field = new FieldElement(
'input',
array(
'type' => 'text',
'name' => 'foo',
'id' => 'foo',
'value' => 'This is foo!',
'class' => 'foo bar',
'size' => 10,
'maxlength' => 8,
),
'Some Label'
);
# @TODO must happen on the fly
$field->set_label();
$field->set_element();
$field->set_desc();
echo $field;
$table = new Table();
$table->get_element();
echo $table;
}
}
class FieldElement
{
private $dom = null;
protected $tag = null;
protected $attr = null;
protected $desc = '';
public function __construct( $tag, $attr, $desc = null )
{
null === $this->dom AND $this->dom = new \DOMDocument( '', '' );
null === $this->tag AND $this->tag = $tag;
null === $this->attr AND $this->attr = $attr;
null === $this->desc AND $this->desc = $desc;
}
public function __toString()
{
if ( $this->dom instanceof \DOMDocument )
return $this->dom->saveXML( $this->dom, LIBXML_NOEMPTYTAG );
return '';
}
public function set_element()
{
$tag = $this->dom->createElement( $this->tag );
$this->dom->appendChild( $tag );
if ( is_array( $this->attr ) )
foreach ( $this->attr as $a => $v )
$tag->setAttribute( $a, $v );
$this->dom->saveHTML();
}
public function set_label()
{
$keys = array_intersect( array_keys( $this->attr ), array( 'name', 'id' ) );
if (
! is_array( $this->attr )
OR empty( $keys )
OR empty( $this->desc )
)
return;
$label = $this->dom->createElement( 'label' );
$this->dom->appendChild( $label );
$label->setAttribute( 'for', $this->attr[ array_pop( $keys ) ] )
->nodeValue = $this->desc;
$this->dom->saveHTML();
}
public function set_desc()
{
if ( empty( $this->desc ) )
return;
$desc = $this->dom->createElement( 'p' );
$desc->setAttribute( 'class', 'description' )
->nodeValue = $this->desc;
$this->dom->saveHTML();
}
}
class Table
{
private $dom = null;
private $table = null;
protected $desc = '';
public function __construct()
{
null === $this->dom AND $this->dom = new \DOMDocument( '', '' );
if ( null === $this->table )
{
$el = $this->dom->createElement( 'table' );
$this->table = $this->dom->appendChild( $el );
}
# null === $this->desc AND $this->desc = $desc;
}
public function __toString()
{
if ( $this->dom instanceof \DOMDocument )
return $this->dom->saveXML( $this->dom, LIBXML_NOEMPTYTAG );
return '';
}
public function get_thead()
{
$thead = $this->dom->createElement( 'thead' );
$thead = $this->table->appendChild( $thead );
$thead_tr = $this->dom->createElement( 'tr' );
$thead_tr = $thead->appendChild( $thead_tr );
$this->dom->saveHTML();
}
public function get_element()
{
$thead = $this->dom->createElement( 'thead' );
$thead = $this->table->appendChild( $thead );
$thead_tr = $this->dom->createElement( 'tr' );
$thead_tr = $thead->appendChild( $thead_tr );
$tbody = $this->dom->createElement( 'tbody' );
$tbody = $this->table->appendChild( $tbody );
$content = array(
'Feed On Offer' => array( 'a', 'b', 'c' ),
'Digestibility (%)' => array( 'd', 'e', 'f' ),
'Equivalent to' => array( 'g', 'h', 'i' ),
);
foreach ( $content as $th => $val )
{
$td = $this->dom->createElement( 'td' );
$td = $thead_tr->appendChild( $td );
$th = $this->dom->createTextNode( $th );
$th = $td->appendChild( $th );
$tbody_tr = $this->dom->createElement( 'tr' );
$tbody_tr = $tbody->appendChild( $tbody_tr );
foreach ( $val as $v )
{
$td = $this->dom->createElement( 'td' );
$td = $tbody_tr->appendChild( $td );
$v = $this->dom->createTextNode( $v );
$v = $td->appendChild( $v );
}
}
$this->dom->saveHTML();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment