theme['authorURI'] = $theme->{'Author URI'}; } else { $theme = wp_get_theme( get_stylesheet_directory() . '/style.css' ); $this->theme['authorURI'] = $theme['AuthorURI']; } // Set constants for theme info $this->theme['name'] = $theme['Name']; $this->theme['nicename'] = strtolower( str_replace( " ", "-", $this->theme['name'] ) ); $this->theme['version'] = $theme['Version']; $this->theme['author'] = $theme['Author']; $this->theme['uri'] = $this->theme['authorURI'] . "themes/" . $this->theme['nicename']; $this->theme['support'] = $this->theme['authorURI'] . 'support/'; $this->theme['readme'] = get_template_directory_uri() . '/readme.txt'; // This will keep track of the checkbox options for the formHandler function. $this->checkboxes = array(); $this->settings = array(); $this->getSettings(); add_action( 'admin_menu', array( &$this, 'addPages' ) ); add_action( 'admin_init', array( &$this, 'registerSettings' ) ); add_action( 'admin_notices', array( &$this, 'updateNotice') ); add_action( 'wp_ajax_readme', array( &$this, 'displayReadme' ) ); /* if ( ! get_option( 'blogy_options' ) ) $this->initializeSettings(); */ } /** * Add options page * * */ public function addPages() { $admin_page = add_theme_page( __( 'Theme Options', 'blogy' ), __( 'Theme Options', 'blogy' ), 'manage_options', 'blogy-options', array( &$this, 'displayPage' ) ); add_action( 'admin_print_scripts-' . $admin_page, array( &$this, 'addScripts' ) ); add_action( 'admin_print_styles-' . $admin_page, array( &$this, 'addStyles' ) ); } /** * Create settings field * * */ public function createSetting( $args = array() ) { $defaults = array( 'id' => 'default_field', 'title' => __( 'Default Field', 'blogy' ), 'desc' => __( 'This is a default description.', 'blogy' ), 'std' => '', 'type' => 'text', 'section' => 'general', 'choices' => array(), 'class' => '', 'html' => '' ); extract( wp_parse_args( $args, $defaults ) ); $field_args = array( 'type' => $type, 'id' => $id, 'desc' => $desc, 'std' => $std, 'choices' => $choices, 'label_for' => $id, 'class' => $class, 'html' => $html ); if ( $type == 'checkbox' ) $this->checkboxes[] = $id; add_settings_field( $id, $title, array( $this, 'buildOptions' ), 'blogy-options', $section, $field_args ); } public function updateNotice( $notice=null, $message=null ) { if ( is_null( $message ) ) { $message = __( 'Settings updated.', 'blogy' ); } // our default notice if ( is_null( $notice ) && ! empty( $_GET['settings-updated'] ) ) { $notice = $_GET['settings-updated']; } // additional classes for styling and fading // i.e. 'updated other class more class' $classes = __( 'updated ', 'blogy' ); if ( $notice ) { add_settings_error( 'blogy-notices', 'blogy-updated', $message, $classes ); } settings_errors( 'blogy-notices' ); } /** * Display options page * * */ public function displayPage() { echo '
'; screen_icon( 'themes' ); echo '

' . __( 'Theme Options', 'blogy' ) . '

