| 1 |
<?php |
| 2 |
/** |
| 3 |
* Recount Activity |
| 4 |
* |
| 5 |
* @package GamiPress\bbPress\Admin\Recount_Activity |
| 6 |
* @since 1.0.2 |
| 7 |
*/ |
| 8 |
// Exit if accessed directly |
| 9 |
if( !defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Add recountable options to the Recount Activity Tool |
| 13 |
* |
| 14 |
* @since 1.0.2 |
| 15 |
* |
| 16 |
* @param array $recountable_activity_triggers |
| 17 |
* |
| 18 |
* @return array |
| 19 |
*/ |
| 20 |
function gamipress_bbp_recountable_activity_triggers( $recountable_activity_triggers ) { |
| 21 |
|
| 22 |
$recountable_activity_triggers[__( 'bbPress', 'gamipress' )] = array( |
| 23 |
'bbp_activities' => __( 'Recount forum activities (forums, topics and replies)', 'gamipress' ), |
| 24 |
'bbp_favorites' => __( 'Recount topics favorites', 'gamipress' ), |
| 25 |
); |
| 26 |
|
| 27 |
return $recountable_activity_triggers; |
| 28 |
|
| 29 |
} |
| 30 |
add_filter( 'gamipress_recountable_activity_triggers', 'gamipress_bbp_recountable_activity_triggers' ); |
| 31 |
|
| 32 |
/** |
| 33 |
* Recount bbPress activity |
| 34 |
* |
| 35 |
* @since 1.0.2 |
| 36 |
* |
| 37 |
* @param array $response |
| 38 |
* @param int $loop |
| 39 |
* @param int $limit |
| 40 |
* @param int $offset |
| 41 |
* |
| 42 |
* @return array $response |
| 43 |
*/ |
| 44 |
function gamipress_bbp_activity_recount_activities( $response, $loop, $limit, $offset ) { |
| 45 |
|
| 46 |
global $wpdb; |
| 47 |
|
| 48 |
$post_types = array( |
| 49 |
bbp_get_forum_post_type(), |
| 50 |
bbp_get_topic_post_type(), |
| 51 |
bbp_get_reply_post_type(), |
| 52 |
); |
| 53 |
|
| 54 |
// Get all stored posts count |
| 55 |
$posts_count = absint( $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts AS p WHERE p.post_type IN ('" . implode( "', '", $post_types ) . "') AND p.post_status = 'publish'" ) ); |
| 56 |
|
| 57 |
// On first loop send an informational text |
| 58 |
if( $loop === 0 && $posts_count > $limit ) { |
| 59 |
$response['run_again'] = true; |
| 60 |
$response['message'] = sprintf( __( '%d items found, recounting...', 'gamipress' ), $posts_count ); |
| 61 |
$response['log'] = sprintf( __( '%d items found, recounting...', 'gamipress' ), $posts_count ) . "<br>\n"; |
| 62 |
|
| 63 |
// Return early to inform |
| 64 |
return $response; |
| 65 |
} |
| 66 |
|
| 67 |
// Get all published forums, topics and replies |
| 68 |
$posts = $wpdb->get_results( |
| 69 |
"SELECT p.ID, p.post_type, p.post_author, p.post_parent FROM $wpdb->posts AS p WHERE p.post_type IN ('" . implode( "', '", $post_types ) . "') AND p.post_status = 'publish' LIMIT {$offset}, {$limit}" |
| 70 |
); |
| 71 |
|
| 72 |
foreach( $posts as $post ) { |
| 73 |
|
| 74 |
if( $post->post_type === bbp_get_forum_post_type() ) { |
| 75 |
|
| 76 |
// Trigger new forum action for each forum |
| 77 |
gamipress_bbp_new_forum( array( |
| 78 |
'forum_id' => $post->ID, |
| 79 |
'forum_author' => $post->post_author, |
| 80 |
) ); |
| 81 |
|
| 82 |
$response['log'] .= sprintf( __( '[Create forum] Forum: %s User: %s', 'gamipress-learndash-integration' ), |
| 83 |
'<a href="' . get_edit_post_link( $post->ID ) . '" target="_blank">' . $post->ID . '</a>', |
| 84 |
'<a href="' . get_edit_user_link( $post->post_author ) . '" target="_blank">' . $post->post_author . '</a>' |
| 85 |
) . "<br>\n"; |
| 86 |
|
| 87 |
} else if( $post->post_type === bbp_get_topic_post_type() ) { |
| 88 |
|
| 89 |
// Trigger new topic action for each topic |
| 90 |
gamipress_bbp_new_topic( $post->ID, $post->post_parent, array(), $post->post_author ); |
| 91 |
|
| 92 |
$response['log'] .= sprintf( __( '[Create topic] Topic: %s Forum: %s User: %s', 'gamipress-learndash-integration' ), |
| 93 |
'<a href="' . get_edit_post_link( $post->ID ) . '" target="_blank">' . $post->ID . '</a>', |
| 94 |
'<a href="' . get_edit_post_link( $post->post_parent ) . '" target="_blank">' . $post->post_parent . '</a>', |
| 95 |
'<a href="' . get_edit_user_link( $post->post_author ) . '" target="_blank">' . $post->post_author . '</a>' |
| 96 |
) . "<br>\n"; |
| 97 |
|
| 98 |
} else if( $post->post_type === bbp_get_reply_post_type() ) { |
| 99 |
|
| 100 |
$forum_id = wp_get_post_parent_id( $post->post_parent ); |
| 101 |
|
| 102 |
if( $forum_id ) { |
| 103 |
// Trigger new reply action for each reply |
| 104 |
gamipress_bbp_new_reply( $post->ID, $post->post_parent, $forum_id, array(), $post->post_author ); |
| 105 |
|
| 106 |
$response['log'] .= sprintf( __( '[Create reply] Reply: %s Topic: %s Forum: %s User: %s', 'gamipress-learndash-integration' ), |
| 107 |
'<a href="' . get_edit_post_link( $post->ID ) . '" target="_blank">' . $post->ID . '</a>', |
| 108 |
'<a href="' . get_edit_post_link( $post->post_parent ) . '" target="_blank">' . $post->post_parent . '</a>', |
| 109 |
'<a href="' . get_edit_post_link( $forum_id ) . '" target="_blank">' . $forum_id . '</a>', |
| 110 |
'<a href="' . get_edit_user_link( $post->post_author ) . '" target="_blank">' . $post->post_author . '</a>' |
| 111 |
) . "<br>\n"; |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
$recounted_posts = $limit * ( $loop + 1 ); |
| 120 |
|
| 121 |
// Check remaining posts |
| 122 |
if( $recounted_posts <= $posts_count ) { |
| 123 |
$response['run_again'] = true; |
| 124 |
$response['message'] = sprintf( __( '%d remaining items to finish recount', 'gamipress' ), ( $posts_count - $recounted_posts ) ); |
| 125 |
} |
| 126 |
|
| 127 |
return $response; |
| 128 |
|
| 129 |
} |
| 130 |
add_filter( 'gamipress_activity_recount_bbp_activities', 'gamipress_bbp_activity_recount_activities', 10, 4 ); |
| 131 |
|
| 132 |
|
| 133 |
/** |
| 134 |
* Recount favorites |
| 135 |
* |
| 136 |
* @since 1.0.2 |
| 137 |
* |
| 138 |
* @param array $response |
| 139 |
* |
| 140 |
* @return array $response |
| 141 |
*/ |
| 142 |
function gamipress_bbp_activity_recount_favorites( $response, $loop, $limit, $offset ) { |
| 143 |
|
| 144 |
global $wpdb; |
| 145 |
|
| 146 |
// Get all stored users count |
| 147 |
$users_count = absint( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->users} AS u" ) ); |
| 148 |
|
| 149 |
// On first loop send an informational text |
| 150 |
if( $loop === 0 && $users_count > $limit ) { |
| 151 |
$response['run_again'] = true; |
| 152 |
$response['message'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count ); |
| 153 |
$response['log'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count ) . "<br>\n"; |
| 154 |
|
| 155 |
// Return early to inform |
| 156 |
return $response; |
| 157 |
} |
| 158 |
|
| 159 |
// Get all stored users |
| 160 |
$users = $wpdb->get_results( "SELECT u.ID FROM {$wpdb->users} AS u LIMIT {$offset}, {$limit}" ); |
| 161 |
|
| 162 |
foreach( $users as $user ) { |
| 163 |
|
| 164 |
// Get all favorites topics |
| 165 |
$favorites = bbp_get_user_favorites_topic_ids( $user->ID ); |
| 166 |
|
| 167 |
if( $favorites ) { |
| 168 |
|
| 169 |
foreach( $favorites as $topic_id ) { |
| 170 |
// Trigger favorite action for each favorite topic |
| 171 |
gamipress_bbp_favorite_topic( $user->ID, $topic_id ); |
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
$recounted_users = $limit * ( $loop + 1 ); |
| 179 |
|
| 180 |
// Check remaining posts |
| 181 |
if( $recounted_users <= $users_count ) { |
| 182 |
$response['run_again'] = true; |
| 183 |
$response['message'] = sprintf( __( '%d remaining users to finish recount', 'gamipress' ), ( $users_count - $recounted_users ) ); |
| 184 |
} |
| 185 |
|
| 186 |
return $response; |
| 187 |
|
| 188 |
} |
| 189 |
add_filter( 'gamipress_activity_recount_bbp_favorites', 'gamipress_bbp_activity_recount_favorites', 10, 4 ); |