available_blocks; if ( ! $blocks[$block_ref] ) { $blocks[$block_ref] = $block_name; update_option( 'hem_available_blocks', $blocks ); } } /* This function returns an array of available blocks in the format of $arr[block_ref] = block_name */ function get_available_blocks() { $this->available_blocks = get_option( 'hem_available_blocks' ); return $this->available_blocks; } /* Returns an array of block_refs in specified block */ function get_block_contents( $block_place ) { if ( ! $this->raw_blocks ) { $this->raw_blocks = get_option( 'hem_blocks' ); } return $this->raw_blocks[$block_place]; } function add_block_to_place( $block_place, $block_ref ) { $block_contents = $this->get_block_contents( $block_place ); if (in_array( $block_ref, $block_contents ) ) return true; $block_contents[] = $block_ref; $this->raw_blocks[$block_place] = $block_contents; update_option( 'hem_blocks', $this->raw_blocks ); return true; } function remove_block_in_place( $block_place, $block_ref ) { $block_contents = $this->get_block_contents( $block_place ); if ( ! in_array( $block_ref, $block_contents ) ) return true; $key = array_search( $block_ref, $block_contents ); unset( $block_contents[$key] ); $this->raw_blocks[$block_place] = $block_contents; update_option( 'hem_blocks', $this->raw_blocks ); return true; } /* Templating functions */ function get_block_output( $block_place ) { $blocks = $this->get_block_contents( $block_place ); if ( ! empty( $blocks ) ) { foreach ( $blocks as $key => $block ) { get_template_part( 'blocks/' . $block ); } } } function get_style() { $this->style = get_option( 'hem_style' ); } function set_style( $new_style ) { update_option( 'hem_style', $new_style ); $this->style = $new_style; } } // Set up option object $hemingway = new Hemingway(); $hemingway->get_available_blocks(); $hemingway->get_style(); $hemingway->version = "0.13"; // Set up form and default options add_action( 'admin_menu', 'hemingway_add_theme_options_page' ); add_action( 'admin_init', 'hemingway_init_theme_options' ); function hemingway_add_theme_options_page() { $page = add_theme_page( __( 'Theme Options', 'hemingway' ), __( 'Theme Options', 'hemingway' ), 'edit_theme_options', 'theme_options', 'hemingway_theme_options' ); add_action( "admin_print_scripts-$page", 'hemingway_admin_js' ); add_action( "load-$page", 'hemingway_save_theme_options' ); } function hemingway_init_theme_options() { global $hemingway; $default_blocks = array( 'recent_entries' => __( 'Recent Entries', 'hemingway' ), 'about_page' => __( 'About Page', 'hemingway' ), 'category_listing' => __( 'Category Listing', 'hemingway' ), 'blogroll' => __( 'Blogroll', 'hemingway' ), 'pages' => __( 'Pages', 'hemingway' ), 'monthly_archives' => __( 'Monthly Archives', 'hemingway' ), ); $default_block_locations = array( 'block_1' => array( 'about_page' ), 'block_2' => array( 'recent_entries' ), 'block_3' => array( 'category_listing' ), ); // Check for previous options if ( !get_option( 'hem_version' ) || get_option( 'hem_version' ) < $hemingway->version ) { // Hemingway wasn't installed before, so we'll need to add options if ( ! get_option( 'hem_version' ) ) add_option( 'hem_version', $hemingway->version, __( 'Hemingway Version installed', 'hemingway' ) ); else update_option( 'hem_version', $hemingway->version); if ( ! get_option( 'hem_available_blocks' ) ) add_option( 'hem_available_blocks', $default_blocks, __( 'A list of available blocks for Hemingway', 'hemingway' ) ); if ( ! get_option( 'hem_blocks' ) ) add_option( 'hem_blocks', $default_block_locations, __( 'An array of blocks and their contents', 'hemingway' ) ); if ( ! get_option( 'hem_style' ) ) add_option( 'hem_style', 'none', __( 'Location of custom style sheet', 'hemingway' ) ); } } function hemingway_admin_js() { wp_enqueue_script( 'prototype' ); wp_enqueue_script( 'scriptaculous-dragdrop' ); wp_enqueue_script( 'scriptaculous-effects' ); } function hemingway_save_theme_options() { global $hemingway, $message; if ( ! current_user_can( 'edit_theme_options' ) ) return; // Process POST requests if ( isset( $_POST['custom_styles'] ) ) { check_admin_referer( 'custom_styles' ); $hemingway->set_style( $_POST['custom_styles'] ); // save new option $message = __( 'Color options updated.', 'hemingway' ); } if ( isset ( $_POST['block_ref'] ) ) { check_admin_referer( 'block_ref' ); $hemingway->add_available_block( $_POST['display_name'], $_POST['block_ref'] ); $hemingway->get_available_blocks(); $message = __( 'Block added.', 'hemingway' ); } // END Process POST requests // Process Ajax requests for adding/removing blocks if ( isset( $_GET['hem_action'] ) ) { $action = $_GET['hem_action']; if ( $action == 'add_block' ) { check_ajax_referer( 'add_block' ); $block_ref = $_GET['block_ref']; $block_place = $_GET['block_place']; $block_name = $hemingway->available_blocks[$block_ref]; $hemingway->add_block_to_place( $block_place, $block_ref ); $output = '
%s a display name (such as "About Page")', 'hemingway' ), $block_ref ); ?>