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

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