admin管理员组

文章数量:1425660

For my ajax popup login i want to use Output Buffer for a whole oop php class. I just want to start buffering from the start of class and end it when the class is finished.

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();

End buffering at _destruct() magic method. Isn't it right way to start buffering at the beginning of class and end when class end. What is the right way use buffer for the whole php class?

EDIT

Take a look and say where should i use ob_start() and ob_end_clean() for my ajax popupbox

// 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();

I think, this time i give you all the code you need to understand that where should i use ob_start() and ob_end_clean().

Thanks.

.

For my ajax popup login i want to use Output Buffer for a whole oop php class. I just want to start buffering from the start of class and end it when the class is finished.

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();

End buffering at _destruct() magic method. Isn't it right way to start buffering at the beginning of class and end when class end. What is the right way use buffer for the whole php class?

EDIT

Take a look and say where should i use ob_start() and ob_end_clean() for my ajax popupbox

// 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();

I think, this time i give you all the code you need to understand that where should i use ob_start() and ob_end_clean().

Thanks.

.

Share Improve this question edited Jun 18, 2019 at 12:55 Zahid Hossain asked Jun 18, 2019 at 7:21 Zahid HossainZahid Hossain 71 silver badge4 bronze badges 6
  • Why? I can't imagine any reason you'd want to do this. add_action() doesn't even produce any output, so there's no reason to buffer it. – Jacob Peattie Commented Jun 18, 2019 at 7:43
  • @jacob Peattie - i write my reason in below – Zahid Hossain Commented Jun 18, 2019 at 7:55
  • I have no idea why your example is using output buffering either. It makes no sense to use it in either context. The only thing that might need to be output buffered is the contents of the shortcode function, but in that case the output buffering should start and end inside just that function. – Jacob Peattie Commented Jun 18, 2019 at 10:50
  • @jacob you mean only included html files files need to output buffered? – Zahid Hossain Commented Jun 18, 2019 at 11:11
  • Correct. You only need to output buffer if you need to capture output. If you want to capture the contents of a file into a variable, you will need to output buffer the include, but you shouldn't need to output buffer an entire class or plugin. – Jacob Peattie Commented Jun 18, 2019 at 11:22
 |  Show 1 more comment

1 Answer 1

Reset to default 0

Your code does not need to use output buffering anywhere. There's nowhere that you need it.

Your shortcode function is already properly using return for the output output, so nothing needs to be captured, and your popup_bootmodal_form() function is outputting in wp_footer, which is fine, and doesn't need to be captured either.

本文标签: theme developmentProper use of Output Buffer for a whole php clas