Validation.php
204 lines
| 1 | <?php namespace NestedPages\Form\Validation; |
| 2 | |
| 3 | use NestedPages\Config\SettingsRepository; |
| 4 | |
| 5 | /** |
| 6 | * Nested Pages Form Validation |
| 7 | */ |
| 8 | class Validation { |
| 9 | |
| 10 | /** |
| 11 | * Settings Repository |
| 12 | */ |
| 13 | private $settings; |
| 14 | |
| 15 | public function __construct() |
| 16 | { |
| 17 | $this->settings = new SettingsRepository; |
| 18 | } |
| 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 | /** |
| 36 | * Validate IDs in an array (tax ids) |
| 37 | */ |
| 38 | public function validateIntegerArray($items) |
| 39 | { |
| 40 | foreach ( $items as $item ) |
| 41 | { |
| 42 | if ( !is_numeric($item) ){ |
| 43 | return wp_send_json(array('status'=>'error', 'message'=>'Incorrect Form Field')); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * Validate Date Input whether default WP or NP Datepicker |
| 51 | */ |
| 52 | public function validateDate($data) |
| 53 | { |
| 54 | if ( $this->settings->datepickerEnabled() ) return $this->validateDatepicker($data); |
| 55 | return $this->validateWPDate($data); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | /** |
| 60 | * Validate Datepicker date and time |
| 61 | * @since 1.3.1 |
| 62 | * @return string - formatted date |
| 63 | */ |
| 64 | private function validateDatepicker($data) |
| 65 | { |
| 66 | // Make sure fields are filled out |
| 67 | if ( $data['np_date'] == "" || $data['np_time'] == "" ) return $this->sendDateError(); |
| 68 | |
| 69 | |
| 70 | $this->checkValidFormattedTime($data['np_time']); |
| 71 | |
| 72 | $date = date('Y-m-d H:i:s', strtotime($data['np_date'] . ' ' . $data['np_time'] . ' ' . $data['np_ampm'])); |
| 73 | if ( $date == '1970-01-01 00:00:00' ) return $this->sendDateError(); |
| 74 | return $date; |
| 75 | } |
| 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 | /** |
| 106 | * Check valid date |
| 107 | */ |
| 108 | private function checkValidDate($month, $day, $year) |
| 109 | { |
| 110 | if ( !wp_checkdate(intval($month), intval($day), intval($year), $year . '-' . $month . '-' . $day)) return $this->sendDateError(); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /** |
| 115 | * Check for Valid 24 hour Time |
| 116 | */ |
| 117 | private function checkValidTime($time) |
| 118 | { |
| 119 | if (!preg_match("/(2[0-3]|[01][0-9]):[0-5][0-9]/", $time)) return $this->sendDateError(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Check for Valid 12 hour Time |
| 124 | */ |
| 125 | private function checkValidFormattedTime($time) |
| 126 | { |
| 127 | if (!preg_match("/^(1[0-2]|0?[1-9]):[0-5][0-9]/", $time)) return $this->sendDateError(); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * Send Date Error |
| 133 | * @return response |
| 134 | */ |
| 135 | private function sendDateError() |
| 136 | { |
| 137 | wp_send_json(array( |
| 138 | 'status' => 'error', |
| 139 | 'message' => __('Please provide a valid date.', 'nestedpages') |
| 140 | )); |
| 141 | die(); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /** |
| 146 | * Validate new redirect/link fields |
| 147 | */ |
| 148 | public function validateRedirect($data) |
| 149 | { |
| 150 | if ( (!isset($data['np_link_title'])) || ($data['np_link_title'] == "") ){ |
| 151 | return wp_send_json(array('status' => 'error', 'message' => __('Please provide a menu title.', 'nestedpages') )); |
| 152 | } |
| 153 | if ( (!isset($data['np_link_content'])) || ($data['np_link_content'] == "") ){ |
| 154 | return wp_send_json(array('status' => 'error', 'message' => __('Please provide a valid URL.', 'nestedpages') )); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /** |
| 160 | * Validate a string isn't empty |
| 161 | */ |
| 162 | public function checkEmpty($var, $title) |
| 163 | { |
| 164 | if ( $var == "" ){ |
| 165 | $message = __('Please provide a ', 'nestedpages') . $title; |
| 166 | return wp_send_json(array('status' => 'error', 'message' => $message)); |
| 167 | die(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | * Validate New Pages |
| 174 | */ |
| 175 | public function validateNewPages($data) |
| 176 | { |
| 177 | // Check for Parent ID |
| 178 | if ( (!isset($data['parent_id'])) || (!is_numeric($data['parent_id'])) ){ |
| 179 | $message = __('A valid parent page was not provided.', 'nestedpages'); |
| 180 | return wp_send_json(array('status' => 'error', 'message' => $message)); |
| 181 | die(); |
| 182 | } |
| 183 | |
| 184 | // Make sure there's at least one page |
| 185 | if ( !isset($data['post_title']) ){ |
| 186 | $message = __('Please provide at least one page title.', 'nestedpages'); |
| 187 | return wp_send_json(array('status' => 'error', 'message' => $message)); |
| 188 | die(); |
| 189 | } |
| 190 | |
| 191 | // Page fields cannot be blank |
| 192 | foreach ( $data['post_title'] as $title ){ |
| 193 | if ( $title == "" ){ |
| 194 | $message = __('Page titles cannot be blank.', 'nestedpages'); |
| 195 | return wp_send_json(array('status' => 'error', 'message' => $message)); |
| 196 | die(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | } |