Skip to content

Commit

Permalink
Built-in CPT<>CT Sync to implement pull request with fixes for #202
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0ttkclark committed Aug 1, 2012
1 parent 4a52a6a commit 1509606
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion classes/PodsAPI.php
Expand Up @@ -874,6 +874,69 @@ public function save_pod ( $params ) {
) );
}

// Sync built-in options for post types and taxonomies
if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && empty( $pod[ 'object' ] ) ) {
// Build list of 'built_in' for later
$built_in = array();

foreach ( $pod[ 'options' ] as $key => $val ) {
if ( false === strpos( $key, 'built_in_' ) )
continue;
elseif ( false === strpos( $key, 'built_in_post_types_' ) )
$built_in_type = 'post_type';
elseif ( false === strpos( $key, 'built_in_taxonomies_' ) )
$built_in_type = 'taxonomy';
else
continue;

if ( $built_in_type == $pod[ 'type' ] )
continue;

if ( !isset( $built_in[ $built_in_type ] ) )
$built_in[ $built_in_type ] = array();

$built_in_object = str_replace( array( 'built_in_post_types_', 'built_in_taxonomies_' ), '', $key );

$built_in[ $built_in_type ][ $built_in_object ] = max( pods_absint( $val ), 1 );
}

$lookup_option = $lookup_built_in = false;

$lookup_name = $pod[ 'name' ];

if ( 'post_type' == $pod[ 'type' ] && isset( $built_in[ $pod[ 'type' ] ] ) ) {
$lookup_option = 'built_in_post_types_' . $lookup_name;
$lookup_built_in = 'taxonomy';
}
elseif ( 'taxonomy' == $pod[ 'type' ] && isset( $built_in[ $pod[ 'type' ] ] ) ) {
$lookup_option = 'built_in_taxonomies_' . $lookup_name;
$lookup_built_in = 'post_type';
}

if ( !empty( $lookup_option ) && !empty( $lookup_built_in ) ) {
foreach ( $built_in[ $lookup_built_in ] as $built_in_object => $val ) {
$search_val = 1 ^ $val;

$query = "SELECT p.ID FROM {$wpdb->posts} AS p
LEFT JOIN {$wpdb->postmeta} AS pm ON pm.post_id = p.ID AND pm.meta_key = '{$lookup_option}'
LEFT JOIN {$wpdb->postmeta} AS pm2 ON pm2.post_id = p.ID AND pm2.meta_key = 'type' AND pm2.meta_value = '{$lookup_built_in}'
LEFT JOIN {$wpdb->postmeta} AS pm3 ON pm3.post_id = p.ID AND pm3.meta_key = 'object' AND pm3.meta_value = ''
WHERE p.post_type = '_pods_pod' AND p.post_name = '{$built_in_object}'
AND pm2.meta_id IS NOT NULL
AND ( pm.meta_id IS NULL OR pm.meta_value = {$search_val} )";

$results = pods_query( $query );

if ( !empty( $results ) ) {
foreach ( $results as $the_pod ) {
delete_post_meta( $the_pod->ID, $lookup_option );
add_post_meta( $the_pod->ID, $lookup_option, $val );
}
}
}
}
}

$saved = array();

if ( isset( $params->fields ) || isset( $params->field_data ) || defined( 'DOING_AJAX' ) ) {
Expand Down Expand Up @@ -2130,7 +2193,7 @@ public function delete_field ( $params, $table_operation = true ) {
$field = $this->load_field( array( 'name' => $params->name, 'id' => $params->id ) );

if ( false === $field )
return pods_error( __( 'Field not found', 'pods' ), $this );
return pods_error( __( 'Field not found', 'pods' ), $this );

$params->id = $field[ 'id' ];
$params->name = $field[ 'name' ];
Expand Down

0 comments on commit 1509606

Please sign in to comment.