| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * Manage BuddyPress favoriting activity items. |
| | 5 | */ |
| | 6 | class BPCLI_Favorite extends BPCLI_Component { |
| | 7 | |
| | 8 | /** |
| | 9 | * Favorite an activity item. |
| | 10 | * |
| | 11 | * ## OPTIONS |
| | 12 | * |
| | 13 | * [--activity-id=<activity-id>] |
| | 14 | * : ID of the activity to favorite item. If none is provided, one will be |
| | 15 | * generated automatically. |
| | 16 | * |
| | 17 | * [--user-id=<user-id>] |
| | 18 | * : ID of the user favoriting the activity. If none is provided, |
| | 19 | * a user will be randomly selected. |
| | 20 | * |
| | 21 | * @synopsis [--activity-id=<activity-id>] [--user-id=<user-id>] |
| | 22 | * |
| | 23 | * @since 1.1 |
| | 24 | */ |
| | 25 | public function create( $args, $assoc_args ) { |
| | 26 | $defaults = array( |
| | 27 | 'activity-id' => '', |
| | 28 | 'user-id' => '', |
| | 29 | 'silent' => false, |
| | 30 | ); |
| | 31 | |
| | 32 | $r = wp_parse_args( $assoc_args, $defaults ); |
| | 33 | |
| | 34 | // Fill in any missing information |
| | 35 | if ( empty( $r['activity-id'] ) ) { |
| | 36 | $r['activity-id'] = $this->get_random_activity(); |
| | 37 | } |
| | 38 | |
| | 39 | if ( empty( $r['user-id'] ) ) { |
| | 40 | $r['user-id'] = $this->get_random_user_id(); |
| | 41 | } |
| | 42 | |
| | 43 | $favorited = bp_activity_add_user_favorite( $r['activity-id'], $r['user-id'] ); |
| | 44 | |
| | 45 | if ( $r['silent'] ) { |
| | 46 | return; |
| | 47 | } |
| | 48 | |
| | 49 | if ( $favorited ) { |
| | 50 | WP_CLI::success( sprintf( 'Successfully favorited new activity item (id #%d)', $r['activity-id'] ) ); |
| | 51 | } else { |
| | 52 | WP_CLI::error( 'Could not favorite activity item.' ); |
| | 53 | } |
| | 54 | } |
| | 55 | |
| | 56 | /** |
| | 57 | * Favorite random activities items. |
| | 58 | * |
| | 59 | * ## OPTIONS |
| | 60 | * |
| | 61 | * [--count=<number>] |
| | 62 | * : How many activity items to favorite. Default: 100 |
| | 63 | * |
| | 64 | * [--user-id=<user-id>] |
| | 65 | * : ID of the user favoriting the activity. If none is provided, |
| | 66 | * a user will be randomly selected. |
| | 67 | * |
| | 68 | * @synopsis [--count=<number>] |
| | 69 | */ |
| | 70 | public function generate( $args, $assoc_args ) { |
| | 71 | global $wpdb, $bp; |
| | 72 | |
| | 73 | $r = wp_parse_args( $assoc_args, array( |
| | 74 | 'count' => 100, |
| | 75 | 'user-id' => '', |
| | 76 | ) ); |
| | 77 | |
| | 78 | $user_id = ! empty( $r['$user-id'] ) ? $r['$user-id'] : ''; |
| | 79 | |
| | 80 | $activities = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} ORDER BY RAND() LIMIT " . $r['count'] ); |
| | 81 | |
| | 82 | $notify = \WP_CLI\Utils\make_progress_bar( 'Favoriting activity items', $r['count'] ); |
| | 83 | |
| | 84 | foreach ( $activities as $activity ) { |
| | 85 | $this->create( array(), array( |
| | 86 | 'activity-id' => $activity, |
| | 87 | 'user-id' => $user_id, |
| | 88 | 'silent' => true, |
| | 89 | ) ); |
| | 90 | |
| | 91 | $notify->tick(); |
| | 92 | } |
| | 93 | |
| | 94 | $notify->finish(); |
| | 95 | } |
| | 96 | |
| | 97 | /** |
| | 98 | * Pull up a random activity id. |
| | 99 | * |
| | 100 | * @since 1.1 |
| | 101 | * |
| | 102 | * @return int |
| | 103 | */ |
| | 104 | protected function get_random_activity() { |
| | 105 | global $wpdb, $bp; |
| | 106 | |
| | 107 | $type = $wpdb->get_col( "SELECT DISTINCT type FROM {$bp->activity->table_name}" ); |
| | 108 | $type = array_diff( $type, array( 'last_activity' ) ); |
| | 109 | $type = array_rand( array_flip( $type ) ); |
| | 110 | |
| | 111 | return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->activity->table_name} WHERE type = '%s' ORDER BY RAND() LIMIT 1", $type ) ); |
| | 112 | } |
| | 113 | |
| | 114 | /** |
| | 115 | * Pull up a random active component for use in activity items. |
| | 116 | * |
| | 117 | * @since 1.1 |
| | 118 | * |
| | 119 | * @return string |
| | 120 | */ |
| | 121 | protected function get_random_component() { |
| | 122 | $c = buddypress()->active_components; |
| | 123 | |
| | 124 | // Core components that accept activity items |
| | 125 | $ca = $this->get_components_and_actions(); |
| | 126 | |
| | 127 | return array_rand( array_flip( array_intersect( array_keys( $c ), array_keys( $ca ) ) ) ); |
| | 128 | } |
| | 129 | |
| | 130 | /** |
| | 131 | * Get a random type from a component. |
| | 132 | * |
| | 133 | * @since 1.1 |
| | 134 | * |
| | 135 | * @param string $component Component name. |
| | 136 | * @return string |
| | 137 | */ |
| | 138 | protected function get_random_type_from_component( $component ) { |
| | 139 | $ca = $this->get_components_and_actions(); |
| | 140 | return array_rand( array_flip( $ca[ $component ] ) ); |
| | 141 | } |
| | 142 | |
| | 143 | /** |
| | 144 | * Get a list of activity components and actions |
| | 145 | * |
| | 146 | * @since 1.1 |
| | 147 | * |
| | 148 | * @return array |
| | 149 | */ |
| | 150 | protected function get_components_and_actions() { |
| | 151 | return array( |
| | 152 | 'activity' => array( |
| | 153 | 'activity_update', |
| | 154 | 'activity_comment', |
| | 155 | ), |
| | 156 | 'blogs' => array( |
| | 157 | 'new_blog', |
| | 158 | 'new_blog_post', |
| | 159 | 'new_blog_comment', |
| | 160 | ), |
| | 161 | 'friends' => array( |
| | 162 | 'friendship_created', |
| | 163 | ), |
| | 164 | 'groups' => array( |
| | 165 | 'joined_group', |
| | 166 | 'created_group', |
| | 167 | ), |
| | 168 | 'profile' => array( |
| | 169 | 'new_avatar', |
| | 170 | 'new_member', |
| | 171 | 'updated_profile', |
| | 172 | ), |
| | 173 | ); |
| | 174 | } |
| | 175 | } |
| | 176 | |
| | 177 | WP_CLI::add_command( 'bp favorite', 'BPCLI_Favorite', array( |
| | 178 | 'before_invoke' => function() { |
| | 179 | if ( ! bp_is_active( 'activity' ) ) { |
| | 180 | WP_CLI::error( 'The Activity component is not active.' ); |
| | 181 | } |
| | 182 | } ) ); |
| | 183 | No newline at end of file |