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 — master ( 87bcf9...ef6a43 )
by Brad
03:01
created

FooGallery_Pro_Attachment_Taxonomies::init_all()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 0
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
if ( ! class_exists( 'FooGallery_Pro_Attachment_Taxonomies' ) ) {
3
4
    define( 'FOOGALLERY_ATTACHMENT_TAXONOMY_TAG', 'foogallery_attachment_tag' );
5
    define( 'FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY', 'foogallery_attachment_category' );
6
7
    class FooGallery_Pro_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
			add_action( 'foogallery_admin_settings_override', array( $this, 'add_admin_setting' ) );
17
        }
18
19
		/**
20
		 * Adds a setting to disable all FooGallery taxonomies
21
		 *
22
		 * @param $settings
23
		 *
24
		 * @return mixed
25
		 */
26
        function add_admin_setting($settings) {
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...
27
28
			$settings['settings'][] = array(
29
				'id'      => 'disable_attachment_taxonomies',
30
				'title'   => __( 'Disable Attachment Taxonomies', 'foogallery' ),
31
				'desc'    => sprintf( __( 'Disables the %s attachment taxonomies (Media Tags and Media Categories).', 'foogallery' ), foogallery_plugin_name() ),
32
				'type'    => 'checkbox',
33
				'tab'     => 'advanced'
34
			);
35
36
        	return $settings;
37
		}
38
39
		/**
40
		 * Initialize all the hooks if the taxonomies are not disabled
41
		 */
42
        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...
43
			if ( foogallery_get_setting( 'disable_attachment_taxonomies' ) === 'on' ) {
44
				return;
45
			}
46
47
			$this->add_taxonomies();
48
49
			if ( is_admin() ) {
50
				add_action( 'admin_menu', array( $this, 'add_menu_items' ), 1 );
51
				add_filter( 'parent_file', array( $this, 'set_current_menu' ) );
52
				add_filter( 'manage_media_columns', array( $this, 'change_attachment_column_names' ) );
53
				add_filter( 'manage_edit-foogallery_attachment_tag_columns', array( $this, 'clean_column_names' ), 999 );
54
				add_filter( 'manage_edit-foogallery_attachment_category_columns', array( $this, 'clean_column_names' ), 999 );
55
56
				//make the attachment taxonomies awesome
57
				add_action( 'admin_head', array( $this, 'include_inline_taxonomy_data_script' ) );
58
				add_filter( 'attachment_fields_to_edit', array( $this, 'inject_code_into_field' ), 10, 2 );
59
60
				add_action( 'wp_enqueue_media', array( $this, 'enqueue_js' ) );
61
62
				//ajax actions from the media modal
63
				add_action( 'wp_ajax_foogallery-taxonomies-add-term', array( $this, 'ajax_add_term' ) );
64
				add_action( 'wp_ajax_foogallery-taxonomies-save-terms', array( $this, 'ajax_save_terms' ) );
65
			}
66
		}
67
        /**
68
         * Save terms for an attachment
69
         *
70
         * @since 1.4.19
71
         */
72
        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...
73
        {
74
            $nonce = $_POST['nonce'];
75
            if (wp_verify_nonce($nonce, 'foogallery-attachment-taxonomy')) {
76
77
                $attachment_id = $_POST['attachment_id'];
78
                $terms = $_POST['terms'];
79
                $taxonomy = $_POST['taxonomy'];
80
81
                $result = wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $terms)), $taxonomy, false);
82
83
				clean_post_cache($attachment_id);
84
85
				if ( !is_wp_error( $result ) ) {
86
					wp_send_json( $terms );
87
				}
88
            }
89
            die();
90
        }
91
92
        /**
93
         * Add new term via an ajax call from admin
94
         *
95
         * @since 1.4.19
96
         * @access public
97
         */
98
        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...
99
            $nonce = $_POST['nonce'];
