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

230 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 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
145 $js_object = $template->as_js_object();
146 if ( $js_object ) {
147 $total[] = $js_object;
148 }
149
150 return $total;
151 }
152
153 /**
154 * Sort applications alphabetically.
155 *
156 * @param array<array> $applications
157 * @return array<array>
158 */
159 private static function sort_templates( $applications ) {
160 usort(
161 $applications,
162 function ( $a, $b ) {
163 return strcmp( $a['name'], $b['name'] );
164 }
165 );
166 return $applications;
167 }
168
169 /**
170 * @usedby FrmAppController::admin_init().
171 *
172 * @since 6.8
173 *
174 * @return void
175 */
176 public static function load_page() {
177 self::load_assets();
178 }
179
180 /**
181 * @return void
182 */
183 public static function load_assets() {
184 $plugin_url = FrmAppHelper::plugin_url();
185 $version = FrmAppHelper::plugin_version();
186
187 wp_enqueue_style( 'formidable-admin' );
188 wp_enqueue_style( 'formidable-grids' );
189
190 $js_dependencies = array(
191 'wp-i18n',
192 // This prevents a console error "wp.hooks is undefined" in WP versions older than 5.7.
193 'wp-hooks',
194 'formidable_dom',
195 );
196 wp_register_script( 'formidable_applications', $plugin_url . '/js/admin/applications.js', $js_dependencies, $version, true );
197 wp_register_style( 'formidable_applications', $plugin_url . '/css/admin/applications.css', array(), $version );
198
199 $js_vars = array(
200 'proUpgradeUrl' => FrmAppHelper::admin_upgrade_link( 'applications' ),
201 );
202 wp_localize_script( 'formidable_applications', 'frmApplicationsVars', $js_vars );
203
204 wp_enqueue_script( 'formidable_applications' );
205 wp_set_script_translations( 'formidable_applications', 'formidable' );
206 wp_enqueue_style( 'formidable_applications' );
207
208 do_action( 'frm_applications_assets' );
209 }
210
211 /**
212 * @return void
213 */
214 public static function dequeue_scripts() {
215 if ( 'formidable-applications' === FrmAppHelper::simple_get( 'page', 'sanitize_title' ) ) {
216 FrmAppHelper::dequeue_extra_global_scripts();
217 }
218 }
219
220 /**
221 * @param string $title
222 * @param string $context values include 'index', 'list', and 'edit'.
223 * @return void
224 */
225 public static function render_applications_header( $title, $context ) {
226 FrmAppHelper::print_admin_banner( true );
227 require self::get_view_path() . 'header.php';
228 }
229 }
230