function replace_placeholders($post, $string) { $userdata = get_userdata($post->post_author); $string = str_replace("%post_author", $userdata ? $userdata->data->display_name : '', $string); $thumb_image = wp_get_attachment_thumb_url(get_post_thumbnail_id($post->ID)); $string = str_replace("%post_feat_image_thumb", $thumb_image, $string); // deprecated $string = str_replace("%post_featured_image_thumb_url", $thumb_image, $string); if (trim($thumb_image) == true) { $string = str_replace("%post_featured_image_thumb", "<img src=\"" . $thumb_image . "\" />", $string); } $featured_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); $string = str_replace("%post_feat_image", $featured_image, $string); // deprecated $string = str_replace("%post_featured_image_url", $featured_image, $string); if (trim($featured_image) == true) { $string = str_replace("%post_featured_image", "<img src=\"" . $featured_image . "\" />", $string); } $string = str_replace("%post_title", $post->post_title, $string); $string = str_replace("%post_excerpt", $post->post_excerpt, $string); $string = str_replace("%post_url", get_permalink($post->ID), $string); $string = replace_dates($post, $string); $string = str_replace("%post_comment_count", $post->comment_count, $string); // Process custom fields last, so that users cannot override the built-in options like %post_featured_image with a custom field named "featured_image" // As reported here https://wordpress.org/support/topic/category-lastes-post-with-images?replies=7#post-7052239 $custom_field_keys = get_post_custom_keys($post->ID); foreach ((array) $custom_field_keys as $key => $value) { $valuet = trim($value); if ('_' == $valuet[0]) { continue; } $meta = get_post_meta($post->ID, $valuet, true); $valuet_str = str_replace(' ', '_', $valuet); // Check if post_myfield occurs if (substr_count($string, "%post_" . $valuet_str) > 0) { if (is_string($meta)) { $string = str_replace("%post_" . $valuet_str, $meta, $string); } } } // Remove remaining %post_ occurrences. $pattern = "/" . "((\\((?P<lbrack>(\\S*))))?" . "\\%post_[-\\w]*(?P<brackets>(\\(((?P<inner>[^\\(\\)]*)|(?P>brackets))\\)))" . "(((?P<rbrack>(\\S*))\\)))?" . "/"; $string = preg_replace($pattern, '', $string); $pattern = "/%post_[-\\w]*(?P<brackets>(\\(((?P<inner>[^\\(\\)]*)|(?P>brackets))\\)))?/"; $string = preg_replace($pattern, '', $string); $pattern = "/%post_[-\\w]*(\\([-\\w]*\\))?/"; $string = preg_replace($pattern, '', $string); return $string; }
function test_string_post_date_formattingwithslashes_returns_formatted_time() { // Arrange $post = $this->factory->post->create_and_get(array('post_title' => 'Test Post', 'post_date_gmt' => '2000-01-01 0:00:00', 'post_date' => '2016-01-13 1:15:00')); // Act $result = replace_dates($post, "%post_date(Y/m/d)"); // Assert $this->assertEquals("2016/01/13", $result); }