Cli
1 year ago
Exporters
1 year ago
Importers
11 months ago
ResourceStorages
1 year ago
ResultFormatters
1 year ago
Schemas
1 year ago
Steps
1 year ago
docs
1 year ago
BuiltInExporters.php
1 year ago
BuiltInStepProcessors.php
1 year ago
ClassExtractor.php
1 year ago
Cli.php
1 year ago
ExportSchema.php
1 year ago
ImportSchema.php
1 year ago
ImportStep.php
1 year ago
Logger.php
1 year ago
ResourceStorages.php
1 year ago
StepProcessor.php
1 year ago
StepProcessorResult.php
1 year ago
UsePluginHelpers.php
1 year ago
UsePubSub.php
1 year ago
UseWPFunctions.php
1 year ago
Util.php
1 year ago
Util.php
168 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Blueprint; |
| 4 | |
| 5 | use RecursiveArrayIterator; |
| 6 | use RecursiveIteratorIterator; |
| 7 | |
| 8 | /** |
| 9 | * Utility functions. |
| 10 | */ |
| 11 | class Util { |
| 12 | /** |
| 13 | * Ensure that the given path is a valid path within the WP_CONTENT_DIR. |
| 14 | * |
| 15 | * @param string $path The path to be validated. |
| 16 | * |
| 17 | * @return string |
| 18 | * @throws \InvalidArgumentException If the path is invalid. |
| 19 | */ |
| 20 | public static function ensure_wp_content_path( $path ) { |
| 21 | $path = realpath( $path ); |
| 22 | if ( false === $path || strpos( $path, WP_CONTENT_DIR ) !== 0 ) { |
| 23 | throw new \InvalidArgumentException( "Invalid path: $path" ); |
| 24 | } |
| 25 | |
| 26 | return $path; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Convert an array to an insert SQL query. |
| 31 | * |
| 32 | * @param array $row Array row with key and value. |
| 33 | * @param string $table Name of the table. |
| 34 | * @param string $type One of insert, insert ignore, replace into. |
| 35 | * |
| 36 | * @return false|string |
| 37 | */ |
| 38 | public static function array_to_insert_sql( $row, $table, $type = 'insert ignore' ) { |
| 39 | if ( empty( $row ) || ! is_array( $row ) ) { |
| 40 | return false; // Return false if input data is empty or not an array. |
| 41 | } |
| 42 | |
| 43 | $allowed_types = array( 'insert', 'insert ignore', 'replace into' ); |
| 44 | if ( ! in_array( $type, $allowed_types, true ) ) { |
| 45 | return false; // Return false if input type is not valid. |
| 46 | } |
| 47 | |
| 48 | // Get column names and values. |
| 49 | $columns = '`' . implode( '`, `', array_keys( $row ) ) . '`'; |
| 50 | $escaped_values = array_map( fn( $value ) => "'" . addslashes( $value ) . "'", $row ); |
| 51 | $values = implode( ', ', $escaped_values ); |
| 52 | // Construct final SQL query. |
| 53 | return "{$type} `$table` ($columns) VALUES ($values);"; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Convert a string from snake_case to camelCase. |
| 58 | * |
| 59 | * @param string $string_to_convert The string to be converted. |
| 60 | * |
| 61 | * @return string |
| 62 | */ |
| 63 | public static function snake_to_camel( $string_to_convert ) { |
| 64 | // Split the string by underscores. |
| 65 | $words = explode( '_', $string_to_convert ); |
| 66 | |
| 67 | // Capitalize the first letter of each word. |
| 68 | $words = array_map( 'ucfirst', $words ); |
| 69 | |
| 70 | // Join the words back together. |
| 71 | return implode( '', $words ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Flatten an array. |
| 76 | * |
| 77 | * @param array $array_to_flatten The array to be flattened. |
| 78 | * |
| 79 | * @return \RecursiveIteratorIterator |
| 80 | */ |
| 81 | public static function array_flatten( $array_to_flatten ) { |
| 82 | return new RecursiveIteratorIterator( new RecursiveArrayIterator( $array_to_flatten ) ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Convert a string from camelCase to snake_case. |
| 87 | * |
| 88 | * @param string $input The string to be converted. |
| 89 | * |
| 90 | * @return string |
| 91 | */ |
| 92 | public static function camel_to_snake( $input ) { |
| 93 | // Replace all uppercase letters with an underscore followed by the lowercase version of the letter. |
| 94 | $pattern = '/([a-z])([A-Z])/'; |
| 95 | $replacement = '$1_$2'; |
| 96 | $snake = preg_replace( $pattern, $replacement, $input ); |
| 97 | |
| 98 | // Replace spaces with underscores. |
| 99 | $snake = str_replace( ' ', '_', $snake ); |
| 100 | |
| 101 | // Convert the entire string to lowercase. |
| 102 | return strtolower( $snake ); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /** |
| 107 | * Index an array using a callback function. |
| 108 | * |
| 109 | * @param array $array The array to be indexed. |
| 110 | * @param callable $callback The callback function to be called for each element. |
| 111 | * |
| 112 | * @return array |
| 113 | */ |
| 114 | // phpcs:ignore |
| 115 | public static function index_array( $array, $callback ) { |
| 116 | $result = array(); |
| 117 | foreach ( $array as $key => $value ) { |
| 118 | $new_key = $callback( $key, $value ); |
| 119 | $result[ $new_key ] = $value; |
| 120 | } |
| 121 | return $result; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Check to see if given string is a valid WordPress plugin slug. |
| 126 | * |
| 127 | * @param string $slug The slug to be validated. |
| 128 | * |
| 129 | * @return bool |
| 130 | */ |
| 131 | public static function is_valid_wp_plugin_slug( $slug ) { |
| 132 | // Check if the slug only contains allowed characters. |
| 133 | if ( preg_match( '/^[a-z0-9-]+$/', $slug ) ) { |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Recursively delete a directory. |
| 142 | * |
| 143 | * @param string $dir_path The path to the directory. |
| 144 | * |
| 145 | * @return void |
| 146 | * @throws \InvalidArgumentException If $dir_path is not a directory. |
| 147 | */ |
| 148 | public static function delete_dir( $dir_path ) { |
| 149 | if ( ! is_dir( $dir_path ) ) { |
| 150 | throw new \InvalidArgumentException( "$dir_path must be a directory" ); |
| 151 | } |
| 152 | if ( substr( $dir_path, strlen( $dir_path ) - 1, 1 ) !== '/' ) { |
| 153 | $dir_path .= '/'; |
| 154 | } |
| 155 | $files = glob( $dir_path . '*', GLOB_MARK ); |
| 156 | foreach ( $files as $file ) { |
| 157 | if ( is_dir( $file ) ) { |
| 158 | static::delete_dir( $file ); |
| 159 | } else { |
| 160 | // phpcs:ignore |
| 161 | unlink( $file ); |
| 162 | } |
| 163 | } |
| 164 | // phpcs:ignore |
| 165 | rmdir( $dir_path ); |
| 166 | } |
| 167 | } |
| 168 |