Skip to content

Commit 10fbfa5

Browse files
committed
Convert leading double spaces to tabs... once more, with feeling
1 parent 413cd8c commit 10fbfa5

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

admin/class-pods-export-code.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,7 @@ public function pods_export_code () {
208208
echo "function {$function}() {\n\n";
209209

210210
foreach ( $pod_names as $this_pod ) {
211-
$code_output = $export_to_code->export_pod( $this_pod );
212-
echo preg_replace( "/ {2}/", "\t", $code_output );
211+
echo $export_to_code->export_pod( $this_pod );
213212
}
214213

215214
echo "}\n";

admin/classes/pods-export-code-api.php

+31-2
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,46 @@ public function export_pod( $pod_name ) {
182182
}
183183

184184
// Output the pods_register_type() call
185-
$output .= sprintf( "\t\$pod = %s;\n\n", preg_replace( '/\d+ => /', '', var_export( $pod, true ) ) );
185+
$output .= sprintf( "\t\$pod = %s;\n\n", $this->var_export_format( $pod, 1 ) );
186186
$output .= "\tpods_register_type( \$pod[ 'type' ], \$pod[ 'name' ], \$pod );\n\n";
187187

188188
// Output a pods_register_field() call for each field
189189
foreach ( $fields as $this_field ) {
190-
$output .= sprintf( "\t\$field = %s;\n\n", preg_replace( '/\d+ => /', '', var_export( $this_field, true ) ) );
190+
$output .= sprintf( "\t\$field = %s;\n\n", preg_replace( '/\d+ => /', '', $this->var_export_format( $this_field, 1 ) ) );
191191
$output .= "\tpods_register_field( \$pod[ 'name' ], \$field[ 'name' ], \$field );\n\n";
192192
}
193193

194194
return $output;
195195

196196
}
197197

198+
/**
199+
* @param mixed $var
200+
* @param int $leading_tabs
201+
*
202+
* @return string
203+
*/
204+
private function var_export_format( $var, $leading_tabs = 0 ) {
205+
$var = var_export( $var, true );
206+
$leading_tabs = str_repeat( "\t", $leading_tabs );
207+
208+
// Convert params like 0 => 'Option 1' to just 'Option 1'
209+
$var = preg_replace( '/\d+ => /', '', $var );
210+
211+
$output = '';
212+
foreach( preg_split('~[\r\n]+~', $var ) as $line ){
213+
214+
// Skip blank lines
215+
if( empty($line) || ctype_space( $line ) ) {
216+
continue;
217+
}
218+
219+
// Leading tabs plus replace double spaces with tabs
220+
$output .= sprintf( "%s%s\n", $leading_tabs, preg_replace( "/ {2}/", "\t", $line ) );
221+
}
222+
223+
// Trim the leading tab and the final newline
224+
return ltrim( rtrim( $output, "\n"), "\t" );
225+
}
226+
198227
}

0 commit comments

Comments
 (0)