Skip to content

Instantly share code, notes, and snippets.

@ituk
Created May 18, 2020 16:57
Show Gist options
  • Save ituk/fac06ccd9ad2cae7017e857267ef3351 to your computer and use it in GitHub Desktop.
Save ituk/fac06ccd9ad2cae7017e857267ef3351 to your computer and use it in GitHub Desktop.
wp-members modifictaions
<?php
/***** membership and subscription *****/
/** Change user role depending on expiration status, hooked to init when user is logged in **/
add_action( 'init', 'itamar_check_role_expiration__init' );
function itamar_check_role_expiration__init() {
// What are the role slugs for the two roles?
$active_member = 'memberfriend';
$expired_member = 'subscriber';
// If user is logged in, but is also expired...
if ( is_user_logged_in() && ! is_user_expired() ) {
// Check the user role, if
if ( wpmem_user_has_role( $active_member ) ) {
$user = new WP_User( get_current_user_id() );
$user->remove_role( $active_member );
$user->add_role( $expired_member );
}
}
elseif( is_user_logged_in() && is_user_expired() ){
if ( wpmem_user_has_role( $expired_member ) ){
$user = new WP_User( get_current_user_id() );
$user->remove_role( $expired_member );
$user->add_role( $active_member );
}
}
}
/** Change user role depending on expiration status, hooked to the end of registration **/
add_action( 'user_register', 'itamar_check_role_expiration__register', 10, 1 );
function itamar_check_role_expiration__register($user_id) {
// What are the role slugs for the two roles?
$active_member = 'memberfriend';
$expired_member = 'subscriber';
$user = new WP_User( $user_id );
// If user is logged in, but is also expired...
if ( is_user_expired($user_id) ) {
// Check the user role, if
if ( wpmem_user_has_role( $active_member ) ) {
$user->remove_role( $active_member );
$user->add_role( $expired_member );
}
}
else{
if ( wpmem_user_has_role( $expired_member ) ){
$user->remove_role( $expired_member );
$user->add_role( $active_member );
}
}
}
/** redirect wp native logout to home page **/
add_action('wp_logout', 'logout_redirect_home');
function logout_redirect_home(){
wp_safe_redirect(home_url());
exit;
}
/** redirect wp-members logout to home page **/
add_filter( 'wpmem_logout_redirect', 'itamar_logout_redirect' );
function itamar_logout_redirect( $redirect_to ) {
return home_url();
}
/** redirect wp-login.php to frontend login **/
add_action('login_init', 'itamar_login_page');
function itamar_login_page() {
if ( is_plugin_active( 'wp-members/wp-members.php' ) ) {
wp_redirect($url, 302);
}
}
/** redirect logged-out visitors away from change password page to login page, with a redirection back after login **/
add_action( 'template_redirect', function() {
if ( 'pwdchange' == wpmem_get( 'a', false, 'get' ) ) {
if ( ! is_user_logged_in() ) {
wpmem_redirect_to_login();
}
}
});
/** redirect users to profile page upon first login **/
add_filter( 'wpmem_login_redirect', 'bayadaim_first_login', 1, 2 );
function bayadaim_first_login( $redirect_to, $user_id ) {
// Check for the presence of a flag if the user has logged in before.
$first_login = get_user_meta( $user_id, 'first_login', true );
// If no flag, then set one, and redirect to profile edit page.
if ( ! $first_login ) {
update_user_meta( $user_id, 'first_login', 'true', '' );
// Return the user profile url for profile edit.
return wpmem_profile_url('edit')."&first_login=1";
} else {
// If we are here, it's not a first-time login
//if redirect destination is set in url
if($redirect_to != null){
return $redirect_to;
}else{
//redirect to main profile page;
return wpmem_profile_url();
}
}
}
/** redirect expired logged-in users from registration/join to renew page **/
function bayadaim_register_redirect() {
if ( is_user_logged_in() && !is_user_expired() && (is_page( 37254 ) || is_page( 35332 ) ) ) {
wp_redirect( wpmem_profile_url('renew') );
die;
}
}
add_action( 'template_redirect', 'bayadaim_register_redirect' );
/** change wp lost password url **/
add_filter( 'lostpassword_url', 'itamar_lost_password_page', 10, 2 );
function itamar_lost_password_page( $lostpassword_url, $redirect ) {
if ( is_plugin_active( 'wp-members/wp-members.php' ) ) {
return wpmem_profile_url('pwdreset');
}
}
/** change wp login url **/
add_filter('login_url', 'itamar_login_url', 10, 2 );
function itamar_login_url($login_url, $redirect) {
if ( is_plugin_active( 'wp-members/wp-members.php' ) ) {
$redirect = urlencode($redirect);
return wpmem_login_url()."?redirect_to=".$redirect;
}
}
/** change text for renew membership page **/
add_filter( 'wpmem_payment_form_msg', 'bayadaim_payment_form_msg' );
function bayadaim_payment_form_msg( $str ) {
global $wpmem_pp_sub;
$user = get_current_user_id();
extract( wpmem_discount_code_values() );
$user_disc = get_user_meta( $user, $field, true );
$c = "&#8362;";
$trial_cost = $wpmem_pp_sub->subscriptions['default']['trial_cost'];
if ( $user_disc == $disc_code ) {
// if the discount code is valid,
$trial_cost = round( ( $trial_cost * $discount ), 2 );
}
$trial_cost = $trial_cost.$c;
$sbsc_cost = $wpmem_pp_sub->subscriptions['default']['subscription_cost'];
$sbsc_cost = $sbsc_cost.$c;
$exp_date1 = get_user_meta( $user, 'expires', 'true' );
$exp_date = strtotime($exp_date1);
$exp_date = date_i18n('j בF Y', $exp_date) . PHP_EOL;
$ftr_date = strtotime('+1 year', strtotime($exp_date1));
$ftr_date = date_i18n('j בF Y', $ftr_date) . PHP_EOL;
if(!is_user_expired()){
if(exp_date1 != 0){
$msg = '<h2>חברותך במעגל העסקים לא בתוקף.</h2>';
$msg .= '<p>תאריך תפוגת החברות שלך חל ב-'.$exp_date.'.';
$msg .= '<br />עליך להשלים את תהליך התשלום על ידי לחיצה על הכפתור "תשלום". חברותך תוארך בשנה (מהיום) ותוקפה יחול עד ה-'.date_i18n('j בF Y', strtotime('+1 year')).'<br /></p>';
}else{
$msg = '<h2>רוצה לפרסם את העסק שלך?</h2>';
$msg .= '<p>משתמש עסקי באתר בידיים מקבל את כל מה שמשתמש חינמי מקבל, וגם:</p>';
$msg .= '<ul>';
$msg .= '<li>פתיחת דף עסק</li>';
$msg .= '<li>פרסום מודעות ברחבי האתר</li>';
$msg .= '<li>פרסום אירועים</li>';
$msg .= '<li>סטיקר "בידיים" ישלח בדואר</li>';
$msg .= '</ul>';
$msg .= '<p>תוקף החברות הינו לשנה. עד ה-<strong>'.date_i18n('j בF Y', strtotime('+1 year')).'.</strong></p>';
}
}
else{
$msg = '<h2>חברותך במעגל העסקים עוד בתוקף, איזה יופי!</h2><br />';
$msg .= '<p>תקופת החברות שלך תסתיים ב-'.$exp_date.'.';
$msg .= '<br />ניתן להאריך את תקופת החברות בשנה נוספת, עד לתאריך ה-'.$ftr_date.'</p>';
}
$msg .= '<p>עלות חברות הינה <strong>'.$trial_cost.'</strong> לשנה הראשונה. לאחר שנה יחודש המנוי באופן אוטומטי בעלות של '.$sbsc_cost.' לשנה.</p>';
$msg .= '<p>התשלום מופעל על ידי Paypal ויכול להתבצע בעזרת כרטיס אשראי או חשבון Paypal פעיל.</p>';
$msg .= '<p>ניתן לבטל את המנוי בכל עת דרך חשבון הPaypal, או על ידי יצירת קשר עימנו.</p>';
return $msg;
}
/** add reg_type hidden field to register post **/
add_filter( 'wpmem_register_hidden_fields', function( $html, $tag ){
if ( 'new' == $tag ) {
$reg_type = wpmem_get( 'reg_type', false, 'request' );
if ( 'memberfriend' == $reg_type ) {
$field = wpmem_form_field( array(
'name' => 'reg_type',
'type' => 'hidden',
'value' => $reg_type,
'compare' => '',
));
$html = $field . $html;
}
}
return $html;
}, 10, 2 );
/** Push users directly to paypal if they click on the relevant lead **/
add_action( 'wpmem_register_redirect', 'bayadaim_push_user_to_paypal' );
function bayadaim_push_user_to_paypal( $fields ) {
global $wpmem, $wpmem_pp_sub;
// Set the Thank you page
$thanks = add_query_arg( array( 'auth'=>'1','user'=>$fields["username"] ), get_page_link(37287) );
//push only potential paying (memberfriend) users
if ( 'memberfriend' == wpmem_get( 'reg_type' ) ) {
//wpmem_email_to_user( $fields['ID'], $fields['password'], '1', $wpmem->fields, $fields );
//wpmem_notify_admin( $fields['ID'], $wpmem->fields, $fields );
// Get the PayPal Settings.
$arr = $wpmem_pp_sub->subscriptions['default'];
// Set up defaults.
$button_args = array(
"cmd" => ( ! $wpmem_pp_sub->paypal_cmd ) ? '_xclick' : $wpmem_pp_sub->paypal_cmd,
"business" => $wpmem_pp_sub->paypal_id,
"item_name" => stripslashes( $arr['subscription_name'] ),
"no_shipping" => '',
"return" => $thanks,
"notify_url" => $wpmem_pp_sub->paypal_ipn,
"no_note" => '1',
"currency_code" => $wpmem_pp_sub->subscriptions['default']['currency'],
"rm" => '2',
"custom" => $fields['ID'],
);
// Add the user ID.
$button_args['custom'] = $fields['ID'];
// Handle regular vs recurring & recurring with trial.
if ( $button_args['cmd'] === '_xclick' ) {
$button_args['amount'] = $wpmem_pp_sub->subscriptions['default']['subscription_cost'];
} else {
$button_args['a3'] = $wpmem_pp_sub->subscriptions['default']['subscription_cost'];
$button_args['p3'] = $wpmem_pp_sub->subscriptions['default']['subscription_num'];
$button_args['t3'] = strtoupper( $wpmem_pp_sub->subscriptions['default']['subscription_per'][0] );
$button_args['src'] = "1";
$button_args['sra'] = "1";
if( $arr['trial_num'] ) {
$button_args['a1'] = $wpmem_pp_sub->subscriptions['default']['trial_cost'];
$button_args['p1'] = $wpmem_pp_sub->subscriptions['default']['trial_num'];
$button_args['t1'] = strtoupper( $wpmem_pp_sub->subscriptions['default']['trial_per'][0] );
}
}
// Build and output the form so it can be submitted.
echo '<form name="paypalform" action="' . $wpmem_pp_sub->paypal_url . '" method="post">';
foreach ( $button_args as $key => $val ) {
echo '<input type="hidden" name="' . $key . '" value="' . $val . '">';
}
echo '</form>';
// Submit the form with JS.
echo '<script language="JavaScript">document.paypalform.submit();</script>';
// Exit so no screen output.
exit();
} //endif for payers
else{ //non-payers continiue to default thank you message
wp_redirect($thanks);
}
}
// pass ?redirect_to= query from login to registration
add_filter( 'wpmem_reg_link', 'bayadaim_reg_link' );
function bayadaim_reg_link( $str ) {
$redirect_to = $_GET['redirect_to'];
$url = add_query_arg( 'redirect_to', $redirect_to, $str );
return $url;
}
/** send welcome email to paying members **/
//add the email to email tab in wp-members plugin page
add_action( 'wpmem_after_admin_init', 'bayadaim_welcome_paying_email' );
function bayadaim_welcome_paying_email() {
// first paying email
$tag = 'welcome_paying_email';
$heading = 'תודה על תשלום ראשון';
$subject_input = 'welcome_paying_subj_tag';
$message_input = 'welcome_paying_body_tag';
wpmem_add_custom_email( $tag, $heading, $subject_input, $message_input );
// renew email
$tag = 'welcome_renew_email';
$heading = 'תודה על חידוש תשלום';
$subject_input = 'welcome_renew_subj_tag';
$message_input = 'welcome_renew_body_tag';
wpmem_add_custom_email( $tag, $heading, $subject_input, $message_input );
/*
// notify admin on new paying member
$tag = 'admin_paying_email';
$heading = 'עדכון מנהל על תשלום חדש';
$subject_input = 'admin_paying_subj_tag';
$message_input = 'admin_paying_body_tag';
wpmem_add_custom_email( $tag, $heading, $subject_input, $message_input );
// notify admin on renew
$tag = 'admin_renew_email';
$heading = 'עדכון מנהל על חידוש תשלום';
$subject_input = 'admin_renew_subj_tag';
$message_input = 'admin_renew_body_tag';
wpmem_add_custom_email( $tag, $heading, $subject_input, $message_input );*/
}
//send the email
//add_action('wpmem_exp_after_set_exp', 'bayadaim_send_welcome_paying_email', 10, 2);
function bayadaim_send_welcome_paying_email($user_id, $renew){
if ( 'expire' == wpmem_get( 'expire_user' ) || 'pending' == wpmem_get( 'expire_user' ))
return;
if(!$renew){
$tag = 'welcome_paying_email';
}
else{
$tag = 'welcome_renew_email';
}
$email = get_option( $tag );
$custom = array(
'subj' => $email['subj'],
'body' => $email['body'],
'tag' => $tag,
);
wpmem_email_to_user( $user_id, '', 5, '', '', $custom );
}
/** fix date format in wp-members emails **/
add_filter( 'wpmem_email_shortcodes', function( $shortcodes ) {
$format = get_option('date_format'); //from wp settings
$shortcodes['exp-date'] = date_i18n( $format, strtotime( $shortcodes['exp-date'] ) );
return $shortcodes;
});
/** Override the WP random password generator **/
add_filter( 'random_password', 'bayadain_random_password' );
function bayadain_random_password() {
$characters ='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$length = 8;
$password = '';
for( $i = 0; $i < $length; $i++ ) {
$password .= substr( $characters, wp_rand( 0, strlen( $characters ) - 1 ), 1 );
}
return $password;
}
/** add screen reader class to registration form **/
add_filter( 'wpmem_register_form', 'bayadaim_register_form_filter', 10, 4 );
function bayadaim_register_form_filter( $form ) {
$script = "<script>
jQuery('label.text, label.textarea').addClass('screen-reader-text');
</script>";
return $form.$script;
}
/** change register form row: wrap each row with a div **/
add_filter( 'wpmem_register_form_rows', 'my_row_wrapper', 10, 2 );
function my_row_wrapper( $rows, $tag ) {
foreach ( $rows as $meta_key => $row ) {
$rows[ $meta_key ]['row_before'] = '<div class="reg-row-wrapper '.$row["meta"].'">';
$rows[ $meta_key ]['row_after'] = '</div>';
//print_r($row);
}
return $rows;
}
function bayadaim_memberfriend_post_class( $classes ) {
if ( 'memberfriend' == wpmem_get( 'reg_type', false, 'request' ) ) {
$classes[] = 'memberfriend';
}
if('subscriber' == wpmem_get( 'reg_type', false, 'request' )){
$classes[] = 'subscriber';
}
if('1' == wpmem_get( 'first_login', false, 'request' )){
$classes[] = 'first-login';
}
if('edit' == wpmem_get( 'a', false, 'request' )){
$classes[] = 'edit-profile';
}
if('pwdchange' == wpmem_get( 'a', false, 'request' )){
$classes[] = 'edit-password';
}
if('renew' == wpmem_get( 'a', false, 'request' )){
$classes[] = 'renew';
}
return $classes;
}
add_filter( 'post_class', 'bayadaim_memberfriend_post_class' );
/** change register button text only for potential paying users **/
add_filter( 'wpmem_register_form_args', 'itamar_register_form_args', 10, 2 );
function itamar_register_form_args( $args, $toggle ){
if ( 'memberfriend' == wpmem_get( 'reg_type', false, 'request' ) ) {
$args = array('submit_register' => 'המשך לתשלום');
}
if ( '1' == wpmem_get( 'first_login', false, 'get' ) ) {
$text = "<p>רשמו את שמכם וספרו לנו קצת מי אתם.<br />הפרטים יופיעו לצד תכנים שתפרסמו, ובעמוד האישי שלכם באתר.</p>";
$args = array(
'submit_update' => 'סיום הרשמה',
'heading_before' => '<legend>',
'heading_after' => '</legend>'.$text,
);
}
return $args;
}
/* change payment button text **/
add_filter( 'wpmem_payment_button_args', 'bayadaim_payment_button_args', 10, 2 );
function bayadaim_payment_button_args( $arr ){
$arr['button_text'] = 'המשך לתשלום';
return $arr;
}
add_filter( 'wpmem_user_edit_heading', 'bayadaim_first_login_profile_heading' );
function bayadaim_first_login_profile_heading( $heading ){
if ( '1' == wpmem_get( 'first_login', false, 'get' ) ) {
$heading = 'נעים להכיר!';
}
return $heading;
}
/** remove register heading **/
add_filter( 'wpmem_register_heading', 'itamar_register_form_heading' );
function itamar_register_form_heading( $heading ) {
$heading = '';
return $heading;
}
/** remove extra space in wp-members messages */
add_filter( 'wpmem_msg_dialog_arr', 'my_msg_dialog', 10, 2 );
function my_msg_dialog( $args, $toggle ) {
$args['div_before'] = '<div>';
$args['p_before'] = '';
$args['p_after'] = '';
/** return any values you are setting **/
return $args;
}
/** Change all links in main profile page **/
add_filter( 'wpmem_member_links', 'bayadaim_profile_links' );
function bayadaim_profile_links($string){
if( is_user_logged_in() ) {
$user_id = get_current_user_id();
$locations = count_user_posts( $user_id , 'location' ); //cout user's posts (type = location) to check if they have any
$posts = count_user_posts( $user_id ); //cout user's posts
$author_page = get_author_posts_url( $user_id );
$string = '';
if($locations == 0 && current_user_can('edit_locations')) {
$string .= '<a href="'.admin_url("post-new.php?post_type=location").'" title="הוספת עסק חדש" target="_blank" class="button reg_button profile_button">פתיחת עמוד עסק</a>';
}
if(current_user_can('edit_posts')) {
$string .= '<a href="'.admin_url("post-new.php").'" title="הוספת תוכן חדש" target="_blank" class="button reg_button profile_button">יצירת תוכן</a>';
}
if(current_user_can('edit_ads')) {
$string .= '<a href="'.admin_url("post-new.php?post_type=ads").'" title="הוספת מודעה חדשה" target="_blank" class="button reg_button profile_button">יצירת מודעה</a>';
}
if(current_user_can('edit_events')) {
$string .= '<a href="'.admin_url("post-new.php?post_type=event").'" title="הוספת אירוע חדש" target="_blank" class="button reg_button profile_button">יצירת אירוע</a>';
}
if ( ! is_user_expired() && ! current_user_can('manage_options') ){
$string .= '<a href="'.wpmem_profile_url("renew").'" title="שדרוג למשתמש עסקי" target="_blank" class="button reg_button profile_button upgrade_button">שדרוג למשתמש עסקי</a>';
}
$string .= '<ul class="profile_bottom_links"><li><a href="' . wpmem_profile_url("edit") . '">עדכון פרופיל</a></li>';
$string .= '<li>|</li>';
$string .= '<li><a href="' . wpmem_profile_url("pwdchange") . '">' . __( 'Change Password', 'wp-members' ) . '</a></li>';
if( $posts > 0 && $author_page != ''){
$string .= '<li>|</li>';
$string .= '<li><a href="'.$author_page.'">עמוד כותב</a></li>';
}
if(current_user_can('read')) {
$string .= '<li>|</li>';
$string .= '<li><a href="'.admin_url().'" title="לוח בקרה" target="_blank">לוח בקרה</a></li>';
}
if ( is_user_expired() ){
$string .= '<li>|</li>';
$string .= '<li><a href="'.wpmem_profile_url("renew").'" title="חידוש מנוי" target="_blank">חידוש מנוי</a></li>';
}
$string .= '</ul>';
}
return $string;
}
/** fix missing translations **/
function bayadaim_fix_translations( $translated_text, $text, $domain ) {
if(function_exists('wpmem_current_url') ) :
//if(in_array( wpmem_current_url(), wpmem_user_pages() )) :
switch ( $translated_text ) {
case 'Username or Email' :
$translated_text = 'שם משתמש'; //this is weird, but working. do not change
break;
case 'E-mail' :
$translated_text = 'שם משתמש או דוא"ל'; //this is weird, but working. do not change
break;
case 'Subscription' :
$translated_text = 'משתמש עסקי';
break;
case 'Renew Subscription' :
$translated_text = 'חידוש משתמש עסקי';
break;
}
//endif; //in_array
endif; //function_exists(wpmem_current_url)
return $translated_text;
}
add_filter( 'gettext', 'bayadaim_fix_translations', 20, 3 );
// [wpmem_get] enclosing shortcode with attributes
function wpmem_get_func( $atts, $content = null ) {
$a = shortcode_atts( array(
'value' => '',
'name' => '',
'default' => null,
'method' => 'post'
), $atts );
if(function_exists('wpmem_get')){
$key = wpmem_get( $a['name'], $a['default'], $a['method'] );
if( $a['value'] == $key ){
return $content;
}else{
return false;
}
}
}
add_shortcode( 'wpmem_get', 'wpmem_get_func' );
// [get_query_arg] shortcode with attributes
function get_query_arg( $atts ) {
$a = shortcode_atts( array(
'key' => ''
), $atts );
$key = $a['key'];
return $_GET[$key];
}
add_shortcode( 'get_query_arg', 'get_query_arg' );
function bayadaim_auth_login_form_filter( $form, $action ){
if( is_page(37287) && $_GET['auth'] == 1 && isset( $_GET['user']) ){
$script = "<script>";
$script .= "jQuery('input#log').val('". $_GET['user'] ."');";
//$script .= "jQuery('input#log').attr('type', 'hidden');";
$script .= "jQuery('label[for=\"pwd\"], #wpmem_login_form legend, #rememberme, .link-text, label[for=\"rememberme\"]').hide();";
$script .= "</script>";
$form = $form.$script;
}
return $form;
}
add_filter( 'wpmem_login_form', 'bayadaim_auth_login_form_filter', 10, 2 );
add_action( 'wpmem_post_update_data', 'bayadaim_user_update_data' );
add_action( 'wpmem_post_register_data', 'bayadaim_user_update_data' );
function bayadaim_user_update_data( $fields ) {
//get first and last name from form
$first_name = $fields['first_name'];
$last_name = $fields['last_name'];
$space = ($first_name && $last_name) ? " " : "";
$display_name = $first_name.$space.$last_name;
if($display_name == ""){
$display_name = strstr($fields['user_email'], '@', true);
}
wp_update_user( array ('ID' => $fields['ID'], 'display_name'=> $display_name, 'nickname' => $display_name));
return;
}
/** TEMP: give free trial for invitation code users */
/* function wpmem_discount_code_values(){
return array(
'field' => 'invite_code',
'disc_code' => 'FREE_1',
'discount' => 0.00,
);
} */
/**
* You probably don't need to change anything below here
*/
/* add_filter( 'wpmem_payment_button_args', 'wpmem_adjust_payment_button' );
function wpmem_adjust_payment_button( $args ) {
global $wpmem_regchk;
extract( wpmem_discount_code_values() );
if ( $wpmem_regchk == 'success' ) {
// new registration
if ( isset( $_POST[$field] ) && $_POST[$field] == $disc_code ) {
// if the discount code is valid,
$args['trial_cost'] = ( $args['trial_cost'] * $discount );
}
} else {
// everyone else (user is logged in/renewal)
$user_disc = get_user_meta( get_current_user_id(), $field, true );
if ( $user_disc == $disc_code ) {
// if the discount code is valid,
$args['trial_cost'] = round( ( $args['trial_cost'] * $discount ), 2 );
}
}
return $args;
}
add_filter( 'wpmem_payment_form', 'wpmem_adjust_payment_form' );
function wpmem_adjust_payment_form( $args ) {
extract( wpmem_discount_code_values() );
$user_disc = get_user_meta( get_current_user_id(), $field, true );
if ( $user_disc == $disc_code ) {
$args['subscription_cost'] = round( ( $args['subscription_cost'] * $discount ), 2 );
}
return $args;
} */
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment