| 1 |
<?php |
| 2 |
|
| 3 |
namespace Buddypress\CLI\Command; |
| 4 |
|
| 5 |
use WP_CLI; |
| 6 |
use WP_CLI\CommandWithDBObject; |
| 7 |
|
| 8 |
/** |
| 9 |
* Base component class. |
| 10 |
* |
| 11 |
* @since 1.0 |
| 12 |
*/ |
| 13 |
abstract class BuddyPressCommand extends CommandWithDBObject { |
| 14 |
|
| 15 |
/** |
| 16 |
* Default dependency check for a BuddyPress CLI command. |
| 17 |
* |
| 18 |
* @since 2.0 |
| 19 |
*/ |
| 20 |
public static function check_dependencies() { |
| 21 |
if ( ! class_exists( 'Buddypress' ) ) { |
| 22 |
WP_CLI::error( 'The BuddyPress plugin is not active.' ); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Get Formatter object based on supplied parameters. |
| 28 |
* |
| 29 |
* @since 2.0 |
| 30 |
* |
| 31 |
* @param array $assoc_args Parameters passed to command. Determines formatting. |
| 32 |
* @return \WP_CLI\Formatter |
| 33 |
*/ |
| 34 |
protected function get_formatter( &$assoc_args ) { |
| 35 |
return new WP_CLI\Formatter( $assoc_args, $this->obj_fields ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Get a random user id. |
| 40 |
* |
| 41 |
* @global wpdb $wpdb WordPress database abstraction object. |
| 42 |
* |
| 43 |
* @since 1.1 |
| 44 |
* |
| 45 |
* @return int |
| 46 |
*/ |
| 47 |
protected function get_random_user_id() { |
| 48 |
global $wpdb; |
| 49 |
return (int) $wpdb->get_var( "SELECT ID FROM $wpdb->users ORDER BY RAND() LIMIT 1" ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Get an activity ID. |
| 54 |
* |
| 55 |
* @since 2.0 |
| 56 |
* |
| 57 |
* @param int $activity_id Activity ID. |
| 58 |
* @param bool $activity_object Return BP_Activity_Activity object. |
| 59 |
* @return int|BP_Activity_Activity |
| 60 |
*/ |
| 61 |
protected function get_activity_id_from_identifier( $activity_id, $activity_object = false ) { |
| 62 |
$fetcher = new Activity_Fetcher(); |
| 63 |
$activity = $fetcher->get_check( $activity_id ); |
| 64 |
|
| 65 |
if ( true === $activity_object ) { |
| 66 |
return $activity; |
| 67 |
} |
| 68 |
|
| 69 |
return $activity->id; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get a group ID from its identifier (ID or slug). |
| 74 |
* |
| 75 |
* @since 1.5.0 |
| 76 |
* |
| 77 |
* @param int|string $group_id Group ID or slug. |
| 78 |
* @return int|bool |
| 79 |
*/ |
| 80 |
protected function get_group_id_from_identifier( $group_id ) { |
| 81 |
// Group ID or slug. |
| 82 |
if ( ! is_numeric( $group_id ) ) { |
| 83 |
$group_id = groups_get_id( $group_id ); |
| 84 |
} |
| 85 |
|
| 86 |
// Get group object. |
| 87 |
$group_obj = groups_get_group( |
| 88 |
[ 'group_id' => $group_id ] |
| 89 |
); |
| 90 |
|
| 91 |
if ( empty( $group_obj->id ) ) { |
| 92 |
WP_CLI::error( 'No group found by that slug or ID.' ); |
| 93 |
} |
| 94 |
|
| 95 |
return intval( $group_obj->id ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Verify a user ID by the passed identifier. |
| 100 |
* |
| 101 |
* @since 1.2.0 |
| 102 |
* |
| 103 |
* @param mixed $identifier User ID, email, or login. |
| 104 |
* @return WP_User |
| 105 |
*/ |
| 106 |
protected function get_user_id_from_identifier( $identifier ) { |
| 107 |
if ( is_numeric( $identifier ) ) { |
| 108 |
$user = get_user_by( 'id', $identifier ); |
| 109 |
} elseif ( is_email( $identifier ) ) { |
| 110 |
$user = get_user_by( 'email', $identifier ); |
| 111 |
} else { |
| 112 |
$user = get_user_by( 'login', $identifier ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( ! $user ) { |
| 116 |
WP_CLI::error( sprintf( 'No user found by that username or ID (%s).', $identifier ) ); |
| 117 |
} |
| 118 |
|
| 119 |
return $user; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Generate random text |
| 124 |
* |
| 125 |
* @since 1.1 |
| 126 |
* |
| 127 |
* @return string |
| 128 |
*/ |
| 129 |
protected function generate_random_text() { |
| 130 |
return 'Here is some random text'; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get field from an ID. |
| 135 |
* |
| 136 |
* @since 1.5.0 |
| 137 |
* |
| 138 |
* @param int|string $field_id Field ID or Field name. |
| 139 |
* @return int Field ID. |
| 140 |
*/ |
| 141 |
protected function get_field_id( $field_id ) { |
| 142 |
if ( ! is_numeric( $field_id ) ) { |
| 143 |
return xprofile_get_field_id_from_name( $field_id ); |
| 144 |
} |
| 145 |
|
| 146 |
return absint( $field_id ); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* String sanitization. |
| 151 |
* |
| 152 |
* @since 1.5.0 |
| 153 |
* |
| 154 |
* @param string $type String to sanitize. |
| 155 |
* @return string Sanitized string. |
| 156 |
*/ |
| 157 |
protected function sanitize_string( $type ) { |
| 158 |
return strtolower( str_replace( '-', '_', $type ) ); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Pull up a random active component. |
| 163 |
* |
| 164 |
* @since 1.1 |
| 165 |
* |
| 166 |
* @return string |
| 167 |
*/ |
| 168 |
protected function get_random_component() { |
| 169 |
$c = buddypress()->active_components; |
| 170 |
$ca = $this->get_components_and_actions(); |
| 171 |
|
| 172 |
return array_rand( (array) array_flip( array_intersect( array_keys( $c ), array_keys( $ca ) ) ) ); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Get a list of activity components and actions. |
| 177 |
* |
| 178 |
* @since 1.1 |
| 179 |
* |
| 180 |
* @return array |
| 181 |
*/ |
| 182 |
protected function get_components_and_actions() { |
| 183 |
return array_map( |
| 184 |
function ( $component ) { |
| 185 |
return array_keys( (array) $component ); |
| 186 |
}, |
| 187 |
(array) bp_activity_get_actions() |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Generate callback. |
| 193 |
* |
| 194 |
* @param string $message Message to display. |
| 195 |
* @param array $assoc_args Command arguments. |
| 196 |
* @param callable $callback Callback to execute. |
| 197 |
*/ |
| 198 |
protected function generate_callback( $message, $assoc_args, $callback ) { |
| 199 |
$format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'progress' ); |
| 200 |
$limit = $assoc_args['count']; |
| 201 |
$notify = false; |
| 202 |
|
| 203 |
if ( 'progress' === $format ) { |
| 204 |
$notify = WP_CLI\Utils\make_progress_bar( $message, $limit ); |
| 205 |
} |
| 206 |
|
| 207 |
for ( $index = 0; $index < $limit; $index++ ) { |
| 208 |
$object_id = call_user_func( $callback, $assoc_args, $format ); |
| 209 |
|
| 210 |
if ( 'progress' === $format ) { |
| 211 |
$notify->tick(); |
| 212 |
} elseif ( 'ids' === $format ) { |
| 213 |
echo $object_id; |
| 214 |
if ( $index < $limit - 1 ) { |
| 215 |
echo ' '; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
if ( 'progress' === $format ) { |
| 221 |
$notify->finish(); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|