Esempio n. 1
0
 /**
  * Custom implementation for get_context method.
  */
 public function get_context()
 {
     global $content_width;
     if (class_exists('Easy_Digital_Downloads')) {
         global $edd_options;
     }
     $context = Timber::get_context();
     $sidebar_primary = Timber::get_widgets('sidebar_primary');
     $sidebar_footer = Timber::get_widgets('sidebar_footer');
     $context['theme_mods'] = get_theme_mods();
     $context['site_options'] = wp_load_alloptions();
     $context['teaser_mode'] = apply_filters('maera/teaser/mode', 'excerpt');
     $context['thumbnail']['width'] = apply_filters('maera/image/width', 600);
     $context['thumbnail']['height'] = apply_filters('maera/image/height', 371);
     $context['menu']['primary'] = has_nav_menu('primary_navigation') ? new TimberMenu('primary_navigation') : null;
     $context['sidebar']['primary'] = apply_filters('maera/sidebar/primary', $sidebar_primary);
     $context['sidebar']['footer'] = apply_filters('maera/sidebar/footer', $sidebar_footer);
     $context['pagination'] = Timber::get_pagination();
     $context['comment_form'] = TimberHelper::get_comment_form();
     $context['comments_args'] = array('style' => 'ul', 'reply_text' => __('Reply', 'maera'), 'short_ping' => true, 'avatar_size' => 60);
     $context['site_logo'] = get_option('site_logo', false);
     $context['content_width'] = $content_width;
     $context['sidebar_template'] = maera_templates_sidebar();
     if (class_exists('Easy_Digital_Downloads')) {
         $data['edd_options'] = $edd_options;
         $data['download_categories'] = Timber::get_terms('download_category');
         $data['download_tags'] = Timber::get_terms('download_tag');
         $data['default_image'] = new TimberImage(get_template_directory_uri() . '/assets/images/default.png');
     }
     return apply_filters('maera/timber/context', $context);
 }
Esempio n. 2
0
 /**
  * @param array $results
  * @param string $PostClass
  * @return array
  */
 static function handle_post_results($results, $PostClass = 'TimberPost')
 {
     $posts = array();
     foreach ($results as $rid) {
         $PostClassUse = $PostClass;
         if (is_array($PostClass)) {
             $post_type = get_post_type($rid);
             $PostClassUse = 'TimberPost';
             if (isset($PostClass[$post_type])) {
                 $PostClassUse = $PostClass[$post_type];
             } else {
                 if (is_array($PostClass)) {
                     TimberHelper::error_log($post_type . ' of ' . $rid . ' not found in ' . print_r($PostClass, true));
                 } else {
                     TimberHelper::error_log($post_type . ' not found in ' . $PostClass);
                 }
             }
         }
         $post = new $PostClassUse($rid);
         if (isset($post->ID)) {
             $posts[] = $post;
         }
     }
     return new TimberPostsCollection($posts, $PostClass);
 }
