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 ( 03581d )
by Brad
03:49
created

FooGallery_Attachment_Taxonomies::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 10
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
6
    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...
7
8
        /**
9
         * Class Constructor
10
         */function __construct() {
11
            add_action( 'init', array( $this, 'add_tags_to_attachments' ) );
12
            if ( is_admin() ) {
13
                add_filter( 'foogallery_attachment_custom_fields', array( $this, 'add_media_tag_field' ) );
14
                add_filter( 'foogallery_attachment_add_fields', array( $this, 'remove_taxonomy_media_tag_field') );
15
                add_filter( 'foogallery_attachment_field_taxonomy_tag', array( $this, 'customize_media_tag_field'), 10, 2 );
16
                add_filter( 'foogallery_attachment_save_field_taxonomy_tag', array( $this, 'save_media_tag_field' ), 10, 4 );
17
                add_action( 'restrict_manage_posts', array( $this, 'add_tag_filter' ) );
18
            }
19
        }
20
21
        /**
22
         * Register the tag taxonomy for attachments
23
         */
24
        function add_tags_to_attachments() {
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...
25
            $labels = array(
26
                'name'              => __( 'Tags', 'foogallery' ),
27
                'singular_name'     => __( 'Tag', 'foogallery' ),
28
                'search_items'      => __( 'Search Tags', 'foogallery' ),
29
                'all_items'         => __( 'All Tags', 'foogallery' ),
30
                'parent_item'       => __( 'Parent Tag', 'foogallery' ),
31
                'parent_item_colon' => __( 'Parent Tag:', 'foogallery' ),
32
                'edit_item'         => __( 'Edit Tag', 'foogallery' ),
33
                'update_item'       => __( 'Update Tag', 'foogallery' ),
34
                'add_new_item'      => __( 'Add New Tag', 'foogallery' ),
35
                'new_item_name'     => __( 'New Tag Name', 'foogallery' ),
36
                'menu_name'         => __( 'Tags', 'foogallery' )
37
            );
38
39
            $args = array(
40
                'labels'            => $labels,
41
                'hierarchical'      => false,
42
                'query_var'         => true,
43
                'rewrite'           => false,
44
                'show_admin_column' => 'true'
45
            );
46
47
            register_taxonomy( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, 'attachment', $args );
48
        }
49
50
        /**
51
         * Add a new tag field to the attachments
52
         *
53
         * @param $fields array All fields that will be added to the media modal
54
         *
55
         * @return mixed
56
         */
57
        function add_media_tag_field( $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...
58
            $args = array(
59
                'orderby'    => 'name',
60
                'order'      => 'ASC',
61
                'hide_empty' => false
62
            );
63
64
            //pull all terms
65
            $terms = get_terms( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, $args );
66
67
            $media_tags = array();
68
69
            foreach ( $terms as $term ) {
70
                $media_tags[ $term->term_id ] = $term->name;
71
            }
72
73
            $fields[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG] = array(
74
                'label'   => __( 'Tags', 'foogallery' ),
75
                'input'   => FOOGALLERY_ATTACHMENT_TAXONOMY_TAG,
76
                'helps'   => __( 'Categorize your attachments', 'foogallery' ),
77
                'options' => $media_tags,
78
                'exclusions'  => array()
79
            );
80
81
            return $fields;
82
        }
83
84
        /**
85
         * Remove the automatically added media tag field
86
         * @param $fields
87
         *
88
         * @return mixed
89
         */
90
        function remove_taxonomy_media_tag_field( $fields ) {
91
            if ( array_key_exists( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, $fields ) ) {
92
                unset( $fields[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG] );
93
            }
94
95
            return $fields;
96
        }
97
98
        /**
99
         * Customize the media tag field to make sure we output a checkboxlist
100
         * @param $field_values
101
         *
102
         * @return mixed
103
         */
