PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / trunk
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages vtrunk
3.4.2 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 All 195 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 trunk, at admin/class-convertkit-admin-bulk-edit.php

179 lines 5.0 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'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 // Skip Posts the current user cannot edit.
115 // The post type's edit_posts capability checked above does not grant
116 // permission to edit every Post of that type.
117 if ( ! current_user_can( 'edit_post', $post_id ) ) {
118 continue;
119 }
120
121 WP_ConvertKit()->get_class( 'admin_post' )->save_post_settings( $post_id, wp_unslash( $_REQUEST['wp-convertkit'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
122 }
123
124 }
125
126 /**
127 * Outputs Bulk Edit settings fields in the footer of the administration screen.
128 *
129 * The Bulk Edit JS will then move these hidden fields into the Bulk Edit row
130 * when the user clicks on a Bulk Edit action in the WP_List_Table.
131 *
132 * @since 1.9.8.0
133 */
134 public function bulk_edit_fields() {
135
136 // Don't output Bulk Edit fields if the API settings have not been defined.
137 $settings = new ConvertKit_Settings();
138 if ( ! $settings->has_access_and_refresh_token() ) {
139 return;
140 }
141
142 // Initialize Restrict Content Settings class.
143 $restrict_content_settings = new ConvertKit_Settings_Restrict_Content();
144
145 // Fetch Forms, Landing Pages, Products and Tags.
146 $convertkit_forms = new ConvertKit_Resource_Forms();
147 $convertkit_landing_pages = new ConvertKit_Resource_Landing_Pages();
148 $convertkit_products = new ConvertKit_Resource_Products();
149 $convertkit_tags = new ConvertKit_Resource_Tags();
150
151 // Output view.
152 require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/post/bulk-edit.php';
153
154 }
155
156 /**
157 * Determines if the request is for saving values via bulk editing.
158 *
159 * @since 1.9.8.0
160 *
161 * @return bool Is bulk edit request
162 */
163 private function is_bulk_edit_request() {
164
165 // Determine the current bulk action, if any.
166 $wp_list_table = _get_list_table( 'WP_Posts_List_Table' );
167 $bulk_action = $wp_list_table->current_action();
168
169 // Bail if the bulk action isn't edit.
170 if ( $bulk_action !== 'edit' ) {
171 return false;
172 }
173
174 return filter_has_var( INPUT_GET, 'bulk_edit' );
175
176 }
177
178 }
179