diff --git a/README.txt b/README.md similarity index 85% rename from README.txt rename to README.md index 63b734c..36e0e96 100644 --- a/README.txt +++ b/README.md @@ -1,12 +1,13 @@ -/** - * CheezCap - Cheezburger Custom Administration Panel - * (c) 2008 - 2011 Cheezburger Network (Pet Holdings, Inc.) - * LOL: http://cheezburger.com - * Source: http://github.com/cheezburger/cheezcap/ - * Authors: Kyall Barrows, Toby McKes, Stefan Rusek, Scott Porad - * UnLOLs by Mo Jangda (batmoo@gmail.com) - * License: GNU General Public License, version 2 (GPL), http://www.gnu.org/licenses/gpl-2.0.html - */ +CheezCap - Cheezburger Custom Administration Panel +================ +* CheezCap - Cheezburger Custom Administration Panel +* (c) 2008 - 2011 Cheezburger Network (Pet Holdings, Inc.) +* LOL: http://cheezburger.com +* Source: http://github.com/cheezburger/cheezcap/ +* Authors: Kyall Barrows, Toby McKes, Stefan Rusek, Scott Porad +* UnLOLs by Mo Jangda (batmoo@gmail.com) +* License: GNU General Public License, version 2 (GPL), http://www.gnu.org/licenses/gpl-2.0.html + This is a fork of the original CheezCap developed by the fine Cheez-loving Cats over at ICHC. In has various bits of cleanup, the biggest being that it can be shared across multiple themes. @@ -20,24 +21,24 @@ The fork lives at https://github.com/mjangda/cheezcap 1. Copy the cheezcap folder into an appropriate location (maybe where you store your other shared plugins). 2. Add the following line to functions.php (if you don't have a functions.php, create one in your theme directory). Adjust the path as needed. - - require_once( WP_PLUGINS_DIR . '/cheezcap/cheezcap.php'); - +


+	require_once( WP_PLUGINS_DIR . '/cheezcap/cheezcap.php');
+

3. Use the included config-sample.php as a starting point to set up your options. Copy the modified version into your theme and include it. - - require_once( dirname( __FILE__ ) . '/cheezcap-config.php'); - +


+	require_once( dirname( __FILE__ ) . '/cheezcap-config.php');
+

4. Sprinkle theme options around your code, like this: - +


 	global $cap;
 	if ($cap->my_boolean_option) {
 		// do stuff	
 	}
-
+

4b. Or use the helper function - +


 	cheezcap_get_option( 'my_boolean_option', true, 'esc_html' );
-
+

