正确使用整个php类输出缓冲区

时间:2019-06-18 作者:Zahid Hossain

对于我的ajax弹出式登录,我想对整个oop php类使用输出缓冲区。我只想从类的开头开始缓冲,然后在类结束时结束缓冲

class Wp_Ajax_Popup_Login {
    public function __construct() {
        // Output Buffering for whole class
        ob_start();
        add_action( \'after_switch_theme\', array( $this, \'theme_activation\' ) );
        add_action( \'switch_theme\', array( $this, \'theme_deactivation\' ) );

        add_action( \'init\', array( $this, \'start_session\' ), 1 );

        add_action( \'wp_login\', array( $this, \'end_session\' ) );
        add_action( \'wp_logout\', array( $this, \'end_session\' ) );

        if( is_admin() ) {
            add_action( \'admin_menu\', array( $this, \'custom_theme_option_menu\' ) );
        }

        add_action( \'wp_enqueue_scripts\', array( $this, \'theme_login_enqueue_script\' ) );

        add_action( \'init\', array( $this, \'ajax_login_modal_init\' ) );
        add_shortcode( \'Bootstrap_Register_login\', array( $this, \'theme_login_shortcode\' ) );
    }

    public function __destruct() {
        // Is it will work for the whole class buffer cleanup?
        ob_end_clean();
    }

    public static function theme_activation() {}
    public function theme_deactivation() {}

    // Other functions will write here
}
new Wp_Ajax_Popup_Login();
在\\u destruct()魔术方法处结束缓冲。这不是正确的方式吗start buffering at the beginning of class and end when class end. 什么是正确的方法use buffer for the whole php class?

EDIT

看一看,然后说我应该在哪里为我的ajax弹出框使用ob\\u start()和ob\\u end\\u clean()

// restrict direct access of theme files
if ( ! defined( \'ABSPATH\' ) )

class Wp_Ajax_Popup_Login {

    public function __construct() {
        add_action( \'wp_enqueue_scripts\', array( $this, \'theme_login_enqueue_script\' ) );
        add_action( \'init\', array( $this, \'ajax_login_modal_init\' ) );
        add_shortcode( \'Bootstrap_Register_login\', array( $this, \'theme_login_shortcode\' ) );

        // Other necessary hooks will write here
    }


    public function theme_login_enqueue_script() {
        if (get_option( \'bootstrap_support_required\' ) == 1){
            wp_enqueue_style( \'bootstrap-bs3patch\', get_parent_theme_file_uri( \'admin/assets/css/bootstrap-modal-bs3patch.css\' ) );
        }

        //bootModal style
        wp_enqueue_style( \'bootstrap-modal\', get_parent_theme_file_uri(\'admin/assets/css/bootstrap-modal.css\') );

        // jquery
        wp_enqueue_script(\'jquery\');
        //bootmodal script
        wp_enqueue_script(
            \'bootstrap-modal\',
            get_parent_theme_file_uri( \'admin/assets/js/bootstrap-modal.js\' ),
        false,\'\',true
        );
    }


    public function ajax_login_modal_init() {
        if ( ! is_admin() || is_customize_preview() ) {
            wp_register_script( \'ajax-login-script\', get_parent_theme_file_uri( \'admin/assets/js/scripts.js\' ), array( \'jquery\' ) );
            wp_enqueue_script( \'ajax-login-script\' );
        }

        wp_localize_script( \'ajax-login-script\', \'ajax_login_object\', array(
            \'ajax_url\'                 => admin_url( \'admin-ajax.php\' ),
            \'login_nonce_token\'        => wp_create_nonce( \'login_nonce\' ),
            \'register_nonce_token\'     => wp_create_nonce( \'register_nonce\' ),
            \'lostpassword_nonce_token\' => wp_create_nonce( \'lostpassword_nonce\' ),
            \'loginRedirectURL\'         => (get_option( \'login_modal_redirect\' ) == \'\') ? \'\' : get_option( \'login_modal_redirect\' ),
            \'registerRedirectURL\'     => (get_option( \'register_modal_redirect\' ) == \'\') ? \'\' : get_option( \'register_modal_redirect\' )
        ) );

        add_action( \'wp_ajax_nopriv_ajaxlogin\', array( $this, \'ajax_login_jquery_callable\' ) );
        // add_action( \'wp_ajax_nopriv_ajaxregister\', array( $this, \'ajax_register_jquery_callable\' ) );
        // add_action( \'wp_ajax_nopriv_ajaxlostpassword\', array( $this, \'ajax_lost_password_jquery_callable\' ) );
    }


    public function theme_login_shortcode() {
        // html forms
        add_action( \'wp_footer\', array( $this, \'popup_bootmodal_form\' ) );

        if ( ! is_user_logged_in() ) {
            return \'<button type="button" class="btn \' . $this->default_buttons() . \' \' . $this->theme_button_block() . \' \' . $this->theme_button_size() . \'" data-toggle="modal" data-target="#bootmodal">\' . $this->login_button_text() . \'</button>\';
        } else {
            if ( get_option( \'login_modals_profile\' ) != 1 ) {
                return \'<button type="button" class="btn \' . $this->default_buttons() . \' \' . $this->theme_button_block() . \' \' . $this->theme_button_size() . \'" data-toggle="modal" data-target="#bootmodal">\' . $this->login_button_text() . \'</button>\';
            } else {
                return \'<button type="button" class="btn \' . $this->default_buttons() . \' \' . $this->theme_button_block() . \' \' . $this->theme_button_size() . \' disabled" disabled="disabled">\' . $this->login_button_text() . \'</button>\';
            }
        }
    }


    // call it inside theme_login_shortcode() function
    public function popup_bootmodal_form() {
        if ( ! is_user_logged_in() ) :
            ?>

                <div id="bootmodal" class="modal fade" tabindex="-1" data-width="370" data-backdrop="static" data-keyboard="false" style="display: none;">
                    <div class="tab-content">
                        <div class="tab-pane active fade in" id="login_tab">
                            <?php include_once \'templates/login-form.php\'; ?>
                        </div>
                        <?php if (get_option( \'users_can_register\' ) == true): ?>
                            <div class="tab-pane fade in" id="register_tab">
                                <?php include_once \'templates/register-form.php\'; ?>
                            </div>
                        <?php endif; ?>
                        <div class="tab-pane fade in" id="lostpass_tab">
                            <?php  include_once \'templates/password-lost-form.php\'; ?>
                        </div>
                    </div><!-- #tab-content -->
                </div><!-- #bootmodal -->

            <?php
        endif;
    }

    // Other functions will write here


}
new Wp_Ajax_Popup_Login();
我想,这一次我给你所有你需要理解的代码,我应该在哪里使用ob_start() and ob_end_clean().

谢谢

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

您的代码不需要在任何地方使用输出缓冲。你哪里都不需要它。

您的shortcode函数已正确使用return 对于输出,无需捕获任何内容popup_bootmodal_form() 函数正在输出wp_footer, 这很好,也不需要捕捉。