Skip to content

Instantly share code, notes, and snippets.

@dcavins
Created July 23, 2015 19:58
Show Gist options
  • Save dcavins/80c2c83e6b74295e9b10 to your computer and use it in GitHub Desktop.
Save dcavins/80c2c83e6b74295e9b10 to your computer and use it in GitHub Desktop.
Do some data cleaning/unification on activation or upgrade.
<?php
function bp_docs_upgrade_script_1_9() {
global $wpdb;
$bp = buddypress();
// Clean the meta_values that got grossed up in BP 2.0
$wpdb->update( $bp->groups->table_name_groupmeta,
array( 'meta_key' => 'bp-docs' ),
array( 'meta_key' => 'bpdocs' )
);
// Make sure every group has a valid setting.
$group_ids = bp_docs_get_groups_missing_docs_settings();
// Set default settings of "on" and "member" because that was the old default if no setting was set.
$settings = array(
'group-enable' => 1,
'can-create' => 'member',
);
foreach ( $group_ids as $group_id ) {
groups_update_groupmeta( $group_id, 'bp-docs', $settings );
}
}
function bp_docs_install_script() {
global $wpdb;
$bp = buddypress();
// Make sure every group has a valid setting.
$group_ids = bp_docs_get_groups_missing_docs_settings();
// Set default settings of "off" and "member" so that docs are enabled by request, not default.
$settings = array(
'group-enable' => 0,
'can-create' => 'member',
);
foreach ( $group_ids as $group_id ) {
groups_update_groupmeta( $group_id, 'bp-docs', $settings );
}
}
function bp_docs_get_groups_missing_docs_settings() {
global $wpdb;
$bp = buddypress();
$group_ids = $wpdb->get_col(
"SELECT g.id FROM {$bp->groups->table_name} g
LEFT JOIN {$bp->groups->table_name_groupmeta} AS m
ON (g.id = m.group_id AND m.meta_key = 'bp-docs')
WHERE ( CAST(m.meta_value AS CHAR) = '' OR m.id IS NULL )"
);
return array_map( 'intval', $group_ids );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment