|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class that defines a theme settings dashboard for a Google Maps integration. |
| 4 | + * This includes both the page and individual setting field. |
| 5 | + * |
| 6 | + * @package MITlib_Parent |
| 7 | + * @since 0.8 |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Mitlib\Parent; |
| 11 | + |
| 12 | +/** |
| 13 | + * Defines settings dashboard for maps integration. This class gets loaded and |
| 14 | + * hooked into from the theme's functions.php file. |
| 15 | + */ |
| 16 | +class Maps { |
| 17 | + /** |
| 18 | + * The required permission to access the admin page. |
| 19 | + */ |
| 20 | + const PERMS = 'manage_options'; |
| 21 | + |
| 22 | + /** |
| 23 | + * This defines the admin page which holds the settings management form. |
| 24 | + */ |
| 25 | + public static function init() { |
| 26 | + if ( current_user_can( self::PERMS ) ) { |
| 27 | + add_options_page( |
| 28 | + 'Maps', |
| 29 | + 'Map', |
| 30 | + self::PERMS, |
| 31 | + 'mitlib-maps-dashboard', |
| 32 | + array( 'Mitlib\Parent\Maps', 'dashboard' ) |
| 33 | + ); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * This defines the content displayed on the admin page. It also receives the |
| 39 | + * submission of the web form on that page, and calls the update action when |
| 40 | + * needed. |
| 41 | + */ |
| 42 | + public static function dashboard() { |
| 43 | + // Confirm user capabilities before doing anything. |
| 44 | + if ( ! current_user_can( self::PERMS ) ) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + // If the form has been posted, the update method handles that. |
| 49 | + $action = filter_input( INPUT_POST, 'action' ); |
| 50 | + if ( ! empty( $action ) ) { |
| 51 | + self::update(); |
| 52 | + } |
| 53 | + |
| 54 | + // Finally, we render the form itself. |
| 55 | + echo '<div class="wrap">'; |
| 56 | + echo '<h1>Maps settings</h1>'; |
| 57 | + echo '<p>This form will update the Google Maps API key which grants us access to use their map assets on our locations page.</p>'; |
| 58 | + echo '<p>This key is managed by the [email protected] user as part of the <a href="https://console.cloud.google.com/apis/credentials?project=api-project-260287310852">Google Cloud Platform</a>.</p>'; |
| 59 | + echo '<form method="post" action="">'; |
| 60 | + wp_nonce_field( 'custom_nonce_action', 'custom_nonce_field' ); |
| 61 | + settings_fields( 'mitlib_parent_maps' ); |
| 62 | + do_settings_sections( 'mitlib-maps-dashboard' ); |
| 63 | + submit_button( 'Update maps settings' ); |
| 64 | + echo '</form>'; |
| 65 | + echo '</div>'; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * This is the general descriptive statement at the top of the settings form. |
| 70 | + * We don't use this, but the method needs to exist. |
| 71 | + */ |
| 72 | + public static function general() { |
| 73 | + echo ''; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Field rendering callback for the Google Maps key setting. |
| 78 | + */ |
| 79 | + public static function google_maps_key_callback() { |
| 80 | + $google_maps_key = get_option( 'google_maps_key' ); |
| 81 | + echo '<input type="text" name="google_maps_key" value="' . esc_attr( $google_maps_key ) . '" id="google_maps_key" size="60">'; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Register the setting field which holds the Google maps key. This includes |
| 86 | + * creating a section, which is a bit overkill for only one setting, but here |
| 87 | + * we are. |
| 88 | + */ |
| 89 | + public static function settings() { |
| 90 | + register_setting( 'mitlib_parent_maps', 'google_maps_key' ); |
| 91 | + |
| 92 | + add_settings_section( |
| 93 | + 'mitlib_parent_maps_general', |
| 94 | + 'General settings', |
| 95 | + array( 'Mitlib\Parent\Maps', 'general' ), |
| 96 | + 'mitlib-maps-dashboard' |
| 97 | + ); |
| 98 | + |
| 99 | + add_settings_field( |
| 100 | + 'google_maps_key', |
| 101 | + 'Google Maps API key', |
| 102 | + array( 'Mitlib\Parent\Maps', 'google_maps_key_callback' ), |
| 103 | + 'mitlib-maps-dashboard', |
| 104 | + 'mitlib_parent_maps_general', |
| 105 | + array( |
| 106 | + 'label_for' => 'google_maps_key', |
| 107 | + 'class' => 'mitlib_maps_row', |
| 108 | + ) |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Update setting based on posted form information. |
| 114 | + */ |
| 115 | + public static function update() { |
| 116 | + check_admin_referer( 'custom_nonce_action', 'custom_nonce_field' ); |
| 117 | + |
| 118 | + if ( 'update' == filter_input( INPUT_POST, 'action' ) ) { |
| 119 | + $google_maps_key = ''; |
| 120 | + |
| 121 | + if ( filter_input( INPUT_POST, 'google_maps_key' ) ) { |
| 122 | + $google_maps_key = sanitize_text_field( |
| 123 | + wp_unslash( filter_input( INPUT_POST, 'google_maps_key' ) ) |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + update_option( 'google_maps_key', $google_maps_key ); |
| 128 | + } |
| 129 | + |
| 130 | + echo( '<div class="updated"><p>The maps settings have been updated.</p></div>' ); |
| 131 | + } |
| 132 | +} |
0 commit comments