Post_Types   B
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 333
Duplicated Lines 6.01 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 20
loc 333
rs 8.3999
c 0
b 0
f 0
wmc 38
lcom 0
cbo 3

11 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 27 1
A add_calendar_feed_column_headers() 0 12 1
D calendar_feed_column_content() 0 39 9
A row_actions() 0 10 2
A edit_table_hooks() 0 10 2
B duplicate_feed() 0 28 5
A default_event_template() 0 3 2
A clear_cache_button() 0 11 3
B add_shortcode_button() 0 24 3
B add_shortcode_panel() 0 32 5
B bulk_actions() 20 41 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Admin Post Types
4
 *
5
 * @package SimpleCalendar\Admin
6
 */
7
namespace SimpleCalendar\Admin;
8
9
use SimpleCalendar\Abstracts\Calendar;
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * Admin post types.
17
 *
18
 * Handles admin views and custom content for post types and taxonomies in admin dashboard screens.
19
 *
20
 * @since 3.0.0
21
 */
22
class Post_Types {
23
24
	/**
25
	 * Hook in tabs.
26
	 *
27
	 * @since 3.0.0
28
	 */
29
	public function __construct() {
30
31
		// Add meta boxes to custom content.
32
		new Meta_Boxes();
33
34
		// Add column headers in calendar feeds admin archives.
35
		add_filter( 'manage_calendar_posts_columns', array( $this, 'add_calendar_feed_column_headers' ) );
36
		// Process column contents for calendar feeds.
37
		add_action( 'manage_calendar_posts_custom_column', array( $this, 'calendar_feed_column_content' ), 10, 2 );
38
39
		// Add actions in calendar feed rows.
40
		add_filter( 'post_row_actions', array( $this, 'row_actions' ), 10, 2 );
41
		// Add bulk actions.
42
		add_action( 'admin_init', array( $this, 'bulk_actions' ) );
43
		// Add content to edit calendars page.
44
		add_action( 'load-edit.php', array( $this, 'edit_table_hooks' ) );
45
46
		// Default calendar post type content (default event template).
47
		add_filter( 'default_content', array( $this, 'default_event_template' ), 10, 2 );
48
49
		// Add a clear cache link in submit post box.
50
		add_action( 'post_submitbox_misc_actions', array( $this, 'clear_cache_button' ) );
51
52
		// Add media button to post editor for adding a shortcode.
53
		add_action( 'media_buttons', array( $this, 'add_shortcode_button' ), 100 );
54
		add_action( 'edit_form_after_editor', array( $this, 'add_shortcode_panel' ), 100 );
55
	}
56
57
	/**
58
	 * Add column headers to Calendar feeds custom post type page.
59
	 *
60
	 * @since  3.0.0
61
	 *
62
	 * @param  array $columns Default columns.
63
	 *
64
	 * @return array Filtered output.
65
	 */
66
	public function add_calendar_feed_column_headers( $columns ) {
67
68
		// New columns.
69
		$feed_info = array( 'feed' => __( 'Events Source', 'google-calendar-events' ) );
70
		$calendar_info = array( 'calendar' => __( 'Calendar Type', 'google-calendar-events' ) );
71
		$shortcode = array( 'shortcode' => __( 'Shortcode', 'google-calendar-events' ) );
72
73
		// Merge with existing columns and rearrange.
74
		$columns = array_slice( $columns, 0, 2, true ) + $feed_info + $calendar_info + $shortcode + array_slice( $columns, 2, null, true );
75
76
		return $columns;
77
	}
78
79
	/**
80
	 * Fill out the Calendar feed post type columns.
81
	 *
82
	 * @since  3.0.0
83
	 *
84
	 * @param  string $column_name Column identifier.
85
	 * @param  int    $post_id     The calendar feed post id.
86
	 *
87
	 * @return void
88
	 */
89
	public function calendar_feed_column_content( $column_name, $post_id ) {
90
91
		switch ( $column_name ) {
92
93
			case 'feed':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
94
95
				$feed = simcal_get_feed( $post_id );
96
				echo isset( $feed->name ) ? $feed->name : '&mdash;';
97
				break;
98
99
			case 'calendar':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
100
101
				$info = '&mdash;';
102
103
				if ( $terms = wp_get_object_terms( $post_id, 'calendar_type' ) ) {
104
105
					$calendar_type  = sanitize_title( current( $terms )->name );
106
					$calendar       = simcal_get_calendar( $calendar_type );
107
108
					if ( $calendar instanceof Calendar ) {
109
						$info = $calendar->name;
110
						$views = get_post_meta( $post_id, '_calendar_view', true );;
111
						$view = isset( $views[ $calendar->type ] ) ? $views[ $calendar->type ] : '';
112
113
						if ( isset( $calendar->views[ $view ] ) ) {
114
							$info .= ' &rarr; ' . $calendar->views[ $view ];
115
						}
116
					}
117
				}
118
119
				echo $info;
120
				break;
121
122
			case 'shortcode' :
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
123
124
				simcal_print_shortcode_tip( $post_id );
125
				break;
126
		}
127
	}
128
129
	/**
130
	 * Add actions to Calendar feed post type row view.
131
	 *
132
	 * @since  3.0.0
133
	 *
134
	 * @param  array    $actions Default actions
135
	 * @param  \WP_Post $post    Post object.
136
	 *
137
	 * @return array Filtered output.
138
	 */
139
	public function row_actions( $actions, $post ) {
140
141
		// Add a clear feed cache action link.
142
		if ( $post->post_type == 'calendar' ) {
143
			$actions['duplicate_feed'] = '<a href="' . esc_url( add_query_arg( array( 'duplicate_feed' => $post->ID ) ) ) . '">' . __( 'Clone', 'google-calendar-events' )       . '</a>';
144
			$actions['clear_cache']    = '<a href="' . esc_url( add_query_arg( array( 'clear_cache' => $post->ID ) ) ) . '">'    . __( 'Clear Cache', 'google-calendar-events' ) . '</a>';
145
		}
146
147
		return $actions;
148
	}
149
150
	/**
151
	 * Bulk actions.
152
	 *
153
	 * @since 3.0.0
154
	 */
155
	public function bulk_actions() {
0 ignored issues
show
Coding Style introduced by
bulk_actions uses the super-global variable $_REQUEST 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...
156
157
		// Clear an individual feed cache.
158
		// @todo Convert the clear cache request to ajax.
159 View Code Duplication
		if ( isset( $_REQUEST['clear_cache'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
161
			$id = intval( $_REQUEST['clear_cache'] );
162
163
			if ( $id > 0 ) {
164
				simcal_delete_feed_transients( $id );
165
			}
166
167
			wp_redirect( remove_query_arg( 'clear_cache' ) );
168
		}
169
170
		// Duplicate a feed post type.
171 View Code Duplication
		if ( isset( $_REQUEST['duplicate_feed'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
172
173
			$id = intval( $_REQUEST['duplicate_feed'] );
174
175
			if ( $id > 0 ) {
176
				$this->duplicate_feed( $id );
177
			}
178
179
			wp_redirect( remove_query_arg( 'duplicate_feed' ) );
180
		}
181
182
		$bulk_actions = new Bulk_Actions( 'calendar' );
183
184
		$bulk_actions->register_bulk_action( array(
185
			'menu_text'     => __( 'Clear cache', 'google-calendar-events' ),
186
			'action_name'   => 'clear_calendars_cache',
187
			'callback'      => function( $post_ids ) {
188
				simcal_delete_feed_transients( $post_ids );
189
			},
190
			'admin_notice'  => __( 'Cache cleared.', 'google-calendar-events' ),
191
			)
192
		);
193
194
		$bulk_actions->init();
195
	}
196
197
	/**
198
	 * Edit calendars table hooks.
199
	 *
200
	 * @since 3.0.0
201
	 * @internal
202
	 */
203
	public function edit_table_hooks() {
204
205
		$screen = simcal_is_admin_screen();
206
207
		if ( 'edit-calendar' == $screen ) {
208
			add_action( 'in_admin_footer', function() {
209
210
			} );
211
		}
212
	}
213
214
	/**
215
	 * Clone a feed post type.
216
	 *
217
	 * @since 3.0.0
218
	 *
219
	 * @param int $post_id
220
	 */
221
	private function duplicate_feed( $post_id ) {
222
223
		if ( $duplicate = get_post( intval( $post_id ), 'ARRAY_A' ) ) {
224
225
			if ( 'calendar' == $duplicate['post_type'] ) {
226
227
				$duplicate['post_title'] = $duplicate['post_title'] . ' (' . __( 'Copy', 'google-calendar-events' ) . ')';
228
229
				unset( $duplicate['ID'] );
230
				unset( $duplicate['guid'] );
231
				unset( $duplicate['comment_count'] );
232
233
				$duplicate_id = wp_insert_post( $duplicate );
234
235
				$taxonomies = get_object_taxonomies( $duplicate['post_type'] );
236
				foreach ( $taxonomies as $taxonomy ) {
237
					$terms = wp_get_post_terms( $post_id, $taxonomy, array( 'fields' => 'names' ) );
238
					wp_set_object_terms( $duplicate_id, $terms, $taxonomy );
239
				}
240
241
				$custom_fields = get_post_custom( $post_id );
242
				foreach ( $custom_fields as $key => $value ) {
243
					add_post_meta( $duplicate_id, $key, maybe_unserialize( $value[0] ) );
244
				}
245
			}
246
247
		}
248
	}
249
250
	/**
251
	 * Default event template builder content.
252
	 *
253
	 * @since  3.0.0
254
	 *
255
	 * @param  string   $content
256
	 * @param  \WP_Post $post
257
	 *
258
	 * @return string
259
	 */
260
	public function default_event_template( $content, $post ) {
261
		return 'calendar' == $post->post_type ? simcal_default_event_template() : $content;
262
	}
263
264
	/**
265
	 * Clear cache button.
266
	 *
267
	 * @since 3.0.0
268
	 */
269
	public function clear_cache_button() {
270
271
		global $post, $post_type;
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...
272
273
		if ( $post_type == 'calendar' && isset( $post->ID ) ) {
274
			echo '<a id="simcal-clear-cache" class="button" data-id="' . $post->ID . ' ">' .
275
			     '<i class="simcal-icon-spinner simcal-icon-spin" style="display: none;"></i> ' .
276
			     __( 'Clear cache', 'google-calendar-events' ) .
277
			     '</a>';
278
		}
279
	}
280
281
	/**
282
	 * Add a shortcode button.
283
	 *
284
	 * Adds a button to add a calendar shortcode in WordPress content editor.
285
	 * Uses Thickbox. http://codex.wordpress.org/ThickBox
286
	 *
287
	 * @since 3.0.0
288
	 */
289
	public function add_shortcode_button() {
290
291
		$post_types = array();
292
293
		$settings = get_option( 'simple-calendar_settings_calendars' );
294
		if ( isset( $settings['general']['attach_calendars_posts'] ) ) {
295
			$post_types = $settings['general']['attach_calendars_posts'];
296
		}
297
298
		global $post_type;
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...
299
300
		if ( in_array( $post_type, $post_types ) ) {
301
302
			// Thickbox will ignore height and width, will adjust these in js.
303
			// @see https://core.trac.wordpress.org/ticket/17249
304
			?>
305
			<a href="#TB_inline?height=250&width=500&inlineId=simcal-insert-shortcode-panel" id="simcal-insert-shortcode-button" class="thickbox button insert-calendar add_calendar">
306
				<span class="wp-media-buttons-icon dashicons-before dashicons-calendar-alt"></span> <?php _e( 'Add Calendar', 'google-calendar-events' ); ?>
307
			</a>
308
			<?php
309
310
		}
311
312
	}
313
314
	/**
315
	 * Panel for the add shortcode media button.
316
	 *
317
	 * Prints the panel for choosing a calendar to insert as a shortcode in a page or post.
318
	 *
319
	 * @since 3.0.0
320
	 */
321
	public function add_shortcode_panel() {
322
323
		$calendars = simcal_get_calendars();
324
325
		?>
326
		<div id="simcal-insert-shortcode-panel" style="display:none;">
327
			<div class="simcal-insert-shortcode-panel">
328
				<h1><?php _e( 'Add Calendar', 'google-calendar-events' ); ?></h1>
329
				<?php _e( 'Add a calendar to your post.', 'google-calendar-events' ); ?>
330
				<?php if ( ! empty( $calendars ) && is_array( $calendars ) ) : ?>
331
					<p>
332
						<label for="simcal-choose-calendar">
333
							<?php $multiselect = count( $calendars ) > 15 ? ' simcal-field-select-enhanced' : ''; ?>
334
							<select id="simcal-choose-calendar"
335
							        class="simcal-field simcal-field-select<?php echo $multiselect; ?>"
336
							        name="">
337
								<?php foreach ( $calendars as $id => $title ) : ?>
338
									<option value="<?php echo $id ?>"><?php echo $title ?></option>
339
								<?php endforeach; ?>
340
							</select>
341
						</label>
342
					</p>
343
					<p><input type="button" value="<?php _e( 'Insert Calendar', 'google-calendar-events' ); ?>" id="simcal-insert-shortcode" class="button button-primary button-large" name="" /></p>
344
				<?php else : ?>
345
					<p><em><?php _e( 'Could not find any calendars to add to this post.', 'google-calendar-events' ); ?></em></p>
346
					<strong><a href="post-new.php?post_type=calendar"><?php _e( 'Please add and configure new calendar first.', 'google-calendar-events' ); ?></a></strong>
347
				<?php endif; ?>
348
			</div>
349
		</div>
350
		<?php
351
352
	}
353
354
}
355