Esempio n. 3
0
 public function initializeContext($context)
 {
     $context = parent::initializeContext($context);
     $context['menu'] = new \TimberMenu();
     $context['wp_nonce_field'] = \TimberHelper::function_wrapper('wp_nonce_field');
     return $context;
 }
 public function __construct($posts = array(), $post_class = 'TimberPost')
 {
     $returned_posts = array();
     if (is_null($posts)) {
         $posts = array();
     }
     foreach ($posts as $post_object) {
         $post_class_use = $post_class;
         if (is_array($post_class)) {
             $post_type = get_post_type($post_object);
             $post_class_use = 'TimberPost';
             if (isset($post_class[$post_type])) {
                 $post_class_use = $post_class[$post_type];
             } else {
                 if (is_array($post_class)) {
                     TimberHelper::error_log($post_type . ' of ' . $post_object->ID . ' not found in ' . print_r($post_class, true));
                 } else {
                     TimberHelper::error_log($post_type . ' not found in ' . $post_class);
                 }
             }
         }
         // Don't create yet another object if $post_object is already of the right type
         if (is_a($post_object, $post_class_use)) {
             $post = $post_object;
         } else {
             $post = new $post_class_use($post_object);
         }
         if (isset($post->ID)) {
             $returned_posts[] = $post;
         }
     }
     $returned_posts = self::maybe_set_preview($returned_posts);
     parent::__construct($returned_posts, $flags = 0, 'TimberPostsIterator');
 }
 /**
  * Performs the actual image manipulation,
  * including saving the target file.
  *
  * @param  string $load_filename filepath (not URL) to source file
  *                               (ex: /src/var/www/wp-content/uploads/my-pic.jpg)
  * @param  string $save_filename filepath (not URL) where result file should be saved
  *                               (ex: /src/var/www/wp-content/uploads/my-pic@2x.jpg)
  * @return bool                  true if everything went fine, false otherwise
  */
 function run($load_filename, $save_filename)
 {
     $image = wp_get_image_editor($load_filename);
     if (!is_wp_error($image)) {
         $current_size = $image->get_size();
         $src_w = $current_size['width'];
         $src_h = $current_size['height'];
         // Get ratios
         $w = $src_w * $this->factor;
         $h = $src_h * $this->factor;
         $image->crop(0, 0, $src_w, $src_h, $w, $h);
         $result = $image->save($save_filename);
         if (is_wp_error($result)) {
             error_log('Error resizing image');
             error_log(print_r($result, true));
             return false;
         } else {
             return true;
         }
     } else {
         if (isset($image->error_data['error_loading_image'])) {
             TimberHelper::error_log('Error loading ' . $image->error_data['error_loading_image']);
         } else {
             TimberHelper::error_log($image);
         }
     }
     return false;
 }
 public function __construct($query = false, $posts_class = 'TimberPost')
 {
     add_action('pre_get_posts', array($this, 'fix_number_posts_wp_quirk'));
     if ($posts_class) {
         $this->_posts_class = $posts_class;
     }
     if (is_a($query, 'WP_Query')) {
         // We got a full-fledged WP Query, look no further!
         $the_query = $query;
     } elseif (false === $query) {
         // If query is explicitly set to false, use the main loop
         global $wp_query;
         $the_query =& $wp_query;
     } elseif (TimberHelper::is_array_assoc($query) || is_string($query) && strstr($query, '=')) {
         // We have a regularly formed WP query string or array to use
         $the_query = new WP_Query($query);
     } elseif (is_numeric($query) || is_string($query)) {
         // We have what could be a post name or post ID to pull out
         $the_query = self::get_query_from_string($query);
     } elseif (is_array($query) && count($query) && (is_integer($query[0]) || is_string($query[0]))) {
         // We have a list of pids (post IDs) to extract from
         $the_query = self::get_query_from_array_of_ids($query);
     } else {
         TimberHelper::error_log('I have failed you! in ' . basename(__FILE__) . '::' . __LINE__);
         TimberHelper::error_log($query);
         // We have failed hard, at least let get something.
         $the_query = new WP_Query();
     }
     $this->_query = $the_query;
 }
 /**
  *
  *
  * @return string
  */
 public function call()
 {
     $args = $this->_parse_args(func_get_args(), $this->_args);
     if ($this->_use_ob) {
         return TimberHelper::ob_function($this->_function, $args);
     } else {
         return call_user_func_array($this->_function, $args);
     }
 }
 function add_to_context($context)
 {
     $context['current_url'] = TimberHelper::get_current_url();
     $context['pagination'] = Timber::get_pagination();
     $context['bethel_header_menu'] = new TimberMenu('primary');
     $context['bethel_sidebar_menu'] = new TimberMenu('sidebar');
     $context['site'] = $this;
     return $context;
 }
 function init()
 {
     $this->name = get_bloginfo('name');
     $this->title = $this->name;
     $this->description = get_bloginfo('description');
     $this->url = get_bloginfo('url');
     $this->language = get_bloginfo('language');
     $this->charset = get_bloginfo('charset');
     $this->pingback_url = get_bloginfo('pingback_url');
     $this->language_attributes = TimberHelper::function_wrapper('language_attributes');
 }
Esempio n. 10
0
 function testGetPostsByMeta()
 {
     $pids = array();
     $lauren = $this->factory->post->create(array('post_title' => 'Lauren Richler'));
     $jared = $this->factory->post->create(array('post_title' => 'Jared Novack'));
     update_post_meta($lauren, 'in', 'love');
     update_post_meta($jared, 'in', 'love');
     $in_love = TimberHelper::get_posts_by_meta('in', 'love');
     $this->assertContains($lauren, $in_love);
     $this->assertContains($jared, $in_love);
     $this->assertEquals(2, count($in_love));
 }
Esempio n. 11
0
 function testGetObjectIndexByProperty()
 {
     $obj1 = new stdClass();
     $obj1->name = 'mark';
     $obj1->skill = 'acro yoga';
     $obj2 = new stdClass();
     $obj2->name = 'austin';
     $obj2->skill = 'cooking';
     $arr = array($obj1, $obj2);
     $index = TimberHelper::get_object_index_by_property($arr, 'skill', 'cooking');
     $this->assertEquals(1, $index);
     $obj = TimberHelper::get_object_by_property($arr, 'skill', 'cooking');
     $this->assertEquals('austin', $obj->name);
 }
