diff --git a/src/Access.php b/src/Access.php index f9ae737..8de0176 100644 --- a/src/Access.php +++ b/src/Access.php @@ -388,11 +388,11 @@ public function setup_nav_menu_item( $item ) { */ public function hide_menu_items() { ?> - + post_type == $mg_post_type ) { echo ''; + .misc-pub-section.misc-pub-visibility, + .misc-pub-section.curtime + { + display:none; + } + '; } ?> - - + 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 ), ]; } } diff --git a/src/Metaboxes.php b/src/Metaboxes.php index a77bf26..fb2e716 100644 --- a/src/Metaboxes.php +++ b/src/Metaboxes.php @@ -8,6 +8,8 @@ namespace Redbit\SimpleShop\WpPlugin; +use WP_User; + class Metaboxes { public $prefix = '_ssc_'; @@ -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' ] ); } /** @@ -25,7 +30,6 @@ public function __construct( Plugin $loader ) { * TODO: find a way to add custom post types */ public function page_metaboxes() { - $ssc_group = new Group(); $groups = $ssc_group->get_groups(); $ssc_access = $this->loader->get_access(); @@ -36,46 +40,60 @@ public function page_metaboxes() { /** * Initiate the metabox */ - $cmb = new_cmb2_box( [ - 'id' => 'ssc_page_groups', - 'title' => __( 'SimpleShop - Member sections', 'simpleshop-cz' ), - 'object_types' => $post_types, - 'context' => 'normal', - 'priority' => 'high', - 'show_names' => true, - ] ); - - $cmb->add_field( [ - 'name' => __( 'Member section with allowed access to page', 'simpleshop-cz' ), - 'desc' => __( 'Access to this page is permit only for users of selected Member Sections. If no one section selected, all users is permit to access this page.', - 'simpleshop-cz' ), - 'id' => $this->prefix . 'groups', - 'type' => 'multicheck', - 'options' => $groups, - ] ); + $cmb = new_cmb2_box( + [ + 'id' => 'ssc_page_groups', + 'title' => __( 'SimpleShop - Member sections', 'simpleshop-cz' ), + 'object_types' => $post_types, + 'context' => 'normal', + 'priority' => 'high', + 'show_names' => true, + ] + ); + + $cmb->add_field( + [ + 'name' => __( 'Member section with allowed access to page', 'simpleshop-cz' ), + 'desc' => __( + 'Access to this page is permit only for users of selected Member Sections. If no one section selected, all users is permit to access this page.', + 'simpleshop-cz' + ), + 'id' => $this->prefix . 'groups', + 'type' => 'multicheck', + 'options' => $groups, + ] + ); $tmp_post_types = $post_types; unset( $tmp_post_types['attachment'] ); - $cmb->add_field( [ - 'name' => __( 'Page ID to redirect', 'simpleshop-cz' ), - 'desc' => __( 'Select one Page to which user will be redirected when uses is logged in, but have no access to page. This is preffered way. Redirect will works even if target page URL will be changer in future.', - 'simpleshop-cz' ), - 'id' => $this->prefix . 'no_access_redirect_post_id', - 'type' => 'post_search_text', - 'select_type' => 'radio', - 'select_behavior' => 'replace', - 'post_type' => $tmp_post_types - ] ); - - - $cmb->add_field( [ - 'name' => __( 'Manual URL to redirect', 'simpleshop-cz' ), - 'desc' => __( 'Put URL to which user will be redirected when uses is logged in, but have no access to page. If you want to use this method, keep previous field empty.', - 'simpleshop-cz' ), - 'id' => $this->prefix . 'no_access_redirect', - 'type' => 'text' - ] ); + $cmb->add_field( + [ + 'name' => __( 'Page ID to redirect', 'simpleshop-cz' ), + 'desc' => __( + 'Select one Page to which user will be redirected when uses is logged in, but have no access to page. This is preffered way. Redirect will works even if target page URL will be changer in future.', + 'simpleshop-cz' + ), + 'id' => $this->prefix . 'no_access_redirect_post_id', + 'type' => 'post_search_text', + 'select_type' => 'radio', + 'select_behavior' => 'replace', + 'post_type' => $tmp_post_types, + ] + ); + + + $cmb->add_field( + [ + 'name' => __( 'Manual URL to redirect', 'simpleshop-cz' ), + 'desc' => __( + 'Put URL to which user will be redirected when uses is logged in, but have no access to page. If you want to use this method, keep previous field empty.', + 'simpleshop-cz' + ), + 'id' => $this->prefix . 'no_access_redirect', + 'type' => 'text', + ] + ); // $cmb->add_field(array( // 'name' => __('Přesměrovat na přihlášení', 'ssc'), @@ -85,99 +103,159 @@ public function page_metaboxes() { // )); - $cmb->add_field( [ - 'name' => __( 'Delay access to content (days from group assign)', 'simpleshop-cz' ), - 'desc' => __( 'Put number of days to delay before access to content is allowed after registration. For example: If registration to group is at January 1st and you set delay to 5 days, user get access to content from January 6th.', - 'simpleshop-cz' ), - 'id' => $this->prefix . 'days_to_access', - 'type' => 'text' - ] ); - - $cmb->add_field( [ - 'name' => __( 'Allow access from date', 'simpleshop-cz' ), - 'desc' => __( 'Put date which will be access to content allowed (applied to all groups)', - 'simpleshop-cz' ), - 'id' => $this->prefix . 'date_to_access', - 'type' => 'text_date', - 'date_format' => 'Y-m-d', - ] ); - - $cmb->add_field( [ - 'name' => __( 'Allow access to date', 'simpleshop-cz' ), - 'desc' => __( 'Put date until will be access to content allowed (applied to all groups)', - 'simpleshop-cz' ), - 'id' => $this->prefix . 'date_until_to_access', - 'type' => 'text_date', - 'date_format' => 'Y-m-d', - ] ); - - $cmb->add_field( [ - 'name' => __( 'Subject of email when content access allowed', 'simpleshop-cz' ), - 'desc' => __( 'Enter the subject of the email that will automatically be sent to the user when gains access to this content based on the days set above.', - 'simpleshop-cz' ), - 'id' => $this->prefix . 'email_subject_user_can_access', - 'type' => 'text' - ] ); - - $cmb->add_field( [ - 'name' => __( 'Email message when content access allowed', 'simpleshop-cz' ), - 'desc' => __( 'Enter an email that will automatically be sent to the user when gains access to this content based on the days set above.', - 'simpleshop-cz' ), - 'id' => $this->prefix . 'email_user_can_access', - 'type' => 'wysiwyg' - ] ); + $cmb->add_field( + [ + 'name' => __( 'Delay access to content (days from group assign)', 'simpleshop-cz' ), + 'desc' => __( + 'Put number of days to delay before access to content is allowed after registration. For example: If registration to group is at January 1st and you set delay to 5 days, user get access to content from January 6th.', + 'simpleshop-cz' + ), + 'id' => $this->prefix . 'days_to_access', + 'type' => 'text', + ] + ); + + $cmb->add_field( + [ + 'name' => __( 'Allow access from date', 'simpleshop-cz' ), + 'desc' => __( + 'Put date which will be access to content allowed (applied to all groups)', + 'simpleshop-cz' + ), + 'id' => $this->prefix . 'date_to_access', + 'type' => 'text_date', + 'date_format' => 'Y-m-d', + ] + ); + + $cmb->add_field( + [ + 'name' => __( 'Allow access to date', 'simpleshop-cz' ), + 'desc' => __( + 'Put date until will be access to content allowed (applied to all groups)', + 'simpleshop-cz' + ), + 'id' => $this->prefix . 'date_until_to_access', + 'type' => 'text_date', + 'date_format' => 'Y-m-d', + ] + ); + + $cmb->add_field( + [ + 'name' => __( 'Subject of email when content access allowed', 'simpleshop-cz' ), + 'desc' => __( + 'Enter the subject of the email that will automatically be sent to the user when gains access to this content based on the days set above.', + 'simpleshop-cz' + ), + 'id' => $this->prefix . 'email_subject_user_can_access', + 'type' => 'text', + ] + ); + + $cmb->add_field( + [ + 'name' => __( 'Email message when content access allowed', 'simpleshop-cz' ), + 'desc' => __( + 'Enter an email that will automatically be sent to the user when gains access to this content based on the days set above.', + 'simpleshop-cz' + ), + 'id' => $this->prefix . 'email_user_can_access', + 'type' => 'wysiwyg', + ] + ); } } /** - * 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(); ?> + + + + + + +
+ + + + + + + + + + + + + $group_name ) { ?> + + + + + + + + +
+ user_is_admin() ) { ?> + groups ), true ); ?>/> + groups ) ? __( 'Yes', 'simpleshop-cz' ) : __( 'No', 'simpleshop-cz' ); + } ?> + + user_is_admin() ) { ?> + + ID, $this->prefix . 'group_subscription_date_' . $group_id, true ); + } ?> + + user_is_admin() ) { ?> + + ID, $this->prefix . 'group_subscription_valid_to_' . $group_id, true ); + } ?> +
+
+ loader->get_access(); + if ( ! $access->user_is_admin() ) { + return; + } - if ( $access->user_is_admin() ) { - $cmb->add_field( [ - 'name' => __( 'SimpleShop - member sections
Choose which member sections the user should have access to.', - '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' => '' . sprintf( __( 'Registration date to group %s.', - 'simpleshop-cz' ), $group ) . '', - 'id' => $this->prefix . 'group_subscription_date_' . $key, - 'type' => 'text_date', - 'date_format' => 'Y-m-d', - ] ); - $cmb->add_field( [ - 'name' => '' . sprintf( __( 'Expiration date of registration to group %s.', - 'simpleshop-cz' ), $group ) . '', - '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'] ) ) ); } } } diff --git a/src/Settings.php b/src/Settings.php index 3012ccc..36af331 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -82,8 +82,8 @@ public function field_type_disconnect_button( $field_type_object ) { echo '' - . _e( 'Disconnect Simple Shop', 'simpleshop-cz' ) - . ''; + . _e( 'Disconnect Simple Shop', 'simpleshop-cz' ) + . ''; } /** @@ -128,10 +128,10 @@ public function add_options_page() { */ public function admin_page_display() { ?> -
-

+
+

metabox_id, $this->key ); ?> -
+
__( 'Email message', 'simpleshop-cz' ), 'desc' => __( 'Povolené zástupné znaky:
' - . '
{login} = login
' - . '{password} = heslo
' - . '{login_url} = adresa, na které je možné se přihlásit
' - . '{pages} = seznam stránek, do kterých má uživatel zakoupený přístup
' - . '{mail} = e-mail uživatele (většinou stejný jako login)
' - . '
' - . '', 'simpleshop-cz' ), + . '
{login} = login
' + . '{password} = heslo
' + . '{login_url} = adresa, na které je možné se přihlásit
' + . '{pages} = seznam stránek, do kterých má uživatel zakoupený přístup
' + . '{mail} = e-mail uživatele (většinou stejný jako login)
' + . '
' + . '', 'simpleshop-cz' ), 'id' => 'ssc_email_text', 'type' => 'wysiwyg', 'classes_cb' => [ $this, 'hide_when_invalid_keys' ],