<?php
/**
 * A simple example to show you how to add a Renew link to the Member Profile page when a membership level expires in 30 days.
 *
 * You can add this recipe to your site by creating a custom plugin
 * or using the Code Snippets plugin available for free in the WordPress repository.
 * Read this companion article for step-by-step directions on either method.
 * https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
 */
function my_pmpro_show_renew_link_when_level_expires_in_x_days( $pmpro_member_action_links, $level_id ) {
	// Set the number of days until expiration.
	$days = 30;

	// Get the user's specific level data.
	$level = pmpro_getSpecificMembershipLevelForUser( get_current_user_id(), $level_id );

	// Bail if no level or no expiration date is set.
	if ( empty( $level ) || empty( $level->enddate ) ) {
		return $pmpro_member_action_links;
	}

	// Are we within the days until expiration?
	$now = current_time( 'timestamp' );
	$expiration_check = $now + ( $days * 3600 * 24 );

	if ( $expiration_check >= $level->enddate ) {
		$pmpro_member_action_links['renew'] = '<a href="' . esc_url( pmpro_url( 'renew' ) ) . '">' . __( 'Renew', 'paid-memberships-pro' ) . '</a>';
	}

	return $pmpro_member_action_links;
}
add_filter( 'pmpro_member_action_links', 'my_pmpro_show_renew_link_when_level_expires_in_x_days', 10, 2 );