| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file handles the addintion and updating of Post Meta Boxes. |
| 4 |
* |
| 5 |
* @package MainWP/Dashboard |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace MainWP\Dashboard; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class MainWP_Meta_Boxes |
| 12 |
* |
| 13 |
* @package MainWP\Dashboard |
| 14 |
*/ |
| 15 |
class MainWP_Meta_Boxes { |
| 16 |
|
| 17 |
/** |
| 18 |
* Method select_sites_handle() |
| 19 |
* |
| 20 |
* Update Post meta for Select Sites Meta boxes. |
| 21 |
* |
| 22 |
* @param mixed $post_id Post ID. |
| 23 |
* @param mixed $post_type Post type. |
| 24 |
* |
| 25 |
* @return int $post_id Post ID. |
| 26 |
*/ |
| 27 |
public function select_sites_handle( $post_id, $post_type ) { // phpcs:ignore -- complex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated. |
| 28 |
|
| 29 |
/** |
| 30 |
* Verify this came from the our screen and with proper authorization. |
| 31 |
*/ |
| 32 |
// phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 33 |
if ( ! isset( $_POST['select_sites_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['select_sites_nonce'] ), 'select_sites_' . $post_id ) ) { |
| 34 |
return $post_id; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything. |
| 39 |
*/ |
| 40 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 41 |
return $post_id; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Check permissions. |
| 46 |
*/ |
| 47 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 48 |
return $post_id; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* OK, we're authenticated: we need to find and save the data. |
| 53 |
*/ |
| 54 |
$_post = get_post( $post_id ); |
| 55 |
if ( $_post->post_type === $post_type && isset( $_POST['select_by'] ) ) { |
| 56 |
$selected_wp = array(); |
| 57 |
if ( isset( $_POST['selected_sites'] ) ) { |
| 58 |
if ( is_array( $_POST['selected_sites'] ) ) { |
| 59 |
$selected_wp = ! empty( $_POST['selected_sites'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['selected_sites'] ) ) : array(); |
| 60 |
} else { // radio selection. |
| 61 |
$selected_wp = ! empty( $_POST['selected_sites'] ) ? array( sanitize_text_field( wp_unslash( $_POST['selected_sites'] ) ) ) : array(); |
| 62 |
} |
| 63 |
} |
| 64 |
update_post_meta( $post_id, '_selected_sites', $selected_wp ); |
| 65 |
$selected_groups = array(); |
| 66 |
if ( isset( $_POST['selected_groups'] ) ) { |
| 67 |
if ( is_array( $_POST['selected_groups'] ) ) { |
| 68 |
$selected_groups = ! empty( $_POST['selected_groups'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['selected_groups'] ) ) : array(); |
| 69 |
} else { // radio selection. |
| 70 |
$selected_groups = ! empty( $_POST['selected_groups'] ) ? array( sanitize_text_field( wp_unslash( $_POST['selected_groups'] ) ) ) : array(); |
| 71 |
} |
| 72 |
} |
| 73 |
update_post_meta( $post_id, '_selected_groups', $selected_groups ); |
| 74 |
$selected_clients = array(); |
| 75 |
if ( isset( $_POST['selected_clients'] ) ) { |
| 76 |
if ( is_array( $_POST['selected_clients'] ) ) { |
| 77 |
$selected_clients = ! empty( $_POST['selected_clients'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['selected_clients'] ) ) : array(); |
| 78 |
} else { // radio selection. |
| 79 |
$selected_clients = ! empty( $_POST['selected_clients'] ) ? array( sanitize_text_field( wp_unslash( $_POST['selected_clients'] ) ) ) : array(); |
| 80 |
} |
| 81 |
} |
| 82 |
update_post_meta( $post_id, '_selected_clients', $selected_clients ); |
| 83 |
update_post_meta( $post_id, '_selected_by', sanitize_text_field( wp_unslash( $_POST['select_by'] ) ) ); |
| 84 |
|
| 85 |
if ( ( 'group' === $_POST['select_by'] && 0 < count( $selected_groups ) ) || ( 'site' === $_POST['select_by'] && 0 < count( $selected_wp ) ) || ( 'client' === $_POST['select_by'] && 0 < count( $selected_clients ) ) ) { |
| 86 |
return sanitize_text_field( wp_unslash( $_POST['select_by'] ) ); |
| 87 |
} |
| 88 |
} |
| 89 |
// phpcs:enable |
| 90 |
|
| 91 |
return $post_id; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Method add_categories() |
| 96 |
* |
| 97 |
* Add categories. |
| 98 |
* |
| 99 |
* @param int $post_id Post ID. |
| 100 |
*/ |
| 101 |
public function add_categories( $post_id = false ) { |
| 102 |
if ( empty( $post_id ) ) { |
| 103 |
return; |
| 104 |
} |
| 105 |
$post = get_post( $post_id ); |
| 106 |
MainWP_Post::render_categories( $post ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Method add_categories_handle() |
| 111 |
* |
| 112 |
* Handle adding categories. |
| 113 |
* |
| 114 |
* @param int $post_id Post ID. |
| 115 |
* @param string $post_type Post type. |
| 116 |
*/ |
| 117 |
public function add_categories_handle( $post_id, $post_type ) { |
| 118 |
/** |
| 119 |
* Verify this came from the our screen and with proper authorization. |
| 120 |
*/ |
| 121 |
|
| 122 |
// phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 123 |
if ( ! isset( $_POST['post_category_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['post_category_nonce'] ), 'post_category_' . $post_id ) ) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything. |
| 129 |
*/ |
| 130 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Check permissions. |
| 136 |
*/ |
| 137 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* OK, we're authenticated: we need to find and save the data. |
| 143 |
*/ |
| 144 |
$_post = get_post( $post_id ); |
| 145 |
if ( $_post->post_type === $post_type ) { |
| 146 |
if ( isset( $_POST['post_category'] ) && is_array( $_POST['post_category'] ) ) { |
| 147 |
update_post_meta( $post_id, '_categories', base64_encode( implode( ',', wp_unslash( $_POST['post_category'] ) ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible. |
| 148 |
do_action( 'mainwp_bulkpost_categories_handle', $post_id, wp_unslash( $_POST['post_category'] ) ); |
| 149 |
} |
| 150 |
|
| 151 |
$post_existing = ! empty( $_POST['post_only_existing'] ) ? 1 : 0; |
| 152 |
update_post_meta( $post_id, '_post_to_only_existing_categories', $post_existing ); |
| 153 |
|
| 154 |
return; |
| 155 |
} |
| 156 |
// phpcs:enable |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Method add_tags() |
| 161 |
* |
| 162 |
* Add tags to Post array. |
| 163 |
* |
| 164 |
* @param object $post Post object. |
| 165 |
*/ |
| 166 |
public function add_tags( $post ) { |
| 167 |
$this->add_extra( 'Tags', '_tags', 'add_tags', $post ); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Method add_tags_handle() |
| 172 |
* |
| 173 |
* Add Tags to post array handler. |
| 174 |
* |
| 175 |
* @param int $post_id Post ID. |
| 176 |
* @param string $post_type Post type. |
| 177 |
*/ |
| 178 |
public function add_tags_handle( $post_id, $post_type ) { |
| 179 |
$this->add_extra_handle( 'Tags', '_tags', 'add_tags', $post_id, $post_type ); |
| 180 |
if ( isset( $_POST['add_tags'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 181 |
do_action( 'mainwp_bulkpost_tags_handle', $post_id, $post_type, wp_strip_all_tags( wp_unslash( $_POST['add_tags'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Method add_slug() |
| 187 |
* |
| 188 |
* Add Slug to Post object. |
| 189 |
* |
| 190 |
* @param object $post Post object. |
| 191 |
*/ |
| 192 |
public function add_slug( $post ) { |
| 193 |
$this->add_extra( 'Slug', '_slug', 'add_slug', $post ); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Method add_extra() |
| 198 |
* |
| 199 |
* Add nounce to post object. |
| 200 |
* |
| 201 |
* @param string $title Post title. |
| 202 |
* @param string $saveto Save to. |
| 203 |
* @param string $prefix Custom prefix. |
| 204 |
* @param object $post Post object. |
| 205 |
*/ |
| 206 |
private function add_extra( $title, $saveto, $prefix, $post ) { |
| 207 |
$extra = base64_decode( get_post_meta( $post->ID, $saveto, true ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_decode used for http encoding compatible. |
| 208 |
?> |
| 209 |
<input type="hidden" name="<?php echo esc_attr( $prefix ); ?>_nonce" value="<?php echo esc_attr( wp_create_nonce( $prefix . '_' . $post->ID ) ); ?>"/> |
| 210 |
<input type="text" name="<?php echo esc_attr( $prefix ); ?>" value="<?php echo esc_attr( $extra ); ?>"/> |
| 211 |
<?php |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
/** |
| 216 |
* Method add_slug_handle() |
| 217 |
* |
| 218 |
* Add post slug. |
| 219 |
* |
| 220 |
* @param int $post_id Post ID. |
| 221 |
* @param string $post_type Post type. |
| 222 |
*/ |
| 223 |
public function add_slug_handle( $post_id, $post_type ) { |
| 224 |
$this->add_extra_handle( 'Slug', '_slug', 'add_slug', $post_id, $post_type ); |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Method add_extra_handle() |
| 229 |
* |
| 230 |
* Update Post meta & add Security Nonce Prefix. |
| 231 |
* |
| 232 |
* @param string $title Post title. |
| 233 |
* @param string $saveto Where to save. |
| 234 |
* @param string $prefix Custom prefix. |
| 235 |
* @param int $post_id Post ID. |
| 236 |
* @param string $post_type Post type. |
| 237 |
* |
| 238 |
* @return int $post_id Post ID. |
| 239 |
*/ |
| 240 |
private function add_extra_handle( $title, $saveto, $prefix, $post_id, $post_type ) { |
| 241 |
/** |
| 242 |
* Verify this came from the our screen and with proper authorization. |
| 243 |
*/ |
| 244 |
// phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 245 |
if ( ! isset( $_POST[ $prefix . '_nonce' ] ) || ! wp_verify_nonce( sanitize_key( $_POST[ $prefix . '_nonce' ] ), $prefix . '_' . $post_id ) ) { |
| 246 |
return $post_id; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything. |
| 251 |
*/ |
| 252 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 253 |
return $post_id; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Check permissions. |
| 258 |
*/ |
| 259 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 260 |
return $post_id; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* OK, we're authenticated: we need to find and save the data. |
| 265 |
*/ |
| 266 |
$_post = get_post( $post_id ); |
| 267 |
if ( $_post->post_type === $post_type && isset( $_POST[ $prefix ] ) ) { |
| 268 |
$value = isset( $_POST[ $prefix ] ) ? base64_encode( wp_unslash( $_POST[ $prefix ] ) ) : ''; // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible. |
| 269 |
update_post_meta( $post_id, $saveto, $value ); |
| 270 |
return $value; |
| 271 |
} |
| 272 |
// phpcs:enable |
| 273 |
|
| 274 |
return $post_id; |
| 275 |
} |
| 276 |
} |
| 277 |
?> |
| 278 |
|