$args Query arguments. */ public static function url( array $args = array() ): string { return add_query_arg( $args, admin_url( 'admin.php?page=' . Connection::CALLBACK_PAGE ) ); } public static function handle_callback(): void { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- the OAuth state replaces the nonce. if ( ! isset( $_GET['page'], $_GET['state'] ) || Connection::CALLBACK_PAGE !== $_GET['page'] || ! current_user_can( self::CAPABILITY ) ) { return; } $state = is_string( $_GET['state'] ) ? sanitize_text_field( wp_unslash( $_GET['state'] ) ) : ''; if ( isset( $_GET['error'] ) ) { delete_transient( Connection::STATE_PREFIX . $state ); self::notice( 'warning', __( 'The connection was cancelled.', 'awesomeforms' ) ); } else { $code = isset( $_GET['code'] ) && is_string( $_GET['code'] ) ? sanitize_text_field( wp_unslash( $_GET['code'] ) ) : ''; $result = Connection::complete( $state, $code ); delete_transient( 'awesomeforms_account' ); self::notice_result( $result, __( 'This site is now connected to AwesomeFor.ms.', 'awesomeforms' ) ); } // phpcs:enable wp_safe_redirect( self::url() ); exit; } public static function action_connect(): void { self::authorize(); check_admin_referer( 'awesomeforms_connect' ); $url = Connection::authorization_url(); if ( is_wp_error( $url ) ) { self::notice( 'error', $url->get_error_message() ); wp_safe_redirect( self::url() ); exit; } wp_redirect( $url ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- the service lives on another host. exit; } public static function action_disconnect(): void { self::authorize(); check_admin_referer( 'awesomeforms_disconnect' ); Connection::forget(); delete_transient( 'awesomeforms_account' ); self::notice( 'success', __( 'This site is disconnected. Forms already in your pages keep working.', 'awesomeforms' ) ); wp_safe_redirect( self::url() ); exit; } public static function action_token(): void { self::authorize(); check_admin_referer( 'awesomeforms_token' ); $token = isset( $_POST['awesomeforms_token'] ) && is_string( $_POST['awesomeforms_token'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['awesomeforms_token'] ) ) ) : ''; if ( 0 !== strpos( $token, 'afm_' ) ) { self::notice( 'error', __( 'This is not an AwesomeFor.ms access token: it starts with afm_.', 'awesomeforms' ) ); } else { Connection::use_personal_token( $token ); delete_transient( 'awesomeforms_account' ); $account = Api_Client::account(); self::notice_result( is_wp_error( $account ) ? $account : true, __( 'This site is now connected to AwesomeFor.ms.', 'awesomeforms' ) ); if ( is_wp_error( $account ) ) { Connection::forget(); } } wp_safe_redirect( self::url() ); exit; } public static function action_create(): void { self::authorize(); check_admin_referer( 'awesomeforms_create' ); $title = isset( $_POST['awesomeforms_title'] ) && is_string( $_POST['awesomeforms_title'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['awesomeforms_title'] ) ) ) : ''; $form = Api_Client::request( 'POST', 'forms', array( 'title' => '' !== $title ? $title : __( 'New form', 'awesomeforms' ), 'pages' => array( array( 'title' => '', 'fields' => array( array( 'type' => 'text', 'label' => __( 'Name', 'awesomeforms' ), 'required' => true, ), ), ), ), ) ); if ( is_wp_error( $form ) ) { self::notice( 'error', $form->get_error_message() ); wp_safe_redirect( self::url() ); exit; } wp_redirect( Connection::public_url( Cast::string( $form['edit_url'] ?? '' ) ) ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- the editor lives on the service. exit; } public static function action_publish(): void { self::change_status( 'publish', __( 'The form is published.', 'awesomeforms' ) ); } public static function action_unpublish(): void { self::change_status( 'unpublish', __( 'The form is offline. Its responses are kept.', 'awesomeforms' ) ); } private static function change_status( string $action, string $success ): void { self::authorize(); check_admin_referer( 'awesomeforms_' . $action ); $id = isset( $_POST['awesomeforms_form'] ) && is_string( $_POST['awesomeforms_form'] ) ? absint( sanitize_text_field( wp_unslash( $_POST['awesomeforms_form'] ) ) ) : 0; $result = Api_Client::request( 'POST', 'forms/' . $id . '/' . $action ); self::notice_result( is_wp_error( $result ) ? $result : true, $success ); wp_safe_redirect( self::url() ); exit; } private static function authorize(): void { if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'awesomeforms' ), 403 ); } } /** * @param true|\WP_Error $result Outcome of the action. */ private static function notice_result( $result, string $success ): void { if ( is_wp_error( $result ) ) { self::notice( 'error', $result->get_error_message() ); } else { self::notice( 'success', $success ); } } public static function notice( string $type, string $message ): void { set_transient( self::NOTICE_KEY . get_current_user_id(), array( $type, $message ), MINUTE_IN_SECONDS ); } public static function flash(): void { $notice = get_transient( self::NOTICE_KEY . get_current_user_id() ); delete_transient( self::NOTICE_KEY . get_current_user_id() ); if ( is_array( $notice ) ) { printf( '

%2$s

', esc_attr( Cast::string( $notice[0] ?? '' ) ), esc_html( Cast::string( $notice[1] ?? '' ) ) ); } } /** * @param array $fields Hidden fields. */ public static function form_button( string $action, string $label, array $fields = array(), string $css_class = 'button' ): void { echo '
'; echo ''; wp_nonce_field( 'awesomeforms_' . $action ); foreach ( $fields as $name => $value ) { echo ''; } echo ''; echo '
'; } public static function render(): void { echo '
'; echo '

AwesomeFor.ms

'; self::flash(); if ( ! Connection::is_connected() ) { self::render_connect(); echo '
'; return; } $account = Api_Client::account(); if ( is_wp_error( $account ) ) { echo '

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

'; if ( ! Connection::is_connected() ) { self::render_connect(); } echo ''; return; } echo '
'; printf( /* translators: 1: account email, 2: plan name. */ '

' . esc_html__( 'Connected as %1$s (%2$s plan).', 'awesomeforms' ) . '

', '' . esc_html( Cast::string( $account['email'] ?? '' ) ) . '', esc_html( Cast::string( Cast::map( $account['plan'] ?? array() )['label'] ?? '' ) ) ); self::form_button( 'disconnect', __( 'Disconnect', 'awesomeforms' ) ); echo '
'; do_action( 'awesomeforms_admin_sections', $account ); self::render_forms(); echo ''; } private static function render_connect(): void { echo '
'; echo '

' . esc_html__( 'Connect this site', 'awesomeforms' ) . '

'; echo '

' . esc_html__( 'Sign in to your AwesomeFor.ms account and allow this site to list, create and publish your forms. No password is stored here.', 'awesomeforms' ) . '

'; self::form_button( 'connect', __( 'Connect to AwesomeFor.ms', 'awesomeforms' ), array(), 'button button-primary' ); echo '
' . esc_html__( 'Connect with an access token', 'awesomeforms' ) . ''; echo '

' . esc_html__( 'If the connection does not come back to this site, create an access token in your AwesomeFor.ms account settings and paste it here.', 'awesomeforms' ) . '

'; echo '
'; echo ''; wp_nonce_field( 'awesomeforms_token' ); echo ''; echo ''; echo ' '; echo '
'; echo '
'; } private static function render_forms(): void { $forms = Api_Client::forms(); echo '

' . esc_html__( 'Your forms', 'awesomeforms' ) . '

'; echo '
'; echo ''; wp_nonce_field( 'awesomeforms_create' ); echo ' '; echo ' '; echo ''; echo '
'; if ( is_wp_error( $forms ) ) { echo '

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

'; return; } if ( array() === $forms ) { echo '

' . esc_html__( 'No form yet.', 'awesomeforms' ) . '

'; return; } echo ''; foreach ( array( __( 'Title', 'awesomeforms' ), __( 'Status', 'awesomeforms' ), __( 'Responses', 'awesomeforms' ), __( 'Shortcode', 'awesomeforms' ), __( 'Actions', 'awesomeforms' ) ) as $heading ) { echo ''; } echo ''; foreach ( $forms as $form ) { $published = 'published' === ( $form['status'] ?? '' ); $shortcode = '[' . Embed::SHORTCODE . ' id="' . Cast::string( $form['uuid'] ?? '' ) . '"]'; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; } echo '
' . esc_html( $heading ) . '
' . esc_html( Cast::string( $form['title'] ?? '' ) ) . '' . ( $published ? esc_html__( 'Published', 'awesomeforms' ) : esc_html__( 'Draft', 'awesomeforms' ) ) . '' . esc_html( number_format_i18n( Cast::int( $form['responses'] ?? 0 ) ) ) . '' . esc_html( $shortcode ) . ' '; echo '' . esc_html__( 'Edit', 'awesomeforms' ) . ' '; self::form_button( $published ? 'unpublish' : 'publish', $published ? __( 'Unpublish', 'awesomeforms' ) : __( 'Publish', 'awesomeforms' ), array( 'awesomeforms_form' => Cast::string( $form['id'] ?? '' ) ) ); do_action( 'awesomeforms_form_row_actions', $form ); echo '
'; } }