Created
May 30, 2019 20:41
-
-
Save wpmudev-sls/94ab1bd2deaab5f9d6ab051eada57e9d to your computer and use it in GitHub Desktop.
[Membership 2] - PayPal Profile Link. Adds a link to PayPal's profile page in M2 Subscription page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Membership 2] - PayPal Profile Link | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Adds a link to PayPal's profile page in M2 Subscription page | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_MS_PP_Profile_Link' ) ) { | |
class WPMUDEV_MS_PP_Profile_Link { | |
private static $_instance = null; | |
private static $gateway_id = 'paypalstandard'; | |
private static $mode = 'sandbox'; // Change to 'live' for live transactions | |
private static $pp_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments'; // This will be changed in constructor when mode is set to `live` | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_MS_PP_Profile_Link(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
if ( ! class_exists( 'MS_Factory' ) ) { | |
return; | |
} | |
if ( 'live' == self::$mode ) { | |
self::$pp_url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments'; | |
} | |
add_filter( 'ms_view_member_editor_fields_edit', array( $this, 'subscription_page_fields' ) ); | |
} | |
public function subscription_page_fields( $fields ) { | |
foreach ( $fields['subscriptions'] as $key => $field ) { | |
if ( isset( $field['title'] ) ) { | |
$subscription_id = $field['value'][0][1]; | |
if ( empty( $subscription_id ) || ! is_numeric( $subscription_id ) ) { | |
continue; | |
} | |
$subscription = MS_Factory::load( 'MS_Model_Relationship', $subscription_id ); | |
if ( $subscription->gateway_id != self::$gateway_id ) { | |
return $fields; | |
} | |
$profile_id = $this->get_profile_id( $subscription->id ); | |
if ( ! $profile_id ) { | |
return $fields; | |
} | |
$profile_url = add_query_arg( 'encrypted_profile_id', $profile_id, self::$pp_url ); | |
$fields['subscriptions'][$key]['value'][] = array( 'Link to PayPal Profile', "<a href=\"{$profile_url}\">PayPal Profile</a>" ); | |
} | |
} | |
return $fields; | |
} | |
public function get_profile_id( $subscription_id ) { | |
if ( is_object( $subscription_id ) ) { | |
if ( ! isset( $subscription_id->id ) ) { | |
return false; | |
} | |
$subscription_id = $subscription_id->id; | |
} | |
$transaction = $this->get_subscription_last_transaction( $subscription_id ); | |
$transaction_post_meta = get_post_meta( $transaction->ID, 'post', true ); | |
return isset( $transaction_post_meta['subscr_id'] ) ? $transaction_post_meta['subscr_id'] : false; | |
} | |
public function get_subscription_last_transaction( $subscription_id ) { | |
if ( is_object( $subscription_id ) ) { | |
if ( ! isset( $subscription_id->id ) ) { | |
return false; | |
} | |
$subscription_id = $subscription_id->id; | |
} | |
$transaction = array(); | |
$args = array( | |
'numberposts' => 1, | |
'offset' => 0, | |
'post_status' => 'any', // OR 'private', as transaction logs have private status in posts table | |
'post_type' => 'ms_transaction_log', | |
'meta_query' => array( | |
array( | |
'key' => 'subscription_id', | |
'value' => $subscription_id, | |
'compare' => '=', | |
), | |
array( | |
'key' => 'success', | |
'value' => 'ok', | |
'compare' => '=' | |
) | |
) | |
); | |
$transactions = get_posts( $args ); | |
if ( ! empty( $transactions ) ) { | |
return $transactions[0]; | |
} | |
else { | |
return false; | |
} | |
} | |
} | |
if ( ! function_exists( 'wpmudev_ms_pp_profile_link' ) ) { | |
function wpmudev_ms_pp_profile_link(){ | |
return WPMUDEV_MS_PP_Profile_Link::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_ms_pp_profile_link', 10 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment