Test Failed
Push — master ( 33c2aa...380661 )
by Stiofan
04:39
created

Geodir_Tax_Meta_Class::show()   C

Complexity

Conditions 7
Paths 13

Size

Total Lines 23
Code Lines 14

Duplication

Lines 2
Ratio 8.7 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 13
nop 1
dl 2
loc 23
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * Tax Meta Class
4
 *
5
 * The Tax Meta Class is used by including it in your plugin r theme files and using its methods to
6
 * Add meta fields for WordPress Taxonomies (categories,tags and custom taxonomies). It is meant to be very simple and
7
 * straightforward.
8
 *
9
 * This class is derived from My-Meta-Box (https://github.com/bainternet/My-Meta-Box script) which is
10
 * a class for creating custom meta boxes for WordPress.
11
 *
12
 * @version 1.2
13
 * @copyright 2012
14
 * @author Ohad Raz (email: [email protected])
15
 * @link http://en.bainternet.info
16
 *
17
 * @license GNU General Public LIcense v3.0 - license.txt
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 *
26
 * @package Tax Meta Class
27
 */
28
29
if (!class_exists('Geodir_Tax_Meta_Class')) :
30
31
    /**
32
     * All Types Meta Box class.
33
     *
34
     * @package All Types Meta Box
35
     * @since 1.0
36
     *
37
     * @todo Nothing.
38
     */
39
40
41
    class Geodir_Tax_Meta_Class
42
    {
43
44
        /**
45
         * Holds meta box object
46
         *
47
         * @var object
48
         * @access protected
49
         */
50
        protected $_meta_box;
51
52
        /**
53
         * Holds meta box fields.
54
         *
55
         * @var array
56
         * @access protected
57
         */
58
        protected $_prefix;
59
60
        /**
61
         * Holds Prefix for meta box fields.
62
         *
63
         * @var array
64
         * @access protected
65
         */
66
        protected $_fields;
67
68
        /**
69
         * Use local images.
70
         *
71
         * @var bool
72
         * @access protected
73
         */
74
        protected $_Local_images;
75
76
        /**
77
         * What form is this? edit or new term.
78
         *
79
         * @var string
80
         * @access protected
81
         * $since 1.0
82
         */
83
        protected $_form_type;
84
        /**
85
         * SelfPath to allow themes as well as plugins.
86
         *
87
         * @var string
88
         * @access protected
89
         * $since 1.0
90
         */
91
        protected $SelfPath;
92
93
        /**
94
         * Constructor
95
         *
96
         * @since 1.0
97
         * @access public
98
         *
99
         * @param array $meta_box
100
         */
101
        public function __construct($meta_box)
102
        {
103
104
            // If we are not in admin area exit.
105
            if (!is_admin())
106
                return;
107
108
            // Assign meta box values to local variables and add it's missed values.
109
            $this->_meta_box = $meta_box;
0 ignored issues
show
Documentation Bug introduced by
It seems like $meta_box of type array is incompatible with the declared type object of property $_meta_box.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
110
            $this->_prefix = (isset($meta_box['prefix'])) ? $meta_box['prefix'] : '';
111
            $this->_fields = &$this->_meta_box['fields'];
112
            $this->_Local_images = (isset($meta_box['local_images'])) ? true : false;
113
            $this->add_missed_values();
114
            if (isset($meta_box['use_with_theme']))
115
                if ($meta_box['use_with_theme'] === true) {
116
                    $this->SelfPath = get_stylesheet_directory_uri() . '/library/cat-meta';
117
                } elseif ($meta_box['use_with_theme'] === false) {
118
                    $this->SelfPath = plugins_url('cat-meta-functions', plugin_basename(dirname(__FILE__)));
119
                } else {
120
                    $this->SelfPath = $meta_box['use_with_theme'];
121
                }
122
            else {
123
                $this->SelfPath = plugins_url('cat-meta-functions', plugin_basename(dirname(__FILE__)));
124
            }
125
126
127
            // Add Actions
128
            add_action('admin_init', array(&$this, 'add'));
129
130
            // Check for special fields and add needed actions for them.
131
            $this->check_field_upload();
132
            $this->check_field_color();
133
            $this->check_field_date();
134
            $this->check_field_time();
135
136
            // Load common js, css files
137
            // Must enqueue for all pages as we need js for the media upload, too.
138
            add_action('admin_print_styles', array(&$this, 'load_scripts_styles'));
139
140
        }
141
142
        /**
143
         * Load all Javascript and CSS
144
         *
145
         * @since 1.0
146
         * @access public
147
         */
148
        public function load_scripts_styles()
149
        {
150
151
            // Get Plugin Path
152
            $plugin_path = $this->SelfPath;
0 ignored issues
show
Unused Code introduced by
$plugin_path 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...
153
            //only load styles and js when needed
154
            /* 
155
		 * since 1.0
156
		 */
157
            $taxnow = isset($_REQUEST['taxonomy']) ? $_REQUEST['taxonomy'] : '';
158
159
            if (!empty($this->_meta_box['pages'])) {
160
                if (in_array($taxnow, $this->_meta_box['pages'])) {
161
                    // Enqueue Meta Box Style
162
                    //wp_enqueue_style( 'tax-meta-clss', $plugin_path . '/css/Tax-meta-class.css' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
163
                    // Enqueue Meta Box Scripts
164
                    //wp_enqueue_script( 'tax-meta-clss', $plugin_path . '/js/tax-meta-clss.js', array( 'jquery' ), null, true );
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
165
166
                }
167
            }
168
169
        }
170
171
        /**
172
         * Check the Field Upload, Add needed Actions
173
         *
174
         * @since 1.0
175
         * @access public
176
         */
177
        public function enqueue_tax_meta_scripts()
178
        {
179
            // Make upload feature work event when custom post type doesn't support 'editor'
180
            wp_enqueue_script('media-upload');
181
            wp_enqueue_script('thickbox');
182
            add_thickbox();
183
            wp_enqueue_script('jquery-ui-core');
184
            wp_enqueue_script('jquery-ui-sortable');
185
186
187
        }
188
189
        public function check_field_upload()
190
        {
191
192
            // Check if the field is an image or file. If not, return.
193
            if (!$this->has_field('image') && !$this->has_field('file'))
194
                return;
195
196
197
            add_action('wp_enqueue_scripts', array(&$this, 'enqueue_tax_meta_scripts'), 100);
198
199
            // Add data encoding type for file uploading.	
200
            add_action('post_edit_form_tag', array(&$this, 'add_enctype'));
201
202
203
            // Add filters for media upload.
204
            add_filter('media_upload_gallery', array(&$this, 'insert_images'));
205
            add_filter('media_upload_library', array(&$this, 'insert_images'));
206
            add_filter('media_upload_image', array(&$this, 'insert_images'));
207
208
            // Delete all attachments when delete custom post type.
209
            add_action('wp_ajax_at_delete_file', array(&$this, 'delete_file'));
210
            add_action('wp_ajax_at_reorder_images', array(&$this, 'reorder_images'));
211
            // Delete file via Ajax
212
            add_action('wp_ajax_at_delete_mupload', array($this, 'wp_ajax_delete_image'));
213
        }
214
215
        /**
216
         * Add data encoding type for file uploading
217
         *
218
         * @since 1.0
219
         * @access public
220
         */
221
        public function add_enctype()
222
        {
223
            echo ' enctype="multipart/form-data"';
224
        }
225
226
        /**
227
         * Process images added to meta field.
228
         *
229
         * Modified from Faster Image Insert plugin.
230
         *
231
         * @return void
232
         * @author Cory Crowley
233
         */
234
        public function insert_images()
235
        {
236
237
            // If post variables are empty, return.
238
            if (!isset($_POST['at-insert']) || empty($_POST['attachments']))
239
                return;
240
241
            // Security Check
242
            check_admin_referer('media-form');
243
244
            // Create Security Nonce
245
            $nonce = wp_create_nonce('at_ajax_delete');
246
247
            // Get Post Id and Field Id
248
            $term_id = $_POST['post_id'];
249
            $id = $_POST['field_id'];
250
251
            // Modify the insertion string
252
            $html = '';
253
            foreach ($_POST['attachments'] as $attachment_id => $attachment) {
254
255
                // Strip Slashes
256
                $attachment = stripslashes_deep($attachment);
257
258
                // If not selected or url is empty, continue in loop.
259
                if (empty($attachment['selected']) || empty($attachment['url']))
260
                    continue;
261
262
                $li = "<li id='item_{$attachment_id}'>";
263
                $li .= "<img src='{$attachment['url']}' alt='image_{$attachment_id}' />";
264
                //$li 	.= "<a title='" . __( 'Delete this image' ) . "' class='at-delete-file' href='#' rel='{$nonce}|{$term_id}|{$id}|{$attachment_id}'>" . __( 'Delete' ) . "</a>";
265
                $li .= "<a title='" . __('Remove this image', 'geodirectory') . "' class='at-delete-file' href='#' rel='{$nonce}|{$term_id}|{$id}|{$attachment_id}'><img src='" . $this->SelfPath . "/images/delete-16.png' alt='" . __('Remove', 'geodirectory') . "' /></a>";
266
                $li .= "<input type='hidden' name='{$id}[]' value='{$attachment_id}' />";
267
                $li .= "</li>";
268
                $html .= $li;
269
270
            } // End For Each
271
272
            return media_send_to_editor($html);
273
274
        }
275
276
        /**
277
         * Delete attachments associated with the post.
278
         *
279
         * @since 1.0
280
         * @access public
281
         *
282
         * @param int|string $term_id The term ID.
283
         */
284
        /*public function delete_attachments( $term_id ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
285
		
286
		// Get Attachments
287
		$attachments = get_posts( array( 'numberposts' => -1, 'post_type' => 'attachment', 'post_parent' => $term_id ) );
288
		
289
		// Loop through attachments, if not empty, delete it.
290
		if ( ! empty( $attachments ) ) {
291
			foreach ( $attachments as $att ) {
292
				wp_delete_attachment( $att->ID );
293
			}
294
		}
295
		
296
	}*/
297
298
        /**
299
         * Ajax callback for deleting files.
300
         *
301
         * Modified from a function used by "Verve Meta Boxes" plugin ( http://goo.gl/aw64H )
302
         *
303
         * @since 1.0
304
         * @access public
305
         */
306
        public function delete_file()
307
        {
308
309
310
            // If data is not set, die.
311
            if (!isset($_POST['data']))
312
                die();
313
314
            list($nonce, $term_id, $key, $attach_id) = explode('|', $_POST['data']);
315
316
            if (!wp_verify_nonce($nonce, 'at_ajax_delete'))
317
                die('1');
318
319
            $this->delete_tax_meta($term_id, $key, $attach_id);
320
321
            die('0');
322
323
        }
324
325
        /**
326
         * Ajax callback for deleting files.
327
         * Modified from a function used by "Verve Meta Boxes" plugin (http://goo.gl/LzYSq)
328
         * @since 1.0
329
         * @access public
330
         */
331
        public function wp_ajax_delete_image()
332
        {
333
            $term_id = isset($_GET['post_id']) ? intval($_GET['post_id']) : 0;
334
            $field_id = isset($_GET['field_id']) ? $_GET['field_id'] : 0;
335
            $attachment_id = isset($_GET['attachment_id']) ? intval($_GET['attachment_id']) : 0;
0 ignored issues
show
Unused Code introduced by
$attachment_id 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...
336
            $ok = false;
337
            if (strpos($field_id, '[') === false) {
338
                check_admin_referer("at-delete-mupload_" . urldecode($field_id));
339
                if ($term_id > 0)
340
                    $this->delete_tax_meta($term_id, $field_id);
341
                //$ok = wp_delete_attachment( $attachment_id );
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
342
                $ok = 1;
343
            } else {
344
                $f = explode('[', urldecode($field_id));
345
                $f_fiexed = array();
0 ignored issues
show
Unused Code introduced by
$f_fiexed 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...
346
                foreach ($f as $k => $v) {
347
                    $f[$k] = str_replace(']', '', $v);
348
                }
349
                $saved = $this->get_tax_meta($term_id, $f[0], true);
350
                if (isset($saved[$f[1]][$f[2]])) {
351
                    unset($saved[$f[1]][$f[2]]);
352
                    if ($term_id > 0)
353
                        update_post_meta($term_id, $f[0], $saved);
354
                    //$ok = wp_delete_attachment( $attachment_id );
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
355
                    $ok = 1;
356
                }
357
            }
358
359
360
            if ($ok) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ok of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
361
                echo json_encode(array('status' => 'success'));
362
                die();
363
            } else {
364
                echo json_encode(array('message' => __('Cannot delete file. Something\'s wrong.', 'geodirectory')));
365
                die();
366
            }
367
        }
368
369
        /**
370
         * Ajax callback for reordering Images.
371
         *
372
         * @since 1.0
373
         * @access public
374
         */
375
        public function reorder_images()
376
        {
377
378
            if (!isset($_POST['data']))
379
                die();
380
381
            list($order, $term_id, $key, $nonce) = explode('|', $_POST['data']);
0 ignored issues
show
Unused Code introduced by
The assignment to $key is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
382
383
            if (!wp_verify_nonce($nonce, 'at_ajax_reorder'))
384
                die('1');
385
386
            parse_str($order, $items);
387
            $items = $items['item'];
388
            $order = 1;
389
            foreach ($items as $item) {
390
                wp_update_post(array('ID' => $item, 'post_parent' => $term_id, 'menu_order' => $order));
391
                $order++;
392
            }
393
394
            die('0');
395
396
        }
397
398
        /**
399
         * Check Field Color
400
         *
401
         * @since 1.0
402
         * @access public
403
         */
404
        public function check_field_color()
405
        {
406
407
            if ($this->has_field('color') && $this->is_edit_page()) {
408
                // Enqueu built-in script and style for color picker.
409
                wp_enqueue_style('farbtastic');
410
                wp_enqueue_script('farbtastic');
411
            }
412
413
        }
414
415
        /**
416
         * Check Field Date
417
         *
418
         * @since 1.0
419
         * @access public
420
         */
421
        public function check_field_date()
422
        {
423
424
            if ($this->has_field('date') && $this->is_edit_page()) {
425
                // Enqueu JQuery UI, use proper version.
426
                wp_enqueue_style('tmc-jquery-ui-css', 'https://ajax.googleapis.com/ajax/libs/jqueryui/' . $this->get_jqueryui_ver() . '/themes/base/jquery-ui.css');
427
                wp_enqueue_script('tmc-jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/' . $this->get_jqueryui_ver() . '/jquery-ui.min.js', array('jquery'));
428
            }
429
430
        }
431
432
        /**
433
         * Check Field Time
434
         *
435
         * @since 1.0
436
         * @access public
437
         */
438
        public function check_field_time()
439
        {
440
441
            if ($this->has_field('time') && $this->is_edit_page()) {
442
443
                // Enqueu JQuery UI, use proper version.
444
                wp_enqueue_style('tmc-jquery-ui-css', 'https://ajax.googleapis.com/ajax/libs/jqueryui/' . $this->get_jqueryui_ver() . '/themes/base/jquery-ui.css', array(), false, true);
445
                wp_enqueue_script('tmc-jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/' . $this->get_jqueryui_ver() . '/jquery-ui.min.js', array('jquery'), false, true);
446
                wp_enqueue_script('at-timepicker', 'https://github.com/trentrichardson/jQuery-Timepicker-Addon/raw/master/jquery-ui-timepicker-addon.js', array('tmc-jquery-ui'), false, true);
447
448
            }
449
450
        }
451
452
        /**
453
         * Add Meta Box for multiple post types.
454
         *
455
         * @since 1.0
456
         * @access public
457
         */
458
        public function add()
459
        {
460
461
            // Loop through array
462
            if (!empty($this->_meta_box['pages'])) {
463
                foreach ($this->_meta_box['pages'] as $page) {
464
                    //add fields to edit form
465
                    add_action($page . '_edit_form_fields', array(&$this, 'show_edit_form'));
466
                    //add fields to add new form
467
                    add_action($page . '_add_form_fields', array(&$this, 'show_new_form'));
468
                    // this saves the edit fields
469
                    add_action('edited_' . $page, array(&$this, 'save'), 10, 2);
470
                    // this saves the add fields
471
                    add_action('created_' . $page, array(&$this, 'save'), 10, 2);
472
                }
473
            }
474
475
        }
476
477
        /**
478
         * Callback function to show fields on add new taxonomy term form.
479
         *
480
         * @since 1.0
481
         * @access public
482
         */
483
        public function show_new_form($term_id)
484
        {
485
            $this->_form_type = 'new';
486
            $this->show($term_id);
487
        }
488
489
        /**
490
         * Callback function to show fields on term edit form.
491
         *
492
         * @since 1.0
493
         * @access public
494
         */
495
        public function show_edit_form($term_id)
496
        {
497
            $this->_form_type = 'edit';
498
            $this->show($term_id);
499
        }
500
501
502
        /**
503
         * Callback function to show fields in meta box.
504
         *
505
         * @since 1.0
506
         * @access public
507
         */
508
        public function show($term_id)
509
        {
510
511
            wp_nonce_field(basename(__FILE__), 'tax_meta_class_nonce');
512
513
            foreach ($this->_fields as $field) {
514
                $meta = $this->get_tax_meta($term_id, $field['id'], !$field['multiple']);
515
                $meta = ($meta !== '') ? $meta : $field['std'];
516 View Code Duplication
                if ('image' != $field['type'] && $field['type'] != 'repeater')
517
                    $meta = is_array($meta) ? array_map('esc_attr', $meta) : esc_attr($meta);
518
519
                if ($field['validate_func']) {
520
                    echo '<tr class="form-field form-required ' . $field['style'] . '">';
521
                } else {
522
                    echo '<tr class="form-field ' . $field['style'] . '">';
523
                }
524
525
                // Call Separated methods for displaying each type of field.
526
                call_user_func(array(&$this, 'show_field_' . $field['type']), $field, $meta);
527
                echo '</tr>';
528
            }
529
            echo '</table>';
530
        }
531
532
        /**
533
         * Show Repeater Fields.
534
         *
535
         * @param string $field
536
         * @param string $meta
537
         * @since 1.0
538
         * @access public
539
         */
540
        public function show_field_repeater($field, $meta)
541
        {
542
            // Get Plugin Path
543
            $plugin_path = $this->SelfPath;
544
            $this->show_field_begin($field, $meta);
545
            echo "<div class='at-repeat' id='{$field['id']}'>";
546
547
            $c = 0;
548
549
            if (count($meta) > 0 && is_array($meta)) {
550
                foreach ($meta as $me) {
551
                    //for labling toggles
552
                    $mmm = $me[$field['fields'][0]['id']];
553
                    echo '<div class="at-repater-block">' . $mmm . '<br/><table class="repeater-table" style="display: none;">';
554
                    if ($field['inline']) {
555
                        echo '<tr class="at-inline" VALIGN="top">';
556
                    }
557
                    foreach ($field['fields'] as $f) {
0 ignored issues
show
Bug introduced by
The expression $field['fields'] of type string is not traversable.
Loading history...
558
                        //reset var $id for repeater
559
                        $id = '';
0 ignored issues
show
Unused Code introduced by
$id 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...
560
                        $id = $field['id'] . '[' . $c . '][' . $f['id'] . ']';
561
                        $m = $me[$f['id']];
562
                        $m = ($m !== '') ? $m : $f['std'];
563 View Code Duplication
                        if ('image' != $f['type'] && $f['type'] != 'repeater')
564
                            $m = is_array($m) ? array_map('esc_attr', $m) : esc_attr($m);
565
                        //set new id for field in array format
566
                        $f['id'] = $id;
567
                        if (!$field['inline']) {
568
                            echo '<tr>';
569
                        }
570
                        call_user_func(array(&$this, 'show_field_' . $f['type']), $f, $m);
571
                        if (!$field['inline']) {
572
                            echo '</tr>';
573
                        }
574
                    }
575
                    if ($field['inline']) {
576
                        echo '</tr>';
577
                    }
578
                    echo '</table>
579
				<span class="at-re-toggle"><img src="';
580
                    if ($this->_Local_images) {
581
                        echo $plugin_path . '/images/edit.png';
582
                    } else {
583
                        echo 'http://i.imgur.com/ka0E2.png';
584
                    }
585
                    echo '" alt="Edit" title="Edit"/></span> 
586
				<img src="';
587
                    if ($this->_Local_images) {
588
                        echo $plugin_path . '/images/remove.png';
589
                    } else {
590
                        echo 'http://i.imgur.com/g8Duj.png';
591
                    }
592
                    echo '" alt="' . __('Remove', 'geodirectory') . '" title="' . __('Remove', 'geodirectory') . '" id="remove-' . $field['id'] . '"></div>';
593
                    $c = $c + 1;
594
595
                }
596
                $this->show_field_end($field, $meta);
0 ignored issues
show
Documentation introduced by
$meta is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
597
            }
598
599
            echo '<img src="';
600
            if ($this->_Local_images) {
601
                echo $plugin_path . '/images/add.png';
602
            } else {
603
                echo 'http://i.imgur.com/w5Tuc.png';
604
            }
605
            echo '" alt="' . __('Add', 'geodirectory') . '" title="' . __('Add', 'geodirectory') . '" id="add-' . $field['id'] . '"><br/></div>';
606
607
            //create all fields once more for js function and catch with object buffer
608
            ob_start();
609
            echo '<div class="at-repater-block"><table class="repeater-table">';
610
            if ($field['inline']) {
611
                echo '<tr class="at-inline" VALIGN="top">';
612
            }
613
            foreach ($field['fields'] as $f) {
0 ignored issues
show
Bug introduced by
The expression $field['fields'] of type string is not traversable.
Loading history...
614
                //reset var $id for repeater
615
                $id = '';
0 ignored issues
show
Unused Code introduced by
$id 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...
616
                $id = $field['id'] . '[CurrentCounter][' . $f['id'] . ']';
617
                $f['id'] = $id;
618
                if (!$field['inline']) {
619
                    echo '<tr>';
620
                }
621
                call_user_func(array(&$this, 'show_field_' . $f['type']), $f, '');
622
                if (!$field['inline']) {
623
                    echo '</tr>';
624
                }
625
            }
626
            if ($field['inline']) {
627
                echo '</tr>';
628
            }
629
            echo '</table><img src="';
630
            if ($this->_Local_images) {
631
                echo $plugin_path . '/images/remove.png';
632
            } else {
633
                echo 'http://i.imgur.com/g8Duj.png';
634
            }
635
            echo '" alt="' . __('Remove', 'geodirectory') . '" title="' . __('Remove', 'geodirectory') . '" id="remove-' . $field['id'] . '"></div>';
636
            $counter = 'countadd_' . $field['id'];
637
            $js_code = ob_get_clean();
638
            $js_code = str_replace("'", "\"", $js_code);
639
            $js_code = str_replace("CurrentCounter", "' + " . $counter . " + '", $js_code);
640
            echo '<script>
641
				jQuery(document).ready(function() {
642
					var ' . $counter . ' = ' . $c . ';
643
					jQuery("#add-' . $field['id'] . '").live(\'click\', function() {
644
						' . $counter . ' = ' . $counter . ' + 1;
645
						jQuery(this).before(\'' . $js_code . '\');						
646
						update_repeater_fields();
647
					});
648
        			jQuery("#remove-' . $field['id'] . '").live(\'click\', function() {
649
            			jQuery(this).parent().remove();
650
        			});
651
    			});
652
    		</script>';
653
            echo '<br/><style>
654
.at-inline{line-height: 1 !important;}
655
.at-inline .at-field{border: 0px !important;}
656
.at-inline .at-label{margin: 0 0 1px !important;}
657
.at-inline .at-text{width: 70px;}
658
.at-inline .at-textarea{width: 100px; height: 75px;}
659
.at-repater-block{background-color: #FFFFFF;border: 1px solid;margin: 2px;}
660
</style>';
661
            $this->show_field_end($field, $meta);
0 ignored issues
show
Bug introduced by
It seems like $meta can also be of type array; however, Geodir_Tax_Meta_Class::show_field_end() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
662
        }
663
664
        /**
665
         * Begin Field.
666
         *
667
         * @param string $field
668
         * @param string $meta
669
         * @since 1.0
670
         * @access public
671
         */
672
        public function show_field_begin($field, $meta)
0 ignored issues
show
Unused Code introduced by
The parameter $meta is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
673
        {
674
            if (isset($field['group'])) {
675
                if ($field['group'] == "start") {
676
                    echo "<td class='at-field'>";
677
                }
678 View Code Duplication
            } else {
679
                if ($this->_form_type == 'edit') {
680
                    echo '<th valign="top" scope="row">';
681
                } else {
682
                    if ($field['validate_func']) {
683
                        echo '<td><div class="form-field form-required">';
684
                    } else {
685
                        echo '<td><div class="form-field">';
686
                    }
687
                }
688
            }
689
            if ($field['name'] != '' || $field['name'] != FALSE) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $field['name'] of type string to the boolean FALSE. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
690
                //echo "<div class='at-label'>";
691
                echo "<label for='{$field['id']}'>{$field['name']}</label>";
692
                //echo "</div>";
693
            }
694
            if ($this->_form_type == 'edit') {
695
                echo '</th><td>';
696
            }
697
        }
