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.6 4.7.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / compatibility / WcMemberships / class-wcml-wc-memberships.php
woocommerce-multilingual / compatibility / WcMemberships Last commit date
Factory.php 1 month ago class-wcml-wc-memberships.php 1 month ago
class-wcml-wc-memberships.php
144 lines
1 <?php
2
3 use PHPUnit\Framework\ExpectationFailedException;
4
5 class WCML_WC_Memberships implements \IWPML_Action {
6
7 const SAVED_POST_PARENT = 'wcml_memberships_post_parent';
8 private $wp_api;
9
10 public function __construct( WPML_WP_API $wp_api ) {
11 $this->wp_api = $wp_api;
12 }
13
14 public function add_hooks() {
15 add_filter( 'wcml_endpoint_keys_to_options', [ $this, 'endpoint_keys_to_options' ] );
16 add_filter( 'wcml_register_endpoints_store_urls', [ $this, 'register_endpoints_store_urls' ] );
17 add_filter( 'wcml_endpoints_translation_controls', [ $this, 'register_translation_controls' ] );
18 add_filter( 'wc_memberships_members_area_my-memberships_actions', [ $this, 'filter_actions_links' ] );
19 add_filter( 'wpml_pre_parse_query', [ $this, 'save_post_parent' ] );
20 add_filter( 'wpml_post_parse_query', [ $this, 'restore_post_parent' ] );
21 add_filter( 'wc_memberships_rule_object_ids', [ $this, 'add_translated_object_ids' ] );
22 add_action( 'wp_enqueue_scripts', [ $this, 'load_assets' ] );
23
24 add_filter( 'woocommerce_order_get__wc_memberships_access_granted', [ $this, 'orderMemberships' ] );
25 }
26
27 public function filter_actions_links( $actions ) {
28 foreach ( $actions as $key => $action ) {
29 if ( 'view' === $key ) {
30 $membership_endpoints = $this->get_members_area_endpoint();
31 $actions[ $key ]['url'] = str_replace( $membership_endpoints['original'], $membership_endpoints['translated'], $action['url'] );
32 }
33 }
34
35 return $actions;
36 }
37
38 public function save_post_parent( $q ) {
39 if ( isset( $q->query_vars['post_type'] )
40 && in_array( 'wc_user_membership', (array) $q->query_vars['post_type'], true )
41 && ! empty( $q->query_vars['post_parent'] ) ) {
42 $q->query_vars[ self::SAVED_POST_PARENT ] = $q->query_vars['post_parent'];
43 }
44
45 return $q;
46 }
47
48 public function restore_post_parent( $q ) {
49 if ( isset( $q->query_vars[ self::SAVED_POST_PARENT ] ) ) {
50 $q->query_vars['post_parent'] = $q->query_vars[ self::SAVED_POST_PARENT ];
51 unset( $q->query_vars[ self::SAVED_POST_PARENT ] );
52 }
53
54 return $q;
55 }
56
57 public function add_translated_object_ids( $object_ids ) {
58 $result = [];
59 foreach ( $object_ids as $object_id ) {
60 $type = apply_filters( 'wpml_element_type', get_post_type( $object_id ) );
61 $trid = apply_filters( 'wpml_element_trid', null, $object_id, $type );
62 $translations = array_values( wp_list_pluck(
63 apply_filters( 'wpml_get_element_translations', [], $trid, $type ),
64 'element_id'
65 ) );
66
67 $result = array_merge( $result, [ $object_id ], $translations );
68 }
69
70 return array_values( array_unique( array_map( 'intval', $result ) ) );
71 }
72
73 public function load_assets() {
74 if ( wc_get_page_id( 'myaccount' ) === get_the_ID() ) {
75 $wcml_plugin_url = $this->wp_api->constant( 'WCML_PLUGIN_URL' );
76 $wcml_version = $this->wp_api->constant( 'WCML_VERSION' );
77 wp_register_script( 'wcml-members-js', $wcml_plugin_url . '/compatibility/res/js/wcml-members.js', [ 'jquery' ], $wcml_version, true );
78 wp_enqueue_script( 'wcml-members-js' );
79 wp_localize_script( 'wcml-members-js', 'wc_memberships_memebers_area_endpoint', $this->get_members_area_endpoint() );
80 }
81 }
82
83 private function get_members_area_endpoint() {
84 $endpoint = get_option( 'woocommerce_myaccount_members_area_endpoint', 'members-area' );
85 $string_context = WCML_Url_Translation::get_endpoints_string_context();
86 $translated_endpoint = apply_filters( 'wpml_translate_single_string', $endpoint, $string_context, 'members_area' );
87
88 return [
89 'original' => $endpoint,
90 'translated' => $translated_endpoint,
91 ];
92 }
93
94 public function orderMemberships( $memberships ) {
95 if ( ! doing_action( 'woocommerce_thankyou' ) ) {
96 return $memberships;
97 }
98
99 $relevantMemberships = [];
100 $wpmlCurrentLanguage = apply_filters( 'wpml_current_language', null );
101 foreach ( $memberships as $membershipId => $membershipData ) {
102 $userMembership = wc_memberships_get_user_membership( (int) $membershipId );
103 $membershipPlan = $userMembership ? $userMembership->get_plan() : null;
104
105 if ( null === $membershipPlan ) {
106 continue;
107 }
108
109 $membershipPlanLanguageDetails = apply_filters( 'wpml_element_language_details', null, [
110 'element_id' => $membershipPlan->id,
111 'element_type' => 'wc_membership_plan',
112 ] );
113
114 if (
115 null === $membershipPlanLanguageDetails
116 || $wpmlCurrentLanguage === $membershipPlanLanguageDetails->language_code
117 ) {
118 $relevantMemberships[ $membershipId ] = $membershipData;
119 }
120 }
121
122 return $relevantMemberships;
123 }
124
125 public function endpoint_keys_to_options( $endpoint_keys_to_options ) {
126 $endpoint_keys_to_options['members_area'] = 'woocommerce_myaccount_members_area_endpoint';
127 $endpoint_keys_to_options['profile_fields_area'] = 'woocommerce_myaccount_profile_fields_area_endpoint';
128 return $endpoint_keys_to_options;
129 }
130
131 public function register_endpoints_store_urls( $store_urls ) {
132 $store_urls['members_area'] = get_option( 'woocommerce_myaccount_members_area_endpoint', 'members-area' );
133 $store_urls['profile_fields_area'] = get_option( 'woocommerce_myaccount_profile_fields_area_endpoint', 'my-profile' );
134 return $store_urls;
135 }
136
137 public function register_translation_controls( $translation_controls ) {
138 $translation_controls['members_area'] = 'woocommerce_myaccount_members_area_endpoint';
139 $translation_controls['profile_fields_area'] = 'woocommerce_myaccount_profile_fields_area_endpoint';
140 return $translation_controls;
141 }
142
143 }
144