WP插件+OOP:添加菜单页面不会产生所需效果

时间:2018-04-01 作者:EBennett

所以,我已经关注这个问题好几天了。我正试图进一步提高我在PHP开发方面的技能,并决定采用面向对象的方法来处理一个WP插件。我知道WP不一定是最好的选择,但我喜欢好的挑战。

我正在编写一个助手类,因为我打算定期使用一些函数,但我遇到了一点问题。

助手类

Class Helper implements HelperInterface
{

/**
 * Default Options for making an admin page
 *
 * @var array
 */
public $default_options = [
    \'slug\'          =>      \'\',
    \'title\'         =>      \'\',
    \'page_title\'    =>      \'\',
    \'parent\'        =>      null,
    \'id\'            =>      \'\',
    \'capability\'    =>      \'update_core\',
    \'icon\'          =>      \'dashicons-admin-generic\',
    \'position\'      =>      null,
    \'file_name\'     =>      null,
    \'desc\'          =>      \'\',
];

/**
 * Store options passed in by user.
 *
 * @var array
 */
public $options = [];

public $parent_id;

public $settings_id;

/**
 * Stores the media type for a stylesheet
 *
 * @var string
 */
public $media_type;

public function __construct(  )
{

}

public function add_new_page( $opt )
{   
    $this->options = array_merge( $this->default_options, $opt );
    add_action( \'admin_menu\', function() {
        
        add_menu_page(
            $this->options[\'page_title\'],
            $this->options[\'title\'],
            $this->options[\'capability\'],
            $this->options[\'slug\'],
            array($this, display_page_template),
            $this->options[\'icon\'],
            $this->options[\'position\']
        );
    } );

}
助手类用法
Class GoogleMaps 
{

protected $helper;

public function __construct()
{
    $this->helper = new Helper;
    $this->helper->add_new_page([
        \'slug\'          =>      \'google-maps\',
        \'title\'         =>      \'EdsGoogleMaps\',
        \'page_title\'    =>      \'EdsGoogleMaps\',
        \'capability\'    =>      \'update_core\',
        \'icon\'          =>      \'dashicons-admin-generic\',
        \'position\'      =>      null,
        \'file_name\'     =>      \'GoogleMapsHome.php\',
    ]);
    
    $this->helper->add_new_page([
        \'slug\'          =>      \'google-maps-2\',
        \'title\'         =>      \'EdsGoogleMaps2\',
        \'page_title\'    =>      \'EdsGoogleMaps2\',
        \'capability\'    =>      \'update_core\',
        \'icon\'          =>      \'dashicons-admin-generic\',
        \'position\'      =>      null,
        \'file_name\'     =>      \'GoogleMapsHome.php\',
    ]);

    $this->helper->add_new_page([
        \'slug\'          =>      \'google-maps-3\',
        \'title\'         =>      \'EdsGoogleMaps3\',
        \'page_title\'    =>      \'EdsGoogleMaps3\',
        \'capability\'    =>      \'update_core\',
        \'icon\'          =>      \'dashicons-admin-generic\',
        \'position\'      =>      null,
        \'file_name\'     =>      \'GoogleMapsHome.php\',
    ]);
    
    }
}
电流输出

enter image description here

<我尝试过将数组放入foreach循环。不起作用,因为,将两次返回第一个或再次返回所有三个

希望有人对此有某种解决方案的想法,希望我对我的问题已经足够清楚了。

谢谢:)

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

匿名函数的调用是在add_new_page. 然后需要存储所有元素来创建菜单项。

尝试这样修改类帮助器

public function __construct() // method to modify
{

    add_action("admin_menu", [$this, "add_admin_menu_items"]);

}

public function add_new_page( $opt ) // method to modify
{
    $element = array_merge( $this->default_options, $opt );

    $this->options[] = $element;

}


public function add_admin_menu_items() // method to create
{

    $helper = $this;


    foreach ($this->options as $element) {

        add_menu_page(
            $element[\'page_title\'],
            $element[\'title\'],
            $element[\'capability\'],
            $element[\'slug\'],
            function () use ($helper, $element) {

                $helper->display_page_template($element);

            },
            $element[\'icon\'],
            $element[\'position\']
        );

    }


}


public function display_page_template($itemDatas)
{

    ?>

        <pre><?php
            echo htmlspecialchars(var_export($itemDatas, TRUE));
        ?></pre>

    <?php

}

}

结束