class-comment-meta.php
5 months ago
class-comment.php
5 months ago
class-meta.php
5 months ago
class-options.php
5 months ago
class-post-meta.php
5 months ago
class-post.php
5 months ago
class-terms.php
4 months ago
class-user-meta.php
5 months ago
class-user.php
5 months ago
class-user.php
177 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex\Source\Core; |
| 4 | |
| 5 | use SearchRegex\Source; |
| 6 | use SearchRegex\Search; |
| 7 | use SearchRegex\Sql; |
| 8 | use SearchRegex\Plugin; |
| 9 | |
| 10 | /** |
| 11 | * User source |
| 12 | */ |
| 13 | class User extends Source\Source { |
| 14 | use Source\Has_Meta; |
| 15 | |
| 16 | public function get_table_id() { |
| 17 | return 'ID'; |
| 18 | } |
| 19 | |
| 20 | public function get_table_name() { |
| 21 | global $wpdb; |
| 22 | |
| 23 | return $wpdb->users; |
| 24 | } |
| 25 | |
| 26 | public function get_actions( Search\Result $result ) { |
| 27 | return [ |
| 28 | 'edit' => get_edit_profile_url( $result->get_row_id(), 'admin' ), |
| 29 | ]; |
| 30 | } |
| 31 | |
| 32 | public function get_title_column() { |
| 33 | return 'user_nicename'; |
| 34 | } |
| 35 | |
| 36 | public function get_row_columns( $row_id ) { |
| 37 | $meta = $this->get_meta( get_user_meta( $row_id ) ); |
| 38 | $parent = parent::get_row_columns( $row_id ); |
| 39 | |
| 40 | if ( $parent instanceof \WP_Error ) { |
| 41 | return $parent; |
| 42 | } |
| 43 | |
| 44 | return array_merge( $parent, [ $meta ] ); |
| 45 | } |
| 46 | |
| 47 | public function save( $row_id, array $changes ) { |
| 48 | $user = $this->get_columns_to_change( $changes ); |
| 49 | $user['ID'] = $row_id; |
| 50 | |
| 51 | $this->process_meta( $row_id, 'user', $changes ); |
| 52 | |
| 53 | if ( count( $user ) > 1 ) { |
| 54 | $this->log_save( 'user', $user ); |
| 55 | |
| 56 | $result = true; |
| 57 | |
| 58 | if ( Plugin\Settings::init()->can_save() ) { |
| 59 | $result = wp_update_user( $user ); |
| 60 | } |
| 61 | |
| 62 | if ( $result ) { |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | return new \WP_Error( 'searchregex', 'Failed to update user.' ); |
| 67 | } |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | public function delete_row( $row_id ) { |
| 73 | $this->log_save( 'delete comment', $row_id ); |
| 74 | |
| 75 | if ( Plugin\Settings::init()->can_save() ) { |
| 76 | if ( wp_delete_user( $row_id ) ) { |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | return new \WP_Error( 'searchregex_delete', 'Failed to delete user', 401 ); |
| 81 | } |
| 82 | |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | public function autocomplete( array $column, $value ) { |
| 87 | global $wpdb; |
| 88 | |
| 89 | if ( $column['column'] === 'ID' ) { |
| 90 | return $wpdb->get_results( $wpdb->prepare( "SELECT ID as id,user_login as value FROM {$wpdb->users} WHERE user_login LIKE %s LIMIT %d", '%' . $wpdb->esc_like( $value ) . '%', self::AUTOCOMPLETE_LIMIT ) ); |
| 91 | } |
| 92 | |
| 93 | $user_fields = [ |
| 94 | 'user_login', |
| 95 | 'user_nicename', |
| 96 | 'display_name', |
| 97 | 'user_email', |
| 98 | ]; |
| 99 | |
| 100 | if ( in_array( $column['column'], $user_fields, true ) ) { |
| 101 | // phpcs:ignore |
| 102 | return $wpdb->get_results( $wpdb->prepare( "SELECT " . $column['column'] . " as id," . $column['column'] . " as value FROM {$wpdb->users} WHERE " . $column['column'] . " LIKE %s LIMIT %d", '%' . $wpdb->esc_like( $value ) . '%', self::AUTOCOMPLETE_LIMIT ) ); |
| 103 | } |
| 104 | |
| 105 | if ( $column['column'] === 'meta' ) { |
| 106 | return Source\Autocomplete::get_meta( Sql\Value::table( 'usermeta' ), $value ); |
| 107 | } |
| 108 | |
| 109 | return []; |
| 110 | } |
| 111 | |
| 112 | public function get_schema() { |
| 113 | global $wpdb; |
| 114 | |
| 115 | return [ |
| 116 | 'name' => __( 'Users', 'search-regex' ), |
| 117 | 'table' => $wpdb->users, |
| 118 | 'columns' => [ |
| 119 | [ |
| 120 | 'column' => 'ID', |
| 121 | 'type' => 'integer', |
| 122 | 'title' => __( 'ID', 'search-regex' ), |
| 123 | 'options' => 'api', |
| 124 | 'modify' => false, |
| 125 | ], |
| 126 | [ |
| 127 | 'column' => 'user_login', |
| 128 | 'type' => 'string', |
| 129 | 'title' => __( 'Login', 'search-regex' ), |
| 130 | 'options' => 'api', |
| 131 | 'modify' => false, |
| 132 | ], |
| 133 | [ |
| 134 | 'column' => 'user_nicename', |
| 135 | 'type' => 'string', |
| 136 | 'title' => __( 'Nicename', 'search-regex' ), |
| 137 | 'options' => 'api', |
| 138 | 'global' => true, |
| 139 | ], |
| 140 | [ |
| 141 | 'column' => 'display_name', |
| 142 | 'type' => 'string', |
| 143 | 'title' => __( 'Display Name', 'search-regex' ), |
| 144 | 'options' => 'api', |
| 145 | 'global' => true, |
| 146 | ], |
| 147 | [ |
| 148 | 'column' => 'user_email', |
| 149 | 'type' => 'string', |
| 150 | 'title' => __( 'Email', 'search-regex' ), |
| 151 | 'options' => 'api', |
| 152 | ], |
| 153 | [ |
| 154 | 'column' => 'user_url', |
| 155 | 'type' => 'string', |
| 156 | 'title' => __( 'URL', 'search-regex' ), |
| 157 | 'options' => 'api', |
| 158 | 'global' => true, |
| 159 | ], |
| 160 | [ |
| 161 | 'column' => 'user_registered', |
| 162 | 'type' => 'date', |
| 163 | 'title' => __( 'Registered', 'search-regex' ), |
| 164 | 'source' => 'date', |
| 165 | ], |
| 166 | [ |
| 167 | 'column' => 'meta', |
| 168 | 'type' => 'keyvalue', |
| 169 | 'title' => __( 'User meta', 'search-regex' ), |
| 170 | 'options' => 'api', |
| 171 | 'join' => 'usermeta', |
| 172 | ], |
| 173 | ], |
| 174 | ]; |
| 175 | } |
| 176 | } |
| 177 |