Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 172 lines (156 sloc) 5.489 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
<?php
/**
* SnS_Settings_Page
*
* Allows WordPress admin users the ability to add custom CSS
* and JavaScript directly to individual Post, Pages or custom
* post types.
*/

class SnS_Settings_Page
{
/**
* Constants
*/
const MENU_SLUG = 'sns_settings';

/**
* Initializing method.
* @static
*/
static function init() {
$hook_suffix = add_submenu_page( SnS_Admin::$parent_slug, __( 'Scripts n Styles', 'scripts-n-styles' ), __( 'Settings' ), 'unfiltered_html', self::MENU_SLUG, array( 'SnS_Form', 'page' ) );

add_action( "load-$hook_suffix", array( __CLASS__, 'admin_load' ) );
add_action( "load-$hook_suffix", array( 'SnS_Admin', 'help' ) );
add_action( "load-$hook_suffix", array( 'SnS_Form', 'take_action' ), 49 );
add_action( "admin_print_styles-$hook_suffix", array( __CLASS__, 'admin_enqueue_scripts' ) );

// Make the page into a tab.
if ( SnS_Admin::MENU_SLUG != SnS_Admin::$parent_slug ) {
remove_submenu_page( SnS_Admin::$parent_slug, self::MENU_SLUG );
add_filter( 'parent_file', array( __CLASS__, 'parent_file') );
}
}
static function parent_file( $parent_file ) {
global $plugin_page, $submenu_file;
if ( self::MENU_SLUG == $plugin_page ) $submenu_file = SnS_Admin::MENU_SLUG;
return $parent_file;
}

static function admin_enqueue_scripts() {
$options = get_option( 'SnS_options' );
$cm_theme = isset( $options[ 'cm_theme' ] ) ? $options[ 'cm_theme' ] : '';

wp_enqueue_style( 'sns-options' );

wp_enqueue_script( 'sns-settings-page' );
wp_localize_script( 'sns-settings-page', 'codemirror_options', array( 'theme' => $cm_theme ) );
}

/**
* Settings Page
* Adds Admin Menu Item via WordPress' "Administration Menus" API. Also hook actions to register options via WordPress' Settings API.
*/
static function admin_load() {
wp_enqueue_style( 'sns-options' );

register_setting(
SnS_Admin::OPTION_GROUP,
'SnS_options' );

add_settings_section(
'settings',
__( 'Scripts n Styles Settings', 'scripts-n-styles' ),
array( __CLASS__, 'settings_section' ),
SnS_Admin::MENU_SLUG );

add_settings_field(
'metabox',
__( '<strong>Hide Metabox by default</strong>: ', 'scripts-n-styles' ),
array( 'SnS_Form', 'radio' ),
SnS_Admin::MENU_SLUG,
'settings',
array(
'label_for' => 'metabox',
'setting' => 'SnS_options',
'choices' => array( 'yes', 'no' ),
'layout' => 'horizontal',
'default' => 'yes',
'legend' => __( 'Hide Metabox by default', 'scripts-n-styles' ),
'description' => __( '<span class="description" style="max-width: 500px; display: inline-block;">This is overridable via Screen Options on each edit screen.</span>', 'scripts-n-styles' )
) );

add_settings_field(
'menu_position',
__( '<strong>Menu Position</strong>: ', 'scripts-n-styles' ),
array( 'SnS_Form', 'radio' ),
SnS_Admin::MENU_SLUG,
'settings',
array(
'label_for' => 'menu_position',
'setting' => 'SnS_options',
'choices' => array( 'menu', 'object', 'utility', 'tools.php', 'options-general.php', 'themes.php' ),
'default' => 'tools.php',
'legend' => __( 'Theme', 'scripts-n-styles' ),
'layout' => 'vertical',
'description' => __( '<span class="description" style="max-width: 500px; display: inline-block;">Some people are fussy about where the menu goes, so I made an option.</span>', 'scripts-n-styles' ),
) );

add_settings_section(
'demo',
__( 'Code Mirror Demo', 'scripts-n-styles' ),
array( __CLASS__, 'demo_section' ),
SnS_Admin::MENU_SLUG );

add_settings_field(
'cm_theme',
__( '<strong>Theme</strong>: ', 'scripts-n-styles' ),
array( 'SnS_Form', 'radio' ),
SnS_Admin::MENU_SLUG,
'demo',
array(
'label_for' => 'cm_theme',
'setting' => 'SnS_options',
'choices' => Scripts_n_Styles::$cm_themes,
'default' => 'default',
'legend' => __( 'Theme', 'scripts-n-styles' ),
'layout' => 'horizontal',
'description' => '',
) );
add_settings_field(
'hoops_widget',
__( '<strong>Hoops Widgets</strong>: ', 'scripts-n-styles' ),
array( 'SnS_Form', 'radio' ),
SnS_Admin::MENU_SLUG,
'settings',
array(
'label_for' => 'hoops_widget',
'setting' => 'SnS_options',
'choices' => array( 'yes', 'no' ),
'layout' => 'horizontal',
'default' => 'no',
'legend' => __( 'Shortcode Widgets', 'scripts-n-styles' ),
'description' => __( '<span class="description" style="max-width: 500px; display: inline-block;">This enables Hoops shortcodes to be used via a "Hoops" Text Widget.</span>', 'scripts-n-styles' )
) );
}

/**
* Settings Page
* Outputs Description text for the Global Section.
*/
static function settings_section() {
?>
<div style="max-width: 55em;">
<p><?php _e( 'Control how and where Scripts n Styles menus and metaboxes appear. These options are here because sometimes users really care about this stuff. Feel free to adjust to your liking. :-)', 'scripts-n-styles' ) ?></p>
</div>
<?php
}

/**
* Settings Page
* Outputs Description text for the Global Section.
*/
static function demo_section() {
?>
<div style="max-width: 55em;">
<textarea id="codemirror_demo" name="code" style="min-width: 500px; width:97%;" rows="5" cols="40">
<?php echo esc_textarea( '<?php
function hello($who) {
return "Hello " . $who;
}
?>
<p>The program says <?= hello("World") ?>.</p>
<script>
alert("And here is some JS code"); // also colored
</script>' ); ?>
</textarea>
</div>
<?php
}
}
?>
Something went wrong with that request. Please try again.