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 |