/**
  * Method to receive and store submitted options when posted.
  */
 public function on_post()
 {
     if (isset($_POST[JanrainCapture::$name . '_action'])) {
         foreach ($this->fields as $field) {
             if (isset($_POST[$field['name']])) {
                 $value = $_POST[$field['name']];
                 if ($field['name'] == JanrainCapture::$name . '_address' || $field['name'] == JanrainCapture::$name . '_sso_address') {
                     $value = preg_replace('/^https?\\:\\/\\//i', '', $value);
                 }
                 JanrainCapture::update_option($field['name'], $value);
             } else {
                 if ($field['type'] == 'checkbox' && $field['screen'] == $_POST[JanrainCapture::$name . '_action']) {
                     $value = '0';
                     JanrainCapture::update_option($field['name'], $value);
                 } else {
                     if (JanrainCapture::get_option($field['name']) === false && isset($field['default']) && (!is_multisite() || is_main_site())) {
                         JanrainCapture::update_option($field['name'], $field['default']);
                     }
                 }
             }
         }
         $this->postMessage = array('class' => 'updated', 'message' => 'Configuration Saved');
     }
 }
 /**
  * Retrieves the plugin version.
  *
  * @return string
  *	 String version
  */
 static function share_enabled()
 {
     $enabled = self::get_option(self::$name . '_ui_share_enabled');
     if ($enabled == '0') {
         return false;
     }
     $realm = self::get_option(self::$name . '_rpx_realm');
     $share_providers = JanrainCapture::get_option(JanrainCapture::$name . '_rpx_share_providers');
     $share_providers = implode("', '", array_map('esc_js', (array) $share_providers));
     return $realm && "['{$share_providers}']";
 }
    /**
     * Outputs backplane setttings js block
     */
    function backplane_js()
    {
        $bus = JanrainCapture::get_option(JanrainCapture::$name . '_bp_bus_name');
        $ver = JanrainCapture::get_option(JanrainCapture::$name . '_bp_version', 1.2);
        if ($ver == 1.2) {
            echo <<<BACKPLANE
<script type="text/javascript">
 function setup_bp() {
\t/*
\t * Initialize Backplane:
\t * This creates a channel and adds a cookie for the channel.
\t * It also sets the function to call when this is complete.
\t */
\tBackplane(bp_ready);
\tBackplane.init({
\t\tserverBaseURL: "http://backplane1.janrainbackplane.com/v1.2",
\t\tbusName: "{$bus}"
\t});
}

function bp_ready() {
\t/*
\t * This function is called when Backplane.init is complete.
\t */
\tif (Backplane.getChannelID() != undefined) {
\t\t// backplane loaded
\t\t//console.log(Backplane.getChannelID());
\t\treturn false;
\t}
}

setup_bp();
</script>
BACKPLANE;
        }
    }
 /**
  * Implementation of the janrain_capture shortcode.
  *
  * @param string $args
  *   Arguments appended to the shortcode
  *
  * @return string
  *   Text or HTML to render in place of the shortcode
  */
 function shortcode($args)
 {
     extract(shortcode_atts(array('type' => 'modal', 'text' => 'Sign in / Register', 'action' => 'signin', 'height' => '400', 'width' => '700', 'href_only' => 'false', 'callback' => 'CAPTURE.closeProfile'), $args));
     $class = 'capture-anon';
     $capture_addr = self::get_option(self::$name . '_ui_address') ? self::get_option(self::$name . '_ui_address') : self::get_option(self::$name . '_address');
     if (strpos($action, 'profile') === 0) {
         $uargs = array('action' => self::$name . '_profile', 'callback' => self::sanitize($callback));
         if (strlen($action) > 7) {
             $method = substr($action, 7);
             $uargs['method'] = self::sanitize($method);
         }
         $link = admin_url('admin-ajax.php', '') . '?' . http_build_query($uargs, '', '&');
         $class = 'capture-auth';
     } else {
         if (strpos($action, 'signin') === 0) {
             $action .= JanrainCapture::get_option(JanrainCapture::$name . '_signin_ext');
         } elseif (strpos($action, 'edit_profile') === 0) {
             $display = $this->widget_profile();
             return $display;
         }
         $link = 'https://' . $capture_addr . '/oauth/' . $action;
         $args = array('response_type' => 'code', 'redirect_uri' => admin_url('admin-ajax.php', '') . '?action=' . self::$name . '_redirect_uri', 'client_id' => self::get_option(self::$name . '_client_id'), 'xd_receiver' => admin_url('admin-ajax.php', '') . '?action=' . self::$name . '_xdcomm', 'recover_password_callback' => 'CAPTURE.closeRecoverPassword');
         $link = $link . '?' . http_build_query($args, '', '&');
         if (JanrainCapture::get_option(JanrainCapture::$name . '_ui_type') == 'Capture') {
             if (!is_user_logged_in()) {
                 $link = '<a href="javascript:janrain.capture.ui.modal.open();" >' . $text . '</a>';
             } else {
                 $link = '<a href="' . wp_logout_url(JanrainCaptureUi::current_page_url()) . '" onclick="janrain.capture.ui.endCaptureSession();" >Log out</a>';
             }
             return $link;
         }
     }
     if ($href_only == 'true') {
         return esc_url($link);
     }
     if ($type == 'inline') {
         $iframe = '<iframe src="' . esc_url($link) . '" style="width:' . (int) $width . 'px;height:' . (int) $height . 'px;" class="' . self::$name . '_iframe ' . $class . ' ' . self::$name . '_' . self::sanitize($action) . '"></iframe>';
         return $iframe;
     } else {
         $anchor = '<a href="' . esc_url($link) . '" rel="width:' . (int) $width . 'px;height:' . (int) $height . 'px;" class="' . self::$name . '_anchor modal-link ' . $class . ' ' . self::$name . '_' . self::sanitize($action) . '">' . $text . '</a>';
         return $anchor;
     }
 }
 function edit_screen_js()
 {
     $url = $this->ifolder . '/';
     $file = JanrainCapture::get_option(JanrainCapture::$name . '_widget_edit_screen');
     $url .= preg_replace('"\\.(php|html|htm)$"', '.js', $file);
     echo '<script type="text/javascript">';
     if ($this->local) {
         if (file_exists($url)) {
             include_once $url;
         } else {
             if (true === WP_DEBUG) {
                 error_log('janrain-capture: Could not find screen file at path ' . $url);
             }
         }
     } else {
         $resp = wp_remote_get($url);
         $out = wp_remote_retrieve_body($resp);
         echo $out ?: sprintf('Janrain: Unable to load %s', $url);
     }
     echo '</script>';
 }
 /**
  * Method to receive and store submitted options when posted.
  */
 public function on_post()
 {
     if (isset($_POST[JanrainCapture::$name . '_action']) && current_user_can('manage_options') && check_admin_referer(JanrainCapture::$name . '_action')) {
         foreach ($this->fields as $field) {
             if (isset($_POST[$field['name']])) {
                 $value = $_POST[$field['name']];
                 if (is_string($value)) {
                     $value = sanitize_text_field($value);
                 } elseif (is_array($value)) {
                     foreach ($value as $k => $v) {
                         if (is_string($v)) {
                             $value[$k] = sanitize_text_field($v);
                         }
                     }
                 }
                 JanrainCapture::update_option($field['name'], $value);
             } else {
                 if ($field['type'] == 'checkbox' && $field['screen'] == $_POST[JanrainCapture::$name . '_action']) {
                     $value = '0';
                     JanrainCapture::update_option($field['name'], $value);
                 } else {
                     if (JanrainCapture::get_option($field['name']) === false && isset($field['default'])) {
                         JanrainCapture::update_option($field['name'], $field['default']);
                     }
                 }
             }
         }
     }
 }
 /**
  * Fetches the Engage share providers and realm name
  *
  * @return boolean
  *   Boolean success
  */
 public function rpx_lookup_rp()
 {
     $key = JanrainCapture::get_option(JanrainCapture::$name . '_rpx_api_key');
     if (!$key) {
         return false;
     }
     $url = 'https://rpxnow.com/plugin/lookup_rp';
     $headers = array('Accept-encoding' => 'identity');
     $arg_array = array('format' => 'json', 'apiKey' => $key, 'pluginName' => 'wordpress-capture', 'pluginVersion' => JanrainCapture::get_plugin_version());
     $result = wp_remote_post($url, array('method' => 'POST', 'body' => $arg_array, 'headers' => $headers));
     if (is_wp_error($result) || !isset($result['body'])) {
         return false;
     }
     $json_data = json_decode($result['body'], true);
     return $json_data;
 }