This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MetaboxesController.class.php
119 lines (105 loc) · 3.1 KB
/
MetaboxesController.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* stores MetaboxesController class definition
*/
/**
* Generic abstract metaboxes controller: decides which metaboxes are to be shown on the various page of the website
*/
abstract class MetaboxesController {
/**
* @var array stores the list of MetaboxInfo objects
*/
protected $metabox_infos;
/**
* @var array list of metaboxes to be shown on the current page
*/
protected $enabled_metaboxes = null;
/**
* Build a new object
*
* @param $metabox_infos array list of MetaboxInfo
*/
protected function __construct( $metabox_infos ) {
$this->metabox_infos = $metabox_infos;
add_action( 'add_meta_boxes', array( &$this, 'register_admin' ) );
add_action( 'save_post', array( &$this, 'save_metabox_data' ), 1 );
}
/**
* Populates the $this->enabled_metaboxes list
*/
public function filter_metaboxes() {
if ( ! is_null( $this->enabled_metaboxes ) ) {
return;
}
$this->enabled_metaboxes = array();
$template = get_post_meta( $_GET['post'], '_wp_page_template', true );
foreach ( $this->metabox_infos as $metabox_info ) {
$template_checker = new TemplateChecker( $metabox_info->positions['include'], $metabox_info->positions['exclude'] );
if (
( $template == 'default' || $template == '' )
&& (
get_the_ID() == get_option( 'page_on_front' )
|| get_option( 'page_on_front' ) == $_GET['post']
|| get_option( 'page_on_front' ) == $_POST['post_ID']
)
) {
$template = 'front-page.php';
}
if ( $template_checker->check( $template ) ) {
$this->enabled_metaboxes[] = $metabox_info->metabox;
}
}
}
/**
* Register the metaboxes to the WordPress admin page
*/
public function register_admin() {
$this->filter_metaboxes();
foreach ( $this->enabled_metaboxes as $metabox ) {
$metabox->register_metaboxes();
}
}
/**
* Saves the metabox data on post_save action
*
* @param $post_id the post ID
*/
public function save_metabox_data( $post_id ) {
$this->filter_metaboxes();
foreach ( $this->enabled_metaboxes as $metabox ) {
$metabox->save_metabox_data( $post_id );
}
}
/**
* Retrieves the values stored in the given metabox_id post meta
*
* @param $metabox_id the metabox ID
* @param null $post_id the post ID
*
* @return mixed
*/
public function get_value( $metabox_id, $post_id = null ) {
foreach ( $this->metabox_infos as $metabox_info ) {
if ( $metabox_info->metabox->metabox['id'] == $metabox_id ) {
return $metabox_info->metabox->get_meta( $post_id );
}
}
}
/**
* Retrieves a list of values for the metaboxes created by the given $classname class
*
* @param $classname the name of the class
* @param null $post_id the post ID
*
* @return array
*/
public function get_values_by_classname( $classname, $post_id = null ) {
$toret = array();
foreach ( $this->metabox_infos as $metabox_info ) {
if ( get_class( $metabox_info->metabox ) == $classname ) {
$toret[] = $metabox_info->metabox->get_meta( $post_id );
}
}
return $toret;
}
}