Skip to content

Commit

Permalink
missing dir function
Browse files Browse the repository at this point in the history
  • Loading branch information
rlankhorst committed May 9, 2023
1 parent 337e4bc commit e4b168d
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,41 @@ function cmplz_upload_dir( string $path=''): string {
return trailingslashit( $upload_dir );
}
}
if ( ! function_exists('cmplz_create_missing_directories_recursively')) {

/**
* Create directories recursively
*
* @param string $path
*/
/**
* Create directories recursively
*
* @param string $path
*/

function cmplz_create_missing_directories_recursively( string $path) {
if ( !cmplz_user_can_manage() ){
return;
function cmplz_create_missing_directories_recursively( string $path ) {
if ( ! cmplz_user_can_manage() ) {
return;
}
$parts = explode( '/', $path );
$dir = '';
foreach ( $parts as $part ) {
$dir .= $part . '/';
if ( !cmplz_has_open_basedir_restriction($dir) && !is_dir( $dir ) && strlen( $dir ) > 0 && is_writable( dirname( $dir, 1 ) ) ) {
mkdir($dir);
}
}
}
$parts = explode('/', $path);
$dir = '';
foreach ($parts as $part) {
$dir .= $part . '/';
if (!is_dir($dir) && strlen($dir) > 0 && is_writable(dirname($dir, 1))) {
if ( ! mkdir( $dir ) && ! is_dir( $dir ) ) {
throw new \RuntimeException( sprintf( 'Directory "%s" was not created', $dir ) );
} }
}

if (!function_exists('cmplz_has_open_basedir_restriction')) {
function cmplz_has_open_basedir_restriction($path) {
// Default error handler is required
set_error_handler(null);
// Clean last error info.
error_clear_last();
// Testing...
@file_exists($path);
// Restore previous error handler
restore_error_handler();
// Return `true` if error has occurred
return ($error = error_get_last()) && $error['message'] !== '__clean_error_info';
}
}

Expand All @@ -76,7 +92,8 @@ function cmplz_create_missing_directories_recursively( string $path) {
function cmplz_upload_url( string $path=''): string {
$uploads = wp_upload_dir();
$upload_url = $uploads['baseurl'];
return trailingslashit( apply_filters('cmplz_upload_url', $upload_url) ).'complianz/'.$path;
$upload_url = trailingslashit( apply_filters('cmplz_upload_url', $upload_url) );
return trailingslashit($upload_url.'complianz/'.$path);
}
}

Expand Down

0 comments on commit e4b168d

Please sign in to comment.