/** * Convert Table * * @param string to type * @param int Start row */ public function convert_table($to_type, $start) { // Are we usig a sync table, or postmeta? if ($this->wpdb->get_var("SHOW TABLES LIKE '" . $this->sync_table_name . "'") == $this->sync_table_name) { $this->sync_table = true; } else { $this->sync_table = false; } // Set some defaults $has_insert = false; $from_tablename = ''; $field_list = $from_tables = $tablefield_array = array(); // Toggle Table Name based on $to_type (destination) switch ($to_type) { case 'user': $tablename = $this->wpdb->users; break; case 'tags': $tablename = ''; break; case 'forum_subscriptions': $tablename = $this->wpdb->usermeta; break; case 'topic_subscriptions': $tablename = $this->wpdb->usermeta; break; case 'favorites': $tablename = $this->wpdb->usermeta; break; default: $tablename = $this->wpdb->posts; } // Get the fields from the destination table if (!empty($tablename)) { $tablefield_array = $this->get_fields($tablename); } /** Step 1 ************************************************************/ // Loop through the field maps, and look for to_type matches foreach ($this->field_map as $item) { // Yay a match, and we have a from table, too if ($item['to_type'] == $to_type && !empty($item['from_tablename'])) { // $from_tablename was set from a previous loop iteration if (!empty($from_tablename)) { // Doing some joining if (!in_array($item['from_tablename'], $from_tables) && in_array($item['join_tablename'], $from_tables)) { $from_tablename .= ' ' . $item['join_type'] . ' JOIN ' . $this->opdb->prefix . $item['from_tablename'] . ' AS ' . $item['from_tablename'] . ' ' . $item['join_expression']; } // $from_tablename needs to be set } else { $from_tablename = $item['from_tablename'] . ' AS ' . $item['from_tablename']; } // Specific FROM expression data used if (!empty($item['from_expression'])) { // No 'WHERE' in expression if (stripos($from_tablename, "WHERE") === false) { $from_tablename .= ' ' . $item['from_expression']; // 'WHERE' in expression, so replace with 'AND' } else { $from_tablename .= ' ' . str_replace("WHERE", "AND", $item['from_expression']); } } // Add tablename and fieldname to arrays, formatted for querying $from_tables[] = $item['from_tablename']; $field_list[] = 'convert(' . $item['from_tablename'] . '.' . $item['from_fieldname'] . ' USING "' . $this->charset . '") AS ' . $item['from_fieldname']; } } /** Step 2 ************************************************************/ // We have a $from_tablename, so we want to get some data to convert if (!empty($from_tablename)) { // Get some data from the old forums $field_list = array_unique($field_list); $forum_query = 'SELECT ' . implode(',', $field_list) . ' FROM ' . $this->opdb->prefix . $from_tablename . ' LIMIT ' . $start . ', ' . $this->max_rows; $forum_array = $this->opdb->get_results($forum_query, ARRAY_A); // Set this query as the last one ran update_option('_bbp_converter_query', $forum_query); // Query returned some results if (!empty($forum_array)) { // Loop through results foreach ((array) $forum_array as $forum) { // Reset some defaults $insert_post = $insert_postmeta = $insert_data = array(); // Loop through field map, again... foreach ($this->field_map as $row) { // Types match and to_fieldname is present. This means // we have some work to do here. if ($row['to_type'] == $to_type && isset($row['to_fieldname'])) { // This row has a destination that matches one of the // columns in this table. if (in_array($row['to_fieldname'], $tablefield_array)) { // Allows us to set default fields. if (isset($row['default'])) { $insert_post[$row['to_fieldname']] = $row['default']; // Translates a field from the old forum. } elseif (isset($row['callback_method'])) { if ('callback_userid' == $row['callback_method'] && empty($_POST['_bbp_converter_convert_users'])) { $insert_post[$row['to_fieldname']] = $forum[$row['from_fieldname']]; } else { $insert_post[$row['to_fieldname']] = call_user_func_array(array($this, $row['callback_method']), array($forum[$row['from_fieldname']], $forum)); } // Maps the field from the old forum. } else { $insert_post[$row['to_fieldname']] = $forum[$row['from_fieldname']]; } // Destination field is not empty, so we might need // to do some extra work or set a default. } elseif (!empty($row['to_fieldname'])) { // Allows us to set default fields. if (isset($row['default'])) { $insert_postmeta[$row['to_fieldname']] = $row['default']; // Translates a field from the old forum. } elseif (isset($row['callback_method'])) { if ($row['callback_method'] == 'callback_userid' && 0 == $_POST['_bbp_converter_convert_users']) { $insert_postmeta[$row['to_fieldname']] = $forum[$row['from_fieldname']]; } else { $insert_postmeta[$row['to_fieldname']] = call_user_func_array(array($this, $row['callback_method']), array($forum[$row['from_fieldname']], $forum)); } // Maps the field from the old forum. } else { $insert_postmeta[$row['to_fieldname']] = $forum[$row['from_fieldname']]; } } } } /** Step 3 ************************************************/ // Something to insert into the destination field if (count($insert_post) > 0 || $to_type == 'tags' && count($insert_postmeta) > 0) { switch ($to_type) { /** New user **************************************/ case 'user': if (username_exists($insert_post['user_login'])) { $insert_post['user_login'] = '******' . $insert_post['user_login']; } if (email_exists($insert_post['user_email'])) { $insert_post['user_email'] = 'imported_' . $insert_post['user_email']; } $post_id = wp_insert_user($insert_post); if (is_numeric($post_id)) { foreach ($insert_postmeta as $key => $value) { add_user_meta($post_id, $key, $value, true); if ('_id' == substr($key, -3) && true === $this->sync_table) { $this->wpdb->insert($this->sync_table_name, array('value_type' => 'user', 'value_id' => $post_id, 'meta_key' => $key, 'meta_value' => $value)); } } } break; /** New Topic-Tag *********************************/ /** New Topic-Tag *********************************/ case 'tags': $post_id = wp_set_object_terms($insert_postmeta['objectid'], $insert_postmeta['name'], 'topic-tag', true); $term = get_term_by('name', $insert_postmeta['name'], 'topic-tag'); if (false !== $term) { wp_update_term($term->term_id, 'topic-tag', array('description' => $insert_postmeta['description'], 'slug' => $insert_postmeta['slug'])); } break; /** Forum Subscriptions *********************************/ /** Forum Subscriptions *********************************/ case 'forum_subscriptions': $user_id = $insert_post['user_id']; if (is_numeric($user_id)) { foreach ($insert_postmeta as $key => $value) { // Only extract values from the key _bbp_forum_subscriptions if ('_bbp_forum_subscriptions' == $key) { // Get the new forum ID $forum_id = $this->callback_forumid($value); // Add the topic ID to the users subscribed forums bbp_add_user_forum_subscription($user_id, $forum_id); } } } break; /** Subscriptions *********************************/ /** Subscriptions *********************************/ case 'topic_subscriptions': $user_id = $insert_post['user_id']; if (is_numeric($user_id)) { foreach ($insert_postmeta as $key => $value) { // Only extract values from the key _bbp_subscriptions if ('_bbp_subscriptions' == $key) { // Get the new topic ID $topic_id = $this->callback_topicid($value); // Add the topic ID to the users subscribed topics bbp_add_user_topic_subscription($user_id, $topic_id); } } } break; /** Favorites *********************************/ /** Favorites *********************************/ case 'favorites': $user_id = $insert_post['user_id']; if (is_numeric($user_id)) { // Loop through the array foreach ($insert_postmeta as $key => $value) { // Only extract values from the key _bbp_favorites if ('_bbp_favorites' == $key) { // Our array may contain comma delimited favorites so lets explode these $insert_postmeta = explode(",", $insert_postmeta['_bbp_favorites']); // Loop through our updated exploded array foreach ($insert_postmeta as $key => $value) { // Get the new topic ID $topic_id = $this->callback_topicid($value); // Add the topic ID to the users favorites bbp_add_user_favorite($user_id, $topic_id); } } } } break; /** Forum, Topic, Reply ***************************/ /** Forum, Topic, Reply ***************************/ default: $post_id = wp_insert_post($insert_post); if (is_numeric($post_id)) { foreach ($insert_postmeta as $key => $value) { add_post_meta($post_id, $key, $value, true); /** * If we are using the sync_table add * the meta '_id' keys to the table * * Forums: _bbp_old_forum_id // The old forum ID * _bbp_old_forum_parent_id // The old forum parent ID * * Topics: _bbp_forum_id // The new forum ID * _bbp_old_topic_id // The old topic ID * _bbp_old_closed_status_id // The old topic open/closed status * _bbp_old_sticky_status_id // The old topic sticky status * * Replies: _bbp_forum_id // The new forum ID * _bbp_topic_id // The new topic ID * _bbp_old_reply_id // The old reply ID * _bbp_old_reply_to_id // The old reply to ID */ if ('_id' == substr($key, -3) && true === $this->sync_table) { $this->wpdb->insert($this->sync_table_name, array('value_type' => 'post', 'value_id' => $post_id, 'meta_key' => $key, 'meta_value' => $value)); } /** * Replies need to save their old reply_to ID for * hierarchical replies association. Later we update * the _bbp_reply_to value with the new bbPress * value using convert_reply_to_parents() */ if ('reply' == $to_type && '_bbp_old_reply_to_id' == $key) { add_post_meta($post_id, '_bbp_reply_to', $value); } } } break; } $has_insert = true; } } } } return !$has_insert; }
/** * Add a topic to user's subscriptions * * @since bbPress (r5156) * * @param int $user_id Optional. User id * @param int $topic_id Optional. Topic id * @uses get_post() To get the post object * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions * @uses bbp_get_forum_post_type() To get the forum post type * @uses bbp_get_topic_post_type() To get the topic post type * @uses update_user_option() To update the user's subscriptions * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id * @return bool Always true */ function bbp_add_user_subscription($user_id = 0, $object_id = 0) { if (empty($user_id) || empty($object_id)) { return false; } // Get the post type $post_type = get_post_type($object_id); if (empty($post_type)) { return false; } switch ($post_type) { // Forum case bbp_get_forum_post_type(): bbp_add_user_forum_subscription($user_id, $object_id); break; // Topic // Topic case bbp_get_topic_post_type(): default: bbp_add_user_topic_subscription($user_id, $object_id); break; } do_action('bbp_add_user_subscription', $user_id, $object_id, $post_type); return true; }
/** * @covers ::bbp_remove_user_topic_subscription */ public function test_bbp_remove_user_topic_subscription() { $u = $this->factory->user->create(); $t = $this->factory->topic->create(); // Add forum subscription. bbp_add_user_topic_subscription($u, $t); $this->assertTrue(bbp_is_user_subscribed_to_topic($u, $t)); // Remove topic subscription. bbp_remove_user_topic_subscription($u, $t); $this->assertFalse(bbp_is_user_subscribed_to_topic($u, $t)); }