Skip to content

Commit

Permalink
Save the edited values
Browse files Browse the repository at this point in the history
  • Loading branch information
vaclavgreif authored and jakubboucek committed Jan 18, 2021
1 parent 2f7ae2b commit e999042
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public function __construct( $user_id = '' ) {
*/
public function get() {
$ssc_groups = new Group();
$groups = $ssc_groups->get_user_groups( $this->user_id );
$groups = $ssc_groups->get_user_groups( $this->user_id ) ?: [];

foreach ( $groups as $group ) {
$this->groups[ $group ] = [
'group_id' => $group,
'subscription_date' => $this->get_subscription_date( $group ),
'valid_to' => $this->get_valid_to( $group )
'valid_to' => $this->get_valid_to( $group ),
];
}
}
Expand Down
150 changes: 88 additions & 62 deletions src/Metaboxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Redbit\SimpleShop\WpPlugin;

use WP_User;

class Metaboxes {
public $prefix = '_ssc_';

Expand All @@ -17,7 +19,10 @@ class Metaboxes {
public function __construct( Plugin $loader ) {
$this->loader = $loader;
add_action( 'cmb2_admin_init', [ $this, 'page_metaboxes' ] );
add_action( 'cmb2_admin_init', [ $this, 'user_metaboxes' ] );
add_action( 'show_user_profile', [ $this, 'render_user_profile_groups' ] );
add_action( 'edit_user_profile', [ $this, 'render_user_profile_groups' ] );
add_action( 'personal_options_update', [ $this, 'save_user_profile_groups' ] );
add_action( 'edit_user_profile_update', [ $this, 'save_user_profile_groups' ] );
}

/**
Expand Down Expand Up @@ -163,73 +168,94 @@ public function page_metaboxes() {
}

/**
* Add metabox to user profile
* Add groups table to user profile
*
* @param WP_User $user WP User
*/
public function user_metaboxes() {
/**
* Initiate the metabox
*/
$cmb = new_cmb2_box(
[
'id' => 'ssc_user_groups',
'title' => __( 'SimpleShop', 'simpleshop-cz' ),
'object_types' => [ 'user' ],
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
]
);

$ssc_group = new Group();
$groups = $ssc_group->get_groups();
public function render_user_profile_groups( $user ) {
$ssc_group = new Group();
$groups = $ssc_group->get_groups();
$membership = new Membership( $user->ID );
$access = $this->loader->get_access(); ?>
<style type="text/css">
#simpleshop__groups th {
padding: 15px 10px;
}
</style>
<table id="custom_user_field_table" class="form-table">
<tr id="simpleshop__groups">
<th>
<label for="custom_field"><?php _e( 'Simpleshop Groups', 'simpleshop-cz' ); ?></label>
</th>
<td>
<table>
<thead>
<tr>
<th><?php _e( 'Group name', 'simpleshop-cz' ); ?></th>
<th><?php _e( 'Is member', 'simpleshop-cz' ); ?></th>
<th><?php _e( 'Membership from', 'simpleshop-cz' ); ?></th>
<th><?php _e( 'Membership to', 'simpleshop-cz' ); ?></th>

</tr>
</thead>
<tbody>
<?php foreach ( $groups as $group_id => $group_name ) { ?>
<tr>
<td><?php echo $group_name; ?></td>
<td>
<?php if ( $access->user_is_admin() ) { ?>
<input type="checkbox" name="ssc_groups[<?php echo $group_id ?>][is_member]" value="on" <?php checked( array_key_exists( $group_id, $membership->groups ), true ); ?>/>
<?php } else {
echo array_key_exists( $group_id, $membership->groups ) ? __( 'Yes', 'simpleshop-cz' ) : __( 'No', 'simpleshop-cz' );
} ?>
</td>
<td>
<?php if ( $access->user_is_admin() ) { ?>
<input type="text" name="ssc_groups[<?php echo $group_id ?>][subscription_date]" value="<?php echo get_user_meta( $user->ID, $this->prefix . 'group_subscription_date_' . $group_id, true ) ?>"/>
<?php } else {
echo get_user_meta( $user->ID, $this->prefix . 'group_subscription_date_' . $group_id, true );
} ?>
</td>
<td>
<?php if ( $access->user_is_admin() ) { ?>
<input type="text" name="ssc_groups[<?php echo $group_id ?>][subscription_valid_to]" value="<?php echo get_user_meta( $user->ID, $this->prefix . 'group_subscription_valid_to_' . $group_id, true ) ?>"/>
<?php } else {
echo get_user_meta( $user->ID, $this->prefix . 'group_subscription_valid_to_' . $group_id, true );
} ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</td>
</tr>
</table>
<?php
}

/**
* Save user groups to profile
*
* @param $user_id
*/
public function save_user_profile_groups( $user_id ) {
$access = $this->loader->get_access();
if ( ! $access->user_is_admin() ) {
return;
}

if ( $access->user_is_admin() ) {
$cmb->add_field(
[
'name' => __(
'SimpleShop - member sections<br/><small style=\"font-weight:normal;\">Choose which member sections the user should have access to.</small>',
'simpleshop-cz'
),
// 'desc' => __('Vyberte, do kterých členských sekcí má mít uživatel přístup','ssc'),
'id' => $this->prefix . 'user_groups',
'type' => 'multicheck',
'options' => $groups,
]
);

foreach ( $groups as $key => $group ) {
$cmb->add_field(
[
'name' => '<small style="font-weight:normal;">' . sprintf(
__(
'Registration date to group %s.',
'simpleshop-cz'
),
$group
) . '</small>',
'id' => $this->prefix . 'group_subscription_date_' . $key,
'type' => 'text_date',
'date_format' => 'Y-m-d',
]
);
$cmb->add_field(
[
'name' => '<small style="font-weight:normal;">' . sprintf(
__(
'Expiration date of registration to group %s.',
'simpleshop-cz'
),
$group
) . '</small>',
'id' => $this->prefix . 'group_subscription_valid_to_' . $key,
'type' => 'text_date',
'date_format' => 'Y-m-d',
]
);
$groups = [];
foreach ( $_POST['ssc_groups'] as $group_id => $group ) {
if ( ! empty( $group['is_member'] ) && $group['is_member'] ) {
$groups[] = $group_id;
if ( empty( $group['subscription_date'] ) ) {
$group['subscription_date'] = date( 'Y-m-d' );
}
}

update_user_meta( $user_id, $this->prefix . 'user_groups', $groups );
update_user_meta( $user_id, $this->prefix . 'group_subscription_date_' . $group_id, empty( $group['subscription_date'] ) ? '' : date( 'Y-m-d', strtotime( $group['subscription_date'] ) ) );
update_user_meta( $user_id, $this->prefix . 'group_subscription_valid_to_' . $group_id, empty( $group['subscription_valid_to'] ) ? '' : date( 'Y-m-d', strtotime( $group['subscription_valid_to'] ) ) );
}
}
}

0 comments on commit e999042

Please sign in to comment.