简体   繁体   English

插件设置页面单选按钮无法正常运行

[英]Plugin settings page radio button not functioning properly

I'm having trouble with a plugin I'm working on, in the plugin admin area, there should be radio buttons for the user to choose between test mode or live mode the problem is when the user chooses live mode and clicks save, the page refresh and the radio button go back to test mode 我在使用的插件遇到问题,在插件管理区域中,应该有单选按钮供用户在测试模式或实时模式之间进行选择,问题是当用户选择实时模式并单击保存时,页面刷新,单选按钮返回测试模式

here is the code 这是代码

 <?php

class GpgBookingAdmin {


     /**
     * Holds the values to be used in the fields callbacks
     */
    private $options;

    /**
     * Start up
     */
    public function __construct()
    {
        add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
        add_action( 'admin_init', array( $this, 'page_init' ) );
    }

    /**
     * Add options page
     */
    public function add_plugin_page()
    {
        // This page will be under "Settings"
        add_options_page(
            'Settings Admin',
            'Gpg Setting',
            'manage_options',
            'gpg-setting-admin',
            array( $this, 'create_admin_page' )
        );
    }

    /**
     * Options page callback
     */
    public function create_admin_page()
    {
        // Set class property
        $this->options = get_option( 'gpg_option_name' );
        ?>
        <div class="wrap">
            <h1>My Settings</h1>
            <form method="post" action="options.php">
            <?php
                // This prints out all hidden setting fields
                settings_fields( 'gpg_option_group' );
                do_settings_sections( 'gpg-setting-admin' );
                submit_button();
            ?>
            </form>
        </div>
        <?php
    }

    /**
     * Register and add settings
     */
    public function page_init()
    {
        register_setting(
            'gpg_option_group', // Option group
            'gpg_option_name', // Option name
            array( $this, 'sanitize' ) // Sanitize
        );

        add_settings_section(
            'setting_section_id', // ID
            'Gpg Custom Settings', // Title
            array( $this, 'print_section_info' ), // Callback
            'gpg-setting-admin' // Page
        );

        add_settings_field(
            'gpg_golf_apikey',
            'Golf API key',
            array( $this, 'gpg_golf_apikey' ),
            'gpg-setting-admin',
            'setting_section_id'
        );

        add_settings_field(
            'gpg_api_mode',
            'Golf API Mode',
            array( $this, 'gpg_api_mode' ),
            'gpg-setting-admin',
            'setting_section_id'
        );

        add_settings_field(
            'gpg_strip_publishkey', // ID
            'Strip PublishKey', // Title
            array( $this, 'gpg_strip_publishkey' ), // Callback
            'gpg-setting-admin', // Page
            'setting_section_id' // Section
        );

        add_settings_field(
            'gpg_strip_secretkey',
            'Strip Secret key',
            array( $this, 'gpg_strip_secretkey' ),
            'gpg-setting-admin',
            'setting_section_id'
        );


    }

    /**
     * Sanitize each setting field as needed
     *
     * @param array $input Contains all settings fields as array keys
     */
    public function sanitize( $input )
    {
       // $new_input = array();

        return $input;
    }

    /**
     * Print the Section text
     */
    public function print_section_info()
    {
        print 'Enter your settings below:';
    }

    /**
     * Get the settings option array and print one of its values
     */
    public function gpg_golf_apikey()
    {
        printf(
            '<input type="text" id="gpg_golf_apikey" name="gpg_option_name[gpg_golf_apikey]" value="%s" />',
            isset( $this->options['gpg_golf_apikey'] ) ? esc_attr( $this->options['gpg_golf_apikey']) : ''
        );
    }

    /**
     * Get the settings option array and print one of its values
     */
    public function gpg_api_mode()
    {
        printf(
            '<input type="radio" name="gpg_option_name[gpg_api_mode]" value="0" checked /> Test
<input type="radio" name="gpg_option_name[gpg_api_mode]" value="1" /> Live',
            isset( $this->options['gpg_api_mode'] ) ? esc_attr( $this->options['gpg_api_mode']) : ''
        );
    }

    /**
     * Get the settings option array and print one of its values
     */
    public function gpg_strip_publishkey()
    {
        printf(
            '<input type="text" id="gpg_strip_publishkey" name="gpg_option_name[gpg_strip_publishkey]" value="%s" />',
            isset( $this->options['gpg_strip_publishkey'] ) ? esc_attr( $this->options['gpg_strip_publishkey']) : ''
        );
    }

