From 8b1bc2b2b6233e3d5b4b2e0864181f8236809249 Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Fri, 13 Dec 2013 11:36:10 -0600 Subject: [PATCH] Export cleanup for options that we don't need to export --- admin/class-pods-export-code.php | 9 ++ admin/classes/pods-export-code-api.php | 158 +++++++++++++++++++++++-- 2 files changed, 157 insertions(+), 10 deletions(-) diff --git a/admin/class-pods-export-code.php b/admin/class-pods-export-code.php index eb33f3d..fb79881 100644 --- a/admin/class-pods-export-code.php +++ b/admin/class-pods-export-code.php @@ -181,10 +181,19 @@ public function pods_export_code () { } $export_to_code = new Pods_Export_Code_API(); + + // Output function + $function = 'register_my_pods_config_' . rand( 11, rand( 1212, 452452 ) * 651 ); + + echo "function {$function}() {\n\n"; + foreach ( $pod_names as $this_pod ) { echo $export_to_code->export_pod( $this_pod ); } + echo "}\n"; + echo "add_action( 'init', '{$function}' );"; + die(); } diff --git a/admin/classes/pods-export-code-api.php b/admin/classes/pods-export-code-api.php index 71733bd..8760f62 100644 --- a/admin/classes/pods-export-code-api.php +++ b/admin/classes/pods-export-code-api.php @@ -13,8 +13,10 @@ class Pods_Export_Code_API { /** * */ - public function __construct () { + public function __construct() { + $this->api = pods_api(); + } /** @@ -22,15 +24,16 @@ public function __construct () { * * @return string */ - public function export_pod ( $pod_name ) { + public function export_pod( $pod_name ) { $output = ''; // Attempt to load the pod, don't throw an exception on error $params = array( - 'name' => $pod_name, + 'name' => $pod_name, 'fields' => true, ); + $pod = $this->api->load_pod( $params, false ); // Exit if the pod wasn't found or is table based (not supported) @@ -40,21 +43,156 @@ public function export_pod ( $pod_name ) { // Pull out the field list $fields = $pod[ 'fields' ]; - unset( $pod[ 'fields' ] ); - unset( $pod[ 'object_fields' ] ); - unset( $pod[ 'id' ] ); + + $options_ignore = array( + 'id', + 'pod_id', + 'old_name', + 'object_type', + 'object_name', + 'object_hierarchical', + 'table', + 'meta_table', + 'pod_table', + 'field_id', + 'field_index', + 'field_slug', + 'field_type', + 'field_parent', + 'field_parent_select', + 'meta_field_id', + 'meta_field_index', + 'meta_field_value', + 'pod_field_id', + 'pod_field_index', + 'fields', + 'object_fields', + 'join', + 'where', + 'where_default', + 'orderby', + 'pod', + 'recurse', + 'table_info', + 'attributes', + 'group', + 'grouped', + 'developer_mode', + 'dependency', + 'depends-on', + 'excludes-on' + ); + + $empties = array( + 'description', + 'alias', + 'help', + 'class', + 'pick_object', + 'pick_val', + 'sister_id', + 'required', + 'unique', + 'admin_only', + 'restrict_role', + 'restrict_capability', + 'hidden', + 'read_only', + 'object', + 'label_singular' + ); + + $field_types = PodsForm::field_types(); + + $field_type_options = array(); + + foreach ( $field_types as $type => $field_type_data ) { + $field_type_options[ $type ] = PodsForm::ui_options( $type ); + } + + if ( isset( $pod[ 'options' ] ) ) { + $pod = array_merge( $pod, $pod[ 'options' ] ); + + unset( $pod[ 'options' ] ); + } + + foreach ( $pod as $option => $option_value ) { + if ( in_array( $option, $options_ignore ) || null === $option_value ) { + unset( $pod[ $option ] ); + } + elseif ( in_array( $option, $empties ) && ( empty( $option_value ) || '0' == $option_value ) ) { + if ( 'restrict_role' == $option && isset( $pod[ 'roles_allowed' ] ) ) { + unset( $pod[ 'roles_allowed' ] ); + } + elseif ( 'restrict_capability' == $option && isset( $pod[ 'capabilities_allowed' ] ) ) { + unset( $pod[ 'capabilities_allowed' ] ); + } + + unset( $pod[ $option ] ); + } + } + + if ( !empty( $fields ) ) { + foreach ( $fields as &$field ) { + if ( isset( $field[ 'options' ] ) ) { + $field = array_merge( $field, $field[ 'options' ] ); + + unset( $field[ 'options' ] ); + } + + foreach ( $field as $option => $option_value ) { + if ( in_array( $option, $options_ignore ) || null === $option_value ) { + unset( $field[ $option ] ); + } + elseif ( in_array( $option, $empties ) && ( empty( $option_value ) || '0' == $option_value ) ) { + if ( 'restrict_role' == $option && isset( $field[ 'roles_allowed' ] ) ) { + unset( $field[ 'roles_allowed' ] ); + } + elseif ( 'restrict_capability' == $option && isset( $field[ 'capabilities_allowed' ] ) ) { + unset( $field[ 'capabilities_allowed' ] ); + } + + unset( $field[ $option ] ); + } + } + + foreach ( $field_type_options as $type => $options ) { + if ( $type == pods_var( 'type', $field ) ) { + continue; + } + + foreach ( $options as $option_data ) { + if ( isset( $option_data[ 'group' ] ) && is_array( $option_data[ 'group' ] ) && !empty( $option_data[ 'group' ] ) ) { + if ( isset( $field[ $option_data[ 'name' ] ] ) ) { + unset( $field[ $option_data[ 'name' ] ] ); + } + + foreach ( $option_data[ 'group' ] as $group_option_data ) { + if ( isset( $field[ $group_option_data[ 'name' ] ] ) ) { + unset( $field[ $group_option_data[ 'name' ] ] ); + } + } + } + elseif ( isset( $field[ $option_data[ 'name' ] ] ) ) { + unset( $field[ $option_data[ 'name' ] ] ); + } + } + } + } + } // Output the pods_register_type() call - $output .= sprintf( "\$pod = %s;\n\n", var_export( $pod, true ) ); - $output .= sprintf( "pods_register_type( '%s', '%s', \$pod );\n\n", $pod[ 'type' ], $pod_name ); + $output .= sprintf( "\t\$pod = %s;\n\n", preg_replace( '/\d+ => /', '', var_export( $pod, true ) ) ); + $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( "\$field = %s;\n\n", var_export( $this_field, true ) ); - $output .= sprintf( "pods_register_field( '%s', '%s', \$field );\n\n", $pod_name, $this_field[ 'name' ] ); + $output .= sprintf( "\t\$field = %s;\n\n", preg_replace( '/\d+ => /', '', var_export( $this_field, true ) ) ); + $output .= "\tpods_register_field( \$pod[ 'name' ], \$field[ 'name' ], \$field );\n\n"; } return $output; + } } \ No newline at end of file