Make WordPress Core

Ticket #34012: 34012.diff

File 34012.diff, 3.4 KB (added by MikeHansenMe, 9 years ago)

Adds basic tests and includes previous patch

  • src/wp-admin/includes/post.php

     
    657657/**
    658658 * Determine if a post exists based on title, content, and date
    659659 *
     660 * For best performance use the $date, $type, and $status together
     661 * all of which are required to properly use a compound index.
     662 * $date is left where it is positionally for legacy reasons
     663 *
    660664 * @since 2.0.0
     665 * @since 4.4.0 Added the $type, $status parameters
    661666 *
    662667 * @global wpdb $wpdb
    663668 *
     
    664669 * @param string $title Post title
    665670 * @param string $content Optional post content
    666671 * @param string $date Optional post date
     672 * @param string $type Optional post type
     673 * @param string $status Optional post status
    667674 * @return int Post ID if post exists, 0 otherwise.
    668675 */
    669 function post_exists($title, $content = '', $date = '') {
     676function post_exists( $title, $content = '', $date = '', $type = '', $status = '' ) {
    670677        global $wpdb;
    671678
    672         $post_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
    673         $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
    674         $post_date = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
    675 
    676         $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
    677         $args = array();
    678 
    679         if ( !empty ( $date ) ) {
    680                 $query .= ' AND post_date = %s';
    681                 $args[] = $post_date;
     679        $query = array();
     680        foreach( compact( 'title', 'content', 'date', 'type', 'status' ) as $key => $val ) {
     681                if ( empty( $val ) ) {
     682                        continue;
     683                }
     684                $key = sprintf( 'post_%s', $key );
     685                $query[] = $wpdb->prepare(
     686                        $key . ' = %s',
     687                        wp_unslash( sanitize_post_field( $key, $val, 0, 'db' ) )
     688                );
    682689        }
    683690
    684         if ( !empty ( $title ) ) {
    685                 $query .= ' AND post_title = %s';
    686                 $args[] = $post_title;
     691        if ( empty( $query ) ) {
     692                return 0;
    687693        }
    688694
    689         if ( !empty ( $content ) ) {
    690                 $query .= 'AND post_content = %s';
    691                 $args[] = $post_content;
    692         }
    693 
    694         if ( !empty ( $args ) )
    695                 return (int) $wpdb->get_var( $wpdb->prepare($query, $args) );
    696 
    697         return 0;
     695        return (int)$wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE 1=1 AND " . implode( ' AND ', $query ) . ' LIMIT 1' );
    698696}
    699697
    700698/**
  • tests/phpunit/tests/post/post-exists.php

     
     1<?php
     2
     3/**
     4 * @group post
     5 */
     6class Tests_Post_Exists extends WP_UnitTestCase {
     7        function setUp() {
     8                parent::setUp();
     9                $this->post1 = $this->factory->post->create( array( 'post_title' => 'Post One Title', 'post_content' => 'Post One Content' ) );
     10                $this->post2 = $this->factory->post->create( array( 'post_title' => 'Post Two Title', 'post_content' => 'Post Two Content' ) );
     11        }
     12
     13        function tearDown() {
     14                wp_delete_post($this->post1);
     15                wp_delete_post($this->post2);
     16                parent::tearDown();
     17        }
     18
     19        function test_post_exists_title() {
     20                $post = post_exists( 'Post One Title' );
     21                $this->assertEquals( $post, $this->post1 );
     22        }
     23
     24        function test_post_exists_bad_title() {
     25                $post = post_exists( 'No existing Title' );
     26                $this->assertEquals( $post, 0 );
     27        }
     28
     29        function test_post_exists_content() {
     30                $post = post_exists( 'Post One Title', 'Post One Content' );
     31                $this->assertEquals( $post, $this->post1 );
     32        }
     33
     34}
     35 No newline at end of file