'object', 'sanitize_callback' => 'dicegem_sanitize_settings', ) ); wp_add_privacy_policy_content( __( 'DiceGem Games', 'dicegem-games' ), wp_kses_post( wpautop( dicegem_privacy_text() ) ) ); } add_action( 'admin_init', 'dicegem_admin_init' ); /** * Suggested privacy policy text. * * @return string Plain text paragraphs. */ function dicegem_privacy_text() { return __( 'This site embeds games from DiceGem (dicegem.com). When you play an embedded game, it is loaded from play.dicegem.com, which receives your IP address and browser details like any website. The games send anonymous play events (game loaded, started, ended) together with this site\'s address and its publisher ID. No account, name or email is collected.', 'dicegem-games' ) . "\n\n" /* translators: 1: privacy policy URL, 2: terms of service URL. */ . sprintf( __( 'DiceGem privacy policy: %1$s. Terms of service: %2$s.', 'dicegem-games' ), 'https://dicegem.com/privacy-policy/', 'https://dicegem.com/terms-of-service/' ); } /** * Cleans submitted settings; an invalid publisher ID keeps the previous value, and so do the two lists the * settings form does not carry (they are edited on the Games tab). * * @param mixed $input Submitted values. * @return array Clean settings. */ function dicegem_sanitize_settings( $input ) { $input = is_array( $input ) ? $input : array(); $old = dicegem_settings(); $pub_id = isset( $input['pub_id'] ) ? sanitize_text_field( $input['pub_id'] ) : ''; if ( '' !== $pub_id && ! preg_match( '/^[A-Za-z0-9_-]{1,64}$/', $pub_id ) ) { add_settings_error( DICEGEM_OPTION, 'dicegem_pub_id', __( 'Publisher ID may only contain letters, numbers, "-" and "_" (up to 64). The previous value was kept.', 'dicegem-games' ) ); $pub_id = $old['pub_id']; } $ratio = isset( $input['ratio'] ) ? (string) $input['ratio'] : ''; $slug = isset( $input['pages_slug'] ) ? sanitize_title( $input['pages_slug'] ) : ''; $slugs = function ( $value ) { $value = is_array( $value ) ? $value : preg_split( '/[\s,]+/', (string) $value ); return array_values( array_unique( array_filter( array_map( 'sanitize_title', $value ) ) ) ); }; return array( 'pub_id' => $pub_id, 'credit' => ! empty( $input['credit'] ), 'casino' => ! empty( $input['casino'] ), 'ratio' => in_array( $ratio, DICEGEM_RATIOS, true ) ? $ratio : '16:10', 'click_to_play' => ! empty( $input['click_to_play'] ), 'pages' => ! empty( $input['pages'] ), 'pages_slug' => '' === $slug ? 'games' : $slug, 'pages_categories' => $slugs( isset( $input['pages_categories'] ) ? $input['pages_categories'] : $old['pages_categories'] ), 'pages_exclude' => $slugs( isset( $input['pages_exclude'] ) ? $input['pages_exclude'] : $old['pages_exclude'] ), 'covers' => ! empty( $input['covers'] ), ); } /** * Opens a settings row: the label and one line of help. The caller prints the control and closes the row. * * @param string $key Setting key. * @param string $label Label. * @param string $help One line under the label. * @param string $class Extra class of the row. */ function dicegem_row_open( $key, $label, $help, $class = '' ) { printf( '

%3$s

', esc_attr( $key ), esc_html( $label ), esc_html( $help ), esc_attr( $class ) ); } /** * A settings row with an on/off switch. * * @param string $key Setting key. * @param string $label Label. * @param string $help One line under the label. */ function dicegem_switch_row( $key, $label, $help ) { dicegem_row_open( $key, $label, $help ); printf( '
', esc_attr( $key ), esc_attr( DICEGEM_OPTION ), checked( dicegem_settings()[ $key ], true, false ) ); } /** * Address of the plugin's admin page. * * @param string $tab Tab to open. * @return string URL. */ function dicegem_admin_url( $tab = 'start' ) { return admin_url( 'admin.php?page=dicegem-games' . ( 'start' === $tab ? '' : '&tab=' . $tab ) ); } /** * Tabs of the admin page. * * @return string[] Tab key => label. */ function dicegem_admin_tabs() { return array( 'start' => __( 'Start', 'dicegem-games' ), 'games' => __( 'Games', 'dicegem-games' ), 'settings' => __( 'Settings', 'dicegem-games' ), ); } /** * Loads the admin style and script on the plugin's page only. * * @param string $hook Current admin page. */ function dicegem_admin_assets( $hook ) { if ( 'toplevel_page_dicegem-games' !== $hook ) { return; } $chosen = dicegem_settings()['pages_categories']; $categories = array(); foreach ( dicegem_categories() as $slug => $name ) { $categories[ $slug ] = ! $chosen || in_array( $slug, $chosen, true ); } wp_enqueue_style( 'dicegem-games-admin', DICEGEM_URL . 'assets/admin.css', array( 'dicegem-games' ), DICEGEM_VERSION ); wp_enqueue_script( 'dicegem-games-admin', DICEGEM_URL . 'assets/admin.js', array(), DICEGEM_VERSION, true ); wp_add_inline_script( 'dicegem-games-admin', 'window.dicegemAdmin = ' . wp_json_encode( array( 'url' => admin_url( 'admin-ajax.php' ), /* translators: 1: games done, 2: games in total. */ 'progress' => __( '%1$d of %2$d games', 'dicegem-games' ), /* translators: 1: pages created, 2: pages updated. */ 'done' => __( 'Done: %1$d pages created, %2$d updated.', 'dicegem-games' ), 'error' => __( 'The import stopped. Reload the page and try again; games already imported are kept.', 'dicegem-games' ), 'copied' => __( 'Copied', 'dicegem-games' ), 'failed' => __( 'That did not save. Reload the page and try again.', 'dicegem-games' ), /* translators: %d: number of games. */ 'shownOne' => __( '%d game', 'dicegem-games' ), /* translators: %d: number of games. */ 'shownMany' => __( '%d games', 'dicegem-games' ), 'nonce' => wp_create_nonce( 'dicegem_games' ), 'importNonce' => wp_create_nonce( 'dicegem_import' ), 'categories' => $categories, ) ) . ';', 'before' ); } add_action( 'admin_enqueue_scripts', 'dicegem_admin_assets' ); /** * Adds the DiceGem Games menu. Priority 9: the game pages list (the post type's own entry) lands right under Start. */ function dicegem_admin_menu() { add_menu_page( __( 'DiceGem Games', 'dicegem-games' ), __( 'DiceGem Games', 'dicegem-games' ), 'manage_options', 'dicegem-games', 'dicegem_admin_page', 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCI+PHBhdGggZmlsbD0iI2E3YWFhZCIgZmlsbC1ydWxlPSJldmVub2RkIiBkPSJNMTIgMS41IDIyLjUgOSAxMiAyMi41IDEuNSA5Wm0wIDYuNmEyLjQgMi40IDAgMSAwIDAgNC44IDIuNCAyLjQgMCAwIDAgMC00LjhaIi8+PC9zdmc+', 58 ); add_submenu_page( 'dicegem-games', __( 'DiceGem Games', 'dicegem-games' ), __( 'Start', 'dicegem-games' ), 'manage_options', 'dicegem-games', 'dicegem_admin_page' ); } add_action( 'admin_menu', 'dicegem_admin_menu', 9 ); /** * Adds the game categories screen under the menu, after the game pages list. */ function dicegem_admin_menu_categories() { if ( dicegem_pages_enabled() ) { add_submenu_page( 'dicegem-games', __( 'Game categories', 'dicegem-games' ), __( 'Game categories', 'dicegem-games' ), 'manage_categories', 'edit-tags.php?taxonomy=' . DICEGEM_TAXONOMY . '&post_type=' . DICEGEM_POST_TYPE ); } } add_action( 'admin_menu', 'dicegem_admin_menu_categories', 11 ); /** * Keeps the DiceGem Games menu open on the game categories screen. * * @param string $file Parent menu file. * @return string */ function dicegem_parent_file( $file ) { $screen = get_current_screen(); return $screen && DICEGEM_TAXONOMY === $screen->taxonomy ? 'dicegem-games' : $file; } add_filter( 'parent_file', 'dicegem_parent_file' ); /** * Opens the Start tab once after the plugin is activated on its own. */ function dicegem_activation_redirect() { if ( ! get_option( DICEGEM_ACTIVATED ) ) { return; } delete_option( DICEGEM_ACTIVATED ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only tells a single activation from a bulk one. if ( wp_doing_ajax() || is_network_admin() || isset( $_GET['activate-multi'] ) || ! current_user_can( 'manage_options' ) ) { return; } wp_safe_redirect( dicegem_admin_url() ); exit; } add_action( 'admin_init', 'dicegem_activation_redirect' ); /** * One line about the catalog copy in use. * * @return string Plain text. */ function dicegem_catalog_status() { $backup = dicegem_backup(); if ( ! $backup['time'] ) { return __( 'The catalog has not been downloaded yet.', 'dicegem-games' ); } $count = count( $backup['games'] ); /* translators: 1: number of games, 2: date and time. */ return sprintf( _n( '%1$d game, last updated %2$s.', '%1$d games, last updated %2$s.', $count, 'dicegem-games' ), $count, wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $backup['time'] ) ); } /** * Admin page: header, tabs and the current tab. */ function dicegem_admin_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only choice of tab. $current = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'start'; $tabs = dicegem_admin_tabs(); if ( ! isset( $tabs[ $current ] ) ) { $current = 'start'; } ?>


'; dicegem_connect_panel(); echo '
'; } elseif ( 'games' === $current ) { dicegem_tab_games(); } else { dicegem_tab_start(); } ?>

' . esc_html( DICEGEM_CATALOG_URL ) . '' ); ?>

|

'chess', 'category_slug' => 'board-games', ); $codes = array( __( 'One game', 'dicegem-games' ) => sprintf( '[dicegem game="%s"]', $example['slug'] ), __( 'A grid of games', 'dicegem-games' ) => sprintf( '[dicegem_grid category="%s" count="6"]', $example['category_slug'] ), ); $enabled = dicegem_pages_enabled(); $stats = dicegem_page_stats(); $format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); $section = home_url( user_trailingslashit( dicegem_settings()['pages_slug'] ) ); $rows = array( __( 'Published pages', 'dicegem-games' ) => number_format_i18n( $stats['published'] ), __( 'Draft pages', 'dicegem-games' ) => number_format_i18n( $stats['drafts'] ), __( 'Covers downloaded', 'dicegem-games' ) => number_format_i18n( $stats['covers'] ) . ( $stats['errors'] ? ' ' . sprintf( /* translators: %s: list of game slugs. */ __( '(failed: %s)', 'dicegem-games' ), implode( ', ', array_slice( $stats['errors'], 0, 10 ) ) ) : '' ), __( 'Last sync', 'dicegem-games' ) => $stats['synced'] ? wp_date( $format, $stats['synced'] ) : __( 'Never', 'dicegem-games' ), __( 'Next daily sync', 'dicegem-games' ) => $stats['next'] ? wp_date( $format, $stats['next'] ) : __( 'Not scheduled', 'dicegem-games' ), ); $update = __( 'Update game pages', 'dicegem-games' ); $index = 0; ?>

$code ) : ?>

' . esc_html( $section ) . '' ); ?>

> $value ) : ?>

