Make WordPress Core

Ticket #28425: 28425.test.php

File 28425.test.php, 4.0 KB (added by SergeyBiryukov, 11 years ago)
Line 
1<?php
2require 'wp-load.php';
3
4function get_permalink_with_obj( $id = 0, $leavename = false ) {
5        $rewritecode = array(
6                '%year%',
7                '%monthnum%',
8                '%day%',
9                '%hour%',
10                '%minute%',
11                '%second%',
12                $leavename? '' : '%postname%',
13                '%post_id%',
14                '%category%',
15                '%author%',
16                $leavename? '' : '%pagename%',
17        );
18
19        if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) {
20                $post = $id;
21                $sample = true;
22        } else {
23                $post = get_post($id);
24                $sample = false;
25        }
26
27        if ( empty($post->ID) )
28                return false;
29
30        if ( $post->post_type == 'page' )
31                return get_page_link($post, $leavename, $sample);
32        elseif ( $post->post_type == 'attachment' )
33                return get_attachment_link( $post->ID, $leavename );
34        elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
35                return get_post_permalink($post->ID, $leavename, $sample);
36
37        $permalink = get_option('permalink_structure');
38
39        /**
40         * Filter the permalink structure for a post before token replacement occurs.
41         *
42         * Only applies to posts with post_type of 'post'.
43         *
44         * @since 3.0.0
45         *
46         * @param string  $permalink The site's permalink structure.
47         * @param WP_Post $post      The post in question.
48         * @param bool    $leavename Whether to keep the post name.
49         */
50        $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
51
52        if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
53                $unixtime = strtotime($post->post_date);
54
55                $category = '';
56                if ( strpos($permalink, '%category%') !== false ) {
57                        $cats = get_the_category($post->ID);
58                        if ( $cats ) {
59                                usort($cats, '_usort_terms_by_ID'); // order by ID
60
61                                /**
62                                 * Filter the category that gets used in the %category% permalink token.
63                                 *
64                                 * @since 3.5.0
65                                 *
66                                 * @param stdClass $cat  The category to use in the permalink.
67                                 * @param array    $cats Array of all categories associated with the post.
68                                 * @param WP_Post  $post The post in question.
69                                 */
70                                $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
71
72                                $category_object = get_term( $category_object, 'category' );
73                                $category = $category_object->slug;
74                                if ( $parent = $category_object->parent )
75                                        $category = get_category_parents($parent, false, '/', true) . $category;
76                        }
77                        // show default category in permalinks, without
78                        // having to assign it explicitly
79                        if ( empty($category) ) {
80                                $default_category = get_term( get_option( 'default_category' ), 'category' );
81                                $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
82                        }
83                }
84
85                $author = '';
86                if ( strpos($permalink, '%author%') !== false ) {
87                        $authordata = get_userdata($post->post_author);
88                        $author = $authordata->user_nicename;
89                }
90
91                $date = explode(" ",date('Y m d H i s', $unixtime));
92                $rewritereplace =
93                array(
94                        $date[0],
95                        $date[1],
96                        $date[2],
97                        $date[3],
98                        $date[4],
99                        $date[5],
100                        $post->post_name,
101                        $post->ID,
102                        $category,
103                        $author,
104                        $post->post_name,
105                );
106                $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
107                $permalink = user_trailingslashit($permalink, 'single');
108        } else { // if they're not using the fancy permalink option
109                $permalink = home_url('?p=' . $post->ID);
110        }
111
112        /**
113         * Filter the permalink for a post.
114         *
115         * Only applies to posts with post_type of 'post'.
116         *
117         * @since 1.5.0
118         *
119         * @param string  $permalink The post's permalink.
120         * @param WP_Post $post      The post in question.
121         * @param bool    $leavename Whether to keep the post name.
122         */
123        return apply_filters( 'post_link', $permalink, $post, $leavename );
124}
125
126
127$count = 100000;
128
129$page = get_post( 2 );
130
131timer_start();
132for ( $i = 0; $i < $count; $i++ ) {
133        $link = get_permalink( $page );
134}
135printf( 'get_permalink(): %s seconds<br />', timer_stop( 0, 3 ) );
136
137timer_start();
138for ( $i = 0; $i < $count; $i++ ) {
139        $link = get_permalink_with_obj( $page );
140}
141printf( 'get_permalink_with_obj(): %s seconds<br />', timer_stop( 0, 3 ) );