cmb = $cmb; $this->option_key = $option_key; } public function hooks() { if ( empty( $this->option_key ) ) { return; } // Register setting to cmb2 group. register_setting( 'cmb2', $this->option_key ); // Handle saving the data. add_action( 'admin_post_' . $this->option_key, array( $this, 'save_options' ) ); // Optionally network_admin_menu. $hook = $this->cmb->prop( 'admin_menu_hook' ); // Hook in to add our menu. add_action( $hook, array( $this, 'options_page_menu_hooks' ) ); // If in the network admin, need to use get/update_site_option. if ( 'network_admin_menu' === $hook ) { // Override CMB's getter. add_filter( "cmb2_override_option_get_{$this->option_key}", array( $this, 'network_get_override' ), 10, 2 ); // Override CMB's setter. add_filter( "cmb2_override_option_save_{$this->option_key}", array( $this, 'network_update_override' ), 10, 2 ); } } /** * Hook up our admin menu item and admin page. * * @since 2.2.5 * * @return void */ public function options_page_menu_hooks() { $parent_slug = $this->cmb->prop( 'parent_slug' ); $title = $this->cmb->prop( 'title' ); $menu_title = $this->cmb->prop( 'menu_title', $title ); $capability = $this->cmb->prop( 'capability' ); $callback = array( $this, 'options_page_output' ); if ( $this->cmb->prop( 'cmb_styles' ) ) { // Include CMB CSS in the head to avoid FOUC add_action( "admin_print_styles-{$page_hook}", array( 'CMB2_hookup', 'enqueue_cmb_css' ) ); } if ( ! empty( $_GET['settings-updated'] ) ) { if ( 'true' === $_GET['settings-updated'] ) { add_settings_error( "{$this->option_key}-notices", '', __( 'Settings updated.', 'ascend' ), 'updated' ); } else { add_settings_error( "{$this->option_key}-notices", '', __( 'Nothing to update.', 'ascend' ), 'notice-warning' ); } } } /** * Display options-page output. To override, set 'display_cb' box property. * * @since 2.2.5 */ public function options_page_output() { $this->maybe_output_settings_notices(); $callback = $this->cmb->prop( 'display_cb' ); if ( is_callable( $callback ) ) { return $callback( $this ); } ?>
cmb->prop( 'title' ) ) : ?>

cmb->prop( 'title' ) ); ?>

options_page_metabox(); ?> cmb->prop( 'save_button' ) ), 'primary', 'submit-cmb' ); ?>
cmb->prop( 'disable_settings_errors' ) ) { settings_errors( "{$this->option_key}-notices" ); } } /** * Display metaboxes for an options-page object. * * @since 2.2.5 */ public function options_page_metabox() { $this->show_form_for_type( 'options-page' ); } /** * Save data from options page, then redirects back. * * @since 2.2.5 * @return void */ public function save_options() { $url = wp_get_referer(); if ( ! $url ) { $url = admin_url(); } if ( $this->can_save( 'options-page' ) // check params && isset( $_POST['submit-cmb'], $_POST['action'] ) && $this->option_key === $_POST['action'] ) { $updated = $this->cmb ->save_fields( $this->option_key, $this->cmb->object_type(), $_POST ) ->was_updated(); // Will be false if no values were changed/updated. $url = add_query_arg( 'settings-updated', $updated ? 'true' : 'false', $url ); } wp_safe_redirect( esc_url_raw( $url ), WP_Http::SEE_OTHER ); exit; } /** * Replaces get_option with get_site_option * @since 2.2.5 * @return mixed Value set for the network option. */ public function network_get_override( $test, $default = false ) { return get_site_option( $this->option_key, $default ); } /** * Replaces update_option with update_site_option * @since 2.2.5 * @return bool Success/Failure */ public function network_update_override( $test, $option_value ) { return update_site_option( $this->option_key, $option_value ); } /** * Magic getter for our object. * * @param string $field * @throws Exception Throws an exception if the field is invalid. * @return mixed */ public function __get( $field ) { switch ( $field ) { case 'object_type': case 'option_key': case 'cmb': return $this->{$field}; default: throw new Exception( sprintf( esc_html__( 'Invalid %1$s property: %2$s', 'ascend' ), __CLASS__, $field ) ); } } }