/** * Returns image metadata. * * NOTE: The image must be within the WP_CONTENT/UPLOADS folder or within the STYLESHEETPATH folder. * * @access public * @since 8.1.2 * @static * * @param string $source URL or absolute path to an image. * * @return mixed array | object An associative array of image meta or an instance of WP_Error. */ public static function info($source) { // Define upload path & dir. $upload_info = cnUpload::info(); $theme_url = get_stylesheet_directory_uri(); $theme_dir = get_stylesheet_directory(); if (path_is_absolute($source)) { // Ensure the supplied path is in either the WP_CONTENT/UPLOADS directory or // the STYLESHEETPATH directory. if (strpos($source, $upload_info['base_path']) !== FALSE || strpos($source, $theme_dir) !== FALSE) { $img_path = $source; } else { $img_path = FALSE; } } else { // find the path of the image. Perform 2 checks: // #1 check if the image is in the uploads folder if (strpos($source, $upload_info['base_url']) !== FALSE) { $rel_path = str_replace($upload_info['base_url'], '', $source); $img_path = $upload_info['base_path'] . $rel_path; // #2 check if the image is in the current theme folder } else { if (strpos($source, $theme_url) !== FALSE) { $rel_path = str_replace($theme_url, '', $source); $img_path = $theme_dir . $rel_path; } } } // Fail if we can't find the image in our WP local directory if (empty($img_path) || !@file_exists($img_path)) { if (empty($img_path)) { return new WP_Error('image_path_not_set', esc_html__('The $img_path variable has not been set.', 'connections')); } else { return new WP_Error('image_path_not_found', __(sprintf('Image path %s does not exist.', $img_path), 'connections'), $img_path); } } // Check if img path exists, and is an image. if (($image_info = getimagesize($img_path)) === FALSE) { return new WP_Error('image_not_image', __(sprintf('The file %s is not an image.', basename($img_path)), 'connections'), basename($img_path)); } $image_info['path'] = $img_path; $image_info['modified'] = filemtime($img_path); $image_info = array_merge(pathinfo($img_path), $image_info); return $image_info; }
/** * A filter to change the WP core upload path for files. * * @access private * @static * @since 8.1 * @param array $file The WP core upload path values. * * @return array */ public function subDirectory($file) { // If this is a multi site AND Connections is in multi site mode then the the paths passed by WP can be used. if (is_multisite() && CN_MULTISITE_ENABLED) { $file['subdir'] = empty($this->subDirectory) ? cnURL::preslashit($file['subdir']) : cnURL::preslashit($this->subDirectory); $file['path'] = untrailingslashit($file['basedir']) . $file['subdir']; $file['url'] = untrailingslashit($file['baseurl']) . $file['subdir']; // If Connections is on single site or in single site mode on a multi site setup use cnUpload::info() to get the path info. } else { // NOTE: Important! cnUpload::info() can not be used within this class when `if ( is_multisite() && CN_MULTISITE_ENABLED )` // because it will cause a infinite loop due to the filter added in $this->file() which add this method as a callback // to the `upload_dir` hook. $info = cnUpload::info(); $file['subdir'] = empty($this->subDirectory) ? cnURL::preslashit($file['subdir']) : cnURL::preslashit($this->subDirectory); $file['path'] = untrailingslashit($info['base_path']) . $file['subdir']; $file['url'] = untrailingslashit($info['base_url']) . $file['subdir']; } return $file; }