104
        function customize_media_tag_field( $field_values, $post_id ) {
105
106
            $media_tags = array();
107
108
            //get the terms linked to the attachment
109
            $terms = get_the_terms( $post_id, FOOGALLERY_ATTACHMENT_TAXONOMY_TAG );
110
            if ( $terms && ! is_wp_error( $terms ) ) {
111
                foreach ( $terms as $term ) {
112
                    $media_tags[ $term->term_id ] = $term->name;
113
                }
114
            }
115
116
            //set to html
117
            $field_values['input'] = 'html';
118
119
            $html = '';
120
            $i = 0;
121
122
            if ( ! empty( $field_values['options'] ) ) {
123
124
                foreach ( $field_values['options'] as $k => $v ) {
125
                    if ( array_key_exists( $k, $media_tags ) ) {
126
                        $checked = ' checked="checked"';
127
                    } else {
128
                        $checked = '';
129
                    }
130
131
                    $html .= '<input' . $checked . ' value="' . $k . '" type="checkbox" name="attachments[' . $post_id . '][' . FOOGALLERY_MEDIA_CATEGORIES_EXTENSION_SLUG . '][' . $k . ']" id="' . sanitize_key( FOOGALLERY_MEDIA_CATEGORIES_EXTENSION_SLUG . '_' . $post_id . '_' . $i ) . '" /> <label for="' . sanitize_key( FOOGALLERY_MEDIA_CATEGORIES_EXTENSION_SLUG . '_' . $post_id . '_' . $i ) . '">' . $v . '</label> ';
132
                    $i++;
133
                }
134
            }
135
136
            if ( 0 === $i ) {
137
                $html .= __( 'No Tags Available!', 'foogallery' );
138
            }
139
140
            $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>';
141
142
            $html .= '<br /><a target="_blank" href="' . admin_url( 'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '&post_type=attachment' ) . '">' . __( 'Manage Tags', 'foogallery' ) . '</a>';
143
144
            $field_values['value'] = '';
145
            $field_values['html'] = $html;
146
147
            return $field_values;
148
        }
149
150
        /**
151
         * Save the tags for the attachment
152
         *
153
         * @param $field
154
         * @param $values
155
         * @param $post
156
         * @param $attachment
157
         */
158
        function save_media_tag_field($field, $values, $post, $attachment) {
159
            $post_id = $post['ID'];
160
161
            //first clear any tags for the post
162
            wp_delete_object_term_relationships( $post_id, FOOGALLERY_ATTACHMENT_TAXONOMY_TAG );
163
164
            $tag_ids = $attachment[ $field ];
165
166
            if ( !empty( $tag_ids ) ) {
167
                //clean tag ids
168
                $tag_ids = array_keys( $tag_ids );
169
                $tag_ids = array_map( 'intval', $tag_ids );
170
                $tag_ids = array_unique( $tag_ids );
171
172
                $term_taxonomy_ids = wp_set_object_terms( $post_id, $tag_ids, FOOGALLERY_ATTACHMENT_TAXONOMY_TAG );
173
174
                if ( is_wp_error( $term_taxonomy_ids ) ) {
175
                    // There was an error somewhere and the terms couldn't be set.
176
                    $post['errors'][ $field ]['errors'][] = __( 'Error saving the tags for the attachment!', 'foogallery' );
177
                }
178
            }
179
        }
180
181
182
        /***
183
         *
184
         * Add a tag filter
185
         */
186
        function add_tag_filter() {
187
            global $pagenow;
188
            if ( 'upload.php' == $pagenow ) {
189
190
                $dropdown_options = array(
191
                    'taxonomy'        => FOOGALLERY_ATTACHMENT_TAXONOMY_TAG,
192
                    'show_option_all' => __( 'View all tags' ),
193
                    'hide_empty'      => false,
194
                    'hierarchical'    => true,
195
                    'orderby'         => 'name',
196
                    'show_count'      => true,
197
                    //'walker'          => new foogallery_walker_category_dropdown(),
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...
198
                    'value'           => 'slug'
199
                );
200
201
                wp_dropdown_categories( $dropdown_options );
202
            }
203
        }
204
    }
205
}
206
207
/** Custom walker for wp_dropdown_categories, based on https://gist.github.com/stephenh1988/2902509 */
208
class foogallery_walker_category_dropdown extends Walker_CategoryDropdown{
209
210
    function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
211
        $pad = str_repeat( '&nbsp;', $depth * 3 );
212
        $cat_name = apply_filters( 'list_cats', $category->name, $category );
213
214
        if( ! isset( $args['value'] ) ) {
215
            $args['value'] = ( $category->taxonomy != 'category' ? 'slug' : 'id' );
216
        }
217
218
        $value = ( $args['value']=='slug' ? $category->slug : $category->term_id );
219
        if ( 0 == $args['selected'] && isset( $_GET['category_media'] ) && '' != $_GET['category_media'] ) {
220
            $args['selected'] = $_GET['category_media'];
221
        }
222
223
        $output .= '<option class="level-' . $depth . '" value="' . $value . '"';
224
        if ( $value === (string) $args['selected'] ) {
225
            $output .= ' selected="selected"';
226
        }
227
        $output .= '>';
228
        $output .= $pad . $cat_name;
229
        if ( $args['show_count'] )
230
            $output .= '&nbsp;&nbsp;(' . $category->count . ')';
231
232
        $output .= "</option>\n";
233
    }
234
}