PluginProbe ʕ •ᴥ•ʔ
Nested Pages / 2.0.1
Nested Pages v2.0.1
3.2.14 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.3.0 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.2 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.3.1 1.6.3.2 1.6.4 1.6.5 1.6.5.1 1.6.5.2 1.6.6 1.6.7 1.6.8 1.7.0 1.7.1 2.0.1 2.0.2 2.0.3 2.0.4 3.0.1 3.0.10 3.0.11 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.20 3.1.21 3.1.22 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7
wp-nested-pages / app / Form / Validation / Validation.php
wp-nested-pages / app / Form / Validation Last commit date
Validation.php 8 years ago
Validation.php
196 lines
1 <?php
2 namespace NestedPages\Form\Validation;
3
4 use NestedPages\Config\SettingsRepository;
5
6 /**
7 * Nested Pages Form Validation
8 */
9 class Validation
10 {
11 /**
12 * Settings Repository
13 */
14 private $settings;
15
16 public function __construct()
17 {
18 $this->settings = new SettingsRepository;
19 }
20
21 /**
22 * Validate Post IDs in an array of posts
23 */
24 public function validatePostIDs($posts)
25 {
26 foreach ($posts as $post)
27 {
28 if ( !is_numeric($post['id']) ){
29 return wp_send_json(array('status'=>'error', 'message'=>'Incorrect Form Field'));
30 }
31 }
32 }
33
34 /**
35 * Validate IDs in an array (tax ids)
36 */
37 public function validateIntegerArray($items)
38 {
39 foreach ( $items as $item )
40 {
41 if ( !is_numeric($item) ){
42 return wp_send_json(array('status'=>'error', 'message'=>'Incorrect Form Field'));
43 }
44 }
45 }
46
47 /**
48 * Validate Date Input whether default WP or NP Datepicker
49 */
50 public function validateDate($data)
51 {
52 if ( $this->settings->datepickerEnabled() ) return $this->validateDatepicker($data);
53 return $this->validateWPDate($data);
54 }
55
56 /**
57 * Validate Datepicker date and time
58 * @since 1.3.1
59 * @return string - formatted date
60 */
61 private function validateDatepicker($data)
62 {
63 // Make sure fields are filled out
64 if ( $data['np_date'] == "" || $data['np_time'] == "" ) return $this->sendDateError();
65
66 $time_format = get_option('time_format');
67 if ( $time_format == 'H:i' ){
68 $this->checkValidTime($data['np_time']);
69 } else {
70 $this->checkValidFormattedTime($data['np_time']);
71 }
72
73 $date = date('Y-m-d H:i:s', strtotime($data['np_date'] . ' ' . $data['np_time'] . ' ' . $data['np_ampm']));
74 if ( $date == '1970-01-01 00:00:00' ) return $this->sendDateError();
75 return $date;
76 }
77
78 /**
79 * Validate Default WP Date Fields
80 * @since 1.3.1
81 * @return string - formatted date
82 */
83 private function validateWPDate($data)
84 {
85 $this->checkValidDate($data['mm'], $data['jj'], $data['aa']);
86 $this->checkValidTime($data['hh'] . ':' . $data['mn']);
87
88 // Validate all the fields are there
89 if ( ($data['aa'] !== "")
90 && ( $data['mm'] !== "" )
91 && ( $data['jj'] !== "" )
92 && ( $data['hh'] !== "" )
93 && ( $data['mm'] !== "" )
94 && ( $data['ss'] !== "" ) )
95 {
96 $date = strtotime($data['aa'] . '-' . $data['mm'] . '-' . $data['jj'] . ' ' . $data['hh'] . ':' . $data['mn'] . ':' . $data['ss']);
97 return date('Y-m-d H:i:s', $date);
98 }
99
100 return $this->sendDateError();
101
102 }
103
104 /**
105 * Check valid date
106 */
107 private function checkValidDate($month, $day, $year)
108 {
109 if ( !wp_checkdate(intval($month), intval($day), intval($year), $year . '-' . $month . '-' . $day)) return $this->sendDateError();
110 }
111
112 /**
113 * Check for Valid 24 hour Time
114 */
115 private function checkValidTime($time)
116 {
117 if (!preg_match("/(2[0-3]|[01][0-9]):[0-5][0-9]/", $time)) return $this->sendDateError();
118 }
119
120 /**
121 * Check for Valid 12 hour Time
122 */
123 private function checkValidFormattedTime($time)
124 {
125 if (!preg_match("/^(1[0-2]|0?[1-9]):[0-5][0-9]/", $time)) return $this->sendDateError();
126 }
127
128 /**
129 * Send Date Error
130 * @return response
131 */
132 private function sendDateError()
133 {
134 wp_send_json(array(
135 'status' => 'error',
136 'message' => __('Please provide a valid date.', 'wp-nested-pages')
137 ));
138 die();
139 }
140
141 /**
142 * Validate new redirect/link fields
143 */
144 public function validateRedirect($data)
145 {
146 if ( (!isset($data['np_link_title'])) || ($data['np_link_title'] == "") ){
147 return wp_send_json(array('status' => 'error', 'message' => __('Please provide a menu title.', 'wp-nested-pages') ));
148 }
149 if ( (!isset($data['np_link_content'])) || ($data['np_link_content'] == "") ){
150 return wp_send_json(array('status' => 'error', 'message' => __('Please provide a valid URL.', 'wp-nested-pages') ));
151 }
152 }
153
154 /**
155 * Validate a string isn't empty
156 */
157 public function checkEmpty($var, $title)
158 {
159 if ( $var == "" ){
160 $message = __('Please provide a ', 'wp-nested-pages') . $title;
161 return wp_send_json(array('status' => 'error', 'message' => $message));
162 die();
163 }
164 }
165
166 /**
167 * Validate New Pages
168 */
169 public function validateNewPages($data)
170 {
171 // Check for Parent ID
172 if ( (!isset($data['parent_id'])) || (!is_numeric($data['parent_id'])) ){
173 $message = __('A valid parent page was not provided.', 'wp-nested-pages');
174 return wp_send_json(array('status' => 'error', 'message' => $message));
175 die();
176 }
177
178 // Make sure there's at least one page
179 if ( !isset($data['post_title']) ){
180 $message = __('Please provide at least one page title.', 'wp-nested-pages');
181 return wp_send_json(array('status' => 'error', 'message' => $message));
182 die();
183 }
184
185 // Page fields cannot be blank
186 foreach ( $data['post_title'] as $title ){
187 if ( $title == "" ){
188 $message = __('Page titles cannot be blank.', 'wp-nested-pages');
189 return wp_send_json(array('status' => 'error', 'message' => $message));
190 die();
191 }
192 }
193
194 return true;
195 }
196 }