Source for file geo-mashup-options.php
Documentation is available at geo-mashup-options.php
* Management of Geo Mashup saved options.
* A singleton to manage Geo Mashup saved options.
* Valid options with default values.
'category_link_separator' => '::',
'category_link_text' => 'map',
'category_zoom' => 'auto',
'add_category_links' => 'false',
'copy_geodata' => 'false',
'theme_stylesheet_with_maps' => 'false',
'located_post_types' => array( 'post', 'page' ),
'located_object_name' => array(
'enable_reverse_geocoding' => 'true',
'adsense_code' => 'partner-pub-5088093001880917',
'geonames_username' => 'geomashup',
'import_custom_field' => '' ),
'map_type' => 'G_NORMAL_MAP',
'background_color' => 'c0c0c0',
'category_color' => array ( ),
'category_line_zoom' => array ( ),
'map_control' => 'GSmallZoomControl3D',
'add_map_type_control' => array(),
'add_overview_control' => 'false',
'add_google_bar' => 'false',
'enable_scroll_wheel_zoom' => 'false',
'show_future' => 'false',
'marker_select_info_window' => 'true',
'marker_select_highlight' => 'false',
'marker_select_center' => 'false',
'marker_select_attachments' => 'false',
'auto_info_open' => 'true',
'click_to_load' => 'false',
'click_to_load_text' => '',
'cluster_max_zoom' => '',
'cluster_lib' => 'clustermarker' ),
'map_control' => 'GSmallZoomControl3D',
'map_type' => 'G_NORMAL_MAP',
'background_color' => 'c0c0c0',
'add_overview_control' => 'false',
'add_map_type_control' => array(),
'add_google_bar' => 'false',
'enable_scroll_wheel_zoom' => 'false',
'click_to_load' => 'false',
'click_to_load_text' => '' ),
'map_control' => 'GSmallZoomControl3D',
'map_type' => 'G_NORMAL_MAP',
'background_color' => 'c0c0c0',
'add_overview_control' => 'false',
'add_map_type_control' => array(),
'add_google_bar' => 'false',
'enable_scroll_wheel_zoom' => 'false',
'marker_select_info_window' => 'true',
'marker_select_highlight' => 'false',
'marker_select_center' => 'false',
'marker_select_attachments' => 'false',
'click_to_load' => 'false',
'click_to_load_text' => '' ) );
* Map of old option names to new ones.
private $conversions = array (
'google_key' => array ( 'overall', 'google_key' ),
'mashup_page' => array ( 'overall', 'mashup_page' ),
'category_link_separator' => array ( 'overall', 'category_link_separator' ),
'category_link_text' => array ( 'overall', 'category_link_text' ),
'category_zoom' => array ( 'overall', 'category_zoom' ),
'add_category_links' => array ( 'overall', 'add_category_links' ),
'theme_stylesheet_with_maps' => array ( 'overall', 'theme_stylesheet_with_maps' ),
'map_width' => array ( 'global_map', 'width' ),
'map_height' => array ( 'global_map', 'height' ),
'map_type' => array ( 'global_map', 'map_type' ),
'zoom_level' => array ( 'global_map', 'zoom' ),
'category_color' => array ( 'global_map', 'category_color' ),
'category_line_zoom' => array ( 'global_map', 'category_line_zoom' ),
'map_control' => array ( 'global_map', 'map_control' ),
'add_map_type_control' => array ( 'global_map', 'add_map_type_control' ),
'add_overview_control' => array ( 'global_map', 'add_overview_control' ),
'show_post' => array ( 'global_map', 'show_post' ),
'show_future' => array ( 'global_map', 'show_future' ),
'max_posts' => array ( 'global_map', 'max_posts' ),
'auto_info_open' => array ( 'global_map', 'auto_info_open' ),
'auto_open_info_window' => array ( 'global_map', 'auto_info_open' ),
'click_to_load' => array ( 'global_map', 'click_to_load' ),
'click_to_load_text' => array ( 'global_map', 'click_to_load_text' ),
'in_post_map_control' => array ( 'single_map', 'map_control' ),
'in_post_map_width' => array ( 'single_map', 'width' ),
'in_post_map_height' => array ( 'single_map', 'height' ),
'in_post_map_type' => array ( 'single_map', 'map_type' ),
'in_post_zoom_level' => array ( 'single_map', 'zoom' ),
'in_post_add_overview_control' => array ( 'single_map', 'add_overview_control' ),
'in_post_add_map_type_control' => array ( 'single_map', 'add_map_type_control' ),
'in_post_click_to_load' => array ( 'single_map', 'click_to_load' ),
'in_post_click_to_load_text' => array ( 'single_map', 'click_to_load_text' ) );
* Options keys whose values aren't predictable.
private $freeform_option_keys = array ( 'category_color', 'category_line_zoom', 'add_map_type_control', 'located_post_types' );
public $valid_map_types = array ( 'G_NORMAL_MAP', 'G_SATELLITE_MAP', 'G_HYBRID_MAP', 'G_PHYSICAL_MAP', 'G_SATELLITE_3D_MAP' );
* Use the GeoMashupOptions::get() method for access.
* Old option values that can't be converted.
private $corrupt_options = '';
private $validation_errors = array();
* Should be used only in this file.
$shared_google_api_key = get_option ( 'google_api_key' );
if ( $shared_google_api_key ) {
$settings = get_option ( 'geo_mashup_options' );
$settings = $this->convert_old_settings ( $settings );
$this->options = $this->valid_options ( $settings, $this->default_options, $add_missing = true );
$failed_options = $settings;
if ( is_string ( $settings ) && !empty ( $settings ) ) {
$this->corrupt_options = $settings;
* Change old option names.
* @param array $settings Existing settings.
* @return array Converted settings.
private function convert_old_settings( $settings ) {
foreach ( $this->conversions as $old_key => $new_keys ) {
if ( isset ( $settings[$old_key] ) ) {
$settings[$new_keys[0]][$new_keys[1]] = $settings[$old_key];
unset ( $settings[$old_key] );
if ( isset ( $settings['overall']['located_object_name']['post']) and 'true' == $settings['overall']['located_object_name']['post'] ) {
$settings['overall']['located_object_name']['post'] = 'deprecated';
if ( empty( $settings['overall']['located_post_types']['post'] ) )
$settings['overall']['located_post_types'] = array( 'post', 'page' );
* Write current values to the database.
* @return bool Success or failure.
if ($this->options == $this->valid_options ( $this->options ) ) {
$saved = update_option('geo_mashup_options', $this->options);
// Share our Google API key
$google_api_key = $this->options['overall']['google_key'];
if ( !empty ( $google_api_key ) && !get_option ( 'google_api_key' ) ) {
update_option( 'google_api_key', $google_api_key );
* Get a saved option value.
* $single_map_options_array = $geo_mashup_options->get( 'single_map' );
* $google_key = $geo_mashup_options->get( 'overall', 'google_key' );
* $add_global_satellite_map = $geo_mashup_options->get( 'global_map', 'add_map_type_control', 'G_SATELLITE_MAP' );
* @param string $key1 Highest level key.
* @param string $key2 Second level key.
* @param string $key3 Third level key.
* @return string|arrayThe option value or values.
public function get( $key1, $key2 = null, $key3 = null ) {
// Getting first dimension options
foreach ( $key1 as $key ) $subset[$key] = isset ( $this->options[$key] ) ? $this->options[$key] : null;
return isset ( $this->options[$key1] ) ? $this->options[$key1] : null;
// Getting second dimension options
foreach ( $key2 as $key ) $subset[$key] = isset ( $this->options[$key1][$key] ) ? $this->options[$key1][$key] : null;
return isset ( $this->options[$key1][$key2] ) ? $this->options[$key1][$key2] : null;
// Getting third dimension options
foreach ( $key3 as $key ) $subset[$key] = isset ( $this->options[$key1][$key2][$key] ) ? $this->options[$key1][$key2][$key] : null;
return isset ( $this->options[$key1][$key2][$key3] ) ? $this->options[$key1][$key2][$key3] : null;
* Import valid options from an array.
* @param array $option_array Associative array of option names and values.
$this->validation_errors = array ( );
$this->options = $this->valid_options ( $option_array );
* Remove invalid option keys from an array, and replace invalid values with defaults.
* @param array $option_array An array of options to validate.
* @param array $defaults Optional array of valid default values.
* @param boolean $add_missing Set if input is not from a form, so missing values are not unchecked checkboxes and should get default values.
* @return array Valid options.
private function valid_options( $option_array, $defaults = null, $add_missing = false ) {
$valid_options = array ( );
$defaults = ( empty ( $this->options ) ) ? $this->default_options : $this->options;
if ( !is_array( $option_array ) ) return $defaults;
foreach ( $defaults as $key => $default_value ) {
if ( isset ( $option_array[$key] ) && $this->is_valid ( $key, $option_array[$key] ) ) {
if ( is_array ( $option_array[$key] ) && !in_array ( $key, $this->freeform_option_keys ) ) {
// Validate options in sub-arrays, except freeform array options, whose keys aren't known
$valid_options[$key] = $this->valid_options ( $option_array[$key], $default_value );
// Use the valid non-array value
$valid_options[$key] = $option_array[$key];
// Value in question is invalid
if ( ! $add_missing and empty ( $option_array[$key] ) and in_array ($default_value, array ( 'true', 'false' ) ) ) {
// Convert empty booleans to false to handle unchecked checkboxes
$valid_options[$key] = 'false';
$valid_options[$key] = $default_value;
* Check an option key and value for validity.
* @param string $key Option key.
* @param mixed $value Option value, modified in some cases.
* @return bool True if valid.
public function is_valid( $key, &$value ) {
array_push ( $this->validation_errors, '"'. $value . '" ' . __('is invalid for', 'GeoMashup') . ' ' . $key .
__(', which must be a valid map type (see documentation)', 'GeoMashup') );
case 'add_map_type_control':
array_push ( $this->validation_errors, '"'. $value . '" ' . __('is invalid for', 'GeoMashup') . ' ' . $key .
__(', which must be an array of valid map types (see documentation)', 'GeoMashup') );
foreach( $value as $map_type ) {
array_push ( $this->validation_errors, '"'. $map_type . '" ' . __('is invalid for', 'GeoMashup') . ' ' . $key .
__(', which must be a valid map type (see documentation)', 'GeoMashup') );
$valid_map_controls = array ( 'GSmallZoomControl', 'GSmallMapControl', 'GLargeMapControl', 'GLargeMapControl3D', 'GSmallZoomControl3D' );
if ( !in_array ( $value, $valid_map_controls ) ) {
array_push ( $this->validation_errors, '"'. $value . '" ' . __('is invalid for', 'GeoMashup') . ' ' . $key .
__(', which must be a valid map control (see documentation)', 'GeoMashup') );
$valid_apis = array( 'google', 'openlayers', 'googlev3' );
if ( !in_array ( $value, $valid_apis ) ) {
array_push ( $this->validation_errors, '"'. $value . '" ' . __('is invalid for', 'GeoMashup') . ' ' . $key .
__(', which must be a valid map provider.', 'GeoMashup') );
$valid_libs = array( 'clustermarker', 'markerclusterer' );
if ( !in_array( $value, $valid_libs ) ) {
array_push( $this->validation_errors, '"'. $value . '" ' . __('is invalid for', 'GeoMashup') . ' ' . $key .
__(', which must be a valid clustering library.', 'GeoMashup') );
$value = 'partner-pub-5088093001880917';
case 'category_link_separator':
case 'category_link_text':
case 'click_to_load_text':
case 'import_custom_field':
if ( empty ( $value ) ) return true;
array_push ( $this->validation_errors, '"'. $value . '" ' . __('is invalid for', 'GeoMashup') . ' ' . $key .
__(', which must be a string', 'GeoMashup') );
array_push ( $this->validation_errors, '"'. esc_html( $value ) . '" ' . __('is invalid for', 'GeoMashup') . ' ' . $key .
__(', which must not contain XML tags.', 'GeoMashup') );
if ( empty ( $value ) ) return true;
array_push ( $this->validation_errors, '"'. $value . '" ' . __('is invalid for', 'GeoMashup') . ' ' . $key .
__(', which must be a number', 'GeoMashup') );
// show_future: boolean or 'only'
case 'add_category_links':
case 'add_overview_control':
case 'enable_scroll_wheel_zoom':
case 'marker_select_info_window':
case 'marker_select_highlight':
case 'marker_select_center':
case 'marker_select_attachments':
case 'theme_stylesheet_with_maps':
case 'enable_reverse_geocoding':
if ( empty ( $value ) ) {
// fail quietly - it will be converted to false
} else if ( $value != 'true' && $value != 'false' ) {
array_push ( $this->validation_errors, '"'. $value . '" ' . __('is invalid for', 'GeoMashup') . ' ' . $key .
__(', which must be "true" or "false"', 'GeoMashup') );
case 'category_line_zoom':
case 'located_post_types':
case 'located_object_name':
array_push ( $this->validation_errors, '"'. $value . '" ' . __('is invalid for', 'GeoMashup') . ' ' . $key .
__(', which must be an array', 'GeoMashup') );
if ( empty ( $value ) ) return true;
if ( $value == 'auto' ) return true;
if ( !is_numeric ( $value ) || $value < 0 || $value > GEO_MASHUP_MAX_ZOOM ) {
array_push ( $this->validation_errors, '"'. $value . '" ' . __('is invalid for', 'GeoMashup') . ' ' . $key .
__(', which must be a number from 0 to ', 'GeoMashup') . GEO_MASHUP_MAX_ZOOM );
if ( 'deprecated' != $value ) {
array_push( $this->validation_errors, '"'. $value . '" ' . __( 'is invalid for', 'GeoMashup' ) . ' ' . $key .
__( ', which is a deprecated option', 'GeoMashup' ) );
* @global GeoMashupOptions The global instance of the GeoMashupOptions class.
global $geo_mashup_options;
|