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

Tax_Meta_Class::addImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %
Metric Value
dl 11
loc 11
rs 9.4285
cc 2
eloc 7
nc 2
nop 3
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('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 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', 'http://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, 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
70% 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
944
                $file_info = pathinfo($meta['src']);
945
946
                //print_r($meta);
947
                //print_r($uploads);
948
                //print_r($file_info);
949
950 View Code Duplication
                if ($file_info['dirname'] != '.' && $file_info['dirname'] != '..')
951
                    $sub_dir = $file_info['dirname'];
952
953
                $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...
954
                $uploads_baseurl = $uploads['baseurl'];
955
                $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...
956
957
                $file_name = $file_info['basename'];
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
966
                $html .= "<span class='mupload_img_holder'><img src='" . $meta['src'] . "' style='max-height: 150px;max-width: 150px;' /></span>";
967
                $html .= "<input type='hidden' name='" . $field['id'] . "[id]' id='" . $field['id'] . "[id]' value='" . $meta['id'] . "' />";
968
                $html .= "<input type='hidden' class='" . $field['id'] . "[src]' name='" . $field['id'] . "[src]' id='" . $field['id'] . "[src]' value='" . $meta['src'] . "' />";
969
                $html .= "<input class='at-delete_image_button' type='button' rel='" . $field['id'] . "' value='" . __('Remove Image', 'geodirectory') . "' />";
970
            } else {
971
                $html .= "<span class='mupload_img_holder'></span>";
972
                $html .= "<input type='hidden' name='" . $field['id'] . "[id]' id='" . $field['id'] . "[id]' value='' />";
973
                $html .= "<input class='" . $field['id'] . "[src]' style='position:absolute;left:-500px;width:50px;' type='text' name='" . $field['id'] . "[src]' id='" . $field['id'] . "[src]' value='' />";
974
                $html .= "<input class='at-upload_image_button' type='button' rel='" . $field['id'] . "' value='" . __('Upload Image', 'geodirectory') . "' />";
975
            }
976
            echo $html;
977
            $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...
978
        }
979
980
        /**
981
         * Show Color Field.
982
         *
983
         * @param string $field
984
         * @param string $meta
985
         * @since 1.0
986
         * @access public
987
         */
988
        public function show_field_color($field, $meta)
989
        {
990
991
            if (empty($meta))
992
                $meta = '#';
993
994
            $this->show_field_begin($field, $meta);
995
996
            echo "<input class='at-color' type='text' name='{$field['id']}' id='{$field['id']}' value='{$meta}' size='8' />";
997
            //	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...
998
            echo "<input type='button' class='at-color-select button' rel='{$field['id']}' value='" . __('Select a color', 'geodirectory') . "'/>";
999
            echo "<div style='display:none' class='at-color-picker' rel='{$field['id']}'></div>";
1000
            $this->show_field_end($field, $meta);
1001
1002
        }
1003
1004
        /**
1005
         * Show Checkbox List Field
1006
         *
1007
         * @param string $field
1008
         * @param string $meta
1009
         * @since 1.0
1010
         * @access public
1011
         */
1012
        public function show_field_checkbox_list($field, $meta)
1013
        {
1014
1015
            if (!is_array($meta))
1016
                $meta = (array)$meta;
1017
1018
            $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...
1019
1020
            $html = array();
1021
1022 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...
1023
                $html[] = "<input type='checkbox' class='at-checkbox_list' name='{$field['id']}[]' value='{$key}'" . checked(in_array($key, $meta), true, false) . " /> {$value}";
1024
            }
1025
1026
            echo implode('<br />', $html);
1027
1028
            $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...
1029
1030
        }
1031
1032
        /**
1033
         * Show Date Field.
1034
         *
1035
         * @param string $field
1036
         * @param string $meta
1037
         * @since 1.0
1038
         * @access public
1039
         */
1040
        public function show_field_date($field, $meta)
1041
        {
1042
            $this->show_field_begin($field, $meta);
1043
            echo "<input type='text' class='at-date' name='{$field['id']}' id='{$field['id']}' rel='{$field['format']}' value='{$meta}' size='30' />";
1044
            $this->show_field_end($field, $meta);
1045
        }
1046
1047
        /**
1048
         * Show time field.
1049
         *
1050
         * @param string $field
1051
         * @param string $meta
1052
         * @since 1.0
1053
         * @access public
1054
         */
1055
        public function show_field_time($field, $meta)
1056
        {
1057
            $this->show_field_begin($field, $meta);
1058
            echo "<input type='text' class='at-time' name='{$field['id']}' id='{$field['id']}' rel='{$field['format']}' value='{$meta}' size='30' />";
1059
            $this->show_field_end($field, $meta);
1060
        }
1061
1062
        /**
1063
         * Show Posts field.
1064
         * used creating a posts/pages/custom types checkboxlist or a select dropdown
1065
         *
1066
         * @global object $post The current post object.
1067
         * @param string $field
1068
         * @param string $meta
1069
         * @since 1.0
1070
         * @access public
1071
         */
