Making WordPress.org


Ignore:
Timestamp:
11/10/2020 04:24:59 AM (4 years ago)
Author:
dd32
Message:

Plugin Directory: Cache the list of meta keys for the Custom Fields metabox.

This avoids running the ~5s query on each plugin edit admin view, and removes the many translated meta caches.
This could have been simply __return_empty_array() but there's the potential this would be useful in future for other things.

File:
1 edited

Legend:

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

    r10214 r10437  
    4949        add_action( 'add_meta_boxes', array( $this, 'register_admin_metaboxes' ), 10, 2 );
    5050        add_action( 'do_meta_boxes', array( $this, 'replace_title_global' ) );
     51
     52        add_filter( 'postmeta_form_keys', array( $this, 'postmeta_form_keys' ) );
    5153
    5254        add_filter( 'postbox_classes_plugin_internal-notes', array( __NAMESPACE__ . '\Metabox\Internal_Notes', 'postbox_classes' ) );
     
    658660        $x->send();
    659661    }
     662
     663    /**
     664     * Cache / Filter the keys to display in the dropdown list for custom post meta.
     665     */
     666    function postmeta_form_keys( $keys ) {
     667        global $wpdb;
     668        if ( ! is_null( $keys ) ) {
     669            return $keys;
     670        }
     671
     672        // We're going to cache this for 24hrs.. that might be enough.
     673        $keys = wp_cache_get( __METHOD__, 'distinct-meta-keys' );
     674
     675        if ( ! $keys ) {
     676            // Exclude the translated meta fields.
     677            $keys = $wpdb->get_col(
     678                "SELECT DISTINCT meta_key
     679                FROM $wpdb->postmeta
     680                WHERE meta_key NOT BETWEEN '_' AND '_z'
     681                HAVING
     682                    meta_key NOT LIKE '\_%' AND
     683                    meta_key NOT LIKE 'title\_%' AND
     684                    meta_key NOT LIKE 'block_title\_%' AND
     685                    meta_key NOT LIKE 'excerpt\_%' AND
     686                    meta_key NOT LIKE 'content\_%'
     687                ORDER BY meta_key
     688                LIMIT 300",
     689            );
     690        }
     691
     692        wp_cache_set( __METHOD__, $keys, 'distinct-meta-keys', DAY_IN_SECONDS );
     693
     694        return $keys;
     695    }
    660696}
Note: See TracChangeset for help on using the changeset viewer.