Making WordPress.org

Changeset 4423


Ignore:
Timestamp:
11/26/2016 03:47:38 AM (8 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.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
Files:
3 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
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php

    r4389 r4423  
    147147        }
    148148
     149        // Update the tested-up-to value
     150        $tested = $readme->tested;
     151        if ( function_exists( 'wporg_get_version_equivalents' ) ) {
     152            foreach ( wporg_get_version_equivalents() as $latest_compatible_version => $compatible_with ) {
     153                if ( in_array( $readme->tested, $compatible_with, true ) ) {
     154                    $tested = $latest_compatible_version;
     155                    break;
     156                }
     157            }
     158        }
     159
     160        // Update all readme meta
    149161        foreach ( $this->readme_fields as $readme_field ) {
    150             // Don't change the tested version if a newer version was specified through wp-admin
    151             if ( 'tested' == $readme_field && version_compare( get_post_meta( $plugin->ID, 'tested', true ), $readme->$readme_field, '>' ) ) {
    152                 continue;
    153             }
    154 
    155             update_post_meta( $plugin->ID, $readme_field, wp_slash( $readme->$readme_field ) );
     162            $value = ( 'tested' == $readme_field ) ? $tested : $readme->field;
     163            update_post_meta( $plugin->ID, $readme_field, wp_slash( $value ) );
    156164        }
    157165
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/jobs/class-meta-sync.php

    r4420 r4423  
    2424        $this->sync_downloads();
    2525        $this->sync_ratings();
     26        $this->update_tested_up_to();
    2627    }
    2728
     
    3940            FROM `{$wpdb->posts}` p
    4041                JOIN `{$bbpress_topic_slug_table}` t ON p.post_name = t.topic_slug
    41                 LEFT JOIN `{$download_count_table}` c on t.topic_id = c.topic_id
     42                JOIN `{$download_count_table}` c on t.topic_id = c.topic_id
    4243                LEFT JOIN `{$wpdb->postmeta}` pm ON p.id = pm.post_id AND pm.meta_key = 'downloads'
    4344
     
    9697        update_option( 'plugin_last_review_sync', $current_review_time, 'no' );
    9798    }
     99
     100    /**
     101     * After WordPress is released, update the 'tested' meta keys to the latest version as
     102     * specified by `wporg_get_version_equivalents()`.
     103     */
     104    function update_tested_up_to() {
     105        global $wpdb;
     106        if ( ! function_exists( 'wporg_get_version_equivalents' ) ) {
     107            return;
     108        }
     109
     110        $equivs = wporg_get_version_equivalents();
     111        $equivs_key = md5( serialize( $equivs ) );
     112        if ( $equivs_key === get_option( 'plugin_last_tested_sync' ) ) {
     113            return;
     114        }
     115
     116        $latest_equiv = array();
     117        foreach ( $equivs as $latest_compatible_version => $compatible_with ) {
     118            foreach ( $compatible_with as $version ) {
     119                $latest_equiv[ $version ] = $latest_compatible_version;
     120            }
     121        }
     122
     123        $tested_meta_value_esc_sql = '"' . implode( '", "', array_map( 'esc_sql', array_keys( $latest_equiv ) ) ) . '"';
     124        $tested_values = $wpdb->get_results( "SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = 'tested' AND meta_value IN( {$tested_meta_value_esc_sql} )" );
     125
     126        foreach ( $tested_values as $row ) {
     127            update_post_meta(
     128                $row->post_id,
     129                'tested',
     130                $latest_equiv[ $row->meta_value ]
     131            );
     132        }
     133
     134        update_option( 'plugin_last_tested_sync', $equivs_key );
     135    }
    98136}
Note: See TracChangeset for help on using the changeset viewer.