1072 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...
1073
        {
1074
            global $post;
1075
1076
            if (!is_array($meta)) $meta = (array)$meta;
1077
            $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...
1078
            $options = $field['options'];
1079
            $posts = get_posts($options['args']);
1080
1081
            // checkbox_list
1082
            if ('checkbox_list' == $options['type']) {
1083
                foreach ($posts as $p) {
1084
                    echo "<input type='checkbox' name='{$field['id']}[]' value='$p->ID'" . checked(in_array($p->ID, $meta), true, false) . " /> $p->post_title<br/>";
1085
                }
1086
            } // select
1087
            else {
1088
                echo "<select name='{$field['id']}" . ($field['multiple'] ? "[]' multiple='multiple' style='height:auto'" : "'") . ">";
1089
                foreach ($posts as $p) {
1090
                    echo "<option value='$p->ID'" . selected(in_array($p->ID, $meta), true, false) . ">$p->post_title</option>";
1091
                }
1092
                echo "</select>";
1093
            }
1094
1095
            $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...
1096
        }
1097
1098
        /**
1099
         * Show Taxonomy field.
1100
         * used creating a category/tags/custom taxonomy checkboxlist or a select dropdown
1101
         *
1102
         * @global object $post The current post object.
1103
         * @param string $field
1104
         * @param string $meta
1105
         * @since 1.0
1106
         * @access public
1107
         *
1108
         * @uses get_terms()
1109
         */
1110 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...
1111
        {
1112
            global $post;
1113
1114
            if (!is_array($meta)) $meta = (array)$meta;
1115
            $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...
1116
            $options = $field['options'];
1117
            $terms = get_terms($options['taxonomy'], $options['args']);
1118
1119
            // checkbox_list
1120
            if ('checkbox_list' == $options['type']) {
1121
                foreach ($terms as $term) {
1122
                    echo "<input type='checkbox' name='{$field['id']}[]' value='$term->slug'" . checked(in_array($term->slug, $meta), true, false) . " /> $term->name<br/>";
1123
                }
1124
            } // select
1125
            else {
1126
                echo "<select name='{$field['id']}" . ($field['multiple'] ? "[]' multiple='multiple' style='height:auto'" : "'") . ">";
1127
                foreach ($terms as $term) {
1128
                    echo "<option value='$term->slug'" . selected(in_array($term->slug, $meta), true, false) . ">$term->name</option>";
1129
                }
1130
                echo "</select>";
1131
            }
1132
1133
            $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...
1134
        }
1135
1136
        /**
1137
         * Save Data from Metabox
1138
         *
1139
         * @param string $term_id The term ID.
1140
         * @since 1.0
1141
         * @access public
1142
         * @return string
1143
         */
1144
        public function save($term_id)
1145
        {
1146
1147
            $taxnow = '';
1148
            if (isset($_POST['taxonomy']))
1149
                $taxnow = $_POST['taxonomy'];
1150
1151
            if (!isset($term_id)                                                        // Check Revision
1152
                || (!in_array($taxnow, $this->_meta_box['pages']))                            // Check if current taxonomy type is supported.
1153
                || (!check_admin_referer(basename(__FILE__), 'tax_meta_class_nonce'))        // Check nonce - Security
1154
                || (!current_user_can('manage_categories'))
1155
            )                                // Check permission
1156
            {
1157
                return $term_id;
1158
            }
1159
1160
1161
            foreach ($this->_fields as $field) {
1162
1163
                $name = $field['id'];
1164
                $type = $field['type'];
1165
                $old = $this->get_tax_meta($term_id, $name, !$field['multiple']);
1166
                $new = (isset($_POST[$name])) ? $_POST[$name] : (($field['multiple']) ? array() : '');
1167
1168
                // Validate meta value
1169
                if (class_exists('Tax_Meta_Validate') && method_exists('Tax_Meta_Validate', $field['validate_func'])) {
1170
                    $new = call_user_func(array('Tax_Meta_Validate', $field['validate_func']), $new);
1171
                }
1172
1173
1174
                if ($name == 'ct_cat_icon') {
1175
1176
                    $upload_dir = wp_upload_dir();
1177
1178
                    $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...
1179
                    //$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...
1180
                    //$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...
1181
1182
                    //$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...
1183
1184
                    $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...
1185
1186
                    /*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...
1187
				
1188
				//subdir
1189
				$new['src'] = $upload_dir['url'].'/'.'cat_icon_'.$term_id.'.png';
1190
				
1191
				update_attached_file( $new['id'], $new['src'] );*/
1192
1193
1194
                    /*	
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...
1195
		
1196
		$new['src'] = $upload_dir['url'].'/'.'cat_icon_'.$term_id.'.png';
1197
		
1198
		$filename = $new_filename;
1199
		
1200
		
1201
		$filetype = wp_check_filetype( basename( $filename ), null );
1202
		
1203
	
1204
		$wp_upload_dir = wp_upload_dir();
1205
		
1206
		
1207
		$attachment = array(
1208
			'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
1209
			'post_mime_type' => $filetype['type'],
1210
			'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
1211
			'post_content'   => '',
1212
			'post_status'    => 'inherit'
1213
		);
1214
		
1215
	
1216
		$attach_id = wp_insert_attachment( $attachment, $filename);*/
1217
1218
1219
                }
1220
1221
1222
                //skip on Paragraph field
1223
                if ($type != "paragraph") {
1224
1225
                    // Call defined method to save meta value, if there's no methods, call common one.
1226
                    $save_func = 'save_field_' . $type;
1227
                    if (method_exists($this, $save_func)) {
1228
                        call_user_func(array(&$this, 'save_field_' . $type), $term_id, $field, $old, $new);
1229
                    } else {
1230
                        $this->save_field($term_id, $field, $old, $new);
1231
                    }
1232
                }
1233
1234
            } // End foreach
1235
1236
        }
