| 4812 | |
| 4813 | /** |
| 4814 | * Get the prefix of the backup name, compatible with transient names. |
| 4815 | * |
| 4816 | * The backup name is used for both a transient (which stores backup metadata) and a temporary directory (which |
| 4817 | * stores backup files and directories). |
| 4818 | * |
| 4819 | * Plugin/theme names get truncated after 21 characters and it's possible (but quite improbable) that two different |
| 4820 | * packages have exactly the same first 21 characters. In such a case there would be a prefix clash. However, to |
| 4821 | * tell them apart, you can check the original plugin/theme directory name which is stored into the transient. |
| 4822 | * |
| 4823 | * Notice that many backups can potentially exist for each plugin/theme at the same time. The number of backups is at |
| 4824 | * most the number of times the user updated to the latest version, which can occur many times in a row against one |
| 4825 | * new version (due to user's retries) or many successive versions (due to frequent releases). |
| 4826 | * |
| 4827 | * Example for copying back in place user's customizations. |
| 4828 | * <code> |
| 4829 | * $plugin_dir = dirname(__FILE__); |
| 4830 | * $prefix = backup_prefix($plugin_dir, 'plugin'); |
| 4831 | * $backups = get_transients_like("%$prefix%"); |
| 4832 | * if (count($backups)) { |
| 4833 | * $last = array_pop($backups); |
| 4834 | * copy_dir($last['backup_dir'] . '/custom_files', $plugin_dir . '/custom_files'); |
| 4835 | * } |
| 4836 | * </code> |
| 4837 | * |
| 4838 | * @param string $package_dir Plugin/theme directory. |
| 4839 | * @param string $type Either 'plugin' or 'theme'. |
| 4840 | * |
| 4841 | * @return string The prefix. |
| 4842 | */ |
| 4843 | function backup_prefix( $package_dir, $type ) { |
| 4844 | $package_name = basename($package_dir); |
| 4845 | $package_type = ''; |
| 4846 | if ( $type == 'plugin' ) { |
| 4847 | $package_type = '_p'; |
| 4848 | } elseif ( $type == 'theme' ) { |
| 4849 | $package_type = '_t'; |
| 4850 | } |
| 4851 | $transient_name_limit = 45; |
| 4852 | $uniqid_length = 13; |
| 4853 | $placeholder = '?'; |
| 4854 | $backup_name = 'backup' . $package_type . '_' . $placeholder . '_'; |
| 4855 | $cut_length = $transient_name_limit - (strlen($backup_name) + $uniqid_length - strlen($placeholder)); // 21 chars |
| 4856 | $max_package_name = substr($package_name, 0, $cut_length); |
| 4857 | $result = str_replace('?', $max_package_name, $backup_name); // eg: 'backup_p_akismet_' |
| 4858 | return $result; |
| 4859 | } |