Skip to content

Commit

Permalink
Merge pull request #11 from jawordpressorg/feature/basic-auth
Browse files Browse the repository at this point in the history
Feature/basic auth
  • Loading branch information
fumikito committed Feb 12, 2024
2 parents d87fa45 + 89e52f1 commit 25bc215
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 473 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/wordpress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
with:
flags: '-rptv --checksum --delete'
options: '--exclude-from=.distignore'
ssh_options: ''
ssh_options: '-p 2222'
src: './'
dest: "${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:/home/users/2/${{ secrets.DEPLOY_USER }}/web/wp-checkin/wp-content/plugins/${{ github.event.repository.name }}/"

Expand Down
50 changes: 0 additions & 50 deletions lib/WCTokyo/WpCheckin/FireBase.php

This file was deleted.

71 changes: 69 additions & 2 deletions lib/WCTokyo/WpCheckin/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ protected function init() {
add_action( 'init', [ $this, 'add_rewrite_rules' ] );
add_filter( 'query_vars', [ $this, 'add_query_vars' ] );
add_action( 'pre_get_posts', [ $this, 'pre_get_posts' ] );
add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ], 300 );
}

/**
Expand All @@ -45,12 +46,15 @@ public function pre_get_posts( $wp_query ) {
if ( ! get_query_var( 'checkin' ) || ! $wp_query->is_main_query() ) {
return;
}
if ( in_array( $is_checkin, [ 'archive', 'single' ], true ) ) {
if ( in_array( $is_checkin, [ 'archive', 'single', 'qr' ], true ) ) {
$do_auth_header = true;
wp_enqueue_style( 'wp-checkin' );
// Load template and exit.
$args = [];
switch ( $is_checkin ) {
case 'qr':
$this->render_qr();
break;
case 'archive':
$args = [
'title' => __( '登録済みのチケット', 'wp-checkin' ),
Expand Down Expand Up @@ -99,6 +103,7 @@ public function pre_get_posts( $wp_query ) {
public function add_rewrite_rules() {
// Front archive.
add_rewrite_rule( '^checkin/?$', 'index.php?checkin=archive', 'top' );
add_rewrite_rule( '^checkin/qr.png/?$', 'index.php?checkin=qr', 'top' );
add_rewrite_rule( '^checkin/page/(\d+)/?$', 'index.php?checkin=archive&paged=$matches[1]', 'top' );
add_rewrite_rule( '^checkin/ticket/(\d+)/?$', 'index.php?checkin=single&p=$matches[1]', 'top' );
}
Expand All @@ -109,6 +114,68 @@ public function add_rewrite_rules() {
* @return void
*/
public function do_authorization_header() {
// W.I.P
$user = get_option( 'wordcamp_auth_user' );
$pass = get_option( 'wordcamp_auth_pass' );
if ( ! isset( $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] ) || $user !== $_SERVER['PHP_AUTH_USER'] || $pass !== $_SERVER['PHP_AUTH_PW'] ) {
header( 'WWW-Authenticate: Basic realm="Enter username and password."' );
header( 'Content-Type: text/plain; charset=utf-8' );
wp_die( __( 'このページを閲覧するためにはユーザー名とパスワードが必要です。', 'wp-checkin' ), get_status_header_desc( 401 ), [
'status' => 401,
'response' => 401,
] );
}
}

/**
* Custom admin bar.
*
* @param \WP_Admin_Bar $admin_bar Admin bar instance.
* @return void
*/
public function admin_bar_menu( \WP_Admin_Bar &$admin_bar ) {
$admin_bar->add_node( [
'parent' => 'site-name',
'id' => 'wp-checkin',
'title' => __( 'チケット一覧ページ', 'wp=-checkin' ),
'href' => home_url( 'checkin' ),
'meta' => [
'tabindex' => 0,
],
] );
}

/**
* Render QR code.
*
* @return void
*/
public function render_qr() {
$url = home_url( 'checkin' );
$params = [
'g' => 2,
'f' => 3,
'e' => 4,
];
$query = [];
foreach ( $params as $name => $index ) {
$query[ $index ] = filter_input( INPUT_GET, $name );
}
$tickets = Tickets::search( $query );
if ( 1 === $tickets['total'] ) {
$url = home_url( 'checkin/' . $tickets['tickets'][0][0] );
} elseif ( ! empty( $query[4] ) ) {
// Not found. Try to search with email.
$url = home_url( 'checkin/?s=' . rawurlencode( $query[4] ) );
}
// Generate URL with Google Chart API.
$api_url = add_query_arg( [
'cht' => 'qr',
'chs' => '300x300',
'chl' => $url,
], 'https://chart.apis.google.com/chart' );
$content = file_get_contents( $api_url );
header( 'Content-Type: image/png' );
echo $content;
exit;
}
}
71 changes: 70 additions & 1 deletion lib/WCTokyo/WpCheckin/Screen/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace WCTokyo\WpCheckin\Screen;


