|
| 1 | +<?php |
| 2 | +if ( ! defined( 'ABSPATH' ) ) { |
| 3 | + exit; // Exit if accessed directly |
| 4 | +} |
| 5 | + |
| 6 | +/** |
| 7 | + * Class DT_Multisite_Tab_Google Map_Keys |
| 8 | + */ |
| 9 | +class DT_Multisite_Tab_Google_Keys |
| 10 | +{ |
| 11 | + public function content(){ |
| 12 | + $this->process_post(); |
| 13 | + ?> |
| 14 | + <div class="wrap"> |
| 15 | + <div id="poststuff"> |
| 16 | + <div id="post-body" class="metabox-holder columns-2"> |
| 17 | + <div id="post-body-content"> |
| 18 | + <!-- Main Column --> |
| 19 | + |
| 20 | + <?php $this->list_keys() ?> |
| 21 | + |
| 22 | + <!-- End Main Column --> |
| 23 | + </div><!-- end post-body-content --> |
| 24 | + <div id="postbox-container-1" class="postbox-container"> |
| 25 | + <!-- Right Column --> |
| 26 | + |
| 27 | + <?php $this->bulk_key_add() ?> |
| 28 | + |
| 29 | + <?php $this->default_key() ?> |
| 30 | + |
| 31 | + |
| 32 | + <!-- End Right Column --> |
| 33 | + </div><!-- postbox-container 1 --> |
| 34 | + <div id="postbox-container-2" class="postbox-container"> |
| 35 | + </div><!-- postbox-container 2 --> |
| 36 | + </div><!-- post-body meta box container --> |
| 37 | + </div><!--poststuff end --> |
| 38 | + </div><!-- wrap end --> |
| 39 | + <?php |
| 40 | + } |
| 41 | + |
| 42 | + public function process_post() { |
| 43 | + // update |
| 44 | + if ( isset( $_POST['google_nonce'] ) |
| 45 | + && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['google_nonce'] ) ), 'google' ) ) { |
| 46 | + |
| 47 | + if ( isset( $_POST['site_id'] ) && ! empty( $_POST['site_id'] ) ) { |
| 48 | + $site_id = sanitize_text_field( wp_unslash( $_POST['site_id'] ) ); |
| 49 | + |
| 50 | + if ( isset( $_POST['google_key'] ) && empty( $_POST['google_key'] ) ) { |
| 51 | + update_blog_option( $site_id, 'dt_google_map_key', '' ); |
| 52 | + } else { |
| 53 | + update_blog_option( $site_id, 'dt_google_map_key', sanitize_text_field( wp_unslash( $_POST['google_key'] ) ) ); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + // bulk |
| 58 | + if ( isset( $_POST['bulk_google_nonce'] ) |
| 59 | + && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['bulk_google_nonce'] ) ), 'bulk_google' ) |
| 60 | + && isset( $_POST['add_key'] ) && ! empty( $_POST['add_key'] ) |
| 61 | + ) { |
| 62 | + $key = sanitize_text_field( wp_unslash( $_POST['add_key'] ) ); |
| 63 | + |
| 64 | + global $wpdb; |
| 65 | + $sites = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->base_prefix}blogs;" ); |
| 66 | + if ( ! empty( $sites ) ) { |
| 67 | + foreach ( $sites as $site ) { |
| 68 | + if ( get_blog_option( $site, 'stylesheet' ) === 'disciple-tools-theme' ) { |
| 69 | + $current_key = get_blog_option( $site, 'dt_google_map_key' ); |
| 70 | + if ( empty( $current_key ) ) { |
| 71 | + update_blog_option( $site, 'dt_google_map_key', $key ); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + // default |
| 78 | + if ( isset( $_POST['default_google_nonce'] ) |
| 79 | + && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['default_google_nonce'] ) ), 'default_google' ) |
| 80 | + && isset( $_POST['default_key'] ) |
| 81 | + ) { |
| 82 | + $key = sanitize_text_field( wp_unslash( $_POST['default_key'] ) ); |
| 83 | + if ( empty( $_POST['default_key'] ) ) { |
| 84 | + update_network_option( 1, 'dt_google_map_key', '' ); |
| 85 | + } else { |
| 86 | + update_network_option( 1, 'dt_google_map_key', $key ); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public function list_keys(){ |
| 92 | + global $wpdb; |
| 93 | + |
| 94 | + $sites = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->base_prefix}blogs;" ); |
| 95 | + $list = []; |
| 96 | + $list['count'] = 0; |
| 97 | + if ( ! empty( $sites ) ) { |
| 98 | + foreach ( $sites as $site ) { |
| 99 | + if ( get_blog_option( $site, 'stylesheet' ) === 'disciple-tools-theme' ) { |
| 100 | + $dt_setup_options = get_blog_option( $site, "dt_setup_options", [] ); |
| 101 | + $list[$site] = []; |
| 102 | + $list[$site]['url'] = get_blog_option( $site, 'siteurl' ); |
| 103 | + $list[$site]['google_key'] = get_blog_option( $site, 'dt_google_map_key' ); |
| 104 | + $list[$site]['locations_upgraded'] = isset( $dt_setup_options["google_upgrade"] ); |
| 105 | + $list['count']++; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + ?> |
| 111 | + <!-- Box --> |
| 112 | + <table class="widefat striped"> |
| 113 | + <thead> |
| 114 | + <tr> |
| 115 | + <th>Site Name (<?php echo esc_attr( $list['count'] ) ?>)</th> |
| 116 | + <th>Keys</th> |
| 117 | + <th></th> |
| 118 | + </tr> |
| 119 | + </thead> |
| 120 | + <tbody> |
| 121 | + |
| 122 | + <?php if ( ! empty( $list ) ) : unset( $list['count'] ); foreach ( $list as $id => $site ) : ?> |
| 123 | + <form method="post"> |
| 124 | + <tr> |
| 125 | + <td style="width:30%;"> |
| 126 | + <?php echo esc_url( $site['url'] ) ?> |
| 127 | + </td> |
| 128 | + <td> |
| 129 | + <?php wp_nonce_field( 'google', 'google_nonce' ) ?> |
| 130 | + <input type="hidden" name="site_id" value="<?php echo esc_attr( $id ) ?>" /> |
| 131 | + <input type="text" class="regular-text" style="width:100%;" value="<?php echo esc_attr( $site['google_key'] ) ?>" name="google_key" placeholder="add google key" /> |
| 132 | + </td> |
| 133 | + <td style="width:10%;"> |
| 134 | + <button class="button btn" type="submit">Update</button> |
| 135 | + </td> |
| 136 | + </tr> |
| 137 | + </form> |
| 138 | + <?php endforeach; |
| 139 | + endif; ?> |
| 140 | + </tbody> |
| 141 | + </table> |
| 142 | + <br> |
| 143 | + <!-- End Box --> |
| 144 | + <?php |
| 145 | + } |
| 146 | + |
| 147 | + public function bulk_key_add(){ |
| 148 | + ?> |
| 149 | + <!-- Box --> |
| 150 | + <form method="post"> |
| 151 | + <?php wp_nonce_field( 'bulk_google', 'bulk_google_nonce' ) ?> |
| 152 | + <table class="widefat striped"> |
| 153 | + <thead> |
| 154 | + <tr> |
| 155 | + <th>Add Google Map Key to All Empty Sites</th> |
| 156 | + </tr> |
| 157 | + </thead> |
| 158 | + <tbody> |
| 159 | + <tr> |
| 160 | + <td> |
| 161 | + <input type="text" name="add_key" /> |
| 162 | + </td> |
| 163 | + </tr> |
| 164 | + <tr> |
| 165 | + <td> |
| 166 | + <button class="button btn" type="submit">Add Key to All Empty Fields</button> |
| 167 | + </td> |
| 168 | + </tr> |
| 169 | + </tbody> |
| 170 | + </table> |
| 171 | + </form> |
| 172 | + <br> |
| 173 | + <!-- End Box --> |
| 174 | + <?php |
| 175 | + } |
| 176 | + |
| 177 | + public function default_key(){ |
| 178 | + $key = get_network_option( 1, 'dt_google_map_key' ); |
| 179 | + ?> |
| 180 | + <!-- Box --> |
| 181 | + <form method="post"> |
| 182 | + <?php wp_nonce_field( 'default_google', 'default_google_nonce' ) ?> |
| 183 | + <table class="widefat striped"> |
| 184 | + <thead> |
| 185 | + <tr> |
| 186 | + <th>Set Default Google Map Key</th> |
| 187 | + </tr> |
| 188 | + </thead> |
| 189 | + <tbody> |
| 190 | + <tr> |
| 191 | + <td> |
| 192 | + <input type="text" name="default_key" value="<?php echo esc_attr( $key ) ?>" /> |
| 193 | + </td> |
| 194 | + </tr> |
| 195 | + <tr> |
| 196 | + <td> |
| 197 | + <button class="button btn" type="submit">Set Default Key</button> |
| 198 | + </td> |
| 199 | + </tr> |
| 200 | + </tbody> |
| 201 | + </table> |
| 202 | + </form> |
| 203 | + <br> |
| 204 | + <!-- End Box --> |
| 205 | + <?php |
| 206 | + } |
| 207 | + |
| 208 | +} |
0 commit comments