Changeset 6320 for trunk/src/includes/admin/tools/upgrades.php
- Timestamp:
- 02/26/2017 07:12:34 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/includes/admin/tools/upgrades.php
r6282 r6320 156 156 157 157 /** 158 * Upgrade user engagements for bbPress 2.6 and higher 159 * 160 * @since 2.6.0 bbPress (r63010) 161 * 162 * @return array An array of the status code and the message 163 */ 164 function bbp_admin_upgrade_user_engagements() { 165 166 // Define variables 167 $bbp_db = bbp_db(); 168 $statement = __( 'Upgrading user engagements … %s', 'bbpress' ); 169 $result = __( 'No engagements to upgrade.', 'bbpress' ); 170 $total = 0; 171 172 // Delete previous engagements 173 $sql_delete = "DELETE FROM {$bbp_db->postmeta} WHERE meta_key = '_bbp_engagement'"; 174 if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) { 175 return array( 1, sprintf( $statement, $result ) ); 176 } 177 178 // Post types and status 179 $tpt = bbp_get_topic_post_type(); 180 $rpt = bbp_get_reply_post_type(); 181 $pps = bbp_get_public_status_id(); 182 $cps = bbp_get_closed_status_id(); 183 $sql = $bbp_db->prepare( "SELECT postmeta.meta_value, '_bbp_engagement', posts.post_author 184 FROM {$bbp_db->posts} AS posts 185 LEFT JOIN {$bbp_db->postmeta} AS postmeta 186 ON posts.ID = postmeta.post_id 187 AND postmeta.meta_key = '_bbp_topic_id' 188 WHERE posts.post_type IN (%s, %s) 189 AND posts.post_status IN (%s, %s)", $tpt, $rpt, $pps, $cps ); 190 191 $engagements = $bbp_db->get_results( $sql ); 192 193 // Bail if no closed topics found 194 if ( empty( $engagements ) || is_wp_error( $engagements ) ) { 195 return array( 1, sprintf( $statement, $result ) ); 196 } 197 198 // Loop through each user's favorites 199 foreach ( $engagements as $meta ) { 200 201 // Skip if already exists 202 if ( $bbp_db->get_var( $bbp_db->prepare( "SELECT * FROM {$bbp_db->postmeta} WHERE post_id = %d AND meta_key = %s AND meta_value = %d LIMIT 1", $meta->meta_value, '_bbp_engagement', $meta->post_author ) ) ) { 203 continue; 204 } 205 206 // Add the post meta 207 $added = add_post_meta( $meta->meta_value, '_bbp_engagement', $meta->post_author, false ); 208 209 // Bump counts if successfully added 210 if ( ! empty( $added ) ) { 211 ++$total; 212 } 213 } 214 215 // Cleanup 216 unset( $engagements, $added ); 217 218 // Complete results 219 $result = sprintf( _n( 'Complete! %d engagements upgraded.', 'Complete! %d engagements upgraded.', $total, 'bbpress' ), $total ); 220 221 return array( 0, sprintf( $statement, $result ) ); 222 } 223 224 /** 158 225 * Upgrade user favorites for bbPress 2.6 and higher 159 226 *
Note: See TracChangeset
for help on using the changeset viewer.