'', 'text' => 'Buy Now', 'class' => 'affloc-localized-link' ], $atts, 'affloc_link' ); if ( empty( $atts['slug'] ) ) return ''; $this->used_slugs[] = $atts['slug']; $mapping = Affloc_DB::get_mapping( $atts['slug'] ); $mapping = apply_filters( 'affloc_mapping', $mapping, $atts['slug'] ); $default_url = isset( $mapping['default'] ) ? $mapping['default'] : '#'; return sprintf( '%s', esc_url( $default_url ), esc_attr( $atts['class'] ), esc_attr( $atts['slug'] ), esc_html( $atts['text'] ) ); } /** * Enqueue frontend scripts. */ public function enqueue_scripts() { if ( ! is_singular() ) return; global $post; $shortcode_tag = get_option( 'affloc_custom_shortcode', 'affloc_link' ); if ( empty( $post ) || ( ! has_shortcode( $post->post_content, 'affloc_link' ) && ! has_shortcode( $post->post_content, $shortcode_tag ) ) ) { return; } wp_enqueue_script( 'affloc-frontend', AFFLOC_URL . 'assets/js/frontend.js', [], AFFLOC_VERSION, true ); wp_localize_script( 'affloc-frontend', 'afflocVars', [ 'restUrl' => esc_url_raw( rest_url( 'affloc/v1/country' ) ), 'nonce' => wp_create_nonce( 'wp_rest' ) ] ); } /** * Pass ONLY used slugs to the frontend via wp_add_inline_script. */ public function localize_mapping() { if ( empty( $this->used_slugs ) ) return; $this->used_slugs = array_unique( $this->used_slugs ); $mapping_data = []; foreach ( $this->used_slugs as $slug ) { $mapping_data[ $slug ] = Affloc_DB::get_mapping( $slug ); } wp_register_script( 'affloc-mapping-data', '', [], AFFLOC_VERSION, true ); wp_enqueue_script( 'affloc-mapping-data' ); wp_add_inline_script( 'affloc-mapping-data', 'window.affloc_mapping = ' . wp_json_encode( $mapping_data ) . ';' ); } /** * Register REST API endpoint for Geo detection. */ public function register_geo_endpoint() { register_rest_route( 'affloc/v1', '/country', [ 'methods' => 'GET', 'callback' => [ $this, 'get_geo_country' ], 'permission_callback' => '__return_true' ] ); } public function get_geo_country() { $ip = ''; if ( ! empty( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ) { $ip = sanitize_text_field( wp_unslash( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ); } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { $ips = explode( ',', sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) ); $ip = trim( $ips[0] ); } else { $ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; } if ( empty( $ip ) || ! filter_var( $ip, FILTER_VALIDATE_IP ) ) { $ip = '127.0.0.1'; } if ( $ip === '127.0.0.1' || $ip === '::1' ) { return new WP_REST_Response( [ 'country' => 'US' ], 200 ); } $response = wp_remote_get( "https://ipapi.co/{$ip}/json/" ); if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) { return new WP_REST_Response( [ 'country' => 'default' ], 200 ); } $body = json_decode( wp_remote_retrieve_body( $response ), true ); $country = isset( $body['country_code'] ) ? $body['country_code'] : ( isset( $body['countryCode'] ) ? $body['countryCode'] : 'default' ); return new WP_REST_Response( [ 'country' => apply_filters( 'affloc_detected_country', $country ) ], 200 ); } }