| 1 |
<?php |
| 2 |
/** |
| 3 |
* Flash Sidebars |
| 4 |
* |
| 5 |
* Handles the building of the Sidebars on the fly. |
| 6 |
* |
| 7 |
* @class FT_Sidebars |
| 8 |
* @version 1.0.0 |
| 9 |
* @package FlashToolkit/Classes |
| 10 |
* @category Class |
| 11 |
* @author ThemeGrill |
| 12 |
*/ |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* FT_Sidebars Class |
| 20 |
*/ |
| 21 |
class FT_Sidebars { |
| 22 |
|
| 23 |
/** |
| 24 |
* Hook in tabs. |
| 25 |
*/ |
| 26 |
public function __construct() { |
| 27 |
add_action( 'widgets_admin_page', array( $this, 'output_sidebar_tmpl' ) ); |
| 28 |
add_action( 'load-widgets.php', array( $this, 'add_custom_sidebars' ), 100 ); |
| 29 |
add_action( 'widgets_init', array( $this, 'register_custom_sidebars' ), 1000 ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Add a sidebar. |
| 34 |
* @param string $name |
| 35 |
*/ |
| 36 |
public static function add_sidebar( $name ) { |
| 37 |
$sidebars = array_unique( array_merge( get_option( 'flash_toolkit_custom_sidebars', array() ), array( $name ) ) ); |
| 38 |
update_option( 'flash_toolkit_custom_sidebars', $sidebars ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Remove a sidebar. |
| 43 |
* @param string $name |
| 44 |
*/ |
| 45 |
public static function remove_sidebar( $name ) { |
| 46 |
$sidebars = array_diff( get_option( 'flash_toolkit_custom_sidebars', array() ), array( $name ) ); |
| 47 |
update_option( 'flash_toolkit_custom_sidebars', $sidebars ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Remove all sidebars. |
| 52 |
*/ |
| 53 |
public static function remove_all_sidebars() { |
| 54 |
delete_option( 'flash_toolkit_custom_sidebars' ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Validate sidebar name to prevent collisions. |
| 59 |
* @param string $sidebar_name Raw sidebar name. |
| 60 |
* @return string $sidebar_name Valid sidebar name. |
| 61 |
*/ |
| 62 |
public static function validate_sidebar_name( $sidebar_name ) { |
| 63 |
global $wp_registered_sidebars; |
| 64 |
|
| 65 |
// Get the existing sidebars. |
| 66 |
$existing_sidebars = array(); |
| 67 |
foreach ( $wp_registered_sidebars as $sidebar ) { |
| 68 |
$existing_sidebars[] = $sidebar['name']; |
| 69 |
} |
| 70 |
|
| 71 |
// Rename if sidebar exists. |
| 72 |
if ( in_array( $sidebar_name, $existing_sidebars ) ) { |
| 73 |
$count = substr( $sidebar_name, -1 ); |
| 74 |
$rename = is_numeric( $count ) ? ( substr( $sidebar_name, 0, -1 ) . ( (int) $count + 1 ) ) : ( $sidebar_name . ' - 1' ); |
| 75 |
$sidebar_name = self::validate_sidebar_name( $rename ); |
| 76 |
} |
| 77 |
|
| 78 |
return $sidebar_name; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Output Sidebar Templates. |
| 83 |
*/ |
| 84 |
public function output_sidebar_tmpl() { |
| 85 |
include_once( 'admin/views/html-admin-tmpl-sidebars.php' ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Add a sidebar if the POST variable is set. |
| 90 |
*/ |
| 91 |
public function add_custom_sidebars() { |
| 92 |
if ( ! empty( $_POST['flash-toolkit-add-sidebar'] ) && isset( $_POST['_flash_toolkit_sidebar_nonce'] ) ) { |
| 93 |
if ( ! wp_verify_nonce( $_POST['_flash_toolkit_sidebar_nonce'], 'flash_toolkit_add_sidebar' ) ) { |
| 94 |
wp_die( __( 'Action failed. Please refresh the page and retry.', 'flash-toolkit' ) ); |
| 95 |
} |
| 96 |
|
| 97 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 98 |
wp_die( __( 'Cheatin’ huh?', 'flash-toolkit' ) ); |
| 99 |
} |
| 100 |
|
| 101 |
$sidebar_name = flash_clean( $_POST['flash-toolkit-add-sidebar'] ); |
| 102 |
self::add_sidebar( self::validate_sidebar_name( $sidebar_name ) ); |
| 103 |
wp_redirect( admin_url( 'widgets.php' ) ); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Register Custom Widgets Area (Sidebars). |
| 109 |
*/ |
| 110 |
public function register_custom_sidebars() { |
| 111 |
$args = apply_filters( 'flash_toolkit_custom_widget_args', array( |
| 112 |
'before_widget' => '<aside id="%1$s" class="widget clearfix %2$s">', |
| 113 |
'after_widget' => '<span class="seperator extralight-border"></span></aside>', |
| 114 |
'before_title' => '<h3 class="widget-title">', |
| 115 |
'after_title' => '</h3>' |
| 116 |
) ); |
| 117 |
|
| 118 |
$sidebars = get_option( 'flash_toolkit_custom_sidebars', array() ); |
| 119 |
|
| 120 |
foreach ( (array) $sidebars as $id => $name ) { |
| 121 |
$args['name'] = $name; |
| 122 |
$args['id'] = 'flash-toolkit-sidebar-' . ++$id; |
| 123 |
$args['class'] = 'flash-toolkit-custom-widgets-area'; |
| 124 |
$args['description'] = sprintf( __( 'Custom Widget Area of the site - %s ', 'flash-toolkit' ), $name ); |
| 125 |
register_sidebar( $args ); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
new FT_Sidebars(); |
| 131 |
|