/** * fb_opengraph * */ public static function fb_opengraph() { $post = new \TimberPost(); $title = $post->title; $description = is_single() ? str_replace('', "'", $post->get_preview(60, true, false, true)) : get_bloginfo('description', 'display'); $img = has_post_thumbnail($post->id) ? wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large')[0] : (get_theme_mod('og_img') ? get_theme_mod('og_img') : esc_url(get_theme_mod('logo'))); $type = is_author() ? 'profile' : (is_single() ? 'article' : 'website'); $url = get_permalink(); $url = rtrim(esc_url(apply_filters('og_url', $url))); if (!substr($url, -1) === '/') { $url .= '/'; // slash fixes Facebook Debugger "Circular Redirect Path" } $open_graph = array('site_name' => esc_attr(apply_filters('og_site_name', get_bloginfo())), 'title' => esc_attr(apply_filters('og_title', $title)), 'description' => esc_attr(apply_filters('og_description', $description)), 'type' => esc_attr(apply_filters('og_type', $type)), 'url' => $url, 'image' => esc_url(apply_filters('og_image', $img))); \Timber::render('open-graph.twig', array('open_graph' => $open_graph)); }
/** * add meta_description * * hook after post has loaded to add a unique meta-description * */ public static function meta_description() { $post = new \TimberPost(); // check for custom field $description = wptexturize($post->get_field('meta_description')); // else use preview if (empty($description)) { $description = str_replace('', "'", $post->get_preview(40, true, false, true)); } // finally use the blog description if (empty($description)) { $description = get_bloginfo('description', 'display'); } $description = esc_attr($description); // limit to SEO recommended length if (strlen($description) > 155) { $description = substr($description, 0, 155); $description = \TimberHelper::trim_words($description, str_word_count($description) - 1); } return $description; }
function testPreviewWithCustomMoreTag() { $pid = $this->factory->post->create(array('post_content' => 'Eric is a polar bear <!-- more But what is Elaina? --> Lauren is not a duck', 'post_excerpt' => '')); $post = new TimberPost($pid); $this->assertEquals('Eric is a polar bear <a href="' . $post->link() . '" class="read-more">But what is Elaina?</a>', $post->get_preview()); }
function testGetPreview() { global $wp_rewrite; $struc = false; $wp_rewrite->permalink_structure = $struc; update_option('permalink_structure', $struc); $post_id = $this->factory->post->create(array('post_content' => 'this is super dooper trooper long words')); $post = new TimberPost($post_id); // no excerpt $post->post_excerpt = ''; $preview = $post->get_preview(3); $this->assertRegExp('/this is super … <a href="http:\\/\\/example.org\\/\\?p=\\d+" class="read-more">Read More<\\/a>/', $preview); // excerpt set, force is false, no read more $post->post_excerpt = 'this is excerpt longer than three words'; $preview = $post->get_preview(3, false, ''); $this->assertEquals($preview, $post->post_excerpt); // custom read more set $post->post_excerpt = ''; $preview = $post->get_preview(3, false, 'Custom more'); $this->assertRegExp('/this is super … <a href="http:\\/\\/example.org\\/\\?p=\\d+" class="read-more">Custom more<\\/a>/', $preview); // content with <!--more--> tag, force false $post->post_content = 'this is super dooper<!--more--> trooper long words'; $preview = $post->get_preview(2, false, ''); $this->assertEquals('this is super dooper', $preview); }
function testDoubleEllipsis() { $post_id = $this->factory->post->create(); $post = new TimberPost($post_id); $post->post_excerpt = 'this is super dooper trooper long words'; $prev = $post->get_preview(3, true); $this->assertEquals(1, substr_count($prev, '…')); }
<?php $data = Timber::get_context(); $pi = new TimberPost(); if (isset($pi->banner_image)) { $pi->banner_image = new TimberImage($pi->banner_image); } $data['post'] = $pi; if ($pi->custom_title_tag) { $data['wp_title'] = $pi->custom_title_tag . ' - Upstatement Blog'; } else { $data['wp_title'] = $pi->get_title() . ' - Upstatement Blog'; } if ($pi->custom_description) { $data['meta_desc'] = $pi->custom_description; } else { $data['meta_desc'] = strip_tags($pi->get_preview(30, true, '', true)); } $data['sidebar'] = Timber::get_sidebar(); if (post_password_required($post->ID)) { Timber::render('single-password.twig', $data); } else { Timber::render('single.twig', $data); }
public function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true) { return $this->post->get_preview($len, $force, $readmore, $strip); }
/** * @group failing */ function testPreviewWithCustomStripTags() { $pid = $this->factory->post->create(array('post_content' => '<span>Even in the <a href="">world</a> of make-believe there have to be rules. The parts have to be consistent and belong together</span>')); $post = new TimberPost($pid); $post->post_excerpt = ''; $preview = $post->get_preview(6, true, 'Read More', '<span>'); $this->assertEquals('<span>Even in the world of</span>… <a href="' . $post->link() . '" class="read-more">Read More</a>', $preview); }
/** * add meta_description * * hook after post has loaded to add a unique meta-description * */ public static function meta_description() { $post = new \TimberPost(); // check for custom field $description = wptexturize($post->get_field('meta_description')); if (is_tax()) { if ($temp = term_description(get_queried_object(), get_query_var('taxonomy'))) { $description = $temp; } } elseif (is_post_type_archive()) { if ($temp = get_the_archive_description()) { $description = $temp; } } // else use preview if (empty($description)) { $description = str_replace('', "'", $post->get_preview(40, true, false, true)); } // finally use the blog description if (empty($description)) { $description = get_bloginfo('description', 'raw'); } $description = esc_attr(wp_strip_all_tags($description)); // limit to SEO recommended length if (strlen($description) > 155) { $description = substr($description, 0, 155); $description = \TimberHelper::trim_words($description, str_word_count($description) - 1); } return $description; }