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 ( 54ac97...05a68d )
by Brad
02:53
created

add_taxonomy_fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 2
dl 0
loc 18
rs 9.4285
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
         */function __construct() {
14
            add_action( 'init', array( $this, 'add_taxonomies' ) );
15
16
            if ( is_admin() ) {
17
                add_action( 'admin_menu', array( $this, 'add_menu_items' ), 1 );
18
                add_filter( 'parent_file', array( $this, 'set_current_menu' ) );
19
                add_filter( 'manage_media_columns', array( $this, 'change_attachment_column_names' ) );
20
                add_filter( 'manage_edit-foogallery_attachment_tag_columns', array( $this, 'clean_column_names' ), 999 );
21
                add_filter( 'manage_edit-foogallery_attachment_collection_columns', array( $this, 'clean_column_names' ), 999 );
22
                //add_filter( 'foogallery_attachment_add_fields', array( $this, 'remove_taxonomy_fields') );
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
23
                add_action( 'restrict_manage_posts', array( $this, 'add_collection_filter' ) );
24
25
                add_filter( 'foogallery_attachment_custom_fields_with_post', array( $this, 'add_taxonomy_fields' ), 10, 2 );
26
                //add_filter( 'foogallery_attachment_field_taxonomy_tag', array( $this, 'customize_media_tag_field'), 10, 2 );
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% 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...
27
                //add_filter( 'foogallery_attachment_save_field_taxonomy_tag', array( $this, 'save_media_tag_field' ), 10, 4 );
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...
28
            }
29
        }
30
31
        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...
32
33
             if ( array_key_exists( 'taxonomy-foogallery_attachment_collection', $columns ) ) {
34
                 $columns['taxonomy-foogallery_attachment_collection'] = __('Collections', 'foogallery');
35
             }
36
37
             return $columns;
38
        }
39
40
        /**
41
         * Clean up the taxonomy columns
42
         *
43
         * @param $columns
44
         * @return mixed
45
         */
46
        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...
47
48
             //cleanup wpseo columns!
49
             if ( array_key_exists( 'wpseo_score', $columns ) ) {
50
                 unset( $columns['wpseo_score'] );
51
             }
52
            if ( array_key_exists( 'wpseo_score_readability', $columns ) ) {
53
                unset( $columns['wpseo_score_readability'] );
54
            }
55
             return $columns;
56
        }
57
58
        /**
59
         * Add the menu items under the FooGalleru main menu
60
         */
61
        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...
62
            foogallery_add_submenu_page(
63
                __( 'Media Tags', 'foogallery' ),
64
                'manage_options',
65
                'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '&post_type=' . FOOGALLERY_CPT_GALLERY,
66
                null
67
            );
68
69
            foogallery_add_submenu_page(
70
                __( 'Media Collections', 'foogallery' ),
71
                'manage_options',
72
                'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION . '&post_type=' . FOOGALLERY_CPT_GALLERY,
73
                null
74
            );
75
        }
76
77
        /**
78
         * Make sure the tqaxonomy menu items are highlighted
79
         * @param $parent_file
80
         * @return mixed
81
         */
82
        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...
83
            global $submenu_file, $current_screen, $pagenow;
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...
84
85
            if ( $current_screen->post_type == FOOGALLERY_CPT_GALLERY ) {
86
87
                if ( 'edit-foogallery_attachment_tag' === $current_screen->id ) {
88
                    $submenu_file = 'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '&post_type=' . FOOGALLERY_CPT_GALLERY;
89
                }
90
91
                if ( 'edit-foogallery_attachment_collection' === $current_screen->id ) {
92
                    $submenu_file = 'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION . '&post_type=' . FOOGALLERY_CPT_GALLERY;
93
                }
94
            }
95
            return $parent_file;
96
        }
97
98
        /**
99
         * Register the taxonomies for attachments
100
         */
101
        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...
102
103
            $tag_args = array(
104
                'labels'            => array(
105
                    'name'              => __( 'Media Tags', 'foogallery' ),
106
                    'singular_name'     => __( 'Tag', 'foogallery' ),
107
                    'search_items'      => __( 'Search Tags', 'foogallery' ),
108
                    'all_items'         => __( 'All Tags', 'foogallery' ),
109
                    'parent_item'       => __( 'Parent Tag', 'foogallery' ),
110
                    'parent_item_colon' => __( 'Parent Tag:', 'foogallery' ),
111
                    'edit_item'         => __( 'Edit Tag', 'foogallery' ),
112
                    'update_item'       => __( 'Update Tag', 'foogallery' ),
113
                    'add_new_item'      => __( 'Add New Tag', 'foogallery' ),
114
                    'new_item_name'     => __( 'New Tag Name', 'foogallery' ),
115
                    'menu_name'         => __( 'Media Tags', 'foogallery' )
116
                ),
117
                'hierarchical'      => false,
118
                'query_var'         => true,
119
                'rewrite'           => false,
120
                'show_admin_column' => false,
121
                'show_in_menu'      => false,
122
                'update_count_callback' => '_update_generic_term_count'
123
            );
