| 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() |
| 19 |
* |
| 20 |
* Select Sites. |
| 21 |
* |
| 22 |
* @param object $post Post object. |
| 23 |
*/ |
| 24 |
public function select_sites( $post ) { |
| 25 |
|
| 26 |
$val = get_post_meta( $post->ID, '_selected_sites', true ); |
| 27 |
$selected_sites = MainWP_System_Utility::maybe_unserialyze( $val ); |
| 28 |
|
| 29 |
if ( '' == $selected_sites ) { |
| 30 |
$selected_sites = array(); |
| 31 |
} |
| 32 |
|
| 33 |
if ( isset( $_REQUEST['select'] ) ) { |
| 34 |
$selected_sites = ( 'all' === $_REQUEST['select'] ? 'all' : array( sanitize_text_field( wp_unslash( $_REQUEST['select'] ) ) ) ); |
| 35 |
} |
| 36 |
|
| 37 |
$val = get_post_meta( $post->ID, '_selected_groups', true ); |
| 38 |
$selected_groups = MainWP_System_Utility::maybe_unserialyze( $val ); |
| 39 |
|
| 40 |
if ( '' == $selected_groups ) { |
| 41 |
$selected_groups = array(); |
| 42 |
} |
| 43 |
?> |
| 44 |
<input type="hidden" name="select_sites_nonce" id="select_sites_nonce" value="<?php echo wp_create_nonce( 'select_sites_' . $post->ID ); ?>" /> |
| 45 |
<div class="mainwp-select-sites"> |
| 46 |
<?php MainWP_UI::select_sites_box( 'checkbox', true, true, 'mainwp_select_sites_box_left', '', $selected_sites, $selected_groups, false, $post->ID ); ?> |
| 47 |
</div> |
| 48 |
<?php |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Method select_sites_handle() |
| 53 |
* |
| 54 |
* Update Post meta for Select Sites Meta boxes. |
| 55 |
* |
| 56 |
* @param mixed $post_id Post ID. |
| 57 |
* @param mixed $post_type Post type. |
| 58 |
* |
| 59 |
* @return int $post_id Post ID. |
| 60 |
*/ |
| 61 |
public function select_sites_handle( $post_id, $post_type ) { |
| 62 |
/** |
| 63 |
* Verify this came from the our screen and with proper authorization. |
| 64 |
*/ |
| 65 |
if ( ! isset( $_POST['select_sites_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['select_sites_nonce'] ), 'select_sites_' . $post_id ) ) { |
| 66 |
return $post_id; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything. |
| 71 |
*/ |
| 72 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 73 |
return $post_id; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Check permissions. |
| 78 |
*/ |
| 79 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 80 |
return $post_id; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* OK, we're authenticated: we need to find and save the data. |
| 85 |
*/ |
| 86 |
$_post = get_post( $post_id ); |
| 87 |
if ( $_post->post_type == $post_type && isset( $_POST['select_by'] ) ) { |
| 88 |
$selected_wp = array(); |
| 89 |
if ( isset( $_POST['selected_sites'] ) ) { |
| 90 |
if ( is_array( $_POST['selected_sites'] ) ) { |
| 91 |
$selected_wp = ! empty( $_POST['selected_sites'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['selected_sites'] ) ) : array(); |
| 92 |
} else { // radio selection. |
| 93 |
$selected_wp = ! empty( $_POST['selected_sites'] ) ? array( sanitize_text_field( wp_unslash( $_POST['selected_sites'] ) ) ) : array(); |
| 94 |
} |
| 95 |
} |
| 96 |
update_post_meta( $post_id, '_selected_sites', $selected_wp ); |
| 97 |
$selected_groups = array(); |
| 98 |
if ( isset( $_POST['selected_groups'] ) ) { |
| 99 |
if ( is_array( $_POST['selected_groups'] ) ) { |
| 100 |
$selected_groups = ! empty( $_POST['selected_groups'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['selected_groups'] ) ) : array(); |
| 101 |
} else { // radio selection. |
| 102 |
$selected_wp = ! empty( $_POST['selected_groups'] ) ? array( sanitize_text_field( wp_unslash( $_POST['selected_groups'] ) ) ) : array(); |
| 103 |
} |
| 104 |
} |
| 105 |
update_post_meta( $post_id, '_selected_groups', $selected_groups ); |
| 106 |
update_post_meta( $post_id, '_selected_by', sanitize_text_field( wp_unslash( $_POST['select_by'] ) ) ); |
| 107 |
|
| 108 |
if ( ( 'group' === $_POST['select_by'] && 0 < count( $selected_groups ) ) || ( 'site' === $_POST['select_by'] && 0 < count( $selected_wp ) ) ) { |
| 109 |
return sanitize_text_field( wp_unslash( $_POST['select_by'] ) ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return $post_id; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Method add_categories() |
| 118 |
* |
| 119 |
* Render add categories. |
| 120 |
* |
| 121 |
* @param object $post Post object. |
| 122 |
*/ |
| 123 |
public function add_categories( $post ) { |
| 124 |
$categories = apply_filters( 'mainwp_bulkpost_saved_categories', $post, array() ); |
| 125 |
if ( empty( $categories ) || ! is_array( $categories ) || ( is_array( $categories ) && 1 == count( $categories ) && empty( $categories[0] ) ) ) { |
| 126 |
if ( $post ) { |
| 127 |
$categories = base64_decode( get_post_meta( $post->ID, '_categories', true ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_decode used for http encoding compatible. |
| 128 |
$categories = explode( ',', $categories ); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
if ( ! is_array( $categories ) ) { |
| 133 |
$categories = array(); |
| 134 |
} |
| 135 |
$uncat = __( 'Uncategorized', 'mainwp' ); |
| 136 |
|
| 137 |
$post_only = false; |
| 138 |
if ( $post ) { |
| 139 |
$post_only = get_post_meta( $post->ID, '_post_to_only_existing_categories', true ); |
| 140 |
} |
| 141 |
?> |
| 142 |
<input type="hidden" name="post_category_nonce" value="<?php echo esc_attr( wp_create_nonce( 'post_category_' . $post->ID ) ); ?>" /> |
| 143 |
|
| 144 |
<div id="taxonomy-category" class="categorydiv"> |
| 145 |
<div> |
| 146 |
<label> |
| 147 |
<input value="1" type="checkbox" <?php echo $post_only ? 'checked' : ''; ?> name="post_only_existing"><?php esc_html_e( 'Post only to existing categories', 'mainwp' ); ?> |
| 148 |
</label> |
| 149 |
</div> |
| 150 |
<ul id="category-tabs" class="category-tabs"> |
| 151 |
<li class="tabs"><a href="#category-all"><?php esc_html_e( 'All categories', 'mainwp' ); ?></a></li> |
| 152 |
</ul> |
| 153 |
|
| 154 |
<div id="category-all" class="tabs-panel" style="display: block;"> |
| 155 |
<ul id="categorychecklist" data-wp-lists="list:category" class="categorychecklist form-no-clear post_add_categories"> |
| 156 |
<?php if ( ! in_array( $uncat, $categories ) ) { ?> |
| 157 |
<li class="popular-category sitecategory"> |
| 158 |
<label class="selectit"> |
| 159 |
<input value="Uncategorized" type="checkbox" name="post_category[]"><?php esc_html_e( 'Uncategorized', 'mainwp' ); ?> |
| 160 |
</label> |
| 161 |
</li> |
| 162 |
<?php } ?> |
| 163 |
<?php |
| 164 |
foreach ( $categories as $cat ) { |
| 165 |
if ( empty( $cat ) ) { |
| 166 |
continue; |
| 167 |
} |
| 168 |
$cat_name = rawurldecode( $cat ); |
| 169 |
?> |
| 170 |
<li class="popular-category sitecategory"> |
| 171 |
<label class="selectit"> |
| 172 |
<input value="<?php echo esc_attr( $cat ); ?>" type="checkbox" checked name="post_category[]"><?php echo esc_attr( $cat_name ); ?> |
| 173 |
</label> |
| 174 |
</li> |
| 175 |
<?php } ?> |
| 176 |
</ul> |
| 177 |
</div> |
| 178 |
|
| 179 |
<div id="category-adder" class="wp-hidden-children"> |
| 180 |
<h4><a id="category-add-toggle" href="#category-add" class="hide-if-no-js"><?php esc_html_e( '+ Add new category', 'mainwp' ); ?></a></h4> |
| 181 |
|
| 182 |
<p id="category-add" class="category-add wp-hidden-child"> |
| 183 |
<label class="screen-reader-text" for="newcategory"><?php esc_html_e( 'Add new category', 'mainwp' ); ?></label> |
| 184 |
<input type="text" name="newcategory" id="newcategory" class="form-required" value="<?php esc_attr_e( 'New category name', 'mainwp' ); ?>" aria-required="true"> |
| 185 |
<input type="button" id="mainwp-category-add-submit" class="button mainwp-category-add-submit" value="<?php esc_attr_e( 'Add new category', 'mainwp' ); ?>"> |
| 186 |
<input type="hidden" id="_ajax_nonce-add-category" name="_ajax_nonce-add-category" value="<?php echo esc_attr( wp_create_nonce( 'add-category' . $post->ID ) ); ?>"> |
| 187 |
<span id="category-ajax-response"></span> |
| 188 |
</p> |
| 189 |
</div> |
| 190 |
</div> |
| 191 |
<?php |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Method add_categories_handle() |
| 196 |
* |
| 197 |
* Handle adding categories. |
| 198 |
* |
| 199 |
* @param int $post_id Post ID. |
| 200 |
* @param string $post_type Post type. |
| 201 |
*/ |
| 202 |
public function add_categories_handle( $post_id, $post_type ) { |
| 203 |
/** |
| 204 |
* Verify this came from the our screen and with proper authorization. |
| 205 |
*/ |
| 206 |
if ( ! isset( $_POST['post_category_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['post_category_nonce'] ), 'post_category_' . $post_id ) ) { |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything. |
| 212 |
*/ |
| 213 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Check permissions. |
| 219 |
*/ |
| 220 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 221 |
return; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* OK, we're authenticated: we need to find and save the data. |
| 226 |
*/ |
| 227 |
$_post = get_post( $post_id ); |
| 228 |
if ( $_post->post_type == $post_type ) { |
| 229 |
if ( isset( $_POST['post_category'] ) && is_array( $_POST['post_category'] ) ) { |
| 230 |
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. |
| 231 |
do_action( 'mainwp_bulkpost_categories_handle', $post_id, wp_unslash( $_POST['post_category'] ) ); |
| 232 |
} |
| 233 |
|
| 234 |
$post_existing = ! empty( $_POST['post_only_existing'] ) ? 1 : 0; |
| 235 |
update_post_meta( $post_id, '_post_to_only_existing_categories', $post_existing ); |
| 236 |
|
| 237 |
return; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Method add_tags() |
| 243 |
* |
| 244 |
* Add tags to Post array. |
| 245 |
* |
| 246 |
* @param object $post Post object. |
| 247 |
*/ |
| 248 |
public function add_tags( $post ) { |
| 249 |
$this->add_extra( 'Tags', '_tags', 'add_tags', $post ); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Method add_tags_handle() |
| 254 |
* |
| 255 |
* Add Tags to post array handler. |
| 256 |
* |
| 257 |
* @param int $post_id Post ID. |
| 258 |
* @param string $post_type Post type. |
| 259 |
*/ |
| 260 |
public function add_tags_handle( $post_id, $post_type ) { |
| 261 |
$this->add_extra_handle( 'Tags', '_tags', 'add_tags', $post_id, $post_type ); |
| 262 |
if ( isset( $_POST['add_tags'] ) ) { |
| 263 |
do_action( 'mainwp_bulkpost_tags_handle', $post_id, $post_type, wp_unslash( $_POST['add_tags'] ) ); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Method add_slug() |
| 269 |
* |
| 270 |
* Add Slug to Post object. |
| 271 |
* |
| 272 |
* @param object $post Post object. |
| 273 |
*/ |
| 274 |
public function add_slug( $post ) { |
| 275 |
$this->add_extra( 'Slug', '_slug', 'add_slug', $post ); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Method add_extra() |
| 280 |
* |
| 281 |
* Add nounce to post object. |
| 282 |
* |
| 283 |
* @param string $title Post title. |
| 284 |
* @param string $saveto Save to. |
| 285 |
* @param string $prefix Custom prefix. |
| 286 |
* @param object $post Post object. |
| 287 |
*/ |
| 288 |
private function add_extra( $title, $saveto, $prefix, $post ) { |
| 289 |
$extra = base64_decode( get_post_meta( $post->ID, $saveto, true ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_decode used for http encoding compatible. |
| 290 |
?> |
| 291 |
<input type="hidden" name="<?php echo esc_attr( $prefix ); ?>_nonce" value="<?php echo esc_attr( wp_create_nonce( $prefix . '_' . $post->ID ) ); ?>"/> |
| 292 |
<input type="text" name="<?php echo esc_attr( $prefix ); ?>" value="<?php echo esc_attr( $extra ); ?>"/> |
| 293 |
<?php |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
/** |
| 298 |
* Method add_slug_handle() |
| 299 |
* |
| 300 |
* Add post slug. |
| 301 |
* |
| 302 |
* @param int $post_id Post ID. |
| 303 |
* @param string $post_type Post type. |
| 304 |
*/ |
| 305 |
public function add_slug_handle( $post_id, $post_type ) { |
| 306 |
$this->add_extra_handle( 'Slug', '_slug', 'add_slug', $post_id, $post_type ); |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Method add_extra_handle() |
| 311 |
* |
| 312 |
* Update Post meta & add Security Nonce Prefix. |
| 313 |
* |
| 314 |
* @param string $title Post title. |
| 315 |
* @param string $saveto Where to save. |
| 316 |
* @param string $prefix Custom prefix. |
| 317 |
* @param int $post_id Post ID. |
| 318 |
* @param string $post_type Post type. |
| 319 |
* |
| 320 |
* @return int $post_id Post ID. |
| 321 |
*/ |
| 322 |
private function add_extra_handle( $title, $saveto, $prefix, $post_id, $post_type ) { |
| 323 |
/** |
| 324 |
* Verify this came from the our screen and with proper authorization. |
| 325 |
*/ |
| 326 |
if ( ! isset( $_POST[ $prefix . '_nonce' ] ) || ! wp_verify_nonce( sanitize_key( $_POST[ $prefix . '_nonce' ] ), $prefix . '_' . $post_id ) ) { |
| 327 |
return $post_id; |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything. |
| 332 |
*/ |
| 333 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 334 |
return $post_id; |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Check permissions. |
| 339 |
*/ |
| 340 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 341 |
return $post_id; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* OK, we're authenticated: we need to find and save the data. |
| 346 |
*/ |
| 347 |
$_post = get_post( $post_id ); |
| 348 |
if ( $_post->post_type == $post_type && isset( $_POST[ $prefix ] ) ) { |
| 349 |
$value = isset( $_POST[ $prefix ] ) ? base64_encode( wp_unslash( $_POST[ $prefix ] ) ) : ''; // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible. |
| 350 |
update_post_meta( $post_id, $saveto, $value ); |
| 351 |
return $value; |
| 352 |
} |
| 353 |
|
| 354 |
return $post_id; |
| 355 |
} |
| 356 |
|
| 357 |
} |
| 358 |
?> |
| 359 |
|