PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.26.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.26.1
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.26.1, at classes/controllers/FrmApplicationsController.php

237 lines 6.1 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
23 if ( current_user_can( $custom_applications_cap ) ) {
24 $cap = $custom_applications_cap;
25 $slug = 'edit-tags.php?taxonomy=frm_application';
26 $callback = '';
27 }
28 }
29
30 if ( ! isset( $slug ) ) {
31 $slug = 'formidable-applications';
32 $callback = array( self::class, 'landing_page' );
33 }
34
35 add_submenu_page( 'formidable', 'Formidable | ' . $label, $label, $cap, $slug, $callback );
36 }
37
38 /**
39 * Get the required capability for accessing the Applications dashboard.
40 *
41 * @since 5.3.1
42 *
43 * @return string
44 */
45 private static function get_required_capability() {
46 if ( is_callable( 'FrmProApplicationsHelper::get_required_templates_capability' ) ) {
47 return FrmProApplicationsHelper::get_required_templates_capability();
48 }
49 return 'frm_edit_forms';
50 }
51
52 /**
53 * Render Applications index page.
54 *
55 * @return void
56 */
57 public static function landing_page() {
58 require self::get_view_path() . 'index.php';
59 }
60
61 /**
62 * Get path to application views.
63 *
64 * @return string
65 */
66 private static function get_view_path() {
67 return FrmAppHelper::plugin_path() . '/classes/views/applications/';
68 }
69
70 /**
71 * Get information about applications via AJAX action.
72 *
73 * @return void
74 */
75 public static function get_applications_data() {
76 FrmAppHelper::permission_check( 'frm_view_forms' );
77 check_ajax_referer( 'frm_ajax', 'nonce' );
78
79 $view = FrmAppHelper::get_param( 'view', '', 'get', 'sanitize_text_field' );
80 $data = array();
81
82 if ( 'applications' !== $view ) {
83 FrmAppHelper::permission_check( self::get_required_capability() );
84
85 // view may be 'applications', 'templates', or empty.
86 $data['templates'] = self::get_prepared_template_data();
87 $data['categories'] = FrmApplicationTemplate::get_categories();
88 } else {
89 FrmAppHelper::permission_check( 'frm_edit_applications' );
90 }
91
92 /**
93 * @param array $data
94 */
95 $data = apply_filters( 'frm_applications_data', $data );
96
97 wp_send_json_success( $data );
98 }
99
100 /**
101 * Retrieve API data and return a reduced set back with some restructuring applied to make it easier to read.
102 *
103 * @return array<array>
104 */
105 private static function get_prepared_template_data() {
106 $api = new FrmApplicationApi();
107 $applications = $api->get_api_info();
108 $applications = array_filter( $applications, 'is_array' );
109
110 $unlocked_templates = array();
111 $locked_templates = array();
112
113 foreach ( $applications as $key => $application ) {
114 if ( ! is_numeric( $key ) ) {
115 // Skip "error" or any other non-numeric key.
116 continue;
117 }
118
119 if ( ! empty( $application['url'] ) ) {
120 $unlocked_templates[] = $application;
121 } else {
122 $locked_templates[] = $application;
123 }
124 }
125
126 $unlocked_templates = self::sort_templates( $unlocked_templates );
127
128 $applications = $unlocked_templates;
129
130 if ( current_user_can( 'administrator' ) || current_user_can( 'frm_change_settings' ) ) {
131 $locked_templates = self::sort_templates( $locked_templates );
132 $applications = array_merge( $applications, $locked_templates );
133 }
134
135 FrmApplicationTemplate::init();
136
137 return array_reduce( $applications, array( self::class, 'reduce_template' ), array() );
138 }
139
140 /**
141 * @param array $total the accumulated array of reduced application data.
142 * @param array $current data for the current template from the API.
143 *
144 * @return array<array>
145 */
146 private static function reduce_template( $total, $current ) {
147 $template = new FrmApplicationTemplate( $current );
148
149 $js_object = $template->as_js_object();
150
151 if ( $js_object ) {
152 $total[] = $js_object;
153 }
154
155 return $total;
156 }
157
158 /**
159 * Sort applications alphabetically.
160 *
161 * @param array<array> $applications
162 *
163 * @return array<array>
164 */
165 private static function sort_templates( $applications ) {
166 usort(
167 $applications,
168 function ( $a, $b ) {
169 return strcmp( $a['name'], $b['name'] );
170 }
171 );
172 return $applications;
173 }
174
175 /**
176 * @usedby FrmAppController::admin_init().
177 *
178 * @since 6.8
179 *
180 * @return void
181 */
182 public static function load_page() {
183 self::load_assets();
184 }
185
186 /**
187 * @return void
188 */
189 public static function load_assets() {
190 $plugin_url = FrmAppHelper::plugin_url();
191 $version = FrmAppHelper::plugin_version();
192
193 wp_enqueue_style( 'formidable-admin' );
194 wp_enqueue_style( 'formidable-grids' );
195
196 $js_dependencies = array(
197 'wp-i18n',
198 // This prevents a console error "wp.hooks is undefined" in WP versions older than 5.7.
199 'wp-hooks',
200 'formidable_dom',
201 );
202 wp_register_script( 'formidable_applications', $plugin_url . '/js/admin/applications.js', $js_dependencies, $version, true );
203 wp_register_style( 'formidable_applications', $plugin_url . '/css/admin/applications.css', array(), $version );
204
205 $js_vars = array(
206 'proUpgradeUrl' => FrmAppHelper::admin_upgrade_link( 'applications' ),
207 );
208 wp_localize_script( 'formidable_applications', 'frmApplicationsVars', $js_vars );
209
210 wp_enqueue_script( 'formidable_applications' );
211 wp_set_script_translations( 'formidable_applications', 'formidable' );
212 wp_enqueue_style( 'formidable_applications' );
213
214 do_action( 'frm_applications_assets' );
215 }
216
217 /**
218 * @return void
219 */
220 public static function dequeue_scripts() {
221 if ( 'formidable-applications' === FrmAppHelper::simple_get( 'page', 'sanitize_title' ) ) {
222 FrmAppHelper::dequeue_extra_global_scripts();
223 }
224 }
225
226 /**
227 * @param string $title
228 * @param string $context values include 'index', 'list', and 'edit'.
229 *
230 * @return void
231 */
232 public static function render_applications_header( $title, $context ) {
233 FrmAppHelper::print_admin_banner( true );
234 require self::get_view_path() . 'header.php';
235 }
236 }
237