| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Admin Bulk Edit class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Registers settings fields for output when using WordPress' Bulk Edit functionality |
| 11 |
* in a Post, Page or Custom Post Type WP_List_Table. |
| 12 |
* |
| 13 |
* @package ConvertKit |
| 14 |
* @author ConvertKit |
| 15 |
*/ |
| 16 |
class ConvertKit_Admin_Bulk_Edit { |
| 17 |
|
| 18 |
/** |
| 19 |
* Registers action and filter hooks. |
| 20 |
* |
| 21 |
* @since 1.9.8.0 |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
|
| 25 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 26 |
add_action( 'load-edit.php', array( $this, 'bulk_edit_save' ) ); |
| 27 |
|
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Enqueues scripts and CSS for Bulk Edit functionality in the Post, Page and Custom Post WP_List_Tables |
| 32 |
* |
| 33 |
* @since 1.9.8.0 |
| 34 |
*/ |
| 35 |
public function enqueue_assets() { |
| 36 |
|
| 37 |
// Bail if we're not on a Post Type Edit screen. |
| 38 |
if ( convertkit_get_current_screen( 'base' ) !== 'edit' ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
// Bail if the Post isn't a supported Post Type. |
| 43 |
if ( ! in_array( convertkit_get_current_screen( 'post_type' ), convertkit_get_supported_post_types(), true ) ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
// Enqueue JS. |
| 48 |
wp_enqueue_script( 'convertkit-bulk-edit', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/bulk-edit.js', array(), CONVERTKIT_PLUGIN_VERSION, true ); |
| 49 |
|
| 50 |
// Output Bulk Edit fields in the footer of the Administration screen. |
| 51 |
add_action( 'in_admin_footer', array( $this, 'bulk_edit_fields' ), 10 ); |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Save Bulk Edit data. |
| 57 |
* |
| 58 |
* Logic used here follows how WordPress handles bulk editing in bulk_edit_posts(). |
| 59 |
* |
| 60 |
* @since 2.0.0 |
| 61 |
*/ |
| 62 |
public function bulk_edit_save() { |
| 63 |
|
| 64 |
// Bail if the bulk action isn't 'edit'. |
| 65 |
if ( ! $this->is_bulk_edit_request() ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
// Bail if no nonce field exists. |
| 70 |
if ( ! isset( $_REQUEST['wp-convertkit-save-meta-nonce'] ) ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
// Bail if the nonce verification fails. |
| 75 |
if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['wp-convertkit-save-meta-nonce'] ) ), 'wp-convertkit-save-meta' ) ) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
// Bail if the Post Type or Post IDs are not specified. |
| 80 |
if ( ! isset( $_REQUEST['post_type'] ) || ! isset( $_REQUEST['post'] ) ) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
// Bail if the Post isn't a supported Post Type. |
| 85 |
$post_type = sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ); |
| 86 |
if ( ! in_array( $post_type, convertkit_get_supported_post_types(), true ) ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
// Bail if no ConvertKit settings were included in the Bulk Edit request. |
| 91 |
if ( ! isset( $_REQUEST['wp-convertkit'] ) ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
// Get Post Type object. |
| 96 |
$post_type_object = get_post_type_object( $post_type ); |
| 97 |
|
| 98 |
// Bail if the logged in user cannot edit Pages/Posts. |
| 99 |
if ( ! current_user_can( $post_type_object->cap->edit_posts ) ) { |
| 100 |
wp_die( |
| 101 |
sprintf( |
| 102 |
/* translators: Post Type name */ |
| 103 |
esc_html__( 'Sorry, you are not allowed to edit %s.', 'convertkit' ), |
| 104 |
esc_html( $post_type_object->name ) |
| 105 |
) |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
// Get Post IDs that are bulk edited. |
| 110 |
$post_ids = array_map( 'intval', (array) $_REQUEST['post'] ); |
| 111 |
|
| 112 |
// Iterate through each Post, updating its settings. |
| 113 |
foreach ( $post_ids as $post_id ) { |
| 114 |
WP_ConvertKit()->get_class( 'admin_post' )->save_post_settings( $post_id, wp_unslash( $_REQUEST['wp-convertkit'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Outputs Bulk Edit settings fields in the footer of the administration screen. |
| 121 |
* |
| 122 |
* The Bulk Edit JS will then move these hidden fields into the Bulk Edit row |
| 123 |
* when the user clicks on a Bulk Edit action in the WP_List_Table. |
| 124 |
* |
| 125 |
* @since 1.9.8.0 |
| 126 |
*/ |
| 127 |
public function bulk_edit_fields() { |
| 128 |
|
| 129 |
// Don't output Bulk Edit fields if the API settings have not been defined. |
| 130 |
$settings = new ConvertKit_Settings(); |
| 131 |
if ( ! $settings->has_access_and_refresh_token() ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
// Initialize Restrict Content Settings class. |
| 136 |
$restrict_content_settings = new ConvertKit_Settings_Restrict_Content(); |
| 137 |
|
| 138 |
// Fetch Forms, Landing Pages, Products and Tags. |
| 139 |
$convertkit_forms = new ConvertKit_Resource_Forms(); |
| 140 |
$convertkit_landing_pages = new ConvertKit_Resource_Landing_Pages(); |
| 141 |
$convertkit_products = new ConvertKit_Resource_Products(); |
| 142 |
$convertkit_tags = new ConvertKit_Resource_Tags(); |
| 143 |
|
| 144 |
// Output view. |
| 145 |
require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/post/bulk-edit.php'; |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Determines if the request is for saving values via bulk editing. |
| 151 |
* |
| 152 |
* @since 1.9.8.0 |
| 153 |
* |
| 154 |
* @return bool Is bulk edit request |
| 155 |
*/ |
| 156 |
private function is_bulk_edit_request() { |
| 157 |
|
| 158 |
// Determine the current bulk action, if any. |
| 159 |
$wp_list_table = _get_list_table( 'WP_Posts_List_Table' ); |
| 160 |
$bulk_action = $wp_list_table->current_action(); |
| 161 |
|
| 162 |
// Bail if the bulk action isn't edit. |
| 163 |
if ( $bulk_action !== 'edit' ) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
if ( ! array_key_exists( 'bulk_edit', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
return true; |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
|