PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / trunk
MainWP Dashboard: Self-hosted WordPress Management for Agencies vtrunk
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-bulk-post.php

class-mainwp-bulk-post.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies trunk, at class/class-mainwp-bulk-post.php

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