/var/www/html_de/wp-content/plugins/woocommerce/packages/blueprint/src/Util.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php

namespace Automattic\WooCommerce\Blueprint;

use 
RecursiveArrayIterator;
use 
RecursiveIteratorIterator;

/**
 * Utility functions.
 */
class Util {
    
/**
     * Ensure that the given path is a valid path within the WP_CONTENT_DIR.
     *
     * @param string $path The path to be validated.
     *
     * @return string
     * @throws \InvalidArgumentException If the path is invalid.
     */
    
public static function ensure_wp_content_path$path ) {
        
$path realpath$path );
        if ( 
false === $path || strpos$pathWP_CONTENT_DIR ) !== ) {
            throw new 
\InvalidArgumentException"Invalid path: $path);
        }

        return 
$path;
    }

    
/**
     * Convert an array to an insert SQL query.
     *
     * @param array  $row Array row with key and value.
     * @param string $table Name of the table.
     * @param string $type One of insert, insert ignore, replace into.
     *
     * @return false|string
     */
    
public static function array_to_insert_sql$row$table$type 'insert ignore' ) {
        if ( empty( 
$row ) || ! is_array$row ) ) {
            return 
false// Return false if input data is empty or not an array.
        
}

        
$allowed_types = array( 'insert''insert ignore''replace into' );
        if ( ! 
in_array$type$allowed_typestrue ) ) {
            return 
false// Return false if input type is not valid.
        
}

        
// Get column names and values.
        
$columns        '`' implode'`, `'array_keys$row ) ) . '`';
        
$escaped_values array_map( fn( $value ) => "'" addslashes$value ) . "'"$row );
        
$values         implode', '$escaped_values );
        
// Construct final SQL query.
        
return "{$type} `$table` ($columns) VALUES ($values);";
    }

    
/**
     * Convert a string from snake_case to camelCase.
     *
     * @param string $string_to_convert The string to be converted.
     *
     * @return string
     */
    
public static function snake_to_camel$string_to_convert ) {
        
// Split the string by underscores.
        
$words explode'_'$string_to_convert );

        
// Capitalize the first letter of each word.
        
$words array_map'ucfirst'$words );

        
// Join the words back together.
        
return implode''$words );
    }

    
/**
     * Flatten an array.
     *
     * @param array $array_to_flatten The array to be flattened.
     *
     * @return \RecursiveIteratorIterator
     */
    
public static function array_flatten$array_to_flatten ) {
        return new 
RecursiveIteratorIterator( new RecursiveArrayIterator$array_to_flatten ) );
    }

    
/**
     * Convert a string from camelCase to snake_case.
     *
     * @param string $input The string to be converted.
     *
     * @return string
     */
    
public static function camel_to_snake$input ) {
        
// Replace all uppercase letters with an underscore followed by the lowercase version of the letter.
        
$pattern     '/([a-z])([A-Z])/';
        
$replacement '$1_$2';
        
$snake       preg_replace$pattern$replacement$input );

        
// Replace spaces with underscores.
        
$snake str_replace' ''_'$snake );

        
// Convert the entire string to lowercase.
        
return strtolower$snake );
    }


    
/**
     * Index an array using a callback function.
     *
     * @param array    $array The array to be indexed.
     * @param callable $callback The callback function to be called for each element.
     *
     * @return array
     */
    // phpcs:ignore
    
public static function index_array$array$callback ) {
        
$result = array();
        foreach ( 
$array as $key => $value ) {
            
$new_key            $callback$key$value );
            
$result$new_key ] = $value;
        }
        return 
$result;
    }

    
/**
     * Check to see if given string is a valid WordPress plugin slug.
     *
     * @param string $slug The slug to be validated.
     *
     * @return bool
     */
    
public static function is_valid_wp_plugin_slug$slug ) {
        
// Check if the slug only contains allowed characters.
        
if ( preg_match'/^[a-z0-9-]+$/'$slug ) ) {
            return 
true;
        }

        return 
false;
    }

    
/**
     * Recursively delete a directory.
     *
     * @param string $dir_path The path to the directory.
     *
     * @return void
     * @throws \InvalidArgumentException If $dir_path is not a directory.
     */
    
public static function delete_dir$dir_path ) {
        if ( ! 
is_dir$dir_path ) ) {
            throw new 
\InvalidArgumentException"$dir_path must be a directory" );
        }
        if ( 
substr$dir_pathstrlen$dir_path ) - 1) !== '/' ) {
            
$dir_path .= '/';
        }
        
$files glob$dir_path '*'GLOB_MARK );
        foreach ( 
$files as $file ) {
            if ( 
is_dir$file ) ) {
                static::
delete_dir$file );
            } else {
                
// phpcs:ignore
                
unlink$file );
            }
        }
        
// phpcs:ignore
        
rmdir$dir_path );
    }
}