| 1 |
<?php |
| 2 |
namespace Cbx\Googlemap\Widgets\Classic; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
// Prevent direct file access |
| 10 |
use Cbx\Googlemap\CBXGooglemapSettings; |
| 11 |
use WP_Widget; |
| 12 |
|
| 13 |
class CBXGoogleMapWidget extends WP_Widget { |
| 14 |
/** |
| 15 |
* Unique identifier for your widget. |
| 16 |
* |
| 17 |
* |
| 18 |
* @since 1.1.7 |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $widget_slug = 'cbxgooglemap-widget'; //main parent plugin's language file |
| 23 |
/*--------------------------------------------------*/ |
| 24 |
/* Constructor |
| 25 |
/*--------------------------------------------------*/ |
| 26 |
|
| 27 |
/** |
| 28 |
* Specifies the classname and description, instantiates the widget, |
| 29 |
* loads localization files, and includes necessary stylesheets and JavaScript. |
| 30 |
*/ |
| 31 |
public function __construct() { |
| 32 |
parent::__construct( |
| 33 |
$this->get_widget_slug(), |
| 34 |
esc_html__( 'CBX Map: Location Map', 'cbxgooglemap' ), |
| 35 |
[ |
| 36 |
'classname' => 'widget-cbxgooglemap', |
| 37 |
'description' => esc_html__( 'CBX Map Widget', 'cbxgooglemap' ) |
| 38 |
] |
| 39 |
); |
| 40 |
|
| 41 |
}//end constructor |
| 42 |
|
| 43 |
/** |
| 44 |
* Return the widget slug. |
| 45 |
* |
| 46 |
* @return Plugin slug variable. |
| 47 |
* @since 1.0.0 |
| 48 |
* |
| 49 |
*/ |
| 50 |
public function get_widget_slug() { |
| 51 |
return $this->widget_slug; |
| 52 |
}//end method get_widget_slug |
| 53 |
|
| 54 |
/** |
| 55 |
* Outputs the content of the widget. |
| 56 |
* |
| 57 |
* @param array args The array of form elements |
| 58 |
* @param array instance The current instance of the widget |
| 59 |
*/ |
| 60 |
public function widget( $args, $instance ) { |
| 61 |
extract( $args, EXTR_SKIP ); |
| 62 |
|
| 63 |
$widget_string = $before_widget; |
| 64 |
|
| 65 |
$title = apply_filters( 'widget_title', |
| 66 |
empty( $instance['title'] ) ? esc_html__( 'CBX Map: Location Map','cbxgooglemap' ) : $instance['title'], $instance, $this->id_base ); |
| 67 |
// Defining the Widget Title |
| 68 |
if ( $title ) { |
| 69 |
$widget_string .= $args['before_title'] . $title . $args['after_title']; |
| 70 |
} else { |
| 71 |
$widget_string .= $args['before_title'] . $args['after_title']; |
| 72 |
} |
| 73 |
|
| 74 |
ob_start(); |
| 75 |
$settings = new CBXGooglemapSettings(); |
| 76 |
$general_settings = get_option( 'cbxgooglemap_general', [] ); |
| 77 |
|
| 78 |
$api_key = $settings->get_field( 'apikey', 'cbxgooglemap_general', '' ); |
| 79 |
$map_source = intval( $settings->get_field( 'mapsource', 'cbxgooglemap_general', 1 ) ); |
| 80 |
|
| 81 |
if ( $map_source == 1 && $api_key == '' ) { |
| 82 |
echo '<p style="text-align: center;">' . esc_html__( 'Google Map Api Key is invalid!', 'cbxgooglemap' ) . '</p>'; |
| 83 |
} else { |
| 84 |
|
| 85 |
$id = intval( $instance['map_id'] ); |
| 86 |
|
| 87 |
if ( $id > 0 ) { |
| 88 |
//render map from saved map |
| 89 |
echo do_shortcode( '[cbxgooglemap id="' . $id . '"]' ); |
| 90 |
} else { |
| 91 |
//render map from custom attributes |
| 92 |
$maptype = sanitize_text_field( wp_unslash($instance['maptype']) ); |
| 93 |
$lat = sanitize_text_field( wp_unslash($instance['lat']) ); |
| 94 |
$lng = sanitize_text_field( wp_unslash($instance['lng']) ); |
| 95 |
$width = sanitize_text_field( wp_unslash($instance['width']) ); |
| 96 |
$height = sanitize_text_field( wp_unslash($instance['height']) ); |
| 97 |
$zoom = sanitize_text_field( wp_unslash($instance['zoom']) ); |
| 98 |
$scrollwheel = absint( $instance['scrollwheel'] ); |
| 99 |
$showinfo = absint( $instance['showinfo'] ); |
| 100 |
$infow_open = absint( $instance['infow_open'] ); |
| 101 |
$heading = sanitize_text_field( wp_unslash($instance['heading']) ); |
| 102 |
$address = sanitize_text_field( wp_unslash($instance['address']) ); |
| 103 |
$website = sanitize_text_field( wp_unslash($instance['website']) ); |
| 104 |
|
| 105 |
echo do_shortcode( '[cbxgooglemap lat="' . esc_attr($lat) . '" lng="' . esc_attr($lng) . '" zoom="' . esc_attr($zoom) . '" scrollwheel="' . esc_attr($scrollwheel) . '" showinfo="' . esc_attr($showinfo) . '" infow_open="' . esc_attr($infow_open) . '" heading="' . esc_attr($heading) . '" maptype="' . esc_attr($maptype) . '" website="' . esc_attr($website) . '" address="' . esc_attr($address) . '" width="' . esc_attr($width) . '" height="' . esc_attr($height) . '"]' ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
$content = ob_get_contents(); |
| 110 |
ob_end_clean(); |
| 111 |
|
| 112 |
$widget_string .= $content; |
| 113 |
$widget_string .= $after_widget; |
| 114 |
echo $widget_string;// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 115 |
}//end of method widget |
| 116 |
|
| 117 |
|
| 118 |
/** |
| 119 |
* Processes the widget's options to be saved. |
| 120 |
* |
| 121 |
* @param array $new_instance |
| 122 |
* @param array $old_instance |
| 123 |
* |
| 124 |
* @return array |
| 125 |
*/ |
| 126 |
public function update( $new_instance, $old_instance ) { |
| 127 |
$instance = $old_instance; |
| 128 |
$instance['title'] = isset( $new_instance['title'] ) ? sanitize_text_field( wp_unslash($new_instance['title']) ) : ''; |
| 129 |
$instance['map_id'] = isset( $new_instance['map_id'] ) ? absint( $new_instance['map_id'] ) : 0; |
| 130 |
$instance['maptype'] = isset( $new_instance['maptype'] ) ? sanitize_text_field( wp_unslash($new_instance['maptype']) ) : 'roadmap'; |
| 131 |
$instance['lat'] = isset( $new_instance['lat'] ) ? sanitize_text_field( wp_unslash($new_instance['lat']) ) : ''; |
| 132 |
$instance['lng'] = isset( $new_instance['lng'] ) ? sanitize_text_field( wp_unslash($new_instance['lng']) ) : ''; |
| 133 |
$instance['width'] = isset( $new_instance['width'] ) ? sanitize_text_field( wp_unslash($new_instance['width']) ) : '100%'; |
| 134 |
$instance['height'] = isset( $new_instance['height'] ) ? sanitize_text_field( wp_unslash($new_instance['height']) ) : '300'; |
| 135 |
$instance['zoom'] = isset( $new_instance['zoom'] ) ? sanitize_text_field( wp_unslash($new_instance['zoom']) ) : '8'; |
| 136 |
$instance['scrollwheel'] = isset( $new_instance['scrollwheel'] ) ? absint( $new_instance['scrollwheel'] ) : 1; |
| 137 |
$instance['showinfo'] = isset( $new_instance['showinfo'] ) ? absint( $new_instance['showinfo'] ) : 1; |
| 138 |
$instance['infow_open'] = isset( $new_instance['infow_open'] ) ? absint( $new_instance['infow_open'] ) : 1; |
| 139 |
$instance['heading'] = isset( $new_instance['heading'] ) ? sanitize_text_field( wp_unslash($new_instance['heading']) ) : ''; |
| 140 |
$instance['address'] = isset( $new_instance['address'] ) ? sanitize_text_field( wp_unslash($new_instance['address']) ) : ''; |
| 141 |
$instance['website'] = isset( $new_instance['website'] ) ? sanitize_text_field( wp_unslash($new_instance['website']) ) : ''; |
| 142 |
|
| 143 |
return $instance; |
| 144 |
}//end of method widget |
| 145 |
|
| 146 |
/** |
| 147 |
* Generates the administration form for the widget. |
| 148 |
* |
| 149 |
* @param array instance The array of keys and values for the widget. |
| 150 |
*/ |
| 151 |
public function form( $instance ) { |
| 152 |
$general_settings = get_option( 'cbxgooglemap_general', [] ); |
| 153 |
|
| 154 |
$map_id = ( isset( $general_settings['map_id'] ) ) ? $general_settings['map_id'] : 0; |
| 155 |
$maptype = ( isset( $general_settings['maptype'] ) ) ? $general_settings['maptype'] : 'roadmap'; |
| 156 |
$lat = ( isset( $general_settings['lat'] ) ) ? $general_settings['lat'] : ''; |
| 157 |
$lng = ( isset( $general_settings['lng'] ) ) ? $general_settings['lng'] : ''; |
| 158 |
$width = ( isset( $general_settings['width'] ) ) ? $general_settings['width'] : '100%'; |
| 159 |
$height = ( isset( $general_settings['height'] ) ) ? $general_settings['height'] : '300'; |
| 160 |
$zoom = ( isset( $general_settings['zoom'] ) ) ? $general_settings['zoom'] : '8'; |
| 161 |
$scrollwheel = ( isset( $general_settings['scrollwheel'] ) ) ? $general_settings['scrollwheel'] : 1; |
| 162 |
$showinfo = ( isset( $general_settings['showinfo'] ) ) ? $general_settings['showinfo'] : 1; |
| 163 |
$infow_open = ( isset( $general_settings['infow_open'] ) ) ? $general_settings['infow_open'] : 1; |
| 164 |
$heading = ( isset( $general_settings['heading'] ) ) ? $general_settings['heading'] : ''; |
| 165 |
$address = ( isset( $general_settings['address'] ) ) ? $general_settings['address'] : ''; |
| 166 |
$website = ( isset( $general_settings['website'] ) ) ? $general_settings['website'] : ''; |
| 167 |
|
| 168 |
$instance = wp_parse_args( (array) $instance, |
| 169 |
[ |
| 170 |
'title' => esc_html__( 'CBX Map: Loation Map', 'cbxgooglemap' ), |
| 171 |
'map_id' => $map_id, |
| 172 |
'maptype' => $maptype, |
| 173 |
'lat' => $lat, |
| 174 |
'lng' => $lng, |
| 175 |
'width' => $width, |
| 176 |
'height' => $height, |
| 177 |
'zoom' => $zoom, |
| 178 |
'scrollwheel' => $scrollwheel, |
| 179 |
'showinfo' => $showinfo, |
| 180 |
'infow_open' => $infow_open, |
| 181 |
'heading' => $heading, |
| 182 |
'address' => $address, |
| 183 |
'website' => $website, |
| 184 |
] ); |
| 185 |
$title = sanitize_text_field( wp_unslash($instance['title']) ); |
| 186 |
$map_id = absint( $instance['map_id'] ); |
| 187 |
$maptype = sanitize_text_field( wp_unslash( $instance['maptype'] ) ); |
| 188 |
$lat = sanitize_text_field( wp_unslash( $instance['lat'] ) ); |
| 189 |
$lng = sanitize_text_field( wp_unslash( $instance['lng'] ) ); |
| 190 |
$width = sanitize_text_field( wp_unslash( $instance['width'] ) ); |
| 191 |
$height = sanitize_text_field( wp_unslash( $instance['height'] ) ); |
| 192 |
$zoom = sanitize_text_field( wp_unslash( $instance['zoom'] ) ); |
| 193 |
$scrollwheel = absint( $instance['scrollwheel'] ); |
| 194 |
$showinfo = absint( $instance['showinfo'] ); |
| 195 |
$infow_open = absint( $instance['infow_open'] ); |
| 196 |
$heading = sanitize_text_field( wp_unslash( $instance['heading'] ) ); |
| 197 |
$address = sanitize_text_field( wp_unslash( $instance['address'] ) ); |
| 198 |
$website = sanitize_text_field( wp_unslash( $instance['website'] ) ); |
| 199 |
|
| 200 |
// Display the admin form |
| 201 |
include( plugin_dir_path( __FILE__ ) . 'views/admin.php' ); |
| 202 |
}//end of method form |
| 203 |
}//end class CBXGoogleMap_Widget |