public function __construct($posts = array(), $post_class = '\\Timber\\Post') { $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 = '\\Timber\\Post'; if (isset($post_class[$post_type])) { $post_class_use = $post_class[$post_type]; } else { if (is_array($post_class)) { Helper::error_log($post_type . ' of ' . $post_object->ID . ' not found in ' . print_r($post_class, true)); } else { Helper::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, 'Timber\\PostsIterator'); }
/** * 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)) { // @codeCoverageIgnoreStart Helper::error_log('Error resizing image'); Helper::error_log($result); return false; // @codeCoverageIgnoreEnd } else { return true; } } else { if (isset($image->error_data['error_loading_image'])) { Helper::error_log('Error loading ' . $image->error_data['error_loading_image']); } else { Helper::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; //if we're on a custom posts page? $the_query = self::handle_maybe_custom_posts_page($the_query); } elseif (Helper::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); } elseif (is_array($query) && empty($query)) { // it's an empty array $the_query = array(); } else { Helper::error_log('I have failed you! in ' . basename(__FILE__) . '::' . __LINE__); Helper::error_log($query); // We have failed hard, at least let get something. $the_query = new \WP_Query(); } $this->_query = $the_query; }
/** * Get the terms associated with the post * This goes across all taxonomies by default * @api * @param string|array $tax What taxonom(y|ies) to pull from. Defaults to all registered taxonomies for the post type. You can use custom ones, or built-in WordPress taxonomies (category, tag). Timber plays nice and figures out that tag/tags/post_tag are all the same (and categories/category), for custom taxonomies you're on your own. * @param bool $merge Should the resulting array be one big one (true)? Or should it be an array of sub-arrays for each taxonomy (false)? * @return array */ public function 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 */ Helper::error_log("Error retrieving terms for taxonomy '{$taxonomy}' on a post in timber-post.php"); Helper::error_log('tax = ' . print_r($tax, true)); Helper::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; }
/** * 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(); $quality = $image->get_quality(); $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); } $result = $image->save($save_filename); $func = 'imagecreatefromjpeg'; $save_func = 'imagejpeg'; $ext = pathinfo($save_filename, PATHINFO_EXTENSION); if ($ext == 'gif') { $func = 'imagecreatefromgif'; $save_func = 'imagegif'; } else { if ($ext == 'png') { $func = 'imagecreatefrompng'; $save_func = 'imagepng'; if ($quality > 9) { $quality = $quality / 10; $quality = round(10 - $quality); } } } $image = $func($save_filename); imagecopy($bg, $image, $x, $y, 0, 0, $owt, $oht); if ($save_func === 'imagegif') { return $save_func($bg, $save_filename); } return $save_func($bg, $save_filename, $quality); } Helper::error_log($image); return false; }
/** * Get sidebar from PHP * @api * @param string $sidebar * @param array $data * @return string */ public static function get_sidebar_from_php($sidebar = '', $data) { $caller = LocationManager::get_calling_script_dir(1); $uris = LocationManager::get_locations($caller); ob_start(); $found = false; foreach ($uris as $uri) { if (file_exists(trailingslashit($uri) . $sidebar)) { include trailingslashit($uri) . $sidebar; $found = true; break; } } if (!$found) { Helper::error_log('error loading your sidebar, check to make sure the file exists'); } $ret = ob_get_contents(); ob_end_clean(); return $ret; }
/** * @internal * @param int $iid */ public function init($iid = false) { //Make sure we actually have something to work with if (!$iid) { Helper::error_log('Initalized TimberImage without providing first parameter.'); return; } //If passed TimberImage, grab the ID and continue if ($iid instanceof self) { $iid = (int) $iid->ID; } //If passed ACF image array if (is_array($iid) && isset($iid['ID'])) { $iid = $iid['ID']; } 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; } $relative = false; $iid_lower = strtolower($iid); foreach ($this->file_types as $type) { if (strstr($iid_lower, $type)) { $relative = true; break; } } if ($relative) { $this->init_with_relative_path($iid); return; } } else { if ($iid instanceof \WP_Post) { $ref = new \ReflectionClass($this); $post = $ref->getParentClass()->newInstance($iid->ID); if (isset($post->_thumbnail_id) && $post->_thumbnail_id) { return $this->init((int) $post->_thumbnail_id); } return $this->init($iid->ID); } else { if ($iid instanceof Post) { /** * This will catch TimberPost and any post classes that extend TimberPost, * see http://php.net/manual/en/internals2.opcodes.instanceof.php#109108 * and https://github.com/timber/timber/wiki/Extending-Timber */ $iid = (int) $iid->_thumbnail_id; } } } $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 = self::get_post_custom($this->ID); foreach ($custom as $key => $value) { $this->{$key} = $value[0]; } $this->id = $this->ID; } else { if (is_array($iid) || is_object($iid)) { Helper::error_log('Not able to init in TimberImage with iid='); Helper::error_log($iid); } else { Helper::error_log('Not able to init in TimberImage with iid=' . $iid); } } }
/** * @param string $post_type * @param string|array $post_class * * @return string */ public static function get_post_class($post_type, $post_class = '\\Timber\\Post') { $post_class = apply_filters('Timber\\PostClassMap', $post_class); $post_class_use = '\\Timber\\Post'; if (is_array($post_class)) { if (isset($post_class[$post_type])) { $post_class_use = $post_class[$post_type]; } else { Helper::error_log($post_type . ' not found in ' . print_r($post_class, true)); } } elseif (is_string($post_class)) { $post_class_use = $post_class; } else { Helper::error_log('Unexpeted value for PostClass: ' . print_r($post_class, true)); } if (!class_exists($post_class_use) || !(is_subclass_of($post_class_use, '\\Timber\\Post') || is_a($post_class_use, '\\Timber\\Post', true))) { Helper::error_log('Class ' . $post_class_use . ' either does not exist or implement \\Timber\\Post'); } return $post_class_use; }
/** * * * @param array $array * @param string $key * @param mixed $value * @return array|null * @throws Exception */ public static function get_object_by_property($array, $key, $value) { if (is_array($array)) { foreach ($array as $arr) { if ($arr->{$key} == $value) { return $arr; } } } else { throw new \InvalidArgumentException('$array is not an array, got:'); Helper::error_log($array); } }
/** * 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-300x200-c-default.jpg) * @return boolean|null true if everything went fine, false otherwise */ public function run($load_filename, $save_filename) { //should be resized by gif resizer if (\Timber\ImageHelper::is_animated_gif($load_filename)) { //attempt to resize //return if successful //proceed if not $gif = self::run_animated_gif($load_filename, $save_filename); if ($gif) { return true; } } $image = wp_get_image_editor($load_filename); if (!is_wp_error($image)) { $crop = self::get_target_sizes($load_filename); $image->crop($crop['x'], $crop['y'], $crop['src_w'], $crop['src_h'], $crop['target_w'], $crop['target_h']); $result = $image->save($save_filename); if (is_wp_error($result)) { // @codeCoverageIgnoreStart Helper::error_log('Error resizing image'); Helper::error_log($result); return false; // @codeCoverageIgnoreEnd } else { return true; } } else { if (isset($image->error_data['error_loading_image'])) { // @codeCoverageIgnoreStart Helper::error_log('Error loading ' . $image->error_data['error_loading_image']); } else { Helper::error_log($image); // @codeCoverageIgnoreEnd } } }