0

プラグインを作成していますが、プラグイン コードの特定の機能をトリガーするタイミングに問題があります。

/*
// Plugin information goes here
*/


// ***** Area A

$GLOBALS['example_class'] = new example_class;

class example_class {

    // ***** Area B

    public function admin_init() {
        add_menu_page(

            // ...

        );
    } // End of admin_init function
} // End of example class

add_action('init', function() {
    global $example_class;

    // ***** Area C

    if ( ?????? ) {

        // Sanitize and set the view role
        $view = ( isset( $_REQUEST['view'] ) ) ? sanitize_key( $_REQUEST['ex'] ) : 'get_all';
        // Manage submitted data
        switch ( $view ) {

            // ...

        } // End of switch for view

        // Sanitize and set the action role
        $action = ( isset( $_REQUEST['action'] ) ) ? sanitize_key( $_REQUEST['action'] ) : NULL;
        // Manage submitted data
        switch ( $action ) {

            //...

        } // End of switch for action

    } // End of if page is being shown
});

add_action( 'admin_menu', function() {
    global $example_class;
    $example_class->admin_init();
});

add_shortcode( 'show_public_random', function () {
    global $example_class;
    // ...
});

stackexchange に関する以前の投稿で提案されたように、プラグインのコントローラー側を、initイベントによって呼び出される関数に分離しました。ただし、initイベント関数に含まれるコードがページの読み込みごとに評価されるのは望ましくありません。ショートコードを含むページが読み込まれるときにのみコードが評価されるようにしたいのです。

false として初期化されるが、add_shortcode 関数内から true に変更される boolean クラス変数を読み込もうとしましたが、その時までには遅すぎます -initイベントが発生し、関数の内容は実行されません。

助けてください - コードのエリア C でどの式を使用すればよいですか? initショートコードが使用されている場合にのみイベント関数が実行されるようにするには、何をテストする必要がありますか?

4

1 に答える 1

0

面倒ですが、答えを見つけました。

/*
// Plugin information goes here
*/
$GLOBALS['example_class'] = new example_class;

class example_class {

    var $public_loaded = false,
        $content = '';

    public function admin_init() {
        add_menu_page(
            // ...
        );
    } // End of admin_init function

    public function get_random( ) {
        // ...
    }
} // End of example class

add_action('init', function() {
    global $example_class;

    // ***** Area A
    // Check for arbitrary variable sent with every user interaction
    if ( if ( isset( sanitize_key( $_REQUEST['tni'] ) ) ) {

        // ***** Area B
        /* Set the class variable `public_loaded` to true after it's
         * clear we're loading a public page which uses our plugin */
        $example_class->public_loaded = true;

        // Sanitize and set the action role
        $action = ( isset( $_REQUEST['action'] ) ) ? sanitize_key( $_REQUEST['action'] ) : NULL;
        // Manage submitted data
        switch ( $action ) {
            // ...
        } // End of switch for action

        // Sanitize and set the view role
        $view = ( isset( $_REQUEST['view'] ) ) ? sanitize_key( $_REQUEST['ex'] ) : 'get_all';
        // Manage submitted data
        switch ( $view ) {
            // ... Generate content and store in $this->content
        } // End of switch for view
    } // End of if page is being shown
});

add_action( 'admin_menu', function() {
    global $example_class;
    $example_class->admin_init();
});

add_shortcode( 'show_public_random', function () {
    global $example_class;
    // ***** Area C
    /* Check to see if page has loaded using the telltale sign
     * If not, load a default view - a random post */
    if ( $example_class->public_loaded === false ) {
        $example_class->content = $example_class->get_random();
        // ...
    }

    // Return the generated content
    return $example_class->content;
});

エリア A では、ユーザーがプラグインとのやり取りとともに変数を送信したかどうかを確認するための条件ステートメントを設定しました。プラグインが私のものである場合、コードが評価され、actionモードviewが評価されます。同様に、この関数はクラス変数public_loadedを true に設定します。

エリア C では、クラス変数が true に設定されているかどうかを確認するための修飾ステートメントを設定しました。そうでない場合は、ショートコードのデフォルト ビューが設定されます。

于 2013-10-14T00:22:55.930 に答える