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

时间:2018-02-06 05:21:07

标签: javascript php html wordpress

我正在处理我正在处理的插件,在插件管理区域,应该有用于在测试模式或实时模式之间进行选择的单选按钮 问题是当用户选择实时模式并单击保存时,页面刷新和单选按钮将返回测试模式

这是代码

 <?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>';
}

这是单选按钮行

 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']) : ''
        );
    }

另外,我正在尝试让按钮选择根据用户选择触发php功能 例如检查测试模式,然后总是使用function1 实时模式然后总是使用function2

这是它调用的两个函数

 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;
    }

最后,我尝试使用此代码解决单选按钮选择问题,

 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']) : ''
        );
    }

但我收到了错误 警告:printf():第15行的class.gpgBookingAdmin.php中的参数太少

1 个答案:

答案 0 :(得分:2)

尝试使用此方法解决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"
        );
    }