OpenLeads: an administrator pastes an OpenLeads API key made for "Our website or web forms" * (OpenLeads > Settings > Apps & API > API keys). That key can only add leads and list forms. * - The "OpenLeads Form" block and the [openleads_form id="123"] shortcode show the form's OpenLeads embed page in a * frame that grows to fit the form (assets/openleads-forms.js listens for the height the form page sends). */ final class OpenLeads_Forms { const OPT_KEY = 'openleads_forms_key'; const OPT_ACCOUNT = 'openleads_forms_account'; const CACHE = 'openleads_forms_list'; const PAGE = 'openleads-forms'; public static function init() { add_action( 'admin_menu', array( __CLASS__, 'menu' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_assets' ) ); add_action( 'admin_post_openleads_forms_save', array( __CLASS__, 'save' ) ); add_action( 'admin_post_openleads_forms_disconnect', array( __CLASS__, 'disconnect' ) ); add_action( 'admin_post_openleads_forms_refresh', array( __CLASS__, 'refresh' ) ); add_action( 'init', array( __CLASS__, 'register' ) ); add_action( 'rest_api_init', array( __CLASS__, 'rest' ) ); add_shortcode( 'openleads_form', array( __CLASS__, 'shortcode' ) ); add_filter( 'plugin_action_links_' . plugin_basename( OPENLEADS_FORMS_FILE ), array( __CLASS__, 'action_links' ) ); } /* ------------------------------------------------------------------ OpenLeads API */ /** * Calls the OpenLeads API with the saved key (or the one given). Returns the decoded answer or a WP_Error. */ private static function api( $path, $key = null ) { $key = null === $key ? (string) get_option( self::OPT_KEY, '' ) : (string) $key; if ( '' === $key ) { return new WP_Error( 'openleads_no_key', __( 'Connect OpenLeads first under Settings > OpenLeads.', 'openleads-forms' ) ); } $res = wp_remote_get( OPENLEADS_FORMS_API . $path, array( 'timeout' => 15, 'headers' => array( 'X-API-Key' => $key, 'Accept' => 'application/json', ), 'user-agent' => 'OpenLeads-WordPress/' . OPENLEADS_FORMS_VERSION . '; ' . home_url( '/' ), ) ); if ( is_wp_error( $res ) ) { return new WP_Error( 'openleads_unreachable', __( 'OpenLeads could not be reached. Please try again in a minute.', 'openleads-forms' ) ); } $code = (int) wp_remote_retrieve_response_code( $res ); $data = json_decode( (string) wp_remote_retrieve_body( $res ), true ); if ( $code < 200 || $code >= 300 ) { /* translators: %d: the HTTP status code OpenLeads answered with */ $msg = ( is_array( $data ) && ! empty( $data['error']['message'] ) ) ? (string) $data['error']['message'] : sprintf( __( 'OpenLeads answered with an error (%d).', 'openleads-forms' ), $code ); return new WP_Error( 'openleads_error', $msg ); } return is_array( $data ) ? $data : array(); } /** The connected account: array( id, name, code ) or null. */ private static function account() { $a = get_option( self::OPT_ACCOUNT ); return ( is_array( $a ) && ! empty( $a['code'] ) ) ? $a : null; } /** The account's forms (cached for 10 minutes): list of array( id, name ), or a WP_Error. */ public static function forms( $fresh = false ) { if ( ! $fresh ) { $cached = get_transient( self::CACHE ); if ( is_array( $cached ) ) { return $cached; } } $data = self::api( 'forms/' ); if ( is_wp_error( $data ) ) { return $data; } $forms = array(); foreach ( $data as $f ) { if ( isset( $f['id'], $f['name'] ) ) { $forms[] = array( 'id' => (int) $f['id'], 'name' => sanitize_text_field( $f['name'] ), ); } } set_transient( self::CACHE, $forms, 10 * MINUTE_IN_SECONDS ); return $forms; } /* ------------------------------------------------------------------ Settings > OpenLeads */ public static function menu() { add_options_page( __( 'OpenLeads', 'openleads-forms' ), __( 'OpenLeads', 'openleads-forms' ), 'manage_options', self::PAGE, array( __CLASS__, 'page' ) ); } /** Settings > OpenLeads only: clicking a shortcode box selects it, ready to copy. */ public static function admin_assets( $hook ) { if ( 'settings_page_' . self::PAGE !== $hook ) { return; } wp_enqueue_script( 'openleads-forms-admin', plugins_url( 'assets/openleads-forms-admin.js', OPENLEADS_FORMS_FILE ), array(), OPENLEADS_FORMS_VERSION, true ); } public static function action_links( $links ) { array_unshift( $links, '' . esc_html__( 'Settings', 'openleads-forms' ) . '' ); return $links; } /** Back to Settings > OpenLeads; an error message is kept for this administrator for a minute (never put in the address). */ private static function fail( $msg ) { set_transient( 'openleads_forms_err_' . get_current_user_id(), (string) $msg, MINUTE_IN_SECONDS ); self::back( array( 'ol_error' => 1 ) ); } private static function back( $args = array() ) { wp_safe_redirect( add_query_arg( $args, admin_url( 'options-general.php?page=' . self::PAGE ) ) ); exit; } public static function save() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'openleads-forms' ) ); } check_admin_referer( 'openleads_forms_save' ); $key = isset( $_POST['openleads_key'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['openleads_key'] ) ) ) : ''; if ( ! preg_match( '/^olk_[0-9a-f]{40}$/', $key ) ) { self::fail( __( 'That does not look like an OpenLeads API key. It starts with olk_.', 'openleads-forms' ) ); } $me = self::api( 'me/', $key ); if ( is_wp_error( $me ) ) { self::fail( $me->get_error_message() ); } $code = isset( $me['account']['code'] ) ? strtolower( preg_replace( '/[^a-z0-9-]/i', '', (string) $me['account']['code'] ) ) : ''; if ( '' === $code ) { self::fail( __( 'OpenLeads did not say which account this key belongs to.', 'openleads-forms' ) ); } update_option( self::OPT_KEY, $key, false ); update_option( self::OPT_ACCOUNT, array( 'id' => isset( $me['account']['id'] ) ? (int) $me['account']['id'] : 0, 'name' => isset( $me['account']['name'] ) ? sanitize_text_field( (string) $me['account']['name'] ) : $code, 'code' => $code, ), false ); delete_transient( self::CACHE ); self::back( array( 'ol_saved' => 1 ) ); } public static function disconnect() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'openleads-forms' ) ); } check_admin_referer( 'openleads_forms_disconnect' ); delete_option( self::OPT_KEY ); delete_option( self::OPT_ACCOUNT ); delete_transient( self::CACHE ); self::back( array( 'ol_off' => 1 ) ); } public static function refresh() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'openleads-forms' ) ); } check_admin_referer( 'openleads_forms_refresh' ); delete_transient( self::CACHE ); self::back(); } public static function page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $acc = self::account(); $err = get_transient( 'openleads_forms_err_' . get_current_user_id() ); if ( false !== $err ) { delete_transient( 'openleads_forms_err_' . get_current_user_id() ); } // phpcs:disable WordPress.Security.NonceVerification.Recommended -- these only pick which notice to show. ?>

  1. Settings > Apps & API > API keys and click New API key.', 'openleads-forms' ) ); ?>
  2. What is it for? choose Our website or web forms. That key can only add leads, so it is safe on a website.', 'openleads-forms' ) ); ?>