5. Enjoy! @@ -149,3 +150,25 @@ CheezCap makes it easy to access the values that are set in your custom admin pa You can use the built-in helper function: cheezcap_get_option( $option, $echo = false, $sanitize_callback = '' ) + +## +## Actions +## + +Below are all of the actions you can hook in to: + +* cheezcap_update +* cheezcap_reset +* cheezcap_export +* cheezcap_import + +## +## Methods +## + +Calling the get_data method returns all of the input. + +``` + $data_arr = $cap->get_data(); +``` + diff --git a/cheezcap.php b/cheezcap.php index 3156db2..78abfc3 100644 --- a/cheezcap.php +++ b/cheezcap.php @@ -1,317 +1,406 @@ -post_ratings is the same as get_bool_option("cap_post_ratings", false) - */ -class CheezCap { - private $data = false; - private $cache = array(); - private $settings = array(); - private $options = array(); - - private $messages = array(); - - function __construct( $options, $settings = array() ) { - $settings = wp_parse_args( $settings, array( - 'themename' => 'CheezCap', - 'req_cap_to_edit' => 'manage_options', - 'cap_menu_position' => 99, - 'cap_icon_url' => '', - ) ); - - $settings['themeslug'] = sanitize_key( $settings['themename'] ); - - // Let's prevent accidentally allowing low-level users access to cap - if( ! in_array( $settings['req_cap_to_edit'], apply_filters( 'cheezcap_req_cap_to_edit_whitelist', array( 'manage_network', 'manage_options', 'edit_others_posts', 'publish_posts' ) ) ) ) - $settings['req_cap_to_edit'] = 'manage_options'; - - $this->settings = $settings; - $this->options = $options; - $this->messages = $this->get_default_messages(); - - add_action( 'admin_menu', array( $this, 'add_admin_page' ) ); - add_action( 'admin_init', array( $this, 'handle_admin_actions' ) ); - } - - function init() { - if ( $this->data ) - return; - - $this->data = array(); - $options = $this->get_options(); - - foreach ( $options as $group ) { - foreach( $group->options as $option ) { - $this->data[$option->_key] = $option; - } - } - } - - public function __get( $name ) { - $this->init(); - - if ( array_key_exists( $name, $this->cache ) ) - return $this->cache[$name]; - - $option = $this->data[$name]; - if ( empty( $option ) && defined( 'WP_DEBUG' ) && WP_DEBUG ) - throw new Exception( "Unknown key: $name" ); - elseif( empty( $option ) ) - $value = ''; - else - $value = $this->cache[$name] = $option->get(); - - return $value; - } - - public function get_options() { - return $this->options; - } - - public function get_settings() { - return $this->settings; - } - - public function get_setting( $setting, $default = '' ) { - if( isset( $this->settings[$setting] ) ) - return $this->settings[$setting]; - return $default; - } - - // UI-related functions - function add_admin_page() { - $page_name = sprintf( __( '%s Settings', 'cheezcap' ), esc_html( $this->get_setting( 'themename' ) ) ); - $page_hook = add_menu_page( $page_name, $page_name, $this->get_setting( 'req_cap_to_edit' ), $this->get_setting( 'themeslug' ), array( $this, 'display_admin_page' ), $this->get_setting( 'cap_icon_url' ), $this->get_setting( 'cap_menu_position' ) ); - - add_action( "admin_print_scripts-$page_hook", array( $this, 'admin_js_libs' ) ); - add_action( "admin_footer-$page_hook", array( $this, 'admin_js_footer' ) ); - add_action( "admin_print_styles-$page_hook", array( $this, 'admin_css' ) ); - } - - function handle_admin_actions() { - global $plugin_page; - - $themeslug = $this->get_setting( 'themeslug' ); - - if ( $plugin_page == $themeslug ) { - - $action = isset( $_POST['action'] ) ? strtolower( $_POST['action'] ) : ''; - - if( ! $action ) - return; - - check_admin_referer( $themeslug . '-action', $themeslug . '-nonce' ); - - if ( ! current_user_can ( $this->get_setting( 'req_cap_to_edit' ) ) ) - return; - - $options = $this->get_options(); - $method = false; - $done = false; - $redirect = false; - $data = new CheezCapImportData(); - - switch ( $action ) { - case 'save': - $method = 'update'; - $redirect = array( 'success' => $method ); - break; - - case 'reset': - $method = 'reset'; - $redirect = array( 'success' => $method ); - break; - - case 'export': - $method = 'export'; - $done = array( $this, 'serialize_export' ); - break; - - case 'import': - - $data = @ unserialize( file_get_contents( $_FILES['file']['tmp_name'] ) ); // We're using @ to suppress the E_NOTICE - - if( $data && is_a( $data, 'CheezCapImportData' ) ) { - $method = 'import'; - $redirect = array( 'success' => $method ); - } else { - $redirect = array( 'error' => 'import' ); - } - - break; - } - - if ( $method ) { - foreach ( $options as $group ) { - foreach ( $group->options as $option ) { - call_user_func( array( $option, $method ), $data ); - } - } - - if ( $done ) - call_user_func( $done, $data ); - } - - if( ! empty( $redirect ) ) - wp_redirect( add_query_arg( $redirect, menu_page_url( $plugin_page, false ) ) ); - - } - } - - function display_message( $type ) { - $theme_name = $this->get_setting( 'themename' ); - $message_key = sanitize_key( $_GET[ $type ] ); - $message = isset( $this->messages[$type][$message_key] ) ? $this->messages[$type][$message_key] : ''; - - $message_class = ( $type != 'error' ) ? 'updated' : $type; - - if( $message ) - echo sprintf( '

%1$s

', sprintf( $message, esc_html( $theme_name ) ), $message_class ); - } - - function display_admin_page() { - $themename = $this->get_setting( 'themename' ); - $themeslug = $this->get_setting( 'themeslug' ); - - if ( isset( $_GET['success'] ) ) - $this->display_message( 'success' ); - elseif ( isset( $_GET['error'] ) ) - $this->display_message( 'error' ); - - ?> - -
-

-
-
- - - -
- write_html(); ?> -
- -
- -

- - -

- - -
-
-

- -

-

- - - -

- -
-
-