698
699
        /**
700
         * End Field.
701
         *
702
         * @param string $field
703
         * @param string $meta
704
         * @since 1.0
705
         * @access public
706
         */
707
        public function show_field_end($field, $meta = NULL, $group = false)
0 ignored issues
show
Unused Code introduced by
The parameter $meta is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
708
        {
709
            if (isset($field['group'])) {
710
                if ($group == 'end') {
711
                    if ($field['desc'] != '') {
712
                        echo "<p class='desc-field'>{$field['desc']}</p></td>";
713
                    } else {
714
                        echo "</td>";
715
                    }
716
                } else {
717
                    if ($field['desc'] != '') {
718
                        echo "<p class='desc-field'>{$field['desc']}</p><br/>";
719
                    } else {
720
                        echo '<br/>';
721
                    }
722
                }
723 View Code Duplication
            } else {
724
                if ($field['desc'] != '') {
725
                    echo "<p class='desc-field'>{$field['desc']}</p>";
726
                }
727
                if ($this->_form_type == 'edit') {
728
                    echo '</td>';
729
                } else {
730
                    echo '</td></div>';
731
                }
732
            }
733
        }
734
735
        /**
736
         * Show Field Text.
737
         *
738
         * @param string $field
739
         * @param string $meta
740
         * @since 1.0
741
         * @access public
742
         */
743
        public function show_field_text($field, $meta)
744
        {
745
            $this->show_field_begin($field, $meta);
746
            echo "<input type='text' class='at-text' name='{$field['id']}' id='{$field['id']}' value='{$meta}' size='30' />";
747
            $this->show_field_end($field, $meta);
748
        }
749
750
        /**
751
         * Show Field hidden.
752
         *
753
         * @param string $field
754
         * @param string|mixed $meta
755
         * @since 0.1.3
756
         * @access public
757
         */
758
        public function show_field_hidden($field, $meta)
759
        {
760
            //$this->show_field_begin( $field, $meta );
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...
761
            echo "<input type='hidden' class='at-text' name='{$field['id']}' id='{$field['id']}' value='{$meta}'/>";
762
            //$this->show_field_end( $field, $meta );
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...
763
        }
764
765
        /**
766
         * Show Field Paragraph.
767
         *
768
         * @param string $field
769
         * @since 0.1.3
770
         * @access public
771
         */
772
        public function show_field_paragraph($field)
773
        {
774
            //$this->show_field_begin( $field, $meta );
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...
775
            echo '<p>' . $field['value'] . '</p>';
776
            //$this->show_field_end( $field, $meta );
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...
777
        }
778
779
        /**
780
         * Show Field Textarea.
781
         *
782
         * @param string $field
783
         * @param string $meta
784
         * @since 1.0
785
         * @access public
786
         */
787
        public function show_field_textarea($field, $meta)
788
        {
789
            $this->show_field_begin($field, $meta);
790
            echo "<textarea class='at-textarea large-text' name='{$field['id']}' id='{$field['id']}' cols='60' rows='10'>{$meta}</textarea>";
791
            $this->show_field_end($field, $meta);
792
        }
793
794
        /**
795
         * Show Field Select.
796
         *
797
         * @param string $field
798
         * @param string $meta
799
         * @since 1.0
800
         * @access public
801
         */
802
        public function show_field_select($field, $meta)
803
        {
804
805
            if (!is_array($meta))
806
                $meta = (array)$meta;
807
808
            $this->show_field_begin($field, $meta);
0 ignored issues
show
Documentation introduced by
$meta is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
809
            echo "<select class='at-select' name='{$field['id']}" . ($field['multiple'] ? "[]' id='{$field['id']}' multiple='multiple'" : "'") . ">";
810
            foreach ($field['options'] as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $field['options'] of type string is not traversable.
Loading history...
811
                echo "<option value='{$key}'" . selected(in_array($key, $meta), true, false) . ">{$value}</option>";
812
            }
813
            echo "</select>";
814
            $this->show_field_end($field, $meta);
0 ignored issues
show
Documentation introduced by
$meta is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
815
816
        }
817
818
        /**
819
         * Show Radio Field.
820
         *
821
         * @param string $field
822
         * @param string $meta
823
         * @since 1.0
824
         * @access public
825
         */
826
        public function show_field_radio($field, $meta)
827
        {
828
829
            if (!is_array($meta))
830
                $meta = (array)$meta;
831
832
            $this->show_field_begin($field, $meta);
0 ignored issues
show
Documentation introduced by
$meta is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
833 View Code Duplication
            foreach ($field['options'] as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $field['options'] of type string is not traversable.
Loading history...
834
                echo "<input type='radio' class='at-radio' name='{$field['id']}' value='{$key}'" . checked(in_array($key, $meta), true, false) . " /> <span class='at-radio-label'>{$value}</span>";
835
            }
836
            $this->show_field_end($field, $meta);
0 ignored issues
show
Documentation introduced by
$meta is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
837
        }
838
839
        /**
840
         * Show Checkbox Field.
841
         *
842
         * @param string $field
843
         * @param string $meta
844
         * @since 1.0
845
         * @access public
846
         */
847
        public function show_field_checkbox($field, $meta)
848
        {
849
850
            $this->show_field_begin($field, $meta);
851
            echo "<input type='checkbox' class='rw-checkbox' name='{$field['id']}' id='{$field['id']}'" . checked(!empty($meta), true, false) . " /> {$field['desc']}";
852
            $this->show_field_end($field, $meta);
853
        }
854
855
        /**
856
         * Show Wysiwig Field.
857
         *
858
         * @param string $field
859
         * @param string $meta
860
         * @since 1.0
861
         * @access public
862
         */
863
        public function show_field_wysiwyg($field, $meta)
864
        {
865
            $this->show_field_begin($field, $meta);
866
            // Add TinyMCE script for WP version < 3.3
867
            global $wp_version;
868
869
            if (version_compare($wp_version, '3.2.1') < 1) {
870
                echo "<textarea class='at-wysiwyg theEditor large-text' name='{$field['id']}' id='{$field['id']}' cols='60' rows='10'>{$meta}</textarea>";
871
            } else {
872
                // Use new wp_editor() since WP 3.3
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% 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...
873
                wp_editor(stripslashes(html_entity_decode($meta)), $field['id'], array('editor_class' => 'at-wysiwyg'));
874
            }
875
            $this->show_field_end($field, $meta);
876
        }
877
878
        /**
879
         * Show File Field.
880
         *
881
         * @global object $post The current post object.
882
         * @param string $field
883
         * @param string $meta
884
         * @since 1.0
885
         * @access public
886
         */
887
        public function show_field_file($field, $meta)
888
        {
889
890
            global $post;
891
892
            if (!is_array($meta))
893
                $meta = (array)$meta;
894
895
            $this->show_field_begin($field, $meta);
0 ignored issues
show
Documentation introduced by
$meta is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
896
            echo "{$field['desc']}<br />";
897
898
            if (!empty($meta)) {
899
                $nonce = wp_create_nonce('at_ajax_delete');
900
                echo '<div style="margin-bottom: 10px"><strong>' . __('Uploaded files', 'geodirectory') . '</strong></div>';
901
                echo '<ol class="at-upload">';
902
                foreach ($meta as $att) {
903
                    // if (wp_attachment_is_image($att)) continue; // what's image uploader for?
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
904
                    echo "<li>" . wp_get_attachment_link($att, '', false, false, ' ') . " (<a class='at-delete-file' href='#' rel='{$nonce}|{$post->ID}|{$field['id']}|{$att}'>" . __('Remove', 'geodirectory') . "</a>)</li>";
905
                }
906
                echo '</ol>';
907
            }
908
909
            // show form upload
910
911
            echo "<div class='at-file-upload-label'>";
912
            echo "<strong>" . __('Upload new files', 'geodirectory') . "</strong>";
913
            echo "</div>";
914
            echo "<div class='new-files'>";
915
            echo "<div class='file-input'>";
916
            echo "<input type='file' name='{$field['id']}[]' />";
917
            echo "</div><!-- End .file-input -->";
918
            echo "<a class='at-add-file button' href='#'>" . __('Add more files', 'geodirectory') . "</a>";
919
            echo "</div><!-- End .new-files -->";
920
            echo "</td>";
921
            $this->show_field_end($field, $meta);
0 ignored issues
show
Documentation introduced by
$meta is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
922
        }
923
924
        /**
925
         * Show Image Field.
926
         *
927
         * @param array $field
928
         * @param array $meta
929
         * @since 1.0
930
         * @access public
931
         */
932
        public function show_field_image($field, $meta)
