laybuy-gateway-for-woocommerce.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /*
  3. Plugin Name: Laybuy Gateway for WooCommerce
  4. Description: Provide Laybuy as a payment option for WooCommerce orders.
  5. Author: Laybuy
  6. Author URI: https://www.laybuy.com/
  7. Version: 5.3.9
  8. Text Domain: laybuy-gateway-for-woocommerce
  9. License: GPL2
  10. Tested up to: 6.0.2
  11. WC requires at least: 3.0.0
  12. WC tested up to: 6.8.2
  13. Woocommerce Laybuy is free software: you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation, either version 2 of the License, or
  16. any later version.
  17. Woocommerce Laybuy is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with Woocommerce Laybuy. If not, see https://www.laybuy.com/.
  23. */
  24. if (!defined('ABSPATH')) {
  25. exit;
  26. }
  27. function woocommerce_laybuy_missing_wc_notice() {
  28. echo '<div class="error"><p><strong>' . sprintf( esc_html__( 'LayBuy requires WooCommerce to be installed and active. You can download %s here.', 'woocommerce-gateway-stripe' ), '<a href="https://woocommerce.com/" target="_blank">WooCommerce</a>' ) . '</strong></p></div>';
  29. }
  30. add_action( 'plugins_loaded', 'woocommerce_gateway_laybuy_init' );
  31. function woocommerce_gateway_laybuy_init() {
  32. if ( ! class_exists( 'WooCommerce' ) ) {
  33. add_action( 'admin_notices', 'woocommerce_laybuy_missing_wc_notice' );
  34. return;
  35. }
  36. }
  37. define( 'WC_LAYBUY_VERSION', '5.3.9' );
  38. define('WC_LAYBUY_PLUGIN_URL', plugin_dir_url(__FILE__));
  39. class WC_Laybuy
  40. {
  41. protected static $instance;
  42. public static $version = WC_LAYBUY_VERSION;
  43. private function __construct() {
  44. add_action('parse_request', array($this, 'laybuy_plugin_version'));
  45. add_filter( 'page_template', array($this, 'laybuy_page_template') );
  46. if (!defined('LAYBUY_MAIN_PATH')) {
  47. define('LAYBUY_MAIN_PATH', plugin_dir_path(__FILE__));
  48. }
  49. require_once dirname( __FILE__ ) . '/includes/constants.php';
  50. require_once dirname( __FILE__ ) . '/includes/wc-functions.php';
  51. require_once dirname( __FILE__ ) . '/includes/class-wc-laybuy-helper.php';
  52. require_once dirname( __FILE__ ) . '/includes/class-wc-laybuy-logger.php';
  53. require_once dirname( __FILE__ ) . '/includes/class-wc-gateway-laybuy.php';
  54. require_once dirname( __FILE__ ) . '/includes/Laybuy/ApiGateway.php';
  55. require_once dirname( __FILE__ ) . '/includes/Laybuy/ProcessManager.php';
  56. require_once dirname( __FILE__ ) . '/includes/Laybuy/SupportRequest.php';
  57. $gateway = WC_Payment_Gateway_Laybuy::getInstance();
  58. $settings = get_option( 'woocommerce_laybuy_settings', true );
  59. add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateways' ), 10, 1 );
  60. add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array($this, 'filter_action_links'), 10, 1 );
  61. add_filter( 'woocommerce_available_payment_gateways', array($gateway, 'check_cart_within_limits'), 99, 1 );
  62. if ($gateway->is_enabled()) {
  63. add_filter( 'woocommerce_currency', [$gateway, 'change_woocommerce_currency']);
  64. }
  65. add_action( 'wp_enqueue_scripts', array($this, 'init_website_assets'), 10, 0 );
  66. add_action( 'admin_enqueue_scripts', array($this, 'init_admin_assets'), 10, 0 );
  67. add_action( 'woocommerce_update_options_payment_gateways_laybuy', array( $gateway, 'process_admin_options' ) );
  68. add_action( 'woocommerce_settings_saved', array($this, 'wc_settings_saved'));
  69. add_action( 'woocommerce_review_order_before_payment', array($gateway, 'checkout_add_laybuy_paragraph'));
  70. add_shortcode( 'laybuy_iframe_src', array($gateway, 'shortcode_laybuy_iframe_src') );
  71. // add_shortcode( 'laybuy_paragraph', array($gateway, 'shortcode_laybuy_paragraph') );
  72. if (!$gateway->is_available()) {
  73. return;
  74. }
  75. // filters
  76. add_filter( 'woocommerce_create_order', array($gateway, 'create_order_quote'), 10, 2 );
  77. add_filter( 'woocommerce_new_order_data', array($gateway, 'filter_woocommerce_new_order_data'), 10, 1 );
  78. add_filter( 'checkout_modify_description', array($gateway, 'checkout_modify_description'), 10, 2 );
  79. if (WC_Laybuy_Helper::is_wc_gt('3.7')) {
  80. add_filter('woocommerce_get_price_html', array($gateway, 'filter_woocommerce_get_price_html'), 199, 2);
  81. } else {
  82. add_action( 'woocommerce_after_shop_loop_item_title', array($gateway, 'print_info_for_listed_products'), 199, 0 );
  83. }
  84. // actions
  85. add_action( 'woocommerce_cart_totals_after_order_total', array($gateway, 'render_schedule_on_cart_page'), 10, 0 );
  86. add_action( 'template_redirect', array( $gateway, 'process_redirect_order' ) );
  87. if (isset($gateway->settings['price_breakdown_option_product_page_position'])) {
  88. add_action(
  89. $gateway->settings['price_breakdown_option_product_page_position'],
  90. [$gateway, 'product_price_breakdown'],
  91. $gateway->getProductPriceBreakdownHookPriority()
  92. );
  93. }
  94. // shortcodes
  95. add_shortcode( 'laybuy_logo', array($gateway, 'shortcode_laybuy_logo') );
  96. add_action( 'wp_ajax_send_laybuy_support_request', array(new Laybuy_SupportRequest(), 'send'));
  97. }
  98. /**
  99. * Initialise the class and return an instance.
  100. *
  101. * Note: Hooked onto the "plugins_loaded" Action.
  102. *
  103. * @since 2.0.0
  104. * @uses self::load_classes()
  105. * @return WC_Laybuy
  106. */
  107. public static function init()
  108. {
  109. if (!defined('WC_VERSION')) {
  110. return;
  111. }
  112. if (version_compare( WC_VERSION, '3.9', '>=' )) {
  113. if (!file_exists(WC()->plugin_path() .'/vendor/autoload.php')) {
  114. return;
  115. }
  116. }
  117. require_once dirname( __FILE__ ) . '/includes/Laybuy/Plugin/UpdateManager.php';
  118. $laybuyCurrentPluginVersion = get_option('wc_laybuy_version');
  119. $pluginUpdateManager = new Laybuy_Plugin_UpdateManager();
  120. $pluginUpdateManager->setPluginVersion(WC_LAYBUY_VERSION)
  121. ->setPluginDbVersion($laybuyCurrentPluginVersion)
  122. ->update();
  123. if (is_null(self::$instance)) {
  124. self::$instance = new self;
  125. }
  126. return self::$instance;
  127. }
  128. public function laybuy_page_template($page_template) {
  129. if ( is_page( 'laybuy' ) ) {
  130. $page_template = dirname( __FILE__ ) . '/templates/laybuy-page-template.php';
  131. }
  132. return $page_template;
  133. }
  134. /**
  135. * Add the gateways to WooCommerce.
  136. *
  137. * @since 1.0.0
  138. * @version 4.0.0
  139. */
  140. public function add_gateways($methods) {
  141. if (isset($_GET['debug'])) {
  142. var_dump('add laybuy gateway');
  143. }
  144. $methods[] = 'WC_Payment_Gateway_LayBuy';
  145. return $methods;
  146. }
  147. /**
  148. * Note: Hooked onto the "plugin_action_links_laybuy-gateway-for-woocommerce/laybuy-gateway-for-woocommerce.php" Action.
  149. *
  150. * @since 2.0.0
  151. * @see self::__construct() For hook attachment.
  152. * @param array $links
  153. * @return array
  154. */
  155. public function filter_action_links($links)
  156. {
  157. $additional_links = array(
  158. '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=checkout&section=laybuy' ) . '">' . __( 'Settings', 'woo_laybuy' ) . '</a>',
  159. );
  160. return array_merge($additional_links, $links);
  161. }
  162. /**
  163. * Note: Hooked onto the "admin_enqueue_scripts" Action.
  164. *
  165. * @since 2.0.0
  166. * @see self::__construct() For hook attachment.
  167. */
  168. public function init_admin_assets()
  169. {
  170. wp_enqueue_script( 'laybuy_admin_js', plugins_url( 'assets/js/laybuy-admin.js', __FILE__ ), array(), WC_LAYBUY_VERSION );
  171. wp_enqueue_style('laybuy_admin_css', plugins_url( 'assets/css/laybuy_admin.css', __FILE__ ), array(), WC_LAYBUY_VERSION );
  172. }
  173. public function init_website_assets() {
  174. wp_enqueue_style( 'laybuy_css', plugins_url( 'assets/css/laybuy.css', __FILE__ ), array(), WC_LAYBUY_VERSION );
  175. }
  176. public function laybuy_plugin_version() {
  177. if($_SERVER["REQUEST_URI"] == '/laybuy_version') {
  178. echo "Laybuy version: " . self::$version;
  179. exit();
  180. }
  181. }
  182. public function wc_settings_saved()
  183. {
  184. $settings = (array) get_option( 'woocommerce_laybuy_settings', true );
  185. $laybuyPage = get_page_by_path( 'laybuy' );
  186. if ($settings['laybuy_page_enabled'] == 'yes') {
  187. if (!$laybuyPage)
  188. wp_insert_post( array(
  189. 'post_title' => 'How Laybuy works',
  190. 'post_content' => '',
  191. 'post_status' => 'publish',
  192. 'post_author' => 1,
  193. 'post_type' => 'page',
  194. 'post_name' => 'laybuy'
  195. ));
  196. } else if ($settings['laybuy_page_enabled'] == 'no') {
  197. if ($laybuyPage)
  198. wp_delete_post(intval($laybuyPage->ID), true);
  199. }
  200. }
  201. }
  202. add_action( 'plugins_loaded', array('WC_Laybuy', 'init'), 10, 0 );