Skip to content

Commit

Permalink
preparing for production use
Browse files Browse the repository at this point in the history
  • Loading branch information
kovshenin committed Aug 26, 2011
1 parent 2eddd75 commit f68980f
Show file tree
Hide file tree
Showing 10 changed files with 861 additions and 239 deletions.
File renamed without changes
File renamed without changes
File renamed without changes
306 changes: 67 additions & 239 deletions post-options-api.php → ...ample-plugin/inc/post-options-api.1.0.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<?php
/**
* Plugin Name: Post Options API
* Plugin URI: http://github.com/kovshenin/post-options-api
* Description: Post Options API plugin, test driving.
* Version: 1.1
* Author: kovshenin
* Author URI: http://theme.fm
* Post Options API
*
* This is not a plugin, this is a file you should bundle together with
* your theme or plugin where you'd like to use the Post Options API.
* View the Readme file for more details and examples.
*
* Version 1.0
* Author: Konstantin Kovshenin (kovshenin@gmail.com)
* http://theme.fm
*
* License: GPL2
**/

Expand All @@ -23,7 +27,8 @@
* the post options API.
*
**/
class Post_Options_Fields {
if ( ! class_exists( 'Post_Options_Fields_1_0' ) ):
class Post_Options_Fields_1_0 {

// Used to output description if present (for less redundancy)
public static function description( $args = array() ) {
Expand All @@ -47,7 +52,7 @@ public static function checkbox( $args = array() ) {
$args = wp_parse_args( $args, $defaults );

return array(
'function' => array( 'Post_Options_Fields', '_checkbox' ),
'function' => array( __CLASS__, '_checkbox' ),
'args' => $args
);
}
Expand Down Expand Up @@ -75,7 +80,7 @@ public static function text( $args = array() ) {
extract( $args, EXTR_SKIP );

return array(
'function' => array( 'Post_Options_Fields', '_text' ),
'function' => array( __CLASS__, '_text' ),
'sanitize_callback' => $sanitize_callback,
'args' => array(
'description' => $description
Expand Down Expand Up @@ -107,7 +112,7 @@ public static function textarea( $args = array() ) {
extract( $args, EXTR_SKIP );

return array(
'function' => array( 'Post_Options_Fields', '_textarea' ),
'function' => array( __CLASS__, '_textarea' ),
'sanitize_callback' => $sanitize_callback,
'args' => array(
'description' => $description,
Expand Down Expand Up @@ -142,7 +147,7 @@ public static function select( $args = array() ) {
extract( $args, EXTR_SKIP );

return array(
'function' => array( 'Post_Options_Fields', '_select' ),
'function' => array( __CLASS__, '_select' ),
'sanitize_callback' => $sanitize_callback,
'args' => array(
'description' => $description,
Expand Down Expand Up @@ -179,7 +184,7 @@ public static function radio( $args = array() ) {
extract( $args, EXTR_SKIP );

return array(
'function' => array( 'Post_Options_Fields', '_radio' ),
'function' => array( __CLASS__, '_radio' ),
'sanitize_callback' => $sanitize_callback,
'args' => array(
'description' => $description,
Expand All @@ -197,7 +202,18 @@ public static function _radio( $args = array() ) {
<?php
self::description( $args );
}

// This is a Singleton class
private static $instance;
public static function singleton() {
if ( ! isset( self::$instance ) ) {
$className = __CLASS__;
self::$instance = new $className;
}
return self::$instance;
}
};
endif; // class_exists

/**
*
Expand All @@ -212,7 +228,8 @@ public static function _radio( $args = array() ) {
* metabox UI and the post meta IO operations.
*
**/
class Post_Options {
if ( ! class_exists( 'Post_Options_API_1_0' ) ):
class Post_Options_API_1_0 {

private $sections = array();
private $options = array();
Expand Down Expand Up @@ -453,236 +470,47 @@ private function section_exists( $section_id ) {
public function get_post_option( $post_id, $option_id ) {
return get_post_meta( $post_id, $option_id, true );
}
};

add_action( 'init', create_function( '', 'global $post_options; $post_options = new Post_Options();' ) );
add_action( 'init', 'post_options_test' );

// Let's test out the above
function post_options_test() {
global $post_options;

// Register two sections and add them both to the 'post' post type
$post_options->register_post_options_section( 'showing-off', 'Showing off various post options' );
$post_options->register_post_options_section( 'real-world', 'Some real world examples' );
$post_options->add_section_to_post_type( 'showing-off', 'post' );
$post_options->add_section_to_post_type( 'real-world', 'post' );
$post_options->add_section_to_post_type( 'real-world', 'page' );

// The showing off section

// A simple checkbox
$post_options->register_post_option( array(
'id' => 'a-checkbox',
'title' => 'A checkbox',
'section' => 'showing-off',
'callback' => Post_Options_Fields::checkbox( array(
'label' => 'Check me to win $500',
'description' => 'Any sort of description can go below this checkbox and <a href="#">HTML markup</a> too!'
) )
) );

// A text input with a sanitize callback
$post_options->register_post_option( array(
'id' => 'an-input',
'title' => 'A text input',
'section' => 'showing-off',
'priority' => 5,
'callback' => Post_Options_Fields::text( array(
'description' => 'The text in this input is saved and sanitized using the <code>sanitize_title</code> sanitize callback, so try and input some caps, numbers, symbols and spaces.',
'sanitize_callback' => 'sanitize_title'
) )
) );

// A textarea
$post_options->register_post_option( array(
'id' => 'a-textarea',
'title' => 'A textarea for larger text or perhaps code',
'section' => 'showing-off',
'callback' => Post_Options_Fields::textarea( array(
'description' => 'A textarea might be useful for some custom code or perhaps an addition to the post, like a signature or something. Note how the field title flows nicely in to several lines.'
) )
) );

// A radio group
$post_options->register_post_option( array(
'id' => 'a-radio-group',
'title' => 'A radio group',
'section' => 'showing-off',
'callback' => Post_Options_Fields::radio( array(
'description' => 'Radio groups accept a <code>$radio_data</code> argument where we pass in an array with values and captions for each item in the group.',
'radio_data' => array(
'option-1' => 'The first option',
'option-2' => 'Another option',
'option-3' => 'One more option'
)
) )
) );

// A drop-down select box
$post_options->register_post_option( array(
'id' => 'a-select-input',
'title' => 'A drop-down select box',
'section' => 'showing-off',
'callback' => Post_Options_Fields::select( array(
'description' => 'Select boxes are similar to radio when it comes to data input, so just provide an array of values and captions.',
'select_data' => array(
'option-1' => 'This is the first option',
'option-2' => 'Hurray for the second one',
'option-3' => 'There is room for a third'
)
) )
) );

// The real-world section

// Hide sidebar
$post_options->register_post_option( array(
'id' => 'hide-sidebar',
'title' => 'Hide sidebar',
'section' => 'real-world',
'callback' => Post_Options_Fields::checkbox( array(
'label' => 'Hide sidebar on this post',
'description' => 'Check this to hide the right sidebar on this post.'
) )
) );

// Feature this post
$post_options->register_post_option( array(
'id' => 'featured-post',
'title' => 'Featured post',
'section' => 'real-world',
'callback' => Post_Options_Fields::checkbox( array(
'label' => 'This is a featured post',
'description' => 'Check this to feature the post in the highlights section on the homepage.'
) )
) );

// Hide banners
$post_options->register_post_option( array(
'id' => 'hide-banners',
'title' => 'Hide banners',
'section' => 'real-world',
'callback' => Post_Options_Fields::checkbox( array(
'label' => 'Hide all banner ads on this post',
'description' => 'You might want to hide all your banner advertising if you would like the visitor to focus on the content of this post.'
) )
) );
// This is a Singleton class
private static $instance;
public static function singleton() {
if ( ! isset( self::$instance ) ) {
$className = __CLASS__;
self::$instance = new $className;
}
return self::$instance;
}
};
endif; // class_exists

// Background image
$post_options->register_post_option( array(
'id' => 'background-image',
'title' => 'Background image URL',
'section' => 'real-world',
'callback' => Post_Options_Fields::text( array(
'description' => 'Provide the background image URL to override on this post, useful to create outstanding landing pages without the use of templates.'
) )
) );

// A textarea
$post_options->register_post_option( array(
'id' => 'greeting-text',
'title' => 'Greeting text',
'section' => 'real-world',
'callback' => Post_Options_Fields::textarea( array(
'description' => 'Enter some text here to show a popup greeting message box as soon as this post loads.',
'rows' => 3
) )
) );

// A radio group
$post_options->register_post_option( array(
'id' => 'navigation-style',
'title' => 'Navigation style',
'section' => 'real-world',
'callback' => Post_Options_Fields::radio( array(
'description' => 'Customize the navigation style for this page.',
'radio_data' => array(
'option-1' => 'Default',
'option-2' => 'Full navigation menu and submenu',
'option-3' => 'Menu only, submenu on hover',
'option-4' => 'Submenu only'
)
) )
) );

// Did you know
$post_options->register_post_option( array(
'id' => 'did-you-know',
'title' => 'Did you know?',
'section' => 'real-world',
'callback' => 'my_callback'
) );

// Page Layout
$post_options->register_post_option( array(
'id' => 'page-layout',
'title' => 'Custom Callback',
'section' => 'real-world',
'callback' => 'page_layout_callback'
) );

// Mood Example
$post_options->register_post_option( array(
'id' => 'mood',
'title' => 'Helper Callback',
'section' => 'real-world',
'callback' => Post_Options_Fields::select( array(
'description' => 'How did you feel when writing this post?',
'select_data' => array(
'Happy' => 'Happy',
'Sad' => 'Sad',
'Disappointed' => 'Disappointed',
'Awful' => 'Awful'
)
) )
) );

add_filter( 'the_content', 'my_mood_filter' );
}

// A filter to the_content to show off the current mood
function my_mood_filter( $content ) {
global $post_options, $post;
$mood = $post_options->get_post_option( $post->ID, 'mood' );
if ( ! empty( $mood ) )
$content .= "<p><strong>Mood</strong>: {$mood}</p>";
return $content;
}
/**
* Singleton Creators
*
* Below are the singleton creators for the Post Options API
* and the Post Options Fields if needed. This is made to ensure
* compatibility, like when two plugins are running a different
* version of the Post Options API.
*/

// This function illustrates a custom callback
function my_callback( $args ) {
?>
That you can provide your own callback to the post option registration function and that the above are just helpers to spare you time and money?
<strong>Seriously</strong>, I can do whatever I want here, and even provide a sanitize callback for validation. Want proof? Check out this <code>print_r</code>
call to the arguments provided to this callback function:<br />

<pre style="margin: 10px;"><?php echo htmlspecialchars( print_r( $args, true ) ); ?></pre>

Go ahead and set it to whatever you like and see how it affects the value:<br />
<input class="large-text" type="text" name="<?php echo $args['name_attr']; ?>" value="<?php echo esc_attr( $args['value'] ); ?>" />
<?php
// Returns the Post Options API object (singleton)
if ( ! function_exists( 'get_post_options_api') ) {
function get_post_options_api( $version ) {
$class_name = 'Post_Options_API_' . str_replace( '.', '_', $version );
if ( class_exists( $class_name ) )
return $class_name::singleton();
else
return new WP_Error( 'post-options-api-init', 'You have requested a non-existing version of the Post Options API.' );
}
}

// Custom callback, page layout
function page_layout_callback( $args ) {
$layouts = array(
'layout-1' => 'Default',
'layout-2' => 'Full-width',
'layout-3' => 'Left sidebar'
);
?>

<?php foreach( $layouts as $layout => $caption ): ?>
<div class="mg-color-scheme-item" style="float: left; margin-right: 14px; margin-bottom: 18px;">
<label style="float: left; clear: both;">
<input <?php echo checked( $layout == $args['value'] ); ?> type="radio" name="<?php echo $args['name_attr']; ?>" value="<?php echo $layout; ?>" style="margin-bottom: 4px;" /><br />
<img src="<?php echo plugins_url( 'images/' . $layout . '.png', __FILE__ ); ?>" style="border: solid 1px #ccc;" /><br />
<span class="description" style="margin-top: 8px; float: left;"><?php echo $caption; ?></span>
</label>
</div>
<?php endforeach; ?>
<br class="clear" />
<span class="description">Showing off how one would implement page templates.</span>
<?php
// Returns the Post Options Fields object (singleton)
if ( ! function_exists( 'get_post_options_api_fields' ) ) {
function get_post_options_api_fields( $version ) {
$class_name = 'Post_Options_Fields_' . str_replace( '.', '_', $version );
if ( class_exists( $class_name ) )
return $class_name::singleton();
else
return new WP_Error( 'post-options-api-fields-init', 'You have requested a non-existing version of the Post Options API Fields.' );
}
}
Loading

0 comments on commit f68980f

Please sign in to comment.