PluginProbe
Quill Forms | Conversational Multi Step Forms, Surveys & quizzes / trunk
Quill Forms | Conversational Multi Step Forms, Surveys & quizzes vtrunk
5.7.3 5.7.2 5.7.1 1.8.1 1.8.10 1.8.11 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 2.0.0 2.1.0 2.10.0 2.11.0 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 All 146 releases
quillforms / includes / admin / class-admin.php

class-admin.php in Quill Forms | Conversational Multi Step Forms, Surveys & quizzes trunk, at includes/admin/class-admin.php

391 lines 12.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Admin: class Admin
4 *
5 * @since 1.0.0
6 * @package QuillForms
7 * @subpackage Admin
8 */
9
10 namespace QuillForms\Admin;
11
12 use QuillForms\Capabilities;
13 use QuillForms\Settings;
14
15 /**
16 * QuillForms Admin
17 *
18 * @since 1.0.0
19 */
20 class Admin {
21
22 /**
23 * Class Instance.
24 *
25 * @var Admin
26 *
27 * @since 1.0.0
28 */
29 private static $instance;
30
31 /**
32 * Admin Instance.
33 *
34 * Instantiates or reuses an instance of Admin.
35 *
36 * @since 1.0.0
37 * @static
38 *
39 * @see Admin()
40 *
41 * @return self - Single instance
42 */
43 public static function instance() {
44 if ( ! self::$instance ) {
45 self::$instance = new self();
46 }
47 return self::$instance;
48 }
49
50 /**
51 * Constructor.
52 * Since this is a singleton class, it is better to have its constructor as a private.
53 *
54 * @since 1.0.0
55 */
56 private function __construct() {
57 $this->admin_hooks();
58 Capabilities::assign_capabilities_for_user_roles();
59 }
60
61 /**
62 * Admin Hooks.
63 *
64 * @since 1.0.0
65 */
66 public function admin_hooks() {
67 add_action( 'admin_menu', array( $this, 'create_admin_menu_pages' ) );
68 // admin_footer, not admin_init: the flag is read while localizing the
69 // client script, so clearing it earlier would hide the badge on the
70 // very visit that is supposed to show it.
71 add_action( 'admin_footer', array( $this, 'maybe_mark_mcp_page_seen' ) );
72 add_action( 'wp_ajax_quillforms_duplicate_form', array( $this, 'duplicate_form' ) );
73 add_action( 'wp_ajax_quillforms_install_doublescale', array( $this, 'install_doublescale' ) );
74 add_action( 'wp_ajax_quillforms_activate_doublescale', array( $this, 'activate_doublescale' ) );
75 add_action( 'pre_get_posts', array( $this, 'include_quill_forms_post_type_in_query' ), 11 );
76 add_filter( 'post_type_link', array( $this, 'remove_cpt_slug' ), 10, 3 );
77 add_filter('rewrite_rules_array', array($this, 'rewrite_quillforms_rules') );
78
79 }
80
81 /**
82 * Drop the "New" badge once the MCP page has been opened.
83 *
84 * Runs late in the request so the badge survives the visit that reveals it,
85 * and is gone from the next page load onwards.
86 *
87 * @since 5.8.0
88 *
89 * @return void
90 */
91 public function maybe_mark_mcp_page_seen() {
92 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only check of the current screen, no state is changed on behalf of the user.
93 if ( ! isset( $_GET['page'], $_GET['path'] ) || 'quillforms' !== $_GET['page'] || 'mcp' !== $_GET['path'] ) {
94 return;
95 }
96
97 if ( ! current_user_can( apply_filters( 'quillforms_admin_capability_filter', 'manage_quillforms' ) ) ) {
98 return;
99 }
100
101 if ( ! get_option( 'quillforms_mcp_page_seen' ) ) {
102 update_option( 'quillforms_mcp_page_seen', 1 );
103 }
104 }
105
106 /**
107 * Duplicate form
108 *
109 * @since 1.7.4
110 *
111 * @return integer|WP_Error|false
112 */
113 public function duplicate_form() {
114 // check for valid nonce field.
115 if ( ! check_ajax_referer( 'quillforms_duplicate', '_nonce', false ) ) {
116 wp_send_json_error( esc_html__( 'Invalid nonce', 'quillforms' ), 403 );
117 exit;
118 }
119
120 if ( ! current_user_can( 'manage_quillforms' ) ) {
121 wp_send_json_error( 'Unauthorized' );
122 return;
123 }
124 $form_id = (int) $_POST['form_id'];
125 $form = get_post( $form_id );
126 if ( ! $form ) {
127 return false;
128 }
129
130 $new_form_id = wp_insert_post(
131 array(
132 'post_title' => $form->post_title . ' copy',
133 'post_status' => 'draft',
134 'post_type' => $form->post_type,
135 'post_author' => $form->post_author,
136 )
137 );
138 if ( is_wp_error( $new_form_id ) ) {
139 return $new_form_id;
140 }
141 $form_meta = get_post_meta( $form_id );
142 foreach ( $form_meta as $key => $values ) {
143 foreach ( $values as $value ) {
144 add_post_meta( $new_form_id, $key, maybe_unserialize( $value ) );
145 }
146 }
147
148 wp_send_json_success( $new_form_id );
149 }
150
151 /**
152 * Install DoubleScale plugin (our partner CRM/mailer) from WordPress.org
153 *
154 * @since 3.5.0
155 */
156 public function install_doublescale() {
157 // Check nonce.
158 if ( ! check_ajax_referer( 'quillforms_install_doublescale', '_nonce', false ) ) {
159 wp_send_json_error( esc_html__( 'Invalid nonce', 'quillforms' ), 403 );
160 exit;
161 }
162
163 // Check permissions.
164 if ( ! current_user_can( 'install_plugins' ) ) {
165 wp_send_json_error( esc_html__( 'You do not have permission to install plugins.', 'quillforms' ), 403 );
166 exit;
167 }
168
169 // Include required files.
170 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
171 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
172 require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
173 require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
174
175 // Get plugin info from WordPress.org.
176 $api = plugins_api(
177 'plugin_information',
178 array(
179 'slug' => 'doublescale',
180 'fields' => array(
181 'short_description' => false,
182 'sections' => false,
183 'requires' => false,
184 'rating' => false,
185 'ratings' => false,
186 'downloaded' => false,
187 'last_updated' => false,
188 'added' => false,
189 'tags' => false,
190 'compatibility' => false,
191 'homepage' => false,
192 'donate_link' => false,
193 ),
194 )
195 );
196
197 if ( is_wp_error( $api ) ) {
198 wp_send_json_error( $api->get_error_message() );
199 exit;
200 }
201
202 // Install the plugin.
203 $skin = new \WP_Ajax_Upgrader_Skin();
204 $upgrader = new \Plugin_Upgrader( $skin );
205 $result = $upgrader->install( $api->download_link );
206
207 if ( is_wp_error( $result ) ) {
208 wp_send_json_error( $result->get_error_message() );
209 exit;
210 }
211
212 if ( is_wp_error( $skin->result ) ) {
213 wp_send_json_error( $skin->result->get_error_message() );
214 exit;
215 }
216
217 if ( $skin->get_errors()->has_errors() ) {
218 wp_send_json_error( $skin->get_error_messages() );
219 exit;
220 }
221
222 if ( is_null( $result ) ) {
223 wp_send_json_error( esc_html__( 'Unable to connect to the filesystem. Please confirm your credentials.', 'quillforms' ) );
224 exit;
225 }
226
227 wp_send_json_success( esc_html__( 'DoubleScale installed successfully.', 'quillforms' ) );
228 }
229
230 /**
231 * Activate DoubleScale plugin (our partner CRM/mailer)
232 *
233 * @since 3.5.0
234 */
235 public function activate_doublescale() {
236 // Check nonce.
237 if ( ! check_ajax_referer( 'quillforms_activate_doublescale', '_nonce', false ) ) {
238 wp_send_json_error( esc_html__( 'Invalid nonce', 'quillforms' ), 403 );
239 exit;
240 }
241
242 // Check permissions.
243 if ( ! current_user_can( 'activate_plugins' ) ) {
244 wp_send_json_error( esc_html__( 'You do not have permission to activate plugins.', 'quillforms' ), 403 );
245 exit;
246 }
247
248 $plugin_file = 'doublescale/doublescale.php';
249
250 // Check if plugin exists.
251 if ( ! file_exists( WP_PLUGIN_DIR . '/' . $plugin_file ) ) {
252 wp_send_json_error( esc_html__( 'DoubleScale plugin not found.', 'quillforms' ) );
253 exit;
254 }
255
256 // Activate the plugin.
257 $result = activate_plugin( $plugin_file );
258
259 if ( is_wp_error( $result ) ) {
260 wp_send_json_error( $result->get_error_message() );
261 exit;
262 }
263
264 wp_send_json_success( esc_html__( 'DoubleScale activated successfully.', 'quillforms' ) );
265 }
266
267 /**
268 * Create admin menu pages
269 *
270 * @since 1.0.0
271 */
272 public function create_admin_menu_pages() {
273 add_menu_page(
274 __( 'Quill Forms', 'quillforms' ),
275 __( 'Quill Forms', 'quillforms' ),
276 apply_filters('quillforms_admin_capability_filter', 'manage_quillforms'),
277 'quillforms',
278 array( Admin_Loader::class, 'page_wrapper' ),
279 'data:image/svg+xml;base64,' . base64_encode(
280 '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 5219 5951" width="5519x" height="5519px">
281 <style>
282 tspan { white-space:pre }
283 .shp0 { fill: none }
284 .shp1 { fill: #a9abae; fill-rule:nonzero }
285 </style>
286 <g id="Layer_x0020_1">
287 <g id="_2145120152032">
288 <path id="Layer" class="shp0" d="M-708 -570L6202 -570L6202 6340L-708 6340L-708 -570Z" />
289 <path id="Layer" fill-rule="evenodd" class="shp1" d="M5389 6034C5082 5453 4522 4864 3980 4864C2595 4864 1968 4388 1968 4388C2880 4601 3117 4249 3117 4249C1133 4435 1031 3090 1031 3090C1223 3566 1697 3712 1697 3712C680 2439 1516 1417 1516 1417C1114 2717 2010 3609 3233 3947C4604 4359 5163 5282 5389 6034ZM2162 91C2326 58 2496 40 2670 40C4086 40 5234 1188 5234 2604C5234 3047 5135 3451 4903 3885C4994 4133 5077 4398 5128 4724C5128 4724 4859 4033 4496 3694C4657 3406 4750 3074 4750 2721C4750 1618 3856 724 2753 724C2362 724 1996 839 1686 1035C1123 1392 748 2019 755 2721C770 4038 1718 4582 2182 4791C2463 4911 2836 5035 3456 5045C3208 5125 2944 5169 2670 5169C1254 5169 106 4021 106 2604C106 1362 989 326 2162 91ZM3346 2698C3346 2698 3457 3145 4367 3905C4974 4412 5267 5449 5267 5449C4515 4025 3802 4052 2630 3530C1168 2878 1628 1274 1628 1274C1628 1274 1814 2597 2479 2942C2479 2942 2352 2497 2491 2056C2491 2056 2499 2895 3450 3452C3450 3452 3195 3042 3346 2698Z" />
290 </g>
291 </g>
292 </svg>'
293 ),
294 30
295 );
296 // Add main page as a submenu page.
297 add_submenu_page( 'quillforms', __( 'Quill Forms', 'quillforms' ), __( 'All Forms', 'quillforms' ), apply_filters('quillforms_admin_capability_filter', 'manage_quillforms'), 'quillforms', array( Admin_Loader::class, 'page_wrapper' ) );
298
299 // Add addons page as a submenu page.
300 add_submenu_page( 'quillforms', __( 'Addons', 'quillforms' ), __( 'Addons', 'quillforms' ), apply_filters('quillforms_admin_capability_filter', 'manage_quillforms'), 'quillforms&path=addons', array( Admin_Loader::class, 'page_wrapper' ) );
301
302 // Add settings page as a submenu page.
303 add_submenu_page( 'quillforms', __( 'Settings', 'quillforms' ), __( 'Settings', 'quillforms' ), apply_filters('quillforms_admin_capability_filter', 'manage_quillforms'), 'quillforms&path=settings', array( Admin_Loader::class, 'page_wrapper' ) );
304
305 // Add MCP server page as a submenu page.
306 add_submenu_page( 'quillforms', __( 'MCP Server', 'quillforms' ), __( 'MCP Server', 'quillforms' ), apply_filters('quillforms_admin_capability_filter', 'manage_quillforms'), 'quillforms&path=mcp', array( Admin_Loader::class, 'page_wrapper' ) );
307
308 // Add license page as a submenu page.
309 add_submenu_page( 'quillforms', __( 'License', 'quillforms' ), __( 'License', 'quillforms' ), apply_filters('quillforms_admin_capability_filter', 'manage_quillforms'), 'quillforms&path=license', array( Admin_Loader::class, 'page_wrapper' ) );
310
311 // Add system page as a submenu page.
312 add_submenu_page( 'quillforms', __( 'System', 'quillforms' ), __( 'System', 'quillforms' ), apply_filters('quillforms_admin_capability_filter', 'manage_quillforms'), 'quillforms&path=system', array( Admin_Loader::class, 'page_wrapper' ) );
313
314 // Add import/export page as a submenu page.
315 add_submenu_page( 'quillforms', __( 'Import/Export', 'quillforms' ), __( 'Import/Export', 'quillforms' ), 'manage_quillforms', 'quillforms&path=import-export', array( Admin_Loader::class, 'page_wrapper' ) );
316
317 // Add support page as a submenu page.
318 add_submenu_page( 'quillforms', __( 'Support', 'quillforms' ), __( 'Support', 'quillforms' ), apply_filters('quillforms_admin_capability_filter', 'manage_quillforms'), 'quillforms&path=support', array( Admin_Loader::class, 'page_wrapper' ) );
319 }
320
321 /**
322 * Include quill_forms in post type query if the slug is empty.
323 *
324 * @since 1.17.1
325 *
326 * @param object $query Query object.
327 * @return void
328 */
329 public function include_quill_forms_post_type_in_query( $query ) {
330 if ( Settings::get( 'override_quillforms_slug' ) && empty( Settings::get( 'quillforms_slug' ) ) ) {
331
332 // Only noop the main query.
333 if ( ! $query->is_main_query() ) {
334 return;
335 }
336
337 // Only noop our very specific rewrite rule match.
338 if (
339 2 !== count( $query->query )
340 || ! isset( $query->query['page'] )
341 ) {
342 return;
343 }
344
345 // Include my post type in the query.
346 if ( ! empty( $query->query['name'] ) ) {
347 $query->set( 'post_type', array( 'post', 'page', 'quill_forms' ) );
348 }
349 }
350
351 }
352
353 /**
354 * Remove CPT Slug
355 *
356 * @since 1.25.0
357 */
358 public function remove_cpt_slug( $post_link, $post, $leavename ) {
359
360 if ( 'quill_forms' !== $post->post_type || 'publish' !== $post->post_status || ! Settings::get( 'override_quillforms_slug' ) || ! empty( Settings::get( 'quillforms_slug' ) ) ) {
361
362 return $post_link;
363 }
364
365 $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
366
367 return $post_link;
368 }
369
370 /**
371 * Rewrite QuillForms Rules
372 *
373 * @since 3.4.0
374 */
375 public function rewrite_quillforms_rules( $rules ) {
376 $override_slug= Settings::get("override_quillforms_slug");
377 $new_slug = Settings::get("quillforms_slug");
378 //
379 if($override_slug && empty($new_slug)) {
380 // in this case the url shouldn't contain quillforms slug
381 $rules = array_merge(
382 ['[^/]+/quillforms/([^/]+)/?$' => 'index.php?quill_forms=$matches[1]'],
383 $rules
384 );
385 }
386
387 return $rules;
388 }
389
390 }
391