| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle the settings menu. |
| 4 |
* |
| 5 |
* @link https://bootstrapped.ventures |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @package BV_Settings |
| 9 |
* @author Brecht Vandersmissen <[email protected]> |
| 10 |
*/ |
| 11 |
|
| 12 |
class BV_Menu { |
| 13 |
private $bvs; |
| 14 |
|
| 15 |
/** |
| 16 |
* Store main instance and initialize. |
| 17 |
* |
| 18 |
* @since 1.0.0 |
| 19 |
*/ |
| 20 |
public function __construct( $bvs ) { |
| 21 |
$this->bvs = $bvs; |
| 22 |
$this->init(); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Register actions and filters. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
private function init() { |
| 31 |
add_action( 'admin_menu', array( $this, 'add_submenu_page' ), $this->bvs->atts['menu_priority'] ); |
| 32 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Add the settings menu page. |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
*/ |
| 40 |
public function add_submenu_page() { |
| 41 |
add_submenu_page( $this->bvs->atts['menu_parent'], $this->bvs->atts['page_title'], $this->bvs->atts['menu_title'], $this->bvs->atts['required_capability'], $this->bvs->atts['page_slug'], array( $this, 'settings_page_template' ) ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get the template for the settings page. |
| 46 |
* |
| 47 |
* @since 1.0.0 |
| 48 |
*/ |
| 49 |
public function settings_page_template() { |
| 50 |
wp_localize_script( 'bv-settings', 'bv_settings', array( |
| 51 |
'structure' => array_values( $this->bvs->get_structure( true ) ), |
| 52 |
'settings' => $this->bvs->get_settings_with_defaults(), |
| 53 |
'defaults' => $this->bvs->get_defaults(), |
| 54 |
'required_addons' => apply_filters( $this->bvs->atts['uid'] . '_settings_required_addons', $this->bvs->atts['required_addons'] ), |
| 55 |
'eol' => PHP_EOL, |
| 56 |
'api' => array( |
| 57 |
'endpoint' => get_rest_url( null, 'bv-settings/v1/' . $this->bvs->atts['uid'] ), |
| 58 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 59 |
), |
| 60 |
) ); |
| 61 |
|
| 62 |
echo '<div id="bvs-settings" class="wrap">Loading...</div>'; |
| 63 |
} |
| 64 |
|
| 65 |
public function enqueue_admin() { |
| 66 |
$screen = get_current_screen(); |
| 67 |
|
| 68 |
// Only enqueue on settings page. |
| 69 |
if ( $this->bvs->atts['page_slug'] === substr( $screen->id, -1 * strlen( $this->bvs->atts['page_slug'] ) ) ) { |
| 70 |
wp_enqueue_style( 'bv-settings', BVS_URL . 'dist/admin.css', array(), BVS_VERSION, 'all' ); |
| 71 |
wp_enqueue_script( 'bv-settings', BVS_URL . 'dist/admin.js', array(), BVS_VERSION, true ); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|