| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
class fmcWidget extends WP_Widget { |
| 5 |
|
| 6 |
function shortcode_form() { |
| 7 |
global $fmc_widgets; |
| 8 |
|
| 9 |
$widget_info = $fmc_widgets[ get_class($this) ]; |
| 10 |
|
| 11 |
$settings_content = $this->settings_form( array('_instance_type' => 'shortcode') ); |
| 12 |
$settings_content = $settings_content; |
| 13 |
|
| 14 |
$response = array( |
| 15 |
'title' => $widget_info['title'] .' widget', |
| 16 |
'body' => flexmlsConnect::shortcode_header() . $settings_content . flexmlsConnect::shortcode_footer() |
| 17 |
); |
| 18 |
|
| 19 |
$js = new Moxiecode_JSON(); |
| 20 |
echo str_replace("\'", "'", $js->encode($response)); |
| 21 |
exit; |
| 22 |
|
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
function cache_jelly($args, $instance, $type) { |
| 27 |
global $fmc_widgets; |
| 28 |
|
| 29 |
$widget_info = $fmc_widgets[ get_class($this) ]; |
| 30 |
|
| 31 |
$cache_item_name = md5(get_class($this) .'_'. serialize($instance) . $type); |
| 32 |
$cache = get_transient('fmc_cache_'. $cache_item_name); |
| 33 |
|
| 34 |
if (!empty($cache) && flexmlsConnect::cache_turned_on() == true) { |
| 35 |
$return = $cache; |
| 36 |
} |
| 37 |
else { |
| 38 |
$return = $this->jelly($args, $instance, $type); |
| 39 |
$cache_set_result = set_transient('fmc_cache_'. $cache_item_name, $return, $widget_info['max_cache_time']); |
| 40 |
|
| 41 |
// update transient item which tracks cache items |
| 42 |
$cache_tracker = get_transient('fmc_cache_tracker'); |
| 43 |
$cache_tracker[ $cache_item_name ] = true; |
| 44 |
set_transient('fmc_cache_tracker', $cache_tracker, 60*60*24*7); |
| 45 |
} |
| 46 |
|
| 47 |
return $return; |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
function form($instance) { |
| 53 |
echo $this->settings_form($instance); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
function shortcode_generate() { |
| 58 |
global $fmc_widgets; |
| 59 |
|
| 60 |
$widget_info = $fmc_widgets[ get_class($this) ]; |
| 61 |
|
| 62 |
$shortcode = "[{$widget_info['shortcode']}"; |
| 63 |
|
| 64 |
foreach ($_REQUEST as $k => $v) { |
| 65 |
if ($k == "action") { |
| 66 |
continue; |
| 67 |
} |
| 68 |
if (!empty($v)) { |
| 69 |
$v = htmlentities(stripslashes($v), ENT_QUOTES); |
| 70 |
$shortcode .= " {$k}=\"{$v}\""; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
$shortcode .= "]"; |
| 75 |
|
| 76 |
$response = array( |
| 77 |
'body' => $shortcode |
| 78 |
); |
| 79 |
|
| 80 |
$js = new Moxiecode_JSON(); |
| 81 |
echo $js->encode($response); |
| 82 |
exit; |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
function get_field_id($val) { |
| 88 |
$widget = $this->is_called_for_widget(); |
| 89 |
if ($widget) { |
| 90 |
return parent::get_field_id($val); |
| 91 |
} |
| 92 |
else { |
| 93 |
return "fmc_shortcode_field_{$val}"; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
function get_field_name($val) { |
| 99 |
$widget = $this->is_called_for_widget(); |
| 100 |
if ($widget) { |
| 101 |
return parent::get_field_name($val); |
| 102 |
} |
| 103 |
else { |
| 104 |
return $val; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
function is_called_for_widget() { |
| 110 |
// find out what context this was called from |
| 111 |
$backtrace = debug_backtrace(); |
| 112 |
if ($backtrace[3]['function'] == "shortcode_form") { |
| 113 |
return false; |
| 114 |
} |
| 115 |
else { |
| 116 |
return true; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
|