|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WP_CLI\Fetchers; |
| 4 | + |
| 5 | +/** |
| 6 | + * Fetch a signup based on one of its attributes. |
| 7 | + */ |
| 8 | +class Signup extends Base { |
| 9 | + |
| 10 | + /** |
| 11 | + * The message to display when an item is not found. |
| 12 | + * |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + protected $msg = "Invalid signup ID, email, login, or activation key: '%s'"; |
| 16 | + |
| 17 | + /** |
| 18 | + * Get a signup. |
| 19 | + * |
| 20 | + * @param int|string $signup |
| 21 | + * @return stdClass|false |
| 22 | + */ |
| 23 | + public function get( $signup ) { |
| 24 | + return $this->get_signup( $signup ); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Get a signup by one of its identifying attributes. |
| 29 | + * |
| 30 | + * @param string $arg The raw CLI argument. |
| 31 | + * @return stdClass|false The item if found; false otherwise. |
| 32 | + */ |
| 33 | + protected function get_signup( $arg ) { |
| 34 | + global $wpdb; |
| 35 | + |
| 36 | + $signup_object = null; |
| 37 | + |
| 38 | + // Fetch signup with signup_id. |
| 39 | + if ( is_numeric( $arg ) ) { |
| 40 | + $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE signup_id = %d", $arg ) ); |
| 41 | + |
| 42 | + if ( $result ) { |
| 43 | + $signup_object = $result; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if ( ! $signup_object ) { |
| 48 | + // Try to fetch with other keys. |
| 49 | + foreach ( array( 'user_login', 'user_email', 'activation_key' ) as $field ) { |
| 50 | + // phpcs:ignore WordPress.DB.PreparedSQL |
| 51 | + $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE $field = %s", $arg ) ); |
| 52 | + |
| 53 | + if ( $result ) { |
| 54 | + $signup_object = $result; |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if ( $signup_object ) { |
| 61 | + return $signup_object; |
| 62 | + } |
| 63 | + |
| 64 | + return false; |
| 65 | + } |
| 66 | +} |
0 commit comments