1237
1238
        /**
1239
         * Common function for saving fields.
1240
         *
1241
         * @param string $term_id The term ID.
1242
         * @param string $field
1243
         * @param string $old
1244
         * @param string|mixed $new
1245
         * @since 1.0
1246
         * @access public
1247
         */
1248
        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...
1249
        {
1250
            $name = $field['id'];
1251
            $this->delete_tax_meta($term_id, $name);
1252
            if ($new === '' || $new === array())
1253
                return;
1254
1255
            $this->update_tax_meta($term_id, $name, $new);
1256
        }
1257
1258
        /**
1259
         * function for saving image field.
1260
         *
1261
         * @param string $term_id The term ID.
1262
         * @param string $field
1263
         * @param string $old
1264
         * @param string|mixed $new
1265
         * @since 1.0
1266
         * @access public
1267
         */
1268
        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...
1269
        {
1270
            $name = $field['id'];
1271
1272
            $this->delete_tax_meta($term_id, $name);
1273
            if ($new === '' || $new === array() || $new['id'] == '' || $new['src'] == '')
1274
                return;
1275
1276
            $this->update_tax_meta($term_id, $name, $new);
1277
        }
1278
1279
        /*
1280
	 * Save Wysiwyg Field.
1281
	 *
1282
	 * @param string $term_id The term ID. 
1283
	 * @param string $field 
1284
	 * @param string $old 
1285
	 * @param string $new 
1286
	 * @since 1.0
1287
	 * @access public 
1288
	 */
1289
        public function save_field_wysiwyg($term_id, $field, $old, $new)
1290
        {
1291
            $this->save_field($term_id, $field, $old, $new);
1292
        }
1293
1294
        /**
1295
         * Save repeater Fields.
1296
         *
1297
         * @param string $term_id The term ID.
1298
         * @param string $field
1299
         * @param string|mixed $old
1300
         * @param string|mixed $new
1301
         * @since 1.0
1302
         * @access public
1303
         */
1304
        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...
1305
        {
1306
            if (is_array($new) && count($new) > 0) {
1307
                foreach ($new as $n) {
1308
                    foreach ($field['fields'] as $f) {
0 ignored issues
show
Bug introduced by
The expression $field['fields'] of type string is not traversable.
Loading history...
1309
                        $type = $f['type'];
1310
                        switch ($type) {
1311
                            case 'wysiwyg':
1312
                                $n[$f['id']] = wpautop($n[$f['id']]);
1313
                                break;
1314
                            case 'file':
1315
                                $n[$f['id']] = $this->save_field_file_repeater($term_id, $f, '', $n[$f['id']]);
1316
                                break;
1317
                            default:
1318
                                break;
1319
                        }
1320
                    }
1321
                    if (!$this->is_array_empty($n))
1322
                        $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...
1323
                }
1324
                if (isset($temp) && count($temp) > 0 && !$this->is_array_empty($temp)) {
1325
                    $this->update_tax_meta($term_id, $field['id'], $temp);
1326
                } else {
1327
                    //	remove old meta if exists
1328
                    delete_post_meta($term_id, $field['id']);
1329
                }
1330
            } else {
1331
                //	remove old meta if exists
1332
                delete_post_meta($term_id, $field['id']);
1333
            }
1334
        }
1335
1336
        /**
1337
         * Save File Field.
1338
         *
1339
         * @param string $term_id The term ID.
1340
         * @param string $field
1341
         * @param string $old
1342
         * @param string $new
1343
         * @since 1.0
1344
         * @access public
1345
         */
1346
        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...
1347
        {
1348
1349
            $name = $field['id'];
1350
            if (empty($_FILES[$name]))
1351
                return;
1352
            $this->fix_file_array($_FILES[$name]);
1353
            foreach ($_FILES[$name] as $position => $fileitem) {
1354
1355
                $file = wp_handle_upload($fileitem, array('test_form' => false));
1356
                if (empty($file['file']))
1357
                    continue;
1358
                $filename = $file['file'];
1359
1360
                $attachment = array(
1361
                    'post_mime_type' => $file['type'],
1362
                    'guid' => $file['url'],
1363
                    'post_parent' => $term_id,
1364
                    'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
1365
                    'post_content' => ''
1366
                );
1367
1368
                $id = wp_insert_attachment($attachment, $filename, $term_id);
1369
1370
                if (!is_wp_error($id)) {
1371
1372
                    wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $filename));
1373
                    add_post_meta($term_id, $name, $id, false);    // save file's url in meta fields
1374
1375
                } // End if
1376
1377
            } // End foreach
1378
1379
        }
1380
1381
        /**
1382
         * Save repeater File Field.
1383
         * @param string $term_id The term ID.
1384
         * @param string $field
1385
         * @param string $old
1386
         * @param string $new
1387
         * @since 1.0
1388
         * @access public
1389
         * @return int|void
1390
         */
1391
        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...
1392
        {
1393
1394
            $name = $field['id'];
1395
            if (empty($_FILES[$name]))
1396
                return;
1397
            $this->fix_file_array($_FILES[$name]);
1398
            foreach ($_FILES[$name] as $position => $fileitem) {
1399
1400
                $file = wp_handle_upload($fileitem, array('test_form' => false));
1401
                if (empty($file['file']))
1402
                    continue;
1403
                $filename = $file['file'];
1404
1405
                $attachment = array(
1406
                    'post_mime_type' => $file['type'],
1407
                    'guid' => $file['url'],
1408
                    'post_parent' => $term_id,
1409
                    'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
1410
                    'post_content' => ''
1411
                );
1412
1413
                $id = wp_insert_attachment($attachment, $filename);
1414
1415
                if (!is_wp_error($id)) {
1416
1417
                    wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $filename));
1418
                    return $id;    // return file's url in meta fields
1419
                } // End if
