diff --git a/admin/class-pods-export-code.php b/admin/class-pods-export-code.php index 0e4bc6e..4134107 100644 --- a/admin/class-pods-export-code.php +++ b/admin/class-pods-export-code.php @@ -208,8 +208,7 @@ public function pods_export_code () { echo "function {$function}() {\n\n"; foreach ( $pod_names as $this_pod ) { - $code_output = $export_to_code->export_pod( $this_pod ); - echo preg_replace( "/ {2}/", "\t", $code_output ); + echo $export_to_code->export_pod( $this_pod ); } echo "}\n"; diff --git a/admin/classes/pods-export-code-api.php b/admin/classes/pods-export-code-api.php index 8760f62..a8fe855 100644 --- a/admin/classes/pods-export-code-api.php +++ b/admin/classes/pods-export-code-api.php @@ -182,12 +182,12 @@ public function export_pod( $pod_name ) { } // Output the pods_register_type() call - $output .= sprintf( "\t\$pod = %s;\n\n", preg_replace( '/\d+ => /', '', var_export( $pod, true ) ) ); + $output .= sprintf( "\t\$pod = %s;\n\n", $this->var_export_format( $pod, 1 ) ); $output .= "\tpods_register_type( \$pod[ 'type' ], \$pod[ 'name' ], \$pod );\n\n"; // Output a pods_register_field() call for each field foreach ( $fields as $this_field ) { - $output .= sprintf( "\t\$field = %s;\n\n", preg_replace( '/\d+ => /', '', var_export( $this_field, true ) ) ); + $output .= sprintf( "\t\$field = %s;\n\n", preg_replace( '/\d+ => /', '', $this->var_export_format( $this_field, 1 ) ) ); $output .= "\tpods_register_field( \$pod[ 'name' ], \$field[ 'name' ], \$field );\n\n"; } @@ -195,4 +195,33 @@ public function export_pod( $pod_name ) { } + /** + * @param mixed $var + * @param int $leading_tabs + * + * @return string + */ + private function var_export_format( $var, $leading_tabs = 0 ) { + $var = var_export( $var, true ); + $leading_tabs = str_repeat( "\t", $leading_tabs ); + + // Convert params like 0 => 'Option 1' to just 'Option 1' + $var = preg_replace( '/\d+ => /', '', $var ); + + $output = ''; + foreach( preg_split('~[\r\n]+~', $var ) as $line ){ + + // Skip blank lines + if( empty($line) || ctype_space( $line ) ) { + continue; + } + + // Leading tabs plus replace double spaces with tabs + $output .= sprintf( "%s%s\n", $leading_tabs, preg_replace( "/ {2}/", "\t", $line ) ); + } + + // Trim the leading tab and the final newline + return ltrim( rtrim( $output, "\n"), "\t" ); + } + } \ No newline at end of file