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

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

212 lines 5.3 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 // Enqueue Refresh Resources CSS.
89 wp_enqueue_style( 'convertkit-admin-refresh-resources', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/refresh-resources.css', array(), CONVERTKIT_PLUGIN_VERSION );
90
91 /**
92 * Enqueue CSS when editing a Category that outputs
93 * ConvertKit Plugin settings.
94 *
95 * @since 1.9.6.4
96 */
97 do_action( 'convertkit_admin_category_enqueue_styles' );
98
99 }
100
101 /**
102 * Determine if the current request is for editing a Category.
103 *
104 * @since 2.0.3
105 *
106 * @param string $hook Hook.
107 * @return bool Is category edit screen request
108 */
109 private function is_category_edit_screen( $hook ) {
110
111 // Bail if we are not editing a Term.
112 if ( $hook !== 'term.php' && $hook !== 'edit-tags.php' ) {
113 return false;
114 }
115
116 // Bail if we are not editing a Category.
117 if ( convertkit_get_current_screen( 'id' ) !== 'edit-category' ) {
118 return false;
119 }
120
121 // Bail if the API hasn't been configured.
122 $settings = new ConvertKit_Settings();
123 if ( ! $settings->has_access_and_refresh_token() ) {
124 return false;
125 }
126
127 return true;
128 }
129
130 /**
131 * Display the ConvertKit Forms dropdown when adding a Category
132 *
133 * @since 2.0.3
134 */
135 public function add_category_form_fields() {
136
137 // Don't show the form fields if the API hasn't been configured.
138 $settings = new ConvertKit_Settings();
139 if ( ! $settings->has_access_and_refresh_token() ) {
140 return;
141 }
142
143 // Fetch Forms.
144 $convertkit_forms = new ConvertKit_Resource_Forms();
145
146 // Load view.
147 include CONVERTKIT_PLUGIN_PATH . '/views/backend/term/fields-add.php';
148
149 }
150
151 /**
152 * Display the ConvertKit Forms dropdown when editing a Category
153 *
154 * @since 1.9.6
155 *
156 * @param WP_Term $term Category.
157 */
158 public function edit_category_form_fields( $term ) {
159
160 // Don't show the form fields if the API hasn't been configured.
161 $settings = new ConvertKit_Settings();
162 if ( ! $settings->has_access_and_refresh_token() ) {
163 return;
164 }
165
166 // Fetch Category Settings and Forms.
167 $convertkit_term = new ConvertKit_Term( $term->term_id );
168 $convertkit_forms = new ConvertKit_Resource_Forms();
169
170 // Load view.
171 include CONVERTKIT_PLUGIN_PATH . '/views/backend/term/fields-edit.php';
172
173 }
174
175 /**
176 * Save Term Settings.
177 *
178 * @since 1.9.6
179 *
180 * @param int $term_id Term ID.
181 */
182 public function save_category_fields( $term_id ) {
183
184 // Bail if no nonce field exists.
185 if ( ! isset( $_POST['wp-convertkit-save-meta-nonce'] ) ) {
186 return;
187 }
188
189 // Bail if the nonce verification fails.
190 if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['wp-convertkit-save-meta-nonce'] ) ), 'wp-convertkit-save-meta' ) ) {
191 return;
192 }
193
194 // Bail if no ConvertKit settings were posted.
195 if ( ! isset( $_POST['wp-convertkit'] ) ) {
196 return;
197 }
198
199 // Build metadata.
200 $meta = array(
201 'form' => ( isset( $_POST['wp-convertkit']['form'] ) ? intval( $_POST['wp-convertkit']['form'] ) : '' ),
202 'form_position' => ( isset( $_POST['wp-convertkit']['form_position'] ) ? sanitize_text_field( wp_unslash( $_POST['wp-convertkit']['form_position'] ) ) : '' ),
203 );
204
205 // Save metadata.
206 $convertkit_term = new ConvertKit_Term( $term_id );
207 return $convertkit_term->save( $meta );
208
209 }
210
211 }
212