    public function gpg_strip_secretkey()
    {
        printf(
            '<input type="text" id="gpg_strip_secretkey" name="gpg_option_name[gpg_strip_secretkey]" value="%s" />',
            isset( $this->options['gpg_strip_secretkey'] ) ? esc_attr( $this->options['gpg_strip_secretkey']) : ''
        );
    }
}





function add_theme_menu_item()
{
 add_menu_page("Golf Settings", "Golf Settings", "manage_options", "golf-settings-panel", "golfSettingsPage", null, 99);
}

add_action("admin_menu", "add_theme_menu_item");

function golfSettingsPage()
{
    ?>
     <div class="wrap">
     <h1>Golf Settings</h1> <!-- page title is added from here -->
     <form method="post" action="options.php">
         <?php
             settings_fields("section");
             do_settings_sections("theme-options");
             submit_button();
         ?>
     </form>
  </div>
 <?php
}


add_action("admin_init", "display_theme_panel_fields");
function display_theme_panel_fields()
{
 add_settings_section("section", "<hr/>", null, "theme-options");


 add_settings_field("teeTimePage", "Select Tee Time Results", "selectTeeTimePage", "theme-options", "section");
 register_setting("section", "teeTimePage");

 add_settings_field("search_result_page", "Select Search Result Page", "selectPage", "theme-options", "section");
 register_setting("section", "search_result_page");

 add_settings_field("bookingPage", "Select Booking Page", "selectBookingPage", "theme-options", "section");
 register_setting("section", "bookingPage");

 add_settings_field("bookingConfirmationPage", "Select Booking Page", "selectBookingConfirmationPagePage", "theme-options", "section");
 register_setting("section", "bookingConfirmationPage");

 /*add_settings_field("bookingConfirmationPage", "Select Booking Page", "selectBookingConfirmationPagePage", "theme-options", "section");
 register_setting("section", "bookingConfirmationPage");
*/

}



function selectBookingPage()
{
    $args = array(
   'post_type' => 'page',
   'post_status' => 'publish',
   'order'=>'desc',
   'posts_per_page' =>-1
   );
 $new_query = new WP_Query( $args );
 $totalPages = array();

 echo '<select name="bookingPage" id="bookingPage" >';
  while ( $new_query->have_posts() ) : $new_query->the_post();
   $savedOption = get_option('bookingPage');
   $currentID = get_the_ID();

   if($savedOption == $currentID) {
    $select = 'selected';
   }else{
    $select = '';
   }

   echo '<option '.$select.' value='.$currentID.'>'.get_the_title($currentID).'</option>';
  endwhile;
  wp_reset_query();
 echo '</select>';

}

function selectBookingConfirmationPagePage()
{
    $args = array(
   'post_type' => 'page',
   'post_status' => 'publish',
   'order'=>'desc',
   'posts_per_page' =>-1
   );
 $new_query = new WP_Query( $args );
 $totalPages = array();

 echo '<select name="bookingConfirmationPage" id="bookingConfirmationPage" >';
  while ( $new_query->have_posts() ) : $new_query->the_post();
   $savedOption = get_option('bookingConfirmationPage');
   $currentID = get_the_ID();

   if($savedOption == $currentID) {
    $select = 'selected';
   }else{
    $select = '';
   }

   echo '<option '.$select.' value='.$currentID.'>'.get_the_title($currentID).'</option>';
  endwhile;
  wp_reset_query();
 echo '</select>';

}
function selectTeeTimePage()
{
    $args = array(
   'post_type' => 'page',
   'post_status' => 'publish',
   'order'=>'desc',
   'posts_per_page' =>-1
   );
 $new_query = new WP_Query( $args );
 $totalPages = array();

 echo '<select name="teeTimePage" id="teeTimePage" >';
  while ( $new_query->have_posts() ) : $new_query->the_post();
   $savedOption = get_option('teeTimePage');
   $currentID = get_the_ID();

   if($savedOption == $currentID) {
    $select = 'selected';
   }else{
    $select = '';
   }

   echo '<option '.$select.' value='.$currentID.'>'.get_the_title($currentID).'</option>';
  endwhile;
  wp_reset_query();
 echo '</select>';

}

function selectPage()
{

 $args = array(
   'post_type' => 'page',
   'post_status' => 'publish',
   'order'=>'desc',
   'posts_per_page' =>-1
   );
 $new_query = new WP_Query( $args );
 $totalPages = array();

 echo '<select name="search_result_page" id="search_result_page" >';
  while ( $new_query->have_posts() ) : $new_query->the_post();
   $savedOption = get_option('search_result_page');
   $currentID = get_the_ID();

   if($savedOption == $currentID) {
    $select = 'selected';
   }else{
    $select = '';
   }

   echo '<option '.$select.' value='.$currentID.'>'.get_the_title($currentID).'</option>';
  endwhile;
  wp_reset_query();
 echo '</select>';
}