933
        {
934
            $this->show_field_begin($field, $meta);
0 ignored issues
show
Documentation introduced by
$field is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$meta is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
935
            $html = wp_nonce_field("at-delete-mupload_{$field['id']}", "nonce-delete-mupload_" . $field['id'], false, false);
936
            if (is_array($meta)) {
937
                if (isset($meta[0]) && is_array($meta[0]))
938
                    $meta = $meta[0];
939
            }
940
941
            $uploads = wp_upload_dir();
0 ignored issues
show
Unused Code introduced by
$uploads 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...
942
            if (is_array($meta) && isset($meta['src']) && $meta['src'] != '') {
943
                $file_info = pathinfo($meta['src']);
944
945 View Code Duplication
                if ($file_info['dirname'] != '.' && $file_info['dirname'] != '..')
946
                    $sub_dir = $file_info['dirname'];
947
948
                $uploads = wp_upload_dir(trim($sub_dir, '/')); // Array of key => value pairs
0 ignored issues
show
Bug introduced by
The variable $sub_dir does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
949
                $uploads_baseurl = $uploads['baseurl'];
950
                $uploads_path = $uploads['path'];
0 ignored issues
show
Unused Code introduced by
$uploads_path 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...
951
952
                $file_name = $file_info['basename'];
953
954 View Code Duplication
                if (strpos($sub_dir, 'https://') !== false) {
955
                    $uploads['baseurl'] = str_replace('http://', 'https://', $uploads['baseurl']);
956
                } else {
957
                    $uploads['baseurl'] = str_replace('https://', 'http://', $uploads['baseurl']);
958
                }
959
                $sub_dir = str_replace($uploads['baseurl'], '', $sub_dir);
960
961
                $uploads_url = $uploads_baseurl . $sub_dir;
962
963
                $meta['src'] = $uploads_url . '/' . $file_name;
964
965
                $html .= "<span class='mupload_img_holder'><img src='" . $meta['src'] . "' style='max-height: 150px;max-width: 150px;' /></span>";
966
                $html .= "<input type='hidden' name='" . $field['id'] . "[id]' id='" . $field['id'] . "[id]' value='" . $meta['id'] . "' />";
967
                $html .= "<input type='hidden' class='" . $field['id'] . "[src]' name='" . $field['id'] . "[src]' id='" . $field['id'] . "[src]' value='" . $meta['src'] . "' />";
968
                $html .= "<input class='at-delete_image_button' type='button' rel='" . $field['id'] . "' value='" . __('Remove Image', 'geodirectory') . "' />";
969
            } else {
970
                $html .= "<span class='mupload_img_holder'></span>";
971
                $html .= "<input type='hidden' name='" . $field['id'] . "[id]' id='" . $field['id'] . "[id]' value='' />";
972
                $html .= "<input class='" . $field['id'] . "[src]' style='position:absolute;left:-500px;width:50px;' type='text' name='" . $field['id'] . "[src]' id='" . $field['id'] . "[src]' value='' />";
973
                $html .= "<input class='at-upload_image_button' type='button' rel='" . $field['id'] . "' value='" . __('Upload Image', 'geodirectory') . "' />";
974
            }
975
            echo $html;
976
            $this->show_field_end($field, $meta);
0 ignored issues
show
Documentation introduced by
$field is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$meta is of type array<string,?,{"src":"?"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
977
        }
978
979
        /**
980
         * Show Color Field.
981
         *
982
         * @param string $field
983
         * @param string $meta
984
         * @since 1.0
985
         * @access public
986
         */
987
        public function show_field_color($field, $meta)
988
        {
989
990
            if (empty($meta))
991
                $meta = '#';
992
993
            $this->show_field_begin($field, $meta);
994
995
            echo "<input class='at-color' type='text' name='{$field['id']}' id='{$field['id']}' value='{$meta}' size='8' />";
996
            //	echo "<a href='#' class='at-color-select button' rel='{$field['id']}'>" . __( 'Select a color' ) . "</a>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% 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...
997
            echo "<input type='button' class='at-color-select button' rel='{$field['id']}' value='" . __('Select a color', 'geodirectory') . "'/>";
998
            echo "<div style='display:none' class='at-color-picker' rel='{$field['id']}'></div>";
999
            $this->show_field_end($field, $meta);
1000
1001
        }
1002
1003
        /**
1004
         * Show Checkbox List Field
1005
         *
1006
         * @param string $field
1007
         * @param string $meta
1008
         * @since 1.0
1009
         * @access public
1010
         */
1011
        public function show_field_checkbox_list($field, $meta)
1012
        {
1013
1014
            if (!is_array($meta))
1015
                $meta = (array)$meta;
1016
1017
            $this->show_field_begin($field, $meta);
0 ignored issues
show
Documentation introduced by
$meta is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1018
1019
            $html = array();
1020
1021 View Code Duplication
            foreach ($field['options'] as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $field['options'] of type string is not traversable.
Loading history...
1022
                $html[] = "<input type='checkbox' class='at-checkbox_list' name='{$field['id']}[]' value='{$key}'" . checked(in_array($key, $meta), true, false) . " /> {$value}";
1023
            }
1024
1025
            echo implode('<br />', $html);
1026
1027
            $this->show_field_end($field, $meta);
0 ignored issues
show
Documentation introduced by
$meta is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1028
1029
        }
1030
1031
        /**
1032
         * Show Date Field.
1033
         *
1034
         * @param string $field
1035
         * @param string $meta
1036
         * @since 1.0
1037
         * @access public
1038
         */
1039
        public function show_field_date($field, $meta)
1040
        {
1041
            $this->show_field_begin($field, $meta);
1042
            echo "<input type='text' class='at-date' name='{$field['id']}' id='{$field['id']}' rel='{$field['format']}' value='{$meta}' size='30' />";
1043
            $this->show_field_end($field, $meta);
1044
        }
1045
1046
        /**
1047
         * Show time field.
1048
         *
1049
         * @param string $field
1050
         * @param string $meta
1051
         * @since 1.0
1052
         * @access public
1053
         */
1054
        public function show_field_time($field, $meta)
1055
        {
1056
            $this->show_field_begin($field, $meta);
1057
            echo "<input type='text' class='at-time' name='{$field['id']}' id='{$field['id']}' rel='{$field['format']}' value='{$meta}' size='30' />";
1058
            $this->show_field_end($field, $meta);
1059
        }
1060
1061
        /**
1062
         * Show Posts field.
1063
         * used creating a posts/pages/custom types checkboxlist or a select dropdown
1064
         *
1065
         * @global object $post The current post object.
1066
         * @param string $field
1067
         * @param string $meta
1068
         * @since 1.0
1069
         * @access public
1070
         */
1071 View Code Duplication
        public function show_field_posts($field, $meta)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1072
        {
1073
            global $post;
1074
1075
            if (!is_array($meta)) $meta = (array)$meta;
1076
            $this->show_field_begin($field, $meta);
0 ignored issues
show
Documentation introduced by
$meta is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1077
            $options = $field['options'];
1078
            $posts = get_posts($options['args']);
1079
1080
            // checkbox_list
1081
            if ('checkbox_list' == $options['type']) {
1082
                foreach ($posts as $p) {
1083
                    echo "<input type='checkbox' name='{$field['id']}[]' value='$p->ID'" . checked(in_array($p->ID, $meta), true, false) . " /> $p->post_title<br/>";
1084
                }
1085
            } // select
1086
            else {
1087
                echo "<select name='{$field['id']}" . ($field['multiple'] ? "[]' multiple='multiple' style='height:auto'" : "'") . ">";
1088
                foreach ($posts as $p) {
1089
                    echo "<option value='$p->ID'" . selected(in_array($p->ID, $meta), true, false) . ">$p->post_title</option>";
1090
                }
1091
                echo "</select>";
1092
            }
1093
1094
            $this->show_field_end($field, $meta);
0 ignored issues
show
Documentation introduced by
$meta is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1095
        }
1096
1097
        /**
1098
         * Show Taxonomy field.
1099
         * used creating a category/tags/custom taxonomy checkboxlist or a select dropdown
1100
         *
1101
         * @global object $post The current post object.
1102
         * @param string $field
1103
         * @param string $meta
1104
         * @since 1.0
1105
         * @access public
1106
         *
1107
         * @uses get_terms()
1108
         */
1109 View Code Duplication
        public function show_field_taxonomy($field, $meta)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1110
        {
1111
            global $post;
1112
1113
            if (!is_array($meta)) $meta = (array)$meta;
1114
            $this->show_field_begin($field, $meta);
0 ignored issues
show
Documentation introduced by
$meta is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1115
            $options = $field['options'];
1116
            $terms = get_terms($options['taxonomy'], $options['args']);
1117
1118
            // checkbox_list
1119
            if ('checkbox_list' == $options['type']) {
1120
                foreach ($terms as $term) {
1121
                    echo "<input type='checkbox' name='{$field['id']}[]' value='$term->slug'" . checked(in_array($term->slug, $meta), true, false) . " /> $term->name<br/>";
1122
                }
1123
            } // select
1124
            else {
1125
                echo "<select name='{$field['id']}" . ($field['multiple'] ? "[]' multiple='multiple' style='height:auto'" : "'") . ">";
1126
                foreach ($terms as $term) {
1127
                    echo "<option value='$term->slug'" . selected(in_array($term->slug, $meta), true, false) . ">$term->name</option>";
1128
                }
1129
                echo "</select>";
1130
            }
1131
1132
            $this->show_field_end($field, $meta);
0 ignored issues
show
Documentation introduced by
$meta is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1133
        }
1134
1135
        /**
1136
         * Save Data from Metabox
1137
         *
1138
         * @param string $term_id The term ID.
1139
         * @since 1.0
1140
         * @access public
1141
         * @return string
1142
         */
1143
        public function save($term_id)
1144
        {
1145
1146
            $taxnow = '';
1147
            if (isset($_POST['taxonomy']))
1148
                $taxnow = $_POST['taxonomy'];
1149
1150
            if (!isset($term_id)                                                        // Check Revision
1151
                || (!in_array($taxnow, $this->_meta_box['pages']))                            // Check if current taxonomy type is supported.
1152
                || (!check_admin_referer(basename(__FILE__), 'tax_meta_class_nonce'))        // Check nonce - Security
1153
                || (!current_user_can('manage_categories'))
1154
            )                                // Check permission
1155
            {
1156
                return $term_id;
1157
            }
1158
1159
1160
            foreach ($this->_fields as $field) {
1161
1162
                $name = $field['id'];
1163
                $type = $field['type'];
1164
                $old = $this->get_tax_meta($term_id, $name, !$field['multiple']);
1165
                $new = (isset($_POST[$name])) ? $_POST[$name] : (($field['multiple']) ? array() : '');
1166
1167
                // Validate meta value
1168
                if (class_exists('Tax_Meta_Validate') && method_exists('Tax_Meta_Validate', $field['validate_func'])) {
1169
                    $new = call_user_func(array('Tax_Meta_Validate', $field['validate_func']), $new);
1170
                }
1171
1172
1173
                if ($name == 'ct_cat_icon') {
1174
1175
                    $upload_dir = wp_upload_dir();
1176
1177
                    $image_name_arr = explode('/', $new['src']);
0 ignored issues
show
Unused Code introduced by
$image_name_arr 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...
1178
                    //$old_filename = end($image_name_arr);
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
1179
                    //$img_name_arr = explode('.',$old_filename);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
1180
1181
                    //$old_filename = $upload_dir['path'].'/'.$old_filename;
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
1182
1183
                    $new_filename = $upload_dir['path'] . '/' . 'cat_icon_' . $term_id . '.png';
0 ignored issues
show
Unused Code introduced by
$new_filename 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...
1184
1185
                    /*rename($old_filename, $new_filename);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
1186
				
1187
				//subdir
1188
				$new['src'] = $upload_dir['url'].'/'.'cat_icon_'.$term_id.'.png';
1189
				
1190
				update_attached_file( $new['id'], $new['src'] );*/
1191
1192
1193
                    /*	
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% 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...
1194
		
1195
		$new['src'] = $upload_dir['url'].'/'.'cat_icon_'.$term_id.'.png';
1196
		
1197
		$filename = $new_filename;
1198
		
1199
		
1200
		$filetype = wp_check_filetype( basename( $filename ), null );
1201
		
1202
	
1203
		$wp_upload_dir = wp_upload_dir();
1204
		
1205
		
1206
		$attachment = array(
1207
			'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
1208
			'post_mime_type' => $filetype['type'],
1209
			'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
1210
			'post_content'   => '',
1211
			'post_status'    => 'inherit'
1212
		);
1213
		
1214
	
1215
		$attach_id = wp_insert_attachment( $attachment, $filename);*/
1216
1217
1218
                }
1219
1220
1221
                //skip on Paragraph field
1222
                if ($type != "paragraph") {
1223
1224
                    // Call defined method to save meta value, if there's no methods, call common one.
1225
                    $save_func = 'save_field_' . $type;
1226
                    if (method_exists($this, $save_func)) {
1227
                        call_user_func(array(&$this, 'save_field_' . $type), $term_id, $field, $old, $new);
1228
                    } else {
1229
                        $this->save_field($term_id, $field, $old, $new);
1230
                    }
1231
                }
1232
1233
            } // End foreach
1234
1235
        }
1236
1237
        /**
1238
         * Common function for saving fields.
1239
         *
1240
         * @param string $term_id The term ID.
1241
         * @param string $field
1242
         * @param string $old
1243
         * @param string|mixed $new
1244
         * @since 1.0
1245
         * @access public
1246
         */
1247
        public function save_field($term_id, $field, $old, $new)
0 ignored issues
show
Unused Code introduced by
The parameter $old is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1248
        {
1249
            $name = $field['id'];
1250
            $this->delete_tax_meta($term_id, $name);
1251
            if ($new === '' || $new === array())
1252
                return;
1253
1254
            $this->update_tax_meta($term_id, $name, $new);
1255
        }
1256
1257
        /**
1258
         * function for saving image field.
1259
         *
1260
         * @param string $term_id The term ID.
1261
         * @param string $field
1262
         * @param string $old
1263
         * @param string|mixed $new
1264
         * @since 1.0
1265
         * @access public
1266
         */
1267
        public function save_field_image($term_id, $field, $old, $new)
0 ignored issues
show
Unused Code introduced by
The parameter $old is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1268
        {
1269
            $name = $field['id'];
1270
1271
            $this->delete_tax_meta($term_id, $name);
1272
            if ($new === '' || $new === array() || $new['id'] == '' || $new['src'] == '')
1273
                return;
1274
1275
            $this->update_tax_meta($term_id, $name, $new);
1276
        }
1277
1278
        /*
1279
	 * Save Wysiwyg Field.
1280
	 *
1281
	 * @param string $term_id The term ID. 
1282
	 * @param string $field 
1283
	 * @param string $old 
1284
	 * @param string $new 
1285
	 * @since 1.0
1286
	 * @access public 
1287
	 */
1288
        public function save_field_wysiwyg($term_id, $field, $old, $new)
1289
        {
1290
            $this->save_field($term_id, $field, $old, $new);
1291
        }
1292
1293
        /**
1294
         * Save repeater Fields.
1295
         *
1296
         * @param string $term_id The term ID.
1297
         * @param string $field
1298
         * @param string|mixed $old
1299
         * @param string|mixed $new
1300
         * @since 1.0
1301
         * @access public
1302
         */
1303
        public function save_field_repeater($term_id, $field, $old, $new)
0 ignored issues
show
Unused Code introduced by
The parameter $old is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1304
        {
1305
            if (is_array($new) && count($new) > 0) {
1306
                foreach ($new as $n) {
1307
                    foreach ($field['fields'] as $f) {
0 ignored issues
show
Bug introduced by
The expression $field['fields'] of type string is not traversable.
Loading history...
1308
                        $type = $f['type'];
1309
                        switch ($type) {
1310
                            case 'wysiwyg':
1311
                                $n[$f['id']] = wpautop($n[$f['id']]);
1312
                                break;
1313
                            case 'file':
1314
                                $n[$f['id']] = $this->save_field_file_repeater($term_id, $f, '', $n[$f['id']]);
1315
                                break;
1316
                            default:
1317
                                break;
1318
                        }
1319
                    }
1320
                    if (!$this->is_array_empty($n))
1321
                        $temp[] = $n;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$temp was never initialized. Although not strictly required by PHP, it is generally a good practice to add $temp = 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...
1322
                }
1323
                if (isset($temp) && count($temp) > 0 && !$this->is_array_empty($temp)) {
1324
                    $this->update_tax_meta($term_id, $field['id'], $temp);
1325
                } else {
1326
                    //	remove old meta if exists
1327
                    delete_post_meta($term_id, $field['id']);
1328
                }
1329
            } else {
1330
                //	remove old meta if exists
1331
                delete_post_meta($term_id, $field['id']);
1332
            }
1333
        }
1334
1335
        /**
1336
         * Save File Field.
1337
         *
1338
         * @param string $term_id The term ID.
1339
         * @param string $field
1340
         * @param string $old
1341
         * @param string $new
1342
         * @since 1.0
1343
         * @access public
1344
         */
1345
        public function save_field_file($term_id, $field, $old, $new)
0 ignored issues
show
Unused Code introduced by
The parameter $old is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $new is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1346
        {
1347
1348
            $name = $field['id'];
1349
            if (empty($_FILES[$name]))
1350
                return;
1351
            $this->fix_file_array($_FILES[$name]);
1352
            foreach ($_FILES[$name] as $position => $fileitem) {
1353
1354
                $file = wp_handle_upload($fileitem, array('test_form' => false));
1355
                if (empty($file['file']))
1356
                    continue;
1357
                $filename = $file['file'];
1358
1359
                $attachment = array(
1360
                    'post_mime_type' => $file['type'],
1361
                    'guid' => $file['url'],
1362
                    'post_parent' => $term_id,
1363
                    'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
1364
                    'post_content' => ''
1365
                );
1366
1367
                $id = wp_insert_attachment($attachment, $filename, $term_id);
1368
1369
                if (!is_wp_error($id)) {
1370
1371
                    wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $filename));
1372
                    add_post_meta($term_id, $name, $id, false);    // save file's url in meta fields
1373
1374
                } // End if
1375
1376
            } // End foreach
