Skip to content

Commit

Permalink
Merge branch 'trunk' into feat/issue-177-add-product-editor-compatibi…
Browse files Browse the repository at this point in the history
…lity
  • Loading branch information
ravinderk authored Dec 15, 2023
2 parents a3a9822 + 9567bbc commit d85da29
Show file tree
Hide file tree
Showing 5 changed files with 396 additions and 235 deletions.
32 changes: 27 additions & 5 deletions gateway-payfast.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
* WC tested up to: 8.3
* WC requires at least: 8.1
* Requires PHP: 7.3
*
* @package WooCommerce Gateway Payfast
*/

use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;

defined( 'ABSPATH' ) || exit;

define( 'WC_GATEWAY_PAYFAST_VERSION', '1.6.0' ); // WRCS: DEFINED_VERSION.
Expand All @@ -21,25 +25,34 @@

/**
* Initialize the gateway.
*
* @since 1.0.0
*/
function woocommerce_payfast_init() {
if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
return;
}

require_once( plugin_basename( 'includes/class-wc-gateway-payfast.php' ) );
require_once( plugin_basename( 'includes/class-wc-gateway-payfast-privacy.php' ) );
require_once plugin_basename( 'includes/class-wc-gateway-payfast.php' );
require_once plugin_basename( 'includes/class-wc-gateway-payfast-privacy.php' );
load_plugin_textdomain( 'woocommerce-gateway-payfast', false, trailingslashit( dirname( plugin_basename( __FILE__ ) ) ) );
add_filter( 'woocommerce_payment_gateways', 'woocommerce_payfast_add_gateway' );
}
add_action( 'plugins_loaded', 'woocommerce_payfast_init', 0 );