100
            if (wp_verify_nonce($nonce, 'foogallery-attachment-taxonomy')) {
101
102
                $new_term = wp_insert_term($_POST['term_label'], $_POST['taxonomy']);
103
104
                if (is_wp_error($new_term)) {
105
                    die();
106
                }
107
108
                $new_term_obj = null;
109
110
                if (isset($new_term['term_id'])) {
111
                    $new_term_obj = get_term($new_term['term_id']);
112
                }
113
114
                if (!is_wp_error($new_term_obj)) {
115
                    wp_send_json(array(
116
                        'new_term' => $new_term_obj,
117
                        'all_terms' => $this->build_terms_recursive($_POST['taxonomy'], array('hide_empty' => false))
118
                    ));
119
                }
120
            }
121
122
            die();
123
        }
124
125
		/**
126
		 * Enqueue admin script and styles
127
		 *
128
		 * @since 1.0.0
129
		 * @access public
130
		 * @static
131
		 */
132
		public function enqueue_js() {
133
			//enqueue selectize assets
134
			wp_enqueue_script( 'foogallery-selectize-core', FOOGALLERY_URL . 'lib/selectize/selectize.min.js', array('jquery'), FOOGALLERY_VERSION );
135
			wp_enqueue_script( 'foogallery-selectize', FOOGALLERY_URL . 'lib/selectize/foogallery.selectize.js', array('foogallery-selectize-core'), FOOGALLERY_VERSION );
136
			wp_enqueue_style(  'foogallery-selectize', FOOGALLERY_URL . 'lib/selectize/selectize.css', array(), FOOGALLERY_VERSION );
137
138
			//enqueue media attachment autosave script
139
            wp_enqueue_script( 'foogallery-attachment-autosave', FOOGALLERY_URL . 'js/admin-foogallery-attachment-autosave.js', array('media-views') );
140
141
			$this->include_inline_taxonomy_data_script();
142
		}
143
144
		/**
145
		 * Add fields to attachment
146
		 *
147
		 * @since 1.0.0
148
		 * @access public
149
		 * @static
150
		 * @param array $fields An array with all fields to edit
151
		 * @param object $post An object for the current post
152
		 * @return array $fields An array with all fields to edit
153
		 */
154
		public function inject_code_into_field($fields, $post) {
155
			if ( array_key_exists( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, $fields ) ) {
156
157
				$value = trim( $fields[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG]['value'] );
158
159
				$fields[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG] = array(
160
					'show_in_edit' => false,
161
					'input' => 'html',
162
					'html' => $this->build_taxonomy_html( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, $post, $value ),
163
					'label' => __( 'Media Tags', 'foogallery' )
164
				);
165
			}
166
167
			if ( array_key_exists( FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY, $fields ) ) {
168
169
				$value = trim( $fields[FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY]['value'] );
170
171
				$fields[FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY] = array(
172
					'show_in_edit' => false,
173
					'input' => 'html',
174
					'html' => $this->build_taxonomy_html( FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY, $post, $value ),
175
					'label' => __( 'Media Categories', 'foogallery' )
176
				);
177
			}
178
179
			return $fields;
180
		}
181
182
		/**
183
		 * Add custom js into admin head so that we can build up decent taxonomy selectize controls
184
		 *
185
		 * @since 1.0.0
186
		 * @access public
187
		 * @static
188
		 */
189
		public function include_inline_taxonomy_data_script() {
190
			$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...
191
				'slug' => FOOGALLERY_ATTACHMENT_TAXONOMY_TAG,
192
				'terms' => $this->build_terms_recursive(FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, array('hide_empty' => false)),
193
				'query_var' => true,
194
				'labels' => array(
195
					'placeholder' => __( 'Select tags, or add a new tag...', 'foogallery' ),
196
					'add' => __( 'Add new tag', 'foogallery' )
197
				),
198
			);