1420
            } // End foreach
1421
        }
1422
1423
        /**
1424
         * Add missed values for meta box.
1425
         *
1426
         * @since 1.0
1427
         * @access public
1428
         */
1429
        public function add_missed_values()
1430
        {
1431
1432
            // Default values for meta box
1433
            $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...
1434
1435
            if(is_array($this->_fields)) {
1436
                // Default values for fields
1437
                foreach ($this->_fields as &$field) {
1438
                    $multiple = in_array($field['type'], array('checkbox_list', 'file', 'image'));
1439
                    $std = $multiple ? array() : '';
1440
                    $format = 'date' == $field['type'] ? 'yy-mm-dd' : ('time' == $field['type'] ? 'hh:mm' : '');
1441
                    $field = array_merge(array('multiple' => $multiple, 'std' => $std, 'desc' => '', 'format' => $format, 'validate_func' => ''), $field);
1442
                } // End foreach
1443
            }
1444
        }
1445
1446
        /**
1447
         * Check if field with $type exists.
1448
         *
1449
         * @param string $type
1450
         * @since 1.0
1451
         * @access public
1452
         */
1453
        public function has_field($type)
1454
        {
1455
            if(is_array($this->_fields)) {
1456
                foreach ($this->_fields as $field) {
1457
                    if ($type == $field['type'])
1458
                        return true;
1459
                }
1460
            }
1461
            return false;
1462
        }
1463
1464
        /**
1465
         * Check if current page is edit page.
1466
         *
1467
         * @since 1.0
1468
         * @access public
1469
         */
1470
        public function is_edit_page()
1471
        {
1472
            global $pagenow;
1473
            return ($pagenow == 'edit-tags.php');
1474
        }
1475
1476
        /**
1477
         * Fixes the odd indexing of multiple file uploads.
1478
         *
1479
         * Goes from the format:
1480
         * $_FILES['field']['key']['index']
1481
         * to
1482
         * The More standard and appropriate:
1483
         * $_FILES['field']['index']['key']
1484
         *
1485
         * @param string $files
1486
         * @since 1.0
1487
         * @access public
1488
         */
1489
        public function fix_file_array(&$files)
1490
        {
1491
1492
            $output = array();
1493
1494
            foreach ($files as $key => $list) {
0 ignored issues
show
Bug introduced by
The expression $files of type string is not traversable.
Loading history...
1495
                foreach ($list as $index => $value) {
1496
                    $output[$index][$key] = $value;
1497
                }
1498
            }
1499
1500
            return $files = $output;
1501
1502
        }
1503
1504
        /**
1505
         * Get proper JQuery UI version.
1506
         *
1507
         * Used in order to not conflict with WP Admin Scripts.
1508
         *
1509
         * @since 1.0
1510
         * @access public
1511
         */
1512
        public function get_jqueryui_ver()
1513
        {
1514
1515
            global $wp_version;
1516
1517
            if (version_compare($wp_version, '3.1', '>=')) {
1518
                return '1.8.10';
1519
            }
1520
1521
            return '1.7.3';
1522
1523
        }
1524
1525
        /**
1526
         *  Add Field to meta box (generic function)
1527
         * @author Ohad Raz
1528
         * @since 1.0
1529
         * @access public
1530
         * @param $id string  field id, i.e. the meta key
1531
         * @param $args mixed|array
1532
         */
1533
        public function addField($id, $args)
1534
        {
1535
            $new_field = array('id' => $id, 'std' => '', 'desc' => '', 'style' => '');
1536
            $new_field = array_merge($new_field, $args);
1537
            $this->_fields[] = $new_field;
1538
        }
1539
1540
1541
        /**
1542
         *  Add Text Field to meta box
1543
         * @author Ohad Raz
1544
         * @since 1.0
1545
         * @access public
1546
         * @param $id string  field id, i.e. the meta key
1547
         * @param $args mixed|array
1548
         *    'name' => // field name/label string optional
1549
         *    'desc' => // field description, string optional
1550
         *    'std' => // default value, string optional
1551
         *    'style' =>    // custom style for field, string optional
1552
         *    'validate_func' => // validate function, string optional
1553
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1554
         */
1555 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...
1556
        {
1557
            $new_field = array('type' => 'text', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => __('Text Field', 'geodirectory'));
1558
            $new_field = array_merge($new_field, $args);
1559
            if (false === $repeater) {
1560
                $this->_fields[] = $new_field;
1561
            } else {
1562
                return $new_field;
1563
            }
1564
        }
1565
1566
        /**
1567
         *  Add Hidden Field to meta box
1568
         * @author Ohad Raz
1569
         * @since 0.1.3
1570
         * @access public
1571
         * @param $id string  field id, i.e. the meta key
1572
         * @param $args mixed|array
1573
         *    'name' => // field name/label string optional
1574
         *    'desc' => // field description, string optional
1575
         *    'std' => // default value, string optional
1576
         *    'style' =>    // custom style for field, string optional
1577
         *    'validate_func' => // validate function, string optional
1578
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1579
         */
1580 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...
1581
        {
1582
            $new_field = array('type' => 'hidden', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => __('Text Field', 'geodirectory'));
1583
            $new_field = array_merge($new_field, $args);
1584
            if (false === $repeater) {
1585
                $this->_fields[] = $new_field;
1586
            } else {
1587
                return $new_field;
1588
            }
1589
        }
