/**
  * Front-end display of widget.
  *
  * @see WP_Widget::widget()
  *
  * @param array $args	 Widget arguments.
  * @param array $instance Saved values from database.
  */
 public function widget($args, $instance)
 {
     $webform_id = $instance['select'];
     $variants_id = $instance['variants'];
     $style_id = $instance['style'];
     $center = $instance['center'];
     $center_margin = $instance['center_margin'];
     $version = $instance['version'];
     $web_forms = json_decode(get_option($this->GrOptionDbPrefix . 'web_forms'), true);
     if (isset($web_forms[$webform_id])) {
         $webform = $web_forms[$webform_id];
     } else {
         $api_key = get_option($this->GrOptionDbPrefix . 'api_key');
         if (!empty($api_key)) {
             $api = new GetResponseIntegration($api_key);
             $w = $version == 'old' ? $api->getWebForm($webform_id) : $api->getForm($webform_id);
             $webform['scriptUrl'] = $w->scriptUrl;
             $webform['status'] = $w->status;
             $webform_option = array();
             $webform_option[$instance['select']] = (array) $w;
             update_option($this->GrOptionDbPrefix . 'web_forms', json_encode($webform_option));
         }
     }
     // css styles Webform/Wordpress
     $css = $style_id == 1 && $version == 'old' ? '&css=1' : null;
     $variant = $variants_id >= 0 && $version == 'new' ? '&v=' . (int) $variants_id : null;
     if (!empty($webform) && isset($webform['scriptUrl']) && in_array($webform['status'], array('enabled', 'published'))) {
         $api_domain = get_option($this->GrOptionDbPrefix . 'api_domain');
         if (strlen($api_domain) > 0) {
             $webform['scriptUrl'] = 'https://' . $api_domain . '/' . $webform['scriptUrl'];
         }
         $style = $center == '1' ? 'style="margin-left: auto; margin-right: auto; width: ' . $center_margin . 'px;"' : '';
         $webform['scriptUrl'] = Gr_Integration::replaceHttpsToHttpIfSslOn($webform['scriptUrl']);
         $form = '<div ' . $style . '>';
         $form .= '<script type="text/javascript" src="' . htmlspecialchars($webform['scriptUrl'] . $css . $variant) . '"></script>';
         $form .= '</div>';
     }
     if (!empty($form)) {
         echo $args['before_widget'];
         echo __($form, 'text_domain');
         echo $args['after_widget'];
     }
 }
예제 #2
0
    /**
     * Back-end widget form.
     *
     * @see WP_Widget::form()
     *
     * @param array $instance Previously saved values from database.
     * @return string|void
     */
    public function form($instance)
    {
        $select = $instance ? esc_attr($instance['select']) : '';
        $style = $instance ? esc_attr($instance['style']) : '';
        $api_key = get_option($this->GrOptionDbPrefix . 'api_key');
        if (!empty($api_key)) {
            $api = new GetResponseIntegration($api_key);
            $campaigns = $api->getCampaigns();
            if (!empty($campaigns)) {
                $campaign_id = array();
                foreach ($campaigns as $cid => $campaign) {
                    $campaign_id[$cid] = $campaign->name;
                }
                $webforms = $api->getWebforms();
                $webforms = Gr_Integration::SortByKeyValue($webforms, 'name');
            }
        }
        ?>

		<?php 
        if ($api_key) {
            ?>
		<p>
			<?php 
            if (!empty($webforms) and false === (is_array($webforms) and isset($webforms['type']) and $webforms['type'] == 'error')) {
                ?>
			<label for="<?php 
                echo $this->get_field_id('select');
                ?>
"><?php 
                _e('Web Form:');
                ?>
</label>
			<select name="<?php 
                echo $this->get_field_name('select');
                ?>
" id="<?php 
                echo $this->get_field_id('select');
                ?>
" class="widefat">
				<?php 
                foreach ($webforms as $webform) {
                    echo '<option value="' . $webform->id . '" id="' . $webform->id . '"', $select == $webform->id ? ' selected="selected"' : '', '>', $webform->name . ' (' . $campaign_id[$webform->campaign] . ')', '</option>';
                }
                ?>
			</select>
			<?php 
            } else {
                _e('No Webforms', 'Gr_Integration');
            }
            ?>
		</p>
		<p>
			<input id="<?php 
            echo $this->get_field_id('style');
            ?>
" name="<?php 
            echo $this->get_field_name('style');
            ?>
" type="checkbox" value="1" <?php 
            checked('1', $style);
            ?>
 />
			<label for="<?php 
            echo $this->get_field_id('style');
            ?>
"><?php 
            _e('Use Wordpress CSS styles', 'Gr_Integration');
            ?>
</label>
		</p>
		<?php 
        } else {
            ?>
		<p><?php 
            _e('API key is not set.', 'Gr_Integration');
            ?>
</p>
		<?php 
        }
    }