/** * This function is slightly different from the one below in the case of: * an image hosted on the same domain BUT on a different site than the * Wordpress install will be reported as external content. * * @param string $url a URL to evaluate against * @return boolean if $url points to an external location returns true */ public static function is_external_content($url) { // using content_url() instead of site_url or home_url is IMPORTANT // otherwise you run into errors with sites that: // 1. use WPML plugin // 2. or redefine upload directory $is_external = TimberURLHelper::is_absolute($url) && !strstr($url, content_url()); return $is_external; }
/** * @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; } else { if (isset($this->guid)) { if (TimberURLHelper::is_absolute($this->guid)) { $this->file_loc = TimberURLHelper::url_to_file_system($this->guid); } } } } 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); } } }
/** * Takes in an URL and breaks it into components, * that will then be used in the different steps of image processing. * The image is expected to be either part of a theme, plugin, or an upload. * * @param string $url an URL (absolute or relative) pointing to an image * @return array an array (see keys in code below) */ private static function analyze_url($url) { $result = array('url' => $url, 'absolute' => TimberURLHelper::is_absolute($url), 'base' => 0, 'subdir' => '', 'filename' => '', 'extension' => '', 'basename' => ''); $upload_dir = wp_upload_dir(); $tmp = $url; if (0 === strpos($tmp, ABSPATH)) { // we've been given a dir, not an url $result['absolute'] = true; if (0 === strpos($tmp, $upload_dir['basedir'])) { $result['base'] = self::BASE_UPLOADS; // upload based $tmp = str_replace($upload_dir['basedir'], '', $tmp); } if (0 === strpos($tmp, WP_CONTENT_DIR)) { $result['base'] = self::BASE_CONTENT; // content based $tmp = str_replace(WP_CONTENT_DIR, '', $tmp); } } else { if (!$result['absolute']) { $tmp = home_url() . $tmp; } if (0 === strpos($tmp, $upload_dir['baseurl'])) { $result['base'] = self::BASE_UPLOADS; // upload based $tmp = str_replace($upload_dir['baseurl'], '', $tmp); } if (0 === strpos($tmp, content_url())) { $result['base'] = self::BASE_CONTENT; // content-based $tmp = str_replace(content_url(), '', $tmp); } } $parts = pathinfo($tmp); $result['subdir'] = $parts['dirname']; $result['filename'] = $parts['filename']; $result['extension'] = $parts['extension']; $result['basename'] = $parts['basename']; // todo filename return $result; }
/** * This function is slightly different from the one below in the case of: * an image hosted on the same domain BUT on a different site than the * Wordpress install will be reported as external content. * * @param string $url a URL to evaluate against * @return boolean if $url points to an external location returns true */ public static function is_external_content($url) { $is_external = TimberURLHelper::is_absolute($url) && !TimberURLHelper::is_internal_content($url); return $is_external; }