Esempio n. 12
0
 protected function init()
 {
     $this->admin_email = get_bloginfo('admin_email');
     $this->name = get_bloginfo('name');
     $this->title = $this->name;
     $this->description = get_bloginfo('description');
     $this->url = get_bloginfo('url');
     $this->language = get_bloginfo('language');
     $this->charset = get_bloginfo('charset');
     $this->pingback_url = get_bloginfo('pingback_url');
     $this->theme = new TimberTheme();
     $this->language_attributes = TimberHelper::function_wrapper('language_attributes');
     $this->multisite = false;
 }
 function testAgainstFooterFunctionOutput()
 {
     global $wp_scripts;
     $wp_scripts = null;
     wp_enqueue_script('colorpicker', false, array(), false, true);
     wp_enqueue_script('fake-js', 'http://example.org/fake-js.js', array(), false, true);
     $wp_footer = TimberHelper::ob_function('wp_footer');
     global $wp_scripts;
     $wp_scripts = null;
     wp_enqueue_script('colorpicker', false, array(), false, true);
     wp_enqueue_script('fake-js', 'http://example.org/fake-js.js', array(), false, true);
     $str = Timber::compile_string('{{function("wp_footer")}}');
     $this->assertEquals($wp_footer, $str);
     $this->assertGreaterThan(50, strlen($str));
 }
