Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 5 files changed
  • 0 comments
  • 1 contributor
205  wp-includes/class-wp-post.php
... ...
@@ -0,0 +1,205 @@
  1
+<?php
  2
+
  3
+class WP_Post {
  4
+	public $ID;
  5
+	public $post_author;
  6
+	public $post_date;
  7
+	public $post_date_gmt;
  8
+	public $post_content;
  9
+	public $post_title;
  10
+	public $post_excerpt;
  11
+	public $post_status;
  12
+	public $comment_status;
  13
+	public $ping_status;
  14
+	public $post_password;
  15
+	public $post_name;
  16
+	public $to_ping;
  17
+	public $pinged;
  18
+	public $post_modified;
  19
+	public $post_modified_gmt;
  20
+	public $post_content_filtered;
  21
+	public $post_parent;
  22
+	public $guid;
  23
+	public $menu_order;
  24
+	public $post_type;
  25
+	public $post_mime_type;
  26
+	public $comment_count;
  27
+	public $filter;
  28
+
  29
+	/*
  30
+	 * @param array|object $post_data post data, in the form of an array or stdClass object
  31
+	 */
  32
+	public static function get_instance( $post_data ) {
  33
+		$post = new WP_Post();
  34
+
  35
+		foreach ( $post_data as $k=>$v ) {
  36
+			if ( property_exists('WP_Post', $k) ) {
  37
+				$post->$k = $v;
  38
+			}
  39
+		}
  40
+
  41
+		return $post;
  42
+	}
  43
+
  44
+	/*
  45
+	 * @param array $posts_data an array of posts (the posts themselves can be arrays or stdClass objects)
  46
+	 */
  47
+	public static function get_instances( array $posts_data ) {
  48
+		return array_map(array( 'WP_Post', 'get_instance' ), $posts_data );
  49
+	}
  50
+
  51
+	// since all properties are public, this is only
  52
+	// needed to prevent the addition of other properties
  53
+	public function __set( $name, $value ) {
  54
+		return false;
  55
+	}
  56
+
  57
+	public function get_ID() {
  58
+		return $this->ID;
  59
+	}
  60
+
  61
+	public function get_title( $id = 0 ) {
  62
+		$title = isset($this->post_title) ? $this->post_title : '';
  63
+		$id = isset($this->ID) ? $this->ID : (int) $id;
  64
+
  65
+		if ( !is_admin() ) {
  66
+			if ( !empty($this->post_password) ) {
  67
+				$protected_title_format = apply_filters('protected_title_format', __('Protected: %s'));
  68
+				$title = sprintf($protected_title_format, $title);
  69
+			} else if ( isset($this->post_status) && 'private' == $this->post_status ) {
  70
+				$private_title_format = apply_filters('private_title_format', __('Private: %s'));
  71
+				$title = sprintf($private_title_format, $title);
  72
+			}
  73
+		}
  74
+		return apply_filters( 'the_title', $title, $id );
  75
+	}
  76
+
  77
+	public function get_guid() {
  78
+		return apply_filters('get_the_guid', $this->guid);
  79
+	}
  80
+
  81
+	public function get_content($more_link_text = null, $stripteaser = false) {
  82
+		global $more, $page, $pages, $multipage, $preview;
  83
+
  84
+		if ( null === $more_link_text )
  85
+			$more_link_text = __( '(more...)' );
  86
+
  87
+		$output = '';
  88
+		$hasTeaser = false;
  89
+
  90
+		// If post password required and it doesn't match the cookie.
  91
+		if ( post_password_required($this) )
  92
+			return get_the_password_form();
  93
+
  94
+		if ( $page > count($pages) ) // if the requested page doesn't exist
  95
+			$page = count($pages); // give them the highest numbered page that DOES exist
  96
+
  97
+		$content = $pages[$page-1];
  98
+		if ( preg_match('/<!--more(.*?)?-->/', $content, $matches) ) {
  99
+			$content = explode($matches[0], $content, 2);
  100
+			if ( !empty($matches[1]) && !empty($more_link_text) )
  101
+				$more_link_text = strip_tags(wp_kses_no_null(trim($matches[1])));
  102
+
  103
+			$hasTeaser = true;
  104
+		} else {
  105
+			$content = array($content);
  106
+		}
  107
+		if ( (false !== strpos($this->post_content, '<!--noteaser-->') && ((!$multipage) || ($page==1))) )
  108
+			$stripteaser = true;
  109
+		$teaser = $content[0];
  110
+		if ( $more && $stripteaser && $hasTeaser )
  111
+			$teaser = '';
  112
+		$output .= $teaser;
  113
+		if ( count($content) > 1 ) {
  114
+			if ( $more ) {
  115
+				$output .= '<span id="more-' . $this->ID . '"></span>' . $content[1];
  116
+			} else {
  117
+				if ( ! empty($more_link_text) )
  118
+					$output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$this->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );
  119
+				$output = force_balance_tags($output);
  120
+			}
  121
+
  122
+		}
  123
+		if ( $preview ) // preview fix for javascript bug with foreign languages
  124
+			$output =	preg_replace_callback('/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output);
  125
+
  126
+		return $output;
  127
+	}
  128
+
  129
+	public function get_excerpt( $deprecated = '' ) {
  130
+		if ( !empty( $deprecated ) )
  131
+			_deprecated_argument( __FUNCTION__, '2.3' );
  132
+
  133
+		if ( post_password_required($this) ) {
  134
+			return __( 'There is no excerpt because this is a protected post.' );
  135
+		}
  136
+
  137
+		return apply_filters( 'get_the_excerpt', $this->post_excerpt );
  138
+	}
  139
+
  140
+	public function has_excerpt() {
  141
+		return !empty( $this->post_excerpt );
  142
+	}
  143
+
  144
+	public function get_post_class( $class = '' ) {
  145
+		$classes = array();
  146
+
  147
+		$object_vars = get_object_vars($this);
  148
+		if ( empty( $object_vars ) )
  149
+			return $classes;
  150
+
  151
+		$classes[] = 'post-' . $this->ID;
  152
+		$classes[] = $this->post_type;
  153
+		$classes[] = 'type-' . $this->post_type;
  154
+		$classes[] = 'status-' . $this->post_status;
  155
+
  156
+		// Post Format
  157
+		if ( post_type_supports( $this->post_type, 'post-formats' ) ) {
  158
+			$post_format = get_post_format( $this->ID );
  159
+
  160
+			if ( $post_format && !is_wp_error($post_format) )
  161
+				$classes[] = 'format-' . sanitize_html_class( $post_format );
  162
+			else
  163
+				$classes[] = 'format-standard';
  164
+		}
  165
+
  166
+		// post requires password
  167
+		if ( post_password_required($this->ID) )
  168
+			$classes[] = 'post-password-required';
  169
+
  170
+		// sticky for Sticky Posts
  171
+		if ( is_sticky($this->ID) && is_home() && !is_paged() )
  172
+			$classes[] = 'sticky';
  173
+
  174
+		// hentry for hAtom compliance
  175
+		$classes[] = 'hentry';
  176
+
  177
+		// Categories
  178
+		if ( is_object_in_taxonomy( $this->post_type, 'category' ) ) {
  179
+			foreach ( (array) get_the_category($this->ID) as $cat ) {
  180
+				if ( empty($cat->slug ) )
  181
+					continue;
  182
+				$classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->term_id);
  183
+			}
  184
+		}
  185
+
  186
+		// Tags
  187
+		if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) ) {
  188
+			foreach ( (array) get_the_tags($this->ID) as $tag ) {
  189
+				if ( empty($tag->slug ) )
  190
+					continue;
  191
+				$classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id);
  192
+			}
  193
