Custom_Sidebars_Details::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/*
3
This program is free software; you can redistribute it and/or
4
modify it under the terms of the GNU General Public License
5
as published by the Free Software Foundation; either version 2
6
of the License, or (at your option) any later version.
7
8
This program is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
GNU General Public License for more details.
12
13
You should have received a copy of the GNU General Public License
14
along with this program; if not, write to the Free Software
15
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16
*/
17
18
class Custom_Sidebars_Details {
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...
19
20
	/**
21
	 * The class constructor
22
	 */
23
	public function __construct() {
24
25
		add_action( 'save_post', array( $this, 'save_post' ) );
26
27
		foreach ( Custom_Sidebars::get_post_types() as $post_type ) {
28
			add_action( "add_meta_boxes_{$post_type}", array( $this, 'add_meta_boxes' ) );
29
		}
30
31
	}
32
33
	/**
34
	 * Add the metabox to the post edit screen
35
	 *
36
	 * Hooked to add_meta_boxes_{$post_type}
37
	 */
38
	public function add_meta_boxes() {
39
40
		add_meta_box(
41
			'custom-sidebar-details',
42
			__( 'Custom Sidebar', 'custom-sidebar' ),
43
			array( $this, 'render' ),
44
			get_post_type(),
45
			'side',
46
			'default',
47
			array()
48
		);
49
50
	}
51
52
	public function render() {
53
54
		$metabox = $this;
55
		include dirname( __DIR__ ) . '/templates/custom-sidebar-details.php';
56
57
	}
58
59
	public function sidebar_select_options() {
60
61
		global $wp_registered_sidebars;
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...
62
		
63
		foreach ( $wp_registered_sidebars as $key => $sidebar ) {
64
			printf( '<option value="%1$s"%3$s>%2$s</option>', $key, $sidebar['name'], selected( Custom_Sidebars::get_sidebar(), $key ) );
65
		}
66
67
	}
68
69
    /**
70
     * @param int $post_id
71
     */
72
	public function save_post( $post_id ) {
73
74
		$sidebar_id     = filter_input( INPUT_POST, 'custom-sidebar-select', FILTER_VALIDATE_INT );
75
        $custom_sidebar = filter_input( INPUT_POST, 'custom-sidebar', FILTER_VALIDATE_BOOLEAN );
76
77
		if ( isset( $custom_sidebar ) ) {
78
			$sidebar_id = Custom_Sidebars::get_sidebar_id( $post_id );
79
		}
80
81
		update_post_meta( $post_id, '_custom_sidebar',    $custom_sidebar );
82
		update_post_meta( $post_id, '_custom_sidebar_id', sanitize_text_field( $sidebar_id ) );
83
84
	}
85
86
    /**
87
     * @param  int|null $post_id
88
     * @return bool
89
     */
90
	public static function has_custom_sidebar( $post_id = null ) {
91
92
		$value = false;
93
94
		if ( empty( $post_id ) ) {
95
			$post_id = get_post()->ID;
96
		}
97
98
		if ( get_post_meta( $post_id, '_custom_sidebar', true ) ) {
99
			$value = true;
100
		}
101
102
		return $value;
103
104
	} 
105
106
}
107