|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the WPFoundation library. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2015 LIN3S <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace LIN3S\WPFoundation\PostTypes\Fields\Templates; |
|
13
|
|
|
|
|
14
|
|
|
use LIN3S\WPFoundation\PostTypes\Fields\Fields; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Abstract page fields class that extends the fields basic |
|
18
|
|
|
* behaviour, implementing the "connector" and "removeScreenAttributes" |
|
19
|
|
|
* methods with the common use case. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Beñat Espiña <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class PageFields extends Fields |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Constructor. |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct() |
|
29
|
|
|
{ |
|
30
|
|
|
parent::__construct(); |
|
31
|
|
|
|
|
32
|
|
|
$newPostId = filter_input(INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT); |
|
33
|
|
|
$updatePostId = filter_input(INPUT_POST, 'post_ID', FILTER_SANITIZE_NUMBER_INT); |
|
34
|
|
|
|
|
35
|
|
|
if (isset($newPostId)) { |
|
36
|
|
|
$postId = absint($newPostId); |
|
37
|
|
|
} elseif (isset($updatePostId)) { |
|
38
|
|
|
$postId = absint($updatePostId); |
|
39
|
|
|
} |
|
40
|
|
|
if (false === isset($postId) || $this->name === get_post_meta($postId, '_wp_page_template', true)) { |
|
|
|
|
|
|
41
|
|
|
add_action('add_meta_boxes', [$this, 'addMetaBox']); |
|
|
|
|
|
|
42
|
|
|
add_action('admin_init', [$this, 'removeScreenAttributes']); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
|
View Code Duplication |
public function connector() |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
return [ |
|
52
|
|
|
[ |
|
53
|
|
|
[ |
|
54
|
|
|
'param' => 'post_type', |
|
55
|
|
|
'operator' => '==', |
|
56
|
|
|
'value' => 'page', |
|
57
|
|
|
], |
|
58
|
|
|
[ |
|
59
|
|
|
'param' => 'page_template', |
|
60
|
|
|
'operator' => '==', |
|
61
|
|
|
'value' => $this->name, |
|
62
|
|
|
], |
|
63
|
|
|
], |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function removeScreenAttributes() |
|
71
|
|
|
{ |
|
72
|
|
|
remove_post_type_support('page', 'comments'); |
|
|
|
|
|
|
73
|
|
|
remove_post_type_support('page', 'custom-fields'); |
|
|
|
|
|
|
74
|
|
|
remove_post_type_support('page', 'editor'); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Callback that adds the meta box. |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function addMetaBox() |
|
81
|
|
|
{ |
|
82
|
|
|
add_meta_box( |
|
83
|
|
|
'lin3s-id-default-meta-box', |
|
84
|
|
|
'LIN3S INFO', |
|
85
|
|
|
[$this, 'metaBox'], |
|
86
|
|
|
'page' |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Callback that prints the meta box. |
|
92
|
|
|
* |
|
93
|
|
|
* @param mixed $post The post |
|
94
|
|
|
* @param string $box The box |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function metaBox($post, $box) |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
echo <<<EOF |
|
99
|
|
|
<h1 style="text-align: center;"> |
|
100
|
|
|
PLEASE ADD <strong>PAGE TITLE</strong> AND SELECT A TEMPLATE IN THE <strong>TEMPLATE SELECTOR</strong> |
|
101
|
|
|
</h1> |
|
102
|
|
|
EOF; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: