Permalink
Browse files

Add full register_meta() support to the theme layouts and post styles…

…heets extensions.

git-svn-id: http://svn.locallylost.com/themes/hybrid-core/trunk@941 dba0f204-706d-4bc1-bc29-8b92e0485636
  • Loading branch information...
1 parent ab93562 commit c79edd3c3fec4400b99638edf6708b13a9c3f664 greenshady committed Dec 9, 2011
Showing with 90 additions and 23 deletions.
  1. +44 −10 extensions/post-stylesheets.php
  2. +46 −13 extensions/theme-layouts.php
@@ -19,8 +19,13 @@
* @copyright Copyright (c) 2010 - 2011, Justin Tadlock
* @link http://justintadlock.com
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+ *
+ * @todo register_meta()
*/
+/* Register metadata with WordPress. */
+add_action( 'init', 'post_stylesheets_register_meta' );
+
/* Add post type support for the 'post-stylesheets' feature. */
add_action( 'init', 'post_stylesheets_add_post_type_support' );
@@ -31,6 +36,32 @@
add_action( 'admin_menu', 'post_stylesheets_admin_setup' );
/**
+ * Registers the post stylesheets meta key ('Stylesheet') for posts and provides a function to sanitize
+ * the metadata on update.
+ *
+ * @since 0.3.0
+ * @return void
+ */
+function theme_layouts_register_meta() {
+ register_meta( 'post', post_stylesheets_get_meta_key(), 'post_stylesheets_sanitize_meta' );
+}
+
+/**
+ * Callback function for sanitizing meta when add_metadata() or update_metadata() is called by WordPress.
+ * If a developer wants to set up a custom method for sanitizing the data, they should use the
+ * "sanitize_{$meta_type}_meta_{$meta_key}" filter hook to do so.
+ *
+ * @since 0.4.0
+ * @param mixed $meta_value The value of the data to sanitize.
+ * @param string $meta_key The meta key name.
+ * @param string $meta_type The type of metadata (post, comment, user, etc.)
+ * @return mixed $meta_value
+ */
+function post_styleheets_sanitize_meta( $meta_value, $meta_key, $meta_type ) {
+ return esc_attr( strip_tags( $meta_value ) );
+}
+
+/**
* Adds post type support for the 'post-stylesheets' feature to all 'public' post types.
*
* @since 0.3.0
@@ -230,23 +261,26 @@ function post_stylesheets_meta_box_save( $post_id, $post ) {
if ( !post_type_supports( $post->post_type, 'post-stylesheets' ) )
return;
- /* Check if the current user has permission to edit the post. */
- if ( !current_user_can( 'edit_post_meta', $post_id ) )
- return;
+ /* Get the meta key. */
+ $meta_key = post_stylesheets_get_meta_key();
/* Get the previous post stylesheet. */
- $old_stylesheet = get_post_stylesheet( $post_id );
+ $meta_value = get_post_stylesheet( $post_id );
/* Get the submitted post stylesheet. */
- $new_stylesheet = esc_attr( strip_tags( $_POST['post-stylesheets'] ) );
+ $new_meta_value = $_POST['post-stylesheets'];
- /* If the stylesheet has been removed, delete the metadata completely. */
- if ( !empty( $old_stylesheet ) && '' == $new_stylesheet )
+ /* If there is no new meta value but an old value exists, delete it. */
+ if ( current_user_can( 'delete_post_meta', $post_id, $meta_key ) && '' == $new_meta_value && $meta_value )
delete_post_stylesheet( $post_id );
- /* If the old stylesheet doesn't match the new stylesheet, update the post stylesheet meta. */
- elseif ( $old_stylesheet !== $new_stylesheet )
- set_post_stylesheet( $post_id, $new_stylesheet );
+ /* If a new meta value was added and there was no previous value, add it. */
+ elseif ( current_user_can( 'add_post_meta', $post_id, $meta_key ) && $new_meta_value && '' == $meta_value )
+ set_post_stylesheet( $post_id, $new_meta_value );
+
+ /* If the old layout doesn't match the new layout, update the post layout meta. */
+ elseif ( current_user_can( 'edit_post_meta', $post_id, $meta_key ) && $meta_value !== $new_meta_value )
+ set_post_stylesheet( $post_id, $new_meta_value );
}
/**
@@ -46,8 +46,23 @@
* @return void
*/
function theme_layouts_register_meta() {
- register_meta( 'post', theme_layouts_get_meta_key(), 'esc_attr' );
- register_meta( 'user', theme_layouts_get_meta_key(), 'esc_attr' );
+ register_meta( 'post', theme_layouts_get_meta_key(), 'theme_layouts_sanitize_meta' );
+ register_meta( 'user', theme_layouts_get_meta_key(), 'theme_layouts_sanitize_meta' );
+}
+
+/**
+ * Callback function for sanitizing meta when add_metadata() or update_metadata() is called by WordPress.
+ * If a developer wants to set up a custom method for sanitizing the data, they should use the
+ * "sanitize_{$meta_type}_meta_{$meta_key}" filter hook to do so.
+ *
+ * @since 0.4.0
+ * @param mixed $meta_value The value of the data to sanitize.
+ * @param string $meta_key The meta key name.
+ * @param string $meta_type The type of metadata (post, comment, user, etc.)
+ * @return mixed $meta_value
+ */
+function theme_layouts_sanitize_meta( $meta_value, $meta_key, $meta_type ) {
+ return esc_attr( strip_tags( $meta_value ) );
}
/**
@@ -394,19 +409,26 @@ function theme_layouts_save_post( $post_id, $post ) {
if ( !isset( $_POST['theme-layouts-nonce'] ) || !wp_verify_nonce( $_POST['theme-layouts-nonce'], basename( __FILE__ ) ) )
return $post_id;
- /* Check if the current user has permission to edit the post. */
- if ( !current_user_can( 'edit_post_meta', $post_id ) )
- return $post_id;
+ /* Get the meta key. */
+ $meta_key = theme_layouts_get_meta_key();
/* Get the previous post layout. */
- $old_layout = get_post_layout( $post_id );
+ $meta_value = get_post_layout( $post_id );
/* Get the submitted post layout. */
- $new_layout = esc_attr( $_POST['post-layout'] );
+ $new_meta_value = $_POST['post-layout'];
+
+ /* If there is no new meta value but an old value exists, delete it. */
+ if ( current_user_can( 'delete_post_meta', $post_id, $meta_key ) && '' == $new_meta_value && $meta_value )
+ delete_post_layout( $post_id );
+
+ /* If a new meta value was added and there was no previous value, add it. */
+ elseif ( current_user_can( 'add_post_meta', $post_id, $meta_key ) && $new_meta_value && '' == $meta_value )
+ set_post_layout( $post_id, $new_meta_value );
/* If the old layout doesn't match the new layout, update the post layout meta. */
- if ( $old_layout !== $new_layout )
- set_post_layout( $post_id, $new_layout );
+ elseif ( current_user_can( 'edit_post_meta', $post_id, $meta_key ) && $meta_value !== $new_meta_value )
+ set_post_layout( $post_id, $new_meta_value );
}
/**
@@ -462,15 +484,26 @@ function theme_layouts_attachment_fields_to_save( $post, $fields ) {
/* If the theme layouts field was submitted. */
if ( isset( $fields['theme-layouts-post-layout'] ) ) {
+ /* Get the meta key. */
+ $meta_key = theme_layouts_get_meta_key();
+
/* Get the previous post layout. */
- $old_layout = get_post_layout( $post['ID'] );
+ $meta_value = get_post_layout( $post['ID'] );
/* Get the submitted post layout. */
- $new_layout = esc_attr( $fields['theme-layouts-post-layout'] );
+ $new_meta_value = $fields['theme-layouts-post-layout'];
+
+ /* If there is no new meta value but an old value exists, delete it. */
+ if ( current_user_can( 'delete_post_meta', $post['ID'], $meta_key ) && '' == $new_meta_value && $meta_value )
+ delete_post_layout( $post['ID'] );
+
+ /* If a new meta value was added and there was no previous value, add it. */
+ elseif ( current_user_can( 'add_post_meta', $post['ID'], $meta_key ) && $new_meta_value && '' == $meta_value )
+ set_post_layout( $post['ID'], $new_meta_value );
/* If the old layout doesn't match the new layout, update the post layout meta. */
- if ( $old_layout !== $new_layout )
- set_post_layout( $post['ID'], $new_layout );
+ elseif ( current_user_can( 'edit_post_meta', $post['ID'], $meta_key ) && $meta_value !== $new_meta_value )
+ set_post_layout( $post['ID'], $new_meta_value );
}
/* Return the attachment post array. */

0 comments on commit c79edd3

Please sign in to comment.