1377
1378
        }
1379
1380
        /**
1381
         * Save repeater File Field.
1382
         * @param string $term_id The term ID.
1383
         * @param string $field
1384
         * @param string $old
1385
         * @param string $new
1386
         * @since 1.0
1387
         * @access public
1388
         * @return int|void
1389
         */
1390
        public function save_field_file_repeater($term_id, $field, $old, $new)
0 ignored issues
show
Unused Code introduced by
The parameter $old is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $new is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1391
        {
1392
1393
            $name = $field['id'];
1394
            if (empty($_FILES[$name]))
1395
                return;
1396
            $this->fix_file_array($_FILES[$name]);
1397
            foreach ($_FILES[$name] as $position => $fileitem) {
1398
1399
                $file = wp_handle_upload($fileitem, array('test_form' => false));
1400
                if (empty($file['file']))
1401
                    continue;
1402
                $filename = $file['file'];
1403
1404
                $attachment = array(
1405
                    'post_mime_type' => $file['type'],
1406
                    'guid' => $file['url'],
1407
                    'post_parent' => $term_id,
1408
                    'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
1409
                    'post_content' => ''
1410
                );
1411
1412
                $id = wp_insert_attachment($attachment, $filename);
1413
1414
                if (!is_wp_error($id)) {
1415
1416
                    wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $filename));
1417
                    return $id;    // return file's url in meta fields
1418
                } // End if
1419
            } // End foreach
1420
        }
1421
1422
        /**
1423
         * Add missed values for meta box.
1424
         *
1425
         * @since 1.0
1426
         * @access public
1427
         */
1428
        public function add_missed_values()