Esempio n. 14
0
 /**
  * @param string|array $args
  * @param array $maybe_args
  * @param string $TermClass
  * @return mixed
  */
 public static function get_terms($args = null, $maybe_args = array(), $TermClass = 'TimberTerm')
 {
     if (is_string($maybe_args) && !strstr($maybe_args, '=')) {
         //the user is sending the $TermClass in the second argument
         $TermClass = $maybe_args;
     }
     if (is_string($maybe_args) && strstr($maybe_args, '=')) {
         parse_str($maybe_args, $maybe_args);
     }
     if (is_string($args) && strstr($args, '=')) {
         //a string and a query string!
         $parsed = TimberTermGetter::get_term_query_from_query_string($args);
         if (is_array($maybe_args)) {
             $parsed->args = array_merge($parsed->args, $maybe_args);
         }
         return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass);
     } else {
         if (is_string($args)) {
             //its just a string with a single taxonomy
             $parsed = TimberTermGetter::get_term_query_from_string($args);
             if (is_array($maybe_args)) {
                 $parsed->args = array_merge($parsed->args, $maybe_args);
             }
             return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass);
         } else {
             if (is_array($args) && TimberHelper::is_array_assoc($args)) {
                 //its an associative array, like a good ole query
                 $parsed = TimberTermGetter::get_term_query_from_assoc_array($args);
                 return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass);
             } else {
                 if (is_array($args)) {
                     //its just an array of strings or IDs (hopefully)
                     $parsed = TimberTermGetter::get_term_query_from_array($args);
                     if (is_array($maybe_args)) {
                         $parsed->args = array_merge($parsed->args, $maybe_args);
                     }
                     return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass);
                 } else {
                     if (is_null($args)) {
                         return self::handle_term_query(get_taxonomies(), array(), $TermClass);
                     }
                 }
             }
         }
     }
     return null;
 }
 /**
  * Timber extras.
  */
 function timber_extras($data)
 {
     // get secondary sidebar
     $sidebar_secondary = Timber::get_widgets('sidebar_secondary');
     $data['sidebar']['secondary'] = apply_filters('maera/sidebar/secondary', $sidebar_secondary);
     $extra_widget_areas = Maera_BS_Widgets::extra_widget_areas_array();
     foreach ($extra_widget_areas as $extra_widget_area => $options) {
         if (0 != get_theme_mod($extra_widget_area . '_toggle', 0)) {
             $data['sidebar'][$extra_widget_area] = Timber::get_widgets($extra_widget_area);
         }
     }
     $comment_form_args = array('comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x('Comment', 'noun', 'maera_bs') . '</label><textarea class="form-control" id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>', 'id_submit' => 'comment-submit');
     $data['content_width'] = Maera_BS_Structure::content_width_px();
     $data['post_meta'] = Maera_BS_Meta::meta_elements();
     $data['comment_form'] = TimberHelper::get_comment_form(null, $comment_form_args);
     return $data;
 }
Esempio n. 16
0
 function load_json($json_file)
 {
     if (file_exists($json_file)) {
         $json = file_get_contents($json_file);
         $json = json_decode($json);
         foreach ($json->bricks as &$brick) {
             $brick = new ZoneboardBlock($brick);
         }
         $this->json_data = $json;
         $this->_bricks = $json->bricks;
     } else {
         if (is_admin()) {
             $url = TimberHelper::get_current_url();
             $parts = parse_url($url);
             if (isset($parts['query']) && $parts['query'] == 'page=zoneboard' || isset($parts['path']) && strpos($parts['path'], 'wp-admin/plugins.php')) {
                 $this->show_message_for_missing_json_file($json_file);
             }
         }
     }
 }
Esempio n. 17
0
 /**
  * Get pagination.
  *
  * @param array   $prefs
  * @return array mixed
  */
 public static function get_pagination($prefs = array())
 {
     global $wp_query;
     global $paged;
     global $wp_rewrite;
     $args = array();
     $args['total'] = ceil($wp_query->found_posts / $wp_query->query_vars['posts_per_page']);
     if ($wp_rewrite->using_permalinks()) {
         $url = explode('?', get_pagenum_link(0));
         if (isset($url[1])) {
             parse_str($url[1], $query);
             $args['add_args'] = $query;
         }
         $args['format'] = 'page/%#%';
         $args['base'] = trailingslashit($url[0]) . '%_%';
     } else {
         $big = 999999999;
         $args['base'] = str_replace($big, '%#%', esc_url(get_pagenum_link($big)));
     }
     $args['type'] = 'array';
     $args['current'] = max(1, get_query_var('paged'));
     $args['mid_size'] = max(9 - $args['current'], 3);
     $args['prev_next'] = false;
     if (is_int($prefs)) {
         $args['mid_size'] = $prefs - 2;
     } else {
         $args = array_merge($args, $prefs);
     }
     $data = array();
     $data['current'] = $args['current'];
     $data['total'] = $args['total'];
     $data['pages'] = TimberHelper::paginate_links($args);
     $next = get_next_posts_page_link($args['total']);
     if ($next) {
         $data['next'] = array('link' => untrailingslashit($next), 'class' => 'page-numbers next');
     }
     $prev = previous_posts(false);
     if ($prev) {
         $data['prev'] = array('link' => untrailingslashit($prev), 'class' => 'page-numbers prev');
     }
     if ($paged < 2) {
         $data['prev'] = '';
     }
     return $data;
 }
Esempio n. 18
0
<?php

/**
 * @package  WordPress
 * @subpackage  Timber
 * @since   Timber 0.1
 */
if (!class_exists('Timber')) {
    echo 'Timber not activated. Make sure you activate the plugin in <a href="/wp-admin/plugins.php#timber">/wp-admin/plugins.php</a>';
    return;
}
$context = Timber::get_context();
$context['post'] = Timber::get_post();
$context['is_home'] = is_home() || is_front_page();
if ($context['is_home']) {
    $context['site_css_contents'] = TimberHelper::transient('site_css_contents', function () {
        return file_get_contents(get_template_directory_uri() . "/assets/dist/css/application.min.css");
    }, 14400);
}
$templates = array('home.twig');
Timber::render($templates, $context);
Esempio n. 19
0
 function testOSort()
 {
     $michael = new stdClass();
     $michael->name = 'Michael';
     $michael->year = 1981;
     $lauren = new stdClass();
     $lauren->name = 'Lauren';
     $lauren->year = 1984;
     $boo = new stdClass();
     $boo->name = 'Robbie';
     $boo->year = 1989;
     $people = array($lauren, $michael, $boo);
     TimberHelper::osort($people, 'year');
     $this->assertEquals('Michael', $people[0]->name);
     $this->assertEquals('Lauren', $people[1]->name);
     $this->assertEquals('Robbie', $people[2]->name);
     $this->assertEquals(1984, $people[1]->year);
 }
 /**
  *
  *
  * @param Twig_Environment $twig
  * @return Twig_Environment
  */
 function add_timber_filters($twig)
 {
     /* image filters */
     $twig->addFilter(new Twig_SimpleFilter('resize', array('TimberImageHelper', 'resize')));
     $twig->addFilter(new Twig_SimpleFilter('retina', array('TimberImageHelper', 'retina_resize')));
     $twig->addFilter(new Twig_SimpleFilter('letterbox', array('TimberImageHelper', 'letterbox')));
     $twig->addFilter(new Twig_SimpleFilter('tojpg', array('TimberImageHelper', 'img_to_jpg')));
     /* debugging filters */
     $twig->addFilter(new Twig_SimpleFilter('docs', 'twig_object_docs'));
     $twig->addFilter(new Twig_SimpleFilter('get_class', 'get_class'));
     $twig->addFilter(new Twig_SimpleFilter('get_type', 'get_type'));
     $twig->addFilter(new Twig_SimpleFilter('print_r', function ($arr) {
         return print_r($arr, true);
     }));
     $twig->addFilter(new Twig_SimpleFilter('print_a', function ($arr) {
         return '<pre>' . self::object_docs($arr, true) . '</pre>';
     }));
     /* other filters */
     $twig->addFilter(new Twig_SimpleFilter('stripshortcodes', 'strip_shortcodes'));
     $twig->addFilter(new Twig_SimpleFilter('array', array($this, 'to_array')));
     $twig->addFilter(new Twig_SimpleFilter('excerpt', 'wp_trim_words'));
     $twig->addFilter(new Twig_SimpleFilter('function', array($this, 'exec_function')));
     $twig->addFilter(new Twig_SimpleFilter('pretags', array($this, 'twig_pretags')));
     $twig->addFilter(new Twig_SimpleFilter('sanitize', 'sanitize_title'));
     $twig->addFilter(new Twig_SimpleFilter('shortcodes', 'do_shortcode'));
     $twig->addFilter(new Twig_SimpleFilter('time_ago', array($this, 'time_ago')));
     $twig->addFilter(new Twig_SimpleFilter('wpautop', 'wpautop'));
     $twig->addFilter(new Twig_SimpleFilter('relative', function ($link) {
         return TimberURLHelper::get_rel_url($link, true);
     }));
     $twig->addFilter(new Twig_SimpleFilter('date', array($this, 'intl_date')));
     $twig->addFilter(new Twig_SimpleFilter('truncate', function ($text, $len) {
         return TimberHelper::trim_words($text, $len);
     }));
     /* actions and filters */
     $twig->addFunction(new Twig_SimpleFunction('action', function ($context) {
         $args = func_get_args();
         array_shift($args);
         $args[] = $context;
         call_user_func_array('do_action', $args);
     }, array('needs_context' => true)));
     $twig->addFilter(new Twig_SimpleFilter('apply_filters', function () {
         $args = func_get_args();
         $tag = current(array_splice($args, 1, 1));
         return apply_filters_ref_array($tag, $args);
     }));
     $twig->addFunction(new Twig_SimpleFunction('function', array(&$this, 'exec_function')));
     $twig->addFunction(new Twig_SimpleFunction('fn', array(&$this, 'exec_function')));
     $twig->addFunction(new Twig_SimpleFunction('shortcode', 'do_shortcode'));
     /* TimberObjects */
     $twig->addFunction(new Twig_SimpleFunction('TimberPost', function ($pid, $PostClass = 'TimberPost') {
         if (is_array($pid) && !TimberHelper::is_array_assoc($pid)) {
             foreach ($pid as &$p) {
                 $p = new $PostClass($p);
             }
             return $pid;
         }
         return new $PostClass($pid);
     }));
     $twig->addFunction(new Twig_SimpleFunction('TimberImage', function ($pid, $ImageClass = 'TimberImage') {
         if (is_array($pid) && !TimberHelper::is_array_assoc($pid)) {
             foreach ($pid as &$p) {
                 $p = new $ImageClass($p);
             }
             return $pid;
         }
         return new $ImageClass($pid);
     }));
     $twig->addFunction(new Twig_SimpleFunction('TimberTerm', function ($pid, $TermClass = 'TimberTerm') {
         if (is_array($pid) && !TimberHelper::is_array_assoc($pid)) {
             foreach ($pid as &$p) {
                 $p = new $TermClass($p);
             }
             return $pid;
         }
         return new $TermClass($pid);
     }));
     $twig->addFunction(new Twig_SimpleFunction('TimberUser', function ($pid, $UserClass = 'TimberUser') {
         if (is_array($pid) && !TimberHelper::is_array_assoc($pid)) {
             foreach ($pid as &$p) {
                 $p = new $UserClass($p);
             }
             return $pid;
         }
         return new $UserClass($pid);
     }));
     /* TimberObjects Alias */
     $twig->addFunction(new Twig_SimpleFunction('Post', function ($pid, $PostClass = 'TimberPost') {
         if (is_array($pid) && !TimberHelper::is_array_assoc($pid)) {
             foreach ($pid as &$p) {
                 $p = new $PostClass($p);
             }
             return $pid;
         }
         return new $PostClass($pid);
     }));
     $twig->addFunction(new Twig_SimpleFunction('Image', function ($pid, $ImageClass = 'TimberImage') {
         if (is_array($pid) && !TimberHelper::is_array_assoc($pid)) {
             foreach ($pid as &$p) {
                 $p = new $ImageClass($p);
             }
             return $pid;
         }
         return new $ImageClass($pid);
     }));
     $twig->addFunction(new Twig_SimpleFunction('Term', function ($pid, $TermClass = 'TimberTerm') {
         if (is_array($pid) && !TimberHelper::is_array_assoc($pid)) {
             foreach ($pid as &$p) {
                 $p = new $TermClass($p);
             }
             return $pid;
         }
         return new $TermClass($pid);
     }));
     $twig->addFunction(new Twig_SimpleFunction('User', function ($pid, $UserClass = 'TimberUser') {
         if (is_array($pid) && !TimberHelper::is_array_assoc($pid)) {
             foreach ($pid as &$p) {
                 $p = new $UserClass($p);
             }
             return $pid;
         }
         return new $UserClass($pid);
     }));
     /* bloginfo and translate */
     $twig->addFunction('bloginfo', new Twig_SimpleFunction('bloginfo', function ($show = '', $filter = 'raw') {
         return get_bloginfo($show, $filter);
     }));
     $twig->addFunction('__', new Twig_SimpleFunction('__', function ($text, $domain = 'default') {
         return __($text, $domain);
     }));
     /* get_twig is deprecated, use timber/twig */
     $twig = apply_filters('get_twig', $twig);
     $twig = apply_filters('timber/twig', $twig);
     return $twig;
 }
 /** # get terms is good
  *
  */
 function get_terms($tax = '', $merge = true, $TermClass = 'TimberTerm')
 {
     if (is_string($tax)) {
         if (isset($this->_get_terms) && isset($this->_get_terms[$tax])) {
             return $this->_get_terms[$tax];
         }
     }
     if (!strlen($tax) || $tax == 'all' || $tax == 'any') {
         $taxs = get_object_taxonomies($this->post_type);
     } else {
         if (is_array($tax)) {
             $taxs = $tax;
         } else {
             $taxs = array($tax);
         }
     }
     $ret = array();
     foreach ($taxs as $tax) {
         if ($tax == 'tags' || $tax == 'tag') {
             $tax = 'post_tag';
         } else {
             if ($tax == 'categories') {
                 $tax = 'category';
             }
         }
         $terms = wp_get_post_terms($this->ID, $tax);
         if (!is_array($terms) && is_object($terms) && get_class($terms) == 'WP_Error') {
             //something is very wrong
             TimberHelper::error_log('You have an error retrieving terms on a post in timber-post.php:367');
             TimberHelper::error_log('tax = ' . $tax);
             TimberHelper::error_log($terms);
         } else {
             foreach ($terms as &$term) {
                 $term = new $TermClass($term->term_id, $tax);
             }
             if ($merge && is_array($terms)) {
                 $ret = array_merge($ret, $terms);
             } else {
                 if (count($terms)) {
                     $ret[$tax] = $terms;
                 }
             }
         }
     }
     if (!isset($this->_get_terms)) {
         $this->_get_terms = array();
     }
     $this->_get_terms[$tax] = $ret;
     return $ret;
 }
Esempio n. 22
0
 /**
  * @internal
  * @param string|array $tax
  * @param bool $merge
  * @param string $TermClass
  * @return array
  */
 function get_terms($tax = '', $merge = true, $TermClass = '')
 {
     $TermClass = $TermClass ?: $this->TermClass;
     if (is_string($merge) && class_exists($merge)) {
         $TermClass = $merge;
     }
     if (is_array($tax)) {
         $taxonomies = $tax;
     }
     if (is_string($tax)) {
         if (in_array($tax, array('all', 'any', ''))) {
             $taxonomies = get_object_taxonomies($this->post_type);
         } else {
             $taxonomies = array($tax);
         }
     }
     $term_class_objects = array();
     foreach ($taxonomies as $taxonomy) {
         if (in_array($taxonomy, array('tag', 'tags'))) {
             $taxonomy = 'post_tag';
         }
         if ($taxonomy == 'categories') {
             $taxonomy = 'category';
         }
         $terms = wp_get_post_terms($this->ID, $taxonomy);
         if (is_wp_error($terms)) {
             /* @var $terms WP_Error */
             TimberHelper::error_log("Error retrieving terms for taxonomy '{$taxonomy}' on a post in timber-post.php");
             TimberHelper::error_log('tax = ' . print_r($tax, true));
             TimberHelper::error_log('WP_Error: ' . $terms->get_error_message());
             return $term_class_objects;
         }
         // map over array of wordpress terms, and transform them into instances of the TermClass
         $terms = array_map(function ($term) use($TermClass, $taxonomy) {
             return call_user_func(array($TermClass, 'from'), $term->term_id, $taxonomy);
         }, $terms);
         if ($merge && is_array($terms)) {
             $term_class_objects = array_merge($term_class_objects, $terms);
         } else {
             if (count($terms)) {
                 $term_class_objects[$taxonomy] = $terms;
             }
         }
     }
     return $term_class_objects;
 }
 public static function resize($src, $w, $h = 0)
 {
     if (empty($src)) {
         return '';
     }
     if (strstr($src, 'http') && !strstr($src, home_url())) {
         $src = self::sideload_image($src);
     }
     $abs = false;
     if (strstr($src, 'http')) {
         $abs = true;
     }
     //oh good, it's a relative image in the uploads folder!
     $path_parts = pathinfo($src);
     $basename = $path_parts['filename'];
     $ext = $path_parts['extension'];
     $dir = $path_parts['dirname'];
     $newbase = $basename . '-r-' . $w . 'x' . $h;
     $new_path = $dir . '/' . $newbase . '.' . $ext;
     $new_path = str_replace(site_url(), '', $new_path);
     $new_root_path = ABSPATH . $new_path;
     $old_root_path = ABSPATH . str_replace(site_url(), '', $src);
     $old_root_path = str_replace('//', '/', $old_root_path);
     $new_root_path = str_replace('//', '/', $new_root_path);
     if (file_exists($new_root_path)) {
         if ($abs) {
             return untrailingslashit(site_url()) . $new_path;
         } else {
             return TimberHelper::preslashit($new_path);
         }
         return $new_path;
     }
     $image = wp_get_image_editor($old_root_path);
     if (!is_wp_error($image)) {
         $current_size = $image->get_size();
         $ow = $current_size['width'];
         $oh = $current_size['height'];
         $old_aspect = $ow / $oh;
         if ($h) {
             $new_aspect = $w / $h;
             if ($new_aspect > $old_aspect) {
                 //cropping a vertical photo horitzonally
                 $oht = $ow / $new_aspect;
                 $oy = ($oh - $oht) / 6;
                 $image->crop(0, $oy, $ow, $oht, $w, $h);
             } else {
                 $owt = $oh * $new_aspect;
                 $ox = $ow / 2 - $owt / 2;
                 $image->crop($ox, 0, $owt, $oh, $w, $h);
             }
         } else {
             $h = $w;
             if ($old_aspect < 1) {
                 $h = $w / $old_aspect;
                 $image->crop(0, 0, $ow, $oh, $w, $h);
             } else {
                 $image->resize($w, $h);
             }
         }
         $result = $image->save($new_root_path);
         if (is_wp_error($result)) {
             error_log('Error resizing image');
             error_log(print_r($result, true));
         }
         if ($abs) {
             return untrailingslashit(site_url()) . $new_path;
         }
         return $new_path;
     } else {
         if (isset($image->error_data['error_loading_image'])) {
             TimberHelper::error_log('Error loading ' . $image->error_data['error_loading_image']);
         } else {
             TimberHelper::error_log($image);
         }
     }
     return $src;
 }
 public function get_path()
 {
     $link = $this->get_link();
     $rel = TimberHelper::get_rel_url($link, true);
     return apply_filters('timber_term_path', $rel, $this);
 }
Esempio n. 25
0
 function testExpireTransient()
 {
     $transient = $this->_generate_transient_name();
     $first_value = TimberHelper::transient($transient, function () {
         return 'first_value';
     }, 2);
     sleep(3);
     $second_value = TimberHelper::transient($transient, function () {
         return 'second_value';
     }, 2);
     $this->assertEquals('second_value', $second_value);
 }
 function get_archives_link($url, $text)
 {
     $ret['text'] = $ret['title'] = $ret['name'] = wptexturize($text);
     $ret['url'] = $ret['link'] = esc_url(TimberHelper::prepend_to_url($url, $this->base));
     return $ret;
 }
 /**
  * Performs the actual image manipulation,
  * including saving the target file.
  * 
  * @param  string $load_filename filepath (not URL) to source file 
  *                               (ex: /src/var/www/wp-content/uploads/my-pic.jpg)
  * @param  string $save_filename filepath (not URL) where result file should be saved 
  *                               (ex: /src/var/www/wp-content/uploads/my-pic-lbox-300x200-FF3366.jpg)
  * @return bool                  true if everything went fine, false otherwise
  */
 public function run($load_filename, $save_filename)
 {
     $w = $this->w;
     $h = $this->h;
     $bg = imagecreatetruecolor($w, $h);
     $c = self::hexrgb($this->color);
     $bgColor = imagecolorallocate($bg, $c['red'], $c['green'], $c['blue']);
     imagefill($bg, 0, 0, $bgColor);
     $image = wp_get_image_editor($load_filename);
     if (!is_wp_error($image)) {
         $current_size = $image->get_size();
         $ow = $current_size['width'];
         $oh = $current_size['height'];
         $new_aspect = $w / $h;
         $old_aspect = $ow / $oh;
         if ($new_aspect > $old_aspect) {
             //taller than goal
             $h_scale = $h / $oh;
             $owt = $ow * $h_scale;
             $y = 0;
             $x = $w / 2 - $owt / 2;
             $oht = $h;
             $image->crop(0, 0, $ow, $oh, $owt, $oht);
         } else {
             $w_scale = $w / $ow;
             $oht = $oh * $w_scale;
             $x = 0;
             $y = $h / 2 - $oht / 2;
             $owt = $w;
             $image->crop(0, 0, $ow, $oh, $owt, $oht);
         }
         $image->save($save_filename);
         $func = 'imagecreatefromjpeg';
         $ext = pathinfo($save_filename, PATHINFO_EXTENSION);
         if ($ext == 'gif') {
             $func = 'imagecreatefromgif';
         } else {
             if ($ext == 'png') {
                 $func = 'imagecreatefrompng';
             }
         }
         $image = $func($save_filename);
         imagecopy($bg, $image, $x, $y, 0, 0, $owt, $oht);
         imagejpeg($bg, $save_filename);
         return true;
     } else {
         TimberHelper::error_log($image);
     }
     return false;
 }
Esempio n. 28
0
<?php

/**
 * The Template for displaying all single posts
 *
 * Methods for TimberHelper can be found in the /lib sub-directory
 *
 * @package  WordPress
 * @subpackage  Timber
 * @since    Timber 0.1
 */
$context = Timber::get_context();
$post = new TrunckPost();
$context['post'] = $post;
$context['comment_form'] = TimberHelper::get_comment_form();
if (post_password_required($post->ID)) {
    Timber::render('single-password.twig', $context);
} else {
    Timber::render(array('single-' . $post->ID . '.twig', 'single-' . $post->post_type . '.twig', 'single.twig'), $context);
}
Esempio n. 29
0
 /**
  * @param string $widget_id
  * @return \TimberFunctionWrapper
  */
 public function sidebar($widget_id = '')
 {
     return \TimberHelper::function_wrapper('dynamic_sidebar', array($widget_id), true);
 }
Esempio n. 30
0
 /**
  * @param int $iid
  */
 function init($iid = false)
 {
     if (!is_numeric($iid) && is_string($iid)) {
         if (strstr($iid, '://')) {
             $this->init_with_url($iid);
             return;
         }
         if (strstr($iid, ABSPATH)) {
             $this->init_with_file_path($iid);
             return;
         }
         if (strstr(strtolower($iid), '.jpg')) {
             $this->init_with_relative_path($iid);
             return;
         }
     }
     $image_info = $this->get_image_info($iid);
     $this->import($image_info);
     $basedir = self::wp_upload_dir();
     $basedir = $basedir['basedir'];
     if (isset($this->file)) {
         $this->file_loc = $basedir . DIRECTORY_SEPARATOR . $this->file;
     } else {
         if (isset($this->_wp_attached_file)) {
             $this->file = reset($this->_wp_attached_file);
             $this->file_loc = $basedir . DIRECTORY_SEPARATOR . $this->file;
         }
     }
     if (isset($image_info['id'])) {
         $this->ID = $image_info['id'];
     } else {
         if (is_numeric($iid)) {
             $this->ID = $iid;
         }
     }
     if (isset($this->ID)) {
         $custom = get_post_custom($this->ID);
         foreach ($custom as $key => $value) {
             $this->{$key} = $value[0];
         }
     } else {
         if (is_array($iid) || is_object($iid)) {
             TimberHelper::error_log('Not able to init in TimberImage with iid=');
             TimberHelper::error_log($iid);
         } else {
             TimberHelper::error_log('Not able to init in TimberImage with iid=' . $iid);
         }
     }
 }