here is the radio button lines 这是单选按钮行

 public function gpg_api_mode()
    {
        printf(
            '<input type="radio" name="gpg_option_name[gpg_api_mode]" value="0" checked /> Test
<input type="radio" name="gpg_option_name[gpg_api_mode]" value="1" /> Live',
            isset( $this->options['gpg_api_mode'] ) ? esc_attr( $this->options['gpg_api_mode']) : ''
        );
    }

also, I'm trying to make the button choice trigger a php function based on the user choice for example test mode checked, then always use function1 live mode on then always use function2 另外,我试图使按钮选择根据用户选择触发php函数,例如选中测试模式,然后始终使用function1实时模式,然后始终使用function2

here is the 2 functions it calls 这是它调用的2个函数

 public static function http_post($apiName = "courses", $post = array(), $customRequest = "")
        {
            $postString = "";
            //https://www.golf18network.com/apiv2
            //https://www.golf18staging=.com/apiv2/$apiName
            $url        = "https://www.golf18staging.com/apiv2/$apiName";
            $ch         = curl_init();


            $headers    = array();
            $headers[]  = 'X-API-KEY:' . GPGBOOKING_GET_API_KEY;

            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_URL, $url);

            if ($post)
            {
                foreach ($post as $key => $value)
                {
                    $postString .= $key . "=" . $value . "&";
                }

                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
            }

            if ($customRequest)
            {
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
            }

            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_FAILONERROR, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_VERBOSE, true);

            $simplifiedResponse = curl_exec($ch);
            $error               = curl_error($ch);

            $simplifiedResponse = json_decode($simplifiedResponse);

            curl_close($ch);

            /*echo "<pre>";
            print_r($simplifiedResponse);
            print_r($error);
    */

            return $simplifiedResponse;
        }

and the second funtion is


    public static function http_post($apiName = "courses", $post = array(), $customRequest = "")
        {
            $postString = "";
            //https://www.golf18network.com/apiv2
            //https://www.golf18staging=.com/apiv2/$apiName
            $url        = "https://www.golf18network.com/apiv2/$apiName";
            $ch         = curl_init();


            $headers    = array();
            $headers[]  = 'X-API-KEY:' . GPGBOOKING_GET_API_KEY;

            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_URL, $url);

            if ($post)
            {
                foreach ($post as $key => $value)
                {
                    $postString .= $key . "=" . $value . "&";
                }

                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
            }

            if ($customRequest)
            {
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
            }

            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_FAILONERROR, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_VERBOSE, true);

            $simplifiedResponse = curl_exec($ch);
            $error               = curl_error($ch);



$simplifiedResponse = json_decode($simplifiedResponse);

        curl_close($ch);

        /*echo "<pre>";
        print_r($simplifiedResponse);
        print_r($error);
*/

        return $simplifiedResponse;
    }

Last thing, I've tried solving the radio button selection issue using this code, 最后,我尝试使用此代码解决单选按钮选择问题,

 public function gpg_api_mode()
    {
        printf(
            '<%
String whichRadio = request.getParameter("gpg_option_name[gpg_api_mode]");
String r1checked = "";
if (whichRadio.equals("1")) r1checked = " checked";
String r2checked = "";
if (whichRadio.equals("2")) r2checked = " checked";
%>

            <input type="radio" name="gpg_option_name[gpg_api_mode]" value="1"<%= r1Checked %>/> Test
<input type="radio" name="gpg_option_name[gpg_api_mode]" value="2" <%= r2Checked %>/> Live',
            isset( $this->options['gpg_api_mode'] ) ? esc_attr( $this->options['gpg_api_mode']) : ''
        );
    }

but I'm getting error Warning: printf(): Too few arguments in class.gpgBookingAdmin.php on line 15 但出现错误警告:printf():第15行的class.gpgBookingAdmin.php中的参数太少

Try this for solving the radio selection issue: 尝试以下方法解决radio选择问题:

    /**
     * Get the settings option array and print one of its values
     */
    public function gpg_api_mode()
    {
        $value = isset( $this->options['gpg_api_mode'] ) ? $this->options['gpg_api_mode'] : '0';
        printf(
            '<input type="radio" name="gpg_option_name[gpg_api_mode]" value="0"%s /> Test
<input type="radio" name="gpg_option_name[gpg_api_mode]" value="1"%s /> Live',
            checked( '0', $value, false ), // "checked" attr when API mode is "Test"
            checked( '1', $value, false ) // "checked" attr when API mode is "Live"
        );
    }

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

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