PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.0.1
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.0.1
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.1, at admin/class-convertkit-admin-bulk-edit.php

174 lines 4.6 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 // Enqueue CSS.
57 wp_enqueue_style( 'convertkit-bulk-edit', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/bulk-edit.css', array(), CONVERTKIT_PLUGIN_VERSION );
58
59 // Output Bulk Edit fields in the footer of the Administration screen.
60 add_action( 'in_admin_footer', array( $this, 'bulk_edit_fields' ), 10, 2 );
61
62 }
63
64 /**
65 * Save Bulk Edit data.
66 *
67 * Logic used here follows how WordPress handles bulk editing in bulk_edit_posts().
68 *
69 * @since 2.0.0
70 */
71 public function bulk_edit_save() {
72
73 // Bail if the bulk action isn't 'edit'.
74 if ( ! $this->is_bulk_edit_request() ) {
75 return;
76 }
77
78 // Bail if no nonce field exists.
79 if ( ! isset( $_REQUEST['wp-convertkit-save-meta-nonce'] ) ) {
80 return;
81 }
82
83 // Bail if the nonce verification fails.
84 if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['wp-convertkit-save-meta-nonce'] ) ), 'wp-convertkit-save-meta' ) ) {
85 return;
86 }
87
88 // Bail if the Post isn't a supported Post Type.
89 if ( ! in_array( sanitize_text_field( $_REQUEST['post_type'] ), convertkit_get_supported_post_types(), true ) ) {
90 return;
91 }
92
93 // Bail if no ConvertKit settings were included in the Bulk Edit request.
94 if ( ! isset( $_REQUEST['wp-convertkit'] ) ) {
95 return;
96 }
97
98 // Get Post Type object.
99 $post_type = get_post_type_object( $_REQUEST['post_type'] );
100
101 // Bail if the logged in user cannot edit Pages/Posts.
102 if ( ! current_user_can( $post_type->cap->edit_posts ) ) {
103 wp_die(
104 sprintf(
105 /* translators: Post Type name */
106 esc_html__( 'Sorry, you are not allowed to edit %s.', 'convertkit' ),
107 esc_html( $post_type->name )
108 )
109 );
110 }
111
112 // Get Post IDs that are bulk edited.
113 $post_ids = array_map( 'intval', (array) $_REQUEST['post'] );
114
115 // Iterate through each Post, updating its settings.
116 foreach ( $post_ids as $post_id ) {
117 WP_ConvertKit()->get_class( 'admin_post' )->save_post_settings( $post_id, $_REQUEST['wp-convertkit'] );
118 }
119
120 }
121
122 /**
123 * Outputs Bulk Edit settings fields in the footer of the administration screen.
124 *
125 * The Bulk Edit JS will then move these hidden fields into the Bulk Edit row
126 * when the user clicks on a Bulk Edit action in the WP_List_Table.
127 *
128 * @since 1.9.8.0
129 */
130 public function bulk_edit_fields() {
131
132 // Don't output Bulk Edit fields if the API settings have not been defined.
133 $settings = new ConvertKit_Settings();
134 if ( ! $settings->has_api_key_and_secret() ) {
135 return;
136 }
137
138 // Fetch Forms, Landing Pages and Tags.
139 $convertkit_forms = new ConvertKit_Resource_Forms();
140 $convertkit_landing_pages = new ConvertKit_Resource_Landing_Pages();
141 $convertkit_tags = new ConvertKit_Resource_Tags();
142
143 // Output view.
144 require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/post/bulk-edit.php';
145
146 }
147
148 /**
149 * Determines if the request is for saving values via bulk editing.
150 *
151 * @since 1.9.8.0
152 *
153 * @return bool Is bulk edit request
154 */
155 private function is_bulk_edit_request() {
156
157 // Determine the current bulk action, if any.
158 $wp_list_table = _get_list_table( 'WP_Posts_List_Table' );
159 $bulk_action = $wp_list_table->current_action();
160
161 // Bail if the bulk action isn't edit.
162 if ( $bulk_action !== 'edit' ) {
163 return false;
164 }
165 if ( ! array_key_exists( 'bulk_edit', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification
166 return false;
167 }
168
169 return true;
170
171 }
172
173 }
174