+		}
  194
+
  195
+		if ( !empty($class) ) {
  196
+			if ( !is_array( $class ) )
  197
+				$class = preg_split('#\s+#', $class);
  198
+			$classes = array_merge($classes, $class);
  199
+		}
  200
+
  201
+		$classes = array_map('esc_attr', $classes);
  202
+
  203
+		return apply_filters('post_class', $classes, $class, $this->ID);
  204
+	}
  205
+}
138  wp-includes/post-template.php
@@ -27,7 +27,7 @@ function the_ID() {
27 27
  */
28 28
 function get_the_ID() {
29 29
 	global $post;
30  
-	return $post->ID;
  30
+	return $post->get_ID();
31 31
 }
32 32
 
33 33
 /**
@@ -102,20 +102,7 @@ function the_title_attribute( $args = '' ) {
102 102
  */
103 103
 function get_the_title( $id = 0 ) {
104 104
 	$post = &get_post($id);
105  
-
106  
-	$title = isset($post->post_title) ? $post->post_title : '';
107  
-	$id = isset($post->ID) ? $post->ID : (int) $id;
108  
-
109  
-	if ( !is_admin() ) {
110  
-		if ( !empty($post->post_password) ) {
111  
-			$protected_title_format = apply_filters('protected_title_format', __('Protected: %s'));
112  
-			$title = sprintf($protected_title_format, $title);
113  
-		} else if ( isset($post->post_status) && 'private' == $post->post_status ) {
114  
-			$private_title_format = apply_filters('private_title_format', __('Private: %s'));
115  
-			$title = sprintf($private_title_format, $title);
116  
-		}
117  
-	}
118  
-	return apply_filters( 'the_title', $title, $id );
  105
+	return $post->get_title($id);
119 106
 }
120 107
 
121 108
 /**
@@ -149,8 +136,7 @@ function the_guid( $id = 0 ) {
149 136
  */
150 137
 function get_the_guid( $id = 0 ) {
151 138
 	$post = &get_post($id);
152  
-
153  
-	return apply_filters('get_the_guid', $post->guid);
  139
+	return $post->get_guid($id);
154 140
 }
155 141
 
156 142
 /**
@@ -178,51 +164,8 @@ function the_content($more_link_text = null, $stripteaser = false) {
178 164
  * @return string
179 165
  */
180 166
 function get_the_content($more_link_text = null, $stripteaser = false) {
181  
-	global $post, $more, $page, $pages, $multipage, $preview;
182  
-
183  
-	if ( null === $more_link_text )
184  
-		$more_link_text = __( '(more...)' );
185  
-
186  
-	$output = '';
187  
-	$hasTeaser = false;
188  
-
189  
-	// If post password required and it doesn't match the cookie.
190  
-	if ( post_password_required($post) )
191  
-		return get_the_password_form();
192  
-
193  
-	if ( $page > count($pages) ) // if the requested page doesn't exist
194  
-		$page = count($pages); // give them the highest numbered page that DOES exist
195  
-
196  
-	$content = $pages[$page-1];
197  
-	if ( preg_match('/<!--more(.*?)?-->/', $content, $matches) ) {
198  
-		$content = explode($matches[0], $content, 2);
199  
-		if ( !empty($matches[1]) && !empty($more_link_text) )
200  
-			$more_link_text = strip_tags(wp_kses_no_null(trim($matches[1])));
201  
-
202  
-		$hasTeaser = true;
203  
-	} else {
204  
-		$content = array($content);
205  
-	}
206  
-	if ( (false !== strpos($post->post_content, '<!--noteaser-->') && ((!$multipage) || ($page==1))) )
207  
-		$stripteaser = true;
208  
-	$teaser = $content[0];
209  
-	if ( $more && $stripteaser && $hasTeaser )
210  
-		$teaser = '';
211  
-	$output .= $teaser;
212  
-	if ( count($content) > 1 ) {
213  
-		if ( $more ) {
214  
-			$output .= '<span id="more-' . $post->ID . '"></span>' . $content[1];
215  
-		} else {
216  
-			if ( ! empty($more_link_text) )
217  
-				$output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );
218  
-			$output = force_balance_tags($output);
219  
-		}
220  
-
221  
-	}
222  
-	if ( $preview ) // preview fix for javascript bug with foreign languages
223  
-		$output =	preg_replace_callback('/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output);
224  
-
225  
-	return $output;
  167
+	global $post;
  168
+	return $post->get_content($more_link_text, $stripteaser);
226 169
 }
227 170
 
228 171
 /**
@@ -256,15 +199,8 @@ function the_excerpt() {
256 199
  * @return string
257 200
  */
258 201
 function get_the_excerpt( $deprecated = '' ) {
259  
-	if ( !empty( $deprecated ) )
260  
-		_deprecated_argument( __FUNCTION__, '2.3' );
261  
-
262 202
 	global $post;
263  
-	if ( post_password_required($post) ) {
264  
-		return __( 'There is no excerpt because this is a protected post.' );
265  
-	}
266  
-
267  
-	return apply_filters( 'get_the_excerpt', $post->post_excerpt );
  203
+	return $post->get_excerpt($deprecated = '');
268 204
 }
269 205
 
270 206
 /**
@@ -277,7 +213,7 @@ function get_the_excerpt( $deprecated = '' ) {
277 213
  */
278 214
 function has_excerpt( $id = 0 ) {
279 215
 	$post = &get_post( $id );
280  
-	return ( !empty( $post->post_excerpt ) );
  216
+	return $post->has_excerpt();
281 217
 }
282 218
 
283 219
 /**
@@ -312,65 +248,7 @@ function post_class( $class = '', $post_id = null ) {
312 248
  */
313 249
 function get_post_class( $class = '', $post_id = null ) {
314 250
 	$post = get_post($post_id);
315  
-
316  
-	$classes = array();
317  
-
318  
-	if ( empty($post) )
319  
-		return $classes;
320  
-
321  
-	$classes[] = 'post-' . $post->ID;
322  
-	$classes[] = $post->post_type;
323  
-	$classes[] = 'type-' . $post->post_type;
324  
-	$classes[] = 'status-' . $post->post_status;
325  
-
326  
-	// Post Format
327  
-	if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
328  
-		$post_format = get_post_format( $post->ID );
329  
-
330  
-		if ( $post_format && !is_wp_error($post_format) )
331  
-			$classes[] = 'format-' . sanitize_html_class( $post_format );
332  
-		else
333  
-			$classes[] = 'format-standard';
334  
-	}
335  
-
336  
-	// post requires password
337  
-	if ( post_password_required($post->ID) )
338  
-		$classes[] = 'post-password-required';
339  
-
340  
-	// sticky for Sticky Posts
341  
-	if ( is_sticky($post->ID) && is_home() && !is_paged() )
342  
-		$classes[] = 'sticky';
343  
-
344  
-	// hentry for hAtom compliance
345  
-	$classes[] = 'hentry';
346  
-
347  
-	// Categories
348  
-	if ( is_object_in_taxonomy( $post->post_type, 'category' ) ) {
349  
-		foreach ( (array) get_the_category($post->ID) as $cat ) {
350  
-			if ( empty($cat->slug ) )
351  
-				continue;
352  
-			$classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->term_id);
353  
-		}
354  
-	}
355  
-
356  
-	// Tags
357  
-	if ( is_object_in_taxonomy( $post->post_type, 'post_tag' ) ) {
358  
-		foreach ( (array) get_the_tags($post->ID) as $tag ) {
359  
-			if ( empty($tag->slug ) )
360  
-				continue;
361  
-			$classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id);
362  
-		}
363  
-	}
364  
-
365  
-	if ( !empty($class) ) {
366  
-		if ( !is_array( $class ) )
367  
-			$class = preg_split('#\s+#', $class);
368  
-		$classes = array_merge($classes, $class);
369  
-	}
370  
-
371  
-	$classes = array_map('esc_attr', $classes);
372  
-
373  
-	return apply_filters('post_class', $classes, $class, $post->ID);
  251
