繁体   English   中英

如何在Wordpress中加速admin-ajax.php

[英]How to speed up admin-ajax.php in wordpress

我在前端通过ajax调用过的functions.php上有一些代码,我正在使用admin-ajax进行加载,加载时间约为2-2.5秒,因为我有很多插件,而ajax处理程序需要加载所有wp核心和插件数据,我想知道如何编写一个自定义的Ajax处理程序,该处理程序仅加载运行以下代码段所需的内容,我的代码摘要是它通过获取元数据获取woocommerce自定义字段,并读取HTTP标头以进行地理位置定位并实现一些我定义的变量以创建带有链接的按钮。

function simple_amz_link_ajax() {   
    ?>
    <script>
        jQuery(document).ready(function(){

            jQuery.ajax({
                url: "<?php echo admin_url('admin-ajax.php?action=get_amz_btn'); ?>",
                type: 'POST',
                data: {
                        action: 'get_simple_amz_button',
                        postId: <?php echo get_post()->ID; ?>
                },
                dataType: 'html',
                success: function(response) {
                jQuery("#buy_amz_btn_wrap").html(response);

                }

            }); 
        });
    </script> 
    <!-- end Ajax call to get_simple_amz_button -->

    <div id="buy_amz_btn_wrap">

    <div class="spinner">

      <div class="bounce1"></div>
      <div class="bounce2"></div>
      <div class="bounce3"></div>
    </div>

        </div>
    <?php
}



add_action('wp_ajax_get_simple_amz_button', 'simple_amz_button');
add_action('wp_ajax_nopriv_get_simple_amz_button', 'simple_amz_button');

function simple_amz_button() {  
// Variables Declaration
    $postId = filter_input( INPUT_POST, 'postId', FILTER_SANITIZE_NUMBER_INT );
    $de_asin = get_post_meta( $postId, "wccaf_de_asin", true );

    $country_code = $_SERVER ["HTTP_CF_IPCOUNTRY"];
    $not_avilable_country = '<div id="amz_not_avilable" class="amz_not_avilable">This product is not avilable in your country yet</div>';

    // Get Amazon Button Title  
    if (ICL_LANGUAGE_CODE == "de") {
        $amz_btn_title = 'Kaufen auf Amazon'; 
        $not_avilable_country = '<div id="amz_not_avilable" class="amz_not_avilable">Dieses Produkt ist in Ihrem Land noch nicht verfügbar</div>';
    }
    if (ICL_LANGUAGE_CODE == "en")  {
        $amz_btn_title = 'Buy on Amazon'; 
        $not_avilable_country = '<div id="amz_not_avilable" class="amz_not_avilable">This product is not avilable in your country yet</div>';
    }
        //////////////////////////////////////////////
    // Geolocation Condition
    if ($country_code=="DE" or $country_code=="DE" or $country_code=="AT" or $country_code=="CH" or $country_code=="LI" or $country_code=="EG") {
        $associate_id = "bonstato-21";
        $access_key = "HDUHWUIDIUWJDWDWDWD";
        $secret_key = "HDUIWQDUQWUDJUIQJWDJWQD";
        $amazon_domain = "amazon.de";
        $asin = $de_asin;
    }



    /**********************************************************************************/

    // Get price from amazon

    $amazon = new AmazonAPI($associate_id , $access_key, $secret_key , $amazon_domain);
    $item = $amazon->item_lookup($asin)->get_item_data();
    if ($item->price != "0" && $item->price != null ) {
    ?><div class="amz_price_wrap_wrap" >Price: <?php echo $item->price; ?></div><?php
    }

    global $post;
    $product = wc_get_product( $postId );
    $type = $product->get_type();
    if( $type == 'simple' && $item->price != "0"  && $item->price != null ){    
        if( wp_is_mobile() ) {
            // Amazon Link For Mobile       
            ?>
            <div class="buy_amz_btn_wrap" >     
            <button type="button" id="buy_amz_btn" class="buy_amz_btn" onclick="window.location='https://<?php echo $amazon_domain ?>/dp/<?php echo $asin ?>/?tag=<?php echo $associate_id ?>';"><i class="fa fa-amazon fa-amz"></i><?php echo $amz_btn_title ?></button>                        
            </div>
            <?php
        }

        else {
            // Amazon Link For PC
            ?>
             <div class="buy_amz_btn_wrap" >    
            <button type="button" id="buy_amz_btn" class="buy_amz_btn" onclick="window.location='https://<?php echo $amazon_domain ?>/gp/aws/cart/add.html?AssociateTag=<?php echo $associate_id ?>&ASIN.1=<?php echo $asin ?>&Quantity.1=1';"><i class="fa fa-amazon fa-amz"></i><?php echo $amz_btn_title ?></button>                          
            </div>
            <?php 
        }
    }

    else if( $type == 'simple' && $item->price == "0"){  
        echo $not_avilable_country;
    }


    if(is_null($item->price)){   
        echo $not_avilable_country;
    }





die(); 

} 

看起来您仍然需要在Ajax操作上使用core和WooCommerce函数。 因此,基本上,您要做的是使用一个单独的动作,该动作不考虑与wp_ajax_*挂钩的其他动作。 不知道这是否可以节省您的时间,但是您可以尝试:

