Making WordPress.org


Ignore:
Timestamp:
08/14/2020 02:52:59 AM (5 years ago)
Author:
dd32
Message:

Plugin Directory: API: Add a search bypass for exact slug/block matches.

This is intended for internal use only for now, and support may be withdrawn.

  • "slug:my-plugin" will match the my-plugin block only.
  • "block:my-plugin/super-awesome-block" will match a Block Directory plugin, or regular plugins(s) if nothing in the block directory.
File:
1 edited

Legend:

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

    r10117 r10170  
    7777        }
    7878
     79        /*
     80         * Allow an API search bypass for exact-post matches.
     81         * - `slug:example-plugin` will only return THAT plugin, nothing else.
     82         * - `block:example-plugin/my-block` will return Block directory plugins, or
     83         *    regular plugins that supply that block if there were no matches in the block directory.
     84         *
     85         * TODO: This might have been useful as a general search filter for the website too.
     86         */
     87        if ( !empty( $query['s'] ) ) {
     88            if ( 'slug:' === substr( $query['s'], 0, 5 ) ) {
     89                $query['name'] = substr( $query['s'], 5 );
     90
     91                unset( $query['s'] );
     92            }
     93
     94            if ( 'block:' === substr( $query['s'], 0, 6 ) ) {
     95                $query['meta_query'][] = [
     96                    'key' => 'block_name',
     97                    'value' => substr( $query['s'], 6 )
     98                ];
     99
     100                $query['tax_query'][] = [
     101                    'taxonomy' => 'plugin_section',
     102                    'field' => 'slug',
     103                    'terms' => 'block',
     104                ];
     105
     106                // Prioritise block plugins, but try again without the restriction.
     107                $try_again_without_tax_query = true;
     108
     109                unset( $query['s'], $query['block_search'] );
     110            }
     111
     112        }
     113
    79114        $query['post_type']   = 'plugin';
    80115        $query['post_status'] = 'publish';
     
    82117        // Use the main query so that is_main_query() is triggered for the filters.
    83118        $wp_query->query( $query );
     119
     120        // Maybe retry without the the block-specific query if no plugins were found.
     121        if ( ! $wp_query->found_posts && isset( $try_again_without_tax_query ) ) {
     122            unset( $query['tax_query'] );
     123            $wp_query->query( $query );
     124        }
    84125
    85126        $response['info']['page']    = (int) $wp_query->get( 'paged' ) ?: 1;
Note: See TracChangeset for help on using the changeset viewer.