1590
1591
        /**
1592
         *  Add Paragraph to meta box
1593
         * @author Ohad Raz
1594
         * @since 0.1.3
1595
         * @access public
1596
         * @param $id string  field id, i.e. the meta key
1597
         * @param $value  paragraph html
1598
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1599
         */
1600
        public function addParagraph($id, $args, $repeater = false)
1601
        {
1602
            $new_field = array('type' => 'paragraph', 'id' => $id, 'value' => '', 'style' => '');
1603
            $new_field = array_merge($new_field, $args);
1604
            if (false === $repeater) {
1605
                $this->_fields[] = $new_field;
1606
            } else {
1607
                return $new_field;
1608
            }
1609
        }
1610
1611
        /**
1612
         *  Add Checkbox Field to meta box
1613
         * @author Ohad Raz
1614
         * @since 1.0
1615
         * @access public
1616
         * @param $id string  field id, i.e. the meta key
1617
         * @param $args mixed|array
1618
         *    'name' => // field name/label string optional
1619
         *    'desc' => // field description, string optional
1620
         *    'std' => // default value, string optional
1621
         *    'validate_func' => // validate function, string optional
1622
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1623
         */
1624 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...
1625
        {
1626
            $new_field = array('type' => 'checkbox', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => __('Checkbox Field', 'geodirectory'));
1627
            $new_field = array_merge($new_field, $args);
1628
            if (false === $repeater) {
1629
                $this->_fields[] = $new_field;
1630
            } else {
1631
                return $new_field;
1632
            }
1633
        }
1634
1635
        /**
1636
         *  Add CheckboxList Field to meta box
1637
         * @author Ohad Raz
1638
         * @since 1.0
1639
         * @access public
1640
         * @param $id string  field id, i.e. the meta key
1641
         * @param $options (array)  array of key => value pairs for select options
1642
         * @param $args mixed|array
1643
         *    'name' => // field name/label string optional
1644
         *    'desc' => // field description, string optional
1645
         *    'std' => // default value, string optional
1646
         *    'validate_func' => // validate function, string optional
1647
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1648
         *
1649
         * @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...
1650
         *   which means the last param as false to get the values in an array
1651
         */
1652 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...
1653
        {
1654
            $new_field = array('type' => 'checkbox_list', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => __('Checkbox List Field', 'geodirectory'));
1655
            $new_field = array_merge($new_field, $args);
1656
            if (false === $repeater) {
1657
                $this->_fields[] = $new_field;
1658
            } else {
1659
                return $new_field;
1660
            }
1661
        }
1662
1663
        /**
1664
         *  Add Textarea Field to meta box
1665
         * @author Ohad Raz
1666
         * @since 1.0
1667
         * @access public
1668
         * @param $id string  field id, i.e. the meta key
1669
         * @param $args mixed|array
1670
         *    'name' => // field name/label string optional
1671
         *    'desc' => // field description, string optional
1672
         *    'std' => // default value, string optional
1673
         *    'style' =>    // custom style for field, string optional
1674
         *    'validate_func' => // validate function, string optional
1675
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1676
         */
1677 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...
1678
        {
1679
            $new_field = array('type' => 'textarea', 'id' => $id, 'std' => '', 'desc' => '', 'style' => '', 'name' => __('Textarea Field', 'geodirectory'));
1680
            $new_field = array_merge($new_field, $args);
1681
            if (false === $repeater) {
1682
                $this->_fields[] = $new_field;
1683
            } else {
1684
                return $new_field;
1685
            }
1686
        }
1687
1688
        /**
1689
         *  Add Select Field to meta box
1690
         * @author Ohad Raz
1691
         * @since 1.0
1692
         * @access public
1693
         * @param $id string field id, i.e. the meta key
1694
         * @param $options (array)  array of key => value pairs for select options
1695
         * @param $args mixed|array
1696
         *    'name' => // field name/label string optional
1697
         *    'desc' => // field description, string optional
1698
         *    'std' => // default value, (array) optional
1699
         *    'multiple' => // select multiple values, optional. Default is false.
1700
         *    'validate_func' => // validate function, string optional
1701
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1702
         */
1703 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...
1704
        {
1705
            $new_field = array('type' => 'select', 'id' => $id, 'std' => array(), 'desc' => '', 'style' => '', 'name' => __('Select Field', 'geodirectory'), 'multiple' => false, 'options' => $options);
1706
            $new_field = array_merge($new_field, $args);
1707
            if (false === $repeater) {
1708
                $this->_fields[] = $new_field;
1709
            } else {
1710
                return $new_field;
1711
            }
1712
        }
1713
1714
1715
        /**
1716
         *  Add Radio Field to meta box
1717
         * @author Ohad Raz
1718
         * @since 1.0
1719
         * @access public
1720
         * @param $id string field id, i.e. the meta key
1721
         * @param $options (array)  array of key => value pairs for radio options
1722
         * @param $args mixed|array
1723
         *    'name' => // field name/label string optional
1724
         *    'desc' => // field description, string optional
1725
         *    'std' => // default value, string optional
1726
         *    'validate_func' => // validate function, string optional
1727
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1728
         */
1729 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...
1730
        {
1731
            $new_field = array('type' => 'radio', 'id' => $id, 'std' => array(), 'desc' => '', 'style' => '', 'name' => __('Radio Field', 'geodirectory'), 'options' => $options);
1732
            $new_field = array_merge($new_field, $args);
1733
            if (false === $repeater) {
1734
                $this->_fields[] = $new_field;
1735
            } else {
1736
                return $new_field;
1737
            }
1738
        }
