Skip to content

Commit 2907298

Browse files
committed
Merge branch 'v2.8'
2 parents 1f5cabf + ddbcd4f commit 2907298

File tree

3 files changed

+65
-14
lines changed

3 files changed

+65
-14
lines changed

src/Access.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,6 @@ public function check_access() {
7676
return;
7777
}
7878

79-
$post_groups = $this->get_post_groups();
80-
81-
// If the post is protected and user is not logged in, redirect him to login
82-
if ( $post_groups && ! is_user_logged_in() ) {
83-
nocache_headers();
84-
wp_safe_redirect( wp_login_url( site_url( $_SERVER['REQUEST_URI'] ) ) );
85-
exit();
86-
}
87-
8879
// Check if current user has access to the post, if not, redirect him to defined URL or home if the URL is not set
8980
if ( ! $this->user_can_view_post() && ! is_home() && ! is_front_page() ) {
9081
$no_access_url = remove_query_arg( [ 'redirect_to' ], $this->get_no_access_redirect_url() );
@@ -174,16 +165,23 @@ public function user_can_view_post( $post_id = '', $user_id = '' ) {
174165
continue;
175166
}
176167
}
168+
$subscription_date = $membership->groups[ $post_group ]['subscription_date'];
177169

178170
if ( $days_to_access = $this->get_post_days_to_access() ) {
179-
$subscription_date = $membership->groups[ $post_group ]['subscription_date'];
180171
// Get the date of subscription to the group
181172
if ( $subscription_date > date( 'Y-m-d', strtotime( "now -$days_to_access days" ) ) ) {
182173
// if the user does not have access YET, just break the loop here, as the user might have multiple subscriptions
183174
continue;
184175
}
185176
}
186177

178+
if ( $expire_days_after_registration = $this->get_expire_days_after_registration() ) {
179+
if ( $subscription_date < date( 'Y-m-d', strtotime( "now - $expire_days_after_registration" ) ) ) {
180+
// if the access already expired, just break the loop here, as the user might have multiple subscriptions
181+
continue;
182+
}
183+
}
184+
187185
// Check if the user has a valid membership.
188186
if ( ! $membership->is_valid_for_group( $post_group ) ) {
189187
continue;
@@ -261,6 +259,24 @@ public function get_post_days_to_access( $post_id = '' ) {
261259
return get_post_meta( $post_id, SIMPLESHOP_PREFIX . 'days_to_access', true );
262260
}
263261

262+
/**
263+
* Get the number of days the user has to be subscribed to have access to the post
264+
*
265+
* @param string $post_id
266+
*
267+
* @return mixed
268+
*/
269+
public function get_expire_days_after_registration( $post_id = '' ) {
270+
global $post;
271+
272+
if ( ! $post_id ) {
273+
$post_id = $post->ID;
274+
}
275+
276+
return get_post_meta( $post_id, SIMPLESHOP_PREFIX . 'expire_days_after_registration', true );
277+
}
278+
279+
264280
/**
265281
* Get the URL to redirect the user if he has no access
266282
*

src/Metaboxes.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ public function page_metaboxes() {
118118
]
119119
);
120120

121+
$cmb->add_field(
122+
[
123+
'name' => __( 'Allow access only for specific number of days (days after registration to the member section)', 'simpleshop-cz' ),
124+
'desc' => __(
125+
'Allow access only for specific number of days after registration. For example: If the registration to the member section is on January 1st and you set 14 days, the user will be able to access the page until January 14th included.',
126+
'simpleshop-cz'
127+
),
128+
'id' => $this->prefix . 'expire_days_after_registration',
129+
'type' => 'text',
130+
]
131+
);
132+
121133
$cmb->add_field(
122134
[
123135
'name' => __( 'Allow access from this date', 'simpleshop-cz' ),

src/Settings.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public function register_hooks() {
7373
add_action( 'cmb2_admin_init', [ $this, 'add_options_page_metabox' ] );
7474
add_filter( 'cmb2_render_disconnect_button', [ $this, 'field_type_disconnect_button' ], 10, 5 );
7575
add_action( 'admin_init', [ $this, 'maybe_disconnect_simpleshop' ] );
76+
add_action( 'admin_print_styles', [ $this, 'maybe_display_messages' ] );
7677
}
7778

7879
public function field_type_disconnect_button(
@@ -131,10 +132,10 @@ public function add_options_page() {
131132
*/
132133
public function admin_page_display() {
133134
?>
134-
<div class="wrap cmb2-options-page <?php echo $this->key; ?>">
135-
<h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
135+
<div class="wrap cmb2-options-page <?php echo $this->key; ?>">
136+
<h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
136137
<?php cmb2_metabox_form( $this->metabox_id, $this->key ); ?>
137-
</div>
138+
</div>
138139
<?php
139140
}
140141

@@ -372,7 +373,7 @@ public function settings_notices( $object_id, $updated ) {
372373

373374
$vyfakturuj_api = $this->loader->get_api_client( $api_email, $api_key );
374375
try {
375-
$result = $this->loader->init_plugin_activation($vyfakturuj_api);
376+
$result = $this->loader->init_plugin_activation( $vyfakturuj_api );
376377
if ( isset( $result['status'] ) && $result['status'] === 'success' ) {
377378
update_option( 'ssc_valid_api_keys', 1 );
378379
} else {
@@ -471,4 +472,26 @@ public function ssc_get_option( $key = '', $default = null ) {
471472
public function is_settings_page() {
472473
return is_admin() && ! empty( $_GET['page'] ) && 'ssc_options' === $_GET['page'];
473474
}
475+
476+
public function maybe_display_messages() {
477+
if ( ! empty( $_POST['object_id'] ) ) {
478+
if ( ! empty( $_POST['ssc_api_endpoint_url'] ) ) {
479+
add_action( 'admin_notices', [ $this, 'custom_api_endpoint_notice' ] );
480+
}
481+
} else {
482+
if ( $this->ssc_get_option( 'ssc_api_endpoint_url' ) ) {
483+
add_action( 'admin_notices', [ $this, 'custom_api_endpoint_notice' ] );
484+
}
485+
}
486+
487+
}
488+
489+
public function custom_api_endpoint_notice() {
490+
$url = empty( $_POST['ssc_api_endpoint_url'] ) ? $this->ssc_get_option( 'ssc_api_endpoint_url' ) : esc_attr( $_POST['ssc_api_endpoint_url'] );
491+
?>
492+
<div class="notice notice-warning">
493+
<p><?php printf( __( "Warning: You have set custom SimpleShop API Endpoint URL: '%s'", 'simpleshop-cz' ), $url ); ?></p>
494+
</div>
495+
<?php
496+
}
474497
}

0 commit comments

Comments
 (0)