Making WordPress.org

Changeset 2991


Ignore:
Timestamp:
04/21/2016 05:57:55 AM (10 years ago)
Author:
dd32
Message:

Plugin Directory: Add a shim to allow us to retrieve the downloads count from postmeta (although the count is stored elsewhere for now).

See #1596

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/admin/list-table/class-plugin-posts.php

    r2986 r2991  
    199199         */
    200200        public function column_downloads( $post ) {
    201                 if ( ! empty( $this->plugin_meta->downloaded ) ) {
    202                         echo number_format_i18n( $this->plugin_meta->downloaded );
    203                 }
     201                echo number_format_i18n( get_post_meta( $post->ID, 'downloads', true ) );
    204202        }
    205203
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php

    r2985 r2991  
    2929
    3030                add_filter( 'map_meta_cap', array( __NAMESPACE__ . '\Capabilities', 'map_meta_cap' ), 10, 4 );
     31
     32                // Shim in postmeta support for data which doesn't yet live in postmeta
     33                add_filter( 'get_post_metadata', array( $this, 'filter_shim_postmeta' ), 10, 4 );
    3134
    3235                // Load the API routes
     
    405408        }
    406409
     410
     411        /**
     412         * Shim in some postmeta values which get retrieved from other locations temporarily.
     413         *
     414         * @param null|array|string $value     The value get_metadata() should return - a single metadata value,
     415         *                                     or an array of values.
     416         * @param int               $object_id Object ID.
     417         * @param string            $meta_key  Meta key.
     418         * @param bool              $single    Whether to return only the first value of the specified $meta_key.
     419         */
     420        public function filter_shim_postmeta( $value, $object_id, $meta_key, $single ) {
     421                switch ( $meta_key ) {
     422                        case 'downloads':
     423                                $post = get_post( $object_id );
     424                                $count = Template::get_downloads_count( $post );
     425
     426                                return $single ? $count : array( $count );                             
     427                                break;
     428                }
     429                return $value;
     430        }
     431
    407432        /**
    408433         * Returns an array of pages based on section comments in the content.
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-template.php

    r2986 r2991  
    2020
    2121        /**
     22         * @param \WP_Post|int $post Optional.
     23         * @return int
     24         */
     25        static function get_downloads_count( $post = null ) {
     26                $post = get_post( $post );
     27
     28                if ( false === ( $count = wp_cache_get( $post->ID, 'plugin_download_count' ) ) ) {
     29                        global $wpdb;
     30
     31                        // TODO: While the plugin ZIPs are still being served by bbPress, the download stats are stored there.
     32                        $count = $wpdb->get_var( $wpdb->prepare( "SELECT downloads FROM `" . PLUGINS_TABLE_PREFIX . "download_counts` WHERE topic_id = (SELECT topic_id FROM `" . PLUGINS_TABLE_PREFIX . "topics` WHERE topic_slug = %s )", $post->post_name ) );
     33
     34                        wp_cache_set( $post->ID, $count, 'plugin_download_count', HOUR_IN_SECONDS );
     35                }
     36
     37                return (int) $count;
     38        }
     39
     40        /**
    2241         * @return int
    2342         */
     
    2645                        global $wpdb;
    2746
    28                         $count = $wpdb->get_var( "SELECT SUM(downloads) FROM `plugin_2_stats`" );
    29                         wp_cache_add( 'plugin_download_count', $count, 'plugin_download_count', DAY_IN_SECONDS );
     47                        $count = $wpdb->get_var( "SELECT SUM(downloads) FROM `" . PLUGINS_TABLE_PREFIX . "stats`" );
     48                        wp_cache_set( 'plugin_download_count', $count, 'plugin_download_count', DAY_IN_SECONDS );
    3049                }
    3150
Note: See TracChangeset for help on using the changeset viewer.