GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — feature/attachment-taxonomies ( ebe0e0...5ca02b )
by Brad
03:06
created

override_attachment_taxonomy_fields()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 4
nop 2
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
1
<?php
2
if ( ! class_exists( 'FooGallery_Attachment_Taxonomies' ) ) {
3
4
    define( 'FOOGALLERY_ATTACHMENT_TAXONOMY_TAG', 'foogallery_attachment_tag' );
5
    define( 'FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION', 'foogallery_attachment_collection' );
6
7
    class FooGallery_Attachment_Taxonomies {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
9
    	private $cached_terms = array();
10
11
        /**
12
         * Class Constructor
13
         */
14
        function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
15
			add_action( 'init', array( $this, 'init_all' ), 11 );
16
        }
17
18
		/**
19
		 * Initialize all the hooks if the taxonomies are not disabled
20
		 */
21
        function init_all() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
22
			if ( foogallery_get_setting( 'disable_attachment_taxonomies' ) === 'on' ) {
23
				return;
24
			}
25
26
			$this->add_taxonomies();
27
28
			if ( is_admin() ) {
29
				add_action( 'admin_menu', array( $this, 'add_menu_items' ), 1 );
30
				add_filter( 'parent_file', array( $this, 'set_current_menu' ) );
31
				add_filter( 'manage_media_columns', array( $this, 'change_attachment_column_names' ) );
32
				add_filter( 'manage_edit-foogallery_attachment_tag_columns', array( $this, 'clean_column_names' ), 999 );
33
				add_filter( 'manage_edit-foogallery_attachment_collection_columns', array( $this, 'clean_column_names' ), 999 );
34
35
				//make the attachment taxonomies awesome
36
				add_action( 'admin_head', array( $this, 'include_inline_taxonomy_data_script' ) );
37
				add_filter( 'attachment_fields_to_edit', array( $this, 'inject_code_into_field' ), 10, 2 );
38
				//add_filter( 'attachment_fields_to_save', array( $this, 'save_fields' ), 10, 2 );
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
39
				add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_js' ), 99 );
40
41
				//ajax actions from the media modal
42
				add_action( 'wp_ajax_foogallery-taxonomies-add-term', array( $this, 'ajax_add_term' ) );
43
				add_action( 'wp_ajax_foogallery-taxonomies-save-terms', array( $this, 'ajax_save_terms' ) );
44
			}
45
		}
46
        /**
47
         * Save terms for an attachment
48
         *
49
         * @since 1.4.19
50
         */
51
        public function ajax_save_terms()
0 ignored issues
show
Coding Style introduced by
ajax_save_terms uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
52
        {
53
            $nonce = $_POST['nonce'];
54
            if (wp_verify_nonce($nonce, 'foogallery-attachment-taxonomy')) {
55
56
                $attachment_id = $_POST['attachment_id'];
57
                $terms = $_POST['terms'];
58
                $taxonomy = $_POST['taxonomy'];
59
60
                $result = wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $terms)), $taxonomy, false);
61
62
				clean_post_cache($attachment_id);
63
64
				if ( !is_wp_error( $result ) ) {
65
					wp_send_json( $terms );
66
				}
67
            }
68
            die();
69
        }
70
71
        /**
72
         * Add new term via an ajax call from admin
73
         *
74
         * @since 1.4.19
75
         * @access public
76
         */
77
        public function ajax_add_term() {
0 ignored issues
show
Coding Style introduced by
ajax_add_term uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
78
            $nonce = $_POST['nonce'];
79
            if (wp_verify_nonce($nonce, 'foogallery-attachment-taxonomy')) {
80
81
                $new_term = wp_insert_term($_POST['term_label'], $_POST['taxonomy']);
82
83
                if (is_wp_error($new_term)) {
84
                    die();
85
                }
86
87
                $new_term_obj = null;
88
89
                if (isset($new_term['term_id'])) {
90
                    $new_term_obj = get_term($new_term['term_id']);
91
                }
92
93
                if (!is_wp_error($new_term_obj)) {
94
                    wp_send_json(array(
95
                        'new_term' => $new_term_obj,
96
                        'all_terms' => $this->build_terms_recursive($_POST['taxonomy'], array('hide_empty' => false))
97
                    ));
98
                }
99
            }
100
101
            die();
102
        }
103
104
        public function save_fields( $post, $attachment ) {
0 ignored issues
show
Unused Code introduced by
The parameter $attachment 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...
Coding Style introduced by
save_fields uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
105
            $something = $_POST;
0 ignored issues
show
Unused Code introduced by
$something 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...
106
107
            return $post;
108
        }
109
110
		/**
111
		 * Enqueue admin script and styles
112
		 *
113
		 * @since 1.0.0
114
		 * @access public
115
		 * @static
116
		 */
117
		public function enqueue_js() {
118
			global $pagenow, $mode;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
119
120
			$should_add = wp_script_is('media-views') || ($pagenow === 'upload.php' && $mode === 'grid');
121
122
			if( !$should_add ) {
123
				return;
124
			}
125
126
			//enqueue selectize assets
127
			wp_enqueue_script( 'foogallery-selectize-core', FOOGALLERY_URL . 'lib/selectize/selectize.min.js', array('jquery'), FOOGALLERY_VERSION );
128
			wp_enqueue_script( 'foogallery-selectize', FOOGALLERY_URL . 'lib/selectize/foogallery.selectize.js', array('foogallery-selectize-core'), FOOGALLERY_VERSION );
129
			wp_enqueue_style(  'foogallery-selectize', FOOGALLERY_URL . 'lib/selectize/selectize.css', array(), FOOGALLERY_VERSION );
130
131
			//enqueue media attachment autosave script
132
            wp_enqueue_script( 'foogallery-attachment-autosave', FOOGALLERY_URL . 'js/admin-foogallery-attachment-autosave.js', ['media-views']);
133
		}
134
135
		/**
136
		 * Add fields to attachment
137
		 *
138
		 * @since 1.0.0
139
		 * @access public
140
		 * @static
141
		 * @param array $fields An array with all fields to edit
142
		 * @param object $post An object for the current post
143
		 * @return array $fields An array with all fields to edit
144
		 */
145
		public function inject_code_into_field($fields, $post) {
146
			if ( array_key_exists( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, $fields ) ) {
147
148
				$value = trim( $fields[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG]['value'] );
149
150
				$fields[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG] = array(
151
					'show_in_edit' => false,
152
					'input' => 'html',
153
					'html' => $this->build_taxonomy_html( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, $post, $value ),
154
					'label' => __( 'Media Tags', 'foogallery' )
155
				);
156
			}
157
158
			if ( array_key_exists( FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION, $fields ) ) {
159
160
				$value = trim( $fields[FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION]['value'] );
161
162
				$fields[FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION] = array(
163
					'show_in_edit' => false,
164
					'input' => 'html',
165
					'html' => $this->build_taxonomy_html( FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION, $post, $value ),
166
					'label' => __( 'Media Collections', 'foogallery' )
167
				);
168
			}
169
170
			return $fields;
171
		}
172
173
		/**
174
		 * Add custom js into admin head so that we can build up decent taxonomy selectize controls
175
		 *
176
		 * @since 1.0.0
177
		 * @access public
178
		 * @static
179
		 */
180
		public function include_inline_taxonomy_data_script() {
181
			global $pagenow, $mode;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
182
183
			$should_add = wp_script_is('media-views') || ($pagenow === 'upload.php' && $mode === 'grid');
184
185
			if( !$should_add ) {
186
				return;
187
			}
188
189
			$taxonomy_data[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$taxonomy_data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $taxonomy_data = 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...
190
				'slug' => FOOGALLERY_ATTACHMENT_TAXONOMY_TAG,
191
				'terms' => $this->build_terms_recursive(FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, array('hide_empty' => false)),
192
				'query_var' => true,
193
				'labels' => array(
194
					'placeholder' => __( 'Select tags, or add a new tag...', 'foogallery' ),
195
					'add' => __( 'Add new tag', 'foogallery' )
196
				),
197
			);
198
199
			$taxonomy_data[FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION] = array(
200
				'slug' => FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION,
201
				'terms' => $this->build_terms_recursive(FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION, array('hide_empty' => false)),
202
				'query_var' => true,
203
				'labels' => array(
204
					'placeholder' => __( 'Select collections, or add a new collection...', 'foogallery' ),
205
					'add' => __( 'Add new collection', 'foogallery' )
206
				),
207
			);
208
209
			$taxonomy_data['nonce'] = wp_create_nonce( 'foogallery-attachment-taxonomy' );
210
211
			echo '<script type="text/javascript">
212
			window.FOOGALLERY_TAXONOMY_DATA = ' . json_encode($taxonomy_data) . ';
213
		</script>';
214
		}
215
216
        function change_attachment_column_names( $columns ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
217
218
             if ( array_key_exists( 'taxonomy-foogallery_attachment_collection', $columns ) ) {
219
                 $columns['taxonomy-foogallery_attachment_collection'] = __('Collections', 'foogallery');
220
             }
221
222
             return $columns;
223
        }
224
225
        /**
226
         * Clean up the taxonomy columns for WP Seo plugin
227
         *
228
         * @param $columns
229
         * @return mixed
230
         */
231
        function clean_column_names( $columns ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
232
233
             //cleanup wpseo columns!
234
             if ( array_key_exists( 'wpseo_score', $columns ) ) {
235
                 unset( $columns['wpseo_score'] );
236
             }
237
            if ( array_key_exists( 'wpseo_score_readability', $columns ) ) {
238
                unset( $columns['wpseo_score_readability'] );
239
            }
240
             return $columns;
241
        }
242
243
        /**
244
         * Add the menu items under the FooGallery main menu
245
         */
246
        function add_menu_items() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
247
            foogallery_add_submenu_page(
248
                __( 'Media Tags', 'foogallery' ),
249
                'manage_options',
250
                'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '&post_type=' . FOOGALLERY_CPT_GALLERY,
251
                null
252
            );
253
254
            foogallery_add_submenu_page(
255
                __( 'Media Collections', 'foogallery' ),
256
                'manage_options',
257
                'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION . '&post_type=' . FOOGALLERY_CPT_GALLERY,
258
                null
259
            );
260
        }
261
262
        /**
263
         * Make sure the taxonomy menu items are highlighted
264
         * @param $parent_file
265
         * @return mixed
266
         */
267
        function set_current_menu( $parent_file ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
268
            global $submenu_file, $current_screen;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
269
270
            if ( $current_screen->post_type == FOOGALLERY_CPT_GALLERY ) {
271
272
                if ( 'edit-foogallery_attachment_tag' === $current_screen->id ) {
273
                    $submenu_file = 'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '&post_type=' . FOOGALLERY_CPT_GALLERY;
274
                }
275
276
                if ( 'edit-foogallery_attachment_collection' === $current_screen->id ) {
277
                    $submenu_file = 'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION . '&post_type=' . FOOGALLERY_CPT_GALLERY;
278
                }
279
            }
280
281
            return $parent_file;
282
        }
283
284
        /**
285
         * Register the taxonomies for attachments
286
         */
287
        function add_taxonomies() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
288
289
//			if ( foogallery_get_setting( 'disable_attachment_taxonomies') === 'on' ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% 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...
290
//				return;
291
//			}
292
293
            $tag_args = array(
294
                'labels'            => array(
295
                    'name'              => __( 'Media Tags', 'foogallery' ),
296
                    'singular_name'     => __( 'Tag', 'foogallery' ),
297
                    'search_items'      => __( 'Search Tags', 'foogallery' ),
298
                    'all_items'         => __( 'All Tags', 'foogallery' ),
299
                    'parent_item'       => __( 'Parent Tag', 'foogallery' ),
300
                    'parent_item_colon' => __( 'Parent Tag:', 'foogallery' ),
301
                    'edit_item'         => __( 'Edit Tag', 'foogallery' ),
302
                    'update_item'       => __( 'Update Tag', 'foogallery' ),
303
                    'add_new_item'      => __( 'Add New Tag', 'foogallery' ),
304
                    'new_item_name'     => __( 'New Tag Name', 'foogallery' ),
305
                    'menu_name'         => __( 'Media Tags', 'foogallery' )
306
                ),
307
                'hierarchical'      => false,
308
                'query_var'         => true,
309
                'rewrite'           => false,
310
                'show_admin_column' => false,
311
                'show_in_menu'      => false,
312
                'update_count_callback' => '_update_generic_term_count'
313
            );
