get_default_option_providers(), '', false );
add_option( 'acf_osm_proxy', [], '', false );
add_action( 'admin_menu', [ $this, 'admin_menu' ] );
add_action( "load-settings_page_acf_osm", [ $this, 'enqueue_assets' ] );
add_filter( 'plugin_action_links_'.$core->get_wp_plugin(), [ $this, 'plugin_actions_links' ], 20, 4 );
parent::__construct();
}
/**
* @filter plugin_action_links_{$plugin_file}
*/
public function plugin_actions_links( $actions, $plugin_file, $plugin_data, $context ) {
if ( isset( $plugin_data['Name'] ) && current_user_can( 'manage_options' ) ) {
$actions['settings'] = sprintf(
'%s',
esc_url(
add_query_arg(
[ 'page' => $this->optionset ],
admin_url( 'options-general.php' )
)
),
/* translators: %s: Plugin name. */
esc_attr( sprintf( _x( '%s Settings', 'plugin', 'acf-openstreetmap-field' ), $plugin_data['Name'] ) ),
__( 'Settings', 'acf-openstreetmap-field' )
);
}
return $actions;
}
/**
* Add Settings page
*
* @action admin_menu
*/
public function admin_menu() {
$page_hook = add_options_page( __('OpenStreetMap Settings' , 'acf-openstreetmap-field' ),__('OpenStreetMap' , 'acf-openstreetmap-field'),'manage_options', $this->optionset, [ $this, 'settings_page' ] );
}
/**
* Render Settings page
*/
public function settings_page() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.' ) );
}
$core = Core\Core::instance();
$providers = Core\LeafletProviders::instance();
?>
register_assets();
// registering in Core\Core does not work ... why?
wp_enqueue_script( 'acf-osm-settings' );
wp_enqueue_style( 'acf-osm-settings' );
}
/**
* Setup options.
*
* @action admin_init
*/
public function register_settings() {
$core = Core\Core::instance();
$settings_section = 'acf_osm_settings';
add_settings_section(
$settings_section,
__( 'Map Layer Settings', 'acf-openstreetmap-field' ),
[ $this, 'tokens_description' ],
$this->optionset
);
register_setting( $this->optionset, 'acf_osm_provider_tokens', [ $this , 'sanitize_provider_tokens' ] );
register_setting( $this->optionset, 'acf_osm_providers', [ $this , 'sanitize_providers' ] );
register_setting( $this->optionset, 'acf_osm_proxy', [ $this , 'sanitize_proxy' ] );
}
/**
* Print layer tags
*/
private function print_tags( $options ) {
$tag = '%s';
if ( $this->is_insecure( $options ) ) {
$is_https = strpos( get_option('home'), 'https:' ) === 0;
printf(
$tag, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
esc_html__( 'The map tiles are loaded through an insecure http connection.', 'acf-openstreetmap-field' ),
'acf-osm-tag' . ( $is_https ? ' warn' : '' ),
esc_html__( 'Insecure', 'acf-openstreetmap-field' )
);
}
if ( $this->is_overlay( $options ) ) {
printf(
$tag, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
esc_html__( 'This is an overlay to be displayed over a base map.', 'acf-openstreetmap-field' ),
'acf-osm-tag',
esc_html__( 'Overlay', 'acf-openstreetmap-field' )
);
}
if ( $this->has_bounds( $options ) ) {
printf(
$tag, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
esc_html__( 'Only available for a specific region.', 'acf-openstreetmap-field' ),
'acf-osm-tag',
esc_html__( 'Bounds', 'acf-openstreetmap-field' )
);
}
if ( isset( $options['options']['minZoom'] ) && isset( $options['options']['maxZoom'] ) ) {
printf(
$tag, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
esc_html__( 'Zoom is restricted.', 'acf-openstreetmap-field' ),
'acf-osm-tag',
esc_html( sprintf(
/* translators: 1: min zoom value, 2: max zoom value */
__( 'Zoom: %1$d–%2$d', 'acf-openstreetmap-field' ),
$options['options']['minZoom'], $options['options']['maxZoom']
))
);
} else if ( isset( $options['options']['minZoom'] ) ) {
printf(
$tag, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
esc_html__( 'Zoom levels are restricted.', 'acf-openstreetmap-field' ),
'acf-osm-tag',
esc_html( sprintf(
/* translators: min zoom value */
__( 'Min Zoom: %d', 'acf-openstreetmap-field' ),
$options['options']['minZoom']
))
);
} else if ( isset( $options['options']['maxZoom'] ) ) {
printf(
$tag, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
esc_html__( 'Zoom levels are restricted.', 'acf-openstreetmap-field' ),
'acf-osm-tag',
esc_html(sprintf(
/* translators: max zoom value */
__( 'Max Zoom: %d', 'acf-openstreetmap-field' ),
$options['options']['maxZoom'] // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
))
);
}
}
/**
* Whether a map tile provider is insecure.
*
* @param array $options Map provider options
* @return boolean
*/
private function is_insecure( $options ) {
return is_array($options) && isset( $options['url'] ) && strpos( $options['url'], 'http:' ) === 0;
}
/**
* Whether a map tile provider has bounds
*
* @param array $options Map provider options
* @return boolean
*/
private function has_bounds( $options ) {
return is_array($options) && isset( $options['options']['bounds'] );
}
/**
* Whether a map tile is overlay.
*
* @param array $options Map provider options
* @return boolean
*/
private function is_overlay( $options ) {
return is_array($options) && isset( $options['isOverlay'] ) && $options['isOverlay'];;
}
/**
* @param string $key Provider key
*/
private function print_test_link( $key ) {
?>
needs_access_token( $provider_key, $provider_data );
$has_access_key = $providers->has_access_token( $provider_key, $provider_data );
?>
print_tags( $provider_data );
$this->print_test_link( $provider_key );
}
?>
$variant ) {
$is_disabled = isset( $provider_option[ $provider_key ]['variants'][ $variant_key ] )
&& in_array( $provider_option[$provider_key]['variants'][$variant_key], [ '0', false ], true );
?>
print_tags( $variant );
?>
print_access_token_inputs( $provider_key, $provider_data );
?>
']
foreach ( $provider_data['options'] as $option => $value ) {
if ( is_string($value) && ( 1 === preg_match( '/^<([^>]*)>$/imsU', $value, $matches ) ) ) {
$current_value = '';
$has_value = isset( $token_option[ $provider_key ][ 'options' ][ $option ]) && ! empty( $token_option[ $provider_key ][ 'options' ][ $option ] );
?>
]*)>$/imsU', $value, $matches ) ) {
$value = '';
}
printf('',
esc_attr( $field_id ),
esc_attr( $field_name ),
esc_attr( $value )
);
}
/**
* Sanitize value of setting_1
*
* @return string sanitized value
*/
public function sanitize_provider_tokens( $new_values ) {
$core = Core\Core::instance();
$providers = Core\LeafletProviders::instance();
$token_options = $providers->get_token_options();
$prev_values = get_option('acf_osm_provider_tokens');
// merge new values
$values = array_replace_recursive( $prev_values, $new_values );
// remove empty values
$values = Helper\ArrayHelper::filter_recursive( $values );
return $values;
}
/**
* @param string $key
* @return string
*/
private function sanitize_key_case( $key ) {
return preg_replace( '/[^A-Za-z0-9_\-]/', '', $key );
}
/**
*
*/
public function sanitize_providers( $values ) {
try {
$values = array_map( [ $this, 'boolval_recursive' ], (array) $values );
} catch ( \Exception $err ) {
$values = [];
}
return $values;
}
/**
*
*/
public function sanitize_proxy( $values ) {
try {
$values = array_filter( (array) $values );
$values = array_map( 'boolval', $values );
} catch ( \Exception $err ) {
$values = [];
}
return $values;
}
private function boolval_recursive( $val ) {
if ( $val === '0' ) {
return false;
} else if ( is_array( $val ) ) {
return array_map( [ $this, 'boolval_recursive' ], $val );
}
throw( new \Exception('invalid value') );
}
/**
* @return array Disable tile providers with bounds
*/
private function get_default_option_providers() {
$providers = Core\LeafletProviders::instance();
$is_https = strpos( get_option('home'), 'https:' ) === 0;
$provider_settings = $providers->get_providers(['credentials']);
$default_option = [];
foreach ( $provider_settings as $provider_key => $provider_data ) {
if ( $this->has_bounds( $provider_data ) || ( $is_https && $this->is_insecure( $provider_data ) ) ) {
$default_option[ $provider_key ] = '0';
continue;
}
if ( isset( $provider_data['variants'] ) ) {
foreach ( $provider_data['variants'] as $variant_key => $variant_data ) {
if ( $this->has_bounds( $variant_data ) || ( $is_https && $this->is_insecure( $variant_data ) )) {
$default_option[ $provider_key ]['variants'][$variant_key] = '0';
}
}
}
}
return $default_option;
}
}