PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.0.8
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.0.8
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / admin / class-convertkit-admin-bulk-edit.php

class-convertkit-admin-bulk-edit.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 2.0.8, at admin/class-convertkit-admin-bulk-edit.php

171 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 cannot determine the screen.
38 if ( ! function_exists( 'get_current_screen' ) ) {
39 return;
40 }
41
42 // Bail if we're not on a Post Type Edit screen.
43 $screen = get_current_screen();
44 if ( $screen->base !== 'edit' ) {
45 return;
46 }
47
48 // Bail if the Post isn't a supported Post Type.
49 if ( ! in_array( $screen->post_type, convertkit_get_supported_post_types(), true ) ) {
50 return;
51 }
52
53 // Enqueue JS.
54 wp_enqueue_script( 'convertkit-bulk-edit', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/bulk-edit.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
55
56 // Output Bulk Edit fields in the footer of the Administration screen.
57 add_action( 'in_admin_footer', array( $this, 'bulk_edit_fields' ), 10 );
58
59 }
60
61 /**
62 * Save Bulk Edit data.
63 *
64 * Logic used here follows how WordPress handles bulk editing in bulk_edit_posts().
65 *
66 * @since 2.0.0
67 */
68 public function bulk_edit_save() {
69
70 // Bail if the bulk action isn't 'edit'.
71 if ( ! $this->is_bulk_edit_request() ) {
72 return;
73 }
74
75 // Bail if no nonce field exists.
76 if ( ! isset( $_REQUEST['wp-convertkit-save-meta-nonce'] ) ) {
77 return;
78 }
79
80 // Bail if the nonce verification fails.
81 if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['wp-convertkit-save-meta-nonce'] ) ), 'wp-convertkit-save-meta' ) ) {
82 return;
83 }
84
85 // Bail if the Post isn't a supported Post Type.
86 if ( ! in_array( sanitize_text_field( $_REQUEST['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 = get_post_type_object( $_REQUEST['post_type'] );
97
98 // Bail if the logged in user cannot edit Pages/Posts.
99 if ( ! current_user_can( $post_type->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->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, $_REQUEST['wp-convertkit'] );
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_api_key_and_secret() ) {
132 return;
133 }
134
135 // Fetch Forms, Landing Pages and Tags.
136 $convertkit_forms = new ConvertKit_Resource_Forms();
137 $convertkit_landing_pages = new ConvertKit_Resource_Landing_Pages();
138 $convertkit_tags = new ConvertKit_Resource_Tags();
139
140 // Output view.
141 require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/post/bulk-edit.php';
142
143 }
144
145 /**
146 * Determines if the request is for saving values via bulk editing.
147 *
148 * @since 1.9.8.0
149 *
150 * @return bool Is bulk edit request
151 */
152 private function is_bulk_edit_request() {
153
154 // Determine the current bulk action, if any.
155 $wp_list_table = _get_list_table( 'WP_Posts_List_Table' );
156 $bulk_action = $wp_list_table->current_action();
157
158 // Bail if the bulk action isn't edit.
159 if ( $bulk_action !== 'edit' ) {
160 return false;
161 }
162 if ( ! array_key_exists( 'bulk_edit', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification
163 return false;
164 }
165
166 return true;
167
168 }
169
170 }
171