199
200
			$taxonomy_data[FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY] = array(
201
				'slug' => FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY,
202
				'terms' => $this->build_terms_recursive(FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY, array('hide_empty' => false)),
203
				'query_var' => true,
204
				'labels' => array(
205
					'placeholder' => __( 'Select categories, or add a new category...', 'foogallery' ),
206
					'add' => __( 'Add new category', 'foogallery' )
207
				),
208
			);
209
210
			$taxonomy_data['nonce'] = wp_create_nonce( 'foogallery-attachment-taxonomy' );
211
212
			wp_add_inline_script( 'foogallery-selectize', 'window.FOOGALLERY_TAXONOMY_DATA = ' . json_encode($taxonomy_data) . ';' );
213
		}
214
215
        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...
216
217
             if ( array_key_exists( 'taxonomy-foogallery_attachment_category', $columns ) ) {
218
                 $columns['taxonomy-foogallery_attachment_category'] = __('Categories', 'foogallery');
219
             }
220
221
             return $columns;
222
        }
223
224
        /**
225
         * Clean up the taxonomy columns for WP Seo plugin
226
         *
227
         * @param $columns
228
         * @return mixed
229
         */
230
        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...
231
232
             //cleanup wpseo columns!
233
             if ( array_key_exists( 'wpseo_score', $columns ) ) {
234
                 unset( $columns['wpseo_score'] );
235
             }
236
            if ( array_key_exists( 'wpseo_score_readability', $columns ) ) {
237
                unset( $columns['wpseo_score_readability'] );
238
            }
239
             return $columns;
240
        }
241
242
        /**
243
         * Add the menu items under the FooGallery main menu
244
         */
245
        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...
246
            foogallery_add_submenu_page(
247
                __( 'Media Tags', 'foogallery' ),
248
                'manage_options',
249
                'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '&post_type=' . FOOGALLERY_CPT_GALLERY,
250
                null
251
            );
252
253
            foogallery_add_submenu_page(
254
                __( 'Media Categories', 'foogallery' ),
255
                'manage_options',
256
                'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY . '&post_type=' . FOOGALLERY_CPT_GALLERY,
257
                null
258
            );
259
        }
260
261
        /**
262
         * Make sure the taxonomy menu items are highlighted
263
         * @param $parent_file
264
         * @return mixed
265
         */
266
        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...
267
            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...
268
269
            if ( $current_screen->post_type == FOOGALLERY_CPT_GALLERY ) {
270
271
                if ( 'edit-foogallery_attachment_tag' === $current_screen->id ) {
272
                    $submenu_file = 'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '&post_type=' . FOOGALLERY_CPT_GALLERY;
273
                }
274
275
                if ( 'edit-foogallery_attachment_category' === $current_screen->id ) {
276
                    $submenu_file = 'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY . '&post_type=' . FOOGALLERY_CPT_GALLERY;
277
                }
278
            }
279
280
            return $parent_file;
281
        }
282
283
        /**
284
         * Register the taxonomies for attachments
285
         */
286
        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...
287
288
            $tag_args = array(
289
                'labels'            => array(
290
                    'name'              => __( 'Media Tags', 'foogallery' ),
291
                    'singular_name'     => __( 'Tag', 'foogallery' ),
292
                    'search_items'      => __( 'Search Tags', 'foogallery' ),
293
                    'all_items'         => __( 'All Tags', 'foogallery' ),
294
                    'parent_item'       => __( 'Parent Tag', 'foogallery' ),
295
                    'parent_item_colon' => __( 'Parent Tag:', 'foogallery' ),
296
                    'edit_item'         => __( 'Edit Tag', 'foogallery' ),
297
                    'update_item'       => __( 'Update Tag', 'foogallery' ),
298
                    'add_new_item'      => __( 'Add New Tag', 'foogallery' ),
299
                    'new_item_name'     => __( 'New Tag Name', 'foogallery' ),
300
                    'menu_name'         => __( 'Media Tags', 'foogallery' )
301
                ),
302
                'hierarchical'      => false,
303
                'query_var'         => true,
304
                'rewrite'           => false,
305
                'show_admin_column' => false,
306
                'show_in_menu'      => false,
307
                'update_count_callback' => '_update_generic_term_count'
308
            );
