Skip to:
Content

bbPress.org


Ignore:
Timestamp:
12/27/2016 09:39:14 PM (7 years ago)
Author:
johnjamesjacoby
Message:

Converter: Convert favorites & subscriptions to postmeta vs. usermeta.

  • Update table names
  • Support for comma-separated string values
  • Use strict comparisons where it makes sense to

See #2959, #2668.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/admin/converter.php

    r6129 r6184  
    44 * bbPress Converter
    55 *
    6  * Based on the hard work of Adam Ellis at http://bbconverter.com
     6 * Based on the hard work of Adam Ellis
    77 *
    88 * @package bbPress
     
    636636        $bbp_db     = bbp_db();
    637637        $table_name = $bbp_db->prefix . 'bbp_converter_translator';
    638         if ( ! empty( $drop ) && $bbp_db->get_var( "SHOW TABLES LIKE '{$table_name}'" ) == $table_name ) {
     638        if ( ! empty( $drop ) && $bbp_db->get_var( "SHOW TABLES LIKE '{$table_name}'" ) === $table_name ) {
    639639            $bbp_db->query( "DROP TABLE {$table_name}" );
    640640        }
     
    653653        $max_index_length = 191;
    654654
    655         /** Translator ****************************************************/
     655        /** Translator ********************************************************/
    656656
    657657        $sql[] = "CREATE TABLE {$table_name} (
     
    765765         */
    766766        $this->sync_table_name = $this->wpdb->prefix . 'bbp_converter_translator';
    767         if ( $this->wpdb->get_var( "SHOW TABLES LIKE '" . $this->sync_table_name . "'" ) == $this->sync_table_name ) {
     767        if ( $this->wpdb->get_var( "SHOW TABLES LIKE '" . $this->sync_table_name . "'" ) === $this->sync_table_name ) {
    768768            $this->sync_table = true;
    769769        } else {
     
    772772
    773773        /**
    774          * Charset
     774         * Character set
    775775         */
    776776        if ( empty( $this->wpdb->charset ) ) {
     
    927927
    928928        // Are we usig a sync table, or postmeta?
    929         if ( $this->wpdb->get_var( "SHOW TABLES LIKE '" . $this->sync_table_name . "'" ) == $this->sync_table_name ) {
     929        if ( $this->wpdb->get_var( "SHOW TABLES LIKE '" . $this->sync_table_name . "'" ) === $this->sync_table_name ) {
    930930            $this->sync_table = true;
    931931        } else {
     
    949949
    950950            case 'forum_subscriptions' :
    951                 $tablename = $this->wpdb->usermeta;
     951                $tablename = $this->wpdb->postmeta;
    952952                break;
    953953
    954954            case 'topic_subscriptions' :
    955                 $tablename = $this->wpdb->usermeta;
     955                $tablename = $this->wpdb->postmeta;
    956956                break;
    957957
    958958            case 'favorites' :
    959                 $tablename = $this->wpdb->usermeta;
     959                $tablename = $this->wpdb->postmeta;
    960960                break;
    961961
     
    975975
    976976            // Yay a match, and we have a from table, too
    977             if ( ( $item['to_type'] == $to_type ) && ! empty( $item['from_tablename'] ) ) {
     977            if ( ( $item['to_type'] === $to_type ) && ! empty( $item['from_tablename'] ) ) {
    978978
    979979                // $from_tablename was set from a previous loop iteration
     
    10361036                        // Types match and to_fieldname is present. This means
    10371037                        // we have some work to do here.
    1038                         if ( ( $row['to_type'] == $to_type ) && isset( $row['to_fieldname'] ) ) {
     1038                        if ( ( $row['to_type'] === $to_type ) && isset( $row['to_fieldname'] ) ) {
    10391039
    10401040                            // This row has a destination that matches one of the
     
    10441044                                // Allows us to set default fields.
    10451045                                if ( isset( $row['default'] ) ) {
    1046                                     $insert_post[$row['to_fieldname']] = $row['default'];
     1046                                    $insert_post[ $row['to_fieldname'] ] = $row['default'];
    10471047
    10481048                                // Translates a field from the old forum.
    10491049                                } elseif ( isset( $row['callback_method'] ) ) {
    1050                                     if ( ( 'callback_userid' == $row['callback_method'] ) && empty( $_POST['_bbp_converter_convert_users'] ) ) {
    1051                                         $insert_post[$row['to_fieldname']] = $forum[$row['from_fieldname']];
     1050                                    if ( ( 'callback_userid' === $row['callback_method'] ) && empty( $_POST['_bbp_converter_convert_users'] ) ) {
     1051                                        $insert_post[ $row['to_fieldname'] ] = $forum[ $row['from_fieldname'] ];
    10521052                                    } else {
    1053                                         $insert_post[$row['to_fieldname']] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[$row['from_fieldname']], $forum ) );
     1053                                        $insert_post[ $row['to_fieldname'] ] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[ $row['from_fieldname'] ], $forum ) );
    10541054                                    }
    10551055
    10561056                                // Maps the field from the old forum.
    10571057                                } else {
    1058                                     $insert_post[$row['to_fieldname']] = $forum[$row['from_fieldname']];
     1058                                    $insert_post[ $row['to_fieldname'] ] = $forum[ $row['from_fieldname'] ];
    10591059                                }
    10601060
     
    10651065                                // Allows us to set default fields.
    10661066                                if ( isset( $row['default'] ) ) {
    1067                                     $insert_postmeta[$row['to_fieldname']] = $row['default'];
     1067                                    $insert_postmeta[ $row['to_fieldname'] ] = $row['default'];
    10681068
    10691069                                // Translates a field from the old forum.
    10701070                                } elseif ( isset( $row['callback_method'] ) ) {
    1071                                     if ( ( $row['callback_method'] == 'callback_userid' ) && ( 0 == $_POST['_bbp_converter_convert_users'] ) ) {
    1072                                         $insert_postmeta[$row['to_fieldname']] = $forum[$row['from_fieldname']];
     1071                                    if ( ( $row['callback_method'] === 'callback_userid' ) && ( 0 == $_POST['_bbp_converter_convert_users'] ) ) {
     1072                                        $insert_postmeta[ $row['to_fieldname'] ] = $forum[ $row['from_fieldname'] ];
    10731073                                    } else {
    1074                                         $insert_postmeta[$row['to_fieldname']] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[$row['from_fieldname']], $forum ) );
     1074                                        $insert_postmeta[ $row['to_fieldname'] ] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[ $row['from_fieldname'] ], $forum ) );
    10751075                                    }
    10761076
    10771077                                // Maps the field from the old forum.
    10781078                                } else {
    1079                                     $insert_postmeta[$row['to_fieldname']] = $forum[$row['from_fieldname']];
     1079                                    $insert_postmeta[ $row['to_fieldname'] ] = $forum[ $row['from_fieldname'] ];
    10801080                                }
    10811081                            }
     
    11291129                                break;
    11301130
    1131                             /** Forum Subscriptions *********************************/
     1131                            /** Forum Subscriptions ***************************/
    11321132
    11331133                            case 'forum_subscriptions':
    11341134                                $user_id = $insert_post['user_id'];
    1135                                 if ( is_numeric( $user_id ) ) {
    1136                                     foreach ($insert_postmeta as $key => $value) {
    1137 
    1138                                         // Only extract values from the key _bbp_forum_subscriptions
    1139                                         if ( '_bbp_forum_subscriptions' == $key ) {
    1140 
    1141                                             // Get the new forum ID
    1142                                             $forum_id = $this->callback_forumid( $value );
    1143 
    1144                                             // Add the topic ID to the users subscribed forums
    1145                                             bbp_add_user_forum_subscription( $user_id, $forum_id );
     1135                                $items   = wp_list_pluck( $insert_postmeta, '_bbp_forum_subscriptions' );
     1136                                if ( is_numeric( $user_id ) && ! empty( $items ) ) {
     1137                                    foreach ( $items as $value ) {
     1138
     1139                                        // Maybe string with commas
     1140                                        $value = is_string( $value )
     1141                                            ? explode( ',', $value )
     1142                                            : (array) $value;
     1143
     1144                                        // Add user ID to forums subscribed users
     1145                                        foreach ( $value as $fav ) {
     1146                                            bbp_add_user_forum_subscription( $user_id, $this->callback_forumid( $fav ) );
    11461147                                        }
    11471148                                    }
     
    11531154                            case 'topic_subscriptions':
    11541155                                $user_id = $insert_post['user_id'];
    1155                                 if ( is_numeric( $user_id ) ) {
    1156                                     foreach ($insert_postmeta as $key => $value) {
    1157 
    1158                                         // Only extract values from the key _bbp_subscriptions
    1159                                         if ( '_bbp_subscriptions' == $key ) {
    1160 
    1161                                             // Get the new topic ID
    1162                                             $topic_id = $this->callback_topicid( $value );
    1163 
    1164                                             // Add the topic ID to the users subscribed topics
    1165                                             bbp_add_user_topic_subscription( $user_id, $topic_id );
     1156                                $items   = wp_list_pluck( $insert_postmeta, '_bbp_subscriptions' );
     1157                                if ( is_numeric( $user_id ) && ! empty( $items ) ) {
     1158                                    foreach ( $items as $value ) {
     1159
     1160                                        // Maybe string with commas
     1161                                        $value = is_string( $value )
     1162                                            ? explode( ',', $value )
     1163                                            : (array) $value;
     1164
     1165                                        // Add user ID to topics subscribed users
     1166                                        foreach ( $value as $fav ) {
     1167                                            bbp_add_user_topic_subscription( $user_id, $this->callback_topicid( $fav ) );
    11661168                                        }
    11671169                                    }
     
    11691171                                break;
    11701172
    1171                             /** Favorites *********************************/
     1173                            /** Favorites *************************************/
    11721174
    11731175                            case 'favorites':
    11741176                                $user_id = $insert_post['user_id'];
    1175                                 if ( is_numeric( $user_id ) ) {
    1176 
    1177                                     // Loop through the array
    1178                                     foreach ($insert_postmeta as $key => $value) {
    1179 
    1180                                         // Only extract values from the key _bbp_favorites
    1181                                         if ( '_bbp_favorites' == $key ) {
    1182 
    1183                                             // Our array may contain comma delimited favorites so lets explode these
    1184                                             $insert_postmeta = explode(",", $insert_postmeta['_bbp_favorites']);
    1185 
    1186                                             // Loop through our updated exploded array
    1187                                             foreach ($insert_postmeta as $key => $value) {
    1188 
    1189                                                 // Get the new topic ID
    1190                                                 $topic_id = $this->callback_topicid( $value );
    1191 
    1192                                                 // Add the topic ID to the users favorites
    1193                                                 bbp_add_user_favorite( $user_id, $topic_id );
    1194                                             }
     1177                                $items   = wp_list_pluck( $insert_postmeta, '_bbp_favorites' );
     1178                                if ( is_numeric( $user_id ) && ! empty( $items ) ) {
     1179                                    foreach ( $items as $value ) {
     1180
     1181                                        // Maybe string with commas
     1182                                        $value = is_string( $value )
     1183                                            ? explode( ',', $value )
     1184                                            : (array) $value;
     1185
     1186                                        // Add user ID to topics favorited users
     1187                                        foreach ( $value as $fav ) {
     1188                                            bbp_add_user_favorite( $user_id, $this->callback_topicid( $fav ) );
    11951189                                        }
    11961190                                    }
     
    12261220                                         *          _bbp_old_reply_to_id      // The old reply to ID
    12271221                                         */
    1228                                         if ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) {
     1222                                        if ( '_id' === substr( $key, -3 ) && ( true === $this->sync_table ) ) {
    12291223                                            $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => $key, 'meta_value' => $value ) );
    12301224                                        }
     
    12361230                                         * value using convert_reply_to_parents()
    12371231                                         */
    1238                                         if ( ( 'reply' == $to_type ) && ( '_bbp_old_reply_to_id' == $key ) ) {
     1232                                        if ( ( 'reply' === $to_type ) && ( '_bbp_old_reply_to_id' === $key ) ) {
    12391233                                            add_post_meta( $post_id, '_bbp_reply_to', $value );
    12401234                                        }
     
    16001594        }
    16011595
    1602         if ( $tablename == $this->wpdb->users ) {
     1596        if ( $tablename === $this->wpdb->users ) {
    16031597            $rval[] = 'role';
    16041598            $rval[] = 'yim';
     
    16061600            $rval[] = 'jabber';
    16071601        }
     1602
    16081603        return $rval;
    16091604    }
     
    16381633     */
    16391634    private function callback_forumid( $field ) {
    1640         if ( ! isset( $this->map_forumid[$field] ) ) {
     1635        if ( ! isset( $this->map_forumid[ $field ] ) ) {
    16411636            if ( ! empty( $this->sync_table ) ) {
    16421637                $row = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_old_forum_id" AND meta_value = "%s" LIMIT 1', $field ) );
     
    16461641
    16471642            if ( !is_null( $row ) ) {
    1648                 $this->map_forumid[$field] = $row->value_id;
     1643                $this->map_forumid[ $field ] = $row->value_id;
    16491644            } else {
    1650                 $this->map_forumid[$field] = 0;
    1651             }
    1652         }
    1653         return $this->map_forumid[$field];
     1645                $this->map_forumid[ $field ] = 0;
     1646            }
     1647        }
     1648        return $this->map_forumid[ $field ];
    16541649    }
    16551650
     
    16611656     */
    16621657    private function callback_topicid( $field ) {
    1663         if ( ! isset( $this->map_topicid[$field] ) ) {
     1658        if ( ! isset( $this->map_topicid[ $field ] ) ) {
    16641659            if ( ! empty( $this->sync_table ) ) {
    16651660                $row = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_old_topic_id" AND meta_value = "%s" LIMIT 1', $field ) );
     
    16691664
    16701665            if ( !is_null( $row ) ) {
    1671                 $this->map_topicid[$field] = $row->value_id;
     1666                $this->map_topicid[ $field ] = $row->value_id;
    16721667            } else {
    1673                 $this->map_topicid[$field] = 0;
    1674             }
    1675         }
    1676         return $this->map_topicid[$field];
     1668                $this->map_topicid[ $field ] = 0;
     1669            }
     1670        }
     1671        return $this->map_topicid[ $field ];
    16771672    }
    16781673
     
    16861681     */
    16871682    private function callback_reply_to( $field ) {
    1688         if ( ! isset( $this->map_reply_to[$field] ) ) {
     1683        if ( ! isset( $this->map_reply_to[ $field ] ) ) {
    16891684            if ( ! empty( $this->sync_table ) ) {
    16901685                $row = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_old_reply_id" AND meta_value = "%s" LIMIT 1', $field ) );
     
    16941689
    16951690            if ( !is_null( $row ) ) {
    1696                 $this->map_reply_to[$field] = $row->value_id;
     1691                $this->map_reply_to[ $field ] = $row->value_id;
    16971692            } else {
    1698                 $this->map_reply_to[$field] = 0;
    1699             }
    1700         }
    1701         return $this->map_reply_to[$field];
     1693                $this->map_reply_to[ $field ] = 0;
     1694            }
     1695        }
     1696        return $this->map_reply_to[ $field ];
    17021697    }
    17031698
     
    17091704     */
    17101705    private function callback_userid( $field ) {
    1711         if ( ! isset( $this->map_userid[$field] ) ) {
     1706        if ( ! isset( $this->map_userid[ $field ] ) ) {
    17121707            if ( ! empty( $this->sync_table ) ) {
    17131708                $row = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_old_user_id" AND meta_value = "%s" LIMIT 1', $field ) );
     
    17171712
    17181713            if ( !is_null( $row ) ) {
    1719                 $this->map_userid[$field] = $row->value_id;
     1714                $this->map_userid[ $field ] = $row->value_id;
    17201715            } else {
    1721                 if ( ! empty( $_POST['_bbp_converter_convert_users'] ) && ( $_POST['_bbp_converter_convert_users'] == 1 ) ) {
    1722                     $this->map_userid[$field] = 0;
    1723                 } else {
    1724                     $this->map_userid[$field] = $field;
    1725                 }
    1726             }
    1727         }
    1728         return $this->map_userid[$field];
     1716                if ( ! empty( $_POST['_bbp_converter_convert_users'] ) && ( (int) $_POST['_bbp_converter_convert_users'] === 1 ) ) {
     1717                    $this->map_userid[ $field ] = 0;
     1718                } else {
     1719                    $this->map_userid[ $field ] = $field;
     1720                }
     1721            }
     1722        }
     1723        return $this->map_userid[ $field ];
    17291724    }
    17301725
     
    17571752        $topicid = $this->callback_topicid( $field );
    17581753        if ( empty( $topicid ) ) {
    1759             $this->map_topicid_to_forumid[$topicid] = 0;
    1760         } elseif ( ! isset( $this->map_topicid_to_forumid[$topicid] ) ) {
     1754            $this->map_topicid_to_forumid[ $topicid ] = 0;
     1755        } elseif ( ! isset( $this->map_topicid_to_forumid[ $topicid ] ) ) {
    17611756            $row = $this->wpdb->get_row( 'SELECT post_parent FROM ' . $this->wpdb->posts . ' WHERE ID = "' . $topicid . '" LIMIT 1' );
    17621757
    17631758            if ( !is_null( $row ) ) {
    1764                 $this->map_topicid_to_forumid[$topicid] = $row->post_parent;
     1759                $this->map_topicid_to_forumid[ $topicid ] = $row->post_parent;
    17651760            } else {
    1766                 $this->map_topicid_to_forumid[$topicid] = 0;
    1767             }
    1768         }
    1769 
    1770         return $this->map_topicid_to_forumid[$topicid];
     1761                $this->map_topicid_to_forumid[ $topicid ] = 0;
     1762            }
     1763        }
     1764
     1765        return $this->map_topicid_to_forumid[ $topicid ];
    17711766    }
    17721767
     
    18201815            if ( stristr( $file, '.php' ) && stristr( $file, 'index' ) === FALSE ) {
    18211816                $file = preg_replace( '/.php/', '', $file );
    1822                 if ( $platform == $file ) {
     1817                if ( $platform === $file ) {
    18231818                    $found = true;
    18241819                    continue;
Note: See TracChangeset for help on using the changeset viewer.