Make WordPress Core

Ticket #24266: 24266.4.diff

File 24266.4.diff, 6.4 KB (added by swissspidy, 8 years ago)
  • src/wp-includes/default-filters.php

    diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php
    index 1faa084..98f7b32 100644
    add_action( 'admin_menu', '_add_post_type_submenus' ); 
    388388add_action( 'before_delete_post', '_reset_front_page_settings_for_post' );
    389389add_action( 'wp_trash_post',      '_reset_front_page_settings_for_post' );
    390390
     391// Post meta
     392add_action( 'updated_postmeta',  '_collect_updated_post_meta_post_ids', 10, 2 );
     393add_action( 'deleted_post_meta', '_collect_updated_post_meta_post_ids', 10, 2 );
     394add_action( 'added_post_meta',   '_collect_updated_post_meta_post_ids', 10, 2 );
     395add_action( 'shutdown',          'update_post_modified_dates' );
     396
    391397// Post Formats
    392398add_filter( 'request', '_post_format_request' );
    393399add_filter( 'term_link', '_post_format_link', 10, 3 );
  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index 6535d4b..63f4499 100644
    function clean_post_cache( $post ) { 
    55355535}
    55365536
    55375537/**
     5538 * Internal function that collects all updated post IDs when meta was changed.
     5539 *
     5540 * Modified post date will be updated later on the shutdown hook.
     5541 *
     5542 * @since 4.5
     5543 * @access private
     5544 *
     5545 * @see update_post_modified_dates()
     5546 * @global array $updated_post_ids List of posts whose meta data has changed.
     5547 *
     5548 * @param int|array $meta_id Single modified meta ID or array of such.
     5549 * @param int       $post_id Post ID.
     5550 */
     5551function _collect_updated_post_meta_post_ids( $meta_id, $post_id ) {
     5552        global $updated_post_ids;
     5553
     5554        $updated_post_ids = (array) $updated_post_ids;
     5555        $updated_post_ids[] = (int) $post_id;
     5556}
     5557
     5558/**
     5559 * Helper function for updating the post modified date.
     5560 *
     5561 * Iterates over the IDS of all posts whose meta data has changed.
     5562 *
     5563 * @since 4.5.0
     5564 *
     5565 * @global array $updated_post_ids List of posts whose meta data has changed.
     5566 */
     5567function update_post_modified_dates() {
     5568        global $updated_post_ids;
     5569
     5570        $updated_post_ids = array_unique( (array) $updated_post_ids );
     5571
     5572        // Check if there are any posts with modified meta data.
     5573        if ( ! empty( $updated_post_ids ) ) {
     5574                global $wpdb;
     5575
     5576                // Update the post_modified dates for each modified post.
     5577                $wpdb->query(
     5578                        $wpdb->prepare(
     5579                                "UPDATE $wpdb->posts
     5580                                        SET `post_modified` = %s,
     5581                                        `post_modified_gmt` = %s
     5582                                        WHERE `ID` IN(" . implode( ',', $updated_post_ids ) . ")",
     5583                                current_time( 'mysql' ),
     5584                                current_time( 'mysql', 1 )
     5585                        )
     5586                );
     5587
     5588                foreach ( $updated_post_ids as $post_id ) {
     5589                        clean_post_cache( $post_id );
     5590                }
     5591        }
     5592
     5593        $updated_post_ids = array();
     5594}
     5595
     5596/**
    55385597 * Call major cache updating functions for list of Post objects.
    55395598 *
    55405599 * @since 1.5.0
  • new file tests/phpunit/tests/meta/postModifiedDate.php

    diff --git tests/phpunit/tests/meta/postModifiedDate.php tests/phpunit/tests/meta/postModifiedDate.php
    new file mode 100644
    index 0000000..1f8d619
    - +  
     1<?php
     2
     3/**
     4 * @group meta
     5 */
     6class Tests_Meta_Post_Modified_Date extends WP_UnitTestCase {
     7        protected static $posts;
     8
     9        public static function wpSetUpBeforeClass( $factory ) {
     10                self::$posts = $factory->post->create_many( 3, array(
     11                        'post_date'     => '2000-01-01 00:00:00',
     12                        'post_date_gmt' => '2000-01-01 00:00:00',
     13                ) );
     14        }
     15
     16        public static function wpTearDownAfterClass() {
     17                foreach ( self::$posts as $post ) {
     18                        wp_delete_post( $post );
     19                }
     20        }
     21
     22        /**
     23         * @ticket 24266
     24         */
     25        function test_add_post_meta_changes_post_modified_date() {
     26                foreach ( self::$posts as $post ) {
     27                        add_post_meta( $post, '_some_meta', 'abc' );
     28                }
     29
     30                update_post_modified_dates();
     31
     32                foreach ( self::$posts as $post ) {
     33                        $this->assertSame( current_time( 'mysql' ), get_post_modified_time( 'Y-m-d H:i:s', false, $post ) );
     34                        $this->assertSame( current_time( 'mysql', 1 ), get_post_modified_time( 'Y-m-d H:i:s', true, $post ) );
     35                }
     36        }
     37
     38        /**
     39         * @ticket 24266
     40         */
     41        function test_add_unique_post_meta_changes_post_modified_date() {
     42                foreach ( self::$posts as $post ) {
     43                        $this->assertSame( '2000-01-01 00:00:00', get_post_modified_time( 'Y-m-d H:i:s', false, $post ) );
     44                        $this->assertSame( '2000-01-01 00:00:00', get_post_modified_time( 'Y-m-d H:i:s', true, $post ) );
     45                        add_post_meta( $post, '_some_meta', 'abc', true );
     46                }
     47
     48                update_post_modified_dates();
     49
     50                foreach ( self::$posts as $post ) {
     51                        $this->assertSame( current_time( 'mysql' ), get_post_modified_time( 'Y-m-d H:i:s', false, $post ) );
     52                        $this->assertSame( current_time( 'mysql', 1 ), get_post_modified_time( 'Y-m-d H:i:s', true, $post ) );
     53                }
     54        }
     55
     56        /**
     57         * @ticket 24266
     58         */
     59        function test_update_post_meta_changes_post_modified_date() {
     60                foreach ( self::$posts as $post ) {
     61                        $this->assertSame( '2000-01-01 00:00:00', get_post_modified_time( 'Y-m-d H:i:s', false, $post ) );
     62                        $this->assertSame( '2000-01-01 00:00:00', get_post_modified_time( 'Y-m-d H:i:s', true, $post ) );
     63                        update_post_meta( $post, '_some_meta', 'abc' );
     64                }
     65
     66                update_post_modified_dates();
     67
     68                foreach ( self::$posts as $post ) {
     69                        $this->assertSame( current_time( 'mysql' ), get_post_modified_time( 'Y-m-d H:i:s', false, $post ) );
     70                        $this->assertSame( current_time( 'mysql', 1 ), get_post_modified_time( 'Y-m-d H:i:s', true, $post ) );
     71                }
     72        }
     73
     74        /**
     75         * @ticket 24266
     76         */
     77        function test_delete_post_meta_changes_post_modified_date() {
     78                foreach ( self::$posts as $post ) {
     79                        $this->assertSame( '2000-01-01 00:00:00', get_post_modified_time( 'Y-m-d H:i:s', false, $post ) );
     80                        $this->assertSame( '2000-01-01 00:00:00', get_post_modified_time( 'Y-m-d H:i:s', true, $post ) );
     81                        add_post_meta( $post, '_some_meta', 'abc' );
     82                }
     83
     84                $GLOBALS['update_post_ids'] = array();
     85
     86                foreach ( self::$posts as $post ) {
     87                        delete_post_meta( $post, '_some_meta' );
     88                }
     89
     90                update_post_modified_dates();
     91
     92                foreach ( self::$posts as $post ) {
     93                        $this->assertSame( current_time( 'mysql' ), get_post_modified_time( 'Y-m-d H:i:s', false, $post ) );
     94                        $this->assertSame( current_time( 'mysql', 1 ), get_post_modified_time( 'Y-m-d H:i:s', true, $post ) );
     95                }
     96        }
     97}
  • tests/phpunit/tests/post/meta.php

    diff --git tests/phpunit/tests/post/meta.php tests/phpunit/tests/post/meta.php
    index 82c5c12..255dfb5 100644
    class Tests_Post_Meta extends WP_UnitTestCase { 
    235235
    236236                //Check they exists
    237237                $this->assertEquals($funky_meta, get_post_meta($this->post_id, 'test_funky_post_meta', true));
    238 
    239238        }
    240239}