'; echo '

' . esc_html__( 'MenuGPS', 'menugps' ) . '

'; if ( ! MenuGPS_API::connected() ) { self::render_connect(); } else { self::render_connected(); } echo ''; } /** * Not connected yet: ONE decision, shown as two equal choices. * * Existing customers connect; newcomers create an account without leaving this page. The * signup form hides behind a
so nobody starts filling in the wrong path — the two * cards have to read as a fork in the road, not a form with a link above it. */ private static function render_connect() { if ( ! menugps_client_id() ) { echo '

' . esc_html__( 'This copy of the plugin has no MenuGPS client id configured, so it cannot connect. Please reinstall it from the plugin directory.', 'menugps' ) . '

'; return; } // A message this plugin put in the URL itself after a failed enrolment. Sanitised on read // and escaped on output, so even a hand-crafted URL can only produce plain text. // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only notice, no state change. $error = isset( $_GET['menugps-error'] ) ? sanitize_text_field( wp_unslash( $_GET['menugps-error'] ) ) : ''; if ( '' !== $error ) { echo '

' . esc_html( $error ) . '

'; } echo ''; echo ''; } /** One row of the enrolment form. */ private static function enroll_row( $label, $name, $value, $required, $type = 'text' ) { printf( '' . '', esc_attr( $name ), esc_html( $label ), esc_attr( $type ), esc_attr( $value ), $required ? ' required' : '' ); } /** * Create the account and the venue on MenuGPS, and keep the token it returns. * * No OAuth round trip: the platform issues a scoped token directly, so the restaurant is * connected the moment the form succeeds and never leaves wp-admin. That is the whole * acquisition trick — a signup that ends somewhere else loses most of the people who start it. */ public static function enroll() { check_admin_referer( 'menugps_enroll' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'menugps' ) ); } // phpcs:disable WordPress.Security.NonceVerification.Missing -- verified by check_admin_referer above. $body = array( 'venue_name' => sanitize_text_field( wp_unslash( $_POST['venue_name'] ?? '' ) ), 'name' => sanitize_text_field( wp_unslash( $_POST['name'] ?? '' ) ), 'email' => sanitize_email( wp_unslash( $_POST['email'] ?? '' ) ), 'phone' => sanitize_text_field( wp_unslash( $_POST['phone'] ?? '' ) ), 'city' => sanitize_text_field( wp_unslash( $_POST['city'] ?? '' ) ), 'site_url' => home_url(), 'locale' => substr( get_locale(), 0, 2 ), ); // phpcs:enable WordPress.Security.NonceVerification.Missing $response = wp_remote_post( menugps_base_url() . '/api/wordpress/enroll', array( 'timeout' => 20, 'headers' => array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', ), 'body' => wp_json_encode( $body ), ) ); $code = wp_remote_retrieve_response_code( $response ); $data = json_decode( wp_remote_retrieve_body( $response ), true ); if ( is_wp_error( $response ) || 201 !== $code || empty( $data['token'] ) ) { $message = is_array( $data ) && ! empty( $data['message'] ) ? $data['message'] : __( 'MenuGPS could not create the account — please try again.', 'menugps' ); wp_safe_redirect( admin_url( 'admin.php?page=menugps&menugps-error=' . rawurlencode( $message ) ) ); exit; } update_option( 'menugps_access_token', $data['token'], false ); update_option( 'menugps_venue', sanitize_title( $data['venue']['slug'] ?? '' ) ); delete_transient( 'menugps_me' ); wp_safe_redirect( admin_url( 'admin.php?page=menugps&connected=1' ) ); exit; } public static function pick_venue() { check_admin_referer( 'menugps_pick_venue' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to change this setting.', 'menugps' ) ); } // Sanitise BEFORE validating, not instead of: Plugin Check wants the // sanitize_*( wp_unslash( ... ) ) pair on every superglobal read. $slug = isset( $_POST['venue'] ) ? sanitize_title( wp_unslash( $_POST['venue'] ) ) : ''; update_option( 'menugps_venue', $slug ); wp_safe_redirect( admin_url( 'admin.php?page=menugps&picked=1' ) ); exit; } /** Connected: which venue, what to paste, and the way out. */ private static function render_connected() { $me = MenuGPS_API::me(); if ( is_wp_error( $me ) ) { echo '

' . esc_html( $me->get_error_message() ) . '

'; self::render_disconnect(); return; } $venues = (array) ( $me['venues'] ?? array() ); $selected = MenuGPS_API::venue(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only success notice. if ( isset( $_GET['connected'] ) ) { echo '

' . esc_html__( 'Connected to MenuGPS.', 'menugps' ) . '

'; } if ( ! $venues ) { echo '

' . esc_html__( 'This MenuGPS account does not have any venues yet.', 'menugps' ) . '

'; self::render_disconnect(); return; } echo ''; if ( $selected ) { self::render_usage(); } self::render_disconnect(); } /** What to paste, and where to go to change the menu itself. */ private static function render_usage() { $venue = MenuGPS_API::current_venue(); echo ''; } private static function render_disconnect() { echo ''; } }