| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
class FrmFormMigratorsHelper { |
| 7 |
|
| 8 |
/** |
| 9 |
* @param array $form |
| 10 |
* @param array|null $dismissed |
| 11 |
* |
| 12 |
* @return bool |
| 13 |
*/ |
| 14 |
private static function is_dismissed( $form, $dismissed = null ) { |
| 15 |
if ( $dismissed === null ) { |
| 16 |
$dismissed = get_option( 'frm_dismissed' ); |
| 17 |
} |
| 18 |
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict |
| 19 |
return $dismissed && in_array( $form['class'], $dismissed ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public static function maybe_show_download_link() { |
| 26 |
$forms = self::import_links(); |
| 27 |
|
| 28 |
foreach ( $forms as $form ) { |
| 29 |
if ( ! self::is_dismissed( $form ) ) { |
| 30 |
self::install_banner( $form ); |
| 31 |
continue; |
| 32 |
} |
| 33 |
|
| 34 |
echo '<span>'; |
| 35 |
self::install_button( $form, 'auto' ); |
| 36 |
echo '</span>'; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @since 4.05 |
| 42 |
* |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public static function maybe_add_to_inbox() { |
| 46 |
$forms = self::import_links(); |
| 47 |
|
| 48 |
if ( ! $forms ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
$inbox = new FrmInbox(); |
| 53 |
|
| 54 |
foreach ( $forms as $form ) { |
| 55 |
$inbox->add_message( |
| 56 |
array( |
| 57 |
'key' => $form['class'], |
| 58 |
'subject' => 'You have new importable forms', |
| 59 |
'message' => 'Did you know you can import your forms created in ' . esc_html( $form['name'] ) . '?', |
| 60 |
'cta' => '<a href="' . esc_url( admin_url( 'admin.php?page=formidable-import' ) ) . '" class="button-primary frm-button-primary">' . esc_html__( 'Learn More', 'formidable' ) . '</a>', // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong |
| 61 |
'icon' => 'frm_cloud_upload_solid_icon', |
| 62 |
'type' => 'news', |
| 63 |
) |
| 64 |
); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @return array |
| 70 |
*/ |
| 71 |
private static function import_links() { |
| 72 |
if ( ! current_user_can( 'activate_plugins' ) ) { |
| 73 |
return array(); |
| 74 |
} |
| 75 |
|
| 76 |
$forms = array(); |
| 77 |
|
| 78 |
foreach ( self::importable_forms() as $form ) { |
| 79 |
if ( class_exists( $form['class'] ) || ! is_plugin_active( $form['plugin'] ) ) { |
| 80 |
// Either the importer is installed or the source plugin isn't. |
| 81 |
continue; |
| 82 |
} |
| 83 |
|
| 84 |
$installer = new FrmInstallPlugin( array( 'plugin_file' => $form['importer'] ) ); |
| 85 |
$form['installed'] = $installer->is_installed(); |
| 86 |
$form['link'] = $installer->get_activate_link(); |
| 87 |
|
| 88 |
$forms[] = $form; |
| 89 |
} |
| 90 |
|
| 91 |
return $forms; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @return string[][] |
| 96 |
*/ |
| 97 |
private static function importable_forms() { |
| 98 |
return array( |
| 99 |
'gf' => array( |
| 100 |
'class' => 'FrmGravityImporter', |
| 101 |
'plugin' => 'gravityforms/gravityforms.php', |
| 102 |
'importer' => 'formidable-gravity-forms-importer/formidable-gravity-forms-importer.php', |
| 103 |
'name' => 'Gravity Forms', |
| 104 |
'package' => 'https://downloads.wordpress.org/plugin/formidable-gravity-forms-importer.zip', |
| 105 |
), |
| 106 |
'pf' => array( |
| 107 |
'class' => 'FrmPirateImporter', |
| 108 |
'plugin' => 'pirate-forms/pirate-forms.php', |
| 109 |
'importer' => 'formidable-import-pirate-forms/pf-to-frm.php', |
| 110 |
'name' => 'Pirate Forms', |
| 111 |
'package' => 'https://downloads.wordpress.org/plugin/formidable-import-pirate-forms.zip', |
| 112 |
), |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* @param array $install |
| 118 |
* |
| 119 |
* @return string|null |
| 120 |
*/ |
| 121 |
private static function install_banner( $install ) { |
| 122 |
if ( empty( $install['link'] ) ) { |
| 123 |
return ''; |
| 124 |
} |
| 125 |
|
| 126 |
// phpcs:disable Generic.WhiteSpace.ScopeIndent |
| 127 |
?> |
| 128 |
<div class="frm-feature-banner"> |
| 129 |
<a href="#" class="dismiss alignright" id="<?php echo esc_attr( $install['class'] ); ?>" title="<?php esc_attr_e( 'Dismiss this message', 'formidable' ); ?>"> |
| 130 |
<?php FrmAppHelper::icon_by_class( 'frmfont frm_close_icon', array( 'aria-label' => 'Dismiss' ) ); ?> |
| 131 |
</a> |
| 132 |
<div class="frm-big-icon"> |
| 133 |
<?php FrmAppHelper::icon_by_class( 'frmfont frm_cloud_upload_solid_icon', array( 'aria-label' => 'Import' ) ); ?> |
| 134 |
</div> |
| 135 |
<p>Did you know you can import your forms created in <?php echo esc_html( $install['name'] ); ?>?</p> |
| 136 |
<?php self::install_button( $install ); ?> |
| 137 |
</div> |
| 138 |
<?php |
| 139 |
// phpcs:enable Generic.WhiteSpace.ScopeIndent |
| 140 |
return null; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* @param array $install |
| 145 |
* @param string $label |
| 146 |
* |
| 147 |
* @return void |
| 148 |
*/ |
| 149 |
private static function install_button( $install, $label = '' ) { |
| 150 |
$primary = 'button-secondary frm-button-secondary '; |
| 151 |
|
| 152 |
if ( ! $label ) { |
| 153 |
$label = __( 'Get Started', 'formidable' ); |
| 154 |
$primary = 'button-primary frm-button-primary '; |
| 155 |
} |
| 156 |
|
| 157 |
// phpcs:disable Generic.WhiteSpace.ScopeIndent |
| 158 |
if ( $install['installed'] ) { |
| 159 |
?> |
| 160 |
<a rel="<?php echo esc_attr( $install['importer'] ); ?>" class="button frm-activate-addon <?php echo esc_attr( $primary . ( ! empty( $install['link'] ) ? '' : 'frm_hidden' ) ); ?>"><?php // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong ?> |
| 161 |
<?php |
| 162 |
if ( $label === 'auto' ) { |
| 163 |
/* translators: %s: Name of the plugin */ |
| 164 |
$label = sprintf( __( 'Activate %s', 'formidable' ), $install['name'] ); |
| 165 |
} |
| 166 |
} else { |
| 167 |
?> |
| 168 |
<a rel="<?php echo esc_attr( $install['package'] ); ?>" class="frm-install-addon button <?php echo esc_attr( $primary ); ?>" aria-label="<?php esc_attr_e( 'Install', 'formidable' ); ?>"><?php // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong ?> |
| 169 |
<?php |
| 170 |
if ( $label === 'auto' ) { |
| 171 |
/* translators: %s: Name of the plugin */ |
| 172 |
$label = sprintf( __( 'Install %s Importer', 'formidable' ), $install['name'] ); |
| 173 |
} |
| 174 |
} |
| 175 |
?> |
| 176 |
<?php echo esc_html( $label ); ?> |
| 177 |
</a> |
| 178 |
<?php |
| 179 |
// phpcs:enable Generic.WhiteSpace.ScopeIndent |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* @return void |
| 184 |
*/ |
| 185 |
public static function dismiss_migrator() { |
| 186 |
check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 187 |
$dismissed = get_option( 'frm_dismissed' ); |
| 188 |
|
| 189 |
if ( ! $dismissed ) { |
| 190 |
$dismissed = array(); |
| 191 |
} |
| 192 |
|
| 193 |
$dismissed[] = FrmAppHelper::get_param( 'plugin', '', 'post', 'sanitize_text_field' ); |
| 194 |
update_option( 'frm_dismissed', array_filter( $dismissed ), false ); |
| 195 |
wp_die(); |
| 196 |
} |
| 197 |
} |
| 198 |
|