diff options
| author | Jonas Kümmerlin <jonas@kuemmerlin.eu> | 2024-01-27 13:05:41 +0100 |
|---|---|---|
| committer | Jonas Kümmerlin <jonas@kuemmerlin.eu> | 2024-01-27 13:05:41 +0100 |
| commit | 907f39db4ab00de43016646660d9d1ca9c73d1f9 (patch) | |
| tree | 49642a7e9ab374c3eab2ae91b12a24fdf4a21f62 /admin.php | |
Diffstat (limited to 'admin.php')
| -rw-r--r-- | admin.php | 216 |
1 files changed, 216 insertions, 0 deletions
diff --git a/admin.php b/admin.php new file mode 100644 index 0000000..8e98db7 --- /dev/null +++ b/admin.php @@ -0,0 +1,216 @@ +<?php + +function communievents_admin_page_content() { + ?> + <div class="wrap"> + <h1>CommuniEvents</h1> + <form method="post" action="options.php"> + <?php + settings_fields('communievents-admin-page'); + do_settings_sections('communievents-admin-page'); + + submit_button(); + ?> + </form> + <p> <p> + <h2>App-Informationen</h2> + <?php $api = CommuniApi::defaultInstance(); ?> + <table> + <tr> + <td>Benutzer: + <td><?php + $user = $api->user(); + if (!empty($user)) { + $url = $user->webappUrl(); + + if (!empty($url)) + echo '<a href="' . esc_url($url) . '" target="_blank">'; + + echo esc_html("{$user->name()} ({$user->id()})"); + + if (!empty($url)) + echo '</a>'; + } else { + echo 'N/A (Zugangstoken falsch?)'; + } + ?> + <tr> + <td>Hauptgruppe: + <td><?php + $group = $api->group(); + if (!empty($group)) { + $url = $group->webappUrl(); + + if (!empty($url)) + echo '<a href="' . esc_url($url) . '" target="_blank">'; + + echo esc_html("{$group->title()} ({$group->id()})"); + + if (!empty($url)) + echo '</a>'; + } else { + echo 'N/A (Zugangstoken falsch?)'; + } + ?> + <tr> + <td>Weitere Gruppen: + <td><?php + if (!empty($user)) { + $groups = $user->groups(); + $groups_html = array(); + foreach ($groups as $g) { + if ($g === $group) + continue; + + $html = ''; + $url = $g->webappUrl(); + + if (!empty($url)) + $html .= '<a href="' . esc_url($url) . '" target="_blank">'; + + $html .= esc_html("{$g->title()} ({$g->id()})"); + + if (!empty($url)) + $html .= '</a>'; + + $groups_html[] = $html; + } + + echo implode(', ', $groups_html); + } + ?> + <tr> + <td>CommuniApp: + <td><?php + $url = $api->webappUrl(); + if (!empty($url)) { + echo '<a href="' . esc_url($url) . '" target="_blank">' . esc_url($url) . '</a>'; + } else { + echo 'N/A (Zugangstoken falsch?)'; + } + ?> + </table> + <p> <p> + <h2>Synchronisation</h2> + <?php + global $wpdb; + + $tablename = $wpdb->prefix . 'communievents_events'; + + $evcount = $wpdb->get_var("SELECT COUNT(id) FROM $tablename", 0, 0); + $lastsync = $wpdb->get_var("SELECT MAX(lastsync) FROM $tablename", 0, 0); + $nextsync = wp_next_scheduled('communievents_cron_hook'); + + if ($lastsync) { + $lastsync_dt = new DateTime("@$lastsync"); + $lastsync_dt->setTimezone(wp_timezone()); + $lastsync_str = $lastsync_dt->format('Y-m-d H:i:s'); + } else { + $lastsync_str = "nie"; + } + + if ($nextsync) { + $nextsync_dt = new DateTime("@$nextsync"); + $nextsync_dt->setTimezone(wp_timezone()); + $nextsync_str = $nextsync_dt->format('Y-m-d H:i:s'); + } else { + $nextsync_str = "nie"; + } + ?> + <table> + <tr> + <td>Anzahl Ereignisse: + <td><?php echo esc_html($evcount); ?> + <tr> + <td>Letzte Synchronisation: + <td><?php echo esc_html($lastsync_str); ?> + <tr> + <td>Nächste Synchronisation: + <td><?php echo esc_html($nextsync_str); ?> + </table> + <form method="POST" action="<?php echo admin_url('admin-post.php'); ?>"> + <?php wp_nonce_field('communievents-admin-sync'); ?> + <input type="hidden" name="action" value="communievents_sync" /> + <input type="hidden" name="backurl" value="<?php menu_page_url('communievents-admin-page'); ?>" /> + <input type="submit" value="Jetzt Synchronisieren" class="button button-primary" /> + </form> + </div> + <?php +} + +function communievents_render_settings_section() { + ?><p class="description">Das Token findest Du in deiner CommuniApp unter <em>Admin Bereich</em> ➔ <em>Rest-Api</em><?php +} + +function communievents_render_auth_token_input() { + ?><p><textarea class="large-text code" rows="5" name="communievents-auth-token"><?php echo esc_textarea(get_option('communievents-auth-token')); ?></textarea><?php +} + +function communievents_setup_admin_menu() { + add_options_page('CommuniEvents', 'CommuniEvents', 'manage_options', 'communievents-admin-page', 'communievents_admin_page_content'); +} + +function communievents_setup_settings() { + add_settings_section( + 'communievents-settings-section', // ID used to identify this section and with which to register options + 'CommuniApp REST Zugangstoken', // Title to be displayed on the administration page + 'communievents_render_settings_section', // Callback used to render the description of the section + 'communievents-admin-page' // Page on which to add this section of options + ); + + add_settings_field( + 'communievents-auth-token', // ID used to identify the field throughout the theme + 'Zugangstoken', // The label to the left of the option interface element + 'communievents_render_auth_token_input', // The name of the function responsible for rendering the option interface + 'communievents-admin-page', // The page on which this option will be displayed + 'communievents-settings-section', // The name of the section to which this field belongs + array( // The array of arguments to pass to the callback. In this case, just a description. + + ) + ); + + register_setting('communievents-admin-page', 'communievents-auth-token'); +} + +function communievents_admin_handle_sync_click() { + if (!current_user_can('manage_options')) { + wp_die('Insufficient permissions', 'CommuniEvents Error', array('response' => 403)); + } + + check_admin_referer('communievents-admin-sync'); + + // disable output buffering so our headers_sent() trick works + while (ob_get_level() > 0) { + ob_end_flush(); + } + + communievents_scrape_events(); + + // no news is good news! unless the scraper did output something, + // immediately redirect to the admin page + if (!headers_sent()) { + header("Location: $_POST[backurl]"); + die(); + } + + ?> + <p> + Bei der Synchronisierung ist ein Fehler aufgetreten! + </p> + <form method="POST" action="<?php echo admin_url('admin-post.php'); ?>"> + <?php wp_nonce_field('communievents-admin-sync'); ?> + <input type="hidden" name="action" value="<?php echo esc_attr($_POST['action']); ?>" /> + <input type="hidden" name="backurl" value="<?php echo esc_attr($_POST['backurl']); ?>" /> + <input type="submit" value="Nochmal" class="button button-primary" /> + </form> + <p> + <a href="<?php echo esc_url($_POST['backurl']); ?>">Zurück</a> + </p> + <?php +} + +function communievents_setup_admin_page() { + add_action('admin_menu', 'communievents_setup_admin_menu'); + add_action('admin_init', 'communievents_setup_settings'); + add_action('admin_post_communievents_sync', 'communievents_admin_handle_sync_click'); +} |
