Attach_Calendar::html()   B
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 22
nc 1
nop 1
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Add Calendar Meta Box
4
 *
5
 * @package SimpleCalendar/Admin
6
 */
7
namespace SimpleCalendar\Admin\Metaboxes;
8
9
use SimpleCalendar\Abstracts\Meta_Box;
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * Attach a calendar to a post.
17
 *
18
 * Meta box for attaching calendars to WordPress posts.
19
 *
20
 * @since 3.0.0
21
 */
22
class Attach_Calendar implements Meta_Box {
23
24
	/**
25
	 * Output the meta box markup.
26
	 *
27
	 * @since 3.0.0
28
	 *
29
	 * @param \WP_Post $post
30
	 */
31
	public static function html( $post ) {
32
33
		// @see Meta_Boxes::save_meta_boxes()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% 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...
34
		wp_nonce_field( 'simcal_save_data', 'simcal_meta_nonce' );
35
36
		$calendars = simcal_get_calendars();
37
38
		simcal_print_field( array(
39
			'type'       => 'select',
40
			'id'         => '_simcal_attach_calendar_id',
41
			'name'       => '_simcal_attach_calendar_id',
42
			'enhanced'   => count( $calendars ) > 15 ? 'enhanced' : '',
43
			'allow_void' => 'allow_void',
44
			'value'      => absint( get_post_meta( $post->ID, '_simcal_attach_calendar_id', true ) ),
45
			'options'    => $calendars,
46
			'attributes' => array(
47
				'data-allowclear' => 'true',
48
			)
49
		) );
50
51
		$position = get_post_meta( $post->ID, '_simcal_attach_calendar_position', true );
52
53
		simcal_print_field( array(
54
			'type'      => 'radio',
55
			'id'        => '_simcal_attach_calendar_position',
56
			'name'      => '_simcal_attach_calendar_position',
57
			'value'     => $position ? $position : 'after',
58
			'options'   => array(
59
				'after'  => __( 'After Content', 'google-calendar-events' ),
60
				'before' => __( 'Before Content', 'google-calendar-events' ),
61
			),
62
		) );
63
64
	}
65
66
	/**
67
	 * Validate and save the meta box fields.
68
	 *
69
	 * @since 3.0.0
70
	 *
71
	 * @param int      $post_id
72
	 * @param \WP_Post $post
73
	 */
74
	public static function save( $post_id, $post ) {
0 ignored issues
show
Coding Style introduced by
save 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...
75
76
		$id = isset( $_POST['_simcal_attach_calendar_id'] ) ? absint( $_POST['_simcal_attach_calendar_id'] ) : '';
77
		update_post_meta( $post_id, '_simcal_attach_calendar_id', $id );
78
79
		$position = isset( $_POST['_simcal_attach_calendar_position'] ) ? sanitize_title( $_POST['_simcal_attach_calendar_position'] ) : 'after';
80
		update_post_meta( $post_id, '_simcal_attach_calendar_position', $position );
81
82
	}
83
84
}
85