+    return $post->get_post_class( $class );
374 252
 }
375 253
 
376 254
 /**
3  wp-includes/post.php
@@ -403,6 +403,7 @@ function &get_post(&$post, $output = OBJECT, $filter = 'raw') {
403 403
 			$_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id));
404 404
 			if ( ! $_post )
405 405
 				return $null;
  406
+			$_post = WP_Post::get_instance($_post);
406 407
 			_get_post_ancestors($_post);
407 408
 			$_post = sanitize_post($_post, 'raw');
408 409
 			wp_cache_add($_post->ID, $_post, 'posts');
@@ -5347,7 +5348,7 @@ function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache
5347 5348
 	$non_cached_ids = _get_non_cached_ids( $ids, 'posts' );
5348 5349
 	if ( !empty( $non_cached_ids ) ) {
5349 5350
 		$fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", join( ",", $non_cached_ids ) ) );
5350  
-
  5351
+		$fresh_posts = WP_Post::get_instances($fresh_posts);
5351 5352
 		update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
5352 5353
 	}
5353 5354
 }
4  wp-includes/query.php
@@ -2660,6 +2660,10 @@ function &get_posts() {
2660 2660
 		if ( !$q['suppress_filters'] )
2661 2661
 			$this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
2662 2662
 
  2663
+		if ( is_array($this->posts) ) {
  2664
+			$this->posts = WP_Post::get_instances( $this->posts );
  2665
+		}
  2666
+
2663 2667
 		if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
2664 2668
 			$cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
2665 2669
 			$cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
1  wp-settings.php
@@ -142,6 +142,7 @@
142 142
 require( ABSPATH . WPINC . '/nav-menu.php' );
143 143
 require( ABSPATH . WPINC . '/nav-menu-template.php' );
144 144
 require( ABSPATH . WPINC . '/admin-bar.php' );
  145
+require( ABSPATH . WPINC . '/class-wp-post.php' );
145 146
 
146 147
 // Load multisite-specific files.
147 148
 if ( is_multisite() ) {

No commit comments for this range

Something went wrong with that request. Please try again.