| 1 |
<?php |
| 2 |
|
| 3 |
if( ! class_exists( 'wp_mapit_admin_settings' ) ) { |
| 4 |
|
| 5 |
/** |
| 6 |
* Class to manage admin settings for WP MapIt. |
| 7 |
*/ |
| 8 |
class wp_mapit_admin_settings |
| 9 |
{ |
| 10 |
|
| 11 |
/** |
| 12 |
* @since 1.0 |
| 13 |
* @var mapTypes to manage the map types |
| 14 |
* @access private |
| 15 |
*/ |
| 16 |
private static $mapTypes = array( |
| 17 |
'normal' => 'Normal', |
| 18 |
'grayscale' => 'Grayscale', |
| 19 |
'topographic' => 'Topographic' |
| 20 |
); |
| 21 |
|
| 22 |
/** |
| 23 |
* @since 1.0 |
| 24 |
* @var mapPostions to manage the map positions |
| 25 |
* @access private |
| 26 |
*/ |
| 27 |
private static $mapPostions = array( |
| 28 |
'before' => 'Before Content', |
| 29 |
'after' => 'After Content', |
| 30 |
'none' => 'Custom' |
| 31 |
); |
| 32 |
|
| 33 |
/** |
| 34 |
* @since 1.0 |
| 35 |
* @var mapSizeType to manage the map size type |
| 36 |
* @access private |
| 37 |
*/ |
| 38 |
private static $mapSizeType = array( |
| 39 |
'px' => 'px', |
| 40 |
'per' => '%' |
| 41 |
); |
| 42 |
|
| 43 |
/** |
| 44 |
* @since 1.0 |
| 45 |
* @var mapMarker to manage the default map pin |
| 46 |
* @access private |
| 47 |
*/ |
| 48 |
private static $mapMarker = WP_MAPIT_URL . 'images/map-pin.png'; |
| 49 |
|
| 50 |
/** |
| 51 |
* Add hooks and filters for admin menu |
| 52 |
* |
| 53 |
* @since 1.0 |
| 54 |
* @static |
| 55 |
* @access public |
| 56 |
*/ |
| 57 |
public static function init() |
| 58 |
{ |
| 59 |
/* Add admin menu */ |
| 60 |
add_action( 'admin_menu', __CLASS__ . '::admin_menu' ); |
| 61 |
|
| 62 |
/* Add init hook */ |
| 63 |
add_action( 'admin_init', __CLASS__ . '::admin_init' ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Hook to manage WP MapIt admin menu |
| 68 |
* |
| 69 |
* @since 1.0 |
| 70 |
* @static |
| 71 |
* @access public |
| 72 |
*/ |
| 73 |
public static function admin_menu() { |
| 74 |
add_menu_page( __('WP MapIt', WP_MAPIT_TEXTDOMAIN), __('WP MapIt', WP_MAPIT_TEXTDOMAIN), 'manage_options', 'wp_mapit', __CLASS__ . '::wp_mapit_settings', 'dashicons-location-alt' ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Hook to display WP MapIt Settings page |
| 79 |
* |
| 80 |
* @since 1.0 |
| 81 |
* @static |
| 82 |
* @access public |
| 83 |
*/ |
| 84 |
public static function wp_mapit_settings() { |
| 85 |
?> |
| 86 |
<div class="wrap"> |
| 87 |
<div id="wp-mapit-settings-container"> |
| 88 |
<div class="wp-mapit-header"> |
| 89 |
<ul> |
| 90 |
<li><?php _e( 'Support:', WP_MAPIT_TEXTDOMAIN ); ?> <a href="mailto:wp-mapit@chandnipatel.in">wp-mapit@chandnipatel.in</a></li> |
| 91 |
<li><a href="https://www.paypal.me/chandnipatel11" target="_blank"><?php _e( 'Donate', WP_MAPIT_TEXTDOMAIN ); ?></a></li> |
| 92 |
</ul> |
| 93 |
</div> |
| 94 |
|
| 95 |
<?php |
| 96 |
|
| 97 |
if( isset( $_REQUEST['info'] ) && $_REQUEST['info'] == 's' ) { |
| 98 |
?> |
| 99 |
<div class="updated notice is-dismissible"> |
| 100 |
<p><strong><?php _e( 'Settings saved.', WP_MAPIT_TEXTDOMAIN ); ?></strong></p> |
| 101 |
<button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php _e( 'Dismiss this notice.', WP_MAPIT_TEXTDOMAIN ); ?></span></button> |
| 102 |
</div> |
| 103 |
<?php |
| 104 |
} |
| 105 |
|
| 106 |
?> |
| 107 |
<div class="wp-mapit-content"> |
| 108 |
<div class="wp-mapit-form"> |
| 109 |
<form name="frmWpMapitSettings" id="frmWpMapitSettings" method="post" action=""> |
| 110 |
<?php |
| 111 |
wp_nonce_field( 'wp_mapit_settings', 'wp_mapit_settings_nonce' ); |
| 112 |
?> |
| 113 |
|
| 114 |
<div class="wp-mapit-row"> |
| 115 |
<label><?php _e( 'Map Settings', WP_MAPIT_TEXTDOMAIN ); ?></label> |
| 116 |
<div class="wp-mapit-row"> |
| 117 |
<label for="wpmi_map_type"><?php _e( 'Map Type', WP_MAPIT_TEXTDOMAIN ); ?></label> |
| 118 |
<select name="wpmi_map_type" id="wpmi_map_type"> |
| 119 |
<?php |
| 120 |
$selectedMapType = self::get_map_type(); |
| 121 |
foreach (self::$mapTypes as $mapTypeId => $mapType) { |
| 122 |
?> |
| 123 |
<option value="<?php echo $mapTypeId; ?>" <?php echo ( $selectedMapType == $mapTypeId ? "selected='selected'" : '' ); ?>><?php _e( $mapType, WP_MAPIT_TEXTDOMAIN ); ?></option> |
| 124 |
<?php |
| 125 |
} |
| 126 |
?> |
| 127 |
</select> |
| 128 |
<p class="description"><?php _e( 'Type of the map to be used.', WP_MAPIT_TEXTDOMAIN ); ?></p> |
| 129 |
</div> |
| 130 |
<div class="wp-mapit-row"> |
| 131 |
<label for="wpmi_latitude"><?php _e( 'Latitude', WP_MAPIT_TEXTDOMAIN ); ?></label> |
| 132 |
<input type="number" name="wpmi_latitude" id="wpmi_latitude" step="any" value="<?php echo self::get_map_latitude(); ?>"> |
| 133 |
<p class="description"><?php _e( 'Default latitude for the map.', WP_MAPIT_TEXTDOMAIN ); ?></p> |
| 134 |
</div> |
| 135 |
<div class="wp-mapit-row"> |
| 136 |
<label for="wpmi_longitude"><?php _e( 'Longitude', WP_MAPIT_TEXTDOMAIN ); ?></label> |
| 137 |
<input type="number" name="wpmi_longitude" id="wpmi_longitude" step="any" value="<?php echo self::get_map_longitude(); ?>"> |
| 138 |
<p class="description"><?php _e( 'Default longitude for the map.', WP_MAPIT_TEXTDOMAIN ); ?></p> |
| 139 |
</div> |
| 140 |
<div class="wp-mapit-row no-margin"> |
| 141 |
<label for="wpmi_map_zoom"><?php _e( 'Zoom', WP_MAPIT_TEXTDOMAIN ); ?></label> |
| 142 |
<input type="number" name="wpmi_map_zoom" id="wpmi_map_zoom" step="0.01" min="1" max="20" value="<?php echo self::get_map_zoom(); ?>"> |
| 143 |
<p class="description"><?php _e( 'Default zoom for the map.', WP_MAPIT_TEXTDOMAIN ); ?></p> |
| 144 |
</div> |
| 145 |
</div> |
| 146 |
|
| 147 |
<div class="wp-mapit-row"> |
| 148 |
<label><?php _e( 'General Settings', WP_MAPIT_TEXTDOMAIN ); ?></label> |
| 149 |
<div class="wp-mapit-row"> |
| 150 |
<label for="wpmi_map_width"><?php _e( 'Map Width', WP_MAPIT_TEXTDOMAIN ); ?></label> |
| 151 |
<div class="wp-mapit-size"> |
| 152 |
<input type="number" name="wpmi_map_width" id="wpmi_map_width" value="<?php echo self::get_map_width(); ?>"> |
| 153 |
<select name="wpmi_map_width_type" id="wpmi_map_width_type"> |
| 154 |
<?php |
| 155 |
$selectedMapWidthType = self::get_map_width_type(); |
| 156 |
foreach (self::$mapSizeType as $mapSizeTypeId => $mapSizeTypeName) { |
| 157 |
?> |
| 158 |
<option value="<?php echo $mapSizeTypeId; ?>" <?php echo ( $selectedMapWidthType == $mapSizeTypeId ? "selected='selected'" : '' ); ?>><?php _e( $mapSizeTypeName, WP_MAPIT_TEXTDOMAIN ); ?></option> |
| 159 |
<?php |
| 160 |
} |
| 161 |
?> |
| 162 |
</select> |
| 163 |
</div> |
| 164 |
<p class="description"><?php _e( 'Map width', WP_MAPIT_TEXTDOMAIN ); ?></p> |
| 165 |
</div> |
| 166 |
<div class="wp-mapit-row"> |
| 167 |
<label for="wpmi_map_height"><?php _e( 'Map Height', WP_MAPIT_TEXTDOMAIN ); ?></label> |
| 168 |
<div class="wp-mapit-size"> |
| 169 |
<input type="number" name="wpmi_map_height" id="wpmi_map_height" value="<?php echo self::get_map_height(); ?>"> |
| 170 |
<select name="wpmi_map_height_type" id="wpmi_map_height_type"> |
| 171 |
<?php |
| 172 |
$selectedMapHeightType = self::get_map_height_type(); |
| 173 |
foreach (self::$mapSizeType as $mapSizeTypeId => $mapSizeTypeName) { |
| 174 |
?> |
| 175 |
<option value="<?php echo $mapSizeTypeId; ?>" <?php echo ( $selectedMapHeightType == $mapSizeTypeId ? "selected='selected'" : '' ); ?>><?php _e( $mapSizeTypeName, WP_MAPIT_TEXTDOMAIN ); ?></option> |
| 176 |
<?php |
| 177 |
} |
| 178 |
?> |
| 179 |
</select> |
| 180 |
</div> |
| 181 |
<p class="description"><?php _e( 'Map height', WP_MAPIT_TEXTDOMAIN ); ?></p> |
| 182 |
</div> |
| 183 |
<div class="wp-mapit-row"> |
| 184 |
<label for="wpmi_map_marker"><?php _e( 'Map Marker', WP_MAPIT_TEXTDOMAIN ); ?></label> |
| 185 |
<?php |
| 186 |
$wpmiMapPin = self::get_map_marker(); |
| 187 |
?> |
| 188 |
<div class="wp_mapit_image_upload"> |
| 189 |
<img src="<?php echo $wpmiMapPin; ?>" alt="Map Pin" width="24px"> |
| 190 |
<a href="#" id="upload_map_pin" class="button"><?php _e( 'Choose Map Pin', WP_MAPIT_TEXTDOMAIN ); ?></a> |
| 191 |
<a href="#" id="reset_map_pin" class="button" data-default-pin="<?php echo self::$mapMarker; ?>"><?php _e( 'Reset Map Pin', WP_MAPIT_TEXTDOMAIN ); ?></a> |
| 192 |
<input type="hidden" name="wpmi_map_marker" id="wpmi_map_marker" value="<?php echo $wpmiMapPin; ?>"> |
| 193 |
</div> |
| 194 |
<p class="description"><?php _e( 'Default marker for the map. Max size 100px X 100px. Image bigger then the size will be resized.', WP_MAPIT_TEXTDOMAIN ); ?></p> |
| 195 |
</div> |
| 196 |
<div class="wp-mapit-row"> |
| 197 |
<label><?php _e( 'Allow map for', WP_MAPIT_TEXTDOMAIN ); ?></label> |
| 198 |
<?php |
| 199 |
|
| 200 |
$allowedPostTypes = get_option( 'wpmi_allowed_post_types', array() ); |
| 201 |
|
| 202 |
$arrPostTypes = get_post_types( array( |
| 203 |
'public' => true, |
| 204 |
) ); |
| 205 |
|
| 206 |
if( is_array( $arrPostTypes ) && count( $arrPostTypes ) > 0 ) { |
| 207 |
?> |
| 208 |
<ul> |
| 209 |
<?php |
| 210 |
foreach ( $arrPostTypes as $postType ) { |
| 211 |
if( ! in_array( $postType, array( 'wp_mapit_map', 'attachment' )) ) { |
| 212 |
?> |
| 213 |
<li><input type="checkbox" name="wpmi_allowed_post_types[]" id="wpmi_allowed_post_type_<?php echo $postType; ?>" value="<?php echo $postType; ?>" <?php echo ( in_array( $postType , $allowedPostTypes ) ? "checked='checked'" : '' ); ?>><?php echo $postType; ?></li> |
| 214 |
<?php |
| 215 |
} |
| 216 |
} |
| 217 |
?> |
| 218 |
</ul> |
| 219 |
<?php |
| 220 |
} |
| 221 |
?> |
| 222 |
<p class="description"><?php _e( 'Select where the map can be displayed.', WP_MAPIT_TEXTDOMAIN ); ?></p> |
| 223 |
</div> |
| 224 |
<div class="wp-mapit-row no-margin"> |
| 225 |
<label for="wpmi_map_position"><?php _e( 'Show Map', WP_MAPIT_TEXTDOMAIN ); ?></label> |
| 226 |
<select name="wpmi_map_position" id="wpmi_map_position"> |
| 227 |
<?php |
| 228 |
$selectedMapPosition = self::get_map_position(); |
| 229 |
foreach (self::$mapPostions as $mapPositionId => $mapPosition) { |
| 230 |
?> |
| 231 |
<option value="<?php echo $mapPositionId; ?>" <?php echo ( $selectedMapPosition == $mapPositionId ? "selected='selected'" : '' ); ?>><?php _e( $mapPosition, WP_MAPIT_TEXTDOMAIN ); ?></option> |
| 232 |
<?php |
| 233 |
} |
| 234 |
?> |
| 235 |
</select> |
| 236 |
<p class="description"><?php _e( 'Position where the map needs to be displayed.<br>For custom, use one of the following: <br>1. Shortcode [wp_mapit].<br>2. Sidebar widget<br>3. Gutenberg block "WP MAPIT".', WP_MAPIT_TEXTDOMAIN ); ?></p> |
| 237 |
</div> |
| 238 |
</div> |
| 239 |
<div class="wp-mapit-row button-container"> |
| 240 |
<input type='submit' class='button-primary' name='save_settings' id='save_settings' value='<?php _e( 'Save Settings', WP_MAPIT_TEXTDOMAIN ); ?>'> |
| 241 |
</div> |
| 242 |
</form> |
| 243 |
</div> |
| 244 |
<div class="wp-mapit-map"> |
| 245 |
<div class="wp-mapit-row no-margin"> |
| 246 |
<label><?php _e( 'Search Location', WP_MAPIT_TEXTDOMAIN ); ?></label> |
| 247 |
<div class="wp-mapit-search"> |
| 248 |
<input type="text" name="search_map" id="search_map"> |
| 249 |
<input type='button' class='button' name='search_map_btn' id='search_map_btn' value='<?php _e( 'Search', WP_MAPIT_TEXTDOMAIN ); ?>'> |
| 250 |
</div> |
| 251 |
</div> |
| 252 |
<div class="admin-settings-map-container"> |
| 253 |
<div id="admin_setting_map"></div> |
| 254 |
<p class="description"><?php _e( 'Changes in the Map Settings will be reflected on the map and vice versa.', WP_MAPIT_TEXTDOMAIN ); ?></p> |
| 255 |
</div> |
| 256 |
</div> |
| 257 |
<div class="clearfix"></div> |
| 258 |
</div> |
| 259 |
</div> |
| 260 |
</div> |
| 261 |
<?php |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Hook to be called when 'admin_init' action is called by wordpress. |
| 266 |
* Handles saving of the settings |
| 267 |
* |
| 268 |
* @since 1.0 |
| 269 |
* @static |
| 270 |
* @access public |
| 271 |
*/ |
| 272 |
public static function admin_init() { |
| 273 |
if( isset( $_POST['wp_mapit_settings_nonce'] ) && wp_verify_nonce( $_POST['wp_mapit_settings_nonce'], 'wp_mapit_settings' ) ) { |
| 274 |
update_option( 'wpmi_map_type', ( isset( $_POST['wpmi_map_type'] ) ? $_POST['wpmi_map_type'] : '' ) ); |
| 275 |
update_option( 'wpmi_latitude', ( isset( $_POST['wpmi_latitude'] ) ? $_POST['wpmi_latitude'] : '' ) ); |
| 276 |
update_option( 'wpmi_longitude', ( isset( $_POST['wpmi_longitude'] ) ? $_POST['wpmi_longitude'] : '' ) ); |
| 277 |
update_option( 'wpmi_map_width', ( isset( $_POST['wpmi_map_width'] ) ? $_POST['wpmi_map_width'] : '' ) ); |
| 278 |
update_option( 'wpmi_map_width_type', ( isset( $_POST['wpmi_map_width_type'] ) ? $_POST['wpmi_map_width_type'] : '' ) ); |
| 279 |
update_option( 'wpmi_map_height', ( isset( $_POST['wpmi_map_height'] ) ? $_POST['wpmi_map_height'] : '' ) ); |
| 280 |
update_option( 'wpmi_map_height_type', ( isset( $_POST['wpmi_map_height_type'] ) ? $_POST['wpmi_map_height_type'] : '' ) ); |
| 281 |
update_option( 'wpmi_map_zoom', ( isset( $_POST['wpmi_map_zoom'] ) ? $_POST['wpmi_map_zoom'] : '' ) ); |
| 282 |
update_option( 'wpmi_map_marker', ( isset( $_POST['wpmi_map_marker'] ) ? $_POST['wpmi_map_marker'] : '' ) ); |
| 283 |
update_option( 'wpmi_allowed_post_types', ( ( isset( $_POST['wpmi_allowed_post_types'] ) && is_array( $_POST['wpmi_allowed_post_types'] ) ) ? $_POST['wpmi_allowed_post_types'] : array() ) ); |
| 284 |
update_option( 'wpmi_map_position', ( isset( $_POST['wpmi_map_position'] ) ? $_POST['wpmi_map_position'] : '' ) ); |
| 285 |
|
| 286 |
wp_redirect( admin_url( 'admin.php?page=wp_mapit&info=s' ) ); |
| 287 |
die; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Returns the allowed map types |
| 293 |
* |
| 294 |
* @since 1.0 |
| 295 |
* @static |
| 296 |
* @access public |
| 297 |
* @return array Returns array of the allowed map types |
| 298 |
*/ |
| 299 |
public static function get_map_types() { |
| 300 |
return self::$mapTypes; |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Returns the allowed map positions |
| 305 |
* |
| 306 |
* @since 1.0 |
| 307 |
* @static |
| 308 |
* @access public |
| 309 |
* @return array Returns array of the allowed map positions |
| 310 |
*/ |
| 311 |
public static function get_map_positions() { |
| 312 |
return self::$mapPostions; |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Returns the allowed post types for which map can be displayed |
| 317 |
* |
| 318 |
* @since 1.0 |
| 319 |
* @static |
| 320 |
* @access public |
| 321 |
* @return array Returns array of the allowed post types where map can be displayed |
| 322 |
*/ |
| 323 |
public static function get_allowed_posttypes() { |
| 324 |
return get_option( 'wpmi_allowed_post_types', array() ); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Returns the default map type for which map |
| 329 |
* |
| 330 |
* @since 1.0 |
| 331 |
* @static |
| 332 |
* @access public |
| 333 |
* @return string Returns default map type |
| 334 |
*/ |
| 335 |
public static function get_map_type() { |
| 336 |
return get_option( 'wpmi_map_type', 'before' ); |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Returns the default map zoom for which map |
| 341 |
* |
| 342 |
* @since 1.0 |
| 343 |
* @static |
| 344 |
* @access public |
| 345 |
* @return int Returns default map zoom |
| 346 |
*/ |
| 347 |
public static function get_map_zoom() { |
| 348 |
return get_option( 'wpmi_map_zoom', '1' ); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Returns the default map latitude for which map |
| 353 |
* |
| 354 |
* @since 1.0 |
| 355 |
* @static |
| 356 |
* @access public |
| 357 |
* @return int Returns default map latitude |
| 358 |
*/ |
| 359 |
public static function get_map_latitude() { |
| 360 |
return get_option( 'wpmi_latitude', '0' ); |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Returns the default map longitude for which map |
| 365 |
* |
| 366 |
* @since 1.0 |
| 367 |
* @static |
| 368 |
* @access public |
| 369 |
* @return int Returns default map longitude |
| 370 |
*/ |
| 371 |
public static function get_map_longitude() { |
| 372 |
return get_option( 'wpmi_longitude', '0' ); |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* Returns the default map marker |
| 377 |
* |
| 378 |
* @since 1.0 |
| 379 |
* @static |
| 380 |
* @access public |
| 381 |
* @return string Returns url of the default map marker |
| 382 |
*/ |
| 383 |
public static function get_map_marker() { |
| 384 |
$wpmiMapPin = get_option( 'wpmi_map_marker', '' ); |
| 385 |
return ( trim( $wpmiMapPin ) != '' ? $wpmiMapPin : self::$mapMarker ); |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Returns the default map width |
| 390 |
* |
| 391 |
* @since 1.0 |
| 392 |
* @static |
| 393 |
* @access public |
| 394 |
* @return int Returns the default map width |
| 395 |
*/ |
| 396 |
public static function get_map_width() { |
| 397 |
return get_option( 'wpmi_map_width', '100' ); |
| 398 |
} |
| 399 |
|
| 400 |
/** |
| 401 |
* Returns the default map width type |
| 402 |
* |
| 403 |
* @since 1.0 |
| 404 |
* @static |
| 405 |
* @access public |
| 406 |
* @return string Returns the default map width type |
| 407 |
*/ |
| 408 |
public static function get_map_width_type() { |
| 409 |
return get_option( 'wpmi_map__type', 'per' ); |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Returns the default map height |
| 414 |
* |
| 415 |
* @since 1.0 |
| 416 |
* @static |
| 417 |
* @access public |
| 418 |
* @return int Returns the default map height |
| 419 |
*/ |
| 420 |
public static function get_map_height() { |
| 421 |
return get_option( 'wpmi_map_height', '300' ); |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* Returns the default map height type |
| 426 |
* |
| 427 |
* @since 1.0 |
| 428 |
* @static |
| 429 |
* @access public |
| 430 |
* @return string Returns the default map height type |
| 431 |
*/ |
| 432 |
public static function get_map_height_type() { |
| 433 |
return get_option( 'wpmi_map_height_type', 'px' ); |
| 434 |
} |
| 435 |
|
| 436 |
/** |
| 437 |
* Returns the default map position |
| 438 |
* |
| 439 |
* @since 1.0 |
| 440 |
* @static |
| 441 |
* @access public |
| 442 |
* @return string Returns the default map position |
| 443 |
*/ |
| 444 |
public static function get_map_position() { |
| 445 |
return get_option( 'wpmi_map_position', 'before' ); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* Calling init function to activate hooks and filters. |
| 451 |
*/ |
| 452 |
wp_mapit_admin_settings::init(); |
| 453 |
|
| 454 |
} |