PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.6
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.6
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / controllers / FrmApplicationsController.php

FrmApplicationsController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.6, at classes/controllers/FrmApplicationsController.php

214 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 /**
7 * @since 5.3
8 */
9 class FrmApplicationsController {
10
11 /**
12 * Add Applications menu item to sidebar and define Applications index page.
13 *
14 * @return void
15 */
16 public static function menu() {
17 $label = __( 'Applications', 'formidable' );
18 $cap = self::get_required_capability();
19
20 if ( ! current_user_can( $cap ) && is_callable( 'FrmProApplicationsHelper::get_custom_applications_capability' ) ) {
21 $custom_applications_cap = FrmProApplicationsHelper::get_custom_applications_capability();
22 if ( current_user_can( $custom_applications_cap ) ) {
23 $cap = $custom_applications_cap;
24 $slug = 'edit-tags.php?taxonomy=frm_application';
25 $callback = '';
26 }
27 }
28
29 if ( ! isset( $slug ) ) {
30 $slug = 'formidable-applications';
31 $callback = array( __CLASS__, 'landing_page' );
32 }
33
34 add_submenu_page( 'formidable', 'Formidable | ' . $label, $label, $cap, $slug, $callback );
35 }
36
37 /**
38 * Get the required capability for accessing the Applications dashboard.
39 *
40 * @since 5.3.1
41 *
42 * @return string
43 */
44 private static function get_required_capability() {
45 if ( is_callable( 'FrmProApplicationsHelper::get_required_templates_capability' ) ) {
46 return FrmProApplicationsHelper::get_required_templates_capability();
47 }
48 return 'frm_edit_forms';
49 }
50
51 /**
52 * Render Applications index page.
53 *
54 * @return void
55 */
56 public static function landing_page() {
57 require self::get_view_path() . 'index.php';
58 }
59
60 /**
61 * Get path to application views.
62 *
63 * @return string
64 */
65 private static function get_view_path() {
66 return FrmAppHelper::plugin_path() . '/classes/views/applications/';
67 }
68
69 /**
70 * Get information about applications via AJAX action.
71 *
72 * @return void
73 */
74 public static function get_applications_data() {
75 FrmAppHelper::permission_check( 'frm_view_forms' );
76 check_ajax_referer( 'frm_ajax', 'nonce' );
77
78 $view = FrmAppHelper::get_param( 'view', '', 'get', 'sanitize_text_field' );
79 $data = array();
80
81 if ( 'applications' !== $view ) {
82 FrmAppHelper::permission_check( self::get_required_capability() );
83
84 // view may be 'applications', 'templates', or empty.
85 $data['templates'] = self::get_prepared_template_data();
86 $data['categories'] = FrmApplicationTemplate::get_categories();
87 } else {
88 FrmAppHelper::permission_check( 'frm_edit_applications' );
89 }
90
91 /**
92 * @param array $data
93 */
94 $data = apply_filters( 'frm_applications_data', $data );
95
96 wp_send_json_success( $data );
97 }
98
99 /**
100 * Retrieve API data and return a reduced set back with some restructuring applied to make it easier to read.
101 *
102 * @return array<array>
103 */
104 private static function get_prepared_template_data() {
105 $api = new FrmApplicationApi();
106 $applications = $api->get_api_info();
107 $applications = array_filter( $applications, 'is_array' );
108
109 $unlocked_templates = array();
110 $locked_templates = array();
111 foreach ( $applications as $key => $application ) {
112 if ( ! is_numeric( $key ) ) {
113 // Skip "error" or any other non-numeric key.
114 continue;
115 }
116
117 if ( ! empty( $application['url'] ) ) {
118 $unlocked_templates[] = $application;
119 } else {
120 $locked_templates[] = $application;
121 }
122 }
123
124 $unlocked_templates = self::sort_templates( $unlocked_templates );
125
126 $applications = $unlocked_templates;
127 if ( current_user_can( 'administrator' ) || current_user_can( 'frm_change_settings' ) ) {
128 $locked_templates = self::sort_templates( $locked_templates );
129 $applications = array_merge( $applications, $locked_templates );
130 }
131
132 FrmApplicationTemplate::init();
133
134 return array_reduce( $applications, array( __CLASS__, 'reduce_template' ), array() );
135 }
136
137 /**
138 * @param array $total the accumulated array of reduced application data.
139 * @param array $current data for the current template from the API.
140 * @return array<array>
141 */
142 private static function reduce_template( $total, $current ) {
143 $template = new FrmApplicationTemplate( $current );
144 $total[] = $template->as_js_object();
145 return $total;
146 }
147
148 /**
149 * Sort applications alphabetically.
150 *
151 * @param array<array> $applications
152 * @return array<array>
153 */
154 private static function sort_templates( $applications ) {
155 usort(
156 $applications,
157 function( $a, $b ) {
158 return strcmp( $a['name'], $b['name'] );
159 }
160 );
161 return $applications;
162 }
163
164 /**
165 * @return void
166 */
167 public static function load_assets() {
168 $plugin_url = FrmAppHelper::plugin_url();
169 $version = FrmAppHelper::plugin_version();
170
171 wp_enqueue_style( 'formidable-admin' );
172 wp_enqueue_style( 'formidable-grids' );
173
174 $js_dependencies = array(
175 'wp-i18n',
176 'wp-hooks', // This prevents a console error "wp.hooks is undefined" in WP versions older than 5.7.
177 'formidable_dom',
178 );
179 wp_register_script( 'formidable_applications', $plugin_url . '/js/admin/applications.js', $js_dependencies, $version, true );
180 wp_register_style( 'formidable_applications', $plugin_url . '/css/admin/applications.css', array(), $version );
181
182 $js_vars = array(
183 'proUpgradeUrl' => FrmAppHelper::admin_upgrade_link( 'applications' ),
184 );
185 wp_localize_script( 'formidable_applications', 'frmApplicationsVars', $js_vars );
186
187 wp_enqueue_script( 'formidable_applications' );
188 wp_enqueue_style( 'formidable_applications' );
189
190 do_action( 'frm_applications_assets' );
191 }
192
193 /**
194 * @return void
195 */
196 public static function dequeue_scripts() {
197 if ( 'formidable-applications' === FrmAppHelper::simple_get( 'page', 'sanitize_title' ) ) {
198 // Avoid extra scripts loading on applications index that aren't needed.
199 wp_dequeue_script( 'frm-surveys-admin' );
200 wp_dequeue_script( 'frm-quizzes-form-action' );
201 }
202 }
203
204 /**
205 * @param string $title
206 * @param string $context values include 'index', 'list', and 'edit'.
207 * @return void
208 */
209 public static function render_applications_header( $title, $context ) {
210 FrmAppHelper::print_admin_banner( true );
211 require self::get_view_path() . 'header.php';
212 }
213 }
214