<?php
function simple_amz_link_ajax() {
    ?>
    <script>
        jQuery( document ).on( 'ready', function () {
            jQuery.ajax( {
                url: "<?php echo esc_url( home_url( '?amz=get_amz_btn' ) ); ?>",
                type: 'POST',
                data: {
                    postId: <?php echo absint( get_the_ID() ); ?>,
                    nonce: <?php echo wp_create_nonce( 'get_amz_btn' ); ?>,
                },
                dataType: 'html',
                success: function( response ) {
                    jQuery( '#buy_amz_btn_wrap' ).html( response );
                }
            } ); 
        } );
    </script> 
    <!-- end Ajax call to get_simple_amz_button -->

    <div id="buy_amz_btn_wrap">
        <div class="spinner">
            <div class="bounce1"></div>
            <div class="bounce2"></div>
            <div class="bounce3"></div>
        </div>
    </div>
    <?php
}
add_action( 'wp_footer', 'simple_amz_link_ajax' );

function simple_amz_button() {
    if ( check_ajax_referer( 'get_amz_btn', 'nonce', false ) && isset( $_GET['amz'] ) && 'get_amz_btn' === $_GET['amz'] ) {
        // Variables Declaration.
        $postId  = filter_input( INPUT_POST, 'postId', FILTER_SANITIZE_NUMBER_INT );
        $de_asin = get_post_meta( $postId, 'wccaf_de_asin', true );

        $country_code         = $_SERVER ['HTTP_CF_IPCOUNTRY'];
        $not_avilable_country = '<div id="amz_not_avilable" class="amz_not_avilable">This product is not avilable in your country yet</div>';

        // Get Amazon Button Title.
        if ( 'de' === ICL_LANGUAGE_CODE ) {
            $amz_btn_title        = 'Kaufen auf Amazon'; 
            $not_avilable_country = '<div id="amz_not_avilable" class="amz_not_avilable">Dieses Produkt ist in Ihrem Land noch nicht verfügbar</div>';
        } elseif ( 'en' === ICL_LANGUAGE_CODE )  {
            $amz_btn_title        = 'Buy on Amazon'; 
            $not_avilable_country = '<div id="amz_not_avilable" class="amz_not_avilable">This product is not avilable in your country yet</div>';
        }

        // Geolocation Condition.
        if ( 'DE' === $country_code || 'AT' === $country_code || 'CH' === $country_code || 'LI' === $country_code || 'EG' === ) {
            $associate_id  = 'bonstato-21';
            $access_key    = 'HDUHWUIDIUWJDWDWDWD';
            $secret_key    = 'HDUIWQDUQWUDJUIQJWDJWQD';
            $amazon_domain = 'amazon.de';
            $asin          = $de_asin;
        }

        // Get price from amazon
        $amazon = new AmazonAPI( $associate_id , $access_key, $secret_key , $amazon_domain );
        $item   = $amazon->item_lookup( $asin )->get_item_data();

        if ( 0 != $item->price && null != $item->price ) {
            ?>
            <div class="amz_price_wrap_wrap" >Price: <?php echo $item->price; ?></div>
            <?php
        }

        global $post;

        $product = wc_get_product( $postId );
        $type    = $product->get_type();

        if ( 'simple' === $type && 0 != $item->price && null != $item->price ) {    
            if ( wp_is_mobile() ) {
                // Amazon Link For Mobile       
                ?>
                <div class="buy_amz_btn_wrap" >     
                    <button type="button" id="buy_amz_btn" class="buy_amz_btn" onclick="window.location='https://<?php echo $amazon_domain ?>/dp/<?php echo $asin ?>/?tag=<?php echo $associate_id ?>';"><i class="fa fa-amazon fa-amz"></i><?php echo $amz_btn_title ?></button>                        
                </div>
                <?php
            } else {
                // Amazon Link For PC
                ?>
                <div class="buy_amz_btn_wrap" >    
                    <button type="button" id="buy_amz_btn" class="buy_amz_btn" onclick="window.location='https://<?php echo $amazon_domain ?>/gp/aws/cart/add.html?AssociateTag=<?php echo $associate_id ?>&ASIN.1=<?php echo $asin ?>&Quantity.1=1';"><i class="fa fa-amazon fa-amz"></i><?php echo $amz_btn_title ?></button>                          
                </div>
                <?php 
            }
        } elseif( 'simple' === $type && 0 == $item->price ) {  
            echo $not_avilable_country;
        }

        if ( is_null( $item->price ) ) {   
            echo $not_avilable_country;
        }

        die(); 
    }
}
add_action( 'template_redirect', 'simple_amz_button' );

免责声明:此代码未经测试!

您在这里要做的是设置一个端点,使其像其他页面一样运行您想要的Ajax动作,但是您将使用自定义查询参数amz (实际上是一个单独的wp-ajax.php ,而不是使用漂亮的永久链接。 wp-ajax.php )。 这就是为什么我们使用template_redirect钩子检查自定义查询参数是否已定义并具有值,如果是,则停止呈现除Ajax动作以外的任何内容。

我还添加了随机数,因为您应该始终使用保护 ;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM