'string', 'default' => '', 'sanitize_callback' => array( Event_Permalink::class, 'sanitize_slug' ), ) ); register_setting( self::GROUP, self::LIST_PAGE, array( 'type' => 'integer', 'default' => 0, 'sanitize_callback' => array( self::class, 'sanitize_list_page' ), ) ); register_setting( self::GROUP, self::MAPS_KEY, array( 'type' => 'string', 'default' => '', 'sanitize_callback' => array( self::class, 'sanitize_maps_key' ), ) ); register_setting( self::GROUP, Connection::KEY_OPTION, array( 'type' => 'string', 'default' => '', 'sanitize_callback' => array( self::class, 'sanitize_api_key' ), ) ); register_setting( self::GROUP, self::DELETE_EVENTS, array( 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean', ) ); } /** * キーと識別の option を毎回の読み込みに載せない。 * * @param bool|null $autoload 自動で読むか。 * @param string $option 名前。 * @return bool|null */ public static function exclude_key_from_autoload( $autoload, $option ) { return in_array( $option, self::NOT_AUTOLOADED, true ) ? false : $autoload; } /** * 一覧のページ。公開の固定ページだけを受ける。 * * @param mixed $value 入力。 * @return int */ public static function sanitize_list_page( $value ) { $page = get_post( absint( $value ) ); return $page && 'page' === $page->post_type && 'publish' === $page->post_status ? (int) $page->ID : 0; } /** * Google Maps のキー。URL に載せるので、形の違う値を受けない。誤りのときは元の値を保つ。 * * @param mixed $value 入力。 * @return string */ public static function sanitize_maps_key( $value ) { $key = is_string( $value ) ? trim( $value ) : ''; if ( '' === $key || preg_match( '/^[A-Za-z0-9_-]+$/', $key ) ) { return $key; } // 管理画面の外(WP-CLI など)にはこの関数が無い。 if ( function_exists( 'add_settings_error' ) ) { add_settings_error( self::MAPS_KEY, 'eventwatch_invalid_maps_key', esc_html__( 'The Google Maps API key can contain only letters, numbers, hyphens, and underscores.', 'eventwatch' ) ); } return (string) get_option( self::MAPS_KEY, '' ); } /** * EventWatch の API キー。空は「替えない」。保存したキーは画面に出さないので、欄はいつも空で届く。 * 消すのは「キーを削除」だけ。誤りのときは元の値を保つ。 * * @param mixed $value 入力。 * @return string */ public static function sanitize_api_key( $value ) { $current = (string) get_option( Connection::KEY_OPTION, '' ); $key = is_string( $value ) ? trim( $value ) : ''; if ( '' === $key || '' !== Connection::fixed_key() ) { return $current; } if ( preg_match( '/^ewk_[A-Za-z0-9]{43}\z/', $key ) ) { return $key; } // 管理画面の外(WP-CLI など)にはこの関数が無い。 if ( function_exists( 'add_settings_error' ) ) { add_settings_error( Connection::KEY_OPTION, 'eventwatch_invalid_api_key', esc_html__( 'That doesn’t look like an EventWatch API key. Keys start with “ewk_” and have 47 characters. Copy the whole key and try again.', 'eventwatch' ) ); } return $current; } /** * 画面をメニューに足す。 * * @return void */ public static function add_page() { add_options_page( esc_html__( 'EventWatch Settings', 'eventwatch' ), 'EventWatch', 'manage_options', self::PAGE, array( self::class, 'render_page' ) ); } /** * 欄を足す。 * * @return void */ public static function add_fields() { add_settings_section( self::SECTION_CONNECTION, esc_html__( 'EventWatch connection', 'eventwatch' ), array( self::class, 'render_connection_section' ), self::PAGE ); add_settings_field( Connection::KEY_OPTION, esc_html__( 'EventWatch API key', 'eventwatch' ), array( self::class, 'render_api_key_field' ), self::PAGE, self::SECTION_CONNECTION, array( 'label_for' => Connection::KEY_OPTION ) ); add_settings_field( 'eventwatch_sync_status', esc_html__( 'Sending to EventWatch', 'eventwatch' ), array( self::class, 'render_sync_status_field' ), self::PAGE, self::SECTION_CONNECTION ); add_settings_section( self::SECTION_DETAIL, esc_html__( 'Event pages', 'eventwatch' ), '__return_false', self::PAGE ); add_settings_field( Post_Type::SLUG_OPTION, esc_html__( 'URL prefix', 'eventwatch' ), array( self::class, 'render_slug_field' ), self::PAGE, self::SECTION_DETAIL, array( 'label_for' => Post_Type::SLUG_OPTION ) ); add_settings_field( self::LIST_PAGE, esc_html__( 'Event list page', 'eventwatch' ), array( self::class, 'render_list_page_field' ), self::PAGE, self::SECTION_DETAIL, array( 'label_for' => self::LIST_PAGE ) ); add_settings_field( self::MAPS_KEY, esc_html__( 'Google Maps API key (optional)', 'eventwatch' ), array( self::class, 'render_maps_key_field' ), self::PAGE, self::SECTION_DETAIL, array( 'label_for' => self::MAPS_KEY ) ); add_settings_section( self::SECTION_REMOVAL, esc_html__( 'Removing the plugin', 'eventwatch' ), '__return_false', self::PAGE ); add_settings_field( self::DELETE_EVENTS, esc_html__( 'Events', 'eventwatch' ), array( self::class, 'render_delete_events_field' ), self::PAGE, self::SECTION_REMOVAL ); } /** * 設定画面の小さな並びの指定。 * * @param string $hook_suffix 画面。 * @return void */ public static function enqueue_styles( $hook_suffix ) { if ( 'settings_page_' . self::PAGE !== $hook_suffix ) { return; } wp_register_style( 'eventwatch-settings', false, array(), '1' ); wp_enqueue_style( 'eventwatch-settings' ); wp_add_inline_style( 'eventwatch-settings', '.eventwatch-key{display:flex;flex-wrap:wrap;align-items:center;gap:8px}' . '.eventwatch-key input{max-width:100%}' . '.eventwatch-key details[open]{flex-basis:100%}' . '.eventwatch-key__change{display:flex;flex-wrap:wrap;align-items:center;gap:8px;margin-top:8px}' . '.eventwatch-key__change label{flex-basis:100%}' . '.eventwatch-key__change .description{flex-basis:100%;margin:0}' . '.eventwatch-status{display:flex;gap:4px;margin:8px 0 0}' . '.eventwatch-status .dashicons{flex:none}' ); } /** * 画面を描く。 * * @return void */ public static function render_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } ?>
%6$s
%s
', esc_html__( 'This key is set in wp-config.php, so it can’t be changed here.', 'eventwatch' ) ); } self::render_api_key_description( ! $fixed ); } /** * キーがまだ無いときの欄。 * * @return void */ private static function render_empty_api_key_field() { printf( '', esc_attr( Connection::KEY_OPTION ), esc_attr__( 'Not set', 'eventwatch' ) ); printf( '%s
', esc_html__( 'To list your events on EventWatch, you need an EventWatch account and an API key. Your site works without them: events stay on your site and nothing is sent.', 'eventwatch' ) ); printf( '', esc_url( Connection_Admin::web_url( '/organizer/api-keys' ) ), esc_html__( 'Issue an API key on EventWatch', 'eventwatch' ) ); self::render_api_key_description( true ); } /** * キーの説明。 * * @param bool $recommend_constant 定数を勧める文を添えるか。 * @return void */ private static function render_api_key_description( $recommend_constant ) { printf( '%s
', esc_html__( 'The key you issued in EventWatch under Organizer settings › Site connection. Saved keys are hidden.', 'eventwatch' ) ); self::render_consent(); if ( ! $recommend_constant ) { return; } printf( '%s
', esc_html__( 'A key saved here is also included in database backups and copies of your site. We recommend setting it in wp-config.php instead:', 'eventwatch' ) ); echo 'define( \'EVENTWATCH_API_KEY\', \'ewk_…\' );
%s
', esc_html__( 'On a multisite network, the constant applies to every site. To use a different key for each site, save the key on each site’s settings page.', 'eventwatch' ) ); } } /** * 投稿の規約への同意。EventWatch の投稿フォームの送信と同じ形で、操作に添えて伝える。 * 定数のキーは欄を通らないので、チェックボックスでは取れない。 * * @return void */ private static function render_consent() { printf( '', esc_html( '' !== Connection::fixed_key() ? __( 'By sending events with this key, you agree to the EventWatch Terms of Service and Posting Guidelines.', 'eventwatch' ) : __( 'By saving a key, you agree to the EventWatch Terms of Service and Posting Guidelines.', 'eventwatch' ) ), esc_url( Connection_Admin::web_url( '/terms' ) ), esc_html__( 'Terms of Service', 'eventwatch' ), esc_url( Connection_Admin::web_url( '/post/guidelines' ) ), esc_html__( 'Posting Guidelines', 'eventwatch' ) ); } /** * つながっているかをアイコンと語で出す。 * * @return void */ private static function render_connection_status() { $state = Connection::state(); $kinds = array( Connection::CONNECTED => array( 'yes-alt', '#007017' ), Connection::UNCHECKED => array( 'clock', 'inherit' ), Connection::UNREACHABLE => array( 'warning', '#996800' ), Connection::UNAUTHORIZED => array( 'warning', '#b32d2e' ), Connection::SUSPENDED => array( 'warning', '#b32d2e' ), Connection::FORBIDDEN => array( 'warning', '#b32d2e' ), ); $kind = isset( $kinds[ $state['status'] ] ) ? $kinds[ $state['status'] ] : $kinds[ Connection::UNCHECKED ]; printf( '%3$s
', esc_attr( $kind[0] ), esc_attr( $kind[1] ), esc_html( Connection_Admin::status_text( $state ) ) ); if ( Connection::UNAUTHORIZED === $state['status'] || Connection::is_site_bound( $state ) ) { $link = array( '/organizer/api-keys', __( 'Open Site connection on EventWatch', 'eventwatch' ) ); } elseif ( in_array( $state['status'], array( Connection::SUSPENDED, Connection::FORBIDDEN ), true ) ) { $link = array( '/contact', __( 'Contact EventWatch', 'eventwatch' ) ); } else { return; } printf( '', esc_url( Connection_Admin::web_url( $link[0] ) ), esc_html( $link[1] ) ); } /** * 同期の健康状態の欄。 * * @return void */ public static function render_sync_status_field() { $last = Sync_Status::last_success(); $pending = Sync_Status::pending_count(); printf( '%s
', esc_html( sprintf( /* translators: %s: date and time, or "Not yet". */ __( 'Last successful send: %s', 'eventwatch' ), $last > 0 ? wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $last ) : __( 'Not yet', 'eventwatch' ) ) ) ); printf( '%s
', esc_html( sprintf( /* translators: %d: number of events. */ _n( 'Waiting to send: %d event', 'Waiting to send: %d events', $pending, 'eventwatch' ), $pending ) ) ); self::render_send_all(); printf( '%1$s %3$s
', esc_html( Sync_Status::is_wp_cron_disabled() ? __( 'DISABLE_WP_CRON is on, so WP-Cron doesn’t run on its own. If your server’s cron doesn’t run it, failed sends aren’t retried.', 'eventwatch' ) : __( 'Retries use WP-Cron, which only runs when someone visits your site. To retry on time, run it from your server’s cron.', 'eventwatch' ) ), esc_url( self::CRON_HELP_URL ), esc_html__( 'How to run WP-Cron from your server', 'eventwatch' ) ); } /** * 公開中でまだ送っていないイベントの件数と、まとめて送る操作。対象が無ければ出さない。 * キーの保存では送らないので、後からキーを入れたサイトはここから送る。 * * @return void */ private static function render_send_all() { if ( '' === Connection::api_key() ) { return; } $count = count( Sync_Queue::unsent_published_ids() ); if ( 0 === $count ) { return; } printf( '%s
', esc_html( sprintf( /* translators: %d: number of events. */ _n( 'Published but not sent yet: %d event', 'Published but not sent yet: %d events', $count, 'eventwatch' ), $count ) ) ); printf( '', esc_attr( Connection_Admin::ACTION_SEND_ALL ), disabled( Sync_Queue::can_send_unsent(), false, false ), esc_html__( 'Send all to EventWatch', 'eventwatch' ) ); printf( '%s
', esc_html__( 'Each event goes to review on EventWatch. Events are sent a few dozen at a time with WP-Cron, so this can take a while. Events set not to send, events that have ended, and events missing required details aren’t included.', 'eventwatch' ) ); } /** * プラグインの削除で投稿も消すかの欄。 * * @return void */ public static function render_delete_events_field() { printf( '', esc_attr( self::DELETE_EVENTS ), checked( (bool) get_option( self::DELETE_EVENTS, false ), true, false ), esc_html__( 'Delete all events when the plugin is deleted', 'eventwatch' ) ); printf( '%s
', esc_html__( 'Off by default, so your events stay in the database. If you turn this on, deleting the plugin permanently removes the events on this site, and you can’t restore them.', 'eventwatch' ) ); } /** * 接頭辞の欄。 * * @return void */ public static function render_slug_field() { printf( '%1$s/my-event/',
esc_html( home_url( '/' ) ),
esc_attr( Post_Type::SLUG_OPTION ),
esc_attr( (string) get_option( Post_Type::SLUG_OPTION, '' ) ),
esc_attr( Post_Type::REWRITE_SLUG )
);
printf(
'%2$s
', esc_attr( Post_Type::SLUG_OPTION ), esc_html__( 'Lowercase letters, numbers, and hyphens. When you change it, the old URLs redirect to the new ones.', 'eventwatch' ) ); if ( '' === (string) get_option( 'permalink_structure' ) ) { printf( '%s
', esc_html__( 'This has no effect while your permalinks use the Plain setting.', 'eventwatch' ) ); } } /** * 一覧のページの欄。 * * @return void */ public static function render_list_page_field() { wp_dropdown_pages( array( 'name' => esc_attr( self::LIST_PAGE ), 'id' => esc_attr( self::LIST_PAGE ), 'selected' => absint( get_option( self::LIST_PAGE, 0 ) ), 'show_option_none' => esc_html__( '— Not set —', 'eventwatch' ), 'option_none_value' => '0', ) ); printf( '%s
', esc_html__( 'The “Back to events” link on event pages goes here. The link is hidden until you choose a page.', 'eventwatch' ) ); } /** * Google Maps のキーの欄。 * * @return void */ public static function render_maps_key_field() { printf( '', esc_attr( self::MAPS_KEY ), esc_attr( (string) get_option( self::MAPS_KEY, '' ) ), esc_attr__( 'Not set', 'eventwatch' ) ); $lines = array( __( 'When set, a venue map is embedded on event detail pages. When empty, only map links are shown.', 'eventwatch' ), __( 'Embedding a map makes visitors’ browsers load the map from Google. The key appears in the page HTML, so restrict it to your site’s domain with an HTTP referrer restriction.', 'eventwatch' ), __( 'Create the key in Google Cloud (with the Maps Embed API enabled). As the site owner, you’re responsible for reviewing Google’s terms and pricing. This key is not sent to EventWatch.', 'eventwatch' ), ); foreach ( $lines as $line ) { printf( '%s
', esc_html( $line ) ); } printf( '', esc_url( self::MAPS_KEY_HELP_URL ), esc_html__( 'How to get an API key (Google)', 'eventwatch' ) ); } }