@@ -13,24 +13,27 @@ class Pods_Export_Code_API {
1313 /**
1414 *
1515 */
16- public function __construct () {
16+ public function __construct () {
17+
1718 $ this ->api = pods_api ();
19+
1820 }
1921
2022 /**
2123 * @param string $pod_name
2224 *
2325 * @return string
2426 */
25- public function export_pod ( $ pod_name ) {
27+ public function export_pod ( $ pod_name ) {
2628
2729 $ output = '' ;
2830
2931 // Attempt to load the pod, don't throw an exception on error
3032 $ params = array (
31- 'name ' => $ pod_name ,
33+ 'name ' => $ pod_name ,
3234 'fields ' => true ,
3335 );
36+
3437 $ pod = $ this ->api ->load_pod ( $ params , false );
3538
3639 // Exit if the pod wasn't found or is table based (not supported)
@@ -40,21 +43,156 @@ public function export_pod ( $pod_name ) {
4043
4144 // Pull out the field list
4245 $ fields = $ pod [ 'fields ' ];
43- unset( $ pod [ 'fields ' ] );
44- unset( $ pod [ 'object_fields ' ] );
45- unset( $ pod [ 'id ' ] );
46+
47+ $ options_ignore = array (
48+ 'id ' ,
49+ 'pod_id ' ,
50+ 'old_name ' ,
51+ 'object_type ' ,
52+ 'object_name ' ,
53+ 'object_hierarchical ' ,
54+ 'table ' ,
55+ 'meta_table ' ,
56+ 'pod_table ' ,
57+ 'field_id ' ,
58+ 'field_index ' ,
59+ 'field_slug ' ,
60+ 'field_type ' ,
61+ 'field_parent ' ,
62+ 'field_parent_select ' ,
63+ 'meta_field_id ' ,
64+ 'meta_field_index ' ,
65+ 'meta_field_value ' ,
66+ 'pod_field_id ' ,
67+ 'pod_field_index ' ,
68+ 'fields ' ,
69+ 'object_fields ' ,
70+ 'join ' ,
71+ 'where ' ,
72+ 'where_default ' ,
73+ 'orderby ' ,
74+ 'pod ' ,
75+ 'recurse ' ,
76+ 'table_info ' ,
77+ 'attributes ' ,
78+ 'group ' ,
79+ 'grouped ' ,
80+ 'developer_mode ' ,
81+ 'dependency ' ,
82+ 'depends-on ' ,
83+ 'excludes-on '
84+ );
85+
86+ $ empties = array (
87+ 'description ' ,
88+ 'alias ' ,
89+ 'help ' ,
90+ 'class ' ,
91+ 'pick_object ' ,
92+ 'pick_val ' ,
93+ 'sister_id ' ,
94+ 'required ' ,
95+ 'unique ' ,
96+ 'admin_only ' ,
97+ 'restrict_role ' ,
98+ 'restrict_capability ' ,
99+ 'hidden ' ,
100+ 'read_only ' ,
101+ 'object ' ,
102+ 'label_singular '
103+ );
104+
105+ $ field_types = PodsForm::field_types ();
106+
107+ $ field_type_options = array ();
108+
109+ foreach ( $ field_types as $ type => $ field_type_data ) {
110+ $ field_type_options [ $ type ] = PodsForm::ui_options ( $ type );
111+ }
112+
113+ if ( isset ( $ pod [ 'options ' ] ) ) {
114+ $ pod = array_merge ( $ pod , $ pod [ 'options ' ] );
115+
116+ unset( $ pod [ 'options ' ] );
117+ }
118+
119+ foreach ( $ pod as $ option => $ option_value ) {
120+ if ( in_array ( $ option , $ options_ignore ) || null === $ option_value ) {
121+ unset( $ pod [ $ option ] );
122+ }
123+ elseif ( in_array ( $ option , $ empties ) && ( empty ( $ option_value ) || '0 ' == $ option_value ) ) {
124+ if ( 'restrict_role ' == $ option && isset ( $ pod [ 'roles_allowed ' ] ) ) {
125+ unset( $ pod [ 'roles_allowed ' ] );
126+ }
127+ elseif ( 'restrict_capability ' == $ option && isset ( $ pod [ 'capabilities_allowed ' ] ) ) {
128+ unset( $ pod [ 'capabilities_allowed ' ] );
129+ }
130+
131+ unset( $ pod [ $ option ] );
132+ }
133+ }
134+
135+ if ( !empty ( $ fields ) ) {
136+ foreach ( $ fields as &$ field ) {
137+ if ( isset ( $ field [ 'options ' ] ) ) {
138+ $ field = array_merge ( $ field , $ field [ 'options ' ] );
139+
140+ unset( $ field [ 'options ' ] );
141+ }
142+
143+ foreach ( $ field as $ option => $ option_value ) {
144+ if ( in_array ( $ option , $ options_ignore ) || null === $ option_value ) {
145+ unset( $ field [ $ option ] );
146+ }
147+ elseif ( in_array ( $ option , $ empties ) && ( empty ( $ option_value ) || '0 ' == $ option_value ) ) {
148+ if ( 'restrict_role ' == $ option && isset ( $ field [ 'roles_allowed ' ] ) ) {
149+ unset( $ field [ 'roles_allowed ' ] );
150+ }
151+ elseif ( 'restrict_capability ' == $ option && isset ( $ field [ 'capabilities_allowed ' ] ) ) {
152+ unset( $ field [ 'capabilities_allowed ' ] );
153+ }
154+
155+ unset( $ field [ $ option ] );
156+ }
157+ }
158+
159+ foreach ( $ field_type_options as $ type => $ options ) {
160+ if ( $ type == pods_var ( 'type ' , $ field ) ) {
161+ continue ;
162+ }
163+
164+ foreach ( $ options as $ option_data ) {
165+ if ( isset ( $ option_data [ 'group ' ] ) && is_array ( $ option_data [ 'group ' ] ) && !empty ( $ option_data [ 'group ' ] ) ) {
166+ if ( isset ( $ field [ $ option_data [ 'name ' ] ] ) ) {
167+ unset( $ field [ $ option_data [ 'name ' ] ] );
168+ }
169+
170+ foreach ( $ option_data [ 'group ' ] as $ group_option_data ) {
171+ if ( isset ( $ field [ $ group_option_data [ 'name ' ] ] ) ) {
172+ unset( $ field [ $ group_option_data [ 'name ' ] ] );
173+ }
174+ }
175+ }
176+ elseif ( isset ( $ field [ $ option_data [ 'name ' ] ] ) ) {
177+ unset( $ field [ $ option_data [ 'name ' ] ] );
178+ }
179+ }
180+ }
181+ }
182+ }
46183
47184 // Output the pods_register_type() call
48- $ output .= sprintf ( "\$pod = %s; \n\n" , var_export ( $ pod , true ) );
49- $ output .= sprintf ( " pods_register_type( '%s', '%s' , \$pod ); \n\n" , $ pod [ 'type ' ], $ pod_name ) ;
185+ $ output .= sprintf ( "\t\ $pod = %s; \n\n" , preg_replace ( ' /\d+ => / ' , '' , var_export ( $ pod , true ) ) );
186+ $ output .= "\t pods_register_type( \$ pod[ 'type' ] , \$pod[ 'name ' ], \$ pod ); \n\n" ;
50187
51188 // Output a pods_register_field() call for each field
52189 foreach ( $ fields as $ this_field ) {
53- $ output .= sprintf ( "\$field = %s; \n\n" , var_export ( $ this_field , true ) );
54- $ output .= sprintf ( " pods_register_field( '%s', '%s' , \$field ); \n\n", $ pod_name , $ this_field [ ' name ' ] ) ;
190+ $ output .= sprintf ( "\t\ $field = %s; \n\n" , preg_replace ( ' /\d+ => / ' , '' , var_export ( $ this_field , true ) ) );
191+ $ output .= "\t pods_register_field( \$ pod[ 'name' ], \$ field[ 'name' ] , \$field ); \n\n" ;
55192 }
56193
57194 return $ output ;
195+
58196 }
59197
60198}
0 commit comments