Completed
Pull Request — master (#206)
by Kiran
05:47
created

geodir_map_listingpage::widget()   F

Complexity

Conditions 18
Paths 2817

Size

Total Lines 97
Code Lines 58

Duplication

Lines 17
Ratio 17.53 %
Metric Value
dl 17
loc 97
rs 2
cc 18
eloc 58
nc 2817
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * GeoDirectory GMap - Listing page Widget
4
 *
5
 * This will display Google map on listing page with use of Google Map Api V3.
6
 *
7
 * @since 1.0.0
8
 *
9
 * @package GeoDirectory
10
 */
11
12
/**
13
 * Enque listing map script.
14
 *
15
 * @since 1.0.0
16
 *
17
 * @global array $list_map_json Empty array.
18
 */
19
function init_listing_map_script()
20
{
21
    global $list_map_json;
22
23
    $list_map_json = array();
24
25
}
26
27
/**
28
 * Create listing json for map script.
29
 *
30
 * @since 1.0.0
31
 *
32
 * @global object $wpdb WordPress Database object.
33
 * @global array $list_map_json Listing map data in json format.
34
 * @global bool $add_post_in_marker_array Displays posts in marker array when the value is true.
35
 */
36
function create_list_jsondata($post)
37
{
38
    global $wpdb, $list_map_json, $add_post_in_marker_array;
39
40
    if ((is_main_query() || $add_post_in_marker_array) && isset($post->marker_json) && $post->marker_json != '') {
41
        /**
42
         * Filter the json data for search listing map.
43
         *
44
         * @since 1.5.7
45
         * @param string $post->marker_json JSON representation of the post marker info.
46
         * @param object $post The post object.
47
         */
48
        $list_map_json[] = apply_filters('geodir_create_list_jsondata',$post->marker_json,$post);
49
    }
50
51
}
52
53
/**
54
 * Send json data to script and show listing map.
55
 *
56
 * @since 1.0.0
57
 *
58
 * @global array $list_map_json Listing map data in json format.
59
 */
60
function show_listing_widget_map()
61
{
62
    global $list_map_json;
63
64
    if (!empty($list_map_json)) {
65
        $list_map_json = array_unique($list_map_json);
66
        $cat_content_info[] = implode(',', $list_map_json);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$cat_content_info was never initialized. Although not strictly required by PHP, it is generally a good practice to add $cat_content_info = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
67
    }
68
69
    $totalcount = count(array_unique($list_map_json));
70
71
72 View Code Duplication
    if (!empty($cat_content_info))
73
        $list_json = '[{"totalcount":"' . $totalcount . '",' . substr(implode(',', $cat_content_info), 1) . ']';
74
    else
75
        $list_json = '[{"totalcount":"0"}]';
76
77
    $listing_map_args = array('list_json' => $list_json);
78
79
    // Pass the json data in listing map script
80
    wp_localize_script('geodir-listing-map-widget', 'listing_map_args', $listing_map_args);
81
82
}
83
84
/**
85
 * GeoDirectory listing page map widget class.
86
 *
87
 * @since 1.0.0
88
 */
89
class geodir_map_listingpage extends WP_Widget
90
{
91
92
    /**
93
	 * Register the listing page map widget.
94
	 *
95
	 * @since 1.0.0
96
     * @since 1.5.1 Changed from PHP4 style constructors to PHP5 __construct.
97
	 */
98
    public function __construct() {
99
        $widget_ops = array('classname' => 'widget geodir-map-listing-page', 'description' => __('Google Map for Listing page. It will show you google map V3 for Listing page.', 'geodirectory'));
100
        parent::__construct(
101
            'geodir_map_v3_listing_map', // Base ID
102
            __('GD > GMap - Listing page', 'geodirectory'), // Name
103
            $widget_ops// Args
104
        );
105
106
        add_action('wp_head', 'init_listing_map_script'); // Initialize the map object and marker array
107
108
        add_action('the_post', 'create_list_jsondata'); // Add marker in json array
109
110
        add_action('wp_footer', 'show_listing_widget_map'); // Show map for listings with markers
111
    }
112
113
	/**
114
	 * Front-end display content for listing page map widget.
115
	 *
116
	 * @since 1.0.0
117
     * @since 1.5.1 Declare function public.
118
	 *
119
     * @global object $post The current post object.
120
     *
121
	 * @param array $args     Widget arguments.
122
	 * @param array $instance Saved values from database.
123
	 */
124
    public function widget($args, $instance)
125
    {
126
127
        if (geodir_is_page('listing') || geodir_is_page('author') || geodir_is_page('search')
128
            || geodir_is_page('detail')
129
        ) :
130
131
            extract($args, EXTR_SKIP);
132
            /** This action is documented in geodirectory_shortcodes.php */
133
            $width = empty($instance['width']) ? '294' : apply_filters('widget_width', $instance['width']);
134
            /** This action is documented in geodirectory_shortcodes.php */
135
            $height = empty($instance['heigh']) ? '370' : apply_filters('widget_heigh', $instance['heigh']);
136
            /** This action is documented in geodirectory_shortcodes.php */
137
            $maptype = empty($instance['maptype']) ? 'ROADMAP' : apply_filters('widget_maptype', $instance['maptype']);
138
            /** This action is documented in geodirectory_shortcodes.php */
139
            $zoom = empty($instance['zoom']) ? '13' : apply_filters('widget_zoom', $instance['zoom']);
140
            /** This action is documented in geodirectory_shortcodes.php */
141
            $autozoom = empty($instance['autozoom']) ? '' : apply_filters('widget_autozoom', $instance['autozoom']);
142
            /**
143
             * Filter the listing map value widget_sticky, to set if the map should be sticky or not (scroll with page).
144
             *
145
             * @since 1.0.0
146
             * @param bool $sticky True if should be sticky, false if not
147
             */
148
            $sticky = empty($instance['sticky']) ? '' : apply_filters('widget_sticky', $instance['sticky']);
149
            /** This action is documented in geodirectory_shortcodes.php */
150
            $scrollwheel = empty($instance['scrollwheel']) ? '0' : apply_filters('widget_scrollwheel', $instance['scrollwheel']);
151
            $showall = empty($instance['showall']) ? '0' : apply_filters('widget_showall', $instance['showall']);
152
			
153
			/**
154
             * Filter the listing map should to be displayed or not.
155
             *
156
             * @since 1.4.6
157
			 *
158
             * @param bool $display true if map should be displayed, false if not.
159
             */
160
			$show_map = apply_filters( 'geodir_show_map_listing', $display = true );
161
			if ( !$show_map ) {
162
				return;
163
			}
164
165
            $map_args = array();
166
            $map_args['map_canvas_name'] = str_replace('-', '_', $args['widget_id']);
167
            $map_args['width'] = $width;
168
            $map_args['height'] = $height;
169
170
            $map_args['scrollwheel'] = $scrollwheel;
171
            $map_args['showall'] = $showall;
172
            $map_args['child_collapse'] = '0';
173
            $map_args['sticky'] = $sticky;
174
            $map_args['enable_cat_filters'] = false;
175
            $map_args['enable_text_search'] = false;
176
            $map_args['enable_post_type_filters'] = false;
177
            $map_args['enable_location_filters'] = false;
178
            $map_args['enable_jason_on_load'] = true;
179
			
180 View Code Duplication
            if (is_single()) {
181
182
                global $post;
183
                $map_default_lat = $address_latitude = $post->post_latitude;
0 ignored issues
show
Unused Code introduced by
$address_latitude is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
184
                $map_default_lng = $address_longitude = $post->post_longitude;
0 ignored issues
show
Unused Code introduced by
$address_longitude is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
185
                $mapview = $post->post_mapview;
0 ignored issues
show
Unused Code introduced by
$mapview is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
186
                $mapzoom = $post->post_mapzoom;
187
                $map_args['map_class_name'] = 'geodir-map-listing-page-single';
188
189
            } else {
190
                $default_location = geodir_get_default_location();
191
192
                $map_default_lat = isset($default_location->city_latitude) ? $default_location->city_latitude : '';
193
                $map_default_lng = isset($default_location->city_longitude) ? $default_location->city_longitude : '';
194
                $map_args['map_class_name'] = 'geodir-map-listing-page';
195
                $mapview = $maptype;
0 ignored issues
show
Unused Code introduced by
$mapview is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
196
            }
197
198
            if (empty($mapzoom)) $mapzoom = $zoom;
0 ignored issues
show
Unused Code introduced by
$mapzoom is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
199
200
            // Set default map options
201
            $map_args['ajax_url'] = geodir_get_ajax_url();
202
            $map_args['latitude'] = $map_default_lat;
203
            $map_args['longitude'] = $map_default_lng;
204
            $map_args['zoom'] = $zoom;
205
            //$map_args['scrollwheel'] = true;
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
206
            $map_args['scrollwheel'] = $scrollwheel;
207
            $map_args['showall'] = $showall;
208
            $map_args['streetViewControl'] = true;
209
            $map_args['maptype'] = $maptype;
210
            $map_args['showPreview'] = '0';
211
            $map_args['maxZoom'] = 21;
212
            $map_args['autozoom'] = $autozoom;
213
            $map_args['bubble_size'] = 'small';
214
215
            echo $before_widget;
216
            geodir_draw_map($map_args);
217
            echo $after_widget;
218
219
        endif;
220
    }
221
222
	/**
223
	 * Sanitize listing page map widget form values as they are saved.
224
	 *
225
	 * @since 1.0.0
226
     * @since 1.5.1 Declare function public.
227
	 *
228
	 * @param array $new_instance Values just sent to be saved.
229
	 * @param array $old_instance Previously saved values from database.
230
	 *
231
	 * @return array Updated safe values to be saved.
232
	 */
233
    public function update($new_instance, $old_instance)
234
    {
235
        //save the widget
236
        $instance = $old_instance;
237
        $instance['width'] = strip_tags($new_instance['width']);
238
        $instance['heigh'] = ($new_instance['heigh']);
239
        $instance['maptype'] = ($new_instance['maptype']);
240
        $instance['zoom'] = ($new_instance['zoom']);
241
        $instance['autozoom'] = isset($new_instance['autozoom']) ? $new_instance['autozoom'] : '';
242
        $instance['sticky'] = isset($new_instance['sticky']) ? $new_instance['sticky'] : '';
243
        $instance['scrollwheel'] = isset($new_instance['scrollwheel']) ? ($new_instance['scrollwheel']) : '';
244
        $instance['showall'] = isset($new_instance['showall']) ? ($new_instance['showall']) : '';
245
246
        return $instance;
247
    }
248
249
	/**
250
	 * Back-end listing page map widget settings form.
251
	 *
252
	 * @since 1.0.0
253
     * @since 1.5.1 Declare function public.
254
	 *
255
	 * @param array $instance Previously saved values from database.
256
	 */
257
    public function form($instance)
258
    {
259
        //widgetform in backend
260
        $instance = wp_parse_args((array)$instance, array('width' => '', 'heigh' => '', 'maptype' => '', 'zoom' => '', 'autozoom' => '', 'sticky' => '', 'scrollwheel' => '0', 'showall' => '0'));
261
        $width = strip_tags($instance['width']);
262
        $heigh = strip_tags($instance['heigh']);
263
        $maptype = strip_tags($instance['maptype']);
264
        $zoom = strip_tags($instance['zoom']);
265
        $autozoom = strip_tags($instance['autozoom']);
266
        $sticky = strip_tags($instance['sticky']);
267
        $scrollwheel = strip_tags($instance['scrollwheel']);
268
        $showall = strip_tags($instance['showall']);
269
        ?>
270
        <p>
271
            <label
272
                for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Map Width <small>(Default is : 294) you can use px or % here</small>', 'geodirectory'); ?>
273
                :
274
                <input class="widefat" id="<?php echo $this->get_field_id('width'); ?>"
275
                       name="<?php echo $this->get_field_name('width'); ?>" type="text"
276
                       value="<?php echo esc_attr($width); ?>"/>
277
            </label>
278
        </p>
279
280
        <p>
281
            <label
282
                for="<?php echo $this->get_field_id('heigh'); ?>"><?php _e('Map Height <small>(Default is : 370) you can use px or vh here</small>', 'geodirectory'); ?>
283
                :
284
                <input class="widefat" id="<?php echo $this->get_field_id('heigh'); ?>"
285
                       name="<?php echo $this->get_field_name('heigh'); ?>" type="text"
286
                       value="<?php echo esc_attr($heigh); ?>"/>
287
            </label>
288
        </p>
289
290
        <p>
291
            <label
292
                for="<?php echo $this->get_field_id('maptype'); ?>"><?php _e(' Select Map View', 'geodirectory'); ?>
293
                :
294
                <select class="widefat" id="<?php echo $this->get_field_id('maptype'); ?>"
295
                        name="<?php echo $this->get_field_name('maptype'); ?>">
296
297
                    <option <?php if (isset($maptype) && $maptype == 'ROADMAP') {
298
                        echo 'selected="selected"';
299
                    } ?> value="ROADMAP"><?php _e('Road Map', 'geodirectory'); ?></option>
300
                    <option <?php if (isset($maptype) && $maptype == 'SATELLITE') {
301
                        echo 'selected="selected"';
302
                    } ?> value="SATELLITE"><?php _e('Satellite Map', 'geodirectory'); ?></option>
303
                    <option <?php if (isset($maptype) && $maptype == 'HYBRID') {
304
                        echo 'selected="selected"';
305
                    } ?> value="HYBRID"><?php _e('Hybrid Map', 'geodirectory'); ?></option>
306
					<option <?php selected($maptype, 'TERRAIN');?> 
307
							value="TERRAIN"><?php _e('Terrain Map', 'geodirectory'); ?></option>
308
                </select>
309
            </label>
310
        </p>
311
312
        <?php
313
        $map_zoom_level = geodir_map_zoom_level();
314
        ?>
315
316
        <p>
317
            <label
318
                for="<?php echo $this->get_field_id('zoom'); ?>"><?php _e('Map Zoom level', 'geodirectory'); ?>
319
                :
320
321
                <select class="widefat" id="<?php echo $this->get_field_id('zoom'); ?>"
322
                        name="<?php echo $this->get_field_name('zoom'); ?>"> <?php
323
324 View Code Duplication
                    foreach ($map_zoom_level as $level) {
325
                        $selected = '';
326
                        if ($level == $zoom)
327
                            $selected = 'selected="selected"';
328
329
                        echo '<option ' . $selected . ' value="' . $level . '">' . $level . '</option>';
330
331
                    } ?>
332
333
                </select>
334
335
            </label>
336
        </p>
337
338
        <p>
339
            <label
340
                for="<?php echo $this->get_field_id('autozoom'); ?>"><?php _e('Map Auto Zoom ?', 'geodirectory'); ?>
341
                :
342
                <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('autozoom'); ?>"
343
                       name="<?php echo $this->get_field_name('autozoom'); ?>"<?php if ($autozoom) {
344
                    echo 'checked="checked"';
345
                } ?> /></label>
346
        </p>
347
348
        <p>
349
            <label
350
                for="<?php echo $this->get_field_id('sticky'); ?>"><?php _e('Map Sticky(should stick to the right of screen) ?', 'geodirectory'); ?>
351
                :
352
                <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('sticky'); ?>"
353
                       name="<?php echo $this->get_field_name('sticky'); ?>"<?php if ($sticky) {
354
                    echo 'checked="checked"';
355
                } ?> /> </label>
356
        </p>
357
358
        <p>
359
            <label
360
                for="<?php echo $this->get_field_id('scrollwheel'); ?>"><?php _e('Enable mouse scroll zoom ?', 'geodirectory'); ?>
361
                :
362
                <input id="<?php echo $this->get_field_id('scrollwheel'); ?>"
363
                       name="<?php echo $this->get_field_name('scrollwheel'); ?>" type="checkbox" value="1"
364
                       <?php if ($scrollwheel){ ?>checked="checked" <?php } ?> />
365
            </label>
366
        </p>
367
368
        <!-- <p>
369
      <label for="<?php echo $this->get_field_id('showall'); ?>"><?php _e('Show all listings on map? (not just page list)', 'geodirectory'); ?>:
370
      <input id="<?php echo $this->get_field_id('showall'); ?>" name="<?php echo $this->get_field_name('showall'); ?>" type="checkbox"  value="1"  <?php if ($showall) { ?>checked="checked" <?php } ?> />
371
      </label>
372
    </p> -->
373
374
    <?php
375
    }
376
} // class geodir_map_listingpage
377
378
register_widget('geodir_map_listingpage');
379