| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Ziina |
| 4 |
* Description: Accept payments via Ziina |
| 5 |
* Author: Ziina |
| 6 |
* Author URI: https://ziina.com/ |
| 7 |
* Text Domain: ziina |
| 8 |
* Domain Path: /languages |
| 9 |
* WC requires at least: 4.8 |
| 10 |
* WC tested up to: 8.7.0 |
| 11 |
* Requires at least: 5.7 |
| 12 |
* Requires PHP: 8.1 |
| 13 |
* Version: 1.2.21 |
| 14 |
* |
| 15 |
* @package ZiinaPayment |
| 16 |
*/ |
| 17 |
|
| 18 |
namespace ZiinaPayment; |
| 19 |
|
| 20 |
defined( 'ABSPATH' ) || exit(); |
| 21 |
|
| 22 |
/** |
| 23 |
* Class Main |
| 24 |
* |
| 25 |
* @package ZiinaPayment |
| 26 |
* @since 1.0.0 |
| 27 |
*/ |
| 28 |
class Main { |
| 29 |
/** |
| 30 |
* Class instance |
| 31 |
* |
| 32 |
* @var Main|null |
| 33 |
*/ |
| 34 |
private static $instance = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* Plugin id |
| 38 |
* |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
public $plugin_id = 'ziina'; |
| 42 |
|
| 43 |
/** |
| 44 |
* Plugin version |
| 45 |
* |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
public $version = '1.2.21'; |
| 49 |
|
| 50 |
/** |
| 51 |
* Plugin url |
| 52 |
* |
| 53 |
* @var string |
| 54 |
*/ |
| 55 |
public $plugin_url; |
| 56 |
|
| 57 |
/** |
| 58 |
* Plugin path |
| 59 |
* |
| 60 |
* @var string |
| 61 |
*/ |
| 62 |
public $plugin_path; |
| 63 |
|
| 64 |
/** |
| 65 |
* Assets url |
| 66 |
* |
| 67 |
* @var string |
| 68 |
*/ |
| 69 |
public $assets_url; |
| 70 |
|
| 71 |
/** |
| 72 |
* Settings array |
| 73 |
* |
| 74 |
* @var array|null |
| 75 |
*/ |
| 76 |
private $settings = null; |
| 77 |
|
| 78 |
/** |
| 79 |
* Admin class instance |
| 80 |
* |
| 81 |
* @var Api\Main|null |
| 82 |
*/ |
| 83 |
private $api = null; |
| 84 |
|
| 85 |
/** |
| 86 |
* Admin class instance |
| 87 |
* |
| 88 |
* @var Ajax\Main|null |
| 89 |
*/ |
| 90 |
private $ajax; |
| 91 |
|
| 92 |
/** |
| 93 |
* Apple Pay domain verification handler. |
| 94 |
* |
| 95 |
* @var ApplePay\ApplePayDomainVerificationHandler|null |
| 96 |
*/ |
| 97 |
private $apple_pay_domain_verification_handler = null; |
| 98 |
|
| 99 |
/** |
| 100 |
* Main constructor. |
| 101 |
*/ |
| 102 |
private function __construct() { |
| 103 |
$this->plugin_url = plugin_dir_url( __FILE__ ); |
| 104 |
$this->plugin_path = plugin_dir_path( __FILE__ ); |
| 105 |
$this->assets_url = $this->plugin_url . '/assets/'; |
| 106 |
|
| 107 |
require_once __DIR__ . '/vendor/autoload.php'; |
| 108 |
|
| 109 |
load_plugin_textdomain( 'ziina', false, $this->plugin_path . 'languages/' ); |
| 110 |
|
| 111 |
add_action('plugins_loaded', function() { |
| 112 |
set_error_handler([$this, 'handle_error'], E_ALL); |
| 113 |
register_shutdown_function([$this, 'handle_fatal_error']); |
| 114 |
}); |
| 115 |
|
| 116 |
add_action( 'plugins_loaded', array( $this, 'action_plugins_loaded' ) ); |
| 117 |
add_action( 'before_woocommerce_init', array( $this, 'before_woocommerce_hpos' ) ); |
| 118 |
|
| 119 |
register_activation_hook( __FILE__, array( $this, 'activation_hook' ) ); |
| 120 |
register_deactivation_hook( __FILE__, array( $this, 'deactivation_hook' ) ); |
| 121 |
|
| 122 |
add_filter( 'woocommerce_payment_gateways', array( $this, 'filter_add_payment_gateway' ) ); |
| 123 |
|
| 124 |
$this->ajax = new Ajax\Main(); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get api class instance. |
| 129 |
* |
| 130 |
* @return Api\Main |
| 131 |
*/ |
| 132 |
public function api(): Api\Main { |
| 133 |
if ( is_null( $this->api ) ) { |
| 134 |
$this->api = new Api\Main(); |
| 135 |
} |
| 136 |
|
| 137 |
return $this->api; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Get Ajax class instance |
| 142 |
* |
| 143 |
* @return Ajax\Main |
| 144 |
*/ |
| 145 |
public function ajax(): Ajax\Main { |
| 146 |
return $this->ajax; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Initialize Apple Pay domain verification handler. |
| 151 |
* |
| 152 |
* @return ApplePay\ApplePayDomainVerificationHandler |
| 153 |
*/ |
| 154 |
public function init_apple_pay_domain_verification(): ApplePay\ApplePayDomainVerificationHandler { |
| 155 |
if ( is_null( $this->apple_pay_domain_verification_handler ) ) { |
| 156 |
$this->apple_pay_domain_verification_handler = new ApplePay\ApplePayDomainVerificationHandler(); |
| 157 |
} |
| 158 |
|
| 159 |
return $this->apple_pay_domain_verification_handler; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Disable plugin if Woocommerce not active |
| 164 |
*/ |
| 165 |
public function action_plugins_loaded() { |
| 166 |
if ( ! class_exists( 'WC_Payment_Gateway' ) ) { |
| 167 |
$this->disable_plugin(); |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
new Integrations\Main(); |
| 172 |
new Frontend\EmbeddedCheckout(); |
| 173 |
$this->init_apple_pay_domain_verification(); |
| 174 |
|
| 175 |
if ( is_admin() ) { |
| 176 |
new Admin\OrderDetails(); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Declare compatibility with WooCommerce High-Performance Order Storage. |
| 182 |
*/ |
| 183 |
public function before_woocommerce_hpos() { |
| 184 |
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { |
| 185 |
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Add plugin gateway to Woo gateways list |
| 191 |
* |
| 192 |
* @param array $gateways Array of gateway classes or class names. |
| 193 |
* |
| 194 |
* @return array |
| 195 |
*/ |
| 196 |
public function filter_add_payment_gateway( array $gateways ): array { |
| 197 |
$gateways[] = Gateway::class; |
| 198 |
|
| 199 |
return $gateways; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Activation hook |
| 204 |
*/ |
| 205 |
public function activation_hook() { |
| 206 |
if ( ! class_exists( 'WC_Payment_Gateway' ) ) { |
| 207 |
wp_die( |
| 208 |
esc_html__( 'Ziina requires WooCommerce to be installed and active. Please install and activate WooCommerce first.', 'ziina' ), |
| 209 |
esc_html__( 'Plugin Activation Error', 'ziina' ), |
| 210 |
array( 'back_link' => true ) |
| 211 |
); |
| 212 |
} |
| 213 |
|
| 214 |
flush_rewrite_rules(); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Deactivation hook |
| 219 |
*/ |
| 220 |
public function deactivation_hook() { |
| 221 |
try { |
| 222 |
$this->api()->delete_webhook(); |
| 223 |
} catch ( \Throwable $e ) {} |
| 224 |
|
| 225 |
$gateway = $this->gateway(); |
| 226 |
if ( $gateway ) { |
| 227 |
$gateway->update_option( 'ziina_webhook_registered', false ); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Get Gateway setting |
| 233 |
* |
| 234 |
* @param string $name Setting key. |
| 235 |
* |
| 236 |
* @return mixed |
| 237 |
*/ |
| 238 |
public function get_setting( string $name ) { |
| 239 |
if ( is_null( $this->settings ) ) { |
| 240 |
$gateway = $this->gateway(); |
| 241 |
|
| 242 |
if ( empty( $gateway ) ) { |
| 243 |
$this->log( 'Get_settings without gateway' ); |
| 244 |
|
| 245 |
return null; |
| 246 |
} |
| 247 |
|
| 248 |
$this->settings = array(); |
| 249 |
|
| 250 |
$is_test = $gateway->get_option( 'is_test' ) === 'yes'; |
| 251 |
|
| 252 |
$this->settings['gateway_id'] = $gateway->id; |
| 253 |
$this->settings['enabled'] = $gateway->get_option( 'enabled' ) === 'yes'; |
| 254 |
$this->settings['authorization_token'] = $gateway->get_option( 'authorization_token' ); |
| 255 |
$this->settings['is_test'] = $is_test; |
| 256 |
$this->settings['logging'] = $is_test || $gateway->get_option( 'logging' ) === 'yes'; |
| 257 |
$this->settings['checkout_mode'] = $gateway->get_option( 'checkout_mode', Gateway::CHECKOUT_MODE_REDIRECT ); |
| 258 |
$this->settings['embedded_locale'] = $gateway->get_option( 'embedded_locale', '' ); |
| 259 |
} |
| 260 |
|
| 261 |
return $this->settings[ $name ] ?? null; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Get plugin gateway instance. |
| 266 |
* |
| 267 |
* @return Gateway|null Gateway instance. |
| 268 |
*/ |
| 269 |
public function gateway(): ?Gateway { |
| 270 |
if ( ! function_exists( 'WC' ) || ! WC()->payment_gateways() ) { |
| 271 |
return null; |
| 272 |
} |
| 273 |
|
| 274 |
return WC()->payment_gateways()->payment_gateways()[ $this->plugin_id ] ?? null; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Wrapper of wc_get_template function |
| 279 |
* |
| 280 |
* @param string $template Template name. |
| 281 |
* @param array $args Arguments. |
| 282 |
* @param bool $return Return or echo. Echo by default. |
| 283 |
* |
| 284 |
* @return bool|string |
| 285 |
*/ |
| 286 |
public function include_template( string $template, $args = array(), $return = false ) { |
| 287 |
if ( $return ) { |
| 288 |
ob_start(); |
| 289 |
} |
| 290 |
|
| 291 |
wc_get_template( |
| 292 |
$template, |
| 293 |
$args, |
| 294 |
'', |
| 295 |
$this->plugin_path . 'templates/' |
| 296 |
); |
| 297 |
|
| 298 |
if ( $return ) { |
| 299 |
return ob_get_clean(); |
| 300 |
} |
| 301 |
|
| 302 |
return true; |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Disable plugin |
| 307 |
*/ |
| 308 |
public function disable_plugin() { |
| 309 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 310 |
|
| 311 |
deactivate_plugins( $this->plugin_id . '/index.php' ); |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Add data to Woo logs |
| 316 |
* |
| 317 |
* @param array|string $data Data to add to logs. |
| 318 |
* @param string $code_source Source of log in code. |
| 319 |
*/ |
| 320 |
public function log( $data, string $code_source = '' ) { |
| 321 |
if ( ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || $this->settings['logging'] ) { |
| 322 |
if ( empty( $code_source ) ) { |
| 323 |
$backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 )[1]; //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
| 324 |
|
| 325 |
$code_source = isset( $backtrace['class'] ) ? $backtrace['class'] . '::' : ''; |
| 326 |
$code_source .= $backtrace['function'] ?? ''; |
| 327 |
} |
| 328 |
|
| 329 |
$data = array( |
| 330 |
'source' => $code_source, |
| 331 |
'data' => $data, |
| 332 |
); |
| 333 |
|
| 334 |
wc_get_logger()->debug( print_r( $data, true ), array( 'source' => $this->plugin_id ) );// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Get singleton instance |
| 340 |
* |
| 341 |
* @return Main |
| 342 |
*/ |
| 343 |
public static function get_instance(): Main { |
| 344 |
if ( is_null( self::$instance ) ) { |
| 345 |
self::$instance = new self(); |
| 346 |
} |
| 347 |
|
| 348 |
return self::$instance; |
| 349 |
} |
| 350 |
|
| 351 |
public function handle_error($errno, $errstr, $errfile, $errline) { |
| 352 |
if (stripos($errfile, 'ziina') !== false) { |
| 353 |
if (class_exists('\\ZiinaPayment\\Logger\\Main')) { |
| 354 |
\ZiinaPayment\Logger\Main::init($this->api()); |
| 355 |
\ZiinaPayment\Logger\Main::error("PHP error occured", [ |
| 356 |
'error_type' => $errno, |
| 357 |
'error_message' => $errstr, |
| 358 |
'error_file' => $errfile, |
| 359 |
'error_line' => $errline |
| 360 |
]); |
| 361 |
} else { |
| 362 |
// Fallback to WC logging if Logger not available |
| 363 |
$this->log("PHP error: $errstr in $errfile on line $errline", 'handle_error'); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
return false; |
| 368 |
} |
| 369 |
|
| 370 |
public function handle_fatal_error() { |
| 371 |
$error = error_get_last(); |
| 372 |
|
| 373 |
if ($error !== null && stripos($error['file'], 'ziina') !== false) { |
| 374 |
if (class_exists('\\ZiinaPayment\\Logger\\Main')) { |
| 375 |
\ZiinaPayment\Logger\Main::init($this->api()); |
| 376 |
\ZiinaPayment\Logger\Main::fatal("Fatal error", [ |
| 377 |
'error_type' => $error['type'], |
| 378 |
'error_message' => $error['message'], |
| 379 |
'error_file' => $error['file'], |
| 380 |
'error_line' => $error['line'] |
| 381 |
]); |
| 382 |
} else { |
| 383 |
// Fallback to WC logging if Logger not available |
| 384 |
$this->log("Fatal error: {$error['message']} in {$error['file']} on line {$error['line']}", 'handle_fatal_error'); |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
require_once __DIR__ . '/main-class-shortcut.php'; |
| 391 |
|
| 392 |
ziina_payment(); |
| 393 |
|