Repository navigation
Add an add-on activation step before importing templates #3657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
dcf752a
f07062e
425e011
824e015
62107b5
b905378
f8fade7
fd5c139
d6faa0c
6cf0464
23b4698
da1bdd1
046e743
cd1bad5
f77170d
592ce95
83d6f88
9a1e7fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| <?php | ||
|
|
||
| if ( ! defined( 'ABSPATH' ) ) { | ||
| die( 'You are not allowed to call this page directly.' ); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve template requirements before importing any forms. | ||
| * | ||
| * @since x.x | ||
| */ | ||
| class FrmTemplateDependenciesHelper { | ||
|
|
||
| /** | ||
| * Get missing dependencies for a validated template download URL. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param string $url The template XML download URL. | ||
| * | ||
| * @return array[] Missing dependencies and their available actions. | ||
| */ | ||
| public static function get_missing_addons( $url ) { | ||
| $api = new FrmFormTemplateApi(); | ||
| $templates = $api->get_api_info(); | ||
| $path = wp_parse_url( $url, PHP_URL_PATH ); | ||
| $required = array(); | ||
|
|
||
| foreach ( $templates as $template ) { | ||
| if ( ! is_array( $template ) || ! is_string( $template['url'] ?? null ) || $path !== wp_parse_url( $template['url'], PHP_URL_PATH ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| $required = self::get_required_addons( $template ); | ||
| break; | ||
| } | ||
|
|
||
| if ( ! $required ) { | ||
| return array(); | ||
|
Comment on lines
+38
to
+39
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| $api = new FrmFormApi(); | ||
| $addons = array_filter( | ||
| $api->get_api_info(), | ||
| function ( $addon ) { | ||
| return is_array( $addon ) && is_string( $addon['plugin'] ?? null ) && '' !== $addon['plugin']; | ||
| } | ||
| ); | ||
|
|
||
| $plugins = FrmAppHelper::get_plugins(); | ||
| $missing = array(); | ||
|
|
||
| foreach ( $required as $requirement ) { | ||
| $addon = self::find_addon( $requirement, $addons ); | ||
|
|
||
| if ( ! $addon ) { | ||
| continue; | ||
| } | ||
|
|
||
| $installed = isset( $plugins[ $addon['plugin'] ] ); | ||
| $variant = self::get_views_variant( $addon ); | ||
| $matches = ! $variant || ! $installed || $variant === self::get_installed_views_variant( $plugins[ $addon['plugin'] ] ); | ||
|
|
||
| if ( $installed && $matches && is_plugin_active( $addon['plugin'] ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| $key = $addon['plugin'] . $variant; | ||
| $missing[ $key ] = self::get_dependency_action( $requirement, $addon, $installed, $matches ); | ||
| } | ||
|
|
||
| return array_values( $missing ); | ||
| } | ||
|
|
||
| /** | ||
| * Get the add-ons a template declares, without the gateways that ship in Lite. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param array $template The API template record. | ||
| * | ||
| * @return string[] Add-on identifiers or legacy display names. | ||
| */ | ||
| public static function get_required_addons( $template ) { | ||
| $required = array(); | ||
|
|
||
| foreach ( (array) ( $template['used_addons'] ?? array() ) as $name ) { | ||
| // Basic payment gateways ship in Lite and do not need separate add-ons. | ||
| if ( ! is_string( $name ) || '' === trim( $name ) || in_array( $name, array( 'Stripe', 'Square', 'PayPal Commerce' ), true ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| $required[] = $name; | ||
| } | ||
|
|
||
| return array_values( array_unique( $required ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve API IDs, slugs, plugin basenames, and legacy display names. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param string $requirement The required add-on identifier. | ||
| * @param array[] $addons The add-on API catalog records that have a plugin file. | ||
| * | ||
| * @return array|false The matching add-on record. | ||
| */ | ||
| private static function find_addon( $requirement, $addons ) { | ||
| $slug = sanitize_title( $requirement ); | ||
|
|
||
| foreach ( $addons as $id => $addon ) { | ||
| $directory = dirname( $addon['plugin'] ); | ||
| $identifiers = array( | ||
| (string) $id, | ||
| $addon['plugin'], | ||
| $directory, | ||
| preg_replace( '/^formidable-/', '', $directory ), | ||
| $addon['slug'] ?? '', | ||
| $addon['title'] ?? '', | ||
| $addon['display_name'] ?? '', | ||
| ); | ||
|
|
||
| foreach ( $identifiers as $identifier ) { | ||
| if ( ! is_string( $identifier ) || '' === $identifier ) { | ||
| continue; | ||
| } | ||
|
|
||
| if ( $slug === sanitize_title( $identifier ) ) { | ||
| return $addon; | ||
| } | ||
| } | ||
| }//end foreach | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Identify the catalog product for the basename shared by both Views add-ons. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param array $addon The catalog record. | ||
| * | ||
| * @return string The Views product slug or an empty string. | ||
| */ | ||
| private static function get_views_variant( $addon ) { | ||
| $slug = $addon['slug'] ?? ''; | ||
| return in_array( $slug, array( 'views', 'visual-views' ), true ) ? $slug : ''; | ||
| } | ||
|
|
||
| /** | ||
| * Identify the Views product by the installed plugin header, even when it is inactive. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param array $plugin The installed plugin header data. | ||
| * | ||
| * @return string The Views product slug. | ||
| */ | ||
| private static function get_installed_views_variant( $plugin ) { | ||
| return FrmAddonsController::get_views_slug_for_version( $plugin['Version'] ); | ||
| } | ||
|
|
||
| /** | ||
| * Describe an available activation, installation, or manual resolution. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param string $requirement The required add-on identifier. | ||
| * @param array $addon The matching catalog record. | ||
| * @param bool $installed Whether the catalog plugin is installed. | ||
| * @param bool $matches Whether the installed plugin is the required variant. | ||
| * | ||
| * @return array The dependency label and action. | ||
| */ | ||
| private static function get_dependency_action( $requirement, $addon, $installed, $matches ) { | ||
| $result = array( | ||
| 'name' => $addon['title'] ?? $requirement, | ||
| 'description' => $addon['excerpt'] ?? '', | ||
| 'action' => '', | ||
| 'plugin' => '', | ||
| 'url' => admin_url( 'admin.php?page=formidable-addons' ), | ||
| 'message' => __( 'Install and activate this add-on before importing the template.', 'formidable' ), | ||
| ); | ||
|
|
||
| if ( ! current_user_can( 'activate_plugins' ) ) { | ||
| $result['url'] = ''; | ||
| $result['message'] = __( 'Ask an administrator to activate this add-on before importing the template.', 'formidable' ); | ||
| return $result; | ||
| } | ||
|
|
||
| if ( ! $matches ) { | ||
| $result['message'] = __( 'A different version of Views is installed. Manage Views on the Add-Ons page, then try again.', 'formidable' ); | ||
| } elseif ( $installed && current_user_can( 'activate_plugin', $addon['plugin'] ) ) { | ||
| $result['action'] = 'frm_activate_addon'; | ||
| $result['plugin'] = $addon['plugin']; | ||
| } elseif ( ! $installed && ! empty( $addon['url'] ) && current_user_can( 'install_plugins' ) ) { | ||
| $result['action'] = 'frm_install_addon'; | ||
| $result['plugin'] = $addon['url']; | ||
| } else { | ||
| $result['message'] = __( 'This add-on is not available to install. Check your license or install it manually, then try again.', 'formidable' ); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
| } | ||
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A function with high cyclomatic complexity can be hard to understand and
maintain. Cyclomatic complexity is a software metric that measures the number of
independent paths through a function. A higher cyclomatic complexity indicates
that the function has more decision points and is more complex.