Skip to content

Instantly share code, notes, and snippets.

@ericandrewlewis
Last active January 2, 2016 10:29

Revisions

  1. ericandrewlewis revised this gist Jan 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    /**
    * Post Meta Box view/controller class.
    *
    * Extends from a basic form object class.
    * Extends from a form object base class.
    *
    * Creates a meta box. Fields can be related to the meta box, but
    * all business logic lives within the field object.
  2. ericandrewlewis revised this gist Jan 6, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -124,5 +124,7 @@ function register_form_containers() {
    * Text input class. Essentially, a view.
    *
    * Provides form element HTML markup, script enqueueing, etc.
    *
    * Relates to a field type.
    */
    class Input_Text extends Input {}
  3. ericandrewlewis revised this gist Jan 6, 2014. 1 changed file with 58 additions and 17 deletions.
    75 changes: 58 additions & 17 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -3,10 +3,12 @@
    /**
    * Post Meta Box view/controller class.
    *
    * Extends from a basic form object class.
    *
    * Creates a meta box. Fields can be related to the meta box, but
    * all business logic lives within the field object.
    */
    class WP_Post_Meta_Box extends WP_Form_Controller {
    class WP_Post_Meta_Box extends WP_Form_Object {

    function __construct() {
    // Register object-specific UI
    @@ -54,30 +56,69 @@ function save_field( $field ) {
    update_post_meta( $post->ID, $field->slug, $this->get_field_value( $field ), true );
    }

    function get_field_value() {
    // retrieve the field's data depending on the
    }
    /**
    * Retrieve a user-submitted value for a field.
    *
    * This may need to call the field object/class, depending how
    * $_POST variable names are built.
    */
    function get_field_value( $field ) {}

    /**
    * Get field objects related to this form object.
    */
    function get_fields() {}
    }

    add_action( 'init', 'register_custom_fields' );

    function register_custom_fields() {
    register_field( array(
    'objects' => array( 'post', 'page' ),
    'slug' => 'background_color',
    'type' => 'color', // Relates to a pre-defined class.
    'auth_callback' => 'callback_function',
    'sanitization_callback' => 'sanitization_function',
    ) );

    }

    /**
    * Field registration.
    *
    * Abstracted from the UI view class, so that the field's business logic
    * and data describing it can be accessed independently.
    *
    * Data about fields is stored in a nested array.
    */
    register_field( array(
    'objects' => array( 'post', 'page' ),
    'slug' => 'background_color',
    'type' => 'color', // Relates to a pre-defined class.
    'auth_callback' => 'callback_function',
    'sanitization_callback' => 'sanitization_function',
    ) );

    // Insantiate a container.
    $container = new WP_Post_Meta_Box();

    // Relate a registered field to the container.
    $container->add_field( array( 'slug' => 'background_color' ) );
    function register_field( $args ) {
    global $object_fields;

    // Parse args with defaults...

    // Sanitize data...

    if ( is_array( $args['objects'] ) {
    foreach ( $objects as $object_type ) {
    $object_fields[$object_type][] = array(
    // Add data...
    );
    // register_meta etc depending on object type...
    }
    }
    }

    add_action( 'init', 'register_form_containers' );
    /**
    * Register form contianer.
    */
    function register_form_containers() {
    // Insantiate a container.
    $container = new WP_Post_Meta_Box();

    // Relate a registered field to the container.
    $container->add_field( array( 'slug' => 'background_color' ) );
    }

    /**
    * Text input class. Essentially, a view.
  4. ericandrewlewis revised this gist Jan 6, 2014. No changes.
  5. ericandrewlewis revised this gist Jan 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    * Creates a meta box. Fields can be related to the meta box, but
    * all business logic lives within the field object.
    */
    class WP_Post_Meta_Box {
    class WP_Post_Meta_Box extends WP_Form_Controller {

    function __construct() {
    // Register object-specific UI
  6. ericandrewlewis revised this gist Jan 6, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -62,8 +62,8 @@ function get_field_value() {
    /**
    * Field registration.
    *
    * Abstracted from the UI view class, so that the business logic
    * and self-descriptive data can be accessed independently.
    * Abstracted from the UI view class, so that the field's business logic
    * and data describing it can be accessed independently.
    */
    register_field( array(
    'objects' => array( 'post', 'page' ),
  7. ericandrewlewis revised this gist Jan 6, 2014. No changes.
  8. ericandrewlewis created this gist Jan 6, 2014.
    87 changes: 87 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    <?php

    /**
    * Post Meta Box view/controller class.
    *
    * Creates a meta box. Fields can be related to the meta box, but
    * all business logic lives within the field object.
    */
    class WP_Post_Meta_Box {

    function __construct() {
    // Register object-specific UI
    add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );

    // Register data saving handler.
    add_action( 'save_post', array( $this, 'save' ), 10, 2 );
    }

    /**
    * Register a meta box.
    */
    function add_meta_box() {}

    /**
    * Output the contents of the meta box.
    */
    function metabox_callback() {
    $this->render_form();
    }

    /**
    * Render input elements for all related fields.
    */
    function render_form() {
    foreach ( $this->get_fields() as $field ) {
    $field->render_input_element();
    }
    }

    /**
    * Save data or each field according to the object's data model.
    */
    function save( $post_id, $post ) {
    foreach ( $this->get_fields() as $field ) {
    $this->save_field( $field );
    }
    }

    /**
    * Save method specific to the object type.
    */
    function save_field( $field ) {
    global $post;
    update_post_meta( $post->ID, $field->slug, $this->get_field_value( $field ), true );
    }

    function get_field_value() {
    // retrieve the field's data depending on the
    }
    }

    /**
    * Field registration.
    *
    * Abstracted from the UI view class, so that the business logic
    * and self-descriptive data can be accessed independently.
    */
    register_field( array(
    'objects' => array( 'post', 'page' ),
    'slug' => 'background_color',
    'type' => 'color', // Relates to a pre-defined class.
    'auth_callback' => 'callback_function',
    'sanitization_callback' => 'sanitization_function',
    ) );

    // Insantiate a container.
    $container = new WP_Post_Meta_Box();

    // Relate a registered field to the container.
    $container->add_field( array( 'slug' => 'background_color' ) );

    /**
    * Text input class. Essentially, a view.
    *
    * Provides form element HTML markup, script enqueueing, etc.
    */
    class Input_Text extends Input {}