Skip to content

Commit

Permalink
[RUA-24] Move members to wp_comments (#43)
Browse files Browse the repository at this point in the history
* [RUA-24] update models to new storage

* [RUA-24] new api functions

* [RUA-24] migration

* [RUA-24] exclude from normal comment queries

* [RUA-24] update member list

* [RUA-24] db update

* [RUA-24] update user count hook
  • Loading branch information
intoxstudio authored Dec 9, 2023
1 parent 7f67158 commit e15fb4f
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 104 deletions.
58 changes: 52 additions & 6 deletions api/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ function rua_get_user($user = null)
/**
* @since 2.1
* @param WP_Post|int $post
*
* @return RUA_Level_Interface
* @throws Exception
*/
Expand All @@ -48,22 +47,69 @@ function rua_get_level($post)
* @since 2.1
* @param RUA_Level_Interface|WP_Post|int $level
* @param RUA_User_Interface|WP_User|int|null $user null or omit for current user
*
* @return RUA_User_Level_Interface
* @throws Exception
*/
function rua_get_user_level($level, $user = null)
{
_deprecated_function(__FUNCTION__, '2.5', 'rua_get_user_levels()');

if (!($level instanceof RUA_Level_Interface)) {
$level = rua_get_level($level);
}
if (!($user instanceof RUA_User_Interface)) {
$user = rua_get_user($user);
}
return $user->level_memberships()->get($level->get_id());
}

/**
* @param RUA_User_Interface|WP_User|int|null $user null or omit for current user
* @return RUA_Collection
*/
function rua_get_user_levels($user)
{
if (!($user instanceof RUA_User_Interface)) {
$user = rua_get_user($user);
}

$user_level = new RUA_User_Level($user, $level);
$user_level->refresh();
return $user_level;
$entities = get_comments([
'type' => 'rua_member',
'user_id' => $user->get_id()
]);

$user_levels = new RUA_Collection();
foreach ($entities as $entity) {
$user_level = new RUA_User_Level($entity);
$user_level->refresh();
$user_levels->put($user_level->get_level_id(), $user_level);
}
return $user_levels;
}

/**
* @param RUA_Level_Interface|WP_Post|int $level
* @param array $query
* @return RUA_Collection
* @throws Exception
*/
function rua_get_level_members($level, $query = [])
{
if (!($level instanceof RUA_Level_Interface)) {
$level = rua_get_level($level);
}

$query['type'] = 'rua_member';
$query['status'] = [RUA_User_Level::STATUS_ACTIVE, RUA_User_Level::STATUS_EXPIRED];
$query['post_id'] = $level->get_id();
$entities = get_comments($query);

$user_levels = new RUA_Collection();
foreach ($entities as $entity) {
$user_level = new RUA_User_Level($entity);
$user_levels->put($user_level->get_user_id(), $user_level);
}
return $user_levels;
}

/**
Expand All @@ -84,7 +130,7 @@ function rua_get_level_by_name($name)
*/
function rua_get_level_caps($level_id, $hierarchical = false)
{
$levels = [ $level_id ];
$levels = [$level_id];
if ($hierarchical) {
$levels = array_merge($levels, get_post_ancestors((int) $level_id));
$levels = array_reverse($levels);
Expand Down
71 changes: 71 additions & 0 deletions db_updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,77 @@
$rua_db_updater->register_version_update('2.2.1', 'rua_update_to_221');
$rua_db_updater->register_version_update('2.4', 'rua_update_to_24');
$rua_db_updater->register_version_update('2.4.2', 'rua_update_to_242');
$rua_db_updater->register_version_update('2.5', 'rua_update_to_25');

function rua_update_to_25()
{
global $wpdb;

$level_id_lookup = array_flip((array)$wpdb->get_col("
SELECT ID FROM $wpdb->posts WHERE post_type = 'restriction'
"));
$existing_user_level_lookup = array_flip((array)$wpdb->get_col("
SELECT CONCAT(user_id, ':', comment_post_ID) FROM $wpdb->comments WHERE comment_type = 'rua_member'
"));

$blog_prefix = $wpdb->get_blog_prefix();
$query = <<<QUERY
SELECT um.user_id, um.meta_key, um.meta_value
FROM $wpdb->usermeta um
INNER JOIN $wpdb->usermeta u ON um.user_id = u.user_id AND u.meta_key = '{$blog_prefix}capabilities'
WHERE um.meta_key LIKE '_ca_level%'
QUERY;

$usermeta = $wpdb->get_results($query);

$data_new = [];
$data_meta_map = [];

foreach ($usermeta as $meta) {
if ($meta->meta_key === '_ca_level') {
if (!isset($level_id_lookup[$meta->meta_value])) {
continue;
}
//prevent dupe
if (isset($existing_user_level_lookup[$meta->user_id . ':' . $meta->meta_value])) {
continue;
}

$data_new[] = [
'comment_approved' => 'active',
'comment_date' => '',
'comment_type' => 'rua_member',
'user_id' => $meta->user_id,
'comment_post_ID' => $meta->meta_value,
'comment_meta' => [],
];
} else {
$data_meta_map[$meta->user_id . ':' . $meta->meta_key] = $meta->meta_value;
}
}

foreach ($data_new as $data) {
$user_id = $data['user_id'];
$level_id = $data['comment_post_ID'];
$insert = $data;

//start date
if (isset($data_meta_map[$user_id . ':_ca_level_' . $level_id])) {
$insert['comment_date'] = date_i18n('Y-m-d H:i:s', $data_meta_map[$user_id . ':_ca_level_' . $level_id]);
}
//status
if (isset($data_meta_map[$user_id . ':_ca_level_status_' . $level_id])) {
$insert['comment_approved'] = $data_meta_map[$user_id . ':_ca_level_status_' . $level_id];
}
//expiry date
if (isset($data_meta_map[$user_id . ':_ca_level_expiry_' . $level_id])) {
$insert['comment_meta']['_ca_member_expiry'] = $data_meta_map[$user_id . ':_ca_level_expiry_' . $level_id];
}

wp_insert_comment($insert);
wp_update_comment_count($level_id);
}
}

/**
* Enable legacy date module and
Expand Down
34 changes: 34 additions & 0 deletions level.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ protected function add_actions()
'user_register',
[$this,'registered_add_level']
);
add_action(
'parse_comment_query',
[$this, 'exclude_comment_type']
);
}

/**
Expand All @@ -72,10 +76,40 @@ protected function add_filters()
add_action('auth_redirect', [$this, 'authorize_admin_access']);
}

add_filter(
'pre_wp_update_comment_count_now',
[$this, 'update_member_count'],
10,
3
);

add_filter('get_edit_post_link', [$this,'get_edit_post_link'], 10, 3);
add_filter('get_delete_post_link', [$this,'get_delete_post_link'], 10, 3);
}

public function update_member_count($new, $old, $post_id)
{
$post = get_post($post_id);
if ($post->post_type !== RUA_App::TYPE_RESTRICT) {
return $new;
}

global $wpdb;
return (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id));
}

public function exclude_comment_type($query)
{
$type = 'rua_member';
if (in_array($type, (array) $query->query_vars['type']) ||
in_array($type, (array) $query->query_vars['type__in'])) {
return;
}

$query->query_vars['type__not_in'] = (array) $query->query_vars['type__not_in'];
$query->query_vars['type__not_in'][] = $type;
}

/**
* @param int $user_id
* @return void
Expand Down
41 changes: 31 additions & 10 deletions list-members.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,24 +305,45 @@ public function prepare_items()

$per_page = $this->get_items_per_page('members_per_page', 20);
$current_page = $this->get_pagenum();
$user_query = new WP_User_Query([
'meta_key' => RUA_App::META_PREFIX . 'level',
'meta_value' => $this->level_id,
'number' => $per_page,
'offset' => ($current_page - 1) * $per_page

$user_ids = '';
if (!empty($_GET['s'])) {
$query_params['fields'] = 'ID';
$query_params['count_total'] = false;
$query_params['search'] = '*' . $_GET['s'] . '*';
$query_params['orderby'] = 'ID';
if (false !== strpos($_GET['s'], '@')) {
$query_params['search_columns'] = ['user_email'];
} elseif (is_numeric($_GET['s'])) {
$query_params['search_columns'] = ['user_login', 'ID'];
} else {
$query_params['search_columns'] = ['user_nicename', 'user_login', 'display_name'];
}
$user_query = new WP_User_Query($query_params);
$user_ids = $user_query->get_results();
}

$total_items = get_comments([
'number' => 0,
'offset' => 0,
'user_id' => $user_ids,
'count' => true,
'post_id' => $this->level_id,
'type' => 'rua_member',
'status' => [RUA_User_Level::STATUS_ACTIVE, RUA_User_Level::STATUS_EXPIRED]
]);
$total_items = (int)$user_query->get_total();

$this->set_pagination_args([
'total_items' => $total_items,
'total_pages' => ceil($total_items / $per_page),
'per_page' => $per_page
]);

$this->items = [];
foreach ($user_query->get_results() as $user) {
$this->items[] = rua_get_user_level($this->level_id, $user);
}
$this->items = rua_get_level_members($this->level_id, [
'number' => $per_page,
'offset' => ($current_page - 1) * $per_page,
'user_id' => $user_ids
])->all();
}

/**
Expand Down
50 changes: 20 additions & 30 deletions models/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class RUA_User implements RUA_User_Interface
private $wp_user;

/**
* @var RUA_Collection<RUA_User_Level>|RUA_User_Level[]|null
* @var RUA_Collection<RUA_User_Level_Interface>|RUA_User_Level_Interface[]|null
*/
private $level_memberships;

Expand Down Expand Up @@ -65,22 +65,7 @@ public function has_global_access()
public function level_memberships()
{
if (is_null($this->level_memberships)) {
$user_id = $this->get_id();
$level_ids = [];

if ($user_id) {
$level_ids = (array)get_user_meta($user_id, RUA_App::META_PREFIX . 'level', false);
}

$this->level_memberships = new RUA_Collection();
$level_ids = array_unique($level_ids);
foreach ($level_ids as $level_id) {
$level_id = (int)$level_id;
try {
$this->level_memberships->put($level_id, rua_get_user_level($level_id, $this));
} catch (Exception $e) {
}
}
$this->level_memberships = rua_get_user_levels($this);
}
return $this->level_memberships;
}
Expand Down Expand Up @@ -122,20 +107,26 @@ public function get_level_ids(
*/
public function add_level($level_id)
{
if($this->level_memberships()->has($level_id)) {
if ($this->level_memberships()->has($level_id)) {
/** @var RUA_User_Level_Interface $user_level */
$user_level = $this->level_memberships()->get($level_id);
$user_level->update_status(RUA_User_Level::STATUS_ACTIVE);
$event = 'extended';
} else {
add_user_meta($this->get_id(), RUA_App::META_PREFIX . 'level', $level_id, false);
$user_level = rua_get_user_level($level_id, $this);
$user_level->update_start(time());
$user_level = new RUA_User_Level(get_comment(wp_insert_comment([
'comment_approved' => RUA_User_Level::STATUS_ACTIVE,
'comment_type' => 'rua_member',
'user_id' => $this->get_id(),
'comment_post_ID' => $level_id,
'comment_meta' => [],
])));
$this->level_memberships()->put($level_id, $user_level);
$event = 'added';
}

$user_level->update_status(RUA_User_Level::STATUS_ACTIVE);
$user_level->reset_expiry();
$this->reset_caps_cache();
do_action('rua/user_level/added', $this, $level_id);
do_action('rua/user_level/' . $event, $this, $level_id);

return true;
}
Expand All @@ -145,15 +136,14 @@ public function add_level($level_id)
*/
public function remove_level($level_id)
{
$user_id = $this->get_id();
$this->reset_caps_cache();
$deleted = delete_user_meta($user_id, RUA_App::META_PREFIX . 'level', $level_id);

delete_user_meta($user_id, RUA_App::META_PREFIX . 'level_' . $level_id);
delete_user_meta($user_id, RUA_App::META_PREFIX . 'level_status_' . $level_id);
delete_user_meta($user_id, RUA_App::META_PREFIX . 'level_expiry_' . $level_id);
$level = $this->level_memberships()->get($level_id);
if (!($level instanceof RUA_User_Level_Interface)) {
return false;
}

$deleted = $level->delete();
if ($deleted) {
$this->reset_caps_cache();
$this->level_memberships()->remove($level_id);
do_action('rua/user_level/removed', $this, $level_id);
}
Expand Down
Loading

0 comments on commit e15fb4f

Please sign in to comment.