Factory.php
3 weeks ago
MulticurrencyHooks.php
3 weeks ago
SharedHooks.php
3 weeks ago
class-wcml-wc-subscriptions.php
3 weeks ago
class-wcml-wc-subscriptions.php
201 lines
| 1 | <?php |
| 2 | |
| 3 | class WCML_WC_Subscriptions implements \IWPML_Action { |
| 4 | |
| 5 | const TRANSLATION_DOMAIN = 'woocommerce_subscriptions'; |
| 6 | |
| 7 | private $woocommerce_wpml; |
| 8 | |
| 9 | private $wpdb; |
| 10 | |
| 11 | private $sitepress; |
| 12 | |
| 13 | private $url_converter; |
| 14 | |
| 15 | public function __construct( woocommerce_wpml $woocommerce_wpml, wpdb $wpdb, SitePress $sitepress, WPML_URL_Converter $url_converter ) { |
| 16 | $this->woocommerce_wpml = $woocommerce_wpml; |
| 17 | $this->wpdb = $wpdb; |
| 18 | $this->sitepress = $sitepress; |
| 19 | $this->url_converter = $url_converter; |
| 20 | } |
| 21 | |
| 22 | public function add_hooks() { |
| 23 | add_action( 'init', [ $this, 'init' ], 9 ); |
| 24 | add_filter( 'wcml_variation_term_taxonomy_ids', [ $this, 'wcml_variation_term_taxonomy_ids' ] ); |
| 25 | add_filter( 'wcml_endpoint_keys_to_options', [ $this, 'endpoint_keys_to_options' ] ); |
| 26 | add_filter( 'wcml_register_endpoints_store_urls', [ $this, 'register_endpoints_store_urls' ] ); |
| 27 | add_filter( 'wcml_endpoints_translation_controls', [ $this, 'register_translation_controls' ] ); |
| 28 | add_filter( 'woocommerce_subscription_lengths', [ $this, 'woocommerce_subscription_lengths' ], 10, 2 ); |
| 29 | |
| 30 | add_action( 'woocommerce_subscriptions_product_options_pricing', [ $this, 'show_pointer_info' ] ); |
| 31 | add_action( 'woocommerce_variable_subscription_pricing', [ $this, 'show_pointer_info' ] ); |
| 32 | |
| 33 | add_filter( 'wcml_xliff_allowed_variations_types', [ $this, 'set_allowed_variations_types_in_xliff' ] ); |
| 34 | |
| 35 | add_filter( 'wcml_emails_options_to_translate', [ $this, 'translate_email_options' ] ); |
| 36 | add_filter( 'wcml_emails_section_name_prefix', [ $this, 'email_option_section_prefix' ], 10, 2 ); |
| 37 | |
| 38 | if ( WPML_LANGUAGE_NEGOTIATION_TYPE_DOMAIN === (int) $this->sitepress->get_setting( 'language_negotiation_type' ) ) { |
| 39 | add_filter( 'wc_subscriptions_site_url', [ $this, 'allowing_different_domains' ], 10, 4 ); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | public function init() { |
| 44 | if ( ! is_admin() ) { |
| 45 | add_filter( 'wcml_should_translate_order_items', [ $this, 'translateSubscriptionProductItems' ], 10, 3 ); |
| 46 | } |
| 47 | |
| 48 | add_filter( 'woocommerce_generated_manual_renewal_order_renewal_notification', [ $this, 'translate_renewal_notification' ], 9 ); |
| 49 | add_filter( 'woocommerce_order_status_failed_renewal_notification', [ $this, 'translate_renewal_notification' ], 9 ); |
| 50 | } |
| 51 | |
| 52 | public function wcml_variation_term_taxonomy_ids( $get_variation_term_taxonomy_ids ) { |
| 53 | |
| 54 | $get_variation_term_taxonomy_id = $this->wpdb->get_var( "SELECT tt.term_taxonomy_id FROM {$this->wpdb->terms} AS t LEFT JOIN {$this->wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE t.slug = 'variable-subscription'" ); |
| 55 | |
| 56 | if ( ! empty( $get_variation_term_taxonomy_id ) ) { |
| 57 | $get_variation_term_taxonomy_ids[] = $get_variation_term_taxonomy_id; |
| 58 | } |
| 59 | |
| 60 | return $get_variation_term_taxonomy_ids; |
| 61 | } |
| 62 | |
| 63 | public function woocommerce_subscription_lengths( $subscription_ranges, $subscription_period ) { |
| 64 | |
| 65 | if ( is_array( $subscription_ranges ) ) { |
| 66 | foreach ( $subscription_ranges as $period => $ranges ) { |
| 67 | if ( is_array( $ranges ) ) { |
| 68 | foreach ( $ranges as $range ) { |
| 69 | if ( '9 months' === $range ) { |
| 70 | $breakpoint = true; |
| 71 | } |
| 72 | $new_subscription_ranges[ $period ][] = apply_filters( 'wpml_translate_single_string', $range, 'wc_subscription_ranges', $range ); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return isset( $new_subscription_ranges ) ? $new_subscription_ranges : $subscription_ranges; |
| 79 | } |
| 80 | |
| 81 | public function show_pointer_info() { |
| 82 | $pointerFactory = new WCML\PointerUi\Factory(); |
| 83 | $pointerFactory |
| 84 | ->create( [ |
| 85 | 'content' => sprintf( |
| 86 | /* translators: %1$s and %2$s are opening and closing HTML link tags */ |
| 87 | esc_html__( 'You can translate text for subscription products from the %1$sTranslation Dashboard%2$s.', 'woocommerce-multilingual' ), |
| 88 | '<a href="' . esc_url( \WCML\Utilities\AdminUrl::getWPMLTMDashboardProducts() ) . '">', |
| 89 | '</a>' |
| 90 | ), |
| 91 | 'selectorId' => 'general_product_data .subscription_pricing', |
| 92 | 'method' => 'prepend', |
| 93 | 'docLink' => WCML_Tracking_Link::getWcmlSubscriptionsDoc(), |
| 94 | ] ) |
| 95 | ->show(); |
| 96 | } |
| 97 | |
| 98 | public function set_allowed_variations_types_in_xliff( $allowed_types ) { |
| 99 | |
| 100 | $allowed_types[] = 'variable-subscription'; |
| 101 | $allowed_types[] = 'subscription_variation'; |
| 102 | |
| 103 | return $allowed_types; |
| 104 | } |
| 105 | |
| 106 | public function translate_renewal_notification( $order_id ) { |
| 107 | |
| 108 | if ( isset( WC()->mailer()->emails['WCS_Email_Customer_Renewal_Invoice'] ) ) { |
| 109 | $this->woocommerce_wpml->emails->refresh_email_lang( $order_id ); |
| 110 | |
| 111 | $WCS_Email_Customer_Renewal_Invoice = WC()->mailer()->emails['WCS_Email_Customer_Renewal_Invoice']; |
| 112 | $WCS_Email_Customer_Renewal_Invoice->heading = __( $WCS_Email_Customer_Renewal_Invoice->heading, 'woocommerce-subscriptions' ); |
| 113 | $WCS_Email_Customer_Renewal_Invoice->subject = __( $WCS_Email_Customer_Renewal_Invoice->subject, 'woocommerce-subscriptions' ); |
| 114 | |
| 115 | add_filter( 'woocommerce_email_get_option', [ $this, 'translate_heading_subject' ], 10, 4 ); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public function translate_heading_subject( $return_value, $obj, $value, $key ) { |
| 120 | |
| 121 | if ( $obj instanceof WCS_Email_Customer_Renewal_Invoice ) { |
| 122 | if ( 'subject' === $key || 'heading' === $key ) { |
| 123 | $translated_admin_string = $this->woocommerce_wpml->emails->getStringTranslation( 'admin_texts_woocommerce_customer_renewal_invoice_settings', '[woocommerce_customer_renewal_invoice_settings]' . $key ); |
| 124 | return empty( $translated_admin_string ) ? $return_value : $translated_admin_string; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return $return_value; |
| 129 | } |
| 130 | |
| 131 | public function translate_email_options( $emails_options ) { |
| 132 | |
| 133 | if ( is_array( $emails_options ) ) { |
| 134 | $emails_options[] = 'woocommerce_customer_renewal_invoice_settings'; |
| 135 | } |
| 136 | |
| 137 | return $emails_options; |
| 138 | } |
| 139 | |
| 140 | public function email_option_section_prefix( $section_prefix, $emails_option ) { |
| 141 | |
| 142 | if ( 'woocommerce_customer_renewal_invoice_settings' === $emails_option ) { |
| 143 | return 'wcs_email_'; |
| 144 | } |
| 145 | |
| 146 | return $section_prefix; |
| 147 | } |
| 148 | |
| 149 | public function translateSubscriptionProductItems( $translateOrderItems, $items, $order ) { |
| 150 | if ( $order instanceof WC_Subscription ) { |
| 151 | return true; |
| 152 | } |
| 153 | return $translateOrderItems; |
| 154 | } |
| 155 | |
| 156 | public function allowing_different_domains( $url, $path, $scheme, $blog_id ) { |
| 157 | |
| 158 | $domains = $this->sitepress->get_setting( 'language_domains' ) ?: []; |
| 159 | |
| 160 | $default_domain = $this->url_converter->get_abs_home(); |
| 161 | array_unshift( $domains, $default_domain ); |
| 162 | |
| 163 | $normalized_domains = array_map( function( $d ) { |
| 164 | return wp_parse_url( $d, PHP_URL_HOST ) ?: $d; |
| 165 | }, $domains ); |
| 166 | |
| 167 | $default_domain_host = wp_parse_url( $url, PHP_URL_HOST ) ?: $url; |
| 168 | $current_domain_host = wp_parse_url( home_url(), PHP_URL_HOST ) ?: home_url(); |
| 169 | |
| 170 | if ( in_array( $default_domain_host, $normalized_domains, true ) && in_array( $current_domain_host, $normalized_domains, true ) ) { |
| 171 | $scheme = wp_is_using_https() ? 'https' : 'http'; |
| 172 | return ( wp_parse_url( $url, PHP_URL_SCHEME ) ?: $scheme ) . '://' . $current_domain_host; |
| 173 | } |
| 174 | |
| 175 | return $url; |
| 176 | |
| 177 | } |
| 178 | |
| 179 | public function endpoint_keys_to_options( $endpoint_keys_to_options ) { |
| 180 | $endpoint_keys_to_options['view-subscription'] = 'woocommerce_myaccount_view_subscription_endpoint'; |
| 181 | $endpoint_keys_to_options['subscriptions'] = 'woocommerce_myaccount_subscriptions_endpoint'; |
| 182 | $endpoint_keys_to_options['subscription-payment-method'] = 'woocommerce_myaccount_subscription_payment_method_endpoint'; |
| 183 | return $endpoint_keys_to_options; |
| 184 | } |
| 185 | |
| 186 | public function register_endpoints_store_urls( $store_urls ) { |
| 187 | $store_urls['view-subscription'] = get_option( 'woocommerce_myaccount_view_subscription_endpoint', 'view-subscription' ); |
| 188 | $store_urls['subscriptions'] = get_option( 'woocommerce_myaccount_subscriptions_endpoint', 'subscriptions' ); |
| 189 | $store_urls['subscription-payment-method'] = get_option( 'woocommerce_myaccount_subscription_payment_method_endpoint', 'subscription-payment-method' ); |
| 190 | return $store_urls; |
| 191 | } |
| 192 | |
| 193 | public function register_translation_controls( $translation_controls ) { |
| 194 | $translation_controls['view-subscription'] = 'woocommerce_myaccount_view_subscription_endpoint'; |
| 195 | $translation_controls['subscriptions'] = 'woocommerce_myaccount_subscriptions_endpoint'; |
| 196 | $translation_controls['subscription-payment-method'] = 'woocommerce_myaccount_subscription_payment_method_endpoint'; |
| 197 | return $translation_controls; |
| 198 | } |
| 199 | |
| 200 | } |
| 201 |