PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.3.3
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.3.3
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-category.php

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

211 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Admin Category class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Registers fields on Categories and saves its settings when the Category
11 * is saved in the WordPress Administration interface.
12 *
13 * @package ConvertKit
14 * @author ConvertKit
15 */
16 class ConvertKit_Admin_Category {
17
18 /**
19 * Registers action and filter hooks.
20 *
21 * @since 1.9.6
22 */
23 public function __construct() {
24
25 // Load CSS and JS when adding/editing Categories.
26 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
27 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
28
29 // Add Category.
30 add_action( 'category_add_form_fields', array( $this, 'add_category_form_fields' ), 10 );
31 add_action( 'created_category', array( $this, 'save_category_fields' ), 20 );
32
33 // Edit Category.
34 add_action( 'category_edit_form_fields', array( $this, 'edit_category_form_fields' ), 20 );
35 add_action( 'edited_category', array( $this, 'save_category_fields' ), 20 );
36
37 }
38
39 /**
40 * Enqueue JavaScript when editing a Category that outputs
41 * ConvertKit Plugin settings.
42 *
43 * @since 1.9.6.4
44 *
45 * @param string $hook Hook.
46 */
47 public function enqueue_scripts( $hook ) {
48
49 // Don't load scripts if not editing a Category.
50 if ( ! $this->is_category_edit_screen( $hook ) ) {
51 return;
52 }
53
54 // Enqueue Select2 JS.
55 convertkit_select2_enqueue_scripts();
56
57 /**
58 * Enqueue JavaScript when editing a Category that outputs
59 * ConvertKit Plugin settings.
60 *
61 * @since 1.9.6.4
62 */
63 do_action( 'convertkit_admin_category_enqueue_scripts' );
64
65 }
66
67 /**
68 * Enqueue CSS when editing a Category that outputs
69 * ConvertKit Plugin settings.
70 *
71 * @since 1.9.6.4
72 *
73 * @param string $hook Hook.
74 */
75 public function enqueue_styles( $hook ) {
76
77 // Don't load scripts if not editing a Category.
78 if ( ! $this->is_category_edit_screen( $hook ) ) {
79 return;
80 }
81
82 // Enqueue Select2 CSS.
83 convertkit_select2_enqueue_styles();
84
85 // Enqueue Category CSS.
86 wp_enqueue_style( 'convertkit-category', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/category.css', array(), CONVERTKIT_PLUGIN_VERSION );
87
88 /**
89 * Enqueue CSS when editing a Category that outputs
90 * ConvertKit Plugin settings.
91 *
92 * @since 1.9.6.4
93 */
94 do_action( 'convertkit_admin_category_enqueue_styles' );
95
96 }
97
98 /**
99 * Determine if the current request is for editing a Category.
100 *
101 * @since 2.0.3
102 *
103 * @param string $hook Hook.
104 * @return bool Is category edit screen request
105 */
106 private function is_category_edit_screen( $hook ) {
107
108 // Bail if we are not editing a Term.
109 if ( $hook !== 'term.php' && $hook !== 'edit-tags.php' ) {
110 return false;
111 }
112
113 // Bail if we are not editing a Category.
114 if ( ! function_exists( 'get_current_screen' ) ) {
115 return false;
116 }
117 $screen = get_current_screen();
118
119 if ( $screen->id !== 'edit-category' ) {
120 return false;
121 }
122
123 // Bail if the API hasn't been configured.
124 $settings = new ConvertKit_Settings();
125 if ( ! $settings->has_api_key_and_secret() ) {
126 return false;
127 }
128
129 return true;
130 }
131
132 /**
133 * Display the ConvertKit Forms dropdown when adding a Category
134 *
135 * @since 2.0.3
136 */
137 public function add_category_form_fields() {
138
139 // Don't show the form fields if the API hasn't been configured.
140 $settings = new ConvertKit_Settings();
141 if ( ! $settings->has_api_key_and_secret() ) {
142 return;
143 }
144
145 // Fetch Forms.
146 $convertkit_forms = new ConvertKit_Resource_Forms();
147
148 // Load view.
149 include CONVERTKIT_PLUGIN_PATH . '/views/backend/term/fields-add.php';
150
151 }
152
153 /**
154 * Display the ConvertKit Forms dropdown when editing a Category
155 *
156 * @since 1.9.6
157 *
158 * @param WP_Term $term Category.
159 */
160 public function edit_category_form_fields( $term ) {
161
162 // Don't show the form fields if the API hasn't been configured.
163 $settings = new ConvertKit_Settings();
164 if ( ! $settings->has_api_key_and_secret() ) {
165 return;
166 }
167
168 // Fetch Category Settings and Forms.
169 $convertkit_term = new ConvertKit_Term( $term->term_id );
170 $convertkit_forms = new ConvertKit_Resource_Forms();
171
172 // Load view.
173 include CONVERTKIT_PLUGIN_PATH . '/views/backend/term/fields-edit.php';
174
175 }
176
177 /**
178 * Save Term Settings.
179 *
180 * @since 1.9.6
181 *
182 * @param int $term_id Term ID.
183 */
184 public function save_category_fields( $term_id ) {
185
186 // Bail if no nonce field exists.
187 if ( ! isset( $_POST['wp-convertkit-save-meta-nonce'] ) ) {
188 return;
189 }
190
191 // Bail if the nonce verification fails.
192 if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['wp-convertkit-save-meta-nonce'] ) ), 'wp-convertkit-save-meta' ) ) {
193 return;
194 }
195
196 // Bail if no ConvertKit settings were posted.
197 if ( ! isset( $_POST['wp-convertkit'] ) ) {
198 return;
199 }
200
201 // Build metadata.
202 $meta = ( isset( $_POST['wp-convertkit']['form'] ) ? intval( $_POST['wp-convertkit']['form'] ) : '' );
203
204 // Save metadata.
205 $convertkit_term = new ConvertKit_Term( $term_id );
206 return $convertkit_term->save( $meta );
207
208 }
209
210 }
211