CustomerRepository.php
1 year ago
ProviderRepository.php
1 year ago
UserRepository.php
1 year ago
WPUserRepository.php
4 years ago
WPUserRepository.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\User; |
| 4 | |
| 5 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 6 | use AmeliaBooking\Infrastructure\Connection; |
| 7 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\User\UsersTable; |
| 8 | |
| 9 | /** |
| 10 | * Class WPUserRepository |
| 11 | * |
| 12 | * @package AmeliaBooking\Infrastructure\Repository\User |
| 13 | */ |
| 14 | class WPUserRepository |
| 15 | { |
| 16 | /** @var \PDO */ |
| 17 | protected $connection; |
| 18 | |
| 19 | /** @var string */ |
| 20 | protected $table; |
| 21 | |
| 22 | /** @var string */ |
| 23 | protected $metaTable; |
| 24 | |
| 25 | /** @var string */ |
| 26 | protected $prefix; |
| 27 | |
| 28 | /** |
| 29 | * WPUserRepository constructor. |
| 30 | * |
| 31 | * @param Connection $connection |
| 32 | * @param $table |
| 33 | * @param $metaTable |
| 34 | * @param $prefix |
| 35 | */ |
| 36 | public function __construct(Connection $connection, $table, $metaTable, $prefix) |
| 37 | { |
| 38 | $this->connection = $connection(); |
| 39 | $this->table = $table; |
| 40 | $this->metaTable = $metaTable; |
| 41 | $this->prefix = $prefix; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param array $params |
| 46 | * @param array $excludeUserIds |
| 47 | * |
| 48 | * @return array |
| 49 | * @throws QueryExecutionException |
| 50 | */ |
| 51 | public function getAllNonRelatedWPUsers($params, $excludeUserIds) |
| 52 | { |
| 53 | $excludeIdParams = []; |
| 54 | |
| 55 | try { |
| 56 | $params = [ |
| 57 | ':id' => $params['id'], |
| 58 | ':role' => '%wpamelia-' . $params['role'] . '%', |
| 59 | ]; |
| 60 | |
| 61 | foreach ((array)$excludeUserIds as $key => $id) { |
| 62 | $excludeIdParams[":id$key"] = $id; |
| 63 | } |
| 64 | |
| 65 | $whereExcludeIds = $excludeIdParams ? |
| 66 | ' AND wp_user.ID NOT IN (' . implode(',', $excludeIdParams) . ')' : ''; |
| 67 | |
| 68 | $usersTable = UsersTable::getTableName(); |
| 69 | |
| 70 | global $wpdb; |
| 71 | |
| 72 | $statement = $this->connection->prepare( |
| 73 | "SELECT wp_user.ID AS value, wp_user.display_name AS label |
| 74 | FROM {$this->table} AS wp_user |
| 75 | INNER JOIN {$this->metaTable} AS wp_usermeta |
| 76 | ON wp_user.ID = wp_usermeta.user_id |
| 77 | WHERE |
| 78 | wp_user.ID NOT IN ( |
| 79 | SELECT externalId |
| 80 | FROM $usersTable |
| 81 | WHERE externalId IS NOT NULL |
| 82 | AND externalId != :id |
| 83 | ) |
| 84 | {$whereExcludeIds} |
| 85 | AND wp_usermeta.meta_key = '{$wpdb->prefix}capabilities' |
| 86 | AND wp_usermeta.meta_value LIKE :role |
| 87 | GROUP BY wp_user.ID" |
| 88 | ); |
| 89 | |
| 90 | $statement->execute($params); |
| 91 | |
| 92 | $rows = $statement->fetchAll(); |
| 93 | } catch (\Exception $e) { |
| 94 | throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e); |
| 95 | } |
| 96 | |
| 97 | $items = []; |
| 98 | foreach ($rows as $row) { |
| 99 | $row['value'] = (int)$row['value']; |
| 100 | $items[] = $row; |
| 101 | } |
| 102 | |
| 103 | return $items; |
| 104 | } |
| 105 | } |
| 106 |