124
125
            register_taxonomy( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, 'attachment', $tag_args );
126
127
            $collection_args = array(
128
                'labels'            => array(
129
                    'name'              => __( 'Media Collections', 'foogallery' ),
130
                    'singular_name'     => __( 'Collection', 'foogallery' ),
131
                    'search_items'      => __( 'Search Collections', 'foogallery' ),
132
                    'all_items'         => __( 'All Collections', 'foogallery' ),
133
                    'parent_item'       => __( 'Parent Collection', 'foogallery' ),
134
                    'parent_item_colon' => __( 'Parent Collection:', 'foogallery' ),
135
                    'edit_item'         => __( 'Edit Collection', 'foogallery' ),
136
                    'update_item'       => __( 'Update Collection', 'foogallery' ),
137
                    'add_new_item'      => __( 'Add New Collection', 'foogallery' ),
138
                    'new_item_name'     => __( 'New Collection Name', 'foogallery' ),
139
                    'menu_name'         => __( 'Media Collections', 'foogallery' )
140
                ),
141
                'hierarchical'      => true,
142
                'query_var'         => true,
143
                'rewrite'           => false,
144
                'show_admin_column' => true,
145
                'show_in_menu'      => false,
146
                'update_count_callback' => '_update_generic_term_count'
147
            );
148
149
            register_taxonomy( FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION, 'attachment', $collection_args );
150
        }
151
152
        /**
153
         * Add the taxonomy fields to the attachment
154
         *
155
         * @param $fields array All fields that will be added to the media modal
156
		 * @param $post
157
         *
158
         * @return mixed
159
         */
160
        function add_taxonomy_fields( $fields, $post ) {
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...
161
162
			$fields[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG] = array(
163
				'show_in_edit' => false,
164
				'input' => 'html',
165
				'html' => $this->build_taxonomy_html( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, $post ),
166
				'label' => __( 'Tags', 'foogallery' )
167
			);
168
169
			$fields[FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION] = array(
170
				'show_in_edit' => false,
171
				'input' => 'html',
172
				'html' => $this->build_taxonomy_html( FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION, $post ),
173
				'label' => __( 'Collections', 'foogallery' )
174
			);
175
176
            return $fields;
177
        }
178
179
		/**
180
		 * Build up a taxonomy field HTML
181
		 *
182
		 * @param $taxonomy
183
		 * @param $post
184
		 *
185
		 * @return array
186
		 */
187
        function build_taxonomy_html( $taxonomy, $post ) {
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...
188
			//$terms_slugs = wp_get_post_terms( $post->ID, $taxonomy, 'ids' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% 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...
189
			$terms_slugs = array();
190
191
			$html = '<input type="text" id="attachments-' . $post->ID .'-' . $taxonomy . '" name="attachments[' . $post->ID .'][' . $taxonomy . ']" value="' . implode(',', $terms_slugs) . '" />';
192
193
			return $html;
194
		}
195
196
		/**
197
		 * Get terms sorted by hierarchy in a recursive way
198
		 *
199
		 * @param  string $taxonomy The taxonomy name
200
		 * @param  array $args The arguments which should be passed to the get_terms function
201
		 * @param  int $parent The terms parent id (for recursive usage)
202
		 * @param  int $level The current level (for recursive usage)
203
		 * @param  array $parents An array with all the parent terms (for recursive usage)
204
		 *
205
		 * @return array $terms_all An array with all the terms for this taxonomy
206
		 */
207
		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...
208
			//check if the taxonomy terms have already been built up
209
			if ( 0 === $parent && array_key_exists( $taxonomy, $this->cached_terms ) ) {
210
				return $this->cached_terms[$taxonomy];
211
			}
212
213
			$terms_all = array();
214
215
			$args['parent'] = $args['child_of'] = $parent;
216
217
			$terms = get_terms($taxonomy, $args);
218
219
			foreach($terms as $term) {
220
				$term->level = $level;
221
				$term->parents = $parents;
222
				$term_parents = $parents;
223
				$term_parents[] = $term->name;
224
				$terms_all[] = $term;
225
				$terms_sub = $this->build_terms_recursive($taxonomy, $args, $term->term_id, $level + 1, $term_parents);
226
227
				if(!empty($terms_sub)) {
228
					$terms_all = array_merge($terms_all, $terms_sub);
229
				}
230
			}
231
232
			//cache what we have built up
233
			if ( 0 === $parent && !array_key_exists( $taxonomy, $this->cached_terms ) ) {
234
				$this->cached_terms[$taxonomy] = $terms_all;
235
			}
236
237
			return $terms_all;
238
		}
239
240
        /**
241
         * Remove the automatically added attachments fields
242
         * @param $fields
243
         *
244
         * @return mixed
245
         */
246
        function remove_taxonomy_fields( $fields ) {
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
            if ( array_key_exists( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, $fields ) ) {
248
                unset( $fields[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG] );
249
            }
250
251
            if ( array_key_exists( FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION, $fields ) ) {
252
                unset( $fields[FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION] );
253
            }
254
255
            return $fields;
256
        }
257
258
        /**
259
         * Customize the media tag field to make sure we output a checkboxlist
260
         * @param $field_values
261
         *
262
         * @return mixed
263
         */
264
        function customize_media_tag_field( $field_values, $post_id ) {
265
266
            $media_tags = array();
267
268
            //get the terms linked to the attachment
269
            $terms = get_the_terms( $post_id, FOOGALLERY_ATTACHMENT_TAXONOMY_TAG );
270
            if ( $terms && ! is_wp_error( $terms ) ) {
271
                foreach ( $terms as $term ) {
272
                    $media_tags[ $term->term_id ] = $term->name;
273
                }
274
            }
275
276
            //set to html
277
            $field_values['input'] = 'html';
278
279
            $html = '';
280
            $i = 0;
281
282
            if ( ! empty( $field_values['options'] ) ) {
283
284
                foreach ( $field_values['options'] as $k => $v ) {
285
                    if ( array_key_exists( $k, $media_tags ) ) {
286
                        $checked = ' checked="checked"';
287
                    } else {
288
                        $checked = '';
289
                    }
290
291
                    $html .= '<input' . $checked . ' value="' . $k . '" type="checkbox" name="attachments[' . $post_id . '][' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '][' . $k . ']" id="' . sanitize_key( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '_' . $post_id . '_' . $i ) . '" /> <label for="' . sanitize_key( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '_' . $post_id . '_' . $i ) . '">' . $v . '</label> ';
292
                    $i++;
293
                }
294
            }
295
296
            if ( 0 === $i ) {
297
                $html .= __( 'No Tags Available!', 'foogallery' );
298
            }
299
300
            $html .= '<style>.compat-field-foogallery_media_tags .field input {margin-right: 0px;} .compat-field-foogallery_media_tags .field label {vertical-align: bottom; margin-right: 10px;}</style>';
301
302
            $html .= '<br /><a target="_blank" href="' . admin_url( 'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '&post_type=attachment' ) . '">' . __( 'Manage Tags', 'foogallery' ) . '</a>';
303
304
            $field_values['value'] = '';
305
            $field_values['html'] = $html;
306
307
            return $field_values;
308
        }
309
310
        /**
311
         * Save the tags for the attachment
312
         *
313
         * @param $field
314
         * @param $values
315
         * @param $post
316
         * @param $attachment
317
         */
318
        function save_media_tag_field($field, $values, $post, $attachment) {
0 ignored issues
show
Unused Code introduced by
The parameter $values 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...
319
            $post_id = $post['ID'];
320
321
            //first clear any tags for the post
322
            wp_delete_object_term_relationships( $post_id, FOOGALLERY_ATTACHMENT_TAXONOMY_TAG );
323
324
            $tag_ids = $attachment[ $field ];
325
326
            if ( !empty( $tag_ids ) ) {
327
                //clean tag ids
328
                $tag_ids = array_keys( $tag_ids );
329
                $tag_ids = array_map( 'intval', $tag_ids );
330
                $tag_ids = array_unique( $tag_ids );
331
332
                $term_taxonomy_ids = wp_set_object_terms( $post_id, $tag_ids, FOOGALLERY_ATTACHMENT_TAXONOMY_TAG );
333
334
                if ( is_wp_error( $term_taxonomy_ids ) ) {
335
                    // There was an error somewhere and the terms couldn't be set.
336
                    $post['errors'][ $field ]['errors'][] = __( 'Error saving the tags for the attachment!', 'foogallery' );
337
                }
338
            }
339
        }
340
341
342
        /***
343
         *
344
         * Add a tag filter to the attachments listing page
345
         */
346
        function add_collection_filter() {
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...
347
            global $pagenow;
348
            if ( 'upload.php' == $pagenow ) {
349
350
                $dropdown_options = array(
351
                    'taxonomy'        => FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION,
352
                    'show_option_all' => __( 'All Collections' ),
353
                    'hide_empty'      => false,
354
                    'hierarchical'    => true,
355
                    'orderby'         => 'name',
356
                    'show_count'      => true,
357
                    'walker'          => new foogallery_walker_category_dropdown(),
358
                    'value'           => 'slug'
359
                );
360
361
                wp_dropdown_categories( $dropdown_options );
362
            }
363
        }
364
    }
365
}
366