为什么我的WordPress过滤器与 AJAX 不工作与阿拉伯语蛞蝓在类别?

lfapxunr  于 2023-06-05  发布在  WordPress
关注(0)|答案(1)|浏览(400)

我有代码来过滤产品与ajax在wordpress,但当创建一个类别与阿拉伯语蛞蝓过滤器不www.example.com工程正确与英语www.example.com我可以解决这个问题?work.it works correctly with English slugs.how can i fix this?
帮助修复带有阿拉伯语slug的类别.................................................................................................................................................................................................................................................

// Post Filter
( function( theme, $ ) {
    'use strict';

    theme = theme || {};

    $.extend( theme, {

        PostFilter: {

            cache: {
            },

            defaults: {
                elements: '.portfolio-filter'
            },

            initialize: function( $elements, post_type ) {
                this.$elements = ( $elements || $( this.defaults.elements ) );
                this.build( post_type );

                return this;
            },

            filterFn: function( e ) {
                if ( typeof e == 'undefined' || typeof e.data == 'undefined' || typeof e.data.elements == 'undefined' || !e.data.elements || !e.data.elements.length ) {
                    return;
                }
                var self = e.data.selfobj;

                if ( self.isLoading ) {
                    return false;
                }
                var $this = e.data.thisobj,
                    $elements = e.data.elements,
                    position = e.data.position,
                    post_type = e.data.post_type,
                    $parent = e.data.parent,
                    $posts_wrap = e.data.posts_wrap,
                    use_ajax = e.data.use_ajax,
                    page_path = e.data.page_path,
                    infinite_load = e.data.infinite_load,
                    load_more = e.data.load_more;
                e.preventDefault();
                if ( $( this ).hasClass( 'active' ) ) {
                    return;
                }

                self.isLoading = true;
                var selector = $( this ).attr( 'data-filterS' );
                if ( 'sidebar' == position ) {
                    $( '.sidebar-overlay' ).trigger( 'click' );
                }

                $this.find( '.active' ).removeClass( 'active' );

                if ( use_ajax ) {
                    var current_cat = '*' == selector ? '' : selector;

                    if ( !page_path ) {
                        page_path = $posts_wrap.data( 'page_path' );
                    }
                    if ( page_path ) {
                        $posts_wrap.data( 'page_path', page_path.replace( /&category=[^&]*&/, '&category=' + current_cat + '&' ) );
                    }

                    $( this ).addClass( 'active' );
                    self.load_posts( current_cat, infinite_load || load_more ? true : false, $parent, post_type, $posts_wrap, undefined, $( this ).children( 'a' ).attr( 'href' ) );
                } else if ( 'faq' == post_type ) {
                    $parent.find( '.faq' ).each( function() {
                        var $that = $( this ), easing = "easeInOutQuart", timeout = 300;
                        if ( selector == '*' ) {
                            if ( $that.css( 'display' ) == 'none' ) $that.stop( true ).slideDown( timeout, easing, function() {
                                $( this ).attr( 'style', '' ).show();
                            } );
                            selected++;
                        } else {
                            if ( $that.hasClass( selector ) ) {
                                if ( $that.css( 'display' ) == 'none' ) $that.stop( true ).slideDown( timeout, easing, function() {
                                    $( this ).attr( 'style', '' ).show();
                                } );
                                selected++;
                            } else {
                                if ( $that.css( 'display' ) != 'none' ) $that.stop( true ).slideUp( timeout, easing, function() {
                                    $( this ).attr( 'style', '' ).hide();
                                } );
                            }
                        }
                    } );

                    if ( !selected && $parent.find( '.faqs-infinite' ).length && typeof ( $.fn.infinitescroll ) != 'undefined' ) {
                        $parent.find( '.faqs-infinite' ).infinitescroll( 'retrieve' );
                    }
                } else if ( $parent.hasClass( 'portfolios-timeline' ) ) {
                    var selected = 0;
                    $parent.find( '.portfolio' ).each( function() {
                        var $that = $( this ), easing = "easeInOutQuart", timeout = 300;
                        if ( selector == '*' ) {
                            if ( $that.css( 'display' ) == 'none' ) $that.stop( true ).slideDown( timeout, easing, function() {
                                $( this ).attr( 'style', '' ).show();
                            } );
                            selected++;
                        } else {
                            if ( $that.hasClass( selector ) ) {
                                if ( $that.css( 'display' ) == 'none' ) $that.stop( true ).slideDown( timeout, easing, function() {
                                    $( this ).attr( 'style', '' ).show();
                                } );
                                selected++;
                            } else {
                                if ( $that.css( 'display' ) != 'none' ) $that.stop( true ).slideUp( timeout, easing, function() {
                                    $( this ).attr( 'style', '' ).hide();
                                } );
                            }
                        }
                    } );
                    if ( !selected && $parent.find( '.portfolios-infinite' ).length && typeof ( $.fn.infinitescroll ) != 'undefined' ) {
                        $parent.find( '.portfolios-infinite' ).infinitescroll( 'retrieve' );
                    }
                    setTimeout( function() {
                        theme.FilterZoom.initialize( $parent );
                    }, 400 );
                } else {
                    $parent.find( '.' + post_type + '-row' ).isotope( {
                        filter: selector == '*' ? selector : '.' + selector
                    } );
                }

                if ( !use_ajax ) {
                    $( this ).addClass( 'active' );
                    self.isLoading = false;
                }

                if ( position == 'sidebar' ) {
                    self.$elements.each( function() {
                        var $that = $( this );

                        if ( $that == $this && $that.data( 'position' ) != 'sidebar' ) return;
                        $that.find( 'li' ).removeClass( 'active' );
                        $that.find( 'li[data-filter="' + selector + '"]' ).addClass( 'active' );
                    } );
                }

                if ( !use_ajax ) {
                    window.location.hash = '#' + selector;
                }
                theme.refreshVCContent();
                return false;
            },
s4n0splo

s4n0splo1#

您遇到了一个URL编码的经典问题。HTTP URL只能发送ASCII字符,阿拉伯字符不包括在ASCII集中。因此,非ASCII字符在发送之前需要进行百分比编码,然后在目的地进行解码。
实现这一点的一种方法是使用JavaScript的encodeURIComponent()函数对slug进行编码,该函数旨在对URL中安全使用的特殊字符进行编码。
在你传递slug的 AJAX 调用中,你应该首先对它进行编码:

var current_cat = '*' == selector ? '' : encodeURIComponent(selector);

现在,在服务器端,在WordPress AJAX 处理函数中,您需要在使用它之前解码这个URL编码的slug:

$slug = urldecode($_POST['slug']);

这应该会将百分比编码的字符串转换回阿拉伯字符。
请注意,这只是一个建议(我建议的解决方案),您可能需要根据您的具体情况进行调整。另外,确保您的数据库和PHP文件已正确配置为处理UTF-8字符。这意味着它们应该被设置为使用utf8mb4字符集。

相关问题