WordPress.org

Make WordPress Core


Ignore:
Timestamp:
08/18/2016 06:20:55 PM (5 years ago)
Author:
wonderboymusic
Message:

Query: add a protected field, $db, (composition, as it were) to WP_*_Query classes to hold the value for the database abstraction, instead of importing the global $wpdb into every method that uses it. Reduces the number of global imports by 32.

See #37699.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-xmlrpc-server.php

    r38077 r38275  
    5555
    5656    /**
     57     * @since 4.7.0
     58     * @access protected
     59     * @var wpdb
     60     */
     61    protected $db;
     62
     63    /**
    5764     * Registers all of the XMLRPC methods that XMLRPC server understands.
    5865     *
     
    6471     */
    6572    public function __construct() {
     73        $this->db = $GLOBALS['wpdb'];
     74
    6675        $this->methods = array(
    6776            // WordPress API
     
    28832892     * @since 2.2.0
    28842893     *
    2885      * @global wpdb $wpdb WordPress database abstraction object.
    2886      *
    28872894     * @param array  $args {
    28882895     *     Method arguments. Note: arguments must be ordered as documented.
     
    28952902     */
    28962903    public function wp_getPageList( $args ) {
    2897         global $wpdb;
    2898 
    28992904        $this->escape( $args );
    29002905
     
    29122917
    29132918        // Get list of pages ids and titles
    2914         $page_list = $wpdb->get_results("
     2919        $page_list = $this->db->get_results("
    29152920            SELECT ID page_id,
    29162921                post_title page_title,
     
    29192924                post_date,
    29202925                post_status
    2921             FROM {$wpdb->posts}
     2926            FROM {$this->db->posts}
    29222927            WHERE post_type = 'page'
    29232928            ORDER BY ID
     
    51315136     * @since 2.1.0
    51325137     *
    5133      * @global wpdb $wpdb WordPress database abstraction object.
    5134      *
    51355138     * @param int $post_ID Post ID.
    51365139     * @param string $post_content Post Content for attachment.
    51375140     */
    51385141    public function attach_uploads( $post_ID, $post_content ) {
    5139         global $wpdb;
    5140 
    51415142        // find any unattached files
    5142         $attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
     5143        $attachments = $this->db->get_results( "SELECT ID, guid FROM {$this->db->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
    51435144        if ( is_array( $attachments ) ) {
    51445145            foreach ( $attachments as $file ) {
    5145                 if ( ! empty( $file->guid ) && strpos( $post_content, $file->guid ) !== false )
    5146                     $wpdb->update($wpdb->posts, array('post_parent' => $post_ID), array('ID' => $file->ID) );
     5146                if ( ! empty( $file->guid ) && strpos( $post_content, $file->guid ) !== false ) {
     5147                    $this->db->update( $this->db->posts, array( 'post_parent' => $post_ID ), array( 'ID' => $file->ID ) );
     5148                }
    51475149            }
    51485150        }
     
    57645766     * @since 1.5.0
    57655767     *
    5766      * @global wpdb $wpdb WordPress database abstraction object.
    5767      *
    57685768     * @param array  $args {
    57695769     *     Method arguments. Note: arguments must be ordered as documented.
     
    57775777     */
    57785778    public function mw_newMediaObject( $args ) {
    5779         global $wpdb;
    5780 
    57815779        $username = $this->escape( $args[1] );
    57825780        $password = $this->escape( $args[2] );
     
    61036101     * @since 1.5.0
    61046102     *
    6105      * @global wpdb $wpdb WordPress database abstraction object.
    6106      *
    61076103     * @param int $post_ID
    61086104     * @return array|IXR_Error
    61096105     */
    61106106    public function mt_getTrackbackPings( $post_ID ) {
    6111         global $wpdb;
    6112 
    61136107        /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
    61146108        do_action( 'xmlrpc_call', 'mt.getTrackbackPings' );
     
    61196113            return new IXR_Error(404, __('Sorry, no such post.'));
    61206114
    6121         $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
     6115        $comments = $this->db->get_results( $this->db->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM {$this->db->comments} WHERE comment_post_ID = %d", $post_ID) );
    61226116
    61236117        if ( !$comments )
     
    61936187     * @since 1.5.0
    61946188     *
    6195      * @global wpdb $wpdb WordPress database abstraction object.
    61966189     * @global string $wp_version
    61976190     *
     
    62056198     */
    62066199    public function pingback_ping( $args ) {
    6207         global $wpdb, $wp_version;
     6200        global $wp_version;
    62086201
    62096202        /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
     
    62596252                // ...or a string #title, a little more complicated
    62606253                $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']);
    6261                 $sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", $title );
    6262                 if (! ($post_ID = $wpdb->get_var($sql)) ) {
     6254                $sql = $this->db->prepare("SELECT ID FROM {$this->db->posts} WHERE post_title RLIKE %s", $title );
     6255                if (! ($post_ID = $this->db->get_var($sql)) ) {
    62636256                    // returning unknown error '0' is better than die()ing
    62646257                    return $this->pingback_error( 0, '' );
     
    62846277
    62856278        // Let's check that the remote site didn't already pingback this entry
    6286         if ( $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) )
     6279        if ( $this->db->get_results( $this->db->prepare("SELECT * FROM {$this->db->comments} WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) )
    62876280            return $this->pingback_error( 48, __( 'The pingback has already been registered.' ) );
    62886281
     
    64096402     * @since 1.5.0
    64106403     *
    6411      * @global wpdb $wpdb WordPress database abstraction object.
    6412      *
    64136404     * @param string $url
    64146405     * @return array|IXR_Error
    64156406     */
    64166407    public function pingback_extensions_getPingbacks( $url ) {
    6417         global $wpdb;
    6418 
    64196408        /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
    64206409        do_action( 'xmlrpc_call', 'pingback.extensions.getPingbacks' );
     
    64356424        }
    64366425
    6437         $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
     6426        $comments = $this->db->get_results( $this->db->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM {$this->db->comments} WHERE comment_post_ID = %d", $post_ID) );
    64386427
    64396428        if ( !$comments )
Note: See TracChangeset for help on using the changeset viewer.