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'); }
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; }
/** * @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); }
/** * 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; }
function __construct($slug = 0) { $locations = get_nav_menu_locations(); if ($slug != 0 && is_numeric($slug)) { $menu_id = $slug; } else { if (count($locations)) { $menu_id = $this->get_menu_id_from_locations($slug, $locations); } else { if ($slug === false) { $menu_id = false; } else { $menu_id = $this->get_menu_id_from_terms($slug); } } } if ($menu_id) { $this->init($menu_id); } else { TimberHelper::error_log("Sorry, the menu you were looking for wasn't found ('" . $slug . "'). Here's what Timber did find:"); } return null; }
/** # 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; }
function testErrorLog() { ob_start(); $this->assertTrue(TimberHelper::error_log('foo')); $this->assertTrue(TimberHelper::error_log(array('Dark Helmet', 'Barf'))); $data = ob_get_flush(); }
/** * @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; }
/** * 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; }
/** * Get sidebar from PHP * * @param string $sidebar * @param array $data * @return string */ public static function get_sidebar_from_php($sidebar = '', $data) { $caller = self::get_calling_script_dir(); $loader = new TimberLoader(); $uris = $loader->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) { TimberHelper::error_log('error loading your sidebar, check to make sure the file exists'); } $ret = ob_get_contents(); ob_end_clean(); 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-300x200-c-default.jpg) * @return bool true if everything went fine, false otherwise */ public function run($load_filename, $save_filename) { $image = wp_get_image_editor($load_filename); if (!is_wp_error($image)) { $w = $this->w; $h = $this->h; $crop = $this->crop; $current_size = $image->get_size(); $src_w = $current_size['width']; $src_h = $current_size['height']; $src_ratio = $src_w / $src_h; if (!$h) { $h = round($w / $src_ratio); } if (!$w) { //the user wants to resize based on constant height $w = round($h * $src_ratio); } // Get ratios $dest_ratio = $w / $h; $src_wt = $src_h * $dest_ratio; $src_ht = $src_w / $dest_ratio; if (!$crop) { // Always crop, to allow resizing upwards $image->crop(0, 0, $src_w, $src_h, $w, $h); } else { //start with defaults: $src_x = $src_w / 2 - $src_wt / 2; $src_y = ($src_h - $src_ht) / 6; //now specific overrides based on options: if ($crop == 'center') { // Get source x and y $src_x = round(($src_w - $src_wt) / 2); $src_y = round(($src_h - $src_ht) / 2); } else { if ($crop == 'top') { $src_y = 0; } else { if ($crop == 'bottom') { $src_y = $src_h - $src_ht; } else { if ($crop == 'left') { $src_x = 0; } else { if ($crop == 'right') { $src_x = $src_w - $src_wt; } } } } } // Crop the image if ($dest_ratio > $src_ratio) { $image->crop(0, $src_y, $src_w, $src_ht, $w, $h); } else { $image->crop($src_x, 0, $src_wt, $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; }
/** * @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); } } }
/** * * * @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:'); TimberHelper::error_log($array); } }
function wp_resize_letterbox($src, $w, $h, $color = '#000000') { //$old_file = TimberHelper::get_full_path($src); $urlinfo = parse_url($src); $old_file = ABSPATH . $urlinfo['path']; $new_file = TimberImageHelper::get_letterbox_file_path($urlinfo['path'], $w, $h); $new_file_rel = TimberImageHelper::get_letterbox_file_rel($urlinfo['path'], $w, $h); $new_file_boxed = str_replace('-lb-', '-lbox-', $new_file); if (file_exists($new_file_boxed)) { $new_file_rel = str_replace('-lb-', '-lbox-', $new_file_rel); return $new_file_rel; } $bg = imagecreatetruecolor($w, $h); $c = hexrgb($color); $white = imagecolorallocate($bg, $c['red'], $c['green'], $c['blue']); imagefill($bg, 0, 0, $white); $image = wp_get_image_editor($old_file); 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($new_file); $func = 'imagecreatefromjpeg'; $ext = pathinfo($new_file, PATHINFO_EXTENSION); if ($ext == 'gif') { $func = 'imagecreatefromgif'; } else { if ($ext == 'png') { $func = 'imagecreatefrompng'; } } $image = $func($new_file); imagecopy($bg, $image, $x, $y, 0, 0, $owt, $oht); $new_file = str_replace('-lb-', '-lbox-', $new_file); imagejpeg($bg, $new_file); return TimberHelper::get_rel_path($new_file); } else { TimberHelper::error_log($image); } return null; }
/** * 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 bool true if everything went fine, false otherwise */ public function run($load_filename, $save_filename) { //should be resized by gif resizer if (TimberImageHelper::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 TimberHelper::error_log('Error resizing image'); TimberHelper::error_log($result); return false; // @codeCoverageIgnoreEnd } else { return true; } } else { if (isset($image->error_data['error_loading_image'])) { // @codeCoverageIgnoreStart TimberHelper::error_log('Error loading ' . $image->error_data['error_loading_image']); } else { TimberHelper::error_log($image); // @codeCoverageIgnoreEnd } } }
/** * @param int $iid */ function init($iid = false) { if (!is_numeric($iid) && is_string($iid)) { if (strstr($iid, '://')) { $this->init_with_url($iid); return; } else { if (strstr(strtolower($iid), '.jpg')) { $this->init_with_url($iid); } } } $image_info = $iid; if (is_numeric($iid)) { $image_info = wp_get_attachment_metadata($iid); if (!is_array($image_info)) { $image_info = array(); } $image_custom = get_post_custom($iid); $basic = get_post($iid); if ($basic) { if (isset($basic->post_excerpt)) { $this->caption = $basic->post_excerpt; } $image_custom = array_merge($image_custom, get_object_vars($basic)); } $image_info = array_merge($image_info, $image_custom); } else { if (is_array($image_info) && isset($image_info['image'])) { $image_info = $image_info['image']; } else { if (is_object($image_info)) { $image_info = get_object_vars($image_info); } } } $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)) { 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); } } }
/** * * * @param string $src * @param int $w * @param int $h * @param string $crop * @param bool $force_resize * @return string */ public static function resize($src, $w, $h = 0, $crop = 'default', $force_resize = false) { 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; } // Sanitize crop position $allowed_crop_positions = array('default', 'center', 'top', 'bottom', 'left', 'right'); if ($crop !== false && !in_array($crop, $allowed_crop_positions)) { $crop = $allowed_crop_positions[0]; } //oh good, it's a relative image in the uploads folder! $new_path = self::get_resize_file_rel($src, $w, $h, $crop); $new_server_path = self::get_resize_file_path($src, $w, $h, $crop); $old_server_path = self::get_server_location($src); $old_server_path = TimberURLHelper::remove_double_slashes($old_server_path); $new_server_path = TimberURLHelper::remove_double_slashes($new_server_path); if (file_exists($new_server_path)) { if ($force_resize) { // Force resize - warning: will regenerate the image on every pageload, use for testing purposes only! unlink($new_server_path); } else { if (!$abs) { return TimberURLHelper::preslashit($new_path); } return untrailingslashit(home_url()) . $new_path; } } $image = wp_get_image_editor($old_server_path); if (!is_wp_error($image)) { $current_size = $image->get_size(); $src_w = $current_size['width']; $src_h = $current_size['height']; $src_ratio = $src_w / $src_h; if (!$h) { $h = round($w / $src_ratio); } // Get ratios $dest_ratio = $w / $h; $src_wt = $src_h * $dest_ratio; $src_ht = $src_w / $dest_ratio; if (!$crop) { // Always crop, to allow resizing upwards $image->crop(0, 0, $src_w, $src_h, $w, $h); } else { //start with defaults: $src_x = $src_w / 2 - $src_wt / 2; $src_y = ($src_h - $src_ht) / 6; //now specific overrides based on options: if ($crop == 'center') { // Get source x and y $src_x = round(($src_w - $src_wt) / 2); $src_y = round(($src_h - $src_ht) / 2); } else { if ($crop == 'top') { $src_y = 0; } else { if ($crop == 'bottom') { $src_y = $src_h - $src_ht; } else { if ($crop == 'left') { $src_x = 0; } else { if ($crop == 'right') { $src_x = $src_w - $src_wt; } } } } } // Crop the image if ($dest_ratio > $src_ratio) { $image->crop(0, $src_y, $src_w, $src_ht, $w, $h); } else { $image->crop($src_x, 0, $src_wt, $src_h, $w, $h); } } $result = $image->save($new_server_path); if (is_wp_error($result)) { error_log('Error resizing image'); error_log(print_r($result, true)); } if ($abs) { return untrailingslashit(home_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; }