wc-cities
3 weeks ago
wc-gateways
3 weeks ago
WcGateways.php
3 weeks ago
WooCommerceCitySelect.php
3 weeks ago
WcGateways.php
180 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPParsidate\App\Integration\WooCommerce; |
| 4 | |
| 5 | defined( 'ABSPATH' ) or exit( 'No direct script access allowed' ); |
| 6 | |
| 7 | use WPParsidate\Helper\Cache; |
| 8 | use WPParsidate\Settings\Settings; |
| 9 | |
| 10 | /** |
| 11 | * Add Iranian payment gateways to WP-Parsidate |
| 12 | * |
| 13 | * @package WP-Parsidate |
| 14 | * @subpackage Plugins/WooCommerce/PaymentGateways |
| 15 | */ |
| 16 | class WcGateways { |
| 17 | public static $instance = null; |
| 18 | |
| 19 | /** |
| 20 | * Hooks required tags |
| 21 | */ |
| 22 | public function __construct() { |
| 23 | add_action( 'before_woocommerce_init', [ $this, 'includeFiles' ] ); |
| 24 | add_filter( 'wp_parsidate_woocommerce_settings_options', [ $this, 'addSettings' ] ); |
| 25 | add_filter( 'woocommerce_payment_gateways', [ $this, 'registerSelectedGateways' ] ); |
| 26 | add_action( 'woocommerce_blocks_loaded', [ $this, 'registerOrderApprovalPaymentMethodType' ] ); |
| 27 | } |
| 28 | |
| 29 | public function gateways(): array { |
| 30 | return array( |
| 31 | 'parsian' => esc_html__( 'Parsian Bank', 'wp-parsidate' ), |
| 32 | 'pasargad' => esc_html__( 'Pasargad Bank', 'wp-parsidate' ), |
| 33 | 'mellat' => esc_html__( 'Mellat Bank (Behpardakht)', 'wp-parsidate' ), |
| 34 | 'melli' => esc_html__( 'Melli Bank (Sadad)', 'wp-parsidate' ), |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Includes files for plugin |
| 40 | * |
| 41 | * @return void |
| 42 | * @since 2.0 |
| 43 | */ |
| 44 | public function includeFiles(): void { |
| 45 | $implemented_gateways = array_keys( $this->gateways() ); |
| 46 | |
| 47 | $selected_gateways = $this->getSelectedGateways(); |
| 48 | $maybe_include = array_intersect( $implemented_gateways, $selected_gateways ); |
| 49 | |
| 50 | foreach ( $maybe_include as $filename ) { |
| 51 | $file_path = __DIR__ . "/wc-gateways/wpp-$filename-gateway.php"; |
| 52 | |
| 53 | if ( file_exists( $file_path ) ) { |
| 54 | require_once( $file_path ); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns an instance of class |
| 61 | * |
| 62 | * @return WcGateways |
| 63 | */ |
| 64 | public static function getInstance(): ?WcGateways { |
| 65 | if ( self::$instance === null ) { |
| 66 | self::$instance = new WcGateways(); |
| 67 | } |
| 68 | |
| 69 | return self::$instance; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Adds settings for toggle fixing |
| 74 | * |
| 75 | * @param array $wooSettings WooCommerce section settings |
| 76 | * |
| 77 | * @return array New settings |
| 78 | * @since 4.0.0 |
| 79 | */ |
| 80 | public function addSettings( $wooSettings ): array { |
| 81 | $gateWays = $this->gateways(); |
| 82 | $gateWaySettings = []; |
| 83 | |
| 84 | foreach ( $gateWays as $code => $name ) { |
| 85 | $gateWaySettings[ $code . '_gateway_enable' ] = array( |
| 86 | 'id' => $code . '_gateway_enable', |
| 87 | 'title' => $name, |
| 88 | 'type' => 'toggle', |
| 89 | 'default' => false, |
| 90 | 'sanitize' => 'bool' |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | $settings = array( |
| 95 | 'woo_gateways_start_grid' => array( |
| 96 | 'id' => 'woo_product_start_grid', |
| 97 | 'title' => esc_html__( 'Payment Gateways', 'wp-parsidate' ), |
| 98 | 'type' => 'startGrid', |
| 99 | ) |
| 100 | ); |
| 101 | $settings = array_merge( $settings, $gateWaySettings ); |
| 102 | $settings['woo_gateways_end_grid'] = array( 'type' => 'endGrid' ); |
| 103 | |
| 104 | $wooSettings['settings'] = array_merge( $wooSettings['settings'], $settings ); |
| 105 | |
| 106 | return $wooSettings; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @param $methods |
| 111 | * |
| 112 | * @return mixed |
| 113 | * @since 5.0.0 |
| 114 | */ |
| 115 | public function registerSelectedGateways( $methods ) { |
| 116 | $selected_pgs = $this->getSelectedGateways(); |
| 117 | |
| 118 | if ( empty( $selected_pgs ) ) { |
| 119 | return $methods; |
| 120 | } |
| 121 | |
| 122 | foreach ( $selected_pgs as $method ) { |
| 123 | $methods[] = 'WPP_WC_' . ucfirst( $method ) . '_Gateway'; |
| 124 | } |
| 125 | |
| 126 | return $methods; |
| 127 | } |
| 128 | |
| 129 | public function registerOrderApprovalPaymentMethodType(): void { |
| 130 | if ( ! class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | add_action( 'woocommerce_blocks_payment_method_type_registration', |
| 135 | function ( \Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) { |
| 136 | $implemented_gateways = array_keys( $this->gateways() ); |
| 137 | |
| 138 | $selected_gateways = self::getSelectedGateways(); |
| 139 | $maybe_include = array_intersect( $implemented_gateways, $selected_gateways ); |
| 140 | |
| 141 | foreach ( $maybe_include as $gateway ) { |
| 142 | $block_path = __DIR__ . "/wc-gateways/blocks/wpp-$gateway-pg-block.php"; |
| 143 | |
| 144 | if ( file_exists( $block_path ) ) { |
| 145 | require_once( $block_path ); |
| 146 | |
| 147 | $class_name = 'WPP_WC_' . ucfirst( $gateway ) . '_Gateway_Blocks'; |
| 148 | |
| 149 | $payment_method_registry->register( new $class_name ); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | public function is_soap_enabled() { |
| 157 | return extension_loaded( 'soap' ); |
| 158 | } |
| 159 | |
| 160 | private function getSelectedGateways() { |
| 161 | $cache = Cache::get( 'woocommerce_active_gateways', false ); |
| 162 | if ( is_array( $cache ) ) { |
| 163 | return $cache; |
| 164 | } |
| 165 | |
| 166 | $gateWays = array_keys( $this->gateways() ); |
| 167 | $activeGateways = []; |
| 168 | foreach ( $gateWays as $code ) { |
| 169 | if ( Settings::get( $code . '_gateway_enable', false, 'woocommerce' ) ) { |
| 170 | $activeGateways[] = $code; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | $activeGateways = apply_filters( 'wpp_get_selected_wc_payment_gateways', $activeGateways ); |
| 175 | Cache::set( 'woocommerce_active_gateways', $activeGateways ); |
| 176 | |
| 177 | return $activeGateways; |
| 178 | } |
| 179 | } |
| 180 |