/**
* Add links to the plugin action links.
*
* @since 1.4.13
*
* @param array $links Plugin action links.
* @return array Modified plugin action links.
*/
function woocommerce_payfast_plugin_links( $links ) {
$settings_url = add_query_arg(
array(
'page' => 'wc-settings',
'tab' => 'checkout',
'page' => 'wc-settings',
'tab' => 'checkout',
'section' => 'wc_gateway_payfast',
),
admin_url( 'admin.php' )
Expand All @@ -58,7 +71,11 @@ function woocommerce_payfast_plugin_links( $links ) {

/**
* Add the gateway to WooCommerce
*
* @since 1.0.0
*
* @param string[] $methods WooCommerce payment methods.
* @return string[] Modified payment methods to include Payfast.
*/
function woocommerce_payfast_add_gateway( $methods ) {
$methods[] = 'WC_Gateway_PayFast';
Expand All @@ -67,13 +84,18 @@ function woocommerce_payfast_add_gateway( $methods ) {

add_action( 'woocommerce_blocks_loaded', 'woocommerce_payfast_woocommerce_blocks_support' );

/**
* Add the gateway to WooCommerce Blocks.
*
* @since 1.4.19
*/
function woocommerce_payfast_woocommerce_blocks_support() {
if ( class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
require_once dirname( __FILE__ ) . '/includes/class-wc-gateway-payfast-blocks-support.php';
add_action(
'woocommerce_blocks_payment_method_type_registration',
function( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) {
$payment_method_registry->register( new WC_PayFast_Blocks_Support );
$payment_method_registry->register( new WC_PayFast_Blocks_Support() );
}
);
}
Expand Down
20 changes: 13 additions & 7 deletions includes/class-wc-gateway-payfast-blocks-support.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?php
/**
* Payfast Payment Gateway
*
* @package WooCommerce Gateway Payfast
*/

use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;

/**
Expand All @@ -18,7 +24,7 @@ final class WC_PayFast_Blocks_Support extends AbstractPaymentMethodType {
* Initializes the payment method type.
*/
public function initialize() {
$this->settings = get_option( 'woocommerce_payfast_settings', [] );
$this->settings = get_option( 'woocommerce_payfast_settings', array() );
}

/**
Expand All @@ -27,8 +33,8 @@ public function initialize() {
* @return boolean
*/
public function is_active() {
$payment_gateways_class = WC()->payment_gateways();
$payment_gateways = $payment_gateways_class->payment_gateways();
$payment_gateways_class = WC()->payment_gateways();
$payment_gateways = $payment_gateways_class->payment_gateways();

return $payment_gateways['payfast']->is_available();
}
Expand All @@ -41,7 +47,7 @@ public function is_active() {
public function get_payment_method_script_handles() {
$asset_path = WC_GATEWAY_PAYFAST_PATH . '/build/index.asset.php';
$version = WC_GATEWAY_PAYFAST_VERSION;
$dependencies = [];
$dependencies = array();
if ( file_exists( $asset_path ) ) {
$asset = require $asset_path;
$version = is_array( $asset ) && isset( $asset['version'] )
Expand All @@ -62,7 +68,7 @@ public function get_payment_method_script_handles() {
'wc-payfast-blocks-integration',
'woocommerce-gateway-payfast'
);
return [ 'wc-payfast-blocks-integration' ];
return array( 'wc-payfast-blocks-integration' );
}

/**
Expand All @@ -71,12 +77,12 @@ public function get_payment_method_script_handles() {
* @return array
*/
public function get_payment_method_data() {
return [
return array(
'title' => $this->get_setting( 'title' ),
'description' => $this->get_setting( 'description' ),
'supports' => $this->get_supported_features(),
'logo_url' => WC_GATEWAY_PAYFAST_URL . '/assets/images/icon.png',
];
);
}

/**
Expand Down
80 changes: 56 additions & 24 deletions includes/class-wc-gateway-payfast-privacy.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
<?php
/**
* Payfast Payment Gateway
*
* @package WooCommerce Gateway Payfast
*/

if ( ! class_exists( 'WC_Abstract_Privacy' ) ) {
return;
}

/**
* Privacy/GDPR related functionality which ties into WordPress functionality.
*/
class WC_Gateway_PayFast_Privacy extends WC_Abstract_Privacy {
/**
* Constructor
*
*/
public function __construct() {
parent::__construct( __( 'Payfast', 'woocommerce-gateway-payfast' ) );
Expand All @@ -23,15 +31,16 @@ public function __construct() {
/**
* Returns a list of orders that are using one of Payfast's payment methods.
*
* @param string $email_address
* @param int $page
* The list of orders is paginated to 10 orders per page.
*
* @return array WP_Post
* @param string $email_address The user email address.
* @param int $page Page number to query.
* @return WC_Order[]|stdClass Number of pages and an array of order objects.
*/
protected function get_payfast_orders( $email_address, $page ) {
$user = get_user_by( 'email', $email_address ); // Check if user has an ID in the DB to load stored personal data.

$order_query = array(
$order_query = array(
'payment_method' => 'payfast',
'limit' => 10,
'page' => $page,
Expand All @@ -48,15 +57,16 @@ protected function get_payfast_orders( $email_address, $page ) {

/**
* Gets the message of the privacy to display.
*
*/
public function get_privacy_message() {
return wpautop( sprintf(
return wpautop(
sprintf(
/* translators: 1: anchor tag 2: closing anchor tag */
esc_html__( 'By using this extension, you may be storing personal data or sharing data with an external service. %1$sLearn more about how this works, including what you may want to include in your privacy policy.%2$s', 'woocommerce-gateway-payfast' ),
esc_html__( 'By using this extension, you may be storing personal data or sharing data with an external service. %1$sLearn more about how this works, including what you may want to include in your privacy policy.%2$s', 'woocommerce-gateway-payfast' ),
'<a href="https://docs.woocommerce.com/document/privacy-payments/#woocommerce-gateway-payfast" target="_blank" rel="noopener noreferrer">',
'</a>'
) );
)
);
}

/**
Expand Down Expand Up @@ -113,7 +123,7 @@ public function subscriptions_data_exporter( $email_address, $page = 1 ) {
$data_to_export = array();

$meta_query = array(
'relation' => 'AND',
'relation' => 'AND',
array(
'key' => '_payment_method',
'value' => 'payfast',
Expand All @@ -126,10 +136,11 @@ public function subscriptions_data_exporter( $email_address, $page = 1 ) {
),
);

$subscription_query = array(
'posts_per_page' => 10,
'page' => $page,
'meta_query' => $meta_query,
$subscription_query = array(
'posts_per_page' => 10,
'page' => $page,
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'meta_query' => $meta_query,
);

$subscriptions = wcs_get_subscriptions( $subscription_query );
Expand Down Expand Up @@ -179,17 +190,17 @@ public function order_data_eraser( $email_address, $page ) {
$order = wc_get_order( $order->get_id() );

list( $removed, $retained, $msgs ) = $this->maybe_handle_order( $order );
$items_removed |= $removed;
$items_retained |= $retained;
$messages = array_merge( $messages, $msgs );
$items_removed |= $removed;
$items_retained |= $retained;
$messages = array_merge( $messages, $msgs );

list( $removed, $retained, $msgs ) = $this->maybe_handle_subscription( $order );
$items_removed |= $removed;
$items_retained |= $retained;
$messages = array_merge( $messages, $msgs );
$items_removed |= $removed;
$items_retained |= $retained;
$messages = array_merge( $messages, $msgs );
}

// Tell core if we have more orders to work on still
// Tell core if we have more orders to work on still.
$done = count( $orders ) < 10;

return array(
Expand All @@ -203,7 +214,7 @@ public function order_data_eraser( $email_address, $page ) {
/**
* Handle eraser of data tied to Subscriptions
*
* @param WC_Order $order
* @param WC_Order $order Order object.
* @return array
*/
protected function maybe_handle_subscription( $order ) {
Expand All @@ -223,8 +234,27 @@ protected function maybe_handle_subscription( $order ) {
return array( false, false, array() );
}

/**
* Filter privacy eraser subscription statuses.
*
* Modify the subscription statuses that are considered active and should be retained.
*
* @since 1.4.13
*
* @param string[] $statuses Array of subscription statuses considered active.
*/
if ( $subscription->has_status( apply_filters( 'wc_payfast_privacy_eraser_subs_statuses', array( 'on-hold', 'active' ) ) ) ) {
return array( false, true, array( sprintf( esc_html__( 'Order ID %d contains an active Subscription' ), $order->get_id() ) ) );
return array(
false,
true,
array(
sprintf(
/* translators: %d: Order ID */
esc_html__( 'Order ID %d contains an active Subscription' ),
$order->get_id()
),
),
);
}

$renewal_orders = WC_Subscriptions_Renewal_Order::get_renewal_orders( $order->get_id(), 'WC_Order' );
Expand All @@ -243,7 +273,9 @@ protected function maybe_handle_subscription( $order ) {
/**
* Handle eraser of data tied to Orders
*
* @param WC_Order $order
* @since 1.4.13
*
* @param WC_Order $order The order object.
* @return array
*/
protected function maybe_handle_order( $order ) {
Expand Down
Loading

0 comments on commit d85da29

Please sign in to comment.