Making WordPress.org


Ignore:
Timestamp:
11/26/2016 03:47:38 AM (10 years ago)
Author:
dd32
Message:

Plugin Directory: Update the tested postmeta field when new versions of WordPress are released.
This removes the need for having a postmeta display filter.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php

    r4422 r4423  
    5050        add_filter( 'oembed_providers', array( $this, 'oembed_whitelist' ) );
    5151
    52         // Shim in postmeta support for data which doesn't yet live in postmeta.
    53         add_filter( 'get_post_metadata', array( $this, 'filter_shim_postmeta' ), 10, 3 );
    54 
     52        // Capability mapping
    5553        add_filter( 'map_meta_cap', array( __NAMESPACE__ . '\Capabilities', 'map_meta_cap' ), 10, 4 );
    5654
     
    951949            return false;
    952950        } );
    953     }
    954 
    955     /**
    956      * Shim in some postmeta values which get retrieved from other locations temporarily.
    957      *
    958      * @param null|array|string $value     The value get_metadata() should return - a single metadata value,
    959      *                                     or an array of values.
    960      * @param int               $object_id Object ID.
    961      * @param string            $meta_key  Meta key.
    962      * @return array
    963      */
    964     public function filter_shim_postmeta( $value, $object_id, $meta_key ) {
    965         switch ( $meta_key ) {
    966             case 'tested':
    967                 // For the tested field, we bump up the minor release to the latest compatible minor release.
    968                 if ( function_exists( 'wporg_get_version_equivalents' ) ) {
    969 
    970                     // As we're on a pre-filter, we'll have to detach, fetch and reattach..
    971                     remove_filter( 'get_post_metadata', array( $this, 'filter_shim_postmeta' ) );
    972                     $value = get_metadata( 'post', $object_id, $meta_key );
    973                     add_filter( 'get_post_metadata', array( $this, 'filter_shim_postmeta' ), 10, 3 );
    974 
    975                     foreach ( wporg_get_version_equivalents() as $latest_compatible_version => $compatible_with ) {
    976                         if ( in_array( (string)$value[0], $compatible_with, true ) ) {
    977                             $value[0] = $latest_compatible_version;
    978                             break;
    979                         }
    980                     }
    981                 }
    982                 break;
    983 
    984             case false:
    985 
    986                 // In the event $meta_key is false, the caller wants all meta fields, so we'll append our custom ones here too.
    987                 remove_filter( 'get_post_metadata', array( $this, 'filter_shim_postmeta' ) );
    988 
    989                 // Fetch the existing ones from the database
    990                 $value = get_metadata( 'post', $object_id, $meta_key );
    991 
    992                 // Re-attach ourselves for next time!
    993                 add_filter( 'get_post_metadata', array( $this, 'filter_shim_postmeta' ), 10, 3 );
    994 
    995                 $custom_meta_fields = array( 'downloads', 'rating', 'ratings', 'tested' );
    996                 $custom_meta_fields = apply_filters( 'wporg_plugins_custom_meta_fields', $custom_meta_fields, $object_id );
    997 
    998                 foreach ( $custom_meta_fields as $key ) {
    999 
    1000                     // When WordPress calls `get_post_meta( $post_id, false )` it expects an array of maybe_serialize()'d data.
    1001                     $shimed_data = $this->filter_shim_postmeta( false, $object_id, $key );
    1002                     if ( $shimed_data ) {
    1003                         $value[ $key ][0] = (string) maybe_serialize( $shimed_data[0] );
    1004                     }
    1005                 }
    1006                 break;
    1007 
    1008         }
    1009 
    1010         return $value;
    1011951    }
    1012952
Note: See TracChangeset for help on using the changeset viewer.