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

211 lines 5.8 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 $view = FrmAppHelper::get_param( 'view', '', 'get', 'sanitize_text_field' );
76 $data = array();
77
78 if ( 'applications' !== $view ) {
79 FrmAppHelper::permission_check( self::get_required_capability() );
80
81 // view may be 'applications', 'templates', or empty.
82 $data['templates'] = self::get_prepared_template_data();
83 $data['categories'] = FrmApplicationTemplate::get_categories();
84 } else {
85 FrmAppHelper::permission_check( 'frm_edit_applications' );
86 }
87
88 /**
89 * @param array $data
90 */
91 $data = apply_filters( 'frm_applications_data', $data );
92
93 wp_send_json_success( $data );
94 }
95
96 /**
97 * Retrieve API data and return a reduced set back with some restructuring applied to make it easier to read.
98 *
99 * @return array<array>
100 */
101 private static function get_prepared_template_data() {
102 $api = new FrmApplicationApi();
103 $applications = $api->get_api_info();
104 $applications = array_filter( $applications, 'is_array' );
105
106 $unlocked_templates = array();
107 $locked_templates = array();
108 foreach ( $applications as $key => $application ) {
109 if ( ! is_numeric( $key ) ) {
110 // Skip "error" or any other non-numeric key.
111 continue;
112 }
113
114 if ( ! empty( $application['url'] ) ) {
115 $unlocked_templates[] = $application;
116 } else {
117 $locked_templates[] = $application;
118 }
119 }
120
121 $unlocked_templates = self::sort_templates( $unlocked_templates );
122
123 $applications = $unlocked_templates;
124 if ( current_user_can( 'administrator' ) || current_user_can( 'frm_change_settings' ) ) {
125 $locked_templates = self::sort_templates( $locked_templates );
126 $applications = array_merge( $applications, $locked_templates );
127 }
128
129 FrmApplicationTemplate::init();
130
131 return array_reduce( $applications, array( __CLASS__, 'reduce_template' ), array() );
132 }
133
134 /**
135 * @param array $total the accumulated array of reduced application data.
136 * @param array $current data for the current template from the API.
137 * @return array<array>
138 */
139 private static function reduce_template( $total, $current ) {
140 $template = new FrmApplicationTemplate( $current );
141 $total[] = $template->as_js_object();
142 return $total;
143 }
144
145 /**
146 * Sort applications alphabetically.
147 *
148 * @param array<array> $applications
149 * @return array<array>
150 */
151 private static function sort_templates( $applications ) {
152 usort(
153 $applications,
154 function( $a, $b ) {
155 return strcmp( $a['name'], $b['name'] );
156 }
157 );
158 return $applications;
159 }
160
161 /**
162 * @return void
163 */
164 public static function load_assets() {
165 $plugin_url = FrmAppHelper::plugin_url();
166 $version = FrmAppHelper::plugin_version();
167
168 wp_enqueue_style( 'formidable-admin' );
169 wp_enqueue_style( 'formidable-grids' );
170
171 $js_dependencies = array(
172 'wp-i18n',
173 'wp-hooks', // This prevents a console error "wp.hooks is undefined" in WP versions older than 5.7.
174 'formidable_dom',
175 );
176 wp_register_script( 'formidable_applications', $plugin_url . '/js/admin/applications.js', $js_dependencies, $version, true );
177 wp_register_style( 'formidable_applications', $plugin_url . '/css/admin/applications.css', array(), $version );
178
179 $js_vars = array(
180 'proUpgradeUrl' => FrmAppHelper::admin_upgrade_link( 'applications' ),
181 );
182 wp_localize_script( 'formidable_applications', 'frmApplicationsVars', $js_vars );
183
184 wp_enqueue_script( 'formidable_applications' );
185 wp_enqueue_style( 'formidable_applications' );
186
187 do_action( 'frm_applications_assets' );
188 }
189
190 /**
191 * @return void
192 */
193 public static function dequeue_scripts() {
194 if ( 'formidable-applications' === FrmAppHelper::simple_get( 'page', 'sanitize_title' ) ) {
195 // Avoid extra scripts loading on applications index that aren't needed.
196 wp_dequeue_script( 'frm-surveys-admin' );
197 wp_dequeue_script( 'frm-quizzes-form-action' );
198 }
199 }
200
201 /**
202 * @param string $title
203 * @param string $context values include 'index', 'list', and 'edit'.
204 * @return void
205 */
206 public static function render_applications_header( $title, $context ) {
207 FrmAppHelper::print_admin_banner( true );
208 require self::get_view_path() . 'header.php';
209 }
210 }
211