309
310
            register_taxonomy( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, 'attachment', $tag_args );
311
312
            $category_args = array(
313
                'labels'            => array(
314
                    'name'              => __( 'Media Categories', 'foogallery' ),
315
                    'singular_name'     => __( 'Category', 'foogallery' ),
316
                    'search_items'      => __( 'Search Categories', 'foogallery' ),
317
                    'all_items'         => __( 'All Categories', 'foogallery' ),
318
                    'parent_item'       => __( 'Parent Category', 'foogallery' ),
319
                    'parent_item_colon' => __( 'Parent Category:', 'foogallery' ),
320
                    'edit_item'         => __( 'Edit Category', 'foogallery' ),
321
                    'update_item'       => __( 'Update Category', 'foogallery' ),
322
                    'add_new_item'      => __( 'Add New Category', 'foogallery' ),
323
                    'new_item_name'     => __( 'New Category Name', 'foogallery' ),
324
                    'menu_name'         => __( 'Media Categories', 'foogallery' )
325
                ),
326
                'hierarchical'      => true,
327
                'query_var'         => true,
328
                'rewrite'           => false,
329
                'show_admin_column' => true,
330
                'show_in_menu'      => false,
331
                'update_count_callback' => '_update_generic_term_count'
332
            );
333
334
            register_taxonomy( FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY, 'attachment', $category_args );
335
        }
336
337
		/**
338
		 * Build up a taxonomy field HTML
339
		 *
340
		 * @param $taxonomy
341
		 * @param $post
342
		 *
343
		 * @return array
344
		 */
345
        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...
346
			$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 . '" />';
347
			$html .= '<script type="script/javascript">
348
				FOOGALLERY_SELECTIZE(\'#attachments-' . $post->ID .'-' . $taxonomy . '\', \'' . $taxonomy .'\');
349
				</script>';
350
			return $html;
351
		}
352
353
		/**
354
		 * Get terms sorted by hierarchy in a recursive way
355
		 *
356
		 * @param  string $taxonomy The taxonomy name
357
		 * @param  array $args The arguments which should be passed to the get_terms function
358
		 * @param  int $parent The terms parent id (for recursive usage)
359
		 * @param  int $level The current level (for recursive usage)
360
		 * @param  array $parents An array with all the parent terms (for recursive usage)
361
		 *
362
		 * @return array $terms_all An array with all the terms for this taxonomy
363
		 */
364
		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...
365
			//check if the taxonomy terms have already been built up
366
			if ( 0 === $parent && array_key_exists( $taxonomy, $this->cached_terms ) ) {
367
				return $this->cached_terms[$taxonomy];
368
			}
369
370
			$terms_all = array();
371
372
			$args['parent'] = $args['child_of'] = $parent;
373
374
			$terms = get_terms($taxonomy, $args);
375
376
			foreach($terms as $term) {
377
				$term->level = $level;
378
				$term->parents = $parents;
379
				$term_parents = $parents;
380
				$term_parents[] = $term->name;
381
				$terms_all[] = $term;
382
				$terms_sub = $this->build_terms_recursive($taxonomy, $args, $term->term_id, $level + 1, $term_parents);
383
384
				if(!empty($terms_sub)) {
385
					$terms_all = array_merge($terms_all, $terms_sub);
386
				}
387
			}
388
389
			//cache what we have built up
390
			if ( 0 === $parent && !array_key_exists( $taxonomy, $this->cached_terms ) ) {
391
				$this->cached_terms[$taxonomy] = $terms_all;
392
			}
393
394
			return $terms_all;
395
		}
396
    }
397
}
398