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

205 lines 5.6 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 $application ) {
110 if ( ! empty( $application['url'] ) ) {
111 $unlocked_templates[] = $application;
112 } else {
113 $locked_templates[] = $application;
114 }
115 }
116
117 $unlocked_templates = self::sort_templates( $unlocked_templates );
118
119 $applications = $unlocked_templates;
120 if ( current_user_can( 'administrator' ) || current_user_can( 'frm_change_settings' ) ) {
121 $locked_templates = self::sort_templates( $locked_templates );
122 $applications = array_merge( $applications, $locked_templates );
123 }
124
125 FrmApplicationTemplate::init();
126
127 return array_reduce( $applications, array( __CLASS__, 'reduce_template' ), array() );
128 }
129
130 /**
131 * @param array $total the accumulated array of reduced application data.
132 * @param array $current data for the current template from the API.
133 * @return array<array>
134 */
135 private static function reduce_template( $total, $current ) {
136 $template = new FrmApplicationTemplate( $current );
137 $total[] = $template->as_js_object();
138 return $total;
139 }
140
141 /**
142 * Sort applications alphabetically.
143 *
144 * @param array<array> $applications
145 * @return array<array>
146 */
147 private static function sort_templates( $applications ) {
148 usort(
149 $applications,
150 function( $a, $b ) {
151 return strcmp( $a['name'], $b['name'] );
152 }
153 );
154 return $applications;
155 }
156
157 /**
158 * @return void
159 */
160 public static function load_assets() {
161 $plugin_url = FrmAppHelper::plugin_url();
162 $version = FrmAppHelper::plugin_version();
163
164 wp_enqueue_style( 'formidable-admin' );
165 wp_enqueue_style( 'formidable-grids' );
166
167 $js_dependencies = array(
168 'wp-i18n',
169 'formidable_dom',
170 );
171 wp_register_script( 'formidable_applications', $plugin_url . '/js/admin/applications.js', $js_dependencies, $version, true );
172 wp_register_style( 'formidable_applications', $plugin_url . '/css/admin/applications.css', array(), $version );
173
174 $js_vars = array(
175 'proUpgradeUrl' => FrmAppHelper::admin_upgrade_link( 'applications' ),
176 );
177 wp_localize_script( 'formidable_applications', 'frmApplicationsVars', $js_vars );
178
179 wp_enqueue_script( 'formidable_applications' );
180 wp_enqueue_style( 'formidable_applications' );
181
182 do_action( 'frm_applications_assets' );
183 }
184
185 /**
186 * @return void
187 */
188 public static function dequeue_scripts() {
189 if ( 'formidable-applications' === FrmAppHelper::simple_get( 'page', 'sanitize_title' ) ) {
190 // Avoid extra scripts loading on applications index that aren't needed.
191 wp_dequeue_script( 'frm-surveys-admin' );
192 wp_dequeue_script( 'frm-quizzes-form-action' );
193 }
194 }
195
196 /**
197 * @param string $title
198 * @param string $context values include 'index', 'list', and 'edit'.
199 * @return void
200 */
201 public static function render_applications_header( $title, $context ) {
202 require self::get_view_path() . 'header.php';
203 }
204 }
205