Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
pglewis committed Dec 16, 2013
2 parents bdb5137 + 8b1bc2b commit 413cd8c
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 11 deletions.
11 changes: 10 additions & 1 deletion admin/class-pods-export-code.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,21 @@ 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 ) {
$code_output = $export_to_code->export_pod( $this_pod );
echo preg_replace( "/ {2}/", "\t", $code_output );
}

echo "}\n";
echo "add_action( 'init', '{$function}' );";

die();
}

}
}
158 changes: 148 additions & 10 deletions admin/classes/pods-export-code-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,27 @@ class Pods_Export_Code_API {
/**
*
*/
public function __construct () {
public function __construct() {

$this->api = pods_api();

}

/**
* @param string $pod_name
*
* @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)
Expand All @@ -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;

}

}

0 comments on commit 413cd8c

Please sign in to comment.