use PHP_CodeSniffer\Generators\HTML;
use WCTokyo\WpCheckin\Pattern\SingletonPattern;
use WCTokyo\WpCheckin\Tickets;

/**
* Create setting screen.
Expand All @@ -22,6 +24,7 @@ protected function init() {
add_action( 'admin_init', [ $this, 'register_option' ] );
add_action( 'admin_menu', [ $this, 'add_menu' ] );
add_action( 'wp_ajax_' . $this->ajax_action, [ $this, 'upload_csv' ] );
add_action( 'admin_notices', [ $this, 'notification' ] );
}

/**
Expand All @@ -34,7 +37,7 @@ public function add_menu() {
?>
<div class="wrap">
<h1><?php esc_html_e( 'WordCamp チェックイン設定', 'wp-checkin' ); ?></h1>
<form method="post" action="options.php">
<form method="post" action="<?php echo admin_url( 'options.php' ); ?>">
<?php
settings_fields( 'wp-checkin' );
do_settings_sections( 'wp-checkin' );
Expand All @@ -43,6 +46,21 @@ public function add_menu() {
</form>
<h2><?php esc_html_e( 'チケット情報', 'wp-checkin' ); ?></h2>
<?php $this->csv_form(); ?>
<h2><?php esc_html_e( 'HTMLタグ', 'wp-checkin' ); ?></h2>
<p>
<?php esc_html_e( 'WordCampサイトにおいてチケット購入者に送るメールに以下のHTMLタグを入力してください。チケットのチェックインページを開くQRコードが表示されます。', 'wp-checkin' ); ?>
</p>
<?php
$url = add_query_arg( [
'g' => '[first_name]',
'f' => '[last_name]',
'e' => '[email]',
], home_url( '/checkin/qr.png' ) );
$text = <<<HTML
<img src="{$url}" alt="" width="300" height="300" />
HTML;
?>
<textarea readonly onclick="this.select();" style="width: 100%; box-sizing: border-box"><?php echo esc_textarea( $text ); ?></textarea>
</div>
<?php
} );
Expand Down Expand Up @@ -174,4 +192,55 @@ public function upload_csv() {
exit;
}
}

/**
* メールアドレスなどについての注意書きを出す
*
* @return void
*/
public function notification() {
$success = true;
$messages = [];
$admin_ulr = admin_url( 'options-general.php?page=wp-checkin' );
// Site setting
$url = get_option( 'wordcamp_site_url' );
if ( $url ) {
// translators: %1$s is URL.
$messages[] = sprintf( __( 'WordCampサイトは<a href="%1$s">%1$s</a>です。', 'wp-checkin' ), esc_url( $url ) );
} else {
$success = false;
// translators: %s is URL.
$messages[] = sprintf( __( 'WordCampサイトのURLが<a href="%s">設定</a>されていません。', 'wp-checkin' ), $admin_ulr );
}
// Basic auth setting.
$user = get_option( 'wordcamp_auth_user' );
$pass = get_option( 'wordcamp_auth_pass' );
if ( $user && $pass ) {
$messages[] = __( 'チケットページはパスワードで保護されています。', 'wp-checkin' );
} else {
$success = false;
// translators: %s is URL.
$messages[] = sprintf( __( 'チケットページがパスワード保護されていません。パスワードを<a href="%s">設定</a>してください。', 'wp-checkin' ), $admin_ulr );
}
// Ticket imported.
$total = count( Tickets::tickets( false ) );
if ( 1 < $total ) {
// translators: %d is ticket count.
$messages[] = sprintf( __( '%d件のチケット情報が登録されています。', 'wp-checkin' ), $total );
} else {
$success = false;
// translators: %s is URL.
$messages[] = sprintf( __( 'チケットのCSが登録されていません。パスワードを<a href="%s">設定</a>してください。', 'wp-checkin' ), $admin_ulr );
}
// Output message.
?>
<div class="notice notice-<?php echo $success ? 'success' : 'error'; ?>">
<ol>
<?php foreach ( $messages as $message ) : ?>
<li><?php echo wp_kses_post( $message ); ?></li>
<?php endforeach; ?>
</ol>
</div>
<?php
}
}
Loading

0 comments on commit 25bc215

Please sign in to comment.