在类中使用WordPress函数,并将我的代码更改为OOP PHP

时间:2018-08-27 作者:Remzi Cavdar

我尝试将所有代码更改为OOP,我是OOP和PHP的初学者。这很有效,但当我将代码更改为OOP时,我可以在更多地方使用我的类

function advanza_form_shortcode() {

    // Display the Advanza form with a shortcode
    $site_id = get_option(\'advanza_form_site_id\');
    $affiliate_id = get_option(\'advanza_form_affiliate_id\');
    $redirection = get_option(\'advanza_form_thankyou_page\');
    $url = "https://advanzaservices.com/wp/singleform.php?site_id={$site_id}&affiliate_id={$affiliate_id}&thankyou={$redirection}";

    $response = wp_remote_get( $url );

    if ( is_array( $response ) ) {
        $header = wp_remote_retrieve_headers($response[\'headers\']); // array of http header lines
        $body = wp_remote_retrieve_body($response); // use the content
        $status_code = wp_remote_retrieve_response_code($response);

        if($status_code == 200) {
            return $body;
        } else {
            return $status_code;
        }
   }
} add_shortcode( \'advanza-form\', \'advanza_form_shortcode\' );
我不明白为什么这不起作用,我试了很多次,但似乎我没有明白。

class advanza_form {
    public $site_id;
    public $affiliate_id;
    public $redirection;
    public $url;

    public function __construct() {
        $this->site_id = get_option(\'advanza_form_site_id\');
        $this->affiliate_id = get_option(\'advanza_form_affiliate_id\');
        $this->redirection = get_option(\'advanza_form_thankyou_page\');
        $this->url = "https://advanzaservices.com/wp/singleform.php?site_id={$this->site_id}&affiliate_id={$this->affiliate_id}&thankyou={$this->redirection}";
    }

    public function display() {
        $response = wp_remote_get( $this->url );
        $header = wp_remote_retrieve_headers($this->response[\'headers\']); // array of http header lines
        $status_code = wp_remote_retrieve_response_code($this->response);

        try {
            if($status_code == 200) {
                $body = wp_remote_retrieve_body($response);
            } else {
                return $status_code;
            }
        } catch (Exception $e) {
            echo \'Caught exception: \', $e->getMessage(), "\\n";
        }// end try/catch

        return $body;
    }

    function debug() {
        echo \'Site ID: \' . $this->site_id . \'<br>\';
        echo \'Affiliate ID: \' . $this->affiliate_id . \'<br>\';
        echo \'Redirection: \' . $this->redirection . \'<br>\';
        echo \'URL: \' . $this->url . \'<br>\';
    }
}
在这里,我创建了我的短代码

function advanza_form_shortcode() {

    // Display the Advanza form with a shortcode
    $form = new advanza_form;
    $form->display();
    // $form->debug();

}
add_shortcode( \'advanza-form\', \'advanza_form_shortcode\' );

1 个回复
最合适的回答,由SO网友:Remzi Cavdar 整理而成

谢谢大家的建议和回答。我已修复,请参阅下面的代码:

class advanza_form {
// Class properties defined using PHP Coding Guidelines and taking security measures: http://www.php.net/manual/en/language.oop5.visibility.php
public $site_id;
public $affiliate_id;
public $redirection;
protected $url;
private $response;
private $header; // For debugging
public $status_code; // For debugging

public function __construct() {
    $this->site_id = get_option(\'advanza_form_site_id\');
    $this->affiliate_id = get_option(\'advanza_form_affiliate_id\');
    $this->redirection = get_option(\'advanza_form_thankyou_page\');
    $this->url = "https://advanzaservices.com/wp/singleform.php?site_id={$this->site_id}&affiliate_id={$this->affiliate_id}&thankyou={$this->redirection}";
    $this->response = wp_remote_get( $this->url );
    $this->header = wp_remote_retrieve_headers( $this->response ); // array of http header lines
    $this->status_code = wp_remote_retrieve_response_code( $this->response );
}

public function display() {
    try {
        if($this->status_code == 200) {
            $html = wp_remote_retrieve_body($this->response);
        } else {
            return $this->status_code;
        }
    } catch (Exception $e) {
        echo \'Caught exception: \', $e->getMessage(), "\\n";
    }// end try/catch

    return $html;
}

public function debug() {
    $last_modified = wp_remote_retrieve_header( $this->response, \'last-modified\' );
    echo \'<h1>Debug information</h1>\';
    echo \'<p>\';
    echo \'<strong>Site ID:</strong> \' . $this->site_id . \'<br>\';
    echo \'<strong>Affiliate ID:</strong> \' . $this->affiliate_id . \'<br>\';
    echo \'<strong>Redirection:</strong> \' . $this->redirection . \'<br>\';
    echo \'<strong>URL:</strong> \' . $this->url . \'<br>\';
    echo \'</p>\';
    echo \'<h2>Technical details</h2>\';
    echo \'<p>\';
    echo \'<a href="https://en.wikipedia.org/wiki/List_of_HTTP_status_codes" target="_blank"><strong>HTTP status code:</strong></a> \' . $this->status_code . \'<br>\';
    echo \'<strong>Headers:</strong> \'; var_export($this->header);
    echo \'</p>\';
    // error: Invalid or no affiliate_id supplied
}
}
短代码

function advanza_form_shortcode() {
    // Display the Advanza form with a shortcode
    $form = new advanza_form;
    // For debugging
    // $form->debug();
    return $form->display();
}
add_shortcode( \'advanza-form\', \'advanza_form_shortcode\' );

结束