function in_news_int($atts, $content = null) { extract(shortcode_atts(array('title' => '', 'subtitle' => '', 'link' => '/'), $atts)); $limit_show_news = 100; $args = array('posts_per_page' => $limit_show_news, 'post_type' => 'post', 'meta_key' => 'is_video', 'orderby' => 'is_video', 'order' => 'DESC'); if (isset($atts['filter_car_emergence']) && $atts['filter_car_emergence'] == 1) { $args['tax_query'] = array('relation' => 'AND', array('taxonomy' => 'category', 'field' => 'term_id', 'terms' => 5)); } $query = new WP_Query($args); $post_list = ''; while ($query->have_posts()) { $query->the_post(); $atts['thumbnail'] = get_the_post_thumbnail(get_the_ID(), 'post-home-thumbnail'); $atts['categories'] = get_the_category(); $atts['class_categories_id'] = ''; $atts['class_categories_slug'] = ''; foreach ($atts['categories'] as $key => $category) { if ($category->parent == 4) { $atts['class_categories_id'] .= ' category-id-' . $category->term_id; $atts['class_categories_slug'] .= ' category-slug-' . $category->slug; unset($atts['categories'][$key]); } } $atts['title'] = get_the_title(); $atts['content'] = get_the_content(); $atts['permalink'] = get_permalink(); $post_list .= Timber::compile('post.twig', $atts); } $data['post_content'] = $post_list; $out = Timber::compile('template.twig', $data); return $out; }
public static function setup($output, $attr) { global $post; $html5 = current_theme_supports('html5'); $atts = shortcode_atts(array('order' => 'ASC', 'orderby' => 'menu_order ID', 'id' => $post ? $post->ID : 0, 'itemtag' => $html5 ? 'figure' : 'dl', 'icontag' => $html5 ? 'div' : 'dt', 'captiontag' => $html5 ? 'figcaption' : 'dd', 'columns' => 3, 'size' => 'thumbnail', 'include' => '', 'exclude' => '', 'link' => ''), $attr, 'gallery'); $id = intval($atts['id']); if (!empty($atts['include'])) { $posts = get_posts(array('include' => $atts['include'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'])); $attachments = array(); foreach ($posts as $key => $val) { $attachments[$val->ID] = $posts[$key]; } } elseif (!empty($atts['exclude'])) { $attachments = get_children(array('post_parent' => $id, 'exclude' => $atts['exclude'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'])); } else { $attachments = get_children(array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'])); } if (count($attachments)) { foreach ($attachments as &$attachment) { $attachment = new \TimberImage($attachment); } // add a static incrementer for the gallery HTML ID to allow more than one per page. static $gallery_inc = 0; $gallery_inc++; $data['id'] = sprintf("gallery-%s", $gallery_inc); $data['thumbnails'] = $attachments; return \Timber::compile('gallery.twig', $data); } }
function get_my_gallery_content($atts) { foreach (explode(',', $atts['ids']) as $id) { $context["feed"][] = array('medium' => wp_get_attachment_image_src($id, 'Medium'), 'full' => wp_get_attachment_image_src($id, 'Full')); } $context["slide_template"] = 'views/content/post_gallery_img.twig'; return Timber::compile('view/component/post_gallery.twig', $context); }
function testShortcodes() { add_shortcode('timber_shortcode', function ($text) { return 'timber ' . $text[0]; }); $return = Timber::compile('assets/test-shortcodes.twig'); $this->assertEquals('hello timber foo', trim($return)); }
function testCallingPHPFile() { $phpunit = $this; add_filter('timber/calling_php_file', function ($file) use($phpunit) { $phpunit->assertStringEndsWith('/timber/tests/test-timber-debug.php', $file); }); Timber::compile('assets/output.twig'); }
function testInTwig() { global $wp_scripts; $wp_scripts = null; wp_enqueue_script('jquery', false, array(), false, true); $str = Timber::compile('assets/wp-footer.twig', array()); $this->stringContains('http://example.org/wordpress/wp-includes/js/jquery/jquery.js', $str); }
function IgnoretestBasicCollectionWithPaginationAndBlankQuery() { $pids = $this->factory->post->create_many(130); $this->go_to('/'); $pc = new Timber\PostQuery(); $str = Timber::compile('assets/collection-pagination.twig', array('posts' => $pc)); $str = preg_replace('/\\s+/', ' ', $str); $this->assertEquals('<h1>POST</h1> <h1>POST</h1> <h1>POST</h1> <h1>POST</h1> <h1>POST</h1> <h1>POST</h1> <h1>POST</h1> <h1>POST</h1> <h1>POST</h1> <h1>POST</h1> <div class="l--pagination"> <div class="pagination-inner"> <div class="pagination-previous"> <span class="pagination-previous-link pagination-disabled">Previous</span> </div> <div class="pagination-pages"> <ul class="pagination-pages-list"> <li class="pagination-list-item pagination-page">1</li> <li class="pagination-list-item pagination-seperator">of</li> <li class="pagination-list-item pagination-page">13</li> </ul> </div> <div class="pagination-next"> <a href="http://example.org/?paged=2" class="pagination-next-link ">Next</a> </div> </div> </div>', trim($str)); }
function testTwigSidebar() { $context = Timber::get_context(); $sidebar_post = $this->factory->post->create(array('post_title' => 'Sidebar post content')); $sidebar_context = array(); $sidebar_context['post'] = new TimberPost($sidebar_post); $context['sidebar'] = Timber::get_sidebar('assets/sidebar.twig', $sidebar_context); $result = Timber::compile('assets/main-w-sidebar.twig', $context); $this->assertEquals('I am the main stuff <h4>Sidebar post content</h4>', trim($result)); }
function testPHPSidebar() { add_filter('timber_context', function ($context) { $context['sidebar'] = Timber::get_sidebar('assets/my-sidebar.php'); return $context; }); $context = Timber::get_context(); $result = Timber::compile('assets/main-w-sidebar-php.twig', $context); $this->assertEquals("A Fever You Can't Sweat Out by Panic! at the Disco from 2005", trim($result)); }
function testTheLoop() { for ($i = 1; $i < 3; $i++) { $this->factory->post->create(array('post_title' => 'TestPost' . $i)); } $results = Timber::compile('assets/iterator-test.twig', array('posts' => TimberPostGetter::query_posts('post_type=post'))); $results = trim($results); $this->assertStringStartsWith('TestPost2', $results); $this->assertStringEndsWith('TestPost1', $results); }
/** * paypal_item_shortcode * */ public function paypal_item_shortcode($atts) { $atts = shortcode_atts(array('id' => null), $atts); if ($atts['id']) { $item = \Timber::get_post($atts['id']); if ($item->post_type === 'paypal_item') { $account = array('email' => get_field('paypal_email', 'option'), 'currency' => get_field('paypal_currency', 'option'), 'location' => get_field('paypal_location', 'option')); return \Timber::compile('paypal-item.twig', array('paypal_item' => $item, 'paypal_account' => $account)); } } }
function testMenuTwigWithClasses() { $this->setPermalinkStructure(); $this->_createTestMenu(); $this->go_to(home_url('/home')); $context = Timber::get_context(); $context['menu'] = new TimberMenu(); $str = Timber::compile('assets/menu-classes.twig', $context); $str = trim($str); $this->assertContains('current_page_item', $str); $this->assertContains('current-menu-item', $str); $this->assertContains('menu-item-object-page', $str); $this->assertNotContains('foobar', $str); }
function testParentChildGeneral() { TestTimberLoader::_setupParentTheme(); TestTimberLoader::_setupChildTheme(); switch_theme('fake-child-theme'); register_post_type('course'); //copy a specific file to the PARENT directory $dest_dir = WP_CONTENT_DIR . '/themes/twentythirteen'; copy(__DIR__ . '/assets/single-course.twig', $dest_dir . '/views/single-course.twig'); $pid = $this->factory->post->create(); $post = new TimberPost($pid); $str = Timber::compile(array('single-course.twig', 'single.twig'), array('post' => $post)); $this->assertEquals('I am single course', $str); }
function testWordPressPasswordFilters() { $post_id = $this->factory->post->create(array('post_title' => 'My Private Post', 'post_password' => 'abc123')); $context = array(); add_filter('protected_title_format', function ($title) { return 'Protected: ' . $title; }); $context['post'] = new TimberPost($post_id); if (post_password_required($post_id)) { $this->assertTrue(true); $str = Timber::compile('assets/test-wp-filters.twig', $context); $this->assertEquals('Protected: My Private Post', trim($str)); } else { $this->assertTrue(false, 'Something wrong with the post password reqd'); } }
/** * do_shortcode * * Render the shortcode template * * @return string */ public function do_shortcode($atts, $content = null) { $args = shortcode_atts($this->get_defaults(), $atts); wp_register_script('pressgang-google-map', get_template_directory_uri() . '/js/src/custom/google-map.js'); wp_enqueue_script('pressgang-google-map'); // TODO defer - http://matthewhorne.me/defer-async-wordpress-scripts/ wp_register_script('google-maps', sprintf('https://maps.googleapis.com/maps/api/js?key=%s&callback=initMap', $args['api']), array('jquery', 'pressgang-google-map')); wp_enqueue_script('google-maps'); \Pressgang\Scripts::$defer[] = 'google-maps'; \Pressgang\Scripts::$async[] = 'google-maps'; \Pressgang\Scripts::$async[] = 'pressgang-google-map'; static $i; $i++; $args['id'] = sprintf("google-maps-%d", $i); $this->context = $args; return \Timber::compile($this->template, $this->context); }
/** * Returns the callback for the shortcode * * @throws \RuntimeException * @return mixed */ public function getCallback() { if (!function_exists('get_field')) { return false; } if ($accordions = get_field(self::ACF_FIELD_NAME_ACCORDION)) { $markup = '<div class="accordion-block">'; foreach ($accordions as $accordion) { $context = \Timber::get_context(); $context["accordion"] = $accordion; $template = apply_filters('fpshortcode_timber_template', $this->dic['accordionPlugin']->getPath() . "/views/accordion.twig"); $markup .= \Timber::compile($template, $context); } $markup .= '</div>'; return $markup; } return false; }
function testMenuTwigWithClasses() { $struc = '/%postname%/'; global $wp_rewrite; $wp_rewrite->set_permalink_structure($struc); $wp_rewrite->flush_rules(); update_option('permalink_structure', $struc); $this->_createTestMenu(); $this->go_to(home_url('/home')); $context = Timber::get_context(); $context['menu'] = new TimberMenu(); $str = Timber::compile('assets/menu-classes.twig', $context); $str = trim($str); $this->assertContains('current_page_item', $str); $this->assertContains('current-menu-item', $str); $this->assertContains('menu-item-object-page', $str); $this->assertNotContains('foobar', $str); }
function testPasswordedContentWhenEnabledWithCustomForm() { add_filter('timber/post/content/show_password_form_for_protected', function ($maybe_show) { return true; }); add_filter('timber/post/content/password_form', function ($form, $post) { return Timber::compile('assets/password-form.twig', array('post' => $post)); }, 10, 2); $quote = 'The way to do well is to do well.'; $post_id = $this->factory->post->create(array('post_title' => 'Secrets!')); $post = new TimberPost($post_id); $post->post_content = $quote; $post->post_password = '******'; wp_update_post($post); $password_form = '<form>Enter password to see Secrets!</form>'; $this->assertEquals($password_form, $post->content()); $this->assertEquals($password_form, $post->get_content()); }
function get_component($path = '', $template, $vars = array(), $echo = true) { $file = COMPONENTS_DIR . DS . $path . DS . $template; $html = ''; if (file_exists($file . '.twig')) { $html = Timber::compile($path . DS . $template . '.twig', $vars); } else { if (file_exists($file . '.php')) { ob_start(); extract($vars); include $file . '.php'; $html = ob_get_contents(); ob_end_clean(); } } if ($echo) { echo $html; } else { return $html; } }
public static function newsletter_create() { $post_id = $_POST['id']; if (!empty($post_id)) { $post = new TimberPost($post_id); $data = Timber::get_context(); $data['post'] = $post; $html = Timber::compile('newsletter/newsletter.twig', $data); $args = array('type' => 'regular', 'recipients' => array('list_id' => get_field('mailchimp_list_id', 'option')), 'settings' => array('subject_line' => $post->post_title, 'from_name' => 'The Food Rush', 'reply_to' => '*****@*****.**', 'authenticate' => false, 'auto_footer' => false), 'tracking' => array('opens' => true, 'html_clicks' => true, 'text_clicks' => false)); $campaign = self::$mailchimp->post('campaigns', $args); $campaign_id = $campaign['id']; $args = array('html' => $html); $campaign_content = self::$mailchimp->put('campaigns/' . $campaign_id . '/content', $args); if (!empty($campaign_content)) { echo "Successfully created new campaign!"; } else { echo "Error creating new campaign."; } die; } }
/** * do_shortcode * * Render the shortcode template * * @return string */ public function do_shortcode($atts, $content = null) { $args = shortcode_atts($this->get_defaults(), $atts); $args['email'] = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL); $args['message'] = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING); $args['name'] = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING); $args['success'] = false; if ($args['email'] && $args['name'] && $args['message']) { add_action('wp_mail_from', function () use($atts) { return $atts['email']; }); add_action('wp_mail_from_name', function () use($atts) { return $atts['name']; }); if (wp_mail($atts['to'], $this->defaults['subject'], $atts['message'])) { $args['success'] = $this->defaults['success']; } } $this->context = $args; return \Timber::compile($this->template, $this->context); }
function testTwigLoopVar() { $posts = $this->factory->post->create_many(3); $posts = TimberPostGetter::query_posts($posts); $compiled = Timber::compile('assets/iterator-loop-test.twig', array('posts' => TimberPostGetter::query_posts('post_type=post'))); $loop = array_map('json_decode', explode("\n", trim($compiled))); $this->assertSame(1, $loop[0]->index); $this->assertSame(2, $loop[0]->revindex0); $this->assertSame(3, $loop[0]->length); $this->assertTrue($loop[0]->first); $this->assertFalse($loop[0]->last); $this->assertSame(2, $loop[1]->index); $this->assertSame(1, $loop[1]->revindex0); $this->assertSame(3, $loop[1]->length); $this->assertFalse($loop[1]->first); $this->assertFalse($loop[1]->last); $this->assertSame(3, $loop[2]->index); $this->assertSame(0, $loop[2]->revindex0); $this->assertSame(3, $loop[2]->length); $this->assertFalse($loop[2]->first); $this->assertTrue($loop[2]->last); }
function testGetAttachment() { $upload_dir = wp_upload_dir(); $post_id = $this->factory->post->create(); $filename = TimberImageTest::copyTestImage('flag.png'); $destination_url = str_replace(ABSPATH, 'http://' . $_SERVER['HTTP_HOST'] . '/', $filename); $wp_filetype = wp_check_filetype(basename($filename), null); $attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($filename)), 'post_content' => '', 'post_status' => 'inherit'); $attach_id = wp_insert_attachment($attachment, $filename, $post_id); add_post_meta($post_id, '_thumbnail_id', $attach_id, true); $data = array(); $data['post'] = new TimberPost($post_id); $data['size'] = array('width' => 100, 'height' => 50); $data['crop'] = 'default'; Timber::compile('assets/thumb-test.twig', $data); $exists = file_exists($filename); $this->assertTrue($exists); $resized_path = $upload_dir['path'] . '/flag-' . $data['size']['width'] . 'x' . $data['size']['height'] . '-c-' . $data['crop'] . '.png'; $exists = file_exists($resized_path); $this->assertTrue($exists); $attachments = Timber::get_posts('post_type=attachment&post_status=inherit'); $this->assertGreaterThan(0, count($attachments)); }
/** * Render the carousel template * */ public static function render($template = 'carousel.twig', $query = array(), $width = 1140, $height = 500, $slick = array()) { if (!isset($query['post_type'])) { $query['post_type'] = 'slide'; } // uniquely identify each slider on the page static $slider_id = 0; $slider_id++; $slides = \Timber::get_posts($query); foreach ($slides as &$slide) { $slide->height = $height; $slide->width = $width; apply_filters('slick_slide', $slide); } $carousel['id'] = sprintf("%s-carousel-%s", $query['post_type'], $slider_id); $carousel['slides'] = $slides; if (count($slides)) { if ($slick) { $carousel['options'] = $slick; } return \Timber::compile($template, $carousel); } }
public function invite_users($post_id) { $users_invited = get_field('users_invited', $post_id); if ($users_invited) { $event = get_post($post_id); $subject = "You have been invited an the event"; $message = "You have been invited the the event {$event->post_title}"; $url = get_term_link(get_term_by('slug', 'rsvp', 'event_category')); $users_to_notify = array(); foreach ($users_invited as $user) { $user = (object) $user; if (!in_metadata_serialized($post_id, 'user', $user->ID, 'invited_to_events')) { add_filter('wp_mail_content_type', array($this, 'set_content_type')); if (wp_mail($user->user_email, $subject, Timber::compile(EMAILS_VIEW_ABS_DIR . DIRECTORY_SEPARATOR . 'invite_users.twig', compact('user', 'event', 'url', 'message')))) { // send request to browser notifications $users_to_notify[] = $user->ID; update_metadata_serialized('user', $user->ID, 'invited_to_events', (string) $post_id); } remove_filter('wp_mail_content_type', array($this, 'set_content_type')); } } $this->send_desktop_notifications($users_to_notify, $subject, $message); } }
/** * html_sitemap * * @param array $post_types */ public function html_sitemap($atts = array()) { $atts = shortcode_atts(array('post_type' => 'page'), $atts); $data['posts'] = \Timber::get_posts(array('numberposts' => -1, 'post_type' => $atts['post_type'], 'post_status' => 'publish')); return \Timber::compile('sitemap-html.twig', $data); }
function testTwigLoadsFromLocation() { Timber::$locations = __DIR__ . '/assets'; $str = Timber::compile('thumb-test.twig'); $this->assertEquals('<img src="" />', trim($str)); }
function testThemeImageResize() { $dest = get_template_directory() . '/images/cardinals.jpg'; copy(__DIR__ . '/assets/cardinals.jpg', $dest); $image = get_template_directory_uri() . '/images/cardinals.jpg'; $image = str_replace('http://example.org', '', $image); $data = array(); $data['test_image'] = $image; $data['size'] = array('width' => 120, 'height' => 120); $str = Timber::compile('assets/image-test.twig', $data); $this->assertFileExists(get_template_directory() . '/images/cardinals-120x120-c-default.jpg'); unlink(get_template_directory() . '/images/cardinals-120x120-c-default.jpg'); }
/** * Sends the email * * @throws \InvalidArgumentException * @internal param array $to_address * @internal param array $from_address * @return bool * @author Tim Perry */ public function send() { $this->validate(); $messageBody = \Timber::compile($this->template, $this->context); $this->mailer->AltBody = $this->getPlainTextBody($messageBody); $this->mailer->Body = $messageBody; $this->mailer->IsHTML(true); return $this->mailer->Send(); }
function testWPCLIClearCacheAllArray() { $cache_dir = __DIR__ . '/../cache/twig'; if (is_dir($cache_dir)) { TimberLoader::rrmdir($cache_dir); } $this->assertFileNotExists($cache_dir); Timber::$cache = true; $pid = $this->factory->post->create(); $post = new TimberPost($pid); Timber::compile('assets/single-post.twig', array('post' => $post)); sleep(1); $this->assertFileExists($cache_dir); Timber::compile('assets/single.twig', array('data' => 'foobar'), 600); $success = TimberCommand::clear_cache(array('all')); $this->assertTrue($success); Timber::$cache = false; $success = TimberCommand::clear_cache('bunk'); $this->assertNull($success); }