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 — develop ( 3bd383...ce1d31 )
by Brad
02:29
created

FooGallery_Admin_Settings::ajax_uninstall()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
if ( ! class_exists( 'FooGallery_Admin_Settings' ) ) {
4
5
	/**
6
	 * Class FooGallery_Admin_Settings
7
	 */
8
	class FooGallery_Admin_Settings {
9
10
		function __construct() {
11
			add_filter( 'foogallery_admin_settings', array( $this, 'create_settings' ), 10, 2 );
12
			add_action( 'foogallery_admin_settings_custom_type_render_setting', array( $this, 'render_custom_setting_types' ) );
13
			add_action( 'foogallery_admin_settings_after_render_setting', array( $this, 'after_render_setting' ) );
14
15
			// Ajax calls
16
			add_action( 'wp_ajax_foogallery_clear_css_optimizations', array( $this, 'ajax_clear_css_optimizations' ) );
17
			add_action( 'wp_ajax_foogallery_thumb_generation_test', array( $this, 'ajax_thumb_generation_test' ) );
18
			add_action( 'wp_ajax_foogallery_apply_retina_defaults', array( $this, 'ajax_apply_retina_defaults' ) );
19
			add_action( 'wp_ajax_foogallery_uninstall', array( $this, 'ajax_uninstall' ) );
20
		}
21
22
		/**
23
		 * Create the settings for FooGallery
24
		 * @return array
25
		 */
26
		function create_settings() {
27
28
			//region General Tab
29
			$tabs['general'] = __( 'General', 'foogallery' );
30
31
			$settings[] = array(
32
				'id'      => 'clear_css_optimizations',
33
				'title'   => __( 'Clear CSS Cache', 'foogallery' ),
34
				'desc'    => sprintf( __( '%s optimizes the way it loads gallery stylesheets to improve page performance. This can lead to the incorrect CSS being loaded in some cases. Use this button to clear all the CSS optimizations that have been cached across all galleries.', 'foogallery' ), foogallery_plugin_name() ),
35
				'type'    => 'clear_optimization_button',
36
				'tab'     => 'general',
37
				'section' => __( 'Cache', 'foogallery' )
38
			);
39
40
	        $gallery_templates = foogallery_gallery_templates();
41
			$gallery_templates_choices = array();
42
			foreach ( $gallery_templates as $template ) {
43
				$gallery_templates_choices[ $template['slug'] ] = $template['name'];
44
			}
45
46
			$settings[] = array(
47
				'id'      => 'gallery_template',
48
				'title'   => __( 'Default Gallery Template', 'foogallery' ),
49
				'desc'    => __( 'The default gallery template to use for new galleries', 'foogallery' ),
50
				'default' => foogallery_get_default( 'gallery_template' ) ,
51
				'type'    => 'select',
52
				'choices' => $gallery_templates_choices,
53
				'tab'     => 'general',
54
				'section' => __( 'Gallery Defaults', 'foogallery' )
55
			);
56
57
			$settings[] = array(
58
				'id'      => 'gallery_sorting',
59
				'title'   => __( 'Default Gallery Sorting', 'foogallery' ),
60
				'desc'    => __( 'The default attachment sorting to use for new galleries', 'foogallery' ),
61
				'default' => '',
62
				'type'    => 'select',
63
				'choices' => foogallery_sorting_options(),
64
				'tab'     => 'general',
65
				'section' => __( 'Gallery Defaults', 'foogallery' )
66
			);
67
68
			$galleries = foogallery_get_all_galleries();
69
			$gallery_choices = array();
70
			$gallery_choices[] = __( 'No default', 'foogallery' );
71
			foreach ( $galleries as $gallery ) {
72
				$gallery_choices[ $gallery->ID ] = $gallery->name;
73
			}
74
75
			$settings[] = array(
76
				'id'      => 'default_gallery_settings',
77
				'title'   => __( 'Default Gallery Settings', 'foogallery' ),
78
				'desc'    => __( 'When creating a new gallery, it can use the settings from an existing gallery as the default settings. This will save you time when creating many galleries that all have the same look and feel.', 'foogallery' ),
79
				'type'    => 'select',
80
				'choices' => $gallery_choices,
81
				'tab'     => 'general',
82
				'section' => __( 'Gallery Defaults', 'foogallery' )
83
			);
84
85
			$settings[] = array(
86
				'id'      => 'caption_title_source',
87
				'title'   => __( 'Caption Title Source', 'foogallery' ),
88
				'desc'    => __( 'By default, image caption titles are pulled from the attachment "Caption" field. Alternatively, you can also choose to pull from the attachment "Title" field.', 'foogallery' ),
89
				'type'    => 'select',
90
				'choices' => array(
91
					'caption' => __('Attachment Caption Field', 'foogallery'),
92
					'title' => __('Attachment Title Field', 'foogallery')
93
				),
94
				'default' => 'caption',
95
				'tab'     => 'general',
96
				'section' => __( 'Captions', 'foogallery' ),
97
				'spacer'  => '<span class="spacer"></span>'
98
			);
99
100
			$settings[] = array(
101
					'id'      => 'caption_desc_source',
102
					'title'   => __( 'Caption Description Source', 'foogallery' ),
103
					'desc'    => __( 'By default, image caption descriptions are pulled from the attachment "Description" field. Alternatively, you can choose to use other fields.', 'foogallery' ),
104
					'type'    => 'select',
105
					'choices' => array(
106
							'desc' => __('Attachment Description Field', 'foogallery'),
107
							'title' => __('Attachment Title Field', 'foogallery'),
108
							'caption' => __('Attachment Caption Field', 'foogallery'),
109
							'alt' => __('Attachment Alt Field', 'foogallery')
110
					),
111
					'default' => 'desc',
112
					'tab'     => 'general',
113
					'section' => __( 'Captions', 'foogallery' ),
114
					'spacer'  => '<span class="spacer"></span>'
115
			);
116
117
			$settings[] = array(
118
				'id'      => 'hide_gallery_template_help',
119
				'title'   => __( 'Hide Gallery Template Help', 'foogallery' ),
120
				'desc'    => __( 'Some gallery templates show helpful tips, which are useful for new users. You can choose to hide these tips.', 'foogallery' ),
121
				'type'    => 'checkbox',
122
				'tab'     => 'general',
123
				'section' => __( 'Admin', 'foogallery' )
124
			);
125
126
			$settings[] = array(
127
				'id'      => 'hide_editor_button',
128
				'title'   => __( 'Hide WYSIWYG Editor Button', 'foogallery' ),
129
				'desc'    => sprintf( __( 'If enabled, this will hide the "Add %s" button in the WYSIWYG editor.', 'foogallery' ), foogallery_plugin_name() ),
130
				'type'    => 'checkbox',
131
				'tab'     => 'general',
132
				'section' => __( 'Admin', 'foogallery' )
133
			);
134
135
			//endregion General
136
137
	        //region Extensions Tab
138
	        $tabs['extensions'] = __( 'Extensions', 'foogallery' );
139
140
	        $settings[] = array(
141
		        'id'      => 'use_future_endpoint',
142
		        'title'   => __( 'Use Beta Endpoint', 'foogallery' ),
143
		        'desc'    => __( 'The list of available extensions are pulled from an external URL. You can also pull from a "beta" endpoint which will sometimes contain beta extensions that are not publicly available.', 'foogallery' ),
144
		        'type'    => 'checkbox',
145
		        'tab'     => 'extensions',
146
	        );
147
			//endregion Extensions Tab
148
149
			//region Images Tab
150
			$tabs['thumb'] = __( 'Images', 'foogallery' );
151
152
			$settings[] = array(
153
				'id'      => 'thumb_jpeg_quality',
154
				'title'   => __( 'Thumbnail JPEG Quality', 'foogallery' ),
155
				'desc'    => __( 'The image quality to be used when resizing JPEG images.', 'foogallery' ),
156
				'type'    => 'text',
157
				'default' => '80',
158
				'tab'     => 'thumb'
159
			);
160
161
			$settings[] = array(
162
				'id'      => 'default_retina_support',
163
				'title'   => __( 'Default Retina Support', 'foogallery' ),
164
				'desc'    => __( 'Default retina support for all new galleries that are created. This can also be overridden for each gallery.', 'foogallery' ),
165
				'type'    => 'checkboxlist',
166
				'choices' => foogallery_retina_options(),
167
				'tab'     => 'thumb'
168
			);
169
170
			$settings[] = array(
171
					'id'      => 'use_original_thumbs',
172
					'title'   => __( 'Use Original Thumbnails', 'foogallery' ),
173
					'desc'    => __( 'Allow for the original thumbnails to be used when possible. This can be useful if your thumbs are animated gifs.<br/>PLEASE NOTE : this will only work if your gallery thumbnail sizes are identical to your thumbnail sizes under Settings -> Media.', 'foogallery' ),
174
					'type'    => 'checkbox',
175
					'tab'     => 'thumb'
176
			);
177
178
			$settings[] = array(
179
				'id'      => 'thumb_resize_animations',
180
				'title'   => __( 'Resize Animated GIFs', 'foogallery' ),
181
				'desc'    => __( 'Should animated gifs be resized or not. If enabled, only the first frame is used in the resize.', 'foogallery' ),
182
				'type'    => 'checkbox',
183
				'tab'     => 'thumb'
184
			);
185
186
			$settings[] = array(
187
				'id'      => 'animated_gif_use_original_image',
188
				'title'   => __( 'Show Animated Thumbnails', 'foogallery' ),
189
				'desc'    => __( 'If animated GIFs are used, then show the original GIF as the thumbnail.', 'foogallery' ),
190
				'type'    => 'checkbox',
191
				'tab'     => 'thumb'
192
			);
193
194
			$settings[] = array(
195
				'id'      => 'thumb_generation_test',
196
				'title'   => __( 'Thumbnail Generation Test', 'foogallery' ),
197
				'desc'    => sprintf( __( 'Test to see if %s can generate the thumbnails it needs.', 'foogallery' ), foogallery_plugin_name() ),
198
				'type'    => 'thumb_generation_test',
199
				'tab'     => 'thumb'
200
			);
201
202
			//endregion Thumbnail Tab
203
204
//	        //region Advanced Tab
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
205
//	        $tabs['advanced'] = __( 'Advanced', 'foogallery' );
206
//
207
//	        $example_url = '<code>' . trailingslashit( site_url() ) . foogallery_permalink() . '/my-cool-gallery</code>';
208
//
209
//	        $settings[] = array(
210
//		        'id'      => 'gallery_permalinks_enabled',
211
//		        'title'   => __( 'Enable Friendly URL\'s', 'foogallery' ),
212
//		        'desc'    => sprintf( __( 'If enabled, you will be able to access your galleries from a friendly URL e.g. %s', 'foogallery' ), $example_url ),
213
//		        'default' => foogallery_get_default( 'gallery_permalinks_enabled' ),
214
//		        'type'    => 'checkbox',
215
//		        'tab'     => 'advanced',
216
//	        );
217
//
218
//	        $settings[] = array(
219
//		        'id'      => 'gallery_permalink',
220
//		        'title'   => __( 'Gallery Permalink', 'foogallery' ),
221
//		        'desc'    => __( 'If friendly URL\'s are enabled, this is used in building up a friendly URL', 'foogallery' ),
222
//		        'default' => foogallery_get_default( 'gallery_permalink' ),
223
//		        'type'    => 'text',
224
//		        'tab'     => 'advanced',
225
//	        );
226
//	        //endregion Advanced
227
228
			//region Language Tab
229
			$tabs['language'] = __( 'Language', 'foogallery' );
230
231
			$settings[] = array(
232
				'id'      => 'language_images_count_none_text',
233
				'title'   => __( 'Image Count None Text', 'foogallery' ),
234
				'type'    => 'text',
235
				'default' => __( 'No images', 'foogallery' ),
236
				'tab'     => 'language'
237
			);
238
239
			$settings[] = array(
240
				'id'      => 'language_images_count_single_text',
241
				'title'   => __( 'Image Count Single Text', 'foogallery' ),
242
				'type'    => 'text',
243
				'default' => __( '1 image', 'foogallery' ),
244
				'tab'     => 'language'
245
			);
246
247
			$settings[] = array(
248
				'id'      => 'language_images_count_plural_text',
249
				'title'   => __( 'Image Count Many Text', 'foogallery' ),
250
				'type'    => 'text',
251
				'default' => __( '%s images', 'foogallery' ),
252
				'tab'     => 'language'
253
			);
254
			//endregion Language Tab
255
256
			//region Uninstall Tab
257
			$tabs['uninstall'] = __( 'Uninstall', 'foogallery' );
258
259
			$settings[] = array(
260
				'id'      => 'uninstall',
261
				'title'   => __( 'Full Uninstall', 'foogallery' ),
262
				'desc'    => sprintf( __( 'Run a full uninstall of %s, which includes removing all galleries, settings and metadata. This basically removes all traces of the plugin from your system. Please be careful - there is no undo!', 'foogallery' ), foogallery_plugin_name() ),
263
				'type'    => 'uninstall',
264
				'tab'     => 'uninstall'
265
			);
266
267
			//endregion Uninstall Tab
268
269
			return apply_filters( 'foogallery_admin_settings_override', array(
270
				'tabs'     => $tabs,
271
				'sections' => array(),
272
				'settings' => $settings,
273
			) );
274
		}
275
276
		/**
277
		 * Render any custom setting types to the settings page
278
		 */
279
		function render_custom_setting_types( $args ) {
280
			if ( 'clear_optimization_button' === $args['type'] ) { ?>
281
				<div id="foogallery_clear_css_optimizations_container">
282
					<input type="button" data-nonce="<?php echo esc_attr( wp_create_nonce( 'foogallery_clear_css_optimizations' ) ); ?>" class="button-primary foogallery_clear_css_optimizations" value="<?php _e( 'Clear CSS Optimization Cache', 'foogallery' ); ?>">
283
					<span id="foogallery_clear_css_cache_spinner" style="position: absolute" class="spinner"></span>
284
				</div>
285
			<?php } else if ( 'uninstall' === $args['type'] ) { ?>
286
				<div id="foogallery_uninstall_container">
287
					<input type="button" data-nonce="<?php echo esc_attr( wp_create_nonce( 'foogallery_uninstall' ) ); ?>" class="button-primary foogallery_uninstall" value="<?php _e( 'Run Full Uninstall', 'foogallery' ); ?>">
288
					<span id="foogallery_uninstall_spinner" style="position: absolute" class="spinner"></span>
289
				</div>
290
			<?php } else if ( 'thumb_generation_test' === $args['type'] ) { ?>
291
				<div id="foogallery_thumb_generation_test_container">
292
					<input type="button" data-nonce="<?php echo esc_attr( wp_create_nonce( 'foogallery_thumb_generation_test' ) ); ?>" class="button-primary foogallery_thumb_generation_test" value="<?php _e( 'Run Tests', 'foogallery' ); ?>">
293
					<span id="foogallery_thumb_generation_test_spinner" style="position: absolute" class="spinner"></span>
294
				</div>
295
			<?php }
296
		}
297
298
		function after_render_setting( $args ) {
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...
299
			if ( 'default_retina_support' === $args['id'] ) {
300
301
				//build up a list of retina options and add them to a hidden input
302
				// so we can get the values on the client
303
				$input_ids = array();
304
				$count = 0;
305
				foreach( foogallery_retina_options() as $retina_option ) {
306
					$input_ids[] = '#default_retina_support' . $count;
307
					$count++;
308
				}
309
				$nonce = wp_create_nonce( 'foogallery_apply_retina_defaults' );
310
				?><div id="foogallery_apply_retina_support_container">
311
					<input type="button" data-inputs="<?php echo implode( ',', $input_ids ); ?>" data-nonce="<?php echo esc_attr( $nonce ); ?>" class="button-primary foogallery_apply_retina_support" value="<?php _e( 'Apply Defaults to all Galleries', 'foogallery' ); ?>">
312
					<span id="foogallery_apply_retina_support_spinner" style="position: absolute" class="spinner"></span>
313
				</div>
314
			<?php }
315
		}
316
317
		/**
318
		 * AJAX endpoint for clearing all CSS optimizations
319
		 */
320
		function ajax_clear_css_optimizations() {
321
			if ( check_admin_referer( 'foogallery_clear_css_optimizations' ) ) {
322
				foogallery_clear_all_css_load_optimizations();
323
324
				_e('The CSS optimization cache was successfully cleared!', 'foogallery' );
325
				die();
326
			}
327
		}
328
329
		/**
330
		 * AJAX endpoint for testing thumbnail generation using WPThumb
331
		 */
332
		function ajax_thumb_generation_test() {
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...
333
			if ( check_admin_referer( 'foogallery_thumb_generation_test' ) ) {
334
				foogallery_output_thumbnail_generation_results();
335
				die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method ajax_thumb_generation_test() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
336
			}
337
		}
338
339
		/**
340
		 * AJAX endpoint for applying the retina defaults to all galleries
341
		 */
342
		function ajax_apply_retina_defaults() {
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...
Coding Style introduced by
ajax_apply_retina_defaults 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...
343
			if ( check_admin_referer( 'foogallery_apply_retina_defaults' ) ) {
344
345
				$defaults = $_POST['defaults'];
346
347
				//extract the settings using a regex
348
				$regex = '/foogallery\[default_retina_support\|(?<setting>.+?)\]/';
349
350
				preg_match_all($regex, $defaults, $matches);
351
352
				$gallery_retina_settings = array();
353
354
				if ( isset( $matches[1] ) ) {
355
					foreach ( $matches[1] as $match ) {
356
						$gallery_retina_settings[$match] = "true";
357
					}
358
				}
359
360
				//go through all galleries and update the retina settings
361
				$galleries = foogallery_get_all_galleries();
362
				$gallery_update_count = 0;
363
				foreach ( $galleries as $gallery ) {
364
					update_post_meta( $gallery->ID, FOOGALLERY_META_RETINA, $gallery_retina_settings );
365
					$gallery_update_count++;
366
				}
367
368
				echo sprintf( _n(
369
					'1 gallery successfully updated to use the default retina settings.',
370
					'%s galleries successfully updated to use the default retina settings.',
371
					$gallery_update_count, 'foogallery' ), $gallery_update_count );
372
373
				die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method ajax_apply_retina_defaults() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
374
			}
375
		}
376
377
		function ajax_uninstall() {
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...
378
			if ( check_admin_referer( 'foogallery_uninstall' ) && current_user_can( 'install_plugins' ) ) {
379
				foogallery_uninstall();
380
381
				_e('All traces of the plugin were removed from your system!', 'foogallery' );
382
				die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method ajax_uninstall() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
383
			}
384
		}
385
	}
386
}
387