' . esc_html( $acc['name'] ) . '' ); ?>

OpenLeads Form block and pick a form. Anywhere else, paste the shortcode below.', 'openleads-forms' ) ); ?>

get_error_message() ); ?>

Forms.', 'openleads-forms' ); ?>

">
array( __CLASS__, 'render_block' ) ) ); } /** The block editor's list of forms (people who can edit posts). */ public static function rest() { register_rest_route( 'openleads/v1', '/forms', array( 'methods' => 'GET', 'permission_callback' => function () { return current_user_can( 'edit_posts' ); }, 'callback' => function () { $acc = self::account(); if ( ! $acc ) { return array( 'connected' => false, 'settings' => current_user_can( 'manage_options' ) ? admin_url( 'options-general.php?page=' . self::PAGE ) : '', 'forms' => array(), ); } $forms = self::forms(); return array( 'connected' => true, 'account' => $acc['name'], 'error' => is_wp_error( $forms ) ? $forms->get_error_message() : '', 'forms' => is_wp_error( $forms ) ? array() : $forms, ); }, ) ); } public static function render_block( $attrs ) { $html = self::embed( isset( $attrs['formId'] ) ? (int) $attrs['formId'] : 0, isset( $attrs['height'] ) ? (int) $attrs['height'] : 0 ); if ( '' === $html ) { return ''; } /* the block's alignment (wide, full) and any extra CSS class set in the editor */ return '
' . $html . '
'; } public static function shortcode( $atts ) { $atts = shortcode_atts( array( 'id' => 0, 'height' => 0, ), $atts, 'openleads_form' ); return self::embed( (int) $atts['id'], (int) $atts['height'] ); } /** The frame that shows a form. $height is only the starting height: the frame grows or shrinks to fit the form. */ private static function embed( $id, $height ) { $acc = self::account(); if ( $id <= 0 || ! $acc ) { return current_user_can( 'edit_posts' ) ? '

' . esc_html( $acc ? __( 'Choose an OpenLeads form.', 'openleads-forms' ) : __( 'Connect OpenLeads under Settings > OpenLeads to show this form.', 'openleads-forms' ) ) . '

' : ''; } wp_enqueue_script( 'openleads-forms' ); $src = 'https://' . $acc['code'] . '.openleads.com/form/' . $id . '/embed/'; if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { /* the block editor's preview: OpenLeads does not count it as a view of the form */ $src .= '?preview=1'; } $height = $height > 0 ? min( 5000, $height ) : 600; return '
' . '' . '' . '
'; } } OpenLeads_Forms::init();