Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
79 views
in Technique[技术] by (71.8m points)

javascript - Bad requset error in admin-ajax.php when submit a form

I have created a form to display my theme plugins through TGMPA. But now when I click on the submit button, I get an error 400 bad request, and it not shown any ways to fix it in console. my ajax is work good in other function, but in this function its not working anymore. Below are the relevant codes.

JavaScript

         $(document).on( 'click', '.kt-install-plugins, .kt-install-core-plugins', function( e ){
             e.preventDefault();

             if ( $('li.plugin-to-install input:checked').length == 0 ) {
                 alert( satin_theme_admin_vars.select_plugins );
                 return;
             }
             $(this).addClass('kt-checking');

             $('li.plugin-to-install input:checked').siblings('.spinner').css('visibility', 'visible');

             find_next();

         });

         var items_completed = 0;
         var current_item = '';
         var $current_node;
         var current_item_hash = '';

         function ajax_callback(response){
             if(typeof response == 'object' && typeof response.message != 'undefined'){
                 $current_node.find('span').text(response.message);
                 $('.kt-install-core-plugins').text(response.message);
                 if(typeof response.url != 'undefined'){
                     // we have an ajax url action to perform.

                     if(response.hash == current_item_hash){
                         $current_node.find('span').text("failed");
                         find_next();
                     }else {
                         current_item_hash = response.hash;
                         jQuery.post(response.url, response, function(response2) {
                             process_current();
                             $current_node.find('span').text(response.message);
                         }).fail(ajax_callback);
                     }

                 }else if(typeof response.done != 'undefined'){
                     // finished processing this plugin, move onto next
                     $current_node.find('span').addClass('kt-success');
                     find_next();
                 }else{
                     // error processing this plugin
                     find_next();
                 }
             }else{
                 // error - try again with next plugin
                 $current_node.find('span').addClass('kt-success').text("Success");
                 find_next();
             }
         }
         function process_current(){
             if(current_item){
                 // query our ajax handler to get the ajax to send to TGM
                 // if we don't get a reply we can assume everything worked and continue onto the next one.
                 $.post(satin_theme_admin_vars.ajax_url, {
                     action: 'install_plugins',
                     wpnonce: satin_theme_admin_vars.wpnonce,
                     slug: current_item
                 }, ajax_callback).fail(ajax_callback);
             }
         }
         function find_next(){
             var do_next = false;
             if($current_node){
                 if(!$current_node.data('done_item')){
                     items_completed++;
                     $current_node.data('done_item',1);
                 }
                 $current_node.find('.spinner').css('visibility','hidden');
             }
             var $li = $('.kt-plugins-form li').has('input:checked').addClass('installing');
             $('.kt-plugins-form li').find('input').prop('disabled', true);
             $li.each(function(){
                 if(current_item == '' || do_next){
                     current_item = $(this).find('input').data('slug');
                     $current_node = $(this);
                     process_current();
                     do_next = false;
                 }else if($(this).data('slug') == current_item){
                     do_next = true;
                 }
             });
             if(items_completed >= $li.length){
                 // finished all plugins!
                 var failed = false;
                 $('.kt-plugins-form li span').each(function(index, el) {
                     if ($(this).text() == 'failed') {
                         failed = true;
                     }
                 });
                 if (failed) {
                     $('.kt-install-plugins, .kt-install-core-plugins').removeClass('kt-checking');
                     document.cookie = 'pluginFailed=true';
                     // window.location.reload(true);
                 } else {
                     document.cookie = 'pluginFailed=;';
                     $li.removeClass('installing');
                     $('.kt-install-plugins, .kt-install-core-plugins').removeClass('kt-checking');
                     if ( $('.kt-plugins-form').data('redirect') != '' ) {
                         // window.location.replace( $('.kt-plugins-form').data('redirect') );
                     }
                 }
             }
         }
});

PHP Code

      tgmpa_load_bulk_installer();
      // install plugins with TGM.
      if ( ! class_exists( 'TGM_Plugin_Activation' ) || ! isset( $GLOBALS['satin'] ) ) {
          die( 'Failed to find TGM' );
      }
      $url= wp_nonce_url( add_query_arg( array( 'plugins' => 'go' ) ), 'satin-dashboard-plugins' );

      // copied from TGM

      $method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
      $fields = array_keys( $_POST ); // Extra fields to pass to WP_Filesystem.

      if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, $fields ) ) ) {
          return true; // Stop the normal page form from displaying, credential request form will be shown.
      }

      // Now we have some credentials, setup WP_Filesystem.
      if ( ! WP_Filesystem( $creds ) ) {
          // Our credentials were no good, ask the user for them again.
          request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, $fields );

          return true;
      }

      if ( ! function_exists( 'satin_tgm_plugins' ) ) {
          die( __( 'We have released new version of theme with so many improvements. To get all improvements and get access to this page please update the theme.', 'satin' ) );
      }
      /* If we arrive here, we have the filesystem */
      $plugins = satin_tgm_plugins();
      ?>
      <form method="post" class="kt-plugins-page">
          <span class="kt-plugins-page-title"><?php echo esc_html__( 'Default Plugins', 'satin' );?></span>
          <br>
          <?php
          if ( count( $plugins['all'] ) ) {
              ?>
              <span class="kt-plugins-page-desc"><?php echo esc_html__( 'Your website needs a few essential plugins. The following plugins will be installed or updated:', 'satin' ); ?></span>
              <?php
          } else {
              ?>
              <span class="kt-plugins-page-desc"><?php echo esc_html__( 'Good news. All essentials plugins are active and up to date.', 'satin' ); ?></span>
              <?php
          }
          ?>
          <?php
          if ( count( $plugins['all'] ) ) {
              ?>
              <ul class="kt-plugins-form" data-redirect="<?php echo admin_url('admin.php?page=satin-dashboard-plugins');?>">
                  <?php if ( ! empty( $_COOKIE['pluginFailed'] ) ) { ?>
                  <li style="color: red;border: 1px dashed;padding: 5px;"><?php esc_html_e( 'It Seems that some plugins have failed.Please try again.', 'satin' ); ?></li>
                  <?php } ?>
                  <?php
                  $required = array_filter(
                      $plugins['all'],
                      function( $el ) {
                          return $el['required'];
                      }
                  );
                  if ( ! empty( $required ) ) {
                      ?>
                      <li class="kt-plugins-title"><?php esc_html_e( 'Required', 'satin' );?></li>
                      <?php
                  }
                  ?>
                  <?php foreach ( $required as $slug => $plugin ) { ?>
                      <li data-slug="<?php echo esc_attr( $slug ); ?>" class="plugin-to-install">
                          <?php
                          $keys = array();
                          $actions = array(
                              'install'   => '',
                              'update'    => '',
                              'activate'  => ''
                          );

                          if ( isset( $plugins['install'][ $slug ] ) ) {
                              $keys[] = __('Installation','satin');
                              $actions['install'] = 'install';
                          }
                          if ( isset( $plugins['update'][ $slug ] ) ) {
                              $keys[] = __('Update','satin');
                              $actions['update'] = 'update';
                          }
                          if ( isset( $plugins['activate'][ $slug ] ) ) {
                              $keys[] =  __('Activation','satin');
                              $actions['activate'] = 'activate';
                          }
                          ?>
                          <input
                              type="checkbox"
                              name="plugin-import[<?php echo esc_attr( $slug ); ?>]"
                              id="plugin-import[<?php echo esc_attr( $slug ); ?>]"
                              checked="checked"
                              disabled="disabled"
                              data-actions="<?php echo esc_attr( json_encode( $actions ) );?>"
                              data-slug="<?php echo esc_attr( $slug );?>"
                              data-wpnonce="<?php echo wp_create_nonce( 'bulk-plugins' ); ?>"
                          /><?php echo esc_html( $plugin['name'] ); ?>
                          <span class='kt-error'> <?php echo implode( ' and ', $keys ) . __(' required','satin'); ?></span>
                          <div class="spinner"></div>
                      </li>
                  <?php
                  }
                  $recommended = array_filter(
                      $plugins['all'],
                      function( $el ) {
                          return ( ! $el['required'] );
                      }
                  );
                  if ( ! empty( $recommended ) ) {
                      ?>
                      <li class="kt-plugins-title"><?php esc_html_e( 'Recommended', 'satin' );?></li>
                      <?php
                  }
                  foreach ( $recommended as $slug => $plugin ) { ?>
                      <?php
                      $keys = array();
                      $actions = array(
                          'install'   => '',
                          'update'    => '',
                          'activate'  => ''
                      );
                      $data_install = admin_url( 'themes.php?page=install-required-plugins' );
         

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...