Test Failed
Pull Request — master (#238)
by Kiran
09:22
created

map_functions.php ➔ geodir_map_name()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 15
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
1
<?php
2
/**
3
 * Google Map related functions.
4
 *
5
 * @since 1.0.0
6
 * @package GeoDirectory
7
 */
8
9
/**
10
 * Creates a global variable for storing map json data.
11
 *
12
 * @since 1.0.0
13
 * @package GeoDirectory
14
 * @global array $map_jason Empty array.
15
 */
16
function  geodir_init_map_jason()
17
{
18 7
    global $map_jason;
19 7
    $map_jason = array();
20 7
}
21
22
/**
23
 * Creates a global variable for storing map canvas data.
24
 *
25
 * @since 1.0.0
26
 * @package GeoDirectory
27
 * @global array $map_canvas_arr Empty array.
28
 */
29
function geodir_init_map_canvas_array()
30
{
31 7
    global $map_canvas_arr;
32 7
    $map_canvas_arr = array();
33 7
}
34
35
36
/**
37
 * Creates marker json using given $post object.
38
 *
39
 * @since 1.0.0
40
 * @since 1.5.0 Converts icon url to HTTPS if HTTPS is active.
41
 * @since 1.6.1 Marker icons size added.
42
 *
43
 * @package GeoDirectory
44
 * @param null|WP_Post $post Post object.
45
 * @global object $wpdb WordPress Database object.
46
 * @global array $map_jason Map data in json format.
47
 * @global bool $add_post_in_marker_array Displays posts in marker array when the value is true.
48
 * @global array $geodir_cat_icons Category icons array. syntax: array( 'category name' => 'icon url').
49
 * @global array  $gd_marker_sizes Array of the marker icons sizes.
50 25
 */
51
function create_marker_jason_of_posts($post)
52 25
{
53
    global $wpdb, $map_jason, $add_post_in_marker_array, $geodir_cat_icons, $gd_marker_sizes;
54
55
    if (!empty($post) && isset($post->ID) && $post->ID > 0 && (is_main_query() || $add_post_in_marker_array) && $post->marker_json != '') {
56
        $srcharr = array("'", "/", "-", '"', '\\');
57
        $replarr = array("&prime;", "&frasl;", "&ndash;", "&ldquo;", '');
58
59
60
        $geodir_cat_icons = geodir_get_term_icon();
61
        $icon = !empty($geodir_cat_icons) && isset($geodir_cat_icons[$post->default_category]) ? $geodir_cat_icons[$post->default_category] : '';
62
63
        $post_title = $post->post_title;
64
        $title = str_replace($srcharr, $replarr, $post_title);
65
66
        if (is_ssl()) {
67
            $icon = str_replace("http:","https:",$icon );
68
        }
69
        
70 View Code Duplication
        if ($icon != '') {
71
            $gd_marker_sizes = empty($gd_marker_sizes) ? array() : $gd_marker_sizes;
72
            
73
            if (isset($gd_marker_sizes[$icon])) {
74
                $icon_size = $gd_marker_sizes[$icon];
75
            } else {
76
                $icon_size = geodir_get_marker_size($icon);
77
                $gd_marker_sizes[$icon] = $icon_size;
78 25
            }               
79
        } else {
80
            $icon_size = array('w' => 36, 'h' => 45);
81
        }
82
83
        $post_json = '{"id":"' . $post->ID . '","t": "' . $title . '","lt": "' . $post->post_latitude . '","ln": "' . $post->post_longitude . '","mk_id":"' . $post->ID . '_' . $post->default_category . '","i":"' . $icon . '","w":"' . $icon_size['w'] . '","h":"' . $icon_size['h'] . '"}';
84
85
        /**
86
         * Filter the json data when creating output for post json marker..
87
         *
88
         * @since 1.5.7
89
         * @param string $post_json JSON representation of the post marker info.
90 7
         * @param object $post The post object.
91
         */
92 7
        $map_jason[] = apply_filters('geodir_create_marker_jason_of_posts',$post_json, $post);
93 2
    }
94 2
}
95
96 1
/**
97 1
 * Send jason data to script and show listing map.
98 1
 *
99 1
 * @since 1.0.0
100 1
 * @package GeoDirectory
101 1
 * @global array $map_jason Map data in json format.
102 1
 * @global array $map_canvas_arr Map canvas array.
103 1
 */
104 1
function send_marker_jason_to_js()
105 1
{
106
    global $map_jason, $map_canvas_arr;
107
108
    if (is_array($map_canvas_arr) && !empty($map_canvas_arr)) {
109 1
        foreach ($map_canvas_arr as $canvas => $jason) {
110
            if (is_array($map_jason) && !empty($map_jason)) {
111
112
                $canvas_jason = $canvas . "_jason";
0 ignored issues
show
Unused Code introduced by
$canvas_jason 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...
113
                $map_canvas_arr[$canvas] = array_unique($map_jason);
114
                unset($cat_content_info);
115
                $cat_content_info[] = implode(',', $map_canvas_arr[$canvas]);
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...
116
                $totalcount = count(array_unique($map_jason));
117 View Code Duplication
                if (!empty($cat_content_info)) {
118
                    $json_content = substr(implode(',', $cat_content_info), 1);
119
                    $json_content = htmlentities($json_content, ENT_QUOTES); // Quotes in csv title import break maps - FIXED by kiran on 2nd March, 2016
120
                    $canvas_jason = '[{"totalcount":"' . $totalcount . '",' . $json_content . ']';
121 1
                } else {
122
                    $canvas_jason = '[{"totalcount":"0"}]';
123 1
                }
124 1
125 1
                $map_canvas_jason_args = array($canvas . '_jason' => $canvas_jason);
126 1
127
                /**
128
                 * Filter the send_marker_jason_to_js() function map canvas json args.
129
                 *
130
                 * You can use this filter to modify map canvas json args.
131
                 *
132
                 * @since 1.0.0
133
                 * @package GeoDirectory
134
                 * @param string $canvas Map canvas array key.
135
                 * @param array $map_canvas_jason_args Map canvas args.
136
                 */
137
                $map_canvas_jason_args = apply_filters('geodir_map_canvas_jason_' . $canvas, $map_canvas_jason_args);
138 1
139 1
                wp_localize_script('geodir-map-widget', $canvas . '_jason_args', $map_canvas_jason_args);
140
            } else {
141 2
                $canvas_jason = '[{"totalcount":"0"}]';
142
                $map_canvas_jason_args = array($canvas . '_jason' => $canvas_jason);
143 2
144 7
                /**
145
                 * Filter the send_marker_jason_to_js() function map canvas json args.
146
                 *
147
                 * You can use this filter to modify map canvas json args.
148
                 *
149
                 * @since 1.0.0
150
                 * @package GeoDirectory
151
                 * @param string $canvas Map canvas array key.
152
                 * @param array $map_canvas_jason_args Map canvas args.
153
                 */
154
                $map_canvas_jason_args = apply_filters('geodir_map_canvas_jason_' . $canvas, $map_canvas_jason_args);
155
                wp_localize_script('geodir-map-widget', $canvas . '_jason_args', $map_canvas_jason_args);
156
            }
157
        }
158
159
    }
160
}
161
162 3
/**
163
 * Home map Taxonomy walker.
164 3
 *
165 3
 * @since 1.0.0
166
 * @package GeoDirectory
167
 * @param string $cat_taxonomy Name of the taxonomy e.g place_category.
168 3
 * @param int $cat_parent Optional. Parent term ID to retrieve its child terms. Default 0.
169 3
 * @param bool $hide_empty Optional. Do you want to hide the terms that has no posts. Default true.
170 3
 * @param int $pading Optional. CSS padding value in pixels. e.g: 12 will be considers as 12px.
171 3
 * @param string $map_canvas_name Unique canvas name for your map.
172
 * @param bool $child_collapse Do you want to collapse child terms by default?.
173 3
 * @param bool $is_home_map Optional. Is this a home page map? Default: false.
174
 * @return string|void
175 3
 */
176 3
function home_map_taxonomy_walker($cat_taxonomy, $cat_parent = 0, $hide_empty = true, $pading = 0, $map_canvas_name = '', $child_collapse, $is_home_map = false)
177 3
{
178
    global $cat_count, $geodir_cat_icons;
179 3
180
    $exclude_categories = get_option('geodir_exclude_cat_on_map');
181 3
    $exclude_categories_new = get_option('geodir_exclude_cat_on_map_upgrade');
182
183 3
    // check if exclude categories saved before fix of categories identical names
184 View Code Duplication
    if ($exclude_categories_new) {
185 3
        $gd_cat_taxonomy = isset($cat_taxonomy[0]) ? $cat_taxonomy[0] : '';
186 3
        $exclude_categories = !empty($exclude_categories[$gd_cat_taxonomy]) && is_array($exclude_categories[$gd_cat_taxonomy]) ? array_unique($exclude_categories[$gd_cat_taxonomy]) : array();
187
    }
188 3
189 3
    $exclude_cat_str = implode(',', $exclude_categories);
190 3
191 3
    if ($exclude_cat_str == '') {
192
        $exclude_cat_str = '0';
193
    }
194
195
    $cat_terms = get_terms($cat_taxonomy, array('parent' => $cat_parent, 'exclude' => $exclude_cat_str, 'hide_empty ' => $hide_empty));
196
    
197 3
    if ($hide_empty) {
198
        $cat_terms = geodir_filter_empty_terms($cat_terms);
199 3
    }
200
201 3
    $main_list_class = '';
202
    //If there are terms, start displaying
203 3
    if (count($cat_terms) > 0) {
204
        //Displaying as a list
205 3
        $p = $pading * 15;
206
        $pading++;
207 3
208
        if ($cat_parent == 0) {
209 3
            $list_class = 'main_list';
210
            $display = '';
211
        } else {
212 3
            $list_class = 'sub_list';
213
            $display = !$child_collapse ? '' : 'display:none';
214
        }
215
216
217
        $out = '<ul class="treeview ' . $list_class . '" style="margin-left:' . $p . 'px;' . $display . ';">';
218 3
219 3
        $geodir_cat_icons = geodir_get_term_icon();
220 3
221 3
        foreach ($cat_terms as $cat_term):
222 3
223
            $post_type = isset($_REQUEST['post_type']) ? $_REQUEST['post_type'] : 'gd_place';
224 3
225
            $icon = !empty($geodir_cat_icons) && isset($geodir_cat_icons[$cat_term->term_id]) ? $geodir_cat_icons[$cat_term->term_id] : '';
226
227
            if (!in_array($cat_term->term_id, $exclude_categories)):
228 3
                //Secret sauce.  Function calls itself to display child elements, if any
229
                $checked = 'checked="checked"';
230 3
231
                // Untick the category by default on home map
232 3
                if ($is_home_map && $geodir_home_map_untick = get_option('geodir_home_map_untick')) {
233
                    if (!empty($geodir_home_map_untick) && in_array($post_type . '_' . $cat_term->term_id, $geodir_home_map_untick)) {
234 3
                        $checked = '';
235
                    }
236 3
                }
237
238 3
                $term_check = '<input type="checkbox" ' . $checked . ' id="' .$map_canvas_name.'_tick_cat_'. $cat_term->term_id . '" class="group_selector ' . $main_list_class . '"';
239 3
                $term_check .= ' name="' . $map_canvas_name . '_cat[]" ';
240
                $term_check .= '  title="' . esc_attr(ucfirst($cat_term->name)) . '" value="' . $cat_term->term_id . '" onclick="javascript:build_map_ajax_search_param(\'' . $map_canvas_name . '\',false, this)">';
241 3
                $term_img = '<img height="15" width="15" alt="' . $cat_term->taxonomy . '" src="' . $icon . '" title="' . ucfirst($cat_term->name) . '"/>';
242
                $out .= '<li>' . $term_check . '<label for="' . $map_canvas_name.'_tick_cat_'. $cat_term->term_id . '">' . $term_img . ucfirst($cat_term->name) . '</label><i class="fa fa-long-arrow-down"></i>';
243
244
            endif;
245
246
247
            // get sub category by recursion
248
            $out .= home_map_taxonomy_walker($cat_taxonomy, $cat_term->term_id, $hide_empty, $pading, $map_canvas_name, $child_collapse, $is_home_map);
249
250
            $out .= '</li>';
251
252
        endforeach;
253
254
        $out .= '</ul>';
255
256
        return $out;
257
    } else {
258
        if ($cat_parent == 0)
259
            return _e('No category', 'geodirectory');
260
    }
261
    return;
262
}
263
264
/**
265
 * Get the map JS API provider name.
266
 *
267
 * @since 1.6.1
268
 * @package GeoDirectory
269
 *
270
 * @return string The map API provider name.
271
 */
272
function geodir_map_name() {
273
    $geodir_map_name = get_option('geodir_load_map', 'google');
274
    
275
    if (!in_array($geodir_map_name, array('none', 'auto', 'google', 'osm'))) {
276
        $geodir_map_name = 'auto';
277
    }
278
279
    /**
280
     * Filter the map JS API provider name.
281
     *
282
     * @since 1.6.1
283
     * @param string $geodir_map_name The map API provider name.
284
     */
285
    return apply_filters('geodir_map_name', $geodir_map_name);
286
}
287
288
/**
289
 * Get the marker icon size.
290
 * This will return width and height of icon in array (ex: w => 36, h => 45).
291
 *
292
 * @since 1.6.1
293
 * @package GeoDirectory
294
 *
295
 * @global $gd_marker_sizes Array of the marker icons sizes.
296
 *
297
 * @param string $icon Marker icon url.
298
 * @return array The icon size.
299
 */
300
function geodir_get_marker_size($icon, $default_size = array('w' => 36, 'h' => 45)) {
301
    global $gd_marker_sizes;
302
    
303
    if (empty($gd_marker_sizes)) {
304
        $gd_marker_sizes = array();
305
    }
306
      
307
    if (!empty($gd_marker_sizes[$icon])) {
308
        return $gd_marker_sizes[$icon];
309
    }
310
    
311
    if (empty($icon)) {
312
        $gd_marker_sizes[$icon] = $default_size;
313
        
314
        return $default_size;
315
    }
316
    
317
    $icon_url = $icon;
318
    
319
    $uploads = wp_upload_dir(); // Array of key => value pairs
320
      
321 View Code Duplication
    if (!path_is_absolute($icon)) {
322
        $icon = str_replace($uploads['baseurl'], $uploads['basedir'], $icon);
323
    }
324
    
325
    if (!path_is_absolute($icon) && strpos($icon, WP_CONTENT_URL) !== false) {
326
        $icon = str_replace(WP_CONTENT_URL, WP_CONTENT_DIR, $icon);
327
    }
328
    
329
    $sizes = array();
330
    if (is_file($icon) && file_exists($icon)) {
331
        $size = getimagesize(trim($icon));
332
        
333
        if (!empty($size[0]) && !empty($size[1])) {
334
            $sizes = array('w' => $size[0], 'h' => $size[1]);
335
        }
336
    }
337
    
338
    $sizes = !empty($sizes) ? $sizes : $default_size;
339
    
340
    $gd_marker_sizes[$icon_url] = $sizes;
341
    
342
    return $sizes;
343
}
344
345
add_action('wp_head', 'geodir_map_load_style', 10);
346
add_action('admin_head', 'geodir_map_load_style', 10);
347
/**
348
 * Adds the marker cluster style for OpenStreetMap when Google JS Library not loaded.
349
 *
350
 * @since 1.6.1
351
 * @package GeoDirectory
352
 */
353
function geodir_map_load_style() {    
354
    if (in_array(geodir_map_name(), array('auto', 'google')) && wp_script_is( 'geodirectory-googlemap-script', 'done')) {
355
?>
356
<script type="text/javascript">
357
if (!(window.google && typeof google.maps !== 'undefined')) {
358
    document.write('<' + 'link id="geodirectory-leaflet-style-css" media="all" type="text/css" href="<?php echo geodir_plugin_url();?>/geodirectory-assets/leaflet/leaflet.css?ver=<?php echo GEODIRECTORY_VERSION;?>" rel="stylesheet"' + '>');
359
}
360
</script>
361
<?php
362
    }
363
}
364
365
add_action('wp_footer', 'geodir_map_load_script', 10);
366
add_action('admin_footer', 'geodir_map_load_script', 10);
367
/**
368
 * Adds the marker cluster script for OpenStreetMap when Google JS Library not loaded.
369
 *
370
 * @since 1.6.1
371
 * @package GeoDirectory
372
 */
373
function geodir_map_load_script() {
374
    if (in_array(geodir_map_name(), array('auto', 'google')) && wp_script_is( 'geodirectory-googlemap-script', 'done')) {
375
?>
376
<script type="text/javascript">
377
if (!(window.google && typeof google.maps !== 'undefined')) {
378
    document.write('<' + 'script id="geodirectory-leaflet-script" src="<?php echo geodir_plugin_url();?>/geodirectory-assets/leaflet/leaflet.min.js?ver=<?php echo GEODIRECTORY_VERSION;?>" type="text/javascript"><' + '/script>');
379
    document.write('<' + 'script id="geodirectory-leaflet-geo-script" src="<?php echo geodir_plugin_url();?>/geodirectory-assets/leaflet/osm.geocode.js?ver=<?php echo GEODIRECTORY_VERSION;?>" type="text/javascript"><' + '/script>');
380
}
381
</script>
382
<?php
383
    }
384
}