Meta_Boxes   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 176
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 28 2
B add_meta_boxes() 0 62 5
C save_meta_boxes() 0 44 14
1
<?php
2
/**
3
 * Meta Boxes
4
 *
5
 * @package SimpleCalendar/Admin
6
 */
7
namespace SimpleCalendar\Admin;
8
9
use SimpleCalendar\Admin\Metaboxes as Metabox;
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * Meta boxes class.
17
 *
18
 * Handles write panels in post types and post meta.
19
 *
20
 * @since 3.0.0
21
 */
22
class Meta_Boxes {
23
24
	/**
25
	 * Saved meta boxes status.
26
	 *
27
	 * @access private
28
	 * @var bool
29
	 */
30
	private static $saved_meta_boxes = false;
31
32
	/**
33
	 * Post types to attach calendars.
34
	 *
35
	 * @access private
36
	 * @var array
37
	 */
38
	private $post_types = array();
39
40
	/**
41
	 * Hook in tabs.
42
	 *
43
	 * @since 3.0.0
44
	 */
45
	public function __construct() {
46
47
		$settings = get_option( 'simple-calendar_settings_calendars' );
48
		if ( isset( $settings['general']['attach_calendars_posts'] ) ) {
49
			$this->post_types = $settings['general']['attach_calendars_posts'];
50
		}
51
52
		// Load meta boxes to save settings.
53
		new Metabox\Settings();
54
		new Metabox\Attach_Calendar();
55
		new Metabox\Upgrade_To_Premium();
56
		//new Metabox\Newsletter();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
57
58
		do_action( 'simcal_load_meta_boxes' );
59
60
		// Add meta boxes.
61
		add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 30 );
62
63
		// Process meta boxes.
64
		add_action( 'simcal_save_settings_meta','\SimpleCalendar\Admin\Metaboxes\Settings::save', 10, 2 );
65
		add_action( 'simcal_save_attach_calendar_meta','\SimpleCalendar\Admin\Metaboxes\Attach_Calendar::save', 10, 2 );
66
67
		// Save meta boxes data.
68
		add_action( 'save_post', array( $this, 'save_meta_boxes' ), 1, 2 );
69
70
		// Uncomment this for debugging $_POST while saving a meta box.
71
		// add_action( 'save_post', function() { echo '<pre>'; print_r( $_POST ); echo '</pre>'; die(); } );
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
72
	}
73
74
	/**
75
	 * Add meta boxes.
76
	 *
77
	 * @since 3.0.0
78
	 */
79
	public function add_meta_boxes() {
80
81
		add_meta_box(
82
			'simcal-calendar-settings',
83
			__( 'Calendar Settings', 'google-calendar-events' ),
84
			'\SimpleCalendar\Admin\Metaboxes\Settings::html',
85
			'calendar',
86
			'normal',
87
			'core'
88
		);
89
90
		$addons = apply_filters( 'simcal_installed_addons', array() );
91
		if ( empty( $addons ) ) {
92
93
			// Premium add-on feature list and upsell.
94
			add_meta_box(
95
				'simcal-upgrade',
96
				__( 'Looking for more?', 'google-calendar-events' ),
97
				'\SimpleCalendar\Admin\Metaboxes\Upgrade_To_Premium::html',
98
				'calendar',
99
				'side',
100
				'default'
101
			);
102
103
			// Removing coupon code + mailing list sign-up for now. 9/26/16
104
			/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
105
			add_meta_box(
106
				'simcal-newsletter',
107
				__( 'Get 20% off all Pro Add-ons', 'google-calendar-events' ),
108
				'\SimpleCalendar\Admin\Metaboxes\Newsletter::html',
109
				'calendar',
110
				'side',
111
				'default'
112
			);
113
			*/
114
		}
115
116
		add_meta_box(
117
			'simcal-get-shortcode',
118
			__( 'Calendar Shortcode', 'google-calendar-events' ),
119
			'\SimpleCalendar\Admin\Metaboxes\Get_Shortcode::html',
120
			'calendar',
121
			'side',
122
			'default'
123
		);
124
125
		// Add meta box if there are calendars.
126
		if ( ( true == simcal_get_calendars() ) && ! empty( $this->post_types ) ) {
127
			foreach ( $this->post_types as $post_type ) {
128
				add_meta_box(
129
					'simcal-attach-calendar',
130
					__( 'Attach Calendar', 'google-calendar-events' ),
131
					'\SimpleCalendar\Admin\Metaboxes\Attach_Calendar::html',
132
					$post_type,
133
					'side',
134
					'low'
135
				);
136
			}
137
		}
138
139
		do_action( 'simcal_add_meta_boxes' );
140
	}
141
142
	/**
143
	 * Check if we're saving, then trigger action.
144
	 *
145
	 * @since  3.0.0
146
	 *
147
	 * @param  int    $post_id
148
	 * @param  object $post
149
	 *
150
	 * @return void
151
	 */
152
	public function save_meta_boxes( $post_id, $post ) {
0 ignored issues
show
Coding Style introduced by
save_meta_boxes 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...
153
154
		// $post_id and $post are required.
155
		if ( empty( $post_id ) || empty( $post ) || self::$saved_meta_boxes ) {
156
			return;
157
		}
158
159
		// Don't save meta boxes for revisions or autosaves.
160
		if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
161
			return;
162
		}
163
164
		// Check the nonce.
165
		if ( empty( $_POST['simcal_meta_nonce'] ) || ! wp_verify_nonce( $_POST['simcal_meta_nonce'], 'simcal_save_data' ) ) {
166
			return;
167
		}
168
169
		// Check the post being saved == the $post_id to prevent triggering this call for other save_post events.
170
		if ( empty( $_POST['post_ID'] ) || $_POST['post_ID'] != $post_id ) {
171
			return;
172
		}
173
174
		// Check user has permission to edit
175
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
176
			return;
177
		}
178
179
		// We need this save event to run once to avoid potential endless loops.
180
		// This would have been perfect:
181
		// `remove_action( current_filter(), __METHOD__ );`
182
		// But cannot be used due to a WordPress bug:
183
		// @link https://core.trac.wordpress.org/ticket/17817
184
		// @see also https://github.com/woothemes/woocommerce/issues/6485
185
		self::$saved_meta_boxes = true;
186
187
		// Check the post type.
188
		if ( 'calendar' == $post->post_type ) {
189
			do_action( 'simcal_save_settings_meta', $post_id, $post );
190
		} elseif ( in_array( $post->post_type, $this->post_types ) ) {
191
			do_action( 'simcal_save_attach_calendar_meta', $post_id, $post );
192
		}
193
194
		do_action( 'simcal_save_meta_boxes', $post_id, $post );
195
	}
196
197
}
198