| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP Bulk Post Class |
| 4 |
* |
| 5 |
* @package MainWP/Dashboard |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace MainWP\Dashboard; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class MainWP_Bulk_Post |
| 12 |
* |
| 13 |
* @package MainWP\Dashboard |
| 14 |
*/ |
| 15 |
class MainWP_Bulk_Post { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. |
| 16 |
|
| 17 |
/** |
| 18 |
* MainWP_Bulk_Post constructor. |
| 19 |
* |
| 20 |
* Run each time the class is called. |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
add_action( 'admin_post_mainwp_editpost', array( &$this, 'handle_edit_bulkpost' ) ); |
| 24 |
add_action( 'save_post', array( &$this, 'save_bulkpost' ) ); |
| 25 |
add_action( 'save_post', array( &$this, 'save_bulkpage' ) ); |
| 26 |
add_action( 'init', array( &$this, 'create_post_type' ) ); |
| 27 |
add_filter( 'post_updated_messages', array( &$this, 'post_updated_messages' ) ); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Method get_instance(). |
| 33 |
*/ |
| 34 |
public static function get_instance() { |
| 35 |
return new self(); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Method handle_edit_bulkpost() |
| 40 |
* |
| 41 |
* Handle bulk post edit process. |
| 42 |
*/ |
| 43 |
public function handle_edit_bulkpost() { |
| 44 |
|
| 45 |
$post_id = 0; |
| 46 |
if ( isset( $_POST['post_ID'] ) ) { |
| 47 |
$post_id = (int) $_POST['post_ID']; |
| 48 |
} |
| 49 |
|
| 50 |
if ( $post_id && isset( $_POST['select_sites_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['select_sites_nonce'] ), 'select_sites_' . $post_id ) ) { |
| 51 |
check_admin_referer( 'update-post_' . $post_id ); |
| 52 |
edit_post(); |
| 53 |
|
| 54 |
$location = admin_url( 'admin.php?page=PostBulkEdit&post_id=' . $post_id . '&message=1' ); |
| 55 |
|
| 56 |
/** |
| 57 |
* Filter: redirect_post_location |
| 58 |
* |
| 59 |
* Filters the location for the Edit process. |
| 60 |
* |
| 61 |
* @param int $post_id Post ID. |
| 62 |
* |
| 63 |
* @since Unknown |
| 64 |
*/ |
| 65 |
$location = apply_filters( 'redirect_post_location', $location, $post_id ); |
| 66 |
wp_safe_redirect( $location ); |
| 67 |
exit(); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Method redirect_edit_bulkpost(). |
| 73 |
* |
| 74 |
* Redirect to edit post. |
| 75 |
* |
| 76 |
* @param string $location Target URL. |
| 77 |
* @param int $post_id Post ID number. |
| 78 |
* |
| 79 |
* @return string $location Target URL. |
| 80 |
*/ |
| 81 |
public function redirect_edit_bulkpost( $location, $post_id ) { |
| 82 |
unset( $location ); |
| 83 |
if ( $post_id ) { |
| 84 |
$loc = admin_url( 'admin.php?page=PostBulkEdit&post_id=' . intval( $post_id ) ); |
| 85 |
} else { |
| 86 |
$loc = admin_url( 'admin.php?page=PostBulkAdd' ); |
| 87 |
} |
| 88 |
|
| 89 |
return $loc; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Method redirect_edit_bulkpage(). |
| 94 |
* |
| 95 |
* Redirect to edit page. |
| 96 |
* |
| 97 |
* @param string $location Target URL. |
| 98 |
* @param int $post_id Page ID number. |
| 99 |
* |
| 100 |
* @return string $location Target URL. |
| 101 |
*/ |
| 102 |
public function redirect_edit_bulkpage( $location, $post_id ) { |
| 103 |
unset( $location ); |
| 104 |
if ( $post_id ) { |
| 105 |
$loc = admin_url( 'admin.php?page=PageBulkEdit&post_id=' . intval( $post_id ) ); |
| 106 |
} else { |
| 107 |
$loc = admin_url( 'admin.php?page=PageBulkAdd' ); |
| 108 |
} |
| 109 |
|
| 110 |
return $loc; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
/** |
| 115 |
* Method save_bulkpost(). |
| 116 |
* |
| 117 |
* Save page (Bulk post custom post type). |
| 118 |
* |
| 119 |
* @param int $post_id Post ID. |
| 120 |
* |
| 121 |
* @return void |
| 122 |
* |
| 123 |
* @uses \MainWP\Dashboard\MainWP_Meta_Boxes::select_sites_handle() |
| 124 |
* @uses \MainWP\Dashboard\MainWP_Meta_Boxes::add_categories_handle() |
| 125 |
* @uses \MainWP\Dashboard\MainWP_Meta_Boxes::add_slug_handle() |
| 126 |
* @uses \MainWP\Dashboard\MainWP_Post_Page_Handler::add_sticky_handle() |
| 127 |
*/ |
| 128 |
public function save_bulkpost( $post_id ) { |
| 129 |
$_post = get_post( $post_id ); |
| 130 |
|
| 131 |
if ( 'bulkpost' !== $_post->post_type ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'update-post_' . $post_id ) ) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
if ( ! isset( $_POST['post_type'] ) || ( 'bulkpost' !== $_POST['post_type'] ) ) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
if ( isset( $_POST['mainwp_wpseo_metabox_save_values'] ) && ( ! empty( $_POST['mainwp_wpseo_metabox_save_values'] ) ) ) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
$pid = (int) MainWP_System::instance()->metaboxes->select_sites_handle( $post_id, 'bulkpost' ); |
| 152 |
MainWP_System::instance()->metaboxes->add_categories_handle( $post_id, 'bulkpost' ); |
| 153 |
MainWP_System::instance()->metaboxes->add_tags_handle( $post_id, 'bulkpost' ); |
| 154 |
MainWP_System::instance()->metaboxes->add_slug_handle( $post_id, 'bulkpost' ); |
| 155 |
MainWP_Post_Page_Handler::add_sticky_handle( $post_id ); |
| 156 |
MainWP_Post_Page_Handler::add_status_handle( $post_id ); |
| 157 |
|
| 158 |
/** |
| 159 |
* Action: mainwp_save_bulkpost |
| 160 |
* |
| 161 |
* Fires when saving the bulk post. |
| 162 |
* |
| 163 |
* @param int $post_id Post ID. |
| 164 |
* |
| 165 |
* @since Unknown |
| 166 |
*/ |
| 167 |
do_action( 'mainwp_save_bulkpost', $post_id ); |
| 168 |
|
| 169 |
if ( $pid === $post_id ) { |
| 170 |
add_filter( 'redirect_post_location', array( $this, 'redirect_edit_bulkpost' ), 10, 2 ); |
| 171 |
} else { |
| 172 |
/** |
| 173 |
* Action: mainwp_before_redirect_posting_bulkpost |
| 174 |
* |
| 175 |
* Fires before redirection to posting 'bulk post' page after post submission. |
| 176 |
* |
| 177 |
* @param object $_post Object containing post data. |
| 178 |
* |
| 179 |
* @since 4.0 |
| 180 |
*/ |
| 181 |
do_action( 'mainwp_before_redirect_posting_bulkpost', $_post ); |
| 182 |
wp_safe_redirect( get_site_url() . '/wp-admin/admin.php?page=PostingBulkPost&id=' . $post_id . '&hideall=1&posting_nonce=' . wp_create_nonce( 'posting_nonce_' . $post_id ) ); |
| 183 |
die(); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Method save_bulkpage(). |
| 189 |
* |
| 190 |
* Save page (Bulk page custom post type). |
| 191 |
* |
| 192 |
* @param int $post_id Page ID. |
| 193 |
* |
| 194 |
* @return void |
| 195 |
* |
| 196 |
* @uses \MainWP\Dashboard\MainWP_System::$metaboxes |
| 197 |
* @uses \MainWP\Dashboard\MainWP_Page::add_status_handle() |
| 198 |
*/ |
| 199 |
public function save_bulkpage( $post_id ) { |
| 200 |
|
| 201 |
$_post = get_post( $post_id ); |
| 202 |
|
| 203 |
if ( 'bulkpage' !== $_post->post_type ) { |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'update-post_' . $post_id ) ) { |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
if ( ! isset( $_POST['post_type'] ) || ( 'bulkpage' !== $_POST['post_type'] ) ) { |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
if ( isset( $_POST['mainwp_wpseo_metabox_save_values'] ) && ( ! empty( $_POST['mainwp_wpseo_metabox_save_values'] ) ) ) { |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
$pid = (int) MainWP_System::instance()->metaboxes->select_sites_handle( $post_id, 'bulkpage' ); |
| 224 |
MainWP_System::instance()->metaboxes->add_slug_handle( $post_id, 'bulkpage' ); |
| 225 |
MainWP_Post_Page_Handler::add_status_handle( $post_id ); |
| 226 |
|
| 227 |
/** |
| 228 |
* Action: mainwp_save_bulkpage |
| 229 |
* |
| 230 |
* Fires when saving the bulk page. |
| 231 |
* |
| 232 |
* @param int $post_id Post ID. |
| 233 |
* |
| 234 |
* @since Unknown |
| 235 |
*/ |
| 236 |
do_action( 'mainwp_save_bulkpage', $post_id ); |
| 237 |
|
| 238 |
if ( $pid === $post_id ) { |
| 239 |
add_filter( 'redirect_post_location', array( $this, 'redirect_edit_bulkpage' ), 10, 2 ); |
| 240 |
} else { |
| 241 |
/** |
| 242 |
* Action: mainwp_before_redirect_posting_bulkpage |
| 243 |
* |
| 244 |
* Fires before redirection to posting 'bulk page' page after post submission. |
| 245 |
* |
| 246 |
* @param object $_post Object containing post data. |
| 247 |
* |
| 248 |
* @since 4.0 |
| 249 |
*/ |
| 250 |
do_action( 'mainwp_before_redirect_posting_bulkpage', $_post ); |
| 251 |
wp_safe_redirect( get_site_url() . '/wp-admin/admin.php?page=PostingBulkPage&posting_nonce=' . wp_create_nonce( 'posting_nonce_' . $post_id ) . '&id=' . $post_id . '&hideall=1' ); |
| 252 |
die(); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Method create_post_type() |
| 258 |
* |
| 259 |
* Register "Bulkpost" and "Bulkpage" custom post types. |
| 260 |
*/ |
| 261 |
public function create_post_type() { |
| 262 |
|
| 263 |
$queryable = true; |
| 264 |
if ( function_exists( 'is_plugin_active' ) ) { |
| 265 |
$queryable = is_plugin_active( 'mainwp-post-plus-extension/mainwp-post-plus-extension.php' ); |
| 266 |
} |
| 267 |
|
| 268 |
$labels = array( |
| 269 |
'name' => _x( 'Bulkpost', 'bulkpost' ), |
| 270 |
'singular_name' => _x( 'Bulkpost', 'bulkpost' ), |
| 271 |
'add_new' => _x( 'Add New', 'bulkpost' ), |
| 272 |
'add_new_item' => _x( 'Add New Bulkpost', 'bulkpost' ), |
| 273 |
'edit_item' => _x( 'Edit Bulkpost', 'bulkpost' ), |
| 274 |
'new_item' => _x( 'New Bulkpost', 'bulkpost' ), |
| 275 |
'view_item' => _x( 'View Bulkpost', 'bulkpost' ), |
| 276 |
'search_items' => _x( 'Search Bulkpost', 'bulkpost' ), |
| 277 |
'not_found' => _x( 'No bulkpost found', 'bulkpost' ), |
| 278 |
'not_found_in_trash' => _x( 'No bulkpost found in Trash', 'bulkpost' ), |
| 279 |
'parent_item_colon' => _x( 'Parent Bulkpost:', 'bulkpost' ), |
| 280 |
'menu_name' => _x( 'Bulkpost', 'bulkpost' ), |
| 281 |
); |
| 282 |
|
| 283 |
$args = array( |
| 284 |
'labels' => $labels, |
| 285 |
'hierarchical' => false, |
| 286 |
'description' => 'description...', |
| 287 |
'supports' => array( |
| 288 |
'title', |
| 289 |
'editor', |
| 290 |
'excerpt', |
| 291 |
'thumbnail', |
| 292 |
'custom-fields', |
| 293 |
'comments', |
| 294 |
'revisions', |
| 295 |
), |
| 296 |
'public' => true, |
| 297 |
'show_ui' => true, |
| 298 |
'show_in_nav_menus' => false, |
| 299 |
'publicly_queryable' => $queryable, |
| 300 |
'exclude_from_search' => true, |
| 301 |
'has_archive' => false, |
| 302 |
'query_var' => false, |
| 303 |
'can_export' => false, |
| 304 |
'rewrite' => false, |
| 305 |
'capabilities' => array( |
| 306 |
'edit_post' => 'read', |
| 307 |
'edit_posts' => 'read', |
| 308 |
'edit_others_posts' => 'read', |
| 309 |
'publish_posts' => 'read', |
| 310 |
'read_post' => 'read', |
| 311 |
'read_private_posts' => 'read', |
| 312 |
'delete_post' => 'read', |
| 313 |
), |
| 314 |
); |
| 315 |
|
| 316 |
register_post_type( 'bulkpost', $args ); |
| 317 |
|
| 318 |
$labels = array( |
| 319 |
'name' => _x( 'Bulkpage', 'bulkpage' ), |
| 320 |
'singular_name' => _x( 'Bulkpage', 'bulkpage' ), |
| 321 |
'add_new' => _x( 'Add New', 'bulkpage' ), |
| 322 |
'add_new_item' => _x( 'Add New Bulkpage', 'bulkpage' ), |
| 323 |
'edit_item' => _x( 'Edit Bulkpage', 'bulkpage' ), |
| 324 |
'new_item' => _x( 'New Bulkpage', 'bulkpage' ), |
| 325 |
'view_item' => _x( 'View Bulkpage', 'bulkpage' ), |
| 326 |
'search_items' => _x( 'Search Bulkpage', 'bulkpage' ), |
| 327 |
'not_found' => _x( 'No bulkpage found', 'bulkpage' ), |
| 328 |
'not_found_in_trash' => _x( 'No bulkpage found in Trash', 'bulkpage' ), |
| 329 |
'parent_item_colon' => _x( 'Parent Bulkpage:', 'bulkpage' ), |
| 330 |
'menu_name' => _x( 'Bulkpage', 'bulkpage' ), |
| 331 |
); |
| 332 |
|
| 333 |
$args = array( |
| 334 |
'labels' => $labels, |
| 335 |
'hierarchical' => false, |
| 336 |
'description' => 'description...', |
| 337 |
'supports' => array( |
| 338 |
'title', |
| 339 |
'editor', |
| 340 |
'excerpt', |
| 341 |
'thumbnail', |
| 342 |
'custom-fields', |
| 343 |
'comments', |
| 344 |
'revisions', |
| 345 |
), |
| 346 |
'public' => true, |
| 347 |
'show_ui' => true, |
| 348 |
'show_in_nav_menus' => false, |
| 349 |
'publicly_queryable' => $queryable, |
| 350 |
'exclude_from_search' => true, |
| 351 |
'has_archive' => false, |
| 352 |
'query_var' => false, |
| 353 |
'can_export' => false, |
| 354 |
'rewrite' => false, |
| 355 |
'capabilities' => array( |
| 356 |
'edit_post' => 'read', |
| 357 |
'edit_posts' => 'read', |
| 358 |
'edit_others_posts' => 'read', |
| 359 |
'publish_posts' => 'read', |
| 360 |
'read_post' => 'read', |
| 361 |
'read_private_posts' => 'read', |
| 362 |
'delete_post' => 'read', |
| 363 |
), |
| 364 |
); |
| 365 |
|
| 366 |
register_post_type( 'bulkpage', $args ); |
| 367 |
|
| 368 |
do_action( 'mainwp_register_post_type' ); |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Method post_updated_messages() |
| 373 |
* |
| 374 |
* Render post updated message. |
| 375 |
* |
| 376 |
* @param string $messages Message to display. |
| 377 |
* |
| 378 |
* @return string $messages. |
| 379 |
*/ |
| 380 |
public function post_updated_messages( $messages ) { |
| 381 |
$messages['post'][98] = esc_html__( 'WordPress SEO values have been saved.', 'mainwp' ); |
| 382 |
$messages['post'][99] = esc_html__( 'You have to select the sites you wish to publish to.', 'mainwp' ); |
| 383 |
|
| 384 |
return $messages; |
| 385 |
} |
| 386 |
} |
| 387 |
|