Make WordPress Core

Ticket #17850: 17850.7.diff

File 17850.7.diff, 7.1 KB (added by ryan, 14 years ago)

register_meta() and edit_post_meta cap, proof of concept.

  • wp-includes/post-template.php

     
    735735                echo "<ul class='post-meta'>\n";
    736736                foreach ( (array) $keys as $key ) {
    737737                        $keyt = trim($key);
    738                         if ( '_' == $keyt[0] )
     738                        if ( is_hidden_meta( $keyt ) )
    739739                                continue;
    740740                        $values = array_map('trim', get_post_custom_values($key));
    741741                        $value = implode($values,', ');
  • wp-includes/class-wp-xmlrpc-server.php

     
    234234
    235235                foreach ( (array) has_meta($post_id) as $meta ) {
    236236                        // Don't expose protected fields.
    237                         if ( strpos($meta['meta_key'], '_wp_') === 0 ) {
     237                        if ( is_protected_meta( $meta['meta_key'], 'post' ) ) {
    238238                                continue;
    239239                        }
    240240
     
    264264                                $meta['id'] = (int) $meta['id'];
    265265
    266266                                if ( isset($meta['key']) ) {
    267                                         update_meta($meta['id'], $meta['key'], $meta['value']);
     267                                        if ( current_user_can( 'edit_post_meta', $meta['key'], $post_id ) )
     268                                                update_meta($meta['id'], $meta['key'], $meta['value']);
     269                                } else {
     270                                        if ( ! is_protected_meta( $meta['key'] ) )
     271                                                delete_meta($meta['id']);
    268272                                }
    269                                 else {
    270                                         delete_meta($meta['id']);
    271                                 }
     273                        } else {
     274                                if ( ! is_protected_meta( $meta['key'] ) )
     275                                        add_post_meta( $post_id, $meta['key'], $meta['value'] );
    272276                        }
    273                         else {
    274                                 $_POST['metakeyinput'] = $meta['key'];
    275                                 $_POST['metavalue'] = $meta['value'];
    276                                 add_meta($post_id);
    277                         }
    278277                }
    279278        }
    280279
  • wp-includes/capabilities.php

     
    951951                else
    952952                        $caps[] = $post_type->cap->read_private_posts;
    953953                break;
     954        case 'edit_post_meta':
     955        case 'delete_post_meta':
     956        case 'create_post_meta':
     957                $post = get_post( $args[1] );
     958                $post_type_object = get_post_type_object( $post->post_type );
     959                $caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );
     960
     961                global $_wp_meta;
     962                if ( isset( $_wp_meta[ 'post' ][ $args[ 0 ] ] ) && isset( $_wp_meta[ 'post' ][ $args[ 0 ] ]->auth_callback ) ) {
     963                        $meta_cb = $_wp_meta[ 'post' ][ $args[ 0 ] ]->auth_callback;
     964                        $allowed = call_user_func( $meta_cb, $meta_key, $post->ID, $user_id );
     965                        if ( ! $allowed )
     966                                $caps[] = $cap;
     967                } elseif ( is_protected_meta( $args[ 0 ], 'post' ) ) {
     968                        $caps[] = $cap;
     969                }
     970                break;
    954971        case 'edit_comment':
    955972                $comment = get_comment( $args[0] );
    956973                $post = get_post( $comment->comment_post_ID );
  • wp-includes/meta.php

     
    588588 * @return bool True if the key is protected, false otherwise.
    589589 */
    590590function is_protected_meta( $meta_key, $meta_type = null ) {
    591         $protected = (  '_' == $meta_key[0] );
     591        $protected = ( '_' == $meta_key[0] );
    592592
    593593        return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );
    594594}
    595595
    596596/**
     597 * Determine whether a meta key is hidden
     598 *
     599 * @since 3.2.0
     600 *
     601 * @param string $meta_key Meta key
     602 * @return bool True if the key is hidden, false otherwise.
     603 */
     604function is_hidden_meta( $meta_key, $meta_type = null ) {
     605        $hidden = ( '_' == $meta_key[0] );
     606
     607        return apply_filters( 'is_hidden_meta', $hidden, $meta_key, $meta_type );
     608}
     609
     610/**
    597611 * Sanitize meta value
    598612 *
    599613 * @since 3.1.3
     
    603617 * @param string $meta_type Type of meta
    604618 * @return mixed Sanitized $meta_value
    605619 */
    606 function sanitize_meta( $meta_key, $meta_value, $meta_type = null ) {
    607         return apply_filters( 'sanitize_meta', $meta_value, $meta_key, $meta_type );
     620function sanitize_meta( $meta_key, $meta_value, $meta_type ) {
     621        return apply_filters( "sanitize_{$meta_type}_meta_{$meta_key}", $meta_value, $meta_key, $meta_type );
    608622}
    609623
     624function register_meta( $meta_key, $meta_type, $args = array() ) {
     625        global $_wp_meta;
     626
     627        $_wp_meta[ $meta_type ][ $meta_key ] = (object) $args;
     628}
     629
    610630?>
  • wp-admin/admin-ajax.php

     
    396396        if ( !$meta = get_post_meta_by_id( $id ) )
    397397                die('1');
    398398
    399         if ( !current_user_can( 'edit_post', $meta->post_id ) || is_protected_meta( $meta->meta_key ) )
     399        if ( !current_user_can( 'delete_post_meta', $meta->meta_key, $meta->post_id ) || is_hidden_meta( $meta->meta_key ) )
    400400                die('-1');
    401401        if ( delete_meta( $meta->meta_id ) )
    402402                die('1');
     
    868868                        die(__('Please provide a custom field value.'));
    869869                if ( !$meta = get_post_meta_by_id( $mid ) )
    870870                        die('0'); // if meta doesn't exist
    871                 if ( !current_user_can( 'edit_post', $meta->post_id ) )
     871                if ( is_hidden_meta( $meta->meta_key ) || !current_user_can( 'edit_post_meta', $meta->meta_key, $meta->post_id ) )
    872872                        die('-1');
    873                 if ( is_protected_meta( $meta->meta_key ) )
    874                         die('-1');
    875873                if ( $meta->meta_value != stripslashes($value) || $meta->meta_key != stripslashes($key) ) {
    876874                        if ( !$u = update_meta( $mid, $key, $value ) )
    877875                                die('0'); // We know meta exists; we also know it's unchanged (or DB error, in which case there are bigger problems).
  • wp-admin/includes/post.php

     
    207207                                continue;
    208208                        if ( $meta->post_id != $post_ID )
    209209                                continue;
    210                         if ( is_protected_meta( $value['key'] ) )
     210                        if ( is_hidden_meta( $value['key'] ) || !current_user_can( 'edit_post_meta', $value['key'], $post_ID ) )
    211211                                continue;
    212212                        update_meta( $key, $value['key'], $value['value'] );
    213213                }
     
    219219                                continue;
    220220                        if ( $meta->post_id != $post_ID )
    221221                                continue;
    222                         if ( is_protected_meta( $meta->meta_key ) )
     222                        if ( is_hidden_meta( $meta->meta_key ) || !current_user_can( 'edit_post_meta', $meta->meta_key, $post_ID ) )
    223223                                continue;
    224224                        delete_meta( $key );
    225225                }
     
    662662                if ( $metakeyinput)
    663663                        $metakey = $metakeyinput; // default
    664664
    665                 if ( is_protected_meta( $metakey ) )
     665                if ( is_hidden_meta( $metakey ) || is_protected_meta( $metakey, 'post' ) )
    666666                        return false;
    667667
    668668                wp_cache_delete($post_ID, 'post_meta');
     
    770770
    771771        $meta_key = stripslashes($meta_key);
    772772
    773         if ( is_protected_meta( $meta_key ) )
     773        if ( is_protected_meta( $meta_key, 'post' ) )
    774774                return false;
    775775
    776776        if ( '' === trim( $meta_value ) )
  • wp-admin/includes/template.php

     
    466466function _list_meta_row( $entry, &$count ) {
    467467        static $update_nonce = false;
    468468
    469         if ( is_protected_meta( $entry['meta_key'] ) )
     469        if ( is_hidden_meta( $entry['meta_key'] ) || is_protected_meta( $entry['meta_key'], 'post' ) )
    470470                return;
    471471
    472472        if ( !$update_nonce )