314
315
            register_taxonomy( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, 'attachment', $tag_args );
316
317
            $collection_args = array(
318
                'labels'            => array(
319
                    'name'              => __( 'Media Collections', 'foogallery' ),
320
                    'singular_name'     => __( 'Collection', 'foogallery' ),
321
                    'search_items'      => __( 'Search Collections', 'foogallery' ),
322
                    'all_items'         => __( 'All Collections', 'foogallery' ),
323
                    'parent_item'       => __( 'Parent Collection', 'foogallery' ),
324
                    'parent_item_colon' => __( 'Parent Collection:', 'foogallery' ),
325
                    'edit_item'         => __( 'Edit Collection', 'foogallery' ),
326
                    'update_item'       => __( 'Update Collection', 'foogallery' ),
327
                    'add_new_item'      => __( 'Add New Collection', 'foogallery' ),
328
                    'new_item_name'     => __( 'New Collection Name', 'foogallery' ),
329
                    'menu_name'         => __( 'Media Collections', 'foogallery' )
330
                ),
331
                'hierarchical'      => true,
332
                'query_var'         => true,
333
                'rewrite'           => false,
334
                'show_admin_column' => true,
335
                'show_in_menu'      => false,
336
                'update_count_callback' => '_update_generic_term_count'
337
            );
338
339
            register_taxonomy( FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION, 'attachment', $collection_args );
340
        }
341
342
		/**
343
		 * Build up a taxonomy field HTML
344
		 *
345
		 * @param $taxonomy
346
		 * @param $post
347
		 *
348
		 * @return array
349
		 */
350
        function build_taxonomy_html( $taxonomy, $post, $value ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
351
			$html = '<input type="text" data-attachment_id="' . $post->ID . '" class="foogallery-attachment-ignore-change" id="attachments-' . $post->ID .'-' . $taxonomy . '" name="attachments-' . $post->ID .'-' . $taxonomy . '" value="' . $value . '" data-original-value="' . $value . '" />';
352
			$html .= '<script type="script/javascript">
353
				FOOGALLERY_SELECTIZE(\'#attachments-' . $post->ID .'-' . $taxonomy . '\', \'' . $taxonomy .'\');
354
				</script>';
355
			return $html;
356
		}
357
358
		/**
359
		 * Get terms sorted by hierarchy in a recursive way
360
		 *
361
		 * @param  string $taxonomy The taxonomy name
362
		 * @param  array $args The arguments which should be passed to the get_terms function
363
		 * @param  int $parent The terms parent id (for recursive usage)
364
		 * @param  int $level The current level (for recursive usage)
365
		 * @param  array $parents An array with all the parent terms (for recursive usage)
366
		 *
367
		 * @return array $terms_all An array with all the terms for this taxonomy
368
		 */
369
		function build_terms_recursive($taxonomy, $args = array(), $parent = 0, $level = 1, $parents = array()) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
370
			//check if the taxonomy terms have already been built up
371
			if ( 0 === $parent && array_key_exists( $taxonomy, $this->cached_terms ) ) {
372
				return $this->cached_terms[$taxonomy];
373
			}
374
375
			$terms_all = array();
376
377
			$args['parent'] = $args['child_of'] = $parent;
378
379
			$terms = get_terms($taxonomy, $args);
380
381
			foreach($terms as $term) {
382
				$term->level = $level;
383
				$term->parents = $parents;
384
				$term_parents = $parents;
385
				$term_parents[] = $term->name;
386
				$terms_all[] = $term;
387
				$terms_sub = $this->build_terms_recursive($taxonomy, $args, $term->term_id, $level + 1, $term_parents);
388
389
				if(!empty($terms_sub)) {
390
					$terms_all = array_merge($terms_all, $terms_sub);
391
				}
392
			}
393
394
			//cache what we have built up
395
			if ( 0 === $parent && !array_key_exists( $taxonomy, $this->cached_terms ) ) {
396
				$this->cached_terms[$taxonomy] = $terms_all;
397
			}
398
399
			return $terms_all;
400
		}
401
    }
402
}
403