>

$name ) : ?>

/

">
new value. */ function dicegem_update_settings( array $changes ) { update_option( DICEGEM_OPTION, $changes + dicegem_settings() ); } /** * Turns the game pages feature on (the "Create the games section" button). */ function dicegem_enable_pages() { dicegem_update_settings( array( 'pages' => true ) ); } /** * Checks a Games tab request and returns the slug it names. * * @param string $field Request field holding a game or category slug. * @return string Slug. */ function dicegem_ajax_games_slug( $field ) { check_ajax_referer( 'dicegem_games' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( __( 'You are not allowed to do this.', 'dicegem-games' ), 403 ); } if ( ! dicegem_pages_enabled() ) { wp_send_json_error( __( 'Game pages are turned off.', 'dicegem-games' ), 400 ); } return isset( $_POST[ $field ] ) ? sanitize_title( wp_unslash( $_POST[ $field ] ) ) : ''; } /** * Ajax: gives one game a page or takes it away, at once. */ function dicegem_ajax_game_page() { $slug = dicegem_ajax_games_slug( 'slug' ); $games = dicegem_games(); if ( ! isset( $games[ $slug ] ) ) { wp_send_json_error( __( 'Unknown game.', 'dicegem-games' ), 400 ); } // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified in dicegem_ajax_games_slug(). $exclude = array_merge( array_diff( dicegem_settings()['pages_exclude'], array( $slug ) ), empty( $_POST['on'] ) ? array( $slug ) : array() ); dicegem_update_settings( array( 'pages_exclude' => $exclude ) ); $allowed = dicegem_page_games(); $map = dicegem_page_map(); if ( isset( $allowed[ $slug ] ) ) { dicegem_import_game( $allowed[ $slug ], $map ); update_option( DICEGEM_PAGES, $map ); } elseif ( isset( $map[ $slug ] ) ) { dicegem_retire_pages( array( $slug => $map[ $slug ] ), $allowed ); } wp_send_json_success( array( 'on' => isset( $allowed[ $slug ] ) ) ); } add_action( 'wp_ajax_dicegem_game_page', 'dicegem_ajax_game_page' ); /** * Ajax: turns game pages on or off for a whole category; the import that follows creates or drafts the pages. */ function dicegem_ajax_category_pages() { $category = dicegem_ajax_games_slug( 'category' ); $all = array_keys( dicegem_categories() ); if ( ! in_array( $category, $all, true ) ) { wp_send_json_error( __( 'Unknown category.', 'dicegem-games' ), 400 ); } $chosen = dicegem_settings()['pages_categories']; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified in dicegem_ajax_games_slug(). $chosen = array_merge( array_diff( $chosen ? $chosen : $all, array( $category ) ), empty( $_POST['on'] ) ? array() : array( $category ) ); $chosen = array_values( array_intersect( $all, $chosen ) ); if ( ! $chosen ) { wp_send_json_error( __( 'At least one category keeps its pages. To remove every page, use "Delete game pages" in Settings.', 'dicegem-games' ), 400 ); } // An empty list means every category, new ones included. dicegem_update_settings( array( 'pages_categories' => count( $chosen ) === count( $all ) ? array() : $chosen ) ); wp_send_json_success(); } add_action( 'wp_ajax_dicegem_category_pages', 'dicegem_ajax_category_pages' ); /** * Handles "Delete game pages". */ function dicegem_handle_delete_pages() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'dicegem-games' ), 403 ); } check_admin_referer( 'dicegem_delete_pages' ); $deleted = dicegem_delete_pages( ! empty( $_POST['keep_edited'] ) ); /* translators: %d: number of pages. */ dicegem_admin_redirect( 'settings', 'dicegem_delete', sprintf( _n( '%d game page deleted.', '%d game pages deleted.', $deleted, 'dicegem-games' ), $deleted ) ); } add_action( 'admin_post_dicegem_delete_pages', 'dicegem_handle_delete_pages' ); /** * Handles "Restore from the trash". */ function dicegem_handle_restore_pages() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'dicegem-games' ), 403 ); } check_admin_referer( 'dicegem_restore_pages' ); $restored = dicegem_restore_pages(); /* translators: %d: number of pages. */ dicegem_admin_redirect( 'start', 'dicegem_restore', sprintf( _n( '%d game page restored.', '%d game pages restored.', $restored, 'dicegem-games' ), $restored ) ); } add_action( 'admin_post_dicegem_restore_pages', 'dicegem_handle_restore_pages' ); /** * Ajax: imports one batch for the progress bar on the Start tab; the first one may turn the feature on. */ function dicegem_ajax_import() { check_ajax_referer( 'dicegem_import' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( __( 'You are not allowed to do this.', 'dicegem-games' ), 403 ); } if ( ! dicegem_pages_enabled() ) { if ( empty( $_POST['enable'] ) ) { wp_send_json_error( __( 'Game pages are turned off.', 'dicegem-games' ), 400 ); } dicegem_enable_pages(); } wp_send_json_success( dicegem_import_batch( isset( $_POST['offset'] ) ? absint( $_POST['offset'] ) : 0 ) ); } add_action( 'wp_ajax_dicegem_import', 'dicegem_ajax_import' ); /** * Without JavaScript the button starts the import in the background (WP-Cron). */ function dicegem_handle_import() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'dicegem-games' ), 403 ); } check_admin_referer( 'dicegem_import' ); if ( ! dicegem_pages_enabled() ) { dicegem_enable_pages(); } wp_schedule_single_event( time() - 1, DICEGEM_CRON, array( 0 ) ); spawn_cron(); dicegem_admin_redirect( 'start', 'dicegem_import', __( 'The import runs in the background. Reload this page in a few minutes to see the result.', 'dicegem-games' ) ); } add_action( 'admin_post_dicegem_import', 'dicegem_handle_import' ); /** * Handles "Download the game catalog" (the administrator's agreement, recorded here) and "Refresh catalog now". */ function dicegem_handle_refresh() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'dicegem-games' ), 403 ); } check_admin_referer( 'dicegem_refresh' ); $tab = dicegem_connected() ? 'settings' : 'start'; update_option( DICEGEM_CONNECTED, 1 ); $games = dicegem_refresh_catalog(); if ( null === $games ) { dicegem_admin_redirect( $tab, 'dicegem_refresh', __( 'The catalog could not be downloaded; the last good copy is still in use.', 'dicegem-games' ), 'error' ); } /* translators: %d: number of games. */ dicegem_admin_redirect( $tab, 'dicegem_refresh', sprintf( _n( 'Catalog refreshed: %d game.', 'Catalog refreshed: %d games.', count( $games ), 'dicegem-games' ), count( $games ) ) ); } add_action( 'admin_post_dicegem_refresh', 'dicegem_handle_refresh' ); /** * Adds a Settings link on the Plugins screen. * * @param string[] $links Action links. * @return string[] */ function dicegem_action_links( $links ) { array_unshift( $links, '' . esc_html__( 'Settings', 'dicegem-games' ) . '' ); return $links; } add_filter( 'plugin_action_links_' . plugin_basename( DICEGEM_FILE ), 'dicegem_action_links' );