1739
1740
        /**
1741
         *  Add Date Field to meta box
1742
         * @author Ohad Raz
1743
         * @since 1.0
1744
         * @access public
1745
         * @param $id string  field id, i.e. the meta key
1746
         * @param $args mixed|array
1747
         *    'name' => // field name/label string optional
1748
         *    'desc' => // field description, string optional
1749
         *    'std' => // default value, string optional
1750
         *    'validate_func' => // validate function, string optional
1751
         *    'format' => // date format, default yy-mm-dd. Optional. Default "'d MM, yy'"  See more formats here: http://goo.gl/Wcwxn
1752
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1753
         */
1754 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...
1755
        {
1756
            $new_field = array('type' => 'date', 'id' => $id, 'std' => '', 'desc' => '', 'format' => 'yy-mm-dd', 'name' => __('Date Field', 'geodirectory'));
1757
            $new_field = array_merge($new_field, $args);
1758
            if (false === $repeater) {
1759
                $this->_fields[] = $new_field;
1760
            } else {
1761
                return $new_field;
1762
            }
1763
        }
1764
1765
        /**
1766
         *  Add Time Field to meta box
1767
         * @author Ohad Raz
1768
         * @since 1.0
1769
         * @access public
1770
         * @param $id string- field id, i.e. the meta key
1771
         * @param $args mixed|array
1772
         *    'name' => // field name/label string optional
1773
         *    'desc' => // field description, string optional
1774
         *    'std' => // default value, string optional
1775
         *    'validate_func' => // validate function, string optional
1776
         *    'format' => // time format, default hh:mm. Optional. See more formats here: http://goo.gl/83woX
1777
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1778
         */
1779 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...
1780
        {
1781
            $new_field = array('type' => 'time', 'id' => $id, 'std' => '', 'desc' => '', 'format' => 'hh:mm', 'name' => __('Time Field', 'geodirectory'));
1782
            $new_field = array_merge($new_field, $args);
1783
            if (false === $repeater) {
1784
                $this->_fields[] = $new_field;
1785
            } else {
1786
                return $new_field;
1787
            }
1788
        }
1789
1790
        /**
1791
         *  Add Color Field to meta box
1792
         * @author Ohad Raz
1793
         * @since 1.0
1794
         * @access public
1795
         * @param $id string  field id, i.e. the meta key
1796
         * @param $args mixed|array
1797
         *    'name' => // field name/label string optional
1798
         *    'desc' => // field description, string optional
1799
         *    'std' => // default value, string optional
1800
         *    'validate_func' => // validate function, string optional
1801
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1802
         */
1803 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...
1804
        {
1805
            $new_field = array('type' => 'color', 'id' => $id, 'std' => '', 'desc' => '', 'name' => __('ColorPicker Field', 'geodirectory'));
1806
            $new_field = array_merge($new_field, $args);
1807
            if (false === $repeater) {
1808
                $this->_fields[] = $new_field;
1809
            } else {
1810
                return $new_field;
1811
            }
1812
        }
1813
1814
        /**
1815
         *  Add Image Field to meta box
1816
         * @author Ohad Raz
1817
         * @since 1.0
1818
         * @access public
1819
         * @param $id string  field id, i.e. the meta key
1820
         * @param $args mixed|array
1821
         *    'name' => // field name/label string optional
1822
         *    'desc' => // field description, string optional
1823
         *    'validate_func' => // validate function, string optional
1824
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1825
         */
1826 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...
1827
        {
1828
            $new_field = array('type' => 'image', 'id' => $id, 'desc' => '', 'style' => '', 'name' => __('Image Field', 'geodirectory'));
1829
            $new_field = array_merge($new_field, $args);
1830
1831
            if (false === $repeater) {
1832
                $this->_fields[] = $new_field;
1833
            } else {
1834
                return $new_field;
1835
            }
1836
        }
1837
1838
        /**
1839
         *  Add File Field to meta box
1840
         * @author Ohad Raz
1841
         * @since 1.0
1842
         * @access public
1843
         * @param $id string  field id, i.e. the meta key
1844
         * @param $args mixed|array
1845
         *    'name' => // field name/label string optional
1846
         *    'desc' => // field description, string optional
1847
         *    'validate_func' => // validate function, string optional
1848
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1849
         */
1850 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...
1851
        {
1852
            $new_field = array('type' => 'file', 'id' => $id, 'desc' => '', 'style' => '', 'name' => __('File Field', 'geodirectory'));
1853
            $new_field = array_merge($new_field, $args);
1854
            if (false === $repeater) {
1855
                $this->_fields[] = $new_field;
1856
            } else {
1857
                return $new_field;
1858
            }
1859
        }
1860
1861
        /**
1862
         *  Add WYSIWYG Field to meta box
1863
         * @author Ohad Raz
1864
         * @since 1.0
1865
         * @access public
1866
         * @param $id string  field id, i.e. the meta key
1867
         * @param $args mixed|array
1868
         *    'name' => // field name/label string optional
1869
         *    'desc' => // field description, string optional
1870
         *    'std' => // default value, string optional
1871
         *    'style' =>    // custom style for field, string optional Default 'width: 300px; height: 400px'
1872
         *    'validate_func' => // validate function, string optional
1873
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1874
         */
1875 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...
1876
        {
1877
            $new_field = array('type' => 'wysiwyg', 'id' => $id, 'std' => '', 'desc' => '', 'style' => 'width: 300px; height: 400px', 'name' => __('WYSIWYG Editor Field', 'geodirectory'));
1878
            $new_field = array_merge($new_field, $args);
1879
            if (false === $repeater) {
1880
                $this->_fields[] = $new_field;
1881
            } else {
1882
                return $new_field;
1883
            }
1884
        }
1885
1886
        /**
1887
         *  Add Taxonomy Field to meta box
1888
         * @author Ohad Raz
1889
         * @since 1.0
1890
         * @access public
1891
         * @param $id string  field id, i.e. the meta key
1892
         * @param $options mixed|array options of taxonomy field
1893
         *    'taxonomy' =>    // taxonomy name can be category,post_tag or any custom taxonomy default is category
1894
         * 'type' =>  // how to show taxonomy? 'select' (default) or 'checkbox_list'
1895
         * 'args' =>  // arguments to query taxonomy, see http://goo.gl/uAANN default ('hide_empty' => false)
1896
         * @param $args mixed|array
1897
         *    'name' => // field name/label string optional
1898
         *    'desc' => // field description, string optional
1899
         *    'std' => // default value, string optional
1900
         *    'validate_func' => // validate function, string optional
1901
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1902
         */
1903
        public function addTaxonomy($id, $options, $args, $repeater = false)
1904
        {
1905
            $q = array('hide_empty' => 0);
1906
            $tax = 'category';
1907
            $type = 'select';
1908
            $temp = array($tax, $type, $q);
1909
            $options = array_merge($temp, $options);
1910
            $new_field = array('type' => 'taxonomy', 'id' => $id, 'desc' => '', 'name' => __('Taxonomy Field', 'geodirectory'), 'options' => $options);
1911
            $new_field = array_merge($new_field, $args);
1912
            if (false === $repeater) {
1913
                $this->_fields[] = $new_field;
1914
            } else {
1915
                return $new_field;
1916
            }
1917
        }
1918
1919
        /**
1920
         *  Add posts Field to meta box
1921
         * @author Ohad Raz
1922
         * @since 1.0
1923
         * @access public
1924
         * @param $id string  field id, i.e. the meta key
1925
         * @param $options mixed|array options of taxonomy field
1926
         *    'post_type' =>    // post type name, 'post' (default) 'page' or any custom post type
1927
         * 'type' =>  // how to show posts? 'select' (default) or 'checkbox_list'
1928
         * 'args' =>  // arguments to query posts, see http://goo.gl/is0yK default ('posts_per_page' => -1)
1929
         * @param $args mixed|array
1930
         *    'name' => // field name/label string optional
1931
         *    'desc' => // field description, string optional
1932
         *    'std' => // default value, string optional
1933
         *    'validate_func' => // validate function, string optional
1934
         * @param $repeater bool  is this a field inside a repeatr? true|false(default)
1935
         */
1936
        public function addPosts($id, $options, $args, $repeater = false)
1937
        {
1938
            $q = array('posts_per_page' => -1);
1939
            $temp = array('post_type' => 'post', 'type' => 'select', 'args' => $q);
1940
            $options = array_merge($temp, $options);
1941
            $new_field = array('type' => 'posts', 'id' => $id, 'desc' => '', 'name' => __('Posts Field', 'geodirectory'), 'options' => $options);
1942
            $new_field = array_merge($new_field, $args);
1943
            if (false === $repeater) {
1944
                $this->_fields[] = $new_field;
1945
            } else {
1946
                return $new_field;
1947
            }
1948
        }
1949
1950
        /**
1951
         *  Add repeater Field Block to meta box
1952
         * @author Ohad Raz
1953
         * @since 1.0
1954
         * @access public
1955
         * @param $id string  field id, i.e. the meta key
1956
         * @param $args mixed|array
1957
         *    'name' => // field name/label string optional
1958
         *    'desc' => // field description, string optional
1959
         *    'std' => // default value, string optional
1960
         *    'style' =>    // custom style for field, string optional
1961
         *    'validate_func' => // validate function, string optional
1962
         *    'fields' => //fields to repeater
1963
         */
1964
        public function addRepeaterBlock($id, $args)
1965
        {
1966
            $new_field = array('type' => 'repeater', 'id' => $id, 'name' => __('Reapeater Field', 'geodirectory'), 'fields' => array(), 'inline' => false);
1967
            $new_field = array_merge($new_field, $args);
1968
            $this->_fields[] = $new_field;
1969
        }
1970
1971
1972
        /**
1973
         * Finish Declaration of Meta Box
1974
         * @author Ohad Raz
1975
         * @since 1.0
1976
         * @access public
1977
         */
1978
        public function Finish()
1979
        {
1980
            $this->add_missed_values();
1981
            $this->check_field_upload();
1982
            $this->check_field_color();
1983
            $this->check_field_date();
1984
            $this->check_field_time();
1985
        }
1986
1987
        /**
1988
         * Helper function to check for empty arrays
1989
         * @author Ohad Raz
1990
         * @since 1.0
1991
         * @access public
1992
         * @param $args mixed|array
1993
         */
1994
        public function is_array_empty($array)
1995
        {
1996
            if (!is_array($array))
1997
                return true;
1998
1999
            foreach ($array as $a) {
2000
                if (is_array($a)) {
2001
                    foreach ($a as $sub_a) {
2002
                        if (!empty($sub_a) && $sub_a != '')
2003
                            return false;
2004
                    }
2005
                } else {
2006
                    if (!empty($a) && $a != '')
2007
                        return false;
2008
                }
2009
            }
2010
            return true;
2011
        }
2012
2013
2014
        //get term meta field
2015 View Code Duplication
        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...
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...
2016
        {
2017
2018
            if (empty($post_type) && isset($_REQUEST['taxonomy'])) {
2019
                $taxObject = get_taxonomy($_REQUEST['taxonomy']);
2020
                $post_type = $taxObject->object_type[0];
2021
            }
2022
2023
            if($post_type=='post'){$post_type='';}
2024
            if($post_type){$post_type = $post_type.'_';}
2025
2026
            $t_id = (is_object($term_id)) ? $term_id->term_id : $term_id;
2027
2028
            $m = get_option('tax_meta_' . $post_type  . $t_id);
2029
            if (isset($m[$key])) {
2030
                return $m[$key];
2031
            } else {
2032
                return '';
2033
            }
2034
        }
2035
2036
        //delete meta
2037
        public function delete_tax_meta($term_id, $key, $post_type = '')
2038
        {
2039
2040
            if (empty($post_type) && isset($_REQUEST['taxonomy'])) {
2041
                $taxObject = get_taxonomy($_REQUEST['taxonomy']);
2042
                $post_type = $taxObject->object_type[0];
2043
            }
2044
2045
            if($post_type=='post'){$post_type='';}
2046
            if($post_type){$post_type = $post_type.'_';}
2047
2048
            $m = get_option('tax_meta_' . $post_type  . $term_id);
2049
2050
            if (isset($m[$key])) {
2051
                unset($m[$key]);
2052
            }
2053
            update_option('tax_meta_' . $post_type  . $term_id, $m);
2054
        }
2055
2056
        //update meta
2057 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...
2058
        {
2059
2060
            if (empty($post_type) && isset($_REQUEST['taxonomy'])) {
2061
                $taxObject = get_taxonomy($_REQUEST['taxonomy']);
2062
                $post_type = $taxObject->object_type[0];
2063
            }
2064
2065
            if($post_type=='post'){$post_type='';}
2066
            if($post_type){$post_type = $post_type.'_';}
2067
2068
            $m = get_option('tax_meta_' . $post_type  . $term_id);
2069
2070
            $m[$key] = $value;
2071
            update_option('tax_meta_' . $post_type  . $term_id, $m);
2072
2073
            /**
2074
             * Called after the tax meta is updated.
2075
             *
2076
             * Used to update things after a GD category is saved.
2077
             *
2078
             * @since 1.0.0
2079
             * @param bool $false False.
2080
             * @param bool $true True.
2081
             * @param int $term_id The term id being updated.
2082
             * @param string $post_type The post type of the cat being updated.
2083
             */
2084
            do_action('gd_tax_meta_updated', false, true, $term_id, $post_type);
2085
        }
2086
2087
2088
    } // End Class
2089
2090
endif; // End Check Class Exists
2091
2092
/*
2093
 * meta functions for easy access:
2094
 */
2095
2096
//get term meta field
2097
if (!function_exists('get_tax_meta')) {
2098 View Code Duplication
    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...
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...
2099
    {
2100
2101
        if (empty($post_type) && isset($_REQUEST['taxonomy'])) {
2102
            $taxObject = get_taxonomy($_REQUEST['taxonomy']);
2103
            $post_type = $taxObject->object_type[0];
2104
        }
2105
2106
        if($post_type=='post'){$post_type='';}
2107
        if($post_type){$post_type = $post_type.'_';}
2108
2109
        $t_id = (is_object($term_id)) ? $term_id->term_id : $term_id;
2110
2111
        $m = get_option('tax_meta_' . $post_type  . $t_id);
2112
        if (isset($m[$key])) {
2113
            return $m[$key];
2114
        } else {
2115
            return '';
2116
        }
2117
    }
2118
}
2119
2120
//delete meta
2121
if (!function_exists('delete_tax_meta')) {
2122
    function delete_tax_meta($term_id, $key)
2123
    {
2124
2125
        $taxObject = get_taxonomy($_REQUEST['taxonomy']);
2126
        $post_type = $taxObject->object_type[0];
2127
2128
        if($post_type=='post'){$post_type='';}
2129
        if($post_type){$post_type = $post_type.'_';}
2130
2131
        $m = get_option('tax_meta_' . $post_type . $term_id);
2132
2133
        if (isset($m[$key])) {
2134
            unset($m[$key]);
2135
        }
2136
        update_option('tax_meta_' . $post_type  . $term_id, $m);
2137
    }
2138
}
2139
2140
//update meta
2141
if (!function_exists('update_tax_meta')) {
2142 View Code Duplication
    function 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...
2143
    {
2144
2145
        if (empty($post_type) && isset($_REQUEST['taxonomy'])) {
2146
            $taxObject = get_taxonomy($_REQUEST['taxonomy']);
2147
            $post_type = $taxObject->object_type[0];
2148
        }
2149
2150
        if($post_type=='post'){$post_type='';}
2151
        if($post_type){$post_type = $post_type.'_';}
2152
2153
        $m = get_option('tax_meta_' . $post_type  . $term_id);
2154
2155
        $m[$key] = $value;
2156
        update_option('tax_meta_' . $post_type . $term_id, $m);
2157
2158
        /** This action is documented in geodirectory-functions/cat-meta-functions/Tax-meta-class.php */
2159
        do_action('gd_tax_meta_updated', false, true, $term_id, $post_type);
2160
    }
2161
}