Skip to content

Instantly share code, notes, and snippets.

@gaupoit
Created June 9, 2020 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaupoit/0e4809be19a2a22c7dc4402a3b2b9fc8 to your computer and use it in GitHub Desktop.
Save gaupoit/0e4809be19a2a22c7dc4402a3b2b9fc8 to your computer and use it in GitHub Desktop.
Sibling Passwords Protection
<?php
add_filter( 'ppwp_pro_check_valid_password', 'ppwp_pro_check_valid_sibling_password', 20, 2 );
/**
* Check valid sibling password.
*
* @param array $result The result after entering password including is_valid key.
* @param array $params The options passed from the hook including
* string password The password user entered.
* int parent_id The parent ID of current post or itself if it does not have parent.
* array current_roles The user's current roles.
* int post_id The current post ID.
*
* @return array
*/
function ppwp_pro_check_valid_sibling_password( $result, $params ) {
if ( ! isset( $params['parent_id'] ) || ! isset( $params['post_id'] ) ) {
return $result;
}
// If parent_id is same as post_id, it means
// + The current post doesn't have the parent
// + The option "Password Protect Child Pages" is off.
if ( $params['parent_id'] === $params['post_id'] ) {
// No child pages.
return $result;
}
// Only accept password having label with format "Child:id1,id2,id3".
$password_info = $result['password_info'];
if ( empty( $password_info->label ) ) {
$result['is_valid'] = false;
return $result;
}
$tmp = explode( 'Child:', trim( $password_info->label ) );
if ( count( $tmp ) < 2 ) {
return $result;
}
$child_ids = array_map( 'intval', explode( ',', trim( $tmp[1] ) ) );
$post_id = (int) ppw_get_post_id_from_request();
if ( ! in_array( $post_id, $child_ids, true ) ) {
$result['is_valid'] = false;
return $result;
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment