Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (8 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    r41760 r42343  
    5757     */
    5858    public function __construct( $taxonomy ) {
    59         $this->taxonomy = $taxonomy;
     59        $this->taxonomy  = $taxonomy;
    6060        $this->namespace = 'wp/v2';
    61         $tax_obj = get_taxonomy( $taxonomy );
     61        $tax_obj         = get_taxonomy( $taxonomy );
    6262        $this->rest_base = ! empty( $tax_obj->rest_base ) ? $tax_obj->rest_base : $tax_obj->name;
    6363
     
    7474    public function register_routes() {
    7575
    76         register_rest_route( $this->namespace, '/' . $this->rest_base, array(
    77             array(
    78                 'methods'             => WP_REST_Server::READABLE,
    79                 'callback'            => array( $this, 'get_items' ),
    80                 'permission_callback' => array( $this, 'get_items_permissions_check' ),
    81                 'args'                => $this->get_collection_params(),
    82             ),
    83             array(
    84                 'methods'             => WP_REST_Server::CREATABLE,
    85                 'callback'            => array( $this, 'create_item' ),
    86                 'permission_callback' => array( $this, 'create_item_permissions_check' ),
    87                 'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
    88             ),
    89             'schema' => array( $this, 'get_public_item_schema' ),
    90         ) );
    91 
    92         register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
    93             'args' => array(
    94                 'id' => array(
    95                     'description' => __( 'Unique identifier for the term.' ),
    96                     'type'        => 'integer',
    97                 ),
    98             ),
    99             array(
    100                 'methods'             => WP_REST_Server::READABLE,
    101                 'callback'            => array( $this, 'get_item' ),
    102                 'permission_callback' => array( $this, 'get_item_permissions_check' ),
    103                 'args'                => array(
    104                     'context' => $this->get_context_param( array( 'default' => 'view' ) ),
    105                 ),
    106             ),
    107             array(
    108                 'methods'             => WP_REST_Server::EDITABLE,
    109                 'callback'            => array( $this, 'update_item' ),
    110                 'permission_callback' => array( $this, 'update_item_permissions_check' ),
    111                 'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
    112             ),
    113             array(
    114                 'methods'             => WP_REST_Server::DELETABLE,
    115                 'callback'            => array( $this, 'delete_item' ),
    116                 'permission_callback' => array( $this, 'delete_item_permissions_check' ),
    117                 'args'                => array(
    118                     'force' => array(
    119                         'type'        => 'boolean',
    120                         'default'     => false,
    121                         'description' => __( 'Required to be true, as terms do not support trashing.' ),
     76        register_rest_route(
     77            $this->namespace, '/' . $this->rest_base, array(
     78                array(
     79                    'methods'             => WP_REST_Server::READABLE,
     80                    'callback'            => array( $this, 'get_items' ),
     81                    'permission_callback' => array( $this, 'get_items_permissions_check' ),
     82                    'args'                => $this->get_collection_params(),
     83                ),
     84                array(
     85                    'methods'             => WP_REST_Server::CREATABLE,
     86                    'callback'            => array( $this, 'create_item' ),
     87                    'permission_callback' => array( $this, 'create_item_permissions_check' ),
     88                    'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
     89                ),
     90                'schema' => array( $this, 'get_public_item_schema' ),
     91            )
     92        );
     93
     94        register_rest_route(
     95            $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
     96                'args'   => array(
     97                    'id' => array(
     98                        'description' => __( 'Unique identifier for the term.' ),
     99                        'type'        => 'integer',
    122100                    ),
    123101                ),
    124             ),
    125             'schema' => array( $this, 'get_public_item_schema' ),
    126         ) );
     102                array(
     103                    'methods'             => WP_REST_Server::READABLE,
     104                    'callback'            => array( $this, 'get_item' ),
     105                    'permission_callback' => array( $this, 'get_item_permissions_check' ),
     106                    'args'                => array(
     107                        'context' => $this->get_context_param( array( 'default' => 'view' ) ),
     108                    ),
     109                ),
     110                array(
     111                    'methods'             => WP_REST_Server::EDITABLE,
     112                    'callback'            => array( $this, 'update_item' ),
     113                    'permission_callback' => array( $this, 'update_item_permissions_check' ),
     114                    'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
     115                ),
     116                array(
     117                    'methods'             => WP_REST_Server::DELETABLE,
     118                    'callback'            => array( $this, 'delete_item' ),
     119                    'permission_callback' => array( $this, 'delete_item_permissions_check' ),
     120                    'args'                => array(
     121                        'force' => array(
     122                            'type'        => 'boolean',
     123                            'default'     => false,
     124                            'description' => __( 'Required to be true, as terms do not support trashing.' ),
     125                        ),
     126                    ),
     127                ),
     128                'schema' => array( $this, 'get_public_item_schema' ),
     129            )
     130        );
    127131    }
    128132
     
    236240        $prepared_args = apply_filters( "rest_{$this->taxonomy}_query", $prepared_args, $request );
    237241
    238         if ( ! empty( $prepared_args['post'] )  ) {
     242        if ( ! empty( $prepared_args['post'] ) ) {
    239243            $query_result = wp_get_object_terms( $prepared_args['post'], $this->taxonomy, $prepared_args );
    240244
     
    259263
    260264        foreach ( $query_result as $term ) {
    261             $data = $this->prepare_item_for_response( $term, $request );
     265            $data       = $this->prepare_item_for_response( $term, $request );
    262266            $response[] = $this->prepare_response_for_collection( $data );
    263267        }
     
    602606
    603607        $response = new WP_REST_Response();
    604         $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
     608        $response->set_data(
     609            array(
     610                'deleted'  => true,
     611                'previous' => $previous->get_data(),
     612            )
     613        );
    605614
    606615        /**
     
    755764     */
    756765    protected function prepare_links( $term ) {
    757         $base = $this->namespace . '/' . $this->rest_base;
     766        $base  = $this->namespace . '/' . $this->rest_base;
    758767        $links = array(
    759768            'self'       => array(
     
    794803            }
    795804
    796             $rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
     805            $rest_base         = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
    797806            $post_type_links[] = array(
    798807                'href' => add_query_arg( $this->rest_base, $term->term_id, rest_url( sprintf( 'wp/v2/%s', $rest_base ) ) ),
     
    821830            'properties' => array(
    822831                'id'          => array(
    823                     'description'  => __( 'Unique identifier for the term.' ),
    824                     'type'         => 'integer',
    825                     'context'      => array( 'view', 'embed', 'edit' ),
    826                     'readonly'     => true,
     832                    'description' => __( 'Unique identifier for the term.' ),
     833                    'type'        => 'integer',
     834                    'context'     => array( 'view', 'embed', 'edit' ),
     835                    'readonly'    => true,
    827836                ),
    828837                'count'       => array(
    829                     'description'  => __( 'Number of published posts for the term.' ),
    830                     'type'         => 'integer',
    831                     'context'      => array( 'view', 'edit' ),
    832                     'readonly'     => true,
     838                    'description' => __( 'Number of published posts for the term.' ),
     839                    'type'        => 'integer',
     840                    'context'     => array( 'view', 'edit' ),
     841                    'readonly'    => true,
    833842                ),
    834843                'description' => array(
    835                     'description'  => __( 'HTML description of the term.' ),
    836                     'type'         => 'string',
    837                     'context'      => array( 'view', 'edit' ),
     844                    'description' => __( 'HTML description of the term.' ),
     845                    'type'        => 'string',
     846                    'context'     => array( 'view', 'edit' ),
    838847                ),
    839848                'link'        => array(
    840                     'description'  => __( 'URL of the term.' ),
    841                     'type'         => 'string',
    842                     'format'       => 'uri',
    843                     'context'      => array( 'view', 'embed', 'edit' ),
    844                     'readonly'     => true,
     849                    'description' => __( 'URL of the term.' ),
     850                    'type'        => 'string',
     851                    'format'      => 'uri',
     852                    'context'     => array( 'view', 'embed', 'edit' ),
     853                    'readonly'    => true,
    845854                ),
    846855                'name'        => array(
    847                     'description'  => __( 'HTML title for the term.' ),
    848                     'type'         => 'string',
    849                     'context'      => array( 'view', 'embed', 'edit' ),
    850                     'arg_options'  => array(
     856                    'description' => __( 'HTML title for the term.' ),
     857                    'type'        => 'string',
     858                    'context'     => array( 'view', 'embed', 'edit' ),
     859                    'arg_options' => array(
    851860                        'sanitize_callback' => 'sanitize_text_field',
    852861                    ),
    853                     'required'     => true,
     862                    'required'    => true,
    854863                ),
    855864                'slug'        => array(
    856                     'description'  => __( 'An alphanumeric identifier for the term unique to its type.' ),
    857                     'type'         => 'string',
    858                     'context'      => array( 'view', 'embed', 'edit' ),
    859                     'arg_options'  => array(
     865                    'description' => __( 'An alphanumeric identifier for the term unique to its type.' ),
     866                    'type'        => 'string',
     867                    'context'     => array( 'view', 'embed', 'edit' ),
     868                    'arg_options' => array(
    860869                        'sanitize_callback' => array( $this, 'sanitize_slug' ),
    861870                    ),
    862871                ),
    863872                'taxonomy'    => array(
    864                     'description'  => __( 'Type attribution for the term.' ),
    865                     'type'         => 'string',
    866                     'enum'         => array_keys( get_taxonomies() ),
    867                     'context'      => array( 'view', 'embed', 'edit' ),
    868                     'readonly'     => true,
     873                    'description' => __( 'Type attribution for the term.' ),
     874                    'type'        => 'string',
     875                    'enum'        => array_keys( get_taxonomies() ),
     876                    'context'     => array( 'view', 'embed', 'edit' ),
     877                    'readonly'    => true,
    869878                ),
    870879            ),
     
    875884        if ( $taxonomy->hierarchical ) {
    876885            $schema['properties']['parent'] = array(
    877                 'description'  => __( 'The parent term ID.' ),
    878                 'type'         => 'integer',
    879                 'context'      => array( 'view', 'edit' ),
     886                'description' => __( 'The parent term ID.' ),
     887                'type'        => 'integer',
     888                'context'     => array( 'view', 'edit' ),
    880889            );
    881890        }
     
    895904    public function get_collection_params() {
    896905        $query_params = parent::get_collection_params();
    897         $taxonomy = get_taxonomy( $this->taxonomy );
     906        $taxonomy     = get_taxonomy( $this->taxonomy );
    898907
    899908        $query_params['context']['default'] = 'view';
    900909
    901910        $query_params['exclude'] = array(
    902             'description'       => __( 'Ensure result set excludes specific IDs.' ),
    903             'type'              => 'array',
    904             'items'             => array(
    905                 'type'          => 'integer',
     911            'description' => __( 'Ensure result set excludes specific IDs.' ),
     912            'type'        => 'array',
     913            'items'       => array(
     914                'type' => 'integer',
    906915            ),
    907             'default'           => array(),
     916            'default'     => array(),
    908917        );
    909918
    910919        $query_params['include'] = array(
    911             'description'       => __( 'Limit result set to specific IDs.' ),
    912             'type'              => 'array',
    913             'items'             => array(
    914                 'type'          => 'integer',
     920            'description' => __( 'Limit result set to specific IDs.' ),
     921            'type'        => 'array',
     922            'items'       => array(
     923                'type' => 'integer',
    915924            ),
    916             'default'           => array(),
     925            'default'     => array(),
    917926        );
    918927
    919928        if ( ! $taxonomy->hierarchical ) {
    920929            $query_params['offset'] = array(
    921                 'description'       => __( 'Offset the result set by a specific number of items.' ),
    922                 'type'              => 'integer',
     930                'description' => __( 'Offset the result set by a specific number of items.' ),
     931                'type'        => 'integer',
    923932            );
    924933        }
    925934
    926935        $query_params['order'] = array(
    927             'description'       => __( 'Order sort attribute ascending or descending.' ),
    928             'type'              => 'string',
    929             'default'           => 'asc',
    930             'enum'              => array(
     936            'description' => __( 'Order sort attribute ascending or descending.' ),
     937            'type'        => 'string',
     938            'default'     => 'asc',
     939            'enum'        => array(
    931940                'asc',
    932941                'desc',
     
    935944
    936945        $query_params['orderby'] = array(
    937             'description'       => __( 'Sort collection by term attribute.' ),
    938             'type'              => 'string',
    939             'default'           => 'name',
    940             'enum'              => array(
     946            'description' => __( 'Sort collection by term attribute.' ),
     947            'type'        => 'string',
     948            'default'     => 'name',
     949            'enum'        => array(
    941950                'id',
    942951                'include',
     
    951960
    952961        $query_params['hide_empty'] = array(
    953             'description'       => __( 'Whether to hide terms not assigned to any posts.' ),
    954             'type'              => 'boolean',
    955             'default'           => false,
     962            'description' => __( 'Whether to hide terms not assigned to any posts.' ),
     963            'type'        => 'boolean',
     964            'default'     => false,
    956965        );
    957966
    958967        if ( $taxonomy->hierarchical ) {
    959968            $query_params['parent'] = array(
    960                 'description'       => __( 'Limit result set to terms assigned to a specific parent.' ),
    961                 'type'              => 'integer',
     969                'description' => __( 'Limit result set to terms assigned to a specific parent.' ),
     970                'type'        => 'integer',
    962971            );
    963972        }
    964973
    965974        $query_params['post'] = array(
    966             'description'       => __( 'Limit result set to terms assigned to a specific post.' ),
    967             'type'              => 'integer',
    968             'default'           => null,
     975            'description' => __( 'Limit result set to terms assigned to a specific post.' ),
     976            'type'        => 'integer',
     977            'default'     => null,
    969978        );
    970979
    971980        $query_params['slug'] = array(
    972             'description'       => __( 'Limit result set to terms with one or more specific slugs.' ),
    973             'type'              => 'array',
    974             'items'             => array(
    975                 'type'          => 'string'
     981            'description' => __( 'Limit result set to terms with one or more specific slugs.' ),
     982            'type'        => 'array',
     983            'items'       => array(
     984                'type' => 'string',
    976985            ),
    977986        );
Note: See TracChangeset for help on using the changeset viewer.