Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save modemlooper/9ff1cec2f9a02b1d92a84873a2a9344e to your computer and use it in GitHub Desktop.
Save modemlooper/9ff1cec2f9a02b1d92a84873a2a9344e to your computer and use it in GitHub Desktop.
Updates the group meta for forum id based on updated forum ids.
<?php
/*
Plugin Name: WDS CLP Update Forum Group Meta Script
Description: Updates the group meta for forum id based on updated forum ids.
Author: WebDevStudios
Author URI: http://webdevstudios.com
Version: 1.0
License: GPL2
*/
/*
Copyright (C) 2016 WebDevStudios contact@webdevstudios.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
function wds_clp_get_migrated_groups() {
$groups = array(
'1' => 'Known Bugs',
'3' => 'Storer College',
'7' => 'Permaculture &amp; Off-the-Grid',
'148' => 'Historic Archeology',
'149' => 'Anthropology',
'150' => 'Cultural Landscapes',
'151' => 'Museum Management',
'152' => 'Ask An Expert',
'153' => 'History',
'154' => 'Historic Structures',
'155' => 'Welcome to the Commons',
'158' => 'Battlefield Preservation',
'162' => 'Section 106 of the National Historic Preservation Act',
'163' => 'Relevancy, Diversity, and Inclusion (RDI)',
'175' => 'Office of Outreach, Education, and Training',
'183' => 'History Lesson',
'184' => 'Preservation &amp; Treatment of Park Cultural Resources',
'185' => 'Employee Empowerment Collective',
'186' => 'Allies for Inclusion',
'187' => 'Physical Anthropology',
'188' => 'Innovative Leadership Network',
'189' => 'Cultural Landscapes',
'190' => 'Group Facilitators',
'192' => 'Audience Centered Interpretation',
'193' => 'World War I Commemoration',
'194' => 'Interpretive Peer Collaboration Workshop',
'195' => 'Interpretation Leadership and Business Skills (ILBS)',
'199' => 'Test group name',
'200' => 'NPS Employee Development Officers',
'201' => 'Arc to Equality',
'203' => 'Interpreting Slavery in Urban and Rural Contexts',
'204' => 'All Aboard IDP 2.0!',
'206' => 'Interp Workshop: Social Media Posts',
'207' => 'Youth Programs and Education',
'208' => 'LGBTQ Employee Resource Group',
'209' => 'Hidden LGBTQ Employee Resource Group',
'210' => 'ANPR 2015',
'211' => 'LGBTQ Heritage Initiative',
'212' => 'Interp Workshop: Dialogic Program Development',
'213' => 'Learning &amp; Development Advisory Council (LDAC)',
'215' => 'Workshop: Evaluation Plans',
'216' => 'Teaching Slavery to Reluctant Listeners',
'218' => 'Foundations of Cultural Resources',
'219' => 'Coastal Fortifications',
);
return $groups;
}
function wds_clp_get_forum_id_from_group( $group_name ) {
global $wpdb;
$results = $wpdb->get_results(
$wpdb->prepare( "SELECT ID from $wpdb->posts where post_title = %s", $group_name ), ARRAY_A
);
$forum_id = absint( $results[0]['ID'] );
return $forum_id;
}
function wds_clp_update_forum_enabled( $group_id, $forum_id ) {
groups_update_groupmeta( $group_id, '_bbp_forum_enabled_' . $forum_id, 1 );
}
/**
* Function to get the topics.
* @return array Array of topic post objects.
*/
function wds_clp_get_topics() {
return get_posts( array(
'post_type' => 'topic',
'post_status' => 'publish',
'posts_per_page' => -1,
) );
}
/**
* Function to get the replies.
* @return array Array of reply post objects.
*/
function wds_clp_get_replies() {
return get_posts( array(
'post_type' => 'reply',
'post_status' => 'publish',
'posts_per_page' => -1,
) );
}
/**
* Update topic and reply parents.
*/
function wds_clp_update_topic_parents() {
$topics = wds_clp_get_topics();
printf( '<p>' . __( 'Updating Topic Parents' ) . '</p>' ); // WPCS: XSS ok.
foreach ( $topics as $topic ) {
$old_forum_id = $topic->post_parent;
$forum_id = get_post_meta( $topic->ID, '_bbp_forum_id', true );
if ( absint( $forum_id ) !== $old_forum_id ) {
wp_update_post( array( 'ID' => $topic->ID, 'post_parent' => $forum_id ) );
$results[] = array(
'topic_id' => $topic->ID,
'old_forum_id' => $old_forum_id,
'new_forum_id' => $forum_id,
);
} else {
$results[] = array(
'topic_id' => $topic->ID,
'old_forum_id' => $topic->post_parent,
'new_forum_id' => 'skipped',
);
}
} // end foreach.
printf( '<p>' . __( 'Topic Parents updated, dumping the results:' ) . '</p>' ); // WPCS: XSS ok.
var_dump( $results );
$results = array(); // Reset the results.
$replies = wds_clp_get_replies();
printf( '<p>' . __( 'Updating Reply Parents' ) . '</p>' ); // WPCS: XSS ok.
foreach ( $replies as $reply ) {
$old_topic_id = $reply->post_parent;
$topic_id = get_post_meta( $reply->ID, '_bbp_topic_id', true );
if ( absint( $topic_id ) !== $old_topic_id ) {
wp_update_post( array( 'ID' => $reply->ID, 'post_parent' => $topic_id ) );
$results[] = array(
'reply_id' => $reply->ID,
'reply_title' => $reply->post_title,
'old_topic_id' => $old_topic_id,
'new_topic_id' => $topic_id,
);
} else {
$results[] = array(
'reply_id' => $reply->ID,
'old_topic_id' => $reply->post_parent,
'new_topic_id' => 'skipped',
);
}
} // end foreach.
printf( '<p>' . __( 'Reply Parents updated, dumping the results:' ) . '</p>' ); // WPCS: XSS ok.
var_dump( $results );
}
function wds_clp_update_forum_ids_from_names() {
global $wpdb;
printf( '<p>' . __( 'Updating Forum IDs from Group Names' ) . '</p>' );
$groups = wds_clp_get_migrated_groups();
$results = array();
foreach ( $groups as $group_id => $group_name ) {
$forum_id = wds_clp_get_forum_id_from_group( $group_name );
if ( $forum_id && groups_get_groupmeta( $group_id, 'forum_id' ) ) {
$old_forum_id = groups_get_groupmeta( $group_id, 'forum_id' );
groups_update_groupmeta( $group_id, 'forum_id', array( $forum_id ) );
$new_forum_id = groups_get_groupmeta( $group_id, 'forum_id' );
$results[] = array(
'group_id' => $group_id,
'group_name' => $group_name,
'old_forum_id' => $old_forum_id,
'new_forum_id' => $new_forum_id,
);
} else {
$results[] = array(
'group_id' => $group_id,
'group_name' => $group_name,
'old_forum_id' => 'none',
'new_forum_id' => 'skipped',
);
}
wds_clp_update_forum_enabled( $group_id, $forum_id );
}
// Output all the stuff.
var_dump( $results );
// Update topic and reply post_parents from updated postmeta.
wds_clp_update_topic_parents();
// Deactivate the plugin when we're done.
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( 'We\'re all done. Plugin has been deactivated.' );
}
// Let's do this!
wds_clp_update_forum_ids_from_names();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment