From b0d2b29b6dacdfd50a88bbaf9bfb75d530f3ac95 Mon Sep 17 00:00:00 2001 From: Leo Fajardo Date: Mon, 6 Nov 2017 22:25:57 +0800 Subject: [PATCH 1/6] [paths] Store relative paths instead of absolute paths to the storage. --- includes/class-freemius.php | 114 ++++++++++++++++++++++++++++++++---- 1 file changed, 103 insertions(+), 11 deletions(-) diff --git a/includes/class-freemius.php b/includes/class-freemius.php index f91387def..9645db9e1 100755 --- a/includes/class-freemius.php +++ b/includes/class-freemius.php @@ -555,6 +555,18 @@ function _data_migration( $sdk_prev_version, $sdk_version ) { return; } + if ( version_compare( $sdk_prev_version, '1.2.2.11', '<' ) && + version_compare( $sdk_version, '1.2.2.11', '>=' ) + ) { + /** + * Starting from version 1.2.2.11, paths are stored as relative paths and not absolute paths; so when + * upgrading to 1.2.2.11, make paths relative. + * + * @author Leo Fajardo (@leorw) + */ + $this->make_paths_relative(); + } + if ( version_compare( $sdk_prev_version, '1.1.5', '<' ) && version_compare( $sdk_version, '1.1.5', '>=' ) ) { @@ -578,6 +590,30 @@ function _data_migration( $sdk_prev_version, $sdk_version ) { } } + /** + * Makes paths relative. + * + * @author Leo Fajardo + * @since 1.2.2.11 + */ + private function make_paths_relative() { + $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() ); + + if ( isset( $id_slug_type_path_map[ $this->_module_id ]['path'] ) ) { + $id_slug_type_path_map[ $this->_module_id ]['path'] = $this->get_relative_path( $id_slug_type_path_map[ $this->_module_id ]['path'] ); + + self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true ); + } + + if ( isset( $this->_storage->plugin_main_file ) ) { + $plugin_main_file = $this->_storage->plugin_main_file; + + if ( isset( $plugin_main_file->path ) ) { + $this->_storage->plugin_main_file->path = $this->get_relative_path( $this->_storage->plugin_main_file->path ); + } + } + } + /** * @author Vova Feldman (@svovaf) * @since 1.2.2.7 @@ -874,8 +910,11 @@ private function _find_caller_plugin_file( $is_init = false ) { // Try to load the cached value of the file path. if ( isset( $this->_storage->plugin_main_file ) ) { $plugin_main_file = $this->_storage->plugin_main_file; - if ( isset( $plugin_main_file->path ) && file_exists( $plugin_main_file->path ) ) { - return $plugin_main_file->path; + if ( isset( $plugin_main_file->path ) ) { + $absolute_path = $this->get_absolute_path( $plugin_main_file->path ); + if ( file_exists( $absolute_path ) ) { + return $absolute_path; + } } } @@ -894,8 +933,9 @@ private function _find_caller_plugin_file( $is_init = false ) { if ( isset( $this->_storage->plugin_main_file ) && isset( $this->_storage->plugin_main_file->prev_path ) ) { - if ( file_exists( $this->_storage->plugin_main_file->prev_path ) ) { - return $this->_storage->plugin_main_file->prev_path; + $absolute_path = $this->get_absolute_path( $this->_storage->plugin_main_file->prev_path ); + if ( file_exists( $absolute_path ) ) { + return $absolute_path; } } @@ -918,9 +958,49 @@ private function _find_caller_plugin_file( $is_init = false ) { 'path' => $id_slug_type_path_map[ $this->_module_id ]['path'], ); - return $id_slug_type_path_map[ $this->_module_id ]['path']; + return $this->get_absolute_path( $id_slug_type_path_map[ $this->_module_id ]['path'] ); } + /** + * @author Leo Fajardo (@leorw) + * @since 1.2.2.11 + * + * @param string $path + * + * @return string + */ + private function get_relative_path( $path ) { + return $this->_plugin_basename . + ( $this->is_theme() ? + '/' . basename( $path ) : + '' ); + } + + /** + * @author Leo Fajardo (@leorw) + * @since 1.2.2.11 + * + * @param string $path + * @param string|bool $module_type + * + * @return string + */ + private function get_absolute_path( $path, $module_type = false ) { + $is_plugin = empty( $module_type ) ? + $this->is_plugin() : + ( WP_FS__MODULE_TYPE_PLUGIN === $module_type ); + + $module_root_dir = fs_normalize_path( $is_plugin ? + WP_PLUGIN_DIR : + get_theme_root() ); + + if ( 0 !== strpos( $path, $module_root_dir ) ) { + $path = ( $module_root_dir . '/' . $path ); + } + + return $path; + } + /** * @author Leo Fajardo (@leorw) * @@ -950,7 +1030,10 @@ private function store_id_slug_type_path_map( $module_id, $slug ) { * @author Vova Feldman (@svovaf) * @since 1.2.3 */ - ! file_exists( $id_slug_type_path_map[ $module_id ]['path'] ) + ! file_exists( $this->get_absolute_path( + $id_slug_type_path_map[ $module_id ]['path'], + $id_slug_type_path_map[ $module_id ]['type'] + ) ) ) { $caller_main_file_and_type = $this->get_caller_main_file_and_type(); @@ -1042,7 +1125,10 @@ private function get_caller_main_file_and_type() { if ( $caller_file_path == fs_normalize_path( realpath( trailingslashit( $themes_dir ) . basename( dirname( $caller_file_path ) ) . '/' . basename( $caller_file_path ) ) ) ) { $module_type = WP_FS__MODULE_TYPE_THEME; - $caller_file_candidate = $caller_file_path; + $caller_file_candidate = basename( dirname( $caller_file_path ) ) . + '/' . + basename( $caller_file_path ); + continue; } } @@ -1060,7 +1146,7 @@ private function get_caller_main_file_and_type() { if ( isset( $caller_map[ $caller_file_hash ] ) ) { $module_type = WP_FS__MODULE_TYPE_PLUGIN; - $caller_file_candidate = $caller_map[ $caller_file_hash ]; + $caller_file_candidate = plugin_basename( $caller_map[ $caller_file_hash ] ); } } @@ -5855,14 +5941,20 @@ function get_module_label( $lowercase = false ) { * @author Vova Feldman (@svovaf) * @since 1.0.4 * + * @param string $plugin_main_file_path + * * @return string */ - function get_plugin_basename() { + function get_plugin_basename( $plugin_main_file_path = false ) { + if ( empty( $plugin_main_file_path ) ) { + $plugin_main_file_path = $this->_plugin_main_file_path; + } + if ( ! isset( $this->_plugin_basename ) ) { if ( $this->is_plugin() ) { - $this->_plugin_basename = plugin_basename( $this->_plugin_main_file_path ); + $this->_plugin_basename = plugin_basename( $plugin_main_file_path ); } else { - $this->_plugin_basename = basename( dirname( $this->_plugin_main_file_path ) ); + $this->_plugin_basename = basename( dirname( $plugin_main_file_path ) ); } } From 0b4c32b07930be03ad5d4fbbe885730c6db2474a Mon Sep 17 00:00:00 2001 From: Leo Fajardo Date: Tue, 7 Nov 2017 19:40:33 +0800 Subject: [PATCH 2/6] [paths] Enhancements and fixes --- includes/class-freemius.php | 70 +++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/includes/class-freemius.php b/includes/class-freemius.php index 9645db9e1..6de4f072d 100755 --- a/includes/class-freemius.php +++ b/includes/class-freemius.php @@ -555,17 +555,17 @@ function _data_migration( $sdk_prev_version, $sdk_version ) { return; } - if ( version_compare( $sdk_prev_version, '1.2.2.11', '<' ) && - version_compare( $sdk_version, '1.2.2.11', '>=' ) + if ( version_compare( $sdk_prev_version, '1.2.3', '<' ) && + version_compare( $sdk_version, '1.2.3', '>=' ) ) { /** - * Starting from version 1.2.2.11, paths are stored as relative paths and not absolute paths; so when - * upgrading to 1.2.2.11, make paths relative. + * Starting from version 1.2.3, paths are stored as relative paths and not absolute paths; so when + * upgrading to 1.2.3, make paths relative. * * @author Leo Fajardo (@leorw) */ $this->make_paths_relative(); - } + } if ( version_compare( $sdk_prev_version, '1.1.5', '<' ) && version_compare( $sdk_version, '1.1.5', '>=' ) @@ -594,9 +594,9 @@ function _data_migration( $sdk_prev_version, $sdk_version ) { * Makes paths relative. * * @author Leo Fajardo - * @since 1.2.2.11 + * @since 1.2.3 */ - private function make_paths_relative() { + private function make_paths_relative() { $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() ); if ( isset( $id_slug_type_path_map[ $this->_module_id ]['path'] ) ) { @@ -610,6 +610,8 @@ private function make_paths_relative() { if ( isset( $plugin_main_file->path ) ) { $this->_storage->plugin_main_file->path = $this->get_relative_path( $this->_storage->plugin_main_file->path ); + } else if ( isset( $plugin_main_file->prev_path ) ) { + $this->_storage->plugin_main_file->prev_path = $this->get_relative_path( $this->_storage->plugin_main_file->prev_path ); } } } @@ -910,9 +912,9 @@ private function _find_caller_plugin_file( $is_init = false ) { // Try to load the cached value of the file path. if ( isset( $this->_storage->plugin_main_file ) ) { $plugin_main_file = $this->_storage->plugin_main_file; - if ( isset( $plugin_main_file->path ) ) { + if ( isset( $plugin_main_file->path ) ) { $absolute_path = $this->get_absolute_path( $plugin_main_file->path ); - if ( file_exists( $absolute_path ) ) { + if ( file_exists( $absolute_path ) ) { return $absolute_path; } } @@ -963,39 +965,44 @@ private function _find_caller_plugin_file( $is_init = false ) { /** * @author Leo Fajardo (@leorw) - * @since 1.2.2.11 + * @since 1.2.3 * * @param string $path * * @return string */ - private function get_relative_path( $path ) { - return $this->_plugin_basename . - ( $this->is_theme() ? - '/' . basename( $path ) : - '' ); + private function get_relative_path( $path ) { + $module_root_dir = fs_normalize_path( trailingslashit( $this->is_plugin() ? + WP_PLUGIN_DIR : + get_theme_root() ) ); + + if ( 0 === strpos( $path, $module_root_dir ) ) { + $path = str_replace( $module_root_dir, '', $path ); + } + + return $path; } /** * @author Leo Fajardo (@leorw) - * @since 1.2.2.11 + * @since 1.2.3 * * @param string $path * @param string|bool $module_type * * @return string */ - private function get_absolute_path( $path, $module_type = false ) { - $is_plugin = empty( $module_type ) ? + private function get_absolute_path( $path, $module_type = false ) { + $is_plugin = empty( $module_type ) ? $this->is_plugin() : ( WP_FS__MODULE_TYPE_PLUGIN === $module_type ); - $module_root_dir = fs_normalize_path( $is_plugin ? + $module_root_dir = fs_normalize_path( $is_plugin ? WP_PLUGIN_DIR : get_theme_root() ); - if ( 0 !== strpos( $path, $module_root_dir ) ) { - $path = ( $module_root_dir . '/' . $path ); + if ( 0 !== strpos( $path, $module_root_dir ) ) { + $path = trailingslashit( $module_root_dir ) . $path; } return $path; @@ -1124,7 +1131,14 @@ private function get_caller_main_file_and_type() { */ if ( $caller_file_path == fs_normalize_path( realpath( trailingslashit( $themes_dir ) . basename( dirname( $caller_file_path ) ) . '/' . basename( $caller_file_path ) ) ) ) { - $module_type = WP_FS__MODULE_TYPE_THEME; + $module_type = WP_FS__MODULE_TYPE_THEME; + + /** + * Relative path of the theme, e.g.: + * `my-theme/functions.php` + * + * @author Leo Fajardo (@leorw) + */ $caller_file_candidate = basename( dirname( $caller_file_path ) ) . '/' . basename( $caller_file_path ); @@ -5941,20 +5955,14 @@ function get_module_label( $lowercase = false ) { * @author Vova Feldman (@svovaf) * @since 1.0.4 * - * @param string $plugin_main_file_path - * * @return string */ - function get_plugin_basename( $plugin_main_file_path = false ) { - if ( empty( $plugin_main_file_path ) ) { - $plugin_main_file_path = $this->_plugin_main_file_path; - } - + function get_plugin_basename() { if ( ! isset( $this->_plugin_basename ) ) { if ( $this->is_plugin() ) { - $this->_plugin_basename = plugin_basename( $plugin_main_file_path ); + $this->_plugin_basename = plugin_basename( $this->_plugin_main_file_path ); } else { - $this->_plugin_basename = basename( dirname( $plugin_main_file_path ) ); + $this->_plugin_basename = basename( dirname( $this->_plugin_main_file_path ) ); } } From ec29bc9ce539eab55b9ed8998d0a86bfc4fa73eb Mon Sep 17 00:00:00 2001 From: Leo Fajardo Date: Tue, 7 Nov 2017 19:47:31 +0800 Subject: [PATCH 3/6] [paths] [minor] [spacing] --- includes/class-freemius.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-freemius.php b/includes/class-freemius.php index 6de4f072d..3896a2d9d 100755 --- a/includes/class-freemius.php +++ b/includes/class-freemius.php @@ -1139,7 +1139,7 @@ private function get_caller_main_file_and_type() { * * @author Leo Fajardo (@leorw) */ - $caller_file_candidate = basename( dirname( $caller_file_path ) ) . + $caller_file_candidate = basename( dirname( $caller_file_path ) ) . '/' . basename( $caller_file_path ); From 3d37d809be057ee2856b75ff9ac870e09af7ad59 Mon Sep 17 00:00:00 2001 From: Leo Fajardo Date: Tue, 7 Nov 2017 23:42:56 +0800 Subject: [PATCH 4/6] [paths] Enhancements related to absolute and relative paths "calculation". --- includes/class-freemius.php | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/includes/class-freemius.php b/includes/class-freemius.php index 3896a2d9d..4b95c5864 100755 --- a/includes/class-freemius.php +++ b/includes/class-freemius.php @@ -972,12 +972,9 @@ private function _find_caller_plugin_file( $is_init = false ) { * @return string */ private function get_relative_path( $path ) { - $module_root_dir = fs_normalize_path( trailingslashit( $this->is_plugin() ? - WP_PLUGIN_DIR : - get_theme_root() ) ); - + $module_root_dir = $this->get_module_root_dir_path(); if ( 0 === strpos( $path, $module_root_dir ) ) { - $path = str_replace( $module_root_dir, '', $path ); + $path = substr( $path, strlen( $module_root_dir ) ); } return $path; @@ -993,19 +990,30 @@ private function get_relative_path( $path ) { * @return string */ private function get_absolute_path( $path, $module_type = false ) { + $module_root_dir = $this->get_module_root_dir_path( $module_type ); + if ( 0 !== strpos( $path, $module_root_dir ) ) { + $path = fs_normalize_path( $module_root_dir . $path ); + } + + return $path; + } + + /** + * @author Leo Fajardo (@leorw) + * @since 1.2.3 + * + * @param string|bool $module_type + * + * @return string + */ + private function get_module_root_dir_path( $module_type = false ) { $is_plugin = empty( $module_type ) ? $this->is_plugin() : ( WP_FS__MODULE_TYPE_PLUGIN === $module_type ); - $module_root_dir = fs_normalize_path( $is_plugin ? + return fs_normalize_path( trailingslashit( $is_plugin ? WP_PLUGIN_DIR : - get_theme_root() ); - - if ( 0 !== strpos( $path, $module_root_dir ) ) { - $path = trailingslashit( $module_root_dir ) . $path; - } - - return $path; + get_theme_root() ) ); } /** From 76567866afdfe4b68f6f5e8f528c9a75c7a684ba Mon Sep 17 00:00:00 2001 From: Leo Fajardo Date: Wed, 8 Nov 2017 19:48:37 +0800 Subject: [PATCH 5/6] [paths] [symlinks] Made symlink SDK path relative. --- start.php | 57 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/start.php b/start.php index d76a32717..28b95abd0 100644 --- a/start.php +++ b/start.php @@ -364,11 +364,11 @@ define( 'WP_FS__SDK_VERSION', $this_sdk_version ); } - $plugins_or_theme_dir_path = trailingslashit( $is_theme ? + $plugins_or_theme_dir_path = fs_normalize_path( trailingslashit( $is_theme ? get_theme_root() : - WP_PLUGIN_DIR ); + WP_PLUGIN_DIR ) ); - if ( 0 === strpos( $file_path, fs_normalize_path( $plugins_or_theme_dir_path ) ) ) { + if ( 0 === strpos( $file_path, $plugins_or_theme_dir_path ) ) { // No symlinks } else { /** @@ -384,13 +384,24 @@ is_object( $fs_active_plugins->plugins[ $this_sdk_relative_path ] ) && ! empty( $fs_active_plugins->plugins[ $this_sdk_relative_path ]->sdk_symlink ) ) { - $sdk_symlink = $fs_active_plugins->plugins[ $this_sdk_relative_path ]->sdk_symlink; - $realpath = realpath( $sdk_symlink ); - - if ( ! is_string( $realpath ) || ! file_exists( $realpath ) ) { - $sdk_symlink = null; - } - } + $sdk_symlink = $fs_active_plugins->plugins[ $this_sdk_relative_path ]->sdk_symlink; + if ( 0 === strpos( $sdk_symlink, $plugins_or_theme_dir_path ) ) { + /** + * Make the symlink path relative. + * + * @author Leo Fajardo (@leorw) + */ + $sdk_symlink = substr( $sdk_symlink, strlen( $plugins_or_theme_dir_path ) ); + + $fs_active_plugins->plugins[ $this_sdk_relative_path ]->sdk_symlink = $sdk_symlink; + update_option( 'fs_active_plugins', $fs_active_plugins ); + } + + $realpath = realpath( $plugins_or_theme_dir_path . $sdk_symlink ); + if ( ! is_string( $realpath ) || ! file_exists( $realpath ) ) { + $sdk_symlink = null; + } + } if ( empty( $sdk_symlink ) ) // Has symlinks, therefore, we need to configure WP_FS__DIR based on the symlink. { @@ -401,13 +412,26 @@ while ( '/' !== $partial_path_left && ( false === $realpath || $file_path !== fs_normalize_path( $realpath ) ) ) { - $partial_path_right = trailingslashit( basename( $partial_path_left ) ) . $partial_path_right; - $partial_path_left = dirname( $partial_path_left ); - $realpath = realpath( $plugins_or_theme_dir_path . $partial_path_right ); + $partial_path_right = trailingslashit( basename( $partial_path_left ) ) . $partial_path_right; + $partial_path_left_prev = $partial_path_left; + $partial_path_left = dirname( $partial_path_left_prev ); + + /** + * Avoid infinite loop if for example `$partial_path_left_prev` is `C:/`, in this case, + * `dirname( 'C:/' )` will return `C:/`. + * + * @author Leo Fajardo (@leorw) + */ + if ( $partial_path_left === $partial_path_left_prev ) { + $partial_path_left = ''; + break; + } + + $realpath = realpath( $plugins_or_theme_dir_path . $partial_path_right ); } - if ( '/' !== $partial_path_left ) { - $sdk_symlink = fs_normalize_path( $plugins_or_theme_dir_path . dirname( $partial_path_right ) ); + if ( ! empty( $partial_path_left ) && '/' !== $partial_path_left ) { + $sdk_symlink = fs_normalize_path( dirname( $partial_path_right ) ); // Cache value. if ( isset( $fs_active_plugins->plugins[ $this_sdk_relative_path ] ) && @@ -416,13 +440,12 @@ $fs_active_plugins->plugins[ $this_sdk_relative_path ]->sdk_symlink = $sdk_symlink; update_option( 'fs_active_plugins', $fs_active_plugins ); } - } } if ( ! empty( $sdk_symlink ) ) { // Set SDK dir to the symlink path. - define( 'WP_FS__DIR', $sdk_symlink ); + define( 'WP_FS__DIR', $plugins_or_theme_dir_path . $sdk_symlink ); } } From a336d261e70470fef86b191e17e698a19ed10dda Mon Sep 17 00:00:00 2001 From: Leo Fajardo Date: Thu, 9 Nov 2017 20:11:55 +0800 Subject: [PATCH 6/6] [paths] [symlinks] Remove invalid path if there's any. --- includes/class-freemius.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/includes/class-freemius.php b/includes/class-freemius.php index 4b95c5864..0b6be3220 100755 --- a/includes/class-freemius.php +++ b/includes/class-freemius.php @@ -614,6 +614,20 @@ private function make_paths_relative() { $this->_storage->plugin_main_file->prev_path = $this->get_relative_path( $this->_storage->plugin_main_file->prev_path ); } } + + // Remove invalid path that is still associated with the current slug if there's any. + $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() ); + foreach ( $file_slug_map as $plugin_basename => $slug ) { + if ( $slug === $this->_slug && + $plugin_basename !== $this->_plugin_basename && + ! file_exists( $this->get_absolute_path( $plugin_basename ) ) + ) { + unset( $file_slug_map[ $plugin_basename ] ); + self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true ); + + break; + } + } } /**