Restrict all pages except the following ids

This has been requested a few times before:



I really think you should consider implementing this. Maybe by making it possible to add all,-1,-3,-6 or something similar in the options field.
However, since it is not possible at the moment I tried the fix that @KTS915 provided in the last post I linked to and it does not work for me.
I have tried replacing the filter with the with ws_plugin__s2member_options, as I could not find any mention of option_ws_plugin__s2member_options in the s2member codex.

<?php
function kts_update_page_options( $options ) {

	if ( ! is_home() && ! is_front_page() && ! is_page( array( 1, 3, 6 ) ) ) {
		return $options;
	}
	unset( $options['level1_pages'] );

	return $options;
}

/*function kts_options_init() {
	add_filter( 'ws_plugin__s2member_options', 'kts_update_page_options' );
}*/
add_filter( 'ws_plugin__s2member_options', 'kts_update_page_options' );
//add_action( 'parse_query', 'kts_options_init' ); // Does not work

The filter runs, however, think I am filtering the wrong (too early?) place as there is nothing returned is I run get_the_ID() and the unset command is never fired. I also tried adding the filter in the parse_query action, but that never fired.
I hope someone can help.

Before you starting hooking…why not tell us what you are trying to do. That is…what problem are you trying to solve using s2member. Don’t describe your solution…tell us your what you are trying to protect / not protect.

There are many ways to lock / unlock content via s2member without resorting to code. Just pick the right configuration.

Sure, but it is a bit tricky, that is why I left it out. But you might be able to tell me I don’t need this :crossed_fingers:

I want my users to be able to send a link that is protected by the s2member download key to a third party. Because the download key latches on to the user’s IP and user agent I need the download_key to be generated by this third party. In order to make that happen I need them (third party) to hit a public page of my choosing where I can generate the key for them. (The visit to this public page will be validated by a key I made)

So:
User generates link (To the public page with my validation key)
Third party visits this link where a download key is created for the user (and the user is redirected to download the file). Subsequent visits to the public page with correct validation will not generate new download key, but serve the one already made.

I might be able to live with a time based link that does not consider IP, however the ‘universal’ and ‘ip-forever’ directives that you can provide the s2memeber_file_download_key function are too broad.

I hope you get it. It is a bit tricky and if there is another way please let me know :slight_smile:

I figured out how to make it work for one day for all IPs and user agents.
I have chosen to go with a more advanced version of this, so I don’t need the requested feature anymore. However, I still feel it should be implemented.
Thanks for the help @onepresstech
Posting the one day tweak here for people who might need it:

add_filter('ws_plugin__s2member_check_file_download_key', 'check_custom_download_key', 10, 2);    
function check_custom_download_key( $is_valid, $args ) {
	$key  = $args['key'];
	$file = $args['file'];

	if ( ! $is_valid ) {
		if ( $key === c_ws_plugin__s2member_files::file_download_key( $file, 'current_day' ) || $key === c_ws_plugin__s2member_files::file_download_key( '/' . $file, 'current_day' ) ) {
			$is_valid = true;
		}
	}

	return $is_valid;
}
add_filter('ws_plugin__s2member_file_download_key', 'generate_custom_download_key', 10, 2); 
function generate_custom_download_key( $key, $args ) {

	if ( $args['directive'] === 'current_day' && c_ws_plugin__s2member_no_cache::no_cache_constants( true ) ) {
		error_log( 'we get here' );
		$salt = date( 'Y-m-d' ) . $args['file'];

		$key = ( ! empty( $salt ) ) ? md5( c_ws_plugin__s2member_utils_encryption::xencrypt( $salt, false, false ) ) : '';
	}

	return $key;
}
1 Like

Interesting use case. Will work erratically though for ADSL users (ADSL carriers routinely re-allocate IP addresses to running systems) and users that switch their modem off at night. Most people don’t have dedicated IP addresses. It should work fine 99% of the time if the link timeout is short. The longer the link timeout the more failures will occur. Depends on how people are using it. If this is a quick here it is / download it / delete from server then it should work fine.