'; echo ''; echo '
'; $this->updateNotice(); settings_fields( 'blogy_options' ); // show tabbed interface if ( $this->tabbed == true ) { $tab_html = null; foreach ( $this->sections as $section_slug => $section ) { $tab_html .= '
  • ' . $section . '
  • '; } print '
    '; print '
      '.$tab_html.'
    '; do_settings_sections( $_GET['page'] ); print '
    '; } else { // show one page of options do_settings_sections( $_GET['page'] ); } echo '

    '; echo '
    '; } /** * Description for section * * */ public function displaySection() {} /** * Parse the readme.txt. Shown in thickbox using wp_ajax_readme call. * * @uses parseFile() * @return html parsed from markdown in txt * */ public function displayReadme() { print $this->parseFile( $this->theme['readme'] ); exit(); } /** * HTML output for text field * * */ public function buildOptions( $args = array() ) { extract( $args ); $theme_options = get_option( 'blogy_options' ); if ( empty($theme_options) ) $theme_options[$id] = $std; elseif ( ! isset( $theme_options[$id] ) ) $theme_options[$id] = 0; $field_class = ''; if ( $class != '' ) $field_class = ' ' . $class; switch ( $type ) { case 'heading': echo '

    ' . $desc . '

    '; break; case 'checkbox': echo ' '; break; case 'select': echo ''; if ( $desc != '' ) echo '' . $desc . ''; break; case 'multiselect': echo ''; if ( $desc != '' ) echo '' . $desc . ''; break; case 'multicheck': $i = 0; foreach ( $choices as $value => $label ) { /** * If the user has saved options use them * or use what is set in the std value */ if ( is_array( $theme_options[$id] ) && in_array( $value, $theme_options[$id] ) ){ $selected = 'checked="checked"'; } else { $selected = null; } echo ' '; echo '
    '; $i++; } if ( $desc != '' ) echo '' . $desc . ''; break; case 'radio': $i = 0; foreach ( $choices as $value => $label ) { echo ' '; //if ( $i < count( $theme_options ) - 1 ) echo '
    '; $i++; } if ( $desc != '' ) echo '' . $desc . ''; break; case 'radio_img': echo '
    '; $i = 0; $i++; foreach( $choices as $k => $v ) { $selected = (checked($theme_options[$id], $k, false) != '')?' blogy-radio-img-selected':''; echo ''; $i++; }//foreach echo ( isset( $theme_options[$desc] ) && !empty( $theme_options[$desc] ) ) ? '
    '.$theme_options[$desc].'':''; echo '
    '; break; case 'textarea': echo ''; if ( $desc != '' ) echo '' . $desc . ''; break; case 'password': echo ''; if ( $desc != '' ) echo '' . $desc . ''; break; case 'upload': echo '
    '; if( $theme_options[$id] != '' ) { echo ''; } echo "
    "; if ( $theme_options[$id] == '' ) { $remove = ' style="display:none;"'; $upload = ''; } else { $remove = ''; $upload = ' style="display:none;"'; } echo ''.__('Browse', 'blogy').''; echo ''.__('Remove Upload', 'blogy').''; if ( $desc != '' ) echo '' . $desc . ''; break; case 'html': echo $html; break; case 'color': echo '
    '; echo '
    '; echo '
    '; break; case 'slide': if ( $desc != '' ) echo '' . $desc . ''; echo '
    '; echo ''; break; case 'text': default: echo ''; if ( $desc != '' ) echo '' . $desc . ''; break; } } /** * Settings and defaults * * */ public function getSettings() { // extend this class to create theme options. See config.php } /** * Initialize settings to their default values * * */ public function initializeSettings( $settings=null ) { if ( is_null( $settings ) ) $settings = $this->settings; $default_settings = array(); foreach ( $settings as $id => $setting ) { if ( $setting['type'] != 'heading' ) $default_settings[$id] = $setting['std']; } update_option( 'blogy_options', $default_settings ); } /** * Register settings * * */ public function registerSettings() { register_setting( 'blogy_options', 'blogy_options', array ( &$this, 'formHandler' ) ); foreach ( $this->sections as $slug => $title ) { add_settings_section( $slug, $title, array( &$this, 'displaySection' ), 'blogy-options' ); } $this->getSettings(); foreach ( $this->settings as $id => $setting ) { $setting['id'] = $id; $this->createSetting( $setting ); } } /** * jQuery Tabs * * */ public function addScripts() { if ( $this->tabbed == true ) wp_print_scripts( 'jquery-ui-tabs' ); wp_enqueue_script( 'media-upload' ); wp_enqueue_script( 'thickbox' ); wp_enqueue_script( 'farbtastic' ); wp_register_script( 'blogy-options-scripts', get_template_directory_uri() . '/lib/theme-options/js/options.js', array( 'jquery', 'media-upload','thickbox', 'farbtastic' ) ); wp_enqueue_script( 'blogy-options-scripts' ); wp_register_script( 'appendo', get_template_directory_uri() . '/lib/theme-options/js/jquery.appendo.js', array( 'jquery' ) ); wp_enqueue_script( 'appendo' ); wp_enqueue_script( 'jquery-ui-sortable' ); } /** * Styling for the theme options page * * */ public function addStyles() { wp_register_style( 'blogy-options-styles', get_template_directory_uri() . '/lib/theme-options/css/options.css' ); if ( $this->tabbed == true ) wp_enqueue_style( 'blogy-options-styles' ); wp_enqueue_style('thickbox'); wp_enqueue_style( 'farbtastic' ); } /** * Form Handler * * */ public function formHandler( $input ) { /* if ( ! empty( $_POST['reset'] ) ) { //$input = $this->resetSettings( $input ); $input = ""; return $input; } */ if ( ! empty( $_POST['reset'] ) ) { $defaults = array(); foreach ( $this->settings as $id => $setting ) { if ( $setting['type'] != 'heading' ) { $defaults[$id] = $setting['std']; } } return $defaults; } if ( ! isset( $input['reset_theme'] ) ) { $theme_options = get_option( 'blogy_options' ); foreach ( $this->checkboxes as $id ) { if ( isset( $theme_options[$id] ) && ! isset( $input[$id] ) ) unset( $theme_options[$id] ); } return $input; } // Create our array for storing the validated options $output = array(); // Loop through each of the incoming options foreach( $input as $key => $value ) { // Check to see if the current option has a value. If so, process it. if( isset( $input[$key] ) ) { // Strip all HTML and PHP tags and properly handle quoted strings $output[$key] = wp_filter_nohtml_kses( $input[ $key ] ); } // end if } // end foreach // Return the array processing any additional functions filtered by this action return apply_filters( 'formHandler', $output, $input ); } /** * Do reset setting stuff here */ public function resetSettings( $inputs=array() ){ $defaults = array(); foreach( $inputs as $key => $value ){ $defaults[ $key ] = ''; } return $defaults; } /** * Format categories for $choices array. * * @uses get_terms(); * @return $terms['value']['label'] * @todo check if taxonomy exists */ public function getCategories( $taxonomy = null, $firstblank = false ) { if ( is_null( $taxonomy ) ) $taxonomy = 'category'; $args = array( 'hide_empty' => 0 ); $terms_obj = get_terms( $taxonomy, $args ); $items = array(); if( $firstblank ) { $terms[''] = '-- Choose One --'; } foreach ( $terms_obj as $tt ) { $terms[$tt->term_id] = $tt->name; } return $terms; } /** * Format Pages for $choices array. * * @uses get_pages * @param $value, the value to be used in the option value="" * default is ID, any index can be used as a value. * see codex: http://codex.wordpress.org/Function_Reference/get_pages */ public function getPages( $value = null, $firstblank = false ) { if ( is_null( $value ) ) $value = 'ID'; $args = array( 'post_type' => 'page' ); $obj = get_pages( $args ); $items = array(); if( $firstblank ) { $terms[''] = '-- Choose One --'; } foreach ( $obj as $item ) { $items[$item->$value] = $item->post_title; } return $items; } /** * Parse Readme.txt * * @param string $url URL of textfile readme.txt * @return string readme code */ public function parseFile( $url=null ) { // no/wrong url if ( empty($url) or basename($url) != 'readme.txt') return false; $response = wp_remote_get( $url ); if( is_wp_error( $response ) ) { echo 'Unable to load the instructions.'; } else { $readme = $response['body']; } // make links clickable $readme = make_clickable(nl2br(esc_html($readme))); // code, strong, em $readme = preg_replace('/`(.*?)`/', '\\1', $readme); $readme = preg_replace('/[\040]\*\*(.*?)\*\*/', ' \\1', $readme); $readme = preg_replace('/[\040]\*(.*?)\*/', ' \\1', $readme); // headings $readme = preg_replace('/=== (.*?) ===/', '

    \\1

    ', $readme); $readme = preg_replace('/== (.*?) ==/', '

    \\1

    ', $readme); $readme = preg_replace('/= (.*?) =/', '

    \\1

    ', $readme); // links $readme = preg_replace('#(^|[\[]{1}[\s]*)([^\n<>^\)]+)([\]]{1}[\(]{1}[\s]*)(http://|ftp://|mailto:|https://)([^\s<>]+)([\s]*[\)]|$)#', '$2', $readme); $readme = preg_replace('#(^|[^\"=]{1})(http://|ftp://|mailto:|https://)([^\s<>]+)([\s\n<>]|$)#', '$1$2$3$4', $readme); return $readme; } } // end class function blogy_option( $option ) { $theme_options = get_option( 'blogy_options' ); if ( isset( $theme_options[$option] ) ) return $theme_options[$option]; else return false; } ?>