- - - - array( - 'update' => __( 'Sweet! The settings for %s were saved!', 'cheezcap' ), - 'reset' => __( 'Yay! The settings for %s were reset!', 'cheezcap' ), - 'import' => __( 'Woo! The settings for %s were imported!', 'cheezcap' ) - ), - 'error' => array( - 'import' => __( 'That doesn\'t look like a CheezCap Export file. Homie don\'t play that!', 'cheezcap' ), - ) - ); - } - - function serialize_export( $data ) { - $filename = sprintf( '%s-%s-theme-export.txt', date( 'Y.m.d' ), sanitize_key( get_bloginfo( 'name' ) ) ); - header( 'Content-disposition: attachment; filename=' . $filename ); - echo serialize( $data ); - exit(); - } -} - -/** - * Access $cap option using the CheezCap option name - * - * @param mixed $option Option name - * @param bool $echo Should the value be echoed? - * @param string $sanitize_callback Callback function used to sanitize the returned value - */ -function cheezcap_get_option( $option, $echo = false, $sanitize_callback = '' ) { - global $cap; - - $value = $cap->$option; - - if( $sanitize_callback && is_callable( $sanitize_callback ) ) - $value = call_user_func( $sanitize_callback, $value ); - - if( $echo ) - echo $value; - else - return $value; -} +post_ratings is the same as get_bool_option("cap_post_ratings", false) + */ +class CheezCap { + + private $data = false; + private $cache = array(); + private $settings = array(); + private $options = array(); + private $messages = array(); + + function __construct( $options, $settings = array() ) { + $settings = wp_parse_args( $settings, array( + 'themename' => 'CheezCap', + 'req_cap_to_edit' => 'manage_options', + 'cap_menu_position' => 99, + 'cap_icon_url' => '', + ) ); + + $settings['themeslug'] = sanitize_key( $settings['themename'] ); + + // Let's prevent accidentally allowing low-level users access to cap + if ( ! in_array( $settings['req_cap_to_edit'], apply_filters( 'cheezcap_req_cap_to_edit_whitelist', array( 'manage_network', 'manage_options', 'edit_others_posts', 'publish_posts' ) ) ) ) { + $settings['req_cap_to_edit'] = 'manage_options'; + } + + $this->settings = $settings; + $this->options = $options; + $this->messages = $this->get_default_messages(); + + add_action( 'admin_menu', array( $this, 'add_admin_page' ) ); + add_action( 'admin_init', array( $this, 'handle_admin_actions' ) ); + + } + + function init() { + if ( $this->data ) { + return; + } + + $this->data = array(); + $options = $this->get_options(); + + foreach ( $options as $group ) { + foreach ( $group->options as $option ) { + $this->data[ $option->_key ] = $option; + } + } + + } + + public function __get( $name ) { + $this->init(); + + if ( array_key_exists( $name, $this->cache ) ) { + return $this->cache[ $name ]; + } + + $option = $this->data[ $name ]; + if ( empty( $option ) && defined( 'WP_DEBUG' ) && WP_DEBUG ) { + throw new Exception( "Unknown key: $name" ); + } + elseif ( empty( $option ) ) { + $value = ''; + } + else { + $value = $this->cache[ $name ] = $option->get(); + } + + return $value; + + } + + public function get_options() { + return $this->options; + + } + + public function get_settings() { + return $this->settings; + + } + + public function get_setting( $setting, $default = '' ) { + if ( isset( $this->settings[ $setting ] ) ) { + return $this->settings[ $setting ]; + } + return $default; + + } + + // UI-related functions + function add_admin_page() { + $page_name = sprintf( __( '%s Settings', 'cheezcap' ), esc_html( $this->get_setting( 'themename' ) ) ); + $page_hook = add_menu_page( $page_name, $page_name, $this->get_setting( 'req_cap_to_edit' ), $this->get_setting( 'themeslug' ), array( $this, 'display_admin_page' ), $this->get_setting( 'cap_icon_url' ), $this->get_setting( 'cap_menu_position' ) ); + + add_action( "admin_print_scripts-$page_hook", array( $this, 'admin_js_libs' ) ); + add_action( "admin_footer-$page_hook", array( $this, 'admin_js_footer' ) ); + add_action( "admin_print_styles-$page_hook", array( $this, 'admin_css' ) ); + + } + + function handle_admin_actions() { + global $plugin_page; + + $themeslug = $this->get_setting( 'themeslug' ); + + if ( $plugin_page == $themeslug ) { + + $action = isset( $_POST['action'] ) ? strtolower( esc_attr( $_POST['action'] ) ) : ''; + + if ( ! $action ) { + return; + } + + check_admin_referer( $themeslug . '-action', $themeslug . '-nonce' ); + + if ( ! current_user_can( $this->get_setting( 'req_cap_to_edit' ) ) ) { + return; + } + + $options = $this->get_options(); + $method = false; + $done = false; + $redirect = false; + $data = new CheezCapImportData(); + + switch ( $action ){ + case 'save': + $method = 'update'; + $redirect = array( 'success' => $method ); + break; + + case 'reset': + $method = 'reset'; + $redirect = array( 'success' => $method ); + break; + + case 'export': + $method = 'export'; + $done = array( $this, 'serialize_export' ); + break; + + case 'import': + + $data = @ unserialize( file_get_contents( $_FILES['file']['tmp_name'] ) ); // We're using @ to suppress the E_NOTICE + + if ( $data && is_a( $data, 'CheezCapImportData' ) ) { + $method = 'import'; + $redirect = array( 'success' => $method ); + } + else { + $redirect = array( 'error' => 'import' ); + } + + break; + } + + if ( $method ) { + foreach ( $options as $group ) { + foreach ( $group->options as $option ) { + call_user_func( array( $option, $method ), $data ); + } + } + + $this->trigger_action( $method ); + + if ( $done ) { + call_user_func( $done, $data ); + } + } + + if ( ! empty( $redirect ) ) { + wp_redirect( add_query_arg( $redirect, menu_page_url( $plugin_page, false ) ) ); + } + } + + } + + function display_message( $type ) { + + $theme_name = $this->get_setting( 'themename' ); + $message_key = ''; + + if ( isset ( $_GET[ $type ] ) ) { + $message_key = sanitize_key( $_GET[ $type ] ); + } + + $message = isset( $this->messages[ $type ][ $message_key ] ) ? $this->messages[ $type ][ $message_key ] : ''; + + $message_class = ( $type != 'error' ) ? 'updated' : $type; + + if ( $message ) { + echo esc_attr( sprintf( '

%1$s

', sprintf( $message, esc_html( $theme_name ) ), $message_class ) ); + } + + } + + function display_admin_page() { + $themename = $this->get_setting( 'themename' ); + $themeslug = $this->get_setting( 'themeslug' ); + + if ( isset( $_GET['success'] ) ) { + $this->display_message( 'success' ); + } + elseif ( isset( $_GET['error'] ) ) { + $this->display_message( 'error' ); + } + ?> + +
+

+
+
+ + + +
+ write_html(); ?> +
+ +
+ +

+ + +

+ + +
+
+

+ +

+

+ + + +

+ +
+
+

+ + + + array( + 'update' => __( 'Sweet! The settings for %s were saved!', 'cheezcap' ), + 'reset' => __( 'Yay! The settings for %s were reset!', 'cheezcap' ), + 'import' => __( 'Woo! The settings for %s were imported!', 'cheezcap' ) + ), + 'error' => array( + 'import' => __( 'That doesn\'t look like a CheezCap Export file. Homie don\'t play that!', 'cheezcap' ), + ) + ); + + } + + function serialize_export( $data ) { + $filename = sprintf( '%s-%s-theme-export.txt', date( 'Y.m.d' ), sanitize_key( get_bloginfo( 'name' ) ) ); + header( 'Content-disposition: attachment; filename=' . $filename ); + echo serialize( $data ); + exit(); + + } + + function trigger_action( $method ) { + + switch ( $method ){ + + case 'update': + do_action( 'cheezcap_update' ); + break; + + case 'reset': + do_action( 'cheezcap_reset' ); + break; + + case 'export': + do_action( 'cheezcap_export' ); + break; + + case 'import': + do_action( 'cheezcap_import' ); + break; + } + + } + + /** + * returns all of the cheezcap data + * + * @return \CheezCapImportData + */ + public function get_data() { + + $options = $this->get_options(); + $data = new CheezCapImportData(); + + foreach ( $options as $group ) { + + foreach ( $group->options as $option ) { + + call_user_func( array( $option, 'export' ), $data ); + } + } + + return $data; + + } + +} + +/** + * Access $cap option using the CheezCap option name + * + * @param mixed $option Option name + * @param bool $echo Should the value be echoed? + * @param string $sanitize_callback Callback function used to sanitize the returned value + */ +function cheezcap_get_option( $option, $echo = false, $sanitize_callback = '' ) { + global $cap; + + $value = $cap->$option; + + if ( $sanitize_callback && is_callable( $sanitize_callback ) ) { + $value = call_user_func( $sanitize_callback, $value ); + } + + if ( $echo ) { + echo $value; + } + else { + return $value; + } + +}