1429
        {
1430
1431
            // Default values for meta box
1432
            $this->_meta_box = array_merge(array('context' => 'normal', 'priority' => 'high', 'pages' => array('post')), (array)$this->_meta_box);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge(array('conte...rray) $this->_meta_box) of type array is incompatible with the declared type object of property $_meta_box.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
1433
1434
            if(is_array($this->_fields)) {
1435
                // Default values for fields
1436
                foreach ($this->_fields as &$field) {
1437
                    $multiple = in_array($field['type'], array('checkbox_list', 'file', 'image'));
1438
                    $std = $multiple ? array() : '';
1439
                    $format = 'date' == $field['type'] ? 'yy-mm-dd' : ('time' == $field['type'] ? 'hh:mm' : '');
1440
                    $field = array_merge(array('multiple' => $multiple, 'std' => $std, 'desc' => '', 'format' => $format, 'validate_func' => ''), $field);
1441
                } // End foreach
1442
            }
1443
        }
1444
1445
        /**
1446
         * Check if field with $type exists.
1447
         *
1448
         * @param string $type
1449
         * @since 1.0
1450
         * @access public
1451
         */
1452
        public function has_field($type)
1453
        {
1454
            if(is_array($this->_fields)) {
1455
                foreach ($this->_fields as $field) {
1456
                    if ($type == $field['type'])
1457
                        return true;
1458
                }
1459
            }
1460
            return false;
1461
        }
1462
1463
        /**
1464
         * Check if current page is edit page.
1465
         *
1466
         * @since 1.0
1467
         * @access public
1468
         */
1469
        public function is_edit_page()
1470
        {
1471
            global $pagenow;
1472
            return ($pagenow == 'edit-tags.php' || $pagenow == 'term.php');
1473
        }
1474
1475
        /**
1476
         * Fixes the odd indexing of multiple file uploads.
1477
         *
1478
         * Goes from the format:
1479
         * $_FILES['field']['key']['index']
1480
         * to
1481
         * The More standard and appropriate:
1482
         * $_FILES['field']['index']['key']
1483
         *
1484
         * @param string $files
1485
         * @since 1.0
1486
         * @access public
1487
         */
1488
        public function fix_file_array(&$files)
1489
        {
1490
1491
            $output = array();
1492
1493
            foreach ($files as $key => $list) {
0 ignored issues
show
Bug introduced by
The expression $files of type string is not traversable.
Loading history...
1494
                foreach ($list as $index => $value) {
1495
                    $output[$index][$key] = $value;
1496
                }
1497
            }
1498
1499
            return $files = $output;
1500
1501
        }
1502
1503
        /**
1504
         * Get proper JQuery UI version.
1505
         *
1506
         * Used in order to not conflict with WP Admin Scripts.
1507
         *
1508
         * @since 1.0
1509
         * @access public
1510
         */
1511
        public function get_jqueryui_ver()
1512
        {
1513
1514
            global $wp_version;
1515
1516
            if (version_compare($wp_version, '3.1', '>=')) {
1517
                return '1.8.10';
1518
            }
1519
1520
            return '1.7.3';
1521
1522
        }
1523
1524
        /**
1525
         *  Add Field to meta box (generic function)
1526
         * @author Ohad Raz
1527
         * @since 1.0
1528
         * @access public
1529
         * @param $id string  field id, i.e. the meta key
1530
         * @param $args mixed|array
1531
         */
1532
        public function addField($id, $args)
1533
        {
1534
            $new_field = array('id' => $id, 'std' => '', 'desc' => '', 'style' => '');
1535
            $new_field = array_merge($new_field, $args);
1536
            $this->_fields[] = $new_field;
1537
        }
1538
1539
1540
        /**
1541
         *  Add Text Field to meta box
1542
         * @author Ohad Raz
1543
         * @since 1.0
1544
         * @access public
1545
         * @param $id string  field id, i.e. the meta key
1546
         * @param $args mixed|array
1547
         *    'name' => // field name/label string optional
1548
         *    'desc' => // field description, string optional
1549
         *    'std' => // default value, string optional
1550
         *    'style' =>    // custom style for field, string optional
1551
         *    'validate_func' => // validate function, string optional
1552
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1553
         */
1554 View Code Duplication
        public function addText($id, $args, $repeater = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1555
        {
1556
            $new_field = array('type' => 'text', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => __('Text Field', 'geodirectory'));
1557
            $new_field = array_merge($new_field, $args);
1558
            if (false === $repeater) {
1559
                $this->_fields[] = $new_field;
1560
            } else {
1561
                return $new_field;
1562
            }
1563
        }
1564
1565
        /**
1566
         *  Add Hidden Field to meta box
1567
         * @author Ohad Raz
1568
         * @since 0.1.3
1569
         * @access public
1570
         * @param $id string  field id, i.e. the meta key
1571
         * @param $args mixed|array
1572
         *    'name' => // field name/label string optional
1573
         *    'desc' => // field description, string optional
1574
         *    'std' => // default value, string optional
1575
         *    'style' =>    // custom style for field, string optional
1576
         *    'validate_func' => // validate function, string optional
1577
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1578
         */
1579 View Code Duplication
        public function addHidden($id, $args, $repeater = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1580
        {
1581
            $new_field = array('type' => 'hidden', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => __('Text Field', 'geodirectory'));
1582
            $new_field = array_merge($new_field, $args);
1583
            if (false === $repeater) {
1584
                $this->_fields[] = $new_field;
1585
            } else {
1586
                return $new_field;
1587
            }
1588
        }
1589
1590
        /**
1591
         *  Add Paragraph to meta box
1592
         * @author Ohad Raz
1593
         * @since 0.1.3
1594
         * @access public
1595
         * @param $id string  field id, i.e. the meta key
1596
         * @param $value  paragraph html
1597
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1598
         */
1599
        public function addParagraph($id, $args, $repeater = false)
1600
        {
1601
            $new_field = array('type' => 'paragraph', 'id' => $id, 'value' => '', 'style' => '');
1602
            $new_field = array_merge($new_field, $args);
1603
            if (false === $repeater) {
1604
                $this->_fields[] = $new_field;
1605
            } else {
1606
                return $new_field;
1607
            }
1608
        }
1609
1610
        /**
1611
         *  Add Checkbox Field to meta box
1612
         * @author Ohad Raz
1613
         * @since 1.0
1614
         * @access public
1615
         * @param $id string  field id, i.e. the meta key
1616
         * @param $args mixed|array
1617
         *    'name' => // field name/label string optional
1618
         *    'desc' => // field description, string optional
1619
         *    'std' => // default value, string optional
1620
         *    'validate_func' => // validate function, string optional
1621
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1622
         */
1623 View Code Duplication
        public function addCheckbox($id, $args, $repeater = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1624
        {
1625
            $new_field = array('type' => 'checkbox', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => __('Checkbox Field', 'geodirectory'));
1626
            $new_field = array_merge($new_field, $args);
1627
            if (false === $repeater) {
1628
                $this->_fields[] = $new_field;
1629
            } else {
1630
                return $new_field;
1631
            }
1632
        }
1633
1634
        /**
1635
         *  Add CheckboxList Field to meta box
1636
         * @author Ohad Raz
1637
         * @since 1.0
1638
         * @access public
1639
         * @param $id string  field id, i.e. the meta key
1640
         * @param $options (array)  array of key => value pairs for select options
1641
         * @param $args mixed|array
1642
         *    'name' => // field name/label string optional
1643
         *    'desc' => // field description, string optional
1644
         *    'std' => // default value, string optional
1645
         *    'validate_func' => // validate function, string optional
1646
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1647
         *
1648
         * @return : remember to call: $checkbox_list = $this->get_tax_meta(get_the_ID(), 'meta_name', false);
0 ignored issues
show
Documentation introduced by
The doc-type : could not be parsed: Unknown type name ":" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1649
         *   which means the last param as false to get the values in an array
1650
         */
1651 View Code Duplication
        public function addCheckboxList($id, $options, $args, $repeater = false)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1652
        {
1653
            $new_field = array('type' => 'checkbox_list', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => __('Checkbox List Field', 'geodirectory'));
1654
            $new_field = array_merge($new_field, $args);
1655
            if (false === $repeater) {
1656
                $this->_fields[] = $new_field;
1657
            } else {
1658
                return $new_field;
1659
            }
1660
        }
1661
1662
        /**
1663
         *  Add Textarea Field to meta box
1664
         * @author Ohad Raz
1665
         * @since 1.0
1666
         * @access public
1667
         * @param $id string  field id, i.e. the meta key
1668
         * @param $args mixed|array
1669
         *    'name' => // field name/label string optional
1670
         *    'desc' => // field description, string optional
1671
         *    'std' => // default value, string optional
1672
         *    'style' =>    // custom style for field, string optional
1673
         *    'validate_func' => // validate function, string optional
1674
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1675
         */
1676 View Code Duplication
        public function addTextarea($id, $args, $repeater = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1677
        {
1678
            $new_field = array('type' => 'textarea', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => __('Textarea Field', 'geodirectory'));
1679
            $new_field = array_merge($new_field, $args);
1680
            if (false === $repeater) {
1681
                $this->_fields[] = $new_field;
1682
            } else {
1683
                return $new_field;
1684
            }
1685
        }
1686
1687
        /**
1688
         *  Add Select Field to meta box
1689
         * @author Ohad Raz
1690
         * @since 1.0
1691
         * @access public
1692
         * @param $id string field id, i.e. the meta key
1693
         * @param $options (array)  array of key => value pairs for select options
1694
         * @param $args mixed|array
1695
         *    'name' => // field name/label string optional
1696
         *    'desc' => // field description, string optional
1697
         *    'std' => // default value, (array) optional
1698
         *    'multiple' => // select multiple values, optional. Default is false.
1699
         *    'validate_func' => // validate function, string optional
1700
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1701
         */
1702 View Code Duplication
        public function addSelect($id, $options, $args, $repeater = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1703
        {
1704
            $new_field = array('type' => 'select', 'id' => $id, 'std' => array(), 'desc' => '', 'style' => '', 'name' => __('Select Field', 'geodirectory'), 'multiple' => false, 'options' => $options);
1705
            $new_field = array_merge($new_field, $args);
1706
            if (false === $repeater) {
1707
                $this->_fields[] = $new_field;
1708
            } else {
1709
                return $new_field;
1710
            }
1711
        }
1712
1713
1714
        /**
1715
         *  Add Radio Field to meta box
1716
         * @author Ohad Raz
1717
         * @since 1.0
1718
         * @access public
1719
         * @param $id string field id, i.e. the meta key
1720
         * @param $options (array)  array of key => value pairs for radio options
1721
         * @param $args mixed|array
1722
         *    'name' => // field name/label string optional
1723
         *    'desc' => // field description, string optional
1724
         *    'std' => // default value, string optional
1725
         *    'validate_func' => // validate function, string optional
1726
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1727
         */
1728 View Code Duplication
        public function addRadio($id, $options, $args, $repeater = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1729
        {
1730
            $new_field = array('type' => 'radio', 'id' => $id, 'std' => array(), 'desc' => '', 'style' => '', 'name' => __('Radio Field', 'geodirectory'), 'options' => $options);
1731
            $new_field = array_merge($new_field, $args);
1732
            if (false === $repeater) {
1733
                $this->_fields[] = $new_field;
1734
            } else {
1735
                return $new_field;
1736
            }
1737
        }
1738
1739
        /**
1740
         *  Add Date Field to meta box
1741
         * @author Ohad Raz
1742
         * @since 1.0
1743
         * @access public
1744
         * @param $id string  field id, i.e. the meta key
1745
         * @param $args mixed|array
1746
         *    'name' => // field name/label string optional
1747
         *    'desc' => // field description, string optional
1748
         *    'std' => // default value, string optional
1749
         *    'validate_func' => // validate function, string optional
1750
         *    'format' => // date format, default yy-mm-dd. Optional. Default "'d MM, yy'"  See more formats here: http://goo.gl/Wcwxn
1751
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1752
         */
1753 View Code Duplication
        public function addDate($id, $args, $repeater = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1754
        {
1755
            $new_field = array('type' => 'date', 'id' => $id, 'std' => '', 'desc' => '', 'format' => 'yy-mm-dd', 'name' => __('Date Field', 'geodirectory'));
1756
            $new_field = array_merge($new_field, $args);
1757
            if (false === $repeater) {
1758
                $this->_fields[] = $new_field;
1759
            } else {
1760
                return $new_field;
1761
            }
1762
        }
1763
1764
        /**
1765
         *  Add Time Field to meta box
1766
         * @author Ohad Raz
1767
         * @since 1.0
1768
         * @access public
1769
         * @param $id string- field id, i.e. the meta key
1770
         * @param $args mixed|array
1771
         *    'name' => // field name/label string optional
1772
         *    'desc' => // field description, string optional
1773
         *    'std' => // default value, string optional
1774
         *    'validate_func' => // validate function, string optional
1775
         *    'format' => // time format, default hh:mm. Optional. See more formats here: http://goo.gl/83woX
1776
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1777
         */
1778 View Code Duplication
        public function addTime($id, $args, $repeater = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1779
        {
1780
            $new_field = array('type' => 'time', 'id' => $id, 'std' => '', 'desc' => '', 'format' => 'hh:mm', 'name' => __('Time Field', 'geodirectory'));
1781
            $new_field = array_merge($new_field, $args);
1782
            if (false === $repeater) {
1783
                $this->_fields[] = $new_field;
1784
            } else {
1785
                return $new_field;
1786
            }
1787
        }
1788
1789
        /**
1790
         *  Add Color Field to meta box
1791
         * @author Ohad Raz
1792
         * @since 1.0
1793
         * @access public
1794
         * @param $id string  field id, i.e. the meta key
1795
         * @param $args mixed|array
1796
         *    'name' => // field name/label string optional
1797
         *    'desc' => // field description, string optional
1798
         *    'std' => // default value, string optional
1799
         *    'validate_func' => // validate function, string optional
1800
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1801
         */
1802 View Code Duplication
        public function addColor($id, $args, $repeater = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1803
        {
1804
            $new_field = array('type' => 'color', 'id' => $id, 'std' => '', 'desc' => '', 'name' => __('ColorPicker Field', 'geodirectory'));
1805
            $new_field = array_merge($new_field, $args);
1806
            if (false === $repeater) {
1807
                $this->_fields[] = $new_field;
1808
            } else {
1809
                return $new_field;
1810
            }
1811
        }
1812
1813
        /**
1814
         *  Add Image Field to meta box
1815
         * @author Ohad Raz
1816
         * @since 1.0
1817
         * @access public
1818
         * @param $id string  field id, i.e. the meta key
1819
         * @param $args mixed|array
1820
         *    'name' => // field name/label string optional
1821
         *    'desc' => // field description, string optional
1822
         *    'validate_func' => // validate function, string optional
1823
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1824
         */
1825 View Code Duplication
        public function addImage($id, $args, $repeater = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1826
        {
1827
            $new_field = array('type' => 'image', 'id' => $id, 'desc' => '', 'style' => '', 'name' => __('Image Field', 'geodirectory'));
1828
            $new_field = array_merge($new_field, $args);
1829
1830
            if (false === $repeater) {
1831
                $this->_fields[] = $new_field;
1832
            } else {
1833
                return $new_field;
1834
            }
1835
        }
1836
1837
        /**
1838
         *  Add File Field to meta box
1839
         * @author Ohad Raz
1840
         * @since 1.0
1841
         * @access public
1842
         * @param $id string  field id, i.e. the meta key
1843
         * @param $args mixed|array
1844
         *    'name' => // field name/label string optional
1845
         *    'desc' => // field description, string optional
1846
         *    'validate_func' => // validate function, string optional
1847
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1848
         */
1849 View Code Duplication
        public function addFile($id, $args, $repeater = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1850
        {
1851
            $new_field = array('type' => 'file', 'id' => $id, 'desc' => '', 'style' => '', 'name' => __('File Field', 'geodirectory'));
1852
            $new_field = array_merge($new_field, $args);
1853
            if (false === $repeater) {
1854
                $this->_fields[] = $new_field;
1855
            } else {
1856
                return $new_field;
1857
            }
1858
        }
1859
1860
        /**
1861
         *  Add WYSIWYG Field to meta box
1862
         * @author Ohad Raz
1863
         * @since 1.0
1864
         * @access public
1865
         * @param $id string  field id, i.e. the meta key
1866
         * @param $args mixed|array
1867
         *    'name' => // field name/label string optional
1868
         *    'desc' => // field description, string optional
1869
         *    'std' => // default value, string optional
1870
         *    'style' =>    // custom style for field, string optional Default 'width: 300px; height: 400px'
1871
         *    'validate_func' => // validate function, string optional
1872
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1873
         */
1874 View Code Duplication
        public function addWysiwyg($id, $args, $repeater = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1875
        {
1876
            $new_field = array('type' => 'wysiwyg', 'id' => $id, 'std' => '', 'desc' => '', 'style' => 'width: 300px; height: 400px', 'name' => __('WYSIWYG Editor Field', 'geodirectory'));
1877
            $new_field = array_merge($new_field, $args);
1878
            if (false === $repeater) {
1879
                $this->_fields[] = $new_field;
1880
            } else {
1881
                return $new_field;
1882
            }
1883
        }
1884
1885
        /**
1886
         *  Add Taxonomy Field to meta box
1887
         * @author Ohad Raz
1888
         * @since 1.0
1889
         * @access public
1890
         * @param $id string  field id, i.e. the meta key
1891
         * @param $options mixed|array options of taxonomy field
1892
         *    'taxonomy' =>    // taxonomy name can be category,post_tag or any custom taxonomy default is category
1893
         * 'type' =>  // how to show taxonomy? 'select' (default) or 'checkbox_list'
1894
         * 'args' =>  // arguments to query taxonomy, see http://goo.gl/uAANN default ('hide_empty' => false)
1895
         * @param $args mixed|array
1896
         *    'name' => // field name/label string optional
1897
         *    'desc' => // field description, string optional
1898
         *    'std' => // default value, string optional
1899
         *    'validate_func' => // validate function, string optional
1900
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1901
         */
1902
        public function addTaxonomy($id, $options, $args, $repeater = false)
1903
        {
1904
            $q = array('hide_empty' => 0);
1905
            $tax = 'category';
1906
            $type = 'select';
1907
            $temp = array($tax, $type, $q);
1908
            $options = array_merge($temp, $options);
1909
            $new_field = array('type' => 'taxonomy', 'id' => $id, 'desc' => '', 'name' => __('Taxonomy Field', 'geodirectory'), 'options' => $options);
1910
            $new_field = array_merge($new_field, $args);
1911
            if (false === $repeater) {
1912
                $this->_fields[] = $new_field;
1913
            } else {
1914
                return $new_field;
1915
            }
1916
        }
1917
1918
        /**
1919
         *  Add posts Field to meta box
1920
         * @author Ohad Raz
1921
         * @since 1.0
1922
         * @access public
1923
         * @param $id string  field id, i.e. the meta key
1924
         * @param $options mixed|array options of taxonomy field
1925
         *    'post_type' =>    // post type name, 'post' (default) 'page' or any custom post type
1926
         * 'type' =>  // how to show posts? 'select' (default) or 'checkbox_list'
1927
         * 'args' =>  // arguments to query posts, see http://goo.gl/is0yK default ('posts_per_page' => -1)
1928
         * @param $args mixed|array
1929
         *    'name' => // field name/label string optional
1930
         *    'desc' => // field description, string optional
1931
         *    'std' => // default value, string optional
1932
         *    'validate_func' => // validate function, string optional
1933
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1934
         */
1935
        public function addPosts($id, $options, $args, $repeater = false)
1936
        {
1937
            $q = array('posts_per_page' => -1);
1938
            $temp = array('post_type' => 'post', 'type' => 'select', 'args' => $q);
1939
            $options = array_merge($temp, $options);
1940
            $new_field = array('type' => 'posts', 'id' => $id, 'desc' => '', 'name' => __('Posts Field', 'geodirectory'), 'options' => $options);
1941
            $new_field = array_merge($new_field, $args);
1942
            if (false === $repeater) {
1943
                $this->_fields[] = $new_field;
1944
            } else {
1945
                return $new_field;
1946
            }
1947
        }
1948
1949
        /**
1950
         *  Add repeater Field Block to meta box
1951
         * @author Ohad Raz
1952
         * @since 1.0
1953
         * @access public
1954
         * @param $id string  field id, i.e. the meta key
1955
         * @param $args mixed|array
1956
         *    'name' => // field name/label string optional
1957
         *    'desc' => // field description, string optional
1958
         *    'std' => // default value, string optional
1959
         *    'style' =>    // custom style for field, string optional
1960
         *    'validate_func' => // validate function, string optional
1961
         *    'fields' => //fields to repeater
1962
         */
1963
        public function addRepeaterBlock($id, $args)
1964
        {
1965
            $new_field = array('type' => 'repeater', 'id' => $id, 'name' => __('Reapeater Field', 'geodirectory'), 'fields' => array(), 'inline' => false);
1966
            $new_field = array_merge($new_field, $args);
1967
            $this->_fields[] = $new_field;
1968
        }
1969
1970
1971
        /**
1972
         * Finish Declaration of Meta Box
1973
         * @author Ohad Raz
1974
         * @since 1.0
1975
         * @access public
1976
         */
1977
        public function Finish()
1978
        {
1979
            $this->add_missed_values();
1980
            $this->check_field_upload();
1981
            $this->check_field_color();
1982
            $this->check_field_date();
1983
            $this->check_field_time();
1984
        }
1985
1986
        /**
1987
         * Helper function to check for empty arrays
1988
         * @author Ohad Raz
1989
         * @since 1.0
1990
         * @access public
1991
         * @param $args mixed|array
1992
         */
1993
        public function is_array_empty($array)
1994
        {
1995
            if (!is_array($array))
1996
                return true;
1997
1998
            foreach ($array as $a) {
1999
                if (is_array($a)) {
2000
                    foreach ($a as $sub_a) {
2001
                        if (!empty($sub_a) && $sub_a != '')
2002
                            return false;
2003
                    }
2004
                } else {
2005
                    if (!empty($a) && $a != '')
2006
                        return false;
2007
                }
2008
            }
2009
            return true;
2010
        }
2011
2012
2013
        //get term meta field
2014
        public function get_tax_meta($term_id, $key, $multi = false, $post_type = '')
0 ignored issues
show
Unused Code introduced by
The parameter $multi is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
2015
        {
2016
2017
            if (empty($post_type) && isset($_REQUEST['taxonomy'])) {
2018
                $taxObject = get_taxonomy($_REQUEST['taxonomy']);
2019
                $post_type = $taxObject->object_type[0];
2020
            }
2021
2022
            if($post_type=='post'){$post_type='';}
2023
            if($post_type){$post_type = $post_type.'_';}
2024
2025
            $t_id = (is_object($term_id)) ? $term_id->term_id : $term_id;
2026
2027
            $m = get_option('tax_meta_' . $post_type  . $t_id);
2028
            if (isset($m[$key])) {
2029
                return $m[$key];
2030
            } else {
2031
                return '';
2032
            }
2033
        }
2034
2035
        //delete meta
2036
        public function delete_tax_meta($term_id, $key, $post_type = '')
2037
        {
2038
2039
            if (empty($post_type) && isset($_REQUEST['taxonomy'])) {
2040
                $taxObject = get_taxonomy($_REQUEST['taxonomy']);
2041
                $post_type = $taxObject->object_type[0];
2042
            }
2043
2044
            if($post_type=='post'){$post_type='';}
2045
            if($post_type){$post_type = $post_type.'_';}
2046
2047
            $m = get_option('tax_meta_' . $post_type  . $term_id);
2048
2049
            if (isset($m[$key])) {
2050
                unset($m[$key]);
2051
            }
2052
            update_option('tax_meta_' . $post_type  . $term_id, $m);
2053
        }
2054
2055
        //update meta
2056 View Code Duplication
        public function update_tax_meta($term_id, $key, $value, $post_type = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2057
        {
2058
2059
            if (empty($post_type) && isset($_REQUEST['taxonomy'])) {
2060
                $taxObject = get_taxonomy($_REQUEST['taxonomy']);
2061
                $post_type = $taxObject->object_type[0];
2062
            }
2063
2064
            if($post_type=='post'){$post_type='';}
2065
            if($post_type){$post_type = $post_type.'_';}
2066
2067
            $m = get_option('tax_meta_' . $post_type  . $term_id);
2068
2069
            $m[$key] = $value;
2070
            update_option('tax_meta_' . $post_type  . $term_id, $m);
2071
2072
            /**
2073
             * Called after the tax meta is updated.
2074
             *
2075
             * Used to update things after a GD category is saved.
2076
             *
2077
             * @since 1.0.0
2078
             * @param bool $false False.
2079
             * @param bool $true True.
2080
             * @param int $term_id The term id being updated.
2081
             * @param string $post_type The post type of the cat being updated.
2082
             */
2083
            do_action('gd_tax_meta_updated', false, true, $term_id, $post_type);
2084
        }
2085
2086
2087
    } // End Class
2088
2089
endif; // End Check Class Exists
2090
2091
/*
2092
 * meta functions for easy access:
2093
 */
2094
2095
//get term meta field
2096
if (!function_exists('geodir_get_tax_meta')) {
2097
    function geodir_get_tax_meta($term_id, $key, $multi = false, $post_type = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $multi is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
2098
        if (empty($post_type) && isset($_REQUEST['taxonomy'])) {
2099
            $taxObject = get_taxonomy($_REQUEST['taxonomy']);
2100
            
2101
            if (!empty($taxObject->object_type)) {
2102
                $post_type = $taxObject->object_type[0];
2103
            }
2104
        }
2105
2106
        if ($post_type == 'post') {
2107
            $post_type = '';
2108
        }
2109
        if ($post_type) {
2110
            $post_type = $post_type . '_';
2111
        }
2112
2113
        $t_id = (is_object($term_id)) ? $term_id->term_id : $term_id;
2114
2115
        $m = get_option('tax_meta_' . $post_type  . $t_id);
2116
        if (isset($m[$key])) {
2117
            return $m[$key];
2118
        } else {
2119
            return '';
2120
        }
2121
    }
2122
}
2123
2124
//delete meta
2125
if (!function_exists('geodir_delete_tax_meta')) {
2126
    function geodir_delete_tax_meta($term_id, $key) {
2127
        $taxObject = get_taxonomy($_REQUEST['taxonomy']);
2128
        $post_type = !empty($taxObject->object_type) ? $taxObject->object_type[0] : '';
2129
2130
        if ($post_type == 'post') {
2131
            $post_type = '';
2132
        }
2133
        if ($post_type) {
2134
            $post_type = $post_type . '_';
2135
        }
2136
2137
        $m = get_option('tax_meta_' . $post_type . $term_id);
2138
2139
        if (isset($m[$key])) {
2140
            unset($m[$key]);
2141
        }
2142
        update_option('tax_meta_' . $post_type  . $term_id, $m);
2143
    }
2144
}
2145
2146
//update meta
2147
if (!function_exists('geodir_update_tax_meta')) {
2148 View Code Duplication
    function geodir_update_tax_meta($term_id, $key, $value, $post_type = '') {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2149
        if (empty($post_type) && isset($_REQUEST['taxonomy'])) {
2150
            $taxObject = get_taxonomy($_REQUEST['taxonomy']);
2151
            
2152
            if (!empty($taxObject->object_type)) {
2153
                $post_type = $taxObject->object_type[0];
2154
            }
2155
        }
2156
2157
        if ($post_type == 'post') {
2158
            $post_type = '';
2159
        }
2160
        if ($post_type) {
2161
            $post_type = $post_type . '_';
2162
        }
2163
2164
        $m = get_option('tax_meta_' . $post_type  . $term_id);
2165
2166
        $m[$key] = $value;
2167
        update_option('tax_meta_' . $post_type . $term_id, $m);
2168
2169
        /** This action is documented in geodirectory-functions/cat-meta-functions/Tax-meta-class.php */
2170
        do_action('gd_tax_meta_updated', false, true, $term_id, $post_type);
2171
    }
2172
}