Skip to content

Commit

Permalink
Added Network Options page example
Browse files Browse the repository at this point in the history
  • Loading branch information
dmhendricks committed Mar 17, 2018
1 parent 9f896ae commit 0380f04
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Here are some ways that you can contribute:
* Object caching (where available; [usage examples](https://github.com/dmhendricks/wordpress-toolkit/wiki/ObjectCache))
* Easy installable ZIP file generation: `npm run zip`
* Automatic translation file (`.pot`) creation. See [Translation](https://github.com/dmhendricks/wordpress-base-plugin/wiki/Translation).
* Shortcodes, widgets (via [Carbon Fields](https://carbonfields.net)) and custom post types (via [PostTypes](https://github.com/jjgrainger/PostTypes/)) examples
* Network/multi-site options, shortcodes, widgets (via [Carbon Fields](https://carbonfields.net)) and custom post types (via [PostTypes](https://github.com/jjgrainger/PostTypes/)) examples
* Configuration registry ([docs](https://github.com/dmhendricks/wordpress-toolkit/wiki/ConfigRegistry)) and optional `wp-config.php` [constants](https://github.com/dmhendricks/wordpress-base-plugin/wiki/Configuration-&-Constants)
* Customizer options
* Define environmental variables via `.env` files ([reference](https://github.com/dmhendricks/wordpress-toolkit/wiki/ToolKit#environment))
Expand Down Expand Up @@ -90,6 +90,8 @@ Release changes are noted on the [Releases](https://github.com/dmhendricks/wordp
* Moved `is_ajax()`/`prefix()` methods, `CacheObject()` init to wordpress-toolkit
* Updated Composer license to conform to new [SPDX](https://spdx.org/licenses/) identifiers
* Added [phpdotenv](https://github.com/etelford/phpdotenv) support ([reference](https://github.com/dmhendricks/wordpress-toolkit/wiki/ToolKit#environment))
* Added `npm run zip-dev` to create installable ZIP including necessary development files
* Added Network/Multi-site options page example

## Screenshot

Expand Down
18 changes: 17 additions & 1 deletion app/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ function __construct() {

// Example - Remove Emoji code from header
if( $this->get_carbon_plugin_option( 'remove_header_emojicons' ) ) {
if( !$this->is_ajax() ) add_filter( 'init', array( $this, 'disable_wp_emojicons' ) );
if( !$this->is_ajax() ) add_filter( 'init', array( &$this, 'disable_wp_emojicons' ) );
}

// Multisite Example - Change WP Admin footer text
if( is_multisite() && trim( $this->get_carbon_network_option( 'network_site_footer' ) ) ) {
add_filter( 'admin_footer_text', array( &$this, 'set_admin_footer_text' ) );
}

/**
Expand Down Expand Up @@ -74,6 +79,17 @@ public function disable_wp_emojicons() {

}

/**
* Set WP Admin footer text (multisite only)
*
* @param string Default footer text
* @return string Modified footer text
* @since 0.5.0
*/
public function set_admin_footer_text( $footer_text ) {
return trim( $this->get_carbon_network_option( 'network_site_footer' ) ) ?: $footer_text;
}

/**
* Add "Clear Cache" link to admin bar dropdown
* @since 0.3.0
Expand Down
5 changes: 4 additions & 1 deletion app/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public function load_plugin() {
// Add admin settings page using Carbon Fields framework
new Settings\Carbon_Page();

// Add a settings page to the Network Admin (requires multisite)
if ( is_multisite() ) new Settings\Network_Settings_Page();

// Alternatively, add admin settings page using wordpress-settings-api-class
new Settings\WPSAC_Page();

Expand Down Expand Up @@ -214,7 +217,7 @@ public static function get_carbon_plugin_option( $key, $cache = true ) {
* @param int $site_id The network site ID to use - default: SITE_ID_CURRENT_SITE
* @return mixed The value of specified Carbon Fields option key
* @link https://carbonfields.net/docs/containers-usage/ Carbon Fields containers
* @since 0.4.1
* @since 0.5.0
*
*/
public static function get_carbon_network_option( $key, $container = null, $cache = true, $site_id = null ) {
Expand Down
54 changes: 54 additions & 0 deletions app/Settings/Network_Settings_Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
namespace VendorName\PluginName\Settings;
use VendorName\PluginName\Plugin;
use Carbon_Fields\Datastore\Datastore\Serialized_Theme_Options_Datastore;
use Carbon_Fields\Container;
use Carbon_Fields\Field;

/**
* A class to create a settings page in Network Admin
*
* @link https://carbonfields.net/docs/containers-network/ Carbon Fields Network Container
* @since 0.5.0
*/
class Network_Settings_Page extends Plugin {

public function __construct() {

// Flush the cache when settings are saved
add_action( 'carbon_fields_network_container_saved', array( $this, 'options_saved_hook' ) );

// Create tabbed plugin options page (Settings > Plugin Name)
$this->create_network_options_page();

}

/**
* Create network options/settings page in WP Network Admin > Settings > Global Settings
*
* @since 0.5.0
*/
public function create_network_options_page() {

Container::make( 'network', $this->prefix( self::$config->get( 'network/default_options_container' ) ), __( 'Global Settings', self::$textdomain ) )
->set_page_parent( 'settings.php' )
->add_tab( __( 'General', self::$textdomain ), array(
Field::make( 'textarea', $this->prefix( 'network_site_footer' ), __( 'WP Admin Site Footer', self::$textdomain ) )
->help_text( __( 'Replaces the WP Admin footer text. Leave blank for default.', self::$textdomain ) )
->set_rows( 2 )
)
);

}

/**
* Logic that is run when settings are saved.
*/
public function options_saved_hook() {

// Clear the cache so that new settings are loaded
self::$cache->flush();

}

}

0 comments on commit 0380f04

Please sign in to comment.